blob: 1d534d6e01042280a7a984305c2f021329cbe298 [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.org9258b6b2008-09-11 09:11:10 +000028#ifndef V8_V8_DEBUG_H_
29#define V8_V8_DEBUG_H_
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000030
ager@chromium.orgc27e4e72008-09-04 13:52:27 +000031#include "../include/v8-debug.h"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000032#include "assembler.h"
33#include "code-stubs.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"
36#include "platform.h"
37#include "string-stream.h"
38
39
40namespace v8 { namespace internal {
41
42// Step actions. NOTE: These values are in macros.py as well.
43enum StepAction {
44 StepNone = -1, // Stepping not prepared.
45 StepOut = 0, // Step out of the current function.
46 StepNext = 1, // Step to the next statement in the current function.
47 StepIn = 2, // Step into new functions invoked or the next statement
48 // in the current function.
49 StepMin = 3, // Perform a minimum step in the current function.
50 StepInMin = 4 // Step into new functions invoked or perform a minimum step
51 // in the current function.
52};
53
54
55// Type of exception break. NOTE: These values are in macros.py as well.
56enum ExceptionBreakType {
57 BreakException = 0,
58 BreakUncaughtException = 1
59};
60
61
62// Type of exception break. NOTE: These values are in macros.py as well.
63enum BreakLocatorType {
64 ALL_BREAK_LOCATIONS = 0,
65 SOURCE_BREAK_LOCATIONS = 1
66};
67
68
69// Class for iterating through the break points in a function and changing
70// them.
71class BreakLocationIterator {
72 public:
73 explicit BreakLocationIterator(Handle<DebugInfo> debug_info,
74 BreakLocatorType type);
75 virtual ~BreakLocationIterator();
76
77 void Next();
78 void Next(int count);
79 void FindBreakLocationFromAddress(Address pc);
80 void FindBreakLocationFromPosition(int position);
81 void Reset();
82 bool Done() const;
83 void SetBreakPoint(Handle<Object> break_point_object);
84 void ClearBreakPoint(Handle<Object> break_point_object);
85 void SetOneShot();
86 void ClearOneShot();
87 void PrepareStepIn();
88 bool IsExit() const;
89 bool HasBreakPoint();
90 bool IsDebugBreak();
91 Object* BreakPointObjects();
ager@chromium.org381abbb2009-02-25 13:23:22 +000092 void ClearAllDebugBreak();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000093
94
95 inline int code_position() { return pc() - debug_info_->code()->entry(); }
96 inline int break_point() { return break_point_; }
97 inline int position() { return position_; }
98 inline int statement_position() { return statement_position_; }
99 inline Address pc() { return reloc_iterator_->rinfo()->pc(); }
100 inline Code* code() { return debug_info_->code(); }
101 inline RelocInfo* rinfo() { return reloc_iterator_->rinfo(); }
ager@chromium.org236ad962008-09-25 09:45:57 +0000102 inline RelocInfo::Mode rmode() const {
103 return reloc_iterator_->rinfo()->rmode();
104 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000105 inline RelocInfo* original_rinfo() {
106 return reloc_iterator_original_->rinfo();
107 }
ager@chromium.org236ad962008-09-25 09:45:57 +0000108 inline RelocInfo::Mode original_rmode() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000109 return reloc_iterator_original_->rinfo()->rmode();
110 }
111
112 protected:
113 bool RinfoDone() const;
114 void RinfoNext();
115
116 BreakLocatorType type_;
117 int break_point_;
118 int position_;
119 int statement_position_;
120 Handle<DebugInfo> debug_info_;
121 RelocIterator* reloc_iterator_;
122 RelocIterator* reloc_iterator_original_;
123
124 private:
125 void SetDebugBreak();
126 void ClearDebugBreak();
iposva@chromium.org245aa852009-02-10 00:49:54 +0000127 bool IsDebugBreakAtReturn();
128 void SetDebugBreakAtReturn();
129 void ClearDebugBreakAtReturn();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000130
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000131 DISALLOW_COPY_AND_ASSIGN(BreakLocationIterator);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000132};
133
134
135// Linked list holding debug info objects. The debug info objects are kept as
136// weak handles to avoid a debug info object to keep a function alive.
137class DebugInfoListNode {
138 public:
139 explicit DebugInfoListNode(DebugInfo* debug_info);
140 virtual ~DebugInfoListNode();
141
142 DebugInfoListNode* next() { return next_; }
143 void set_next(DebugInfoListNode* next) { next_ = next; }
144 Handle<DebugInfo> debug_info() { return debug_info_; }
145
146 private:
147 // Global (weak) handle to the debug info object.
148 Handle<DebugInfo> debug_info_;
149
150 // Next pointer for linked list.
151 DebugInfoListNode* next_;
152};
153
154
155// This class contains the debugger support. The main purpose is to handle
156// setting break points in the code.
157//
158// This class controls the debug info for all functions which currently have
159// active breakpoints in them. This debug info is held in the heap root object
160// debug_info which is a FixedArray. Each entry in this list is of class
161// DebugInfo.
162class Debug {
163 public:
164 static void Setup(bool create_heap_objects);
165 static bool Load();
166 static void Unload();
167 static bool IsLoaded() { return !debug_context_.is_null(); }
168 static bool InDebugger() { return Top::is_break(); }
169 static void Iterate(ObjectVisitor* v);
170
171 static Object* Break(Arguments args);
172 static void SetBreakPoint(Handle<SharedFunctionInfo> shared,
173 int source_position,
174 Handle<Object> break_point_object);
175 static void ClearBreakPoint(Handle<Object> break_point_object);
ager@chromium.org381abbb2009-02-25 13:23:22 +0000176 static void ClearAllBreakPoints();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000177 static void FloodWithOneShot(Handle<SharedFunctionInfo> shared);
178 static void FloodHandlerWithOneShot();
179 static void ChangeBreakOnException(ExceptionBreakType type, bool enable);
180 static void PrepareStep(StepAction step_action, int step_count);
181 static void ClearStepping();
182 static bool StepNextContinue(BreakLocationIterator* break_location_iterator,
183 JavaScriptFrame* frame);
184 static Handle<DebugInfo> GetDebugInfo(Handle<SharedFunctionInfo> shared);
185 static bool HasDebugInfo(Handle<SharedFunctionInfo> shared);
kasper.lundbd3ec4e2008-07-09 11:06:54 +0000186
ager@chromium.org32912102009-01-16 10:38:43 +0000187 // Returns whether the operation succeeded.
kasper.lundbd3ec4e2008-07-09 11:06:54 +0000188 static bool EnsureDebugInfo(Handle<SharedFunctionInfo> shared);
189
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000190 static bool IsDebugBreak(Address addr);
ager@chromium.org381abbb2009-02-25 13:23:22 +0000191 static bool IsDebugBreakAtReturn(RelocInfo* rinfo);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000192
193 // Check whether a code stub with the specified major key is a possible break
194 // point location.
195 static bool IsSourceBreakStub(Code* code);
196 static bool IsBreakStub(Code* code);
197
198 // Find the builtin to use for invoking the debug break
199 static Handle<Code> FindDebugBreak(RelocInfo* rinfo);
200
201 static Handle<Object> GetSourceBreakLocations(
202 Handle<SharedFunctionInfo> shared);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000203
204 // Getter for the debug_context.
205 inline static Handle<Context> debug_context() { return debug_context_; }
206
207 // Check whether a global object is the debug global object.
208 static bool IsDebugGlobal(GlobalObject* global);
209
210 // Fast check to see if any break points are active.
211 inline static bool has_break_points() { return has_break_points_; }
212
213 static bool StepInActive() { return thread_local_.step_into_fp_ != 0; }
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000214 static void HandleStepIn(Handle<JSFunction> function,
215 Address fp,
216 bool is_constructor);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000217 static Address step_in_fp() { return thread_local_.step_into_fp_; }
218 static Address* step_in_fp_addr() { return &thread_local_.step_into_fp_; }
219
kasper.lundbd3ec4e2008-07-09 11:06:54 +0000220 // Getter and setter for the disable break state.
221 static bool disable_break() { return disable_break_; }
222 static void set_disable_break(bool disable_break) {
223 disable_break_ = disable_break;
224 }
225
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000226 // Getters for the current exception break state.
227 static bool break_on_exception() { return break_on_exception_; }
228 static bool break_on_uncaught_exception() {
229 return break_on_uncaught_exception_;
230 }
231
232 enum AddressId {
233 k_after_break_target_address,
234 k_debug_break_return_address,
235 k_register_address
236 };
237
238 // Support for setting the address to jump to when returning from break point.
239 static Address* after_break_target_address() {
240 return reinterpret_cast<Address*>(&thread_local_.after_break_target_);
241 }
242
243 // Support for saving/restoring registers when handling debug break calls.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000244 static Object** register_address(int r) {
245 return &registers_[r];
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000246 }
247
kasper.lund7276f142008-07-30 08:49:36 +0000248 // Address of the debug break return entry code.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000249 static Code* debug_break_return_entry() { return debug_break_return_entry_; }
250
251 // Support for getting the address of the debug break on return code.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000252 static Code** debug_break_return_address() {
253 return &debug_break_return_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000254 }
255
256 static const int kEstimatedNofDebugInfoEntries = 16;
257 static const int kEstimatedNofBreakPointsInFunction = 16;
258
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000259 static void HandleWeakDebugInfo(v8::Persistent<v8::Value> obj, void* data);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000260
261 friend class Debugger;
ager@chromium.org381abbb2009-02-25 13:23:22 +0000262 friend Handle<FixedArray> GetDebuggedFunctions(); // In test-debug.cc
263 friend void CheckDebuggerUnloaded(bool check_functions); // In test-debug.cc
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000264
265 // Threading support.
266 static char* ArchiveDebug(char* to);
267 static char* RestoreDebug(char* from);
268 static int ArchiveSpacePerThread();
269
ager@chromium.org32912102009-01-16 10:38:43 +0000270 // Mirror cache handling.
271 static void ClearMirrorCache();
272
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000273 // Code generation assumptions.
274 static const int kIa32CallInstructionLength = 5;
275 static const int kIa32JSReturnSequenceLength = 6;
276
ager@chromium.org8bb60582008-12-11 12:02:20 +0000277 // Code generator routines.
278 static void GenerateLoadICDebugBreak(MacroAssembler* masm);
279 static void GenerateStoreICDebugBreak(MacroAssembler* masm);
280 static void GenerateKeyedLoadICDebugBreak(MacroAssembler* masm);
281 static void GenerateKeyedStoreICDebugBreak(MacroAssembler* masm);
282 static void GenerateConstructCallDebugBreak(MacroAssembler* masm);
283 static void GenerateReturnDebugBreak(MacroAssembler* masm);
284 static void GenerateReturnDebugBreakEntry(MacroAssembler* masm);
285 static void GenerateStubNoRegistersDebugBreak(MacroAssembler* masm);
286
287 // Called from stub-cache.cc.
288 static void GenerateCallICDebugBreak(MacroAssembler* masm);
289
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000290 private:
291 static bool CompileDebuggerScript(int index);
292 static void ClearOneShot();
293 static void ActivateStepIn(StackFrame* frame);
294 static void ClearStepIn();
295 static void ClearStepNext();
ager@chromium.org32912102009-01-16 10:38:43 +0000296 // Returns whether the compile succeeded.
kasper.lundbd3ec4e2008-07-09 11:06:54 +0000297 static bool EnsureCompiled(Handle<SharedFunctionInfo> shared);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000298 static void RemoveDebugInfo(Handle<DebugInfo> debug_info);
299 static void SetAfterBreakTarget(JavaScriptFrame* frame);
300 static Handle<Object> CheckBreakPoints(Handle<Object> break_point);
301 static bool CheckBreakPoint(Handle<Object> break_point_object);
302
303 // Global handle to debug context where all the debugger JavaScript code is
304 // loaded.
305 static Handle<Context> debug_context_;
306
307 // Boolean state indicating whether any break points are set.
308 static bool has_break_points_;
309 static DebugInfoListNode* debug_info_list_;
310
kasper.lundbd3ec4e2008-07-09 11:06:54 +0000311 static bool disable_break_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000312 static bool break_on_exception_;
313 static bool break_on_uncaught_exception_;
314
315 // Per-thread:
316 class ThreadLocal {
317 public:
318 // Step action for last step performed.
319 StepAction last_step_action_;
320
321 // Source statement position from last step next action.
322 int last_statement_position_;
323
324 // Number of steps left to perform before debug event.
325 int step_count_;
326
327 // Frame pointer from last step next action.
328 Address last_fp_;
329
330 // Frame pointer for frame from which step in was performed.
331 Address step_into_fp_;
332
333 // Storage location for jump when exiting debug break calls.
334 Address after_break_target_;
335 };
336
337 // Storage location for registers when handling debug break calls
338 static JSCallerSavedBuffer registers_;
339 static ThreadLocal thread_local_;
340 static void ThreadInit();
341
342 // Code object for debug break return entry code.
343 static Code* debug_break_return_entry_;
344
345 // Code to call for handling debug break on return.
346 static Code* debug_break_return_;
347
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000348 DISALLOW_COPY_AND_ASSIGN(Debug);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000349};
350
351
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000352class DebugMessageThread;
353
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000354class Debugger {
355 public:
356 static void DebugRequest(const uint16_t* json_request, int length);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000357
358 static Handle<Object> MakeJSObject(Vector<const char> constructor_name,
359 int argc, Object*** argv,
360 bool* caught_exception);
361 static Handle<Object> MakeExecutionState(bool* caught_exception);
362 static Handle<Object> MakeBreakEvent(Handle<Object> exec_state,
363 Handle<Object> break_points_hit,
364 bool* caught_exception);
365 static Handle<Object> MakeExceptionEvent(Handle<Object> exec_state,
366 Handle<Object> exception,
367 bool uncaught,
368 bool* caught_exception);
369 static Handle<Object> MakeNewFunctionEvent(Handle<Object> func,
370 bool* caught_exception);
371 static Handle<Object> MakeCompileEvent(Handle<Script> script,
iposva@chromium.org245aa852009-02-10 00:49:54 +0000372 bool before,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000373 bool* caught_exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000374 static void OnDebugBreak(Handle<Object> break_points_hit);
375 static void OnException(Handle<Object> exception, bool uncaught);
376 static void OnBeforeCompile(Handle<Script> script);
377 static void OnAfterCompile(Handle<Script> script,
378 Handle<JSFunction> fun);
379 static void OnNewFunction(Handle<JSFunction> fun);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000380 static void ProcessDebugEvent(v8::DebugEvent event,
381 Handle<Object> event_data);
iposva@chromium.org245aa852009-02-10 00:49:54 +0000382 static void SetEventListener(Handle<Object> callback, Handle<Object> data);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000383 static void SetMessageHandler(v8::DebugMessageHandler handler, void* data);
ager@chromium.org381abbb2009-02-25 13:23:22 +0000384 static void SetHostDispatchHandler(v8::DebugHostDispatchHandler handler,
385 void* data);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000386 static void SendMessage(Vector<uint16_t> message);
387 static void ProcessCommand(Vector<const uint16_t> command);
ager@chromium.org381abbb2009-02-25 13:23:22 +0000388 static void ProcessHostDispatch(void* dispatch);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000389 static void UpdateActiveDebugger();
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000390 static Handle<Object> Call(Handle<JSFunction> fun,
391 Handle<Object> data,
392 bool* pending_exception);
393
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000394 inline static bool EventActive(v8::DebugEvent event) {
395 // Currently argument event is not used.
396 return !Debugger::compiling_natives_ && Debugger::debugger_active_;
397 }
398
399 static void set_debugger_active(bool debugger_active) {
400 Debugger::debugger_active_ = debugger_active;
401 }
402 static bool debugger_active() { return Debugger::debugger_active_; }
403 static void set_compiling_natives(bool compiling_natives) {
404 Debugger::compiling_natives_ = compiling_natives;
405 }
406 static bool compiling_natives() { return Debugger::compiling_natives_; }
mads.s.agercbaa0602008-08-14 13:41:48 +0000407 static void set_loading_debugger(bool v) { is_loading_debugger_ = v; }
408 static bool is_loading_debugger() { return Debugger::is_loading_debugger_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000409
410 private:
iposva@chromium.org245aa852009-02-10 00:49:54 +0000411 static Handle<Object> event_listener_; // Global handle to listener
412 static Handle<Object> event_listener_data_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000413 static bool debugger_active_; // Are there any active debugger?
414 static bool compiling_natives_; // Are we compiling natives?
mads.s.agercbaa0602008-08-14 13:41:48 +0000415 static bool is_loading_debugger_; // Are we loading the debugger?
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000416 static DebugMessageThread* message_thread_;
ager@chromium.org381abbb2009-02-25 13:23:22 +0000417 static v8::DebugMessageHandler message_handler_;
418 static void* message_handler_data_;
419 static v8::DebugHostDispatchHandler host_dispatch_handler_;
420 static void* host_dispatch_handler_data_;
421
422 friend class DebugMessageThread;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000423};
424
425
kasper.lund7276f142008-07-30 08:49:36 +0000426// A Queue of Vector<uint16_t> objects. A thread-safe version is
427// LockingMessageQueue, based on this class.
428class MessageQueue BASE_EMBEDDED {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000429 public:
kasper.lund7276f142008-07-30 08:49:36 +0000430 explicit MessageQueue(int size);
431 ~MessageQueue();
432 bool IsEmpty() const { return start_ == end_; }
433 Vector<uint16_t> Get();
434 void Put(const Vector<uint16_t>& message);
435 void Clear() { start_ = end_ = 0; } // Queue is empty after Clear().
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000436 private:
kasper.lund7276f142008-07-30 08:49:36 +0000437 // Doubles the size of the message queue, and copies the messages.
438 void Expand();
439
440 Vector<uint16_t>* messages_;
441 int start_;
442 int end_;
443 int size_; // The size of the queue buffer. Queue can hold size-1 messages.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000444};
445
446
kasper.lund7276f142008-07-30 08:49:36 +0000447// LockingMessageQueue is a thread-safe circular buffer of Vector<uint16_t>
448// messages. The message data is not managed by LockingMessageQueue.
449// Pointers to the data are passed in and out. Implemented by adding a
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +0000450// Mutex to MessageQueue. Includes logging of all puts and gets.
kasper.lund7276f142008-07-30 08:49:36 +0000451class LockingMessageQueue BASE_EMBEDDED {
452 public:
453 explicit LockingMessageQueue(int size);
454 ~LockingMessageQueue();
455 bool IsEmpty() const;
456 Vector<uint16_t> Get();
457 void Put(const Vector<uint16_t>& message);
458 void Clear();
459 private:
kasper.lund7276f142008-07-30 08:49:36 +0000460 MessageQueue queue_;
461 Mutex* lock_;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000462 DISALLOW_COPY_AND_ASSIGN(LockingMessageQueue);
kasper.lund7276f142008-07-30 08:49:36 +0000463};
464
465
466/* This class is the data for a running thread that serializes
467 * event messages and command processing for the debugger.
468 * All uncommented methods are called only from this message thread.
469 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000470class DebugMessageThread: public Thread {
471 public:
kasper.lund7276f142008-07-30 08:49:36 +0000472 DebugMessageThread(); // Called from API thread.
473 virtual ~DebugMessageThread(); // Never called.
474 // Called by V8 thread. Reports events from V8 VM.
475 // Also handles command processing in stopped state of V8,
476 // when host_running_ is false.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000477 void DebugEvent(v8::DebugEvent,
478 Handle<Object> exec_state,
479 Handle<Object> event_data);
kasper.lund7276f142008-07-30 08:49:36 +0000480 // Puts event on the output queue. Called by V8.
481 // This is where V8 hands off
482 // processing of the event to the DebugMessageThread thread,
483 // which forwards it to the debug_message_handler set by the API.
484 void SendMessage(Vector<uint16_t> event_json);
485 // Formats an event into JSON, and calls SendMessage.
ager@chromium.org8bb60582008-12-11 12:02:20 +0000486 bool SetEventJSONFromEvent(Handle<Object> event_data);
kasper.lund7276f142008-07-30 08:49:36 +0000487 // Puts a command coming from the public API on the queue. Called
488 // by the API client thread. This is where the API client hands off
489 // processing of the command to the DebugMessageThread thread.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000490 void ProcessCommand(Vector<uint16_t> command);
ager@chromium.org381abbb2009-02-25 13:23:22 +0000491 void ProcessHostDispatch(void* dispatch);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000492 void OnDebuggerInactive();
493
kasper.lund7276f142008-07-30 08:49:36 +0000494 // Main function of DebugMessageThread thread.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000495 void Run();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000496
kasper.lund7276f142008-07-30 08:49:36 +0000497 bool host_running_; // Is the debugging host running or stopped?
498 Semaphore* command_received_; // Non-zero when command queue is non-empty.
499 Semaphore* message_received_; // Exactly equal to message queue length.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000500 private:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000501 bool TwoByteEqualsAscii(Vector<uint16_t> two_byte, const char* ascii);
502
kasper.lund7276f142008-07-30 08:49:36 +0000503 static const int kQueueInitialSize = 4;
504 LockingMessageQueue command_queue_;
505 LockingMessageQueue message_queue_;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000506 DISALLOW_COPY_AND_ASSIGN(DebugMessageThread);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000507};
508
509
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000510// This class is used for entering the debugger. Create an instance in the stack
511// to enter the debugger. This will set the current break state, make sure the
512// debugger is loaded and switch to the debugger context. If the debugger for
513// some reason could not be entered FailedToEnter will return true.
514class EnterDebugger BASE_EMBEDDED {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000515 public:
ager@chromium.org8bb60582008-12-11 12:02:20 +0000516 EnterDebugger() : has_js_frames_(!it_.done()) {
517 // Store the previous break id and frame id.
518 break_id_ = Top::break_id();
519 break_frame_id_ = Top::break_frame_id();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000520
ager@chromium.org8bb60582008-12-11 12:02:20 +0000521 // Create the new break info. If there is no JavaScript frames there is no
522 // break frame id.
523 if (has_js_frames_) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000524 Top::new_break(it_.frame()->id());
ager@chromium.org8bb60582008-12-11 12:02:20 +0000525 } else {
526 Top::new_break(StackFrame::NO_ID);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000527 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000528
529 // Make sure that debugger is loaded and enter the debugger context.
530 load_failed_ = !Debug::Load();
531 if (!load_failed_) {
532 // NOTE the member variable save which saves the previous context before
533 // this change.
534 Top::set_context(*Debug::debug_context());
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000535 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000536 }
537
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000538 ~EnterDebugger() {
ager@chromium.org8bb60582008-12-11 12:02:20 +0000539 // Restore to the previous break state.
540 Top::set_break(break_frame_id_, break_id_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000541 }
542
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000543 // Check whether the debugger could be entered.
544 inline bool FailedToEnter() { return load_failed_; }
545
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000546 // Check whether there are any JavaScript frames on the stack.
ager@chromium.org8bb60582008-12-11 12:02:20 +0000547 inline bool HasJavaScriptFrames() { return has_js_frames_; }
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000548
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000549 private:
550 JavaScriptFrameIterator it_;
ager@chromium.org8bb60582008-12-11 12:02:20 +0000551 const bool has_js_frames_; // Were there any JavaScript frames?
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000552 StackFrame::Id break_frame_id_; // Previous break frame id.
553 int break_id_; // Previous break id.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000554 bool load_failed_; // Did the debugger fail to load?
555 SaveContext save_; // Saves previous context.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000556};
557
558
kasper.lundbd3ec4e2008-07-09 11:06:54 +0000559// Stack allocated class for disabling break.
560class DisableBreak BASE_EMBEDDED {
561 public:
562 // Enter the debugger by storing the previous top context and setting the
563 // current top context to the debugger context.
564 explicit DisableBreak(bool disable_break) {
565 prev_disable_break_ = Debug::disable_break();
566 Debug::set_disable_break(disable_break);
567 }
568 ~DisableBreak() {
569 Debug::set_disable_break(prev_disable_break_);
570 }
571
572 private:
573 // The previous state of the disable break used to restore the value when this
574 // object is destructed.
575 bool prev_disable_break_;
576};
577
578
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000579// Debug_Address encapsulates the Address pointers used in generating debug
580// code.
581class Debug_Address {
582 public:
583 Debug_Address(Debug::AddressId id, int reg = 0)
584 : id_(id), reg_(reg) {
585 ASSERT(reg == 0 || id == Debug::k_register_address);
586 }
587
588 static Debug_Address AfterBreakTarget() {
589 return Debug_Address(Debug::k_after_break_target_address);
590 }
591
592 static Debug_Address DebugBreakReturn() {
593 return Debug_Address(Debug::k_debug_break_return_address);
594 }
595
596 static Debug_Address Register(int reg) {
597 return Debug_Address(Debug::k_register_address, reg);
598 }
599
600 Address address() const {
601 switch (id_) {
602 case Debug::k_after_break_target_address:
603 return reinterpret_cast<Address>(Debug::after_break_target_address());
604 case Debug::k_debug_break_return_address:
605 return reinterpret_cast<Address>(Debug::debug_break_return_address());
606 case Debug::k_register_address:
607 return reinterpret_cast<Address>(Debug::register_address(reg_));
608 default:
609 UNREACHABLE();
610 return NULL;
611 }
612 }
613 private:
614 Debug::AddressId id_;
615 int reg_;
616};
617
618
619} } // namespace v8::internal
620
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000621#endif // V8_V8_DEBUG_H_