From 81df4a42e47e5ab4a1f6a0f177ee72ed8a6b6502 Mon Sep 17 00:00:00 2001 From: "sgjesse@chromium.org" Date: Tue, 13 Sep 2011 08:31:21 +0000 Subject: [PATCH] Support for precise stepping in functions compiled before debugging was started (step 1) This change will ensure that all non-optimized code will be compiled with debug break slots when debugging is initiated. This is handled by scanning the heap for non-optimized functions without debug break slots and setting their code to be lazy recomplied. When the lazy recompilation happens the code will ge generated with debug break slots (if debugging is still active at that point in time). R=svenpanne@chromium.org Currently this is only implemented for functions which do not have activations on the stack. BUG= TEST= Review URL: http://codereview.chromium.org//7839030 git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@9250 ce2b1a6d-e550-0410-aec6-3dcde31c8c00 --- src/debug.cc | 71 ++++++++++++++++++++++++---- src/debug.h | 2 + src/full-codegen.cc | 2 + src/list-inl.h | 29 ++++++++++++ src/list.h | 9 ++++ src/objects-inl.h | 22 ++++++++- src/objects.h | 15 ++++-- src/runtime.cc | 9 ++++ test/mjsunit/debug-step-2.js | 89 ++++++++++++++++++++++++++++++++++++ 9 files changed, 235 insertions(+), 13 deletions(-) create mode 100644 test/mjsunit/debug-step-2.js diff --git a/src/debug.cc b/src/debug.cc index 2d58ce1f54..0b48293419 100644 --- a/src/debug.cc +++ b/src/debug.cc @@ -40,6 +40,7 @@ #include "global-handles.h" #include "ic.h" #include "ic-inl.h" +#include "list.h" #include "messages.h" #include "natives.h" #include "stub-cache.h" @@ -1105,6 +1106,8 @@ void Debug::SetBreakPoint(Handle shared, int* source_position) { HandleScope scope(isolate_); + PrepareForBreakPoints(); + if (!EnsureDebugInfo(shared)) { // Return if retrieving debug info failed. return; @@ -1178,6 +1181,7 @@ void Debug::ClearAllBreakPoints() { void Debug::FloodWithOneShot(Handle shared) { + PrepareForBreakPoints(); // Make sure the function has setup the debug info. if (!EnsureDebugInfo(shared)) { // Return if we failed to retrieve the debug info. @@ -1234,6 +1238,9 @@ bool Debug::IsBreakOnException(ExceptionBreakType type) { void Debug::PrepareStep(StepAction step_action, int step_count) { HandleScope scope(isolate_); + + PrepareForBreakPoints(); + ASSERT(Debug::InDebugger()); // Remember this step action and count. @@ -1676,19 +1683,63 @@ void Debug::ClearStepNext() { } -// Ensures the debug information is present for shared. -bool Debug::EnsureDebugInfo(Handle shared) { - // Return if we already have the debug info for shared. - if (HasDebugInfo(shared)) return true; - - // Ensure shared in compiled. Return false if this failed. - if (!EnsureCompiled(shared, CLEAR_EXCEPTION)) return false; - +void Debug::PrepareForBreakPoints() { // If preparing for the first break point make sure to deoptimize all // functions as debugging does not work with optimized code. if (!has_break_points_) { Deoptimizer::DeoptimizeAll(); + + AssertNoAllocation no_allocation; + Builtins* builtins = isolate_->builtins(); + Code* lazy_compile = builtins->builtin(Builtins::kLazyCompile); + + // Find all non-optimized code functions with activation frames on + // the stack. + List active_functions(100); + for (JavaScriptFrameIterator it(isolate_); !it.done(); it.Advance()) { + JavaScriptFrame* frame = it.frame(); + if (frame->function()->IsJSFunction()) { + JSFunction* function = JSFunction::cast(frame->function()); + if (function->code()->kind() == Code::FUNCTION) + active_functions.Add(function); + } + } + active_functions.Sort(); + + // Scan the heap for all non-optimized functions which has no + // debug break slots. + HeapIterator iterator; + HeapObject* obj = NULL; + while (((obj = iterator.next()) != NULL)) { + if (obj->IsJSFunction()) { + JSFunction* function = JSFunction::cast(obj); + if (function->shared()->allows_lazy_compilation() && + function->shared()->script()->IsScript() && + function->code()->kind() == Code::FUNCTION && + !function->code()->has_debug_break_slots()) { + bool has_activation = + SortedListBSearch(active_functions, function) != -1; + if (!has_activation) { + function->set_code(lazy_compile); + function->shared()->set_code(lazy_compile); + } + } + } + } } +} + + +// Ensures the debug information is present for shared. +bool Debug::EnsureDebugInfo(Handle shared) { + // Return if we already have the debug info for shared. + if (HasDebugInfo(shared)) { + ASSERT(shared->is_compiled()); + return true; + } + + // Ensure shared in compiled. Return false if this failed. + if (!EnsureCompiled(shared, CLEAR_EXCEPTION)) return false; // Create the debug info object. Handle debug_info = FACTORY->NewDebugInfo(shared); @@ -1739,6 +1790,8 @@ void Debug::RemoveDebugInfo(Handle debug_info) { void Debug::SetAfterBreakTarget(JavaScriptFrame* frame) { HandleScope scope(isolate_); + PrepareForBreakPoints(); + // Get the executing function in which the debug break occurred. Handle shared = Handle(JSFunction::cast(frame->function())->shared()); @@ -1829,6 +1882,8 @@ bool Debug::IsBreakAtReturn(JavaScriptFrame* frame) { return false; } + PrepareForBreakPoints(); + // Get the executing function in which the debug break occurred. Handle shared = Handle(JSFunction::cast(frame->function())->shared()); diff --git a/src/debug.h b/src/debug.h index d39ea4e3e9..ff36f767d9 100644 --- a/src/debug.h +++ b/src/debug.h @@ -247,6 +247,8 @@ class Debug { static Handle GetDebugInfo(Handle shared); static bool HasDebugInfo(Handle shared); + void PrepareForBreakPoints(); + // Returns whether the operation succeeded. bool EnsureDebugInfo(Handle shared); diff --git a/src/full-codegen.cc b/src/full-codegen.cc index 53ace82fe7..e55a56e626 100644 --- a/src/full-codegen.cc +++ b/src/full-codegen.cc @@ -291,6 +291,8 @@ bool FullCodeGenerator::MakeCode(CompilationInfo* info) { code->set_optimizable(info->IsOptimizable()); cgen.PopulateDeoptimizationData(code); code->set_has_deoptimization_support(info->HasDeoptimizationSupport()); + code->set_has_debug_break_slots( + info->isolate()->debugger()->IsDebuggerActive()); code->set_allow_osr_at_loop_nesting_level(0); code->set_stack_check_table_offset(table_offset); CodeGenerator::PrintCode(code, info); diff --git a/src/list-inl.h b/src/list-inl.h index 8ef7514f4f..80bccc9bc3 100644 --- a/src/list-inl.h +++ b/src/list-inl.h @@ -207,6 +207,35 @@ void List::Initialize(int capacity) { } +template +int SortedListBSearch( + const List& list, T elem, int (*cmp)(const T* x, const T* y)) { + int low = 0; + int high = list.length() - 1; + while (low <= high) { + int mid = (low + high) / 2; + T mid_elem = list[mid]; + + if (mid_elem > elem) { + high = mid - 1; + continue; + } + if (mid_elem < elem) { + low = mid + 1; + continue; + } + // Found the elememt. + return mid; + } + return -1; +} + + +template +int SortedListBSearch(const List& list, T elem) { + return SortedListBSearch(list, elem, PointerValueCompare); +} + } } // namespace v8::internal #endif // V8_LIST_INL_H_ diff --git a/src/list.h b/src/list.h index 33267bd611..055870904e 100644 --- a/src/list.h +++ b/src/list.h @@ -168,6 +168,15 @@ class Code; typedef List MapList; typedef List CodeList; +// Perform binary search for an element in an already sorted +// list. Returns the index of the element of -1 if it was not found. +template +int SortedListBSearch( + const List& list, T elem, int (*cmp)(const T* x, const T* y)); +template +int SortedListBSearch(const List& list, T elem); + } } // namespace v8::internal + #endif // V8_LIST_H_ diff --git a/src/objects-inl.h b/src/objects-inl.h index 8a73561468..2987b2c8c4 100644 --- a/src/objects-inl.h +++ b/src/objects-inl.h @@ -2951,13 +2951,31 @@ void Code::set_optimizable(bool value) { bool Code::has_deoptimization_support() { ASSERT(kind() == FUNCTION); - return READ_BYTE_FIELD(this, kHasDeoptimizationSupportOffset) == 1; + byte flags = READ_BYTE_FIELD(this, kFullCodeFlags); + return FullCodeFlagsHasDeoptimizationSupportField::decode(flags); } void Code::set_has_deoptimization_support(bool value) { ASSERT(kind() == FUNCTION); - WRITE_BYTE_FIELD(this, kHasDeoptimizationSupportOffset, value ? 1 : 0); + byte flags = READ_BYTE_FIELD(this, kFullCodeFlags); + flags = FullCodeFlagsHasDeoptimizationSupportField::update(flags, value); + WRITE_BYTE_FIELD(this, kFullCodeFlags, flags); +} + + +bool Code::has_debug_break_slots() { + ASSERT(kind() == FUNCTION); + byte flags = READ_BYTE_FIELD(this, kFullCodeFlags); + return FullCodeFlagsHasDebugBreakSlotsField::decode(flags); +} + + +void Code::set_has_debug_break_slots(bool value) { + ASSERT(kind() == FUNCTION); + byte flags = READ_BYTE_FIELD(this, kFullCodeFlags); + flags = FullCodeFlagsHasDebugBreakSlotsField::update(flags, value); + WRITE_BYTE_FIELD(this, kFullCodeFlags, flags); } diff --git a/src/objects.h b/src/objects.h index 9dc077168b..1f93a4c098 100644 --- a/src/objects.h +++ b/src/objects.h @@ -3683,6 +3683,11 @@ class Code: public HeapObject { inline bool has_deoptimization_support(); inline void set_has_deoptimization_support(bool value); + // [has_debug_break_slots]: For FUNCTION kind, tells if it has + // been compiled with debug break slots. + inline bool has_debug_break_slots(); + inline void set_has_debug_break_slots(bool value); + // [allow_osr_at_loop_nesting_level]: For FUNCTION kind, tells for // how long the function has been marked for OSR and therefore which // level of loop nesting we are willing to do on-stack replacement @@ -3874,11 +3879,15 @@ class Code: public HeapObject { static const int kBinaryOpTypeOffset = kStubMajorKeyOffset + 1; static const int kCompareStateOffset = kStubMajorKeyOffset + 1; static const int kToBooleanTypeOffset = kStubMajorKeyOffset + 1; - static const int kHasDeoptimizationSupportOffset = kOptimizableOffset + 1; + + static const int kFullCodeFlags = kOptimizableOffset + 1; + class FullCodeFlagsHasDeoptimizationSupportField: + public BitField {}; // NOLINT + class FullCodeFlagsHasDebugBreakSlotsField: public BitField {}; static const int kBinaryOpReturnTypeOffset = kBinaryOpTypeOffset + 1; - static const int kAllowOSRAtLoopNestingLevelOffset = - kHasDeoptimizationSupportOffset + 1; + + static const int kAllowOSRAtLoopNestingLevelOffset = kFullCodeFlags + 1; static const int kSafepointTableOffsetOffset = kStackSlotsOffset + kIntSize; static const int kStackCheckTableOffsetOffset = kStackSlotsOffset + kIntSize; diff --git a/src/runtime.cc b/src/runtime.cc index db2a104c37..bf2a52fe18 100644 --- a/src/runtime.cc +++ b/src/runtime.cc @@ -8127,6 +8127,15 @@ RUNTIME_FUNCTION(MaybeObject*, Runtime_LazyRecompile) { HandleScope scope(isolate); ASSERT(args.length() == 1); Handle function = args.at(0); + + // If the function is not compiled ignore the lazy + // recompilation. This can happen if the debugger is activated and + // the function is returned to the not compiled state. + if (!function->shared()->is_compiled()) { + function->ReplaceCode(function->shared()->code()); + return function->code(); + } + // If the function is not optimizable or debugger is active continue using the // code from the full compiler. if (!function->shared()->code()->optimizable() || diff --git a/test/mjsunit/debug-step-2.js b/test/mjsunit/debug-step-2.js new file mode 100644 index 0000000000..502b426ee2 --- /dev/null +++ b/test/mjsunit/debug-step-2.js @@ -0,0 +1,89 @@ +// Copyright 2011 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 + +// This test tests that full code compiled without debug break slots +// is recompiled with debug break slots when debugging is started. + +// Get the Debug object exposed from the debug context global object. +Debug = debug.Debug + +var bp; +var done = false; +var step_count = 0; + +// Debug event listener which steps until the global variable done is true. +function listener(event, exec_state, event_data, data) { + if (event == Debug.DebugEvent.Break) { + if (!done) exec_state.prepareStep(Debug.StepAction.StepNext); + step_count++; + } +}; + +// Set the global variables state to prpare the stepping test. +function prepare_step_test() { + done = false; + step_count = 0; +} + +// Test function to step through. +function f() { + var i = 1; + var j = 2; + done = true; +}; + +prepare_step_test(); +f(); + +// Add the debug event listener. +Debug.setListener(listener); + +bp = Debug.setBreakPoint(f, 1); + +prepare_step_test(); +f(); +assertEquals(4, step_count); +Debug.clearBreakPoint(bp); + +// Set a breakpoint on the first var statement (line 1). +bp = Debug.setBreakPoint(f, 1); + +// Step through the function ensuring that the var statements are hit as well. +prepare_step_test(); +f(); +assertEquals(4, step_count); + +// Clear the breakpoint and check that no stepping happens. +Debug.clearBreakPoint(bp); +prepare_step_test(); +f(); +assertEquals(0, step_count); + +// Get rid of the debug event listener. +Debug.setListener(null);