blob: 29c2bc203692e674851a0de001c22d0a4ab8eb4c [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
105 inline int code_position() { return pc() - debug_info_->code()->entry(); }
106 inline int break_point() { return break_point_; }
107 inline int position() { return position_; }
108 inline int statement_position() { return statement_position_; }
109 inline Address pc() { return reloc_iterator_->rinfo()->pc(); }
110 inline Code* code() { return debug_info_->code(); }
111 inline RelocInfo* rinfo() { return reloc_iterator_->rinfo(); }
ager@chromium.org236ad962008-09-25 09:45:57 +0000112 inline RelocInfo::Mode rmode() const {
113 return reloc_iterator_->rinfo()->rmode();
114 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000115 inline RelocInfo* original_rinfo() {
116 return reloc_iterator_original_->rinfo();
117 }
ager@chromium.org236ad962008-09-25 09:45:57 +0000118 inline RelocInfo::Mode original_rmode() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000119 return reloc_iterator_original_->rinfo()->rmode();
120 }
121
ager@chromium.orga1645e22009-09-09 19:27:10 +0000122 bool IsDebuggerStatement();
123
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000124 protected:
125 bool RinfoDone() const;
126 void RinfoNext();
127
128 BreakLocatorType type_;
129 int break_point_;
130 int position_;
131 int statement_position_;
132 Handle<DebugInfo> debug_info_;
ager@chromium.orga1645e22009-09-09 19:27:10 +0000133 Handle<Code> debug_break_stub_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000134 RelocIterator* reloc_iterator_;
135 RelocIterator* reloc_iterator_original_;
136
137 private:
138 void SetDebugBreak();
139 void ClearDebugBreak();
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000140
141 void SetDebugBreakAtIC();
142 void ClearDebugBreakAtIC();
143
iposva@chromium.org245aa852009-02-10 00:49:54 +0000144 bool IsDebugBreakAtReturn();
145 void SetDebugBreakAtReturn();
146 void ClearDebugBreakAtReturn();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000147
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000148 DISALLOW_COPY_AND_ASSIGN(BreakLocationIterator);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000149};
150
151
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000152// Cache of all script objects in the heap. When a script is added a weak handle
153// to it is created and that weak handle is stored in the cache. The weak handle
154// callback takes care of removing the script from the cache. The key used in
155// the cache is the script id.
156class ScriptCache : private HashMap {
157 public:
158 ScriptCache() : HashMap(ScriptMatch), collected_scripts_(10) {}
159 virtual ~ScriptCache() { Clear(); }
160
161 // Add script to the cache.
162 void Add(Handle<Script> script);
163
164 // Return the scripts in the cache.
165 Handle<FixedArray> GetScripts();
166
167 // Generate debugger events for collected scripts.
168 void ProcessCollectedScripts();
169
170 private:
171 // Calculate the hash value from the key (script id).
172 static uint32_t Hash(int key) { return ComputeIntegerHash(key); }
173
174 // Scripts match if their keys (script id) match.
175 static bool ScriptMatch(void* key1, void* key2) { return key1 == key2; }
176
177 // Clear the cache releasing all the weak handles.
178 void Clear();
179
180 // Weak handle callback for scripts in the cache.
181 static void HandleWeakScript(v8::Persistent<v8::Value> obj, void* data);
182
183 // List used during GC to temporarily store id's of collected scripts.
184 List<int> collected_scripts_;
185};
186
187
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000188// Linked list holding debug info objects. The debug info objects are kept as
189// weak handles to avoid a debug info object to keep a function alive.
190class DebugInfoListNode {
191 public:
192 explicit DebugInfoListNode(DebugInfo* debug_info);
193 virtual ~DebugInfoListNode();
194
195 DebugInfoListNode* next() { return next_; }
196 void set_next(DebugInfoListNode* next) { next_ = next; }
197 Handle<DebugInfo> debug_info() { return debug_info_; }
198
199 private:
200 // Global (weak) handle to the debug info object.
201 Handle<DebugInfo> debug_info_;
202
203 // Next pointer for linked list.
204 DebugInfoListNode* next_;
205};
206
207
208// This class contains the debugger support. The main purpose is to handle
209// setting break points in the code.
210//
211// This class controls the debug info for all functions which currently have
212// active breakpoints in them. This debug info is held in the heap root object
213// debug_info which is a FixedArray. Each entry in this list is of class
214// DebugInfo.
215class Debug {
216 public:
217 static void Setup(bool create_heap_objects);
218 static bool Load();
219 static void Unload();
220 static bool IsLoaded() { return !debug_context_.is_null(); }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000221 static bool InDebugger() { return thread_local_.debugger_entry_ != NULL; }
222 static void PreemptionWhileInDebugger();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000223 static void Iterate(ObjectVisitor* v);
224
225 static Object* Break(Arguments args);
226 static void SetBreakPoint(Handle<SharedFunctionInfo> shared,
227 int source_position,
228 Handle<Object> break_point_object);
229 static void ClearBreakPoint(Handle<Object> break_point_object);
ager@chromium.org381abbb2009-02-25 13:23:22 +0000230 static void ClearAllBreakPoints();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000231 static void FloodWithOneShot(Handle<SharedFunctionInfo> shared);
232 static void FloodHandlerWithOneShot();
233 static void ChangeBreakOnException(ExceptionBreakType type, bool enable);
234 static void PrepareStep(StepAction step_action, int step_count);
235 static void ClearStepping();
236 static bool StepNextContinue(BreakLocationIterator* break_location_iterator,
237 JavaScriptFrame* frame);
238 static Handle<DebugInfo> GetDebugInfo(Handle<SharedFunctionInfo> shared);
239 static bool HasDebugInfo(Handle<SharedFunctionInfo> shared);
kasper.lundbd3ec4e2008-07-09 11:06:54 +0000240
ager@chromium.org32912102009-01-16 10:38:43 +0000241 // Returns whether the operation succeeded.
kasper.lundbd3ec4e2008-07-09 11:06:54 +0000242 static bool EnsureDebugInfo(Handle<SharedFunctionInfo> shared);
243
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000244 // Returns true if the current stub call is patched to call the debugger.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000245 static bool IsDebugBreak(Address addr);
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000246 // Returns true if the current return statement has been patched to be
247 // a debugger breakpoint.
ager@chromium.org381abbb2009-02-25 13:23:22 +0000248 static bool IsDebugBreakAtReturn(RelocInfo* rinfo);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000249
250 // Check whether a code stub with the specified major key is a possible break
251 // point location.
252 static bool IsSourceBreakStub(Code* code);
253 static bool IsBreakStub(Code* code);
254
255 // Find the builtin to use for invoking the debug break
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000256 static Handle<Code> FindDebugBreak(Handle<Code> code, RelocInfo::Mode mode);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000257
258 static Handle<Object> GetSourceBreakLocations(
259 Handle<SharedFunctionInfo> shared);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000260
261 // Getter for the debug_context.
262 inline static Handle<Context> debug_context() { return debug_context_; }
263
264 // Check whether a global object is the debug global object.
265 static bool IsDebugGlobal(GlobalObject* global);
266
267 // Fast check to see if any break points are active.
268 inline static bool has_break_points() { return has_break_points_; }
269
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000270 static void NewBreak(StackFrame::Id break_frame_id);
271 static void SetBreak(StackFrame::Id break_frame_id, int break_id);
272 static StackFrame::Id break_frame_id() {
273 return thread_local_.break_frame_id_;
274 }
275 static int break_id() { return thread_local_.break_id_; }
276
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000277 static bool StepInActive() { return thread_local_.step_into_fp_ != 0; }
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000278 static void HandleStepIn(Handle<JSFunction> function,
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000279 Handle<Object> holder,
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000280 Address fp,
281 bool is_constructor);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000282 static Address step_in_fp() { return thread_local_.step_into_fp_; }
283 static Address* step_in_fp_addr() { return &thread_local_.step_into_fp_; }
284
ager@chromium.orga1645e22009-09-09 19:27:10 +0000285 static bool StepOutActive() { return thread_local_.step_out_fp_ != 0; }
286 static Address step_out_fp() { return thread_local_.step_out_fp_; }
287
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000288 static EnterDebugger* debugger_entry() {
289 return thread_local_.debugger_entry_;
290 }
291 static void set_debugger_entry(EnterDebugger* entry) {
292 thread_local_.debugger_entry_ = entry;
293 }
294
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000295 // Check whether any of the specified interrupts are pending.
296 static bool is_interrupt_pending(InterruptFlag what) {
297 return (thread_local_.pending_interrupts_ & what) != 0;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000298 }
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000299
300 // Set specified interrupts as pending.
301 static void set_interrupts_pending(InterruptFlag what) {
302 thread_local_.pending_interrupts_ |= what;
303 }
304
305 // Clear specified interrupts from pending.
306 static void clear_interrupt_pending(InterruptFlag what) {
307 thread_local_.pending_interrupts_ &= ~static_cast<int>(what);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000308 }
309
kasper.lundbd3ec4e2008-07-09 11:06:54 +0000310 // Getter and setter for the disable break state.
311 static bool disable_break() { return disable_break_; }
312 static void set_disable_break(bool disable_break) {
313 disable_break_ = disable_break;
314 }
315
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000316 // Getters for the current exception break state.
317 static bool break_on_exception() { return break_on_exception_; }
318 static bool break_on_uncaught_exception() {
319 return break_on_uncaught_exception_;
320 }
321
322 enum AddressId {
323 k_after_break_target_address,
324 k_debug_break_return_address,
325 k_register_address
326 };
327
328 // Support for setting the address to jump to when returning from break point.
329 static Address* after_break_target_address() {
330 return reinterpret_cast<Address*>(&thread_local_.after_break_target_);
331 }
332
333 // Support for saving/restoring registers when handling debug break calls.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000334 static Object** register_address(int r) {
335 return &registers_[r];
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000336 }
337
ager@chromium.orga1645e22009-09-09 19:27:10 +0000338 // Access to the debug break on return code.
339 static Code* debug_break_return() { return debug_break_return_; }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000340 static Code** debug_break_return_address() {
341 return &debug_break_return_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000342 }
343
344 static const int kEstimatedNofDebugInfoEntries = 16;
345 static const int kEstimatedNofBreakPointsInFunction = 16;
346
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000347 static void HandleWeakDebugInfo(v8::Persistent<v8::Value> obj, void* data);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000348
349 friend class Debugger;
ager@chromium.org381abbb2009-02-25 13:23:22 +0000350 friend Handle<FixedArray> GetDebuggedFunctions(); // In test-debug.cc
351 friend void CheckDebuggerUnloaded(bool check_functions); // In test-debug.cc
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000352
353 // Threading support.
354 static char* ArchiveDebug(char* to);
355 static char* RestoreDebug(char* from);
356 static int ArchiveSpacePerThread();
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000357 static void FreeThreadResources() { }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000358
ager@chromium.org32912102009-01-16 10:38:43 +0000359 // Mirror cache handling.
360 static void ClearMirrorCache();
361
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000362 // Script cache handling.
363 static void CreateScriptCache();
364 static void DestroyScriptCache();
365 static void AddScriptToScriptCache(Handle<Script> script);
366 static Handle<FixedArray> GetLoadedScripts();
367
368 // Garbage collection notifications.
369 static void AfterGarbageCollection();
370
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000371 // Code generation assumptions.
372 static const int kIa32CallInstructionLength = 5;
373 static const int kIa32JSReturnSequenceLength = 6;
374
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000375 // The x64 JS return sequence is padded with int3 to make it large
376 // enough to hold a call instruction when the debugger patches it.
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000377 static const int kX64CallInstructionLength = 13;
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000378 static const int kX64JSReturnSequenceLength = 13;
379
ager@chromium.org8bb60582008-12-11 12:02:20 +0000380 // Code generator routines.
381 static void GenerateLoadICDebugBreak(MacroAssembler* masm);
382 static void GenerateStoreICDebugBreak(MacroAssembler* masm);
383 static void GenerateKeyedLoadICDebugBreak(MacroAssembler* masm);
384 static void GenerateKeyedStoreICDebugBreak(MacroAssembler* masm);
385 static void GenerateConstructCallDebugBreak(MacroAssembler* masm);
386 static void GenerateReturnDebugBreak(MacroAssembler* masm);
ager@chromium.org8bb60582008-12-11 12:02:20 +0000387 static void GenerateStubNoRegistersDebugBreak(MacroAssembler* masm);
388
389 // Called from stub-cache.cc.
390 static void GenerateCallICDebugBreak(MacroAssembler* masm);
391
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000392 private:
393 static bool CompileDebuggerScript(int index);
394 static void ClearOneShot();
395 static void ActivateStepIn(StackFrame* frame);
396 static void ClearStepIn();
ager@chromium.orga1645e22009-09-09 19:27:10 +0000397 static void ActivateStepOut(StackFrame* frame);
398 static void ClearStepOut();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000399 static void ClearStepNext();
ager@chromium.org32912102009-01-16 10:38:43 +0000400 // Returns whether the compile succeeded.
kasper.lundbd3ec4e2008-07-09 11:06:54 +0000401 static bool EnsureCompiled(Handle<SharedFunctionInfo> shared);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000402 static void RemoveDebugInfo(Handle<DebugInfo> debug_info);
403 static void SetAfterBreakTarget(JavaScriptFrame* frame);
404 static Handle<Object> CheckBreakPoints(Handle<Object> break_point);
405 static bool CheckBreakPoint(Handle<Object> break_point_object);
406
407 // Global handle to debug context where all the debugger JavaScript code is
408 // loaded.
409 static Handle<Context> debug_context_;
410
411 // Boolean state indicating whether any break points are set.
412 static bool has_break_points_;
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000413
414 // Cache of all scripts in the heap.
415 static ScriptCache* script_cache_;
416
417 // List of active debug info objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000418 static DebugInfoListNode* debug_info_list_;
419
kasper.lundbd3ec4e2008-07-09 11:06:54 +0000420 static bool disable_break_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000421 static bool break_on_exception_;
422 static bool break_on_uncaught_exception_;
423
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000424 // Per-thread data.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000425 class ThreadLocal {
426 public:
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000427 // Counter for generating next break id.
428 int break_count_;
429
430 // Current break id.
431 int break_id_;
432
433 // Frame id for the frame of the current break.
434 StackFrame::Id break_frame_id_;
435
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000436 // Step action for last step performed.
437 StepAction last_step_action_;
438
439 // Source statement position from last step next action.
440 int last_statement_position_;
441
442 // Number of steps left to perform before debug event.
443 int step_count_;
444
445 // Frame pointer from last step next action.
446 Address last_fp_;
447
448 // Frame pointer for frame from which step in was performed.
449 Address step_into_fp_;
450
ager@chromium.orga1645e22009-09-09 19:27:10 +0000451 // Frame pointer for the frame where debugger should be called when current
452 // step out action is completed.
453 Address step_out_fp_;
454
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000455 // Storage location for jump when exiting debug break calls.
456 Address after_break_target_;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000457
458 // Top debugger entry.
459 EnterDebugger* debugger_entry_;
460
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000461 // Pending interrupts scheduled while debugging.
462 int pending_interrupts_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000463 };
464
465 // Storage location for registers when handling debug break calls
466 static JSCallerSavedBuffer registers_;
467 static ThreadLocal thread_local_;
468 static void ThreadInit();
469
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000470 // Code to call for handling debug break on return.
471 static Code* debug_break_return_;
472
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000473 DISALLOW_COPY_AND_ASSIGN(Debug);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000474};
475
476
ager@chromium.org5ec48922009-05-05 07:25:34 +0000477// Message delivered to the message handler callback. This is either a debugger
478// event or the response to a command.
479class MessageImpl: public v8::Debug::Message {
480 public:
481 // Create a message object for a debug event.
482 static MessageImpl NewEvent(DebugEvent event,
483 bool running,
484 Handle<JSObject> exec_state,
485 Handle<JSObject> event_data);
486
487 // Create a message object for the response to a debug command.
488 static MessageImpl NewResponse(DebugEvent event,
489 bool running,
490 Handle<JSObject> exec_state,
491 Handle<JSObject> event_data,
492 Handle<String> response_json,
493 v8::Debug::ClientData* client_data);
494
495 // Implementation of interface v8::Debug::Message.
496 virtual bool IsEvent() const;
497 virtual bool IsResponse() const;
498 virtual DebugEvent GetEvent() const;
499 virtual bool WillStartRunning() const;
500 virtual v8::Handle<v8::Object> GetExecutionState() const;
501 virtual v8::Handle<v8::Object> GetEventData() const;
502 virtual v8::Handle<v8::String> GetJSON() const;
503 virtual v8::Handle<v8::Context> GetEventContext() const;
504 virtual v8::Debug::ClientData* GetClientData() const;
505
506 private:
507 MessageImpl(bool is_event,
508 DebugEvent event,
509 bool running,
510 Handle<JSObject> exec_state,
511 Handle<JSObject> event_data,
512 Handle<String> response_json,
513 v8::Debug::ClientData* client_data);
514
515 bool is_event_; // Does this message represent a debug event?
516 DebugEvent event_; // Debug event causing the break.
517 bool running_; // Will the VM start running after this event?
518 Handle<JSObject> exec_state_; // Current execution state.
519 Handle<JSObject> event_data_; // Data associated with the event.
520 Handle<String> response_json_; // Response JSON if message holds a response.
521 v8::Debug::ClientData* client_data_; // Client data passed with the request.
522};
523
524
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000525// Message send by user to v8 debugger or debugger output message.
526// In addition to command text it may contain a pointer to some user data
527// which are expected to be passed along with the command reponse to message
528// handler.
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000529class CommandMessage {
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000530 public:
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000531 static CommandMessage New(const Vector<uint16_t>& command,
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000532 v8::Debug::ClientData* data);
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000533 CommandMessage();
534 ~CommandMessage();
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000535
536 // Deletes user data and disposes of the text.
537 void Dispose();
538 Vector<uint16_t> text() const { return text_; }
539 v8::Debug::ClientData* client_data() const { return client_data_; }
540 private:
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000541 CommandMessage(const Vector<uint16_t>& text,
542 v8::Debug::ClientData* data);
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000543
544 Vector<uint16_t> text_;
545 v8::Debug::ClientData* client_data_;
546};
547
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000548// A Queue of CommandMessage objects. A thread-safe version is
549// LockingCommandMessageQueue, based on this class.
550class CommandMessageQueue BASE_EMBEDDED {
ager@chromium.org41826e72009-03-30 13:30:57 +0000551 public:
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000552 explicit CommandMessageQueue(int size);
553 ~CommandMessageQueue();
ager@chromium.org41826e72009-03-30 13:30:57 +0000554 bool IsEmpty() const { return start_ == end_; }
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000555 CommandMessage Get();
556 void Put(const CommandMessage& message);
ager@chromium.org41826e72009-03-30 13:30:57 +0000557 void Clear() { start_ = end_ = 0; } // Queue is empty after Clear().
558 private:
559 // Doubles the size of the message queue, and copies the messages.
560 void Expand();
561
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000562 CommandMessage* messages_;
ager@chromium.org41826e72009-03-30 13:30:57 +0000563 int start_;
564 int end_;
565 int size_; // The size of the queue buffer. Queue can hold size-1 messages.
566};
567
568
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000569// LockingCommandMessageQueue is a thread-safe circular buffer of CommandMessage
570// messages. The message data is not managed by LockingCommandMessageQueue.
ager@chromium.org41826e72009-03-30 13:30:57 +0000571// Pointers to the data are passed in and out. Implemented by adding a
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000572// Mutex to CommandMessageQueue. Includes logging of all puts and gets.
573class LockingCommandMessageQueue BASE_EMBEDDED {
ager@chromium.org41826e72009-03-30 13:30:57 +0000574 public:
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000575 explicit LockingCommandMessageQueue(int size);
576 ~LockingCommandMessageQueue();
ager@chromium.org41826e72009-03-30 13:30:57 +0000577 bool IsEmpty() const;
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000578 CommandMessage Get();
579 void Put(const CommandMessage& message);
ager@chromium.org41826e72009-03-30 13:30:57 +0000580 void Clear();
581 private:
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000582 CommandMessageQueue queue_;
ager@chromium.org41826e72009-03-30 13:30:57 +0000583 Mutex* lock_;
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000584 DISALLOW_COPY_AND_ASSIGN(LockingCommandMessageQueue);
ager@chromium.org41826e72009-03-30 13:30:57 +0000585};
586
587
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000588class Debugger {
589 public:
590 static void DebugRequest(const uint16_t* json_request, int length);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000591
592 static Handle<Object> MakeJSObject(Vector<const char> constructor_name,
593 int argc, Object*** argv,
594 bool* caught_exception);
595 static Handle<Object> MakeExecutionState(bool* caught_exception);
596 static Handle<Object> MakeBreakEvent(Handle<Object> exec_state,
597 Handle<Object> break_points_hit,
598 bool* caught_exception);
599 static Handle<Object> MakeExceptionEvent(Handle<Object> exec_state,
600 Handle<Object> exception,
601 bool uncaught,
602 bool* caught_exception);
603 static Handle<Object> MakeNewFunctionEvent(Handle<Object> func,
604 bool* caught_exception);
605 static Handle<Object> MakeCompileEvent(Handle<Script> script,
iposva@chromium.org245aa852009-02-10 00:49:54 +0000606 bool before,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000607 bool* caught_exception);
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000608 static Handle<Object> MakeScriptCollectedEvent(int id,
609 bool* caught_exception);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000610 static void OnDebugBreak(Handle<Object> break_points_hit, bool auto_continue);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000611 static void OnException(Handle<Object> exception, bool uncaught);
612 static void OnBeforeCompile(Handle<Script> script);
613 static void OnAfterCompile(Handle<Script> script,
614 Handle<JSFunction> fun);
615 static void OnNewFunction(Handle<JSFunction> fun);
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000616 static void OnScriptCollected(int id);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000617 static void ProcessDebugEvent(v8::DebugEvent event,
ager@chromium.org5ec48922009-05-05 07:25:34 +0000618 Handle<JSObject> event_data,
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000619 bool auto_continue);
ager@chromium.org41826e72009-03-30 13:30:57 +0000620 static void NotifyMessageHandler(v8::DebugEvent event,
ager@chromium.org5ec48922009-05-05 07:25:34 +0000621 Handle<JSObject> exec_state,
622 Handle<JSObject> event_data,
ager@chromium.org41826e72009-03-30 13:30:57 +0000623 bool auto_continue);
iposva@chromium.org245aa852009-02-10 00:49:54 +0000624 static void SetEventListener(Handle<Object> callback, Handle<Object> data);
ager@chromium.org5ec48922009-05-05 07:25:34 +0000625 static void SetMessageHandler(v8::Debug::MessageHandler2 handler);
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000626 static void SetHostDispatchHandler(v8::Debug::HostDispatchHandler handler,
627 int period);
ager@chromium.org41826e72009-03-30 13:30:57 +0000628
629 // Invoke the message handler function.
ager@chromium.org5ec48922009-05-05 07:25:34 +0000630 static void InvokeMessageHandler(MessageImpl message);
ager@chromium.org41826e72009-03-30 13:30:57 +0000631
632 // Add a debugger command to the command queue.
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000633 static void ProcessCommand(Vector<const uint16_t> command,
634 v8::Debug::ClientData* client_data = NULL);
ager@chromium.org41826e72009-03-30 13:30:57 +0000635
636 // Check whether there are commands in the command queue.
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000637 static bool HasCommands();
ager@chromium.org41826e72009-03-30 13:30:57 +0000638
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000639 static Handle<Object> Call(Handle<JSFunction> fun,
640 Handle<Object> data,
641 bool* pending_exception);
642
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000643 // Start the debugger agent listening on the provided port.
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000644 static bool StartAgent(const char* name, int port);
645
646 // Stop the debugger agent.
647 static void StopAgent();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000648
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000649 // Blocks until the agent has started listening for connections
650 static void WaitForAgent();
651
ager@chromium.org71daaf62009-04-01 07:22:49 +0000652 // Unload the debugger if possible. Only called when no debugger is currently
653 // active.
654 static void UnloadDebugger();
655
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000656 inline static bool EventActive(v8::DebugEvent event) {
ager@chromium.org71daaf62009-04-01 07:22:49 +0000657 ScopedLock with(debugger_access_);
658
659 // Check whether the message handler was been cleared.
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000660 if (debugger_unload_pending_) {
ager@chromium.org71daaf62009-04-01 07:22:49 +0000661 UnloadDebugger();
662 }
663
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000664 // Currently argument event is not used.
ager@chromium.org71daaf62009-04-01 07:22:49 +0000665 return !compiling_natives_ && Debugger::IsDebuggerActive();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000666 }
667
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000668 static void set_compiling_natives(bool compiling_natives) {
669 Debugger::compiling_natives_ = compiling_natives;
670 }
671 static bool compiling_natives() { return Debugger::compiling_natives_; }
mads.s.agercbaa0602008-08-14 13:41:48 +0000672 static void set_loading_debugger(bool v) { is_loading_debugger_ = v; }
673 static bool is_loading_debugger() { return Debugger::is_loading_debugger_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000674
675 private:
ager@chromium.org71daaf62009-04-01 07:22:49 +0000676 static bool IsDebuggerActive();
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000677 static void ListenersChanged();
ager@chromium.org71daaf62009-04-01 07:22:49 +0000678
679 static Mutex* debugger_access_; // Mutex guarding debugger variables.
680 static Handle<Object> event_listener_; // Global handle to listener.
iposva@chromium.org245aa852009-02-10 00:49:54 +0000681 static Handle<Object> event_listener_data_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000682 static bool compiling_natives_; // Are we compiling natives?
mads.s.agercbaa0602008-08-14 13:41:48 +0000683 static bool is_loading_debugger_; // Are we loading the debugger?
ager@chromium.org71daaf62009-04-01 07:22:49 +0000684 static bool never_unload_debugger_; // Can we unload the debugger?
ager@chromium.org5ec48922009-05-05 07:25:34 +0000685 static v8::Debug::MessageHandler2 message_handler_;
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000686 static bool debugger_unload_pending_; // Was message handler cleared?
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000687 static v8::Debug::HostDispatchHandler host_dispatch_handler_;
688 static int host_dispatch_micros_;
ager@chromium.org381abbb2009-02-25 13:23:22 +0000689
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000690 static DebuggerAgent* agent_;
691
ager@chromium.org41826e72009-03-30 13:30:57 +0000692 static const int kQueueInitialSize = 4;
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000693 static LockingCommandMessageQueue command_queue_;
ager@chromium.org41826e72009-03-30 13:30:57 +0000694 static Semaphore* command_received_; // Signaled for each command received.
ager@chromium.org41826e72009-03-30 13:30:57 +0000695
ager@chromium.org71daaf62009-04-01 07:22:49 +0000696 friend class EnterDebugger;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000697};
698
699
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000700// This class is used for entering the debugger. Create an instance in the stack
701// to enter the debugger. This will set the current break state, make sure the
702// debugger is loaded and switch to the debugger context. If the debugger for
703// some reason could not be entered FailedToEnter will return true.
704class EnterDebugger BASE_EMBEDDED {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000705 public:
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000706 EnterDebugger()
707 : prev_(Debug::debugger_entry()),
708 has_js_frames_(!it_.done()) {
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000709 ASSERT(prev_ != NULL || !Debug::is_interrupt_pending(PREEMPT));
710 ASSERT(prev_ != NULL || !Debug::is_interrupt_pending(DEBUGBREAK));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000711
712 // Link recursive debugger entry.
713 Debug::set_debugger_entry(this);
714
ager@chromium.org8bb60582008-12-11 12:02:20 +0000715 // Store the previous break id and frame id.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000716 break_id_ = Debug::break_id();
717 break_frame_id_ = Debug::break_frame_id();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000718
ager@chromium.org8bb60582008-12-11 12:02:20 +0000719 // Create the new break info. If there is no JavaScript frames there is no
720 // break frame id.
721 if (has_js_frames_) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000722 Debug::NewBreak(it_.frame()->id());
ager@chromium.org8bb60582008-12-11 12:02:20 +0000723 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000724 Debug::NewBreak(StackFrame::NO_ID);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000725 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000726
727 // Make sure that debugger is loaded and enter the debugger context.
728 load_failed_ = !Debug::Load();
729 if (!load_failed_) {
730 // NOTE the member variable save which saves the previous context before
731 // this change.
732 Top::set_context(*Debug::debug_context());
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000733 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000734 }
735
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000736 ~EnterDebugger() {
ager@chromium.org8bb60582008-12-11 12:02:20 +0000737 // Restore to the previous break state.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000738 Debug::SetBreak(break_frame_id_, break_id_);
739
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000740 // Check for leaving the debugger.
ager@chromium.org71daaf62009-04-01 07:22:49 +0000741 if (prev_ == NULL) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000742 // Clear mirror cache when leaving the debugger. Skip this if there is a
743 // pending exception as clearing the mirror cache calls back into
744 // JavaScript. This can happen if the v8::Debug::Call is used in which
745 // case the exception should end up in the calling code.
746 if (!Top::has_pending_exception()) {
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000747 // Try to avoid any pending debug break breaking in the clear mirror
748 // cache JavaScript code.
749 if (StackGuard::IsDebugBreak()) {
750 Debug::set_interrupts_pending(DEBUGBREAK);
751 StackGuard::Continue(DEBUGBREAK);
752 }
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000753 Debug::ClearMirrorCache();
754 }
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000755
756 // Request preemption and debug break when leaving the last debugger entry
757 // if any of these where recorded while debugging.
758 if (Debug::is_interrupt_pending(PREEMPT)) {
759 // This re-scheduling of preemption is to avoid starvation in some
760 // debugging scenarios.
761 Debug::clear_interrupt_pending(PREEMPT);
762 StackGuard::Preempt();
763 }
764 if (Debug::is_interrupt_pending(DEBUGBREAK)) {
765 Debug::clear_interrupt_pending(DEBUGBREAK);
766 StackGuard::DebugBreak();
767 }
768
769 // If there are commands in the queue when leaving the debugger request
770 // that these commands are processed.
771 if (Debugger::HasCommands()) {
772 StackGuard::DebugCommand();
773 }
774
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000775 // If leaving the debugger with the debugger no longer active unload it.
ager@chromium.org71daaf62009-04-01 07:22:49 +0000776 if (!Debugger::IsDebuggerActive()) {
777 Debugger::UnloadDebugger();
778 }
779 }
780
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000781 // Leaving this debugger entry.
782 Debug::set_debugger_entry(prev_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000783 }
784
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000785 // Check whether the debugger could be entered.
786 inline bool FailedToEnter() { return load_failed_; }
787
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000788 // Check whether there are any JavaScript frames on the stack.
ager@chromium.org8bb60582008-12-11 12:02:20 +0000789 inline bool HasJavaScriptFrames() { return has_js_frames_; }
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000790
ager@chromium.org9085a012009-05-11 19:22:57 +0000791 // Get the active context from before entering the debugger.
792 inline Handle<Context> GetContext() { return save_.context(); }
793
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000794 private:
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000795 EnterDebugger* prev_; // Previous debugger entry if entered recursively.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000796 JavaScriptFrameIterator it_;
ager@chromium.org8bb60582008-12-11 12:02:20 +0000797 const bool has_js_frames_; // Were there any JavaScript frames?
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000798 StackFrame::Id break_frame_id_; // Previous break frame id.
799 int break_id_; // Previous break id.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000800 bool load_failed_; // Did the debugger fail to load?
801 SaveContext save_; // Saves previous context.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000802};
803
804
kasper.lundbd3ec4e2008-07-09 11:06:54 +0000805// Stack allocated class for disabling break.
806class DisableBreak BASE_EMBEDDED {
807 public:
808 // Enter the debugger by storing the previous top context and setting the
809 // current top context to the debugger context.
810 explicit DisableBreak(bool disable_break) {
811 prev_disable_break_ = Debug::disable_break();
812 Debug::set_disable_break(disable_break);
813 }
814 ~DisableBreak() {
815 Debug::set_disable_break(prev_disable_break_);
816 }
817
818 private:
819 // The previous state of the disable break used to restore the value when this
820 // object is destructed.
821 bool prev_disable_break_;
822};
823
824
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000825// Debug_Address encapsulates the Address pointers used in generating debug
826// code.
827class Debug_Address {
828 public:
829 Debug_Address(Debug::AddressId id, int reg = 0)
830 : id_(id), reg_(reg) {
831 ASSERT(reg == 0 || id == Debug::k_register_address);
832 }
833
834 static Debug_Address AfterBreakTarget() {
835 return Debug_Address(Debug::k_after_break_target_address);
836 }
837
838 static Debug_Address DebugBreakReturn() {
839 return Debug_Address(Debug::k_debug_break_return_address);
840 }
841
842 static Debug_Address Register(int reg) {
843 return Debug_Address(Debug::k_register_address, reg);
844 }
845
846 Address address() const {
847 switch (id_) {
848 case Debug::k_after_break_target_address:
849 return reinterpret_cast<Address>(Debug::after_break_target_address());
850 case Debug::k_debug_break_return_address:
851 return reinterpret_cast<Address>(Debug::debug_break_return_address());
852 case Debug::k_register_address:
853 return reinterpret_cast<Address>(Debug::register_address(reg_));
854 default:
855 UNREACHABLE();
856 return NULL;
857 }
858 }
859 private:
860 Debug::AddressId id_;
861 int reg_;
862};
863
864
865} } // namespace v8::internal
866
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000867#endif // ENABLE_DEBUGGER_SUPPORT
868
ager@chromium.org5ec48922009-05-05 07:25:34 +0000869#endif // V8_DEBUG_H_