From e045ca6898e062d69d2f42601b0b1e5e4a3dc544 Mon Sep 17 00:00:00 2001 From: "sgjesse@chromium.org" Date: Thu, 10 Jun 2010 09:02:16 +0000 Subject: [PATCH] Better handling of stepping out of a function With the change in r4820 all return statements are now breakable like any statement, so stepping will stop before the return statement actually returning from a function. With this change the position when breaking in the function return (after executing the return statement) will be the actual end of the function. At this point the return value is available as it saved to the stack by the debug break at return handling. Added information on the actual value returned from the function to the debugger. Review URL: http://codereview.chromium.org/2783002 git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@4835 ce2b1a6d-e550-0410-aec6-3dcde31c8c00 --- src/arm/full-codegen-arm.cc | 6 +- src/debug.cc | 34 ++++++++ src/debug.h | 3 + src/full-codegen.cc | 2 +- src/full-codegen.h | 2 +- src/ia32/full-codegen-ia32.cc | 6 +- src/mips/full-codegen-mips.cc | 2 +- src/mirror-debugger.js | 50 +++++++++++- src/runtime.cc | 55 +++++++++++-- src/x64/full-codegen-x64.cc | 6 +- test/mjsunit/debug-return-value.js | 163 +++++++++++++++++++++++++++++++++++++ 11 files changed, 307 insertions(+), 22 deletions(-) create mode 100644 test/mjsunit/debug-return-value.js diff --git a/src/arm/full-codegen-arm.cc b/src/arm/full-codegen-arm.cc index e2cd84d..e619663 100644 --- a/src/arm/full-codegen-arm.cc +++ b/src/arm/full-codegen-arm.cc @@ -196,11 +196,11 @@ void FullCodeGenerator::Generate(CompilationInfo* info, Mode mode) { // body. __ LoadRoot(r0, Heap::kUndefinedValueRootIndex); } - EmitReturnSequence(function()->end_position()); + EmitReturnSequence(); } -void FullCodeGenerator::EmitReturnSequence(int position) { +void FullCodeGenerator::EmitReturnSequence() { Comment cmnt(masm_, "[ Return sequence"); if (return_label_.is_bound()) { __ b(&return_label_); @@ -224,7 +224,7 @@ void FullCodeGenerator::EmitReturnSequence(int position) { // Here we use masm_-> instead of the __ macro to avoid the code coverage // tool from instrumenting as we rely on the code size here. int32_t sp_delta = (scope()->num_parameters() + 1) * kPointerSize; - CodeGenerator::RecordPositions(masm_, position); + CodeGenerator::RecordPositions(masm_, function()->end_position()); __ RecordJSReturn(); masm_->mov(sp, fp); masm_->ldm(ia_w, sp, fp.bit() | lr.bit()); diff --git a/src/debug.cc b/src/debug.cc index ca20a65..98e366c 100644 --- a/src/debug.cc +++ b/src/debug.cc @@ -1713,6 +1713,40 @@ void Debug::SetAfterBreakTarget(JavaScriptFrame* frame) { } +bool Debug::IsBreakAtReturn(JavaScriptFrame* frame) { + HandleScope scope; + + // Get the executing function in which the debug break occurred. + Handle shared = + Handle(JSFunction::cast(frame->function())->shared()); + if (!EnsureDebugInfo(shared)) { + // Return if we failed to retrieve the debug info. + return false; + } + Handle debug_info = GetDebugInfo(shared); + Handle code(debug_info->code()); +#ifdef DEBUG + // Get the code which is actually executing. + Handle frame_code(frame->code()); + ASSERT(frame_code.is_identical_to(code)); +#endif + + // Find the call address in the running code. + Address addr = frame->pc() - Assembler::kCallTargetAddressOffset; + + // Check if the location is at JS return. + RelocIterator it(debug_info->code()); + while (!it.done()) { + if (RelocInfo::IsJSReturn(it.rinfo()->rmode())) { + return (it.rinfo()->pc() == + addr - Assembler::kPatchReturnSequenceAddressOffset); + } + it.next(); + } + return false; +} + + void Debug::FramesHaveBeenDropped(StackFrame::Id new_break_frame_id) { thread_local_.frames_are_dropped_ = true; thread_local_.break_frame_id_ = new_break_frame_id; diff --git a/src/debug.h b/src/debug.h index 0e12c76..1c67471 100644 --- a/src/debug.h +++ b/src/debug.h @@ -270,6 +270,9 @@ class Debug { // Check whether a global object is the debug global object. static bool IsDebugGlobal(GlobalObject* global); + // Check whether this frame is just about to return. + static bool IsBreakAtReturn(JavaScriptFrame* frame); + // Fast check to see if any break points are active. inline static bool has_break_points() { return has_break_points_; } diff --git a/src/full-codegen.cc b/src/full-codegen.cc index 6316158..b64a179 100644 --- a/src/full-codegen.cc +++ b/src/full-codegen.cc @@ -1077,7 +1077,7 @@ void FullCodeGenerator::VisitReturnStatement(ReturnStatement* stmt) { } __ Drop(stack_depth); - EmitReturnSequence(stmt->statement_pos()); + EmitReturnSequence(); } diff --git a/src/full-codegen.h b/src/full-codegen.h index 0ce600b..3d56232 100644 --- a/src/full-codegen.h +++ b/src/full-codegen.h @@ -389,7 +389,7 @@ class FullCodeGenerator: public AstVisitor { FunctionLiteral* function); // Platform-specific return sequence - void EmitReturnSequence(int position); + void EmitReturnSequence(); // Platform-specific code sequences for calls void EmitCallWithStub(Call* expr); diff --git a/src/ia32/full-codegen-ia32.cc b/src/ia32/full-codegen-ia32.cc index 28b766a..c750444 100644 --- a/src/ia32/full-codegen-ia32.cc +++ b/src/ia32/full-codegen-ia32.cc @@ -186,12 +186,12 @@ void FullCodeGenerator::Generate(CompilationInfo* info, Mode mode) { { Comment cmnt(masm_, "[ return ;"); // Emit a 'return undefined' in case control fell off the end of the body. __ mov(eax, Factory::undefined_value()); - EmitReturnSequence(function()->end_position()); + EmitReturnSequence(); } } -void FullCodeGenerator::EmitReturnSequence(int position) { +void FullCodeGenerator::EmitReturnSequence() { Comment cmnt(masm_, "[ Return sequence"); if (return_label_.is_bound()) { __ jmp(&return_label_); @@ -207,7 +207,7 @@ void FullCodeGenerator::EmitReturnSequence(int position) { Label check_exit_codesize; masm_->bind(&check_exit_codesize); #endif - CodeGenerator::RecordPositions(masm_, position); + CodeGenerator::RecordPositions(masm_, function()->end_position()); __ RecordJSReturn(); // Do not use the leave instruction here because it is too short to // patch with the code required by the debugger. diff --git a/src/mips/full-codegen-mips.cc b/src/mips/full-codegen-mips.cc index afda2cb..17ee531 100644 --- a/src/mips/full-codegen-mips.cc +++ b/src/mips/full-codegen-mips.cc @@ -45,7 +45,7 @@ void FullCodeGenerator::Generate(CompilationInfo* info, Mode mode) { } -void FullCodeGenerator::EmitReturnSequence(int position) { +void FullCodeGenerator::EmitReturnSequence() { UNIMPLEMENTED_MIPS(); } diff --git a/src/mirror-debugger.js b/src/mirror-debugger.js index 29d0069..761b9b3 100644 --- a/src/mirror-debugger.js +++ b/src/mirror-debugger.js @@ -1240,8 +1240,9 @@ const kFrameDetailsArgumentCountIndex = 3; const kFrameDetailsLocalCountIndex = 4; const kFrameDetailsSourcePositionIndex = 5; const kFrameDetailsConstructCallIndex = 6; -const kFrameDetailsDebuggerFrameIndex = 7; -const kFrameDetailsFirstDynamicIndex = 8; +const kFrameDetailsAtReturnIndex = 7; +const kFrameDetailsDebuggerFrameIndex = 8; +const kFrameDetailsFirstDynamicIndex = 9; const kFrameDetailsNameIndex = 0; const kFrameDetailsValueIndex = 1; @@ -1258,8 +1259,11 @@ const kFrameDetailsNameValueSize = 2; * 4: Local count * 5: Source position * 6: Construct call + * 7: Is at return + * 8: Debugger frame * Arguments name, value * Locals name, value + * Return value if any * @param {number} break_id Current break id * @param {number} index Frame number * @constructor @@ -1294,6 +1298,12 @@ FrameDetails.prototype.isConstructCall = function() { } +FrameDetails.prototype.isAtReturn = function() { + %CheckExecutionState(this.break_id_); + return this.details_[kFrameDetailsAtReturnIndex]; +} + + FrameDetails.prototype.isDebuggerFrame = function() { %CheckExecutionState(this.break_id_); return this.details_[kFrameDetailsDebuggerFrameIndex]; @@ -1341,7 +1351,8 @@ FrameDetails.prototype.sourcePosition = function() { FrameDetails.prototype.localName = function(index) { %CheckExecutionState(this.break_id_); if (index >= 0 && index < this.localCount()) { - var locals_offset = kFrameDetailsFirstDynamicIndex + this.argumentCount() * kFrameDetailsNameValueSize + var locals_offset = kFrameDetailsFirstDynamicIndex + + this.argumentCount() * kFrameDetailsNameValueSize return this.details_[locals_offset + index * kFrameDetailsNameValueSize + kFrameDetailsNameIndex] @@ -1352,7 +1363,8 @@ FrameDetails.prototype.localName = function(index) { FrameDetails.prototype.localValue = function(index) { %CheckExecutionState(this.break_id_); if (index >= 0 && index < this.localCount()) { - var locals_offset = kFrameDetailsFirstDynamicIndex + this.argumentCount() * kFrameDetailsNameValueSize + var locals_offset = kFrameDetailsFirstDynamicIndex + + this.argumentCount() * kFrameDetailsNameValueSize return this.details_[locals_offset + index * kFrameDetailsNameValueSize + kFrameDetailsValueIndex] @@ -1360,6 +1372,17 @@ FrameDetails.prototype.localValue = function(index) { } +FrameDetails.prototype.returnValue = function() { + %CheckExecutionState(this.break_id_); + var return_value_offset = + kFrameDetailsFirstDynamicIndex + + (this.argumentCount() + this.localCount()) * kFrameDetailsNameValueSize; + if (this.details_[kFrameDetailsAtReturnIndex]) { + return this.details_[return_value_offset]; + } +} + + FrameDetails.prototype.scopeCount = function() { return %GetScopeCount(this.break_id_, this.frameId()); } @@ -1412,6 +1435,11 @@ FrameMirror.prototype.isConstructCall = function() { }; +FrameMirror.prototype.isAtReturn = function() { + return this.details_.isAtReturn(); +}; + + FrameMirror.prototype.isDebuggerFrame = function() { return this.details_.isDebuggerFrame(); }; @@ -1447,6 +1475,11 @@ FrameMirror.prototype.localValue = function(index) { }; +FrameMirror.prototype.returnValue = function() { + return MakeMirror(this.details_.returnValue()); +}; + + FrameMirror.prototype.sourcePosition = function() { return this.details_.sourcePosition(); }; @@ -1574,6 +1607,11 @@ FrameMirror.prototype.invocationText = function() { result += ')'; } + if (this.isAtReturn()) { + result += ' returning '; + result += this.returnValue().toText(); + } + return result; } @@ -2267,6 +2305,10 @@ JSONProtocolSerializer.prototype.serializeFrame_ = function(mirror, content) { content.script = this.serializeReference(func.script()); } content.constructCall = mirror.isConstructCall(); + content.atReturn = mirror.isAtReturn(); + if (mirror.isAtReturn()) { + content.returnValue = this.serializeReference(mirror.returnValue()); + } content.debuggerFrame = mirror.isDebuggerFrame(); var x = new Array(mirror.argumentCount()); for (var i = 0; i < mirror.argumentCount(); i++) { diff --git a/src/runtime.cc b/src/runtime.cc index 4c27171..88786e8 100644 --- a/src/runtime.cc +++ b/src/runtime.cc @@ -8243,8 +8243,9 @@ static const int kFrameDetailsArgumentCountIndex = 3; static const int kFrameDetailsLocalCountIndex = 4; static const int kFrameDetailsSourcePositionIndex = 5; static const int kFrameDetailsConstructCallIndex = 6; -static const int kFrameDetailsDebuggerFrameIndex = 7; -static const int kFrameDetailsFirstDynamicIndex = 8; +static const int kFrameDetailsAtReturnIndex = 7; +static const int kFrameDetailsDebuggerFrameIndex = 8; +static const int kFrameDetailsFirstDynamicIndex = 9; // Return an array with frame details // args[0]: number: break id @@ -8258,9 +8259,11 @@ static const int kFrameDetailsFirstDynamicIndex = 8; // 4: Local count // 5: Source position // 6: Constructor call -// 7: Debugger frame +// 7: Is at return +// 8: Debugger frame // Arguments name, value // Locals name, value +// Return value if any static Object* Runtime_GetFrameDetails(Arguments args) { HandleScope scope; ASSERT(args.length() == 2); @@ -8336,8 +8339,39 @@ static Object* Runtime_GetFrameDetails(Arguments args) { } } - // Now advance to the arguments adapter frame (if any). If contains all - // the provided parameters and + // Check whether this frame is positioned at return. + int at_return = (index == 0) ? Debug::IsBreakAtReturn(it.frame()) : false; + + // If positioned just before return find the value to be returned and add it + // to the frame information. + Handle return_value = Factory::undefined_value(); + if (at_return) { + StackFrameIterator it2; + Address internal_frame_sp = NULL; + while (!it2.done()) { + if (it2.frame()->is_internal()) { + internal_frame_sp = it2.frame()->sp(); + } else { + if (it2.frame()->is_java_script()) { + if (it2.frame()->id() == it.frame()->id()) { + // The internal frame just before the JavaScript frame contains the + // value to return on top. A debug break at return will create an + // internal frame to store the return value (eax/rax/r0) before + // entering the debug break exit frame. + if (internal_frame_sp != NULL) { + return_value = + Handle(Memory::Object_at(internal_frame_sp)); + break; + } + } + } + + // Indicate that the previous frame was not an internal frame. + internal_frame_sp = NULL; + } + it2.Advance(); + } + } // Now advance to the arguments adapter frame (if any). It contains all // the provided parameters whereas the function frame always have the number @@ -8354,7 +8388,8 @@ static Object* Runtime_GetFrameDetails(Arguments args) { // Calculate the size of the result. int details_size = kFrameDetailsFirstDynamicIndex + - 2 * (argument_count + info.NumberOfLocals()); + 2 * (argument_count + info.NumberOfLocals()) + + (at_return ? 1 : 0); Handle details = Factory::NewFixedArray(details_size); // Add the frame id. @@ -8380,6 +8415,9 @@ static Object* Runtime_GetFrameDetails(Arguments args) { // Add the constructor information. details->set(kFrameDetailsConstructCallIndex, Heap::ToBoolean(constructor)); + // Add the at return information. + details->set(kFrameDetailsAtReturnIndex, Heap::ToBoolean(at_return)); + // Add information on whether this frame is invoked in the debugger context. details->set(kFrameDetailsDebuggerFrameIndex, Heap::ToBoolean(*save->context() == *Debug::debug_context())); @@ -8409,6 +8447,11 @@ static Object* Runtime_GetFrameDetails(Arguments args) { details->set(details_index++, locals->get(i)); } + // Add the value being returned. + if (at_return) { + details->set(details_index++, *return_value); + } + // Add the receiver (same as in function frame). // THIS MUST BE DONE LAST SINCE WE MIGHT ADVANCE // THE FRAME ITERATOR TO WRAP THE RECEIVER. diff --git a/src/x64/full-codegen-x64.cc b/src/x64/full-codegen-x64.cc index 1f29de7..d99ea84 100644 --- a/src/x64/full-codegen-x64.cc +++ b/src/x64/full-codegen-x64.cc @@ -187,12 +187,12 @@ void FullCodeGenerator::Generate(CompilationInfo* info, Mode mode) { { Comment cmnt(masm_, "[ return ;"); // Emit a 'return undefined' in case control fell off the end of the body. __ LoadRoot(rax, Heap::kUndefinedValueRootIndex); - EmitReturnSequence(function()->end_position()); + EmitReturnSequence(); } } -void FullCodeGenerator::EmitReturnSequence(int position) { +void FullCodeGenerator::EmitReturnSequence() { Comment cmnt(masm_, "[ Return sequence"); if (return_label_.is_bound()) { __ jmp(&return_label_); @@ -207,7 +207,7 @@ void FullCodeGenerator::EmitReturnSequence(int position) { Label check_exit_codesize; masm_->bind(&check_exit_codesize); #endif - CodeGenerator::RecordPositions(masm_, position); + CodeGenerator::RecordPositions(masm_, function()->end_position()); __ RecordJSReturn(); // Do not use the leave instruction here because it is too short to // patch with the code required by the debugger. diff --git a/test/mjsunit/debug-return-value.js b/test/mjsunit/debug-return-value.js new file mode 100644 index 0000000..a9ac520 --- /dev/null +++ b/test/mjsunit/debug-return-value.js @@ -0,0 +1,163 @@ +// Copyright 2010 the V8 project authors. All rights reserved. +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following +// disclaimer in the documentation and/or other materials provided +// with the distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived +// from this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +// Flags: --expose-debug-as debug +// Get the Debug object exposed from the debug context global object. +Debug = debug.Debug + + +function ParsedResponse(json) { + this.response_ = eval('(' + json + ')'); + this.refs_ = []; + if (this.response_.refs) { + for (var i = 0; i < this.response_.refs.length; i++) { + this.refs_[this.response_.refs[i].handle] = this.response_.refs[i]; + } + } +} + + +ParsedResponse.prototype.response = function() { + return this.response_; +} + + +ParsedResponse.prototype.body = function() { + return this.response_.body; +} + + +ParsedResponse.prototype.running = function() { + return this.response_.running; +} + + +ParsedResponse.prototype.lookup = function(handle) { + return this.refs_[handle]; +} + + +listener_complete = false; +exception = false; +break_count = 0; +expected_return_value = 0; +debugger_source_position = 0; + +// Listener which expects to do four steps to reach returning from the function. +function listener(event, exec_state, event_data, data) { + try { + if (event == Debug.DebugEvent.Break) + { + break_count++; + if (break_count < 4) { + assertFalse(exec_state.frame(0).isAtReturn()) + switch (break_count) { + case 1: + // Collect the position of the debugger statement. + debugger_source_position = exec_state.frame(0).sourcePosition(); + break; + case 2: + // Position now at the if statement. + assertEquals(debugger_source_position + 10, + exec_state.frame(0).sourcePosition()); + break; + case 3: + // Position now at either of the returns. + if (expected_return_value == 1) { + assertEquals(debugger_source_position + 19, + exec_state.frame(0).sourcePosition()); + } else { + assertEquals(debugger_source_position + 38, + exec_state.frame(0).sourcePosition()); + } + break; + default: + fail("Unexpected"); + } + exec_state.prepareStep(Debug.StepAction.StepIn, 1); + } else { + // Position at the end of the function. + assertEquals(debugger_source_position + 51, + exec_state.frame(0).sourcePosition()); + + // Just about to return from the function. + assertTrue(exec_state.frame(0).isAtReturn()) + assertEquals(expected_return_value, + exec_state.frame(0).returnValue().value()); + + // Check the same using the JSON commands. + var dcp = exec_state.debugCommandProcessor(false); + var request = '{"seq":0,"type":"request","command":"backtrace"}'; + var resp = dcp.processDebugJSONRequest(request); + response = new ParsedResponse(resp); + frames = response.body().frames; + assertTrue(frames[0].atReturn); + assertEquals(expected_return_value, + response.lookup(frames[0].returnValue.ref).value); + + listener_complete = true; + } + } + } catch (e) { + exception = e + }; +}; + +// Add the debug event listener. +Debug.setListener(listener); + +// Four steps from the debugger statement in this function will position us at +// the function return. +// 0 1 2 3 4 5 +// 0123456789012345678901234567890123456789012345678901 + +function f(x) {debugger; if (x) { return 1; } else { return 2; } }; + +// Call f expecting different return values. +break_count = 0; +expected_return_value = 2; +listener_complete = false; +f(); +assertFalse(exception, "exception in listener") +assertTrue(listener_complete); +assertEquals(4, break_count); + +break_count = 0; +expected_return_value = 1; +listener_complete = false; +f(true); +assertFalse(exception, "exception in listener") +assertTrue(listener_complete); +assertEquals(4, break_count); + +break_count = 0; +expected_return_value = 2; +listener_complete = false; +f(false); +assertFalse(exception, "exception in listener") +assertTrue(listener_complete); +assertEquals(4, break_count); -- 2.7.4