blob: 6520f0cce7ecfc4ac92af26f55ea60a86c8c7fac [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();
126
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000127 DISALLOW_COPY_AND_ASSIGN(BreakLocationIterator);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000128};
129
130
131// Linked list holding debug info objects. The debug info objects are kept as
132// weak handles to avoid a debug info object to keep a function alive.
133class DebugInfoListNode {
134 public:
135 explicit DebugInfoListNode(DebugInfo* debug_info);
136 virtual ~DebugInfoListNode();
137
138 DebugInfoListNode* next() { return next_; }
139 void set_next(DebugInfoListNode* next) { next_ = next; }
140 Handle<DebugInfo> debug_info() { return debug_info_; }
141
142 private:
143 // Global (weak) handle to the debug info object.
144 Handle<DebugInfo> debug_info_;
145
146 // Next pointer for linked list.
147 DebugInfoListNode* next_;
148};
149
150
151// This class contains the debugger support. The main purpose is to handle
152// setting break points in the code.
153//
154// This class controls the debug info for all functions which currently have
155// active breakpoints in them. This debug info is held in the heap root object
156// debug_info which is a FixedArray. Each entry in this list is of class
157// DebugInfo.
158class Debug {
159 public:
160 static void Setup(bool create_heap_objects);
161 static bool Load();
162 static void Unload();
163 static bool IsLoaded() { return !debug_context_.is_null(); }
164 static bool InDebugger() { return Top::is_break(); }
165 static void Iterate(ObjectVisitor* v);
166
167 static Object* Break(Arguments args);
168 static void SetBreakPoint(Handle<SharedFunctionInfo> shared,
169 int source_position,
170 Handle<Object> break_point_object);
171 static void ClearBreakPoint(Handle<Object> break_point_object);
172 static void FloodWithOneShot(Handle<SharedFunctionInfo> shared);
173 static void FloodHandlerWithOneShot();
174 static void ChangeBreakOnException(ExceptionBreakType type, bool enable);
175 static void PrepareStep(StepAction step_action, int step_count);
176 static void ClearStepping();
177 static bool StepNextContinue(BreakLocationIterator* break_location_iterator,
178 JavaScriptFrame* frame);
179 static Handle<DebugInfo> GetDebugInfo(Handle<SharedFunctionInfo> shared);
180 static bool HasDebugInfo(Handle<SharedFunctionInfo> shared);
kasper.lundbd3ec4e2008-07-09 11:06:54 +0000181
ager@chromium.org32912102009-01-16 10:38:43 +0000182 // Returns whether the operation succeeded.
kasper.lundbd3ec4e2008-07-09 11:06:54 +0000183 static bool EnsureDebugInfo(Handle<SharedFunctionInfo> shared);
184
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000185 static bool IsDebugBreak(Address addr);
186
187 // Check whether a code stub with the specified major key is a possible break
188 // point location.
189 static bool IsSourceBreakStub(Code* code);
190 static bool IsBreakStub(Code* code);
191
192 // Find the builtin to use for invoking the debug break
193 static Handle<Code> FindDebugBreak(RelocInfo* rinfo);
194
195 static Handle<Object> GetSourceBreakLocations(
196 Handle<SharedFunctionInfo> shared);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000197
198 // Getter for the debug_context.
199 inline static Handle<Context> debug_context() { return debug_context_; }
200
201 // Check whether a global object is the debug global object.
202 static bool IsDebugGlobal(GlobalObject* global);
203
204 // Fast check to see if any break points are active.
205 inline static bool has_break_points() { return has_break_points_; }
206
207 static bool StepInActive() { return thread_local_.step_into_fp_ != 0; }
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000208 static void HandleStepIn(Handle<JSFunction> function,
209 Address fp,
210 bool is_constructor);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000211 static Address step_in_fp() { return thread_local_.step_into_fp_; }
212 static Address* step_in_fp_addr() { return &thread_local_.step_into_fp_; }
213
kasper.lundbd3ec4e2008-07-09 11:06:54 +0000214 // Getter and setter for the disable break state.
215 static bool disable_break() { return disable_break_; }
216 static void set_disable_break(bool disable_break) {
217 disable_break_ = disable_break;
218 }
219
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000220 // Getters for the current exception break state.
221 static bool break_on_exception() { return break_on_exception_; }
222 static bool break_on_uncaught_exception() {
223 return break_on_uncaught_exception_;
224 }
225
226 enum AddressId {
227 k_after_break_target_address,
228 k_debug_break_return_address,
229 k_register_address
230 };
231
232 // Support for setting the address to jump to when returning from break point.
233 static Address* after_break_target_address() {
234 return reinterpret_cast<Address*>(&thread_local_.after_break_target_);
235 }
236
237 // Support for saving/restoring registers when handling debug break calls.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000238 static Object** register_address(int r) {
239 return &registers_[r];
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000240 }
241
kasper.lund7276f142008-07-30 08:49:36 +0000242 // Address of the debug break return entry code.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000243 static Code* debug_break_return_entry() { return debug_break_return_entry_; }
244
245 // Support for getting the address of the debug break on return code.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000246 static Code** debug_break_return_address() {
247 return &debug_break_return_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000248 }
249
250 static const int kEstimatedNofDebugInfoEntries = 16;
251 static const int kEstimatedNofBreakPointsInFunction = 16;
252
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000253 static void HandleWeakDebugInfo(v8::Persistent<v8::Value> obj, void* data);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000254
255 friend class Debugger;
256 friend Handle<FixedArray> GetDebuggedFunctions(); // Found in test-debug.cc
257
258 // Threading support.
259 static char* ArchiveDebug(char* to);
260 static char* RestoreDebug(char* from);
261 static int ArchiveSpacePerThread();
262
ager@chromium.org32912102009-01-16 10:38:43 +0000263 // Mirror cache handling.
264 static void ClearMirrorCache();
265
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000266 // Code generation assumptions.
267 static const int kIa32CallInstructionLength = 5;
268 static const int kIa32JSReturnSequenceLength = 6;
269
ager@chromium.org8bb60582008-12-11 12:02:20 +0000270 // Code generator routines.
271 static void GenerateLoadICDebugBreak(MacroAssembler* masm);
272 static void GenerateStoreICDebugBreak(MacroAssembler* masm);
273 static void GenerateKeyedLoadICDebugBreak(MacroAssembler* masm);
274 static void GenerateKeyedStoreICDebugBreak(MacroAssembler* masm);
275 static void GenerateConstructCallDebugBreak(MacroAssembler* masm);
276 static void GenerateReturnDebugBreak(MacroAssembler* masm);
277 static void GenerateReturnDebugBreakEntry(MacroAssembler* masm);
278 static void GenerateStubNoRegistersDebugBreak(MacroAssembler* masm);
279
280 // Called from stub-cache.cc.
281 static void GenerateCallICDebugBreak(MacroAssembler* masm);
282
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000283 private:
284 static bool CompileDebuggerScript(int index);
285 static void ClearOneShot();
286 static void ActivateStepIn(StackFrame* frame);
287 static void ClearStepIn();
288 static void ClearStepNext();
ager@chromium.org32912102009-01-16 10:38:43 +0000289 // Returns whether the compile succeeded.
kasper.lundbd3ec4e2008-07-09 11:06:54 +0000290 static bool EnsureCompiled(Handle<SharedFunctionInfo> shared);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000291 static void RemoveDebugInfo(Handle<DebugInfo> debug_info);
292 static void SetAfterBreakTarget(JavaScriptFrame* frame);
293 static Handle<Object> CheckBreakPoints(Handle<Object> break_point);
294 static bool CheckBreakPoint(Handle<Object> break_point_object);
295
296 // Global handle to debug context where all the debugger JavaScript code is
297 // loaded.
298 static Handle<Context> debug_context_;
299
300 // Boolean state indicating whether any break points are set.
301 static bool has_break_points_;
302 static DebugInfoListNode* debug_info_list_;
303
kasper.lundbd3ec4e2008-07-09 11:06:54 +0000304 static bool disable_break_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000305 static bool break_on_exception_;
306 static bool break_on_uncaught_exception_;
307
308 // Per-thread:
309 class ThreadLocal {
310 public:
311 // Step action for last step performed.
312 StepAction last_step_action_;
313
314 // Source statement position from last step next action.
315 int last_statement_position_;
316
317 // Number of steps left to perform before debug event.
318 int step_count_;
319
320 // Frame pointer from last step next action.
321 Address last_fp_;
322
323 // Frame pointer for frame from which step in was performed.
324 Address step_into_fp_;
325
326 // Storage location for jump when exiting debug break calls.
327 Address after_break_target_;
328 };
329
330 // Storage location for registers when handling debug break calls
331 static JSCallerSavedBuffer registers_;
332 static ThreadLocal thread_local_;
333 static void ThreadInit();
334
335 // Code object for debug break return entry code.
336 static Code* debug_break_return_entry_;
337
338 // Code to call for handling debug break on return.
339 static Code* debug_break_return_;
340
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000341 DISALLOW_COPY_AND_ASSIGN(Debug);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000342};
343
344
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000345class DebugMessageThread;
346
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000347class Debugger {
348 public:
349 static void DebugRequest(const uint16_t* json_request, int length);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000350
351 static Handle<Object> MakeJSObject(Vector<const char> constructor_name,
352 int argc, Object*** argv,
353 bool* caught_exception);
354 static Handle<Object> MakeExecutionState(bool* caught_exception);
355 static Handle<Object> MakeBreakEvent(Handle<Object> exec_state,
356 Handle<Object> break_points_hit,
357 bool* caught_exception);
358 static Handle<Object> MakeExceptionEvent(Handle<Object> exec_state,
359 Handle<Object> exception,
360 bool uncaught,
361 bool* caught_exception);
362 static Handle<Object> MakeNewFunctionEvent(Handle<Object> func,
363 bool* caught_exception);
364 static Handle<Object> MakeCompileEvent(Handle<Script> script,
365 Handle<Object> script_function,
366 bool* caught_exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000367 static void OnDebugBreak(Handle<Object> break_points_hit);
368 static void OnException(Handle<Object> exception, bool uncaught);
369 static void OnBeforeCompile(Handle<Script> script);
370 static void OnAfterCompile(Handle<Script> script,
371 Handle<JSFunction> fun);
372 static void OnNewFunction(Handle<JSFunction> fun);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000373 static void ProcessDebugEvent(v8::DebugEvent event,
374 Handle<Object> event_data);
375 static void SetMessageHandler(v8::DebugMessageHandler handler, void* data);
376 static void SendMessage(Vector<uint16_t> message);
377 static void ProcessCommand(Vector<const uint16_t> command);
378 static void UpdateActiveDebugger();
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000379 static Handle<Object> Call(Handle<JSFunction> fun,
380 Handle<Object> data,
381 bool* pending_exception);
382
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000383 inline static bool EventActive(v8::DebugEvent event) {
384 // Currently argument event is not used.
385 return !Debugger::compiling_natives_ && Debugger::debugger_active_;
386 }
387
388 static void set_debugger_active(bool debugger_active) {
389 Debugger::debugger_active_ = debugger_active;
390 }
391 static bool debugger_active() { return Debugger::debugger_active_; }
392 static void set_compiling_natives(bool compiling_natives) {
393 Debugger::compiling_natives_ = compiling_natives;
394 }
395 static bool compiling_natives() { return Debugger::compiling_natives_; }
mads.s.agercbaa0602008-08-14 13:41:48 +0000396 static void set_loading_debugger(bool v) { is_loading_debugger_ = v; }
397 static bool is_loading_debugger() { return Debugger::is_loading_debugger_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000398
399 private:
400 static bool debugger_active_; // Are there any active debugger?
401 static bool compiling_natives_; // Are we compiling natives?
mads.s.agercbaa0602008-08-14 13:41:48 +0000402 static bool is_loading_debugger_; // Are we loading the debugger?
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000403 static DebugMessageThread* message_thread_;
404 static v8::DebugMessageHandler debug_message_handler_;
405 static void* debug_message_handler_data_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000406};
407
408
kasper.lund7276f142008-07-30 08:49:36 +0000409// A Queue of Vector<uint16_t> objects. A thread-safe version is
410// LockingMessageQueue, based on this class.
411class MessageQueue BASE_EMBEDDED {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000412 public:
kasper.lund7276f142008-07-30 08:49:36 +0000413 explicit MessageQueue(int size);
414 ~MessageQueue();
415 bool IsEmpty() const { return start_ == end_; }
416 Vector<uint16_t> Get();
417 void Put(const Vector<uint16_t>& message);
418 void Clear() { start_ = end_ = 0; } // Queue is empty after Clear().
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000419 private:
kasper.lund7276f142008-07-30 08:49:36 +0000420 // Doubles the size of the message queue, and copies the messages.
421 void Expand();
422
423 Vector<uint16_t>* messages_;
424 int start_;
425 int end_;
426 int size_; // The size of the queue buffer. Queue can hold size-1 messages.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000427};
428
429
kasper.lund7276f142008-07-30 08:49:36 +0000430// LockingMessageQueue is a thread-safe circular buffer of Vector<uint16_t>
431// messages. The message data is not managed by LockingMessageQueue.
432// Pointers to the data are passed in and out. Implemented by adding a
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +0000433// Mutex to MessageQueue. Includes logging of all puts and gets.
kasper.lund7276f142008-07-30 08:49:36 +0000434class LockingMessageQueue BASE_EMBEDDED {
435 public:
436 explicit LockingMessageQueue(int size);
437 ~LockingMessageQueue();
438 bool IsEmpty() const;
439 Vector<uint16_t> Get();
440 void Put(const Vector<uint16_t>& message);
441 void Clear();
442 private:
kasper.lund7276f142008-07-30 08:49:36 +0000443 MessageQueue queue_;
444 Mutex* lock_;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000445 DISALLOW_COPY_AND_ASSIGN(LockingMessageQueue);
kasper.lund7276f142008-07-30 08:49:36 +0000446};
447
448
449/* This class is the data for a running thread that serializes
450 * event messages and command processing for the debugger.
451 * All uncommented methods are called only from this message thread.
452 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000453class DebugMessageThread: public Thread {
454 public:
kasper.lund7276f142008-07-30 08:49:36 +0000455 DebugMessageThread(); // Called from API thread.
456 virtual ~DebugMessageThread(); // Never called.
457 // Called by V8 thread. Reports events from V8 VM.
458 // Also handles command processing in stopped state of V8,
459 // when host_running_ is false.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000460 void DebugEvent(v8::DebugEvent,
461 Handle<Object> exec_state,
462 Handle<Object> event_data);
kasper.lund7276f142008-07-30 08:49:36 +0000463 // Puts event on the output queue. Called by V8.
464 // This is where V8 hands off
465 // processing of the event to the DebugMessageThread thread,
466 // which forwards it to the debug_message_handler set by the API.
467 void SendMessage(Vector<uint16_t> event_json);
468 // Formats an event into JSON, and calls SendMessage.
ager@chromium.org8bb60582008-12-11 12:02:20 +0000469 bool SetEventJSONFromEvent(Handle<Object> event_data);
kasper.lund7276f142008-07-30 08:49:36 +0000470 // Puts a command coming from the public API on the queue. Called
471 // by the API client thread. This is where the API client hands off
472 // processing of the command to the DebugMessageThread thread.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000473 void ProcessCommand(Vector<uint16_t> command);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000474 void OnDebuggerInactive();
475
kasper.lund7276f142008-07-30 08:49:36 +0000476 // Main function of DebugMessageThread thread.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000477 void Run();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000478
kasper.lund7276f142008-07-30 08:49:36 +0000479 bool host_running_; // Is the debugging host running or stopped?
480 Semaphore* command_received_; // Non-zero when command queue is non-empty.
481 Semaphore* message_received_; // Exactly equal to message queue length.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000482 private:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000483 bool TwoByteEqualsAscii(Vector<uint16_t> two_byte, const char* ascii);
484
kasper.lund7276f142008-07-30 08:49:36 +0000485 static const int kQueueInitialSize = 4;
486 LockingMessageQueue command_queue_;
487 LockingMessageQueue message_queue_;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000488 DISALLOW_COPY_AND_ASSIGN(DebugMessageThread);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000489};
490
491
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000492// This class is used for entering the debugger. Create an instance in the stack
493// to enter the debugger. This will set the current break state, make sure the
494// debugger is loaded and switch to the debugger context. If the debugger for
495// some reason could not be entered FailedToEnter will return true.
496class EnterDebugger BASE_EMBEDDED {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000497 public:
ager@chromium.org8bb60582008-12-11 12:02:20 +0000498 EnterDebugger() : has_js_frames_(!it_.done()) {
499 // Store the previous break id and frame id.
500 break_id_ = Top::break_id();
501 break_frame_id_ = Top::break_frame_id();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000502
ager@chromium.org8bb60582008-12-11 12:02:20 +0000503 // Create the new break info. If there is no JavaScript frames there is no
504 // break frame id.
505 if (has_js_frames_) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000506 Top::new_break(it_.frame()->id());
ager@chromium.org8bb60582008-12-11 12:02:20 +0000507 } else {
508 Top::new_break(StackFrame::NO_ID);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000509 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000510
511 // Make sure that debugger is loaded and enter the debugger context.
512 load_failed_ = !Debug::Load();
513 if (!load_failed_) {
514 // NOTE the member variable save which saves the previous context before
515 // this change.
516 Top::set_context(*Debug::debug_context());
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000517 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000518 }
519
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000520 ~EnterDebugger() {
ager@chromium.org8bb60582008-12-11 12:02:20 +0000521 // Restore to the previous break state.
522 Top::set_break(break_frame_id_, break_id_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000523 }
524
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000525 // Check whether the debugger could be entered.
526 inline bool FailedToEnter() { return load_failed_; }
527
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000528 // Check whether there are any JavaScript frames on the stack.
ager@chromium.org8bb60582008-12-11 12:02:20 +0000529 inline bool HasJavaScriptFrames() { return has_js_frames_; }
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000530
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000531 private:
532 JavaScriptFrameIterator it_;
ager@chromium.org8bb60582008-12-11 12:02:20 +0000533 const bool has_js_frames_; // Were there any JavaScript frames?
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000534 StackFrame::Id break_frame_id_; // Previous break frame id.
535 int break_id_; // Previous break id.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000536 bool load_failed_; // Did the debugger fail to load?
537 SaveContext save_; // Saves previous context.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000538};
539
540
kasper.lundbd3ec4e2008-07-09 11:06:54 +0000541// Stack allocated class for disabling break.
542class DisableBreak BASE_EMBEDDED {
543 public:
544 // Enter the debugger by storing the previous top context and setting the
545 // current top context to the debugger context.
546 explicit DisableBreak(bool disable_break) {
547 prev_disable_break_ = Debug::disable_break();
548 Debug::set_disable_break(disable_break);
549 }
550 ~DisableBreak() {
551 Debug::set_disable_break(prev_disable_break_);
552 }
553
554 private:
555 // The previous state of the disable break used to restore the value when this
556 // object is destructed.
557 bool prev_disable_break_;
558};
559
560
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000561// Debug_Address encapsulates the Address pointers used in generating debug
562// code.
563class Debug_Address {
564 public:
565 Debug_Address(Debug::AddressId id, int reg = 0)
566 : id_(id), reg_(reg) {
567 ASSERT(reg == 0 || id == Debug::k_register_address);
568 }
569
570 static Debug_Address AfterBreakTarget() {
571 return Debug_Address(Debug::k_after_break_target_address);
572 }
573
574 static Debug_Address DebugBreakReturn() {
575 return Debug_Address(Debug::k_debug_break_return_address);
576 }
577
578 static Debug_Address Register(int reg) {
579 return Debug_Address(Debug::k_register_address, reg);
580 }
581
582 Address address() const {
583 switch (id_) {
584 case Debug::k_after_break_target_address:
585 return reinterpret_cast<Address>(Debug::after_break_target_address());
586 case Debug::k_debug_break_return_address:
587 return reinterpret_cast<Address>(Debug::debug_break_return_address());
588 case Debug::k_register_address:
589 return reinterpret_cast<Address>(Debug::register_address(reg_));
590 default:
591 UNREACHABLE();
592 return NULL;
593 }
594 }
595 private:
596 Debug::AddressId id_;
597 int reg_;
598};
599
600
601} } // namespace v8::internal
602
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000603#endif // V8_V8_DEBUG_H_