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