ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 1 | // Copyright 2006-2008 the V8 project authors. All rights reserved. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2 | // Redistribution and use in source and binary forms, with or without |
| 3 | // modification, are permitted provided that the following conditions are |
| 4 | // met: |
| 5 | // |
| 6 | // * Redistributions of source code must retain the above copyright |
| 7 | // notice, this list of conditions and the following disclaimer. |
| 8 | // * Redistributions in binary form must reproduce the above |
| 9 | // copyright notice, this list of conditions and the following |
| 10 | // disclaimer in the documentation and/or other materials provided |
| 11 | // with the distribution. |
| 12 | // * Neither the name of Google Inc. nor the names of its |
| 13 | // contributors may be used to endorse or promote products derived |
| 14 | // from this software without specific prior written permission. |
| 15 | // |
| 16 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 28 | #ifndef V8_DEBUG_H_ |
| 29 | #define V8_DEBUG_H_ |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 30 | |
lrn@chromium.org | 1c09276 | 2011-05-09 09:42:16 +0000 | [diff] [blame^] | 31 | #include "allocation.h" |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 32 | #include "arguments.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 33 | #include "assembler.h" |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 34 | #include "debug-agent.h" |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 35 | #include "execution.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 36 | #include "factory.h" |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 37 | #include "flags.h" |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 38 | #include "hashmap.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 39 | #include "platform.h" |
| 40 | #include "string-stream.h" |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 41 | #include "v8threads.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 42 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 43 | #ifdef ENABLE_DEBUGGER_SUPPORT |
| 44 | #include "../include/v8-debug.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 45 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 46 | namespace v8 { |
| 47 | namespace internal { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 48 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 49 | |
| 50 | // Forward declarations. |
| 51 | class EnterDebugger; |
| 52 | |
| 53 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 54 | // Step actions. NOTE: These values are in macros.py as well. |
| 55 | enum StepAction { |
| 56 | StepNone = -1, // Stepping not prepared. |
| 57 | StepOut = 0, // Step out of the current function. |
| 58 | StepNext = 1, // Step to the next statement in the current function. |
| 59 | StepIn = 2, // Step into new functions invoked or the next statement |
| 60 | // in the current function. |
| 61 | StepMin = 3, // Perform a minimum step in the current function. |
| 62 | StepInMin = 4 // Step into new functions invoked or perform a minimum step |
| 63 | // in the current function. |
| 64 | }; |
| 65 | |
| 66 | |
| 67 | // Type of exception break. NOTE: These values are in macros.py as well. |
| 68 | enum ExceptionBreakType { |
| 69 | BreakException = 0, |
| 70 | BreakUncaughtException = 1 |
| 71 | }; |
| 72 | |
| 73 | |
| 74 | // Type of exception break. NOTE: These values are in macros.py as well. |
| 75 | enum BreakLocatorType { |
| 76 | ALL_BREAK_LOCATIONS = 0, |
| 77 | SOURCE_BREAK_LOCATIONS = 1 |
| 78 | }; |
| 79 | |
| 80 | |
| 81 | // Class for iterating through the break points in a function and changing |
| 82 | // them. |
| 83 | class BreakLocationIterator { |
| 84 | public: |
| 85 | explicit BreakLocationIterator(Handle<DebugInfo> debug_info, |
| 86 | BreakLocatorType type); |
| 87 | virtual ~BreakLocationIterator(); |
| 88 | |
| 89 | void Next(); |
| 90 | void Next(int count); |
| 91 | void FindBreakLocationFromAddress(Address pc); |
| 92 | void FindBreakLocationFromPosition(int position); |
| 93 | void Reset(); |
| 94 | bool Done() const; |
| 95 | void SetBreakPoint(Handle<Object> break_point_object); |
| 96 | void ClearBreakPoint(Handle<Object> break_point_object); |
| 97 | void SetOneShot(); |
| 98 | void ClearOneShot(); |
| 99 | void PrepareStepIn(); |
| 100 | bool IsExit() const; |
| 101 | bool HasBreakPoint(); |
| 102 | bool IsDebugBreak(); |
| 103 | Object* BreakPointObjects(); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 104 | void ClearAllDebugBreak(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 105 | |
| 106 | |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 107 | inline int code_position() { |
| 108 | return static_cast<int>(pc() - debug_info_->code()->entry()); |
| 109 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 110 | inline int break_point() { return break_point_; } |
| 111 | inline int position() { return position_; } |
| 112 | inline int statement_position() { return statement_position_; } |
| 113 | inline Address pc() { return reloc_iterator_->rinfo()->pc(); } |
| 114 | inline Code* code() { return debug_info_->code(); } |
| 115 | inline RelocInfo* rinfo() { return reloc_iterator_->rinfo(); } |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 116 | inline RelocInfo::Mode rmode() const { |
| 117 | return reloc_iterator_->rinfo()->rmode(); |
| 118 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 119 | inline RelocInfo* original_rinfo() { |
| 120 | return reloc_iterator_original_->rinfo(); |
| 121 | } |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 122 | inline RelocInfo::Mode original_rmode() const { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 123 | return reloc_iterator_original_->rinfo()->rmode(); |
| 124 | } |
| 125 | |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 126 | bool IsDebuggerStatement(); |
| 127 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 128 | protected: |
| 129 | bool RinfoDone() const; |
| 130 | void RinfoNext(); |
| 131 | |
| 132 | BreakLocatorType type_; |
| 133 | int break_point_; |
| 134 | int position_; |
| 135 | int statement_position_; |
| 136 | Handle<DebugInfo> debug_info_; |
| 137 | RelocIterator* reloc_iterator_; |
| 138 | RelocIterator* reloc_iterator_original_; |
| 139 | |
| 140 | private: |
| 141 | void SetDebugBreak(); |
| 142 | void ClearDebugBreak(); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 143 | |
| 144 | void SetDebugBreakAtIC(); |
| 145 | void ClearDebugBreakAtIC(); |
| 146 | |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 147 | bool IsDebugBreakAtReturn(); |
| 148 | void SetDebugBreakAtReturn(); |
| 149 | void ClearDebugBreakAtReturn(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 150 | |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 151 | bool IsDebugBreakSlot(); |
| 152 | bool IsDebugBreakAtSlot(); |
| 153 | void SetDebugBreakAtSlot(); |
| 154 | void ClearDebugBreakAtSlot(); |
| 155 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 156 | DISALLOW_COPY_AND_ASSIGN(BreakLocationIterator); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 157 | }; |
| 158 | |
| 159 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 160 | // Cache of all script objects in the heap. When a script is added a weak handle |
| 161 | // to it is created and that weak handle is stored in the cache. The weak handle |
| 162 | // callback takes care of removing the script from the cache. The key used in |
| 163 | // the cache is the script id. |
| 164 | class ScriptCache : private HashMap { |
| 165 | public: |
| 166 | ScriptCache() : HashMap(ScriptMatch), collected_scripts_(10) {} |
| 167 | virtual ~ScriptCache() { Clear(); } |
| 168 | |
| 169 | // Add script to the cache. |
| 170 | void Add(Handle<Script> script); |
| 171 | |
| 172 | // Return the scripts in the cache. |
| 173 | Handle<FixedArray> GetScripts(); |
| 174 | |
| 175 | // Generate debugger events for collected scripts. |
| 176 | void ProcessCollectedScripts(); |
| 177 | |
| 178 | private: |
| 179 | // Calculate the hash value from the key (script id). |
| 180 | static uint32_t Hash(int key) { return ComputeIntegerHash(key); } |
| 181 | |
| 182 | // Scripts match if their keys (script id) match. |
| 183 | static bool ScriptMatch(void* key1, void* key2) { return key1 == key2; } |
| 184 | |
| 185 | // Clear the cache releasing all the weak handles. |
| 186 | void Clear(); |
| 187 | |
| 188 | // Weak handle callback for scripts in the cache. |
| 189 | static void HandleWeakScript(v8::Persistent<v8::Value> obj, void* data); |
| 190 | |
| 191 | // List used during GC to temporarily store id's of collected scripts. |
| 192 | List<int> collected_scripts_; |
| 193 | }; |
| 194 | |
| 195 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 196 | // Linked list holding debug info objects. The debug info objects are kept as |
| 197 | // weak handles to avoid a debug info object to keep a function alive. |
| 198 | class DebugInfoListNode { |
| 199 | public: |
| 200 | explicit DebugInfoListNode(DebugInfo* debug_info); |
| 201 | virtual ~DebugInfoListNode(); |
| 202 | |
| 203 | DebugInfoListNode* next() { return next_; } |
| 204 | void set_next(DebugInfoListNode* next) { next_ = next; } |
| 205 | Handle<DebugInfo> debug_info() { return debug_info_; } |
| 206 | |
| 207 | private: |
| 208 | // Global (weak) handle to the debug info object. |
| 209 | Handle<DebugInfo> debug_info_; |
| 210 | |
| 211 | // Next pointer for linked list. |
| 212 | DebugInfoListNode* next_; |
| 213 | }; |
| 214 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 215 | // This class contains the debugger support. The main purpose is to handle |
| 216 | // setting break points in the code. |
| 217 | // |
| 218 | // This class controls the debug info for all functions which currently have |
| 219 | // active breakpoints in them. This debug info is held in the heap root object |
| 220 | // debug_info which is a FixedArray. Each entry in this list is of class |
| 221 | // DebugInfo. |
| 222 | class Debug { |
| 223 | public: |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 224 | void Setup(bool create_heap_objects); |
| 225 | bool Load(); |
| 226 | void Unload(); |
| 227 | bool IsLoaded() { return !debug_context_.is_null(); } |
| 228 | bool InDebugger() { return thread_local_.debugger_entry_ != NULL; } |
| 229 | void PreemptionWhileInDebugger(); |
| 230 | void Iterate(ObjectVisitor* v); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 231 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 232 | Object* Break(Arguments args); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 233 | void SetBreakPoint(Handle<SharedFunctionInfo> shared, |
| 234 | Handle<Object> break_point_object, |
| 235 | int* source_position); |
| 236 | void ClearBreakPoint(Handle<Object> break_point_object); |
| 237 | void ClearAllBreakPoints(); |
| 238 | void FloodWithOneShot(Handle<SharedFunctionInfo> shared); |
| 239 | void FloodHandlerWithOneShot(); |
| 240 | void ChangeBreakOnException(ExceptionBreakType type, bool enable); |
| 241 | bool IsBreakOnException(ExceptionBreakType type); |
| 242 | void PrepareStep(StepAction step_action, int step_count); |
| 243 | void ClearStepping(); |
| 244 | bool StepNextContinue(BreakLocationIterator* break_location_iterator, |
| 245 | JavaScriptFrame* frame); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 246 | static Handle<DebugInfo> GetDebugInfo(Handle<SharedFunctionInfo> shared); |
| 247 | static bool HasDebugInfo(Handle<SharedFunctionInfo> shared); |
kasper.lund | bd3ec4e | 2008-07-09 11:06:54 +0000 | [diff] [blame] | 248 | |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 249 | // Returns whether the operation succeeded. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 250 | bool EnsureDebugInfo(Handle<SharedFunctionInfo> shared); |
kasper.lund | bd3ec4e | 2008-07-09 11:06:54 +0000 | [diff] [blame] | 251 | |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 252 | // Returns true if the current stub call is patched to call the debugger. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 253 | static bool IsDebugBreak(Address addr); |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 254 | // Returns true if the current return statement has been patched to be |
| 255 | // a debugger breakpoint. |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 256 | static bool IsDebugBreakAtReturn(RelocInfo* rinfo); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 257 | |
| 258 | // Check whether a code stub with the specified major key is a possible break |
| 259 | // point location. |
| 260 | static bool IsSourceBreakStub(Code* code); |
| 261 | static bool IsBreakStub(Code* code); |
| 262 | |
| 263 | // Find the builtin to use for invoking the debug break |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 264 | static Handle<Code> FindDebugBreak(Handle<Code> code, RelocInfo::Mode mode); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 265 | |
| 266 | static Handle<Object> GetSourceBreakLocations( |
| 267 | Handle<SharedFunctionInfo> shared); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 268 | |
| 269 | // Getter for the debug_context. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 270 | inline Handle<Context> debug_context() { return debug_context_; } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 271 | |
| 272 | // Check whether a global object is the debug global object. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 273 | bool IsDebugGlobal(GlobalObject* global); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 274 | |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 275 | // Check whether this frame is just about to return. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 276 | bool IsBreakAtReturn(JavaScriptFrame* frame); |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 277 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 278 | // Fast check to see if any break points are active. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 279 | inline bool has_break_points() { return has_break_points_; } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 280 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 281 | void NewBreak(StackFrame::Id break_frame_id); |
| 282 | void SetBreak(StackFrame::Id break_frame_id, int break_id); |
| 283 | StackFrame::Id break_frame_id() { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 284 | return thread_local_.break_frame_id_; |
| 285 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 286 | int break_id() { return thread_local_.break_id_; } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 287 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 288 | bool StepInActive() { return thread_local_.step_into_fp_ != 0; } |
| 289 | void HandleStepIn(Handle<JSFunction> function, |
| 290 | Handle<Object> holder, |
| 291 | Address fp, |
| 292 | bool is_constructor); |
| 293 | Address step_in_fp() { return thread_local_.step_into_fp_; } |
| 294 | Address* step_in_fp_addr() { return &thread_local_.step_into_fp_; } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 295 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 296 | bool StepOutActive() { return thread_local_.step_out_fp_ != 0; } |
| 297 | Address step_out_fp() { return thread_local_.step_out_fp_; } |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 298 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 299 | EnterDebugger* debugger_entry() { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 300 | return thread_local_.debugger_entry_; |
| 301 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 302 | void set_debugger_entry(EnterDebugger* entry) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 303 | thread_local_.debugger_entry_ = entry; |
| 304 | } |
| 305 | |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 306 | // Check whether any of the specified interrupts are pending. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 307 | bool is_interrupt_pending(InterruptFlag what) { |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 308 | return (thread_local_.pending_interrupts_ & what) != 0; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 309 | } |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 310 | |
| 311 | // Set specified interrupts as pending. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 312 | void set_interrupts_pending(InterruptFlag what) { |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 313 | thread_local_.pending_interrupts_ |= what; |
| 314 | } |
| 315 | |
| 316 | // Clear specified interrupts from pending. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 317 | void clear_interrupt_pending(InterruptFlag what) { |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 318 | thread_local_.pending_interrupts_ &= ~static_cast<int>(what); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 319 | } |
| 320 | |
kasper.lund | bd3ec4e | 2008-07-09 11:06:54 +0000 | [diff] [blame] | 321 | // Getter and setter for the disable break state. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 322 | bool disable_break() { return disable_break_; } |
| 323 | void set_disable_break(bool disable_break) { |
kasper.lund | bd3ec4e | 2008-07-09 11:06:54 +0000 | [diff] [blame] | 324 | disable_break_ = disable_break; |
| 325 | } |
| 326 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 327 | // Getters for the current exception break state. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 328 | bool break_on_exception() { return break_on_exception_; } |
| 329 | bool break_on_uncaught_exception() { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 330 | return break_on_uncaught_exception_; |
| 331 | } |
| 332 | |
| 333 | enum AddressId { |
| 334 | k_after_break_target_address, |
| 335 | k_debug_break_return_address, |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 336 | k_debug_break_slot_address, |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 337 | k_restarter_frame_function_pointer |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 338 | }; |
| 339 | |
| 340 | // Support for setting the address to jump to when returning from break point. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 341 | Address* after_break_target_address() { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 342 | return reinterpret_cast<Address*>(&thread_local_.after_break_target_); |
| 343 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 344 | Address* restarter_frame_function_pointer_address() { |
whesse@chromium.org | e90029b | 2010-08-02 11:52:17 +0000 | [diff] [blame] | 345 | Object*** address = &thread_local_.restarter_frame_function_pointer_; |
| 346 | return reinterpret_cast<Address*>(address); |
| 347 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 348 | |
| 349 | // Support for saving/restoring registers when handling debug break calls. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 350 | Object** register_address(int r) { |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 351 | return ®isters_[r]; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 352 | } |
| 353 | |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 354 | // Access to the debug break on return code. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 355 | Code* debug_break_return() { return debug_break_return_; } |
| 356 | Code** debug_break_return_address() { |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 357 | return &debug_break_return_; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 358 | } |
| 359 | |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 360 | // Access to the debug break in debug break slot code. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 361 | Code* debug_break_slot() { return debug_break_slot_; } |
| 362 | Code** debug_break_slot_address() { |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 363 | return &debug_break_slot_; |
| 364 | } |
| 365 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 366 | static const int kEstimatedNofDebugInfoEntries = 16; |
| 367 | static const int kEstimatedNofBreakPointsInFunction = 16; |
| 368 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 369 | // Passed to MakeWeak. |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 370 | static void HandleWeakDebugInfo(v8::Persistent<v8::Value> obj, void* data); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 371 | |
| 372 | friend class Debugger; |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 373 | friend Handle<FixedArray> GetDebuggedFunctions(); // In test-debug.cc |
| 374 | friend void CheckDebuggerUnloaded(bool check_functions); // In test-debug.cc |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 375 | |
| 376 | // Threading support. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 377 | char* ArchiveDebug(char* to); |
| 378 | char* RestoreDebug(char* from); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 379 | static int ArchiveSpacePerThread(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 380 | void FreeThreadResources() { } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 381 | |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 382 | // Mirror cache handling. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 383 | void ClearMirrorCache(); |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 384 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 385 | // Script cache handling. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 386 | void CreateScriptCache(); |
| 387 | void DestroyScriptCache(); |
| 388 | void AddScriptToScriptCache(Handle<Script> script); |
| 389 | Handle<FixedArray> GetLoadedScripts(); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 390 | |
| 391 | // Garbage collection notifications. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 392 | void AfterGarbageCollection(); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 393 | |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 394 | // Code generator routines. |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 395 | static void GenerateSlot(MacroAssembler* masm); |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 396 | static void GenerateLoadICDebugBreak(MacroAssembler* masm); |
| 397 | static void GenerateStoreICDebugBreak(MacroAssembler* masm); |
| 398 | static void GenerateKeyedLoadICDebugBreak(MacroAssembler* masm); |
| 399 | static void GenerateKeyedStoreICDebugBreak(MacroAssembler* masm); |
| 400 | static void GenerateConstructCallDebugBreak(MacroAssembler* masm); |
| 401 | static void GenerateReturnDebugBreak(MacroAssembler* masm); |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 402 | static void GenerateStubNoRegistersDebugBreak(MacroAssembler* masm); |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 403 | static void GenerateSlotDebugBreak(MacroAssembler* masm); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 404 | static void GeneratePlainReturnLiveEdit(MacroAssembler* masm); |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 405 | |
| 406 | // FrameDropper is a code replacement for a JavaScript frame with possibly |
| 407 | // several frames above. |
| 408 | // There is no calling conventions here, because it never actually gets |
| 409 | // called, it only gets returned to. |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 410 | static void GenerateFrameDropperLiveEdit(MacroAssembler* masm); |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 411 | |
| 412 | // Called from stub-cache.cc. |
| 413 | static void GenerateCallICDebugBreak(MacroAssembler* masm); |
| 414 | |
kmillikin@chromium.org | 69ea396 | 2010-07-05 11:01:40 +0000 | [diff] [blame] | 415 | // Describes how exactly a frame has been dropped from stack. |
| 416 | enum FrameDropMode { |
| 417 | // No frame has been dropped. |
| 418 | FRAMES_UNTOUCHED, |
| 419 | // The top JS frame had been calling IC stub. IC stub mustn't be called now. |
| 420 | FRAME_DROPPED_IN_IC_CALL, |
| 421 | // The top JS frame had been calling debug break slot stub. Patch the |
| 422 | // address this stub jumps to in the end. |
| 423 | FRAME_DROPPED_IN_DEBUG_SLOT_CALL, |
| 424 | // The top JS frame had been calling some C++ function. The return address |
| 425 | // gets patched automatically. |
sgjesse@chromium.org | 8e8294a | 2011-05-02 14:30:53 +0000 | [diff] [blame] | 426 | FRAME_DROPPED_IN_DIRECT_CALL, |
| 427 | FRAME_DROPPED_IN_RETURN_CALL |
kmillikin@chromium.org | 69ea396 | 2010-07-05 11:01:40 +0000 | [diff] [blame] | 428 | }; |
| 429 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 430 | void FramesHaveBeenDropped(StackFrame::Id new_break_frame_id, |
whesse@chromium.org | e90029b | 2010-08-02 11:52:17 +0000 | [diff] [blame] | 431 | FrameDropMode mode, |
| 432 | Object** restarter_frame_function_pointer); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 433 | |
whesse@chromium.org | e90029b | 2010-08-02 11:52:17 +0000 | [diff] [blame] | 434 | // Initializes an artificial stack frame. The data it contains is used for: |
| 435 | // a. successful work of frame dropper code which eventually gets control, |
| 436 | // b. being compatible with regular stack structure for various stack |
| 437 | // iterators. |
| 438 | // Returns address of stack allocated pointer to restarted function, |
| 439 | // the value that is called 'restarter_frame_function_pointer'. The value |
| 440 | // at this address (possibly updated by GC) may be used later when preparing |
| 441 | // 'step in' operation. |
whesse@chromium.org | e90029b | 2010-08-02 11:52:17 +0000 | [diff] [blame] | 442 | static Object** SetUpFrameDropperFrame(StackFrame* bottom_js_frame, |
| 443 | Handle<Code> code); |
| 444 | |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 445 | static const int kFrameDropperFrameSize; |
| 446 | |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 447 | // Architecture-specific constant. |
| 448 | static const bool kFrameDropperSupported; |
| 449 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 450 | private: |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 451 | explicit Debug(Isolate* isolate); |
| 452 | ~Debug(); |
| 453 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 454 | static bool CompileDebuggerScript(int index); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 455 | void ClearOneShot(); |
| 456 | void ActivateStepIn(StackFrame* frame); |
| 457 | void ClearStepIn(); |
| 458 | void ActivateStepOut(StackFrame* frame); |
| 459 | void ClearStepOut(); |
| 460 | void ClearStepNext(); |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 461 | // Returns whether the compile succeeded. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 462 | void RemoveDebugInfo(Handle<DebugInfo> debug_info); |
| 463 | void SetAfterBreakTarget(JavaScriptFrame* frame); |
| 464 | Handle<Object> CheckBreakPoints(Handle<Object> break_point); |
| 465 | bool CheckBreakPoint(Handle<Object> break_point_object); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 466 | |
| 467 | // Global handle to debug context where all the debugger JavaScript code is |
| 468 | // loaded. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 469 | Handle<Context> debug_context_; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 470 | |
| 471 | // Boolean state indicating whether any break points are set. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 472 | bool has_break_points_; |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 473 | |
| 474 | // Cache of all scripts in the heap. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 475 | ScriptCache* script_cache_; |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 476 | |
| 477 | // List of active debug info objects. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 478 | DebugInfoListNode* debug_info_list_; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 479 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 480 | bool disable_break_; |
| 481 | bool break_on_exception_; |
| 482 | bool break_on_uncaught_exception_; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 483 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 484 | // Per-thread data. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 485 | class ThreadLocal { |
| 486 | public: |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 487 | // Counter for generating next break id. |
| 488 | int break_count_; |
| 489 | |
| 490 | // Current break id. |
| 491 | int break_id_; |
| 492 | |
| 493 | // Frame id for the frame of the current break. |
| 494 | StackFrame::Id break_frame_id_; |
| 495 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 496 | // Step action for last step performed. |
| 497 | StepAction last_step_action_; |
| 498 | |
| 499 | // Source statement position from last step next action. |
| 500 | int last_statement_position_; |
| 501 | |
| 502 | // Number of steps left to perform before debug event. |
| 503 | int step_count_; |
| 504 | |
| 505 | // Frame pointer from last step next action. |
| 506 | Address last_fp_; |
| 507 | |
| 508 | // Frame pointer for frame from which step in was performed. |
| 509 | Address step_into_fp_; |
| 510 | |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 511 | // Frame pointer for the frame where debugger should be called when current |
| 512 | // step out action is completed. |
| 513 | Address step_out_fp_; |
| 514 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 515 | // Storage location for jump when exiting debug break calls. |
| 516 | Address after_break_target_; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 517 | |
kmillikin@chromium.org | 69ea396 | 2010-07-05 11:01:40 +0000 | [diff] [blame] | 518 | // Stores the way how LiveEdit has patched the stack. It is used when |
| 519 | // debugger returns control back to user script. |
| 520 | FrameDropMode frame_drop_mode_; |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 521 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 522 | // Top debugger entry. |
| 523 | EnterDebugger* debugger_entry_; |
| 524 | |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 525 | // Pending interrupts scheduled while debugging. |
| 526 | int pending_interrupts_; |
whesse@chromium.org | e90029b | 2010-08-02 11:52:17 +0000 | [diff] [blame] | 527 | |
| 528 | // When restarter frame is on stack, stores the address |
| 529 | // of the pointer to function being restarted. Otherwise (most of the time) |
| 530 | // stores NULL. This pointer is used with 'step in' implementation. |
| 531 | Object** restarter_frame_function_pointer_; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 532 | }; |
| 533 | |
| 534 | // Storage location for registers when handling debug break calls |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 535 | JSCallerSavedBuffer registers_; |
| 536 | ThreadLocal thread_local_; |
| 537 | void ThreadInit(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 538 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 539 | // Code to call for handling debug break on return. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 540 | Code* debug_break_return_; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 541 | |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 542 | // Code to call for handling debug break in debug break slots. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 543 | Code* debug_break_slot_; |
| 544 | |
| 545 | Isolate* isolate_; |
| 546 | |
| 547 | friend class Isolate; |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 548 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 549 | DISALLOW_COPY_AND_ASSIGN(Debug); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 550 | }; |
| 551 | |
| 552 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 553 | DECLARE_RUNTIME_FUNCTION(Object*, Debug_Break); |
| 554 | |
| 555 | |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 556 | // Message delivered to the message handler callback. This is either a debugger |
| 557 | // event or the response to a command. |
| 558 | class MessageImpl: public v8::Debug::Message { |
| 559 | public: |
| 560 | // Create a message object for a debug event. |
| 561 | static MessageImpl NewEvent(DebugEvent event, |
| 562 | bool running, |
| 563 | Handle<JSObject> exec_state, |
| 564 | Handle<JSObject> event_data); |
| 565 | |
| 566 | // Create a message object for the response to a debug command. |
| 567 | static MessageImpl NewResponse(DebugEvent event, |
| 568 | bool running, |
| 569 | Handle<JSObject> exec_state, |
| 570 | Handle<JSObject> event_data, |
| 571 | Handle<String> response_json, |
| 572 | v8::Debug::ClientData* client_data); |
| 573 | |
| 574 | // Implementation of interface v8::Debug::Message. |
| 575 | virtual bool IsEvent() const; |
| 576 | virtual bool IsResponse() const; |
| 577 | virtual DebugEvent GetEvent() const; |
| 578 | virtual bool WillStartRunning() const; |
| 579 | virtual v8::Handle<v8::Object> GetExecutionState() const; |
| 580 | virtual v8::Handle<v8::Object> GetEventData() const; |
| 581 | virtual v8::Handle<v8::String> GetJSON() const; |
| 582 | virtual v8::Handle<v8::Context> GetEventContext() const; |
| 583 | virtual v8::Debug::ClientData* GetClientData() const; |
| 584 | |
| 585 | private: |
| 586 | MessageImpl(bool is_event, |
| 587 | DebugEvent event, |
| 588 | bool running, |
| 589 | Handle<JSObject> exec_state, |
| 590 | Handle<JSObject> event_data, |
| 591 | Handle<String> response_json, |
| 592 | v8::Debug::ClientData* client_data); |
| 593 | |
| 594 | bool is_event_; // Does this message represent a debug event? |
| 595 | DebugEvent event_; // Debug event causing the break. |
| 596 | bool running_; // Will the VM start running after this event? |
| 597 | Handle<JSObject> exec_state_; // Current execution state. |
| 598 | Handle<JSObject> event_data_; // Data associated with the event. |
| 599 | Handle<String> response_json_; // Response JSON if message holds a response. |
| 600 | v8::Debug::ClientData* client_data_; // Client data passed with the request. |
| 601 | }; |
| 602 | |
| 603 | |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 604 | // Details of the debug event delivered to the debug event listener. |
| 605 | class EventDetailsImpl : public v8::Debug::EventDetails { |
| 606 | public: |
| 607 | EventDetailsImpl(DebugEvent event, |
| 608 | Handle<JSObject> exec_state, |
| 609 | Handle<JSObject> event_data, |
mikhail.naganov@gmail.com | 2276287 | 2010-07-14 09:29:05 +0000 | [diff] [blame] | 610 | Handle<Object> callback_data, |
| 611 | v8::Debug::ClientData* client_data); |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 612 | virtual DebugEvent GetEvent() const; |
| 613 | virtual v8::Handle<v8::Object> GetExecutionState() const; |
| 614 | virtual v8::Handle<v8::Object> GetEventData() const; |
| 615 | virtual v8::Handle<v8::Context> GetEventContext() const; |
| 616 | virtual v8::Handle<v8::Value> GetCallbackData() const; |
mikhail.naganov@gmail.com | 2276287 | 2010-07-14 09:29:05 +0000 | [diff] [blame] | 617 | virtual v8::Debug::ClientData* GetClientData() const; |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 618 | private: |
| 619 | DebugEvent event_; // Debug event causing the break. |
mikhail.naganov@gmail.com | 2276287 | 2010-07-14 09:29:05 +0000 | [diff] [blame] | 620 | Handle<JSObject> exec_state_; // Current execution state. |
| 621 | Handle<JSObject> event_data_; // Data associated with the event. |
| 622 | Handle<Object> callback_data_; // User data passed with the callback |
| 623 | // when it was registered. |
| 624 | v8::Debug::ClientData* client_data_; // Data passed to DebugBreakForCommand. |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 625 | }; |
| 626 | |
| 627 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 628 | // Message send by user to v8 debugger or debugger output message. |
| 629 | // In addition to command text it may contain a pointer to some user data |
| 630 | // which are expected to be passed along with the command reponse to message |
| 631 | // handler. |
ager@chromium.org | 3a37e9b | 2009-04-27 09:26:21 +0000 | [diff] [blame] | 632 | class CommandMessage { |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 633 | public: |
ager@chromium.org | 3a37e9b | 2009-04-27 09:26:21 +0000 | [diff] [blame] | 634 | static CommandMessage New(const Vector<uint16_t>& command, |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 635 | v8::Debug::ClientData* data); |
ager@chromium.org | 3a37e9b | 2009-04-27 09:26:21 +0000 | [diff] [blame] | 636 | CommandMessage(); |
| 637 | ~CommandMessage(); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 638 | |
| 639 | // Deletes user data and disposes of the text. |
| 640 | void Dispose(); |
| 641 | Vector<uint16_t> text() const { return text_; } |
| 642 | v8::Debug::ClientData* client_data() const { return client_data_; } |
| 643 | private: |
ager@chromium.org | 3a37e9b | 2009-04-27 09:26:21 +0000 | [diff] [blame] | 644 | CommandMessage(const Vector<uint16_t>& text, |
| 645 | v8::Debug::ClientData* data); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 646 | |
| 647 | Vector<uint16_t> text_; |
| 648 | v8::Debug::ClientData* client_data_; |
| 649 | }; |
| 650 | |
ager@chromium.org | 3a37e9b | 2009-04-27 09:26:21 +0000 | [diff] [blame] | 651 | // A Queue of CommandMessage objects. A thread-safe version is |
| 652 | // LockingCommandMessageQueue, based on this class. |
| 653 | class CommandMessageQueue BASE_EMBEDDED { |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 654 | public: |
ager@chromium.org | 3a37e9b | 2009-04-27 09:26:21 +0000 | [diff] [blame] | 655 | explicit CommandMessageQueue(int size); |
| 656 | ~CommandMessageQueue(); |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 657 | bool IsEmpty() const { return start_ == end_; } |
ager@chromium.org | 3a37e9b | 2009-04-27 09:26:21 +0000 | [diff] [blame] | 658 | CommandMessage Get(); |
| 659 | void Put(const CommandMessage& message); |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 660 | void Clear() { start_ = end_ = 0; } // Queue is empty after Clear(). |
| 661 | private: |
| 662 | // Doubles the size of the message queue, and copies the messages. |
| 663 | void Expand(); |
| 664 | |
ager@chromium.org | 3a37e9b | 2009-04-27 09:26:21 +0000 | [diff] [blame] | 665 | CommandMessage* messages_; |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 666 | int start_; |
| 667 | int end_; |
| 668 | int size_; // The size of the queue buffer. Queue can hold size-1 messages. |
| 669 | }; |
| 670 | |
| 671 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 672 | class MessageDispatchHelperThread; |
| 673 | |
| 674 | |
ager@chromium.org | 3a37e9b | 2009-04-27 09:26:21 +0000 | [diff] [blame] | 675 | // LockingCommandMessageQueue is a thread-safe circular buffer of CommandMessage |
| 676 | // messages. The message data is not managed by LockingCommandMessageQueue. |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 677 | // Pointers to the data are passed in and out. Implemented by adding a |
ager@chromium.org | 3a37e9b | 2009-04-27 09:26:21 +0000 | [diff] [blame] | 678 | // Mutex to CommandMessageQueue. Includes logging of all puts and gets. |
| 679 | class LockingCommandMessageQueue BASE_EMBEDDED { |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 680 | public: |
ager@chromium.org | 3a37e9b | 2009-04-27 09:26:21 +0000 | [diff] [blame] | 681 | explicit LockingCommandMessageQueue(int size); |
| 682 | ~LockingCommandMessageQueue(); |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 683 | bool IsEmpty() const; |
ager@chromium.org | 3a37e9b | 2009-04-27 09:26:21 +0000 | [diff] [blame] | 684 | CommandMessage Get(); |
| 685 | void Put(const CommandMessage& message); |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 686 | void Clear(); |
| 687 | private: |
ager@chromium.org | 3a37e9b | 2009-04-27 09:26:21 +0000 | [diff] [blame] | 688 | CommandMessageQueue queue_; |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 689 | Mutex* lock_; |
ager@chromium.org | 3a37e9b | 2009-04-27 09:26:21 +0000 | [diff] [blame] | 690 | DISALLOW_COPY_AND_ASSIGN(LockingCommandMessageQueue); |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 691 | }; |
| 692 | |
| 693 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 694 | class Debugger { |
| 695 | public: |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 696 | ~Debugger(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 697 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 698 | void DebugRequest(const uint16_t* json_request, int length); |
| 699 | |
| 700 | Handle<Object> MakeJSObject(Vector<const char> constructor_name, |
| 701 | int argc, Object*** argv, |
| 702 | bool* caught_exception); |
| 703 | Handle<Object> MakeExecutionState(bool* caught_exception); |
| 704 | Handle<Object> MakeBreakEvent(Handle<Object> exec_state, |
| 705 | Handle<Object> break_points_hit, |
| 706 | bool* caught_exception); |
| 707 | Handle<Object> MakeExceptionEvent(Handle<Object> exec_state, |
| 708 | Handle<Object> exception, |
| 709 | bool uncaught, |
| 710 | bool* caught_exception); |
| 711 | Handle<Object> MakeNewFunctionEvent(Handle<Object> func, |
| 712 | bool* caught_exception); |
| 713 | Handle<Object> MakeCompileEvent(Handle<Script> script, |
| 714 | bool before, |
| 715 | bool* caught_exception); |
| 716 | Handle<Object> MakeScriptCollectedEvent(int id, |
| 717 | bool* caught_exception); |
| 718 | void OnDebugBreak(Handle<Object> break_points_hit, bool auto_continue); |
| 719 | void OnException(Handle<Object> exception, bool uncaught); |
| 720 | void OnBeforeCompile(Handle<Script> script); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 721 | |
| 722 | enum AfterCompileFlags { |
| 723 | NO_AFTER_COMPILE_FLAGS, |
| 724 | SEND_WHEN_DEBUGGING |
| 725 | }; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 726 | void OnAfterCompile(Handle<Script> script, |
| 727 | AfterCompileFlags after_compile_flags); |
| 728 | void OnNewFunction(Handle<JSFunction> fun); |
| 729 | void OnScriptCollected(int id); |
| 730 | void ProcessDebugEvent(v8::DebugEvent event, |
| 731 | Handle<JSObject> event_data, |
| 732 | bool auto_continue); |
| 733 | void NotifyMessageHandler(v8::DebugEvent event, |
| 734 | Handle<JSObject> exec_state, |
| 735 | Handle<JSObject> event_data, |
| 736 | bool auto_continue); |
| 737 | void SetEventListener(Handle<Object> callback, Handle<Object> data); |
| 738 | void SetMessageHandler(v8::Debug::MessageHandler2 handler); |
| 739 | void SetHostDispatchHandler(v8::Debug::HostDispatchHandler handler, |
| 740 | int period); |
| 741 | void SetDebugMessageDispatchHandler( |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 742 | v8::Debug::DebugMessageDispatchHandler handler, |
| 743 | bool provide_locker); |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 744 | |
| 745 | // Invoke the message handler function. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 746 | void InvokeMessageHandler(MessageImpl message); |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 747 | |
| 748 | // Add a debugger command to the command queue. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 749 | void ProcessCommand(Vector<const uint16_t> command, |
| 750 | v8::Debug::ClientData* client_data = NULL); |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 751 | |
| 752 | // Check whether there are commands in the command queue. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 753 | bool HasCommands(); |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 754 | |
mikhail.naganov@gmail.com | 2276287 | 2010-07-14 09:29:05 +0000 | [diff] [blame] | 755 | // Enqueue a debugger command to the command queue for event listeners. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 756 | void EnqueueDebugCommand(v8::Debug::ClientData* client_data = NULL); |
mikhail.naganov@gmail.com | 2276287 | 2010-07-14 09:29:05 +0000 | [diff] [blame] | 757 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 758 | Handle<Object> Call(Handle<JSFunction> fun, |
| 759 | Handle<Object> data, |
| 760 | bool* pending_exception); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 761 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 762 | // Start the debugger agent listening on the provided port. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 763 | bool StartAgent(const char* name, int port, |
| 764 | bool wait_for_connection = false); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 765 | |
| 766 | // Stop the debugger agent. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 767 | void StopAgent(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 768 | |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 769 | // Blocks until the agent has started listening for connections |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 770 | void WaitForAgent(); |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 771 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 772 | void CallMessageDispatchHandler(); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 773 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 774 | Handle<Context> GetDebugContext(); |
lrn@chromium.org | c34f580 | 2010-04-28 12:53:43 +0000 | [diff] [blame] | 775 | |
ager@chromium.org | 71daaf6 | 2009-04-01 07:22:49 +0000 | [diff] [blame] | 776 | // Unload the debugger if possible. Only called when no debugger is currently |
| 777 | // active. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 778 | void UnloadDebugger(); |
lrn@chromium.org | c34f580 | 2010-04-28 12:53:43 +0000 | [diff] [blame] | 779 | friend void ForceUnloadDebugger(); // In test-debug.cc |
ager@chromium.org | 71daaf6 | 2009-04-01 07:22:49 +0000 | [diff] [blame] | 780 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 781 | inline bool EventActive(v8::DebugEvent event) { |
ager@chromium.org | 71daaf6 | 2009-04-01 07:22:49 +0000 | [diff] [blame] | 782 | ScopedLock with(debugger_access_); |
| 783 | |
| 784 | // Check whether the message handler was been cleared. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 785 | if (debugger_unload_pending_) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 786 | if (isolate_->debug()->debugger_entry() == NULL) { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 787 | UnloadDebugger(); |
| 788 | } |
ager@chromium.org | 71daaf6 | 2009-04-01 07:22:49 +0000 | [diff] [blame] | 789 | } |
| 790 | |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 791 | if (((event == v8::BeforeCompile) || (event == v8::AfterCompile)) && |
| 792 | !FLAG_debug_compile_events) { |
| 793 | return false; |
| 794 | |
| 795 | } else if ((event == v8::ScriptCollected) && |
| 796 | !FLAG_debug_script_collected_events) { |
| 797 | return false; |
| 798 | } |
| 799 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 800 | // Currently argument event is not used. |
ager@chromium.org | 71daaf6 | 2009-04-01 07:22:49 +0000 | [diff] [blame] | 801 | return !compiling_natives_ && Debugger::IsDebuggerActive(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 802 | } |
| 803 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 804 | void set_compiling_natives(bool compiling_natives) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 805 | Debugger::compiling_natives_ = compiling_natives; |
| 806 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 807 | bool compiling_natives() const { return compiling_natives_; } |
| 808 | void set_loading_debugger(bool v) { is_loading_debugger_ = v; } |
| 809 | bool is_loading_debugger() const { return is_loading_debugger_; } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 810 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 811 | bool IsDebuggerActive(); |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 812 | |
| 813 | private: |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 814 | Debugger(); |
ager@chromium.org | 71daaf6 | 2009-04-01 07:22:49 +0000 | [diff] [blame] | 815 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 816 | void CallEventCallback(v8::DebugEvent event, |
| 817 | Handle<Object> exec_state, |
| 818 | Handle<Object> event_data, |
| 819 | v8::Debug::ClientData* client_data); |
| 820 | void CallCEventCallback(v8::DebugEvent event, |
| 821 | Handle<Object> exec_state, |
| 822 | Handle<Object> event_data, |
| 823 | v8::Debug::ClientData* client_data); |
| 824 | void CallJSEventCallback(v8::DebugEvent event, |
| 825 | Handle<Object> exec_state, |
| 826 | Handle<Object> event_data); |
| 827 | void ListenersChanged(); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 828 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 829 | Mutex* debugger_access_; // Mutex guarding debugger variables. |
| 830 | Handle<Object> event_listener_; // Global handle to listener. |
| 831 | Handle<Object> event_listener_data_; |
| 832 | bool compiling_natives_; // Are we compiling natives? |
| 833 | bool is_loading_debugger_; // Are we loading the debugger? |
| 834 | bool never_unload_debugger_; // Can we unload the debugger? |
| 835 | v8::Debug::MessageHandler2 message_handler_; |
| 836 | bool debugger_unload_pending_; // Was message handler cleared? |
| 837 | v8::Debug::HostDispatchHandler host_dispatch_handler_; |
| 838 | Mutex* dispatch_handler_access_; // Mutex guarding dispatch handler. |
| 839 | v8::Debug::DebugMessageDispatchHandler debug_message_dispatch_handler_; |
| 840 | MessageDispatchHelperThread* message_dispatch_helper_thread_; |
| 841 | int host_dispatch_micros_; |
| 842 | |
| 843 | DebuggerAgent* agent_; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 844 | |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 845 | static const int kQueueInitialSize = 4; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 846 | LockingCommandMessageQueue command_queue_; |
| 847 | Semaphore* command_received_; // Signaled for each command received. |
| 848 | LockingCommandMessageQueue event_command_queue_; |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 849 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 850 | Isolate* isolate_; |
mikhail.naganov@gmail.com | 2276287 | 2010-07-14 09:29:05 +0000 | [diff] [blame] | 851 | |
ager@chromium.org | 71daaf6 | 2009-04-01 07:22:49 +0000 | [diff] [blame] | 852 | friend class EnterDebugger; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 853 | friend class Isolate; |
| 854 | |
| 855 | DISALLOW_COPY_AND_ASSIGN(Debugger); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 856 | }; |
| 857 | |
| 858 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 859 | // This class is used for entering the debugger. Create an instance in the stack |
| 860 | // to enter the debugger. This will set the current break state, make sure the |
| 861 | // debugger is loaded and switch to the debugger context. If the debugger for |
| 862 | // some reason could not be entered FailedToEnter will return true. |
| 863 | class EnterDebugger BASE_EMBEDDED { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 864 | public: |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 865 | EnterDebugger() |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 866 | : isolate_(Isolate::Current()), |
| 867 | prev_(isolate_->debug()->debugger_entry()), |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 868 | it_(isolate_), |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 869 | has_js_frames_(!it_.done()), |
| 870 | save_(isolate_) { |
| 871 | Debug* debug = isolate_->debug(); |
| 872 | ASSERT(prev_ != NULL || !debug->is_interrupt_pending(PREEMPT)); |
| 873 | ASSERT(prev_ != NULL || !debug->is_interrupt_pending(DEBUGBREAK)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 874 | |
| 875 | // Link recursive debugger entry. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 876 | debug->set_debugger_entry(this); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 877 | |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 878 | // Store the previous break id and frame id. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 879 | break_id_ = debug->break_id(); |
| 880 | break_frame_id_ = debug->break_frame_id(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 881 | |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 882 | // Create the new break info. If there is no JavaScript frames there is no |
| 883 | // break frame id. |
| 884 | if (has_js_frames_) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 885 | debug->NewBreak(it_.frame()->id()); |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 886 | } else { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 887 | debug->NewBreak(StackFrame::NO_ID); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 888 | } |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 889 | |
| 890 | // Make sure that debugger is loaded and enter the debugger context. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 891 | load_failed_ = !debug->Load(); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 892 | if (!load_failed_) { |
| 893 | // NOTE the member variable save which saves the previous context before |
| 894 | // this change. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 895 | isolate_->set_context(*debug->debug_context()); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 896 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 897 | } |
| 898 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 899 | ~EnterDebugger() { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 900 | ASSERT(Isolate::Current() == isolate_); |
| 901 | Debug* debug = isolate_->debug(); |
| 902 | |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 903 | // Restore to the previous break state. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 904 | debug->SetBreak(break_frame_id_, break_id_); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 905 | |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 906 | // Check for leaving the debugger. |
ager@chromium.org | 71daaf6 | 2009-04-01 07:22:49 +0000 | [diff] [blame] | 907 | if (prev_ == NULL) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 908 | // Clear mirror cache when leaving the debugger. Skip this if there is a |
| 909 | // pending exception as clearing the mirror cache calls back into |
| 910 | // JavaScript. This can happen if the v8::Debug::Call is used in which |
| 911 | // case the exception should end up in the calling code. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 912 | if (!isolate_->has_pending_exception()) { |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 913 | // Try to avoid any pending debug break breaking in the clear mirror |
| 914 | // cache JavaScript code. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 915 | if (isolate_->stack_guard()->IsDebugBreak()) { |
| 916 | debug->set_interrupts_pending(DEBUGBREAK); |
| 917 | isolate_->stack_guard()->Continue(DEBUGBREAK); |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 918 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 919 | debug->ClearMirrorCache(); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 920 | } |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 921 | |
| 922 | // Request preemption and debug break when leaving the last debugger entry |
| 923 | // if any of these where recorded while debugging. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 924 | if (debug->is_interrupt_pending(PREEMPT)) { |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 925 | // This re-scheduling of preemption is to avoid starvation in some |
| 926 | // debugging scenarios. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 927 | debug->clear_interrupt_pending(PREEMPT); |
| 928 | isolate_->stack_guard()->Preempt(); |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 929 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 930 | if (debug->is_interrupt_pending(DEBUGBREAK)) { |
| 931 | debug->clear_interrupt_pending(DEBUGBREAK); |
| 932 | isolate_->stack_guard()->DebugBreak(); |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 933 | } |
| 934 | |
| 935 | // If there are commands in the queue when leaving the debugger request |
| 936 | // that these commands are processed. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 937 | if (isolate_->debugger()->HasCommands()) { |
| 938 | isolate_->stack_guard()->DebugCommand(); |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 939 | } |
| 940 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 941 | // If leaving the debugger with the debugger no longer active unload it. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 942 | if (!isolate_->debugger()->IsDebuggerActive()) { |
| 943 | isolate_->debugger()->UnloadDebugger(); |
ager@chromium.org | 71daaf6 | 2009-04-01 07:22:49 +0000 | [diff] [blame] | 944 | } |
| 945 | } |
| 946 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 947 | // Leaving this debugger entry. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 948 | debug->set_debugger_entry(prev_); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 949 | } |
| 950 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 951 | // Check whether the debugger could be entered. |
| 952 | inline bool FailedToEnter() { return load_failed_; } |
| 953 | |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 954 | // Check whether there are any JavaScript frames on the stack. |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 955 | inline bool HasJavaScriptFrames() { return has_js_frames_; } |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 956 | |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 957 | // Get the active context from before entering the debugger. |
| 958 | inline Handle<Context> GetContext() { return save_.context(); } |
| 959 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 960 | private: |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 961 | Isolate* isolate_; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 962 | EnterDebugger* prev_; // Previous debugger entry if entered recursively. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 963 | JavaScriptFrameIterator it_; |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 964 | const bool has_js_frames_; // Were there any JavaScript frames? |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 965 | StackFrame::Id break_frame_id_; // Previous break frame id. |
| 966 | int break_id_; // Previous break id. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 967 | bool load_failed_; // Did the debugger fail to load? |
| 968 | SaveContext save_; // Saves previous context. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 969 | }; |
| 970 | |
| 971 | |
kasper.lund | bd3ec4e | 2008-07-09 11:06:54 +0000 | [diff] [blame] | 972 | // Stack allocated class for disabling break. |
| 973 | class DisableBreak BASE_EMBEDDED { |
| 974 | public: |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 975 | explicit DisableBreak(bool disable_break) : isolate_(Isolate::Current()) { |
| 976 | prev_disable_break_ = isolate_->debug()->disable_break(); |
| 977 | isolate_->debug()->set_disable_break(disable_break); |
kasper.lund | bd3ec4e | 2008-07-09 11:06:54 +0000 | [diff] [blame] | 978 | } |
| 979 | ~DisableBreak() { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 980 | ASSERT(Isolate::Current() == isolate_); |
| 981 | isolate_->debug()->set_disable_break(prev_disable_break_); |
kasper.lund | bd3ec4e | 2008-07-09 11:06:54 +0000 | [diff] [blame] | 982 | } |
| 983 | |
| 984 | private: |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 985 | Isolate* isolate_; |
kasper.lund | bd3ec4e | 2008-07-09 11:06:54 +0000 | [diff] [blame] | 986 | // The previous state of the disable break used to restore the value when this |
| 987 | // object is destructed. |
| 988 | bool prev_disable_break_; |
| 989 | }; |
| 990 | |
| 991 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 992 | // Debug_Address encapsulates the Address pointers used in generating debug |
| 993 | // code. |
| 994 | class Debug_Address { |
| 995 | public: |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 996 | explicit Debug_Address(Debug::AddressId id) : id_(id) { } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 997 | |
| 998 | static Debug_Address AfterBreakTarget() { |
| 999 | return Debug_Address(Debug::k_after_break_target_address); |
| 1000 | } |
| 1001 | |
| 1002 | static Debug_Address DebugBreakReturn() { |
| 1003 | return Debug_Address(Debug::k_debug_break_return_address); |
| 1004 | } |
| 1005 | |
whesse@chromium.org | e90029b | 2010-08-02 11:52:17 +0000 | [diff] [blame] | 1006 | static Debug_Address RestarterFrameFunctionPointer() { |
| 1007 | return Debug_Address(Debug::k_restarter_frame_function_pointer); |
| 1008 | } |
| 1009 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1010 | Address address(Isolate* isolate) const { |
| 1011 | Debug* debug = isolate->debug(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1012 | switch (id_) { |
| 1013 | case Debug::k_after_break_target_address: |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1014 | return reinterpret_cast<Address>(debug->after_break_target_address()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1015 | case Debug::k_debug_break_return_address: |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1016 | return reinterpret_cast<Address>(debug->debug_break_return_address()); |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 1017 | case Debug::k_debug_break_slot_address: |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1018 | return reinterpret_cast<Address>(debug->debug_break_slot_address()); |
whesse@chromium.org | e90029b | 2010-08-02 11:52:17 +0000 | [diff] [blame] | 1019 | case Debug::k_restarter_frame_function_pointer: |
| 1020 | return reinterpret_cast<Address>( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1021 | debug->restarter_frame_function_pointer_address()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1022 | default: |
| 1023 | UNREACHABLE(); |
| 1024 | return NULL; |
| 1025 | } |
| 1026 | } |
| 1027 | private: |
| 1028 | Debug::AddressId id_; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1029 | }; |
| 1030 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1031 | // The optional thread that Debug Agent may use to temporary call V8 to process |
| 1032 | // pending debug requests if debuggee is not running V8 at the moment. |
| 1033 | // Techincally it does not call V8 itself, rather it asks embedding program |
| 1034 | // to do this via v8::Debug::HostDispatchHandler |
| 1035 | class MessageDispatchHelperThread: public Thread { |
| 1036 | public: |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1037 | explicit MessageDispatchHelperThread(Isolate* isolate); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1038 | ~MessageDispatchHelperThread(); |
| 1039 | |
| 1040 | void Schedule(); |
| 1041 | |
| 1042 | private: |
| 1043 | void Run(); |
| 1044 | |
| 1045 | Semaphore* const sem_; |
| 1046 | Mutex* const mutex_; |
| 1047 | bool already_signalled_; |
| 1048 | |
| 1049 | DISALLOW_COPY_AND_ASSIGN(MessageDispatchHelperThread); |
| 1050 | }; |
| 1051 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1052 | |
| 1053 | } } // namespace v8::internal |
| 1054 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 1055 | #endif // ENABLE_DEBUGGER_SUPPORT |
| 1056 | |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 1057 | #endif // V8_DEBUG_H_ |