blob: 423bbc071d939670b2069c125e8db04c48955791 [file] [log] [blame]
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001// Copyright 2006-2008 Google Inc. All Rights Reserved.
2// 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
28#ifndef V8_DEBUG_H_
29#define V8_DEBUG_H_
30
31#include "../public/debug.h"
32#include "assembler.h"
33#include "code-stubs.h"
34#include "factory.h"
35#include "platform.h"
36#include "string-stream.h"
37
38
39namespace v8 { namespace internal {
40
41// Step actions. NOTE: These values are in macros.py as well.
42enum StepAction {
43 StepNone = -1, // Stepping not prepared.
44 StepOut = 0, // Step out of the current function.
45 StepNext = 1, // Step to the next statement in the current function.
46 StepIn = 2, // Step into new functions invoked or the next statement
47 // in the current function.
48 StepMin = 3, // Perform a minimum step in the current function.
49 StepInMin = 4 // Step into new functions invoked or perform a minimum step
50 // in the current function.
51};
52
53
54// Type of exception break. NOTE: These values are in macros.py as well.
55enum ExceptionBreakType {
56 BreakException = 0,
57 BreakUncaughtException = 1
58};
59
60
61// Type of exception break. NOTE: These values are in macros.py as well.
62enum BreakLocatorType {
63 ALL_BREAK_LOCATIONS = 0,
64 SOURCE_BREAK_LOCATIONS = 1
65};
66
67
68// Class for iterating through the break points in a function and changing
69// them.
70class BreakLocationIterator {
71 public:
72 explicit BreakLocationIterator(Handle<DebugInfo> debug_info,
73 BreakLocatorType type);
74 virtual ~BreakLocationIterator();
75
76 void Next();
77 void Next(int count);
78 void FindBreakLocationFromAddress(Address pc);
79 void FindBreakLocationFromPosition(int position);
80 void Reset();
81 bool Done() const;
82 void SetBreakPoint(Handle<Object> break_point_object);
83 void ClearBreakPoint(Handle<Object> break_point_object);
84 void SetOneShot();
85 void ClearOneShot();
86 void PrepareStepIn();
87 bool IsExit() const;
88 bool HasBreakPoint();
89 bool IsDebugBreak();
90 Object* BreakPointObjects();
91
92
93 inline int code_position() { return pc() - debug_info_->code()->entry(); }
94 inline int break_point() { return break_point_; }
95 inline int position() { return position_; }
96 inline int statement_position() { return statement_position_; }
97 inline Address pc() { return reloc_iterator_->rinfo()->pc(); }
98 inline Code* code() { return debug_info_->code(); }
99 inline RelocInfo* rinfo() { return reloc_iterator_->rinfo(); }
100 inline RelocMode rmode() const { return reloc_iterator_->rinfo()->rmode(); }
101 inline RelocInfo* original_rinfo() {
102 return reloc_iterator_original_->rinfo();
103 }
104 inline RelocMode original_rmode() const {
105 return reloc_iterator_original_->rinfo()->rmode();
106 }
107
108 protected:
109 bool RinfoDone() const;
110 void RinfoNext();
111
112 BreakLocatorType type_;
113 int break_point_;
114 int position_;
115 int statement_position_;
116 Handle<DebugInfo> debug_info_;
117 RelocIterator* reloc_iterator_;
118 RelocIterator* reloc_iterator_original_;
119
120 private:
121 void SetDebugBreak();
122 void ClearDebugBreak();
123
124 DISALLOW_EVIL_CONSTRUCTORS(BreakLocationIterator);
125};
126
127
128// Linked list holding debug info objects. The debug info objects are kept as
129// weak handles to avoid a debug info object to keep a function alive.
130class DebugInfoListNode {
131 public:
132 explicit DebugInfoListNode(DebugInfo* debug_info);
133 virtual ~DebugInfoListNode();
134
135 DebugInfoListNode* next() { return next_; }
136 void set_next(DebugInfoListNode* next) { next_ = next; }
137 Handle<DebugInfo> debug_info() { return debug_info_; }
138
139 private:
140 // Global (weak) handle to the debug info object.
141 Handle<DebugInfo> debug_info_;
142
143 // Next pointer for linked list.
144 DebugInfoListNode* next_;
145};
146
147
148// This class contains the debugger support. The main purpose is to handle
149// setting break points in the code.
150//
151// This class controls the debug info for all functions which currently have
152// active breakpoints in them. This debug info is held in the heap root object
153// debug_info which is a FixedArray. Each entry in this list is of class
154// DebugInfo.
155class Debug {
156 public:
157 static void Setup(bool create_heap_objects);
158 static bool Load();
159 static void Unload();
160 static bool IsLoaded() { return !debug_context_.is_null(); }
161 static bool InDebugger() { return Top::is_break(); }
162 static void Iterate(ObjectVisitor* v);
163
164 static Object* Break(Arguments args);
165 static void SetBreakPoint(Handle<SharedFunctionInfo> shared,
166 int source_position,
167 Handle<Object> break_point_object);
168 static void ClearBreakPoint(Handle<Object> break_point_object);
169 static void FloodWithOneShot(Handle<SharedFunctionInfo> shared);
170 static void FloodHandlerWithOneShot();
171 static void ChangeBreakOnException(ExceptionBreakType type, bool enable);
172 static void PrepareStep(StepAction step_action, int step_count);
173 static void ClearStepping();
174 static bool StepNextContinue(BreakLocationIterator* break_location_iterator,
175 JavaScriptFrame* frame);
176 static Handle<DebugInfo> GetDebugInfo(Handle<SharedFunctionInfo> shared);
177 static bool HasDebugInfo(Handle<SharedFunctionInfo> shared);
kasper.lundbd3ec4e2008-07-09 11:06:54 +0000178
179 // Returns whether the operation succedded.
180 static bool EnsureDebugInfo(Handle<SharedFunctionInfo> shared);
181
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000182 static bool IsDebugBreak(Address addr);
183
184 // Check whether a code stub with the specified major key is a possible break
185 // point location.
186 static bool IsSourceBreakStub(Code* code);
187 static bool IsBreakStub(Code* code);
188
189 // Find the builtin to use for invoking the debug break
190 static Handle<Code> FindDebugBreak(RelocInfo* rinfo);
191
192 static Handle<Object> GetSourceBreakLocations(
193 Handle<SharedFunctionInfo> shared);
194 static Code* GetCodeTarget(Address target);
195
196 // Getter for the debug_context.
197 inline static Handle<Context> debug_context() { return debug_context_; }
198
199 // Check whether a global object is the debug global object.
200 static bool IsDebugGlobal(GlobalObject* global);
201
202 // Fast check to see if any break points are active.
203 inline static bool has_break_points() { return has_break_points_; }
204
205 static bool StepInActive() { return thread_local_.step_into_fp_ != 0; }
206 static Address step_in_fp() { return thread_local_.step_into_fp_; }
207 static Address* step_in_fp_addr() { return &thread_local_.step_into_fp_; }
208
kasper.lundbd3ec4e2008-07-09 11:06:54 +0000209 // Getter and setter for the disable break state.
210 static bool disable_break() { return disable_break_; }
211 static void set_disable_break(bool disable_break) {
212 disable_break_ = disable_break;
213 }
214
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000215 // Getters for the current exception break state.
216 static bool break_on_exception() { return break_on_exception_; }
217 static bool break_on_uncaught_exception() {
218 return break_on_uncaught_exception_;
219 }
220
221 enum AddressId {
222 k_after_break_target_address,
223 k_debug_break_return_address,
224 k_register_address
225 };
226
227 // Support for setting the address to jump to when returning from break point.
228 static Address* after_break_target_address() {
229 return reinterpret_cast<Address*>(&thread_local_.after_break_target_);
230 }
231
232 // Support for saving/restoring registers when handling debug break calls.
233 static Address* register_address(int r) {
234 return reinterpret_cast<Address *>(&registers_[r]);
235 }
236
237 // Addres of the debug break return entry code.
238 static Code* debug_break_return_entry() { return debug_break_return_entry_; }
239
240 // Support for getting the address of the debug break on return code.
241 static Address* debug_break_return_address() {
242 return reinterpret_cast<Address*>(&debug_break_return_);
243 }
244
245 static const int kEstimatedNofDebugInfoEntries = 16;
246 static const int kEstimatedNofBreakPointsInFunction = 16;
247
248 static void HandleWeakDebugInfo(v8::Persistent<v8::Object> obj, void* data);
249
250 friend class Debugger;
251 friend Handle<FixedArray> GetDebuggedFunctions(); // Found in test-debug.cc
252
253 // Threading support.
254 static char* ArchiveDebug(char* to);
255 static char* RestoreDebug(char* from);
256 static int ArchiveSpacePerThread();
257
258 // Code generation assumptions.
259 static const int kIa32CallInstructionLength = 5;
260 static const int kIa32JSReturnSequenceLength = 6;
261
262 private:
263 static bool CompileDebuggerScript(int index);
264 static void ClearOneShot();
265 static void ActivateStepIn(StackFrame* frame);
266 static void ClearStepIn();
267 static void ClearStepNext();
kasper.lundbd3ec4e2008-07-09 11:06:54 +0000268 // Returns whether the compile succedded.
269 static bool EnsureCompiled(Handle<SharedFunctionInfo> shared);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000270 static void RemoveDebugInfo(Handle<DebugInfo> debug_info);
271 static void SetAfterBreakTarget(JavaScriptFrame* frame);
272 static Handle<Object> CheckBreakPoints(Handle<Object> break_point);
273 static bool CheckBreakPoint(Handle<Object> break_point_object);
274
275 // Global handle to debug context where all the debugger JavaScript code is
276 // loaded.
277 static Handle<Context> debug_context_;
278
279 // Boolean state indicating whether any break points are set.
280 static bool has_break_points_;
281 static DebugInfoListNode* debug_info_list_;
282
kasper.lundbd3ec4e2008-07-09 11:06:54 +0000283 static bool disable_break_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000284 static bool break_on_exception_;
285 static bool break_on_uncaught_exception_;
286
287 // Per-thread:
288 class ThreadLocal {
289 public:
290 // Step action for last step performed.
291 StepAction last_step_action_;
292
293 // Source statement position from last step next action.
294 int last_statement_position_;
295
296 // Number of steps left to perform before debug event.
297 int step_count_;
298
299 // Frame pointer from last step next action.
300 Address last_fp_;
301
302 // Frame pointer for frame from which step in was performed.
303 Address step_into_fp_;
304
305 // Storage location for jump when exiting debug break calls.
306 Address after_break_target_;
307 };
308
309 // Storage location for registers when handling debug break calls
310 static JSCallerSavedBuffer registers_;
311 static ThreadLocal thread_local_;
312 static void ThreadInit();
313
314 // Code object for debug break return entry code.
315 static Code* debug_break_return_entry_;
316
317 // Code to call for handling debug break on return.
318 static Code* debug_break_return_;
319
320 DISALLOW_EVIL_CONSTRUCTORS(Debug);
321};
322
323
324class PendingRequest;
325class DebugMessageThread;
326
327
328class Debugger {
329 public:
330 static void DebugRequest(const uint16_t* json_request, int length);
331 static bool ProcessPendingRequests();
332
333 static Handle<Object> MakeJSObject(Vector<const char> constructor_name,
334 int argc, Object*** argv,
335 bool* caught_exception);
336 static Handle<Object> MakeExecutionState(bool* caught_exception);
337 static Handle<Object> MakeBreakEvent(Handle<Object> exec_state,
338 Handle<Object> break_points_hit,
339 bool* caught_exception);
340 static Handle<Object> MakeExceptionEvent(Handle<Object> exec_state,
341 Handle<Object> exception,
342 bool uncaught,
343 bool* caught_exception);
344 static Handle<Object> MakeNewFunctionEvent(Handle<Object> func,
345 bool* caught_exception);
346 static Handle<Object> MakeCompileEvent(Handle<Script> script,
347 Handle<Object> script_function,
348 bool* caught_exception);
349 static Handle<String> ProcessRequest(Handle<Object> exec_state,
350 Handle<Object> request,
351 bool stopped);
352 static bool IsPlainBreakRequest(Handle<Object> request);
353
354 static void OnDebugBreak(Handle<Object> break_points_hit);
355 static void OnException(Handle<Object> exception, bool uncaught);
356 static void OnBeforeCompile(Handle<Script> script);
357 static void OnAfterCompile(Handle<Script> script,
358 Handle<JSFunction> fun);
359 static void OnNewFunction(Handle<JSFunction> fun);
360 static void OnPendingRequestProcessed(Handle<Object> event_data);
361 static void ProcessDebugEvent(v8::DebugEvent event,
362 Handle<Object> event_data);
363 static void SetMessageHandler(v8::DebugMessageHandler handler, void* data);
364 static void SendMessage(Vector<uint16_t> message);
365 static void ProcessCommand(Vector<const uint16_t> command);
366 static void UpdateActiveDebugger();
367 inline static bool EventActive(v8::DebugEvent event) {
368 // Currently argument event is not used.
369 return !Debugger::compiling_natives_ && Debugger::debugger_active_;
370 }
371
372 static void set_debugger_active(bool debugger_active) {
373 Debugger::debugger_active_ = debugger_active;
374 }
375 static bool debugger_active() { return Debugger::debugger_active_; }
376 static void set_compiling_natives(bool compiling_natives) {
377 Debugger::compiling_natives_ = compiling_natives;
378 }
379 static bool compiling_natives() { return Debugger::compiling_natives_; }
380
381 private:
382 static bool debugger_active_; // Are there any active debugger?
383 static bool compiling_natives_; // Are we compiling natives?
384 static DebugMessageThread* message_thread_;
385 static v8::DebugMessageHandler debug_message_handler_;
386 static void* debug_message_handler_data_;
387
388 // Head and tail of linked list of pending commands. The list is protected
389 // by a mutex as it can be updated/read from different threads.
390 static Mutex* pending_requests_access_;
391 static PendingRequest* pending_requests_head_;
392 static PendingRequest* pending_requests_tail_;
393};
394
395
396// Linked list of pending requests issued by debugger while V8 was running.
397class PendingRequest {
398 public:
399 PendingRequest(const uint16_t* json_request, int length);
400 ~PendingRequest();
401
402 PendingRequest* next() { return next_; }
403 void set_next(PendingRequest* next) { next_ = next; }
404 Handle<String> request();
405
406 private:
407 Vector<uint16_t> json_request_; // Request string.
408 PendingRequest* next_; // Next pointer for linked list.
409};
410
411
412class DebugMessageThread: public Thread {
413 public:
414 DebugMessageThread();
415 virtual ~DebugMessageThread();
416
417 void DebugEvent(v8::DebugEvent,
418 Handle<Object> exec_state,
419 Handle<Object> event_data);
420 void SetEventJSON(Vector<uint16_t> event_json);
421 void SetEventJSONFromEvent(Handle<Object> event_data);
422 void SetCommand(Vector<uint16_t> command);
423 void SetResult(const char* result);
424 void SetResult(Vector<uint16_t> result);
425 void CommandResult(Vector<uint16_t> result);
426
427 void ProcessCommand(Vector<uint16_t> command);
428
429 void OnDebuggerInactive();
430
431 protected:
432 void Run();
433 void HandleCommand();
434
435 bool host_running_; // Is the debugging host running or stopped
436 v8::DebugEvent event_; // Active event
437 Semaphore* command_received_; // Signal from the telnet connection
438 Semaphore* debug_event_; // Signal from the V8 thread
439 Semaphore* debug_command_; // Signal to the V8 thread
440 Semaphore* debug_result_; // Signal from the V8 thread
441
442 private:
443 void SetVector(Vector<uint16_t>* vector, Vector<uint16_t> value);
444 bool TwoByteEqualsAscii(Vector<uint16_t> two_byte, const char* ascii);
445
446 Vector<uint16_t> event_json_; // Active event JSON.
447 Vector<uint16_t> command_; // Current command.
448 Vector<uint16_t> result_; // Result of processing command.
449 DISALLOW_EVIL_CONSTRUCTORS(DebugMessageThread);
450};
451
452
453// Helper class to support saving/restoring the top break frame id.
454class SaveBreakFrame {
455 public:
456 SaveBreakFrame() : set_(!it_.done()) {
457 if (set_) {
458 // Store the previous break is and frame id.
459 break_id_ = Top::break_id();
460 break_frame_id_ = Top::break_frame_id();
461
462 // Create the new break info.
463 Top::new_break(it_.frame()->id());
464 }
465 }
466
467 ~SaveBreakFrame() {
468 if (set_) {
469 // restore to the previous break state.
470 Top::set_break(break_frame_id_, break_id_);
471 }
472 }
473
474 private:
475 JavaScriptFrameIterator it_;
476 const bool set_; // Was the break actually set?
477 StackFrame::Id break_frame_id_; // Previous break frame id.
478 int break_id_; // Previous break id.
479};
480
481
482class EnterDebuggerContext BASE_EMBEDDED {
483 public:
484 // Enter the debugger by storing the previous top context and setting the
485 // current top context to the debugger context.
486 EnterDebuggerContext() {
487 // NOTE the member variable save which saves the previous context before
488 // this change.
489 Top::set_context(*Debug::debug_context());
490 Top::set_security_context(*Debug::debug_context());
491 }
492
493 private:
494 SaveContext save;
495};
496
497
kasper.lundbd3ec4e2008-07-09 11:06:54 +0000498// Stack allocated class for disabling break.
499class DisableBreak BASE_EMBEDDED {
500 public:
501 // Enter the debugger by storing the previous top context and setting the
502 // current top context to the debugger context.
503 explicit DisableBreak(bool disable_break) {
504 prev_disable_break_ = Debug::disable_break();
505 Debug::set_disable_break(disable_break);
506 }
507 ~DisableBreak() {
508 Debug::set_disable_break(prev_disable_break_);
509 }
510
511 private:
512 // The previous state of the disable break used to restore the value when this
513 // object is destructed.
514 bool prev_disable_break_;
515};
516
517
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000518// Debug_Address encapsulates the Address pointers used in generating debug
519// code.
520class Debug_Address {
521 public:
522 Debug_Address(Debug::AddressId id, int reg = 0)
523 : id_(id), reg_(reg) {
524 ASSERT(reg == 0 || id == Debug::k_register_address);
525 }
526
527 static Debug_Address AfterBreakTarget() {
528 return Debug_Address(Debug::k_after_break_target_address);
529 }
530
531 static Debug_Address DebugBreakReturn() {
532 return Debug_Address(Debug::k_debug_break_return_address);
533 }
534
535 static Debug_Address Register(int reg) {
536 return Debug_Address(Debug::k_register_address, reg);
537 }
538
539 Address address() const {
540 switch (id_) {
541 case Debug::k_after_break_target_address:
542 return reinterpret_cast<Address>(Debug::after_break_target_address());
543 case Debug::k_debug_break_return_address:
544 return reinterpret_cast<Address>(Debug::debug_break_return_address());
545 case Debug::k_register_address:
546 return reinterpret_cast<Address>(Debug::register_address(reg_));
547 default:
548 UNREACHABLE();
549 return NULL;
550 }
551 }
552 private:
553 Debug::AddressId id_;
554 int reg_;
555};
556
557
558} } // namespace v8::internal
559
560#endif // V8_DEBUG_H_