[V8] Introduce a QML compilation mode
[profile/ivi/qtjsbackend.git] / src / 3rdparty / v8 / src / debug.cc
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
4 // met:
5 //
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.
15 //
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.
27
28 #include "v8.h"
29
30 #include "api.h"
31 #include "arguments.h"
32 #include "bootstrapper.h"
33 #include "code-stubs.h"
34 #include "codegen.h"
35 #include "compilation-cache.h"
36 #include "compiler.h"
37 #include "debug.h"
38 #include "deoptimizer.h"
39 #include "execution.h"
40 #include "full-codegen.h"
41 #include "global-handles.h"
42 #include "ic.h"
43 #include "ic-inl.h"
44 #include "isolate-inl.h"
45 #include "list.h"
46 #include "messages.h"
47 #include "natives.h"
48 #include "stub-cache.h"
49 #include "log.h"
50
51 #include "../include/v8-debug.h"
52
53 namespace v8 {
54 namespace internal {
55
56 #ifdef ENABLE_DEBUGGER_SUPPORT
57
58
59 Debug::Debug(Isolate* isolate)
60     : has_break_points_(false),
61       script_cache_(NULL),
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),
68       isolate_(isolate) {
69   memset(registers_, 0, sizeof(JSCallerSavedBuffer));
70 }
71
72
73 Debug::~Debug() {
74 }
75
76
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");
82     return;
83   }
84   s->WriteAscii(data.start());
85   PrintF("%s\n", data.start());
86 }
87
88
89 static Handle<Code> ComputeCallDebugPrepareStepIn(int argc, Code::Kind kind) {
90   Isolate* isolate = Isolate::Current();
91   return isolate->stub_cache()->ComputeCallDebugPrepareStepIn(argc, kind);
92 }
93
94
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
98   // occured.
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);
102 }
103
104
105 BreakLocationIterator::BreakLocationIterator(Handle<DebugInfo> debug_info,
106                                              BreakLocatorType type) {
107   debug_info_ = debug_info;
108   type_ = type;
109   reloc_iterator_ = NULL;
110   reloc_iterator_original_ = NULL;
111   Reset();  // Initialize the rest of the member variables.
112 }
113
114
115 BreakLocationIterator::~BreakLocationIterator() {
116   ASSERT(reloc_iterator_ != NULL);
117   ASSERT(reloc_iterator_original_ != NULL);
118   delete reloc_iterator_;
119   delete reloc_iterator_original_;
120 }
121
122
123 void BreakLocationIterator::Next() {
124   AssertNoAllocation nogc;
125   ASSERT(!RinfoDone());
126
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();
132     first = false;
133     if (RinfoDone()) return;
134
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());
141       }
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);
148     }
149
150     if (IsDebugBreakSlot()) {
151       // There is always a possible break point at a debug break slot.
152       break_point_++;
153       return;
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())) {
166         break_point_++;
167         return;
168       }
169       if (code->kind() == Code::STUB) {
170         if (IsDebuggerStatement()) {
171           break_point_++;
172           return;
173         }
174         if (type_ == ALL_BREAK_LOCATIONS) {
175           if (Debug::IsBreakStub(code)) {
176             break_point_++;
177             return;
178           }
179         } else {
180           ASSERT(type_ == SOURCE_BREAK_LOCATIONS);
181           if (Debug::IsSourceBreakStub(code)) {
182             break_point_++;
183             return;
184           }
185         }
186       }
187     }
188
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;
195       } else {
196         position_ = 0;
197       }
198       statement_position_ = position_;
199       break_point_++;
200       return;
201     }
202   }
203 }
204
205
206 void BreakLocationIterator::Next(int count) {
207   while (count > 0) {
208     Next();
209     count--;
210   }
211 }
212
213
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;
219   while (!Done()) {
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;
226     }
227     Next();
228   }
229
230   // Move to the break point found.
231   Reset();
232   Next(closest_break_point);
233 }
234
235
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
239   // position.
240   int closest_break_point = 0;
241   int distance = kMaxInt;
242   while (!Done()) {
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;
250     }
251     Next();
252   }
253
254   // Move to the break point found.
255   Reset();
256   Next(closest_break_point);
257 }
258
259
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());
266
267   // Position at the first break point.
268   break_point_ = -1;
269   position_ = 1;
270   statement_position_ = 1;
271   Next();
272 }
273
274
275 bool BreakLocationIterator::Done() const {
276   return RinfoDone();
277 }
278
279
280 void BreakLocationIterator::SetBreakPoint(Handle<Object> break_point_object) {
281   // If there is not already a real break point here patch code with debug
282   // break.
283   if (!HasBreakPoint()) {
284     SetDebugBreak();
285   }
286   ASSERT(IsDebugBreak() || IsDebuggerStatement());
287   // Set the break point information.
288   DebugInfo::SetBreakPoint(debug_info_, code_position(),
289                            position(), statement_position(),
290                            break_point_object);
291 }
292
293
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()) {
299     ClearDebugBreak();
300     ASSERT(!IsDebugBreak());
301   }
302 }
303
304
305 void BreakLocationIterator::SetOneShot() {
306   // Debugger statement always calls debugger. No need to modify it.
307   if (IsDebuggerStatement()) {
308     return;
309   }
310
311   // If there is a real break point here no more to do.
312   if (HasBreakPoint()) {
313     ASSERT(IsDebugBreak());
314     return;
315   }
316
317   // Patch code with debug break.
318   SetDebugBreak();
319 }
320
321
322 void BreakLocationIterator::ClearOneShot() {
323   // Debugger statement always calls debugger. No need to modify it.
324   if (IsDebuggerStatement()) {
325     return;
326   }
327
328   // If there is a real break point here no more to do.
329   if (HasBreakPoint()) {
330     ASSERT(IsDebugBreak());
331     return;
332   }
333
334   // Patch code removing debug break.
335   ClearDebugBreak();
336   ASSERT(!IsDebugBreak());
337 }
338
339
340 void BreakLocationIterator::SetDebugBreak() {
341   // Debugger statement always calls debugger. No need to modify it.
342   if (IsDebuggerStatement()) {
343     return;
344   }
345
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()) {
351     return;
352   }
353
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();
360   } else {
361     // Patch the IC call.
362     SetDebugBreakAtIC();
363   }
364   ASSERT(IsDebugBreak());
365 }
366
367
368 void BreakLocationIterator::ClearDebugBreak() {
369   // Debugger statement always calls debugger. No need to modify it.
370   if (IsDebuggerStatement()) {
371     return;
372   }
373
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();
380   } else {
381     // Patch the IC call.
382     ClearDebugBreakAtIC();
383   }
384   ASSERT(!IsDebugBreak());
385 }
386
387
388 void BreakLocationIterator::PrepareStepIn() {
389   HandleScope scope;
390
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());
405     } else {
406       rinfo()->set_target_address(stub->entry());
407     }
408   } else {
409 #ifdef DEBUG
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));
417     }
418     bool is_call_function_stub =
419         (maybe_call_function_stub->kind() == Code::STUB &&
420          maybe_call_function_stub->major_key() == CodeStub::CallFunction);
421
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
428     // with breakpoints.
429     ASSERT(RelocInfo::IsConstructCall(rmode()) ||
430            target_code->is_inline_cache_stub() ||
431            is_call_function_stub);
432 #endif
433   }
434 }
435
436
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()));
440 }
441
442
443 bool BreakLocationIterator::HasBreakPoint() {
444   return debug_info_->HasBreakPoint(code_position());
445 }
446
447
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();
454   } else {
455     return Debug::IsDebugBreak(rinfo()->target_address());
456   }
457 }
458
459
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());
464
465   RelocInfo::Mode mode = rmode();
466   if (RelocInfo::IsCodeTarget(mode)) {
467     Address target = rinfo()->target_address();
468     Handle<Code> target_code(Code::GetCodeFromTargetAddress(target));
469
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());
474   }
475 }
476
477
478 void BreakLocationIterator::ClearDebugBreakAtIC() {
479   // Patch the code to the original invoke.
480   rinfo()->set_target_address(original_rinfo()->target_address());
481 }
482
483
484 bool BreakLocationIterator::IsDebuggerStatement() {
485   return RelocInfo::DEBUG_BREAK == rmode();
486 }
487
488
489 bool BreakLocationIterator::IsDebugBreakSlot() {
490   return RelocInfo::DEBUG_BREAK_SLOT == rmode();
491 }
492
493
494 Object* BreakLocationIterator::BreakPointObjects() {
495   return debug_info_->GetBreakPointObjects(code_position());
496 }
497
498
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() {
503   while (!Done()) {
504     ClearDebugBreak();
505     Next();
506   }
507 }
508
509
510 bool BreakLocationIterator::RinfoDone() const {
511   ASSERT(reloc_iterator_->done() == reloc_iterator_original_->done());
512   return reloc_iterator_->done();
513 }
514
515
516 void BreakLocationIterator::RinfoNext() {
517   reloc_iterator_->next();
518   reloc_iterator_original_->next();
519 #ifdef DEBUG
520   ASSERT(reloc_iterator_->done() == reloc_iterator_original_->done());
521   if (!reloc_iterator_->done()) {
522     ASSERT(rmode() == original_rmode());
523   }
524 #endif
525 }
526
527
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;
545 }
546
547
548 char* Debug::ArchiveDebug(char* storage) {
549   char* to = storage;
550   memcpy(to, reinterpret_cast<char*>(&thread_local_), sizeof(ThreadLocal));
551   to += sizeof(ThreadLocal);
552   memcpy(to, reinterpret_cast<char*>(&registers_), sizeof(registers_));
553   ThreadInit();
554   ASSERT(to <= storage + ArchiveSpacePerThread());
555   return storage + ArchiveSpacePerThread();
556 }
557
558
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*>(&registers_), from, sizeof(registers_));
564   ASSERT(from <= storage + ArchiveSpacePerThread());
565   return storage + ArchiveSpacePerThread();
566 }
567
568
569 int Debug::ArchiveSpacePerThread() {
570   return sizeof(ThreadLocal) + sizeof(JSCallerSavedBuffer);
571 }
572
573
574 // Frame structure (conforms InternalFrame structure):
575 //   -- code
576 //   -- SMI maker
577 //   -- function (slot is called "context")
578 //   -- frame base
579 Object** Debug::SetUpFrameDropperFrame(StackFrame* bottom_js_frame,
580                                        Handle<Code> code) {
581   ASSERT(bottom_js_frame->is_java_script());
582
583   Address fp = bottom_js_frame->fp();
584
585   // Move function pointer into "context" slot.
586   Memory::Object_at(fp + StandardFrameConstants::kContextOffset) =
587       Memory::Object_at(fp + JavaScriptFrameConstants::kFunctionOffset);
588
589   Memory::Object_at(fp + InternalFrameConstants::kCodeOffset) = *code;
590   Memory::Object_at(fp + StandardFrameConstants::kMarkerOffset) =
591       Smi::FromInt(StackFrame::INTERNAL);
592
593   return reinterpret_cast<Object**>(&Memory::Object_at(
594       fp + StandardFrameConstants::kContextOffset));
595 }
596
597 const int Debug::kFrameDropperFrameSize = 4;
598
599
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));
608     return;
609   }
610
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()),
618       this,
619       ScriptCache::HandleWeakScript);
620   entry->value = script_.location();
621 }
622
623
624 Handle<FixedArray> ScriptCache::GetScripts() {
625   Handle<FixedArray> instances = FACTORY->NewFixedArray(occupancy());
626   int count = 0;
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));
631       count++;
632     }
633   }
634   return instances;
635 }
636
637
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]);
642   }
643   collected_scripts_.Clear();
644 }
645
646
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);
656   }
657   // Clear the content of the hash map.
658   HashMap::Clear();
659 }
660
661
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.
665   Script** location =
666       reinterpret_cast<Script**>(Utils::OpenHandle(*obj).location());
667   ASSERT((*location)->IsScript());
668
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);
673
674   // Clear the weak handle.
675   obj.Dispose();
676   obj.Clear();
677 }
678
679
680 void Debug::SetUp(bool create_heap_objects) {
681   ThreadInit();
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.
688     debug_break_slot_ =
689         isolate_->builtins()->builtin(Builtins::kSlot_DebugBreak);
690     ASSERT(debug_break_slot_->IsCode());
691   }
692 }
693
694
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());
705 #ifdef DEBUG
706   node = debug->debug_info_list_;
707   while (node != NULL) {
708     ASSERT(node != reinterpret_cast<DebugInfoListNode*>(data));
709     node = node->next();
710   }
711 #endif
712 }
713
714
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()),
722       this,
723       Debug::HandleWeakDebugInfo);
724 }
725
726
727 DebugInfoListNode::~DebugInfoListNode() {
728   Isolate::Current()->global_handles()->Destroy(
729       reinterpret_cast<Object**>(debug_info_.location()));
730 }
731
732
733 bool Debug::CompileDebuggerScript(int index) {
734   Isolate* isolate = Isolate::Current();
735   Factory* factory = isolate->factory();
736   HandleScope scope(isolate);
737
738   // Bail out if the index is invalid.
739   if (index == -1) {
740     return false;
741   }
742
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);
748
749   // Compile the script.
750   Handle<SharedFunctionInfo> function_info;
751   function_info = Compiler::Compile(source_code,
752                                     script_name,
753                                     0, 0, NULL, NULL,
754                                     Handle<String>::null(),
755                                     NATIVES_CODE);
756
757   // Silently ignore stack overflows during compilation.
758   if (function_info.is_null()) {
759     ASSERT(isolate->has_pending_exception());
760     isolate->clear_pending_exception();
761     return false;
762   }
763
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);
769
770   Handle<Object> exception =
771       Execution::TryCall(function, Handle<Object>(context->global()),
772                          0, NULL, &caught_exception);
773
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();
786     return false;
787   }
788
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));
792   return true;
793 }
794
795
796 bool Debug::Load() {
797   // Return if debugger is already loaded.
798   if (IsLoaded()) return true;
799
800   Debugger* debugger = isolate_->debugger();
801
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())
806     return false;
807   debugger->set_loading_debugger(true);
808
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_);
813
814   // Create the debugger context.
815   HandleScope scope(isolate_);
816   Handle<Context> context =
817       isolate_->bootstrapper()->CreateEnvironment(
818           isolate_,
819           Handle<Object>::null(),
820           v8::Handle<ObjectTemplate>(),
821           NULL);
822
823   // Fail if no context could be created.
824   if (context.is_null()) return false;
825
826   // Use the debugger context.
827   SaveContext save(isolate_);
828   isolate_->set_context(*context);
829
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(
834       isolate_,
835       JSReceiver::SetProperty(global, key, Handle<Object>(global->builtins()),
836                               NONE, kNonStrictMode),
837       false);
838
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"));
844
845   if (FLAG_enable_liveedit) {
846     caught_exception = caught_exception ||
847         !CompileDebuggerScript(Natives::GetIndex("liveedit"));
848   }
849
850   debugger->set_compiling_natives(false);
851
852   // Make sure we mark the debugger as not loading before we might
853   // return.
854   debugger->set_loading_debugger(false);
855
856   // Check for caught exceptions.
857   if (caught_exception) return false;
858
859   // Debugger loaded.
860   debug_context_ = context;
861
862   return true;
863 }
864
865
866 void Debug::Unload() {
867   // Return debugger is not loaded.
868   if (!IsLoaded()) {
869     return;
870   }
871
872   // Clear the script cache.
873   DestroyScriptCache();
874
875   // Clear debugger context global handle.
876   Isolate::Current()->global_handles()->Destroy(
877       reinterpret_cast<Object**>(debug_context_.location()));
878   debug_context_ = Handle<Context>();
879 }
880
881
882 // Set the flag indicating that preemption happened during debugging.
883 void Debug::PreemptionWhileInDebugger() {
884   ASSERT(InDebugger());
885   Debug::set_interrupts_pending(PREEMPT);
886 }
887
888
889 void Debug::Iterate(ObjectVisitor* v) {
890   v->VisitPointer(BitCast<Object**>(&(debug_break_return_)));
891   v->VisitPointer(BitCast<Object**>(&(debug_break_slot_)));
892 }
893
894
895 void Debug::PutValuesOnStackAndDie(int start,
896                                    Address c_entry_fp,
897                                    Address last_fp,
898                                    Address larger_fp,
899                                    int count,
900                                    int end) {
901   OS::Abort();
902 }
903
904
905 Object* Debug::Break(Arguments args) {
906   Heap* heap = isolate_->heap();
907   HandleScope scope(isolate_);
908   ASSERT(args.length() == 0);
909
910   thread_local_.frame_drop_mode_ = FRAMES_UNTOUCHED;
911
912   // Get the top-most JavaScript frame.
913   JavaScriptFrameIterator it(isolate_);
914   JavaScriptFrame* frame = it.frame();
915
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();
920   }
921
922   // Enter the debugger.
923   EnterDebugger debugger;
924   if (debugger.FailedToEnter()) {
925     return heap->undefined_value();
926   }
927
928   // Postpone interrupt during breakpoint processing.
929   PostponeInterruptsScope postpone(isolate_);
930
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);
935
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());
940
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_--;
946     }
947   }
948
949   // If there is one or more real break points check whether any of these are
950   // triggered.
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);
956   }
957
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.
969
970     // Clear all current stepping setup.
971     ClearStepping();
972
973     if (thread_local_.queued_step_count_ > 0) {
974       // Perform queued steps
975       int step_count = thread_local_.queued_step_count_;
976
977       // Clear queue
978       thread_local_.queued_step_count_ = 0;
979
980       PrepareStep(StepNext, step_count);
981     } else {
982       // Notify the debug event listeners.
983       isolate_->debugger()->OnDebugBreak(break_points_hit, false);
984     }
985   } else if (thread_local_.last_step_action_ != StepNone) {
986     // Hold on to last step action as it is cleared by the call to
987     // ClearStepping.
988     StepAction step_action = thread_local_.last_step_action_;
989     int step_count = thread_local_.step_count_;
990
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
995       int count = 0;
996       JavaScriptFrameIterator it(isolate_);
997       while (!it.done() && it.frame()->fp() < thread_local_.last_fp_) {
998         count++;
999         it.Advance();
1000       }
1001
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.
1007       if (it.done()) {
1008         // We crawled the entire stack, never reaching last_fp_.
1009         PutValuesOnStackAndDie(0xBEEEEEEE,
1010                                frame->fp(),
1011                                thread_local_.last_fp_,
1012                                NULL,
1013                                count,
1014                                0xFEEEEEEE);
1015       } else if (it.frame()->fp() != thread_local_.last_fp_) {
1016         // We crawled over last_fp_, without getting a match.
1017         PutValuesOnStackAndDie(0xBEEEEEEE,
1018                                frame->fp(),
1019                                thread_local_.last_fp_,
1020                                it.frame()->fp(),
1021                                count,
1022                                0xFEEEEEEE);
1023       }
1024
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
1029           // StepOut
1030           thread_local_.queued_step_count_ = step_count - 1;
1031         }
1032
1033         // Set up for StepOut to reach target frame
1034         step_action = StepOut;
1035         step_count = count;
1036       }
1037     }
1038
1039     // Clear all current stepping setup.
1040     ClearStepping();
1041
1042     // Set up for the remaining steps.
1043     PrepareStep(step_action, step_count);
1044   }
1045
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();
1069   } else {
1070     UNREACHABLE();
1071   }
1072
1073   return heap->undefined_value();
1074 }
1075
1076
1077 RUNTIME_FUNCTION(Object*, Debug_Break) {
1078   return isolate->debug()->Break(args);
1079 }
1080
1081
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();
1087
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);
1100       }
1101     }
1102   } else {
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);
1106     }
1107   }
1108
1109   // Return undefined if no break points were triggered.
1110   if (break_points_hit_count == 0) {
1111     return factory->undefined_value();
1112   }
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));
1116   return result;
1117 }
1118
1119
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_);
1124
1125   // Ignore check if break point object is not a JSObject.
1126   if (!break_point_object->IsJSObject()) return true;
1127
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)));
1135
1136   // Get the break id as an object.
1137   Handle<Object> break_id = factory->NewNumberFromInt(Debug::break_id());
1138
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(),
1144                                              ARRAY_SIZE(argv),
1145                                              argv,
1146                                              &caught_exception);
1147
1148   // If exception or non boolean result handle as not triggered
1149   if (caught_exception || !result->IsBoolean()) {
1150     return false;
1151   }
1152
1153   // Return whether the break point is triggered.
1154   ASSERT(!result.is_null());
1155   return (*result)->IsTrue();
1156 }
1157
1158
1159 // Check whether the function has debug information.
1160 bool Debug::HasDebugInfo(Handle<SharedFunctionInfo> shared) {
1161   return !shared->debug_info()->IsUndefined();
1162 }
1163
1164
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()));
1170 }
1171
1172
1173 void Debug::SetBreakPoint(Handle<SharedFunctionInfo> shared,
1174                           Handle<Object> break_point_object,
1175                           int* source_position) {
1176   HandleScope scope(isolate_);
1177
1178   PrepareForBreakPoints();
1179
1180   if (!EnsureDebugInfo(shared)) {
1181     // Return if retrieving debug info failed.
1182     return;
1183   }
1184
1185   Handle<DebugInfo> debug_info = GetDebugInfo(shared);
1186   // Source positions starts with zero.
1187   ASSERT(*source_position >= 0);
1188
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);
1193
1194   *source_position = it.position();
1195
1196   // At least one active break point now.
1197   ASSERT(debug_info->GetBreakPointCount() > 0);
1198 }
1199
1200
1201 void Debug::ClearBreakPoint(Handle<Object> break_point_object) {
1202   HandleScope scope(isolate_);
1203
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();
1214
1215       // Source positions starts with zero.
1216       ASSERT(source_position >= 0);
1217
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);
1222
1223       // If there are no more break points left remove the debug info for this
1224       // function.
1225       if (debug_info->GetBreakPointCount() == 0) {
1226         RemoveDebugInfo(debug_info);
1227       }
1228
1229       return;
1230     }
1231     node = node->next();
1232   }
1233 }
1234
1235
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();
1243   }
1244
1245   // Remove all debug info.
1246   while (debug_info_list_ != NULL) {
1247     RemoveDebugInfo(debug_info_list_->debug_info());
1248   }
1249 }
1250
1251
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.
1257     return;
1258   }
1259
1260   // Flood the function with break points.
1261   BreakLocationIterator it(GetDebugInfo(shared), ALL_BREAK_LOCATIONS);
1262   while (!it.Done()) {
1263     it.SetOneShot();
1264     it.Next();
1265   }
1266 }
1267
1268
1269 void Debug::FloodBoundFunctionWithOneShot(Handle<JSFunction> function) {
1270   Handle<FixedArray> new_bindings(function->function_bindings());
1271   Handle<Object> bindee(new_bindings->get(JSFunction::kBoundFunctionIndex));
1272
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);
1277   }
1278 }
1279
1280
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.
1286     return;
1287   }
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);
1296       return;
1297     }
1298   }
1299 }
1300
1301
1302 void Debug::ChangeBreakOnException(ExceptionBreakType type, bool enable) {
1303   if (type == BreakUncaughtException) {
1304     break_on_uncaught_exception_ = enable;
1305   } else {
1306     break_on_exception_ = enable;
1307   }
1308 }
1309
1310
1311 bool Debug::IsBreakOnException(ExceptionBreakType type) {
1312   if (type == BreakUncaughtException) {
1313     return break_on_uncaught_exception_;
1314   } else {
1315     return break_on_exception_;
1316   }
1317 }
1318
1319
1320 void Debug::PrepareStep(StepAction step_action, int step_count) {
1321   HandleScope scope(isolate_);
1322
1323   PrepareForBreakPoints();
1324
1325   ASSERT(Debug::InDebugger());
1326
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;
1333   } else {
1334     thread_local_.step_count_ = step_count;
1335   }
1336
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.
1344     return;
1345   }
1346   JavaScriptFrameIterator frames_it(isolate_, id);
1347   JavaScriptFrame* frame = frames_it.frame();
1348
1349   // First of all ensure there is one-shot break points in the top handler
1350   // if any.
1351   FloodHandlerWithOneShot();
1352
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
1358     // breakpoints.
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()));
1363     return;
1364   }
1365
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.
1371     return;
1372   }
1373   Handle<DebugInfo> debug_info = GetDebugInfo(shared);
1374
1375   // Find the break location where execution has stopped.
1376   BreakLocationIterator it(debug_info, ALL_BREAK_LOCATIONS);
1377   it.FindBreakLocationFromAddress(frame->pc());
1378
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;
1384
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;
1392       }
1393       if (code->is_inline_cache_stub()) {
1394         is_inline_cache_stub = true;
1395         is_load_or_store = !is_call_target;
1396       }
1397
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);
1406       }
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);
1412       }
1413     }
1414   } else {
1415     is_at_restarted_function = true;
1416   }
1417
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();
1424       }
1425     } else {
1426       ASSERT(it.IsExit());
1427       frames_it.Advance();
1428     }
1429     // Skip builtin functions on the stack.
1430     while (!frames_it.done() &&
1431            JSFunction::cast(frames_it.frame()->function())->IsBuiltin()) {
1432       frames_it.Advance();
1433     }
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());
1442     }
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.
1447
1448     // Fill the current function with one-shot break points.
1449     FloodWithOneShot(shared);
1450
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();
1455   } else {
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.
1467
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.
1471       Handle<Object> obj(
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));
1486
1487       // Find target function on the expression stack.
1488       // Expression stack looks like this (top to bottom):
1489       // argN
1490       // ...
1491       // arg0
1492       // Receiver
1493       // Function to call
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()));
1506         }
1507       }
1508     }
1509
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
1513     // getters/setters.
1514     FloodWithOneShot(shared);
1515
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();
1524     }
1525
1526     // Step in or Step in min
1527     it.PrepareStepIn();
1528     ActivateStepIn(frame);
1529   }
1530 }
1531
1532
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;
1546   }
1547
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;
1554
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;
1560   }
1561
1562   // No step next action - don't continue.
1563   return false;
1564 }
1565
1566
1567 // Check whether the code object at the specified address is a debug break code
1568 // object.
1569 bool Debug::IsDebugBreak(Address addr) {
1570   Code* code = Code::GetCodeFromTargetAddress(addr);
1571   return code->ic_state() == DEBUG_BREAK;
1572 }
1573
1574
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;
1580 }
1581
1582
1583 // Check whether a code stub with the specified major key is a possible break
1584 // location.
1585 bool Debug::IsBreakStub(Code* code) {
1586   CodeStub::Major major_key = CodeStub::GetMajorKey(code);
1587   return major_key == CodeStub::CallFunction;
1588 }
1589
1590
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();
1594
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()) {
1599       case Code::CALL_IC:
1600       case Code::KEYED_CALL_IC:
1601         return isolate->stub_cache()->ComputeCallDebugBreak(
1602             code->arguments_count(), code->kind());
1603
1604       case Code::LOAD_IC:
1605         return isolate->builtins()->LoadIC_DebugBreak();
1606
1607       case Code::STORE_IC:
1608         return isolate->builtins()->StoreIC_DebugBreak();
1609
1610       case Code::KEYED_LOAD_IC:
1611         return isolate->builtins()->KeyedLoadIC_DebugBreak();
1612
1613       case Code::KEYED_STORE_IC:
1614         return isolate->builtins()->KeyedStoreIC_DebugBreak();
1615
1616       default:
1617         UNREACHABLE();
1618     }
1619   }
1620   if (RelocInfo::IsConstructCall(mode)) {
1621     if (code->has_function_cache()) {
1622       return isolate->builtins()->CallConstructStub_Recording_DebugBreak();
1623     } else {
1624       return isolate->builtins()->CallConstructStub_DebugBreak();
1625     }
1626   }
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();
1631     } else {
1632       return isolate->builtins()->CallFunctionStub_DebugBreak();
1633     }
1634   }
1635
1636   UNREACHABLE();
1637   return Handle<Code>::null();
1638 }
1639
1640
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());
1650   }
1651   Handle<FixedArray> locations =
1652       isolate->factory()->NewFixedArray(debug_info->GetBreakPointCount());
1653   int count = 0;
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());
1660       }
1661     }
1662   }
1663   return locations;
1664 }
1665
1666
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_;
1670 }
1671
1672
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;
1676 }
1677
1678
1679 // Handle stepping into a function.
1680 void Debug::HandleStepIn(Handle<JSFunction> function,
1681                          Handle<Object> holder,
1682                          Address fp,
1683                          bool is_constructor) {
1684   // If the frame pointer is not supplied by the caller find it.
1685   if (fp == 0) {
1686     StackFrameIterator it;
1687     it.Advance();
1688     // For constructor functions skip another frame.
1689     if (is_constructor) {
1690       ASSERT(it.frame()->is_construct());
1691       it.Advance();
1692     }
1693     fp = it.frame()->fp();
1694   }
1695
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
1711         // function.
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);
1717         }
1718       } else {
1719         Debug::FloodWithOneShot(Handle<SharedFunctionInfo>(function->shared()));
1720       }
1721     }
1722   }
1723 }
1724
1725
1726 void Debug::ClearStepping() {
1727   // Clear the various stepping setup.
1728   ClearOneShot();
1729   ClearStepIn();
1730   ClearStepOut();
1731   ClearStepNext();
1732
1733   // Clear multiple step counter.
1734   thread_local_.step_count_ = 0;
1735 }
1736
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.
1744
1745   DebugInfoListNode* node = debug_info_list_;
1746   while (node != NULL) {
1747     BreakLocationIterator it(node->debug_info(), ALL_BREAK_LOCATIONS);
1748     while (!it.Done()) {
1749       it.ClearOneShot();
1750       it.Next();
1751     }
1752     node = node->next();
1753   }
1754 }
1755
1756
1757 void Debug::ActivateStepIn(StackFrame* frame) {
1758   ASSERT(!StepOutActive());
1759   thread_local_.step_into_fp_ = frame->fp();
1760 }
1761
1762
1763 void Debug::ClearStepIn() {
1764   thread_local_.step_into_fp_ = 0;
1765 }
1766
1767
1768 void Debug::ActivateStepOut(StackFrame* frame) {
1769   ASSERT(!StepInActive());
1770   thread_local_.step_out_fp_ = frame->fp();
1771 }
1772
1773
1774 void Debug::ClearStepOut() {
1775   thread_local_.step_out_fp_ = 0;
1776 }
1777
1778
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;
1783 }
1784
1785
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());
1799
1800   CompilationInfo info(shared);
1801   info.MarkCompilingForDebugging(current_code);
1802   ASSERT(!info.shared_info()->is_compiled());
1803   ASSERT(!info.isolate()->has_pending_exception());
1804
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();
1810 #if DEBUG
1811   if (result) {
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());
1816   }
1817 #endif
1818   return result;
1819 }
1820
1821
1822 static void CollectActiveFunctionsFromThread(
1823     Isolate* isolate,
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);
1840       }
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);
1846     }
1847   }
1848 }
1849
1850
1851 static void RedirectActivationsToRecompiledCodeOnThread(
1852     Isolate* isolate,
1853     ThreadLocalTop* top) {
1854   for (JavaScriptFrameIterator it(isolate, top); !it.done(); it.Advance()) {
1855     JavaScriptFrame* frame = it.frame();
1856
1857     if (frame->is_optimized() || !frame->function()->IsJSFunction()) continue;
1858
1859     JSFunction* function = JSFunction::cast(frame->function());
1860
1861     ASSERT(frame->LookupCode()->kind() == Code::FUNCTION);
1862
1863     Handle<Code> frame_code(frame->LookupCode());
1864     if (frame_code->has_debug_break_slots()) continue;
1865
1866     Handle<Code> new_code(function->shared()->code());
1867     if (new_code->kind() != Code::FUNCTION ||
1868         !new_code->has_debug_break_slots()) {
1869       continue;
1870     }
1871
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 =
1882           info->pc() -
1883           new_code->instruction_start() -
1884           debug_break_slot_bytes;
1885       if (new_delta > delta) {
1886         break;
1887       }
1888
1889       // Passed a debug break slot in the full code with debug
1890       // break slots.
1891       debug_break_slot_count++;
1892     }
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) "
1898              "for debugging, "
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);
1913     }
1914
1915     // Patch the return address to return into the code with
1916     // debug break slots.
1917     frame->set_pc(
1918         new_code->instruction_start() + delta + debug_break_slot_bytes);
1919   }
1920 }
1921
1922
1923 class ActiveFunctionsCollector : public ThreadVisitor {
1924  public:
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) { }
1929
1930   void VisitThread(Isolate* isolate, ThreadLocalTop* top) {
1931     CollectActiveFunctionsFromThread(isolate,
1932                                      top,
1933                                      active_functions_,
1934                                      active_code_marker_);
1935   }
1936
1937  private:
1938   List<Handle<JSFunction> >* active_functions_;
1939   Object* active_code_marker_;
1940 };
1941
1942
1943 class ActiveFunctionsRedirector : public ThreadVisitor {
1944  public:
1945   void VisitThread(Isolate* isolate, ThreadLocalTop* top) {
1946     RedirectActivationsToRecompiledCodeOnThread(isolate, top);
1947   }
1948 };
1949
1950
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();
1956
1957     Handle<Code> lazy_compile =
1958         Handle<Code>(isolate_->builtins()->builtin(Builtins::kLazyCompile));
1959
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);
1963
1964     {
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");
1969
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;
1973
1974       Object* active_code_marker = isolate_->heap()->the_hole_value();
1975
1976       CollectActiveFunctionsFromThread(isolate_,
1977                                        isolate_->thread_local_top(),
1978                                        &active_functions,
1979                                        active_code_marker);
1980       ActiveFunctionsCollector active_functions_collector(&active_functions,
1981                                                           active_code_marker);
1982       isolate_->thread_manager()->IterateArchivedThreads(
1983           &active_functions_collector);
1984
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);
2001           }
2002         }
2003       }
2004
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));
2009       }
2010     }
2011
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];
2016
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.
2020         continue;
2021       }
2022
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) {
2028         continue;
2029       }
2030
2031       // Make sure that the shared full code is compiled with debug
2032       // break slots.
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);
2048           continue;
2049         }
2050       }
2051
2052       // Keep function code in sync with shared function info.
2053       function->set_code(shared->code());
2054     }
2055
2056     RedirectActivationsToRecompiledCodeOnThread(isolate_,
2057                                                 isolate_->thread_local_top());
2058
2059     ActiveFunctionsRedirector active_functions_redirector;
2060     isolate_->thread_manager()->IterateArchivedThreads(
2061           &active_functions_redirector);
2062   }
2063 }
2064
2065
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());
2071     return true;
2072   }
2073
2074   // Ensure shared in compiled. Return false if this failed.
2075   if (!SharedFunctionInfo::EnsureCompiled(shared, CLEAR_EXCEPTION)) {
2076     return false;
2077   }
2078
2079   // Create the debug info object.
2080   Handle<DebugInfo> debug_info = FACTORY->NewDebugInfo(shared);
2081
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;
2086
2087   // Now there is at least one break point.
2088   has_break_points_ = true;
2089
2090   return true;
2091 }
2092
2093
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.
2102       if (prev == NULL) {
2103         debug_info_list_ = current->next();
2104       } else {
2105         prev->set_next(current->next());
2106       }
2107       current->debug_info()->shared()->set_debug_info(
2108               isolate_->heap()->undefined_value());
2109       delete current;
2110
2111       // If there are no more debug info objects there are not more break
2112       // points.
2113       has_break_points_ = debug_info_list_ != NULL;
2114
2115       return;
2116     }
2117     // Move to next in list.
2118     prev = current;
2119     current = current->next();
2120   }
2121   UNREACHABLE();
2122 }
2123
2124
2125 void Debug::SetAfterBreakTarget(JavaScriptFrame* frame) {
2126   HandleScope scope(isolate_);
2127
2128   PrepareForBreakPoints();
2129
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.
2135     return;
2136   }
2137   Handle<DebugInfo> debug_info = GetDebugInfo(shared);
2138   Handle<Code> code(debug_info->code());
2139   Handle<Code> original_code(debug_info->original_code());
2140 #ifdef DEBUG
2141   // Get the code which is actually executing.
2142   Handle<Code> frame_code(frame->LookupCode());
2143   ASSERT(frame_code.is_identical_to(code));
2144 #endif
2145
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;
2150
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();
2161     }
2162     if (RelocInfo::IsDebugBreakSlot(it.rinfo()->rmode())) {
2163       at_debug_break_slot = (it.rinfo()->pc() ==
2164           addr - Assembler::kPatchDebugBreakSlotAddressOffset);
2165     }
2166     it.next();
2167   }
2168
2169   // Handle the jump to continue execution after break point depending on the
2170   // break location.
2171   if (at_js_return) {
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();
2177     }
2178
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;
2185
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.
2193
2194     // Find the corresponding address in the original code.
2195     addr += original_code->instruction_start() - code->instruction_start();
2196
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);
2200   } else {
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);
2206   }
2207 }
2208
2209
2210 bool Debug::IsBreakAtReturn(JavaScriptFrame* frame) {
2211   HandleScope scope(isolate_);
2212
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
2215   // return.
2216   if (!has_break_points_) {
2217     return false;
2218   }
2219
2220   PrepareForBreakPoints();
2221
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.
2227     return false;
2228   }
2229   Handle<DebugInfo> debug_info = GetDebugInfo(shared);
2230   Handle<Code> code(debug_info->code());
2231 #ifdef DEBUG
2232   // Get the code which is actually executing.
2233   Handle<Code> frame_code(frame->LookupCode());
2234   ASSERT(frame_code.is_identical_to(code));
2235 #endif
2236
2237   // Find the call address in the running code.
2238   Address addr = frame->pc() - Assembler::kCallTargetAddressOffset;
2239
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);
2246     }
2247     it.next();
2248   }
2249   return false;
2250 }
2251
2252
2253 void Debug::FramesHaveBeenDropped(StackFrame::Id new_break_frame_id,
2254                                   FrameDropMode mode,
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;
2260 }
2261
2262
2263 const int Debug::FramePaddingLayout::kInitialSize = 1;
2264
2265
2266 // Any even value bigger than kInitialSize as needed for stack scanning.
2267 const int Debug::FramePaddingLayout::kPaddingValue = kInitialSize + 1;
2268
2269
2270 bool Debug::IsDebugGlobal(GlobalObject* global) {
2271   return IsLoaded() && global == debug_context()->global();
2272 }
2273
2274
2275 void Debug::ClearMirrorCache() {
2276   PostponeInterruptsScope postpone(isolate_);
2277   HandleScope scope(isolate_);
2278   ASSERT(isolate_->context() == *Debug::debug_context());
2279
2280   // Clear the mirror cache.
2281   Handle<String> function_name =
2282       isolate_->factory()->LookupSymbol(CStrVector("ClearMirrorCache"));
2283   Handle<Object> fun(Isolate::Current()->global()->GetPropertyNoExceptionThrown(
2284       *function_name));
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);
2290 }
2291
2292
2293 void Debug::CreateScriptCache() {
2294   Heap* heap = isolate_->heap();
2295   HandleScope scope(isolate_);
2296
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");
2304
2305   ASSERT(script_cache_ == NULL);
2306   script_cache_ = new ScriptCache();
2307
2308   // Scan heap for Script objects.
2309   int count = 0;
2310   HeapIterator iterator;
2311   AssertNoAllocation no_allocation;
2312
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)));
2316       count++;
2317     }
2318   }
2319 }
2320
2321
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;
2327   }
2328 }
2329
2330
2331 void Debug::AddScriptToScriptCache(Handle<Script> script) {
2332   if (script_cache_ != NULL) {
2333     script_cache_->Add(script);
2334   }
2335 }
2336
2337
2338 Handle<FixedArray> Debug::GetLoadedScripts() {
2339   // Create and fill the script cache when the loaded scripts is requested for
2340   // the first time.
2341   if (script_cache_ == NULL) {
2342     CreateScriptCache();
2343   }
2344
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);
2349   }
2350
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");
2355
2356   // Get the scripts from the cache.
2357   return script_cache_->GetScripts();
2358 }
2359
2360
2361 void Debug::AfterGarbageCollection() {
2362   // Generate events for collected scripts.
2363   if (script_cache_ != NULL) {
2364     script_cache_->ProcessCollectedScripts();
2365   }
2366 }
2367
2368
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),
2384       agent_(NULL),
2385       command_queue_(isolate->logger(), kQueueInitialSize),
2386       command_received_(OS::CreateSemaphore(0)),
2387       event_command_queue_(isolate->logger(), kQueueInitialSize),
2388       isolate_(isolate) {
2389 }
2390
2391
2392 Debugger::~Debugger() {
2393   delete dispatch_handler_access_;
2394   dispatch_handler_access_ = 0;
2395   delete command_received_;
2396   command_received_ = 0;
2397 }
2398
2399
2400 Handle<Object> Debugger::MakeJSObject(Vector<const char> constructor_name,
2401                                       int argc,
2402                                       Handle<Object> argv[],
2403                                       bool* caught_exception) {
2404   ASSERT(isolate_->context() == *isolate_->debug()->debug_context());
2405
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();
2415   }
2416   Handle<Object> js_object = Execution::TryCall(
2417       Handle<JSFunction>::cast(constructor),
2418       Handle<JSObject>(isolate_->debug()->debug_context()->global()),
2419       argc,
2420       argv,
2421       caught_exception);
2422   return js_object;
2423 }
2424
2425
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"),
2432                       ARRAY_SIZE(argv),
2433                       argv,
2434                       caught_exception);
2435 }
2436
2437
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"),
2444                       ARRAY_SIZE(argv),
2445                       argv,
2446                       caught_exception);
2447 }
2448
2449
2450 Handle<Object> Debugger::MakeExceptionEvent(Handle<Object> exec_state,
2451                                             Handle<Object> exception,
2452                                             bool uncaught,
2453                                             bool* caught_exception) {
2454   Factory* factory = isolate_->factory();
2455   // Create the new exception event object.
2456   Handle<Object> argv[] = { exec_state,
2457                             exception,
2458                             factory->ToBoolean(uncaught) };
2459   return MakeJSObject(CStrVector("MakeExceptionEvent"),
2460                       ARRAY_SIZE(argv),
2461                       argv,
2462                       caught_exception);
2463 }
2464
2465
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"),
2471                       ARRAY_SIZE(argv),
2472                       argv,
2473                       caught_exception);
2474 }
2475
2476
2477 Handle<Object> Debugger::MakeCompileEvent(Handle<Script> script,
2478                                           bool before,
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,
2485                             script_wrapper,
2486                             factory->ToBoolean(before) };
2487   return MakeJSObject(CStrVector("MakeCompileEvent"),
2488                       ARRAY_SIZE(argv),
2489                       argv,
2490                       caught_exception);
2491 }
2492
2493
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 };
2500
2501   return MakeJSObject(CStrVector("MakeScriptCollectedEvent"),
2502                       ARRAY_SIZE(argv),
2503                       argv,
2504                       caught_exception);
2505 }
2506
2507
2508 void Debugger::OnException(Handle<Object> exception, bool uncaught) {
2509   HandleScope scope(isolate_);
2510   Debug* debug = isolate_->debug();
2511
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;
2515
2516   // Bail out if exception breaks are not active
2517   if (uncaught) {
2518     // Uncaught exceptions are reported by either flags.
2519     if (!(debug->break_on_uncaught_exception() ||
2520           debug->break_on_exception())) return;
2521   } else {
2522     // Caught exceptions are reported is activated.
2523     if (!debug->break_on_exception()) return;
2524   }
2525
2526   // Enter the debugger.
2527   EnterDebugger debugger;
2528   if (debugger.FailedToEnter()) return;
2529
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,
2538                                     &caught_exception);
2539   }
2540   // Bail out and don't call debugger if exception.
2541   if (caught_exception) {
2542     return;
2543   }
2544
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.
2548 }
2549
2550
2551 void Debugger::OnDebugBreak(Handle<Object> break_points_hit,
2552                             bool auto_continue) {
2553   HandleScope scope(isolate_);
2554
2555   // Debugger has already been entered by caller.
2556   ASSERT(isolate_->context() == *isolate_->debug()->debug_context());
2557
2558   // Bail out if there is no listener for this event
2559   if (!Debugger::EventActive(v8::Break)) return;
2560
2561   // Debugger must be entered in advance.
2562   ASSERT(isolate_->context() == *isolate_->debug()->debug_context());
2563
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,
2570                                 &caught_exception);
2571   }
2572   // Bail out and don't call debugger if exception.
2573   if (caught_exception) {
2574     return;
2575   }
2576
2577   // Process debug event.
2578   ProcessDebugEvent(v8::Break,
2579                     Handle<JSObject>::cast(event_data),
2580                     auto_continue);
2581 }
2582
2583
2584 void Debugger::OnBeforeCompile(Handle<Script> script) {
2585   HandleScope scope(isolate_);
2586
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;
2591
2592   // Enter the debugger.
2593   EnterDebugger debugger;
2594   if (debugger.FailedToEnter()) return;
2595
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) {
2601     return;
2602   }
2603
2604   // Process debug event.
2605   ProcessDebugEvent(v8::BeforeCompile,
2606                     Handle<JSObject>::cast(event_data),
2607                     true);
2608 }
2609
2610
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();
2616
2617   // Add the newly compiled script to the script cache.
2618   debug->AddScriptToScriptCache(script);
2619
2620   // No more to do if not debugging.
2621   if (!IsDebuggerActive()) return;
2622
2623   // No compile events while compiling natives.
2624   if (compiling_natives()) return;
2625
2626   // Store whether in debugger before entering debugger.
2627   bool in_debugger = debug->InDebugger();
2628
2629   // Enter the debugger.
2630   EnterDebugger debugger;
2631   if (debugger.FailedToEnter()) return;
2632
2633   // If debugging there might be script break points registered for this
2634   // script. Make sure that these break points are set.
2635
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()) {
2643     return;
2644   }
2645   ASSERT(update_script_break_points->IsJSFunction());
2646
2647   // Wrap the script object in a proper JS object before passing it
2648   // to JavaScript.
2649   Handle<JSValue> wrapper = GetScriptWrapper(script);
2650
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(),
2656                      ARRAY_SIZE(argv),
2657                      argv,
2658                      &caught_exception);
2659   if (caught_exception) {
2660     return;
2661   }
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;
2665
2666   // Create the compile state object.
2667   Handle<Object> event_data = MakeCompileEvent(script,
2668                                                false,
2669                                                &caught_exception);
2670   // Bail out and don't call debugger if exception.
2671   if (caught_exception) {
2672     return;
2673   }
2674   // Process debug event.
2675   ProcessDebugEvent(v8::AfterCompile,
2676                     Handle<JSObject>::cast(event_data),
2677                     true);
2678 }
2679
2680
2681 void Debugger::OnScriptCollected(int id) {
2682   HandleScope scope(isolate_);
2683
2684   // No more to do if not debugging.
2685   if (!IsDebuggerActive()) return;
2686   if (!Debugger::EventActive(v8::ScriptCollected)) return;
2687
2688   // Enter the debugger.
2689   EnterDebugger debugger;
2690   if (debugger.FailedToEnter()) return;
2691
2692   // Create the script collected state object.
2693   bool caught_exception = false;
2694   Handle<Object> event_data = MakeScriptCollectedEvent(id,
2695                                                        &caught_exception);
2696   // Bail out and don't call debugger if exception.
2697   if (caught_exception) {
2698     return;
2699   }
2700
2701   // Process debug event.
2702   ProcessDebugEvent(v8::ScriptCollected,
2703                     Handle<JSObject>::cast(event_data),
2704                     true);
2705 }
2706
2707
2708 void Debugger::ProcessDebugEvent(v8::DebugEvent event,
2709                                  Handle<JSObject> event_data,
2710                                  bool auto_continue) {
2711   HandleScope scope(isolate_);
2712
2713   // Clear any pending debug break if this is a real break.
2714   if (!auto_continue) {
2715     isolate_->debug()->clear_interrupt_pending(DEBUGBREAK);
2716   }
2717
2718   // Create the execution state.
2719   bool caught_exception = false;
2720   Handle<Object> exec_state = MakeExecutionState(&caught_exception);
2721   if (caught_exception) {
2722     return;
2723   }
2724   // First notify the message handler if any.
2725   if (message_handler_ != NULL) {
2726     NotifyMessageHandler(event,
2727                          Handle<JSObject>::cast(exec_state),
2728                          event_data,
2729                          auto_continue);
2730   }
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);
2736   }
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,
2743                           exec_state,
2744                           event_data,
2745                           command.client_data());
2746       }
2747       command.Dispose();
2748     }
2749   }
2750 }
2751
2752
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);
2759   } else {
2760     CallJSEventCallback(event, exec_state, event_data);
2761   }
2762 }
2763
2764
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(
2774       event,
2775       Handle<JSObject>::cast(exec_state),
2776       Handle<JSObject>::cast(event_data),
2777       event_listener_data_,
2778       client_data);
2779   callback(event_details);
2780 }
2781
2782
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_));
2788
2789   // Invoke the JavaScript debug event listener.
2790   Handle<Object> argv[] = { Handle<Object>(Smi::FromInt(event)),
2791                             exec_state,
2792                             event_data,
2793                             event_listener_data_ };
2794   bool caught_exception;
2795   Execution::TryCall(fun,
2796                      isolate_->global(),
2797                      ARRAY_SIZE(argv),
2798                      argv,
2799                      &caught_exception);
2800   // Silently ignore exceptions from debug event listeners.
2801 }
2802
2803
2804 Handle<Context> Debugger::GetDebugContext() {
2805   never_unload_debugger_ = true;
2806   EnterDebugger debugger;
2807   return isolate_->debug()->debug_context();
2808 }
2809
2810
2811 void Debugger::UnloadDebugger() {
2812   Debug* debug = isolate_->debug();
2813
2814   // Make sure that there are no breakpoints left.
2815   debug->ClearAllBreakPoints();
2816
2817   // Unload the debugger if feasible.
2818   if (!never_unload_debugger_) {
2819     debug->Unload();
2820   }
2821
2822   // Clear the flag indicating that the debugger should be unloaded.
2823   debugger_unload_pending_ = false;
2824 }
2825
2826
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_);
2832
2833   if (!isolate_->debug()->Load()) return;
2834
2835   // Process the individual events.
2836   bool sendEventMessage = false;
2837   switch (event) {
2838     case v8::Break:
2839     case v8::BreakForCommand:
2840       sendEventMessage = !auto_continue;
2841       break;
2842     case v8::Exception:
2843       sendEventMessage = true;
2844       break;
2845     case v8::BeforeCompile:
2846       break;
2847     case v8::AfterCompile:
2848       sendEventMessage = true;
2849       break;
2850     case v8::ScriptCollected:
2851       sendEventMessage = true;
2852       break;
2853     case v8::NewFunction:
2854       break;
2855     default:
2856       UNREACHABLE();
2857   }
2858
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
2861   // debugger.
2862   ASSERT(isolate_->debug()->InDebugger());
2863   isolate_->stack_guard()->Continue(DEBUGCOMMAND);
2864
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(
2869         event,
2870         auto_continue,
2871         Handle<JSObject>::cast(exec_state),
2872         Handle<JSObject>::cast(event_data));
2873     InvokeMessageHandler(message);
2874   }
2875
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
2879   // by the client.
2880   if ((auto_continue && !HasCommands()) || event == v8::ScriptCollected) {
2881     return;
2882   }
2883
2884   v8::TryCatch try_catch;
2885
2886   // DebugCommandProcessor goes here.
2887   v8::Local<v8::Object> cmd_processor;
2888   {
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));
2895
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());
2903       return;
2904     }
2905   }
2906
2907   bool running = auto_continue;
2908
2909   // Process requests from the debugger.
2910   while (true) {
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_();
2917         continue;
2918       }
2919     } else {
2920       // In case there is no host dispatch - just wait.
2921       command_received_->Wait();
2922     }
2923
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.
2930       command.Dispose();
2931       return;
2932     }
2933
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));
2941
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);
2947
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);
2954       } else {
2955         response = v8::String::New("");
2956       }
2957
2958       // Log the JSON request/response.
2959       if (FLAG_trace_debug_json) {
2960         PrintLn(request);
2961         PrintLn(response);
2962       }
2963
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();
2972       }
2973     } else {
2974       // In case of failure the result text is the exception text.
2975       response = try_catch.Exception()->ToString();
2976     }
2977
2978     // Return the result.
2979     MessageImpl message = MessageImpl::NewResponse(
2980         event,
2981         running,
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);
2987     command.Dispose();
2988
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()) {
2993       return;
2994     }
2995   }
2996 }
2997
2998
2999 void Debugger::SetEventListener(Handle<Object> callback,
3000                                 Handle<Object> data) {
3001   HandleScope scope(isolate_);
3002   GlobalHandles* global_handles = isolate_->global_handles();
3003
3004   // Clear the global handles for the event listener and the event listener data
3005   // object.
3006   if (!event_listener_.is_null()) {
3007     global_handles->Destroy(
3008         reinterpret_cast<Object**>(event_listener_.location()));
3009     event_listener_ = Handle<Object>();
3010   }
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>();
3015   }
3016
3017   // If there is a new debug event listener register it together with its data
3018   // object.
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();
3024     }
3025     event_listener_data_ = Handle<Object>::cast(
3026         global_handles->Create(*data));
3027   }
3028
3029   ListenersChanged();
3030 }
3031
3032
3033 void Debugger::SetMessageHandler(v8::Debug::MessageHandler2 handler) {
3034   ScopedLock with(debugger_access_);
3035
3036   message_handler_ = handler;
3037   ListenersChanged();
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());
3043     }
3044   }
3045 }
3046
3047
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;
3053   } else {
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;
3058   }
3059 }
3060
3061
3062 void Debugger::SetHostDispatchHandler(v8::Debug::HostDispatchHandler handler,
3063                                       int period) {
3064   host_dispatch_handler_ = handler;
3065   host_dispatch_micros_ = period * 1000;
3066 }
3067
3068
3069 void Debugger::SetDebugMessageDispatchHandler(
3070     v8::Debug::DebugMessageDispatchHandler handler, bool provide_locker) {
3071   ScopedLock with(dispatch_handler_access_);
3072   debug_message_dispatch_handler_ = handler;
3073
3074   if (provide_locker && message_dispatch_helper_thread_ == NULL) {
3075     message_dispatch_helper_thread_ = new MessageDispatchHelperThread(isolate_);
3076     message_dispatch_helper_thread_->Start();
3077   }
3078 }
3079
3080
3081 // Calls the registered debug message handler. This callback is part of the
3082 // public API.
3083 void Debugger::InvokeMessageHandler(MessageImpl message) {
3084   ScopedLock with(debugger_access_);
3085
3086   if (message_handler_ != NULL) {
3087     message_handler_(message);
3088   }
3089 }
3090
3091
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()),
3101                        command.length()),
3102       client_data);
3103   isolate_->logger()->DebugTag("Put command on command_queue.");
3104   command_queue_.Put(message);
3105   command_received_->Signal();
3106
3107   // Set the debug command break flag to have the command processed.
3108   if (!isolate_->debug()->InDebugger()) {
3109     isolate_->stack_guard()->DebugCommand();
3110   }
3111
3112   MessageDispatchHelperThread* dispatch_thread;
3113   {
3114     ScopedLock with(dispatch_handler_access_);
3115     dispatch_thread = message_dispatch_helper_thread_;
3116   }
3117
3118   if (dispatch_thread == NULL) {
3119     CallMessageDispatchHandler();
3120   } else {
3121     dispatch_thread->Schedule();
3122   }
3123 }
3124
3125
3126 bool Debugger::HasCommands() {
3127   return !command_queue_.IsEmpty();
3128 }
3129
3130
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);
3134
3135   // Set the debug command break flag to have the command processed.
3136   if (!isolate_->debug()->InDebugger()) {
3137     isolate_->stack_guard()->DebugCommand();
3138   }
3139 }
3140
3141
3142 bool Debugger::IsDebuggerActive() {
3143   ScopedLock with(debugger_access_);
3144
3145   return message_handler_ != NULL ||
3146       !event_listener_.is_null() ||
3147       force_debugger_active_;
3148 }
3149
3150
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;
3156
3157   // Enter the debugger.
3158   EnterDebugger debugger;
3159   if (debugger.FailedToEnter()) {
3160     return isolate_->factory()->undefined_value();
3161   }
3162
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();
3168   }
3169
3170   Handle<Object> argv[] = { exec_state, data };
3171   Handle<Object> result = Execution::Call(
3172       fun,
3173       Handle<Object>(isolate_->debug()->debug_context_->global_proxy()),
3174       ARRAY_SIZE(argv),
3175       argv,
3176       pending_exception);
3177   return result;
3178 }
3179
3180
3181 static void StubMessageHandler2(const v8::Debug::Message& message) {
3182   // Simply ignore message.
3183 }
3184
3185
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
3191     // it starts.
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();
3198   }
3199
3200   if (Socket::SetUp()) {
3201     if (agent_ == NULL) {
3202       agent_ = new DebuggerAgent(name, port);
3203       agent_->Start();
3204     }
3205     return true;
3206   }
3207
3208   return false;
3209 }
3210
3211
3212 void Debugger::StopAgent() {
3213   ASSERT(Isolate::Current() == isolate_);
3214   if (agent_ != NULL) {
3215     agent_->Shutdown();
3216     agent_->Join();
3217     delete agent_;
3218     agent_ = NULL;
3219   }
3220 }
3221
3222
3223 void Debugger::WaitForAgent() {
3224   ASSERT(Isolate::Current() == isolate_);
3225   if (agent_ != NULL)
3226     agent_->WaitUntilListening();
3227 }
3228
3229
3230 void Debugger::CallMessageDispatchHandler() {
3231   v8::Debug::DebugMessageDispatchHandler handler;
3232   {
3233     ScopedLock with(dispatch_handler_access_);
3234     handler = Debugger::debug_message_dispatch_handler_;
3235   }
3236   if (handler != NULL) {
3237     handler();
3238   }
3239 }
3240
3241
3242 EnterDebugger::EnterDebugger()
3243     : isolate_(Isolate::Current()),
3244       prev_(isolate_->debug()->debugger_entry()),
3245       it_(isolate_),
3246       has_js_frames_(!it_.done()),
3247       save_(isolate_) {
3248   Debug* debug = isolate_->debug();
3249   ASSERT(prev_ != NULL || !debug->is_interrupt_pending(PREEMPT));
3250   ASSERT(prev_ != NULL || !debug->is_interrupt_pending(DEBUGBREAK));
3251
3252   // Link recursive debugger entry.
3253   debug->set_debugger_entry(this);
3254
3255   // Store the previous break id and frame id.
3256   break_id_ = debug->break_id();
3257   break_frame_id_ = debug->break_frame_id();
3258
3259   // Create the new break info. If there is no JavaScript frames there is no
3260   // break frame id.
3261   if (has_js_frames_) {
3262     debug->NewBreak(it_.frame()->id());
3263   } else {
3264     debug->NewBreak(StackFrame::NO_ID);
3265   }
3266
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
3271     // this change.
3272     isolate_->set_context(*debug->debug_context());
3273   }
3274 }
3275
3276
3277 EnterDebugger::~EnterDebugger() {
3278   ASSERT(Isolate::Current() == isolate_);
3279   Debug* debug = isolate_->debug();
3280
3281   // Restore to the previous break state.
3282   debug->SetBreak(break_frame_id_, break_id_);
3283
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);
3296       }
3297       debug->ClearMirrorCache();
3298     }
3299
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();
3307     }
3308     if (debug->is_interrupt_pending(DEBUGBREAK)) {
3309       debug->clear_interrupt_pending(DEBUGBREAK);
3310       isolate_->stack_guard()->DebugBreak();
3311     }
3312
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();
3317     }
3318
3319     // If leaving the debugger with the debugger no longer active unload it.
3320     if (!isolate_->debugger()->IsDebuggerActive()) {
3321       isolate_->debugger()->UnloadDebugger();
3322     }
3323   }
3324
3325   // Leaving this debugger entry.
3326   debug->set_debugger_entry(prev_);
3327 }
3328
3329
3330 MessageImpl MessageImpl::NewEvent(DebugEvent event,
3331                                   bool running,
3332                                   Handle<JSObject> exec_state,
3333                                   Handle<JSObject> event_data) {
3334   MessageImpl message(true, event, running,
3335                       exec_state, event_data, Handle<String>(), NULL);
3336   return message;
3337 }
3338
3339
3340 MessageImpl MessageImpl::NewResponse(DebugEvent event,
3341                                      bool running,
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);
3348   return message;
3349 }
3350
3351
3352 MessageImpl::MessageImpl(bool is_event,
3353                          DebugEvent event,
3354                          bool running,
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),
3360       event_(event),
3361       running_(running),
3362       exec_state_(exec_state),
3363       event_data_(event_data),
3364       response_json_(response_json),
3365       client_data_(client_data) {}
3366
3367
3368 bool MessageImpl::IsEvent() const {
3369   return is_event_;
3370 }
3371
3372
3373 bool MessageImpl::IsResponse() const {
3374   return !is_event_;
3375 }
3376
3377
3378 DebugEvent MessageImpl::GetEvent() const {
3379   return event_;
3380 }
3381
3382
3383 bool MessageImpl::WillStartRunning() const {
3384   return running_;
3385 }
3386
3387
3388 v8::Handle<v8::Object> MessageImpl::GetExecutionState() const {
3389   return v8::Utils::ToLocal(exec_state_);
3390 }
3391
3392
3393 v8::Handle<v8::Object> MessageImpl::GetEventData() const {
3394   return v8::Utils::ToLocal(event_data_);
3395 }
3396
3397
3398 v8::Handle<v8::String> MessageImpl::GetJSON() const {
3399   v8::HandleScope scope;
3400
3401   if (IsEvent()) {
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>();
3406     }
3407     bool caught_exception;
3408     Handle<Object> json = Execution::TryCall(Handle<JSFunction>::cast(fun),
3409                                              event_data_,
3410                                              0, NULL, &caught_exception);
3411     if (caught_exception || !json->IsString()) {
3412       return v8::Handle<v8::String>();
3413     }
3414     return scope.Close(v8::Utils::ToLocal(Handle<String>::cast(json)));
3415   } else {
3416     return v8::Utils::ToLocal(response_json_);
3417   }
3418 }
3419
3420
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);
3426   return context;
3427 }
3428
3429
3430 v8::Debug::ClientData* MessageImpl::GetClientData() const {
3431   return client_data_;
3432 }
3433
3434
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)
3440     : event_(event),
3441       exec_state_(exec_state),
3442       event_data_(event_data),
3443       callback_data_(callback_data),
3444       client_data_(client_data) {}
3445
3446
3447 DebugEvent EventDetailsImpl::GetEvent() const {
3448   return event_;
3449 }
3450
3451
3452 v8::Handle<v8::Object> EventDetailsImpl::GetExecutionState() const {
3453   return v8::Utils::ToLocal(exec_state_);
3454 }
3455
3456
3457 v8::Handle<v8::Object> EventDetailsImpl::GetEventData() const {
3458   return v8::Utils::ToLocal(event_data_);
3459 }
3460
3461
3462 v8::Handle<v8::Context> EventDetailsImpl::GetEventContext() const {
3463   return GetDebugEventContext(Isolate::Current());
3464 }
3465
3466
3467 v8::Handle<v8::Value> EventDetailsImpl::GetCallbackData() const {
3468   return v8::Utils::ToLocal(callback_data_);
3469 }
3470
3471
3472 v8::Debug::ClientData* EventDetailsImpl::GetClientData() const {
3473   return client_data_;
3474 }
3475
3476
3477 CommandMessage::CommandMessage() : text_(Vector<uint16_t>::empty()),
3478                                    client_data_(NULL) {
3479 }
3480
3481
3482 CommandMessage::CommandMessage(const Vector<uint16_t>& text,
3483                                v8::Debug::ClientData* data)
3484     : text_(text),
3485       client_data_(data) {
3486 }
3487
3488
3489 CommandMessage::~CommandMessage() {
3490 }
3491
3492
3493 void CommandMessage::Dispose() {
3494   text_.Dispose();
3495   delete client_data_;
3496   client_data_ = NULL;
3497 }
3498
3499
3500 CommandMessage CommandMessage::New(const Vector<uint16_t>& command,
3501                                    v8::Debug::ClientData* data) {
3502   return CommandMessage(command.Clone(), data);
3503 }
3504
3505
3506 CommandMessageQueue::CommandMessageQueue(int size) : start_(0), end_(0),
3507                                                      size_(size) {
3508   messages_ = NewArray<CommandMessage>(size);
3509 }
3510
3511
3512 CommandMessageQueue::~CommandMessageQueue() {
3513   while (!IsEmpty()) {
3514     CommandMessage m = Get();
3515     m.Dispose();
3516   }
3517   DeleteArray(messages_);
3518 }
3519
3520
3521 CommandMessage CommandMessageQueue::Get() {
3522   ASSERT(!IsEmpty());
3523   int result = start_;
3524   start_ = (start_ + 1) % size_;
3525   return messages_[result];
3526 }
3527
3528
3529 void CommandMessageQueue::Put(const CommandMessage& message) {
3530   if ((end_ + 1) % size_ == start_) {
3531     Expand();
3532   }
3533   messages_[end_] = message;
3534   end_ = (end_ + 1) % size_;
3535 }
3536
3537
3538 void CommandMessageQueue::Expand() {
3539   CommandMessageQueue new_queue(size_ * 2);
3540   while (!IsEmpty()) {
3541     new_queue.Put(Get());
3542   }
3543   CommandMessage* array_to_free = messages_;
3544   *this = new_queue;
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.
3549 }
3550
3551
3552 LockingCommandMessageQueue::LockingCommandMessageQueue(Logger* logger, int size)
3553     : logger_(logger), queue_(size) {
3554   lock_ = OS::CreateMutex();
3555 }
3556
3557
3558 LockingCommandMessageQueue::~LockingCommandMessageQueue() {
3559   delete lock_;
3560 }
3561
3562
3563 bool LockingCommandMessageQueue::IsEmpty() const {
3564   ScopedLock sl(lock_);
3565   return queue_.IsEmpty();
3566 }
3567
3568
3569 CommandMessage LockingCommandMessageQueue::Get() {
3570   ScopedLock sl(lock_);
3571   CommandMessage result = queue_.Get();
3572   logger_->DebugEvent("Get", result.text());
3573   return result;
3574 }
3575
3576
3577 void LockingCommandMessageQueue::Put(const CommandMessage& message) {
3578   ScopedLock sl(lock_);
3579   queue_.Put(message);
3580   logger_->DebugEvent("Put", message.text());
3581 }
3582
3583
3584 void LockingCommandMessageQueue::Clear() {
3585   ScopedLock sl(lock_);
3586   queue_.Clear();
3587 }
3588
3589
3590 MessageDispatchHelperThread::MessageDispatchHelperThread(Isolate* isolate)
3591     : Thread("v8:MsgDispHelpr"),
3592       sem_(OS::CreateSemaphore(0)), mutex_(OS::CreateMutex()),
3593       already_signalled_(false) {
3594 }
3595
3596
3597 MessageDispatchHelperThread::~MessageDispatchHelperThread() {
3598   delete mutex_;
3599   delete sem_;
3600 }
3601
3602
3603 void MessageDispatchHelperThread::Schedule() {
3604   {
3605     ScopedLock lock(mutex_);
3606     if (already_signalled_) {
3607       return;
3608     }
3609     already_signalled_ = true;
3610   }
3611   sem_->Signal();
3612 }
3613
3614
3615 void MessageDispatchHelperThread::Run() {
3616   while (true) {
3617     sem_->Wait();
3618     {
3619       ScopedLock lock(mutex_);
3620       already_signalled_ = false;
3621     }
3622     {
3623       Locker locker;
3624       Isolate::Current()->debugger()->CallMessageDispatchHandler();
3625     }
3626   }
3627 }
3628
3629 #endif  // ENABLE_DEBUGGER_SUPPORT
3630
3631 } }  // namespace v8::internal