deps: update v8 to 4.3.61.21
[platform/upstream/nodejs.git] / deps / v8 / src / debug.h
1 // Copyright 2012 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #ifndef V8_DEBUG_H_
6 #define V8_DEBUG_H_
7
8 #include "src/allocation.h"
9 #include "src/arguments.h"
10 #include "src/assembler.h"
11 #include "src/base/atomicops.h"
12 #include "src/base/platform/platform.h"
13 #include "src/execution.h"
14 #include "src/factory.h"
15 #include "src/flags.h"
16 #include "src/frames-inl.h"
17 #include "src/hashmap.h"
18 #include "src/liveedit.h"
19 #include "src/string-stream.h"
20 #include "src/v8threads.h"
21
22 #include "include/v8-debug.h"
23
24 namespace v8 {
25 namespace internal {
26
27
28 // Forward declarations.
29 class DebugScope;
30
31
32 // Step actions. NOTE: These values are in macros.py as well.
33 enum StepAction {
34   StepNone = -1,  // Stepping not prepared.
35   StepOut = 0,    // Step out of the current function.
36   StepNext = 1,   // Step to the next statement in the current function.
37   StepIn = 2,     // Step into new functions invoked or the next statement
38                   // in the current function.
39   StepMin = 3,    // Perform a minimum step in the current function.
40   StepInMin = 4,  // Step into new functions invoked or perform a minimum step
41                   // in the current function.
42   StepFrame = 5   // Step into a new frame or return to previous frame.
43 };
44
45
46 // Type of exception break. NOTE: These values are in macros.py as well.
47 enum ExceptionBreakType {
48   BreakException = 0,
49   BreakUncaughtException = 1
50 };
51
52
53 // Type of exception break.
54 enum BreakLocatorType {
55   ALL_BREAK_LOCATIONS = 0,
56   SOURCE_BREAK_LOCATIONS = 1,
57   CALLS_AND_RETURNS = 2
58 };
59
60
61 // The different types of breakpoint position alignments.
62 // Must match Debug.BreakPositionAlignment in debug-debugger.js
63 enum BreakPositionAlignment {
64   STATEMENT_ALIGNED = 0,
65   BREAK_POSITION_ALIGNED = 1
66 };
67
68
69 class BreakLocation {
70  public:
71   // Find the break point at the supplied address, or the closest one before
72   // the address.
73   static BreakLocation FromAddress(Handle<DebugInfo> debug_info,
74                                    BreakLocatorType type, Address pc);
75
76   static void FromAddressSameStatement(Handle<DebugInfo> debug_info,
77                                        BreakLocatorType type, Address pc,
78                                        List<BreakLocation>* result_out);
79
80   static BreakLocation FromPosition(Handle<DebugInfo> debug_info,
81                                     BreakLocatorType type, int position,
82                                     BreakPositionAlignment alignment);
83
84   bool IsDebugBreak() const;
85   inline bool IsExit() const { return RelocInfo::IsJSReturn(rmode_); }
86   inline bool IsConstructCall() const {
87     return RelocInfo::IsConstructCall(rmode_);
88   }
89   inline bool IsCodeTarget() const { return RelocInfo::IsCodeTarget(rmode_); }
90
91   Handle<Code> CodeTarget() const;
92   Handle<Code> OriginalCodeTarget() const;
93
94   bool IsStepInLocation() const;
95   inline bool HasBreakPoint() const {
96     return debug_info_->HasBreakPoint(pc_offset_);
97   }
98
99   Handle<Object> BreakPointObjects() const;
100
101   void SetBreakPoint(Handle<Object> break_point_object);
102   void ClearBreakPoint(Handle<Object> break_point_object);
103
104   void SetOneShot();
105   void ClearOneShot();
106
107   inline RelocInfo rinfo() const {
108     return RelocInfo(pc(), rmode(), data_, code());
109   }
110
111   inline RelocInfo original_rinfo() const {
112     return RelocInfo(original_pc(), original_rmode(), original_data_,
113                      original_code());
114   }
115
116   inline int position() const { return position_; }
117   inline int statement_position() const { return statement_position_; }
118
119   inline Address pc() const { return code()->entry() + pc_offset_; }
120   inline Address original_pc() const {
121     return original_code()->entry() + original_pc_offset_;
122   }
123
124   inline RelocInfo::Mode rmode() const { return rmode_; }
125   inline RelocInfo::Mode original_rmode() const { return original_rmode_; }
126
127   inline Code* code() const { return debug_info_->code(); }
128   inline Code* original_code() const { return debug_info_->original_code(); }
129
130  private:
131   BreakLocation(Handle<DebugInfo> debug_info, RelocInfo* rinfo,
132                 RelocInfo* original_rinfo, int position, int statement_position)
133       : debug_info_(debug_info),
134         pc_offset_(static_cast<int>(rinfo->pc() - debug_info->code()->entry())),
135         original_pc_offset_(static_cast<int>(
136             original_rinfo->pc() - debug_info->original_code()->entry())),
137         rmode_(rinfo->rmode()),
138         original_rmode_(original_rinfo->rmode()),
139         data_(rinfo->data()),
140         original_data_(original_rinfo->data()),
141         position_(position),
142         statement_position_(statement_position) {}
143
144   class Iterator {
145    public:
146     Iterator(Handle<DebugInfo> debug_info, BreakLocatorType type);
147
148     BreakLocation GetBreakLocation() {
149       return BreakLocation(debug_info_, rinfo(), original_rinfo(), position(),
150                            statement_position());
151     }
152
153     inline bool Done() const { return RinfoDone(); }
154     void Next();
155
156     void SkipTo(int count) {
157       while (count-- > 0) Next();
158     }
159
160     inline RelocInfo::Mode rmode() { return reloc_iterator_.rinfo()->rmode(); }
161     inline RelocInfo::Mode original_rmode() {
162       return reloc_iterator_.rinfo()->rmode();
163     }
164
165     inline RelocInfo* rinfo() { return reloc_iterator_.rinfo(); }
166     inline RelocInfo* original_rinfo() {
167       return reloc_iterator_original_.rinfo();
168     }
169
170     inline Address pc() { return rinfo()->pc(); }
171     inline Address original_pc() { return original_rinfo()->pc(); }
172
173     int break_index() const { return break_index_; }
174
175     inline int position() const { return position_; }
176     inline int statement_position() const { return statement_position_; }
177
178    private:
179     bool RinfoDone() const;
180     void RinfoNext();
181
182     Handle<DebugInfo> debug_info_;
183     BreakLocatorType type_;
184     RelocIterator reloc_iterator_;
185     RelocIterator reloc_iterator_original_;
186     int break_index_;
187     int position_;
188     int statement_position_;
189
190     DisallowHeapAllocation no_gc_;
191
192     DISALLOW_COPY_AND_ASSIGN(Iterator);
193   };
194
195   friend class Debug;
196
197   static int BreakIndexFromAddress(Handle<DebugInfo> debug_info,
198                                    BreakLocatorType type, Address pc);
199
200   void ClearDebugBreak();
201   void RestoreFromOriginal(int length_in_bytes);
202
203   void SetDebugBreak();
204   void SetDebugBreakAtReturn();
205   void SetDebugBreakAtSlot();
206   void SetDebugBreakAtIC();
207
208   inline bool IsDebuggerStatement() const {
209     return RelocInfo::IsDebuggerStatement(rmode_);
210   }
211   inline bool IsDebugBreakSlot() const {
212     return RelocInfo::IsDebugBreakSlot(rmode_);
213   }
214
215   Handle<DebugInfo> debug_info_;
216   int pc_offset_;
217   int original_pc_offset_;
218   RelocInfo::Mode rmode_;
219   RelocInfo::Mode original_rmode_;
220   intptr_t data_;
221   intptr_t original_data_;
222   int position_;
223   int statement_position_;
224 };
225
226
227 // Cache of all script objects in the heap. When a script is added a weak handle
228 // to it is created and that weak handle is stored in the cache. The weak handle
229 // callback takes care of removing the script from the cache. The key used in
230 // the cache is the script id.
231 class ScriptCache : private HashMap {
232  public:
233   explicit ScriptCache(Isolate* isolate);
234   virtual ~ScriptCache() { Clear(); }
235
236   // Add script to the cache.
237   void Add(Handle<Script> script);
238
239   // Return the scripts in the cache.
240   Handle<FixedArray> GetScripts();
241
242  private:
243   // Calculate the hash value from the key (script id).
244   static uint32_t Hash(int key) {
245     return ComputeIntegerHash(key, v8::internal::kZeroHashSeed);
246   }
247
248   // Clear the cache releasing all the weak handles.
249   void Clear();
250
251   // Weak handle callback for scripts in the cache.
252   static void HandleWeakScript(
253       const v8::WeakCallbackData<v8::Value, void>& data);
254
255   Isolate* isolate_;
256 };
257
258
259 // Linked list holding debug info objects. The debug info objects are kept as
260 // weak handles to avoid a debug info object to keep a function alive.
261 class DebugInfoListNode {
262  public:
263   explicit DebugInfoListNode(DebugInfo* debug_info);
264   virtual ~DebugInfoListNode() { ClearInfo(); }
265
266   DebugInfoListNode* next() { return next_; }
267   void set_next(DebugInfoListNode* next) { next_ = next; }
268   Handle<DebugInfo> debug_info() { return Handle<DebugInfo>(debug_info_); }
269
270   void ClearInfo();
271
272  private:
273   // Global (weak) handle to the debug info object.
274   DebugInfo** debug_info_;
275
276   // Next pointer for linked list.
277   DebugInfoListNode* next_;
278 };
279
280
281
282 // Message delivered to the message handler callback. This is either a debugger
283 // event or the response to a command.
284 class MessageImpl: public v8::Debug::Message {
285  public:
286   // Create a message object for a debug event.
287   static MessageImpl NewEvent(DebugEvent event,
288                               bool running,
289                               Handle<JSObject> exec_state,
290                               Handle<JSObject> event_data);
291
292   // Create a message object for the response to a debug command.
293   static MessageImpl NewResponse(DebugEvent event,
294                                  bool running,
295                                  Handle<JSObject> exec_state,
296                                  Handle<JSObject> event_data,
297                                  Handle<String> response_json,
298                                  v8::Debug::ClientData* client_data);
299
300   // Implementation of interface v8::Debug::Message.
301   virtual bool IsEvent() const;
302   virtual bool IsResponse() const;
303   virtual DebugEvent GetEvent() const;
304   virtual bool WillStartRunning() const;
305   virtual v8::Handle<v8::Object> GetExecutionState() const;
306   virtual v8::Handle<v8::Object> GetEventData() const;
307   virtual v8::Handle<v8::String> GetJSON() const;
308   virtual v8::Handle<v8::Context> GetEventContext() const;
309   virtual v8::Debug::ClientData* GetClientData() const;
310   virtual v8::Isolate* GetIsolate() const;
311
312  private:
313   MessageImpl(bool is_event,
314               DebugEvent event,
315               bool running,
316               Handle<JSObject> exec_state,
317               Handle<JSObject> event_data,
318               Handle<String> response_json,
319               v8::Debug::ClientData* client_data);
320
321   bool is_event_;  // Does this message represent a debug event?
322   DebugEvent event_;  // Debug event causing the break.
323   bool running_;  // Will the VM start running after this event?
324   Handle<JSObject> exec_state_;  // Current execution state.
325   Handle<JSObject> event_data_;  // Data associated with the event.
326   Handle<String> response_json_;  // Response JSON if message holds a response.
327   v8::Debug::ClientData* client_data_;  // Client data passed with the request.
328 };
329
330
331 // Details of the debug event delivered to the debug event listener.
332 class EventDetailsImpl : public v8::Debug::EventDetails {
333  public:
334   EventDetailsImpl(DebugEvent event,
335                    Handle<JSObject> exec_state,
336                    Handle<JSObject> event_data,
337                    Handle<Object> callback_data,
338                    v8::Debug::ClientData* client_data);
339   virtual DebugEvent GetEvent() const;
340   virtual v8::Handle<v8::Object> GetExecutionState() const;
341   virtual v8::Handle<v8::Object> GetEventData() const;
342   virtual v8::Handle<v8::Context> GetEventContext() const;
343   virtual v8::Handle<v8::Value> GetCallbackData() const;
344   virtual v8::Debug::ClientData* GetClientData() const;
345  private:
346   DebugEvent event_;  // Debug event causing the break.
347   Handle<JSObject> exec_state_;         // Current execution state.
348   Handle<JSObject> event_data_;         // Data associated with the event.
349   Handle<Object> callback_data_;        // User data passed with the callback
350                                         // when it was registered.
351   v8::Debug::ClientData* client_data_;  // Data passed to DebugBreakForCommand.
352 };
353
354
355 // Message send by user to v8 debugger or debugger output message.
356 // In addition to command text it may contain a pointer to some user data
357 // which are expected to be passed along with the command reponse to message
358 // handler.
359 class CommandMessage {
360  public:
361   static CommandMessage New(const Vector<uint16_t>& command,
362                             v8::Debug::ClientData* data);
363   CommandMessage();
364
365   // Deletes user data and disposes of the text.
366   void Dispose();
367   Vector<uint16_t> text() const { return text_; }
368   v8::Debug::ClientData* client_data() const { return client_data_; }
369  private:
370   CommandMessage(const Vector<uint16_t>& text,
371                  v8::Debug::ClientData* data);
372
373   Vector<uint16_t> text_;
374   v8::Debug::ClientData* client_data_;
375 };
376
377
378 // A Queue of CommandMessage objects.  A thread-safe version is
379 // LockingCommandMessageQueue, based on this class.
380 class CommandMessageQueue BASE_EMBEDDED {
381  public:
382   explicit CommandMessageQueue(int size);
383   ~CommandMessageQueue();
384   bool IsEmpty() const { return start_ == end_; }
385   CommandMessage Get();
386   void Put(const CommandMessage& message);
387   void Clear() { start_ = end_ = 0; }  // Queue is empty after Clear().
388  private:
389   // Doubles the size of the message queue, and copies the messages.
390   void Expand();
391
392   CommandMessage* messages_;
393   int start_;
394   int end_;
395   int size_;  // The size of the queue buffer.  Queue can hold size-1 messages.
396 };
397
398
399 // LockingCommandMessageQueue is a thread-safe circular buffer of CommandMessage
400 // messages.  The message data is not managed by LockingCommandMessageQueue.
401 // Pointers to the data are passed in and out. Implemented by adding a
402 // Mutex to CommandMessageQueue.  Includes logging of all puts and gets.
403 class LockingCommandMessageQueue BASE_EMBEDDED {
404  public:
405   LockingCommandMessageQueue(Logger* logger, int size);
406   bool IsEmpty() const;
407   CommandMessage Get();
408   void Put(const CommandMessage& message);
409   void Clear();
410  private:
411   Logger* logger_;
412   CommandMessageQueue queue_;
413   mutable base::Mutex mutex_;
414   DISALLOW_COPY_AND_ASSIGN(LockingCommandMessageQueue);
415 };
416
417
418 // This class contains the debugger support. The main purpose is to handle
419 // setting break points in the code.
420 //
421 // This class controls the debug info for all functions which currently have
422 // active breakpoints in them. This debug info is held in the heap root object
423 // debug_info which is a FixedArray. Each entry in this list is of class
424 // DebugInfo.
425 class Debug {
426  public:
427   // Debug event triggers.
428   void OnDebugBreak(Handle<Object> break_points_hit, bool auto_continue);
429
430   void OnThrow(Handle<Object> exception);
431   void OnPromiseReject(Handle<JSObject> promise, Handle<Object> value);
432   void OnCompileError(Handle<Script> script);
433   void OnBeforeCompile(Handle<Script> script);
434   void OnAfterCompile(Handle<Script> script);
435   void OnPromiseEvent(Handle<JSObject> data);
436   void OnAsyncTaskEvent(Handle<JSObject> data);
437
438   // API facing.
439   void SetEventListener(Handle<Object> callback, Handle<Object> data);
440   void SetMessageHandler(v8::Debug::MessageHandler handler);
441   void EnqueueCommandMessage(Vector<const uint16_t> command,
442                              v8::Debug::ClientData* client_data = NULL);
443   // Enqueue a debugger command to the command queue for event listeners.
444   void EnqueueDebugCommand(v8::Debug::ClientData* client_data = NULL);
445   MUST_USE_RESULT MaybeHandle<Object> Call(Handle<JSFunction> fun,
446                                            Handle<Object> data);
447   Handle<Context> GetDebugContext();
448   void HandleDebugBreak();
449   void ProcessDebugMessages(bool debug_command_only);
450
451   // Internal logic
452   bool Load();
453   void Break(Arguments args, JavaScriptFrame*);
454   void SetAfterBreakTarget(JavaScriptFrame* frame);
455
456   // Scripts handling.
457   Handle<FixedArray> GetLoadedScripts();
458
459   // Break point handling.
460   bool SetBreakPoint(Handle<JSFunction> function,
461                      Handle<Object> break_point_object,
462                      int* source_position);
463   bool SetBreakPointForScript(Handle<Script> script,
464                               Handle<Object> break_point_object,
465                               int* source_position,
466                               BreakPositionAlignment alignment);
467   void ClearBreakPoint(Handle<Object> break_point_object);
468   void ClearAllBreakPoints();
469   void FloodWithOneShot(Handle<JSFunction> function,
470                         BreakLocatorType type = ALL_BREAK_LOCATIONS);
471   void FloodBoundFunctionWithOneShot(Handle<JSFunction> function);
472   void FloodDefaultConstructorWithOneShot(Handle<JSFunction> function);
473   void FloodWithOneShotGeneric(Handle<JSFunction> function,
474                                Handle<Object> holder = Handle<Object>());
475   void FloodHandlerWithOneShot();
476   void ChangeBreakOnException(ExceptionBreakType type, bool enable);
477   bool IsBreakOnException(ExceptionBreakType type);
478
479   // Stepping handling.
480   void PrepareStep(StepAction step_action,
481                    int step_count,
482                    StackFrame::Id frame_id);
483   void ClearStepping();
484   void ClearStepOut();
485   bool IsStepping() { return thread_local_.step_count_ > 0; }
486   bool StepNextContinue(BreakLocation* location, JavaScriptFrame* frame);
487   bool StepInActive() { return thread_local_.step_into_fp_ != 0; }
488   void HandleStepIn(Handle<Object> function_obj, Handle<Object> holder,
489                     Address fp, bool is_constructor);
490   bool StepOutActive() { return thread_local_.step_out_fp_ != 0; }
491
492   // Purge all code objects that have no debug break slots.
493   void PrepareForBreakPoints();
494
495   // Returns whether the operation succeeded. Compilation can only be triggered
496   // if a valid closure is passed as the second argument, otherwise the shared
497   // function needs to be compiled already.
498   bool EnsureDebugInfo(Handle<SharedFunctionInfo> shared,
499                        Handle<JSFunction> function);
500   static Handle<DebugInfo> GetDebugInfo(Handle<SharedFunctionInfo> shared);
501   static bool HasDebugInfo(Handle<SharedFunctionInfo> shared);
502
503   // This function is used in FunctionNameUsing* tests.
504   Handle<Object> FindSharedFunctionInfoInScript(Handle<Script> script,
505                                                 int position);
506
507   // Returns true if the current stub call is patched to call the debugger.
508   static bool IsDebugBreak(Address addr);
509
510   static Handle<Object> GetSourceBreakLocations(
511       Handle<SharedFunctionInfo> shared,
512       BreakPositionAlignment position_aligment);
513
514   // Check whether a global object is the debug global object.
515   bool IsDebugGlobal(GlobalObject* global);
516
517   // Check whether this frame is just about to return.
518   bool IsBreakAtReturn(JavaScriptFrame* frame);
519
520   // Support for LiveEdit
521   void FramesHaveBeenDropped(StackFrame::Id new_break_frame_id,
522                              LiveEdit::FrameDropMode mode,
523                              Object** restarter_frame_function_pointer);
524
525   // Passed to MakeWeak.
526   static void HandlePhantomDebugInfo(
527       const PhantomCallbackData<DebugInfoListNode>& data);
528
529   // Threading support.
530   char* ArchiveDebug(char* to);
531   char* RestoreDebug(char* from);
532   static int ArchiveSpacePerThread();
533   void FreeThreadResources() { }
534
535   // Record function from which eval was called.
536   static void RecordEvalCaller(Handle<Script> script);
537
538   bool CheckExecutionState(int id) {
539     return !debug_context().is_null() && break_id() != 0 && break_id() == id;
540   }
541
542   // Flags and states.
543   DebugScope* debugger_entry() {
544     return reinterpret_cast<DebugScope*>(
545         base::NoBarrier_Load(&thread_local_.current_debug_scope_));
546   }
547   inline Handle<Context> debug_context() { return debug_context_; }
548   void set_live_edit_enabled(bool v) { live_edit_enabled_ = v; }
549   bool live_edit_enabled() const {
550     return FLAG_enable_liveedit && live_edit_enabled_ ;
551   }
552
553   inline bool is_active() const { return is_active_; }
554   inline bool is_loaded() const { return !debug_context_.is_null(); }
555   inline bool has_break_points() const { return has_break_points_; }
556   inline bool in_debug_scope() const {
557     return !!base::NoBarrier_Load(&thread_local_.current_debug_scope_);
558   }
559   void set_disable_break(bool v) { break_disabled_ = v; }
560
561   StackFrame::Id break_frame_id() { return thread_local_.break_frame_id_; }
562   int break_id() { return thread_local_.break_id_; }
563
564   // Support for embedding into generated code.
565   Address is_active_address() {
566     return reinterpret_cast<Address>(&is_active_);
567   }
568
569   Address after_break_target_address() {
570     return reinterpret_cast<Address>(&after_break_target_);
571   }
572
573   Address restarter_frame_function_pointer_address() {
574     Object*** address = &thread_local_.restarter_frame_function_pointer_;
575     return reinterpret_cast<Address>(address);
576   }
577
578   Address step_in_fp_addr() {
579     return reinterpret_cast<Address>(&thread_local_.step_into_fp_);
580   }
581
582   StepAction last_step_action() { return thread_local_.last_step_action_; }
583
584  private:
585   explicit Debug(Isolate* isolate);
586
587   void UpdateState();
588   void Unload();
589   void SetNextBreakId() {
590     thread_local_.break_id_ = ++thread_local_.break_count_;
591   }
592
593   // Check whether there are commands in the command queue.
594   inline bool has_commands() const { return !command_queue_.IsEmpty(); }
595   inline bool ignore_events() const { return is_suppressed_ || !is_active_; }
596   inline bool break_disabled() const {
597     return break_disabled_ || in_debug_event_listener_;
598   }
599
600   void OnException(Handle<Object> exception, Handle<Object> promise);
601
602   // Constructors for debug event objects.
603   MUST_USE_RESULT MaybeHandle<Object> MakeJSObject(
604       const char* constructor_name,
605       int argc,
606       Handle<Object> argv[]);
607   MUST_USE_RESULT MaybeHandle<Object> MakeExecutionState();
608   MUST_USE_RESULT MaybeHandle<Object> MakeBreakEvent(
609       Handle<Object> break_points_hit);
610   MUST_USE_RESULT MaybeHandle<Object> MakeExceptionEvent(
611       Handle<Object> exception,
612       bool uncaught,
613       Handle<Object> promise);
614   MUST_USE_RESULT MaybeHandle<Object> MakeCompileEvent(
615       Handle<Script> script, v8::DebugEvent type);
616   MUST_USE_RESULT MaybeHandle<Object> MakePromiseEvent(
617       Handle<JSObject> promise_event);
618   MUST_USE_RESULT MaybeHandle<Object> MakeAsyncTaskEvent(
619       Handle<JSObject> task_event);
620
621   // Mirror cache handling.
622   void ClearMirrorCache();
623
624   MaybeHandle<Object> PromiseHasUserDefinedRejectHandler(
625       Handle<JSObject> promise);
626
627   void CallEventCallback(v8::DebugEvent event,
628                          Handle<Object> exec_state,
629                          Handle<Object> event_data,
630                          v8::Debug::ClientData* client_data);
631   void ProcessCompileEventInDebugScope(v8::DebugEvent event,
632                                        Handle<Script> script);
633   void ProcessDebugEvent(v8::DebugEvent event,
634                          Handle<JSObject> event_data,
635                          bool auto_continue);
636   void NotifyMessageHandler(v8::DebugEvent event,
637                             Handle<JSObject> exec_state,
638                             Handle<JSObject> event_data,
639                             bool auto_continue);
640   void InvokeMessageHandler(MessageImpl message);
641
642   static bool CompileDebuggerScript(Isolate* isolate, int index);
643   void ClearOneShot();
644   void ActivateStepIn(StackFrame* frame);
645   void ClearStepIn();
646   void ActivateStepOut(StackFrame* frame);
647   void ClearStepNext();
648   void RemoveDebugInfoAndClearFromShared(Handle<DebugInfo> debug_info);
649   void RemoveDebugInfo(DebugInfo** debug_info);
650   void RemoveDebugInfo(DebugInfoListNode* node);
651   void RemoveDebugInfo(DebugInfoListNode* prev, DebugInfoListNode* node);
652   Handle<Object> CheckBreakPoints(Handle<Object> break_point);
653   bool CheckBreakPoint(Handle<Object> break_point_object);
654
655   inline void AssertDebugContext() {
656     DCHECK(isolate_->context() == *debug_context());
657     DCHECK(in_debug_scope());
658   }
659
660   void ThreadInit();
661
662   // Global handles.
663   Handle<Context> debug_context_;
664   Handle<Object> event_listener_;
665   Handle<Object> event_listener_data_;
666
667   v8::Debug::MessageHandler message_handler_;
668
669   static const int kQueueInitialSize = 4;
670   base::Semaphore command_received_;  // Signaled for each command received.
671   LockingCommandMessageQueue command_queue_;
672   LockingCommandMessageQueue event_command_queue_;
673
674   bool is_active_;
675   bool is_suppressed_;
676   bool live_edit_enabled_;
677   bool has_break_points_;
678   bool break_disabled_;
679   bool in_debug_event_listener_;
680   bool break_on_exception_;
681   bool break_on_uncaught_exception_;
682
683   ScriptCache* script_cache_;  // Cache of all scripts in the heap.
684   DebugInfoListNode* debug_info_list_;  // List of active debug info objects.
685
686   // Storage location for jump when exiting debug break calls.
687   // Note that this address is not GC safe.  It should be computed immediately
688   // before returning to the DebugBreakCallHelper.
689   Address after_break_target_;
690
691   // Per-thread data.
692   class ThreadLocal {
693    public:
694     // Top debugger entry.
695     base::AtomicWord current_debug_scope_;
696
697     // Counter for generating next break id.
698     int break_count_;
699
700     // Current break id.
701     int break_id_;
702
703     // Frame id for the frame of the current break.
704     StackFrame::Id break_frame_id_;
705
706     // Step action for last step performed.
707     StepAction last_step_action_;
708
709     // Source statement position from last step next action.
710     int last_statement_position_;
711
712     // Number of steps left to perform before debug event.
713     int step_count_;
714
715     // Frame pointer from last step next or step frame action.
716     Address last_fp_;
717
718     // Number of queued steps left to perform before debug event.
719     int queued_step_count_;
720
721     // Frame pointer for frame from which step in was performed.
722     Address step_into_fp_;
723
724     // Frame pointer for the frame where debugger should be called when current
725     // step out action is completed.
726     Address step_out_fp_;
727
728     // Stores the way how LiveEdit has patched the stack. It is used when
729     // debugger returns control back to user script.
730     LiveEdit::FrameDropMode frame_drop_mode_;
731
732     // When restarter frame is on stack, stores the address
733     // of the pointer to function being restarted. Otherwise (most of the time)
734     // stores NULL. This pointer is used with 'step in' implementation.
735     Object** restarter_frame_function_pointer_;
736   };
737
738   // Storage location for registers when handling debug break calls
739   ThreadLocal thread_local_;
740
741   Isolate* isolate_;
742
743   friend class Isolate;
744   friend class DebugScope;
745   friend class DisableBreak;
746   friend class LiveEdit;
747   friend class SuppressDebug;
748
749   friend Handle<FixedArray> GetDebuggedFunctions();  // In test-debug.cc
750   friend void CheckDebuggerUnloaded(bool check_functions);  // In test-debug.cc
751
752   DISALLOW_COPY_AND_ASSIGN(Debug);
753 };
754
755
756 DECLARE_RUNTIME_FUNCTION(Debug_Break);
757
758
759 // This scope is used to load and enter the debug context and create a new
760 // break state.  Leaving the scope will restore the previous state.
761 // On failure to load, FailedToEnter returns true.
762 class DebugScope BASE_EMBEDDED {
763  public:
764   explicit DebugScope(Debug* debug);
765   ~DebugScope();
766
767   // Check whether loading was successful.
768   inline bool failed() { return failed_; }
769
770   // Get the active context from before entering the debugger.
771   inline Handle<Context> GetContext() { return save_.context(); }
772
773  private:
774   Isolate* isolate() { return debug_->isolate_; }
775
776   Debug* debug_;
777   DebugScope* prev_;               // Previous scope if entered recursively.
778   StackFrame::Id break_frame_id_;  // Previous break frame id.
779   int break_id_;                   // Previous break id.
780   bool failed_;                    // Did the debug context fail to load?
781   SaveContext save_;               // Saves previous context.
782   PostponeInterruptsScope no_termination_exceptons_;
783 };
784
785
786 // Stack allocated class for disabling break.
787 class DisableBreak BASE_EMBEDDED {
788  public:
789   explicit DisableBreak(Debug* debug, bool disable_break)
790       : debug_(debug),
791         previous_break_disabled_(debug->break_disabled_),
792         previous_in_debug_event_listener_(debug->in_debug_event_listener_) {
793     debug_->break_disabled_ = disable_break;
794     debug_->in_debug_event_listener_ = disable_break;
795   }
796   ~DisableBreak() {
797     debug_->break_disabled_ = previous_break_disabled_;
798     debug_->in_debug_event_listener_ = previous_in_debug_event_listener_;
799   }
800
801  private:
802   Debug* debug_;
803   bool previous_break_disabled_;
804   bool previous_in_debug_event_listener_;
805   DISALLOW_COPY_AND_ASSIGN(DisableBreak);
806 };
807
808
809 class SuppressDebug BASE_EMBEDDED {
810  public:
811   explicit SuppressDebug(Debug* debug)
812       : debug_(debug), old_state_(debug->is_suppressed_) {
813     debug_->is_suppressed_ = true;
814   }
815   ~SuppressDebug() { debug_->is_suppressed_ = old_state_; }
816
817  private:
818   Debug* debug_;
819   bool old_state_;
820   DISALLOW_COPY_AND_ASSIGN(SuppressDebug);
821 };
822
823
824 // Code generator routines.
825 class DebugCodegen : public AllStatic {
826  public:
827   static void GenerateSlot(MacroAssembler* masm);
828   static void GenerateCallICStubDebugBreak(MacroAssembler* masm);
829   static void GenerateLoadICDebugBreak(MacroAssembler* masm);
830   static void GenerateStoreICDebugBreak(MacroAssembler* masm);
831   static void GenerateKeyedLoadICDebugBreak(MacroAssembler* masm);
832   static void GenerateKeyedStoreICDebugBreak(MacroAssembler* masm);
833   static void GenerateCompareNilICDebugBreak(MacroAssembler* masm);
834   static void GenerateReturnDebugBreak(MacroAssembler* masm);
835   static void GenerateCallFunctionStubDebugBreak(MacroAssembler* masm);
836   static void GenerateCallConstructStubDebugBreak(MacroAssembler* masm);
837   static void GenerateCallConstructStubRecordDebugBreak(MacroAssembler* masm);
838   static void GenerateSlotDebugBreak(MacroAssembler* masm);
839   static void GeneratePlainReturnLiveEdit(MacroAssembler* masm);
840
841   // FrameDropper is a code replacement for a JavaScript frame with possibly
842   // several frames above.
843   // There is no calling conventions here, because it never actually gets
844   // called, it only gets returned to.
845   static void GenerateFrameDropperLiveEdit(MacroAssembler* masm);
846 };
847
848
849 } }  // namespace v8::internal
850
851 #endif  // V8_DEBUG_H_