blob: b9384e574dd224fc8992d32dabaf12112716b7e6 [file] [log] [blame]
whesse@chromium.org030d38e2011-07-13 13:23:34 +00001// Copyright 2011 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.org5ec48922009-05-05 07:25:34 +000028#ifndef V8_DEBUG_H_
29#define V8_DEBUG_H_
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000030
lrn@chromium.org1c092762011-05-09 09:42:16 +000031#include "allocation.h"
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000032#include "arguments.h"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000033#include "assembler.h"
kasperl@chromium.org7be3c992009-03-12 07:19:55 +000034#include "debug-agent.h"
kasperl@chromium.org41044eb2008-10-06 08:24:46 +000035#include "execution.h"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000036#include "factory.h"
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +000037#include "flags.h"
whesse@chromium.org030d38e2011-07-13 13:23:34 +000038#include "frames-inl.h"
kasperl@chromium.org71affb52009-05-26 05:44:31 +000039#include "hashmap.h"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000040#include "platform.h"
41#include "string-stream.h"
kasperl@chromium.org7be3c992009-03-12 07:19:55 +000042#include "v8threads.h"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000043
ager@chromium.org65dad4b2009-04-23 08:48:43 +000044#ifdef ENABLE_DEBUGGER_SUPPORT
45#include "../include/v8-debug.h"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000046
kasperl@chromium.org71affb52009-05-26 05:44:31 +000047namespace v8 {
48namespace internal {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000049
kasperl@chromium.org7be3c992009-03-12 07:19:55 +000050
51// Forward declarations.
52class EnterDebugger;
53
54
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000055// Step actions. NOTE: These values are in macros.py as well.
56enum StepAction {
57 StepNone = -1, // Stepping not prepared.
58 StepOut = 0, // Step out of the current function.
59 StepNext = 1, // Step to the next statement in the current function.
60 StepIn = 2, // Step into new functions invoked or the next statement
61 // in the current function.
62 StepMin = 3, // Perform a minimum step in the current function.
63 StepInMin = 4 // Step into new functions invoked or perform a minimum step
64 // in the current function.
65};
66
67
68// Type of exception break. NOTE: These values are in macros.py as well.
69enum ExceptionBreakType {
70 BreakException = 0,
71 BreakUncaughtException = 1
72};
73
74
75// Type of exception break. NOTE: These values are in macros.py as well.
76enum BreakLocatorType {
77 ALL_BREAK_LOCATIONS = 0,
78 SOURCE_BREAK_LOCATIONS = 1
79};
80
81
82// Class for iterating through the break points in a function and changing
83// them.
84class BreakLocationIterator {
85 public:
86 explicit BreakLocationIterator(Handle<DebugInfo> debug_info,
87 BreakLocatorType type);
88 virtual ~BreakLocationIterator();
89
90 void Next();
91 void Next(int count);
92 void FindBreakLocationFromAddress(Address pc);
93 void FindBreakLocationFromPosition(int position);
94 void Reset();
95 bool Done() const;
96 void SetBreakPoint(Handle<Object> break_point_object);
97 void ClearBreakPoint(Handle<Object> break_point_object);
98 void SetOneShot();
99 void ClearOneShot();
100 void PrepareStepIn();
101 bool IsExit() const;
102 bool HasBreakPoint();
103 bool IsDebugBreak();
104 Object* BreakPointObjects();
ager@chromium.org381abbb2009-02-25 13:23:22 +0000105 void ClearAllDebugBreak();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000106
107
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000108 inline int code_position() {
109 return static_cast<int>(pc() - debug_info_->code()->entry());
110 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000111 inline int break_point() { return break_point_; }
112 inline int position() { return position_; }
113 inline int statement_position() { return statement_position_; }
114 inline Address pc() { return reloc_iterator_->rinfo()->pc(); }
115 inline Code* code() { return debug_info_->code(); }
116 inline RelocInfo* rinfo() { return reloc_iterator_->rinfo(); }
ager@chromium.org236ad962008-09-25 09:45:57 +0000117 inline RelocInfo::Mode rmode() const {
118 return reloc_iterator_->rinfo()->rmode();
119 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000120 inline RelocInfo* original_rinfo() {
121 return reloc_iterator_original_->rinfo();
122 }
ager@chromium.org236ad962008-09-25 09:45:57 +0000123 inline RelocInfo::Mode original_rmode() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000124 return reloc_iterator_original_->rinfo()->rmode();
125 }
126
ager@chromium.orga1645e22009-09-09 19:27:10 +0000127 bool IsDebuggerStatement();
128
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000129 protected:
130 bool RinfoDone() const;
131 void RinfoNext();
132
133 BreakLocatorType type_;
134 int break_point_;
135 int position_;
136 int statement_position_;
137 Handle<DebugInfo> debug_info_;
138 RelocIterator* reloc_iterator_;
139 RelocIterator* reloc_iterator_original_;
140
141 private:
142 void SetDebugBreak();
143 void ClearDebugBreak();
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000144
145 void SetDebugBreakAtIC();
146 void ClearDebugBreakAtIC();
147
iposva@chromium.org245aa852009-02-10 00:49:54 +0000148 bool IsDebugBreakAtReturn();
149 void SetDebugBreakAtReturn();
150 void ClearDebugBreakAtReturn();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000151
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000152 bool IsDebugBreakSlot();
153 bool IsDebugBreakAtSlot();
154 void SetDebugBreakAtSlot();
155 void ClearDebugBreakAtSlot();
156
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000157 DISALLOW_COPY_AND_ASSIGN(BreakLocationIterator);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000158};
159
160
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000161// Cache of all script objects in the heap. When a script is added a weak handle
162// to it is created and that weak handle is stored in the cache. The weak handle
163// callback takes care of removing the script from the cache. The key used in
164// the cache is the script id.
165class ScriptCache : private HashMap {
166 public:
167 ScriptCache() : HashMap(ScriptMatch), collected_scripts_(10) {}
168 virtual ~ScriptCache() { Clear(); }
169
170 // Add script to the cache.
171 void Add(Handle<Script> script);
172
173 // Return the scripts in the cache.
174 Handle<FixedArray> GetScripts();
175
176 // Generate debugger events for collected scripts.
177 void ProcessCollectedScripts();
178
179 private:
180 // Calculate the hash value from the key (script id).
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000181 static uint32_t Hash(int key) {
182 return ComputeIntegerHash(key, v8::internal::kZeroHashSeed);
183 }
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000184
185 // Scripts match if their keys (script id) match.
186 static bool ScriptMatch(void* key1, void* key2) { return key1 == key2; }
187
188 // Clear the cache releasing all the weak handles.
189 void Clear();
190
191 // Weak handle callback for scripts in the cache.
192 static void HandleWeakScript(v8::Persistent<v8::Value> obj, void* data);
193
194 // List used during GC to temporarily store id's of collected scripts.
195 List<int> collected_scripts_;
196};
197
198
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000199// Linked list holding debug info objects. The debug info objects are kept as
200// weak handles to avoid a debug info object to keep a function alive.
201class DebugInfoListNode {
202 public:
203 explicit DebugInfoListNode(DebugInfo* debug_info);
204 virtual ~DebugInfoListNode();
205
206 DebugInfoListNode* next() { return next_; }
207 void set_next(DebugInfoListNode* next) { next_ = next; }
208 Handle<DebugInfo> debug_info() { return debug_info_; }
209
210 private:
211 // Global (weak) handle to the debug info object.
212 Handle<DebugInfo> debug_info_;
213
214 // Next pointer for linked list.
215 DebugInfoListNode* next_;
216};
217
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000218// This class contains the debugger support. The main purpose is to handle
219// setting break points in the code.
220//
221// This class controls the debug info for all functions which currently have
222// active breakpoints in them. This debug info is held in the heap root object
223// debug_info which is a FixedArray. Each entry in this list is of class
224// DebugInfo.
225class Debug {
226 public:
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000227 void SetUp(bool create_heap_objects);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000228 bool Load();
229 void Unload();
230 bool IsLoaded() { return !debug_context_.is_null(); }
231 bool InDebugger() { return thread_local_.debugger_entry_ != NULL; }
232 void PreemptionWhileInDebugger();
233 void Iterate(ObjectVisitor* v);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000234
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000235 Object* Break(Arguments args);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000236 void SetBreakPoint(Handle<SharedFunctionInfo> shared,
237 Handle<Object> break_point_object,
238 int* source_position);
239 void ClearBreakPoint(Handle<Object> break_point_object);
240 void ClearAllBreakPoints();
241 void FloodWithOneShot(Handle<SharedFunctionInfo> shared);
242 void FloodHandlerWithOneShot();
243 void ChangeBreakOnException(ExceptionBreakType type, bool enable);
244 bool IsBreakOnException(ExceptionBreakType type);
245 void PrepareStep(StepAction step_action, int step_count);
246 void ClearStepping();
247 bool StepNextContinue(BreakLocationIterator* break_location_iterator,
248 JavaScriptFrame* frame);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000249 static Handle<DebugInfo> GetDebugInfo(Handle<SharedFunctionInfo> shared);
250 static bool HasDebugInfo(Handle<SharedFunctionInfo> shared);
kasper.lundbd3ec4e2008-07-09 11:06:54 +0000251
lrn@chromium.org34e60782011-09-15 07:25:40 +0000252 void PrepareForBreakPoints();
253
ager@chromium.org32912102009-01-16 10:38:43 +0000254 // Returns whether the operation succeeded.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000255 bool EnsureDebugInfo(Handle<SharedFunctionInfo> shared);
kasper.lundbd3ec4e2008-07-09 11:06:54 +0000256
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000257 // Returns true if the current stub call is patched to call the debugger.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000258 static bool IsDebugBreak(Address addr);
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000259 // Returns true if the current return statement has been patched to be
260 // a debugger breakpoint.
ager@chromium.org381abbb2009-02-25 13:23:22 +0000261 static bool IsDebugBreakAtReturn(RelocInfo* rinfo);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000262
263 // Check whether a code stub with the specified major key is a possible break
264 // point location.
265 static bool IsSourceBreakStub(Code* code);
266 static bool IsBreakStub(Code* code);
267
268 // Find the builtin to use for invoking the debug break
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000269 static Handle<Code> FindDebugBreak(Handle<Code> code, RelocInfo::Mode mode);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000270
271 static Handle<Object> GetSourceBreakLocations(
272 Handle<SharedFunctionInfo> shared);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000273
274 // Getter for the debug_context.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000275 inline Handle<Context> debug_context() { return debug_context_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000276
277 // Check whether a global object is the debug global object.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000278 bool IsDebugGlobal(GlobalObject* global);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000279
ager@chromium.org2cc82ae2010-06-14 07:35:38 +0000280 // Check whether this frame is just about to return.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000281 bool IsBreakAtReturn(JavaScriptFrame* frame);
ager@chromium.org2cc82ae2010-06-14 07:35:38 +0000282
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000283 // Fast check to see if any break points are active.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000284 inline bool has_break_points() { return has_break_points_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000285
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000286 void NewBreak(StackFrame::Id break_frame_id);
287 void SetBreak(StackFrame::Id break_frame_id, int break_id);
288 StackFrame::Id break_frame_id() {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000289 return thread_local_.break_frame_id_;
290 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000291 int break_id() { return thread_local_.break_id_; }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000292
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000293 bool StepInActive() { return thread_local_.step_into_fp_ != 0; }
294 void HandleStepIn(Handle<JSFunction> function,
295 Handle<Object> holder,
296 Address fp,
297 bool is_constructor);
298 Address step_in_fp() { return thread_local_.step_into_fp_; }
299 Address* step_in_fp_addr() { return &thread_local_.step_into_fp_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000300
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000301 bool StepOutActive() { return thread_local_.step_out_fp_ != 0; }
302 Address step_out_fp() { return thread_local_.step_out_fp_; }
ager@chromium.orga1645e22009-09-09 19:27:10 +0000303
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000304 EnterDebugger* debugger_entry() {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000305 return thread_local_.debugger_entry_;
306 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000307 void set_debugger_entry(EnterDebugger* entry) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000308 thread_local_.debugger_entry_ = entry;
309 }
310
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000311 // Check whether any of the specified interrupts are pending.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000312 bool is_interrupt_pending(InterruptFlag what) {
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000313 return (thread_local_.pending_interrupts_ & what) != 0;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000314 }
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000315
316 // Set specified interrupts as pending.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000317 void set_interrupts_pending(InterruptFlag what) {
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000318 thread_local_.pending_interrupts_ |= what;
319 }
320
321 // Clear specified interrupts from pending.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000322 void clear_interrupt_pending(InterruptFlag what) {
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000323 thread_local_.pending_interrupts_ &= ~static_cast<int>(what);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000324 }
325
kasper.lundbd3ec4e2008-07-09 11:06:54 +0000326 // Getter and setter for the disable break state.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000327 bool disable_break() { return disable_break_; }
328 void set_disable_break(bool disable_break) {
kasper.lundbd3ec4e2008-07-09 11:06:54 +0000329 disable_break_ = disable_break;
330 }
331
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000332 // Getters for the current exception break state.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000333 bool break_on_exception() { return break_on_exception_; }
334 bool break_on_uncaught_exception() {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000335 return break_on_uncaught_exception_;
336 }
337
338 enum AddressId {
339 k_after_break_target_address,
340 k_debug_break_return_address,
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000341 k_debug_break_slot_address,
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000342 k_restarter_frame_function_pointer
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000343 };
344
345 // Support for setting the address to jump to when returning from break point.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000346 Address* after_break_target_address() {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000347 return reinterpret_cast<Address*>(&thread_local_.after_break_target_);
348 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000349 Address* restarter_frame_function_pointer_address() {
whesse@chromium.orge90029b2010-08-02 11:52:17 +0000350 Object*** address = &thread_local_.restarter_frame_function_pointer_;
351 return reinterpret_cast<Address*>(address);
352 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000353
354 // Support for saving/restoring registers when handling debug break calls.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000355 Object** register_address(int r) {
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000356 return &registers_[r];
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000357 }
358
ager@chromium.orga1645e22009-09-09 19:27:10 +0000359 // Access to the debug break on return code.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000360 Code* debug_break_return() { return debug_break_return_; }
361 Code** debug_break_return_address() {
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000362 return &debug_break_return_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000363 }
364
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000365 // Access to the debug break in debug break slot code.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000366 Code* debug_break_slot() { return debug_break_slot_; }
367 Code** debug_break_slot_address() {
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000368 return &debug_break_slot_;
369 }
370
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000371 static const int kEstimatedNofDebugInfoEntries = 16;
372 static const int kEstimatedNofBreakPointsInFunction = 16;
373
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000374 // Passed to MakeWeak.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000375 static void HandleWeakDebugInfo(v8::Persistent<v8::Value> obj, void* data);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000376
377 friend class Debugger;
ager@chromium.org381abbb2009-02-25 13:23:22 +0000378 friend Handle<FixedArray> GetDebuggedFunctions(); // In test-debug.cc
379 friend void CheckDebuggerUnloaded(bool check_functions); // In test-debug.cc
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000380
381 // Threading support.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000382 char* ArchiveDebug(char* to);
383 char* RestoreDebug(char* from);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000384 static int ArchiveSpacePerThread();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000385 void FreeThreadResources() { }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000386
ager@chromium.org32912102009-01-16 10:38:43 +0000387 // Mirror cache handling.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000388 void ClearMirrorCache();
ager@chromium.org32912102009-01-16 10:38:43 +0000389
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000390 // Script cache handling.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000391 void CreateScriptCache();
392 void DestroyScriptCache();
393 void AddScriptToScriptCache(Handle<Script> script);
394 Handle<FixedArray> GetLoadedScripts();
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000395
396 // Garbage collection notifications.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000397 void AfterGarbageCollection();
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000398
ager@chromium.org8bb60582008-12-11 12:02:20 +0000399 // Code generator routines.
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000400 static void GenerateSlot(MacroAssembler* masm);
ager@chromium.org8bb60582008-12-11 12:02:20 +0000401 static void GenerateLoadICDebugBreak(MacroAssembler* masm);
402 static void GenerateStoreICDebugBreak(MacroAssembler* masm);
403 static void GenerateKeyedLoadICDebugBreak(MacroAssembler* masm);
404 static void GenerateKeyedStoreICDebugBreak(MacroAssembler* masm);
ager@chromium.org8bb60582008-12-11 12:02:20 +0000405 static void GenerateReturnDebugBreak(MacroAssembler* masm);
danno@chromium.orgc612e022011-11-10 11:38:15 +0000406 static void GenerateCallFunctionStubDebugBreak(MacroAssembler* masm);
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000407 static void GenerateCallFunctionStubRecordDebugBreak(MacroAssembler* masm);
408 static void GenerateCallConstructStubDebugBreak(MacroAssembler* masm);
409 static void GenerateCallConstructStubRecordDebugBreak(MacroAssembler* masm);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000410 static void GenerateSlotDebugBreak(MacroAssembler* masm);
ager@chromium.org357bf652010-04-12 11:30:10 +0000411 static void GeneratePlainReturnLiveEdit(MacroAssembler* masm);
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000412
413 // FrameDropper is a code replacement for a JavaScript frame with possibly
414 // several frames above.
415 // There is no calling conventions here, because it never actually gets
416 // called, it only gets returned to.
ager@chromium.org357bf652010-04-12 11:30:10 +0000417 static void GenerateFrameDropperLiveEdit(MacroAssembler* masm);
ager@chromium.org8bb60582008-12-11 12:02:20 +0000418
419 // Called from stub-cache.cc.
420 static void GenerateCallICDebugBreak(MacroAssembler* masm);
421
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000422 // Describes how exactly a frame has been dropped from stack.
423 enum FrameDropMode {
424 // No frame has been dropped.
425 FRAMES_UNTOUCHED,
426 // The top JS frame had been calling IC stub. IC stub mustn't be called now.
427 FRAME_DROPPED_IN_IC_CALL,
428 // The top JS frame had been calling debug break slot stub. Patch the
429 // address this stub jumps to in the end.
430 FRAME_DROPPED_IN_DEBUG_SLOT_CALL,
431 // The top JS frame had been calling some C++ function. The return address
432 // gets patched automatically.
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000433 FRAME_DROPPED_IN_DIRECT_CALL,
434 FRAME_DROPPED_IN_RETURN_CALL
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000435 };
436
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000437 void FramesHaveBeenDropped(StackFrame::Id new_break_frame_id,
whesse@chromium.orge90029b2010-08-02 11:52:17 +0000438 FrameDropMode mode,
439 Object** restarter_frame_function_pointer);
ager@chromium.org357bf652010-04-12 11:30:10 +0000440
whesse@chromium.orge90029b2010-08-02 11:52:17 +0000441 // Initializes an artificial stack frame. The data it contains is used for:
442 // a. successful work of frame dropper code which eventually gets control,
443 // b. being compatible with regular stack structure for various stack
444 // iterators.
445 // Returns address of stack allocated pointer to restarted function,
446 // the value that is called 'restarter_frame_function_pointer'. The value
447 // at this address (possibly updated by GC) may be used later when preparing
448 // 'step in' operation.
whesse@chromium.orge90029b2010-08-02 11:52:17 +0000449 static Object** SetUpFrameDropperFrame(StackFrame* bottom_js_frame,
450 Handle<Code> code);
451
ager@chromium.org357bf652010-04-12 11:30:10 +0000452 static const int kFrameDropperFrameSize;
453
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000454 // Architecture-specific constant.
455 static const bool kFrameDropperSupported;
456
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000457 private:
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000458 explicit Debug(Isolate* isolate);
459 ~Debug();
460
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000461 static bool CompileDebuggerScript(int index);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000462 void ClearOneShot();
463 void ActivateStepIn(StackFrame* frame);
464 void ClearStepIn();
465 void ActivateStepOut(StackFrame* frame);
466 void ClearStepOut();
467 void ClearStepNext();
ager@chromium.org32912102009-01-16 10:38:43 +0000468 // Returns whether the compile succeeded.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000469 void RemoveDebugInfo(Handle<DebugInfo> debug_info);
470 void SetAfterBreakTarget(JavaScriptFrame* frame);
471 Handle<Object> CheckBreakPoints(Handle<Object> break_point);
472 bool CheckBreakPoint(Handle<Object> break_point_object);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000473
474 // Global handle to debug context where all the debugger JavaScript code is
475 // loaded.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000476 Handle<Context> debug_context_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000477
478 // Boolean state indicating whether any break points are set.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000479 bool has_break_points_;
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000480
481 // Cache of all scripts in the heap.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000482 ScriptCache* script_cache_;
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000483
484 // List of active debug info objects.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000485 DebugInfoListNode* debug_info_list_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000486
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000487 bool disable_break_;
488 bool break_on_exception_;
489 bool break_on_uncaught_exception_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000490
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000491 // Per-thread data.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000492 class ThreadLocal {
493 public:
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000494 // Counter for generating next break id.
495 int break_count_;
496
497 // Current break id.
498 int break_id_;
499
500 // Frame id for the frame of the current break.
501 StackFrame::Id break_frame_id_;
502
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000503 // Step action for last step performed.
504 StepAction last_step_action_;
505
506 // Source statement position from last step next action.
507 int last_statement_position_;
508
509 // Number of steps left to perform before debug event.
510 int step_count_;
511
512 // Frame pointer from last step next action.
513 Address last_fp_;
514
lrn@chromium.org34e60782011-09-15 07:25:40 +0000515 // Number of queued steps left to perform before debug event.
516 int queued_step_count_;
517
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000518 // Frame pointer for frame from which step in was performed.
519 Address step_into_fp_;
520
ager@chromium.orga1645e22009-09-09 19:27:10 +0000521 // Frame pointer for the frame where debugger should be called when current
522 // step out action is completed.
523 Address step_out_fp_;
524
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000525 // Storage location for jump when exiting debug break calls.
526 Address after_break_target_;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000527
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000528 // Stores the way how LiveEdit has patched the stack. It is used when
529 // debugger returns control back to user script.
530 FrameDropMode frame_drop_mode_;
ager@chromium.org357bf652010-04-12 11:30:10 +0000531
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000532 // Top debugger entry.
533 EnterDebugger* debugger_entry_;
534
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000535 // Pending interrupts scheduled while debugging.
536 int pending_interrupts_;
whesse@chromium.orge90029b2010-08-02 11:52:17 +0000537
538 // When restarter frame is on stack, stores the address
539 // of the pointer to function being restarted. Otherwise (most of the time)
540 // stores NULL. This pointer is used with 'step in' implementation.
541 Object** restarter_frame_function_pointer_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000542 };
543
544 // Storage location for registers when handling debug break calls
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000545 JSCallerSavedBuffer registers_;
546 ThreadLocal thread_local_;
547 void ThreadInit();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000548
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000549 // Code to call for handling debug break on return.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000550 Code* debug_break_return_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000551
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000552 // Code to call for handling debug break in debug break slots.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000553 Code* debug_break_slot_;
554
555 Isolate* isolate_;
556
557 friend class Isolate;
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000558
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000559 DISALLOW_COPY_AND_ASSIGN(Debug);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000560};
561
562
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000563DECLARE_RUNTIME_FUNCTION(Object*, Debug_Break);
564
565
ager@chromium.org5ec48922009-05-05 07:25:34 +0000566// Message delivered to the message handler callback. This is either a debugger
567// event or the response to a command.
568class MessageImpl: public v8::Debug::Message {
569 public:
570 // Create a message object for a debug event.
571 static MessageImpl NewEvent(DebugEvent event,
572 bool running,
573 Handle<JSObject> exec_state,
574 Handle<JSObject> event_data);
575
576 // Create a message object for the response to a debug command.
577 static MessageImpl NewResponse(DebugEvent event,
578 bool running,
579 Handle<JSObject> exec_state,
580 Handle<JSObject> event_data,
581 Handle<String> response_json,
582 v8::Debug::ClientData* client_data);
583
584 // Implementation of interface v8::Debug::Message.
585 virtual bool IsEvent() const;
586 virtual bool IsResponse() const;
587 virtual DebugEvent GetEvent() const;
588 virtual bool WillStartRunning() const;
589 virtual v8::Handle<v8::Object> GetExecutionState() const;
590 virtual v8::Handle<v8::Object> GetEventData() const;
591 virtual v8::Handle<v8::String> GetJSON() const;
592 virtual v8::Handle<v8::Context> GetEventContext() const;
593 virtual v8::Debug::ClientData* GetClientData() const;
594
595 private:
596 MessageImpl(bool is_event,
597 DebugEvent event,
598 bool running,
599 Handle<JSObject> exec_state,
600 Handle<JSObject> event_data,
601 Handle<String> response_json,
602 v8::Debug::ClientData* client_data);
603
604 bool is_event_; // Does this message represent a debug event?
605 DebugEvent event_; // Debug event causing the break.
606 bool running_; // Will the VM start running after this event?
607 Handle<JSObject> exec_state_; // Current execution state.
608 Handle<JSObject> event_data_; // Data associated with the event.
609 Handle<String> response_json_; // Response JSON if message holds a response.
610 v8::Debug::ClientData* client_data_; // Client data passed with the request.
611};
612
613
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000614// Details of the debug event delivered to the debug event listener.
615class EventDetailsImpl : public v8::Debug::EventDetails {
616 public:
617 EventDetailsImpl(DebugEvent event,
618 Handle<JSObject> exec_state,
619 Handle<JSObject> event_data,
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +0000620 Handle<Object> callback_data,
621 v8::Debug::ClientData* client_data);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000622 virtual DebugEvent GetEvent() const;
623 virtual v8::Handle<v8::Object> GetExecutionState() const;
624 virtual v8::Handle<v8::Object> GetEventData() const;
625 virtual v8::Handle<v8::Context> GetEventContext() const;
626 virtual v8::Handle<v8::Value> GetCallbackData() const;
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +0000627 virtual v8::Debug::ClientData* GetClientData() const;
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000628 private:
629 DebugEvent event_; // Debug event causing the break.
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +0000630 Handle<JSObject> exec_state_; // Current execution state.
631 Handle<JSObject> event_data_; // Data associated with the event.
632 Handle<Object> callback_data_; // User data passed with the callback
633 // when it was registered.
634 v8::Debug::ClientData* client_data_; // Data passed to DebugBreakForCommand.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000635};
636
637
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000638// Message send by user to v8 debugger or debugger output message.
639// In addition to command text it may contain a pointer to some user data
640// which are expected to be passed along with the command reponse to message
641// handler.
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000642class CommandMessage {
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000643 public:
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000644 static CommandMessage New(const Vector<uint16_t>& command,
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000645 v8::Debug::ClientData* data);
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000646 CommandMessage();
647 ~CommandMessage();
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000648
649 // Deletes user data and disposes of the text.
650 void Dispose();
651 Vector<uint16_t> text() const { return text_; }
652 v8::Debug::ClientData* client_data() const { return client_data_; }
653 private:
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000654 CommandMessage(const Vector<uint16_t>& text,
655 v8::Debug::ClientData* data);
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000656
657 Vector<uint16_t> text_;
658 v8::Debug::ClientData* client_data_;
659};
660
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000661// A Queue of CommandMessage objects. A thread-safe version is
662// LockingCommandMessageQueue, based on this class.
663class CommandMessageQueue BASE_EMBEDDED {
ager@chromium.org41826e72009-03-30 13:30:57 +0000664 public:
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000665 explicit CommandMessageQueue(int size);
666 ~CommandMessageQueue();
ager@chromium.org41826e72009-03-30 13:30:57 +0000667 bool IsEmpty() const { return start_ == end_; }
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000668 CommandMessage Get();
669 void Put(const CommandMessage& message);
ager@chromium.org41826e72009-03-30 13:30:57 +0000670 void Clear() { start_ = end_ = 0; } // Queue is empty after Clear().
671 private:
672 // Doubles the size of the message queue, and copies the messages.
673 void Expand();
674
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000675 CommandMessage* messages_;
ager@chromium.org41826e72009-03-30 13:30:57 +0000676 int start_;
677 int end_;
678 int size_; // The size of the queue buffer. Queue can hold size-1 messages.
679};
680
681
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000682class MessageDispatchHelperThread;
683
684
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000685// LockingCommandMessageQueue is a thread-safe circular buffer of CommandMessage
686// messages. The message data is not managed by LockingCommandMessageQueue.
ager@chromium.org41826e72009-03-30 13:30:57 +0000687// Pointers to the data are passed in and out. Implemented by adding a
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000688// Mutex to CommandMessageQueue. Includes logging of all puts and gets.
689class LockingCommandMessageQueue BASE_EMBEDDED {
ager@chromium.org41826e72009-03-30 13:30:57 +0000690 public:
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000691 LockingCommandMessageQueue(Logger* logger, int size);
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000692 ~LockingCommandMessageQueue();
ager@chromium.org41826e72009-03-30 13:30:57 +0000693 bool IsEmpty() const;
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000694 CommandMessage Get();
695 void Put(const CommandMessage& message);
ager@chromium.org41826e72009-03-30 13:30:57 +0000696 void Clear();
697 private:
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000698 Logger* logger_;
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000699 CommandMessageQueue queue_;
ager@chromium.org41826e72009-03-30 13:30:57 +0000700 Mutex* lock_;
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000701 DISALLOW_COPY_AND_ASSIGN(LockingCommandMessageQueue);
ager@chromium.org41826e72009-03-30 13:30:57 +0000702};
703
704
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000705class Debugger {
706 public:
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000707 ~Debugger();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000708
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000709 void DebugRequest(const uint16_t* json_request, int length);
710
711 Handle<Object> MakeJSObject(Vector<const char> constructor_name,
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +0000712 int argc,
713 Handle<Object> argv[],
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000714 bool* caught_exception);
715 Handle<Object> MakeExecutionState(bool* caught_exception);
716 Handle<Object> MakeBreakEvent(Handle<Object> exec_state,
717 Handle<Object> break_points_hit,
718 bool* caught_exception);
719 Handle<Object> MakeExceptionEvent(Handle<Object> exec_state,
720 Handle<Object> exception,
721 bool uncaught,
722 bool* caught_exception);
723 Handle<Object> MakeNewFunctionEvent(Handle<Object> func,
724 bool* caught_exception);
725 Handle<Object> MakeCompileEvent(Handle<Script> script,
726 bool before,
727 bool* caught_exception);
728 Handle<Object> MakeScriptCollectedEvent(int id,
729 bool* caught_exception);
730 void OnDebugBreak(Handle<Object> break_points_hit, bool auto_continue);
731 void OnException(Handle<Object> exception, bool uncaught);
732 void OnBeforeCompile(Handle<Script> script);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000733
734 enum AfterCompileFlags {
735 NO_AFTER_COMPILE_FLAGS,
736 SEND_WHEN_DEBUGGING
737 };
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000738 void OnAfterCompile(Handle<Script> script,
739 AfterCompileFlags after_compile_flags);
740 void OnNewFunction(Handle<JSFunction> fun);
741 void OnScriptCollected(int id);
742 void ProcessDebugEvent(v8::DebugEvent event,
743 Handle<JSObject> event_data,
744 bool auto_continue);
745 void NotifyMessageHandler(v8::DebugEvent event,
746 Handle<JSObject> exec_state,
747 Handle<JSObject> event_data,
748 bool auto_continue);
749 void SetEventListener(Handle<Object> callback, Handle<Object> data);
750 void SetMessageHandler(v8::Debug::MessageHandler2 handler);
751 void SetHostDispatchHandler(v8::Debug::HostDispatchHandler handler,
752 int period);
753 void SetDebugMessageDispatchHandler(
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000754 v8::Debug::DebugMessageDispatchHandler handler,
755 bool provide_locker);
ager@chromium.org41826e72009-03-30 13:30:57 +0000756
757 // Invoke the message handler function.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000758 void InvokeMessageHandler(MessageImpl message);
ager@chromium.org41826e72009-03-30 13:30:57 +0000759
760 // Add a debugger command to the command queue.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000761 void ProcessCommand(Vector<const uint16_t> command,
762 v8::Debug::ClientData* client_data = NULL);
ager@chromium.org41826e72009-03-30 13:30:57 +0000763
764 // Check whether there are commands in the command queue.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000765 bool HasCommands();
ager@chromium.org41826e72009-03-30 13:30:57 +0000766
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +0000767 // Enqueue a debugger command to the command queue for event listeners.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000768 void EnqueueDebugCommand(v8::Debug::ClientData* client_data = NULL);
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +0000769
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000770 Handle<Object> Call(Handle<JSFunction> fun,
771 Handle<Object> data,
772 bool* pending_exception);
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000773
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000774 // Start the debugger agent listening on the provided port.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000775 bool StartAgent(const char* name, int port,
776 bool wait_for_connection = false);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000777
778 // Stop the debugger agent.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000779 void StopAgent();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000780
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000781 // Blocks until the agent has started listening for connections
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000782 void WaitForAgent();
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000783
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000784 void CallMessageDispatchHandler();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000785
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000786 Handle<Context> GetDebugContext();
lrn@chromium.orgc34f5802010-04-28 12:53:43 +0000787
ager@chromium.org71daaf62009-04-01 07:22:49 +0000788 // Unload the debugger if possible. Only called when no debugger is currently
789 // active.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000790 void UnloadDebugger();
lrn@chromium.orgc34f5802010-04-28 12:53:43 +0000791 friend void ForceUnloadDebugger(); // In test-debug.cc
ager@chromium.org71daaf62009-04-01 07:22:49 +0000792
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000793 inline bool EventActive(v8::DebugEvent event) {
ager@chromium.org71daaf62009-04-01 07:22:49 +0000794 ScopedLock with(debugger_access_);
795
796 // Check whether the message handler was been cleared.
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000797 if (debugger_unload_pending_) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000798 if (isolate_->debug()->debugger_entry() == NULL) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000799 UnloadDebugger();
800 }
ager@chromium.org71daaf62009-04-01 07:22:49 +0000801 }
802
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +0000803 if (((event == v8::BeforeCompile) || (event == v8::AfterCompile)) &&
804 !FLAG_debug_compile_events) {
805 return false;
806
807 } else if ((event == v8::ScriptCollected) &&
808 !FLAG_debug_script_collected_events) {
809 return false;
810 }
811
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000812 // Currently argument event is not used.
ager@chromium.org71daaf62009-04-01 07:22:49 +0000813 return !compiling_natives_ && Debugger::IsDebuggerActive();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000814 }
815
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000816 void set_compiling_natives(bool compiling_natives) {
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000817 compiling_natives_ = compiling_natives;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000818 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000819 bool compiling_natives() const { return compiling_natives_; }
820 void set_loading_debugger(bool v) { is_loading_debugger_ = v; }
821 bool is_loading_debugger() const { return is_loading_debugger_; }
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000822 void set_force_debugger_active(bool force_debugger_active) {
823 force_debugger_active_ = force_debugger_active;
824 }
825 bool force_debugger_active() const { return force_debugger_active_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000826
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000827 bool IsDebuggerActive();
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000828
829 private:
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000830 explicit Debugger(Isolate* isolate);
ager@chromium.org71daaf62009-04-01 07:22:49 +0000831
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000832 void CallEventCallback(v8::DebugEvent event,
833 Handle<Object> exec_state,
834 Handle<Object> event_data,
835 v8::Debug::ClientData* client_data);
836 void CallCEventCallback(v8::DebugEvent event,
837 Handle<Object> exec_state,
838 Handle<Object> event_data,
839 v8::Debug::ClientData* client_data);
840 void CallJSEventCallback(v8::DebugEvent event,
841 Handle<Object> exec_state,
842 Handle<Object> event_data);
843 void ListenersChanged();
ager@chromium.org381abbb2009-02-25 13:23:22 +0000844
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000845 Mutex* debugger_access_; // Mutex guarding debugger variables.
846 Handle<Object> event_listener_; // Global handle to listener.
847 Handle<Object> event_listener_data_;
848 bool compiling_natives_; // Are we compiling natives?
849 bool is_loading_debugger_; // Are we loading the debugger?
850 bool never_unload_debugger_; // Can we unload the debugger?
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000851 bool force_debugger_active_; // Activate debugger without event listeners.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000852 v8::Debug::MessageHandler2 message_handler_;
853 bool debugger_unload_pending_; // Was message handler cleared?
854 v8::Debug::HostDispatchHandler host_dispatch_handler_;
855 Mutex* dispatch_handler_access_; // Mutex guarding dispatch handler.
856 v8::Debug::DebugMessageDispatchHandler debug_message_dispatch_handler_;
857 MessageDispatchHelperThread* message_dispatch_helper_thread_;
858 int host_dispatch_micros_;
859
860 DebuggerAgent* agent_;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000861
ager@chromium.org41826e72009-03-30 13:30:57 +0000862 static const int kQueueInitialSize = 4;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000863 LockingCommandMessageQueue command_queue_;
864 Semaphore* command_received_; // Signaled for each command received.
865 LockingCommandMessageQueue event_command_queue_;
ager@chromium.org41826e72009-03-30 13:30:57 +0000866
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000867 Isolate* isolate_;
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +0000868
ager@chromium.org71daaf62009-04-01 07:22:49 +0000869 friend class EnterDebugger;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000870 friend class Isolate;
871
872 DISALLOW_COPY_AND_ASSIGN(Debugger);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000873};
874
875
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000876// This class is used for entering the debugger. Create an instance in the stack
877// to enter the debugger. This will set the current break state, make sure the
878// debugger is loaded and switch to the debugger context. If the debugger for
879// some reason could not be entered FailedToEnter will return true.
880class EnterDebugger BASE_EMBEDDED {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000881 public:
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000882 EnterDebugger();
883 ~EnterDebugger();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000884
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000885 // Check whether the debugger could be entered.
886 inline bool FailedToEnter() { return load_failed_; }
887
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000888 // Check whether there are any JavaScript frames on the stack.
ager@chromium.org8bb60582008-12-11 12:02:20 +0000889 inline bool HasJavaScriptFrames() { return has_js_frames_; }
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000890
ager@chromium.org9085a012009-05-11 19:22:57 +0000891 // Get the active context from before entering the debugger.
892 inline Handle<Context> GetContext() { return save_.context(); }
893
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000894 private:
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000895 Isolate* isolate_;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000896 EnterDebugger* prev_; // Previous debugger entry if entered recursively.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000897 JavaScriptFrameIterator it_;
ager@chromium.org8bb60582008-12-11 12:02:20 +0000898 const bool has_js_frames_; // Were there any JavaScript frames?
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000899 StackFrame::Id break_frame_id_; // Previous break frame id.
900 int break_id_; // Previous break id.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000901 bool load_failed_; // Did the debugger fail to load?
902 SaveContext save_; // Saves previous context.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000903};
904
905
kasper.lundbd3ec4e2008-07-09 11:06:54 +0000906// Stack allocated class for disabling break.
907class DisableBreak BASE_EMBEDDED {
908 public:
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000909 explicit DisableBreak(bool disable_break) : isolate_(Isolate::Current()) {
910 prev_disable_break_ = isolate_->debug()->disable_break();
911 isolate_->debug()->set_disable_break(disable_break);
kasper.lundbd3ec4e2008-07-09 11:06:54 +0000912 }
913 ~DisableBreak() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000914 ASSERT(Isolate::Current() == isolate_);
915 isolate_->debug()->set_disable_break(prev_disable_break_);
kasper.lundbd3ec4e2008-07-09 11:06:54 +0000916 }
917
918 private:
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000919 Isolate* isolate_;
kasper.lundbd3ec4e2008-07-09 11:06:54 +0000920 // The previous state of the disable break used to restore the value when this
921 // object is destructed.
922 bool prev_disable_break_;
923};
924
925
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000926// Debug_Address encapsulates the Address pointers used in generating debug
927// code.
928class Debug_Address {
929 public:
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000930 explicit Debug_Address(Debug::AddressId id) : id_(id) { }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000931
932 static Debug_Address AfterBreakTarget() {
933 return Debug_Address(Debug::k_after_break_target_address);
934 }
935
936 static Debug_Address DebugBreakReturn() {
937 return Debug_Address(Debug::k_debug_break_return_address);
938 }
939
whesse@chromium.orge90029b2010-08-02 11:52:17 +0000940 static Debug_Address RestarterFrameFunctionPointer() {
941 return Debug_Address(Debug::k_restarter_frame_function_pointer);
942 }
943
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000944 Address address(Isolate* isolate) const {
945 Debug* debug = isolate->debug();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000946 switch (id_) {
947 case Debug::k_after_break_target_address:
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000948 return reinterpret_cast<Address>(debug->after_break_target_address());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000949 case Debug::k_debug_break_return_address:
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000950 return reinterpret_cast<Address>(debug->debug_break_return_address());
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000951 case Debug::k_debug_break_slot_address:
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000952 return reinterpret_cast<Address>(debug->debug_break_slot_address());
whesse@chromium.orge90029b2010-08-02 11:52:17 +0000953 case Debug::k_restarter_frame_function_pointer:
954 return reinterpret_cast<Address>(
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000955 debug->restarter_frame_function_pointer_address());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000956 default:
957 UNREACHABLE();
958 return NULL;
959 }
960 }
kmillikin@chromium.org83e16822011-09-13 08:21:47 +0000961
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000962 private:
963 Debug::AddressId id_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000964};
965
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000966// The optional thread that Debug Agent may use to temporary call V8 to process
967// pending debug requests if debuggee is not running V8 at the moment.
968// Techincally it does not call V8 itself, rather it asks embedding program
969// to do this via v8::Debug::HostDispatchHandler
970class MessageDispatchHelperThread: public Thread {
971 public:
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000972 explicit MessageDispatchHelperThread(Isolate* isolate);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000973 ~MessageDispatchHelperThread();
974
975 void Schedule();
976
977 private:
978 void Run();
979
980 Semaphore* const sem_;
981 Mutex* const mutex_;
982 bool already_signalled_;
983
984 DISALLOW_COPY_AND_ASSIGN(MessageDispatchHelperThread);
985};
986
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000987
988} } // namespace v8::internal
989
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000990#endif // ENABLE_DEBUGGER_SUPPORT
991
ager@chromium.org5ec48922009-05-05 07:25:34 +0000992#endif // V8_DEBUG_H_