blob: b6aba5aad1bf65ebffd6ec29bce0c7ea7254b640 [file] [log] [blame]
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001// Copyright 2006-2008 the V8 project authors. All rights reserved.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002// 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
ager@chromium.org5ec48922009-05-05 07:25:34 +000028#ifndef V8_DEBUG_H_
29#define V8_DEBUG_H_
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000030
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000031#include "assembler.h"
32#include "code-stubs.h"
kasperl@chromium.org7be3c992009-03-12 07:19:55 +000033#include "debug-agent.h"
kasperl@chromium.org41044eb2008-10-06 08:24:46 +000034#include "execution.h"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000035#include "factory.h"
kasperl@chromium.org71affb52009-05-26 05:44:31 +000036#include "hashmap.h"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000037#include "platform.h"
38#include "string-stream.h"
kasperl@chromium.org7be3c992009-03-12 07:19:55 +000039#include "v8threads.h"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000040
ager@chromium.org65dad4b2009-04-23 08:48:43 +000041#ifdef ENABLE_DEBUGGER_SUPPORT
42#include "../include/v8-debug.h"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000043
kasperl@chromium.org71affb52009-05-26 05:44:31 +000044namespace v8 {
45namespace internal {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000046
kasperl@chromium.org7be3c992009-03-12 07:19:55 +000047
48// Forward declarations.
49class EnterDebugger;
50
51
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000052// Step actions. NOTE: These values are in macros.py as well.
53enum StepAction {
54 StepNone = -1, // Stepping not prepared.
55 StepOut = 0, // Step out of the current function.
56 StepNext = 1, // Step to the next statement in the current function.
57 StepIn = 2, // Step into new functions invoked or the next statement
58 // in the current function.
59 StepMin = 3, // Perform a minimum step in the current function.
60 StepInMin = 4 // Step into new functions invoked or perform a minimum step
61 // in the current function.
62};
63
64
65// Type of exception break. NOTE: These values are in macros.py as well.
66enum ExceptionBreakType {
67 BreakException = 0,
68 BreakUncaughtException = 1
69};
70
71
72// Type of exception break. NOTE: These values are in macros.py as well.
73enum BreakLocatorType {
74 ALL_BREAK_LOCATIONS = 0,
75 SOURCE_BREAK_LOCATIONS = 1
76};
77
78
79// Class for iterating through the break points in a function and changing
80// them.
81class BreakLocationIterator {
82 public:
83 explicit BreakLocationIterator(Handle<DebugInfo> debug_info,
84 BreakLocatorType type);
85 virtual ~BreakLocationIterator();
86
87 void Next();
88 void Next(int count);
89 void FindBreakLocationFromAddress(Address pc);
90 void FindBreakLocationFromPosition(int position);
91 void Reset();
92 bool Done() const;
93 void SetBreakPoint(Handle<Object> break_point_object);
94 void ClearBreakPoint(Handle<Object> break_point_object);
95 void SetOneShot();
96 void ClearOneShot();
97 void PrepareStepIn();
98 bool IsExit() const;
99 bool HasBreakPoint();
100 bool IsDebugBreak();
101 Object* BreakPointObjects();
ager@chromium.org381abbb2009-02-25 13:23:22 +0000102 void ClearAllDebugBreak();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000103
104
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000105 inline int code_position() {
106 return static_cast<int>(pc() - debug_info_->code()->entry());
107 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000108 inline int break_point() { return break_point_; }
109 inline int position() { return position_; }
110 inline int statement_position() { return statement_position_; }
111 inline Address pc() { return reloc_iterator_->rinfo()->pc(); }
112 inline Code* code() { return debug_info_->code(); }
113 inline RelocInfo* rinfo() { return reloc_iterator_->rinfo(); }
ager@chromium.org236ad962008-09-25 09:45:57 +0000114 inline RelocInfo::Mode rmode() const {
115 return reloc_iterator_->rinfo()->rmode();
116 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000117 inline RelocInfo* original_rinfo() {
118 return reloc_iterator_original_->rinfo();
119 }
ager@chromium.org236ad962008-09-25 09:45:57 +0000120 inline RelocInfo::Mode original_rmode() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000121 return reloc_iterator_original_->rinfo()->rmode();
122 }
123
ager@chromium.orga1645e22009-09-09 19:27:10 +0000124 bool IsDebuggerStatement();
125
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000126 protected:
127 bool RinfoDone() const;
128 void RinfoNext();
129
130 BreakLocatorType type_;
131 int break_point_;
132 int position_;
133 int statement_position_;
134 Handle<DebugInfo> debug_info_;
135 RelocIterator* reloc_iterator_;
136 RelocIterator* reloc_iterator_original_;
137
138 private:
139 void SetDebugBreak();
140 void ClearDebugBreak();
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000141
142 void SetDebugBreakAtIC();
143 void ClearDebugBreakAtIC();
144
iposva@chromium.org245aa852009-02-10 00:49:54 +0000145 bool IsDebugBreakAtReturn();
146 void SetDebugBreakAtReturn();
147 void ClearDebugBreakAtReturn();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000148
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000149 bool IsDebugBreakSlot();
150 bool IsDebugBreakAtSlot();
151 void SetDebugBreakAtSlot();
152 void ClearDebugBreakAtSlot();
153
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000154 DISALLOW_COPY_AND_ASSIGN(BreakLocationIterator);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000155};
156
157
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000158// Cache of all script objects in the heap. When a script is added a weak handle
159// to it is created and that weak handle is stored in the cache. The weak handle
160// callback takes care of removing the script from the cache. The key used in
161// the cache is the script id.
162class ScriptCache : private HashMap {
163 public:
164 ScriptCache() : HashMap(ScriptMatch), collected_scripts_(10) {}
165 virtual ~ScriptCache() { Clear(); }
166
167 // Add script to the cache.
168 void Add(Handle<Script> script);
169
170 // Return the scripts in the cache.
171 Handle<FixedArray> GetScripts();
172
173 // Generate debugger events for collected scripts.
174 void ProcessCollectedScripts();
175
176 private:
177 // Calculate the hash value from the key (script id).
178 static uint32_t Hash(int key) { return ComputeIntegerHash(key); }
179
180 // Scripts match if their keys (script id) match.
181 static bool ScriptMatch(void* key1, void* key2) { return key1 == key2; }
182
183 // Clear the cache releasing all the weak handles.
184 void Clear();
185
186 // Weak handle callback for scripts in the cache.
187 static void HandleWeakScript(v8::Persistent<v8::Value> obj, void* data);
188
189 // List used during GC to temporarily store id's of collected scripts.
190 List<int> collected_scripts_;
191};
192
193
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000194// Linked list holding debug info objects. The debug info objects are kept as
195// weak handles to avoid a debug info object to keep a function alive.
196class DebugInfoListNode {
197 public:
198 explicit DebugInfoListNode(DebugInfo* debug_info);
199 virtual ~DebugInfoListNode();
200
201 DebugInfoListNode* next() { return next_; }
202 void set_next(DebugInfoListNode* next) { next_ = next; }
203 Handle<DebugInfo> debug_info() { return debug_info_; }
204
205 private:
206 // Global (weak) handle to the debug info object.
207 Handle<DebugInfo> debug_info_;
208
209 // Next pointer for linked list.
210 DebugInfoListNode* next_;
211};
212
213
214// This class contains the debugger support. The main purpose is to handle
215// setting break points in the code.
216//
217// This class controls the debug info for all functions which currently have
218// active breakpoints in them. This debug info is held in the heap root object
219// debug_info which is a FixedArray. Each entry in this list is of class
220// DebugInfo.
221class Debug {
222 public:
223 static void Setup(bool create_heap_objects);
224 static bool Load();
225 static void Unload();
226 static bool IsLoaded() { return !debug_context_.is_null(); }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000227 static bool InDebugger() { return thread_local_.debugger_entry_ != NULL; }
228 static void PreemptionWhileInDebugger();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000229 static void Iterate(ObjectVisitor* v);
230
231 static Object* Break(Arguments args);
232 static void SetBreakPoint(Handle<SharedFunctionInfo> shared,
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +0000233 Handle<Object> break_point_object,
234 int* source_position);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000235 static void ClearBreakPoint(Handle<Object> break_point_object);
ager@chromium.org381abbb2009-02-25 13:23:22 +0000236 static void ClearAllBreakPoints();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000237 static void FloodWithOneShot(Handle<SharedFunctionInfo> shared);
238 static void FloodHandlerWithOneShot();
239 static void ChangeBreakOnException(ExceptionBreakType type, bool enable);
240 static void PrepareStep(StepAction step_action, int step_count);
241 static void ClearStepping();
242 static bool StepNextContinue(BreakLocationIterator* break_location_iterator,
243 JavaScriptFrame* frame);
244 static Handle<DebugInfo> GetDebugInfo(Handle<SharedFunctionInfo> shared);
245 static bool HasDebugInfo(Handle<SharedFunctionInfo> shared);
kasper.lundbd3ec4e2008-07-09 11:06:54 +0000246
ager@chromium.org32912102009-01-16 10:38:43 +0000247 // Returns whether the operation succeeded.
kasper.lundbd3ec4e2008-07-09 11:06:54 +0000248 static bool EnsureDebugInfo(Handle<SharedFunctionInfo> shared);
249
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000250 // Returns true if the current stub call is patched to call the debugger.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000251 static bool IsDebugBreak(Address addr);
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000252 // Returns true if the current return statement has been patched to be
253 // a debugger breakpoint.
ager@chromium.org381abbb2009-02-25 13:23:22 +0000254 static bool IsDebugBreakAtReturn(RelocInfo* rinfo);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000255
256 // Check whether a code stub with the specified major key is a possible break
257 // point location.
258 static bool IsSourceBreakStub(Code* code);
259 static bool IsBreakStub(Code* code);
260
261 // Find the builtin to use for invoking the debug break
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000262 static Handle<Code> FindDebugBreak(Handle<Code> code, RelocInfo::Mode mode);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000263
264 static Handle<Object> GetSourceBreakLocations(
265 Handle<SharedFunctionInfo> shared);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000266
267 // Getter for the debug_context.
268 inline static Handle<Context> debug_context() { return debug_context_; }
269
270 // Check whether a global object is the debug global object.
271 static bool IsDebugGlobal(GlobalObject* global);
272
ager@chromium.org2cc82ae2010-06-14 07:35:38 +0000273 // Check whether this frame is just about to return.
274 static bool IsBreakAtReturn(JavaScriptFrame* frame);
275
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000276 // Fast check to see if any break points are active.
277 inline static bool has_break_points() { return has_break_points_; }
278
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000279 static void NewBreak(StackFrame::Id break_frame_id);
280 static void SetBreak(StackFrame::Id break_frame_id, int break_id);
281 static StackFrame::Id break_frame_id() {
282 return thread_local_.break_frame_id_;
283 }
284 static int break_id() { return thread_local_.break_id_; }
285
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000286 static bool StepInActive() { return thread_local_.step_into_fp_ != 0; }
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000287 static void HandleStepIn(Handle<JSFunction> function,
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000288 Handle<Object> holder,
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000289 Address fp,
290 bool is_constructor);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000291 static Address step_in_fp() { return thread_local_.step_into_fp_; }
292 static Address* step_in_fp_addr() { return &thread_local_.step_into_fp_; }
293
ager@chromium.orga1645e22009-09-09 19:27:10 +0000294 static bool StepOutActive() { return thread_local_.step_out_fp_ != 0; }
295 static Address step_out_fp() { return thread_local_.step_out_fp_; }
296
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000297 static EnterDebugger* debugger_entry() {
298 return thread_local_.debugger_entry_;
299 }
300 static void set_debugger_entry(EnterDebugger* entry) {
301 thread_local_.debugger_entry_ = entry;
302 }
303
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000304 // Check whether any of the specified interrupts are pending.
305 static bool is_interrupt_pending(InterruptFlag what) {
306 return (thread_local_.pending_interrupts_ & what) != 0;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000307 }
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000308
309 // Set specified interrupts as pending.
310 static void set_interrupts_pending(InterruptFlag what) {
311 thread_local_.pending_interrupts_ |= what;
312 }
313
314 // Clear specified interrupts from pending.
315 static void clear_interrupt_pending(InterruptFlag what) {
316 thread_local_.pending_interrupts_ &= ~static_cast<int>(what);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000317 }
318
kasper.lundbd3ec4e2008-07-09 11:06:54 +0000319 // Getter and setter for the disable break state.
320 static bool disable_break() { return disable_break_; }
321 static void set_disable_break(bool disable_break) {
322 disable_break_ = disable_break;
323 }
324
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000325 // Getters for the current exception break state.
326 static bool break_on_exception() { return break_on_exception_; }
327 static bool break_on_uncaught_exception() {
328 return break_on_uncaught_exception_;
329 }
330
331 enum AddressId {
332 k_after_break_target_address,
333 k_debug_break_return_address,
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000334 k_debug_break_slot_address,
whesse@chromium.orge90029b2010-08-02 11:52:17 +0000335 k_restarter_frame_function_pointer,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000336 k_register_address
337 };
338
339 // Support for setting the address to jump to when returning from break point.
340 static Address* after_break_target_address() {
341 return reinterpret_cast<Address*>(&thread_local_.after_break_target_);
342 }
whesse@chromium.orge90029b2010-08-02 11:52:17 +0000343 static Address* restarter_frame_function_pointer_address() {
344 Object*** address = &thread_local_.restarter_frame_function_pointer_;
345 return reinterpret_cast<Address*>(address);
346 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000347
348 // Support for saving/restoring registers when handling debug break calls.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000349 static Object** register_address(int r) {
350 return &registers_[r];
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000351 }
352
ager@chromium.orga1645e22009-09-09 19:27:10 +0000353 // Access to the debug break on return code.
354 static Code* debug_break_return() { return debug_break_return_; }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000355 static Code** debug_break_return_address() {
356 return &debug_break_return_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000357 }
358
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000359 // Access to the debug break in debug break slot code.
360 static Code* debug_break_slot() { return debug_break_slot_; }
361 static Code** debug_break_slot_address() {
362 return &debug_break_slot_;
363 }
364
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000365 static const int kEstimatedNofDebugInfoEntries = 16;
366 static const int kEstimatedNofBreakPointsInFunction = 16;
367
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000368 static void HandleWeakDebugInfo(v8::Persistent<v8::Value> obj, void* data);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000369
370 friend class Debugger;
ager@chromium.org381abbb2009-02-25 13:23:22 +0000371 friend Handle<FixedArray> GetDebuggedFunctions(); // In test-debug.cc
372 friend void CheckDebuggerUnloaded(bool check_functions); // In test-debug.cc
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000373
374 // Threading support.
375 static char* ArchiveDebug(char* to);
376 static char* RestoreDebug(char* from);
377 static int ArchiveSpacePerThread();
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000378 static void FreeThreadResources() { }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000379
ager@chromium.org32912102009-01-16 10:38:43 +0000380 // Mirror cache handling.
381 static void ClearMirrorCache();
382
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000383 // Script cache handling.
384 static void CreateScriptCache();
385 static void DestroyScriptCache();
386 static void AddScriptToScriptCache(Handle<Script> script);
387 static Handle<FixedArray> GetLoadedScripts();
388
389 // Garbage collection notifications.
390 static void AfterGarbageCollection();
391
ager@chromium.org8bb60582008-12-11 12:02:20 +0000392 // Code generator routines.
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000393 static void GenerateSlot(MacroAssembler* masm);
ager@chromium.org8bb60582008-12-11 12:02:20 +0000394 static void GenerateLoadICDebugBreak(MacroAssembler* masm);
395 static void GenerateStoreICDebugBreak(MacroAssembler* masm);
396 static void GenerateKeyedLoadICDebugBreak(MacroAssembler* masm);
397 static void GenerateKeyedStoreICDebugBreak(MacroAssembler* masm);
398 static void GenerateConstructCallDebugBreak(MacroAssembler* masm);
399 static void GenerateReturnDebugBreak(MacroAssembler* masm);
ager@chromium.org8bb60582008-12-11 12:02:20 +0000400 static void GenerateStubNoRegistersDebugBreak(MacroAssembler* masm);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000401 static void GenerateSlotDebugBreak(MacroAssembler* masm);
ager@chromium.org357bf652010-04-12 11:30:10 +0000402 static void GeneratePlainReturnLiveEdit(MacroAssembler* masm);
403 static void GenerateFrameDropperLiveEdit(MacroAssembler* masm);
ager@chromium.org8bb60582008-12-11 12:02:20 +0000404
405 // Called from stub-cache.cc.
406 static void GenerateCallICDebugBreak(MacroAssembler* masm);
407
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000408 // Describes how exactly a frame has been dropped from stack.
409 enum FrameDropMode {
410 // No frame has been dropped.
411 FRAMES_UNTOUCHED,
412 // The top JS frame had been calling IC stub. IC stub mustn't be called now.
413 FRAME_DROPPED_IN_IC_CALL,
414 // The top JS frame had been calling debug break slot stub. Patch the
415 // address this stub jumps to in the end.
416 FRAME_DROPPED_IN_DEBUG_SLOT_CALL,
417 // The top JS frame had been calling some C++ function. The return address
418 // gets patched automatically.
419 FRAME_DROPPED_IN_DIRECT_CALL
420 };
421
422 static void FramesHaveBeenDropped(StackFrame::Id new_break_frame_id,
whesse@chromium.orge90029b2010-08-02 11:52:17 +0000423 FrameDropMode mode,
424 Object** restarter_frame_function_pointer);
ager@chromium.org357bf652010-04-12 11:30:10 +0000425
whesse@chromium.orge90029b2010-08-02 11:52:17 +0000426 // Initializes an artificial stack frame. The data it contains is used for:
427 // a. successful work of frame dropper code which eventually gets control,
428 // b. being compatible with regular stack structure for various stack
429 // iterators.
430 // Returns address of stack allocated pointer to restarted function,
431 // the value that is called 'restarter_frame_function_pointer'. The value
432 // at this address (possibly updated by GC) may be used later when preparing
433 // 'step in' operation.
434 // The implementation is architecture-specific.
435 // TODO(LiveEdit): consider reviewing it as architecture-independent.
436 static Object** SetUpFrameDropperFrame(StackFrame* bottom_js_frame,
437 Handle<Code> code);
438
ager@chromium.org357bf652010-04-12 11:30:10 +0000439 static const int kFrameDropperFrameSize;
440
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000441 private:
442 static bool CompileDebuggerScript(int index);
443 static void ClearOneShot();
444 static void ActivateStepIn(StackFrame* frame);
445 static void ClearStepIn();
ager@chromium.orga1645e22009-09-09 19:27:10 +0000446 static void ActivateStepOut(StackFrame* frame);
447 static void ClearStepOut();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000448 static void ClearStepNext();
ager@chromium.org32912102009-01-16 10:38:43 +0000449 // Returns whether the compile succeeded.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000450 static void RemoveDebugInfo(Handle<DebugInfo> debug_info);
451 static void SetAfterBreakTarget(JavaScriptFrame* frame);
452 static Handle<Object> CheckBreakPoints(Handle<Object> break_point);
453 static bool CheckBreakPoint(Handle<Object> break_point_object);
454
455 // Global handle to debug context where all the debugger JavaScript code is
456 // loaded.
457 static Handle<Context> debug_context_;
458
459 // Boolean state indicating whether any break points are set.
460 static bool has_break_points_;
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000461
462 // Cache of all scripts in the heap.
463 static ScriptCache* script_cache_;
464
465 // List of active debug info objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000466 static DebugInfoListNode* debug_info_list_;
467
kasper.lundbd3ec4e2008-07-09 11:06:54 +0000468 static bool disable_break_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000469 static bool break_on_exception_;
470 static bool break_on_uncaught_exception_;
471
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000472 // Per-thread data.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000473 class ThreadLocal {
474 public:
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000475 // Counter for generating next break id.
476 int break_count_;
477
478 // Current break id.
479 int break_id_;
480
481 // Frame id for the frame of the current break.
482 StackFrame::Id break_frame_id_;
483
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000484 // Step action for last step performed.
485 StepAction last_step_action_;
486
487 // Source statement position from last step next action.
488 int last_statement_position_;
489
490 // Number of steps left to perform before debug event.
491 int step_count_;
492
493 // Frame pointer from last step next action.
494 Address last_fp_;
495
496 // Frame pointer for frame from which step in was performed.
497 Address step_into_fp_;
498
ager@chromium.orga1645e22009-09-09 19:27:10 +0000499 // Frame pointer for the frame where debugger should be called when current
500 // step out action is completed.
501 Address step_out_fp_;
502
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000503 // Storage location for jump when exiting debug break calls.
504 Address after_break_target_;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000505
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000506 // Stores the way how LiveEdit has patched the stack. It is used when
507 // debugger returns control back to user script.
508 FrameDropMode frame_drop_mode_;
ager@chromium.org357bf652010-04-12 11:30:10 +0000509
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000510 // Top debugger entry.
511 EnterDebugger* debugger_entry_;
512
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000513 // Pending interrupts scheduled while debugging.
514 int pending_interrupts_;
whesse@chromium.orge90029b2010-08-02 11:52:17 +0000515
516 // When restarter frame is on stack, stores the address
517 // of the pointer to function being restarted. Otherwise (most of the time)
518 // stores NULL. This pointer is used with 'step in' implementation.
519 Object** restarter_frame_function_pointer_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000520 };
521
522 // Storage location for registers when handling debug break calls
523 static JSCallerSavedBuffer registers_;
524 static ThreadLocal thread_local_;
525 static void ThreadInit();
526
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000527 // Code to call for handling debug break on return.
528 static Code* debug_break_return_;
529
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000530 // Code to call for handling debug break in debug break slots.
531 static Code* debug_break_slot_;
532
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000533 DISALLOW_COPY_AND_ASSIGN(Debug);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000534};
535
536
ager@chromium.org5ec48922009-05-05 07:25:34 +0000537// Message delivered to the message handler callback. This is either a debugger
538// event or the response to a command.
539class MessageImpl: public v8::Debug::Message {
540 public:
541 // Create a message object for a debug event.
542 static MessageImpl NewEvent(DebugEvent event,
543 bool running,
544 Handle<JSObject> exec_state,
545 Handle<JSObject> event_data);
546
547 // Create a message object for the response to a debug command.
548 static MessageImpl NewResponse(DebugEvent event,
549 bool running,
550 Handle<JSObject> exec_state,
551 Handle<JSObject> event_data,
552 Handle<String> response_json,
553 v8::Debug::ClientData* client_data);
554
555 // Implementation of interface v8::Debug::Message.
556 virtual bool IsEvent() const;
557 virtual bool IsResponse() const;
558 virtual DebugEvent GetEvent() const;
559 virtual bool WillStartRunning() const;
560 virtual v8::Handle<v8::Object> GetExecutionState() const;
561 virtual v8::Handle<v8::Object> GetEventData() const;
562 virtual v8::Handle<v8::String> GetJSON() const;
563 virtual v8::Handle<v8::Context> GetEventContext() const;
564 virtual v8::Debug::ClientData* GetClientData() const;
565
566 private:
567 MessageImpl(bool is_event,
568 DebugEvent event,
569 bool running,
570 Handle<JSObject> exec_state,
571 Handle<JSObject> event_data,
572 Handle<String> response_json,
573 v8::Debug::ClientData* client_data);
574
575 bool is_event_; // Does this message represent a debug event?
576 DebugEvent event_; // Debug event causing the break.
577 bool running_; // Will the VM start running after this event?
578 Handle<JSObject> exec_state_; // Current execution state.
579 Handle<JSObject> event_data_; // Data associated with the event.
580 Handle<String> response_json_; // Response JSON if message holds a response.
581 v8::Debug::ClientData* client_data_; // Client data passed with the request.
582};
583
584
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000585// Details of the debug event delivered to the debug event listener.
586class EventDetailsImpl : public v8::Debug::EventDetails {
587 public:
588 EventDetailsImpl(DebugEvent event,
589 Handle<JSObject> exec_state,
590 Handle<JSObject> event_data,
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +0000591 Handle<Object> callback_data,
592 v8::Debug::ClientData* client_data);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000593 virtual DebugEvent GetEvent() const;
594 virtual v8::Handle<v8::Object> GetExecutionState() const;
595 virtual v8::Handle<v8::Object> GetEventData() const;
596 virtual v8::Handle<v8::Context> GetEventContext() const;
597 virtual v8::Handle<v8::Value> GetCallbackData() const;
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +0000598 virtual v8::Debug::ClientData* GetClientData() const;
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000599 private:
600 DebugEvent event_; // Debug event causing the break.
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +0000601 Handle<JSObject> exec_state_; // Current execution state.
602 Handle<JSObject> event_data_; // Data associated with the event.
603 Handle<Object> callback_data_; // User data passed with the callback
604 // when it was registered.
605 v8::Debug::ClientData* client_data_; // Data passed to DebugBreakForCommand.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000606};
607
608
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000609// Message send by user to v8 debugger or debugger output message.
610// In addition to command text it may contain a pointer to some user data
611// which are expected to be passed along with the command reponse to message
612// handler.
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000613class CommandMessage {
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000614 public:
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000615 static CommandMessage New(const Vector<uint16_t>& command,
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000616 v8::Debug::ClientData* data);
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000617 CommandMessage();
618 ~CommandMessage();
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000619
620 // Deletes user data and disposes of the text.
621 void Dispose();
622 Vector<uint16_t> text() const { return text_; }
623 v8::Debug::ClientData* client_data() const { return client_data_; }
624 private:
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000625 CommandMessage(const Vector<uint16_t>& text,
626 v8::Debug::ClientData* data);
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000627
628 Vector<uint16_t> text_;
629 v8::Debug::ClientData* client_data_;
630};
631
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000632// A Queue of CommandMessage objects. A thread-safe version is
633// LockingCommandMessageQueue, based on this class.
634class CommandMessageQueue BASE_EMBEDDED {
ager@chromium.org41826e72009-03-30 13:30:57 +0000635 public:
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000636 explicit CommandMessageQueue(int size);
637 ~CommandMessageQueue();
ager@chromium.org41826e72009-03-30 13:30:57 +0000638 bool IsEmpty() const { return start_ == end_; }
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000639 CommandMessage Get();
640 void Put(const CommandMessage& message);
ager@chromium.org41826e72009-03-30 13:30:57 +0000641 void Clear() { start_ = end_ = 0; } // Queue is empty after Clear().
642 private:
643 // Doubles the size of the message queue, and copies the messages.
644 void Expand();
645
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000646 CommandMessage* messages_;
ager@chromium.org41826e72009-03-30 13:30:57 +0000647 int start_;
648 int end_;
649 int size_; // The size of the queue buffer. Queue can hold size-1 messages.
650};
651
652
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000653class MessageDispatchHelperThread;
654
655
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000656// LockingCommandMessageQueue is a thread-safe circular buffer of CommandMessage
657// messages. The message data is not managed by LockingCommandMessageQueue.
ager@chromium.org41826e72009-03-30 13:30:57 +0000658// Pointers to the data are passed in and out. Implemented by adding a
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000659// Mutex to CommandMessageQueue. Includes logging of all puts and gets.
660class LockingCommandMessageQueue BASE_EMBEDDED {
ager@chromium.org41826e72009-03-30 13:30:57 +0000661 public:
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000662 explicit LockingCommandMessageQueue(int size);
663 ~LockingCommandMessageQueue();
ager@chromium.org41826e72009-03-30 13:30:57 +0000664 bool IsEmpty() const;
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000665 CommandMessage Get();
666 void Put(const CommandMessage& message);
ager@chromium.org41826e72009-03-30 13:30:57 +0000667 void Clear();
668 private:
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000669 CommandMessageQueue queue_;
ager@chromium.org41826e72009-03-30 13:30:57 +0000670 Mutex* lock_;
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000671 DISALLOW_COPY_AND_ASSIGN(LockingCommandMessageQueue);
ager@chromium.org41826e72009-03-30 13:30:57 +0000672};
673
674
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000675class Debugger {
676 public:
677 static void DebugRequest(const uint16_t* json_request, int length);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000678
679 static Handle<Object> MakeJSObject(Vector<const char> constructor_name,
680 int argc, Object*** argv,
681 bool* caught_exception);
682 static Handle<Object> MakeExecutionState(bool* caught_exception);
683 static Handle<Object> MakeBreakEvent(Handle<Object> exec_state,
684 Handle<Object> break_points_hit,
685 bool* caught_exception);
686 static Handle<Object> MakeExceptionEvent(Handle<Object> exec_state,
687 Handle<Object> exception,
688 bool uncaught,
689 bool* caught_exception);
690 static Handle<Object> MakeNewFunctionEvent(Handle<Object> func,
691 bool* caught_exception);
692 static Handle<Object> MakeCompileEvent(Handle<Script> script,
iposva@chromium.org245aa852009-02-10 00:49:54 +0000693 bool before,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000694 bool* caught_exception);
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000695 static Handle<Object> MakeScriptCollectedEvent(int id,
696 bool* caught_exception);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000697 static void OnDebugBreak(Handle<Object> break_points_hit, bool auto_continue);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000698 static void OnException(Handle<Object> exception, bool uncaught);
699 static void OnBeforeCompile(Handle<Script> script);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000700
701 enum AfterCompileFlags {
702 NO_AFTER_COMPILE_FLAGS,
703 SEND_WHEN_DEBUGGING
704 };
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000705 static void OnAfterCompile(Handle<Script> script,
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000706 AfterCompileFlags after_compile_flags);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000707 static void OnNewFunction(Handle<JSFunction> fun);
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000708 static void OnScriptCollected(int id);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000709 static void ProcessDebugEvent(v8::DebugEvent event,
ager@chromium.org5ec48922009-05-05 07:25:34 +0000710 Handle<JSObject> event_data,
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000711 bool auto_continue);
ager@chromium.org41826e72009-03-30 13:30:57 +0000712 static void NotifyMessageHandler(v8::DebugEvent event,
ager@chromium.org5ec48922009-05-05 07:25:34 +0000713 Handle<JSObject> exec_state,
714 Handle<JSObject> event_data,
ager@chromium.org41826e72009-03-30 13:30:57 +0000715 bool auto_continue);
iposva@chromium.org245aa852009-02-10 00:49:54 +0000716 static void SetEventListener(Handle<Object> callback, Handle<Object> data);
ager@chromium.org5ec48922009-05-05 07:25:34 +0000717 static void SetMessageHandler(v8::Debug::MessageHandler2 handler);
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000718 static void SetHostDispatchHandler(v8::Debug::HostDispatchHandler handler,
719 int period);
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000720 static void SetDebugMessageDispatchHandler(
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000721 v8::Debug::DebugMessageDispatchHandler handler,
722 bool provide_locker);
ager@chromium.org41826e72009-03-30 13:30:57 +0000723
724 // Invoke the message handler function.
ager@chromium.org5ec48922009-05-05 07:25:34 +0000725 static void InvokeMessageHandler(MessageImpl message);
ager@chromium.org41826e72009-03-30 13:30:57 +0000726
727 // Add a debugger command to the command queue.
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000728 static void ProcessCommand(Vector<const uint16_t> command,
729 v8::Debug::ClientData* client_data = NULL);
ager@chromium.org41826e72009-03-30 13:30:57 +0000730
731 // Check whether there are commands in the command queue.
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000732 static bool HasCommands();
ager@chromium.org41826e72009-03-30 13:30:57 +0000733
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +0000734 // Enqueue a debugger command to the command queue for event listeners.
735 static void EnqueueDebugCommand(v8::Debug::ClientData* client_data = NULL);
736
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000737 static Handle<Object> Call(Handle<JSFunction> fun,
738 Handle<Object> data,
739 bool* pending_exception);
740
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000741 // Start the debugger agent listening on the provided port.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000742 static bool StartAgent(const char* name, int port,
743 bool wait_for_connection = false);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000744
745 // Stop the debugger agent.
746 static void StopAgent();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000747
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000748 // Blocks until the agent has started listening for connections
749 static void WaitForAgent();
750
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000751 static void CallMessageDispatchHandler();
752
lrn@chromium.orgc34f5802010-04-28 12:53:43 +0000753 static Handle<Context> GetDebugContext();
754
ager@chromium.org71daaf62009-04-01 07:22:49 +0000755 // Unload the debugger if possible. Only called when no debugger is currently
756 // active.
757 static void UnloadDebugger();
lrn@chromium.orgc34f5802010-04-28 12:53:43 +0000758 friend void ForceUnloadDebugger(); // In test-debug.cc
ager@chromium.org71daaf62009-04-01 07:22:49 +0000759
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000760 inline static bool EventActive(v8::DebugEvent event) {
ager@chromium.org71daaf62009-04-01 07:22:49 +0000761 ScopedLock with(debugger_access_);
762
763 // Check whether the message handler was been cleared.
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000764 if (debugger_unload_pending_) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000765 if (Debug::debugger_entry() == NULL) {
766 UnloadDebugger();
767 }
ager@chromium.org71daaf62009-04-01 07:22:49 +0000768 }
769
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000770 // Currently argument event is not used.
ager@chromium.org71daaf62009-04-01 07:22:49 +0000771 return !compiling_natives_ && Debugger::IsDebuggerActive();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000772 }
773
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000774 static void set_compiling_natives(bool compiling_natives) {
775 Debugger::compiling_natives_ = compiling_natives;
776 }
777 static bool compiling_natives() { return Debugger::compiling_natives_; }
mads.s.agercbaa0602008-08-14 13:41:48 +0000778 static void set_loading_debugger(bool v) { is_loading_debugger_ = v; }
779 static bool is_loading_debugger() { return Debugger::is_loading_debugger_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000780
ager@chromium.org71daaf62009-04-01 07:22:49 +0000781 static bool IsDebuggerActive();
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000782
783 private:
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +0000784 static void CallEventCallback(v8::DebugEvent event,
785 Handle<Object> exec_state,
786 Handle<Object> event_data,
787 v8::Debug::ClientData* client_data);
788 static void CallCEventCallback(v8::DebugEvent event,
789 Handle<Object> exec_state,
790 Handle<Object> event_data,
791 v8::Debug::ClientData* client_data);
792 static void CallJSEventCallback(v8::DebugEvent event,
793 Handle<Object> exec_state,
794 Handle<Object> event_data);
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000795 static void ListenersChanged();
ager@chromium.org71daaf62009-04-01 07:22:49 +0000796
797 static Mutex* debugger_access_; // Mutex guarding debugger variables.
798 static Handle<Object> event_listener_; // Global handle to listener.
iposva@chromium.org245aa852009-02-10 00:49:54 +0000799 static Handle<Object> event_listener_data_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000800 static bool compiling_natives_; // Are we compiling natives?
mads.s.agercbaa0602008-08-14 13:41:48 +0000801 static bool is_loading_debugger_; // Are we loading the debugger?
ager@chromium.org71daaf62009-04-01 07:22:49 +0000802 static bool never_unload_debugger_; // Can we unload the debugger?
ager@chromium.org5ec48922009-05-05 07:25:34 +0000803 static v8::Debug::MessageHandler2 message_handler_;
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000804 static bool debugger_unload_pending_; // Was message handler cleared?
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000805 static v8::Debug::HostDispatchHandler host_dispatch_handler_;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000806 static Mutex* dispatch_handler_access_; // Mutex guarding dispatch handler.
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000807 static v8::Debug::DebugMessageDispatchHandler debug_message_dispatch_handler_;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000808 static MessageDispatchHelperThread* message_dispatch_helper_thread_;
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000809 static int host_dispatch_micros_;
ager@chromium.org381abbb2009-02-25 13:23:22 +0000810
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000811 static DebuggerAgent* agent_;
812
ager@chromium.org41826e72009-03-30 13:30:57 +0000813 static const int kQueueInitialSize = 4;
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000814 static LockingCommandMessageQueue command_queue_;
ager@chromium.org41826e72009-03-30 13:30:57 +0000815 static Semaphore* command_received_; // Signaled for each command received.
ager@chromium.org41826e72009-03-30 13:30:57 +0000816
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +0000817 static LockingCommandMessageQueue event_command_queue_;
818
ager@chromium.org71daaf62009-04-01 07:22:49 +0000819 friend class EnterDebugger;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000820};
821
822
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000823// This class is used for entering the debugger. Create an instance in the stack
824// to enter the debugger. This will set the current break state, make sure the
825// debugger is loaded and switch to the debugger context. If the debugger for
826// some reason could not be entered FailedToEnter will return true.
827class EnterDebugger BASE_EMBEDDED {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000828 public:
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000829 EnterDebugger()
830 : prev_(Debug::debugger_entry()),
831 has_js_frames_(!it_.done()) {
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000832 ASSERT(prev_ != NULL || !Debug::is_interrupt_pending(PREEMPT));
833 ASSERT(prev_ != NULL || !Debug::is_interrupt_pending(DEBUGBREAK));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000834
835 // Link recursive debugger entry.
836 Debug::set_debugger_entry(this);
837
ager@chromium.org8bb60582008-12-11 12:02:20 +0000838 // Store the previous break id and frame id.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000839 break_id_ = Debug::break_id();
840 break_frame_id_ = Debug::break_frame_id();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000841
ager@chromium.org8bb60582008-12-11 12:02:20 +0000842 // Create the new break info. If there is no JavaScript frames there is no
843 // break frame id.
844 if (has_js_frames_) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000845 Debug::NewBreak(it_.frame()->id());
ager@chromium.org8bb60582008-12-11 12:02:20 +0000846 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000847 Debug::NewBreak(StackFrame::NO_ID);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000848 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000849
850 // Make sure that debugger is loaded and enter the debugger context.
851 load_failed_ = !Debug::Load();
852 if (!load_failed_) {
853 // NOTE the member variable save which saves the previous context before
854 // this change.
855 Top::set_context(*Debug::debug_context());
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000856 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000857 }
858
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000859 ~EnterDebugger() {
ager@chromium.org8bb60582008-12-11 12:02:20 +0000860 // Restore to the previous break state.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000861 Debug::SetBreak(break_frame_id_, break_id_);
862
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000863 // Check for leaving the debugger.
ager@chromium.org71daaf62009-04-01 07:22:49 +0000864 if (prev_ == NULL) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000865 // Clear mirror cache when leaving the debugger. Skip this if there is a
866 // pending exception as clearing the mirror cache calls back into
867 // JavaScript. This can happen if the v8::Debug::Call is used in which
868 // case the exception should end up in the calling code.
869 if (!Top::has_pending_exception()) {
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000870 // Try to avoid any pending debug break breaking in the clear mirror
871 // cache JavaScript code.
872 if (StackGuard::IsDebugBreak()) {
873 Debug::set_interrupts_pending(DEBUGBREAK);
874 StackGuard::Continue(DEBUGBREAK);
875 }
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000876 Debug::ClearMirrorCache();
877 }
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000878
879 // Request preemption and debug break when leaving the last debugger entry
880 // if any of these where recorded while debugging.
881 if (Debug::is_interrupt_pending(PREEMPT)) {
882 // This re-scheduling of preemption is to avoid starvation in some
883 // debugging scenarios.
884 Debug::clear_interrupt_pending(PREEMPT);
885 StackGuard::Preempt();
886 }
887 if (Debug::is_interrupt_pending(DEBUGBREAK)) {
888 Debug::clear_interrupt_pending(DEBUGBREAK);
889 StackGuard::DebugBreak();
890 }
891
892 // If there are commands in the queue when leaving the debugger request
893 // that these commands are processed.
894 if (Debugger::HasCommands()) {
895 StackGuard::DebugCommand();
896 }
897
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000898 // If leaving the debugger with the debugger no longer active unload it.
ager@chromium.org71daaf62009-04-01 07:22:49 +0000899 if (!Debugger::IsDebuggerActive()) {
900 Debugger::UnloadDebugger();
901 }
902 }
903
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000904 // Leaving this debugger entry.
905 Debug::set_debugger_entry(prev_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000906 }
907
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000908 // Check whether the debugger could be entered.
909 inline bool FailedToEnter() { return load_failed_; }
910
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000911 // Check whether there are any JavaScript frames on the stack.
ager@chromium.org8bb60582008-12-11 12:02:20 +0000912 inline bool HasJavaScriptFrames() { return has_js_frames_; }
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000913
ager@chromium.org9085a012009-05-11 19:22:57 +0000914 // Get the active context from before entering the debugger.
915 inline Handle<Context> GetContext() { return save_.context(); }
916
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000917 private:
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000918 EnterDebugger* prev_; // Previous debugger entry if entered recursively.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000919 JavaScriptFrameIterator it_;
ager@chromium.org8bb60582008-12-11 12:02:20 +0000920 const bool has_js_frames_; // Were there any JavaScript frames?
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000921 StackFrame::Id break_frame_id_; // Previous break frame id.
922 int break_id_; // Previous break id.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000923 bool load_failed_; // Did the debugger fail to load?
924 SaveContext save_; // Saves previous context.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000925};
926
927
kasper.lundbd3ec4e2008-07-09 11:06:54 +0000928// Stack allocated class for disabling break.
929class DisableBreak BASE_EMBEDDED {
930 public:
kasper.lundbd3ec4e2008-07-09 11:06:54 +0000931 explicit DisableBreak(bool disable_break) {
932 prev_disable_break_ = Debug::disable_break();
933 Debug::set_disable_break(disable_break);
934 }
935 ~DisableBreak() {
936 Debug::set_disable_break(prev_disable_break_);
937 }
938
939 private:
940 // The previous state of the disable break used to restore the value when this
941 // object is destructed.
942 bool prev_disable_break_;
943};
944
945
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000946// Debug_Address encapsulates the Address pointers used in generating debug
947// code.
948class Debug_Address {
949 public:
950 Debug_Address(Debug::AddressId id, int reg = 0)
951 : id_(id), reg_(reg) {
952 ASSERT(reg == 0 || id == Debug::k_register_address);
953 }
954
955 static Debug_Address AfterBreakTarget() {
956 return Debug_Address(Debug::k_after_break_target_address);
957 }
958
959 static Debug_Address DebugBreakReturn() {
960 return Debug_Address(Debug::k_debug_break_return_address);
961 }
962
whesse@chromium.orge90029b2010-08-02 11:52:17 +0000963 static Debug_Address RestarterFrameFunctionPointer() {
964 return Debug_Address(Debug::k_restarter_frame_function_pointer);
965 }
966
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000967 static Debug_Address Register(int reg) {
968 return Debug_Address(Debug::k_register_address, reg);
969 }
970
971 Address address() const {
972 switch (id_) {
973 case Debug::k_after_break_target_address:
974 return reinterpret_cast<Address>(Debug::after_break_target_address());
975 case Debug::k_debug_break_return_address:
976 return reinterpret_cast<Address>(Debug::debug_break_return_address());
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000977 case Debug::k_debug_break_slot_address:
978 return reinterpret_cast<Address>(Debug::debug_break_slot_address());
whesse@chromium.orge90029b2010-08-02 11:52:17 +0000979 case Debug::k_restarter_frame_function_pointer:
980 return reinterpret_cast<Address>(
981 Debug::restarter_frame_function_pointer_address());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000982 case Debug::k_register_address:
983 return reinterpret_cast<Address>(Debug::register_address(reg_));
984 default:
985 UNREACHABLE();
986 return NULL;
987 }
988 }
989 private:
990 Debug::AddressId id_;
991 int reg_;
992};
993
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000994// The optional thread that Debug Agent may use to temporary call V8 to process
995// pending debug requests if debuggee is not running V8 at the moment.
996// Techincally it does not call V8 itself, rather it asks embedding program
997// to do this via v8::Debug::HostDispatchHandler
998class MessageDispatchHelperThread: public Thread {
999 public:
1000 MessageDispatchHelperThread();
1001 ~MessageDispatchHelperThread();
1002
1003 void Schedule();
1004
1005 private:
1006 void Run();
1007
1008 Semaphore* const sem_;
1009 Mutex* const mutex_;
1010 bool already_signalled_;
1011
1012 DISALLOW_COPY_AND_ASSIGN(MessageDispatchHelperThread);
1013};
1014
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001015
1016} } // namespace v8::internal
1017
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001018#endif // ENABLE_DEBUGGER_SUPPORT
1019
ager@chromium.org5ec48922009-05-05 07:25:34 +00001020#endif // V8_DEBUG_H_