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