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