blob: 3f90fa66e51b9153f8a38a7008192d530949eea6 [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
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000241 static bool IsDebugBreak(Address addr);
ager@chromium.org381abbb2009-02-25 13:23:22 +0000242 static bool IsDebugBreakAtReturn(RelocInfo* rinfo);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000243
244 // Check whether a code stub with the specified major key is a possible break
245 // point location.
246 static bool IsSourceBreakStub(Code* code);
247 static bool IsBreakStub(Code* code);
248
249 // Find the builtin to use for invoking the debug break
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000250 static Handle<Code> FindDebugBreak(Handle<Code> code, RelocInfo::Mode mode);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000251
252 static Handle<Object> GetSourceBreakLocations(
253 Handle<SharedFunctionInfo> shared);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000254
255 // Getter for the debug_context.
256 inline static Handle<Context> debug_context() { return debug_context_; }
257
258 // Check whether a global object is the debug global object.
259 static bool IsDebugGlobal(GlobalObject* global);
260
261 // Fast check to see if any break points are active.
262 inline static bool has_break_points() { return has_break_points_; }
263
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000264 static void NewBreak(StackFrame::Id break_frame_id);
265 static void SetBreak(StackFrame::Id break_frame_id, int break_id);
266 static StackFrame::Id break_frame_id() {
267 return thread_local_.break_frame_id_;
268 }
269 static int break_id() { return thread_local_.break_id_; }
270
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000271 static bool StepInActive() { return thread_local_.step_into_fp_ != 0; }
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000272 static void HandleStepIn(Handle<JSFunction> function,
273 Address fp,
274 bool is_constructor);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000275 static Address step_in_fp() { return thread_local_.step_into_fp_; }
276 static Address* step_in_fp_addr() { return &thread_local_.step_into_fp_; }
277
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000278 static EnterDebugger* debugger_entry() {
279 return thread_local_.debugger_entry_;
280 }
281 static void set_debugger_entry(EnterDebugger* entry) {
282 thread_local_.debugger_entry_ = entry;
283 }
284
285 static bool preemption_pending() {
286 return thread_local_.preemption_pending_;
287 }
288 static void set_preemption_pending(bool preemption_pending) {
289 thread_local_.preemption_pending_ = preemption_pending;
290 }
291
kasper.lundbd3ec4e2008-07-09 11:06:54 +0000292 // Getter and setter for the disable break state.
293 static bool disable_break() { return disable_break_; }
294 static void set_disable_break(bool disable_break) {
295 disable_break_ = disable_break;
296 }
297
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000298 // Getters for the current exception break state.
299 static bool break_on_exception() { return break_on_exception_; }
300 static bool break_on_uncaught_exception() {
301 return break_on_uncaught_exception_;
302 }
303
304 enum AddressId {
305 k_after_break_target_address,
306 k_debug_break_return_address,
307 k_register_address
308 };
309
310 // Support for setting the address to jump to when returning from break point.
311 static Address* after_break_target_address() {
312 return reinterpret_cast<Address*>(&thread_local_.after_break_target_);
313 }
314
315 // Support for saving/restoring registers when handling debug break calls.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000316 static Object** register_address(int r) {
317 return &registers_[r];
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000318 }
319
kasper.lund7276f142008-07-30 08:49:36 +0000320 // Address of the debug break return entry code.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000321 static Code* debug_break_return_entry() { return debug_break_return_entry_; }
322
323 // Support for getting the address of the debug break on return code.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000324 static Code** debug_break_return_address() {
325 return &debug_break_return_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000326 }
327
328 static const int kEstimatedNofDebugInfoEntries = 16;
329 static const int kEstimatedNofBreakPointsInFunction = 16;
330
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000331 static void HandleWeakDebugInfo(v8::Persistent<v8::Value> obj, void* data);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000332
333 friend class Debugger;
ager@chromium.org381abbb2009-02-25 13:23:22 +0000334 friend Handle<FixedArray> GetDebuggedFunctions(); // In test-debug.cc
335 friend void CheckDebuggerUnloaded(bool check_functions); // In test-debug.cc
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000336
337 // Threading support.
338 static char* ArchiveDebug(char* to);
339 static char* RestoreDebug(char* from);
340 static int ArchiveSpacePerThread();
341
ager@chromium.org32912102009-01-16 10:38:43 +0000342 // Mirror cache handling.
343 static void ClearMirrorCache();
344
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000345 // Script cache handling.
346 static void CreateScriptCache();
347 static void DestroyScriptCache();
348 static void AddScriptToScriptCache(Handle<Script> script);
349 static Handle<FixedArray> GetLoadedScripts();
350
351 // Garbage collection notifications.
352 static void AfterGarbageCollection();
353
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000354 // Code generation assumptions.
355 static const int kIa32CallInstructionLength = 5;
356 static const int kIa32JSReturnSequenceLength = 6;
357
ager@chromium.org8bb60582008-12-11 12:02:20 +0000358 // Code generator routines.
359 static void GenerateLoadICDebugBreak(MacroAssembler* masm);
360 static void GenerateStoreICDebugBreak(MacroAssembler* masm);
361 static void GenerateKeyedLoadICDebugBreak(MacroAssembler* masm);
362 static void GenerateKeyedStoreICDebugBreak(MacroAssembler* masm);
363 static void GenerateConstructCallDebugBreak(MacroAssembler* masm);
364 static void GenerateReturnDebugBreak(MacroAssembler* masm);
365 static void GenerateReturnDebugBreakEntry(MacroAssembler* masm);
366 static void GenerateStubNoRegistersDebugBreak(MacroAssembler* masm);
367
368 // Called from stub-cache.cc.
369 static void GenerateCallICDebugBreak(MacroAssembler* masm);
370
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000371 private:
372 static bool CompileDebuggerScript(int index);
373 static void ClearOneShot();
374 static void ActivateStepIn(StackFrame* frame);
375 static void ClearStepIn();
376 static void ClearStepNext();
ager@chromium.org32912102009-01-16 10:38:43 +0000377 // Returns whether the compile succeeded.
kasper.lundbd3ec4e2008-07-09 11:06:54 +0000378 static bool EnsureCompiled(Handle<SharedFunctionInfo> shared);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000379 static void RemoveDebugInfo(Handle<DebugInfo> debug_info);
380 static void SetAfterBreakTarget(JavaScriptFrame* frame);
381 static Handle<Object> CheckBreakPoints(Handle<Object> break_point);
382 static bool CheckBreakPoint(Handle<Object> break_point_object);
383
384 // Global handle to debug context where all the debugger JavaScript code is
385 // loaded.
386 static Handle<Context> debug_context_;
387
388 // Boolean state indicating whether any break points are set.
389 static bool has_break_points_;
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000390
391 // Cache of all scripts in the heap.
392 static ScriptCache* script_cache_;
393
394 // List of active debug info objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000395 static DebugInfoListNode* debug_info_list_;
396
kasper.lundbd3ec4e2008-07-09 11:06:54 +0000397 static bool disable_break_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000398 static bool break_on_exception_;
399 static bool break_on_uncaught_exception_;
400
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000401 // Per-thread data.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000402 class ThreadLocal {
403 public:
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000404 // Counter for generating next break id.
405 int break_count_;
406
407 // Current break id.
408 int break_id_;
409
410 // Frame id for the frame of the current break.
411 StackFrame::Id break_frame_id_;
412
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000413 // Step action for last step performed.
414 StepAction last_step_action_;
415
416 // Source statement position from last step next action.
417 int last_statement_position_;
418
419 // Number of steps left to perform before debug event.
420 int step_count_;
421
422 // Frame pointer from last step next action.
423 Address last_fp_;
424
425 // Frame pointer for frame from which step in was performed.
426 Address step_into_fp_;
427
428 // Storage location for jump when exiting debug break calls.
429 Address after_break_target_;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000430
431 // Top debugger entry.
432 EnterDebugger* debugger_entry_;
433
434 // Preemption happened while debugging.
435 bool preemption_pending_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000436 };
437
438 // Storage location for registers when handling debug break calls
439 static JSCallerSavedBuffer registers_;
440 static ThreadLocal thread_local_;
441 static void ThreadInit();
442
443 // Code object for debug break return entry code.
444 static Code* debug_break_return_entry_;
445
446 // Code to call for handling debug break on return.
447 static Code* debug_break_return_;
448
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000449 DISALLOW_COPY_AND_ASSIGN(Debug);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000450};
451
452
ager@chromium.org5ec48922009-05-05 07:25:34 +0000453// Message delivered to the message handler callback. This is either a debugger
454// event or the response to a command.
455class MessageImpl: public v8::Debug::Message {
456 public:
457 // Create a message object for a debug event.
458 static MessageImpl NewEvent(DebugEvent event,
459 bool running,
460 Handle<JSObject> exec_state,
461 Handle<JSObject> event_data);
462
463 // Create a message object for the response to a debug command.
464 static MessageImpl NewResponse(DebugEvent event,
465 bool running,
466 Handle<JSObject> exec_state,
467 Handle<JSObject> event_data,
468 Handle<String> response_json,
469 v8::Debug::ClientData* client_data);
470
471 // Implementation of interface v8::Debug::Message.
472 virtual bool IsEvent() const;
473 virtual bool IsResponse() const;
474 virtual DebugEvent GetEvent() const;
475 virtual bool WillStartRunning() const;
476 virtual v8::Handle<v8::Object> GetExecutionState() const;
477 virtual v8::Handle<v8::Object> GetEventData() const;
478 virtual v8::Handle<v8::String> GetJSON() const;
479 virtual v8::Handle<v8::Context> GetEventContext() const;
480 virtual v8::Debug::ClientData* GetClientData() const;
481
482 private:
483 MessageImpl(bool is_event,
484 DebugEvent event,
485 bool running,
486 Handle<JSObject> exec_state,
487 Handle<JSObject> event_data,
488 Handle<String> response_json,
489 v8::Debug::ClientData* client_data);
490
491 bool is_event_; // Does this message represent a debug event?
492 DebugEvent event_; // Debug event causing the break.
493 bool running_; // Will the VM start running after this event?
494 Handle<JSObject> exec_state_; // Current execution state.
495 Handle<JSObject> event_data_; // Data associated with the event.
496 Handle<String> response_json_; // Response JSON if message holds a response.
497 v8::Debug::ClientData* client_data_; // Client data passed with the request.
498};
499
500
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000501// Message send by user to v8 debugger or debugger output message.
502// In addition to command text it may contain a pointer to some user data
503// which are expected to be passed along with the command reponse to message
504// handler.
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000505class CommandMessage {
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000506 public:
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000507 static CommandMessage New(const Vector<uint16_t>& command,
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000508 v8::Debug::ClientData* data);
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000509 CommandMessage();
510 ~CommandMessage();
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000511
512 // Deletes user data and disposes of the text.
513 void Dispose();
514 Vector<uint16_t> text() const { return text_; }
515 v8::Debug::ClientData* client_data() const { return client_data_; }
516 private:
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000517 CommandMessage(const Vector<uint16_t>& text,
518 v8::Debug::ClientData* data);
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000519
520 Vector<uint16_t> text_;
521 v8::Debug::ClientData* client_data_;
522};
523
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000524// A Queue of CommandMessage objects. A thread-safe version is
525// LockingCommandMessageQueue, based on this class.
526class CommandMessageQueue BASE_EMBEDDED {
ager@chromium.org41826e72009-03-30 13:30:57 +0000527 public:
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000528 explicit CommandMessageQueue(int size);
529 ~CommandMessageQueue();
ager@chromium.org41826e72009-03-30 13:30:57 +0000530 bool IsEmpty() const { return start_ == end_; }
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000531 CommandMessage Get();
532 void Put(const CommandMessage& message);
ager@chromium.org41826e72009-03-30 13:30:57 +0000533 void Clear() { start_ = end_ = 0; } // Queue is empty after Clear().
534 private:
535 // Doubles the size of the message queue, and copies the messages.
536 void Expand();
537
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000538 CommandMessage* messages_;
ager@chromium.org41826e72009-03-30 13:30:57 +0000539 int start_;
540 int end_;
541 int size_; // The size of the queue buffer. Queue can hold size-1 messages.
542};
543
544
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000545// LockingCommandMessageQueue is a thread-safe circular buffer of CommandMessage
546// messages. The message data is not managed by LockingCommandMessageQueue.
ager@chromium.org41826e72009-03-30 13:30:57 +0000547// Pointers to the data are passed in and out. Implemented by adding a
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000548// Mutex to CommandMessageQueue. Includes logging of all puts and gets.
549class LockingCommandMessageQueue BASE_EMBEDDED {
ager@chromium.org41826e72009-03-30 13:30:57 +0000550 public:
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000551 explicit LockingCommandMessageQueue(int size);
552 ~LockingCommandMessageQueue();
ager@chromium.org41826e72009-03-30 13:30:57 +0000553 bool IsEmpty() const;
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000554 CommandMessage Get();
555 void Put(const CommandMessage& message);
ager@chromium.org41826e72009-03-30 13:30:57 +0000556 void Clear();
557 private:
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000558 CommandMessageQueue queue_;
ager@chromium.org41826e72009-03-30 13:30:57 +0000559 Mutex* lock_;
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000560 DISALLOW_COPY_AND_ASSIGN(LockingCommandMessageQueue);
ager@chromium.org41826e72009-03-30 13:30:57 +0000561};
562
563
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000564class Debugger {
565 public:
566 static void DebugRequest(const uint16_t* json_request, int length);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000567
568 static Handle<Object> MakeJSObject(Vector<const char> constructor_name,
569 int argc, Object*** argv,
570 bool* caught_exception);
571 static Handle<Object> MakeExecutionState(bool* caught_exception);
572 static Handle<Object> MakeBreakEvent(Handle<Object> exec_state,
573 Handle<Object> break_points_hit,
574 bool* caught_exception);
575 static Handle<Object> MakeExceptionEvent(Handle<Object> exec_state,
576 Handle<Object> exception,
577 bool uncaught,
578 bool* caught_exception);
579 static Handle<Object> MakeNewFunctionEvent(Handle<Object> func,
580 bool* caught_exception);
581 static Handle<Object> MakeCompileEvent(Handle<Script> script,
iposva@chromium.org245aa852009-02-10 00:49:54 +0000582 bool before,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000583 bool* caught_exception);
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000584 static Handle<Object> MakeScriptCollectedEvent(int id,
585 bool* caught_exception);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000586 static void OnDebugBreak(Handle<Object> break_points_hit, bool auto_continue);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000587 static void OnException(Handle<Object> exception, bool uncaught);
588 static void OnBeforeCompile(Handle<Script> script);
589 static void OnAfterCompile(Handle<Script> script,
590 Handle<JSFunction> fun);
591 static void OnNewFunction(Handle<JSFunction> fun);
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000592 static void OnScriptCollected(int id);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000593 static void ProcessDebugEvent(v8::DebugEvent event,
ager@chromium.org5ec48922009-05-05 07:25:34 +0000594 Handle<JSObject> event_data,
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000595 bool auto_continue);
ager@chromium.org41826e72009-03-30 13:30:57 +0000596 static void NotifyMessageHandler(v8::DebugEvent event,
ager@chromium.org5ec48922009-05-05 07:25:34 +0000597 Handle<JSObject> exec_state,
598 Handle<JSObject> event_data,
ager@chromium.org41826e72009-03-30 13:30:57 +0000599 bool auto_continue);
iposva@chromium.org245aa852009-02-10 00:49:54 +0000600 static void SetEventListener(Handle<Object> callback, Handle<Object> data);
ager@chromium.org5ec48922009-05-05 07:25:34 +0000601 static void SetMessageHandler(v8::Debug::MessageHandler2 handler);
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000602 static void SetHostDispatchHandler(v8::Debug::HostDispatchHandler handler,
603 int period);
ager@chromium.org41826e72009-03-30 13:30:57 +0000604
605 // Invoke the message handler function.
ager@chromium.org5ec48922009-05-05 07:25:34 +0000606 static void InvokeMessageHandler(MessageImpl message);
ager@chromium.org41826e72009-03-30 13:30:57 +0000607
608 // Add a debugger command to the command queue.
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000609 static void ProcessCommand(Vector<const uint16_t> command,
610 v8::Debug::ClientData* client_data = NULL);
ager@chromium.org41826e72009-03-30 13:30:57 +0000611
612 // Check whether there are commands in the command queue.
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000613 static bool HasCommands();
ager@chromium.org41826e72009-03-30 13:30:57 +0000614
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000615 static Handle<Object> Call(Handle<JSFunction> fun,
616 Handle<Object> data,
617 bool* pending_exception);
618
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000619 // Start the debugger agent listening on the provided port.
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000620 static bool StartAgent(const char* name, int port);
621
622 // Stop the debugger agent.
623 static void StopAgent();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000624
ager@chromium.org71daaf62009-04-01 07:22:49 +0000625 // Unload the debugger if possible. Only called when no debugger is currently
626 // active.
627 static void UnloadDebugger();
628
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000629 inline static bool EventActive(v8::DebugEvent event) {
ager@chromium.org71daaf62009-04-01 07:22:49 +0000630 ScopedLock with(debugger_access_);
631
632 // Check whether the message handler was been cleared.
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000633 if (debugger_unload_pending_) {
ager@chromium.org71daaf62009-04-01 07:22:49 +0000634 UnloadDebugger();
635 }
636
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000637 // Currently argument event is not used.
ager@chromium.org71daaf62009-04-01 07:22:49 +0000638 return !compiling_natives_ && Debugger::IsDebuggerActive();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000639 }
640
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000641 static void set_compiling_natives(bool compiling_natives) {
642 Debugger::compiling_natives_ = compiling_natives;
643 }
644 static bool compiling_natives() { return Debugger::compiling_natives_; }
mads.s.agercbaa0602008-08-14 13:41:48 +0000645 static void set_loading_debugger(bool v) { is_loading_debugger_ = v; }
646 static bool is_loading_debugger() { return Debugger::is_loading_debugger_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000647
648 private:
ager@chromium.org71daaf62009-04-01 07:22:49 +0000649 static bool IsDebuggerActive();
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000650 static void ListenersChanged();
ager@chromium.org71daaf62009-04-01 07:22:49 +0000651
652 static Mutex* debugger_access_; // Mutex guarding debugger variables.
653 static Handle<Object> event_listener_; // Global handle to listener.
iposva@chromium.org245aa852009-02-10 00:49:54 +0000654 static Handle<Object> event_listener_data_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000655 static bool compiling_natives_; // Are we compiling natives?
mads.s.agercbaa0602008-08-14 13:41:48 +0000656 static bool is_loading_debugger_; // Are we loading the debugger?
ager@chromium.org71daaf62009-04-01 07:22:49 +0000657 static bool never_unload_debugger_; // Can we unload the debugger?
ager@chromium.org5ec48922009-05-05 07:25:34 +0000658 static v8::Debug::MessageHandler2 message_handler_;
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000659 static bool debugger_unload_pending_; // Was message handler cleared?
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000660 static v8::Debug::HostDispatchHandler host_dispatch_handler_;
661 static int host_dispatch_micros_;
ager@chromium.org381abbb2009-02-25 13:23:22 +0000662
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000663 static DebuggerAgent* agent_;
664
ager@chromium.org41826e72009-03-30 13:30:57 +0000665 static const int kQueueInitialSize = 4;
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000666 static LockingCommandMessageQueue command_queue_;
ager@chromium.org41826e72009-03-30 13:30:57 +0000667 static Semaphore* command_received_; // Signaled for each command received.
ager@chromium.org41826e72009-03-30 13:30:57 +0000668
ager@chromium.org71daaf62009-04-01 07:22:49 +0000669 friend class EnterDebugger;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000670};
671
672
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000673// This class is used for entering the debugger. Create an instance in the stack
674// to enter the debugger. This will set the current break state, make sure the
675// debugger is loaded and switch to the debugger context. If the debugger for
676// some reason could not be entered FailedToEnter will return true.
677class EnterDebugger BASE_EMBEDDED {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000678 public:
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000679 EnterDebugger()
680 : prev_(Debug::debugger_entry()),
681 has_js_frames_(!it_.done()) {
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000682 ASSERT(prev_ == NULL ? !Debug::preemption_pending() : true);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000683
684 // Link recursive debugger entry.
685 Debug::set_debugger_entry(this);
686
ager@chromium.org8bb60582008-12-11 12:02:20 +0000687 // Store the previous break id and frame id.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000688 break_id_ = Debug::break_id();
689 break_frame_id_ = Debug::break_frame_id();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000690
ager@chromium.org8bb60582008-12-11 12:02:20 +0000691 // Create the new break info. If there is no JavaScript frames there is no
692 // break frame id.
693 if (has_js_frames_) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000694 Debug::NewBreak(it_.frame()->id());
ager@chromium.org8bb60582008-12-11 12:02:20 +0000695 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000696 Debug::NewBreak(StackFrame::NO_ID);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000697 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000698
699 // Make sure that debugger is loaded and enter the debugger context.
700 load_failed_ = !Debug::Load();
701 if (!load_failed_) {
702 // NOTE the member variable save which saves the previous context before
703 // this change.
704 Top::set_context(*Debug::debug_context());
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000705 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000706 }
707
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000708 ~EnterDebugger() {
ager@chromium.org8bb60582008-12-11 12:02:20 +0000709 // Restore to the previous break state.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000710 Debug::SetBreak(break_frame_id_, break_id_);
711
712 // Request preemption when leaving the last debugger entry and a preemption
713 // had been recorded while debugging. This is to avoid starvation in some
714 // debugging scenarios.
715 if (prev_ == NULL && Debug::preemption_pending()) {
716 StackGuard::Preempt();
717 Debug::set_preemption_pending(false);
718 }
719
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000720 // If there are commands in the queue when leaving the debugger request that
721 // these commands are processed.
722 if (prev_ == NULL && Debugger::HasCommands()) {
723 StackGuard::DebugCommand();
724 }
725
ager@chromium.org71daaf62009-04-01 07:22:49 +0000726 if (prev_ == NULL) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000727 // Clear mirror cache when leaving the debugger. Skip this if there is a
728 // pending exception as clearing the mirror cache calls back into
729 // JavaScript. This can happen if the v8::Debug::Call is used in which
730 // case the exception should end up in the calling code.
731 if (!Top::has_pending_exception()) {
732 Debug::ClearMirrorCache();
733 }
734 // If leaving the debugger with the debugger no longer active unload it.
ager@chromium.org71daaf62009-04-01 07:22:49 +0000735 if (!Debugger::IsDebuggerActive()) {
736 Debugger::UnloadDebugger();
737 }
738 }
739
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000740 // Leaving this debugger entry.
741 Debug::set_debugger_entry(prev_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000742 }
743
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000744 // Check whether the debugger could be entered.
745 inline bool FailedToEnter() { return load_failed_; }
746
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000747 // Check whether there are any JavaScript frames on the stack.
ager@chromium.org8bb60582008-12-11 12:02:20 +0000748 inline bool HasJavaScriptFrames() { return has_js_frames_; }
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000749
ager@chromium.org9085a012009-05-11 19:22:57 +0000750 // Get the active context from before entering the debugger.
751 inline Handle<Context> GetContext() { return save_.context(); }
752
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000753 private:
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000754 EnterDebugger* prev_; // Previous debugger entry if entered recursively.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000755 JavaScriptFrameIterator it_;
ager@chromium.org8bb60582008-12-11 12:02:20 +0000756 const bool has_js_frames_; // Were there any JavaScript frames?
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000757 StackFrame::Id break_frame_id_; // Previous break frame id.
758 int break_id_; // Previous break id.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000759 bool load_failed_; // Did the debugger fail to load?
760 SaveContext save_; // Saves previous context.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000761};
762
763
kasper.lundbd3ec4e2008-07-09 11:06:54 +0000764// Stack allocated class for disabling break.
765class DisableBreak BASE_EMBEDDED {
766 public:
767 // Enter the debugger by storing the previous top context and setting the
768 // current top context to the debugger context.
769 explicit DisableBreak(bool disable_break) {
770 prev_disable_break_ = Debug::disable_break();
771 Debug::set_disable_break(disable_break);
772 }
773 ~DisableBreak() {
774 Debug::set_disable_break(prev_disable_break_);
775 }
776
777 private:
778 // The previous state of the disable break used to restore the value when this
779 // object is destructed.
780 bool prev_disable_break_;
781};
782
783
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000784// Debug_Address encapsulates the Address pointers used in generating debug
785// code.
786class Debug_Address {
787 public:
788 Debug_Address(Debug::AddressId id, int reg = 0)
789 : id_(id), reg_(reg) {
790 ASSERT(reg == 0 || id == Debug::k_register_address);
791 }
792
793 static Debug_Address AfterBreakTarget() {
794 return Debug_Address(Debug::k_after_break_target_address);
795 }
796
797 static Debug_Address DebugBreakReturn() {
798 return Debug_Address(Debug::k_debug_break_return_address);
799 }
800
801 static Debug_Address Register(int reg) {
802 return Debug_Address(Debug::k_register_address, reg);
803 }
804
805 Address address() const {
806 switch (id_) {
807 case Debug::k_after_break_target_address:
808 return reinterpret_cast<Address>(Debug::after_break_target_address());
809 case Debug::k_debug_break_return_address:
810 return reinterpret_cast<Address>(Debug::debug_break_return_address());
811 case Debug::k_register_address:
812 return reinterpret_cast<Address>(Debug::register_address(reg_));
813 default:
814 UNREACHABLE();
815 return NULL;
816 }
817 }
818 private:
819 Debug::AddressId id_;
820 int reg_;
821};
822
823
824} } // namespace v8::internal
825
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000826#endif // ENABLE_DEBUGGER_SUPPORT
827
ager@chromium.org5ec48922009-05-05 07:25:34 +0000828#endif // V8_DEBUG_H_