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