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