blob: 069e49b227df27adebcecb4dfa0c682780793a69 [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);
178 static bool IsDebugBreak(Address addr);
179
180 // Check whether a code stub with the specified major key is a possible break
181 // point location.
182 static bool IsSourceBreakStub(Code* code);
183 static bool IsBreakStub(Code* code);
184
185 // Find the builtin to use for invoking the debug break
186 static Handle<Code> FindDebugBreak(RelocInfo* rinfo);
187
188 static Handle<Object> GetSourceBreakLocations(
189 Handle<SharedFunctionInfo> shared);
190 static Code* GetCodeTarget(Address target);
191
192 // Getter for the debug_context.
193 inline static Handle<Context> debug_context() { return debug_context_; }
194
195 // Check whether a global object is the debug global object.
196 static bool IsDebugGlobal(GlobalObject* global);
197
198 // Fast check to see if any break points are active.
199 inline static bool has_break_points() { return has_break_points_; }
200
201 static bool StepInActive() { return thread_local_.step_into_fp_ != 0; }
202 static Address step_in_fp() { return thread_local_.step_into_fp_; }
203 static Address* step_in_fp_addr() { return &thread_local_.step_into_fp_; }
204
205 // Getters for the current exception break state.
206 static bool break_on_exception() { return break_on_exception_; }
207 static bool break_on_uncaught_exception() {
208 return break_on_uncaught_exception_;
209 }
210
211 enum AddressId {
212 k_after_break_target_address,
213 k_debug_break_return_address,
214 k_register_address
215 };
216
217 // Support for setting the address to jump to when returning from break point.
218 static Address* after_break_target_address() {
219 return reinterpret_cast<Address*>(&thread_local_.after_break_target_);
220 }
221
222 // Support for saving/restoring registers when handling debug break calls.
223 static Address* register_address(int r) {
224 return reinterpret_cast<Address *>(&registers_[r]);
225 }
226
227 // Addres of the debug break return entry code.
228 static Code* debug_break_return_entry() { return debug_break_return_entry_; }
229
230 // Support for getting the address of the debug break on return code.
231 static Address* debug_break_return_address() {
232 return reinterpret_cast<Address*>(&debug_break_return_);
233 }
234
235 static const int kEstimatedNofDebugInfoEntries = 16;
236 static const int kEstimatedNofBreakPointsInFunction = 16;
237
238 static void HandleWeakDebugInfo(v8::Persistent<v8::Object> obj, void* data);
239
240 friend class Debugger;
241 friend Handle<FixedArray> GetDebuggedFunctions(); // Found in test-debug.cc
242
243 // Threading support.
244 static char* ArchiveDebug(char* to);
245 static char* RestoreDebug(char* from);
246 static int ArchiveSpacePerThread();
247
248 // Code generation assumptions.
249 static const int kIa32CallInstructionLength = 5;
250 static const int kIa32JSReturnSequenceLength = 6;
251
252 private:
253 static bool CompileDebuggerScript(int index);
254 static void ClearOneShot();
255 static void ActivateStepIn(StackFrame* frame);
256 static void ClearStepIn();
257 static void ClearStepNext();
258 static void EnsureCompiled(Handle<SharedFunctionInfo> shared);
259 static Handle<DebugInfo> AddDebugInfo(Handle<SharedFunctionInfo> shared);
260 static void RemoveDebugInfo(Handle<DebugInfo> debug_info);
261 static void SetAfterBreakTarget(JavaScriptFrame* frame);
262 static Handle<Object> CheckBreakPoints(Handle<Object> break_point);
263 static bool CheckBreakPoint(Handle<Object> break_point_object);
264
265 // Global handle to debug context where all the debugger JavaScript code is
266 // loaded.
267 static Handle<Context> debug_context_;
268
269 // Boolean state indicating whether any break points are set.
270 static bool has_break_points_;
271 static DebugInfoListNode* debug_info_list_;
272
273 static bool break_on_exception_;
274 static bool break_on_uncaught_exception_;
275
276 // Per-thread:
277 class ThreadLocal {
278 public:
279 // Step action for last step performed.
280 StepAction last_step_action_;
281
282 // Source statement position from last step next action.
283 int last_statement_position_;
284
285 // Number of steps left to perform before debug event.
286 int step_count_;
287
288 // Frame pointer from last step next action.
289 Address last_fp_;
290
291 // Frame pointer for frame from which step in was performed.
292 Address step_into_fp_;
293
294 // Storage location for jump when exiting debug break calls.
295 Address after_break_target_;
296 };
297
298 // Storage location for registers when handling debug break calls
299 static JSCallerSavedBuffer registers_;
300 static ThreadLocal thread_local_;
301 static void ThreadInit();
302
303 // Code object for debug break return entry code.
304 static Code* debug_break_return_entry_;
305
306 // Code to call for handling debug break on return.
307 static Code* debug_break_return_;
308
309 DISALLOW_EVIL_CONSTRUCTORS(Debug);
310};
311
312
313class PendingRequest;
314class DebugMessageThread;
315
316
317class Debugger {
318 public:
319 static void DebugRequest(const uint16_t* json_request, int length);
320 static bool ProcessPendingRequests();
321
322 static Handle<Object> MakeJSObject(Vector<const char> constructor_name,
323 int argc, Object*** argv,
324 bool* caught_exception);
325 static Handle<Object> MakeExecutionState(bool* caught_exception);
326 static Handle<Object> MakeBreakEvent(Handle<Object> exec_state,
327 Handle<Object> break_points_hit,
328 bool* caught_exception);
329 static Handle<Object> MakeExceptionEvent(Handle<Object> exec_state,
330 Handle<Object> exception,
331 bool uncaught,
332 bool* caught_exception);
333 static Handle<Object> MakeNewFunctionEvent(Handle<Object> func,
334 bool* caught_exception);
335 static Handle<Object> MakeCompileEvent(Handle<Script> script,
336 Handle<Object> script_function,
337 bool* caught_exception);
338 static Handle<String> ProcessRequest(Handle<Object> exec_state,
339 Handle<Object> request,
340 bool stopped);
341 static bool IsPlainBreakRequest(Handle<Object> request);
342
343 static void OnDebugBreak(Handle<Object> break_points_hit);
344 static void OnException(Handle<Object> exception, bool uncaught);
345 static void OnBeforeCompile(Handle<Script> script);
346 static void OnAfterCompile(Handle<Script> script,
347 Handle<JSFunction> fun);
348 static void OnNewFunction(Handle<JSFunction> fun);
349 static void OnPendingRequestProcessed(Handle<Object> event_data);
350 static void ProcessDebugEvent(v8::DebugEvent event,
351 Handle<Object> event_data);
352 static void SetMessageHandler(v8::DebugMessageHandler handler, void* data);
353 static void SendMessage(Vector<uint16_t> message);
354 static void ProcessCommand(Vector<const uint16_t> command);
355 static void UpdateActiveDebugger();
356 inline static bool EventActive(v8::DebugEvent event) {
357 // Currently argument event is not used.
358 return !Debugger::compiling_natives_ && Debugger::debugger_active_;
359 }
360
361 static void set_debugger_active(bool debugger_active) {
362 Debugger::debugger_active_ = debugger_active;
363 }
364 static bool debugger_active() { return Debugger::debugger_active_; }
365 static void set_compiling_natives(bool compiling_natives) {
366 Debugger::compiling_natives_ = compiling_natives;
367 }
368 static bool compiling_natives() { return Debugger::compiling_natives_; }
369
370 private:
371 static bool debugger_active_; // Are there any active debugger?
372 static bool compiling_natives_; // Are we compiling natives?
373 static DebugMessageThread* message_thread_;
374 static v8::DebugMessageHandler debug_message_handler_;
375 static void* debug_message_handler_data_;
376
377 // Head and tail of linked list of pending commands. The list is protected
378 // by a mutex as it can be updated/read from different threads.
379 static Mutex* pending_requests_access_;
380 static PendingRequest* pending_requests_head_;
381 static PendingRequest* pending_requests_tail_;
382};
383
384
385// Linked list of pending requests issued by debugger while V8 was running.
386class PendingRequest {
387 public:
388 PendingRequest(const uint16_t* json_request, int length);
389 ~PendingRequest();
390
391 PendingRequest* next() { return next_; }
392 void set_next(PendingRequest* next) { next_ = next; }
393 Handle<String> request();
394
395 private:
396 Vector<uint16_t> json_request_; // Request string.
397 PendingRequest* next_; // Next pointer for linked list.
398};
399
400
401class DebugMessageThread: public Thread {
402 public:
403 DebugMessageThread();
404 virtual ~DebugMessageThread();
405
406 void DebugEvent(v8::DebugEvent,
407 Handle<Object> exec_state,
408 Handle<Object> event_data);
409 void SetEventJSON(Vector<uint16_t> event_json);
410 void SetEventJSONFromEvent(Handle<Object> event_data);
411 void SetCommand(Vector<uint16_t> command);
412 void SetResult(const char* result);
413 void SetResult(Vector<uint16_t> result);
414 void CommandResult(Vector<uint16_t> result);
415
416 void ProcessCommand(Vector<uint16_t> command);
417
418 void OnDebuggerInactive();
419
420 protected:
421 void Run();
422 void HandleCommand();
423
424 bool host_running_; // Is the debugging host running or stopped
425 v8::DebugEvent event_; // Active event
426 Semaphore* command_received_; // Signal from the telnet connection
427 Semaphore* debug_event_; // Signal from the V8 thread
428 Semaphore* debug_command_; // Signal to the V8 thread
429 Semaphore* debug_result_; // Signal from the V8 thread
430
431 private:
432 void SetVector(Vector<uint16_t>* vector, Vector<uint16_t> value);
433 bool TwoByteEqualsAscii(Vector<uint16_t> two_byte, const char* ascii);
434
435 Vector<uint16_t> event_json_; // Active event JSON.
436 Vector<uint16_t> command_; // Current command.
437 Vector<uint16_t> result_; // Result of processing command.
438 DISALLOW_EVIL_CONSTRUCTORS(DebugMessageThread);
439};
440
441
442// Helper class to support saving/restoring the top break frame id.
443class SaveBreakFrame {
444 public:
445 SaveBreakFrame() : set_(!it_.done()) {
446 if (set_) {
447 // Store the previous break is and frame id.
448 break_id_ = Top::break_id();
449 break_frame_id_ = Top::break_frame_id();
450
451 // Create the new break info.
452 Top::new_break(it_.frame()->id());
453 }
454 }
455
456 ~SaveBreakFrame() {
457 if (set_) {
458 // restore to the previous break state.
459 Top::set_break(break_frame_id_, break_id_);
460 }
461 }
462
463 private:
464 JavaScriptFrameIterator it_;
465 const bool set_; // Was the break actually set?
466 StackFrame::Id break_frame_id_; // Previous break frame id.
467 int break_id_; // Previous break id.
468};
469
470
471class EnterDebuggerContext BASE_EMBEDDED {
472 public:
473 // Enter the debugger by storing the previous top context and setting the
474 // current top context to the debugger context.
475 EnterDebuggerContext() {
476 // NOTE the member variable save which saves the previous context before
477 // this change.
478 Top::set_context(*Debug::debug_context());
479 Top::set_security_context(*Debug::debug_context());
480 }
481
482 private:
483 SaveContext save;
484};
485
486
487// Debug_Address encapsulates the Address pointers used in generating debug
488// code.
489class Debug_Address {
490 public:
491 Debug_Address(Debug::AddressId id, int reg = 0)
492 : id_(id), reg_(reg) {
493 ASSERT(reg == 0 || id == Debug::k_register_address);
494 }
495
496 static Debug_Address AfterBreakTarget() {
497 return Debug_Address(Debug::k_after_break_target_address);
498 }
499
500 static Debug_Address DebugBreakReturn() {
501 return Debug_Address(Debug::k_debug_break_return_address);
502 }
503
504 static Debug_Address Register(int reg) {
505 return Debug_Address(Debug::k_register_address, reg);
506 }
507
508 Address address() const {
509 switch (id_) {
510 case Debug::k_after_break_target_address:
511 return reinterpret_cast<Address>(Debug::after_break_target_address());
512 case Debug::k_debug_break_return_address:
513 return reinterpret_cast<Address>(Debug::debug_break_return_address());
514 case Debug::k_register_address:
515 return reinterpret_cast<Address>(Debug::register_address(reg_));
516 default:
517 UNREACHABLE();
518 return NULL;
519 }
520 }
521 private:
522 Debug::AddressId id_;
523 int reg_;
524};
525
526
527} } // namespace v8::internal
528
529#endif // V8_DEBUG_H_