blob: fdb80bd20b6a5bc784321105d260855fae8206bf [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
182 // Returns whether the operation succedded.
183 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; }
208 static Address step_in_fp() { return thread_local_.step_into_fp_; }
209 static Address* step_in_fp_addr() { return &thread_local_.step_into_fp_; }
210
kasper.lundbd3ec4e2008-07-09 11:06:54 +0000211 // Getter and setter for the disable break state.
212 static bool disable_break() { return disable_break_; }
213 static void set_disable_break(bool disable_break) {
214 disable_break_ = disable_break;
215 }
216
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000217 // Getters for the current exception break state.
218 static bool break_on_exception() { return break_on_exception_; }
219 static bool break_on_uncaught_exception() {
220 return break_on_uncaught_exception_;
221 }
222
223 enum AddressId {
224 k_after_break_target_address,
225 k_debug_break_return_address,
226 k_register_address
227 };
228
229 // Support for setting the address to jump to when returning from break point.
230 static Address* after_break_target_address() {
231 return reinterpret_cast<Address*>(&thread_local_.after_break_target_);
232 }
233
234 // Support for saving/restoring registers when handling debug break calls.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000235 static Object** register_address(int r) {
236 return &registers_[r];
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000237 }
238
kasper.lund7276f142008-07-30 08:49:36 +0000239 // Address of the debug break return entry code.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000240 static Code* debug_break_return_entry() { return debug_break_return_entry_; }
241
242 // Support for getting the address of the debug break on return code.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000243 static Code** debug_break_return_address() {
244 return &debug_break_return_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000245 }
246
247 static const int kEstimatedNofDebugInfoEntries = 16;
248 static const int kEstimatedNofBreakPointsInFunction = 16;
249
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000250 static void HandleWeakDebugInfo(v8::Persistent<v8::Value> obj, void* data);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000251
252 friend class Debugger;
253 friend Handle<FixedArray> GetDebuggedFunctions(); // Found in test-debug.cc
254
255 // Threading support.
256 static char* ArchiveDebug(char* to);
257 static char* RestoreDebug(char* from);
258 static int ArchiveSpacePerThread();
259
260 // Code generation assumptions.
261 static const int kIa32CallInstructionLength = 5;
262 static const int kIa32JSReturnSequenceLength = 6;
263
ager@chromium.org8bb60582008-12-11 12:02:20 +0000264 // Code generator routines.
265 static void GenerateLoadICDebugBreak(MacroAssembler* masm);
266 static void GenerateStoreICDebugBreak(MacroAssembler* masm);
267 static void GenerateKeyedLoadICDebugBreak(MacroAssembler* masm);
268 static void GenerateKeyedStoreICDebugBreak(MacroAssembler* masm);
269 static void GenerateConstructCallDebugBreak(MacroAssembler* masm);
270 static void GenerateReturnDebugBreak(MacroAssembler* masm);
271 static void GenerateReturnDebugBreakEntry(MacroAssembler* masm);
272 static void GenerateStubNoRegistersDebugBreak(MacroAssembler* masm);
273
274 // Called from stub-cache.cc.
275 static void GenerateCallICDebugBreak(MacroAssembler* masm);
276
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000277 private:
278 static bool CompileDebuggerScript(int index);
279 static void ClearOneShot();
280 static void ActivateStepIn(StackFrame* frame);
281 static void ClearStepIn();
282 static void ClearStepNext();
kasper.lundbd3ec4e2008-07-09 11:06:54 +0000283 // Returns whether the compile succedded.
284 static bool EnsureCompiled(Handle<SharedFunctionInfo> shared);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000285 static void RemoveDebugInfo(Handle<DebugInfo> debug_info);
286 static void SetAfterBreakTarget(JavaScriptFrame* frame);
287 static Handle<Object> CheckBreakPoints(Handle<Object> break_point);
288 static bool CheckBreakPoint(Handle<Object> break_point_object);
289
290 // Global handle to debug context where all the debugger JavaScript code is
291 // loaded.
292 static Handle<Context> debug_context_;
293
294 // Boolean state indicating whether any break points are set.
295 static bool has_break_points_;
296 static DebugInfoListNode* debug_info_list_;
297
kasper.lundbd3ec4e2008-07-09 11:06:54 +0000298 static bool disable_break_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000299 static bool break_on_exception_;
300 static bool break_on_uncaught_exception_;
301
302 // Per-thread:
303 class ThreadLocal {
304 public:
305 // Step action for last step performed.
306 StepAction last_step_action_;
307
308 // Source statement position from last step next action.
309 int last_statement_position_;
310
311 // Number of steps left to perform before debug event.
312 int step_count_;
313
314 // Frame pointer from last step next action.
315 Address last_fp_;
316
317 // Frame pointer for frame from which step in was performed.
318 Address step_into_fp_;
319
320 // Storage location for jump when exiting debug break calls.
321 Address after_break_target_;
322 };
323
324 // Storage location for registers when handling debug break calls
325 static JSCallerSavedBuffer registers_;
326 static ThreadLocal thread_local_;
327 static void ThreadInit();
328
329 // Code object for debug break return entry code.
330 static Code* debug_break_return_entry_;
331
332 // Code to call for handling debug break on return.
333 static Code* debug_break_return_;
334
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000335 DISALLOW_COPY_AND_ASSIGN(Debug);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000336};
337
338
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000339class DebugMessageThread;
340
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000341class Debugger {
342 public:
343 static void DebugRequest(const uint16_t* json_request, int length);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000344
345 static Handle<Object> MakeJSObject(Vector<const char> constructor_name,
346 int argc, Object*** argv,
347 bool* caught_exception);
348 static Handle<Object> MakeExecutionState(bool* caught_exception);
349 static Handle<Object> MakeBreakEvent(Handle<Object> exec_state,
350 Handle<Object> break_points_hit,
351 bool* caught_exception);
352 static Handle<Object> MakeExceptionEvent(Handle<Object> exec_state,
353 Handle<Object> exception,
354 bool uncaught,
355 bool* caught_exception);
356 static Handle<Object> MakeNewFunctionEvent(Handle<Object> func,
357 bool* caught_exception);
358 static Handle<Object> MakeCompileEvent(Handle<Script> script,
359 Handle<Object> script_function,
360 bool* caught_exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000361 static void OnDebugBreak(Handle<Object> break_points_hit);
362 static void OnException(Handle<Object> exception, bool uncaught);
363 static void OnBeforeCompile(Handle<Script> script);
364 static void OnAfterCompile(Handle<Script> script,
365 Handle<JSFunction> fun);
366 static void OnNewFunction(Handle<JSFunction> fun);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000367 static void ProcessDebugEvent(v8::DebugEvent event,
368 Handle<Object> event_data);
369 static void SetMessageHandler(v8::DebugMessageHandler handler, void* data);
370 static void SendMessage(Vector<uint16_t> message);
371 static void ProcessCommand(Vector<const uint16_t> command);
372 static void UpdateActiveDebugger();
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000373 static Handle<Object> Call(Handle<JSFunction> fun,
374 Handle<Object> data,
375 bool* pending_exception);
376
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000377 inline static bool EventActive(v8::DebugEvent event) {
378 // Currently argument event is not used.
379 return !Debugger::compiling_natives_ && Debugger::debugger_active_;
380 }
381
382 static void set_debugger_active(bool debugger_active) {
383 Debugger::debugger_active_ = debugger_active;
384 }
385 static bool debugger_active() { return Debugger::debugger_active_; }
386 static void set_compiling_natives(bool compiling_natives) {
387 Debugger::compiling_natives_ = compiling_natives;
388 }
389 static bool compiling_natives() { return Debugger::compiling_natives_; }
mads.s.agercbaa0602008-08-14 13:41:48 +0000390 static void set_loading_debugger(bool v) { is_loading_debugger_ = v; }
391 static bool is_loading_debugger() { return Debugger::is_loading_debugger_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000392
393 private:
394 static bool debugger_active_; // Are there any active debugger?
395 static bool compiling_natives_; // Are we compiling natives?
mads.s.agercbaa0602008-08-14 13:41:48 +0000396 static bool is_loading_debugger_; // Are we loading the debugger?
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000397 static DebugMessageThread* message_thread_;
398 static v8::DebugMessageHandler debug_message_handler_;
399 static void* debug_message_handler_data_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000400};
401
402
kasper.lund7276f142008-07-30 08:49:36 +0000403// A Queue of Vector<uint16_t> objects. A thread-safe version is
404// LockingMessageQueue, based on this class.
405class MessageQueue BASE_EMBEDDED {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000406 public:
kasper.lund7276f142008-07-30 08:49:36 +0000407 explicit MessageQueue(int size);
408 ~MessageQueue();
409 bool IsEmpty() const { return start_ == end_; }
410 Vector<uint16_t> Get();
411 void Put(const Vector<uint16_t>& message);
412 void Clear() { start_ = end_ = 0; } // Queue is empty after Clear().
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000413 private:
kasper.lund7276f142008-07-30 08:49:36 +0000414 // Doubles the size of the message queue, and copies the messages.
415 void Expand();
416
417 Vector<uint16_t>* messages_;
418 int start_;
419 int end_;
420 int size_; // The size of the queue buffer. Queue can hold size-1 messages.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000421};
422
423
kasper.lund7276f142008-07-30 08:49:36 +0000424// LockingMessageQueue is a thread-safe circular buffer of Vector<uint16_t>
425// messages. The message data is not managed by LockingMessageQueue.
426// Pointers to the data are passed in and out. Implemented by adding a
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +0000427// Mutex to MessageQueue. Includes logging of all puts and gets.
kasper.lund7276f142008-07-30 08:49:36 +0000428class LockingMessageQueue BASE_EMBEDDED {
429 public:
430 explicit LockingMessageQueue(int size);
431 ~LockingMessageQueue();
432 bool IsEmpty() const;
433 Vector<uint16_t> Get();
434 void Put(const Vector<uint16_t>& message);
435 void Clear();
436 private:
kasper.lund7276f142008-07-30 08:49:36 +0000437 MessageQueue queue_;
438 Mutex* lock_;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000439 DISALLOW_COPY_AND_ASSIGN(LockingMessageQueue);
kasper.lund7276f142008-07-30 08:49:36 +0000440};
441
442
443/* This class is the data for a running thread that serializes
444 * event messages and command processing for the debugger.
445 * All uncommented methods are called only from this message thread.
446 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000447class DebugMessageThread: public Thread {
448 public:
kasper.lund7276f142008-07-30 08:49:36 +0000449 DebugMessageThread(); // Called from API thread.
450 virtual ~DebugMessageThread(); // Never called.
451 // Called by V8 thread. Reports events from V8 VM.
452 // Also handles command processing in stopped state of V8,
453 // when host_running_ is false.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000454 void DebugEvent(v8::DebugEvent,
455 Handle<Object> exec_state,
456 Handle<Object> event_data);
kasper.lund7276f142008-07-30 08:49:36 +0000457 // Puts event on the output queue. Called by V8.
458 // This is where V8 hands off
459 // processing of the event to the DebugMessageThread thread,
460 // which forwards it to the debug_message_handler set by the API.
461 void SendMessage(Vector<uint16_t> event_json);
462 // Formats an event into JSON, and calls SendMessage.
ager@chromium.org8bb60582008-12-11 12:02:20 +0000463 bool SetEventJSONFromEvent(Handle<Object> event_data);
kasper.lund7276f142008-07-30 08:49:36 +0000464 // Puts a command coming from the public API on the queue. Called
465 // by the API client thread. This is where the API client hands off
466 // processing of the command to the DebugMessageThread thread.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000467 void ProcessCommand(Vector<uint16_t> command);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000468 void OnDebuggerInactive();
469
kasper.lund7276f142008-07-30 08:49:36 +0000470 // Main function of DebugMessageThread thread.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000471 void Run();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000472
kasper.lund7276f142008-07-30 08:49:36 +0000473 bool host_running_; // Is the debugging host running or stopped?
474 Semaphore* command_received_; // Non-zero when command queue is non-empty.
475 Semaphore* message_received_; // Exactly equal to message queue length.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000476 private:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000477 bool TwoByteEqualsAscii(Vector<uint16_t> two_byte, const char* ascii);
478
kasper.lund7276f142008-07-30 08:49:36 +0000479 static const int kQueueInitialSize = 4;
480 LockingMessageQueue command_queue_;
481 LockingMessageQueue message_queue_;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000482 DISALLOW_COPY_AND_ASSIGN(DebugMessageThread);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000483};
484
485
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000486// This class is used for entering the debugger. Create an instance in the stack
487// to enter the debugger. This will set the current break state, make sure the
488// debugger is loaded and switch to the debugger context. If the debugger for
489// some reason could not be entered FailedToEnter will return true.
490class EnterDebugger BASE_EMBEDDED {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000491 public:
ager@chromium.org8bb60582008-12-11 12:02:20 +0000492 EnterDebugger() : has_js_frames_(!it_.done()) {
493 // Store the previous break id and frame id.
494 break_id_ = Top::break_id();
495 break_frame_id_ = Top::break_frame_id();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000496
ager@chromium.org8bb60582008-12-11 12:02:20 +0000497 // Create the new break info. If there is no JavaScript frames there is no
498 // break frame id.
499 if (has_js_frames_) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000500 Top::new_break(it_.frame()->id());
ager@chromium.org8bb60582008-12-11 12:02:20 +0000501 } else {
502 Top::new_break(StackFrame::NO_ID);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000503 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000504
505 // Make sure that debugger is loaded and enter the debugger context.
506 load_failed_ = !Debug::Load();
507 if (!load_failed_) {
508 // NOTE the member variable save which saves the previous context before
509 // this change.
510 Top::set_context(*Debug::debug_context());
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000511 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000512 }
513
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000514 ~EnterDebugger() {
ager@chromium.org8bb60582008-12-11 12:02:20 +0000515 // Restore to the previous break state.
516 Top::set_break(break_frame_id_, break_id_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000517 }
518
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000519 // Check whether the debugger could be entered.
520 inline bool FailedToEnter() { return load_failed_; }
521
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000522 // Check whether there are any JavaScript frames on the stack.
ager@chromium.org8bb60582008-12-11 12:02:20 +0000523 inline bool HasJavaScriptFrames() { return has_js_frames_; }
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000524
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000525 private:
526 JavaScriptFrameIterator it_;
ager@chromium.org8bb60582008-12-11 12:02:20 +0000527 const bool has_js_frames_; // Were there any JavaScript frames?
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000528 StackFrame::Id break_frame_id_; // Previous break frame id.
529 int break_id_; // Previous break id.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000530 bool load_failed_; // Did the debugger fail to load?
531 SaveContext save_; // Saves previous context.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000532};
533
534
kasper.lundbd3ec4e2008-07-09 11:06:54 +0000535// Stack allocated class for disabling break.
536class DisableBreak BASE_EMBEDDED {
537 public:
538 // Enter the debugger by storing the previous top context and setting the
539 // current top context to the debugger context.
540 explicit DisableBreak(bool disable_break) {
541 prev_disable_break_ = Debug::disable_break();
542 Debug::set_disable_break(disable_break);
543 }
544 ~DisableBreak() {
545 Debug::set_disable_break(prev_disable_break_);
546 }
547
548 private:
549 // The previous state of the disable break used to restore the value when this
550 // object is destructed.
551 bool prev_disable_break_;
552};
553
554
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000555// Debug_Address encapsulates the Address pointers used in generating debug
556// code.
557class Debug_Address {
558 public:
559 Debug_Address(Debug::AddressId id, int reg = 0)
560 : id_(id), reg_(reg) {
561 ASSERT(reg == 0 || id == Debug::k_register_address);
562 }
563
564 static Debug_Address AfterBreakTarget() {
565 return Debug_Address(Debug::k_after_break_target_address);
566 }
567
568 static Debug_Address DebugBreakReturn() {
569 return Debug_Address(Debug::k_debug_break_return_address);
570 }
571
572 static Debug_Address Register(int reg) {
573 return Debug_Address(Debug::k_register_address, reg);
574 }
575
576 Address address() const {
577 switch (id_) {
578 case Debug::k_after_break_target_address:
579 return reinterpret_cast<Address>(Debug::after_break_target_address());
580 case Debug::k_debug_break_return_address:
581 return reinterpret_cast<Address>(Debug::debug_break_return_address());
582 case Debug::k_register_address:
583 return reinterpret_cast<Address>(Debug::register_address(reg_));
584 default:
585 UNREACHABLE();
586 return NULL;
587 }
588 }
589 private:
590 Debug::AddressId id_;
591 int reg_;
592};
593
594
595} } // namespace v8::internal
596
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000597#endif // V8_V8_DEBUG_H_