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 | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 28 | #ifndef V8_V8_DEBUG_H_ |
| 29 | #define V8_V8_DEBUG_H_ |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 30 | |
ager@chromium.org | c27e4e7 | 2008-09-04 13:52:27 +0000 | [diff] [blame] | 31 | #include "../include/v8-debug.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 32 | #include "assembler.h" |
| 33 | #include "code-stubs.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" |
| 38 | |
| 39 | |
| 40 | namespace v8 { namespace internal { |
| 41 | |
| 42 | // Step actions. NOTE: These values are in macros.py as well. |
| 43 | enum StepAction { |
| 44 | StepNone = -1, // Stepping not prepared. |
| 45 | StepOut = 0, // Step out of the current function. |
| 46 | StepNext = 1, // Step to the next statement in the current function. |
| 47 | StepIn = 2, // Step into new functions invoked or the next statement |
| 48 | // in the current function. |
| 49 | StepMin = 3, // Perform a minimum step in the current function. |
| 50 | StepInMin = 4 // Step into new functions invoked or perform a minimum step |
| 51 | // in the current function. |
| 52 | }; |
| 53 | |
| 54 | |
| 55 | // Type of exception break. NOTE: These values are in macros.py as well. |
| 56 | enum ExceptionBreakType { |
| 57 | BreakException = 0, |
| 58 | BreakUncaughtException = 1 |
| 59 | }; |
| 60 | |
| 61 | |
| 62 | // Type of exception break. NOTE: These values are in macros.py as well. |
| 63 | enum BreakLocatorType { |
| 64 | ALL_BREAK_LOCATIONS = 0, |
| 65 | SOURCE_BREAK_LOCATIONS = 1 |
| 66 | }; |
| 67 | |
| 68 | |
| 69 | // Class for iterating through the break points in a function and changing |
| 70 | // them. |
| 71 | class BreakLocationIterator { |
| 72 | public: |
| 73 | explicit BreakLocationIterator(Handle<DebugInfo> debug_info, |
| 74 | BreakLocatorType type); |
| 75 | virtual ~BreakLocationIterator(); |
| 76 | |
| 77 | void Next(); |
| 78 | void Next(int count); |
| 79 | void FindBreakLocationFromAddress(Address pc); |
| 80 | void FindBreakLocationFromPosition(int position); |
| 81 | void Reset(); |
| 82 | bool Done() const; |
| 83 | void SetBreakPoint(Handle<Object> break_point_object); |
| 84 | void ClearBreakPoint(Handle<Object> break_point_object); |
| 85 | void SetOneShot(); |
| 86 | void ClearOneShot(); |
| 87 | void PrepareStepIn(); |
| 88 | bool IsExit() const; |
| 89 | bool HasBreakPoint(); |
| 90 | bool IsDebugBreak(); |
| 91 | Object* BreakPointObjects(); |
| 92 | |
| 93 | |
| 94 | inline int code_position() { return pc() - debug_info_->code()->entry(); } |
| 95 | inline int break_point() { return break_point_; } |
| 96 | inline int position() { return position_; } |
| 97 | inline int statement_position() { return statement_position_; } |
| 98 | inline Address pc() { return reloc_iterator_->rinfo()->pc(); } |
| 99 | inline Code* code() { return debug_info_->code(); } |
| 100 | inline RelocInfo* rinfo() { return reloc_iterator_->rinfo(); } |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 101 | inline RelocInfo::Mode rmode() const { |
| 102 | return reloc_iterator_->rinfo()->rmode(); |
| 103 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 104 | inline RelocInfo* original_rinfo() { |
| 105 | return reloc_iterator_original_->rinfo(); |
| 106 | } |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 107 | inline RelocInfo::Mode original_rmode() const { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 108 | return reloc_iterator_original_->rinfo()->rmode(); |
| 109 | } |
| 110 | |
| 111 | protected: |
| 112 | bool RinfoDone() const; |
| 113 | void RinfoNext(); |
| 114 | |
| 115 | BreakLocatorType type_; |
| 116 | int break_point_; |
| 117 | int position_; |
| 118 | int statement_position_; |
| 119 | Handle<DebugInfo> debug_info_; |
| 120 | RelocIterator* reloc_iterator_; |
| 121 | RelocIterator* reloc_iterator_original_; |
| 122 | |
| 123 | private: |
| 124 | void SetDebugBreak(); |
| 125 | void ClearDebugBreak(); |
| 126 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 127 | DISALLOW_COPY_AND_ASSIGN(BreakLocationIterator); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 128 | }; |
| 129 | |
| 130 | |
| 131 | // Linked list holding debug info objects. The debug info objects are kept as |
| 132 | // weak handles to avoid a debug info object to keep a function alive. |
| 133 | class DebugInfoListNode { |
| 134 | public: |
| 135 | explicit DebugInfoListNode(DebugInfo* debug_info); |
| 136 | virtual ~DebugInfoListNode(); |
| 137 | |
| 138 | DebugInfoListNode* next() { return next_; } |
| 139 | void set_next(DebugInfoListNode* next) { next_ = next; } |
| 140 | Handle<DebugInfo> debug_info() { return debug_info_; } |
| 141 | |
| 142 | private: |
| 143 | // Global (weak) handle to the debug info object. |
| 144 | Handle<DebugInfo> debug_info_; |
| 145 | |
| 146 | // Next pointer for linked list. |
| 147 | DebugInfoListNode* next_; |
| 148 | }; |
| 149 | |
| 150 | |
| 151 | // This class contains the debugger support. The main purpose is to handle |
| 152 | // setting break points in the code. |
| 153 | // |
| 154 | // This class controls the debug info for all functions which currently have |
| 155 | // active breakpoints in them. This debug info is held in the heap root object |
| 156 | // debug_info which is a FixedArray. Each entry in this list is of class |
| 157 | // DebugInfo. |
| 158 | class Debug { |
| 159 | public: |
| 160 | static void Setup(bool create_heap_objects); |
| 161 | static bool Load(); |
| 162 | static void Unload(); |
| 163 | static bool IsLoaded() { return !debug_context_.is_null(); } |
| 164 | static bool InDebugger() { return Top::is_break(); } |
| 165 | static void Iterate(ObjectVisitor* v); |
| 166 | |
| 167 | static Object* Break(Arguments args); |
| 168 | static void SetBreakPoint(Handle<SharedFunctionInfo> shared, |
| 169 | int source_position, |
| 170 | Handle<Object> break_point_object); |
| 171 | static void ClearBreakPoint(Handle<Object> break_point_object); |
| 172 | static void FloodWithOneShot(Handle<SharedFunctionInfo> shared); |
| 173 | static void FloodHandlerWithOneShot(); |
| 174 | static void ChangeBreakOnException(ExceptionBreakType type, bool enable); |
| 175 | static void PrepareStep(StepAction step_action, int step_count); |
| 176 | static void ClearStepping(); |
| 177 | static bool StepNextContinue(BreakLocationIterator* break_location_iterator, |
| 178 | JavaScriptFrame* frame); |
| 179 | static Handle<DebugInfo> GetDebugInfo(Handle<SharedFunctionInfo> shared); |
| 180 | static bool HasDebugInfo(Handle<SharedFunctionInfo> shared); |
kasper.lund | bd3ec4e | 2008-07-09 11:06:54 +0000 | [diff] [blame] | 181 | |
| 182 | // Returns whether the operation succedded. |
| 183 | static bool EnsureDebugInfo(Handle<SharedFunctionInfo> shared); |
| 184 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 185 | static bool IsDebugBreak(Address addr); |
| 186 | |
| 187 | // Check whether a code stub with the specified major key is a possible break |
| 188 | // point location. |
| 189 | static bool IsSourceBreakStub(Code* code); |
| 190 | static bool IsBreakStub(Code* code); |
| 191 | |
| 192 | // Find the builtin to use for invoking the debug break |
| 193 | static Handle<Code> FindDebugBreak(RelocInfo* rinfo); |
| 194 | |
| 195 | static Handle<Object> GetSourceBreakLocations( |
| 196 | Handle<SharedFunctionInfo> shared); |
| 197 | static Code* GetCodeTarget(Address target); |
| 198 | |
| 199 | // Getter for the debug_context. |
| 200 | inline static Handle<Context> debug_context() { return debug_context_; } |
| 201 | |
| 202 | // Check whether a global object is the debug global object. |
| 203 | static bool IsDebugGlobal(GlobalObject* global); |
| 204 | |
| 205 | // Fast check to see if any break points are active. |
| 206 | inline static bool has_break_points() { return has_break_points_; } |
| 207 | |
| 208 | static bool StepInActive() { return thread_local_.step_into_fp_ != 0; } |
| 209 | static Address step_in_fp() { return thread_local_.step_into_fp_; } |
| 210 | static Address* step_in_fp_addr() { return &thread_local_.step_into_fp_; } |
| 211 | |
kasper.lund | bd3ec4e | 2008-07-09 11:06:54 +0000 | [diff] [blame] | 212 | // Getter and setter for the disable break state. |
| 213 | static bool disable_break() { return disable_break_; } |
| 214 | static void set_disable_break(bool disable_break) { |
| 215 | disable_break_ = disable_break; |
| 216 | } |
| 217 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 218 | // Getters for the current exception break state. |
| 219 | static bool break_on_exception() { return break_on_exception_; } |
| 220 | static bool break_on_uncaught_exception() { |
| 221 | return break_on_uncaught_exception_; |
| 222 | } |
| 223 | |
| 224 | enum AddressId { |
| 225 | k_after_break_target_address, |
| 226 | k_debug_break_return_address, |
| 227 | k_register_address |
| 228 | }; |
| 229 | |
| 230 | // Support for setting the address to jump to when returning from break point. |
| 231 | static Address* after_break_target_address() { |
| 232 | return reinterpret_cast<Address*>(&thread_local_.after_break_target_); |
| 233 | } |
| 234 | |
| 235 | // Support for saving/restoring registers when handling debug break calls. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame^] | 236 | static Object** register_address(int r) { |
| 237 | return ®isters_[r]; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 238 | } |
| 239 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 240 | // Address of the debug break return entry code. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 241 | static Code* debug_break_return_entry() { return debug_break_return_entry_; } |
| 242 | |
| 243 | // 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^] | 244 | static Code** debug_break_return_address() { |
| 245 | return &debug_break_return_; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | static const int kEstimatedNofDebugInfoEntries = 16; |
| 249 | static const int kEstimatedNofBreakPointsInFunction = 16; |
| 250 | |
| 251 | static void HandleWeakDebugInfo(v8::Persistent<v8::Object> obj, void* data); |
| 252 | |
| 253 | friend class Debugger; |
| 254 | friend Handle<FixedArray> GetDebuggedFunctions(); // Found in test-debug.cc |
| 255 | |
| 256 | // Threading support. |
| 257 | static char* ArchiveDebug(char* to); |
| 258 | static char* RestoreDebug(char* from); |
| 259 | static int ArchiveSpacePerThread(); |
| 260 | |
| 261 | // Code generation assumptions. |
| 262 | static const int kIa32CallInstructionLength = 5; |
| 263 | static const int kIa32JSReturnSequenceLength = 6; |
| 264 | |
| 265 | private: |
| 266 | static bool CompileDebuggerScript(int index); |
| 267 | static void ClearOneShot(); |
| 268 | static void ActivateStepIn(StackFrame* frame); |
| 269 | static void ClearStepIn(); |
| 270 | static void ClearStepNext(); |
kasper.lund | bd3ec4e | 2008-07-09 11:06:54 +0000 | [diff] [blame] | 271 | // Returns whether the compile succedded. |
| 272 | static bool EnsureCompiled(Handle<SharedFunctionInfo> shared); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 273 | static void RemoveDebugInfo(Handle<DebugInfo> debug_info); |
| 274 | static void SetAfterBreakTarget(JavaScriptFrame* frame); |
| 275 | static Handle<Object> CheckBreakPoints(Handle<Object> break_point); |
| 276 | static bool CheckBreakPoint(Handle<Object> break_point_object); |
| 277 | |
| 278 | // Global handle to debug context where all the debugger JavaScript code is |
| 279 | // loaded. |
| 280 | static Handle<Context> debug_context_; |
| 281 | |
| 282 | // Boolean state indicating whether any break points are set. |
| 283 | static bool has_break_points_; |
| 284 | static DebugInfoListNode* debug_info_list_; |
| 285 | |
kasper.lund | bd3ec4e | 2008-07-09 11:06:54 +0000 | [diff] [blame] | 286 | static bool disable_break_; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 287 | static bool break_on_exception_; |
| 288 | static bool break_on_uncaught_exception_; |
| 289 | |
| 290 | // Per-thread: |
| 291 | class ThreadLocal { |
| 292 | public: |
| 293 | // Step action for last step performed. |
| 294 | StepAction last_step_action_; |
| 295 | |
| 296 | // Source statement position from last step next action. |
| 297 | int last_statement_position_; |
| 298 | |
| 299 | // Number of steps left to perform before debug event. |
| 300 | int step_count_; |
| 301 | |
| 302 | // Frame pointer from last step next action. |
| 303 | Address last_fp_; |
| 304 | |
| 305 | // Frame pointer for frame from which step in was performed. |
| 306 | Address step_into_fp_; |
| 307 | |
| 308 | // Storage location for jump when exiting debug break calls. |
| 309 | Address after_break_target_; |
| 310 | }; |
| 311 | |
| 312 | // Storage location for registers when handling debug break calls |
| 313 | static JSCallerSavedBuffer registers_; |
| 314 | static ThreadLocal thread_local_; |
| 315 | static void ThreadInit(); |
| 316 | |
| 317 | // Code object for debug break return entry code. |
| 318 | static Code* debug_break_return_entry_; |
| 319 | |
| 320 | // Code to call for handling debug break on return. |
| 321 | static Code* debug_break_return_; |
| 322 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 323 | DISALLOW_COPY_AND_ASSIGN(Debug); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 324 | }; |
| 325 | |
| 326 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 327 | class DebugMessageThread; |
| 328 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 329 | class Debugger { |
| 330 | public: |
| 331 | static void DebugRequest(const uint16_t* json_request, int length); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 332 | |
| 333 | static Handle<Object> MakeJSObject(Vector<const char> constructor_name, |
| 334 | int argc, Object*** argv, |
| 335 | bool* caught_exception); |
| 336 | static Handle<Object> MakeExecutionState(bool* caught_exception); |
| 337 | static Handle<Object> MakeBreakEvent(Handle<Object> exec_state, |
| 338 | Handle<Object> break_points_hit, |
| 339 | bool* caught_exception); |
| 340 | static Handle<Object> MakeExceptionEvent(Handle<Object> exec_state, |
| 341 | Handle<Object> exception, |
| 342 | bool uncaught, |
| 343 | bool* caught_exception); |
| 344 | static Handle<Object> MakeNewFunctionEvent(Handle<Object> func, |
| 345 | bool* caught_exception); |
| 346 | static Handle<Object> MakeCompileEvent(Handle<Script> script, |
| 347 | Handle<Object> script_function, |
| 348 | bool* caught_exception); |
| 349 | static Handle<String> ProcessRequest(Handle<Object> exec_state, |
| 350 | Handle<Object> request, |
| 351 | bool stopped); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 352 | static void OnDebugBreak(Handle<Object> break_points_hit); |
| 353 | static void OnException(Handle<Object> exception, bool uncaught); |
| 354 | static void OnBeforeCompile(Handle<Script> script); |
| 355 | static void OnAfterCompile(Handle<Script> script, |
| 356 | Handle<JSFunction> fun); |
| 357 | static void OnNewFunction(Handle<JSFunction> fun); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 358 | static void ProcessDebugEvent(v8::DebugEvent event, |
| 359 | Handle<Object> event_data); |
| 360 | static void SetMessageHandler(v8::DebugMessageHandler handler, void* data); |
| 361 | static void SendMessage(Vector<uint16_t> message); |
| 362 | static void ProcessCommand(Vector<const uint16_t> command); |
| 363 | static void UpdateActiveDebugger(); |
| 364 | inline static bool EventActive(v8::DebugEvent event) { |
| 365 | // Currently argument event is not used. |
| 366 | return !Debugger::compiling_natives_ && Debugger::debugger_active_; |
| 367 | } |
| 368 | |
| 369 | static void set_debugger_active(bool debugger_active) { |
| 370 | Debugger::debugger_active_ = debugger_active; |
| 371 | } |
| 372 | static bool debugger_active() { return Debugger::debugger_active_; } |
| 373 | static void set_compiling_natives(bool compiling_natives) { |
| 374 | Debugger::compiling_natives_ = compiling_natives; |
| 375 | } |
| 376 | static bool compiling_natives() { return Debugger::compiling_natives_; } |
mads.s.ager | cbaa060 | 2008-08-14 13:41:48 +0000 | [diff] [blame] | 377 | static void set_loading_debugger(bool v) { is_loading_debugger_ = v; } |
| 378 | static bool is_loading_debugger() { return Debugger::is_loading_debugger_; } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 379 | |
| 380 | private: |
| 381 | static bool debugger_active_; // Are there any active debugger? |
| 382 | static bool compiling_natives_; // Are we compiling natives? |
mads.s.ager | cbaa060 | 2008-08-14 13:41:48 +0000 | [diff] [blame] | 383 | static bool is_loading_debugger_; // Are we loading the debugger? |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 384 | static DebugMessageThread* message_thread_; |
| 385 | static v8::DebugMessageHandler debug_message_handler_; |
| 386 | static void* debug_message_handler_data_; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 387 | }; |
| 388 | |
| 389 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 390 | // A Queue of Vector<uint16_t> objects. A thread-safe version is |
| 391 | // LockingMessageQueue, based on this class. |
| 392 | class MessageQueue BASE_EMBEDDED { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 393 | public: |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 394 | explicit MessageQueue(int size); |
| 395 | ~MessageQueue(); |
| 396 | bool IsEmpty() const { return start_ == end_; } |
| 397 | Vector<uint16_t> Get(); |
| 398 | void Put(const Vector<uint16_t>& message); |
| 399 | void Clear() { start_ = end_ = 0; } // Queue is empty after Clear(). |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 400 | private: |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 401 | // Doubles the size of the message queue, and copies the messages. |
| 402 | void Expand(); |
| 403 | |
| 404 | Vector<uint16_t>* messages_; |
| 405 | int start_; |
| 406 | int end_; |
| 407 | int size_; // The size of the queue buffer. Queue can hold size-1 messages. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 408 | }; |
| 409 | |
| 410 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 411 | // LockingMessageQueue is a thread-safe circular buffer of Vector<uint16_t> |
| 412 | // messages. The message data is not managed by LockingMessageQueue. |
| 413 | // Pointers to the data are passed in and out. Implemented by adding a |
mads.s.ager@gmail.com | 769cc96 | 2008-08-06 10:02:49 +0000 | [diff] [blame] | 414 | // Mutex to MessageQueue. Includes logging of all puts and gets. |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 415 | class LockingMessageQueue BASE_EMBEDDED { |
| 416 | public: |
| 417 | explicit LockingMessageQueue(int size); |
| 418 | ~LockingMessageQueue(); |
| 419 | bool IsEmpty() const; |
| 420 | Vector<uint16_t> Get(); |
| 421 | void Put(const Vector<uint16_t>& message); |
| 422 | void Clear(); |
| 423 | private: |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 424 | MessageQueue queue_; |
| 425 | Mutex* lock_; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 426 | DISALLOW_COPY_AND_ASSIGN(LockingMessageQueue); |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 427 | }; |
| 428 | |
| 429 | |
| 430 | /* This class is the data for a running thread that serializes |
| 431 | * event messages and command processing for the debugger. |
| 432 | * All uncommented methods are called only from this message thread. |
| 433 | */ |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 434 | class DebugMessageThread: public Thread { |
| 435 | public: |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 436 | DebugMessageThread(); // Called from API thread. |
| 437 | virtual ~DebugMessageThread(); // Never called. |
| 438 | // Called by V8 thread. Reports events from V8 VM. |
| 439 | // Also handles command processing in stopped state of V8, |
| 440 | // when host_running_ is false. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 441 | void DebugEvent(v8::DebugEvent, |
| 442 | Handle<Object> exec_state, |
| 443 | Handle<Object> event_data); |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 444 | // Puts event on the output queue. Called by V8. |
| 445 | // This is where V8 hands off |
| 446 | // processing of the event to the DebugMessageThread thread, |
| 447 | // which forwards it to the debug_message_handler set by the API. |
| 448 | void SendMessage(Vector<uint16_t> event_json); |
| 449 | // Formats an event into JSON, and calls SendMessage. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 450 | void SetEventJSONFromEvent(Handle<Object> event_data); |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 451 | // Puts a command coming from the public API on the queue. Called |
| 452 | // by the API client thread. This is where the API client hands off |
| 453 | // processing of the command to the DebugMessageThread thread. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 454 | void ProcessCommand(Vector<uint16_t> command); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 455 | void OnDebuggerInactive(); |
| 456 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 457 | // Main function of DebugMessageThread thread. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 458 | void Run(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 459 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 460 | bool host_running_; // Is the debugging host running or stopped? |
| 461 | Semaphore* command_received_; // Non-zero when command queue is non-empty. |
| 462 | Semaphore* message_received_; // Exactly equal to message queue length. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 463 | private: |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 464 | bool TwoByteEqualsAscii(Vector<uint16_t> two_byte, const char* ascii); |
| 465 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 466 | static const int kQueueInitialSize = 4; |
| 467 | LockingMessageQueue command_queue_; |
| 468 | LockingMessageQueue message_queue_; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 469 | DISALLOW_COPY_AND_ASSIGN(DebugMessageThread); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 470 | }; |
| 471 | |
| 472 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame^] | 473 | // This class is used for entering the debugger. Create an instance in the stack |
| 474 | // to enter the debugger. This will set the current break state, make sure the |
| 475 | // debugger is loaded and switch to the debugger context. If the debugger for |
| 476 | // some reason could not be entered FailedToEnter will return true. |
| 477 | class EnterDebugger BASE_EMBEDDED { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 478 | public: |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame^] | 479 | EnterDebugger() : set_(!it_.done()) { |
| 480 | // If there is no JavaScript frames on the stack don't switch to new break |
| 481 | // and break frame. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 482 | if (set_) { |
| 483 | // Store the previous break is and frame id. |
| 484 | break_id_ = Top::break_id(); |
| 485 | break_frame_id_ = Top::break_frame_id(); |
| 486 | |
| 487 | // Create the new break info. |
| 488 | Top::new_break(it_.frame()->id()); |
| 489 | } |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame^] | 490 | |
| 491 | // Make sure that debugger is loaded and enter the debugger context. |
| 492 | load_failed_ = !Debug::Load(); |
| 493 | if (!load_failed_) { |
| 494 | // NOTE the member variable save which saves the previous context before |
| 495 | // this change. |
| 496 | Top::set_context(*Debug::debug_context()); |
| 497 | Top::set_security_context(*Debug::debug_context()); |
| 498 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 499 | } |
| 500 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame^] | 501 | ~EnterDebugger() { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 502 | if (set_) { |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame^] | 503 | // Restore to the previous break state. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 504 | Top::set_break(break_frame_id_, break_id_); |
| 505 | } |
| 506 | } |
| 507 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame^] | 508 | // Check whether the debugger could be entered. |
| 509 | inline bool FailedToEnter() { return load_failed_; } |
| 510 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 511 | private: |
| 512 | JavaScriptFrameIterator it_; |
| 513 | const bool set_; // Was the break actually set? |
| 514 | StackFrame::Id break_frame_id_; // Previous break frame id. |
| 515 | int break_id_; // Previous break id. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame^] | 516 | bool load_failed_; // Did the debugger fail to load? |
| 517 | SaveContext save_; // Saves previous context. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 518 | }; |
| 519 | |
| 520 | |
kasper.lund | bd3ec4e | 2008-07-09 11:06:54 +0000 | [diff] [blame] | 521 | // Stack allocated class for disabling break. |
| 522 | class DisableBreak BASE_EMBEDDED { |
| 523 | public: |
| 524 | // Enter the debugger by storing the previous top context and setting the |
| 525 | // current top context to the debugger context. |
| 526 | explicit DisableBreak(bool disable_break) { |
| 527 | prev_disable_break_ = Debug::disable_break(); |
| 528 | Debug::set_disable_break(disable_break); |
| 529 | } |
| 530 | ~DisableBreak() { |
| 531 | Debug::set_disable_break(prev_disable_break_); |
| 532 | } |
| 533 | |
| 534 | private: |
| 535 | // The previous state of the disable break used to restore the value when this |
| 536 | // object is destructed. |
| 537 | bool prev_disable_break_; |
| 538 | }; |
| 539 | |
| 540 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 541 | // Debug_Address encapsulates the Address pointers used in generating debug |
| 542 | // code. |
| 543 | class Debug_Address { |
| 544 | public: |
| 545 | Debug_Address(Debug::AddressId id, int reg = 0) |
| 546 | : id_(id), reg_(reg) { |
| 547 | ASSERT(reg == 0 || id == Debug::k_register_address); |
| 548 | } |
| 549 | |
| 550 | static Debug_Address AfterBreakTarget() { |
| 551 | return Debug_Address(Debug::k_after_break_target_address); |
| 552 | } |
| 553 | |
| 554 | static Debug_Address DebugBreakReturn() { |
| 555 | return Debug_Address(Debug::k_debug_break_return_address); |
| 556 | } |
| 557 | |
| 558 | static Debug_Address Register(int reg) { |
| 559 | return Debug_Address(Debug::k_register_address, reg); |
| 560 | } |
| 561 | |
| 562 | Address address() const { |
| 563 | switch (id_) { |
| 564 | case Debug::k_after_break_target_address: |
| 565 | return reinterpret_cast<Address>(Debug::after_break_target_address()); |
| 566 | case Debug::k_debug_break_return_address: |
| 567 | return reinterpret_cast<Address>(Debug::debug_break_return_address()); |
| 568 | case Debug::k_register_address: |
| 569 | return reinterpret_cast<Address>(Debug::register_address(reg_)); |
| 570 | default: |
| 571 | UNREACHABLE(); |
| 572 | return NULL; |
| 573 | } |
| 574 | } |
| 575 | private: |
| 576 | Debug::AddressId id_; |
| 577 | int reg_; |
| 578 | }; |
| 579 | |
| 580 | |
| 581 | } } // namespace v8::internal |
| 582 | |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 583 | #endif // V8_V8_DEBUG_H_ |