1 // Copyright 2012 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided
11 // with the distribution.
12 // * Neither the name of Google Inc. nor the names of its
13 // contributors may be used to endorse or promote products derived
14 // from this software without specific prior written permission.
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #include "arguments.h"
32 #include "bootstrapper.h"
33 #include "code-stubs.h"
35 #include "compilation-cache.h"
38 #include "deoptimizer.h"
39 #include "execution.h"
40 #include "full-codegen.h"
41 #include "global-handles.h"
44 #include "isolate-inl.h"
48 #include "stub-cache.h"
51 #include "../include/v8-debug.h"
56 #ifdef ENABLE_DEBUGGER_SUPPORT
59 Debug::Debug(Isolate* isolate)
60 : has_break_points_(false),
62 debug_info_list_(NULL),
63 disable_break_(false),
64 break_on_exception_(false),
65 break_on_uncaught_exception_(false),
66 debug_break_return_(NULL),
67 debug_break_slot_(NULL),
69 memset(registers_, 0, sizeof(JSCallerSavedBuffer));
77 static void PrintLn(v8::Local<v8::Value> value) {
78 v8::Local<v8::String> s = value->ToString();
79 ScopedVector<char> data(s->Length() + 1);
80 if (data.start() == NULL) {
81 V8::FatalProcessOutOfMemory("PrintLn");
84 s->WriteAscii(data.start());
85 PrintF("%s\n", data.start());
89 static Handle<Code> ComputeCallDebugPrepareStepIn(int argc, Code::Kind kind) {
90 Isolate* isolate = Isolate::Current();
91 return isolate->stub_cache()->ComputeCallDebugPrepareStepIn(argc, kind);
95 static v8::Handle<v8::Context> GetDebugEventContext(Isolate* isolate) {
96 Handle<Context> context = isolate->debug()->debugger_entry()->GetContext();
97 // Isolate::context() may have been NULL when "script collected" event
99 if (context.is_null()) return v8::Local<v8::Context>();
100 Handle<Context> global_context(context->global_context());
101 return v8::Utils::ToLocal(global_context);
105 BreakLocationIterator::BreakLocationIterator(Handle<DebugInfo> debug_info,
106 BreakLocatorType type) {
107 debug_info_ = debug_info;
109 reloc_iterator_ = NULL;
110 reloc_iterator_original_ = NULL;
111 Reset(); // Initialize the rest of the member variables.
115 BreakLocationIterator::~BreakLocationIterator() {
116 ASSERT(reloc_iterator_ != NULL);
117 ASSERT(reloc_iterator_original_ != NULL);
118 delete reloc_iterator_;
119 delete reloc_iterator_original_;
123 void BreakLocationIterator::Next() {
124 AssertNoAllocation nogc;
125 ASSERT(!RinfoDone());
127 // Iterate through reloc info for code and original code stopping at each
128 // breakable code target.
129 bool first = break_point_ == -1;
130 while (!RinfoDone()) {
131 if (!first) RinfoNext();
133 if (RinfoDone()) return;
135 // Whenever a statement position or (plain) position is passed update the
136 // current value of these.
137 if (RelocInfo::IsPosition(rmode())) {
138 if (RelocInfo::IsStatementPosition(rmode())) {
139 statement_position_ = static_cast<int>(
140 rinfo()->data() - debug_info_->shared()->start_position());
142 // Always update the position as we don't want that to be before the
143 // statement position.
144 position_ = static_cast<int>(
145 rinfo()->data() - debug_info_->shared()->start_position());
146 ASSERT(position_ >= 0);
147 ASSERT(statement_position_ >= 0);
150 if (IsDebugBreakSlot()) {
151 // There is always a possible break point at a debug break slot.
154 } else if (RelocInfo::IsCodeTarget(rmode())) {
155 // Check for breakable code target. Look in the original code as setting
156 // break points can cause the code targets in the running (debugged) code
157 // to be of a different kind than in the original code.
158 Address target = original_rinfo()->target_address();
159 Code* code = Code::GetCodeFromTargetAddress(target);
160 if ((code->is_inline_cache_stub() &&
161 !code->is_binary_op_stub() &&
162 !code->is_unary_op_stub() &&
163 !code->is_compare_ic_stub() &&
164 !code->is_to_boolean_ic_stub()) ||
165 RelocInfo::IsConstructCall(rmode())) {
169 if (code->kind() == Code::STUB) {
170 if (IsDebuggerStatement()) {
174 if (type_ == ALL_BREAK_LOCATIONS) {
175 if (Debug::IsBreakStub(code)) {
180 ASSERT(type_ == SOURCE_BREAK_LOCATIONS);
181 if (Debug::IsSourceBreakStub(code)) {
189 // Check for break at return.
190 if (RelocInfo::IsJSReturn(rmode())) {
191 // Set the positions to the end of the function.
192 if (debug_info_->shared()->HasSourceCode()) {
193 position_ = debug_info_->shared()->end_position() -
194 debug_info_->shared()->start_position() - 1;
198 statement_position_ = position_;
206 void BreakLocationIterator::Next(int count) {
214 // Find the break point closest to the supplied address.
215 void BreakLocationIterator::FindBreakLocationFromAddress(Address pc) {
216 // Run through all break points to locate the one closest to the address.
217 int closest_break_point = 0;
218 int distance = kMaxInt;
220 // Check if this break point is closer that what was previously found.
221 if (this->pc() < pc && pc - this->pc() < distance) {
222 closest_break_point = break_point();
223 distance = static_cast<int>(pc - this->pc());
224 // Check whether we can't get any closer.
225 if (distance == 0) break;
230 // Move to the break point found.
232 Next(closest_break_point);
236 // Find the break point closest to the supplied source position.
237 void BreakLocationIterator::FindBreakLocationFromPosition(int position) {
238 // Run through all break points to locate the one closest to the source
240 int closest_break_point = 0;
241 int distance = kMaxInt;
243 // Check if this break point is closer that what was previously found.
244 if (position <= statement_position() &&
245 statement_position() - position < distance) {
246 closest_break_point = break_point();
247 distance = statement_position() - position;
248 // Check whether we can't get any closer.
249 if (distance == 0) break;
254 // Move to the break point found.
256 Next(closest_break_point);
260 void BreakLocationIterator::Reset() {
261 // Create relocation iterators for the two code objects.
262 if (reloc_iterator_ != NULL) delete reloc_iterator_;
263 if (reloc_iterator_original_ != NULL) delete reloc_iterator_original_;
264 reloc_iterator_ = new RelocIterator(debug_info_->code());
265 reloc_iterator_original_ = new RelocIterator(debug_info_->original_code());
267 // Position at the first break point.
270 statement_position_ = 1;
275 bool BreakLocationIterator::Done() const {
280 void BreakLocationIterator::SetBreakPoint(Handle<Object> break_point_object) {
281 // If there is not already a real break point here patch code with debug
283 if (!HasBreakPoint()) {
286 ASSERT(IsDebugBreak() || IsDebuggerStatement());
287 // Set the break point information.
288 DebugInfo::SetBreakPoint(debug_info_, code_position(),
289 position(), statement_position(),
294 void BreakLocationIterator::ClearBreakPoint(Handle<Object> break_point_object) {
295 // Clear the break point information.
296 DebugInfo::ClearBreakPoint(debug_info_, code_position(), break_point_object);
297 // If there are no more break points here remove the debug break.
298 if (!HasBreakPoint()) {
300 ASSERT(!IsDebugBreak());
305 void BreakLocationIterator::SetOneShot() {
306 // Debugger statement always calls debugger. No need to modify it.
307 if (IsDebuggerStatement()) {
311 // If there is a real break point here no more to do.
312 if (HasBreakPoint()) {
313 ASSERT(IsDebugBreak());
317 // Patch code with debug break.
322 void BreakLocationIterator::ClearOneShot() {
323 // Debugger statement always calls debugger. No need to modify it.
324 if (IsDebuggerStatement()) {
328 // If there is a real break point here no more to do.
329 if (HasBreakPoint()) {
330 ASSERT(IsDebugBreak());
334 // Patch code removing debug break.
336 ASSERT(!IsDebugBreak());
340 void BreakLocationIterator::SetDebugBreak() {
341 // Debugger statement always calls debugger. No need to modify it.
342 if (IsDebuggerStatement()) {
346 // If there is already a break point here just return. This might happen if
347 // the same code is flooded with break points twice. Flooding the same
348 // function twice might happen when stepping in a function with an exception
349 // handler as the handler and the function is the same.
350 if (IsDebugBreak()) {
354 if (RelocInfo::IsJSReturn(rmode())) {
355 // Patch the frame exit code with a break point.
356 SetDebugBreakAtReturn();
357 } else if (IsDebugBreakSlot()) {
358 // Patch the code in the break slot.
359 SetDebugBreakAtSlot();
361 // Patch the IC call.
364 ASSERT(IsDebugBreak());
368 void BreakLocationIterator::ClearDebugBreak() {
369 // Debugger statement always calls debugger. No need to modify it.
370 if (IsDebuggerStatement()) {
374 if (RelocInfo::IsJSReturn(rmode())) {
375 // Restore the frame exit code.
376 ClearDebugBreakAtReturn();
377 } else if (IsDebugBreakSlot()) {
378 // Restore the code in the break slot.
379 ClearDebugBreakAtSlot();
381 // Patch the IC call.
382 ClearDebugBreakAtIC();
384 ASSERT(!IsDebugBreak());
388 void BreakLocationIterator::PrepareStepIn() {
391 // Step in can only be prepared if currently positioned on an IC call,
392 // construct call or CallFunction stub call.
393 Address target = rinfo()->target_address();
394 Handle<Code> target_code(Code::GetCodeFromTargetAddress(target));
395 if (target_code->is_call_stub() || target_code->is_keyed_call_stub()) {
396 // Step in through IC call is handled by the runtime system. Therefore make
397 // sure that the any current IC is cleared and the runtime system is
398 // called. If the executing code has a debug break at the location change
399 // the call in the original code as it is the code there that will be
400 // executed in place of the debug break call.
401 Handle<Code> stub = ComputeCallDebugPrepareStepIn(
402 target_code->arguments_count(), target_code->kind());
403 if (IsDebugBreak()) {
404 original_rinfo()->set_target_address(stub->entry());
406 rinfo()->set_target_address(stub->entry());
410 // All the following stuff is needed only for assertion checks so the code
411 // is wrapped in ifdef.
412 Handle<Code> maybe_call_function_stub = target_code;
413 if (IsDebugBreak()) {
414 Address original_target = original_rinfo()->target_address();
415 maybe_call_function_stub =
416 Handle<Code>(Code::GetCodeFromTargetAddress(original_target));
418 bool is_call_function_stub =
419 (maybe_call_function_stub->kind() == Code::STUB &&
420 maybe_call_function_stub->major_key() == CodeStub::CallFunction);
422 // Step in through construct call requires no changes to the running code.
423 // Step in through getters/setters should already be prepared as well
424 // because caller of this function (Debug::PrepareStep) is expected to
425 // flood the top frame's function with one shot breakpoints.
426 // Step in through CallFunction stub should also be prepared by caller of
427 // this function (Debug::PrepareStep) which should flood target function
429 ASSERT(RelocInfo::IsConstructCall(rmode()) ||
430 target_code->is_inline_cache_stub() ||
431 is_call_function_stub);
437 // Check whether the break point is at a position which will exit the function.
438 bool BreakLocationIterator::IsExit() const {
439 return (RelocInfo::IsJSReturn(rmode()));
443 bool BreakLocationIterator::HasBreakPoint() {
444 return debug_info_->HasBreakPoint(code_position());
448 // Check whether there is a debug break at the current position.
449 bool BreakLocationIterator::IsDebugBreak() {
450 if (RelocInfo::IsJSReturn(rmode())) {
451 return IsDebugBreakAtReturn();
452 } else if (IsDebugBreakSlot()) {
453 return IsDebugBreakAtSlot();
455 return Debug::IsDebugBreak(rinfo()->target_address());
460 void BreakLocationIterator::SetDebugBreakAtIC() {
461 // Patch the original code with the current address as the current address
462 // might have changed by the inline caching since the code was copied.
463 original_rinfo()->set_target_address(rinfo()->target_address());
465 RelocInfo::Mode mode = rmode();
466 if (RelocInfo::IsCodeTarget(mode)) {
467 Address target = rinfo()->target_address();
468 Handle<Code> target_code(Code::GetCodeFromTargetAddress(target));
470 // Patch the code to invoke the builtin debug break function matching the
471 // calling convention used by the call site.
472 Handle<Code> dbgbrk_code(Debug::FindDebugBreak(target_code, mode));
473 rinfo()->set_target_address(dbgbrk_code->entry());
478 void BreakLocationIterator::ClearDebugBreakAtIC() {
479 // Patch the code to the original invoke.
480 rinfo()->set_target_address(original_rinfo()->target_address());
484 bool BreakLocationIterator::IsDebuggerStatement() {
485 return RelocInfo::DEBUG_BREAK == rmode();
489 bool BreakLocationIterator::IsDebugBreakSlot() {
490 return RelocInfo::DEBUG_BREAK_SLOT == rmode();
494 Object* BreakLocationIterator::BreakPointObjects() {
495 return debug_info_->GetBreakPointObjects(code_position());
499 // Clear out all the debug break code. This is ONLY supposed to be used when
500 // shutting down the debugger as it will leave the break point information in
501 // DebugInfo even though the code is patched back to the non break point state.
502 void BreakLocationIterator::ClearAllDebugBreak() {
510 bool BreakLocationIterator::RinfoDone() const {
511 ASSERT(reloc_iterator_->done() == reloc_iterator_original_->done());
512 return reloc_iterator_->done();
516 void BreakLocationIterator::RinfoNext() {
517 reloc_iterator_->next();
518 reloc_iterator_original_->next();
520 ASSERT(reloc_iterator_->done() == reloc_iterator_original_->done());
521 if (!reloc_iterator_->done()) {
522 ASSERT(rmode() == original_rmode());
528 // Threading support.
529 void Debug::ThreadInit() {
530 thread_local_.break_count_ = 0;
531 thread_local_.break_id_ = 0;
532 thread_local_.break_frame_id_ = StackFrame::NO_ID;
533 thread_local_.last_step_action_ = StepNone;
534 thread_local_.last_statement_position_ = RelocInfo::kNoPosition;
535 thread_local_.step_count_ = 0;
536 thread_local_.last_fp_ = 0;
537 thread_local_.queued_step_count_ = 0;
538 thread_local_.step_into_fp_ = 0;
539 thread_local_.step_out_fp_ = 0;
540 thread_local_.after_break_target_ = 0;
541 // TODO(isolates): frames_are_dropped_?
542 thread_local_.debugger_entry_ = NULL;
543 thread_local_.pending_interrupts_ = 0;
544 thread_local_.restarter_frame_function_pointer_ = NULL;
548 char* Debug::ArchiveDebug(char* storage) {
550 memcpy(to, reinterpret_cast<char*>(&thread_local_), sizeof(ThreadLocal));
551 to += sizeof(ThreadLocal);
552 memcpy(to, reinterpret_cast<char*>(®isters_), sizeof(registers_));
554 ASSERT(to <= storage + ArchiveSpacePerThread());
555 return storage + ArchiveSpacePerThread();
559 char* Debug::RestoreDebug(char* storage) {
560 char* from = storage;
561 memcpy(reinterpret_cast<char*>(&thread_local_), from, sizeof(ThreadLocal));
562 from += sizeof(ThreadLocal);
563 memcpy(reinterpret_cast<char*>(®isters_), from, sizeof(registers_));
564 ASSERT(from <= storage + ArchiveSpacePerThread());
565 return storage + ArchiveSpacePerThread();
569 int Debug::ArchiveSpacePerThread() {
570 return sizeof(ThreadLocal) + sizeof(JSCallerSavedBuffer);
574 // Frame structure (conforms InternalFrame structure):
577 // -- function (slot is called "context")
579 Object** Debug::SetUpFrameDropperFrame(StackFrame* bottom_js_frame,
581 ASSERT(bottom_js_frame->is_java_script());
583 Address fp = bottom_js_frame->fp();
585 // Move function pointer into "context" slot.
586 Memory::Object_at(fp + StandardFrameConstants::kContextOffset) =
587 Memory::Object_at(fp + JavaScriptFrameConstants::kFunctionOffset);
589 Memory::Object_at(fp + InternalFrameConstants::kCodeOffset) = *code;
590 Memory::Object_at(fp + StandardFrameConstants::kMarkerOffset) =
591 Smi::FromInt(StackFrame::INTERNAL);
593 return reinterpret_cast<Object**>(&Memory::Object_at(
594 fp + StandardFrameConstants::kContextOffset));
597 const int Debug::kFrameDropperFrameSize = 4;
600 void ScriptCache::Add(Handle<Script> script) {
601 GlobalHandles* global_handles = Isolate::Current()->global_handles();
602 // Create an entry in the hash map for the script.
603 int id = Smi::cast(script->id())->value();
604 HashMap::Entry* entry =
605 HashMap::Lookup(reinterpret_cast<void*>(id), Hash(id), true);
606 if (entry->value != NULL) {
607 ASSERT(*script == *reinterpret_cast<Script**>(entry->value));
611 // Globalize the script object, make it weak and use the location of the
612 // global handle as the value in the hash map.
613 Handle<Script> script_ =
614 Handle<Script>::cast(
615 (global_handles->Create(*script)));
616 global_handles->MakeWeak(
617 reinterpret_cast<Object**>(script_.location()),
619 ScriptCache::HandleWeakScript);
620 entry->value = script_.location();
624 Handle<FixedArray> ScriptCache::GetScripts() {
625 Handle<FixedArray> instances = FACTORY->NewFixedArray(occupancy());
627 for (HashMap::Entry* entry = Start(); entry != NULL; entry = Next(entry)) {
628 ASSERT(entry->value != NULL);
629 if (entry->value != NULL) {
630 instances->set(count, *reinterpret_cast<Script**>(entry->value));
638 void ScriptCache::ProcessCollectedScripts() {
639 Debugger* debugger = Isolate::Current()->debugger();
640 for (int i = 0; i < collected_scripts_.length(); i++) {
641 debugger->OnScriptCollected(collected_scripts_[i]);
643 collected_scripts_.Clear();
647 void ScriptCache::Clear() {
648 GlobalHandles* global_handles = Isolate::Current()->global_handles();
649 // Iterate the script cache to get rid of all the weak handles.
650 for (HashMap::Entry* entry = Start(); entry != NULL; entry = Next(entry)) {
651 ASSERT(entry != NULL);
652 Object** location = reinterpret_cast<Object**>(entry->value);
653 ASSERT((*location)->IsScript());
654 global_handles->ClearWeakness(location);
655 global_handles->Destroy(location);
657 // Clear the content of the hash map.
662 void ScriptCache::HandleWeakScript(v8::Persistent<v8::Value> obj, void* data) {
663 ScriptCache* script_cache = reinterpret_cast<ScriptCache*>(data);
664 // Find the location of the global handle.
666 reinterpret_cast<Script**>(Utils::OpenHandle(*obj).location());
667 ASSERT((*location)->IsScript());
669 // Remove the entry from the cache.
670 int id = Smi::cast((*location)->id())->value();
671 script_cache->Remove(reinterpret_cast<void*>(id), Hash(id));
672 script_cache->collected_scripts_.Add(id);
674 // Clear the weak handle.
680 void Debug::SetUp(bool create_heap_objects) {
682 if (create_heap_objects) {
683 // Get code to handle debug break on return.
684 debug_break_return_ =
685 isolate_->builtins()->builtin(Builtins::kReturn_DebugBreak);
686 ASSERT(debug_break_return_->IsCode());
687 // Get code to handle debug break in debug break slots.
689 isolate_->builtins()->builtin(Builtins::kSlot_DebugBreak);
690 ASSERT(debug_break_slot_->IsCode());
695 void Debug::HandleWeakDebugInfo(v8::Persistent<v8::Value> obj, void* data) {
696 Debug* debug = Isolate::Current()->debug();
697 DebugInfoListNode* node = reinterpret_cast<DebugInfoListNode*>(data);
698 // We need to clear all breakpoints associated with the function to restore
699 // original code and avoid patching the code twice later because
700 // the function will live in the heap until next gc, and can be found by
701 // Runtime::FindSharedFunctionInfoInScript.
702 BreakLocationIterator it(node->debug_info(), ALL_BREAK_LOCATIONS);
703 it.ClearAllDebugBreak();
704 debug->RemoveDebugInfo(node->debug_info());
706 node = debug->debug_info_list_;
707 while (node != NULL) {
708 ASSERT(node != reinterpret_cast<DebugInfoListNode*>(data));
715 DebugInfoListNode::DebugInfoListNode(DebugInfo* debug_info): next_(NULL) {
716 GlobalHandles* global_handles = Isolate::Current()->global_handles();
717 // Globalize the request debug info object and make it weak.
718 debug_info_ = Handle<DebugInfo>::cast(
719 (global_handles->Create(debug_info)));
720 global_handles->MakeWeak(
721 reinterpret_cast<Object**>(debug_info_.location()),
723 Debug::HandleWeakDebugInfo);
727 DebugInfoListNode::~DebugInfoListNode() {
728 Isolate::Current()->global_handles()->Destroy(
729 reinterpret_cast<Object**>(debug_info_.location()));
733 bool Debug::CompileDebuggerScript(int index) {
734 Isolate* isolate = Isolate::Current();
735 Factory* factory = isolate->factory();
736 HandleScope scope(isolate);
738 // Bail out if the index is invalid.
743 // Find source and name for the requested script.
744 Handle<String> source_code =
745 isolate->bootstrapper()->NativesSourceLookup(index);
746 Vector<const char> name = Natives::GetScriptName(index);
747 Handle<String> script_name = factory->NewStringFromAscii(name);
749 // Compile the script.
750 Handle<SharedFunctionInfo> function_info;
751 function_info = Compiler::Compile(source_code,
754 Handle<String>::null(),
757 // Silently ignore stack overflows during compilation.
758 if (function_info.is_null()) {
759 ASSERT(isolate->has_pending_exception());
760 isolate->clear_pending_exception();
764 // Execute the shared function in the debugger context.
765 Handle<Context> context = isolate->global_context();
766 bool caught_exception;
767 Handle<JSFunction> function =
768 factory->NewFunctionFromSharedFunctionInfo(function_info, context);
770 Handle<Object> exception =
771 Execution::TryCall(function, Handle<Object>(context->global()),
772 0, NULL, &caught_exception);
774 // Check for caught exceptions.
775 if (caught_exception) {
776 ASSERT(!isolate->has_pending_exception());
777 MessageLocation computed_location;
778 isolate->ComputeLocation(&computed_location);
779 Handle<Object> message = MessageHandler::MakeMessageObject(
780 "error_loading_debugger", &computed_location,
781 Vector<Handle<Object> >::empty(), Handle<String>(), Handle<JSArray>());
782 ASSERT(!isolate->has_pending_exception());
783 isolate->set_pending_exception(*exception);
784 MessageHandler::ReportMessage(Isolate::Current(), NULL, message);
785 isolate->clear_pending_exception();
789 // Mark this script as native and return successfully.
790 Handle<Script> script(Script::cast(function->shared()->script()));
791 script->set_type(Smi::FromInt(Script::TYPE_NATIVE));
797 // Return if debugger is already loaded.
798 if (IsLoaded()) return true;
800 Debugger* debugger = isolate_->debugger();
802 // Bail out if we're already in the process of compiling the native
803 // JavaScript source code for the debugger.
804 if (debugger->compiling_natives() ||
805 debugger->is_loading_debugger())
807 debugger->set_loading_debugger(true);
809 // Disable breakpoints and interrupts while compiling and running the
810 // debugger scripts including the context creation code.
811 DisableBreak disable(true);
812 PostponeInterruptsScope postpone(isolate_);
814 // Create the debugger context.
815 HandleScope scope(isolate_);
816 Handle<Context> context =
817 isolate_->bootstrapper()->CreateEnvironment(
819 Handle<Object>::null(),
820 v8::Handle<ObjectTemplate>(),
823 // Fail if no context could be created.
824 if (context.is_null()) return false;
826 // Use the debugger context.
827 SaveContext save(isolate_);
828 isolate_->set_context(*context);
830 // Expose the builtins object in the debugger context.
831 Handle<String> key = isolate_->factory()->LookupAsciiSymbol("builtins");
832 Handle<GlobalObject> global = Handle<GlobalObject>(context->global());
833 RETURN_IF_EMPTY_HANDLE_VALUE(
835 JSReceiver::SetProperty(global, key, Handle<Object>(global->builtins()),
836 NONE, kNonStrictMode),
839 // Compile the JavaScript for the debugger in the debugger context.
840 debugger->set_compiling_natives(true);
841 bool caught_exception =
842 !CompileDebuggerScript(Natives::GetIndex("mirror")) ||
843 !CompileDebuggerScript(Natives::GetIndex("debug"));
845 if (FLAG_enable_liveedit) {
846 caught_exception = caught_exception ||
847 !CompileDebuggerScript(Natives::GetIndex("liveedit"));
850 debugger->set_compiling_natives(false);
852 // Make sure we mark the debugger as not loading before we might
854 debugger->set_loading_debugger(false);
856 // Check for caught exceptions.
857 if (caught_exception) return false;
860 debug_context_ = context;
866 void Debug::Unload() {
867 // Return debugger is not loaded.
872 // Clear the script cache.
873 DestroyScriptCache();
875 // Clear debugger context global handle.
876 Isolate::Current()->global_handles()->Destroy(
877 reinterpret_cast<Object**>(debug_context_.location()));
878 debug_context_ = Handle<Context>();
882 // Set the flag indicating that preemption happened during debugging.
883 void Debug::PreemptionWhileInDebugger() {
884 ASSERT(InDebugger());
885 Debug::set_interrupts_pending(PREEMPT);
889 void Debug::Iterate(ObjectVisitor* v) {
890 v->VisitPointer(BitCast<Object**>(&(debug_break_return_)));
891 v->VisitPointer(BitCast<Object**>(&(debug_break_slot_)));
895 void Debug::PutValuesOnStackAndDie(int start,
905 Object* Debug::Break(Arguments args) {
906 Heap* heap = isolate_->heap();
907 HandleScope scope(isolate_);
908 ASSERT(args.length() == 0);
910 thread_local_.frame_drop_mode_ = FRAMES_UNTOUCHED;
912 // Get the top-most JavaScript frame.
913 JavaScriptFrameIterator it(isolate_);
914 JavaScriptFrame* frame = it.frame();
916 // Just continue if breaks are disabled or debugger cannot be loaded.
917 if (disable_break() || !Load()) {
918 SetAfterBreakTarget(frame);
919 return heap->undefined_value();
922 // Enter the debugger.
923 EnterDebugger debugger;
924 if (debugger.FailedToEnter()) {
925 return heap->undefined_value();
928 // Postpone interrupt during breakpoint processing.
929 PostponeInterruptsScope postpone(isolate_);
931 // Get the debug info (create it if it does not exist).
932 Handle<SharedFunctionInfo> shared =
933 Handle<SharedFunctionInfo>(JSFunction::cast(frame->function())->shared());
934 Handle<DebugInfo> debug_info = GetDebugInfo(shared);
936 // Find the break point where execution has stopped.
937 BreakLocationIterator break_location_iterator(debug_info,
938 ALL_BREAK_LOCATIONS);
939 break_location_iterator.FindBreakLocationFromAddress(frame->pc());
941 // Check whether step next reached a new statement.
942 if (!StepNextContinue(&break_location_iterator, frame)) {
943 // Decrease steps left if performing multiple steps.
944 if (thread_local_.step_count_ > 0) {
945 thread_local_.step_count_--;
949 // If there is one or more real break points check whether any of these are
951 Handle<Object> break_points_hit(heap->undefined_value());
952 if (break_location_iterator.HasBreakPoint()) {
953 Handle<Object> break_point_objects =
954 Handle<Object>(break_location_iterator.BreakPointObjects());
955 break_points_hit = CheckBreakPoints(break_point_objects);
958 // If step out is active skip everything until the frame where we need to step
959 // out to is reached, unless real breakpoint is hit.
960 if (StepOutActive() && frame->fp() != step_out_fp() &&
961 break_points_hit->IsUndefined() ) {
962 // Step count should always be 0 for StepOut.
963 ASSERT(thread_local_.step_count_ == 0);
964 } else if (!break_points_hit->IsUndefined() ||
965 (thread_local_.last_step_action_ != StepNone &&
966 thread_local_.step_count_ == 0)) {
967 // Notify debugger if a real break point is triggered or if performing
968 // single stepping with no more steps to perform. Otherwise do another step.
970 // Clear all current stepping setup.
973 if (thread_local_.queued_step_count_ > 0) {
974 // Perform queued steps
975 int step_count = thread_local_.queued_step_count_;
978 thread_local_.queued_step_count_ = 0;
980 PrepareStep(StepNext, step_count);
982 // Notify the debug event listeners.
983 isolate_->debugger()->OnDebugBreak(break_points_hit, false);
985 } else if (thread_local_.last_step_action_ != StepNone) {
986 // Hold on to last step action as it is cleared by the call to
988 StepAction step_action = thread_local_.last_step_action_;
989 int step_count = thread_local_.step_count_;
991 // If StepNext goes deeper in code, StepOut until original frame
992 // and keep step count queued up in the meantime.
993 if (step_action == StepNext && frame->fp() < thread_local_.last_fp_) {
994 // Count frames until target frame
996 JavaScriptFrameIterator it(isolate_);
997 while (!it.done() && it.frame()->fp() < thread_local_.last_fp_) {
1002 // Catch the cases that would lead to crashes and capture
1003 // - C entry FP at which to start stack crawl.
1004 // - FP of the frame at which we plan to stop stepping out (last FP).
1005 // - current FP that's larger than last FP.
1006 // - Counter for the number of steps to step out.
1008 // We crawled the entire stack, never reaching last_fp_.
1009 PutValuesOnStackAndDie(0xBEEEEEEE,
1011 thread_local_.last_fp_,
1015 } else if (it.frame()->fp() != thread_local_.last_fp_) {
1016 // We crawled over last_fp_, without getting a match.
1017 PutValuesOnStackAndDie(0xBEEEEEEE,
1019 thread_local_.last_fp_,
1025 // If we found original frame
1026 if (it.frame()->fp() == thread_local_.last_fp_) {
1027 if (step_count > 1) {
1028 // Save old count and action to continue stepping after
1030 thread_local_.queued_step_count_ = step_count - 1;
1033 // Set up for StepOut to reach target frame
1034 step_action = StepOut;
1039 // Clear all current stepping setup.
1042 // Set up for the remaining steps.
1043 PrepareStep(step_action, step_count);
1046 if (thread_local_.frame_drop_mode_ == FRAMES_UNTOUCHED) {
1047 SetAfterBreakTarget(frame);
1048 } else if (thread_local_.frame_drop_mode_ ==
1049 FRAME_DROPPED_IN_IC_CALL) {
1050 // We must have been calling IC stub. Do not go there anymore.
1051 Code* plain_return = isolate_->builtins()->builtin(
1052 Builtins::kPlainReturn_LiveEdit);
1053 thread_local_.after_break_target_ = plain_return->entry();
1054 } else if (thread_local_.frame_drop_mode_ ==
1055 FRAME_DROPPED_IN_DEBUG_SLOT_CALL) {
1056 // Debug break slot stub does not return normally, instead it manually
1057 // cleans the stack and jumps. We should patch the jump address.
1058 Code* plain_return = isolate_->builtins()->builtin(
1059 Builtins::kFrameDropper_LiveEdit);
1060 thread_local_.after_break_target_ = plain_return->entry();
1061 } else if (thread_local_.frame_drop_mode_ ==
1062 FRAME_DROPPED_IN_DIRECT_CALL) {
1063 // Nothing to do, after_break_target is not used here.
1064 } else if (thread_local_.frame_drop_mode_ ==
1065 FRAME_DROPPED_IN_RETURN_CALL) {
1066 Code* plain_return = isolate_->builtins()->builtin(
1067 Builtins::kFrameDropper_LiveEdit);
1068 thread_local_.after_break_target_ = plain_return->entry();
1073 return heap->undefined_value();
1077 RUNTIME_FUNCTION(Object*, Debug_Break) {
1078 return isolate->debug()->Break(args);
1082 // Check the break point objects for whether one or more are actually
1083 // triggered. This function returns a JSArray with the break point objects
1084 // which is triggered.
1085 Handle<Object> Debug::CheckBreakPoints(Handle<Object> break_point_objects) {
1086 Factory* factory = isolate_->factory();
1088 // Count the number of break points hit. If there are multiple break points
1089 // they are in a FixedArray.
1090 Handle<FixedArray> break_points_hit;
1091 int break_points_hit_count = 0;
1092 ASSERT(!break_point_objects->IsUndefined());
1093 if (break_point_objects->IsFixedArray()) {
1094 Handle<FixedArray> array(FixedArray::cast(*break_point_objects));
1095 break_points_hit = factory->NewFixedArray(array->length());
1096 for (int i = 0; i < array->length(); i++) {
1097 Handle<Object> o(array->get(i));
1098 if (CheckBreakPoint(o)) {
1099 break_points_hit->set(break_points_hit_count++, *o);
1103 break_points_hit = factory->NewFixedArray(1);
1104 if (CheckBreakPoint(break_point_objects)) {
1105 break_points_hit->set(break_points_hit_count++, *break_point_objects);
1109 // Return undefined if no break points were triggered.
1110 if (break_points_hit_count == 0) {
1111 return factory->undefined_value();
1113 // Return break points hit as a JSArray.
1114 Handle<JSArray> result = factory->NewJSArrayWithElements(break_points_hit);
1115 result->set_length(Smi::FromInt(break_points_hit_count));
1120 // Check whether a single break point object is triggered.
1121 bool Debug::CheckBreakPoint(Handle<Object> break_point_object) {
1122 Factory* factory = isolate_->factory();
1123 HandleScope scope(isolate_);
1125 // Ignore check if break point object is not a JSObject.
1126 if (!break_point_object->IsJSObject()) return true;
1128 // Get the function IsBreakPointTriggered (defined in debug-debugger.js).
1129 Handle<String> is_break_point_triggered_symbol =
1130 factory->LookupAsciiSymbol("IsBreakPointTriggered");
1131 Handle<JSFunction> check_break_point =
1132 Handle<JSFunction>(JSFunction::cast(
1133 debug_context()->global()->GetPropertyNoExceptionThrown(
1134 *is_break_point_triggered_symbol)));
1136 // Get the break id as an object.
1137 Handle<Object> break_id = factory->NewNumberFromInt(Debug::break_id());
1139 // Call HandleBreakPointx.
1140 bool caught_exception;
1141 Handle<Object> argv[] = { break_id, break_point_object };
1142 Handle<Object> result = Execution::TryCall(check_break_point,
1143 isolate_->js_builtins_object(),
1148 // If exception or non boolean result handle as not triggered
1149 if (caught_exception || !result->IsBoolean()) {
1153 // Return whether the break point is triggered.
1154 ASSERT(!result.is_null());
1155 return (*result)->IsTrue();
1159 // Check whether the function has debug information.
1160 bool Debug::HasDebugInfo(Handle<SharedFunctionInfo> shared) {
1161 return !shared->debug_info()->IsUndefined();
1165 // Return the debug info for this function. EnsureDebugInfo must be called
1166 // prior to ensure the debug info has been generated for shared.
1167 Handle<DebugInfo> Debug::GetDebugInfo(Handle<SharedFunctionInfo> shared) {
1168 ASSERT(HasDebugInfo(shared));
1169 return Handle<DebugInfo>(DebugInfo::cast(shared->debug_info()));
1173 void Debug::SetBreakPoint(Handle<SharedFunctionInfo> shared,
1174 Handle<Object> break_point_object,
1175 int* source_position) {
1176 HandleScope scope(isolate_);
1178 PrepareForBreakPoints();
1180 if (!EnsureDebugInfo(shared)) {
1181 // Return if retrieving debug info failed.
1185 Handle<DebugInfo> debug_info = GetDebugInfo(shared);
1186 // Source positions starts with zero.
1187 ASSERT(*source_position >= 0);
1189 // Find the break point and change it.
1190 BreakLocationIterator it(debug_info, SOURCE_BREAK_LOCATIONS);
1191 it.FindBreakLocationFromPosition(*source_position);
1192 it.SetBreakPoint(break_point_object);
1194 *source_position = it.position();
1196 // At least one active break point now.
1197 ASSERT(debug_info->GetBreakPointCount() > 0);
1201 void Debug::ClearBreakPoint(Handle<Object> break_point_object) {
1202 HandleScope scope(isolate_);
1204 DebugInfoListNode* node = debug_info_list_;
1205 while (node != NULL) {
1206 Object* result = DebugInfo::FindBreakPointInfo(node->debug_info(),
1207 break_point_object);
1208 if (!result->IsUndefined()) {
1209 // Get information in the break point.
1210 BreakPointInfo* break_point_info = BreakPointInfo::cast(result);
1211 Handle<DebugInfo> debug_info = node->debug_info();
1212 Handle<SharedFunctionInfo> shared(debug_info->shared());
1213 int source_position = break_point_info->statement_position()->value();
1215 // Source positions starts with zero.
1216 ASSERT(source_position >= 0);
1218 // Find the break point and clear it.
1219 BreakLocationIterator it(debug_info, SOURCE_BREAK_LOCATIONS);
1220 it.FindBreakLocationFromPosition(source_position);
1221 it.ClearBreakPoint(break_point_object);
1223 // If there are no more break points left remove the debug info for this
1225 if (debug_info->GetBreakPointCount() == 0) {
1226 RemoveDebugInfo(debug_info);
1231 node = node->next();
1236 void Debug::ClearAllBreakPoints() {
1237 DebugInfoListNode* node = debug_info_list_;
1238 while (node != NULL) {
1239 // Remove all debug break code.
1240 BreakLocationIterator it(node->debug_info(), ALL_BREAK_LOCATIONS);
1241 it.ClearAllDebugBreak();
1242 node = node->next();
1245 // Remove all debug info.
1246 while (debug_info_list_ != NULL) {
1247 RemoveDebugInfo(debug_info_list_->debug_info());
1252 void Debug::FloodWithOneShot(Handle<SharedFunctionInfo> shared) {
1253 PrepareForBreakPoints();
1254 // Make sure the function has set up the debug info.
1255 if (!EnsureDebugInfo(shared)) {
1256 // Return if we failed to retrieve the debug info.
1260 // Flood the function with break points.
1261 BreakLocationIterator it(GetDebugInfo(shared), ALL_BREAK_LOCATIONS);
1262 while (!it.Done()) {
1269 void Debug::FloodBoundFunctionWithOneShot(Handle<JSFunction> function) {
1270 Handle<FixedArray> new_bindings(function->function_bindings());
1271 Handle<Object> bindee(new_bindings->get(JSFunction::kBoundFunctionIndex));
1273 if (!bindee.is_null() && bindee->IsJSFunction() &&
1274 !JSFunction::cast(*bindee)->IsBuiltin()) {
1275 Handle<SharedFunctionInfo> shared_info(JSFunction::cast(*bindee)->shared());
1276 Debug::FloodWithOneShot(shared_info);
1281 void Debug::FloodHandlerWithOneShot() {
1282 // Iterate through the JavaScript stack looking for handlers.
1283 StackFrame::Id id = break_frame_id();
1284 if (id == StackFrame::NO_ID) {
1285 // If there is no JavaScript stack don't do anything.
1288 for (JavaScriptFrameIterator it(isolate_, id); !it.done(); it.Advance()) {
1289 JavaScriptFrame* frame = it.frame();
1290 if (frame->HasHandler()) {
1291 Handle<SharedFunctionInfo> shared =
1292 Handle<SharedFunctionInfo>(
1293 JSFunction::cast(frame->function())->shared());
1294 // Flood the function with the catch block with break points
1295 FloodWithOneShot(shared);
1302 void Debug::ChangeBreakOnException(ExceptionBreakType type, bool enable) {
1303 if (type == BreakUncaughtException) {
1304 break_on_uncaught_exception_ = enable;
1306 break_on_exception_ = enable;
1311 bool Debug::IsBreakOnException(ExceptionBreakType type) {
1312 if (type == BreakUncaughtException) {
1313 return break_on_uncaught_exception_;
1315 return break_on_exception_;
1320 void Debug::PrepareStep(StepAction step_action, int step_count) {
1321 HandleScope scope(isolate_);
1323 PrepareForBreakPoints();
1325 ASSERT(Debug::InDebugger());
1327 // Remember this step action and count.
1328 thread_local_.last_step_action_ = step_action;
1329 if (step_action == StepOut) {
1330 // For step out target frame will be found on the stack so there is no need
1331 // to set step counter for it. It's expected to always be 0 for StepOut.
1332 thread_local_.step_count_ = 0;
1334 thread_local_.step_count_ = step_count;
1337 // Get the frame where the execution has stopped and skip the debug frame if
1338 // any. The debug frame will only be present if execution was stopped due to
1339 // hitting a break point. In other situations (e.g. unhandled exception) the
1340 // debug frame is not present.
1341 StackFrame::Id id = break_frame_id();
1342 if (id == StackFrame::NO_ID) {
1343 // If there is no JavaScript stack don't do anything.
1346 JavaScriptFrameIterator frames_it(isolate_, id);
1347 JavaScriptFrame* frame = frames_it.frame();
1349 // First of all ensure there is one-shot break points in the top handler
1351 FloodHandlerWithOneShot();
1353 // If the function on the top frame is unresolved perform step out. This will
1354 // be the case when calling unknown functions and having the debugger stopped
1355 // in an unhandled exception.
1356 if (!frame->function()->IsJSFunction()) {
1357 // Step out: Find the calling JavaScript frame and flood it with
1359 frames_it.Advance();
1360 // Fill the function to return to with one-shot break points.
1361 JSFunction* function = JSFunction::cast(frames_it.frame()->function());
1362 FloodWithOneShot(Handle<SharedFunctionInfo>(function->shared()));
1366 // Get the debug info (create it if it does not exist).
1367 Handle<SharedFunctionInfo> shared =
1368 Handle<SharedFunctionInfo>(JSFunction::cast(frame->function())->shared());
1369 if (!EnsureDebugInfo(shared)) {
1370 // Return if ensuring debug info failed.
1373 Handle<DebugInfo> debug_info = GetDebugInfo(shared);
1375 // Find the break location where execution has stopped.
1376 BreakLocationIterator it(debug_info, ALL_BREAK_LOCATIONS);
1377 it.FindBreakLocationFromAddress(frame->pc());
1379 // Compute whether or not the target is a call target.
1380 bool is_load_or_store = false;
1381 bool is_inline_cache_stub = false;
1382 bool is_at_restarted_function = false;
1383 Handle<Code> call_function_stub;
1385 if (thread_local_.restarter_frame_function_pointer_ == NULL) {
1386 if (RelocInfo::IsCodeTarget(it.rinfo()->rmode())) {
1387 bool is_call_target = false;
1388 Address target = it.rinfo()->target_address();
1389 Code* code = Code::GetCodeFromTargetAddress(target);
1390 if (code->is_call_stub() || code->is_keyed_call_stub()) {
1391 is_call_target = true;
1393 if (code->is_inline_cache_stub()) {
1394 is_inline_cache_stub = true;
1395 is_load_or_store = !is_call_target;
1398 // Check if target code is CallFunction stub.
1399 Code* maybe_call_function_stub = code;
1400 // If there is a breakpoint at this line look at the original code to
1401 // check if it is a CallFunction stub.
1402 if (it.IsDebugBreak()) {
1403 Address original_target = it.original_rinfo()->target_address();
1404 maybe_call_function_stub =
1405 Code::GetCodeFromTargetAddress(original_target);
1407 if (maybe_call_function_stub->kind() == Code::STUB &&
1408 maybe_call_function_stub->major_key() == CodeStub::CallFunction) {
1409 // Save reference to the code as we may need it to find out arguments
1410 // count for 'step in' later.
1411 call_function_stub = Handle<Code>(maybe_call_function_stub);
1415 is_at_restarted_function = true;
1418 // If this is the last break code target step out is the only possibility.
1419 if (it.IsExit() || step_action == StepOut) {
1420 if (step_action == StepOut) {
1421 // Skip step_count frames starting with the current one.
1422 while (step_count-- > 0 && !frames_it.done()) {
1423 frames_it.Advance();
1426 ASSERT(it.IsExit());
1427 frames_it.Advance();
1429 // Skip builtin functions on the stack.
1430 while (!frames_it.done() &&
1431 JSFunction::cast(frames_it.frame()->function())->IsBuiltin()) {
1432 frames_it.Advance();
1434 // Step out: If there is a JavaScript caller frame, we need to
1435 // flood it with breakpoints.
1436 if (!frames_it.done()) {
1437 // Fill the function to return to with one-shot break points.
1438 JSFunction* function = JSFunction::cast(frames_it.frame()->function());
1439 FloodWithOneShot(Handle<SharedFunctionInfo>(function->shared()));
1440 // Set target frame pointer.
1441 ActivateStepOut(frames_it.frame());
1443 } else if (!(is_inline_cache_stub || RelocInfo::IsConstructCall(it.rmode()) ||
1444 !call_function_stub.is_null() || is_at_restarted_function)
1445 || step_action == StepNext || step_action == StepMin) {
1446 // Step next or step min.
1448 // Fill the current function with one-shot break points.
1449 FloodWithOneShot(shared);
1451 // Remember source position and frame to handle step next.
1452 thread_local_.last_statement_position_ =
1453 debug_info->code()->SourceStatementPosition(frame->pc());
1454 thread_local_.last_fp_ = frame->fp();
1456 // If there's restarter frame on top of the stack, just get the pointer
1457 // to function which is going to be restarted.
1458 if (is_at_restarted_function) {
1459 Handle<JSFunction> restarted_function(
1460 JSFunction::cast(*thread_local_.restarter_frame_function_pointer_));
1461 Handle<SharedFunctionInfo> restarted_shared(
1462 restarted_function->shared());
1463 FloodWithOneShot(restarted_shared);
1464 } else if (!call_function_stub.is_null()) {
1465 // If it's CallFunction stub ensure target function is compiled and flood
1466 // it with one shot breakpoints.
1468 // Find out number of arguments from the stub minor key.
1469 // Reverse lookup required as the minor key cannot be retrieved
1470 // from the code object.
1472 isolate_->heap()->code_stubs()->SlowReverseLookup(
1473 *call_function_stub));
1474 ASSERT(!obj.is_null());
1475 ASSERT(!(*obj)->IsUndefined());
1476 ASSERT(obj->IsSmi());
1477 // Get the STUB key and extract major and minor key.
1478 uint32_t key = Smi::cast(*obj)->value();
1479 // Argc in the stub is the number of arguments passed - not the
1480 // expected arguments of the called function.
1481 int call_function_arg_count =
1482 CallFunctionStub::ExtractArgcFromMinorKey(
1483 CodeStub::MinorKeyFromKey(key));
1484 ASSERT(call_function_stub->major_key() ==
1485 CodeStub::MajorKeyFromKey(key));
1487 // Find target function on the expression stack.
1488 // Expression stack looks like this (top to bottom):
1494 int expressions_count = frame->ComputeExpressionsCount();
1495 ASSERT(expressions_count - 2 - call_function_arg_count >= 0);
1496 Object* fun = frame->GetExpression(
1497 expressions_count - 2 - call_function_arg_count);
1498 if (fun->IsJSFunction()) {
1499 Handle<JSFunction> js_function(JSFunction::cast(fun));
1500 if (js_function->shared()->bound()) {
1501 Debug::FloodBoundFunctionWithOneShot(js_function);
1502 } else if (!js_function->IsBuiltin()) {
1503 // Don't step into builtins.
1504 // It will also compile target function if it's not compiled yet.
1505 FloodWithOneShot(Handle<SharedFunctionInfo>(js_function->shared()));
1510 // Fill the current function with one-shot break points even for step in on
1511 // a call target as the function called might be a native function for
1512 // which step in will not stop. It also prepares for stepping in
1514 FloodWithOneShot(shared);
1516 if (is_load_or_store) {
1517 // Remember source position and frame to handle step in getter/setter. If
1518 // there is a custom getter/setter it will be handled in
1519 // Object::Get/SetPropertyWithCallback, otherwise the step action will be
1520 // propagated on the next Debug::Break.
1521 thread_local_.last_statement_position_ =
1522 debug_info->code()->SourceStatementPosition(frame->pc());
1523 thread_local_.last_fp_ = frame->fp();
1526 // Step in or Step in min
1528 ActivateStepIn(frame);
1533 // Check whether the current debug break should be reported to the debugger. It
1534 // is used to have step next and step in only report break back to the debugger
1535 // if on a different frame or in a different statement. In some situations
1536 // there will be several break points in the same statement when the code is
1537 // flooded with one-shot break points. This function helps to perform several
1538 // steps before reporting break back to the debugger.
1539 bool Debug::StepNextContinue(BreakLocationIterator* break_location_iterator,
1540 JavaScriptFrame* frame) {
1541 // StepNext and StepOut shouldn't bring us deeper in code, so last frame
1542 // shouldn't be a parent of current frame.
1543 if (thread_local_.last_step_action_ == StepNext ||
1544 thread_local_.last_step_action_ == StepOut) {
1545 if (frame->fp() < thread_local_.last_fp_) return true;
1548 // If the step last action was step next or step in make sure that a new
1549 // statement is hit.
1550 if (thread_local_.last_step_action_ == StepNext ||
1551 thread_local_.last_step_action_ == StepIn) {
1552 // Never continue if returning from function.
1553 if (break_location_iterator->IsExit()) return false;
1555 // Continue if we are still on the same frame and in the same statement.
1556 int current_statement_position =
1557 break_location_iterator->code()->SourceStatementPosition(frame->pc());
1558 return thread_local_.last_fp_ == frame->fp() &&
1559 thread_local_.last_statement_position_ == current_statement_position;
1562 // No step next action - don't continue.
1567 // Check whether the code object at the specified address is a debug break code
1569 bool Debug::IsDebugBreak(Address addr) {
1570 Code* code = Code::GetCodeFromTargetAddress(addr);
1571 return code->ic_state() == DEBUG_BREAK;
1575 // Check whether a code stub with the specified major key is a possible break
1576 // point location when looking for source break locations.
1577 bool Debug::IsSourceBreakStub(Code* code) {
1578 CodeStub::Major major_key = CodeStub::GetMajorKey(code);
1579 return major_key == CodeStub::CallFunction;
1583 // Check whether a code stub with the specified major key is a possible break
1585 bool Debug::IsBreakStub(Code* code) {
1586 CodeStub::Major major_key = CodeStub::GetMajorKey(code);
1587 return major_key == CodeStub::CallFunction;
1591 // Find the builtin to use for invoking the debug break
1592 Handle<Code> Debug::FindDebugBreak(Handle<Code> code, RelocInfo::Mode mode) {
1593 Isolate* isolate = Isolate::Current();
1595 // Find the builtin debug break function matching the calling convention
1596 // used by the call site.
1597 if (code->is_inline_cache_stub()) {
1598 switch (code->kind()) {
1600 case Code::KEYED_CALL_IC:
1601 return isolate->stub_cache()->ComputeCallDebugBreak(
1602 code->arguments_count(), code->kind());
1605 return isolate->builtins()->LoadIC_DebugBreak();
1607 case Code::STORE_IC:
1608 return isolate->builtins()->StoreIC_DebugBreak();
1610 case Code::KEYED_LOAD_IC:
1611 return isolate->builtins()->KeyedLoadIC_DebugBreak();
1613 case Code::KEYED_STORE_IC:
1614 return isolate->builtins()->KeyedStoreIC_DebugBreak();
1620 if (RelocInfo::IsConstructCall(mode)) {
1621 if (code->has_function_cache()) {
1622 return isolate->builtins()->CallConstructStub_Recording_DebugBreak();
1624 return isolate->builtins()->CallConstructStub_DebugBreak();
1627 if (code->kind() == Code::STUB) {
1628 ASSERT(code->major_key() == CodeStub::CallFunction);
1629 if (code->has_function_cache()) {
1630 return isolate->builtins()->CallFunctionStub_Recording_DebugBreak();
1632 return isolate->builtins()->CallFunctionStub_DebugBreak();
1637 return Handle<Code>::null();
1641 // Simple function for returning the source positions for active break points.
1642 Handle<Object> Debug::GetSourceBreakLocations(
1643 Handle<SharedFunctionInfo> shared) {
1644 Isolate* isolate = Isolate::Current();
1645 Heap* heap = isolate->heap();
1646 if (!HasDebugInfo(shared)) return Handle<Object>(heap->undefined_value());
1647 Handle<DebugInfo> debug_info = GetDebugInfo(shared);
1648 if (debug_info->GetBreakPointCount() == 0) {
1649 return Handle<Object>(heap->undefined_value());
1651 Handle<FixedArray> locations =
1652 isolate->factory()->NewFixedArray(debug_info->GetBreakPointCount());
1654 for (int i = 0; i < debug_info->break_points()->length(); i++) {
1655 if (!debug_info->break_points()->get(i)->IsUndefined()) {
1656 BreakPointInfo* break_point_info =
1657 BreakPointInfo::cast(debug_info->break_points()->get(i));
1658 if (break_point_info->GetBreakPointCount() > 0) {
1659 locations->set(count++, break_point_info->statement_position());
1667 void Debug::NewBreak(StackFrame::Id break_frame_id) {
1668 thread_local_.break_frame_id_ = break_frame_id;
1669 thread_local_.break_id_ = ++thread_local_.break_count_;
1673 void Debug::SetBreak(StackFrame::Id break_frame_id, int break_id) {
1674 thread_local_.break_frame_id_ = break_frame_id;
1675 thread_local_.break_id_ = break_id;
1679 // Handle stepping into a function.
1680 void Debug::HandleStepIn(Handle<JSFunction> function,
1681 Handle<Object> holder,
1683 bool is_constructor) {
1684 // If the frame pointer is not supplied by the caller find it.
1686 StackFrameIterator it;
1688 // For constructor functions skip another frame.
1689 if (is_constructor) {
1690 ASSERT(it.frame()->is_construct());
1693 fp = it.frame()->fp();
1696 // Flood the function with one-shot break points if it is called from where
1697 // step into was requested.
1698 if (fp == step_in_fp()) {
1699 if (function->shared()->bound()) {
1700 // Handle Function.prototype.bind
1701 Debug::FloodBoundFunctionWithOneShot(function);
1702 } else if (!function->IsBuiltin()) {
1703 // Don't allow step into functions in the native context.
1704 if (function->shared()->code() ==
1705 Isolate::Current()->builtins()->builtin(Builtins::kFunctionApply) ||
1706 function->shared()->code() ==
1707 Isolate::Current()->builtins()->builtin(Builtins::kFunctionCall)) {
1708 // Handle function.apply and function.call separately to flood the
1709 // function to be called and not the code for Builtins::FunctionApply or
1710 // Builtins::FunctionCall. The receiver of call/apply is the target
1712 if (!holder.is_null() && holder->IsJSFunction() &&
1713 !JSFunction::cast(*holder)->IsBuiltin()) {
1714 Handle<SharedFunctionInfo> shared_info(
1715 JSFunction::cast(*holder)->shared());
1716 Debug::FloodWithOneShot(shared_info);
1719 Debug::FloodWithOneShot(Handle<SharedFunctionInfo>(function->shared()));
1726 void Debug::ClearStepping() {
1727 // Clear the various stepping setup.
1733 // Clear multiple step counter.
1734 thread_local_.step_count_ = 0;
1737 // Clears all the one-shot break points that are currently set. Normally this
1738 // function is called each time a break point is hit as one shot break points
1739 // are used to support stepping.
1740 void Debug::ClearOneShot() {
1741 // The current implementation just runs through all the breakpoints. When the
1742 // last break point for a function is removed that function is automatically
1743 // removed from the list.
1745 DebugInfoListNode* node = debug_info_list_;
1746 while (node != NULL) {
1747 BreakLocationIterator it(node->debug_info(), ALL_BREAK_LOCATIONS);
1748 while (!it.Done()) {
1752 node = node->next();
1757 void Debug::ActivateStepIn(StackFrame* frame) {
1758 ASSERT(!StepOutActive());
1759 thread_local_.step_into_fp_ = frame->fp();
1763 void Debug::ClearStepIn() {
1764 thread_local_.step_into_fp_ = 0;
1768 void Debug::ActivateStepOut(StackFrame* frame) {
1769 ASSERT(!StepInActive());
1770 thread_local_.step_out_fp_ = frame->fp();
1774 void Debug::ClearStepOut() {
1775 thread_local_.step_out_fp_ = 0;
1779 void Debug::ClearStepNext() {
1780 thread_local_.last_step_action_ = StepNone;
1781 thread_local_.last_statement_position_ = RelocInfo::kNoPosition;
1782 thread_local_.last_fp_ = 0;
1786 // Helper function to compile full code for debugging. This code will
1787 // have debug break slots and deoptimization
1788 // information. Deoptimization information is required in case that an
1789 // optimized version of this function is still activated on the
1790 // stack. It will also make sure that the full code is compiled with
1791 // the same flags as the previous version - that is flags which can
1792 // change the code generated. The current method of mapping from
1793 // already compiled full code without debug break slots to full code
1794 // with debug break slots depends on the generated code is otherwise
1795 // exactly the same.
1796 static bool CompileFullCodeForDebugging(Handle<SharedFunctionInfo> shared,
1797 Handle<Code> current_code) {
1798 ASSERT(!current_code->has_debug_break_slots());
1800 CompilationInfo info(shared);
1801 info.MarkCompilingForDebugging(current_code);
1802 ASSERT(!info.shared_info()->is_compiled());
1803 ASSERT(!info.isolate()->has_pending_exception());
1805 // Use compile lazy which will end up compiling the full code in the
1806 // configuration configured above.
1807 bool result = Compiler::CompileLazy(&info);
1808 ASSERT(result != Isolate::Current()->has_pending_exception());
1809 info.isolate()->clear_pending_exception();
1812 Handle<Code> new_code(shared->code());
1813 ASSERT(new_code->has_debug_break_slots());
1814 ASSERT(current_code->is_compiled_optimizable() ==
1815 new_code->is_compiled_optimizable());
1822 static void CollectActiveFunctionsFromThread(
1824 ThreadLocalTop* top,
1825 List<Handle<JSFunction> >* active_functions,
1826 Object* active_code_marker) {
1827 // Find all non-optimized code functions with activation frames
1828 // on the stack. This includes functions which have optimized
1829 // activations (including inlined functions) on the stack as the
1830 // non-optimized code is needed for the lazy deoptimization.
1831 for (JavaScriptFrameIterator it(isolate, top); !it.done(); it.Advance()) {
1832 JavaScriptFrame* frame = it.frame();
1833 if (frame->is_optimized()) {
1834 List<JSFunction*> functions(Compiler::kMaxInliningLevels + 1);
1835 frame->GetFunctions(&functions);
1836 for (int i = 0; i < functions.length(); i++) {
1837 JSFunction* function = functions[i];
1838 active_functions->Add(Handle<JSFunction>(function));
1839 function->shared()->code()->set_gc_metadata(active_code_marker);
1841 } else if (frame->function()->IsJSFunction()) {
1842 JSFunction* function = JSFunction::cast(frame->function());
1843 ASSERT(frame->LookupCode()->kind() == Code::FUNCTION);
1844 active_functions->Add(Handle<JSFunction>(function));
1845 function->shared()->code()->set_gc_metadata(active_code_marker);
1851 static void RedirectActivationsToRecompiledCodeOnThread(
1853 ThreadLocalTop* top) {
1854 for (JavaScriptFrameIterator it(isolate, top); !it.done(); it.Advance()) {
1855 JavaScriptFrame* frame = it.frame();
1857 if (frame->is_optimized() || !frame->function()->IsJSFunction()) continue;
1859 JSFunction* function = JSFunction::cast(frame->function());
1861 ASSERT(frame->LookupCode()->kind() == Code::FUNCTION);
1863 Handle<Code> frame_code(frame->LookupCode());
1864 if (frame_code->has_debug_break_slots()) continue;
1866 Handle<Code> new_code(function->shared()->code());
1867 if (new_code->kind() != Code::FUNCTION ||
1868 !new_code->has_debug_break_slots()) {
1872 intptr_t delta = frame->pc() - frame_code->instruction_start();
1873 int debug_break_slot_count = 0;
1874 int mask = RelocInfo::ModeMask(RelocInfo::DEBUG_BREAK_SLOT);
1875 for (RelocIterator it(*new_code, mask); !it.done(); it.next()) {
1876 // Check if the pc in the new code with debug break
1877 // slots is before this slot.
1878 RelocInfo* info = it.rinfo();
1879 int debug_break_slot_bytes =
1880 debug_break_slot_count * Assembler::kDebugBreakSlotLength;
1881 intptr_t new_delta =
1883 new_code->instruction_start() -
1884 debug_break_slot_bytes;
1885 if (new_delta > delta) {
1889 // Passed a debug break slot in the full code with debug
1891 debug_break_slot_count++;
1893 int debug_break_slot_bytes =
1894 debug_break_slot_count * Assembler::kDebugBreakSlotLength;
1895 if (FLAG_trace_deopt) {
1896 PrintF("Replacing code %08" V8PRIxPTR " - %08" V8PRIxPTR " (%d) "
1897 "with %08" V8PRIxPTR " - %08" V8PRIxPTR " (%d) "
1899 "changing pc from %08" V8PRIxPTR " to %08" V8PRIxPTR "\n",
1900 reinterpret_cast<intptr_t>(
1901 frame_code->instruction_start()),
1902 reinterpret_cast<intptr_t>(
1903 frame_code->instruction_start()) +
1904 frame_code->instruction_size(),
1905 frame_code->instruction_size(),
1906 reinterpret_cast<intptr_t>(new_code->instruction_start()),
1907 reinterpret_cast<intptr_t>(new_code->instruction_start()) +
1908 new_code->instruction_size(),
1909 new_code->instruction_size(),
1910 reinterpret_cast<intptr_t>(frame->pc()),
1911 reinterpret_cast<intptr_t>(new_code->instruction_start()) +
1912 delta + debug_break_slot_bytes);
1915 // Patch the return address to return into the code with
1916 // debug break slots.
1918 new_code->instruction_start() + delta + debug_break_slot_bytes);
1923 class ActiveFunctionsCollector : public ThreadVisitor {
1925 explicit ActiveFunctionsCollector(List<Handle<JSFunction> >* active_functions,
1926 Object* active_code_marker)
1927 : active_functions_(active_functions),
1928 active_code_marker_(active_code_marker) { }
1930 void VisitThread(Isolate* isolate, ThreadLocalTop* top) {
1931 CollectActiveFunctionsFromThread(isolate,
1934 active_code_marker_);
1938 List<Handle<JSFunction> >* active_functions_;
1939 Object* active_code_marker_;
1943 class ActiveFunctionsRedirector : public ThreadVisitor {
1945 void VisitThread(Isolate* isolate, ThreadLocalTop* top) {
1946 RedirectActivationsToRecompiledCodeOnThread(isolate, top);
1951 void Debug::PrepareForBreakPoints() {
1952 // If preparing for the first break point make sure to deoptimize all
1953 // functions as debugging does not work with optimized code.
1954 if (!has_break_points_) {
1955 Deoptimizer::DeoptimizeAll();
1957 Handle<Code> lazy_compile =
1958 Handle<Code>(isolate_->builtins()->builtin(Builtins::kLazyCompile));
1960 // Keep the list of activated functions in a handlified list as it
1961 // is used both in GC and non-GC code.
1962 List<Handle<JSFunction> > active_functions(100);
1965 // We are going to iterate heap to find all functions without
1966 // debug break slots.
1967 isolate_->heap()->CollectAllGarbage(Heap::kMakeHeapIterableMask,
1968 "preparing for breakpoints");
1970 // Ensure no GC in this scope as we are going to use gc_metadata
1971 // field in the Code object to mark active functions.
1972 AssertNoAllocation no_allocation;
1974 Object* active_code_marker = isolate_->heap()->the_hole_value();
1976 CollectActiveFunctionsFromThread(isolate_,
1977 isolate_->thread_local_top(),
1979 active_code_marker);
1980 ActiveFunctionsCollector active_functions_collector(&active_functions,
1981 active_code_marker);
1982 isolate_->thread_manager()->IterateArchivedThreads(
1983 &active_functions_collector);
1985 // Scan the heap for all non-optimized functions which have no
1986 // debug break slots and are not active or inlined into an active
1987 // function and mark them for lazy compilation.
1988 HeapIterator iterator;
1989 HeapObject* obj = NULL;
1990 while (((obj = iterator.next()) != NULL)) {
1991 if (obj->IsJSFunction()) {
1992 JSFunction* function = JSFunction::cast(obj);
1993 SharedFunctionInfo* shared = function->shared();
1994 if (shared->allows_lazy_compilation() &&
1995 shared->script()->IsScript() &&
1996 function->code()->kind() == Code::FUNCTION &&
1997 !function->code()->has_debug_break_slots() &&
1998 shared->code()->gc_metadata() != active_code_marker) {
1999 function->set_code(*lazy_compile);
2000 function->shared()->set_code(*lazy_compile);
2005 // Clear gc_metadata field.
2006 for (int i = 0; i < active_functions.length(); i++) {
2007 Handle<JSFunction> function = active_functions[i];
2008 function->shared()->code()->set_gc_metadata(Smi::FromInt(0));
2012 // Now recompile all functions with activation frames and and
2013 // patch the return address to run in the new compiled code.
2014 for (int i = 0; i < active_functions.length(); i++) {
2015 Handle<JSFunction> function = active_functions[i];
2017 if (function->code()->kind() == Code::FUNCTION &&
2018 function->code()->has_debug_break_slots()) {
2019 // Nothing to do. Function code already had debug break slots.
2023 Handle<SharedFunctionInfo> shared(function->shared());
2024 // If recompilation is not possible just skip it.
2025 if (shared->is_toplevel() ||
2026 !shared->allows_lazy_compilation() ||
2027 shared->code()->kind() == Code::BUILTIN) {
2031 // Make sure that the shared full code is compiled with debug
2033 if (!shared->code()->has_debug_break_slots()) {
2034 // Try to compile the full code with debug break slots. If it
2035 // fails just keep the current code.
2036 Handle<Code> current_code(function->shared()->code());
2037 ZoneScope zone_scope(isolate_, DELETE_ON_EXIT);
2038 shared->set_code(*lazy_compile);
2039 bool prev_force_debugger_active =
2040 isolate_->debugger()->force_debugger_active();
2041 isolate_->debugger()->set_force_debugger_active(true);
2042 ASSERT(current_code->kind() == Code::FUNCTION);
2043 CompileFullCodeForDebugging(shared, current_code);
2044 isolate_->debugger()->set_force_debugger_active(
2045 prev_force_debugger_active);
2046 if (!shared->is_compiled()) {
2047 shared->set_code(*current_code);
2052 // Keep function code in sync with shared function info.
2053 function->set_code(shared->code());
2056 RedirectActivationsToRecompiledCodeOnThread(isolate_,
2057 isolate_->thread_local_top());
2059 ActiveFunctionsRedirector active_functions_redirector;
2060 isolate_->thread_manager()->IterateArchivedThreads(
2061 &active_functions_redirector);
2066 // Ensures the debug information is present for shared.
2067 bool Debug::EnsureDebugInfo(Handle<SharedFunctionInfo> shared) {
2068 // Return if we already have the debug info for shared.
2069 if (HasDebugInfo(shared)) {
2070 ASSERT(shared->is_compiled());
2074 // Ensure shared in compiled. Return false if this failed.
2075 if (!SharedFunctionInfo::EnsureCompiled(shared, CLEAR_EXCEPTION)) {
2079 // Create the debug info object.
2080 Handle<DebugInfo> debug_info = FACTORY->NewDebugInfo(shared);
2082 // Add debug info to the list.
2083 DebugInfoListNode* node = new DebugInfoListNode(*debug_info);
2084 node->set_next(debug_info_list_);
2085 debug_info_list_ = node;
2087 // Now there is at least one break point.
2088 has_break_points_ = true;
2094 void Debug::RemoveDebugInfo(Handle<DebugInfo> debug_info) {
2095 ASSERT(debug_info_list_ != NULL);
2096 // Run through the debug info objects to find this one and remove it.
2097 DebugInfoListNode* prev = NULL;
2098 DebugInfoListNode* current = debug_info_list_;
2099 while (current != NULL) {
2100 if (*current->debug_info() == *debug_info) {
2101 // Unlink from list. If prev is NULL we are looking at the first element.
2103 debug_info_list_ = current->next();
2105 prev->set_next(current->next());
2107 current->debug_info()->shared()->set_debug_info(
2108 isolate_->heap()->undefined_value());
2111 // If there are no more debug info objects there are not more break
2113 has_break_points_ = debug_info_list_ != NULL;
2117 // Move to next in list.
2119 current = current->next();
2125 void Debug::SetAfterBreakTarget(JavaScriptFrame* frame) {
2126 HandleScope scope(isolate_);
2128 PrepareForBreakPoints();
2130 // Get the executing function in which the debug break occurred.
2131 Handle<SharedFunctionInfo> shared =
2132 Handle<SharedFunctionInfo>(JSFunction::cast(frame->function())->shared());
2133 if (!EnsureDebugInfo(shared)) {
2134 // Return if we failed to retrieve the debug info.
2137 Handle<DebugInfo> debug_info = GetDebugInfo(shared);
2138 Handle<Code> code(debug_info->code());
2139 Handle<Code> original_code(debug_info->original_code());
2141 // Get the code which is actually executing.
2142 Handle<Code> frame_code(frame->LookupCode());
2143 ASSERT(frame_code.is_identical_to(code));
2146 // Find the call address in the running code. This address holds the call to
2147 // either a DebugBreakXXX or to the debug break return entry code if the
2148 // break point is still active after processing the break point.
2149 Address addr = frame->pc() - Assembler::kCallTargetAddressOffset;
2151 // Check if the location is at JS exit or debug break slot.
2152 bool at_js_return = false;
2153 bool break_at_js_return_active = false;
2154 bool at_debug_break_slot = false;
2155 RelocIterator it(debug_info->code());
2156 while (!it.done() && !at_js_return && !at_debug_break_slot) {
2157 if (RelocInfo::IsJSReturn(it.rinfo()->rmode())) {
2158 at_js_return = (it.rinfo()->pc() ==
2159 addr - Assembler::kPatchReturnSequenceAddressOffset);
2160 break_at_js_return_active = it.rinfo()->IsPatchedReturnSequence();
2162 if (RelocInfo::IsDebugBreakSlot(it.rinfo()->rmode())) {
2163 at_debug_break_slot = (it.rinfo()->pc() ==
2164 addr - Assembler::kPatchDebugBreakSlotAddressOffset);
2169 // Handle the jump to continue execution after break point depending on the
2172 // If the break point as return is still active jump to the corresponding
2173 // place in the original code. If not the break point was removed during
2174 // break point processing.
2175 if (break_at_js_return_active) {
2176 addr += original_code->instruction_start() - code->instruction_start();
2179 // Move back to where the call instruction sequence started.
2180 thread_local_.after_break_target_ =
2181 addr - Assembler::kPatchReturnSequenceAddressOffset;
2182 } else if (at_debug_break_slot) {
2183 // Address of where the debug break slot starts.
2184 addr = addr - Assembler::kPatchDebugBreakSlotAddressOffset;
2186 // Continue just after the slot.
2187 thread_local_.after_break_target_ = addr + Assembler::kDebugBreakSlotLength;
2188 } else if (IsDebugBreak(Assembler::target_address_at(addr))) {
2189 // We now know that there is still a debug break call at the target address,
2190 // so the break point is still there and the original code will hold the
2191 // address to jump to in order to complete the call which is replaced by a
2192 // call to DebugBreakXXX.
2194 // Find the corresponding address in the original code.
2195 addr += original_code->instruction_start() - code->instruction_start();
2197 // Install jump to the call address in the original code. This will be the
2198 // call which was overwritten by the call to DebugBreakXXX.
2199 thread_local_.after_break_target_ = Assembler::target_address_at(addr);
2201 // There is no longer a break point present. Don't try to look in the
2202 // original code as the running code will have the right address. This takes
2203 // care of the case where the last break point is removed from the function
2204 // and therefore no "original code" is available.
2205 thread_local_.after_break_target_ = Assembler::target_address_at(addr);
2210 bool Debug::IsBreakAtReturn(JavaScriptFrame* frame) {
2211 HandleScope scope(isolate_);
2213 // If there are no break points this cannot be break at return, as
2214 // the debugger statement and stack guard bebug break cannot be at
2216 if (!has_break_points_) {
2220 PrepareForBreakPoints();
2222 // Get the executing function in which the debug break occurred.
2223 Handle<SharedFunctionInfo> shared =
2224 Handle<SharedFunctionInfo>(JSFunction::cast(frame->function())->shared());
2225 if (!EnsureDebugInfo(shared)) {
2226 // Return if we failed to retrieve the debug info.
2229 Handle<DebugInfo> debug_info = GetDebugInfo(shared);
2230 Handle<Code> code(debug_info->code());
2232 // Get the code which is actually executing.
2233 Handle<Code> frame_code(frame->LookupCode());
2234 ASSERT(frame_code.is_identical_to(code));
2237 // Find the call address in the running code.
2238 Address addr = frame->pc() - Assembler::kCallTargetAddressOffset;
2240 // Check if the location is at JS return.
2241 RelocIterator it(debug_info->code());
2242 while (!it.done()) {
2243 if (RelocInfo::IsJSReturn(it.rinfo()->rmode())) {
2244 return (it.rinfo()->pc() ==
2245 addr - Assembler::kPatchReturnSequenceAddressOffset);
2253 void Debug::FramesHaveBeenDropped(StackFrame::Id new_break_frame_id,
2255 Object** restarter_frame_function_pointer) {
2256 thread_local_.frame_drop_mode_ = mode;
2257 thread_local_.break_frame_id_ = new_break_frame_id;
2258 thread_local_.restarter_frame_function_pointer_ =
2259 restarter_frame_function_pointer;
2263 const int Debug::FramePaddingLayout::kInitialSize = 1;
2266 // Any even value bigger than kInitialSize as needed for stack scanning.
2267 const int Debug::FramePaddingLayout::kPaddingValue = kInitialSize + 1;
2270 bool Debug::IsDebugGlobal(GlobalObject* global) {
2271 return IsLoaded() && global == debug_context()->global();
2275 void Debug::ClearMirrorCache() {
2276 PostponeInterruptsScope postpone(isolate_);
2277 HandleScope scope(isolate_);
2278 ASSERT(isolate_->context() == *Debug::debug_context());
2280 // Clear the mirror cache.
2281 Handle<String> function_name =
2282 isolate_->factory()->LookupSymbol(CStrVector("ClearMirrorCache"));
2283 Handle<Object> fun(Isolate::Current()->global()->GetPropertyNoExceptionThrown(
2285 ASSERT(fun->IsJSFunction());
2286 bool caught_exception;
2287 Execution::TryCall(Handle<JSFunction>::cast(fun),
2288 Handle<JSObject>(Debug::debug_context()->global()),
2289 0, NULL, &caught_exception);
2293 void Debug::CreateScriptCache() {
2294 Heap* heap = isolate_->heap();
2295 HandleScope scope(isolate_);
2297 // Perform two GCs to get rid of all unreferenced scripts. The first GC gets
2298 // rid of all the cached script wrappers and the second gets rid of the
2299 // scripts which are no longer referenced. The second also sweeps precisely,
2300 // which saves us doing yet another GC to make the heap iterable.
2301 heap->CollectAllGarbage(Heap::kNoGCFlags, "Debug::CreateScriptCache");
2302 heap->CollectAllGarbage(Heap::kMakeHeapIterableMask,
2303 "Debug::CreateScriptCache");
2305 ASSERT(script_cache_ == NULL);
2306 script_cache_ = new ScriptCache();
2308 // Scan heap for Script objects.
2310 HeapIterator iterator;
2311 AssertNoAllocation no_allocation;
2313 for (HeapObject* obj = iterator.next(); obj != NULL; obj = iterator.next()) {
2314 if (obj->IsScript() && Script::cast(obj)->HasValidSource()) {
2315 script_cache_->Add(Handle<Script>(Script::cast(obj)));
2322 void Debug::DestroyScriptCache() {
2323 // Get rid of the script cache if it was created.
2324 if (script_cache_ != NULL) {
2325 delete script_cache_;
2326 script_cache_ = NULL;
2331 void Debug::AddScriptToScriptCache(Handle<Script> script) {
2332 if (script_cache_ != NULL) {
2333 script_cache_->Add(script);
2338 Handle<FixedArray> Debug::GetLoadedScripts() {
2339 // Create and fill the script cache when the loaded scripts is requested for
2341 if (script_cache_ == NULL) {
2342 CreateScriptCache();
2345 // If the script cache is not active just return an empty array.
2346 ASSERT(script_cache_ != NULL);
2347 if (script_cache_ == NULL) {
2348 isolate_->factory()->NewFixedArray(0);
2351 // Perform GC to get unreferenced scripts evicted from the cache before
2352 // returning the content.
2353 isolate_->heap()->CollectAllGarbage(Heap::kNoGCFlags,
2354 "Debug::GetLoadedScripts");
2356 // Get the scripts from the cache.
2357 return script_cache_->GetScripts();
2361 void Debug::AfterGarbageCollection() {
2362 // Generate events for collected scripts.
2363 if (script_cache_ != NULL) {
2364 script_cache_->ProcessCollectedScripts();
2369 Debugger::Debugger(Isolate* isolate)
2370 : debugger_access_(isolate->debugger_access()),
2371 event_listener_(Handle<Object>()),
2372 event_listener_data_(Handle<Object>()),
2373 compiling_natives_(false),
2374 is_loading_debugger_(false),
2375 never_unload_debugger_(false),
2376 force_debugger_active_(false),
2377 message_handler_(NULL),
2378 debugger_unload_pending_(false),
2379 host_dispatch_handler_(NULL),
2380 dispatch_handler_access_(OS::CreateMutex()),
2381 debug_message_dispatch_handler_(NULL),
2382 message_dispatch_helper_thread_(NULL),
2383 host_dispatch_micros_(100 * 1000),
2385 command_queue_(isolate->logger(), kQueueInitialSize),
2386 command_received_(OS::CreateSemaphore(0)),
2387 event_command_queue_(isolate->logger(), kQueueInitialSize),
2392 Debugger::~Debugger() {
2393 delete dispatch_handler_access_;
2394 dispatch_handler_access_ = 0;
2395 delete command_received_;
2396 command_received_ = 0;
2400 Handle<Object> Debugger::MakeJSObject(Vector<const char> constructor_name,
2402 Handle<Object> argv[],
2403 bool* caught_exception) {
2404 ASSERT(isolate_->context() == *isolate_->debug()->debug_context());
2406 // Create the execution state object.
2407 Handle<String> constructor_str =
2408 isolate_->factory()->LookupSymbol(constructor_name);
2409 Handle<Object> constructor(
2410 isolate_->global()->GetPropertyNoExceptionThrown(*constructor_str));
2411 ASSERT(constructor->IsJSFunction());
2412 if (!constructor->IsJSFunction()) {
2413 *caught_exception = true;
2414 return isolate_->factory()->undefined_value();
2416 Handle<Object> js_object = Execution::TryCall(
2417 Handle<JSFunction>::cast(constructor),
2418 Handle<JSObject>(isolate_->debug()->debug_context()->global()),
2426 Handle<Object> Debugger::MakeExecutionState(bool* caught_exception) {
2427 // Create the execution state object.
2428 Handle<Object> break_id = isolate_->factory()->NewNumberFromInt(
2429 isolate_->debug()->break_id());
2430 Handle<Object> argv[] = { break_id };
2431 return MakeJSObject(CStrVector("MakeExecutionState"),
2438 Handle<Object> Debugger::MakeBreakEvent(Handle<Object> exec_state,
2439 Handle<Object> break_points_hit,
2440 bool* caught_exception) {
2441 // Create the new break event object.
2442 Handle<Object> argv[] = { exec_state, break_points_hit };
2443 return MakeJSObject(CStrVector("MakeBreakEvent"),
2450 Handle<Object> Debugger::MakeExceptionEvent(Handle<Object> exec_state,
2451 Handle<Object> exception,
2453 bool* caught_exception) {
2454 Factory* factory = isolate_->factory();
2455 // Create the new exception event object.
2456 Handle<Object> argv[] = { exec_state,
2458 factory->ToBoolean(uncaught) };
2459 return MakeJSObject(CStrVector("MakeExceptionEvent"),
2466 Handle<Object> Debugger::MakeNewFunctionEvent(Handle<Object> function,
2467 bool* caught_exception) {
2468 // Create the new function event object.
2469 Handle<Object> argv[] = { function };
2470 return MakeJSObject(CStrVector("MakeNewFunctionEvent"),
2477 Handle<Object> Debugger::MakeCompileEvent(Handle<Script> script,
2479 bool* caught_exception) {
2480 Factory* factory = isolate_->factory();
2481 // Create the compile event object.
2482 Handle<Object> exec_state = MakeExecutionState(caught_exception);
2483 Handle<Object> script_wrapper = GetScriptWrapper(script);
2484 Handle<Object> argv[] = { exec_state,
2486 factory->ToBoolean(before) };
2487 return MakeJSObject(CStrVector("MakeCompileEvent"),
2494 Handle<Object> Debugger::MakeScriptCollectedEvent(int id,
2495 bool* caught_exception) {
2496 // Create the script collected event object.
2497 Handle<Object> exec_state = MakeExecutionState(caught_exception);
2498 Handle<Object> id_object = Handle<Smi>(Smi::FromInt(id));
2499 Handle<Object> argv[] = { exec_state, id_object };
2501 return MakeJSObject(CStrVector("MakeScriptCollectedEvent"),
2508 void Debugger::OnException(Handle<Object> exception, bool uncaught) {
2509 HandleScope scope(isolate_);
2510 Debug* debug = isolate_->debug();
2512 // Bail out based on state or if there is no listener for this event
2513 if (debug->InDebugger()) return;
2514 if (!Debugger::EventActive(v8::Exception)) return;
2516 // Bail out if exception breaks are not active
2518 // Uncaught exceptions are reported by either flags.
2519 if (!(debug->break_on_uncaught_exception() ||
2520 debug->break_on_exception())) return;
2522 // Caught exceptions are reported is activated.
2523 if (!debug->break_on_exception()) return;
2526 // Enter the debugger.
2527 EnterDebugger debugger;
2528 if (debugger.FailedToEnter()) return;
2530 // Clear all current stepping setup.
2531 debug->ClearStepping();
2532 // Create the event data object.
2533 bool caught_exception = false;
2534 Handle<Object> exec_state = MakeExecutionState(&caught_exception);
2535 Handle<Object> event_data;
2536 if (!caught_exception) {
2537 event_data = MakeExceptionEvent(exec_state, exception, uncaught,
2540 // Bail out and don't call debugger if exception.
2541 if (caught_exception) {
2545 // Process debug event.
2546 ProcessDebugEvent(v8::Exception, Handle<JSObject>::cast(event_data), false);
2547 // Return to continue execution from where the exception was thrown.
2551 void Debugger::OnDebugBreak(Handle<Object> break_points_hit,
2552 bool auto_continue) {
2553 HandleScope scope(isolate_);
2555 // Debugger has already been entered by caller.
2556 ASSERT(isolate_->context() == *isolate_->debug()->debug_context());
2558 // Bail out if there is no listener for this event
2559 if (!Debugger::EventActive(v8::Break)) return;
2561 // Debugger must be entered in advance.
2562 ASSERT(isolate_->context() == *isolate_->debug()->debug_context());
2564 // Create the event data object.
2565 bool caught_exception = false;
2566 Handle<Object> exec_state = MakeExecutionState(&caught_exception);
2567 Handle<Object> event_data;
2568 if (!caught_exception) {
2569 event_data = MakeBreakEvent(exec_state, break_points_hit,
2572 // Bail out and don't call debugger if exception.
2573 if (caught_exception) {
2577 // Process debug event.
2578 ProcessDebugEvent(v8::Break,
2579 Handle<JSObject>::cast(event_data),
2584 void Debugger::OnBeforeCompile(Handle<Script> script) {
2585 HandleScope scope(isolate_);
2587 // Bail out based on state or if there is no listener for this event
2588 if (isolate_->debug()->InDebugger()) return;
2589 if (compiling_natives()) return;
2590 if (!EventActive(v8::BeforeCompile)) return;
2592 // Enter the debugger.
2593 EnterDebugger debugger;
2594 if (debugger.FailedToEnter()) return;
2596 // Create the event data object.
2597 bool caught_exception = false;
2598 Handle<Object> event_data = MakeCompileEvent(script, true, &caught_exception);
2599 // Bail out and don't call debugger if exception.
2600 if (caught_exception) {
2604 // Process debug event.
2605 ProcessDebugEvent(v8::BeforeCompile,
2606 Handle<JSObject>::cast(event_data),
2611 // Handle debugger actions when a new script is compiled.
2612 void Debugger::OnAfterCompile(Handle<Script> script,
2613 AfterCompileFlags after_compile_flags) {
2614 HandleScope scope(isolate_);
2615 Debug* debug = isolate_->debug();
2617 // Add the newly compiled script to the script cache.
2618 debug->AddScriptToScriptCache(script);
2620 // No more to do if not debugging.
2621 if (!IsDebuggerActive()) return;
2623 // No compile events while compiling natives.
2624 if (compiling_natives()) return;
2626 // Store whether in debugger before entering debugger.
2627 bool in_debugger = debug->InDebugger();
2629 // Enter the debugger.
2630 EnterDebugger debugger;
2631 if (debugger.FailedToEnter()) return;
2633 // If debugging there might be script break points registered for this
2634 // script. Make sure that these break points are set.
2636 // Get the function UpdateScriptBreakPoints (defined in debug-debugger.js).
2637 Handle<String> update_script_break_points_symbol =
2638 isolate_->factory()->LookupAsciiSymbol("UpdateScriptBreakPoints");
2639 Handle<Object> update_script_break_points =
2640 Handle<Object>(debug->debug_context()->global()->
2641 GetPropertyNoExceptionThrown(*update_script_break_points_symbol));
2642 if (!update_script_break_points->IsJSFunction()) {
2645 ASSERT(update_script_break_points->IsJSFunction());
2647 // Wrap the script object in a proper JS object before passing it
2649 Handle<JSValue> wrapper = GetScriptWrapper(script);
2651 // Call UpdateScriptBreakPoints expect no exceptions.
2652 bool caught_exception;
2653 Handle<Object> argv[] = { wrapper };
2654 Execution::TryCall(Handle<JSFunction>::cast(update_script_break_points),
2655 Isolate::Current()->js_builtins_object(),
2659 if (caught_exception) {
2662 // Bail out based on state or if there is no listener for this event
2663 if (in_debugger && (after_compile_flags & SEND_WHEN_DEBUGGING) == 0) return;
2664 if (!Debugger::EventActive(v8::AfterCompile)) return;
2666 // Create the compile state object.
2667 Handle<Object> event_data = MakeCompileEvent(script,
2670 // Bail out and don't call debugger if exception.
2671 if (caught_exception) {
2674 // Process debug event.
2675 ProcessDebugEvent(v8::AfterCompile,
2676 Handle<JSObject>::cast(event_data),
2681 void Debugger::OnScriptCollected(int id) {
2682 HandleScope scope(isolate_);
2684 // No more to do if not debugging.
2685 if (!IsDebuggerActive()) return;
2686 if (!Debugger::EventActive(v8::ScriptCollected)) return;
2688 // Enter the debugger.
2689 EnterDebugger debugger;
2690 if (debugger.FailedToEnter()) return;
2692 // Create the script collected state object.
2693 bool caught_exception = false;
2694 Handle<Object> event_data = MakeScriptCollectedEvent(id,
2696 // Bail out and don't call debugger if exception.
2697 if (caught_exception) {
2701 // Process debug event.
2702 ProcessDebugEvent(v8::ScriptCollected,
2703 Handle<JSObject>::cast(event_data),
2708 void Debugger::ProcessDebugEvent(v8::DebugEvent event,
2709 Handle<JSObject> event_data,
2710 bool auto_continue) {
2711 HandleScope scope(isolate_);
2713 // Clear any pending debug break if this is a real break.
2714 if (!auto_continue) {
2715 isolate_->debug()->clear_interrupt_pending(DEBUGBREAK);
2718 // Create the execution state.
2719 bool caught_exception = false;
2720 Handle<Object> exec_state = MakeExecutionState(&caught_exception);
2721 if (caught_exception) {
2724 // First notify the message handler if any.
2725 if (message_handler_ != NULL) {
2726 NotifyMessageHandler(event,
2727 Handle<JSObject>::cast(exec_state),
2731 // Notify registered debug event listener. This can be either a C or
2732 // a JavaScript function. Don't call event listener for v8::Break
2733 // here, if it's only a debug command -- they will be processed later.
2734 if ((event != v8::Break || !auto_continue) && !event_listener_.is_null()) {
2735 CallEventCallback(event, exec_state, event_data, NULL);
2737 // Process pending debug commands.
2738 if (event == v8::Break) {
2739 while (!event_command_queue_.IsEmpty()) {
2740 CommandMessage command = event_command_queue_.Get();
2741 if (!event_listener_.is_null()) {
2742 CallEventCallback(v8::BreakForCommand,
2745 command.client_data());
2753 void Debugger::CallEventCallback(v8::DebugEvent event,
2754 Handle<Object> exec_state,
2755 Handle<Object> event_data,
2756 v8::Debug::ClientData* client_data) {
2757 if (event_listener_->IsForeign()) {
2758 CallCEventCallback(event, exec_state, event_data, client_data);
2760 CallJSEventCallback(event, exec_state, event_data);
2765 void Debugger::CallCEventCallback(v8::DebugEvent event,
2766 Handle<Object> exec_state,
2767 Handle<Object> event_data,
2768 v8::Debug::ClientData* client_data) {
2769 Handle<Foreign> callback_obj(Handle<Foreign>::cast(event_listener_));
2770 v8::Debug::EventCallback2 callback =
2771 FUNCTION_CAST<v8::Debug::EventCallback2>(
2772 callback_obj->foreign_address());
2773 EventDetailsImpl event_details(
2775 Handle<JSObject>::cast(exec_state),
2776 Handle<JSObject>::cast(event_data),
2777 event_listener_data_,
2779 callback(event_details);
2783 void Debugger::CallJSEventCallback(v8::DebugEvent event,
2784 Handle<Object> exec_state,
2785 Handle<Object> event_data) {
2786 ASSERT(event_listener_->IsJSFunction());
2787 Handle<JSFunction> fun(Handle<JSFunction>::cast(event_listener_));
2789 // Invoke the JavaScript debug event listener.
2790 Handle<Object> argv[] = { Handle<Object>(Smi::FromInt(event)),
2793 event_listener_data_ };
2794 bool caught_exception;
2795 Execution::TryCall(fun,
2800 // Silently ignore exceptions from debug event listeners.
2804 Handle<Context> Debugger::GetDebugContext() {
2805 never_unload_debugger_ = true;
2806 EnterDebugger debugger;
2807 return isolate_->debug()->debug_context();
2811 void Debugger::UnloadDebugger() {
2812 Debug* debug = isolate_->debug();
2814 // Make sure that there are no breakpoints left.
2815 debug->ClearAllBreakPoints();
2817 // Unload the debugger if feasible.
2818 if (!never_unload_debugger_) {
2822 // Clear the flag indicating that the debugger should be unloaded.
2823 debugger_unload_pending_ = false;
2827 void Debugger::NotifyMessageHandler(v8::DebugEvent event,
2828 Handle<JSObject> exec_state,
2829 Handle<JSObject> event_data,
2830 bool auto_continue) {
2831 HandleScope scope(isolate_);
2833 if (!isolate_->debug()->Load()) return;
2835 // Process the individual events.
2836 bool sendEventMessage = false;
2839 case v8::BreakForCommand:
2840 sendEventMessage = !auto_continue;
2843 sendEventMessage = true;
2845 case v8::BeforeCompile:
2847 case v8::AfterCompile:
2848 sendEventMessage = true;
2850 case v8::ScriptCollected:
2851 sendEventMessage = true;
2853 case v8::NewFunction:
2859 // The debug command interrupt flag might have been set when the command was
2860 // added. It should be enough to clear the flag only once while we are in the
2862 ASSERT(isolate_->debug()->InDebugger());
2863 isolate_->stack_guard()->Continue(DEBUGCOMMAND);
2865 // Notify the debugger that a debug event has occurred unless auto continue is
2866 // active in which case no event is send.
2867 if (sendEventMessage) {
2868 MessageImpl message = MessageImpl::NewEvent(
2871 Handle<JSObject>::cast(exec_state),
2872 Handle<JSObject>::cast(event_data));
2873 InvokeMessageHandler(message);
2876 // If auto continue don't make the event cause a break, but process messages
2877 // in the queue if any. For script collected events don't even process
2878 // messages in the queue as the execution state might not be what is expected
2880 if ((auto_continue && !HasCommands()) || event == v8::ScriptCollected) {
2884 v8::TryCatch try_catch;
2886 // DebugCommandProcessor goes here.
2887 v8::Local<v8::Object> cmd_processor;
2889 v8::Local<v8::Object> api_exec_state =
2890 v8::Utils::ToLocal(Handle<JSObject>::cast(exec_state));
2891 v8::Local<v8::String> fun_name =
2892 v8::String::New("debugCommandProcessor");
2893 v8::Local<v8::Function> fun =
2894 v8::Function::Cast(*api_exec_state->Get(fun_name));
2896 v8::Handle<v8::Boolean> running =
2897 auto_continue ? v8::True() : v8::False();
2898 static const int kArgc = 1;
2899 v8::Handle<Value> argv[kArgc] = { running };
2900 cmd_processor = v8::Object::Cast(*fun->Call(api_exec_state, kArgc, argv));
2901 if (try_catch.HasCaught()) {
2902 PrintLn(try_catch.Exception());
2907 bool running = auto_continue;
2909 // Process requests from the debugger.
2911 // Wait for new command in the queue.
2912 if (Debugger::host_dispatch_handler_) {
2913 // In case there is a host dispatch - do periodic dispatches.
2914 if (!command_received_->Wait(host_dispatch_micros_)) {
2915 // Timout expired, do the dispatch.
2916 Debugger::host_dispatch_handler_();
2920 // In case there is no host dispatch - just wait.
2921 command_received_->Wait();
2924 // Get the command from the queue.
2925 CommandMessage command = command_queue_.Get();
2926 isolate_->logger()->DebugTag(
2927 "Got request from command queue, in interactive loop.");
2928 if (!Debugger::IsDebuggerActive()) {
2929 // Delete command text and user data.
2934 // Invoke JavaScript to process the debug request.
2935 v8::Local<v8::String> fun_name;
2936 v8::Local<v8::Function> fun;
2937 v8::Local<v8::Value> request;
2938 v8::TryCatch try_catch;
2939 fun_name = v8::String::New("processDebugRequest");
2940 fun = v8::Function::Cast(*cmd_processor->Get(fun_name));
2942 request = v8::String::New(command.text().start(),
2943 command.text().length());
2944 static const int kArgc = 1;
2945 v8::Handle<Value> argv[kArgc] = { request };
2946 v8::Local<v8::Value> response_val = fun->Call(cmd_processor, kArgc, argv);
2948 // Get the response.
2949 v8::Local<v8::String> response;
2950 if (!try_catch.HasCaught()) {
2951 // Get response string.
2952 if (!response_val->IsUndefined()) {
2953 response = v8::String::Cast(*response_val);
2955 response = v8::String::New("");
2958 // Log the JSON request/response.
2959 if (FLAG_trace_debug_json) {
2964 // Get the running state.
2965 fun_name = v8::String::New("isRunning");
2966 fun = v8::Function::Cast(*cmd_processor->Get(fun_name));
2967 static const int kArgc = 1;
2968 v8::Handle<Value> argv[kArgc] = { response };
2969 v8::Local<v8::Value> running_val = fun->Call(cmd_processor, kArgc, argv);
2970 if (!try_catch.HasCaught()) {
2971 running = running_val->ToBoolean()->Value();
2974 // In case of failure the result text is the exception text.
2975 response = try_catch.Exception()->ToString();
2978 // Return the result.
2979 MessageImpl message = MessageImpl::NewResponse(
2982 Handle<JSObject>::cast(exec_state),
2983 Handle<JSObject>::cast(event_data),
2984 Handle<String>(Utils::OpenHandle(*response)),
2985 command.client_data());
2986 InvokeMessageHandler(message);
2989 // Return from debug event processing if either the VM is put into the
2990 // running state (through a continue command) or auto continue is active
2991 // and there are no more commands queued.
2992 if (running && !HasCommands()) {
2999 void Debugger::SetEventListener(Handle<Object> callback,
3000 Handle<Object> data) {
3001 HandleScope scope(isolate_);
3002 GlobalHandles* global_handles = isolate_->global_handles();
3004 // Clear the global handles for the event listener and the event listener data
3006 if (!event_listener_.is_null()) {
3007 global_handles->Destroy(
3008 reinterpret_cast<Object**>(event_listener_.location()));
3009 event_listener_ = Handle<Object>();
3011 if (!event_listener_data_.is_null()) {
3012 global_handles->Destroy(
3013 reinterpret_cast<Object**>(event_listener_data_.location()));
3014 event_listener_data_ = Handle<Object>();
3017 // If there is a new debug event listener register it together with its data
3019 if (!callback->IsUndefined() && !callback->IsNull()) {
3020 event_listener_ = Handle<Object>::cast(
3021 global_handles->Create(*callback));
3022 if (data.is_null()) {
3023 data = isolate_->factory()->undefined_value();
3025 event_listener_data_ = Handle<Object>::cast(
3026 global_handles->Create(*data));
3033 void Debugger::SetMessageHandler(v8::Debug::MessageHandler2 handler) {
3034 ScopedLock with(debugger_access_);
3036 message_handler_ = handler;
3038 if (handler == NULL) {
3039 // Send an empty command to the debugger if in a break to make JavaScript
3040 // run again if the debugger is closed.
3041 if (isolate_->debug()->InDebugger()) {
3042 ProcessCommand(Vector<const uint16_t>::empty());
3048 void Debugger::ListenersChanged() {
3049 if (IsDebuggerActive()) {
3050 // Disable the compilation cache when the debugger is active.
3051 isolate_->compilation_cache()->Disable();
3052 debugger_unload_pending_ = false;
3054 isolate_->compilation_cache()->Enable();
3055 // Unload the debugger if event listener and message handler cleared.
3056 // Schedule this for later, because we may be in non-V8 thread.
3057 debugger_unload_pending_ = true;
3062 void Debugger::SetHostDispatchHandler(v8::Debug::HostDispatchHandler handler,
3064 host_dispatch_handler_ = handler;
3065 host_dispatch_micros_ = period * 1000;
3069 void Debugger::SetDebugMessageDispatchHandler(
3070 v8::Debug::DebugMessageDispatchHandler handler, bool provide_locker) {
3071 ScopedLock with(dispatch_handler_access_);
3072 debug_message_dispatch_handler_ = handler;
3074 if (provide_locker && message_dispatch_helper_thread_ == NULL) {
3075 message_dispatch_helper_thread_ = new MessageDispatchHelperThread(isolate_);
3076 message_dispatch_helper_thread_->Start();
3081 // Calls the registered debug message handler. This callback is part of the
3083 void Debugger::InvokeMessageHandler(MessageImpl message) {
3084 ScopedLock with(debugger_access_);
3086 if (message_handler_ != NULL) {
3087 message_handler_(message);
3092 // Puts a command coming from the public API on the queue. Creates
3093 // a copy of the command string managed by the debugger. Up to this
3094 // point, the command data was managed by the API client. Called
3095 // by the API client thread.
3096 void Debugger::ProcessCommand(Vector<const uint16_t> command,
3097 v8::Debug::ClientData* client_data) {
3098 // Need to cast away const.
3099 CommandMessage message = CommandMessage::New(
3100 Vector<uint16_t>(const_cast<uint16_t*>(command.start()),
3103 isolate_->logger()->DebugTag("Put command on command_queue.");
3104 command_queue_.Put(message);
3105 command_received_->Signal();
3107 // Set the debug command break flag to have the command processed.
3108 if (!isolate_->debug()->InDebugger()) {
3109 isolate_->stack_guard()->DebugCommand();
3112 MessageDispatchHelperThread* dispatch_thread;
3114 ScopedLock with(dispatch_handler_access_);
3115 dispatch_thread = message_dispatch_helper_thread_;
3118 if (dispatch_thread == NULL) {
3119 CallMessageDispatchHandler();
3121 dispatch_thread->Schedule();
3126 bool Debugger::HasCommands() {
3127 return !command_queue_.IsEmpty();
3131 void Debugger::EnqueueDebugCommand(v8::Debug::ClientData* client_data) {
3132 CommandMessage message = CommandMessage::New(Vector<uint16_t>(), client_data);
3133 event_command_queue_.Put(message);
3135 // Set the debug command break flag to have the command processed.
3136 if (!isolate_->debug()->InDebugger()) {
3137 isolate_->stack_guard()->DebugCommand();
3142 bool Debugger::IsDebuggerActive() {
3143 ScopedLock with(debugger_access_);
3145 return message_handler_ != NULL ||
3146 !event_listener_.is_null() ||
3147 force_debugger_active_;
3151 Handle<Object> Debugger::Call(Handle<JSFunction> fun,
3152 Handle<Object> data,
3153 bool* pending_exception) {
3154 // When calling functions in the debugger prevent it from beeing unloaded.
3155 Debugger::never_unload_debugger_ = true;
3157 // Enter the debugger.
3158 EnterDebugger debugger;
3159 if (debugger.FailedToEnter()) {
3160 return isolate_->factory()->undefined_value();
3163 // Create the execution state.
3164 bool caught_exception = false;
3165 Handle<Object> exec_state = MakeExecutionState(&caught_exception);
3166 if (caught_exception) {
3167 return isolate_->factory()->undefined_value();
3170 Handle<Object> argv[] = { exec_state, data };
3171 Handle<Object> result = Execution::Call(
3173 Handle<Object>(isolate_->debug()->debug_context_->global_proxy()),
3181 static void StubMessageHandler2(const v8::Debug::Message& message) {
3182 // Simply ignore message.
3186 bool Debugger::StartAgent(const char* name, int port,
3187 bool wait_for_connection) {
3188 ASSERT(Isolate::Current() == isolate_);
3189 if (wait_for_connection) {
3190 // Suspend V8 if it is already running or set V8 to suspend whenever
3192 // Provide stub message handler; V8 auto-continues each suspend
3193 // when there is no message handler; we doesn't need it.
3194 // Once become suspended, V8 will stay so indefinitely long, until remote
3195 // debugger connects and issues "continue" command.
3196 Debugger::message_handler_ = StubMessageHandler2;
3197 v8::Debug::DebugBreak();
3200 if (Socket::SetUp()) {
3201 if (agent_ == NULL) {
3202 agent_ = new DebuggerAgent(name, port);
3212 void Debugger::StopAgent() {
3213 ASSERT(Isolate::Current() == isolate_);
3214 if (agent_ != NULL) {
3223 void Debugger::WaitForAgent() {
3224 ASSERT(Isolate::Current() == isolate_);
3226 agent_->WaitUntilListening();
3230 void Debugger::CallMessageDispatchHandler() {
3231 v8::Debug::DebugMessageDispatchHandler handler;
3233 ScopedLock with(dispatch_handler_access_);
3234 handler = Debugger::debug_message_dispatch_handler_;
3236 if (handler != NULL) {
3242 EnterDebugger::EnterDebugger()
3243 : isolate_(Isolate::Current()),
3244 prev_(isolate_->debug()->debugger_entry()),
3246 has_js_frames_(!it_.done()),
3248 Debug* debug = isolate_->debug();
3249 ASSERT(prev_ != NULL || !debug->is_interrupt_pending(PREEMPT));
3250 ASSERT(prev_ != NULL || !debug->is_interrupt_pending(DEBUGBREAK));
3252 // Link recursive debugger entry.
3253 debug->set_debugger_entry(this);
3255 // Store the previous break id and frame id.
3256 break_id_ = debug->break_id();
3257 break_frame_id_ = debug->break_frame_id();
3259 // Create the new break info. If there is no JavaScript frames there is no
3261 if (has_js_frames_) {
3262 debug->NewBreak(it_.frame()->id());
3264 debug->NewBreak(StackFrame::NO_ID);
3267 // Make sure that debugger is loaded and enter the debugger context.
3268 load_failed_ = !debug->Load();
3269 if (!load_failed_) {
3270 // NOTE the member variable save which saves the previous context before
3272 isolate_->set_context(*debug->debug_context());
3277 EnterDebugger::~EnterDebugger() {
3278 ASSERT(Isolate::Current() == isolate_);
3279 Debug* debug = isolate_->debug();
3281 // Restore to the previous break state.
3282 debug->SetBreak(break_frame_id_, break_id_);
3284 // Check for leaving the debugger.
3285 if (!load_failed_ && prev_ == NULL) {
3286 // Clear mirror cache when leaving the debugger. Skip this if there is a
3287 // pending exception as clearing the mirror cache calls back into
3288 // JavaScript. This can happen if the v8::Debug::Call is used in which
3289 // case the exception should end up in the calling code.
3290 if (!isolate_->has_pending_exception()) {
3291 // Try to avoid any pending debug break breaking in the clear mirror
3292 // cache JavaScript code.
3293 if (isolate_->stack_guard()->IsDebugBreak()) {
3294 debug->set_interrupts_pending(DEBUGBREAK);
3295 isolate_->stack_guard()->Continue(DEBUGBREAK);
3297 debug->ClearMirrorCache();
3300 // Request preemption and debug break when leaving the last debugger entry
3301 // if any of these where recorded while debugging.
3302 if (debug->is_interrupt_pending(PREEMPT)) {
3303 // This re-scheduling of preemption is to avoid starvation in some
3304 // debugging scenarios.
3305 debug->clear_interrupt_pending(PREEMPT);
3306 isolate_->stack_guard()->Preempt();
3308 if (debug->is_interrupt_pending(DEBUGBREAK)) {
3309 debug->clear_interrupt_pending(DEBUGBREAK);
3310 isolate_->stack_guard()->DebugBreak();
3313 // If there are commands in the queue when leaving the debugger request
3314 // that these commands are processed.
3315 if (isolate_->debugger()->HasCommands()) {
3316 isolate_->stack_guard()->DebugCommand();
3319 // If leaving the debugger with the debugger no longer active unload it.
3320 if (!isolate_->debugger()->IsDebuggerActive()) {
3321 isolate_->debugger()->UnloadDebugger();
3325 // Leaving this debugger entry.
3326 debug->set_debugger_entry(prev_);
3330 MessageImpl MessageImpl::NewEvent(DebugEvent event,
3332 Handle<JSObject> exec_state,
3333 Handle<JSObject> event_data) {
3334 MessageImpl message(true, event, running,
3335 exec_state, event_data, Handle<String>(), NULL);
3340 MessageImpl MessageImpl::NewResponse(DebugEvent event,
3342 Handle<JSObject> exec_state,
3343 Handle<JSObject> event_data,
3344 Handle<String> response_json,
3345 v8::Debug::ClientData* client_data) {
3346 MessageImpl message(false, event, running,
3347 exec_state, event_data, response_json, client_data);
3352 MessageImpl::MessageImpl(bool is_event,
3355 Handle<JSObject> exec_state,
3356 Handle<JSObject> event_data,
3357 Handle<String> response_json,
3358 v8::Debug::ClientData* client_data)
3359 : is_event_(is_event),
3362 exec_state_(exec_state),
3363 event_data_(event_data),
3364 response_json_(response_json),
3365 client_data_(client_data) {}
3368 bool MessageImpl::IsEvent() const {
3373 bool MessageImpl::IsResponse() const {
3378 DebugEvent MessageImpl::GetEvent() const {
3383 bool MessageImpl::WillStartRunning() const {
3388 v8::Handle<v8::Object> MessageImpl::GetExecutionState() const {
3389 return v8::Utils::ToLocal(exec_state_);
3393 v8::Handle<v8::Object> MessageImpl::GetEventData() const {
3394 return v8::Utils::ToLocal(event_data_);
3398 v8::Handle<v8::String> MessageImpl::GetJSON() const {
3399 v8::HandleScope scope;
3402 // Call toJSONProtocol on the debug event object.
3403 Handle<Object> fun = GetProperty(event_data_, "toJSONProtocol");
3404 if (!fun->IsJSFunction()) {
3405 return v8::Handle<v8::String>();
3407 bool caught_exception;
3408 Handle<Object> json = Execution::TryCall(Handle<JSFunction>::cast(fun),
3410 0, NULL, &caught_exception);
3411 if (caught_exception || !json->IsString()) {
3412 return v8::Handle<v8::String>();
3414 return scope.Close(v8::Utils::ToLocal(Handle<String>::cast(json)));
3416 return v8::Utils::ToLocal(response_json_);
3421 v8::Handle<v8::Context> MessageImpl::GetEventContext() const {
3422 Isolate* isolate = Isolate::Current();
3423 v8::Handle<v8::Context> context = GetDebugEventContext(isolate);
3424 // Isolate::context() may be NULL when "script collected" event occures.
3425 ASSERT(!context.IsEmpty() || event_ == v8::ScriptCollected);
3430 v8::Debug::ClientData* MessageImpl::GetClientData() const {
3431 return client_data_;
3435 EventDetailsImpl::EventDetailsImpl(DebugEvent event,
3436 Handle<JSObject> exec_state,
3437 Handle<JSObject> event_data,
3438 Handle<Object> callback_data,
3439 v8::Debug::ClientData* client_data)
3441 exec_state_(exec_state),
3442 event_data_(event_data),
3443 callback_data_(callback_data),
3444 client_data_(client_data) {}
3447 DebugEvent EventDetailsImpl::GetEvent() const {
3452 v8::Handle<v8::Object> EventDetailsImpl::GetExecutionState() const {
3453 return v8::Utils::ToLocal(exec_state_);
3457 v8::Handle<v8::Object> EventDetailsImpl::GetEventData() const {
3458 return v8::Utils::ToLocal(event_data_);
3462 v8::Handle<v8::Context> EventDetailsImpl::GetEventContext() const {
3463 return GetDebugEventContext(Isolate::Current());
3467 v8::Handle<v8::Value> EventDetailsImpl::GetCallbackData() const {
3468 return v8::Utils::ToLocal(callback_data_);
3472 v8::Debug::ClientData* EventDetailsImpl::GetClientData() const {
3473 return client_data_;
3477 CommandMessage::CommandMessage() : text_(Vector<uint16_t>::empty()),
3478 client_data_(NULL) {
3482 CommandMessage::CommandMessage(const Vector<uint16_t>& text,
3483 v8::Debug::ClientData* data)
3485 client_data_(data) {
3489 CommandMessage::~CommandMessage() {
3493 void CommandMessage::Dispose() {
3495 delete client_data_;
3496 client_data_ = NULL;
3500 CommandMessage CommandMessage::New(const Vector<uint16_t>& command,
3501 v8::Debug::ClientData* data) {
3502 return CommandMessage(command.Clone(), data);
3506 CommandMessageQueue::CommandMessageQueue(int size) : start_(0), end_(0),
3508 messages_ = NewArray<CommandMessage>(size);
3512 CommandMessageQueue::~CommandMessageQueue() {
3513 while (!IsEmpty()) {
3514 CommandMessage m = Get();
3517 DeleteArray(messages_);
3521 CommandMessage CommandMessageQueue::Get() {
3523 int result = start_;
3524 start_ = (start_ + 1) % size_;
3525 return messages_[result];
3529 void CommandMessageQueue::Put(const CommandMessage& message) {
3530 if ((end_ + 1) % size_ == start_) {
3533 messages_[end_] = message;
3534 end_ = (end_ + 1) % size_;
3538 void CommandMessageQueue::Expand() {
3539 CommandMessageQueue new_queue(size_ * 2);
3540 while (!IsEmpty()) {
3541 new_queue.Put(Get());
3543 CommandMessage* array_to_free = messages_;
3545 new_queue.messages_ = array_to_free;
3546 // Make the new_queue empty so that it doesn't call Dispose on any messages.
3547 new_queue.start_ = new_queue.end_;
3548 // Automatic destructor called on new_queue, freeing array_to_free.
3552 LockingCommandMessageQueue::LockingCommandMessageQueue(Logger* logger, int size)
3553 : logger_(logger), queue_(size) {
3554 lock_ = OS::CreateMutex();
3558 LockingCommandMessageQueue::~LockingCommandMessageQueue() {
3563 bool LockingCommandMessageQueue::IsEmpty() const {
3564 ScopedLock sl(lock_);
3565 return queue_.IsEmpty();
3569 CommandMessage LockingCommandMessageQueue::Get() {
3570 ScopedLock sl(lock_);
3571 CommandMessage result = queue_.Get();
3572 logger_->DebugEvent("Get", result.text());
3577 void LockingCommandMessageQueue::Put(const CommandMessage& message) {
3578 ScopedLock sl(lock_);
3579 queue_.Put(message);
3580 logger_->DebugEvent("Put", message.text());
3584 void LockingCommandMessageQueue::Clear() {
3585 ScopedLock sl(lock_);
3590 MessageDispatchHelperThread::MessageDispatchHelperThread(Isolate* isolate)
3591 : Thread("v8:MsgDispHelpr"),
3592 sem_(OS::CreateSemaphore(0)), mutex_(OS::CreateMutex()),
3593 already_signalled_(false) {
3597 MessageDispatchHelperThread::~MessageDispatchHelperThread() {
3603 void MessageDispatchHelperThread::Schedule() {
3605 ScopedLock lock(mutex_);
3606 if (already_signalled_) {
3609 already_signalled_ = true;
3615 void MessageDispatchHelperThread::Run() {
3619 ScopedLock lock(mutex_);
3620 already_signalled_ = false;
3624 Isolate::Current()->debugger()->CallMessageDispatchHandler();
3629 #endif // ENABLE_DEBUGGER_SUPPORT
3631 } } // namespace v8::internal