blob: bb804206cdd2a8626c22858eac05520448eb0fbe [file] [log] [blame]
mstarzinger@chromium.org88d326b2012-04-23 12:57:22 +00001// Copyright 2012 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);
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000236 void SetBreakPoint(Handle<JSFunction> function,
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000237 Handle<Object> break_point_object,
238 int* source_position);
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000239 bool SetBreakPointForScript(Handle<Script> script,
240 Handle<Object> break_point_object,
241 int* source_position);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000242 void ClearBreakPoint(Handle<Object> break_point_object);
243 void ClearAllBreakPoints();
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000244 void FloodWithOneShot(Handle<JSFunction> function);
rossberg@chromium.org2c067b12012-03-19 11:01:52 +0000245 void FloodBoundFunctionWithOneShot(Handle<JSFunction> function);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000246 void FloodHandlerWithOneShot();
247 void ChangeBreakOnException(ExceptionBreakType type, bool enable);
248 bool IsBreakOnException(ExceptionBreakType type);
249 void PrepareStep(StepAction step_action, int step_count);
250 void ClearStepping();
mstarzinger@chromium.org88d326b2012-04-23 12:57:22 +0000251 void ClearStepOut();
252 bool IsStepping() { return thread_local_.step_count_ > 0; }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000253 bool StepNextContinue(BreakLocationIterator* break_location_iterator,
254 JavaScriptFrame* frame);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000255 static Handle<DebugInfo> GetDebugInfo(Handle<SharedFunctionInfo> shared);
256 static bool HasDebugInfo(Handle<SharedFunctionInfo> shared);
kasper.lundbd3ec4e2008-07-09 11:06:54 +0000257
lrn@chromium.org34e60782011-09-15 07:25:40 +0000258 void PrepareForBreakPoints();
259
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000260 // Returns whether the operation succeeded. Compilation can only be triggered
261 // if a valid closure is passed as the second argument, otherwise the shared
262 // function needs to be compiled already.
263 bool EnsureDebugInfo(Handle<SharedFunctionInfo> shared,
264 Handle<JSFunction> function);
kasper.lundbd3ec4e2008-07-09 11:06:54 +0000265
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000266 // Returns true if the current stub call is patched to call the debugger.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000267 static bool IsDebugBreak(Address addr);
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000268 // Returns true if the current return statement has been patched to be
269 // a debugger breakpoint.
ager@chromium.org381abbb2009-02-25 13:23:22 +0000270 static bool IsDebugBreakAtReturn(RelocInfo* rinfo);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000271
272 // Check whether a code stub with the specified major key is a possible break
273 // point location.
274 static bool IsSourceBreakStub(Code* code);
275 static bool IsBreakStub(Code* code);
276
277 // Find the builtin to use for invoking the debug break
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000278 static Handle<Code> FindDebugBreak(Handle<Code> code, RelocInfo::Mode mode);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000279
280 static Handle<Object> GetSourceBreakLocations(
281 Handle<SharedFunctionInfo> shared);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000282
283 // Getter for the debug_context.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000284 inline Handle<Context> debug_context() { return debug_context_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000285
286 // Check whether a global object is the debug global object.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000287 bool IsDebugGlobal(GlobalObject* global);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000288
ager@chromium.org2cc82ae2010-06-14 07:35:38 +0000289 // Check whether this frame is just about to return.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000290 bool IsBreakAtReturn(JavaScriptFrame* frame);
ager@chromium.org2cc82ae2010-06-14 07:35:38 +0000291
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000292 // Fast check to see if any break points are active.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000293 inline bool has_break_points() { return has_break_points_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000294
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000295 void NewBreak(StackFrame::Id break_frame_id);
296 void SetBreak(StackFrame::Id break_frame_id, int break_id);
297 StackFrame::Id break_frame_id() {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000298 return thread_local_.break_frame_id_;
299 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000300 int break_id() { return thread_local_.break_id_; }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000301
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000302 bool StepInActive() { return thread_local_.step_into_fp_ != 0; }
303 void HandleStepIn(Handle<JSFunction> function,
304 Handle<Object> holder,
305 Address fp,
306 bool is_constructor);
307 Address step_in_fp() { return thread_local_.step_into_fp_; }
308 Address* step_in_fp_addr() { return &thread_local_.step_into_fp_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000309
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000310 bool StepOutActive() { return thread_local_.step_out_fp_ != 0; }
311 Address step_out_fp() { return thread_local_.step_out_fp_; }
ager@chromium.orga1645e22009-09-09 19:27:10 +0000312
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000313 EnterDebugger* debugger_entry() {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000314 return thread_local_.debugger_entry_;
315 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000316 void set_debugger_entry(EnterDebugger* entry) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000317 thread_local_.debugger_entry_ = entry;
318 }
319
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000320 // Check whether any of the specified interrupts are pending.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000321 bool is_interrupt_pending(InterruptFlag what) {
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000322 return (thread_local_.pending_interrupts_ & what) != 0;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000323 }
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000324
325 // Set specified interrupts as pending.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000326 void set_interrupts_pending(InterruptFlag what) {
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000327 thread_local_.pending_interrupts_ |= what;
328 }
329
330 // Clear specified interrupts from pending.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000331 void clear_interrupt_pending(InterruptFlag what) {
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000332 thread_local_.pending_interrupts_ &= ~static_cast<int>(what);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000333 }
334
kasper.lundbd3ec4e2008-07-09 11:06:54 +0000335 // Getter and setter for the disable break state.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000336 bool disable_break() { return disable_break_; }
337 void set_disable_break(bool disable_break) {
kasper.lundbd3ec4e2008-07-09 11:06:54 +0000338 disable_break_ = disable_break;
339 }
340
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000341 // Getters for the current exception break state.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000342 bool break_on_exception() { return break_on_exception_; }
343 bool break_on_uncaught_exception() {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000344 return break_on_uncaught_exception_;
345 }
346
347 enum AddressId {
348 k_after_break_target_address,
349 k_debug_break_return_address,
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000350 k_debug_break_slot_address,
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000351 k_restarter_frame_function_pointer
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000352 };
353
354 // Support for setting the address to jump to when returning from break point.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000355 Address* after_break_target_address() {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000356 return reinterpret_cast<Address*>(&thread_local_.after_break_target_);
357 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000358 Address* restarter_frame_function_pointer_address() {
whesse@chromium.orge90029b2010-08-02 11:52:17 +0000359 Object*** address = &thread_local_.restarter_frame_function_pointer_;
360 return reinterpret_cast<Address*>(address);
361 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000362
363 // Support for saving/restoring registers when handling debug break calls.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000364 Object** register_address(int r) {
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000365 return &registers_[r];
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000366 }
367
ager@chromium.orga1645e22009-09-09 19:27:10 +0000368 // Access to the debug break on return code.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000369 Code* debug_break_return() { return debug_break_return_; }
370 Code** debug_break_return_address() {
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000371 return &debug_break_return_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000372 }
373
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000374 // Access to the debug break in debug break slot code.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000375 Code* debug_break_slot() { return debug_break_slot_; }
376 Code** debug_break_slot_address() {
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000377 return &debug_break_slot_;
378 }
379
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000380 static const int kEstimatedNofDebugInfoEntries = 16;
381 static const int kEstimatedNofBreakPointsInFunction = 16;
382
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000383 // Passed to MakeWeak.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000384 static void HandleWeakDebugInfo(v8::Persistent<v8::Value> obj, void* data);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000385
386 friend class Debugger;
ager@chromium.org381abbb2009-02-25 13:23:22 +0000387 friend Handle<FixedArray> GetDebuggedFunctions(); // In test-debug.cc
388 friend void CheckDebuggerUnloaded(bool check_functions); // In test-debug.cc
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000389
390 // Threading support.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000391 char* ArchiveDebug(char* to);
392 char* RestoreDebug(char* from);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000393 static int ArchiveSpacePerThread();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000394 void FreeThreadResources() { }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000395
ager@chromium.org32912102009-01-16 10:38:43 +0000396 // Mirror cache handling.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000397 void ClearMirrorCache();
ager@chromium.org32912102009-01-16 10:38:43 +0000398
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000399 // Script cache handling.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000400 void CreateScriptCache();
401 void DestroyScriptCache();
402 void AddScriptToScriptCache(Handle<Script> script);
403 Handle<FixedArray> GetLoadedScripts();
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000404
405 // Garbage collection notifications.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000406 void AfterGarbageCollection();
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000407
ager@chromium.org8bb60582008-12-11 12:02:20 +0000408 // Code generator routines.
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000409 static void GenerateSlot(MacroAssembler* masm);
ager@chromium.org8bb60582008-12-11 12:02:20 +0000410 static void GenerateLoadICDebugBreak(MacroAssembler* masm);
411 static void GenerateStoreICDebugBreak(MacroAssembler* masm);
412 static void GenerateKeyedLoadICDebugBreak(MacroAssembler* masm);
413 static void GenerateKeyedStoreICDebugBreak(MacroAssembler* masm);
ager@chromium.org8bb60582008-12-11 12:02:20 +0000414 static void GenerateReturnDebugBreak(MacroAssembler* masm);
danno@chromium.orgc612e022011-11-10 11:38:15 +0000415 static void GenerateCallFunctionStubDebugBreak(MacroAssembler* masm);
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000416 static void GenerateCallFunctionStubRecordDebugBreak(MacroAssembler* masm);
417 static void GenerateCallConstructStubDebugBreak(MacroAssembler* masm);
418 static void GenerateCallConstructStubRecordDebugBreak(MacroAssembler* masm);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000419 static void GenerateSlotDebugBreak(MacroAssembler* masm);
ager@chromium.org357bf652010-04-12 11:30:10 +0000420 static void GeneratePlainReturnLiveEdit(MacroAssembler* masm);
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000421
422 // FrameDropper is a code replacement for a JavaScript frame with possibly
423 // several frames above.
424 // There is no calling conventions here, because it never actually gets
425 // called, it only gets returned to.
ager@chromium.org357bf652010-04-12 11:30:10 +0000426 static void GenerateFrameDropperLiveEdit(MacroAssembler* masm);
ager@chromium.org8bb60582008-12-11 12:02:20 +0000427
428 // Called from stub-cache.cc.
429 static void GenerateCallICDebugBreak(MacroAssembler* masm);
430
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000431 // Describes how exactly a frame has been dropped from stack.
432 enum FrameDropMode {
433 // No frame has been dropped.
434 FRAMES_UNTOUCHED,
435 // The top JS frame had been calling IC stub. IC stub mustn't be called now.
436 FRAME_DROPPED_IN_IC_CALL,
437 // The top JS frame had been calling debug break slot stub. Patch the
438 // address this stub jumps to in the end.
439 FRAME_DROPPED_IN_DEBUG_SLOT_CALL,
440 // The top JS frame had been calling some C++ function. The return address
441 // gets patched automatically.
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000442 FRAME_DROPPED_IN_DIRECT_CALL,
ulan@chromium.orgd9e468a2012-06-25 09:47:40 +0000443 FRAME_DROPPED_IN_RETURN_CALL,
444 CURRENTLY_SET_MODE
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000445 };
446
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000447 void FramesHaveBeenDropped(StackFrame::Id new_break_frame_id,
whesse@chromium.orge90029b2010-08-02 11:52:17 +0000448 FrameDropMode mode,
449 Object** restarter_frame_function_pointer);
ager@chromium.org357bf652010-04-12 11:30:10 +0000450
whesse@chromium.orge90029b2010-08-02 11:52:17 +0000451 // Initializes an artificial stack frame. The data it contains is used for:
452 // a. successful work of frame dropper code which eventually gets control,
453 // b. being compatible with regular stack structure for various stack
454 // iterators.
455 // Returns address of stack allocated pointer to restarted function,
456 // the value that is called 'restarter_frame_function_pointer'. The value
457 // at this address (possibly updated by GC) may be used later when preparing
458 // 'step in' operation.
whesse@chromium.orge90029b2010-08-02 11:52:17 +0000459 static Object** SetUpFrameDropperFrame(StackFrame* bottom_js_frame,
460 Handle<Code> code);
461
ager@chromium.org357bf652010-04-12 11:30:10 +0000462 static const int kFrameDropperFrameSize;
463
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000464 // Architecture-specific constant.
465 static const bool kFrameDropperSupported;
466
jkummerow@chromium.org212d9642012-05-11 15:02:09 +0000467 /**
468 * Defines layout of a stack frame that supports padding. This is a regular
469 * internal frame that has a flexible stack structure. LiveEdit can shift
470 * its lower part up the stack, taking up the 'padding' space when additional
471 * stack memory is required.
472 * Such frame is expected immediately above the topmost JavaScript frame.
473 *
474 * Stack Layout:
475 * --- Top
476 * LiveEdit routine frames
477 * ---
478 * C frames of debug handler
479 * ---
480 * ...
481 * ---
482 * An internal frame that has n padding words:
483 * - any number of words as needed by code -- upper part of frame
484 * - padding size: a Smi storing n -- current size of padding
485 * - padding: n words filled with kPaddingValue in form of Smi
486 * - 3 context/type words of a regular InternalFrame
487 * - fp
488 * ---
489 * Topmost JavaScript frame
490 * ---
491 * ...
492 * --- Bottom
493 */
494 class FramePaddingLayout : public AllStatic {
495 public:
496 // Architecture-specific constant.
497 static const bool kIsSupported;
498
499 // A size of frame base including fp. Padding words starts right above
500 // the base.
501 static const int kFrameBaseSize = 4;
502
503 // A number of words that should be reserved on stack for the LiveEdit use.
504 // Normally equals 1. Stored on stack in form of Smi.
505 static const int kInitialSize;
506 // A value that padding words are filled with (in form of Smi). Going
507 // bottom-top, the first word not having this value is a counter word.
508 static const int kPaddingValue;
509 };
510
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000511 private:
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000512 explicit Debug(Isolate* isolate);
513 ~Debug();
514
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000515 static bool CompileDebuggerScript(int index);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000516 void ClearOneShot();
517 void ActivateStepIn(StackFrame* frame);
518 void ClearStepIn();
519 void ActivateStepOut(StackFrame* frame);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000520 void ClearStepNext();
ager@chromium.org32912102009-01-16 10:38:43 +0000521 // Returns whether the compile succeeded.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000522 void RemoveDebugInfo(Handle<DebugInfo> debug_info);
523 void SetAfterBreakTarget(JavaScriptFrame* frame);
524 Handle<Object> CheckBreakPoints(Handle<Object> break_point);
525 bool CheckBreakPoint(Handle<Object> break_point_object);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000526
527 // Global handle to debug context where all the debugger JavaScript code is
528 // loaded.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000529 Handle<Context> debug_context_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000530
531 // Boolean state indicating whether any break points are set.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000532 bool has_break_points_;
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000533
534 // Cache of all scripts in the heap.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000535 ScriptCache* script_cache_;
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000536
537 // List of active debug info objects.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000538 DebugInfoListNode* debug_info_list_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000539
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000540 bool disable_break_;
541 bool break_on_exception_;
542 bool break_on_uncaught_exception_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000543
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000544 // Per-thread data.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000545 class ThreadLocal {
546 public:
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000547 // Counter for generating next break id.
548 int break_count_;
549
550 // Current break id.
551 int break_id_;
552
553 // Frame id for the frame of the current break.
554 StackFrame::Id break_frame_id_;
555
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000556 // Step action for last step performed.
557 StepAction last_step_action_;
558
559 // Source statement position from last step next action.
560 int last_statement_position_;
561
562 // Number of steps left to perform before debug event.
563 int step_count_;
564
565 // Frame pointer from last step next action.
566 Address last_fp_;
567
lrn@chromium.org34e60782011-09-15 07:25:40 +0000568 // Number of queued steps left to perform before debug event.
569 int queued_step_count_;
570
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000571 // Frame pointer for frame from which step in was performed.
572 Address step_into_fp_;
573
ager@chromium.orga1645e22009-09-09 19:27:10 +0000574 // Frame pointer for the frame where debugger should be called when current
575 // step out action is completed.
576 Address step_out_fp_;
577
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000578 // Storage location for jump when exiting debug break calls.
579 Address after_break_target_;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000580
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000581 // Stores the way how LiveEdit has patched the stack. It is used when
582 // debugger returns control back to user script.
583 FrameDropMode frame_drop_mode_;
ager@chromium.org357bf652010-04-12 11:30:10 +0000584
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000585 // Top debugger entry.
586 EnterDebugger* debugger_entry_;
587
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000588 // Pending interrupts scheduled while debugging.
589 int pending_interrupts_;
whesse@chromium.orge90029b2010-08-02 11:52:17 +0000590
591 // When restarter frame is on stack, stores the address
592 // of the pointer to function being restarted. Otherwise (most of the time)
593 // stores NULL. This pointer is used with 'step in' implementation.
594 Object** restarter_frame_function_pointer_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000595 };
596
597 // Storage location for registers when handling debug break calls
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000598 JSCallerSavedBuffer registers_;
599 ThreadLocal thread_local_;
600 void ThreadInit();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000601
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000602 // Code to call for handling debug break on return.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000603 Code* debug_break_return_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000604
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000605 // Code to call for handling debug break in debug break slots.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000606 Code* debug_break_slot_;
607
608 Isolate* isolate_;
609
610 friend class Isolate;
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000611
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000612 DISALLOW_COPY_AND_ASSIGN(Debug);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000613};
614
615
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000616DECLARE_RUNTIME_FUNCTION(Object*, Debug_Break);
617
618
ager@chromium.org5ec48922009-05-05 07:25:34 +0000619// Message delivered to the message handler callback. This is either a debugger
620// event or the response to a command.
621class MessageImpl: public v8::Debug::Message {
622 public:
623 // Create a message object for a debug event.
624 static MessageImpl NewEvent(DebugEvent event,
625 bool running,
626 Handle<JSObject> exec_state,
627 Handle<JSObject> event_data);
628
629 // Create a message object for the response to a debug command.
630 static MessageImpl NewResponse(DebugEvent event,
631 bool running,
632 Handle<JSObject> exec_state,
633 Handle<JSObject> event_data,
634 Handle<String> response_json,
635 v8::Debug::ClientData* client_data);
636
637 // Implementation of interface v8::Debug::Message.
638 virtual bool IsEvent() const;
639 virtual bool IsResponse() const;
640 virtual DebugEvent GetEvent() const;
641 virtual bool WillStartRunning() const;
642 virtual v8::Handle<v8::Object> GetExecutionState() const;
643 virtual v8::Handle<v8::Object> GetEventData() const;
644 virtual v8::Handle<v8::String> GetJSON() const;
645 virtual v8::Handle<v8::Context> GetEventContext() const;
646 virtual v8::Debug::ClientData* GetClientData() const;
647
648 private:
649 MessageImpl(bool is_event,
650 DebugEvent event,
651 bool running,
652 Handle<JSObject> exec_state,
653 Handle<JSObject> event_data,
654 Handle<String> response_json,
655 v8::Debug::ClientData* client_data);
656
657 bool is_event_; // Does this message represent a debug event?
658 DebugEvent event_; // Debug event causing the break.
659 bool running_; // Will the VM start running after this event?
660 Handle<JSObject> exec_state_; // Current execution state.
661 Handle<JSObject> event_data_; // Data associated with the event.
662 Handle<String> response_json_; // Response JSON if message holds a response.
663 v8::Debug::ClientData* client_data_; // Client data passed with the request.
664};
665
666
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000667// Details of the debug event delivered to the debug event listener.
668class EventDetailsImpl : public v8::Debug::EventDetails {
669 public:
670 EventDetailsImpl(DebugEvent event,
671 Handle<JSObject> exec_state,
672 Handle<JSObject> event_data,
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +0000673 Handle<Object> callback_data,
674 v8::Debug::ClientData* client_data);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000675 virtual DebugEvent GetEvent() const;
676 virtual v8::Handle<v8::Object> GetExecutionState() const;
677 virtual v8::Handle<v8::Object> GetEventData() const;
678 virtual v8::Handle<v8::Context> GetEventContext() const;
679 virtual v8::Handle<v8::Value> GetCallbackData() const;
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +0000680 virtual v8::Debug::ClientData* GetClientData() const;
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000681 private:
682 DebugEvent event_; // Debug event causing the break.
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +0000683 Handle<JSObject> exec_state_; // Current execution state.
684 Handle<JSObject> event_data_; // Data associated with the event.
685 Handle<Object> callback_data_; // User data passed with the callback
686 // when it was registered.
687 v8::Debug::ClientData* client_data_; // Data passed to DebugBreakForCommand.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000688};
689
690
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000691// Message send by user to v8 debugger or debugger output message.
692// In addition to command text it may contain a pointer to some user data
693// which are expected to be passed along with the command reponse to message
694// handler.
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000695class CommandMessage {
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000696 public:
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000697 static CommandMessage New(const Vector<uint16_t>& command,
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000698 v8::Debug::ClientData* data);
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000699 CommandMessage();
700 ~CommandMessage();
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000701
702 // Deletes user data and disposes of the text.
703 void Dispose();
704 Vector<uint16_t> text() const { return text_; }
705 v8::Debug::ClientData* client_data() const { return client_data_; }
706 private:
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000707 CommandMessage(const Vector<uint16_t>& text,
708 v8::Debug::ClientData* data);
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000709
710 Vector<uint16_t> text_;
711 v8::Debug::ClientData* client_data_;
712};
713
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000714// A Queue of CommandMessage objects. A thread-safe version is
715// LockingCommandMessageQueue, based on this class.
716class CommandMessageQueue BASE_EMBEDDED {
ager@chromium.org41826e72009-03-30 13:30:57 +0000717 public:
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000718 explicit CommandMessageQueue(int size);
719 ~CommandMessageQueue();
ager@chromium.org41826e72009-03-30 13:30:57 +0000720 bool IsEmpty() const { return start_ == end_; }
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000721 CommandMessage Get();
722 void Put(const CommandMessage& message);
ager@chromium.org41826e72009-03-30 13:30:57 +0000723 void Clear() { start_ = end_ = 0; } // Queue is empty after Clear().
724 private:
725 // Doubles the size of the message queue, and copies the messages.
726 void Expand();
727
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000728 CommandMessage* messages_;
ager@chromium.org41826e72009-03-30 13:30:57 +0000729 int start_;
730 int end_;
731 int size_; // The size of the queue buffer. Queue can hold size-1 messages.
732};
733
734
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000735class MessageDispatchHelperThread;
736
737
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000738// LockingCommandMessageQueue is a thread-safe circular buffer of CommandMessage
739// messages. The message data is not managed by LockingCommandMessageQueue.
ager@chromium.org41826e72009-03-30 13:30:57 +0000740// Pointers to the data are passed in and out. Implemented by adding a
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000741// Mutex to CommandMessageQueue. Includes logging of all puts and gets.
742class LockingCommandMessageQueue BASE_EMBEDDED {
ager@chromium.org41826e72009-03-30 13:30:57 +0000743 public:
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000744 LockingCommandMessageQueue(Logger* logger, int size);
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000745 ~LockingCommandMessageQueue();
ager@chromium.org41826e72009-03-30 13:30:57 +0000746 bool IsEmpty() const;
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000747 CommandMessage Get();
748 void Put(const CommandMessage& message);
ager@chromium.org41826e72009-03-30 13:30:57 +0000749 void Clear();
750 private:
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000751 Logger* logger_;
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000752 CommandMessageQueue queue_;
ager@chromium.org41826e72009-03-30 13:30:57 +0000753 Mutex* lock_;
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000754 DISALLOW_COPY_AND_ASSIGN(LockingCommandMessageQueue);
ager@chromium.org41826e72009-03-30 13:30:57 +0000755};
756
757
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000758class Debugger {
759 public:
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000760 ~Debugger();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000761
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000762 void DebugRequest(const uint16_t* json_request, int length);
763
764 Handle<Object> MakeJSObject(Vector<const char> constructor_name,
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +0000765 int argc,
766 Handle<Object> argv[],
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000767 bool* caught_exception);
768 Handle<Object> MakeExecutionState(bool* caught_exception);
769 Handle<Object> MakeBreakEvent(Handle<Object> exec_state,
770 Handle<Object> break_points_hit,
771 bool* caught_exception);
772 Handle<Object> MakeExceptionEvent(Handle<Object> exec_state,
773 Handle<Object> exception,
774 bool uncaught,
775 bool* caught_exception);
776 Handle<Object> MakeNewFunctionEvent(Handle<Object> func,
777 bool* caught_exception);
778 Handle<Object> MakeCompileEvent(Handle<Script> script,
779 bool before,
780 bool* caught_exception);
781 Handle<Object> MakeScriptCollectedEvent(int id,
782 bool* caught_exception);
783 void OnDebugBreak(Handle<Object> break_points_hit, bool auto_continue);
784 void OnException(Handle<Object> exception, bool uncaught);
785 void OnBeforeCompile(Handle<Script> script);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000786
787 enum AfterCompileFlags {
788 NO_AFTER_COMPILE_FLAGS,
789 SEND_WHEN_DEBUGGING
790 };
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000791 void OnAfterCompile(Handle<Script> script,
792 AfterCompileFlags after_compile_flags);
793 void OnNewFunction(Handle<JSFunction> fun);
794 void OnScriptCollected(int id);
795 void ProcessDebugEvent(v8::DebugEvent event,
796 Handle<JSObject> event_data,
797 bool auto_continue);
798 void NotifyMessageHandler(v8::DebugEvent event,
799 Handle<JSObject> exec_state,
800 Handle<JSObject> event_data,
801 bool auto_continue);
802 void SetEventListener(Handle<Object> callback, Handle<Object> data);
803 void SetMessageHandler(v8::Debug::MessageHandler2 handler);
804 void SetHostDispatchHandler(v8::Debug::HostDispatchHandler handler,
805 int period);
806 void SetDebugMessageDispatchHandler(
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000807 v8::Debug::DebugMessageDispatchHandler handler,
808 bool provide_locker);
ager@chromium.org41826e72009-03-30 13:30:57 +0000809
810 // Invoke the message handler function.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000811 void InvokeMessageHandler(MessageImpl message);
ager@chromium.org41826e72009-03-30 13:30:57 +0000812
813 // Add a debugger command to the command queue.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000814 void ProcessCommand(Vector<const uint16_t> command,
815 v8::Debug::ClientData* client_data = NULL);
ager@chromium.org41826e72009-03-30 13:30:57 +0000816
817 // Check whether there are commands in the command queue.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000818 bool HasCommands();
ager@chromium.org41826e72009-03-30 13:30:57 +0000819
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +0000820 // Enqueue a debugger command to the command queue for event listeners.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000821 void EnqueueDebugCommand(v8::Debug::ClientData* client_data = NULL);
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +0000822
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000823 Handle<Object> Call(Handle<JSFunction> fun,
824 Handle<Object> data,
825 bool* pending_exception);
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000826
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000827 // Start the debugger agent listening on the provided port.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000828 bool StartAgent(const char* name, int port,
829 bool wait_for_connection = false);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000830
831 // Stop the debugger agent.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000832 void StopAgent();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000833
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000834 // Blocks until the agent has started listening for connections
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000835 void WaitForAgent();
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000836
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000837 void CallMessageDispatchHandler();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000838
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000839 Handle<Context> GetDebugContext();
lrn@chromium.orgc34f5802010-04-28 12:53:43 +0000840
ager@chromium.org71daaf62009-04-01 07:22:49 +0000841 // Unload the debugger if possible. Only called when no debugger is currently
842 // active.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000843 void UnloadDebugger();
lrn@chromium.orgc34f5802010-04-28 12:53:43 +0000844 friend void ForceUnloadDebugger(); // In test-debug.cc
ager@chromium.org71daaf62009-04-01 07:22:49 +0000845
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000846 inline bool EventActive(v8::DebugEvent event) {
ager@chromium.org71daaf62009-04-01 07:22:49 +0000847 ScopedLock with(debugger_access_);
848
849 // Check whether the message handler was been cleared.
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000850 if (debugger_unload_pending_) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000851 if (isolate_->debug()->debugger_entry() == NULL) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000852 UnloadDebugger();
853 }
ager@chromium.org71daaf62009-04-01 07:22:49 +0000854 }
855
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +0000856 if (((event == v8::BeforeCompile) || (event == v8::AfterCompile)) &&
857 !FLAG_debug_compile_events) {
858 return false;
859
860 } else if ((event == v8::ScriptCollected) &&
861 !FLAG_debug_script_collected_events) {
862 return false;
863 }
864
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000865 // Currently argument event is not used.
ager@chromium.org71daaf62009-04-01 07:22:49 +0000866 return !compiling_natives_ && Debugger::IsDebuggerActive();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000867 }
868
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000869 void set_compiling_natives(bool compiling_natives) {
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000870 compiling_natives_ = compiling_natives;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000871 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000872 bool compiling_natives() const { return compiling_natives_; }
873 void set_loading_debugger(bool v) { is_loading_debugger_ = v; }
874 bool is_loading_debugger() const { return is_loading_debugger_; }
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000875 void set_force_debugger_active(bool force_debugger_active) {
876 force_debugger_active_ = force_debugger_active;
877 }
878 bool force_debugger_active() const { return force_debugger_active_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000879
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000880 bool IsDebuggerActive();
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000881
882 private:
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000883 explicit Debugger(Isolate* isolate);
ager@chromium.org71daaf62009-04-01 07:22:49 +0000884
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000885 void CallEventCallback(v8::DebugEvent event,
886 Handle<Object> exec_state,
887 Handle<Object> event_data,
888 v8::Debug::ClientData* client_data);
889 void CallCEventCallback(v8::DebugEvent event,
890 Handle<Object> exec_state,
891 Handle<Object> event_data,
892 v8::Debug::ClientData* client_data);
893 void CallJSEventCallback(v8::DebugEvent event,
894 Handle<Object> exec_state,
895 Handle<Object> event_data);
896 void ListenersChanged();
ager@chromium.org381abbb2009-02-25 13:23:22 +0000897
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000898 Mutex* debugger_access_; // Mutex guarding debugger variables.
899 Handle<Object> event_listener_; // Global handle to listener.
900 Handle<Object> event_listener_data_;
901 bool compiling_natives_; // Are we compiling natives?
902 bool is_loading_debugger_; // Are we loading the debugger?
903 bool never_unload_debugger_; // Can we unload the debugger?
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000904 bool force_debugger_active_; // Activate debugger without event listeners.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000905 v8::Debug::MessageHandler2 message_handler_;
906 bool debugger_unload_pending_; // Was message handler cleared?
907 v8::Debug::HostDispatchHandler host_dispatch_handler_;
908 Mutex* dispatch_handler_access_; // Mutex guarding dispatch handler.
909 v8::Debug::DebugMessageDispatchHandler debug_message_dispatch_handler_;
910 MessageDispatchHelperThread* message_dispatch_helper_thread_;
911 int host_dispatch_micros_;
912
913 DebuggerAgent* agent_;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000914
ager@chromium.org41826e72009-03-30 13:30:57 +0000915 static const int kQueueInitialSize = 4;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000916 LockingCommandMessageQueue command_queue_;
917 Semaphore* command_received_; // Signaled for each command received.
918 LockingCommandMessageQueue event_command_queue_;
ager@chromium.org41826e72009-03-30 13:30:57 +0000919
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000920 Isolate* isolate_;
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +0000921
ager@chromium.org71daaf62009-04-01 07:22:49 +0000922 friend class EnterDebugger;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000923 friend class Isolate;
924
925 DISALLOW_COPY_AND_ASSIGN(Debugger);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000926};
927
928
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000929// This class is used for entering the debugger. Create an instance in the stack
930// to enter the debugger. This will set the current break state, make sure the
931// debugger is loaded and switch to the debugger context. If the debugger for
932// some reason could not be entered FailedToEnter will return true.
933class EnterDebugger BASE_EMBEDDED {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000934 public:
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000935 EnterDebugger();
936 ~EnterDebugger();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000937
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000938 // Check whether the debugger could be entered.
939 inline bool FailedToEnter() { return load_failed_; }
940
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000941 // Check whether there are any JavaScript frames on the stack.
ager@chromium.org8bb60582008-12-11 12:02:20 +0000942 inline bool HasJavaScriptFrames() { return has_js_frames_; }
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000943
ager@chromium.org9085a012009-05-11 19:22:57 +0000944 // Get the active context from before entering the debugger.
945 inline Handle<Context> GetContext() { return save_.context(); }
946
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000947 private:
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000948 Isolate* isolate_;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000949 EnterDebugger* prev_; // Previous debugger entry if entered recursively.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000950 JavaScriptFrameIterator it_;
ager@chromium.org8bb60582008-12-11 12:02:20 +0000951 const bool has_js_frames_; // Were there any JavaScript frames?
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000952 StackFrame::Id break_frame_id_; // Previous break frame id.
953 int break_id_; // Previous break id.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000954 bool load_failed_; // Did the debugger fail to load?
955 SaveContext save_; // Saves previous context.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000956};
957
958
kasper.lundbd3ec4e2008-07-09 11:06:54 +0000959// Stack allocated class for disabling break.
960class DisableBreak BASE_EMBEDDED {
961 public:
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000962 explicit DisableBreak(bool disable_break) : isolate_(Isolate::Current()) {
963 prev_disable_break_ = isolate_->debug()->disable_break();
964 isolate_->debug()->set_disable_break(disable_break);
kasper.lundbd3ec4e2008-07-09 11:06:54 +0000965 }
966 ~DisableBreak() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000967 ASSERT(Isolate::Current() == isolate_);
968 isolate_->debug()->set_disable_break(prev_disable_break_);
kasper.lundbd3ec4e2008-07-09 11:06:54 +0000969 }
970
971 private:
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000972 Isolate* isolate_;
kasper.lundbd3ec4e2008-07-09 11:06:54 +0000973 // The previous state of the disable break used to restore the value when this
974 // object is destructed.
975 bool prev_disable_break_;
976};
977
978
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000979// Debug_Address encapsulates the Address pointers used in generating debug
980// code.
981class Debug_Address {
982 public:
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000983 explicit Debug_Address(Debug::AddressId id) : id_(id) { }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000984
985 static Debug_Address AfterBreakTarget() {
986 return Debug_Address(Debug::k_after_break_target_address);
987 }
988
989 static Debug_Address DebugBreakReturn() {
990 return Debug_Address(Debug::k_debug_break_return_address);
991 }
992
whesse@chromium.orge90029b2010-08-02 11:52:17 +0000993 static Debug_Address RestarterFrameFunctionPointer() {
994 return Debug_Address(Debug::k_restarter_frame_function_pointer);
995 }
996
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000997 Address address(Isolate* isolate) const {
998 Debug* debug = isolate->debug();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000999 switch (id_) {
1000 case Debug::k_after_break_target_address:
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001001 return reinterpret_cast<Address>(debug->after_break_target_address());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001002 case Debug::k_debug_break_return_address:
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001003 return reinterpret_cast<Address>(debug->debug_break_return_address());
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001004 case Debug::k_debug_break_slot_address:
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001005 return reinterpret_cast<Address>(debug->debug_break_slot_address());
whesse@chromium.orge90029b2010-08-02 11:52:17 +00001006 case Debug::k_restarter_frame_function_pointer:
1007 return reinterpret_cast<Address>(
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001008 debug->restarter_frame_function_pointer_address());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001009 default:
1010 UNREACHABLE();
1011 return NULL;
1012 }
1013 }
kmillikin@chromium.org83e16822011-09-13 08:21:47 +00001014
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001015 private:
1016 Debug::AddressId id_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001017};
1018
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001019// The optional thread that Debug Agent may use to temporary call V8 to process
1020// pending debug requests if debuggee is not running V8 at the moment.
1021// Techincally it does not call V8 itself, rather it asks embedding program
1022// to do this via v8::Debug::HostDispatchHandler
1023class MessageDispatchHelperThread: public Thread {
1024 public:
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001025 explicit MessageDispatchHelperThread(Isolate* isolate);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001026 ~MessageDispatchHelperThread();
1027
1028 void Schedule();
1029
1030 private:
1031 void Run();
1032
1033 Semaphore* const sem_;
1034 Mutex* const mutex_;
1035 bool already_signalled_;
1036
1037 DISALLOW_COPY_AND_ASSIGN(MessageDispatchHelperThread);
1038};
1039
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001040
1041} } // namespace v8::internal
1042
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001043#endif // ENABLE_DEBUGGER_SUPPORT
1044
ager@chromium.org5ec48922009-05-05 07:25:34 +00001045#endif // V8_DEBUG_H_