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