blob: c614844ab562b777c1d2963b1ed0e858b83df07b [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).
181 static uint32_t Hash(int key) { return ComputeIntegerHash(key); }
182
183 // Scripts match if their keys (script id) match.
184 static bool ScriptMatch(void* key1, void* key2) { return key1 == key2; }
185
186 // Clear the cache releasing all the weak handles.
187 void Clear();
188
189 // Weak handle callback for scripts in the cache.
190 static void HandleWeakScript(v8::Persistent<v8::Value> obj, void* data);
191
192 // List used during GC to temporarily store id's of collected scripts.
193 List<int> collected_scripts_;
194};
195
196
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000197// Linked list holding debug info objects. The debug info objects are kept as
198// weak handles to avoid a debug info object to keep a function alive.
199class DebugInfoListNode {
200 public:
201 explicit DebugInfoListNode(DebugInfo* debug_info);
202 virtual ~DebugInfoListNode();
203
204 DebugInfoListNode* next() { return next_; }
205 void set_next(DebugInfoListNode* next) { next_ = next; }
206 Handle<DebugInfo> debug_info() { return debug_info_; }
207
208 private:
209 // Global (weak) handle to the debug info object.
210 Handle<DebugInfo> debug_info_;
211
212 // Next pointer for linked list.
213 DebugInfoListNode* next_;
214};
215
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000216// This class contains the debugger support. The main purpose is to handle
217// setting break points in the code.
218//
219// This class controls the debug info for all functions which currently have
220// active breakpoints in them. This debug info is held in the heap root object
221// debug_info which is a FixedArray. Each entry in this list is of class
222// DebugInfo.
223class Debug {
224 public:
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000225 void Setup(bool create_heap_objects);
226 bool Load();
227 void Unload();
228 bool IsLoaded() { return !debug_context_.is_null(); }
229 bool InDebugger() { return thread_local_.debugger_entry_ != NULL; }
230 void PreemptionWhileInDebugger();
231 void Iterate(ObjectVisitor* v);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000232
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000233 Object* Break(Arguments args);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000234 void SetBreakPoint(Handle<SharedFunctionInfo> shared,
235 Handle<Object> break_point_object,
236 int* source_position);
237 void ClearBreakPoint(Handle<Object> break_point_object);
238 void ClearAllBreakPoints();
239 void FloodWithOneShot(Handle<SharedFunctionInfo> shared);
240 void FloodHandlerWithOneShot();
241 void ChangeBreakOnException(ExceptionBreakType type, bool enable);
242 bool IsBreakOnException(ExceptionBreakType type);
243 void PrepareStep(StepAction step_action, int step_count);
244 void ClearStepping();
245 bool StepNextContinue(BreakLocationIterator* break_location_iterator,
246 JavaScriptFrame* frame);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000247 static Handle<DebugInfo> GetDebugInfo(Handle<SharedFunctionInfo> shared);
248 static bool HasDebugInfo(Handle<SharedFunctionInfo> shared);
kasper.lundbd3ec4e2008-07-09 11:06:54 +0000249
ager@chromium.org32912102009-01-16 10:38:43 +0000250 // Returns whether the operation succeeded.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000251 bool EnsureDebugInfo(Handle<SharedFunctionInfo> shared);
kasper.lundbd3ec4e2008-07-09 11:06:54 +0000252
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000253 // Returns true if the current stub call is patched to call the debugger.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000254 static bool IsDebugBreak(Address addr);
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000255 // Returns true if the current return statement has been patched to be
256 // a debugger breakpoint.
ager@chromium.org381abbb2009-02-25 13:23:22 +0000257 static bool IsDebugBreakAtReturn(RelocInfo* rinfo);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000258
259 // Check whether a code stub with the specified major key is a possible break
260 // point location.
261 static bool IsSourceBreakStub(Code* code);
262 static bool IsBreakStub(Code* code);
263
264 // Find the builtin to use for invoking the debug break
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000265 static Handle<Code> FindDebugBreak(Handle<Code> code, RelocInfo::Mode mode);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000266
267 static Handle<Object> GetSourceBreakLocations(
268 Handle<SharedFunctionInfo> shared);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000269
270 // Getter for the debug_context.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000271 inline Handle<Context> debug_context() { return debug_context_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000272
273 // Check whether a global object is the debug global object.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000274 bool IsDebugGlobal(GlobalObject* global);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000275
ager@chromium.org2cc82ae2010-06-14 07:35:38 +0000276 // Check whether this frame is just about to return.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000277 bool IsBreakAtReturn(JavaScriptFrame* frame);
ager@chromium.org2cc82ae2010-06-14 07:35:38 +0000278
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000279 // Fast check to see if any break points are active.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000280 inline bool has_break_points() { return has_break_points_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000281
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000282 void NewBreak(StackFrame::Id break_frame_id);
283 void SetBreak(StackFrame::Id break_frame_id, int break_id);
284 StackFrame::Id break_frame_id() {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000285 return thread_local_.break_frame_id_;
286 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000287 int break_id() { return thread_local_.break_id_; }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000288
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000289 bool StepInActive() { return thread_local_.step_into_fp_ != 0; }
290 void HandleStepIn(Handle<JSFunction> function,
291 Handle<Object> holder,
292 Address fp,
293 bool is_constructor);
294 Address step_in_fp() { return thread_local_.step_into_fp_; }
295 Address* step_in_fp_addr() { return &thread_local_.step_into_fp_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000296
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000297 bool StepOutActive() { return thread_local_.step_out_fp_ != 0; }
298 Address step_out_fp() { return thread_local_.step_out_fp_; }
ager@chromium.orga1645e22009-09-09 19:27:10 +0000299
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000300 EnterDebugger* debugger_entry() {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000301 return thread_local_.debugger_entry_;
302 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000303 void set_debugger_entry(EnterDebugger* entry) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000304 thread_local_.debugger_entry_ = entry;
305 }
306
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000307 // Check whether any of the specified interrupts are pending.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000308 bool is_interrupt_pending(InterruptFlag what) {
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000309 return (thread_local_.pending_interrupts_ & what) != 0;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000310 }
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000311
312 // Set specified interrupts as pending.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000313 void set_interrupts_pending(InterruptFlag what) {
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000314 thread_local_.pending_interrupts_ |= what;
315 }
316
317 // Clear specified interrupts from pending.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000318 void clear_interrupt_pending(InterruptFlag what) {
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000319 thread_local_.pending_interrupts_ &= ~static_cast<int>(what);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000320 }
321
kasper.lundbd3ec4e2008-07-09 11:06:54 +0000322 // Getter and setter for the disable break state.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000323 bool disable_break() { return disable_break_; }
324 void set_disable_break(bool disable_break) {
kasper.lundbd3ec4e2008-07-09 11:06:54 +0000325 disable_break_ = disable_break;
326 }
327
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000328 // Getters for the current exception break state.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000329 bool break_on_exception() { return break_on_exception_; }
330 bool break_on_uncaught_exception() {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000331 return break_on_uncaught_exception_;
332 }
333
334 enum AddressId {
335 k_after_break_target_address,
336 k_debug_break_return_address,
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000337 k_debug_break_slot_address,
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000338 k_restarter_frame_function_pointer
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000339 };
340
341 // Support for setting the address to jump to when returning from break point.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000342 Address* after_break_target_address() {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000343 return reinterpret_cast<Address*>(&thread_local_.after_break_target_);
344 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000345 Address* restarter_frame_function_pointer_address() {
whesse@chromium.orge90029b2010-08-02 11:52:17 +0000346 Object*** address = &thread_local_.restarter_frame_function_pointer_;
347 return reinterpret_cast<Address*>(address);
348 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000349
350 // Support for saving/restoring registers when handling debug break calls.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000351 Object** register_address(int r) {
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000352 return &registers_[r];
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000353 }
354
ager@chromium.orga1645e22009-09-09 19:27:10 +0000355 // Access to the debug break on return code.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000356 Code* debug_break_return() { return debug_break_return_; }
357 Code** debug_break_return_address() {
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000358 return &debug_break_return_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000359 }
360
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000361 // Access to the debug break in debug break slot code.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000362 Code* debug_break_slot() { return debug_break_slot_; }
363 Code** debug_break_slot_address() {
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000364 return &debug_break_slot_;
365 }
366
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000367 static const int kEstimatedNofDebugInfoEntries = 16;
368 static const int kEstimatedNofBreakPointsInFunction = 16;
369
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000370 // Passed to MakeWeak.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000371 static void HandleWeakDebugInfo(v8::Persistent<v8::Value> obj, void* data);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000372
373 friend class Debugger;
ager@chromium.org381abbb2009-02-25 13:23:22 +0000374 friend Handle<FixedArray> GetDebuggedFunctions(); // In test-debug.cc
375 friend void CheckDebuggerUnloaded(bool check_functions); // In test-debug.cc
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000376
377 // Threading support.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000378 char* ArchiveDebug(char* to);
379 char* RestoreDebug(char* from);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000380 static int ArchiveSpacePerThread();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000381 void FreeThreadResources() { }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000382
ager@chromium.org32912102009-01-16 10:38:43 +0000383 // Mirror cache handling.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000384 void ClearMirrorCache();
ager@chromium.org32912102009-01-16 10:38:43 +0000385
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000386 // Script cache handling.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000387 void CreateScriptCache();
388 void DestroyScriptCache();
389 void AddScriptToScriptCache(Handle<Script> script);
390 Handle<FixedArray> GetLoadedScripts();
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000391
392 // Garbage collection notifications.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000393 void AfterGarbageCollection();
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000394
ager@chromium.org8bb60582008-12-11 12:02:20 +0000395 // Code generator routines.
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000396 static void GenerateSlot(MacroAssembler* masm);
ager@chromium.org8bb60582008-12-11 12:02:20 +0000397 static void GenerateLoadICDebugBreak(MacroAssembler* masm);
398 static void GenerateStoreICDebugBreak(MacroAssembler* masm);
399 static void GenerateKeyedLoadICDebugBreak(MacroAssembler* masm);
400 static void GenerateKeyedStoreICDebugBreak(MacroAssembler* masm);
401 static void GenerateConstructCallDebugBreak(MacroAssembler* masm);
402 static void GenerateReturnDebugBreak(MacroAssembler* masm);
ager@chromium.org8bb60582008-12-11 12:02:20 +0000403 static void GenerateStubNoRegistersDebugBreak(MacroAssembler* masm);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000404 static void GenerateSlotDebugBreak(MacroAssembler* masm);
ager@chromium.org357bf652010-04-12 11:30:10 +0000405 static void GeneratePlainReturnLiveEdit(MacroAssembler* masm);
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000406
407 // FrameDropper is a code replacement for a JavaScript frame with possibly
408 // several frames above.
409 // There is no calling conventions here, because it never actually gets
410 // called, it only gets returned to.
ager@chromium.org357bf652010-04-12 11:30:10 +0000411 static void GenerateFrameDropperLiveEdit(MacroAssembler* masm);
ager@chromium.org8bb60582008-12-11 12:02:20 +0000412
413 // Called from stub-cache.cc.
414 static void GenerateCallICDebugBreak(MacroAssembler* masm);
415
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000416 // Describes how exactly a frame has been dropped from stack.
417 enum FrameDropMode {
418 // No frame has been dropped.
419 FRAMES_UNTOUCHED,
420 // The top JS frame had been calling IC stub. IC stub mustn't be called now.
421 FRAME_DROPPED_IN_IC_CALL,
422 // The top JS frame had been calling debug break slot stub. Patch the
423 // address this stub jumps to in the end.
424 FRAME_DROPPED_IN_DEBUG_SLOT_CALL,
425 // The top JS frame had been calling some C++ function. The return address
426 // gets patched automatically.
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000427 FRAME_DROPPED_IN_DIRECT_CALL,
428 FRAME_DROPPED_IN_RETURN_CALL
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000429 };
430
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000431 void FramesHaveBeenDropped(StackFrame::Id new_break_frame_id,
whesse@chromium.orge90029b2010-08-02 11:52:17 +0000432 FrameDropMode mode,
433 Object** restarter_frame_function_pointer);
ager@chromium.org357bf652010-04-12 11:30:10 +0000434
whesse@chromium.orge90029b2010-08-02 11:52:17 +0000435 // Initializes an artificial stack frame. The data it contains is used for:
436 // a. successful work of frame dropper code which eventually gets control,
437 // b. being compatible with regular stack structure for various stack
438 // iterators.
439 // Returns address of stack allocated pointer to restarted function,
440 // the value that is called 'restarter_frame_function_pointer'. The value
441 // at this address (possibly updated by GC) may be used later when preparing
442 // 'step in' operation.
whesse@chromium.orge90029b2010-08-02 11:52:17 +0000443 static Object** SetUpFrameDropperFrame(StackFrame* bottom_js_frame,
444 Handle<Code> code);
445
ager@chromium.org357bf652010-04-12 11:30:10 +0000446 static const int kFrameDropperFrameSize;
447
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000448 // Architecture-specific constant.
449 static const bool kFrameDropperSupported;
450
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000451 private:
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000452 explicit Debug(Isolate* isolate);
453 ~Debug();
454
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000455 static bool CompileDebuggerScript(int index);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000456 void ClearOneShot();
457 void ActivateStepIn(StackFrame* frame);
458 void ClearStepIn();
459 void ActivateStepOut(StackFrame* frame);
460 void ClearStepOut();
461 void ClearStepNext();
ager@chromium.org32912102009-01-16 10:38:43 +0000462 // Returns whether the compile succeeded.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000463 void RemoveDebugInfo(Handle<DebugInfo> debug_info);
464 void SetAfterBreakTarget(JavaScriptFrame* frame);
465 Handle<Object> CheckBreakPoints(Handle<Object> break_point);
466 bool CheckBreakPoint(Handle<Object> break_point_object);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000467
468 // Global handle to debug context where all the debugger JavaScript code is
469 // loaded.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000470 Handle<Context> debug_context_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000471
472 // Boolean state indicating whether any break points are set.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000473 bool has_break_points_;
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000474
475 // Cache of all scripts in the heap.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000476 ScriptCache* script_cache_;
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000477
478 // List of active debug info objects.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000479 DebugInfoListNode* debug_info_list_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000480
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000481 bool disable_break_;
482 bool break_on_exception_;
483 bool break_on_uncaught_exception_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000484
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000485 // Per-thread data.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000486 class ThreadLocal {
487 public:
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000488 // Counter for generating next break id.
489 int break_count_;
490
491 // Current break id.
492 int break_id_;
493
494 // Frame id for the frame of the current break.
495 StackFrame::Id break_frame_id_;
496
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000497 // Step action for last step performed.
498 StepAction last_step_action_;
499
500 // Source statement position from last step next action.
501 int last_statement_position_;
502
503 // Number of steps left to perform before debug event.
504 int step_count_;
505
506 // Frame pointer from last step next action.
507 Address last_fp_;
508
509 // Frame pointer for frame from which step in was performed.
510 Address step_into_fp_;
511
ager@chromium.orga1645e22009-09-09 19:27:10 +0000512 // Frame pointer for the frame where debugger should be called when current
513 // step out action is completed.
514 Address step_out_fp_;
515
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000516 // Storage location for jump when exiting debug break calls.
517 Address after_break_target_;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000518
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000519 // Stores the way how LiveEdit has patched the stack. It is used when
520 // debugger returns control back to user script.
521 FrameDropMode frame_drop_mode_;
ager@chromium.org357bf652010-04-12 11:30:10 +0000522
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000523 // Top debugger entry.
524 EnterDebugger* debugger_entry_;
525
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000526 // Pending interrupts scheduled while debugging.
527 int pending_interrupts_;
whesse@chromium.orge90029b2010-08-02 11:52:17 +0000528
529 // When restarter frame is on stack, stores the address
530 // of the pointer to function being restarted. Otherwise (most of the time)
531 // stores NULL. This pointer is used with 'step in' implementation.
532 Object** restarter_frame_function_pointer_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000533 };
534
535 // Storage location for registers when handling debug break calls
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000536 JSCallerSavedBuffer registers_;
537 ThreadLocal thread_local_;
538 void ThreadInit();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000539
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000540 // Code to call for handling debug break on return.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000541 Code* debug_break_return_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000542
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000543 // Code to call for handling debug break in debug break slots.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000544 Code* debug_break_slot_;
545
546 Isolate* isolate_;
547
548 friend class Isolate;
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000549
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000550 DISALLOW_COPY_AND_ASSIGN(Debug);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000551};
552
553
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000554DECLARE_RUNTIME_FUNCTION(Object*, Debug_Break);
555
556
ager@chromium.org5ec48922009-05-05 07:25:34 +0000557// Message delivered to the message handler callback. This is either a debugger
558// event or the response to a command.
559class MessageImpl: public v8::Debug::Message {
560 public:
561 // Create a message object for a debug event.
562 static MessageImpl NewEvent(DebugEvent event,
563 bool running,
564 Handle<JSObject> exec_state,
565 Handle<JSObject> event_data);
566
567 // Create a message object for the response to a debug command.
568 static MessageImpl NewResponse(DebugEvent event,
569 bool running,
570 Handle<JSObject> exec_state,
571 Handle<JSObject> event_data,
572 Handle<String> response_json,
573 v8::Debug::ClientData* client_data);
574
575 // Implementation of interface v8::Debug::Message.
576 virtual bool IsEvent() const;
577 virtual bool IsResponse() const;
578 virtual DebugEvent GetEvent() const;
579 virtual bool WillStartRunning() const;
580 virtual v8::Handle<v8::Object> GetExecutionState() const;
581 virtual v8::Handle<v8::Object> GetEventData() const;
582 virtual v8::Handle<v8::String> GetJSON() const;
583 virtual v8::Handle<v8::Context> GetEventContext() const;
584 virtual v8::Debug::ClientData* GetClientData() const;
585
586 private:
587 MessageImpl(bool is_event,
588 DebugEvent event,
589 bool running,
590 Handle<JSObject> exec_state,
591 Handle<JSObject> event_data,
592 Handle<String> response_json,
593 v8::Debug::ClientData* client_data);
594
595 bool is_event_; // Does this message represent a debug event?
596 DebugEvent event_; // Debug event causing the break.
597 bool running_; // Will the VM start running after this event?
598 Handle<JSObject> exec_state_; // Current execution state.
599 Handle<JSObject> event_data_; // Data associated with the event.
600 Handle<String> response_json_; // Response JSON if message holds a response.
601 v8::Debug::ClientData* client_data_; // Client data passed with the request.
602};
603
604
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000605// Details of the debug event delivered to the debug event listener.
606class EventDetailsImpl : public v8::Debug::EventDetails {
607 public:
608 EventDetailsImpl(DebugEvent event,
609 Handle<JSObject> exec_state,
610 Handle<JSObject> event_data,
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +0000611 Handle<Object> callback_data,
612 v8::Debug::ClientData* client_data);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000613 virtual DebugEvent GetEvent() const;
614 virtual v8::Handle<v8::Object> GetExecutionState() const;
615 virtual v8::Handle<v8::Object> GetEventData() const;
616 virtual v8::Handle<v8::Context> GetEventContext() const;
617 virtual v8::Handle<v8::Value> GetCallbackData() const;
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +0000618 virtual v8::Debug::ClientData* GetClientData() const;
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000619 private:
620 DebugEvent event_; // Debug event causing the break.
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +0000621 Handle<JSObject> exec_state_; // Current execution state.
622 Handle<JSObject> event_data_; // Data associated with the event.
623 Handle<Object> callback_data_; // User data passed with the callback
624 // when it was registered.
625 v8::Debug::ClientData* client_data_; // Data passed to DebugBreakForCommand.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000626};
627
628
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000629// Message send by user to v8 debugger or debugger output message.
630// In addition to command text it may contain a pointer to some user data
631// which are expected to be passed along with the command reponse to message
632// handler.
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000633class CommandMessage {
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000634 public:
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000635 static CommandMessage New(const Vector<uint16_t>& command,
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000636 v8::Debug::ClientData* data);
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000637 CommandMessage();
638 ~CommandMessage();
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000639
640 // Deletes user data and disposes of the text.
641 void Dispose();
642 Vector<uint16_t> text() const { return text_; }
643 v8::Debug::ClientData* client_data() const { return client_data_; }
644 private:
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000645 CommandMessage(const Vector<uint16_t>& text,
646 v8::Debug::ClientData* data);
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000647
648 Vector<uint16_t> text_;
649 v8::Debug::ClientData* client_data_;
650};
651
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000652// A Queue of CommandMessage objects. A thread-safe version is
653// LockingCommandMessageQueue, based on this class.
654class CommandMessageQueue BASE_EMBEDDED {
ager@chromium.org41826e72009-03-30 13:30:57 +0000655 public:
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000656 explicit CommandMessageQueue(int size);
657 ~CommandMessageQueue();
ager@chromium.org41826e72009-03-30 13:30:57 +0000658 bool IsEmpty() const { return start_ == end_; }
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000659 CommandMessage Get();
660 void Put(const CommandMessage& message);
ager@chromium.org41826e72009-03-30 13:30:57 +0000661 void Clear() { start_ = end_ = 0; } // Queue is empty after Clear().
662 private:
663 // Doubles the size of the message queue, and copies the messages.
664 void Expand();
665
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000666 CommandMessage* messages_;
ager@chromium.org41826e72009-03-30 13:30:57 +0000667 int start_;
668 int end_;
669 int size_; // The size of the queue buffer. Queue can hold size-1 messages.
670};
671
672
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000673class MessageDispatchHelperThread;
674
675
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000676// LockingCommandMessageQueue is a thread-safe circular buffer of CommandMessage
677// messages. The message data is not managed by LockingCommandMessageQueue.
ager@chromium.org41826e72009-03-30 13:30:57 +0000678// Pointers to the data are passed in and out. Implemented by adding a
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000679// Mutex to CommandMessageQueue. Includes logging of all puts and gets.
680class LockingCommandMessageQueue BASE_EMBEDDED {
ager@chromium.org41826e72009-03-30 13:30:57 +0000681 public:
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000682 LockingCommandMessageQueue(Logger* logger, int size);
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000683 ~LockingCommandMessageQueue();
ager@chromium.org41826e72009-03-30 13:30:57 +0000684 bool IsEmpty() const;
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000685 CommandMessage Get();
686 void Put(const CommandMessage& message);
ager@chromium.org41826e72009-03-30 13:30:57 +0000687 void Clear();
688 private:
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000689 Logger* logger_;
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000690 CommandMessageQueue queue_;
ager@chromium.org41826e72009-03-30 13:30:57 +0000691 Mutex* lock_;
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000692 DISALLOW_COPY_AND_ASSIGN(LockingCommandMessageQueue);
ager@chromium.org41826e72009-03-30 13:30:57 +0000693};
694
695
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000696class Debugger {
697 public:
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000698 ~Debugger();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000699
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000700 void DebugRequest(const uint16_t* json_request, int length);
701
702 Handle<Object> MakeJSObject(Vector<const char> constructor_name,
703 int argc, Object*** argv,
704 bool* caught_exception);
705 Handle<Object> MakeExecutionState(bool* caught_exception);
706 Handle<Object> MakeBreakEvent(Handle<Object> exec_state,
707 Handle<Object> break_points_hit,
708 bool* caught_exception);
709 Handle<Object> MakeExceptionEvent(Handle<Object> exec_state,
710 Handle<Object> exception,
711 bool uncaught,
712 bool* caught_exception);
713 Handle<Object> MakeNewFunctionEvent(Handle<Object> func,
714 bool* caught_exception);
715 Handle<Object> MakeCompileEvent(Handle<Script> script,
716 bool before,
717 bool* caught_exception);
718 Handle<Object> MakeScriptCollectedEvent(int id,
719 bool* caught_exception);
720 void OnDebugBreak(Handle<Object> break_points_hit, bool auto_continue);
721 void OnException(Handle<Object> exception, bool uncaught);
722 void OnBeforeCompile(Handle<Script> script);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000723
724 enum AfterCompileFlags {
725 NO_AFTER_COMPILE_FLAGS,
726 SEND_WHEN_DEBUGGING
727 };
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000728 void OnAfterCompile(Handle<Script> script,
729 AfterCompileFlags after_compile_flags);
730 void OnNewFunction(Handle<JSFunction> fun);
731 void OnScriptCollected(int id);
732 void ProcessDebugEvent(v8::DebugEvent event,
733 Handle<JSObject> event_data,
734 bool auto_continue);
735 void NotifyMessageHandler(v8::DebugEvent event,
736 Handle<JSObject> exec_state,
737 Handle<JSObject> event_data,
738 bool auto_continue);
739 void SetEventListener(Handle<Object> callback, Handle<Object> data);
740 void SetMessageHandler(v8::Debug::MessageHandler2 handler);
741 void SetHostDispatchHandler(v8::Debug::HostDispatchHandler handler,
742 int period);
743 void SetDebugMessageDispatchHandler(
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000744 v8::Debug::DebugMessageDispatchHandler handler,
745 bool provide_locker);
ager@chromium.org41826e72009-03-30 13:30:57 +0000746
747 // Invoke the message handler function.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000748 void InvokeMessageHandler(MessageImpl message);
ager@chromium.org41826e72009-03-30 13:30:57 +0000749
750 // Add a debugger command to the command queue.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000751 void ProcessCommand(Vector<const uint16_t> command,
752 v8::Debug::ClientData* client_data = NULL);
ager@chromium.org41826e72009-03-30 13:30:57 +0000753
754 // Check whether there are commands in the command queue.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000755 bool HasCommands();
ager@chromium.org41826e72009-03-30 13:30:57 +0000756
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +0000757 // Enqueue a debugger command to the command queue for event listeners.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000758 void EnqueueDebugCommand(v8::Debug::ClientData* client_data = NULL);
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +0000759
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000760 Handle<Object> Call(Handle<JSFunction> fun,
761 Handle<Object> data,
762 bool* pending_exception);
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000763
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000764 // Start the debugger agent listening on the provided port.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000765 bool StartAgent(const char* name, int port,
766 bool wait_for_connection = false);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000767
768 // Stop the debugger agent.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000769 void StopAgent();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000770
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000771 // Blocks until the agent has started listening for connections
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000772 void WaitForAgent();
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000773
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000774 void CallMessageDispatchHandler();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000775
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000776 Handle<Context> GetDebugContext();
lrn@chromium.orgc34f5802010-04-28 12:53:43 +0000777
ager@chromium.org71daaf62009-04-01 07:22:49 +0000778 // Unload the debugger if possible. Only called when no debugger is currently
779 // active.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000780 void UnloadDebugger();
lrn@chromium.orgc34f5802010-04-28 12:53:43 +0000781 friend void ForceUnloadDebugger(); // In test-debug.cc
ager@chromium.org71daaf62009-04-01 07:22:49 +0000782
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000783 inline bool EventActive(v8::DebugEvent event) {
ager@chromium.org71daaf62009-04-01 07:22:49 +0000784 ScopedLock with(debugger_access_);
785
786 // Check whether the message handler was been cleared.
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000787 if (debugger_unload_pending_) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000788 if (isolate_->debug()->debugger_entry() == NULL) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000789 UnloadDebugger();
790 }
ager@chromium.org71daaf62009-04-01 07:22:49 +0000791 }
792
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +0000793 if (((event == v8::BeforeCompile) || (event == v8::AfterCompile)) &&
794 !FLAG_debug_compile_events) {
795 return false;
796
797 } else if ((event == v8::ScriptCollected) &&
798 !FLAG_debug_script_collected_events) {
799 return false;
800 }
801
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000802 // Currently argument event is not used.
ager@chromium.org71daaf62009-04-01 07:22:49 +0000803 return !compiling_natives_ && Debugger::IsDebuggerActive();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000804 }
805
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000806 void set_compiling_natives(bool compiling_natives) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000807 Debugger::compiling_natives_ = compiling_natives;
808 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000809 bool compiling_natives() const { return compiling_natives_; }
810 void set_loading_debugger(bool v) { is_loading_debugger_ = v; }
811 bool is_loading_debugger() const { return is_loading_debugger_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000812
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000813 bool IsDebuggerActive();
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000814
815 private:
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000816 explicit Debugger(Isolate* isolate);
ager@chromium.org71daaf62009-04-01 07:22:49 +0000817
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000818 void CallEventCallback(v8::DebugEvent event,
819 Handle<Object> exec_state,
820 Handle<Object> event_data,
821 v8::Debug::ClientData* client_data);
822 void CallCEventCallback(v8::DebugEvent event,
823 Handle<Object> exec_state,
824 Handle<Object> event_data,
825 v8::Debug::ClientData* client_data);
826 void CallJSEventCallback(v8::DebugEvent event,
827 Handle<Object> exec_state,
828 Handle<Object> event_data);
829 void ListenersChanged();
ager@chromium.org381abbb2009-02-25 13:23:22 +0000830
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000831 Mutex* debugger_access_; // Mutex guarding debugger variables.
832 Handle<Object> event_listener_; // Global handle to listener.
833 Handle<Object> event_listener_data_;
834 bool compiling_natives_; // Are we compiling natives?
835 bool is_loading_debugger_; // Are we loading the debugger?
836 bool never_unload_debugger_; // Can we unload the debugger?
837 v8::Debug::MessageHandler2 message_handler_;
838 bool debugger_unload_pending_; // Was message handler cleared?
839 v8::Debug::HostDispatchHandler host_dispatch_handler_;
840 Mutex* dispatch_handler_access_; // Mutex guarding dispatch handler.
841 v8::Debug::DebugMessageDispatchHandler debug_message_dispatch_handler_;
842 MessageDispatchHelperThread* message_dispatch_helper_thread_;
843 int host_dispatch_micros_;
844
845 DebuggerAgent* agent_;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000846
ager@chromium.org41826e72009-03-30 13:30:57 +0000847 static const int kQueueInitialSize = 4;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000848 LockingCommandMessageQueue command_queue_;
849 Semaphore* command_received_; // Signaled for each command received.
850 LockingCommandMessageQueue event_command_queue_;
ager@chromium.org41826e72009-03-30 13:30:57 +0000851
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000852 Isolate* isolate_;
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +0000853
ager@chromium.org71daaf62009-04-01 07:22:49 +0000854 friend class EnterDebugger;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000855 friend class Isolate;
856
857 DISALLOW_COPY_AND_ASSIGN(Debugger);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000858};
859
860
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000861// This class is used for entering the debugger. Create an instance in the stack
862// to enter the debugger. This will set the current break state, make sure the
863// debugger is loaded and switch to the debugger context. If the debugger for
864// some reason could not be entered FailedToEnter will return true.
865class EnterDebugger BASE_EMBEDDED {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000866 public:
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000867 EnterDebugger()
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000868 : isolate_(Isolate::Current()),
869 prev_(isolate_->debug()->debugger_entry()),
vegorov@chromium.org74f333b2011-04-06 11:17:46 +0000870 it_(isolate_),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000871 has_js_frames_(!it_.done()),
872 save_(isolate_) {
873 Debug* debug = isolate_->debug();
874 ASSERT(prev_ != NULL || !debug->is_interrupt_pending(PREEMPT));
875 ASSERT(prev_ != NULL || !debug->is_interrupt_pending(DEBUGBREAK));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000876
877 // Link recursive debugger entry.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000878 debug->set_debugger_entry(this);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000879
ager@chromium.org8bb60582008-12-11 12:02:20 +0000880 // Store the previous break id and frame id.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000881 break_id_ = debug->break_id();
882 break_frame_id_ = debug->break_frame_id();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000883
ager@chromium.org8bb60582008-12-11 12:02:20 +0000884 // Create the new break info. If there is no JavaScript frames there is no
885 // break frame id.
886 if (has_js_frames_) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000887 debug->NewBreak(it_.frame()->id());
ager@chromium.org8bb60582008-12-11 12:02:20 +0000888 } else {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000889 debug->NewBreak(StackFrame::NO_ID);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000890 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000891
892 // Make sure that debugger is loaded and enter the debugger context.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000893 load_failed_ = !debug->Load();
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000894 if (!load_failed_) {
895 // NOTE the member variable save which saves the previous context before
896 // this change.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000897 isolate_->set_context(*debug->debug_context());
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000898 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000899 }
900
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000901 ~EnterDebugger() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000902 ASSERT(Isolate::Current() == isolate_);
903 Debug* debug = isolate_->debug();
904
ager@chromium.org8bb60582008-12-11 12:02:20 +0000905 // Restore to the previous break state.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000906 debug->SetBreak(break_frame_id_, break_id_);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000907
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000908 // Check for leaving the debugger.
ager@chromium.org71daaf62009-04-01 07:22:49 +0000909 if (prev_ == NULL) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000910 // Clear mirror cache when leaving the debugger. Skip this if there is a
911 // pending exception as clearing the mirror cache calls back into
912 // JavaScript. This can happen if the v8::Debug::Call is used in which
913 // case the exception should end up in the calling code.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000914 if (!isolate_->has_pending_exception()) {
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000915 // Try to avoid any pending debug break breaking in the clear mirror
916 // cache JavaScript code.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000917 if (isolate_->stack_guard()->IsDebugBreak()) {
918 debug->set_interrupts_pending(DEBUGBREAK);
919 isolate_->stack_guard()->Continue(DEBUGBREAK);
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000920 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000921 debug->ClearMirrorCache();
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000922 }
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000923
924 // Request preemption and debug break when leaving the last debugger entry
925 // if any of these where recorded while debugging.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000926 if (debug->is_interrupt_pending(PREEMPT)) {
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000927 // This re-scheduling of preemption is to avoid starvation in some
928 // debugging scenarios.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000929 debug->clear_interrupt_pending(PREEMPT);
930 isolate_->stack_guard()->Preempt();
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000931 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000932 if (debug->is_interrupt_pending(DEBUGBREAK)) {
933 debug->clear_interrupt_pending(DEBUGBREAK);
934 isolate_->stack_guard()->DebugBreak();
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000935 }
936
937 // If there are commands in the queue when leaving the debugger request
938 // that these commands are processed.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000939 if (isolate_->debugger()->HasCommands()) {
940 isolate_->stack_guard()->DebugCommand();
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000941 }
942
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000943 // If leaving the debugger with the debugger no longer active unload it.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000944 if (!isolate_->debugger()->IsDebuggerActive()) {
945 isolate_->debugger()->UnloadDebugger();
ager@chromium.org71daaf62009-04-01 07:22:49 +0000946 }
947 }
948
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000949 // Leaving this debugger entry.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000950 debug->set_debugger_entry(prev_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000951 }
952
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000953 // Check whether the debugger could be entered.
954 inline bool FailedToEnter() { return load_failed_; }
955
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000956 // Check whether there are any JavaScript frames on the stack.
ager@chromium.org8bb60582008-12-11 12:02:20 +0000957 inline bool HasJavaScriptFrames() { return has_js_frames_; }
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000958
ager@chromium.org9085a012009-05-11 19:22:57 +0000959 // Get the active context from before entering the debugger.
960 inline Handle<Context> GetContext() { return save_.context(); }
961
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000962 private:
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000963 Isolate* isolate_;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000964 EnterDebugger* prev_; // Previous debugger entry if entered recursively.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000965 JavaScriptFrameIterator it_;
ager@chromium.org8bb60582008-12-11 12:02:20 +0000966 const bool has_js_frames_; // Were there any JavaScript frames?
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000967 StackFrame::Id break_frame_id_; // Previous break frame id.
968 int break_id_; // Previous break id.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000969 bool load_failed_; // Did the debugger fail to load?
970 SaveContext save_; // Saves previous context.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000971};
972
973
kasper.lundbd3ec4e2008-07-09 11:06:54 +0000974// Stack allocated class for disabling break.
975class DisableBreak BASE_EMBEDDED {
976 public:
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000977 explicit DisableBreak(bool disable_break) : isolate_(Isolate::Current()) {
978 prev_disable_break_ = isolate_->debug()->disable_break();
979 isolate_->debug()->set_disable_break(disable_break);
kasper.lundbd3ec4e2008-07-09 11:06:54 +0000980 }
981 ~DisableBreak() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000982 ASSERT(Isolate::Current() == isolate_);
983 isolate_->debug()->set_disable_break(prev_disable_break_);
kasper.lundbd3ec4e2008-07-09 11:06:54 +0000984 }
985
986 private:
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000987 Isolate* isolate_;
kasper.lundbd3ec4e2008-07-09 11:06:54 +0000988 // The previous state of the disable break used to restore the value when this
989 // object is destructed.
990 bool prev_disable_break_;
991};
992
993
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000994// Debug_Address encapsulates the Address pointers used in generating debug
995// code.
996class Debug_Address {
997 public:
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000998 explicit Debug_Address(Debug::AddressId id) : id_(id) { }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000999
1000 static Debug_Address AfterBreakTarget() {
1001 return Debug_Address(Debug::k_after_break_target_address);
1002 }
1003
1004 static Debug_Address DebugBreakReturn() {
1005 return Debug_Address(Debug::k_debug_break_return_address);
1006 }
1007
whesse@chromium.orge90029b2010-08-02 11:52:17 +00001008 static Debug_Address RestarterFrameFunctionPointer() {
1009 return Debug_Address(Debug::k_restarter_frame_function_pointer);
1010 }
1011
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001012 Address address(Isolate* isolate) const {
1013 Debug* debug = isolate->debug();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001014 switch (id_) {
1015 case Debug::k_after_break_target_address:
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001016 return reinterpret_cast<Address>(debug->after_break_target_address());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001017 case Debug::k_debug_break_return_address:
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001018 return reinterpret_cast<Address>(debug->debug_break_return_address());
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001019 case Debug::k_debug_break_slot_address:
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001020 return reinterpret_cast<Address>(debug->debug_break_slot_address());
whesse@chromium.orge90029b2010-08-02 11:52:17 +00001021 case Debug::k_restarter_frame_function_pointer:
1022 return reinterpret_cast<Address>(
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001023 debug->restarter_frame_function_pointer_address());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001024 default:
1025 UNREACHABLE();
1026 return NULL;
1027 }
1028 }
1029 private:
1030 Debug::AddressId id_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001031};
1032
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001033// The optional thread that Debug Agent may use to temporary call V8 to process
1034// pending debug requests if debuggee is not running V8 at the moment.
1035// Techincally it does not call V8 itself, rather it asks embedding program
1036// to do this via v8::Debug::HostDispatchHandler
1037class MessageDispatchHelperThread: public Thread {
1038 public:
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001039 explicit MessageDispatchHelperThread(Isolate* isolate);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001040 ~MessageDispatchHelperThread();
1041
1042 void Schedule();
1043
1044 private:
1045 void Run();
1046
1047 Semaphore* const sem_;
1048 Mutex* const mutex_;
1049 bool already_signalled_;
1050
1051 DISALLOW_COPY_AND_ASSIGN(MessageDispatchHelperThread);
1052};
1053
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001054
1055} } // namespace v8::internal
1056
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001057#endif // ENABLE_DEBUGGER_SUPPORT
1058
ager@chromium.org5ec48922009-05-05 07:25:34 +00001059#endif // V8_DEBUG_H_