blob: 5b0273aa2244a883457683de8318c43c6676486c [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
122 protected:
123 bool RinfoDone() const;
124 void RinfoNext();
125
126 BreakLocatorType type_;
127 int break_point_;
128 int position_;
129 int statement_position_;
130 Handle<DebugInfo> debug_info_;
131 RelocIterator* reloc_iterator_;
132 RelocIterator* reloc_iterator_original_;
133
134 private:
135 void SetDebugBreak();
136 void ClearDebugBreak();
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000137
138 void SetDebugBreakAtIC();
139 void ClearDebugBreakAtIC();
140
iposva@chromium.org245aa852009-02-10 00:49:54 +0000141 bool IsDebugBreakAtReturn();
142 void SetDebugBreakAtReturn();
143 void ClearDebugBreakAtReturn();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000144
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000145 DISALLOW_COPY_AND_ASSIGN(BreakLocationIterator);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000146};
147
148
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000149// Cache of all script objects in the heap. When a script is added a weak handle
150// to it is created and that weak handle is stored in the cache. The weak handle
151// callback takes care of removing the script from the cache. The key used in
152// the cache is the script id.
153class ScriptCache : private HashMap {
154 public:
155 ScriptCache() : HashMap(ScriptMatch), collected_scripts_(10) {}
156 virtual ~ScriptCache() { Clear(); }
157
158 // Add script to the cache.
159 void Add(Handle<Script> script);
160
161 // Return the scripts in the cache.
162 Handle<FixedArray> GetScripts();
163
164 // Generate debugger events for collected scripts.
165 void ProcessCollectedScripts();
166
167 private:
168 // Calculate the hash value from the key (script id).
169 static uint32_t Hash(int key) { return ComputeIntegerHash(key); }
170
171 // Scripts match if their keys (script id) match.
172 static bool ScriptMatch(void* key1, void* key2) { return key1 == key2; }
173
174 // Clear the cache releasing all the weak handles.
175 void Clear();
176
177 // Weak handle callback for scripts in the cache.
178 static void HandleWeakScript(v8::Persistent<v8::Value> obj, void* data);
179
180 // List used during GC to temporarily store id's of collected scripts.
181 List<int> collected_scripts_;
182};
183
184
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000185// Linked list holding debug info objects. The debug info objects are kept as
186// weak handles to avoid a debug info object to keep a function alive.
187class DebugInfoListNode {
188 public:
189 explicit DebugInfoListNode(DebugInfo* debug_info);
190 virtual ~DebugInfoListNode();
191
192 DebugInfoListNode* next() { return next_; }
193 void set_next(DebugInfoListNode* next) { next_ = next; }
194 Handle<DebugInfo> debug_info() { return debug_info_; }
195
196 private:
197 // Global (weak) handle to the debug info object.
198 Handle<DebugInfo> debug_info_;
199
200 // Next pointer for linked list.
201 DebugInfoListNode* next_;
202};
203
204
205// This class contains the debugger support. The main purpose is to handle
206// setting break points in the code.
207//
208// This class controls the debug info for all functions which currently have
209// active breakpoints in them. This debug info is held in the heap root object
210// debug_info which is a FixedArray. Each entry in this list is of class
211// DebugInfo.
212class Debug {
213 public:
214 static void Setup(bool create_heap_objects);
215 static bool Load();
216 static void Unload();
217 static bool IsLoaded() { return !debug_context_.is_null(); }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000218 static bool InDebugger() { return thread_local_.debugger_entry_ != NULL; }
219 static void PreemptionWhileInDebugger();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000220 static void Iterate(ObjectVisitor* v);
221
222 static Object* Break(Arguments args);
223 static void SetBreakPoint(Handle<SharedFunctionInfo> shared,
224 int source_position,
225 Handle<Object> break_point_object);
226 static void ClearBreakPoint(Handle<Object> break_point_object);
ager@chromium.org381abbb2009-02-25 13:23:22 +0000227 static void ClearAllBreakPoints();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000228 static void FloodWithOneShot(Handle<SharedFunctionInfo> shared);
229 static void FloodHandlerWithOneShot();
230 static void ChangeBreakOnException(ExceptionBreakType type, bool enable);
231 static void PrepareStep(StepAction step_action, int step_count);
232 static void ClearStepping();
233 static bool StepNextContinue(BreakLocationIterator* break_location_iterator,
234 JavaScriptFrame* frame);
235 static Handle<DebugInfo> GetDebugInfo(Handle<SharedFunctionInfo> shared);
236 static bool HasDebugInfo(Handle<SharedFunctionInfo> shared);
kasper.lundbd3ec4e2008-07-09 11:06:54 +0000237
ager@chromium.org32912102009-01-16 10:38:43 +0000238 // Returns whether the operation succeeded.
kasper.lundbd3ec4e2008-07-09 11:06:54 +0000239 static bool EnsureDebugInfo(Handle<SharedFunctionInfo> shared);
240
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000241 // Returns true if the current stub call is patched to call the debugger.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000242 static bool IsDebugBreak(Address addr);
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000243 // Returns true if the current return statement has been patched to be
244 // a debugger breakpoint.
ager@chromium.org381abbb2009-02-25 13:23:22 +0000245 static bool IsDebugBreakAtReturn(RelocInfo* rinfo);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000246
247 // Check whether a code stub with the specified major key is a possible break
248 // point location.
249 static bool IsSourceBreakStub(Code* code);
250 static bool IsBreakStub(Code* code);
251
252 // Find the builtin to use for invoking the debug break
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000253 static Handle<Code> FindDebugBreak(Handle<Code> code, RelocInfo::Mode mode);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000254
255 static Handle<Object> GetSourceBreakLocations(
256 Handle<SharedFunctionInfo> shared);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000257
258 // Getter for the debug_context.
259 inline static Handle<Context> debug_context() { return debug_context_; }
260
261 // Check whether a global object is the debug global object.
262 static bool IsDebugGlobal(GlobalObject* global);
263
264 // Fast check to see if any break points are active.
265 inline static bool has_break_points() { return has_break_points_; }
266
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000267 static void NewBreak(StackFrame::Id break_frame_id);
268 static void SetBreak(StackFrame::Id break_frame_id, int break_id);
269 static StackFrame::Id break_frame_id() {
270 return thread_local_.break_frame_id_;
271 }
272 static int break_id() { return thread_local_.break_id_; }
273
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000274 static bool StepInActive() { return thread_local_.step_into_fp_ != 0; }
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000275 static void HandleStepIn(Handle<JSFunction> function,
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000276 Handle<Object> holder,
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000277 Address fp,
278 bool is_constructor);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000279 static Address step_in_fp() { return thread_local_.step_into_fp_; }
280 static Address* step_in_fp_addr() { return &thread_local_.step_into_fp_; }
281
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000282 static EnterDebugger* debugger_entry() {
283 return thread_local_.debugger_entry_;
284 }
285 static void set_debugger_entry(EnterDebugger* entry) {
286 thread_local_.debugger_entry_ = entry;
287 }
288
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000289 // Check whether any of the specified interrupts are pending.
290 static bool is_interrupt_pending(InterruptFlag what) {
291 return (thread_local_.pending_interrupts_ & what) != 0;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000292 }
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000293
294 // Set specified interrupts as pending.
295 static void set_interrupts_pending(InterruptFlag what) {
296 thread_local_.pending_interrupts_ |= what;
297 }
298
299 // Clear specified interrupts from pending.
300 static void clear_interrupt_pending(InterruptFlag what) {
301 thread_local_.pending_interrupts_ &= ~static_cast<int>(what);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000302 }
303
kasper.lundbd3ec4e2008-07-09 11:06:54 +0000304 // Getter and setter for the disable break state.
305 static bool disable_break() { return disable_break_; }
306 static void set_disable_break(bool disable_break) {
307 disable_break_ = disable_break;
308 }
309
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000310 // Getters for the current exception break state.
311 static bool break_on_exception() { return break_on_exception_; }
312 static bool break_on_uncaught_exception() {
313 return break_on_uncaught_exception_;
314 }
315
316 enum AddressId {
317 k_after_break_target_address,
318 k_debug_break_return_address,
319 k_register_address
320 };
321
322 // Support for setting the address to jump to when returning from break point.
323 static Address* after_break_target_address() {
324 return reinterpret_cast<Address*>(&thread_local_.after_break_target_);
325 }
326
327 // Support for saving/restoring registers when handling debug break calls.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000328 static Object** register_address(int r) {
329 return &registers_[r];
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000330 }
331
kasper.lund7276f142008-07-30 08:49:36 +0000332 // Address of the debug break return entry code.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000333 static Code* debug_break_return_entry() { return debug_break_return_entry_; }
334
335 // Support for getting the address of the debug break on return code.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000336 static Code** debug_break_return_address() {
337 return &debug_break_return_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000338 }
339
340 static const int kEstimatedNofDebugInfoEntries = 16;
341 static const int kEstimatedNofBreakPointsInFunction = 16;
342
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000343 static void HandleWeakDebugInfo(v8::Persistent<v8::Value> obj, void* data);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000344
345 friend class Debugger;
ager@chromium.org381abbb2009-02-25 13:23:22 +0000346 friend Handle<FixedArray> GetDebuggedFunctions(); // In test-debug.cc
347 friend void CheckDebuggerUnloaded(bool check_functions); // In test-debug.cc
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000348
349 // Threading support.
350 static char* ArchiveDebug(char* to);
351 static char* RestoreDebug(char* from);
352 static int ArchiveSpacePerThread();
353
ager@chromium.org32912102009-01-16 10:38:43 +0000354 // Mirror cache handling.
355 static void ClearMirrorCache();
356
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000357 // Script cache handling.
358 static void CreateScriptCache();
359 static void DestroyScriptCache();
360 static void AddScriptToScriptCache(Handle<Script> script);
361 static Handle<FixedArray> GetLoadedScripts();
362
363 // Garbage collection notifications.
364 static void AfterGarbageCollection();
365
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000366 // Code generation assumptions.
367 static const int kIa32CallInstructionLength = 5;
368 static const int kIa32JSReturnSequenceLength = 6;
369
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000370 // The x64 JS return sequence is padded with int3 to make it large
371 // enough to hold a call instruction when the debugger patches it.
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000372 static const int kX64CallInstructionLength = 13;
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000373 static const int kX64JSReturnSequenceLength = 13;
374
ager@chromium.org8bb60582008-12-11 12:02:20 +0000375 // Code generator routines.
376 static void GenerateLoadICDebugBreak(MacroAssembler* masm);
377 static void GenerateStoreICDebugBreak(MacroAssembler* masm);
378 static void GenerateKeyedLoadICDebugBreak(MacroAssembler* masm);
379 static void GenerateKeyedStoreICDebugBreak(MacroAssembler* masm);
380 static void GenerateConstructCallDebugBreak(MacroAssembler* masm);
381 static void GenerateReturnDebugBreak(MacroAssembler* masm);
382 static void GenerateReturnDebugBreakEntry(MacroAssembler* masm);
383 static void GenerateStubNoRegistersDebugBreak(MacroAssembler* masm);
384
385 // Called from stub-cache.cc.
386 static void GenerateCallICDebugBreak(MacroAssembler* masm);
387
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000388 private:
389 static bool CompileDebuggerScript(int index);
390 static void ClearOneShot();
391 static void ActivateStepIn(StackFrame* frame);
392 static void ClearStepIn();
393 static void ClearStepNext();
ager@chromium.org32912102009-01-16 10:38:43 +0000394 // Returns whether the compile succeeded.
kasper.lundbd3ec4e2008-07-09 11:06:54 +0000395 static bool EnsureCompiled(Handle<SharedFunctionInfo> shared);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000396 static void RemoveDebugInfo(Handle<DebugInfo> debug_info);
397 static void SetAfterBreakTarget(JavaScriptFrame* frame);
398 static Handle<Object> CheckBreakPoints(Handle<Object> break_point);
399 static bool CheckBreakPoint(Handle<Object> break_point_object);
400
401 // Global handle to debug context where all the debugger JavaScript code is
402 // loaded.
403 static Handle<Context> debug_context_;
404
405 // Boolean state indicating whether any break points are set.
406 static bool has_break_points_;
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000407
408 // Cache of all scripts in the heap.
409 static ScriptCache* script_cache_;
410
411 // List of active debug info objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000412 static DebugInfoListNode* debug_info_list_;
413
kasper.lundbd3ec4e2008-07-09 11:06:54 +0000414 static bool disable_break_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000415 static bool break_on_exception_;
416 static bool break_on_uncaught_exception_;
417
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000418 // Per-thread data.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000419 class ThreadLocal {
420 public:
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000421 // Counter for generating next break id.
422 int break_count_;
423
424 // Current break id.
425 int break_id_;
426
427 // Frame id for the frame of the current break.
428 StackFrame::Id break_frame_id_;
429
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000430 // Step action for last step performed.
431 StepAction last_step_action_;
432
433 // Source statement position from last step next action.
434 int last_statement_position_;
435
436 // Number of steps left to perform before debug event.
437 int step_count_;
438
439 // Frame pointer from last step next action.
440 Address last_fp_;
441
442 // Frame pointer for frame from which step in was performed.
443 Address step_into_fp_;
444
445 // Storage location for jump when exiting debug break calls.
446 Address after_break_target_;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000447
448 // Top debugger entry.
449 EnterDebugger* debugger_entry_;
450
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000451 // Pending interrupts scheduled while debugging.
452 int pending_interrupts_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000453 };
454
455 // Storage location for registers when handling debug break calls
456 static JSCallerSavedBuffer registers_;
457 static ThreadLocal thread_local_;
458 static void ThreadInit();
459
460 // Code object for debug break return entry code.
461 static Code* debug_break_return_entry_;
462
463 // Code to call for handling debug break on return.
464 static Code* debug_break_return_;
465
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000466 DISALLOW_COPY_AND_ASSIGN(Debug);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000467};
468
469
ager@chromium.org5ec48922009-05-05 07:25:34 +0000470// Message delivered to the message handler callback. This is either a debugger
471// event or the response to a command.
472class MessageImpl: public v8::Debug::Message {
473 public:
474 // Create a message object for a debug event.
475 static MessageImpl NewEvent(DebugEvent event,
476 bool running,
477 Handle<JSObject> exec_state,
478 Handle<JSObject> event_data);
479
480 // Create a message object for the response to a debug command.
481 static MessageImpl NewResponse(DebugEvent event,
482 bool running,
483 Handle<JSObject> exec_state,
484 Handle<JSObject> event_data,
485 Handle<String> response_json,
486 v8::Debug::ClientData* client_data);
487
488 // Implementation of interface v8::Debug::Message.
489 virtual bool IsEvent() const;
490 virtual bool IsResponse() const;
491 virtual DebugEvent GetEvent() const;
492 virtual bool WillStartRunning() const;
493 virtual v8::Handle<v8::Object> GetExecutionState() const;
494 virtual v8::Handle<v8::Object> GetEventData() const;
495 virtual v8::Handle<v8::String> GetJSON() const;
496 virtual v8::Handle<v8::Context> GetEventContext() const;
497 virtual v8::Debug::ClientData* GetClientData() const;
498
499 private:
500 MessageImpl(bool is_event,
501 DebugEvent event,
502 bool running,
503 Handle<JSObject> exec_state,
504 Handle<JSObject> event_data,
505 Handle<String> response_json,
506 v8::Debug::ClientData* client_data);
507
508 bool is_event_; // Does this message represent a debug event?
509 DebugEvent event_; // Debug event causing the break.
510 bool running_; // Will the VM start running after this event?
511 Handle<JSObject> exec_state_; // Current execution state.
512 Handle<JSObject> event_data_; // Data associated with the event.
513 Handle<String> response_json_; // Response JSON if message holds a response.
514 v8::Debug::ClientData* client_data_; // Client data passed with the request.
515};
516
517
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000518// Message send by user to v8 debugger or debugger output message.
519// In addition to command text it may contain a pointer to some user data
520// which are expected to be passed along with the command reponse to message
521// handler.
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000522class CommandMessage {
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000523 public:
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000524 static CommandMessage New(const Vector<uint16_t>& command,
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000525 v8::Debug::ClientData* data);
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000526 CommandMessage();
527 ~CommandMessage();
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000528
529 // Deletes user data and disposes of the text.
530 void Dispose();
531 Vector<uint16_t> text() const { return text_; }
532 v8::Debug::ClientData* client_data() const { return client_data_; }
533 private:
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000534 CommandMessage(const Vector<uint16_t>& text,
535 v8::Debug::ClientData* data);
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000536
537 Vector<uint16_t> text_;
538 v8::Debug::ClientData* client_data_;
539};
540
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000541// A Queue of CommandMessage objects. A thread-safe version is
542// LockingCommandMessageQueue, based on this class.
543class CommandMessageQueue BASE_EMBEDDED {
ager@chromium.org41826e72009-03-30 13:30:57 +0000544 public:
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000545 explicit CommandMessageQueue(int size);
546 ~CommandMessageQueue();
ager@chromium.org41826e72009-03-30 13:30:57 +0000547 bool IsEmpty() const { return start_ == end_; }
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000548 CommandMessage Get();
549 void Put(const CommandMessage& message);
ager@chromium.org41826e72009-03-30 13:30:57 +0000550 void Clear() { start_ = end_ = 0; } // Queue is empty after Clear().
551 private:
552 // Doubles the size of the message queue, and copies the messages.
553 void Expand();
554
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000555 CommandMessage* messages_;
ager@chromium.org41826e72009-03-30 13:30:57 +0000556 int start_;
557 int end_;
558 int size_; // The size of the queue buffer. Queue can hold size-1 messages.
559};
560
561
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000562// LockingCommandMessageQueue is a thread-safe circular buffer of CommandMessage
563// messages. The message data is not managed by LockingCommandMessageQueue.
ager@chromium.org41826e72009-03-30 13:30:57 +0000564// Pointers to the data are passed in and out. Implemented by adding a
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000565// Mutex to CommandMessageQueue. Includes logging of all puts and gets.
566class LockingCommandMessageQueue BASE_EMBEDDED {
ager@chromium.org41826e72009-03-30 13:30:57 +0000567 public:
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000568 explicit LockingCommandMessageQueue(int size);
569 ~LockingCommandMessageQueue();
ager@chromium.org41826e72009-03-30 13:30:57 +0000570 bool IsEmpty() const;
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000571 CommandMessage Get();
572 void Put(const CommandMessage& message);
ager@chromium.org41826e72009-03-30 13:30:57 +0000573 void Clear();
574 private:
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000575 CommandMessageQueue queue_;
ager@chromium.org41826e72009-03-30 13:30:57 +0000576 Mutex* lock_;
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000577 DISALLOW_COPY_AND_ASSIGN(LockingCommandMessageQueue);
ager@chromium.org41826e72009-03-30 13:30:57 +0000578};
579
580
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000581class Debugger {
582 public:
583 static void DebugRequest(const uint16_t* json_request, int length);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000584
585 static Handle<Object> MakeJSObject(Vector<const char> constructor_name,
586 int argc, Object*** argv,
587 bool* caught_exception);
588 static Handle<Object> MakeExecutionState(bool* caught_exception);
589 static Handle<Object> MakeBreakEvent(Handle<Object> exec_state,
590 Handle<Object> break_points_hit,
591 bool* caught_exception);
592 static Handle<Object> MakeExceptionEvent(Handle<Object> exec_state,
593 Handle<Object> exception,
594 bool uncaught,
595 bool* caught_exception);
596 static Handle<Object> MakeNewFunctionEvent(Handle<Object> func,
597 bool* caught_exception);
598 static Handle<Object> MakeCompileEvent(Handle<Script> script,
iposva@chromium.org245aa852009-02-10 00:49:54 +0000599 bool before,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000600 bool* caught_exception);
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000601 static Handle<Object> MakeScriptCollectedEvent(int id,
602 bool* caught_exception);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000603 static void OnDebugBreak(Handle<Object> break_points_hit, bool auto_continue);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000604 static void OnException(Handle<Object> exception, bool uncaught);
605 static void OnBeforeCompile(Handle<Script> script);
606 static void OnAfterCompile(Handle<Script> script,
607 Handle<JSFunction> fun);
608 static void OnNewFunction(Handle<JSFunction> fun);
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000609 static void OnScriptCollected(int id);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000610 static void ProcessDebugEvent(v8::DebugEvent event,
ager@chromium.org5ec48922009-05-05 07:25:34 +0000611 Handle<JSObject> event_data,
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000612 bool auto_continue);
ager@chromium.org41826e72009-03-30 13:30:57 +0000613 static void NotifyMessageHandler(v8::DebugEvent event,
ager@chromium.org5ec48922009-05-05 07:25:34 +0000614 Handle<JSObject> exec_state,
615 Handle<JSObject> event_data,
ager@chromium.org41826e72009-03-30 13:30:57 +0000616 bool auto_continue);
iposva@chromium.org245aa852009-02-10 00:49:54 +0000617 static void SetEventListener(Handle<Object> callback, Handle<Object> data);
ager@chromium.org5ec48922009-05-05 07:25:34 +0000618 static void SetMessageHandler(v8::Debug::MessageHandler2 handler);
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000619 static void SetHostDispatchHandler(v8::Debug::HostDispatchHandler handler,
620 int period);
ager@chromium.org41826e72009-03-30 13:30:57 +0000621
622 // Invoke the message handler function.
ager@chromium.org5ec48922009-05-05 07:25:34 +0000623 static void InvokeMessageHandler(MessageImpl message);
ager@chromium.org41826e72009-03-30 13:30:57 +0000624
625 // Add a debugger command to the command queue.
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000626 static void ProcessCommand(Vector<const uint16_t> command,
627 v8::Debug::ClientData* client_data = NULL);
ager@chromium.org41826e72009-03-30 13:30:57 +0000628
629 // Check whether there are commands in the command queue.
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000630 static bool HasCommands();
ager@chromium.org41826e72009-03-30 13:30:57 +0000631
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000632 static Handle<Object> Call(Handle<JSFunction> fun,
633 Handle<Object> data,
634 bool* pending_exception);
635
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000636 // Start the debugger agent listening on the provided port.
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000637 static bool StartAgent(const char* name, int port);
638
639 // Stop the debugger agent.
640 static void StopAgent();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000641
ager@chromium.org71daaf62009-04-01 07:22:49 +0000642 // Unload the debugger if possible. Only called when no debugger is currently
643 // active.
644 static void UnloadDebugger();
645
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000646 inline static bool EventActive(v8::DebugEvent event) {
ager@chromium.org71daaf62009-04-01 07:22:49 +0000647 ScopedLock with(debugger_access_);
648
649 // Check whether the message handler was been cleared.
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000650 if (debugger_unload_pending_) {
ager@chromium.org71daaf62009-04-01 07:22:49 +0000651 UnloadDebugger();
652 }
653
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000654 // Currently argument event is not used.
ager@chromium.org71daaf62009-04-01 07:22:49 +0000655 return !compiling_natives_ && Debugger::IsDebuggerActive();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000656 }
657
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000658 static void set_compiling_natives(bool compiling_natives) {
659 Debugger::compiling_natives_ = compiling_natives;
660 }
661 static bool compiling_natives() { return Debugger::compiling_natives_; }
mads.s.agercbaa0602008-08-14 13:41:48 +0000662 static void set_loading_debugger(bool v) { is_loading_debugger_ = v; }
663 static bool is_loading_debugger() { return Debugger::is_loading_debugger_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000664
665 private:
ager@chromium.org71daaf62009-04-01 07:22:49 +0000666 static bool IsDebuggerActive();
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000667 static void ListenersChanged();
ager@chromium.org71daaf62009-04-01 07:22:49 +0000668
669 static Mutex* debugger_access_; // Mutex guarding debugger variables.
670 static Handle<Object> event_listener_; // Global handle to listener.
iposva@chromium.org245aa852009-02-10 00:49:54 +0000671 static Handle<Object> event_listener_data_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000672 static bool compiling_natives_; // Are we compiling natives?
mads.s.agercbaa0602008-08-14 13:41:48 +0000673 static bool is_loading_debugger_; // Are we loading the debugger?
ager@chromium.org71daaf62009-04-01 07:22:49 +0000674 static bool never_unload_debugger_; // Can we unload the debugger?
ager@chromium.org5ec48922009-05-05 07:25:34 +0000675 static v8::Debug::MessageHandler2 message_handler_;
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000676 static bool debugger_unload_pending_; // Was message handler cleared?
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000677 static v8::Debug::HostDispatchHandler host_dispatch_handler_;
678 static int host_dispatch_micros_;
ager@chromium.org381abbb2009-02-25 13:23:22 +0000679
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000680 static DebuggerAgent* agent_;
681
ager@chromium.org41826e72009-03-30 13:30:57 +0000682 static const int kQueueInitialSize = 4;
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000683 static LockingCommandMessageQueue command_queue_;
ager@chromium.org41826e72009-03-30 13:30:57 +0000684 static Semaphore* command_received_; // Signaled for each command received.
ager@chromium.org41826e72009-03-30 13:30:57 +0000685
ager@chromium.org71daaf62009-04-01 07:22:49 +0000686 friend class EnterDebugger;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000687};
688
689
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000690// This class is used for entering the debugger. Create an instance in the stack
691// to enter the debugger. This will set the current break state, make sure the
692// debugger is loaded and switch to the debugger context. If the debugger for
693// some reason could not be entered FailedToEnter will return true.
694class EnterDebugger BASE_EMBEDDED {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000695 public:
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000696 EnterDebugger()
697 : prev_(Debug::debugger_entry()),
698 has_js_frames_(!it_.done()) {
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000699 ASSERT(prev_ != NULL || !Debug::is_interrupt_pending(PREEMPT));
700 ASSERT(prev_ != NULL || !Debug::is_interrupt_pending(DEBUGBREAK));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000701
702 // Link recursive debugger entry.
703 Debug::set_debugger_entry(this);
704
ager@chromium.org8bb60582008-12-11 12:02:20 +0000705 // Store the previous break id and frame id.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000706 break_id_ = Debug::break_id();
707 break_frame_id_ = Debug::break_frame_id();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000708
ager@chromium.org8bb60582008-12-11 12:02:20 +0000709 // Create the new break info. If there is no JavaScript frames there is no
710 // break frame id.
711 if (has_js_frames_) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000712 Debug::NewBreak(it_.frame()->id());
ager@chromium.org8bb60582008-12-11 12:02:20 +0000713 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000714 Debug::NewBreak(StackFrame::NO_ID);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000715 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000716
717 // Make sure that debugger is loaded and enter the debugger context.
718 load_failed_ = !Debug::Load();
719 if (!load_failed_) {
720 // NOTE the member variable save which saves the previous context before
721 // this change.
722 Top::set_context(*Debug::debug_context());
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000723 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000724 }
725
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000726 ~EnterDebugger() {
ager@chromium.org8bb60582008-12-11 12:02:20 +0000727 // Restore to the previous break state.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000728 Debug::SetBreak(break_frame_id_, break_id_);
729
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000730 // Check for leaving the debugger.
ager@chromium.org71daaf62009-04-01 07:22:49 +0000731 if (prev_ == NULL) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000732 // Clear mirror cache when leaving the debugger. Skip this if there is a
733 // pending exception as clearing the mirror cache calls back into
734 // JavaScript. This can happen if the v8::Debug::Call is used in which
735 // case the exception should end up in the calling code.
736 if (!Top::has_pending_exception()) {
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000737 // Try to avoid any pending debug break breaking in the clear mirror
738 // cache JavaScript code.
739 if (StackGuard::IsDebugBreak()) {
740 Debug::set_interrupts_pending(DEBUGBREAK);
741 StackGuard::Continue(DEBUGBREAK);
742 }
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000743 Debug::ClearMirrorCache();
744 }
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000745
746 // Request preemption and debug break when leaving the last debugger entry
747 // if any of these where recorded while debugging.
748 if (Debug::is_interrupt_pending(PREEMPT)) {
749 // This re-scheduling of preemption is to avoid starvation in some
750 // debugging scenarios.
751 Debug::clear_interrupt_pending(PREEMPT);
752 StackGuard::Preempt();
753 }
754 if (Debug::is_interrupt_pending(DEBUGBREAK)) {
755 Debug::clear_interrupt_pending(DEBUGBREAK);
756 StackGuard::DebugBreak();
757 }
758
759 // If there are commands in the queue when leaving the debugger request
760 // that these commands are processed.
761 if (Debugger::HasCommands()) {
762 StackGuard::DebugCommand();
763 }
764
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000765 // If leaving the debugger with the debugger no longer active unload it.
ager@chromium.org71daaf62009-04-01 07:22:49 +0000766 if (!Debugger::IsDebuggerActive()) {
767 Debugger::UnloadDebugger();
768 }
769 }
770
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000771 // Leaving this debugger entry.
772 Debug::set_debugger_entry(prev_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000773 }
774
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000775 // Check whether the debugger could be entered.
776 inline bool FailedToEnter() { return load_failed_; }
777
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000778 // Check whether there are any JavaScript frames on the stack.
ager@chromium.org8bb60582008-12-11 12:02:20 +0000779 inline bool HasJavaScriptFrames() { return has_js_frames_; }
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000780
ager@chromium.org9085a012009-05-11 19:22:57 +0000781 // Get the active context from before entering the debugger.
782 inline Handle<Context> GetContext() { return save_.context(); }
783
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000784 private:
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000785 EnterDebugger* prev_; // Previous debugger entry if entered recursively.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000786 JavaScriptFrameIterator it_;
ager@chromium.org8bb60582008-12-11 12:02:20 +0000787 const bool has_js_frames_; // Were there any JavaScript frames?
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000788 StackFrame::Id break_frame_id_; // Previous break frame id.
789 int break_id_; // Previous break id.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000790 bool load_failed_; // Did the debugger fail to load?
791 SaveContext save_; // Saves previous context.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000792};
793
794
kasper.lundbd3ec4e2008-07-09 11:06:54 +0000795// Stack allocated class for disabling break.
796class DisableBreak BASE_EMBEDDED {
797 public:
798 // Enter the debugger by storing the previous top context and setting the
799 // current top context to the debugger context.
800 explicit DisableBreak(bool disable_break) {
801 prev_disable_break_ = Debug::disable_break();
802 Debug::set_disable_break(disable_break);
803 }
804 ~DisableBreak() {
805 Debug::set_disable_break(prev_disable_break_);
806 }
807
808 private:
809 // The previous state of the disable break used to restore the value when this
810 // object is destructed.
811 bool prev_disable_break_;
812};
813
814
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000815// Debug_Address encapsulates the Address pointers used in generating debug
816// code.
817class Debug_Address {
818 public:
819 Debug_Address(Debug::AddressId id, int reg = 0)
820 : id_(id), reg_(reg) {
821 ASSERT(reg == 0 || id == Debug::k_register_address);
822 }
823
824 static Debug_Address AfterBreakTarget() {
825 return Debug_Address(Debug::k_after_break_target_address);
826 }
827
828 static Debug_Address DebugBreakReturn() {
829 return Debug_Address(Debug::k_debug_break_return_address);
830 }
831
832 static Debug_Address Register(int reg) {
833 return Debug_Address(Debug::k_register_address, reg);
834 }
835
836 Address address() const {
837 switch (id_) {
838 case Debug::k_after_break_target_address:
839 return reinterpret_cast<Address>(Debug::after_break_target_address());
840 case Debug::k_debug_break_return_address:
841 return reinterpret_cast<Address>(Debug::debug_break_return_address());
842 case Debug::k_register_address:
843 return reinterpret_cast<Address>(Debug::register_address(reg_));
844 default:
845 UNREACHABLE();
846 return NULL;
847 }
848 }
849 private:
850 Debug::AddressId id_;
851 int reg_;
852};
853
854
855} } // namespace v8::internal
856
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000857#endif // ENABLE_DEBUGGER_SUPPORT
858
ager@chromium.org5ec48922009-05-05 07:25:34 +0000859#endif // V8_DEBUG_H_