blob: 4ac9221dfdee84fafde313cf5f840e23ae4abb9a [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();
92
93
94 inline int code_position() { return pc() - debug_info_->code()->entry(); }
95 inline int break_point() { return break_point_; }
96 inline int position() { return position_; }
97 inline int statement_position() { return statement_position_; }
98 inline Address pc() { return reloc_iterator_->rinfo()->pc(); }
99 inline Code* code() { return debug_info_->code(); }
100 inline RelocInfo* rinfo() { return reloc_iterator_->rinfo(); }
ager@chromium.org236ad962008-09-25 09:45:57 +0000101 inline RelocInfo::Mode rmode() const {
102 return reloc_iterator_->rinfo()->rmode();
103 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000104 inline RelocInfo* original_rinfo() {
105 return reloc_iterator_original_->rinfo();
106 }
ager@chromium.org236ad962008-09-25 09:45:57 +0000107 inline RelocInfo::Mode original_rmode() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000108 return reloc_iterator_original_->rinfo()->rmode();
109 }
110
111 protected:
112 bool RinfoDone() const;
113 void RinfoNext();
114
115 BreakLocatorType type_;
116 int break_point_;
117 int position_;
118 int statement_position_;
119 Handle<DebugInfo> debug_info_;
120 RelocIterator* reloc_iterator_;
121 RelocIterator* reloc_iterator_original_;
122
123 private:
124 void SetDebugBreak();
125 void ClearDebugBreak();
iposva@chromium.org245aa852009-02-10 00:49:54 +0000126 bool IsDebugBreakAtReturn();
127 void SetDebugBreakAtReturn();
128 void ClearDebugBreakAtReturn();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000129
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000130 DISALLOW_COPY_AND_ASSIGN(BreakLocationIterator);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000131};
132
133
134// Linked list holding debug info objects. The debug info objects are kept as
135// weak handles to avoid a debug info object to keep a function alive.
136class DebugInfoListNode {
137 public:
138 explicit DebugInfoListNode(DebugInfo* debug_info);
139 virtual ~DebugInfoListNode();
140
141 DebugInfoListNode* next() { return next_; }
142 void set_next(DebugInfoListNode* next) { next_ = next; }
143 Handle<DebugInfo> debug_info() { return debug_info_; }
144
145 private:
146 // Global (weak) handle to the debug info object.
147 Handle<DebugInfo> debug_info_;
148
149 // Next pointer for linked list.
150 DebugInfoListNode* next_;
151};
152
153
154// This class contains the debugger support. The main purpose is to handle
155// setting break points in the code.
156//
157// This class controls the debug info for all functions which currently have
158// active breakpoints in them. This debug info is held in the heap root object
159// debug_info which is a FixedArray. Each entry in this list is of class
160// DebugInfo.
161class Debug {
162 public:
163 static void Setup(bool create_heap_objects);
164 static bool Load();
165 static void Unload();
166 static bool IsLoaded() { return !debug_context_.is_null(); }
167 static bool InDebugger() { return Top::is_break(); }
168 static void Iterate(ObjectVisitor* v);
169
170 static Object* Break(Arguments args);
171 static void SetBreakPoint(Handle<SharedFunctionInfo> shared,
172 int source_position,
173 Handle<Object> break_point_object);
174 static void ClearBreakPoint(Handle<Object> break_point_object);
175 static void FloodWithOneShot(Handle<SharedFunctionInfo> shared);
176 static void FloodHandlerWithOneShot();
177 static void ChangeBreakOnException(ExceptionBreakType type, bool enable);
178 static void PrepareStep(StepAction step_action, int step_count);
179 static void ClearStepping();
180 static bool StepNextContinue(BreakLocationIterator* break_location_iterator,
181 JavaScriptFrame* frame);
182 static Handle<DebugInfo> GetDebugInfo(Handle<SharedFunctionInfo> shared);
183 static bool HasDebugInfo(Handle<SharedFunctionInfo> shared);
kasper.lundbd3ec4e2008-07-09 11:06:54 +0000184
ager@chromium.org32912102009-01-16 10:38:43 +0000185 // Returns whether the operation succeeded.
kasper.lundbd3ec4e2008-07-09 11:06:54 +0000186 static bool EnsureDebugInfo(Handle<SharedFunctionInfo> shared);
187
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000188 static bool IsDebugBreak(Address addr);
189
190 // Check whether a code stub with the specified major key is a possible break
191 // point location.
192 static bool IsSourceBreakStub(Code* code);
193 static bool IsBreakStub(Code* code);
194
195 // Find the builtin to use for invoking the debug break
196 static Handle<Code> FindDebugBreak(RelocInfo* rinfo);
197
198 static Handle<Object> GetSourceBreakLocations(
199 Handle<SharedFunctionInfo> shared);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000200
201 // Getter for the debug_context.
202 inline static Handle<Context> debug_context() { return debug_context_; }
203
204 // Check whether a global object is the debug global object.
205 static bool IsDebugGlobal(GlobalObject* global);
206
207 // Fast check to see if any break points are active.
208 inline static bool has_break_points() { return has_break_points_; }
209
210 static bool StepInActive() { return thread_local_.step_into_fp_ != 0; }
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000211 static void HandleStepIn(Handle<JSFunction> function,
212 Address fp,
213 bool is_constructor);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000214 static Address step_in_fp() { return thread_local_.step_into_fp_; }
215 static Address* step_in_fp_addr() { return &thread_local_.step_into_fp_; }
216
kasper.lundbd3ec4e2008-07-09 11:06:54 +0000217 // Getter and setter for the disable break state.
218 static bool disable_break() { return disable_break_; }
219 static void set_disable_break(bool disable_break) {
220 disable_break_ = disable_break;
221 }
222
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000223 // Getters for the current exception break state.
224 static bool break_on_exception() { return break_on_exception_; }
225 static bool break_on_uncaught_exception() {
226 return break_on_uncaught_exception_;
227 }
228
229 enum AddressId {
230 k_after_break_target_address,
231 k_debug_break_return_address,
232 k_register_address
233 };
234
235 // Support for setting the address to jump to when returning from break point.
236 static Address* after_break_target_address() {
237 return reinterpret_cast<Address*>(&thread_local_.after_break_target_);
238 }
239
240 // Support for saving/restoring registers when handling debug break calls.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000241 static Object** register_address(int r) {
242 return &registers_[r];
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000243 }
244
kasper.lund7276f142008-07-30 08:49:36 +0000245 // Address of the debug break return entry code.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000246 static Code* debug_break_return_entry() { return debug_break_return_entry_; }
247
248 // Support for getting the address of the debug break on return code.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000249 static Code** debug_break_return_address() {
250 return &debug_break_return_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000251 }
252
253 static const int kEstimatedNofDebugInfoEntries = 16;
254 static const int kEstimatedNofBreakPointsInFunction = 16;
255
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000256 static void HandleWeakDebugInfo(v8::Persistent<v8::Value> obj, void* data);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000257
258 friend class Debugger;
259 friend Handle<FixedArray> GetDebuggedFunctions(); // Found in test-debug.cc
260
261 // Threading support.
262 static char* ArchiveDebug(char* to);
263 static char* RestoreDebug(char* from);
264 static int ArchiveSpacePerThread();
265
ager@chromium.org32912102009-01-16 10:38:43 +0000266 // Mirror cache handling.
267 static void ClearMirrorCache();
268
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000269 // Code generation assumptions.
270 static const int kIa32CallInstructionLength = 5;
271 static const int kIa32JSReturnSequenceLength = 6;
272
ager@chromium.org8bb60582008-12-11 12:02:20 +0000273 // Code generator routines.
274 static void GenerateLoadICDebugBreak(MacroAssembler* masm);
275 static void GenerateStoreICDebugBreak(MacroAssembler* masm);
276 static void GenerateKeyedLoadICDebugBreak(MacroAssembler* masm);
277 static void GenerateKeyedStoreICDebugBreak(MacroAssembler* masm);
278 static void GenerateConstructCallDebugBreak(MacroAssembler* masm);
279 static void GenerateReturnDebugBreak(MacroAssembler* masm);
280 static void GenerateReturnDebugBreakEntry(MacroAssembler* masm);
281 static void GenerateStubNoRegistersDebugBreak(MacroAssembler* masm);
282
283 // Called from stub-cache.cc.
284 static void GenerateCallICDebugBreak(MacroAssembler* masm);
285
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000286 private:
287 static bool CompileDebuggerScript(int index);
288 static void ClearOneShot();
289 static void ActivateStepIn(StackFrame* frame);
290 static void ClearStepIn();
291 static void ClearStepNext();
ager@chromium.org32912102009-01-16 10:38:43 +0000292 // Returns whether the compile succeeded.
kasper.lundbd3ec4e2008-07-09 11:06:54 +0000293 static bool EnsureCompiled(Handle<SharedFunctionInfo> shared);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000294 static void RemoveDebugInfo(Handle<DebugInfo> debug_info);
295 static void SetAfterBreakTarget(JavaScriptFrame* frame);
296 static Handle<Object> CheckBreakPoints(Handle<Object> break_point);
297 static bool CheckBreakPoint(Handle<Object> break_point_object);
298
299 // Global handle to debug context where all the debugger JavaScript code is
300 // loaded.
301 static Handle<Context> debug_context_;
302
303 // Boolean state indicating whether any break points are set.
304 static bool has_break_points_;
305 static DebugInfoListNode* debug_info_list_;
306
kasper.lundbd3ec4e2008-07-09 11:06:54 +0000307 static bool disable_break_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000308 static bool break_on_exception_;
309 static bool break_on_uncaught_exception_;
310
311 // Per-thread:
312 class ThreadLocal {
313 public:
314 // Step action for last step performed.
315 StepAction last_step_action_;
316
317 // Source statement position from last step next action.
318 int last_statement_position_;
319
320 // Number of steps left to perform before debug event.
321 int step_count_;
322
323 // Frame pointer from last step next action.
324 Address last_fp_;
325
326 // Frame pointer for frame from which step in was performed.
327 Address step_into_fp_;
328
329 // Storage location for jump when exiting debug break calls.
330 Address after_break_target_;
331 };
332
333 // Storage location for registers when handling debug break calls
334 static JSCallerSavedBuffer registers_;
335 static ThreadLocal thread_local_;
336 static void ThreadInit();
337
338 // Code object for debug break return entry code.
339 static Code* debug_break_return_entry_;
340
341 // Code to call for handling debug break on return.
342 static Code* debug_break_return_;
343
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000344 DISALLOW_COPY_AND_ASSIGN(Debug);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000345};
346
347
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000348class DebugMessageThread;
349
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000350class Debugger {
351 public:
352 static void DebugRequest(const uint16_t* json_request, int length);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000353
354 static Handle<Object> MakeJSObject(Vector<const char> constructor_name,
355 int argc, Object*** argv,
356 bool* caught_exception);
357 static Handle<Object> MakeExecutionState(bool* caught_exception);
358 static Handle<Object> MakeBreakEvent(Handle<Object> exec_state,
359 Handle<Object> break_points_hit,
360 bool* caught_exception);
361 static Handle<Object> MakeExceptionEvent(Handle<Object> exec_state,
362 Handle<Object> exception,
363 bool uncaught,
364 bool* caught_exception);
365 static Handle<Object> MakeNewFunctionEvent(Handle<Object> func,
366 bool* caught_exception);
367 static Handle<Object> MakeCompileEvent(Handle<Script> script,
iposva@chromium.org245aa852009-02-10 00:49:54 +0000368 bool before,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000369 bool* caught_exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000370 static void OnDebugBreak(Handle<Object> break_points_hit);
371 static void OnException(Handle<Object> exception, bool uncaught);
372 static void OnBeforeCompile(Handle<Script> script);
373 static void OnAfterCompile(Handle<Script> script,
374 Handle<JSFunction> fun);
375 static void OnNewFunction(Handle<JSFunction> fun);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000376 static void ProcessDebugEvent(v8::DebugEvent event,
377 Handle<Object> event_data);
iposva@chromium.org245aa852009-02-10 00:49:54 +0000378 static void SetEventListener(Handle<Object> callback, Handle<Object> data);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000379 static void SetMessageHandler(v8::DebugMessageHandler handler, void* data);
380 static void SendMessage(Vector<uint16_t> message);
381 static void ProcessCommand(Vector<const uint16_t> command);
382 static void UpdateActiveDebugger();
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000383 static Handle<Object> Call(Handle<JSFunction> fun,
384 Handle<Object> data,
385 bool* pending_exception);
386
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000387 inline static bool EventActive(v8::DebugEvent event) {
388 // Currently argument event is not used.
389 return !Debugger::compiling_natives_ && Debugger::debugger_active_;
390 }
391
392 static void set_debugger_active(bool debugger_active) {
393 Debugger::debugger_active_ = debugger_active;
394 }
395 static bool debugger_active() { return Debugger::debugger_active_; }
396 static void set_compiling_natives(bool compiling_natives) {
397 Debugger::compiling_natives_ = compiling_natives;
398 }
399 static bool compiling_natives() { return Debugger::compiling_natives_; }
mads.s.agercbaa0602008-08-14 13:41:48 +0000400 static void set_loading_debugger(bool v) { is_loading_debugger_ = v; }
401 static bool is_loading_debugger() { return Debugger::is_loading_debugger_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000402
403 private:
iposva@chromium.org245aa852009-02-10 00:49:54 +0000404 static Handle<Object> event_listener_; // Global handle to listener
405 static Handle<Object> event_listener_data_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000406 static bool debugger_active_; // Are there any active debugger?
407 static bool compiling_natives_; // Are we compiling natives?
mads.s.agercbaa0602008-08-14 13:41:48 +0000408 static bool is_loading_debugger_; // Are we loading the debugger?
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000409 static DebugMessageThread* message_thread_;
410 static v8::DebugMessageHandler debug_message_handler_;
411 static void* debug_message_handler_data_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000412};
413
414
kasper.lund7276f142008-07-30 08:49:36 +0000415// A Queue of Vector<uint16_t> objects. A thread-safe version is
416// LockingMessageQueue, based on this class.
417class MessageQueue BASE_EMBEDDED {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000418 public:
kasper.lund7276f142008-07-30 08:49:36 +0000419 explicit MessageQueue(int size);
420 ~MessageQueue();
421 bool IsEmpty() const { return start_ == end_; }
422 Vector<uint16_t> Get();
423 void Put(const Vector<uint16_t>& message);
424 void Clear() { start_ = end_ = 0; } // Queue is empty after Clear().
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000425 private:
kasper.lund7276f142008-07-30 08:49:36 +0000426 // Doubles the size of the message queue, and copies the messages.
427 void Expand();
428
429 Vector<uint16_t>* messages_;
430 int start_;
431 int end_;
432 int size_; // The size of the queue buffer. Queue can hold size-1 messages.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000433};
434
435
kasper.lund7276f142008-07-30 08:49:36 +0000436// LockingMessageQueue is a thread-safe circular buffer of Vector<uint16_t>
437// messages. The message data is not managed by LockingMessageQueue.
438// Pointers to the data are passed in and out. Implemented by adding a
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +0000439// Mutex to MessageQueue. Includes logging of all puts and gets.
kasper.lund7276f142008-07-30 08:49:36 +0000440class LockingMessageQueue BASE_EMBEDDED {
441 public:
442 explicit LockingMessageQueue(int size);
443 ~LockingMessageQueue();
444 bool IsEmpty() const;
445 Vector<uint16_t> Get();
446 void Put(const Vector<uint16_t>& message);
447 void Clear();
448 private:
kasper.lund7276f142008-07-30 08:49:36 +0000449 MessageQueue queue_;
450 Mutex* lock_;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000451 DISALLOW_COPY_AND_ASSIGN(LockingMessageQueue);
kasper.lund7276f142008-07-30 08:49:36 +0000452};
453
454
455/* This class is the data for a running thread that serializes
456 * event messages and command processing for the debugger.
457 * All uncommented methods are called only from this message thread.
458 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000459class DebugMessageThread: public Thread {
460 public:
kasper.lund7276f142008-07-30 08:49:36 +0000461 DebugMessageThread(); // Called from API thread.
462 virtual ~DebugMessageThread(); // Never called.
463 // Called by V8 thread. Reports events from V8 VM.
464 // Also handles command processing in stopped state of V8,
465 // when host_running_ is false.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000466 void DebugEvent(v8::DebugEvent,
467 Handle<Object> exec_state,
468 Handle<Object> event_data);
kasper.lund7276f142008-07-30 08:49:36 +0000469 // Puts event on the output queue. Called by V8.
470 // This is where V8 hands off
471 // processing of the event to the DebugMessageThread thread,
472 // which forwards it to the debug_message_handler set by the API.
473 void SendMessage(Vector<uint16_t> event_json);
474 // Formats an event into JSON, and calls SendMessage.
ager@chromium.org8bb60582008-12-11 12:02:20 +0000475 bool SetEventJSONFromEvent(Handle<Object> event_data);
kasper.lund7276f142008-07-30 08:49:36 +0000476 // Puts a command coming from the public API on the queue. Called
477 // by the API client thread. This is where the API client hands off
478 // processing of the command to the DebugMessageThread thread.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000479 void ProcessCommand(Vector<uint16_t> command);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000480 void OnDebuggerInactive();
481
kasper.lund7276f142008-07-30 08:49:36 +0000482 // Main function of DebugMessageThread thread.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000483 void Run();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000484
kasper.lund7276f142008-07-30 08:49:36 +0000485 bool host_running_; // Is the debugging host running or stopped?
486 Semaphore* command_received_; // Non-zero when command queue is non-empty.
487 Semaphore* message_received_; // Exactly equal to message queue length.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000488 private:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000489 bool TwoByteEqualsAscii(Vector<uint16_t> two_byte, const char* ascii);
490
kasper.lund7276f142008-07-30 08:49:36 +0000491 static const int kQueueInitialSize = 4;
492 LockingMessageQueue command_queue_;
493 LockingMessageQueue message_queue_;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000494 DISALLOW_COPY_AND_ASSIGN(DebugMessageThread);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000495};
496
497
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000498// This class is used for entering the debugger. Create an instance in the stack
499// to enter the debugger. This will set the current break state, make sure the
500// debugger is loaded and switch to the debugger context. If the debugger for
501// some reason could not be entered FailedToEnter will return true.
502class EnterDebugger BASE_EMBEDDED {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000503 public:
ager@chromium.org8bb60582008-12-11 12:02:20 +0000504 EnterDebugger() : has_js_frames_(!it_.done()) {
505 // Store the previous break id and frame id.
506 break_id_ = Top::break_id();
507 break_frame_id_ = Top::break_frame_id();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000508
ager@chromium.org8bb60582008-12-11 12:02:20 +0000509 // Create the new break info. If there is no JavaScript frames there is no
510 // break frame id.
511 if (has_js_frames_) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000512 Top::new_break(it_.frame()->id());
ager@chromium.org8bb60582008-12-11 12:02:20 +0000513 } else {
514 Top::new_break(StackFrame::NO_ID);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000515 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000516
517 // Make sure that debugger is loaded and enter the debugger context.
518 load_failed_ = !Debug::Load();
519 if (!load_failed_) {
520 // NOTE the member variable save which saves the previous context before
521 // this change.
522 Top::set_context(*Debug::debug_context());
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000523 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000524 }
525
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000526 ~EnterDebugger() {
ager@chromium.org8bb60582008-12-11 12:02:20 +0000527 // Restore to the previous break state.
528 Top::set_break(break_frame_id_, break_id_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000529 }
530
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000531 // Check whether the debugger could be entered.
532 inline bool FailedToEnter() { return load_failed_; }
533
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000534 // Check whether there are any JavaScript frames on the stack.
ager@chromium.org8bb60582008-12-11 12:02:20 +0000535 inline bool HasJavaScriptFrames() { return has_js_frames_; }
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000536
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000537 private:
538 JavaScriptFrameIterator it_;
ager@chromium.org8bb60582008-12-11 12:02:20 +0000539 const bool has_js_frames_; // Were there any JavaScript frames?
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000540 StackFrame::Id break_frame_id_; // Previous break frame id.
541 int break_id_; // Previous break id.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000542 bool load_failed_; // Did the debugger fail to load?
543 SaveContext save_; // Saves previous context.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000544};
545
546
kasper.lundbd3ec4e2008-07-09 11:06:54 +0000547// Stack allocated class for disabling break.
548class DisableBreak BASE_EMBEDDED {
549 public:
550 // Enter the debugger by storing the previous top context and setting the
551 // current top context to the debugger context.
552 explicit DisableBreak(bool disable_break) {
553 prev_disable_break_ = Debug::disable_break();
554 Debug::set_disable_break(disable_break);
555 }
556 ~DisableBreak() {
557 Debug::set_disable_break(prev_disable_break_);
558 }
559
560 private:
561 // The previous state of the disable break used to restore the value when this
562 // object is destructed.
563 bool prev_disable_break_;
564};
565
566
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000567// Debug_Address encapsulates the Address pointers used in generating debug
568// code.
569class Debug_Address {
570 public:
571 Debug_Address(Debug::AddressId id, int reg = 0)
572 : id_(id), reg_(reg) {
573 ASSERT(reg == 0 || id == Debug::k_register_address);
574 }
575
576 static Debug_Address AfterBreakTarget() {
577 return Debug_Address(Debug::k_after_break_target_address);
578 }
579
580 static Debug_Address DebugBreakReturn() {
581 return Debug_Address(Debug::k_debug_break_return_address);
582 }
583
584 static Debug_Address Register(int reg) {
585 return Debug_Address(Debug::k_register_address, reg);
586 }
587
588 Address address() const {
589 switch (id_) {
590 case Debug::k_after_break_target_address:
591 return reinterpret_cast<Address>(Debug::after_break_target_address());
592 case Debug::k_debug_break_return_address:
593 return reinterpret_cast<Address>(Debug::debug_break_return_address());
594 case Debug::k_register_address:
595 return reinterpret_cast<Address>(Debug::register_address(reg_));
596 default:
597 UNREACHABLE();
598 return NULL;
599 }
600 }
601 private:
602 Debug::AddressId id_;
603 int reg_;
604};
605
606
607} } // namespace v8::internal
608
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000609#endif // V8_V8_DEBUG_H_