blob: d1b3b23afbdf227ea737d106fa85f56a0666b666 [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"
jkummerow@chromium.org32aa03c2013-10-01 08:21:50 +000041#include "platform/socket.h"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000042#include "string-stream.h"
kasperl@chromium.org7be3c992009-03-12 07:19:55 +000043#include "v8threads.h"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000044
ager@chromium.org65dad4b2009-04-23 08:48:43 +000045#ifdef ENABLE_DEBUGGER_SUPPORT
46#include "../include/v8-debug.h"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000047
kasperl@chromium.org71affb52009-05-26 05:44:31 +000048namespace v8 {
49namespace internal {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000050
kasperl@chromium.org7be3c992009-03-12 07:19:55 +000051
52// Forward declarations.
53class EnterDebugger;
54
55
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000056// Step actions. NOTE: These values are in macros.py as well.
57enum StepAction {
58 StepNone = -1, // Stepping not prepared.
59 StepOut = 0, // Step out of the current function.
60 StepNext = 1, // Step to the next statement in the current function.
61 StepIn = 2, // Step into new functions invoked or the next statement
62 // in the current function.
63 StepMin = 3, // Perform a minimum step in the current function.
64 StepInMin = 4 // Step into new functions invoked or perform a minimum step
65 // in the current function.
66};
67
68
69// Type of exception break. NOTE: These values are in macros.py as well.
70enum ExceptionBreakType {
71 BreakException = 0,
72 BreakUncaughtException = 1
73};
74
75
76// Type of exception break. NOTE: These values are in macros.py as well.
77enum BreakLocatorType {
78 ALL_BREAK_LOCATIONS = 0,
79 SOURCE_BREAK_LOCATIONS = 1
80};
81
82
jkummerow@chromium.org93a47f42013-07-02 14:43:41 +000083// The different types of breakpoint position alignments.
84// Must match Debug.BreakPositionAlignment in debug-debugger.js
85enum BreakPositionAlignment {
86 STATEMENT_ALIGNED = 0,
87 BREAK_POSITION_ALIGNED = 1
88};
89
90
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000091// Class for iterating through the break points in a function and changing
92// them.
93class BreakLocationIterator {
94 public:
95 explicit BreakLocationIterator(Handle<DebugInfo> debug_info,
96 BreakLocatorType type);
97 virtual ~BreakLocationIterator();
98
99 void Next();
100 void Next(int count);
101 void FindBreakLocationFromAddress(Address pc);
jkummerow@chromium.org93a47f42013-07-02 14:43:41 +0000102 void FindBreakLocationFromPosition(int position,
103 BreakPositionAlignment alignment);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000104 void Reset();
105 bool Done() const;
106 void SetBreakPoint(Handle<Object> break_point_object);
107 void ClearBreakPoint(Handle<Object> break_point_object);
108 void SetOneShot();
109 void ClearOneShot();
mstarzinger@chromium.org1510d582013-06-28 14:00:48 +0000110 bool IsStepInLocation(Isolate* isolate);
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +0000111 void PrepareStepIn(Isolate* isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000112 bool IsExit() const;
113 bool HasBreakPoint();
114 bool IsDebugBreak();
115 Object* BreakPointObjects();
ager@chromium.org381abbb2009-02-25 13:23:22 +0000116 void ClearAllDebugBreak();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000117
118
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000119 inline int code_position() {
120 return static_cast<int>(pc() - debug_info_->code()->entry());
121 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000122 inline int break_point() { return break_point_; }
123 inline int position() { return position_; }
124 inline int statement_position() { return statement_position_; }
125 inline Address pc() { return reloc_iterator_->rinfo()->pc(); }
126 inline Code* code() { return debug_info_->code(); }
127 inline RelocInfo* rinfo() { return reloc_iterator_->rinfo(); }
ager@chromium.org236ad962008-09-25 09:45:57 +0000128 inline RelocInfo::Mode rmode() const {
129 return reloc_iterator_->rinfo()->rmode();
130 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000131 inline RelocInfo* original_rinfo() {
132 return reloc_iterator_original_->rinfo();
133 }
ager@chromium.org236ad962008-09-25 09:45:57 +0000134 inline RelocInfo::Mode original_rmode() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000135 return reloc_iterator_original_->rinfo()->rmode();
136 }
137
ager@chromium.orga1645e22009-09-09 19:27:10 +0000138 bool IsDebuggerStatement();
139
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000140 protected:
141 bool RinfoDone() const;
142 void RinfoNext();
143
144 BreakLocatorType type_;
145 int break_point_;
146 int position_;
147 int statement_position_;
148 Handle<DebugInfo> debug_info_;
149 RelocIterator* reloc_iterator_;
150 RelocIterator* reloc_iterator_original_;
151
152 private:
153 void SetDebugBreak();
154 void ClearDebugBreak();
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000155
156 void SetDebugBreakAtIC();
157 void ClearDebugBreakAtIC();
158
iposva@chromium.org245aa852009-02-10 00:49:54 +0000159 bool IsDebugBreakAtReturn();
160 void SetDebugBreakAtReturn();
161 void ClearDebugBreakAtReturn();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000162
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000163 bool IsDebugBreakSlot();
164 bool IsDebugBreakAtSlot();
165 void SetDebugBreakAtSlot();
166 void ClearDebugBreakAtSlot();
167
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000168 DISALLOW_COPY_AND_ASSIGN(BreakLocationIterator);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000169};
170
171
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000172// Cache of all script objects in the heap. When a script is added a weak handle
173// to it is created and that weak handle is stored in the cache. The weak handle
174// callback takes care of removing the script from the cache. The key used in
175// the cache is the script id.
176class ScriptCache : private HashMap {
177 public:
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +0000178 explicit ScriptCache(Isolate* isolate)
179 : HashMap(ScriptMatch), isolate_(isolate), collected_scripts_(10) {}
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000180 virtual ~ScriptCache() { Clear(); }
181
182 // Add script to the cache.
183 void Add(Handle<Script> script);
184
185 // Return the scripts in the cache.
186 Handle<FixedArray> GetScripts();
187
188 // Generate debugger events for collected scripts.
189 void ProcessCollectedScripts();
190
191 private:
192 // Calculate the hash value from the key (script id).
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000193 static uint32_t Hash(int key) {
194 return ComputeIntegerHash(key, v8::internal::kZeroHashSeed);
195 }
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000196
197 // Scripts match if their keys (script id) match.
198 static bool ScriptMatch(void* key1, void* key2) { return key1 == key2; }
199
200 // Clear the cache releasing all the weak handles.
201 void Clear();
202
203 // Weak handle callback for scripts in the cache.
hpayer@chromium.org4f99be92013-12-18 16:23:55 +0000204 static void HandleWeakScript(
205 const v8::WeakCallbackData<v8::Value, void>& data);
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000206
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +0000207 Isolate* isolate_;
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000208 // List used during GC to temporarily store id's of collected scripts.
209 List<int> collected_scripts_;
210};
211
212
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000213// Linked list holding debug info objects. The debug info objects are kept as
214// weak handles to avoid a debug info object to keep a function alive.
215class DebugInfoListNode {
216 public:
217 explicit DebugInfoListNode(DebugInfo* debug_info);
218 virtual ~DebugInfoListNode();
219
220 DebugInfoListNode* next() { return next_; }
221 void set_next(DebugInfoListNode* next) { next_ = next; }
222 Handle<DebugInfo> debug_info() { return debug_info_; }
223
224 private:
225 // Global (weak) handle to the debug info object.
226 Handle<DebugInfo> debug_info_;
227
228 // Next pointer for linked list.
229 DebugInfoListNode* next_;
230};
231
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000232// This class contains the debugger support. The main purpose is to handle
233// setting break points in the code.
234//
235// This class controls the debug info for all functions which currently have
236// active breakpoints in them. This debug info is held in the heap root object
237// debug_info which is a FixedArray. Each entry in this list is of class
238// DebugInfo.
239class Debug {
240 public:
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000241 void SetUp(bool create_heap_objects);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000242 bool Load();
243 void Unload();
244 bool IsLoaded() { return !debug_context_.is_null(); }
245 bool InDebugger() { return thread_local_.debugger_entry_ != NULL; }
246 void PreemptionWhileInDebugger();
247 void Iterate(ObjectVisitor* v);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000248
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000249 Object* Break(Arguments args);
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000250 void SetBreakPoint(Handle<JSFunction> function,
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000251 Handle<Object> break_point_object,
252 int* source_position);
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000253 bool SetBreakPointForScript(Handle<Script> script,
254 Handle<Object> break_point_object,
jkummerow@chromium.org93a47f42013-07-02 14:43:41 +0000255 int* source_position,
256 BreakPositionAlignment alignment);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000257 void ClearBreakPoint(Handle<Object> break_point_object);
258 void ClearAllBreakPoints();
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000259 void FloodWithOneShot(Handle<JSFunction> function);
rossberg@chromium.org2c067b12012-03-19 11:01:52 +0000260 void FloodBoundFunctionWithOneShot(Handle<JSFunction> function);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000261 void FloodHandlerWithOneShot();
262 void ChangeBreakOnException(ExceptionBreakType type, bool enable);
263 bool IsBreakOnException(ExceptionBreakType type);
dslomov@chromium.org639bac02013-09-09 11:58:54 +0000264 void PrepareStep(StepAction step_action,
265 int step_count,
266 StackFrame::Id frame_id);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000267 void ClearStepping();
mstarzinger@chromium.org88d326b2012-04-23 12:57:22 +0000268 void ClearStepOut();
269 bool IsStepping() { return thread_local_.step_count_ > 0; }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000270 bool StepNextContinue(BreakLocationIterator* break_location_iterator,
271 JavaScriptFrame* frame);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000272 static Handle<DebugInfo> GetDebugInfo(Handle<SharedFunctionInfo> shared);
273 static bool HasDebugInfo(Handle<SharedFunctionInfo> shared);
kasper.lundbd3ec4e2008-07-09 11:06:54 +0000274
lrn@chromium.org34e60782011-09-15 07:25:40 +0000275 void PrepareForBreakPoints();
276
jkummerow@chromium.org78502a92012-09-06 13:50:42 +0000277 // This function is used in FunctionNameUsing* tests.
278 Object* FindSharedFunctionInfoInScript(Handle<Script> script, int position);
279
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000280 // Returns whether the operation succeeded. Compilation can only be triggered
281 // if a valid closure is passed as the second argument, otherwise the shared
282 // function needs to be compiled already.
283 bool EnsureDebugInfo(Handle<SharedFunctionInfo> shared,
284 Handle<JSFunction> function);
kasper.lundbd3ec4e2008-07-09 11:06:54 +0000285
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000286 // Returns true if the current stub call is patched to call the debugger.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000287 static bool IsDebugBreak(Address addr);
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000288 // Returns true if the current return statement has been patched to be
289 // a debugger breakpoint.
ager@chromium.org381abbb2009-02-25 13:23:22 +0000290 static bool IsDebugBreakAtReturn(RelocInfo* rinfo);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000291
292 // Check whether a code stub with the specified major key is a possible break
293 // point location.
294 static bool IsSourceBreakStub(Code* code);
295 static bool IsBreakStub(Code* code);
296
297 // Find the builtin to use for invoking the debug break
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000298 static Handle<Code> FindDebugBreak(Handle<Code> code, RelocInfo::Mode mode);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000299
300 static Handle<Object> GetSourceBreakLocations(
jkummerow@chromium.org93a47f42013-07-02 14:43:41 +0000301 Handle<SharedFunctionInfo> shared,
302 BreakPositionAlignment position_aligment);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000303
304 // Getter for the debug_context.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000305 inline Handle<Context> debug_context() { return debug_context_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000306
307 // Check whether a global object is the debug global object.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000308 bool IsDebugGlobal(GlobalObject* global);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000309
ager@chromium.org2cc82ae2010-06-14 07:35:38 +0000310 // Check whether this frame is just about to return.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000311 bool IsBreakAtReturn(JavaScriptFrame* frame);
ager@chromium.org2cc82ae2010-06-14 07:35:38 +0000312
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000313 // Fast check to see if any break points are active.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000314 inline bool has_break_points() { return has_break_points_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000315
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000316 void NewBreak(StackFrame::Id break_frame_id);
317 void SetBreak(StackFrame::Id break_frame_id, int break_id);
318 StackFrame::Id break_frame_id() {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000319 return thread_local_.break_frame_id_;
320 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000321 int break_id() { return thread_local_.break_id_; }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000322
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000323 bool StepInActive() { return thread_local_.step_into_fp_ != 0; }
324 void HandleStepIn(Handle<JSFunction> function,
325 Handle<Object> holder,
326 Address fp,
327 bool is_constructor);
328 Address step_in_fp() { return thread_local_.step_into_fp_; }
329 Address* step_in_fp_addr() { return &thread_local_.step_into_fp_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000330
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000331 bool StepOutActive() { return thread_local_.step_out_fp_ != 0; }
332 Address step_out_fp() { return thread_local_.step_out_fp_; }
ager@chromium.orga1645e22009-09-09 19:27:10 +0000333
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000334 EnterDebugger* debugger_entry() {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000335 return thread_local_.debugger_entry_;
336 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000337 void set_debugger_entry(EnterDebugger* entry) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000338 thread_local_.debugger_entry_ = entry;
339 }
340
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000341 // Check whether any of the specified interrupts are pending.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000342 bool is_interrupt_pending(InterruptFlag what) {
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000343 return (thread_local_.pending_interrupts_ & what) != 0;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000344 }
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000345
346 // Set specified interrupts as pending.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000347 void set_interrupts_pending(InterruptFlag what) {
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000348 thread_local_.pending_interrupts_ |= what;
349 }
350
351 // Clear specified interrupts from pending.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000352 void clear_interrupt_pending(InterruptFlag what) {
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000353 thread_local_.pending_interrupts_ &= ~static_cast<int>(what);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000354 }
355
kasper.lundbd3ec4e2008-07-09 11:06:54 +0000356 // Getter and setter for the disable break state.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000357 bool disable_break() { return disable_break_; }
358 void set_disable_break(bool disable_break) {
kasper.lundbd3ec4e2008-07-09 11:06:54 +0000359 disable_break_ = disable_break;
360 }
361
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000362 // Getters for the current exception break state.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000363 bool break_on_exception() { return break_on_exception_; }
364 bool break_on_uncaught_exception() {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000365 return break_on_uncaught_exception_;
366 }
367
368 enum AddressId {
369 k_after_break_target_address,
370 k_debug_break_return_address,
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000371 k_debug_break_slot_address,
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000372 k_restarter_frame_function_pointer
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000373 };
374
375 // Support for setting the address to jump to when returning from break point.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000376 Address* after_break_target_address() {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000377 return reinterpret_cast<Address*>(&thread_local_.after_break_target_);
378 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000379 Address* restarter_frame_function_pointer_address() {
whesse@chromium.orge90029b2010-08-02 11:52:17 +0000380 Object*** address = &thread_local_.restarter_frame_function_pointer_;
381 return reinterpret_cast<Address*>(address);
382 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000383
384 // Support for saving/restoring registers when handling debug break calls.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000385 Object** register_address(int r) {
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000386 return &registers_[r];
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000387 }
388
ager@chromium.orga1645e22009-09-09 19:27:10 +0000389 // Access to the debug break on return code.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000390 Code* debug_break_return() { return debug_break_return_; }
391 Code** debug_break_return_address() {
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000392 return &debug_break_return_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000393 }
394
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000395 // Access to the debug break in debug break slot code.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000396 Code* debug_break_slot() { return debug_break_slot_; }
397 Code** debug_break_slot_address() {
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000398 return &debug_break_slot_;
399 }
400
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000401 static const int kEstimatedNofDebugInfoEntries = 16;
402 static const int kEstimatedNofBreakPointsInFunction = 16;
403
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000404 // Passed to MakeWeak.
hpayer@chromium.org4f99be92013-12-18 16:23:55 +0000405 static void HandleWeakDebugInfo(
406 const v8::WeakCallbackData<v8::Value, void>& data);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000407
408 friend class Debugger;
ager@chromium.org381abbb2009-02-25 13:23:22 +0000409 friend Handle<FixedArray> GetDebuggedFunctions(); // In test-debug.cc
410 friend void CheckDebuggerUnloaded(bool check_functions); // In test-debug.cc
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000411
412 // Threading support.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000413 char* ArchiveDebug(char* to);
414 char* RestoreDebug(char* from);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000415 static int ArchiveSpacePerThread();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000416 void FreeThreadResources() { }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000417
ager@chromium.org32912102009-01-16 10:38:43 +0000418 // Mirror cache handling.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000419 void ClearMirrorCache();
ager@chromium.org32912102009-01-16 10:38:43 +0000420
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000421 // Script cache handling.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000422 void CreateScriptCache();
423 void DestroyScriptCache();
424 void AddScriptToScriptCache(Handle<Script> script);
425 Handle<FixedArray> GetLoadedScripts();
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000426
yangguo@chromium.org49546742013-12-23 16:17:49 +0000427 // Record function from which eval was called.
428 static void RecordEvalCaller(Handle<Script> script);
429
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000430 // Garbage collection notifications.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000431 void AfterGarbageCollection();
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000432
ager@chromium.org8bb60582008-12-11 12:02:20 +0000433 // Code generator routines.
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000434 static void GenerateSlot(MacroAssembler* masm);
ager@chromium.org8bb60582008-12-11 12:02:20 +0000435 static void GenerateLoadICDebugBreak(MacroAssembler* masm);
436 static void GenerateStoreICDebugBreak(MacroAssembler* masm);
437 static void GenerateKeyedLoadICDebugBreak(MacroAssembler* masm);
438 static void GenerateKeyedStoreICDebugBreak(MacroAssembler* masm);
danno@chromium.orgf005df62013-04-30 16:36:45 +0000439 static void GenerateCompareNilICDebugBreak(MacroAssembler* masm);
ager@chromium.org8bb60582008-12-11 12:02:20 +0000440 static void GenerateReturnDebugBreak(MacroAssembler* masm);
danno@chromium.orgc612e022011-11-10 11:38:15 +0000441 static void GenerateCallFunctionStubDebugBreak(MacroAssembler* masm);
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000442 static void GenerateCallFunctionStubRecordDebugBreak(MacroAssembler* masm);
443 static void GenerateCallConstructStubDebugBreak(MacroAssembler* masm);
444 static void GenerateCallConstructStubRecordDebugBreak(MacroAssembler* masm);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000445 static void GenerateSlotDebugBreak(MacroAssembler* masm);
ager@chromium.org357bf652010-04-12 11:30:10 +0000446 static void GeneratePlainReturnLiveEdit(MacroAssembler* masm);
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000447
448 // FrameDropper is a code replacement for a JavaScript frame with possibly
449 // several frames above.
450 // There is no calling conventions here, because it never actually gets
451 // called, it only gets returned to.
ager@chromium.org357bf652010-04-12 11:30:10 +0000452 static void GenerateFrameDropperLiveEdit(MacroAssembler* masm);
ager@chromium.org8bb60582008-12-11 12:02:20 +0000453
454 // Called from stub-cache.cc.
455 static void GenerateCallICDebugBreak(MacroAssembler* masm);
456
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000457 // Describes how exactly a frame has been dropped from stack.
458 enum FrameDropMode {
459 // No frame has been dropped.
460 FRAMES_UNTOUCHED,
461 // The top JS frame had been calling IC stub. IC stub mustn't be called now.
462 FRAME_DROPPED_IN_IC_CALL,
463 // The top JS frame had been calling debug break slot stub. Patch the
464 // address this stub jumps to in the end.
465 FRAME_DROPPED_IN_DEBUG_SLOT_CALL,
466 // The top JS frame had been calling some C++ function. The return address
467 // gets patched automatically.
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000468 FRAME_DROPPED_IN_DIRECT_CALL,
ulan@chromium.orgd9e468a2012-06-25 09:47:40 +0000469 FRAME_DROPPED_IN_RETURN_CALL,
470 CURRENTLY_SET_MODE
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000471 };
472
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000473 void FramesHaveBeenDropped(StackFrame::Id new_break_frame_id,
whesse@chromium.orge90029b2010-08-02 11:52:17 +0000474 FrameDropMode mode,
475 Object** restarter_frame_function_pointer);
ager@chromium.org357bf652010-04-12 11:30:10 +0000476
whesse@chromium.orge90029b2010-08-02 11:52:17 +0000477 // Initializes an artificial stack frame. The data it contains is used for:
478 // a. successful work of frame dropper code which eventually gets control,
479 // b. being compatible with regular stack structure for various stack
480 // iterators.
481 // Returns address of stack allocated pointer to restarted function,
482 // the value that is called 'restarter_frame_function_pointer'. The value
483 // at this address (possibly updated by GC) may be used later when preparing
484 // 'step in' operation.
whesse@chromium.orge90029b2010-08-02 11:52:17 +0000485 static Object** SetUpFrameDropperFrame(StackFrame* bottom_js_frame,
486 Handle<Code> code);
487
ager@chromium.org357bf652010-04-12 11:30:10 +0000488 static const int kFrameDropperFrameSize;
489
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000490 // Architecture-specific constant.
491 static const bool kFrameDropperSupported;
492
jkummerow@chromium.org212d9642012-05-11 15:02:09 +0000493 /**
494 * Defines layout of a stack frame that supports padding. This is a regular
495 * internal frame that has a flexible stack structure. LiveEdit can shift
496 * its lower part up the stack, taking up the 'padding' space when additional
497 * stack memory is required.
498 * Such frame is expected immediately above the topmost JavaScript frame.
499 *
500 * Stack Layout:
501 * --- Top
502 * LiveEdit routine frames
503 * ---
504 * C frames of debug handler
505 * ---
506 * ...
507 * ---
508 * An internal frame that has n padding words:
509 * - any number of words as needed by code -- upper part of frame
510 * - padding size: a Smi storing n -- current size of padding
511 * - padding: n words filled with kPaddingValue in form of Smi
512 * - 3 context/type words of a regular InternalFrame
513 * - fp
514 * ---
515 * Topmost JavaScript frame
516 * ---
517 * ...
518 * --- Bottom
519 */
520 class FramePaddingLayout : public AllStatic {
521 public:
522 // Architecture-specific constant.
523 static const bool kIsSupported;
524
525 // A size of frame base including fp. Padding words starts right above
526 // the base.
527 static const int kFrameBaseSize = 4;
528
529 // A number of words that should be reserved on stack for the LiveEdit use.
530 // Normally equals 1. Stored on stack in form of Smi.
531 static const int kInitialSize;
532 // A value that padding words are filled with (in form of Smi). Going
533 // bottom-top, the first word not having this value is a counter word.
534 static const int kPaddingValue;
535 };
536
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000537 private:
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000538 explicit Debug(Isolate* isolate);
539 ~Debug();
540
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +0000541 static bool CompileDebuggerScript(Isolate* isolate, int index);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000542 void ClearOneShot();
543 void ActivateStepIn(StackFrame* frame);
544 void ClearStepIn();
545 void ActivateStepOut(StackFrame* frame);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000546 void ClearStepNext();
ager@chromium.org32912102009-01-16 10:38:43 +0000547 // Returns whether the compile succeeded.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000548 void RemoveDebugInfo(Handle<DebugInfo> debug_info);
549 void SetAfterBreakTarget(JavaScriptFrame* frame);
550 Handle<Object> CheckBreakPoints(Handle<Object> break_point);
551 bool CheckBreakPoint(Handle<Object> break_point_object);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000552
553 // Global handle to debug context where all the debugger JavaScript code is
554 // loaded.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000555 Handle<Context> debug_context_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000556
557 // Boolean state indicating whether any break points are set.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000558 bool has_break_points_;
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000559
560 // Cache of all scripts in the heap.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000561 ScriptCache* script_cache_;
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000562
563 // List of active debug info objects.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000564 DebugInfoListNode* debug_info_list_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000565
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000566 bool disable_break_;
567 bool break_on_exception_;
568 bool break_on_uncaught_exception_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000569
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000570 // Per-thread data.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000571 class ThreadLocal {
572 public:
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000573 // Counter for generating next break id.
574 int break_count_;
575
576 // Current break id.
577 int break_id_;
578
579 // Frame id for the frame of the current break.
580 StackFrame::Id break_frame_id_;
581
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000582 // Step action for last step performed.
583 StepAction last_step_action_;
584
585 // Source statement position from last step next action.
586 int last_statement_position_;
587
588 // Number of steps left to perform before debug event.
589 int step_count_;
590
591 // Frame pointer from last step next action.
592 Address last_fp_;
593
lrn@chromium.org34e60782011-09-15 07:25:40 +0000594 // Number of queued steps left to perform before debug event.
595 int queued_step_count_;
596
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000597 // Frame pointer for frame from which step in was performed.
598 Address step_into_fp_;
599
ager@chromium.orga1645e22009-09-09 19:27:10 +0000600 // Frame pointer for the frame where debugger should be called when current
601 // step out action is completed.
602 Address step_out_fp_;
603
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000604 // Storage location for jump when exiting debug break calls.
605 Address after_break_target_;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000606
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000607 // Stores the way how LiveEdit has patched the stack. It is used when
608 // debugger returns control back to user script.
609 FrameDropMode frame_drop_mode_;
ager@chromium.org357bf652010-04-12 11:30:10 +0000610
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000611 // Top debugger entry.
612 EnterDebugger* debugger_entry_;
613
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000614 // Pending interrupts scheduled while debugging.
615 int pending_interrupts_;
whesse@chromium.orge90029b2010-08-02 11:52:17 +0000616
617 // When restarter frame is on stack, stores the address
618 // of the pointer to function being restarted. Otherwise (most of the time)
619 // stores NULL. This pointer is used with 'step in' implementation.
620 Object** restarter_frame_function_pointer_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000621 };
622
623 // Storage location for registers when handling debug break calls
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000624 JSCallerSavedBuffer registers_;
625 ThreadLocal thread_local_;
626 void ThreadInit();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000627
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000628 // Code to call for handling debug break on return.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000629 Code* debug_break_return_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000630
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000631 // Code to call for handling debug break in debug break slots.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000632 Code* debug_break_slot_;
633
634 Isolate* isolate_;
635
636 friend class Isolate;
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000637
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000638 DISALLOW_COPY_AND_ASSIGN(Debug);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000639};
640
641
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000642DECLARE_RUNTIME_FUNCTION(Object*, Debug_Break);
643
644
ager@chromium.org5ec48922009-05-05 07:25:34 +0000645// Message delivered to the message handler callback. This is either a debugger
646// event or the response to a command.
647class MessageImpl: public v8::Debug::Message {
648 public:
649 // Create a message object for a debug event.
650 static MessageImpl NewEvent(DebugEvent event,
651 bool running,
652 Handle<JSObject> exec_state,
653 Handle<JSObject> event_data);
654
655 // Create a message object for the response to a debug command.
656 static MessageImpl NewResponse(DebugEvent event,
657 bool running,
658 Handle<JSObject> exec_state,
659 Handle<JSObject> event_data,
660 Handle<String> response_json,
661 v8::Debug::ClientData* client_data);
662
663 // Implementation of interface v8::Debug::Message.
664 virtual bool IsEvent() const;
665 virtual bool IsResponse() const;
666 virtual DebugEvent GetEvent() const;
667 virtual bool WillStartRunning() const;
668 virtual v8::Handle<v8::Object> GetExecutionState() const;
669 virtual v8::Handle<v8::Object> GetEventData() const;
670 virtual v8::Handle<v8::String> GetJSON() const;
671 virtual v8::Handle<v8::Context> GetEventContext() const;
672 virtual v8::Debug::ClientData* GetClientData() const;
hpayer@chromium.orgc5d49712013-09-11 08:25:48 +0000673 virtual v8::Isolate* GetIsolate() const;
ager@chromium.org5ec48922009-05-05 07:25:34 +0000674
675 private:
676 MessageImpl(bool is_event,
677 DebugEvent event,
678 bool running,
679 Handle<JSObject> exec_state,
680 Handle<JSObject> event_data,
681 Handle<String> response_json,
682 v8::Debug::ClientData* client_data);
683
684 bool is_event_; // Does this message represent a debug event?
685 DebugEvent event_; // Debug event causing the break.
686 bool running_; // Will the VM start running after this event?
687 Handle<JSObject> exec_state_; // Current execution state.
688 Handle<JSObject> event_data_; // Data associated with the event.
689 Handle<String> response_json_; // Response JSON if message holds a response.
690 v8::Debug::ClientData* client_data_; // Client data passed with the request.
691};
692
693
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000694// Details of the debug event delivered to the debug event listener.
695class EventDetailsImpl : public v8::Debug::EventDetails {
696 public:
697 EventDetailsImpl(DebugEvent event,
698 Handle<JSObject> exec_state,
699 Handle<JSObject> event_data,
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +0000700 Handle<Object> callback_data,
701 v8::Debug::ClientData* client_data);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000702 virtual DebugEvent GetEvent() const;
703 virtual v8::Handle<v8::Object> GetExecutionState() const;
704 virtual v8::Handle<v8::Object> GetEventData() const;
705 virtual v8::Handle<v8::Context> GetEventContext() const;
706 virtual v8::Handle<v8::Value> GetCallbackData() const;
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +0000707 virtual v8::Debug::ClientData* GetClientData() const;
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000708 private:
709 DebugEvent event_; // Debug event causing the break.
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +0000710 Handle<JSObject> exec_state_; // Current execution state.
711 Handle<JSObject> event_data_; // Data associated with the event.
712 Handle<Object> callback_data_; // User data passed with the callback
713 // when it was registered.
714 v8::Debug::ClientData* client_data_; // Data passed to DebugBreakForCommand.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000715};
716
717
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000718// Message send by user to v8 debugger or debugger output message.
719// In addition to command text it may contain a pointer to some user data
720// which are expected to be passed along with the command reponse to message
721// handler.
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000722class CommandMessage {
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000723 public:
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000724 static CommandMessage New(const Vector<uint16_t>& command,
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000725 v8::Debug::ClientData* data);
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000726 CommandMessage();
727 ~CommandMessage();
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000728
729 // Deletes user data and disposes of the text.
730 void Dispose();
731 Vector<uint16_t> text() const { return text_; }
732 v8::Debug::ClientData* client_data() const { return client_data_; }
733 private:
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000734 CommandMessage(const Vector<uint16_t>& text,
735 v8::Debug::ClientData* data);
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000736
737 Vector<uint16_t> text_;
738 v8::Debug::ClientData* client_data_;
739};
740
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000741// A Queue of CommandMessage objects. A thread-safe version is
742// LockingCommandMessageQueue, based on this class.
743class CommandMessageQueue BASE_EMBEDDED {
ager@chromium.org41826e72009-03-30 13:30:57 +0000744 public:
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000745 explicit CommandMessageQueue(int size);
746 ~CommandMessageQueue();
ager@chromium.org41826e72009-03-30 13:30:57 +0000747 bool IsEmpty() const { return start_ == end_; }
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000748 CommandMessage Get();
749 void Put(const CommandMessage& message);
ager@chromium.org41826e72009-03-30 13:30:57 +0000750 void Clear() { start_ = end_ = 0; } // Queue is empty after Clear().
751 private:
752 // Doubles the size of the message queue, and copies the messages.
753 void Expand();
754
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000755 CommandMessage* messages_;
ager@chromium.org41826e72009-03-30 13:30:57 +0000756 int start_;
757 int end_;
758 int size_; // The size of the queue buffer. Queue can hold size-1 messages.
759};
760
761
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000762class MessageDispatchHelperThread;
763
764
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000765// LockingCommandMessageQueue is a thread-safe circular buffer of CommandMessage
766// messages. The message data is not managed by LockingCommandMessageQueue.
ager@chromium.org41826e72009-03-30 13:30:57 +0000767// Pointers to the data are passed in and out. Implemented by adding a
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000768// Mutex to CommandMessageQueue. Includes logging of all puts and gets.
769class LockingCommandMessageQueue BASE_EMBEDDED {
ager@chromium.org41826e72009-03-30 13:30:57 +0000770 public:
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000771 LockingCommandMessageQueue(Logger* logger, int size);
ager@chromium.org41826e72009-03-30 13:30:57 +0000772 bool IsEmpty() const;
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000773 CommandMessage Get();
774 void Put(const CommandMessage& message);
ager@chromium.org41826e72009-03-30 13:30:57 +0000775 void Clear();
776 private:
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000777 Logger* logger_;
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000778 CommandMessageQueue queue_;
jkummerow@chromium.orgdc94e192013-08-30 11:35:42 +0000779 mutable Mutex mutex_;
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000780 DISALLOW_COPY_AND_ASSIGN(LockingCommandMessageQueue);
ager@chromium.org41826e72009-03-30 13:30:57 +0000781};
782
783
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000784class Debugger {
785 public:
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000786 ~Debugger();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000787
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000788 void DebugRequest(const uint16_t* json_request, int length);
789
790 Handle<Object> MakeJSObject(Vector<const char> constructor_name,
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +0000791 int argc,
792 Handle<Object> argv[],
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000793 bool* caught_exception);
794 Handle<Object> MakeExecutionState(bool* caught_exception);
795 Handle<Object> MakeBreakEvent(Handle<Object> exec_state,
796 Handle<Object> break_points_hit,
797 bool* caught_exception);
798 Handle<Object> MakeExceptionEvent(Handle<Object> exec_state,
799 Handle<Object> exception,
800 bool uncaught,
801 bool* caught_exception);
802 Handle<Object> MakeNewFunctionEvent(Handle<Object> func,
803 bool* caught_exception);
804 Handle<Object> MakeCompileEvent(Handle<Script> script,
805 bool before,
806 bool* caught_exception);
807 Handle<Object> MakeScriptCollectedEvent(int id,
808 bool* caught_exception);
809 void OnDebugBreak(Handle<Object> break_points_hit, bool auto_continue);
810 void OnException(Handle<Object> exception, bool uncaught);
811 void OnBeforeCompile(Handle<Script> script);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000812
813 enum AfterCompileFlags {
814 NO_AFTER_COMPILE_FLAGS,
815 SEND_WHEN_DEBUGGING
816 };
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000817 void OnAfterCompile(Handle<Script> script,
818 AfterCompileFlags after_compile_flags);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000819 void OnScriptCollected(int id);
820 void ProcessDebugEvent(v8::DebugEvent event,
821 Handle<JSObject> event_data,
822 bool auto_continue);
823 void NotifyMessageHandler(v8::DebugEvent event,
824 Handle<JSObject> exec_state,
825 Handle<JSObject> event_data,
826 bool auto_continue);
827 void SetEventListener(Handle<Object> callback, Handle<Object> data);
828 void SetMessageHandler(v8::Debug::MessageHandler2 handler);
829 void SetHostDispatchHandler(v8::Debug::HostDispatchHandler handler,
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +0000830 TimeDelta period);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000831 void SetDebugMessageDispatchHandler(
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000832 v8::Debug::DebugMessageDispatchHandler handler,
833 bool provide_locker);
ager@chromium.org41826e72009-03-30 13:30:57 +0000834
835 // Invoke the message handler function.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000836 void InvokeMessageHandler(MessageImpl message);
ager@chromium.org41826e72009-03-30 13:30:57 +0000837
838 // Add a debugger command to the command queue.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000839 void ProcessCommand(Vector<const uint16_t> command,
840 v8::Debug::ClientData* client_data = NULL);
ager@chromium.org41826e72009-03-30 13:30:57 +0000841
842 // Check whether there are commands in the command queue.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000843 bool HasCommands();
ager@chromium.org41826e72009-03-30 13:30:57 +0000844
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +0000845 // Enqueue a debugger command to the command queue for event listeners.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000846 void EnqueueDebugCommand(v8::Debug::ClientData* client_data = NULL);
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +0000847
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000848 Handle<Object> Call(Handle<JSFunction> fun,
849 Handle<Object> data,
850 bool* pending_exception);
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000851
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000852 // Start the debugger agent listening on the provided port.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000853 bool StartAgent(const char* name, int port,
854 bool wait_for_connection = false);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000855
856 // Stop the debugger agent.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000857 void StopAgent();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000858
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000859 // Blocks until the agent has started listening for connections
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000860 void WaitForAgent();
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000861
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000862 void CallMessageDispatchHandler();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000863
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000864 Handle<Context> GetDebugContext();
lrn@chromium.orgc34f5802010-04-28 12:53:43 +0000865
ager@chromium.org71daaf62009-04-01 07:22:49 +0000866 // Unload the debugger if possible. Only called when no debugger is currently
867 // active.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000868 void UnloadDebugger();
lrn@chromium.orgc34f5802010-04-28 12:53:43 +0000869 friend void ForceUnloadDebugger(); // In test-debug.cc
ager@chromium.org71daaf62009-04-01 07:22:49 +0000870
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000871 inline bool EventActive(v8::DebugEvent event) {
jkummerow@chromium.orgdc94e192013-08-30 11:35:42 +0000872 LockGuard<RecursiveMutex> lock_guard(debugger_access_);
ager@chromium.org71daaf62009-04-01 07:22:49 +0000873
874 // Check whether the message handler was been cleared.
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000875 if (debugger_unload_pending_) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000876 if (isolate_->debug()->debugger_entry() == NULL) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000877 UnloadDebugger();
878 }
ager@chromium.org71daaf62009-04-01 07:22:49 +0000879 }
880
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +0000881 if (((event == v8::BeforeCompile) || (event == v8::AfterCompile)) &&
882 !FLAG_debug_compile_events) {
883 return false;
884
885 } else if ((event == v8::ScriptCollected) &&
886 !FLAG_debug_script_collected_events) {
887 return false;
888 }
889
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000890 // Currently argument event is not used.
ager@chromium.org71daaf62009-04-01 07:22:49 +0000891 return !compiling_natives_ && Debugger::IsDebuggerActive();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000892 }
893
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000894 void set_compiling_natives(bool compiling_natives) {
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000895 compiling_natives_ = compiling_natives;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000896 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000897 bool compiling_natives() const { return compiling_natives_; }
898 void set_loading_debugger(bool v) { is_loading_debugger_ = v; }
899 bool is_loading_debugger() const { return is_loading_debugger_; }
mstarzinger@chromium.orgde886792012-09-11 13:22:37 +0000900 void set_live_edit_enabled(bool v) { live_edit_enabled_ = v; }
mvstanton@chromium.org6bec0092013-01-23 13:46:53 +0000901 bool live_edit_enabled() const {
902 return FLAG_enable_liveedit && live_edit_enabled_ ;
903 }
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000904 void set_force_debugger_active(bool force_debugger_active) {
905 force_debugger_active_ = force_debugger_active;
906 }
907 bool force_debugger_active() const { return force_debugger_active_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000908
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000909 bool IsDebuggerActive();
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000910
911 private:
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000912 explicit Debugger(Isolate* isolate);
ager@chromium.org71daaf62009-04-01 07:22:49 +0000913
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000914 void CallEventCallback(v8::DebugEvent event,
915 Handle<Object> exec_state,
916 Handle<Object> event_data,
917 v8::Debug::ClientData* client_data);
918 void CallCEventCallback(v8::DebugEvent event,
919 Handle<Object> exec_state,
920 Handle<Object> event_data,
921 v8::Debug::ClientData* client_data);
922 void CallJSEventCallback(v8::DebugEvent event,
923 Handle<Object> exec_state,
924 Handle<Object> event_data);
925 void ListenersChanged();
ager@chromium.org381abbb2009-02-25 13:23:22 +0000926
jkummerow@chromium.orgdc94e192013-08-30 11:35:42 +0000927 RecursiveMutex* debugger_access_; // Mutex guarding debugger variables.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000928 Handle<Object> event_listener_; // Global handle to listener.
929 Handle<Object> event_listener_data_;
930 bool compiling_natives_; // Are we compiling natives?
931 bool is_loading_debugger_; // Are we loading the debugger?
mstarzinger@chromium.orgde886792012-09-11 13:22:37 +0000932 bool live_edit_enabled_; // Enable LiveEdit.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000933 bool never_unload_debugger_; // Can we unload the debugger?
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000934 bool force_debugger_active_; // Activate debugger without event listeners.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000935 v8::Debug::MessageHandler2 message_handler_;
936 bool debugger_unload_pending_; // Was message handler cleared?
937 v8::Debug::HostDispatchHandler host_dispatch_handler_;
jkummerow@chromium.orgdc94e192013-08-30 11:35:42 +0000938 Mutex dispatch_handler_access_; // Mutex guarding dispatch handler.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000939 v8::Debug::DebugMessageDispatchHandler debug_message_dispatch_handler_;
940 MessageDispatchHelperThread* message_dispatch_helper_thread_;
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +0000941 TimeDelta host_dispatch_period_;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000942
943 DebuggerAgent* agent_;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000944
ager@chromium.org41826e72009-03-30 13:30:57 +0000945 static const int kQueueInitialSize = 4;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000946 LockingCommandMessageQueue command_queue_;
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +0000947 Semaphore command_received_; // Signaled for each command received.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000948 LockingCommandMessageQueue event_command_queue_;
ager@chromium.org41826e72009-03-30 13:30:57 +0000949
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000950 Isolate* isolate_;
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +0000951
ager@chromium.org71daaf62009-04-01 07:22:49 +0000952 friend class EnterDebugger;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000953 friend class Isolate;
954
955 DISALLOW_COPY_AND_ASSIGN(Debugger);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000956};
957
958
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000959// This class is used for entering the debugger. Create an instance in the stack
960// to enter the debugger. This will set the current break state, make sure the
961// debugger is loaded and switch to the debugger context. If the debugger for
962// some reason could not be entered FailedToEnter will return true.
963class EnterDebugger BASE_EMBEDDED {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000964 public:
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +0000965 explicit EnterDebugger(Isolate* isolate);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000966 ~EnterDebugger();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000967
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000968 // Check whether the debugger could be entered.
969 inline bool FailedToEnter() { return load_failed_; }
970
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000971 // Check whether there are any JavaScript frames on the stack.
ager@chromium.org8bb60582008-12-11 12:02:20 +0000972 inline bool HasJavaScriptFrames() { return has_js_frames_; }
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000973
ager@chromium.org9085a012009-05-11 19:22:57 +0000974 // Get the active context from before entering the debugger.
975 inline Handle<Context> GetContext() { return save_.context(); }
976
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000977 private:
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000978 Isolate* isolate_;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000979 EnterDebugger* prev_; // Previous debugger entry if entered recursively.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000980 JavaScriptFrameIterator it_;
ager@chromium.org8bb60582008-12-11 12:02:20 +0000981 const bool has_js_frames_; // Were there any JavaScript frames?
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000982 StackFrame::Id break_frame_id_; // Previous break frame id.
983 int break_id_; // Previous break id.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000984 bool load_failed_; // Did the debugger fail to load?
985 SaveContext save_; // Saves previous context.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000986};
987
988
kasper.lundbd3ec4e2008-07-09 11:06:54 +0000989// Stack allocated class for disabling break.
990class DisableBreak BASE_EMBEDDED {
991 public:
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +0000992 explicit DisableBreak(Isolate* isolate, bool disable_break)
993 : isolate_(isolate) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000994 prev_disable_break_ = isolate_->debug()->disable_break();
995 isolate_->debug()->set_disable_break(disable_break);
kasper.lundbd3ec4e2008-07-09 11:06:54 +0000996 }
997 ~DisableBreak() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000998 isolate_->debug()->set_disable_break(prev_disable_break_);
kasper.lundbd3ec4e2008-07-09 11:06:54 +0000999 }
1000
1001 private:
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001002 Isolate* isolate_;
kasper.lundbd3ec4e2008-07-09 11:06:54 +00001003 // The previous state of the disable break used to restore the value when this
1004 // object is destructed.
1005 bool prev_disable_break_;
1006};
1007
1008
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001009// Debug_Address encapsulates the Address pointers used in generating debug
1010// code.
1011class Debug_Address {
1012 public:
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001013 explicit Debug_Address(Debug::AddressId id) : id_(id) { }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001014
1015 static Debug_Address AfterBreakTarget() {
1016 return Debug_Address(Debug::k_after_break_target_address);
1017 }
1018
1019 static Debug_Address DebugBreakReturn() {
1020 return Debug_Address(Debug::k_debug_break_return_address);
1021 }
1022
whesse@chromium.orge90029b2010-08-02 11:52:17 +00001023 static Debug_Address RestarterFrameFunctionPointer() {
1024 return Debug_Address(Debug::k_restarter_frame_function_pointer);
1025 }
1026
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001027 Address address(Isolate* isolate) const {
1028 Debug* debug = isolate->debug();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001029 switch (id_) {
1030 case Debug::k_after_break_target_address:
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001031 return reinterpret_cast<Address>(debug->after_break_target_address());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001032 case Debug::k_debug_break_return_address:
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001033 return reinterpret_cast<Address>(debug->debug_break_return_address());
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001034 case Debug::k_debug_break_slot_address:
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001035 return reinterpret_cast<Address>(debug->debug_break_slot_address());
whesse@chromium.orge90029b2010-08-02 11:52:17 +00001036 case Debug::k_restarter_frame_function_pointer:
1037 return reinterpret_cast<Address>(
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001038 debug->restarter_frame_function_pointer_address());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001039 default:
1040 UNREACHABLE();
1041 return NULL;
1042 }
1043 }
kmillikin@chromium.org83e16822011-09-13 08:21:47 +00001044
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001045 private:
1046 Debug::AddressId id_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001047};
1048
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001049// The optional thread that Debug Agent may use to temporary call V8 to process
1050// pending debug requests if debuggee is not running V8 at the moment.
1051// Techincally it does not call V8 itself, rather it asks embedding program
1052// to do this via v8::Debug::HostDispatchHandler
1053class MessageDispatchHelperThread: public Thread {
1054 public:
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001055 explicit MessageDispatchHelperThread(Isolate* isolate);
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +00001056 ~MessageDispatchHelperThread() {}
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001057
1058 void Schedule();
1059
1060 private:
1061 void Run();
1062
mstarzinger@chromium.orgf705b502013-04-04 11:38:09 +00001063 Isolate* isolate_;
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +00001064 Semaphore sem_;
jkummerow@chromium.orgdc94e192013-08-30 11:35:42 +00001065 Mutex mutex_;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001066 bool already_signalled_;
1067
1068 DISALLOW_COPY_AND_ASSIGN(MessageDispatchHelperThread);
1069};
1070
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001071
1072} } // namespace v8::internal
1073
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001074#endif // ENABLE_DEBUGGER_SUPPORT
1075
ager@chromium.org5ec48922009-05-05 07:25:34 +00001076#endif // V8_DEBUG_H_