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 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 31 | #include "assembler.h" |
| 32 | #include "code-stubs.h" |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 33 | #include "debug-agent.h" |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 34 | #include "execution.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 35 | #include "factory.h" |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 36 | #include "hashmap.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 37 | #include "platform.h" |
| 38 | #include "string-stream.h" |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 39 | #include "v8threads.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 40 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 41 | #ifdef ENABLE_DEBUGGER_SUPPORT |
| 42 | #include "../include/v8-debug.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 43 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 44 | namespace v8 { |
| 45 | namespace internal { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 46 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 47 | |
| 48 | // Forward declarations. |
| 49 | class EnterDebugger; |
| 50 | |
| 51 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 52 | // Step actions. NOTE: These values are in macros.py as well. |
| 53 | enum StepAction { |
| 54 | StepNone = -1, // Stepping not prepared. |
| 55 | StepOut = 0, // Step out of the current function. |
| 56 | StepNext = 1, // Step to the next statement in the current function. |
| 57 | StepIn = 2, // Step into new functions invoked or the next statement |
| 58 | // in the current function. |
| 59 | StepMin = 3, // Perform a minimum step in the current function. |
| 60 | StepInMin = 4 // Step into new functions invoked or perform a minimum step |
| 61 | // in the current function. |
| 62 | }; |
| 63 | |
| 64 | |
| 65 | // Type of exception break. NOTE: These values are in macros.py as well. |
| 66 | enum ExceptionBreakType { |
| 67 | BreakException = 0, |
| 68 | BreakUncaughtException = 1 |
| 69 | }; |
| 70 | |
| 71 | |
| 72 | // Type of exception break. NOTE: These values are in macros.py as well. |
| 73 | enum BreakLocatorType { |
| 74 | ALL_BREAK_LOCATIONS = 0, |
| 75 | SOURCE_BREAK_LOCATIONS = 1 |
| 76 | }; |
| 77 | |
| 78 | |
| 79 | // Class for iterating through the break points in a function and changing |
| 80 | // them. |
| 81 | class BreakLocationIterator { |
| 82 | public: |
| 83 | explicit BreakLocationIterator(Handle<DebugInfo> debug_info, |
| 84 | BreakLocatorType type); |
| 85 | virtual ~BreakLocationIterator(); |
| 86 | |
| 87 | void Next(); |
| 88 | void Next(int count); |
| 89 | void FindBreakLocationFromAddress(Address pc); |
| 90 | void FindBreakLocationFromPosition(int position); |
| 91 | void Reset(); |
| 92 | bool Done() const; |
| 93 | void SetBreakPoint(Handle<Object> break_point_object); |
| 94 | void ClearBreakPoint(Handle<Object> break_point_object); |
| 95 | void SetOneShot(); |
| 96 | void ClearOneShot(); |
| 97 | void PrepareStepIn(); |
| 98 | bool IsExit() const; |
| 99 | bool HasBreakPoint(); |
| 100 | bool IsDebugBreak(); |
| 101 | Object* BreakPointObjects(); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 102 | void ClearAllDebugBreak(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 103 | |
| 104 | |
| 105 | inline int code_position() { return pc() - debug_info_->code()->entry(); } |
| 106 | inline int break_point() { return break_point_; } |
| 107 | inline int position() { return position_; } |
| 108 | inline int statement_position() { return statement_position_; } |
| 109 | inline Address pc() { return reloc_iterator_->rinfo()->pc(); } |
| 110 | inline Code* code() { return debug_info_->code(); } |
| 111 | inline RelocInfo* rinfo() { return reloc_iterator_->rinfo(); } |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 112 | inline RelocInfo::Mode rmode() const { |
| 113 | return reloc_iterator_->rinfo()->rmode(); |
| 114 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 115 | inline RelocInfo* original_rinfo() { |
| 116 | return reloc_iterator_original_->rinfo(); |
| 117 | } |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 118 | inline RelocInfo::Mode original_rmode() const { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 119 | return reloc_iterator_original_->rinfo()->rmode(); |
| 120 | } |
| 121 | |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 122 | bool IsDebuggerStatement(); |
| 123 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 124 | protected: |
| 125 | bool RinfoDone() const; |
| 126 | void RinfoNext(); |
| 127 | |
| 128 | BreakLocatorType type_; |
| 129 | int break_point_; |
| 130 | int position_; |
| 131 | int statement_position_; |
| 132 | Handle<DebugInfo> debug_info_; |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 133 | Handle<Code> debug_break_stub_; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 134 | RelocIterator* reloc_iterator_; |
| 135 | RelocIterator* reloc_iterator_original_; |
| 136 | |
| 137 | private: |
| 138 | void SetDebugBreak(); |
| 139 | void ClearDebugBreak(); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 140 | |
| 141 | void SetDebugBreakAtIC(); |
| 142 | void ClearDebugBreakAtIC(); |
| 143 | |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 144 | bool IsDebugBreakAtReturn(); |
| 145 | void SetDebugBreakAtReturn(); |
| 146 | void ClearDebugBreakAtReturn(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 147 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 148 | DISALLOW_COPY_AND_ASSIGN(BreakLocationIterator); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 149 | }; |
| 150 | |
| 151 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 152 | // Cache of all script objects in the heap. When a script is added a weak handle |
| 153 | // to it is created and that weak handle is stored in the cache. The weak handle |
| 154 | // callback takes care of removing the script from the cache. The key used in |
| 155 | // the cache is the script id. |
| 156 | class ScriptCache : private HashMap { |
| 157 | public: |
| 158 | ScriptCache() : HashMap(ScriptMatch), collected_scripts_(10) {} |
| 159 | virtual ~ScriptCache() { Clear(); } |
| 160 | |
| 161 | // Add script to the cache. |
| 162 | void Add(Handle<Script> script); |
| 163 | |
| 164 | // Return the scripts in the cache. |
| 165 | Handle<FixedArray> GetScripts(); |
| 166 | |
| 167 | // Generate debugger events for collected scripts. |
| 168 | void ProcessCollectedScripts(); |
| 169 | |
| 170 | private: |
| 171 | // Calculate the hash value from the key (script id). |
| 172 | static uint32_t Hash(int key) { return ComputeIntegerHash(key); } |
| 173 | |
| 174 | // Scripts match if their keys (script id) match. |
| 175 | static bool ScriptMatch(void* key1, void* key2) { return key1 == key2; } |
| 176 | |
| 177 | // Clear the cache releasing all the weak handles. |
| 178 | void Clear(); |
| 179 | |
| 180 | // Weak handle callback for scripts in the cache. |
| 181 | static void HandleWeakScript(v8::Persistent<v8::Value> obj, void* data); |
| 182 | |
| 183 | // List used during GC to temporarily store id's of collected scripts. |
| 184 | List<int> collected_scripts_; |
| 185 | }; |
| 186 | |
| 187 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 188 | // Linked list holding debug info objects. The debug info objects are kept as |
| 189 | // weak handles to avoid a debug info object to keep a function alive. |
| 190 | class DebugInfoListNode { |
| 191 | public: |
| 192 | explicit DebugInfoListNode(DebugInfo* debug_info); |
| 193 | virtual ~DebugInfoListNode(); |
| 194 | |
| 195 | DebugInfoListNode* next() { return next_; } |
| 196 | void set_next(DebugInfoListNode* next) { next_ = next; } |
| 197 | Handle<DebugInfo> debug_info() { return debug_info_; } |
| 198 | |
| 199 | private: |
| 200 | // Global (weak) handle to the debug info object. |
| 201 | Handle<DebugInfo> debug_info_; |
| 202 | |
| 203 | // Next pointer for linked list. |
| 204 | DebugInfoListNode* next_; |
| 205 | }; |
| 206 | |
| 207 | |
| 208 | // This class contains the debugger support. The main purpose is to handle |
| 209 | // setting break points in the code. |
| 210 | // |
| 211 | // This class controls the debug info for all functions which currently have |
| 212 | // active breakpoints in them. This debug info is held in the heap root object |
| 213 | // debug_info which is a FixedArray. Each entry in this list is of class |
| 214 | // DebugInfo. |
| 215 | class Debug { |
| 216 | public: |
| 217 | static void Setup(bool create_heap_objects); |
| 218 | static bool Load(); |
| 219 | static void Unload(); |
| 220 | static bool IsLoaded() { return !debug_context_.is_null(); } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 221 | static bool InDebugger() { return thread_local_.debugger_entry_ != NULL; } |
| 222 | static void PreemptionWhileInDebugger(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 223 | static void Iterate(ObjectVisitor* v); |
| 224 | |
| 225 | static Object* Break(Arguments args); |
| 226 | static void SetBreakPoint(Handle<SharedFunctionInfo> shared, |
| 227 | int source_position, |
| 228 | Handle<Object> break_point_object); |
| 229 | static void ClearBreakPoint(Handle<Object> break_point_object); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 230 | static void ClearAllBreakPoints(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 231 | static void FloodWithOneShot(Handle<SharedFunctionInfo> shared); |
| 232 | static void FloodHandlerWithOneShot(); |
| 233 | static void ChangeBreakOnException(ExceptionBreakType type, bool enable); |
| 234 | static void PrepareStep(StepAction step_action, int step_count); |
| 235 | static void ClearStepping(); |
| 236 | static bool StepNextContinue(BreakLocationIterator* break_location_iterator, |
| 237 | JavaScriptFrame* frame); |
| 238 | static Handle<DebugInfo> GetDebugInfo(Handle<SharedFunctionInfo> shared); |
| 239 | static bool HasDebugInfo(Handle<SharedFunctionInfo> shared); |
kasper.lund | bd3ec4e | 2008-07-09 11:06:54 +0000 | [diff] [blame] | 240 | |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 241 | // Returns whether the operation succeeded. |
kasper.lund | bd3ec4e | 2008-07-09 11:06:54 +0000 | [diff] [blame] | 242 | static bool EnsureDebugInfo(Handle<SharedFunctionInfo> shared); |
| 243 | |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 244 | // 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] | 245 | static bool IsDebugBreak(Address addr); |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 246 | // Returns true if the current return statement has been patched to be |
| 247 | // a debugger breakpoint. |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 248 | static bool IsDebugBreakAtReturn(RelocInfo* rinfo); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 249 | |
| 250 | // Check whether a code stub with the specified major key is a possible break |
| 251 | // point location. |
| 252 | static bool IsSourceBreakStub(Code* code); |
| 253 | static bool IsBreakStub(Code* code); |
| 254 | |
| 255 | // Find the builtin to use for invoking the debug break |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 256 | static Handle<Code> FindDebugBreak(Handle<Code> code, RelocInfo::Mode mode); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 257 | |
| 258 | static Handle<Object> GetSourceBreakLocations( |
| 259 | Handle<SharedFunctionInfo> shared); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 260 | |
| 261 | // Getter for the debug_context. |
| 262 | inline static Handle<Context> debug_context() { return debug_context_; } |
| 263 | |
| 264 | // Check whether a global object is the debug global object. |
| 265 | static bool IsDebugGlobal(GlobalObject* global); |
| 266 | |
| 267 | // Fast check to see if any break points are active. |
| 268 | inline static bool has_break_points() { return has_break_points_; } |
| 269 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 270 | static void NewBreak(StackFrame::Id break_frame_id); |
| 271 | static void SetBreak(StackFrame::Id break_frame_id, int break_id); |
| 272 | static StackFrame::Id break_frame_id() { |
| 273 | return thread_local_.break_frame_id_; |
| 274 | } |
| 275 | static int break_id() { return thread_local_.break_id_; } |
| 276 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 277 | static bool StepInActive() { return thread_local_.step_into_fp_ != 0; } |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 278 | static void HandleStepIn(Handle<JSFunction> function, |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 279 | Handle<Object> holder, |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 280 | Address fp, |
| 281 | bool is_constructor); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 282 | static Address step_in_fp() { return thread_local_.step_into_fp_; } |
| 283 | static Address* step_in_fp_addr() { return &thread_local_.step_into_fp_; } |
| 284 | |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 285 | static bool StepOutActive() { return thread_local_.step_out_fp_ != 0; } |
| 286 | static Address step_out_fp() { return thread_local_.step_out_fp_; } |
| 287 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 288 | static EnterDebugger* debugger_entry() { |
| 289 | return thread_local_.debugger_entry_; |
| 290 | } |
| 291 | static void set_debugger_entry(EnterDebugger* entry) { |
| 292 | thread_local_.debugger_entry_ = entry; |
| 293 | } |
| 294 | |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 295 | // Check whether any of the specified interrupts are pending. |
| 296 | static bool is_interrupt_pending(InterruptFlag what) { |
| 297 | return (thread_local_.pending_interrupts_ & what) != 0; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 298 | } |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 299 | |
| 300 | // Set specified interrupts as pending. |
| 301 | static void set_interrupts_pending(InterruptFlag what) { |
| 302 | thread_local_.pending_interrupts_ |= what; |
| 303 | } |
| 304 | |
| 305 | // Clear specified interrupts from pending. |
| 306 | static void clear_interrupt_pending(InterruptFlag what) { |
| 307 | thread_local_.pending_interrupts_ &= ~static_cast<int>(what); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 308 | } |
| 309 | |
kasper.lund | bd3ec4e | 2008-07-09 11:06:54 +0000 | [diff] [blame] | 310 | // Getter and setter for the disable break state. |
| 311 | static bool disable_break() { return disable_break_; } |
| 312 | static void set_disable_break(bool disable_break) { |
| 313 | disable_break_ = disable_break; |
| 314 | } |
| 315 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 316 | // Getters for the current exception break state. |
| 317 | static bool break_on_exception() { return break_on_exception_; } |
| 318 | static bool break_on_uncaught_exception() { |
| 319 | return break_on_uncaught_exception_; |
| 320 | } |
| 321 | |
| 322 | enum AddressId { |
| 323 | k_after_break_target_address, |
| 324 | k_debug_break_return_address, |
| 325 | k_register_address |
| 326 | }; |
| 327 | |
| 328 | // Support for setting the address to jump to when returning from break point. |
| 329 | static Address* after_break_target_address() { |
| 330 | return reinterpret_cast<Address*>(&thread_local_.after_break_target_); |
| 331 | } |
| 332 | |
| 333 | // Support for saving/restoring registers when handling debug break calls. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 334 | static Object** register_address(int r) { |
| 335 | return ®isters_[r]; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 336 | } |
| 337 | |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 338 | // Access to the debug break on return code. |
| 339 | static Code* debug_break_return() { return debug_break_return_; } |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 340 | static Code** debug_break_return_address() { |
| 341 | return &debug_break_return_; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 342 | } |
| 343 | |
| 344 | static const int kEstimatedNofDebugInfoEntries = 16; |
| 345 | static const int kEstimatedNofBreakPointsInFunction = 16; |
| 346 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 347 | static void HandleWeakDebugInfo(v8::Persistent<v8::Value> obj, void* data); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 348 | |
| 349 | friend class Debugger; |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 350 | friend Handle<FixedArray> GetDebuggedFunctions(); // In test-debug.cc |
| 351 | friend void CheckDebuggerUnloaded(bool check_functions); // In test-debug.cc |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 352 | |
| 353 | // Threading support. |
| 354 | static char* ArchiveDebug(char* to); |
| 355 | static char* RestoreDebug(char* from); |
| 356 | static int ArchiveSpacePerThread(); |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame^] | 357 | static void FreeThreadResources() { } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 358 | |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 359 | // Mirror cache handling. |
| 360 | static void ClearMirrorCache(); |
| 361 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 362 | // Script cache handling. |
| 363 | static void CreateScriptCache(); |
| 364 | static void DestroyScriptCache(); |
| 365 | static void AddScriptToScriptCache(Handle<Script> script); |
| 366 | static Handle<FixedArray> GetLoadedScripts(); |
| 367 | |
| 368 | // Garbage collection notifications. |
| 369 | static void AfterGarbageCollection(); |
| 370 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 371 | // Code generation assumptions. |
| 372 | static const int kIa32CallInstructionLength = 5; |
| 373 | static const int kIa32JSReturnSequenceLength = 6; |
| 374 | |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 375 | // The x64 JS return sequence is padded with int3 to make it large |
| 376 | // enough to hold a call instruction when the debugger patches it. |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 377 | static const int kX64CallInstructionLength = 13; |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 378 | static const int kX64JSReturnSequenceLength = 13; |
| 379 | |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 380 | // Code generator routines. |
| 381 | static void GenerateLoadICDebugBreak(MacroAssembler* masm); |
| 382 | static void GenerateStoreICDebugBreak(MacroAssembler* masm); |
| 383 | static void GenerateKeyedLoadICDebugBreak(MacroAssembler* masm); |
| 384 | static void GenerateKeyedStoreICDebugBreak(MacroAssembler* masm); |
| 385 | static void GenerateConstructCallDebugBreak(MacroAssembler* masm); |
| 386 | static void GenerateReturnDebugBreak(MacroAssembler* masm); |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 387 | static void GenerateStubNoRegistersDebugBreak(MacroAssembler* masm); |
| 388 | |
| 389 | // Called from stub-cache.cc. |
| 390 | static void GenerateCallICDebugBreak(MacroAssembler* masm); |
| 391 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 392 | private: |
| 393 | static bool CompileDebuggerScript(int index); |
| 394 | static void ClearOneShot(); |
| 395 | static void ActivateStepIn(StackFrame* frame); |
| 396 | static void ClearStepIn(); |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 397 | static void ActivateStepOut(StackFrame* frame); |
| 398 | static void ClearStepOut(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 399 | static void ClearStepNext(); |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 400 | // Returns whether the compile succeeded. |
kasper.lund | bd3ec4e | 2008-07-09 11:06:54 +0000 | [diff] [blame] | 401 | static bool EnsureCompiled(Handle<SharedFunctionInfo> shared); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 402 | static void RemoveDebugInfo(Handle<DebugInfo> debug_info); |
| 403 | static void SetAfterBreakTarget(JavaScriptFrame* frame); |
| 404 | static Handle<Object> CheckBreakPoints(Handle<Object> break_point); |
| 405 | static bool CheckBreakPoint(Handle<Object> break_point_object); |
| 406 | |
| 407 | // Global handle to debug context where all the debugger JavaScript code is |
| 408 | // loaded. |
| 409 | static Handle<Context> debug_context_; |
| 410 | |
| 411 | // Boolean state indicating whether any break points are set. |
| 412 | static bool has_break_points_; |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 413 | |
| 414 | // Cache of all scripts in the heap. |
| 415 | static ScriptCache* script_cache_; |
| 416 | |
| 417 | // List of active debug info objects. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 418 | static DebugInfoListNode* debug_info_list_; |
| 419 | |
kasper.lund | bd3ec4e | 2008-07-09 11:06:54 +0000 | [diff] [blame] | 420 | static bool disable_break_; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 421 | static bool break_on_exception_; |
| 422 | static bool break_on_uncaught_exception_; |
| 423 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 424 | // Per-thread data. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 425 | class ThreadLocal { |
| 426 | public: |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 427 | // Counter for generating next break id. |
| 428 | int break_count_; |
| 429 | |
| 430 | // Current break id. |
| 431 | int break_id_; |
| 432 | |
| 433 | // Frame id for the frame of the current break. |
| 434 | StackFrame::Id break_frame_id_; |
| 435 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 436 | // Step action for last step performed. |
| 437 | StepAction last_step_action_; |
| 438 | |
| 439 | // Source statement position from last step next action. |
| 440 | int last_statement_position_; |
| 441 | |
| 442 | // Number of steps left to perform before debug event. |
| 443 | int step_count_; |
| 444 | |
| 445 | // Frame pointer from last step next action. |
| 446 | Address last_fp_; |
| 447 | |
| 448 | // Frame pointer for frame from which step in was performed. |
| 449 | Address step_into_fp_; |
| 450 | |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 451 | // Frame pointer for the frame where debugger should be called when current |
| 452 | // step out action is completed. |
| 453 | Address step_out_fp_; |
| 454 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 455 | // Storage location for jump when exiting debug break calls. |
| 456 | Address after_break_target_; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 457 | |
| 458 | // Top debugger entry. |
| 459 | EnterDebugger* debugger_entry_; |
| 460 | |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 461 | // Pending interrupts scheduled while debugging. |
| 462 | int pending_interrupts_; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 463 | }; |
| 464 | |
| 465 | // Storage location for registers when handling debug break calls |
| 466 | static JSCallerSavedBuffer registers_; |
| 467 | static ThreadLocal thread_local_; |
| 468 | static void ThreadInit(); |
| 469 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 470 | // Code to call for handling debug break on return. |
| 471 | static Code* debug_break_return_; |
| 472 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 473 | DISALLOW_COPY_AND_ASSIGN(Debug); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 474 | }; |
| 475 | |
| 476 | |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 477 | // Message delivered to the message handler callback. This is either a debugger |
| 478 | // event or the response to a command. |
| 479 | class MessageImpl: public v8::Debug::Message { |
| 480 | public: |
| 481 | // Create a message object for a debug event. |
| 482 | static MessageImpl NewEvent(DebugEvent event, |
| 483 | bool running, |
| 484 | Handle<JSObject> exec_state, |
| 485 | Handle<JSObject> event_data); |
| 486 | |
| 487 | // Create a message object for the response to a debug command. |
| 488 | static MessageImpl NewResponse(DebugEvent event, |
| 489 | bool running, |
| 490 | Handle<JSObject> exec_state, |
| 491 | Handle<JSObject> event_data, |
| 492 | Handle<String> response_json, |
| 493 | v8::Debug::ClientData* client_data); |
| 494 | |
| 495 | // Implementation of interface v8::Debug::Message. |
| 496 | virtual bool IsEvent() const; |
| 497 | virtual bool IsResponse() const; |
| 498 | virtual DebugEvent GetEvent() const; |
| 499 | virtual bool WillStartRunning() const; |
| 500 | virtual v8::Handle<v8::Object> GetExecutionState() const; |
| 501 | virtual v8::Handle<v8::Object> GetEventData() const; |
| 502 | virtual v8::Handle<v8::String> GetJSON() const; |
| 503 | virtual v8::Handle<v8::Context> GetEventContext() const; |
| 504 | virtual v8::Debug::ClientData* GetClientData() const; |
| 505 | |
| 506 | private: |
| 507 | MessageImpl(bool is_event, |
| 508 | DebugEvent event, |
| 509 | bool running, |
| 510 | Handle<JSObject> exec_state, |
| 511 | Handle<JSObject> event_data, |
| 512 | Handle<String> response_json, |
| 513 | v8::Debug::ClientData* client_data); |
| 514 | |
| 515 | bool is_event_; // Does this message represent a debug event? |
| 516 | DebugEvent event_; // Debug event causing the break. |
| 517 | bool running_; // Will the VM start running after this event? |
| 518 | Handle<JSObject> exec_state_; // Current execution state. |
| 519 | Handle<JSObject> event_data_; // Data associated with the event. |
| 520 | Handle<String> response_json_; // Response JSON if message holds a response. |
| 521 | v8::Debug::ClientData* client_data_; // Client data passed with the request. |
| 522 | }; |
| 523 | |
| 524 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 525 | // Message send by user to v8 debugger or debugger output message. |
| 526 | // In addition to command text it may contain a pointer to some user data |
| 527 | // which are expected to be passed along with the command reponse to message |
| 528 | // handler. |
ager@chromium.org | 3a37e9b | 2009-04-27 09:26:21 +0000 | [diff] [blame] | 529 | class CommandMessage { |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 530 | public: |
ager@chromium.org | 3a37e9b | 2009-04-27 09:26:21 +0000 | [diff] [blame] | 531 | static CommandMessage New(const Vector<uint16_t>& command, |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 532 | v8::Debug::ClientData* data); |
ager@chromium.org | 3a37e9b | 2009-04-27 09:26:21 +0000 | [diff] [blame] | 533 | CommandMessage(); |
| 534 | ~CommandMessage(); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 535 | |
| 536 | // Deletes user data and disposes of the text. |
| 537 | void Dispose(); |
| 538 | Vector<uint16_t> text() const { return text_; } |
| 539 | v8::Debug::ClientData* client_data() const { return client_data_; } |
| 540 | private: |
ager@chromium.org | 3a37e9b | 2009-04-27 09:26:21 +0000 | [diff] [blame] | 541 | CommandMessage(const Vector<uint16_t>& text, |
| 542 | v8::Debug::ClientData* data); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 543 | |
| 544 | Vector<uint16_t> text_; |
| 545 | v8::Debug::ClientData* client_data_; |
| 546 | }; |
| 547 | |
ager@chromium.org | 3a37e9b | 2009-04-27 09:26:21 +0000 | [diff] [blame] | 548 | // A Queue of CommandMessage objects. A thread-safe version is |
| 549 | // LockingCommandMessageQueue, based on this class. |
| 550 | class CommandMessageQueue BASE_EMBEDDED { |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 551 | public: |
ager@chromium.org | 3a37e9b | 2009-04-27 09:26:21 +0000 | [diff] [blame] | 552 | explicit CommandMessageQueue(int size); |
| 553 | ~CommandMessageQueue(); |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 554 | bool IsEmpty() const { return start_ == end_; } |
ager@chromium.org | 3a37e9b | 2009-04-27 09:26:21 +0000 | [diff] [blame] | 555 | CommandMessage Get(); |
| 556 | void Put(const CommandMessage& message); |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 557 | void Clear() { start_ = end_ = 0; } // Queue is empty after Clear(). |
| 558 | private: |
| 559 | // Doubles the size of the message queue, and copies the messages. |
| 560 | void Expand(); |
| 561 | |
ager@chromium.org | 3a37e9b | 2009-04-27 09:26:21 +0000 | [diff] [blame] | 562 | CommandMessage* messages_; |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 563 | int start_; |
| 564 | int end_; |
| 565 | int size_; // The size of the queue buffer. Queue can hold size-1 messages. |
| 566 | }; |
| 567 | |
| 568 | |
ager@chromium.org | 3a37e9b | 2009-04-27 09:26:21 +0000 | [diff] [blame] | 569 | // LockingCommandMessageQueue is a thread-safe circular buffer of CommandMessage |
| 570 | // messages. The message data is not managed by LockingCommandMessageQueue. |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 571 | // 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] | 572 | // Mutex to CommandMessageQueue. Includes logging of all puts and gets. |
| 573 | class LockingCommandMessageQueue BASE_EMBEDDED { |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 574 | public: |
ager@chromium.org | 3a37e9b | 2009-04-27 09:26:21 +0000 | [diff] [blame] | 575 | explicit LockingCommandMessageQueue(int size); |
| 576 | ~LockingCommandMessageQueue(); |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 577 | bool IsEmpty() const; |
ager@chromium.org | 3a37e9b | 2009-04-27 09:26:21 +0000 | [diff] [blame] | 578 | CommandMessage Get(); |
| 579 | void Put(const CommandMessage& message); |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 580 | void Clear(); |
| 581 | private: |
ager@chromium.org | 3a37e9b | 2009-04-27 09:26:21 +0000 | [diff] [blame] | 582 | CommandMessageQueue queue_; |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 583 | Mutex* lock_; |
ager@chromium.org | 3a37e9b | 2009-04-27 09:26:21 +0000 | [diff] [blame] | 584 | DISALLOW_COPY_AND_ASSIGN(LockingCommandMessageQueue); |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 585 | }; |
| 586 | |
| 587 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 588 | class Debugger { |
| 589 | public: |
| 590 | static void DebugRequest(const uint16_t* json_request, int length); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 591 | |
| 592 | static Handle<Object> MakeJSObject(Vector<const char> constructor_name, |
| 593 | int argc, Object*** argv, |
| 594 | bool* caught_exception); |
| 595 | static Handle<Object> MakeExecutionState(bool* caught_exception); |
| 596 | static Handle<Object> MakeBreakEvent(Handle<Object> exec_state, |
| 597 | Handle<Object> break_points_hit, |
| 598 | bool* caught_exception); |
| 599 | static Handle<Object> MakeExceptionEvent(Handle<Object> exec_state, |
| 600 | Handle<Object> exception, |
| 601 | bool uncaught, |
| 602 | bool* caught_exception); |
| 603 | static Handle<Object> MakeNewFunctionEvent(Handle<Object> func, |
| 604 | bool* caught_exception); |
| 605 | static Handle<Object> MakeCompileEvent(Handle<Script> script, |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 606 | bool before, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 607 | bool* caught_exception); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 608 | static Handle<Object> MakeScriptCollectedEvent(int id, |
| 609 | bool* caught_exception); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 610 | static void OnDebugBreak(Handle<Object> break_points_hit, bool auto_continue); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 611 | static void OnException(Handle<Object> exception, bool uncaught); |
| 612 | static void OnBeforeCompile(Handle<Script> script); |
| 613 | static void OnAfterCompile(Handle<Script> script, |
| 614 | Handle<JSFunction> fun); |
| 615 | static void OnNewFunction(Handle<JSFunction> fun); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 616 | static void OnScriptCollected(int id); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 617 | static void ProcessDebugEvent(v8::DebugEvent event, |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 618 | Handle<JSObject> event_data, |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 619 | bool auto_continue); |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 620 | static void NotifyMessageHandler(v8::DebugEvent event, |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 621 | Handle<JSObject> exec_state, |
| 622 | Handle<JSObject> event_data, |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 623 | bool auto_continue); |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 624 | static void SetEventListener(Handle<Object> callback, Handle<Object> data); |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 625 | static void SetMessageHandler(v8::Debug::MessageHandler2 handler); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 626 | static void SetHostDispatchHandler(v8::Debug::HostDispatchHandler handler, |
| 627 | int period); |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 628 | |
| 629 | // Invoke the message handler function. |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 630 | static void InvokeMessageHandler(MessageImpl message); |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 631 | |
| 632 | // Add a debugger command to the command queue. |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 633 | static void ProcessCommand(Vector<const uint16_t> command, |
| 634 | v8::Debug::ClientData* client_data = NULL); |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 635 | |
| 636 | // Check whether there are commands in the command queue. |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 637 | static bool HasCommands(); |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 638 | |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 639 | static Handle<Object> Call(Handle<JSFunction> fun, |
| 640 | Handle<Object> data, |
| 641 | bool* pending_exception); |
| 642 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 643 | // Start the debugger agent listening on the provided port. |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 644 | static bool StartAgent(const char* name, int port); |
| 645 | |
| 646 | // Stop the debugger agent. |
| 647 | static void StopAgent(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 648 | |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame^] | 649 | // Blocks until the agent has started listening for connections |
| 650 | static void WaitForAgent(); |
| 651 | |
ager@chromium.org | 71daaf6 | 2009-04-01 07:22:49 +0000 | [diff] [blame] | 652 | // Unload the debugger if possible. Only called when no debugger is currently |
| 653 | // active. |
| 654 | static void UnloadDebugger(); |
| 655 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 656 | inline static bool EventActive(v8::DebugEvent event) { |
ager@chromium.org | 71daaf6 | 2009-04-01 07:22:49 +0000 | [diff] [blame] | 657 | ScopedLock with(debugger_access_); |
| 658 | |
| 659 | // Check whether the message handler was been cleared. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 660 | if (debugger_unload_pending_) { |
ager@chromium.org | 71daaf6 | 2009-04-01 07:22:49 +0000 | [diff] [blame] | 661 | UnloadDebugger(); |
| 662 | } |
| 663 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 664 | // Currently argument event is not used. |
ager@chromium.org | 71daaf6 | 2009-04-01 07:22:49 +0000 | [diff] [blame] | 665 | return !compiling_natives_ && Debugger::IsDebuggerActive(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 666 | } |
| 667 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 668 | static void set_compiling_natives(bool compiling_natives) { |
| 669 | Debugger::compiling_natives_ = compiling_natives; |
| 670 | } |
| 671 | static bool compiling_natives() { return Debugger::compiling_natives_; } |
mads.s.ager | cbaa060 | 2008-08-14 13:41:48 +0000 | [diff] [blame] | 672 | static void set_loading_debugger(bool v) { is_loading_debugger_ = v; } |
| 673 | static bool is_loading_debugger() { return Debugger::is_loading_debugger_; } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 674 | |
| 675 | private: |
ager@chromium.org | 71daaf6 | 2009-04-01 07:22:49 +0000 | [diff] [blame] | 676 | static bool IsDebuggerActive(); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 677 | static void ListenersChanged(); |
ager@chromium.org | 71daaf6 | 2009-04-01 07:22:49 +0000 | [diff] [blame] | 678 | |
| 679 | static Mutex* debugger_access_; // Mutex guarding debugger variables. |
| 680 | static Handle<Object> event_listener_; // Global handle to listener. |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 681 | static Handle<Object> event_listener_data_; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 682 | static bool compiling_natives_; // Are we compiling natives? |
mads.s.ager | cbaa060 | 2008-08-14 13:41:48 +0000 | [diff] [blame] | 683 | static bool is_loading_debugger_; // Are we loading the debugger? |
ager@chromium.org | 71daaf6 | 2009-04-01 07:22:49 +0000 | [diff] [blame] | 684 | static bool never_unload_debugger_; // Can we unload the debugger? |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 685 | static v8::Debug::MessageHandler2 message_handler_; |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 686 | static bool debugger_unload_pending_; // Was message handler cleared? |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 687 | static v8::Debug::HostDispatchHandler host_dispatch_handler_; |
| 688 | static int host_dispatch_micros_; |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 689 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 690 | static DebuggerAgent* agent_; |
| 691 | |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 692 | static const int kQueueInitialSize = 4; |
ager@chromium.org | 3a37e9b | 2009-04-27 09:26:21 +0000 | [diff] [blame] | 693 | static LockingCommandMessageQueue command_queue_; |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 694 | static Semaphore* command_received_; // Signaled for each command received. |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 695 | |
ager@chromium.org | 71daaf6 | 2009-04-01 07:22:49 +0000 | [diff] [blame] | 696 | friend class EnterDebugger; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 697 | }; |
| 698 | |
| 699 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 700 | // This class is used for entering the debugger. Create an instance in the stack |
| 701 | // to enter the debugger. This will set the current break state, make sure the |
| 702 | // debugger is loaded and switch to the debugger context. If the debugger for |
| 703 | // some reason could not be entered FailedToEnter will return true. |
| 704 | class EnterDebugger BASE_EMBEDDED { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 705 | public: |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 706 | EnterDebugger() |
| 707 | : prev_(Debug::debugger_entry()), |
| 708 | has_js_frames_(!it_.done()) { |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 709 | ASSERT(prev_ != NULL || !Debug::is_interrupt_pending(PREEMPT)); |
| 710 | ASSERT(prev_ != NULL || !Debug::is_interrupt_pending(DEBUGBREAK)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 711 | |
| 712 | // Link recursive debugger entry. |
| 713 | Debug::set_debugger_entry(this); |
| 714 | |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 715 | // Store the previous break id and frame id. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 716 | break_id_ = Debug::break_id(); |
| 717 | break_frame_id_ = Debug::break_frame_id(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 718 | |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 719 | // Create the new break info. If there is no JavaScript frames there is no |
| 720 | // break frame id. |
| 721 | if (has_js_frames_) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 722 | Debug::NewBreak(it_.frame()->id()); |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 723 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 724 | Debug::NewBreak(StackFrame::NO_ID); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 725 | } |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 726 | |
| 727 | // Make sure that debugger is loaded and enter the debugger context. |
| 728 | load_failed_ = !Debug::Load(); |
| 729 | if (!load_failed_) { |
| 730 | // NOTE the member variable save which saves the previous context before |
| 731 | // this change. |
| 732 | Top::set_context(*Debug::debug_context()); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 733 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 734 | } |
| 735 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 736 | ~EnterDebugger() { |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 737 | // Restore to the previous break state. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 738 | Debug::SetBreak(break_frame_id_, break_id_); |
| 739 | |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 740 | // Check for leaving the debugger. |
ager@chromium.org | 71daaf6 | 2009-04-01 07:22:49 +0000 | [diff] [blame] | 741 | if (prev_ == NULL) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 742 | // Clear mirror cache when leaving the debugger. Skip this if there is a |
| 743 | // pending exception as clearing the mirror cache calls back into |
| 744 | // JavaScript. This can happen if the v8::Debug::Call is used in which |
| 745 | // case the exception should end up in the calling code. |
| 746 | if (!Top::has_pending_exception()) { |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 747 | // Try to avoid any pending debug break breaking in the clear mirror |
| 748 | // cache JavaScript code. |
| 749 | if (StackGuard::IsDebugBreak()) { |
| 750 | Debug::set_interrupts_pending(DEBUGBREAK); |
| 751 | StackGuard::Continue(DEBUGBREAK); |
| 752 | } |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 753 | Debug::ClearMirrorCache(); |
| 754 | } |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 755 | |
| 756 | // Request preemption and debug break when leaving the last debugger entry |
| 757 | // if any of these where recorded while debugging. |
| 758 | if (Debug::is_interrupt_pending(PREEMPT)) { |
| 759 | // This re-scheduling of preemption is to avoid starvation in some |
| 760 | // debugging scenarios. |
| 761 | Debug::clear_interrupt_pending(PREEMPT); |
| 762 | StackGuard::Preempt(); |
| 763 | } |
| 764 | if (Debug::is_interrupt_pending(DEBUGBREAK)) { |
| 765 | Debug::clear_interrupt_pending(DEBUGBREAK); |
| 766 | StackGuard::DebugBreak(); |
| 767 | } |
| 768 | |
| 769 | // If there are commands in the queue when leaving the debugger request |
| 770 | // that these commands are processed. |
| 771 | if (Debugger::HasCommands()) { |
| 772 | StackGuard::DebugCommand(); |
| 773 | } |
| 774 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 775 | // If leaving the debugger with the debugger no longer active unload it. |
ager@chromium.org | 71daaf6 | 2009-04-01 07:22:49 +0000 | [diff] [blame] | 776 | if (!Debugger::IsDebuggerActive()) { |
| 777 | Debugger::UnloadDebugger(); |
| 778 | } |
| 779 | } |
| 780 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 781 | // Leaving this debugger entry. |
| 782 | Debug::set_debugger_entry(prev_); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 783 | } |
| 784 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 785 | // Check whether the debugger could be entered. |
| 786 | inline bool FailedToEnter() { return load_failed_; } |
| 787 | |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 788 | // Check whether there are any JavaScript frames on the stack. |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 789 | inline bool HasJavaScriptFrames() { return has_js_frames_; } |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 790 | |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 791 | // Get the active context from before entering the debugger. |
| 792 | inline Handle<Context> GetContext() { return save_.context(); } |
| 793 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 794 | private: |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 795 | EnterDebugger* prev_; // Previous debugger entry if entered recursively. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 796 | JavaScriptFrameIterator it_; |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 797 | const bool has_js_frames_; // Were there any JavaScript frames? |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 798 | StackFrame::Id break_frame_id_; // Previous break frame id. |
| 799 | int break_id_; // Previous break id. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 800 | bool load_failed_; // Did the debugger fail to load? |
| 801 | SaveContext save_; // Saves previous context. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 802 | }; |
| 803 | |
| 804 | |
kasper.lund | bd3ec4e | 2008-07-09 11:06:54 +0000 | [diff] [blame] | 805 | // Stack allocated class for disabling break. |
| 806 | class DisableBreak BASE_EMBEDDED { |
| 807 | public: |
| 808 | // Enter the debugger by storing the previous top context and setting the |
| 809 | // current top context to the debugger context. |
| 810 | explicit DisableBreak(bool disable_break) { |
| 811 | prev_disable_break_ = Debug::disable_break(); |
| 812 | Debug::set_disable_break(disable_break); |
| 813 | } |
| 814 | ~DisableBreak() { |
| 815 | Debug::set_disable_break(prev_disable_break_); |
| 816 | } |
| 817 | |
| 818 | private: |
| 819 | // The previous state of the disable break used to restore the value when this |
| 820 | // object is destructed. |
| 821 | bool prev_disable_break_; |
| 822 | }; |
| 823 | |
| 824 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 825 | // Debug_Address encapsulates the Address pointers used in generating debug |
| 826 | // code. |
| 827 | class Debug_Address { |
| 828 | public: |
| 829 | Debug_Address(Debug::AddressId id, int reg = 0) |
| 830 | : id_(id), reg_(reg) { |
| 831 | ASSERT(reg == 0 || id == Debug::k_register_address); |
| 832 | } |
| 833 | |
| 834 | static Debug_Address AfterBreakTarget() { |
| 835 | return Debug_Address(Debug::k_after_break_target_address); |
| 836 | } |
| 837 | |
| 838 | static Debug_Address DebugBreakReturn() { |
| 839 | return Debug_Address(Debug::k_debug_break_return_address); |
| 840 | } |
| 841 | |
| 842 | static Debug_Address Register(int reg) { |
| 843 | return Debug_Address(Debug::k_register_address, reg); |
| 844 | } |
| 845 | |
| 846 | Address address() const { |
| 847 | switch (id_) { |
| 848 | case Debug::k_after_break_target_address: |
| 849 | return reinterpret_cast<Address>(Debug::after_break_target_address()); |
| 850 | case Debug::k_debug_break_return_address: |
| 851 | return reinterpret_cast<Address>(Debug::debug_break_return_address()); |
| 852 | case Debug::k_register_address: |
| 853 | return reinterpret_cast<Address>(Debug::register_address(reg_)); |
| 854 | default: |
| 855 | UNREACHABLE(); |
| 856 | return NULL; |
| 857 | } |
| 858 | } |
| 859 | private: |
| 860 | Debug::AddressId id_; |
| 861 | int reg_; |
| 862 | }; |
| 863 | |
| 864 | |
| 865 | } } // namespace v8::internal |
| 866 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 867 | #endif // ENABLE_DEBUGGER_SUPPORT |
| 868 | |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 869 | #endif // V8_DEBUG_H_ |