christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1 | // Copyright 2006-2008 Google Inc. All Rights Reserved. |
| 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 | |
| 28 | #include "v8.h" |
| 29 | |
| 30 | #include "api.h" |
| 31 | #include "arguments.h" |
| 32 | #include "bootstrapper.h" |
| 33 | #include "code-stubs.h" |
| 34 | #include "compiler.h" |
| 35 | #include "debug.h" |
| 36 | #include "execution.h" |
| 37 | #include "global-handles.h" |
| 38 | #include "natives.h" |
| 39 | #include "stub-cache.h" |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 40 | #include "log.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 41 | |
| 42 | namespace v8 { namespace internal { |
| 43 | |
| 44 | DEFINE_bool(remote_debugging, false, "enable remote debugging"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 45 | DEFINE_bool(trace_debug_json, false, "trace debugging JSON request/response"); |
| 46 | DECLARE_bool(allow_natives_syntax); |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 47 | DECLARE_bool(log_debugger); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 48 | |
| 49 | |
| 50 | static void PrintLn(v8::Local<v8::Value> value) { |
| 51 | v8::Local<v8::String> s = value->ToString(); |
| 52 | char* data = NewArray<char>(s->Length() + 1); |
| 53 | if (data == NULL) { |
| 54 | V8::FatalProcessOutOfMemory("PrintLn"); |
| 55 | return; |
| 56 | } |
| 57 | s->WriteAscii(data); |
| 58 | PrintF("%s\n", data); |
| 59 | DeleteArray(data); |
| 60 | } |
| 61 | |
| 62 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 63 | static Handle<Code> ComputeCallDebugBreak(int argc) { |
| 64 | CALL_HEAP_FUNCTION(StubCache::ComputeCallDebugBreak(argc), Code); |
| 65 | } |
| 66 | |
| 67 | |
| 68 | static Handle<Code> ComputeCallDebugPrepareStepIn(int argc) { |
| 69 | CALL_HEAP_FUNCTION(StubCache::ComputeCallDebugPrepareStepIn(argc), Code); |
| 70 | } |
| 71 | |
| 72 | |
| 73 | BreakLocationIterator::BreakLocationIterator(Handle<DebugInfo> debug_info, |
| 74 | BreakLocatorType type) { |
| 75 | debug_info_ = debug_info; |
| 76 | type_ = type; |
| 77 | reloc_iterator_ = NULL; |
| 78 | reloc_iterator_original_ = NULL; |
| 79 | Reset(); // Initialize the rest of the member variables. |
| 80 | } |
| 81 | |
| 82 | |
| 83 | BreakLocationIterator::~BreakLocationIterator() { |
| 84 | ASSERT(reloc_iterator_ != NULL); |
| 85 | ASSERT(reloc_iterator_original_ != NULL); |
| 86 | delete reloc_iterator_; |
| 87 | delete reloc_iterator_original_; |
| 88 | } |
| 89 | |
| 90 | |
| 91 | void BreakLocationIterator::Next() { |
| 92 | AssertNoAllocation nogc; |
| 93 | ASSERT(!RinfoDone()); |
| 94 | |
| 95 | // Iterate through reloc info for code and original code stopping at each |
| 96 | // breakable code target. |
| 97 | bool first = break_point_ == -1; |
| 98 | while (!RinfoDone()) { |
| 99 | if (!first) RinfoNext(); |
| 100 | first = false; |
| 101 | if (RinfoDone()) return; |
| 102 | |
| 103 | // Update the current source position each time a source position is |
| 104 | // passed. |
| 105 | if (is_position(rmode())) { |
| 106 | position_ = rinfo()->data() - debug_info_->shared()->start_position(); |
| 107 | if (is_statement_position(rmode())) { |
| 108 | statement_position_ = |
| 109 | rinfo()->data() - debug_info_->shared()->start_position(); |
| 110 | } |
| 111 | ASSERT(position_ >= 0); |
| 112 | ASSERT(statement_position_ >= 0); |
| 113 | } |
| 114 | |
| 115 | // Check for breakable code target. Look in the original code as setting |
| 116 | // break points can cause the code targets in the running (debugged) code to |
| 117 | // be of a different kind than in the original code. |
| 118 | if (is_code_target(rmode())) { |
| 119 | Address target = original_rinfo()->target_address(); |
| 120 | Code* code = Debug::GetCodeTarget(target); |
| 121 | if (code->is_inline_cache_stub() || is_js_construct_call(rmode())) { |
| 122 | break_point_++; |
| 123 | return; |
| 124 | } |
| 125 | if (code->kind() == Code::STUB) { |
| 126 | if (type_ == ALL_BREAK_LOCATIONS) { |
| 127 | if (Debug::IsBreakStub(code)) { |
| 128 | break_point_++; |
| 129 | return; |
| 130 | } |
| 131 | } else { |
| 132 | ASSERT(type_ == SOURCE_BREAK_LOCATIONS); |
| 133 | if (Debug::IsSourceBreakStub(code)) { |
| 134 | break_point_++; |
| 135 | return; |
| 136 | } |
| 137 | } |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | // Check for break at return. |
| 142 | // Currently is_exit_js_frame is used on ARM. |
| 143 | if (is_js_return(rmode()) || is_exit_js_frame(rmode())) { |
| 144 | // Set the positions to the end of the function. |
| 145 | if (debug_info_->shared()->HasSourceCode()) { |
| 146 | position_ = debug_info_->shared()->end_position() - |
| 147 | debug_info_->shared()->start_position(); |
| 148 | } else { |
| 149 | position_ = 0; |
| 150 | } |
| 151 | statement_position_ = position_; |
| 152 | break_point_++; |
| 153 | return; |
| 154 | } |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | |
| 159 | void BreakLocationIterator::Next(int count) { |
| 160 | while (count > 0) { |
| 161 | Next(); |
| 162 | count--; |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | |
| 167 | // Find the break point closest to the supplied address. |
| 168 | void BreakLocationIterator::FindBreakLocationFromAddress(Address pc) { |
| 169 | // Run through all break points to locate the one closest to the address. |
| 170 | int closest_break_point = 0; |
| 171 | int distance = kMaxInt; |
| 172 | while (!Done()) { |
| 173 | // Check if this break point is closer that what was previously found. |
| 174 | if (this->pc() < pc && pc - this->pc() < distance) { |
| 175 | closest_break_point = break_point(); |
| 176 | distance = pc - this->pc(); |
| 177 | // Check whether we can't get any closer. |
| 178 | if (distance == 0) break; |
| 179 | } |
| 180 | Next(); |
| 181 | } |
| 182 | |
| 183 | // Move to the break point found. |
| 184 | Reset(); |
| 185 | Next(closest_break_point); |
| 186 | } |
| 187 | |
| 188 | |
| 189 | // Find the break point closest to the supplied source position. |
| 190 | void BreakLocationIterator::FindBreakLocationFromPosition(int position) { |
| 191 | // Run through all break points to locate the one closest to the source |
| 192 | // position. |
| 193 | int closest_break_point = 0; |
| 194 | int distance = kMaxInt; |
| 195 | while (!Done()) { |
| 196 | // Check if this break point is closer that what was previously found. |
| 197 | if (position <= statement_position() && |
| 198 | statement_position() - position < distance) { |
| 199 | closest_break_point = break_point(); |
| 200 | distance = statement_position() - position; |
| 201 | // Check whether we can't get any closer. |
| 202 | if (distance == 0) break; |
| 203 | } |
| 204 | Next(); |
| 205 | } |
| 206 | |
| 207 | // Move to the break point found. |
| 208 | Reset(); |
| 209 | Next(closest_break_point); |
| 210 | } |
| 211 | |
| 212 | |
| 213 | void BreakLocationIterator::Reset() { |
| 214 | // Create relocation iterators for the two code objects. |
| 215 | if (reloc_iterator_ != NULL) delete reloc_iterator_; |
| 216 | if (reloc_iterator_original_ != NULL) delete reloc_iterator_original_; |
| 217 | reloc_iterator_ = new RelocIterator(debug_info_->code()); |
| 218 | reloc_iterator_original_ = new RelocIterator(debug_info_->original_code()); |
| 219 | |
| 220 | // Position at the first break point. |
| 221 | break_point_ = -1; |
| 222 | position_ = 1; |
| 223 | statement_position_ = 1; |
| 224 | Next(); |
| 225 | } |
| 226 | |
| 227 | |
| 228 | bool BreakLocationIterator::Done() const { |
| 229 | return RinfoDone(); |
| 230 | } |
| 231 | |
| 232 | |
| 233 | void BreakLocationIterator::SetBreakPoint(Handle<Object> break_point_object) { |
| 234 | // If there is not already a real break point here patch code with debug |
| 235 | // break. |
| 236 | if (!HasBreakPoint()) { |
| 237 | SetDebugBreak(); |
| 238 | } |
| 239 | ASSERT(IsDebugBreak()); |
| 240 | // Set the break point information. |
| 241 | DebugInfo::SetBreakPoint(debug_info_, code_position(), |
| 242 | position(), statement_position(), |
| 243 | break_point_object); |
| 244 | } |
| 245 | |
| 246 | |
| 247 | void BreakLocationIterator::ClearBreakPoint(Handle<Object> break_point_object) { |
| 248 | // Clear the break point information. |
| 249 | DebugInfo::ClearBreakPoint(debug_info_, code_position(), break_point_object); |
| 250 | // If there are no more break points here remove the debug break. |
| 251 | if (!HasBreakPoint()) { |
| 252 | ClearDebugBreak(); |
| 253 | ASSERT(!IsDebugBreak()); |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | |
| 258 | void BreakLocationIterator::SetOneShot() { |
| 259 | // If there is a real break point here no more to do. |
| 260 | if (HasBreakPoint()) { |
| 261 | ASSERT(IsDebugBreak()); |
| 262 | return; |
| 263 | } |
| 264 | |
| 265 | // Patch code with debug break. |
| 266 | SetDebugBreak(); |
| 267 | } |
| 268 | |
| 269 | |
| 270 | void BreakLocationIterator::ClearOneShot() { |
| 271 | // If there is a real break point here no more to do. |
| 272 | if (HasBreakPoint()) { |
| 273 | ASSERT(IsDebugBreak()); |
| 274 | return; |
| 275 | } |
| 276 | |
| 277 | // Patch code removing debug break. |
| 278 | ClearDebugBreak(); |
| 279 | ASSERT(!IsDebugBreak()); |
| 280 | } |
| 281 | |
| 282 | |
| 283 | void BreakLocationIterator::SetDebugBreak() { |
| 284 | // If there is already a break point here just return. This might happen if |
| 285 | // the same code is flodded with break points twice. Flodding the same |
| 286 | // function twice might happen when stepping in a function with an exception |
| 287 | // handler as the handler and the function is the same. |
| 288 | if (IsDebugBreak()) { |
| 289 | return; |
| 290 | } |
| 291 | |
| 292 | if (is_js_return(rmode())) { |
| 293 | // This path is currently only used on IA32 as JSExitFrame on ARM uses a |
| 294 | // stub. |
| 295 | // Patch the JS frame exit code with a debug break call. See |
| 296 | // VisitReturnStatement and ExitJSFrame in codegen-ia32.cc for the |
| 297 | // precise return instructions sequence. |
| 298 | ASSERT(Debug::kIa32JSReturnSequenceLength >= |
| 299 | Debug::kIa32CallInstructionLength); |
| 300 | rinfo()->patch_code_with_call(Debug::debug_break_return_entry()->entry(), |
| 301 | Debug::kIa32JSReturnSequenceLength - Debug::kIa32CallInstructionLength); |
| 302 | } else { |
| 303 | // Patch the original code with the current address as the current address |
| 304 | // might have changed by the inline caching since the code was copied. |
| 305 | original_rinfo()->set_target_address(rinfo()->target_address()); |
| 306 | |
| 307 | // Patch the code to invoke the builtin debug break function matching the |
| 308 | // calling convention used by the call site. |
| 309 | Handle<Code> dbgbrk_code(Debug::FindDebugBreak(rinfo())); |
| 310 | rinfo()->set_target_address(dbgbrk_code->entry()); |
| 311 | } |
| 312 | ASSERT(IsDebugBreak()); |
| 313 | } |
| 314 | |
| 315 | |
| 316 | void BreakLocationIterator::ClearDebugBreak() { |
| 317 | if (is_js_return(rmode())) { |
| 318 | // Restore the JS frame exit code. |
| 319 | rinfo()->patch_code(original_rinfo()->pc(), |
| 320 | Debug::kIa32JSReturnSequenceLength); |
| 321 | } else { |
| 322 | // Patch the code to the original invoke. |
| 323 | rinfo()->set_target_address(original_rinfo()->target_address()); |
| 324 | } |
| 325 | ASSERT(!IsDebugBreak()); |
| 326 | } |
| 327 | |
| 328 | |
| 329 | void BreakLocationIterator::PrepareStepIn() { |
| 330 | // Step in can only be prepared if currently positioned on an IC call or |
| 331 | // construct call. |
| 332 | Address target = rinfo()->target_address(); |
| 333 | Code* code = Debug::GetCodeTarget(target); |
| 334 | if (code->is_call_stub()) { |
| 335 | // Step in through IC call is handled by the runtime system. Therefore make |
| 336 | // sure that the any current IC is cleared and the runtime system is |
| 337 | // called. If the executing code has a debug break at the location change |
| 338 | // the call in the original code as it is the code there that will be |
| 339 | // executed in place of the debug break call. |
| 340 | Handle<Code> stub = ComputeCallDebugPrepareStepIn(code->arguments_count()); |
| 341 | if (IsDebugBreak()) { |
| 342 | original_rinfo()->set_target_address(stub->entry()); |
| 343 | } else { |
| 344 | rinfo()->set_target_address(stub->entry()); |
| 345 | } |
| 346 | } else { |
| 347 | // Step in through constructs call requires no changs to the running code. |
| 348 | ASSERT(is_js_construct_call(rmode())); |
| 349 | } |
| 350 | } |
| 351 | |
| 352 | |
| 353 | // Check whether the break point is at a position which will exit the function. |
| 354 | bool BreakLocationIterator::IsExit() const { |
| 355 | // Currently is_exit_js_frame is used on ARM. |
| 356 | return (is_js_return(rmode()) || is_exit_js_frame(rmode())); |
| 357 | } |
| 358 | |
| 359 | |
| 360 | bool BreakLocationIterator::HasBreakPoint() { |
| 361 | return debug_info_->HasBreakPoint(code_position()); |
| 362 | } |
| 363 | |
| 364 | |
| 365 | // Check whether there is a debug break at the current position. |
| 366 | bool BreakLocationIterator::IsDebugBreak() { |
| 367 | if (is_js_return(rmode())) { |
| 368 | // This is IA32 specific but works as long as the ARM version |
| 369 | // still uses a stub for JSExitFrame. |
| 370 | // |
| 371 | // TODO(1240753): Make the test architecture independent or split |
| 372 | // parts of the debugger into architecture dependent files. |
| 373 | return (*(rinfo()->pc()) == 0xE8); |
| 374 | } else { |
| 375 | return Debug::IsDebugBreak(rinfo()->target_address()); |
| 376 | } |
| 377 | } |
| 378 | |
| 379 | |
| 380 | Object* BreakLocationIterator::BreakPointObjects() { |
| 381 | return debug_info_->GetBreakPointObjects(code_position()); |
| 382 | } |
| 383 | |
| 384 | |
| 385 | bool BreakLocationIterator::RinfoDone() const { |
| 386 | ASSERT(reloc_iterator_->done() == reloc_iterator_original_->done()); |
| 387 | return reloc_iterator_->done(); |
| 388 | } |
| 389 | |
| 390 | |
| 391 | void BreakLocationIterator::RinfoNext() { |
| 392 | reloc_iterator_->next(); |
| 393 | reloc_iterator_original_->next(); |
| 394 | #ifdef DEBUG |
| 395 | ASSERT(reloc_iterator_->done() == reloc_iterator_original_->done()); |
| 396 | if (!reloc_iterator_->done()) { |
| 397 | ASSERT(rmode() == original_rmode()); |
| 398 | } |
| 399 | #endif |
| 400 | } |
| 401 | |
| 402 | |
| 403 | bool Debug::has_break_points_ = false; |
| 404 | DebugInfoListNode* Debug::debug_info_list_ = NULL; |
| 405 | |
| 406 | |
| 407 | // Threading support. |
| 408 | void Debug::ThreadInit() { |
| 409 | thread_local_.last_step_action_ = StepNone; |
| 410 | thread_local_.last_statement_position_ = kNoPosition; |
| 411 | thread_local_.step_count_ = 0; |
| 412 | thread_local_.last_fp_ = 0; |
| 413 | thread_local_.step_into_fp_ = 0; |
| 414 | thread_local_.after_break_target_ = 0; |
| 415 | } |
| 416 | |
| 417 | |
| 418 | JSCallerSavedBuffer Debug::registers_; |
| 419 | Debug::ThreadLocal Debug::thread_local_; |
| 420 | |
| 421 | |
| 422 | char* Debug::ArchiveDebug(char* storage) { |
| 423 | char* to = storage; |
| 424 | memcpy(to, reinterpret_cast<char*>(&thread_local_), sizeof(ThreadLocal)); |
| 425 | to += sizeof(ThreadLocal); |
| 426 | memcpy(to, reinterpret_cast<char*>(®isters_), sizeof(registers_)); |
| 427 | ThreadInit(); |
| 428 | ASSERT(to <= storage + ArchiveSpacePerThread()); |
| 429 | return storage + ArchiveSpacePerThread(); |
| 430 | } |
| 431 | |
| 432 | |
| 433 | char* Debug::RestoreDebug(char* storage) { |
| 434 | char* from = storage; |
| 435 | memcpy(reinterpret_cast<char*>(&thread_local_), from, sizeof(ThreadLocal)); |
| 436 | from += sizeof(ThreadLocal); |
| 437 | memcpy(reinterpret_cast<char*>(®isters_), from, sizeof(registers_)); |
| 438 | ASSERT(from <= storage + ArchiveSpacePerThread()); |
| 439 | return storage + ArchiveSpacePerThread(); |
| 440 | } |
| 441 | |
| 442 | |
| 443 | int Debug::ArchiveSpacePerThread() { |
| 444 | return sizeof(ThreadLocal) + sizeof(registers_); |
| 445 | } |
| 446 | |
| 447 | |
kasper.lund | bd3ec4e | 2008-07-09 11:06:54 +0000 | [diff] [blame] | 448 | // Default break enabled. |
| 449 | bool Debug::disable_break_ = false; |
| 450 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 451 | // Default call debugger on uncaught exception. |
| 452 | bool Debug::break_on_exception_ = false; |
| 453 | bool Debug::break_on_uncaught_exception_ = true; |
| 454 | |
| 455 | Handle<Context> Debug::debug_context_ = Handle<Context>(); |
| 456 | Code* Debug::debug_break_return_entry_ = NULL; |
| 457 | Code* Debug::debug_break_return_ = NULL; |
| 458 | |
| 459 | |
| 460 | void Debug::HandleWeakDebugInfo(v8::Persistent<v8::Object> obj, void* data) { |
| 461 | DebugInfoListNode* node = reinterpret_cast<DebugInfoListNode*>(data); |
| 462 | RemoveDebugInfo(node->debug_info()); |
| 463 | #ifdef DEBUG |
| 464 | node = Debug::debug_info_list_; |
| 465 | while (node != NULL) { |
| 466 | ASSERT(node != reinterpret_cast<DebugInfoListNode*>(data)); |
| 467 | node = node->next(); |
| 468 | } |
| 469 | #endif |
| 470 | } |
| 471 | |
| 472 | |
| 473 | DebugInfoListNode::DebugInfoListNode(DebugInfo* debug_info): next_(NULL) { |
| 474 | // Globalize the request debug info object and make it weak. |
| 475 | debug_info_ = Handle<DebugInfo>::cast((GlobalHandles::Create(debug_info))); |
| 476 | GlobalHandles::MakeWeak(reinterpret_cast<Object**>(debug_info_.location()), |
| 477 | this, Debug::HandleWeakDebugInfo); |
| 478 | } |
| 479 | |
| 480 | |
| 481 | DebugInfoListNode::~DebugInfoListNode() { |
| 482 | GlobalHandles::Destroy(reinterpret_cast<Object**>(debug_info_.location())); |
| 483 | } |
| 484 | |
| 485 | |
| 486 | void Debug::Setup(bool create_heap_objects) { |
| 487 | ThreadInit(); |
| 488 | if (create_heap_objects) { |
| 489 | // Get code to handle entry to debug break on return. |
| 490 | debug_break_return_entry_ = |
| 491 | Builtins::builtin(Builtins::Return_DebugBreakEntry); |
| 492 | ASSERT(debug_break_return_entry_->IsCode()); |
| 493 | |
| 494 | // Get code to handle debug break on return. |
| 495 | debug_break_return_ = |
| 496 | Builtins::builtin(Builtins::Return_DebugBreak); |
| 497 | ASSERT(debug_break_return_->IsCode()); |
| 498 | } |
| 499 | } |
| 500 | |
| 501 | |
| 502 | bool Debug::CompileDebuggerScript(int index) { |
| 503 | HandleScope scope; |
| 504 | |
kasper.lund | 4451067 | 2008-07-25 07:37:58 +0000 | [diff] [blame] | 505 | // Bail out if the index is invalid. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 506 | if (index == -1) { |
| 507 | return false; |
| 508 | } |
kasper.lund | 4451067 | 2008-07-25 07:37:58 +0000 | [diff] [blame] | 509 | |
| 510 | // Find source and name for the requested script. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 511 | Handle<String> source_code = Bootstrapper::NativesSourceLookup(index); |
| 512 | Vector<const char> name = Natives::GetScriptName(index); |
| 513 | Handle<String> script_name = Factory::NewStringFromAscii(name); |
| 514 | |
| 515 | // Compile the script. |
| 516 | bool allow_natives_syntax = FLAG_allow_natives_syntax; |
| 517 | FLAG_allow_natives_syntax = true; |
| 518 | Handle<JSFunction> boilerplate; |
| 519 | boilerplate = Compiler::Compile(source_code, script_name, 0, 0, NULL, NULL); |
| 520 | FLAG_allow_natives_syntax = allow_natives_syntax; |
| 521 | |
| 522 | // Silently ignore stack overflows during compilation. |
| 523 | if (boilerplate.is_null()) { |
| 524 | ASSERT(Top::has_pending_exception()); |
| 525 | Top::clear_pending_exception(); |
| 526 | return false; |
| 527 | } |
| 528 | |
kasper.lund | 4451067 | 2008-07-25 07:37:58 +0000 | [diff] [blame] | 529 | // Execute the boilerplate function in the debugger context. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 530 | Handle<Context> context = Top::global_context(); |
kasper.lund | 4451067 | 2008-07-25 07:37:58 +0000 | [diff] [blame] | 531 | bool caught_exception = false; |
| 532 | Handle<JSFunction> function = |
| 533 | Factory::NewFunctionFromBoilerplate(boilerplate, context); |
| 534 | Handle<Object> result = |
| 535 | Execution::TryCall(function, Handle<Object>(context->global()), |
| 536 | 0, NULL, &caught_exception); |
| 537 | |
| 538 | // Check for caught exceptions. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 539 | if (caught_exception) { |
kasper.lund | 4451067 | 2008-07-25 07:37:58 +0000 | [diff] [blame] | 540 | MessageHandler::ReportMessage("error_loading_debugger", NULL, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 541 | HandleVector<Object>(&result, 1)); |
| 542 | return false; |
| 543 | } |
| 544 | |
kasper.lund | 4451067 | 2008-07-25 07:37:58 +0000 | [diff] [blame] | 545 | // Mark this script as native and return successfully. |
| 546 | Handle<Script> script(Script::cast(function->shared()->script())); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 547 | script->set_type(Smi::FromInt(SCRIPT_TYPE_NATIVE)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 548 | return true; |
| 549 | } |
| 550 | |
| 551 | |
| 552 | bool Debug::Load() { |
| 553 | // Return if debugger is already loaded. |
kasper.lund | 212ac23 | 2008-07-16 07:07:30 +0000 | [diff] [blame] | 554 | if (IsLoaded()) return true; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 555 | |
kasper.lund | 4451067 | 2008-07-25 07:37:58 +0000 | [diff] [blame] | 556 | // Bail out if we're already in the process of compiling the native |
| 557 | // JavaScript source code for the debugger. |
mads.s.ager | cbaa060 | 2008-08-14 13:41:48 +0000 | [diff] [blame] | 558 | if (Debugger::compiling_natives() || Debugger::is_loading_debugger()) |
| 559 | return false; |
| 560 | Debugger::set_loading_debugger(true); |
kasper.lund | 4451067 | 2008-07-25 07:37:58 +0000 | [diff] [blame] | 561 | |
| 562 | // Disable breakpoints and interrupts while compiling and running the |
| 563 | // debugger scripts including the context creation code. |
| 564 | DisableBreak disable(true); |
| 565 | PostponeInterruptsScope postpone; |
| 566 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 567 | // Create the debugger context. |
| 568 | HandleScope scope; |
kasper.lund | 4451067 | 2008-07-25 07:37:58 +0000 | [diff] [blame] | 569 | Handle<Context> context = |
| 570 | Bootstrapper::CreateEnvironment(Handle<Object>::null(), |
| 571 | v8::Handle<ObjectTemplate>(), |
| 572 | NULL); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 573 | |
kasper.lund | 4451067 | 2008-07-25 07:37:58 +0000 | [diff] [blame] | 574 | // Use the debugger context. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 575 | SaveContext save; |
kasper.lund | 4451067 | 2008-07-25 07:37:58 +0000 | [diff] [blame] | 576 | Top::set_context(*context); |
| 577 | Top::set_security_context(*context); |
| 578 | |
| 579 | // Expose the builtins object in the debugger context. |
| 580 | Handle<String> key = Factory::LookupAsciiSymbol("builtins"); |
| 581 | Handle<GlobalObject> global = Handle<GlobalObject>(context->global()); |
| 582 | SetProperty(global, key, Handle<Object>(global->builtins()), NONE); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 583 | |
| 584 | // Compile the JavaScript for the debugger in the debugger context. |
| 585 | Debugger::set_compiling_natives(true); |
kasper.lund | 4451067 | 2008-07-25 07:37:58 +0000 | [diff] [blame] | 586 | bool caught_exception = |
| 587 | !CompileDebuggerScript(Natives::GetIndex("mirror")) || |
| 588 | !CompileDebuggerScript(Natives::GetIndex("debug")); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 589 | Debugger::set_compiling_natives(false); |
| 590 | |
mads.s.ager | cbaa060 | 2008-08-14 13:41:48 +0000 | [diff] [blame] | 591 | // Make sure we mark the debugger as not loading before we might |
| 592 | // return. |
| 593 | Debugger::set_loading_debugger(false); |
| 594 | |
kasper.lund | 4451067 | 2008-07-25 07:37:58 +0000 | [diff] [blame] | 595 | // Check for caught exceptions. |
| 596 | if (caught_exception) return false; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 597 | |
| 598 | // Debugger loaded. |
kasper.lund | 4451067 | 2008-07-25 07:37:58 +0000 | [diff] [blame] | 599 | debug_context_ = Handle<Context>::cast(GlobalHandles::Create(*context)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 600 | return true; |
| 601 | } |
| 602 | |
| 603 | |
| 604 | void Debug::Unload() { |
| 605 | // Return debugger is not loaded. |
| 606 | if (!IsLoaded()) { |
| 607 | return; |
| 608 | } |
| 609 | |
| 610 | // Clear debugger context global handle. |
| 611 | GlobalHandles::Destroy(reinterpret_cast<Object**>(debug_context_.location())); |
| 612 | debug_context_ = Handle<Context>(); |
| 613 | } |
| 614 | |
| 615 | |
| 616 | void Debug::Iterate(ObjectVisitor* v) { |
| 617 | #define VISIT(field) v->VisitPointer(reinterpret_cast<Object**>(&(field))); |
| 618 | VISIT(debug_break_return_entry_); |
| 619 | VISIT(debug_break_return_); |
| 620 | #undef VISIT |
| 621 | } |
| 622 | |
| 623 | |
| 624 | Object* Debug::Break(Arguments args) { |
| 625 | HandleScope scope; |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 626 | ASSERT(args.length() == 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 627 | |
kasper.lund | bd3ec4e | 2008-07-09 11:06:54 +0000 | [diff] [blame] | 628 | // Get the top-most JavaScript frame. |
| 629 | JavaScriptFrameIterator it; |
| 630 | JavaScriptFrame* frame = it.frame(); |
| 631 | |
| 632 | // Just continue if breaks are disabled or debugger cannot be loaded. |
| 633 | if (disable_break() || !Load()) { |
| 634 | SetAfterBreakTarget(frame); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 635 | return Heap::undefined_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 636 | } |
| 637 | |
| 638 | SaveBreakFrame save; |
| 639 | EnterDebuggerContext enter; |
| 640 | |
kasper.lund | 4451067 | 2008-07-25 07:37:58 +0000 | [diff] [blame] | 641 | // Postpone interrupt during breakpoint processing. |
| 642 | PostponeInterruptsScope postpone; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 643 | |
| 644 | // Get the debug info (create it if it does not exist). |
| 645 | Handle<SharedFunctionInfo> shared = |
| 646 | Handle<SharedFunctionInfo>(JSFunction::cast(frame->function())->shared()); |
| 647 | Handle<DebugInfo> debug_info = GetDebugInfo(shared); |
| 648 | |
| 649 | // Find the break point where execution has stopped. |
| 650 | BreakLocationIterator break_location_iterator(debug_info, |
| 651 | ALL_BREAK_LOCATIONS); |
| 652 | break_location_iterator.FindBreakLocationFromAddress(frame->pc()); |
| 653 | |
| 654 | // Check whether step next reached a new statement. |
| 655 | if (!StepNextContinue(&break_location_iterator, frame)) { |
| 656 | // Decrease steps left if performing multiple steps. |
| 657 | if (thread_local_.step_count_ > 0) { |
| 658 | thread_local_.step_count_--; |
| 659 | } |
| 660 | } |
| 661 | |
| 662 | // If there is one or more real break points check whether any of these are |
| 663 | // triggered. |
| 664 | Handle<Object> break_points_hit(Heap::undefined_value()); |
| 665 | if (break_location_iterator.HasBreakPoint()) { |
| 666 | Handle<Object> break_point_objects = |
| 667 | Handle<Object>(break_location_iterator.BreakPointObjects()); |
| 668 | break_points_hit = CheckBreakPoints(break_point_objects); |
| 669 | } |
| 670 | |
| 671 | // Notify debugger if a real break point is triggered or if performing single |
| 672 | // stepping with no more steps to perform. Otherwise do another step. |
| 673 | if (!break_points_hit->IsUndefined() || |
| 674 | (thread_local_.last_step_action_ != StepNone && |
| 675 | thread_local_.step_count_ == 0)) { |
| 676 | // Clear all current stepping setup. |
| 677 | ClearStepping(); |
| 678 | |
| 679 | // Notify the debug event listeners. |
| 680 | Debugger::OnDebugBreak(break_points_hit); |
| 681 | } else if (thread_local_.last_step_action_ != StepNone) { |
| 682 | // Hold on to last step action as it is cleared by the call to |
| 683 | // ClearStepping. |
| 684 | StepAction step_action = thread_local_.last_step_action_; |
| 685 | int step_count = thread_local_.step_count_; |
| 686 | |
| 687 | // Clear all current stepping setup. |
| 688 | ClearStepping(); |
| 689 | |
| 690 | // Set up for the remaining steps. |
| 691 | PrepareStep(step_action, step_count); |
| 692 | } |
| 693 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 694 | // Install jump to the call address which was overwritten. |
| 695 | SetAfterBreakTarget(frame); |
| 696 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 697 | return Heap::undefined_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 698 | } |
| 699 | |
| 700 | |
| 701 | // Check the break point objects for whether one or more are actually |
| 702 | // triggered. This function returns a JSArray with the break point objects |
| 703 | // which is triggered. |
| 704 | Handle<Object> Debug::CheckBreakPoints(Handle<Object> break_point_objects) { |
| 705 | int break_points_hit_count = 0; |
| 706 | Handle<JSArray> break_points_hit = Factory::NewJSArray(1); |
| 707 | |
| 708 | // If there are multiple break points they are in a Fixedrray. |
| 709 | ASSERT(!break_point_objects->IsUndefined()); |
| 710 | if (break_point_objects->IsFixedArray()) { |
| 711 | Handle<FixedArray> array(FixedArray::cast(*break_point_objects)); |
| 712 | for (int i = 0; i < array->length(); i++) { |
| 713 | Handle<Object> o(array->get(i)); |
| 714 | if (CheckBreakPoint(o)) { |
| 715 | break_points_hit->SetElement(break_points_hit_count++, *o); |
| 716 | } |
| 717 | } |
| 718 | } else { |
| 719 | if (CheckBreakPoint(break_point_objects)) { |
| 720 | break_points_hit->SetElement(break_points_hit_count++, |
| 721 | *break_point_objects); |
| 722 | } |
| 723 | } |
| 724 | |
| 725 | // Return undefined if no break points where triggered. |
| 726 | if (break_points_hit_count == 0) { |
| 727 | return Factory::undefined_value(); |
| 728 | } |
| 729 | return break_points_hit; |
| 730 | } |
| 731 | |
| 732 | |
| 733 | // Check whether a single break point object is triggered. |
| 734 | bool Debug::CheckBreakPoint(Handle<Object> break_point_object) { |
| 735 | // Ignore check if break point object is not a JSObject. |
| 736 | if (!break_point_object->IsJSObject()) return true; |
| 737 | |
| 738 | // Get the function CheckBreakPoint (defined in debug.js). |
| 739 | Handle<JSFunction> check_break_point = |
| 740 | Handle<JSFunction>(JSFunction::cast( |
| 741 | debug_context()->global()->GetProperty( |
| 742 | *Factory::LookupAsciiSymbol("IsBreakPointTriggered")))); |
| 743 | |
| 744 | // Get the break id as an object. |
| 745 | Handle<Object> break_id = Factory::NewNumberFromInt(Top::break_id()); |
| 746 | |
| 747 | // Call HandleBreakPointx. |
| 748 | bool caught_exception = false; |
| 749 | const int argc = 2; |
| 750 | Object** argv[argc] = { |
| 751 | break_id.location(), |
| 752 | reinterpret_cast<Object**>(break_point_object.location()) |
| 753 | }; |
| 754 | Handle<Object> result = Execution::TryCall(check_break_point, |
| 755 | Top::builtins(), argc, argv, |
| 756 | &caught_exception); |
| 757 | |
| 758 | // If exception or non boolean result handle as not triggered |
| 759 | if (caught_exception || !result->IsBoolean()) { |
| 760 | return false; |
| 761 | } |
| 762 | |
| 763 | // Return whether the break point is triggered. |
| 764 | return *result == Heap::true_value(); |
| 765 | } |
| 766 | |
| 767 | |
| 768 | // Check whether the function has debug information. |
| 769 | bool Debug::HasDebugInfo(Handle<SharedFunctionInfo> shared) { |
| 770 | return !shared->debug_info()->IsUndefined(); |
| 771 | } |
| 772 | |
| 773 | |
kasper.lund | bd3ec4e | 2008-07-09 11:06:54 +0000 | [diff] [blame] | 774 | // Return the debug info for this function. EnsureDebugInfo must be called |
| 775 | // prior to ensure the debug info has been generated for shared. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 776 | Handle<DebugInfo> Debug::GetDebugInfo(Handle<SharedFunctionInfo> shared) { |
kasper.lund | bd3ec4e | 2008-07-09 11:06:54 +0000 | [diff] [blame] | 777 | ASSERT(HasDebugInfo(shared)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 778 | return Handle<DebugInfo>(DebugInfo::cast(shared->debug_info())); |
| 779 | } |
| 780 | |
| 781 | |
| 782 | void Debug::SetBreakPoint(Handle<SharedFunctionInfo> shared, |
| 783 | int source_position, |
| 784 | Handle<Object> break_point_object) { |
kasper.lund | bd3ec4e | 2008-07-09 11:06:54 +0000 | [diff] [blame] | 785 | if (!EnsureDebugInfo(shared)) { |
| 786 | // Return if retrieving debug info failed. |
| 787 | return; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 788 | } |
| 789 | |
kasper.lund | bd3ec4e | 2008-07-09 11:06:54 +0000 | [diff] [blame] | 790 | Handle<DebugInfo> debug_info = GetDebugInfo(shared); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 791 | // Source positions starts with zero. |
| 792 | ASSERT(source_position >= 0); |
| 793 | |
| 794 | // Find the break point and change it. |
| 795 | BreakLocationIterator it(debug_info, SOURCE_BREAK_LOCATIONS); |
| 796 | it.FindBreakLocationFromPosition(source_position); |
| 797 | it.SetBreakPoint(break_point_object); |
| 798 | |
| 799 | // At least one active break point now. |
| 800 | ASSERT(debug_info->GetBreakPointCount() > 0); |
| 801 | } |
| 802 | |
| 803 | |
| 804 | void Debug::ClearBreakPoint(Handle<Object> break_point_object) { |
| 805 | DebugInfoListNode* node = debug_info_list_; |
| 806 | while (node != NULL) { |
| 807 | Object* result = DebugInfo::FindBreakPointInfo(node->debug_info(), |
| 808 | break_point_object); |
| 809 | if (!result->IsUndefined()) { |
| 810 | // Get information in the break point. |
| 811 | BreakPointInfo* break_point_info = BreakPointInfo::cast(result); |
| 812 | Handle<DebugInfo> debug_info = node->debug_info(); |
| 813 | Handle<SharedFunctionInfo> shared(debug_info->shared()); |
| 814 | int source_position = break_point_info->statement_position()->value(); |
| 815 | |
| 816 | // Source positions starts with zero. |
| 817 | ASSERT(source_position >= 0); |
| 818 | |
| 819 | // Find the break point and clear it. |
| 820 | BreakLocationIterator it(debug_info, SOURCE_BREAK_LOCATIONS); |
| 821 | it.FindBreakLocationFromPosition(source_position); |
| 822 | it.ClearBreakPoint(break_point_object); |
| 823 | |
| 824 | // If there are no more break points left remove the debug info for this |
| 825 | // function. |
| 826 | if (debug_info->GetBreakPointCount() == 0) { |
| 827 | RemoveDebugInfo(debug_info); |
| 828 | } |
| 829 | |
| 830 | return; |
| 831 | } |
| 832 | node = node->next(); |
| 833 | } |
| 834 | } |
| 835 | |
| 836 | |
| 837 | void Debug::FloodWithOneShot(Handle<SharedFunctionInfo> shared) { |
kasper.lund | bd3ec4e | 2008-07-09 11:06:54 +0000 | [diff] [blame] | 838 | // Make sure the function has setup the debug info. |
| 839 | if (!EnsureDebugInfo(shared)) { |
| 840 | // Return if we failed to retrieve the debug info. |
| 841 | return; |
| 842 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 843 | |
| 844 | // Flood the function with break points. |
kasper.lund | bd3ec4e | 2008-07-09 11:06:54 +0000 | [diff] [blame] | 845 | BreakLocationIterator it(GetDebugInfo(shared), ALL_BREAK_LOCATIONS); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 846 | while (!it.Done()) { |
| 847 | it.SetOneShot(); |
| 848 | it.Next(); |
| 849 | } |
| 850 | } |
| 851 | |
| 852 | |
| 853 | void Debug::FloodHandlerWithOneShot() { |
| 854 | StackFrame::Id id = Top::break_frame_id(); |
| 855 | for (JavaScriptFrameIterator it(id); !it.done(); it.Advance()) { |
| 856 | JavaScriptFrame* frame = it.frame(); |
| 857 | if (frame->HasHandler()) { |
| 858 | Handle<SharedFunctionInfo> shared = |
| 859 | Handle<SharedFunctionInfo>( |
| 860 | JSFunction::cast(frame->function())->shared()); |
| 861 | // Flood the function with the catch block with break points |
| 862 | FloodWithOneShot(shared); |
| 863 | return; |
| 864 | } |
| 865 | } |
| 866 | } |
| 867 | |
| 868 | |
| 869 | void Debug::ChangeBreakOnException(ExceptionBreakType type, bool enable) { |
| 870 | if (type == BreakUncaughtException) { |
| 871 | break_on_uncaught_exception_ = enable; |
| 872 | } else { |
| 873 | break_on_exception_ = enable; |
| 874 | } |
| 875 | } |
| 876 | |
| 877 | |
| 878 | void Debug::PrepareStep(StepAction step_action, int step_count) { |
| 879 | HandleScope scope; |
| 880 | ASSERT(Debug::InDebugger()); |
| 881 | |
| 882 | // Remember this step action and count. |
| 883 | thread_local_.last_step_action_ = step_action; |
| 884 | thread_local_.step_count_ = step_count; |
| 885 | |
| 886 | // Get the frame where the execution has stopped and skip the debug frame if |
| 887 | // any. The debug frame will only be present if execution was stopped due to |
| 888 | // hitting a break point. In other situations (e.g. unhandled exception) the |
| 889 | // debug frame is not present. |
| 890 | StackFrame::Id id = Top::break_frame_id(); |
| 891 | JavaScriptFrameIterator frames_it(id); |
| 892 | JavaScriptFrame* frame = frames_it.frame(); |
| 893 | |
| 894 | // First of all ensure there is one-shot break points in the top handler |
| 895 | // if any. |
| 896 | FloodHandlerWithOneShot(); |
| 897 | |
| 898 | // If the function on the top frame is unresolved perform step out. This will |
| 899 | // be the case when calling unknown functions and having the debugger stopped |
| 900 | // in an unhandled exception. |
| 901 | if (!frame->function()->IsJSFunction()) { |
| 902 | // Step out: Find the calling JavaScript frame and flood it with |
| 903 | // breakpoints. |
| 904 | frames_it.Advance(); |
| 905 | // Fill the function to return to with one-shot break points. |
| 906 | JSFunction* function = JSFunction::cast(frames_it.frame()->function()); |
| 907 | FloodWithOneShot(Handle<SharedFunctionInfo>(function->shared())); |
| 908 | return; |
| 909 | } |
| 910 | |
| 911 | // Get the debug info (create it if it does not exist). |
| 912 | Handle<SharedFunctionInfo> shared = |
| 913 | Handle<SharedFunctionInfo>(JSFunction::cast(frame->function())->shared()); |
kasper.lund | bd3ec4e | 2008-07-09 11:06:54 +0000 | [diff] [blame] | 914 | if (!EnsureDebugInfo(shared)) { |
| 915 | // Return if ensuring debug info failed. |
| 916 | return; |
| 917 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 918 | Handle<DebugInfo> debug_info = GetDebugInfo(shared); |
| 919 | |
| 920 | // Find the break location where execution has stopped. |
| 921 | BreakLocationIterator it(debug_info, ALL_BREAK_LOCATIONS); |
| 922 | it.FindBreakLocationFromAddress(frame->pc()); |
| 923 | |
| 924 | // Compute whether or not the target is a call target. |
| 925 | bool is_call_target = false; |
| 926 | if (is_code_target(it.rinfo()->rmode())) { |
| 927 | Address target = it.rinfo()->target_address(); |
| 928 | Code* code = Debug::GetCodeTarget(target); |
| 929 | if (code->is_call_stub()) is_call_target = true; |
| 930 | } |
| 931 | |
| 932 | // If this is the last break code target step out is the only posibility. |
| 933 | if (it.IsExit() || step_action == StepOut) { |
| 934 | // Step out: If there is a JavaScript caller frame, we need to |
| 935 | // flood it with breakpoints. |
| 936 | frames_it.Advance(); |
| 937 | if (!frames_it.done()) { |
| 938 | // Fill the function to return to with one-shot break points. |
| 939 | JSFunction* function = JSFunction::cast(frames_it.frame()->function()); |
| 940 | FloodWithOneShot(Handle<SharedFunctionInfo>(function->shared())); |
| 941 | } |
| 942 | } else if (!(is_call_target || is_js_construct_call(it.rmode())) || |
| 943 | step_action == StepNext || step_action == StepMin) { |
| 944 | // Step next or step min. |
| 945 | |
| 946 | // Fill the current function with one-shot break points. |
| 947 | FloodWithOneShot(shared); |
| 948 | |
| 949 | // Remember source position and frame to handle step next. |
| 950 | thread_local_.last_statement_position_ = |
| 951 | debug_info->code()->SourceStatementPosition(frame->pc()); |
| 952 | thread_local_.last_fp_ = frame->fp(); |
| 953 | } else { |
| 954 | // Fill the current function with one-shot break points even for step in on |
| 955 | // a call target as the function called might be a native function for |
| 956 | // which step in will not stop. |
| 957 | FloodWithOneShot(shared); |
| 958 | |
| 959 | // Step in or Step in min |
| 960 | it.PrepareStepIn(); |
| 961 | ActivateStepIn(frame); |
| 962 | } |
| 963 | } |
| 964 | |
| 965 | |
| 966 | // Check whether the current debug break should be reported to the debugger. It |
| 967 | // is used to have step next and step in only report break back to the debugger |
| 968 | // if on a different frame or in a different statement. In some situations |
| 969 | // there will be several break points in the same statement when the code is |
| 970 | // flodded with one-shot break points. This function helps to perform several |
| 971 | // steps before reporting break back to the debugger. |
| 972 | bool Debug::StepNextContinue(BreakLocationIterator* break_location_iterator, |
| 973 | JavaScriptFrame* frame) { |
| 974 | // If the step last action was step next or step in make sure that a new |
| 975 | // statement is hit. |
| 976 | if (thread_local_.last_step_action_ == StepNext || |
| 977 | thread_local_.last_step_action_ == StepIn) { |
| 978 | // Never continue if returning from function. |
| 979 | if (break_location_iterator->IsExit()) return false; |
| 980 | |
| 981 | // Continue if we are still on the same frame and in the same statement. |
| 982 | int current_statement_position = |
| 983 | break_location_iterator->code()->SourceStatementPosition(frame->pc()); |
| 984 | return thread_local_.last_fp_ == frame->fp() && |
| 985 | thread_local_.last_statement_position_ == current_statement_position; |
| 986 | } |
| 987 | |
| 988 | // No step next action - don't continue. |
| 989 | return false; |
| 990 | } |
| 991 | |
| 992 | |
| 993 | // Check whether the code object at the specified address is a debug break code |
| 994 | // object. |
| 995 | bool Debug::IsDebugBreak(Address addr) { |
| 996 | Code* code = GetCodeTarget(addr); |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 997 | return code->ic_state() == DEBUG_BREAK; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 998 | } |
| 999 | |
| 1000 | |
| 1001 | // Check whether a code stub with the specified major key is a possible break |
| 1002 | // point location when looking for source break locations. |
| 1003 | bool Debug::IsSourceBreakStub(Code* code) { |
| 1004 | CodeStub::Major major_key = code->major_key(); |
| 1005 | return major_key == CodeStub::CallFunction; |
| 1006 | } |
| 1007 | |
| 1008 | |
| 1009 | // Check whether a code stub with the specified major key is a possible break |
| 1010 | // location. |
| 1011 | bool Debug::IsBreakStub(Code* code) { |
| 1012 | CodeStub::Major major_key = code->major_key(); |
| 1013 | return major_key == CodeStub::CallFunction || |
| 1014 | major_key == CodeStub::StackCheck; |
| 1015 | } |
| 1016 | |
| 1017 | |
| 1018 | // Find the builtin to use for invoking the debug break |
| 1019 | Handle<Code> Debug::FindDebugBreak(RelocInfo* rinfo) { |
| 1020 | // Find the builtin debug break function matching the calling convention |
| 1021 | // used by the call site. |
| 1022 | RelocMode mode = rinfo->rmode(); |
| 1023 | |
| 1024 | if (is_code_target(mode)) { |
| 1025 | Address target = rinfo->target_address(); |
| 1026 | Code* code = Debug::GetCodeTarget(target); |
| 1027 | if (code->is_inline_cache_stub()) { |
| 1028 | if (code->is_call_stub()) { |
| 1029 | return ComputeCallDebugBreak(code->arguments_count()); |
| 1030 | } |
| 1031 | if (code->is_load_stub()) { |
| 1032 | return Handle<Code>(Builtins::builtin(Builtins::LoadIC_DebugBreak)); |
| 1033 | } |
| 1034 | if (code->is_store_stub()) { |
| 1035 | return Handle<Code>(Builtins::builtin(Builtins::StoreIC_DebugBreak)); |
| 1036 | } |
| 1037 | if (code->is_keyed_load_stub()) { |
| 1038 | Handle<Code> result = |
| 1039 | Handle<Code>(Builtins::builtin(Builtins::KeyedLoadIC_DebugBreak)); |
| 1040 | return result; |
| 1041 | } |
| 1042 | if (code->is_keyed_store_stub()) { |
| 1043 | Handle<Code> result = |
| 1044 | Handle<Code>(Builtins::builtin(Builtins::KeyedStoreIC_DebugBreak)); |
| 1045 | return result; |
| 1046 | } |
| 1047 | } |
| 1048 | if (is_js_construct_call(mode)) { |
| 1049 | Handle<Code> result = |
| 1050 | Handle<Code>(Builtins::builtin(Builtins::ConstructCall_DebugBreak)); |
| 1051 | return result; |
| 1052 | } |
| 1053 | // Currently is_exit_js_frame is used on ARM. |
| 1054 | if (is_exit_js_frame(mode)) { |
| 1055 | return Handle<Code>(Builtins::builtin(Builtins::Return_DebugBreak)); |
| 1056 | } |
| 1057 | if (code->kind() == Code::STUB) { |
| 1058 | ASSERT(code->major_key() == CodeStub::CallFunction || |
| 1059 | code->major_key() == CodeStub::StackCheck); |
| 1060 | Handle<Code> result = |
| 1061 | Handle<Code>(Builtins::builtin(Builtins::StubNoRegisters_DebugBreak)); |
| 1062 | return result; |
| 1063 | } |
| 1064 | } |
| 1065 | |
| 1066 | UNREACHABLE(); |
| 1067 | return Handle<Code>::null(); |
| 1068 | } |
| 1069 | |
| 1070 | |
| 1071 | // Simple function for returning the source positions for active break points. |
| 1072 | Handle<Object> Debug::GetSourceBreakLocations( |
| 1073 | Handle<SharedFunctionInfo> shared) { |
| 1074 | if (!HasDebugInfo(shared)) return Handle<Object>(Heap::undefined_value()); |
| 1075 | Handle<DebugInfo> debug_info = GetDebugInfo(shared); |
| 1076 | if (debug_info->GetBreakPointCount() == 0) { |
| 1077 | return Handle<Object>(Heap::undefined_value()); |
| 1078 | } |
| 1079 | Handle<FixedArray> locations = |
| 1080 | Factory::NewFixedArray(debug_info->GetBreakPointCount()); |
| 1081 | int count = 0; |
| 1082 | for (int i = 0; i < debug_info->break_points()->length(); i++) { |
| 1083 | if (!debug_info->break_points()->get(i)->IsUndefined()) { |
| 1084 | BreakPointInfo* break_point_info = |
| 1085 | BreakPointInfo::cast(debug_info->break_points()->get(i)); |
| 1086 | if (break_point_info->GetBreakPointCount() > 0) { |
| 1087 | locations->set(count++, break_point_info->statement_position()); |
| 1088 | } |
| 1089 | } |
| 1090 | } |
| 1091 | return locations; |
| 1092 | } |
| 1093 | |
| 1094 | |
| 1095 | void Debug::ClearStepping() { |
| 1096 | // Clear the various stepping setup. |
| 1097 | ClearOneShot(); |
| 1098 | ClearStepIn(); |
| 1099 | ClearStepNext(); |
| 1100 | |
| 1101 | // Clear multiple step counter. |
| 1102 | thread_local_.step_count_ = 0; |
| 1103 | } |
| 1104 | |
| 1105 | // Clears all the one-shot break points that are currently set. Normally this |
| 1106 | // function is called each time a break point is hit as one shot break points |
| 1107 | // are used to support stepping. |
| 1108 | void Debug::ClearOneShot() { |
| 1109 | // The current implementation just runs through all the breakpoints. When the |
| 1110 | // last break point for a function is removed that function is automaticaly |
| 1111 | // removed from the list. |
| 1112 | |
| 1113 | DebugInfoListNode* node = debug_info_list_; |
| 1114 | while (node != NULL) { |
| 1115 | BreakLocationIterator it(node->debug_info(), ALL_BREAK_LOCATIONS); |
| 1116 | while (!it.Done()) { |
| 1117 | it.ClearOneShot(); |
| 1118 | it.Next(); |
| 1119 | } |
| 1120 | node = node->next(); |
| 1121 | } |
| 1122 | } |
| 1123 | |
| 1124 | |
| 1125 | void Debug::ActivateStepIn(StackFrame* frame) { |
| 1126 | thread_local_.step_into_fp_ = frame->fp(); |
| 1127 | } |
| 1128 | |
| 1129 | |
| 1130 | void Debug::ClearStepIn() { |
| 1131 | thread_local_.step_into_fp_ = 0; |
| 1132 | } |
| 1133 | |
| 1134 | |
| 1135 | void Debug::ClearStepNext() { |
| 1136 | thread_local_.last_step_action_ = StepNone; |
| 1137 | thread_local_.last_statement_position_ = kNoPosition; |
| 1138 | thread_local_.last_fp_ = 0; |
| 1139 | } |
| 1140 | |
| 1141 | |
kasper.lund | bd3ec4e | 2008-07-09 11:06:54 +0000 | [diff] [blame] | 1142 | bool Debug::EnsureCompiled(Handle<SharedFunctionInfo> shared) { |
| 1143 | if (shared->is_compiled()) return true; |
| 1144 | return CompileLazyShared(shared, CLEAR_EXCEPTION); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1145 | } |
| 1146 | |
| 1147 | |
kasper.lund | bd3ec4e | 2008-07-09 11:06:54 +0000 | [diff] [blame] | 1148 | // Ensures the debug information is present for shared. |
| 1149 | bool Debug::EnsureDebugInfo(Handle<SharedFunctionInfo> shared) { |
| 1150 | // Return if we already have the debug info for shared. |
| 1151 | if (HasDebugInfo(shared)) return true; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1152 | |
kasper.lund | bd3ec4e | 2008-07-09 11:06:54 +0000 | [diff] [blame] | 1153 | // Ensure shared in compiled. Return false if this failed. |
| 1154 | if (!EnsureCompiled(shared)) return false; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1155 | |
| 1156 | // Create the debug info object. |
| 1157 | Handle<DebugInfo> debug_info = |
| 1158 | Handle<DebugInfo>::cast(Factory::NewStruct(DEBUG_INFO_TYPE)); |
| 1159 | |
| 1160 | // Get the function original code. |
| 1161 | Handle<Code> code(shared->code()); |
| 1162 | |
| 1163 | // Debug info contains function, a copy of the original code and the executing |
| 1164 | // code. |
| 1165 | debug_info->set_shared(*shared); |
| 1166 | debug_info->set_original_code(*Factory::CopyCode(code)); |
| 1167 | debug_info->set_code(*code); |
| 1168 | |
| 1169 | // Link debug info to function. |
| 1170 | shared->set_debug_info(*debug_info); |
| 1171 | |
| 1172 | // Initially no active break points. |
| 1173 | debug_info->set_break_points( |
| 1174 | *Factory::NewFixedArray(Debug::kEstimatedNofBreakPointsInFunction)); |
| 1175 | |
| 1176 | // Add debug info to the list. |
| 1177 | DebugInfoListNode* node = new DebugInfoListNode(*debug_info); |
| 1178 | node->set_next(debug_info_list_); |
| 1179 | debug_info_list_ = node; |
| 1180 | |
| 1181 | // Now there is at least one break point. |
| 1182 | has_break_points_ = true; |
| 1183 | |
kasper.lund | bd3ec4e | 2008-07-09 11:06:54 +0000 | [diff] [blame] | 1184 | return true; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1185 | } |
| 1186 | |
| 1187 | |
| 1188 | void Debug::RemoveDebugInfo(Handle<DebugInfo> debug_info) { |
| 1189 | ASSERT(debug_info_list_ != NULL); |
| 1190 | // Run through the debug info objects to find this one and remove it. |
| 1191 | DebugInfoListNode* prev = NULL; |
| 1192 | DebugInfoListNode* current = debug_info_list_; |
| 1193 | while (current != NULL) { |
| 1194 | if (*current->debug_info() == *debug_info) { |
| 1195 | // Unlink from list. If prev is NULL we are looking at the first element. |
| 1196 | if (prev == NULL) { |
| 1197 | debug_info_list_ = current->next(); |
| 1198 | } else { |
| 1199 | prev->set_next(current->next()); |
| 1200 | } |
| 1201 | current->debug_info()->shared()->set_debug_info(Heap::undefined_value()); |
| 1202 | delete current; |
| 1203 | |
| 1204 | // If there are no more debug info objects there are not more break |
| 1205 | // points. |
| 1206 | has_break_points_ = debug_info_list_ != NULL; |
| 1207 | |
| 1208 | return; |
| 1209 | } |
| 1210 | // Move to next in list. |
| 1211 | prev = current; |
| 1212 | current = current->next(); |
| 1213 | } |
| 1214 | UNREACHABLE(); |
| 1215 | } |
| 1216 | |
| 1217 | |
| 1218 | void Debug::SetAfterBreakTarget(JavaScriptFrame* frame) { |
| 1219 | // Get the executing function in which the debug break occurred. |
| 1220 | Handle<SharedFunctionInfo> shared = |
| 1221 | Handle<SharedFunctionInfo>(JSFunction::cast(frame->function())->shared()); |
kasper.lund | bd3ec4e | 2008-07-09 11:06:54 +0000 | [diff] [blame] | 1222 | if (!EnsureDebugInfo(shared)) { |
| 1223 | // Return if we failed to retrieve the debug info. |
| 1224 | return; |
| 1225 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1226 | Handle<DebugInfo> debug_info = GetDebugInfo(shared); |
| 1227 | Handle<Code> code(debug_info->code()); |
| 1228 | Handle<Code> original_code(debug_info->original_code()); |
| 1229 | #ifdef DEBUG |
| 1230 | // Get the code which is actually executing. |
| 1231 | Handle<Code> frame_code(frame->FindCode()); |
| 1232 | ASSERT(frame_code.is_identical_to(code)); |
| 1233 | #endif |
| 1234 | |
| 1235 | // Find the call address in the running code. This address holds the call to |
| 1236 | // either a DebugBreakXXX or to the debug break return entry code if the |
| 1237 | // break point is still active after processing the break point. |
| 1238 | Address addr = frame->pc() - Assembler::kTargetAddrToReturnAddrDist; |
| 1239 | |
| 1240 | // Check if the location is at JS exit. |
| 1241 | bool at_js_exit = false; |
| 1242 | RelocIterator it(debug_info->code()); |
| 1243 | while (!it.done()) { |
| 1244 | if (is_js_return(it.rinfo()->rmode())) { |
| 1245 | at_js_exit = it.rinfo()->pc() == addr - 1; |
| 1246 | } |
| 1247 | it.next(); |
| 1248 | } |
| 1249 | |
| 1250 | // Handle the jump to continue execution after break point depending on the |
| 1251 | // break location. |
| 1252 | if (at_js_exit) { |
| 1253 | // First check if the call in the code is still the debug break return |
| 1254 | // entry code. If it is the break point is still active. If not the break |
| 1255 | // point was removed during break point processing. |
| 1256 | if (Assembler::target_address_at(addr) == |
| 1257 | debug_break_return_entry()->entry()) { |
| 1258 | // Break point still active. Jump to the corresponding place in the |
| 1259 | // original code. |
| 1260 | addr += original_code->instruction_start() - code->instruction_start(); |
| 1261 | } |
| 1262 | |
| 1263 | // Move one byte back to where the call instruction was placed. |
| 1264 | thread_local_.after_break_target_ = addr - 1; |
| 1265 | } else { |
| 1266 | // Check if there still is a debug break call at the target address. If the |
| 1267 | // break point has been removed it will have disappeared. If it have |
| 1268 | // disappeared don't try to look in the original code as the running code |
| 1269 | // will have the right address. This takes care of the case where the last |
| 1270 | // break point is removed from the function and therefore no "original code" |
| 1271 | // is available. If the debug break call is still there find the address in |
| 1272 | // the original code. |
| 1273 | if (IsDebugBreak(Assembler::target_address_at(addr))) { |
| 1274 | // If the break point is still there find the call address which was |
| 1275 | // overwritten in the original code by the call to DebugBreakXXX. |
| 1276 | |
| 1277 | // Find the corresponding address in the original code. |
| 1278 | addr += original_code->instruction_start() - code->instruction_start(); |
| 1279 | } |
| 1280 | |
| 1281 | // Install jump to the call address in the original code. This will be the |
| 1282 | // call which was overwritten by the call to DebugBreakXXX. |
| 1283 | thread_local_.after_break_target_ = Assembler::target_address_at(addr); |
| 1284 | } |
| 1285 | } |
| 1286 | |
| 1287 | |
| 1288 | Code* Debug::GetCodeTarget(Address target) { |
| 1289 | // Maybe this can be refactored with the stuff in ic-inl.h? |
| 1290 | Code* result = |
| 1291 | Code::cast(HeapObject::FromAddress(target - Code::kHeaderSize)); |
| 1292 | return result; |
| 1293 | } |
| 1294 | |
| 1295 | |
| 1296 | bool Debug::IsDebugGlobal(GlobalObject* global) { |
| 1297 | return IsLoaded() && global == Debug::debug_context()->global(); |
| 1298 | } |
| 1299 | |
| 1300 | |
| 1301 | bool Debugger::debugger_active_ = false; |
| 1302 | bool Debugger::compiling_natives_ = false; |
mads.s.ager | cbaa060 | 2008-08-14 13:41:48 +0000 | [diff] [blame] | 1303 | bool Debugger::is_loading_debugger_ = false; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1304 | DebugMessageThread* Debugger::message_thread_ = NULL; |
| 1305 | v8::DebugMessageHandler Debugger::debug_message_handler_ = NULL; |
| 1306 | void* Debugger::debug_message_handler_data_ = NULL; |
| 1307 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1308 | |
| 1309 | Handle<Object> Debugger::MakeJSObject(Vector<const char> constructor_name, |
| 1310 | int argc, Object*** argv, |
| 1311 | bool* caught_exception) { |
| 1312 | ASSERT(Top::context() == *Debug::debug_context()); |
| 1313 | |
| 1314 | // Create the execution state object. |
| 1315 | Handle<String> constructor_str = Factory::LookupSymbol(constructor_name); |
| 1316 | Handle<Object> constructor(Top::global()->GetProperty(*constructor_str)); |
| 1317 | ASSERT(constructor->IsJSFunction()); |
| 1318 | if (!constructor->IsJSFunction()) { |
| 1319 | *caught_exception = true; |
| 1320 | return Factory::undefined_value(); |
| 1321 | } |
| 1322 | Handle<Object> js_object = Execution::TryCall( |
| 1323 | Handle<JSFunction>::cast(constructor), |
| 1324 | Handle<JSObject>(Debug::debug_context()->global()), argc, argv, |
| 1325 | caught_exception); |
| 1326 | return js_object; |
| 1327 | } |
| 1328 | |
| 1329 | |
| 1330 | Handle<Object> Debugger::MakeExecutionState(bool* caught_exception) { |
| 1331 | // Create the execution state object. |
| 1332 | Handle<Object> break_id = Factory::NewNumberFromInt(Top::break_id()); |
| 1333 | const int argc = 1; |
| 1334 | Object** argv[argc] = { break_id.location() }; |
| 1335 | return MakeJSObject(CStrVector("MakeExecutionState"), |
| 1336 | argc, argv, caught_exception); |
| 1337 | } |
| 1338 | |
| 1339 | |
| 1340 | Handle<Object> Debugger::MakeBreakEvent(Handle<Object> exec_state, |
| 1341 | Handle<Object> break_points_hit, |
| 1342 | bool* caught_exception) { |
| 1343 | // Create the new break event object. |
| 1344 | const int argc = 2; |
| 1345 | Object** argv[argc] = { exec_state.location(), |
| 1346 | break_points_hit.location() }; |
| 1347 | return MakeJSObject(CStrVector("MakeBreakEvent"), |
| 1348 | argc, |
| 1349 | argv, |
| 1350 | caught_exception); |
| 1351 | } |
| 1352 | |
| 1353 | |
| 1354 | Handle<Object> Debugger::MakeExceptionEvent(Handle<Object> exec_state, |
| 1355 | Handle<Object> exception, |
| 1356 | bool uncaught, |
| 1357 | bool* caught_exception) { |
| 1358 | // Create the new exception event object. |
| 1359 | const int argc = 3; |
| 1360 | Object** argv[argc] = { exec_state.location(), |
| 1361 | exception.location(), |
| 1362 | uncaught ? Factory::true_value().location() : |
| 1363 | Factory::false_value().location()}; |
| 1364 | return MakeJSObject(CStrVector("MakeExceptionEvent"), |
| 1365 | argc, argv, caught_exception); |
| 1366 | } |
| 1367 | |
| 1368 | |
| 1369 | Handle<Object> Debugger::MakeNewFunctionEvent(Handle<Object> function, |
| 1370 | bool* caught_exception) { |
| 1371 | // Create the new function event object. |
| 1372 | const int argc = 1; |
| 1373 | Object** argv[argc] = { function.location() }; |
| 1374 | return MakeJSObject(CStrVector("MakeNewFunctionEvent"), |
| 1375 | argc, argv, caught_exception); |
| 1376 | } |
| 1377 | |
| 1378 | |
| 1379 | Handle<Object> Debugger::MakeCompileEvent(Handle<Script> script, |
| 1380 | Handle<Object> script_function, |
| 1381 | bool* caught_exception) { |
| 1382 | // Create the compile event object. |
| 1383 | Handle<Object> exec_state = MakeExecutionState(caught_exception); |
| 1384 | Handle<Object> script_source(script->source()); |
| 1385 | Handle<Object> script_name(script->name()); |
| 1386 | const int argc = 3; |
| 1387 | Object** argv[argc] = { script_source.location(), |
| 1388 | script_name.location(), |
| 1389 | script_function.location() }; |
| 1390 | return MakeJSObject(CStrVector("MakeCompileEvent"), |
| 1391 | argc, |
| 1392 | argv, |
| 1393 | caught_exception); |
| 1394 | } |
| 1395 | |
| 1396 | |
| 1397 | Handle<String> Debugger::ProcessRequest(Handle<Object> exec_state, |
| 1398 | Handle<Object> request, |
| 1399 | bool stopped) { |
| 1400 | // Get the function ProcessDebugRequest (declared in debug.js). |
| 1401 | Handle<JSFunction> process_denbug_request = |
| 1402 | Handle<JSFunction>(JSFunction::cast( |
| 1403 | Debug::debug_context()->global()->GetProperty( |
| 1404 | *Factory::LookupAsciiSymbol("ProcessDebugRequest")))); |
| 1405 | |
| 1406 | // Call ProcessDebugRequest expect String result. The ProcessDebugRequest |
| 1407 | // will never throw an exception (see debug.js). |
kasper.lund | 212ac23 | 2008-07-16 07:07:30 +0000 | [diff] [blame] | 1408 | bool caught_exception; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1409 | const int argc = 3; |
| 1410 | Object** argv[argc] = { exec_state.location(), |
| 1411 | request.location(), |
| 1412 | stopped ? Factory::true_value().location() : |
| 1413 | Factory::false_value().location()}; |
kasper.lund | 212ac23 | 2008-07-16 07:07:30 +0000 | [diff] [blame] | 1414 | Handle<Object> result = Execution::TryCall(process_denbug_request, |
| 1415 | Factory::undefined_value(), |
| 1416 | argc, argv, |
| 1417 | &caught_exception); |
| 1418 | if (caught_exception) { |
| 1419 | return Factory::empty_symbol(); |
| 1420 | } |
| 1421 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1422 | return Handle<String>::cast(result); |
| 1423 | } |
| 1424 | |
| 1425 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1426 | void Debugger::OnException(Handle<Object> exception, bool uncaught) { |
| 1427 | HandleScope scope; |
| 1428 | |
| 1429 | // Bail out based on state or if there is no listener for this event |
| 1430 | if (Debug::InDebugger()) return; |
| 1431 | if (!Debugger::EventActive(v8::Exception)) return; |
| 1432 | |
| 1433 | // Bail out if exception breaks are not active |
| 1434 | if (uncaught) { |
| 1435 | // Uncaught exceptions are reported by either flags. |
| 1436 | if (!(Debug::break_on_uncaught_exception() || |
| 1437 | Debug::break_on_exception())) return; |
| 1438 | } else { |
| 1439 | // Caught exceptions are reported is activated. |
| 1440 | if (!Debug::break_on_exception()) return; |
| 1441 | } |
| 1442 | |
kasper.lund | 212ac23 | 2008-07-16 07:07:30 +0000 | [diff] [blame] | 1443 | // Enter the debugger. Bail out if the debugger cannot be loaded. |
| 1444 | if (!Debug::Load()) return; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1445 | SaveBreakFrame save; |
| 1446 | EnterDebuggerContext enter; |
| 1447 | |
| 1448 | // Clear all current stepping setup. |
| 1449 | Debug::ClearStepping(); |
| 1450 | // Create the event data object. |
| 1451 | bool caught_exception = false; |
| 1452 | Handle<Object> exec_state = MakeExecutionState(&caught_exception); |
| 1453 | Handle<Object> event_data; |
| 1454 | if (!caught_exception) { |
| 1455 | event_data = MakeExceptionEvent(exec_state, exception, uncaught, |
| 1456 | &caught_exception); |
| 1457 | } |
| 1458 | // Bail out and don't call debugger if exception. |
| 1459 | if (caught_exception) { |
| 1460 | return; |
| 1461 | } |
| 1462 | |
| 1463 | // Process debug event |
| 1464 | ProcessDebugEvent(v8::Exception, event_data); |
| 1465 | // Return to continue execution from where the exception was thrown. |
| 1466 | } |
| 1467 | |
| 1468 | |
| 1469 | void Debugger::OnDebugBreak(Handle<Object> break_points_hit) { |
| 1470 | HandleScope scope; |
| 1471 | |
kasper.lund | 212ac23 | 2008-07-16 07:07:30 +0000 | [diff] [blame] | 1472 | // Debugger has already been entered by caller. |
| 1473 | ASSERT(Top::context() == *Debug::debug_context()); |
| 1474 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1475 | // Bail out if there is no listener for this event |
| 1476 | if (!Debugger::EventActive(v8::Break)) return; |
| 1477 | |
| 1478 | // Debugger must be entered in advance. |
| 1479 | ASSERT(Top::context() == *Debug::debug_context()); |
| 1480 | |
| 1481 | // Create the event data object. |
| 1482 | bool caught_exception = false; |
| 1483 | Handle<Object> exec_state = MakeExecutionState(&caught_exception); |
| 1484 | Handle<Object> event_data; |
| 1485 | if (!caught_exception) { |
| 1486 | event_data = MakeBreakEvent(exec_state, break_points_hit, |
| 1487 | &caught_exception); |
| 1488 | } |
| 1489 | // Bail out and don't call debugger if exception. |
| 1490 | if (caught_exception) { |
| 1491 | return; |
| 1492 | } |
| 1493 | |
| 1494 | // Process debug event |
| 1495 | ProcessDebugEvent(v8::Break, event_data); |
| 1496 | } |
| 1497 | |
| 1498 | |
| 1499 | void Debugger::OnBeforeCompile(Handle<Script> script) { |
| 1500 | HandleScope scope; |
| 1501 | |
| 1502 | // Bail out based on state or if there is no listener for this event |
| 1503 | if (Debug::InDebugger()) return; |
| 1504 | if (compiling_natives()) return; |
| 1505 | if (!EventActive(v8::BeforeCompile)) return; |
| 1506 | |
kasper.lund | 212ac23 | 2008-07-16 07:07:30 +0000 | [diff] [blame] | 1507 | // Enter the debugger. Bail out if the debugger cannot be loaded. |
| 1508 | if (!Debug::Load()) return; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1509 | SaveBreakFrame save; |
| 1510 | EnterDebuggerContext enter; |
| 1511 | |
| 1512 | // Create the event data object. |
| 1513 | bool caught_exception = false; |
| 1514 | Handle<Object> event_data = MakeCompileEvent(script, |
| 1515 | Factory::undefined_value(), |
| 1516 | &caught_exception); |
| 1517 | // Bail out and don't call debugger if exception. |
| 1518 | if (caught_exception) { |
| 1519 | return; |
| 1520 | } |
| 1521 | |
| 1522 | // Process debug event |
| 1523 | ProcessDebugEvent(v8::BeforeCompile, event_data); |
| 1524 | } |
| 1525 | |
| 1526 | |
| 1527 | // Handle debugger actions when a new script is compiled. |
| 1528 | void Debugger::OnAfterCompile(Handle<Script> script, Handle<JSFunction> fun) { |
kasper.lund | 212ac23 | 2008-07-16 07:07:30 +0000 | [diff] [blame] | 1529 | HandleScope scope; |
| 1530 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1531 | // No compile events while compiling natives. |
| 1532 | if (compiling_natives()) return; |
| 1533 | |
| 1534 | // No more to do if not debugging. |
| 1535 | if (!debugger_active()) return; |
| 1536 | |
kasper.lund | 212ac23 | 2008-07-16 07:07:30 +0000 | [diff] [blame] | 1537 | // Enter the debugger. Bail out if the debugger cannot be loaded. |
| 1538 | if (!Debug::Load()) return; |
| 1539 | SaveBreakFrame save; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1540 | EnterDebuggerContext enter; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1541 | |
| 1542 | // If debugging there might be script break points registered for this |
| 1543 | // script. Make sure that these break points are set. |
| 1544 | |
| 1545 | // Get the function UpdateScriptBreakPoints (defined in debug-delay.js). |
| 1546 | Handle<Object> update_script_break_points = |
| 1547 | Handle<Object>(Debug::debug_context()->global()->GetProperty( |
| 1548 | *Factory::LookupAsciiSymbol("UpdateScriptBreakPoints"))); |
| 1549 | if (!update_script_break_points->IsJSFunction()) { |
| 1550 | return; |
| 1551 | } |
| 1552 | ASSERT(update_script_break_points->IsJSFunction()); |
| 1553 | |
| 1554 | // Wrap the script object in a proper JS object before passing it |
| 1555 | // to JavaScript. |
| 1556 | Handle<JSValue> wrapper = GetScriptWrapper(script); |
| 1557 | |
| 1558 | // Call UpdateScriptBreakPoints expect no exceptions. |
kasper.lund | 212ac23 | 2008-07-16 07:07:30 +0000 | [diff] [blame] | 1559 | bool caught_exception = false; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1560 | const int argc = 1; |
| 1561 | Object** argv[argc] = { reinterpret_cast<Object**>(wrapper.location()) }; |
| 1562 | Handle<Object> result = Execution::TryCall( |
| 1563 | Handle<JSFunction>::cast(update_script_break_points), |
| 1564 | Top::builtins(), argc, argv, |
| 1565 | &caught_exception); |
| 1566 | if (caught_exception) { |
| 1567 | return; |
| 1568 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1569 | // Bail out based on state or if there is no listener for this event |
| 1570 | if (Debug::InDebugger()) return; |
| 1571 | if (!Debugger::EventActive(v8::AfterCompile)) return; |
| 1572 | |
| 1573 | // Create the compile state object. |
| 1574 | Handle<Object> event_data = MakeCompileEvent(script, |
| 1575 | Factory::undefined_value(), |
| 1576 | &caught_exception); |
| 1577 | // Bail out and don't call debugger if exception. |
| 1578 | if (caught_exception) { |
| 1579 | return; |
| 1580 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1581 | // Process debug event |
| 1582 | ProcessDebugEvent(v8::AfterCompile, event_data); |
| 1583 | } |
| 1584 | |
| 1585 | |
| 1586 | void Debugger::OnNewFunction(Handle<JSFunction> function) { |
| 1587 | return; |
| 1588 | HandleScope scope; |
| 1589 | |
| 1590 | // Bail out based on state or if there is no listener for this event |
| 1591 | if (Debug::InDebugger()) return; |
| 1592 | if (compiling_natives()) return; |
| 1593 | if (!Debugger::EventActive(v8::NewFunction)) return; |
| 1594 | |
kasper.lund | 212ac23 | 2008-07-16 07:07:30 +0000 | [diff] [blame] | 1595 | // Enter the debugger. Bail out if the debugger cannot be loaded. |
| 1596 | if (!Debug::Load()) return; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1597 | SaveBreakFrame save; |
| 1598 | EnterDebuggerContext enter; |
| 1599 | |
| 1600 | // Create the event object. |
| 1601 | bool caught_exception = false; |
| 1602 | Handle<Object> event_data = MakeNewFunctionEvent(function, &caught_exception); |
| 1603 | // Bail out and don't call debugger if exception. |
| 1604 | if (caught_exception) { |
| 1605 | return; |
| 1606 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1607 | // Process debug event. |
| 1608 | ProcessDebugEvent(v8::NewFunction, event_data); |
| 1609 | } |
| 1610 | |
| 1611 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1612 | void Debugger::ProcessDebugEvent(v8::DebugEvent event, |
| 1613 | Handle<Object> event_data) { |
| 1614 | // Create the execution state. |
| 1615 | bool caught_exception = false; |
| 1616 | Handle<Object> exec_state = MakeExecutionState(&caught_exception); |
| 1617 | if (caught_exception) { |
| 1618 | return; |
| 1619 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1620 | // First notify the builtin debugger. |
| 1621 | if (message_thread_ != NULL) { |
| 1622 | message_thread_->DebugEvent(event, exec_state, event_data); |
| 1623 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1624 | // Notify registered debug event listeners. The list can contain both C and |
| 1625 | // JavaScript functions. |
| 1626 | v8::NeanderArray listeners(Factory::debug_event_listeners()); |
| 1627 | int length = listeners.length(); |
| 1628 | for (int i = 0; i < length; i++) { |
| 1629 | if (listeners.get(i)->IsUndefined()) continue; // Skip deleted ones. |
| 1630 | v8::NeanderObject listener(JSObject::cast(listeners.get(i))); |
| 1631 | Handle<Object> callback_data(listener.get(1)); |
| 1632 | if (listener.get(0)->IsProxy()) { |
| 1633 | // C debug event listener. |
| 1634 | Handle<Proxy> callback_obj(Proxy::cast(listener.get(0))); |
| 1635 | v8::DebugEventCallback callback = |
| 1636 | FUNCTION_CAST<v8::DebugEventCallback>(callback_obj->proxy()); |
| 1637 | callback(event, |
| 1638 | v8::Utils::ToLocal(Handle<JSObject>::cast(exec_state)), |
| 1639 | v8::Utils::ToLocal(Handle<JSObject>::cast(event_data)), |
| 1640 | v8::Utils::ToLocal(callback_data)); |
| 1641 | } else { |
| 1642 | // JavaScript debug event listener. |
| 1643 | ASSERT(listener.get(0)->IsJSFunction()); |
| 1644 | Handle<JSFunction> fun(JSFunction::cast(listener.get(0))); |
| 1645 | |
| 1646 | // Invoke the JavaScript debug event listener. |
| 1647 | const int argc = 4; |
| 1648 | Object** argv[argc] = { Handle<Object>(Smi::FromInt(event)).location(), |
| 1649 | exec_state.location(), |
| 1650 | event_data.location(), |
| 1651 | callback_data.location() }; |
| 1652 | Handle<Object> result = Execution::TryCall(fun, Top::global(), |
| 1653 | argc, argv, &caught_exception); |
| 1654 | if (caught_exception) { |
| 1655 | // Silently ignore exceptions from debug event listeners. |
| 1656 | } |
| 1657 | } |
| 1658 | } |
| 1659 | } |
| 1660 | |
| 1661 | |
| 1662 | void Debugger::SetMessageHandler(v8::DebugMessageHandler handler, void* data) { |
| 1663 | debug_message_handler_ = handler; |
| 1664 | debug_message_handler_data_ = data; |
| 1665 | if (!message_thread_) { |
| 1666 | message_thread_ = new DebugMessageThread(); |
| 1667 | message_thread_->Start(); |
| 1668 | } |
| 1669 | UpdateActiveDebugger(); |
| 1670 | } |
| 1671 | |
| 1672 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 1673 | // Posts an output message from the debugger to the debug_message_handler |
| 1674 | // callback. This callback is part of the public API. Messages are |
| 1675 | // kept internally as Vector<uint16_t> strings, which are allocated in various |
| 1676 | // places and deallocated by the calling function sometime after this call. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1677 | void Debugger::SendMessage(Vector< uint16_t> message) { |
| 1678 | if (debug_message_handler_ != NULL) { |
| 1679 | debug_message_handler_(message.start(), message.length(), |
| 1680 | debug_message_handler_data_); |
| 1681 | } |
| 1682 | } |
| 1683 | |
| 1684 | |
| 1685 | void Debugger::ProcessCommand(Vector<const uint16_t> command) { |
| 1686 | if (message_thread_ != NULL) { |
| 1687 | message_thread_->ProcessCommand( |
| 1688 | Vector<uint16_t>(const_cast<uint16_t *>(command.start()), |
| 1689 | command.length())); |
| 1690 | } |
| 1691 | } |
| 1692 | |
| 1693 | |
| 1694 | void Debugger::UpdateActiveDebugger() { |
| 1695 | v8::NeanderArray listeners(Factory::debug_event_listeners()); |
| 1696 | int length = listeners.length(); |
| 1697 | bool active_listener = false; |
| 1698 | for (int i = 0; i < length && !active_listener; i++) { |
| 1699 | active_listener = !listeners.get(i)->IsUndefined(); |
| 1700 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1701 | set_debugger_active((Debugger::message_thread_ != NULL && |
| 1702 | Debugger::debug_message_handler_ != NULL) || |
| 1703 | active_listener); |
| 1704 | if (!debugger_active() && message_thread_) |
| 1705 | message_thread_->OnDebuggerInactive(); |
| 1706 | } |
| 1707 | |
| 1708 | |
| 1709 | DebugMessageThread::DebugMessageThread() |
| 1710 | : host_running_(true), |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 1711 | command_queue_(kQueueInitialSize), |
| 1712 | message_queue_(kQueueInitialSize) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1713 | command_received_ = OS::CreateSemaphore(0); |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 1714 | message_received_ = OS::CreateSemaphore(0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1715 | } |
| 1716 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 1717 | // Does not free resources held by DebugMessageThread |
| 1718 | // because this cannot be done thread-safely. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1719 | DebugMessageThread::~DebugMessageThread() { |
| 1720 | } |
| 1721 | |
| 1722 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 1723 | // Puts an event coming from V8 on the queue. Creates |
| 1724 | // a copy of the JSON formatted event string managed by the V8. |
| 1725 | // Called by the V8 thread. |
| 1726 | // The new copy of the event string is destroyed in Run(). |
| 1727 | void DebugMessageThread::SendMessage(Vector<uint16_t> message) { |
| 1728 | Vector<uint16_t> message_copy = message.Clone(); |
mads.s.ager@gmail.com | 769cc96 | 2008-08-06 10:02:49 +0000 | [diff] [blame] | 1729 | Logger::DebugTag("Put message on event message_queue."); |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 1730 | message_queue_.Put(message_copy); |
| 1731 | message_received_->Signal(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1732 | } |
| 1733 | |
| 1734 | |
| 1735 | void DebugMessageThread::SetEventJSONFromEvent(Handle<Object> event_data) { |
| 1736 | v8::HandleScope scope; |
| 1737 | // Call toJSONProtocol on the debug event object. |
| 1738 | v8::Local<v8::Object> api_event_data = |
| 1739 | v8::Utils::ToLocal(Handle<JSObject>::cast(event_data)); |
| 1740 | v8::Local<v8::String> fun_name = v8::String::New("toJSONProtocol"); |
| 1741 | v8::Local<v8::Function> fun = |
| 1742 | v8::Function::Cast(*api_event_data->Get(fun_name)); |
| 1743 | v8::TryCatch try_catch; |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 1744 | v8::Local<v8::Value> json_event = *fun->Call(api_event_data, 0, NULL); |
| 1745 | v8::Local<v8::String> json_event_string; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1746 | if (!try_catch.HasCaught()) { |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 1747 | if (!json_event->IsUndefined()) { |
| 1748 | json_event_string = json_event->ToString(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1749 | if (FLAG_trace_debug_json) { |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 1750 | PrintLn(json_event_string); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1751 | } |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 1752 | v8::String::Value val(json_event_string); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1753 | Vector<uint16_t> str(reinterpret_cast<uint16_t*>(*val), |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 1754 | json_event_string->Length()); |
| 1755 | SendMessage(str); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1756 | } else { |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 1757 | SendMessage(Vector<uint16_t>::empty()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1758 | } |
| 1759 | } else { |
| 1760 | PrintLn(try_catch.Exception()); |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 1761 | SendMessage(Vector<uint16_t>::empty()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1762 | } |
| 1763 | } |
| 1764 | |
| 1765 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1766 | void DebugMessageThread::Run() { |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 1767 | // Sends debug events to an installed debugger message callback. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1768 | while (true) { |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 1769 | // Wait and Get are paired so that semaphore count equals queue length. |
| 1770 | message_received_->Wait(); |
mads.s.ager@gmail.com | 769cc96 | 2008-08-06 10:02:49 +0000 | [diff] [blame] | 1771 | Logger::DebugTag("Get message from event message_queue."); |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 1772 | Vector<uint16_t> message = message_queue_.Get(); |
| 1773 | if (message.length() > 0) { |
| 1774 | Debugger::SendMessage(message); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1775 | } |
| 1776 | } |
| 1777 | } |
| 1778 | |
| 1779 | |
kasper.lund | 4451067 | 2008-07-25 07:37:58 +0000 | [diff] [blame] | 1780 | // This method is called by the V8 thread whenever a debug event occurs in |
| 1781 | // the VM. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1782 | void DebugMessageThread::DebugEvent(v8::DebugEvent event, |
| 1783 | Handle<Object> exec_state, |
| 1784 | Handle<Object> event_data) { |
kasper.lund | 212ac23 | 2008-07-16 07:07:30 +0000 | [diff] [blame] | 1785 | if (!Debug::Load()) return; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1786 | |
| 1787 | // Process the individual events. |
| 1788 | bool interactive = false; |
| 1789 | switch (event) { |
| 1790 | case v8::Break: |
| 1791 | interactive = true; // Break event is always interavtive |
| 1792 | break; |
| 1793 | case v8::Exception: |
| 1794 | interactive = true; // Exception event is always interavtive |
| 1795 | break; |
| 1796 | case v8::BeforeCompile: |
| 1797 | break; |
| 1798 | case v8::AfterCompile: |
| 1799 | break; |
| 1800 | case v8::NewFunction: |
| 1801 | break; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1802 | default: |
| 1803 | UNREACHABLE(); |
| 1804 | } |
| 1805 | |
| 1806 | // Done if not interactive. |
| 1807 | if (!interactive) return; |
| 1808 | |
| 1809 | // Get the DebugCommandProcessor. |
| 1810 | v8::Local<v8::Object> api_exec_state = |
| 1811 | v8::Utils::ToLocal(Handle<JSObject>::cast(exec_state)); |
| 1812 | v8::Local<v8::String> fun_name = |
| 1813 | v8::String::New("debugCommandProcessor"); |
| 1814 | v8::Local<v8::Function> fun = |
| 1815 | v8::Function::Cast(*api_exec_state->Get(fun_name)); |
| 1816 | v8::TryCatch try_catch; |
| 1817 | v8::Local<v8::Object> cmd_processor = |
| 1818 | v8::Object::Cast(*fun->Call(api_exec_state, 0, NULL)); |
| 1819 | if (try_catch.HasCaught()) { |
| 1820 | PrintLn(try_catch.Exception()); |
| 1821 | return; |
| 1822 | } |
| 1823 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 1824 | // Notify the debugger that a debug event has occoured. |
| 1825 | host_running_ = false; |
| 1826 | SetEventJSONFromEvent(event_data); |
| 1827 | |
| 1828 | // Wait for commands from the debugger. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1829 | while (true) { |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 1830 | command_received_->Wait(); |
mads.s.ager@gmail.com | 769cc96 | 2008-08-06 10:02:49 +0000 | [diff] [blame] | 1831 | Logger::DebugTag("Get command from command queue, in interactive loop."); |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 1832 | Vector<uint16_t> command = command_queue_.Get(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1833 | ASSERT(!host_running_); |
| 1834 | if (!Debugger::debugger_active()) { |
| 1835 | host_running_ = true; |
| 1836 | return; |
| 1837 | } |
| 1838 | |
| 1839 | // Invoke the JavaScript to convert the debug command line to a JSON |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 1840 | // request, invoke the JSON request and convert the JSON response to a text |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1841 | // representation. |
| 1842 | v8::Local<v8::String> fun_name; |
| 1843 | v8::Local<v8::Function> fun; |
| 1844 | v8::Local<v8::Value> args[1]; |
| 1845 | v8::TryCatch try_catch; |
| 1846 | fun_name = v8::String::New("processDebugCommand"); |
| 1847 | fun = v8::Function::Cast(*cmd_processor->Get(fun_name)); |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 1848 | args[0] = v8::String::New(reinterpret_cast<uint16_t*>(command.start()), |
| 1849 | command.length()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1850 | v8::Local<v8::Value> result_val = fun->Call(cmd_processor, 1, args); |
| 1851 | |
| 1852 | // Get the result of the command. |
| 1853 | v8::Local<v8::String> result_string; |
| 1854 | bool running = false; |
| 1855 | if (!try_catch.HasCaught()) { |
| 1856 | // Get the result as an object. |
| 1857 | v8::Local<v8::Object> result = v8::Object::Cast(*result_val); |
| 1858 | |
| 1859 | // Log the JSON request/response. |
| 1860 | if (FLAG_trace_debug_json) { |
| 1861 | PrintLn(result->Get(v8::String::New("request"))); |
| 1862 | PrintLn(result->Get(v8::String::New("response"))); |
| 1863 | } |
| 1864 | |
| 1865 | // Get the running state. |
| 1866 | running = result->Get(v8::String::New("running"))->ToBoolean()->Value(); |
| 1867 | |
| 1868 | // Get result text. |
| 1869 | v8::Local<v8::Value> text_result = |
| 1870 | result->Get(v8::String::New("response")); |
| 1871 | if (!text_result->IsUndefined()) { |
| 1872 | result_string = text_result->ToString(); |
| 1873 | } else { |
| 1874 | result_string = v8::String::New(""); |
| 1875 | } |
| 1876 | } else { |
| 1877 | // In case of failure the result text is the exception text. |
| 1878 | result_string = try_catch.Exception()->ToString(); |
| 1879 | } |
| 1880 | |
| 1881 | // Convert text result to C string. |
| 1882 | v8::String::Value val(result_string); |
| 1883 | Vector<uint16_t> str(reinterpret_cast<uint16_t*>(*val), |
| 1884 | result_string->Length()); |
| 1885 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 1886 | // Set host_running_ correctly for nested debugger evaluations. |
| 1887 | host_running_ = running; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1888 | |
| 1889 | // Return the result. |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 1890 | SendMessage(str); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1891 | |
| 1892 | // Return from debug event processing is VM should be running. |
| 1893 | if (running) { |
| 1894 | return; |
| 1895 | } |
| 1896 | } |
| 1897 | } |
| 1898 | |
| 1899 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 1900 | // Puts a command coming from the public API on the queue. Creates |
| 1901 | // a copy of the command string managed by the debugger. Up to this |
| 1902 | // point, the command data was managed by the API client. Called |
| 1903 | // by the API client thread. This is where the API client hands off |
| 1904 | // processing of the command to the DebugMessageThread thread. |
| 1905 | // The new copy of the command is destroyed in HandleCommand(). |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1906 | void DebugMessageThread::ProcessCommand(Vector<uint16_t> command) { |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 1907 | Vector<uint16_t> command_copy = command.Clone(); |
mads.s.ager@gmail.com | 769cc96 | 2008-08-06 10:02:49 +0000 | [diff] [blame] | 1908 | Logger::DebugTag("Put command on command_queue."); |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 1909 | command_queue_.Put(command_copy); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1910 | command_received_->Signal(); |
| 1911 | } |
| 1912 | |
| 1913 | |
| 1914 | void DebugMessageThread::OnDebuggerInactive() { |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 1915 | // Send an empty command to the debugger if in a break to make JavaScript run |
| 1916 | // again if the debugger is closed. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1917 | if (!host_running_) { |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 1918 | ProcessCommand(Vector<uint16_t>::empty()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1919 | } |
| 1920 | } |
| 1921 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 1922 | |
| 1923 | MessageQueue::MessageQueue(int size) : start_(0), end_(0), size_(size) { |
| 1924 | messages_ = NewArray<Vector<uint16_t> >(size); |
| 1925 | } |
| 1926 | |
| 1927 | |
| 1928 | MessageQueue::~MessageQueue() { |
| 1929 | DeleteArray(messages_); |
| 1930 | } |
| 1931 | |
| 1932 | |
| 1933 | Vector<uint16_t> MessageQueue::Get() { |
| 1934 | ASSERT(!IsEmpty()); |
| 1935 | int result = start_; |
| 1936 | start_ = (start_ + 1) % size_; |
| 1937 | return messages_[result]; |
| 1938 | } |
| 1939 | |
| 1940 | |
| 1941 | void MessageQueue::Put(const Vector<uint16_t>& message) { |
| 1942 | if ((end_ + 1) % size_ == start_) { |
| 1943 | Expand(); |
| 1944 | } |
| 1945 | messages_[end_] = message; |
| 1946 | end_ = (end_ + 1) % size_; |
| 1947 | } |
| 1948 | |
| 1949 | |
| 1950 | void MessageQueue::Expand() { |
| 1951 | MessageQueue new_queue(size_ * 2); |
| 1952 | while (!IsEmpty()) { |
| 1953 | new_queue.Put(Get()); |
| 1954 | } |
| 1955 | Vector<uint16_t>* array_to_free = messages_; |
| 1956 | *this = new_queue; |
| 1957 | new_queue.messages_ = array_to_free; |
| 1958 | // Automatic destructor called on new_queue, freeing array_to_free. |
| 1959 | } |
| 1960 | |
| 1961 | |
| 1962 | LockingMessageQueue::LockingMessageQueue(int size) : queue_(size) { |
| 1963 | lock_ = OS::CreateMutex(); |
| 1964 | } |
| 1965 | |
| 1966 | |
| 1967 | LockingMessageQueue::~LockingMessageQueue() { |
| 1968 | delete lock_; |
| 1969 | } |
| 1970 | |
| 1971 | |
| 1972 | bool LockingMessageQueue::IsEmpty() const { |
| 1973 | ScopedLock sl(lock_); |
| 1974 | return queue_.IsEmpty(); |
| 1975 | } |
| 1976 | |
| 1977 | |
| 1978 | Vector<uint16_t> LockingMessageQueue::Get() { |
| 1979 | ScopedLock sl(lock_); |
| 1980 | Vector<uint16_t> result = queue_.Get(); |
mads.s.ager@gmail.com | 769cc96 | 2008-08-06 10:02:49 +0000 | [diff] [blame] | 1981 | Logger::DebugEvent("Get", result); |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 1982 | return result; |
| 1983 | } |
| 1984 | |
| 1985 | |
| 1986 | void LockingMessageQueue::Put(const Vector<uint16_t>& message) { |
| 1987 | ScopedLock sl(lock_); |
| 1988 | queue_.Put(message); |
mads.s.ager@gmail.com | 769cc96 | 2008-08-06 10:02:49 +0000 | [diff] [blame] | 1989 | Logger::DebugEvent("Put", message); |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 1990 | } |
| 1991 | |
| 1992 | |
| 1993 | void LockingMessageQueue::Clear() { |
| 1994 | ScopedLock sl(lock_); |
| 1995 | queue_.Clear(); |
| 1996 | } |
| 1997 | |
| 1998 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1999 | } } // namespace v8::internal |