Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1 | // Copyright 2006-2008 the V8 project authors. All rights reserved. |
| 2 | // Redistribution and use in source and binary forms, with or without |
| 3 | // modification, are permitted provided that the following conditions are |
| 4 | // met: |
| 5 | // |
| 6 | // * Redistributions of source code must retain the above copyright |
| 7 | // notice, this list of conditions and the following disclaimer. |
| 8 | // * Redistributions in binary form must reproduce the above |
| 9 | // copyright notice, this list of conditions and the following |
| 10 | // disclaimer in the documentation and/or other materials provided |
| 11 | // with the distribution. |
| 12 | // * Neither the name of Google Inc. nor the names of its |
| 13 | // contributors may be used to endorse or promote products derived |
| 14 | // from this software without specific prior written permission. |
| 15 | // |
| 16 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | |
| 28 | #include "v8.h" |
| 29 | |
| 30 | #include "api.h" |
| 31 | #include "arguments.h" |
| 32 | #include "bootstrapper.h" |
| 33 | #include "code-stubs.h" |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 34 | #include "codegen.h" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 35 | #include "compilation-cache.h" |
| 36 | #include "compiler.h" |
| 37 | #include "debug.h" |
| 38 | #include "execution.h" |
| 39 | #include "global-handles.h" |
| 40 | #include "ic.h" |
| 41 | #include "ic-inl.h" |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 42 | #include "messages.h" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 43 | #include "natives.h" |
| 44 | #include "stub-cache.h" |
| 45 | #include "log.h" |
| 46 | |
| 47 | #include "../include/v8-debug.h" |
| 48 | |
| 49 | namespace v8 { |
| 50 | namespace internal { |
| 51 | |
| 52 | #ifdef ENABLE_DEBUGGER_SUPPORT |
| 53 | static void PrintLn(v8::Local<v8::Value> value) { |
| 54 | v8::Local<v8::String> s = value->ToString(); |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 55 | ScopedVector<char> data(s->Length() + 1); |
| 56 | if (data.start() == NULL) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 57 | V8::FatalProcessOutOfMemory("PrintLn"); |
| 58 | return; |
| 59 | } |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 60 | s->WriteAscii(data.start()); |
| 61 | PrintF("%s\n", data.start()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 65 | static Handle<Code> ComputeCallDebugBreak(int argc, Code::Kind kind) { |
| 66 | CALL_HEAP_FUNCTION(StubCache::ComputeCallDebugBreak(argc, kind), Code); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 70 | static Handle<Code> ComputeCallDebugPrepareStepIn(int argc, Code::Kind kind) { |
| 71 | CALL_HEAP_FUNCTION( |
| 72 | StubCache::ComputeCallDebugPrepareStepIn(argc, kind), Code); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 76 | static v8::Handle<v8::Context> GetDebugEventContext() { |
| 77 | Handle<Context> context = Debug::debugger_entry()->GetContext(); |
| 78 | // Top::context() may have been NULL when "script collected" event occured. |
| 79 | if (*context == NULL) { |
| 80 | return v8::Local<v8::Context>(); |
| 81 | } |
| 82 | Handle<Context> global_context(context->global_context()); |
| 83 | return v8::Utils::ToLocal(global_context); |
| 84 | } |
| 85 | |
| 86 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 87 | BreakLocationIterator::BreakLocationIterator(Handle<DebugInfo> debug_info, |
| 88 | BreakLocatorType type) { |
| 89 | debug_info_ = debug_info; |
| 90 | type_ = type; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 91 | reloc_iterator_ = NULL; |
| 92 | reloc_iterator_original_ = NULL; |
| 93 | Reset(); // Initialize the rest of the member variables. |
| 94 | } |
| 95 | |
| 96 | |
| 97 | BreakLocationIterator::~BreakLocationIterator() { |
| 98 | ASSERT(reloc_iterator_ != NULL); |
| 99 | ASSERT(reloc_iterator_original_ != NULL); |
| 100 | delete reloc_iterator_; |
| 101 | delete reloc_iterator_original_; |
| 102 | } |
| 103 | |
| 104 | |
| 105 | void BreakLocationIterator::Next() { |
| 106 | AssertNoAllocation nogc; |
| 107 | ASSERT(!RinfoDone()); |
| 108 | |
| 109 | // Iterate through reloc info for code and original code stopping at each |
| 110 | // breakable code target. |
| 111 | bool first = break_point_ == -1; |
| 112 | while (!RinfoDone()) { |
| 113 | if (!first) RinfoNext(); |
| 114 | first = false; |
| 115 | if (RinfoDone()) return; |
| 116 | |
| 117 | // Whenever a statement position or (plain) position is passed update the |
| 118 | // current value of these. |
| 119 | if (RelocInfo::IsPosition(rmode())) { |
| 120 | if (RelocInfo::IsStatementPosition(rmode())) { |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 121 | statement_position_ = static_cast<int>( |
| 122 | rinfo()->data() - debug_info_->shared()->start_position()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 123 | } |
| 124 | // Always update the position as we don't want that to be before the |
| 125 | // statement position. |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 126 | position_ = static_cast<int>( |
| 127 | rinfo()->data() - debug_info_->shared()->start_position()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 128 | ASSERT(position_ >= 0); |
| 129 | ASSERT(statement_position_ >= 0); |
| 130 | } |
| 131 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 132 | if (IsDebugBreakSlot()) { |
| 133 | // There is always a possible break point at a debug break slot. |
| 134 | break_point_++; |
| 135 | return; |
| 136 | } else if (RelocInfo::IsCodeTarget(rmode())) { |
| 137 | // Check for breakable code target. Look in the original code as setting |
| 138 | // break points can cause the code targets in the running (debugged) code |
| 139 | // to be of a different kind than in the original code. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 140 | Address target = original_rinfo()->target_address(); |
| 141 | Code* code = Code::GetCodeFromTargetAddress(target); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 142 | if ((code->is_inline_cache_stub() && |
| 143 | code->kind() != Code::BINARY_OP_IC) || |
| 144 | RelocInfo::IsConstructCall(rmode())) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 145 | break_point_++; |
| 146 | return; |
| 147 | } |
| 148 | if (code->kind() == Code::STUB) { |
| 149 | if (IsDebuggerStatement()) { |
| 150 | break_point_++; |
| 151 | return; |
| 152 | } |
| 153 | if (type_ == ALL_BREAK_LOCATIONS) { |
| 154 | if (Debug::IsBreakStub(code)) { |
| 155 | break_point_++; |
| 156 | return; |
| 157 | } |
| 158 | } else { |
| 159 | ASSERT(type_ == SOURCE_BREAK_LOCATIONS); |
| 160 | if (Debug::IsSourceBreakStub(code)) { |
| 161 | break_point_++; |
| 162 | return; |
| 163 | } |
| 164 | } |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | // Check for break at return. |
| 169 | if (RelocInfo::IsJSReturn(rmode())) { |
| 170 | // Set the positions to the end of the function. |
| 171 | if (debug_info_->shared()->HasSourceCode()) { |
| 172 | position_ = debug_info_->shared()->end_position() - |
Ben Murdoch | bb769b2 | 2010-08-11 14:56:33 +0100 | [diff] [blame] | 173 | debug_info_->shared()->start_position() - 1; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 174 | } else { |
| 175 | position_ = 0; |
| 176 | } |
| 177 | statement_position_ = position_; |
| 178 | break_point_++; |
| 179 | return; |
| 180 | } |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | |
| 185 | void BreakLocationIterator::Next(int count) { |
| 186 | while (count > 0) { |
| 187 | Next(); |
| 188 | count--; |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | |
| 193 | // Find the break point closest to the supplied address. |
| 194 | void BreakLocationIterator::FindBreakLocationFromAddress(Address pc) { |
| 195 | // Run through all break points to locate the one closest to the address. |
| 196 | int closest_break_point = 0; |
| 197 | int distance = kMaxInt; |
| 198 | while (!Done()) { |
| 199 | // Check if this break point is closer that what was previously found. |
| 200 | if (this->pc() < pc && pc - this->pc() < distance) { |
| 201 | closest_break_point = break_point(); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 202 | distance = static_cast<int>(pc - this->pc()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 203 | // Check whether we can't get any closer. |
| 204 | if (distance == 0) break; |
| 205 | } |
| 206 | Next(); |
| 207 | } |
| 208 | |
| 209 | // Move to the break point found. |
| 210 | Reset(); |
| 211 | Next(closest_break_point); |
| 212 | } |
| 213 | |
| 214 | |
| 215 | // Find the break point closest to the supplied source position. |
| 216 | void BreakLocationIterator::FindBreakLocationFromPosition(int position) { |
| 217 | // Run through all break points to locate the one closest to the source |
| 218 | // position. |
| 219 | int closest_break_point = 0; |
| 220 | int distance = kMaxInt; |
| 221 | while (!Done()) { |
| 222 | // Check if this break point is closer that what was previously found. |
| 223 | if (position <= statement_position() && |
| 224 | statement_position() - position < distance) { |
| 225 | closest_break_point = break_point(); |
| 226 | distance = statement_position() - position; |
| 227 | // Check whether we can't get any closer. |
| 228 | if (distance == 0) break; |
| 229 | } |
| 230 | Next(); |
| 231 | } |
| 232 | |
| 233 | // Move to the break point found. |
| 234 | Reset(); |
| 235 | Next(closest_break_point); |
| 236 | } |
| 237 | |
| 238 | |
| 239 | void BreakLocationIterator::Reset() { |
| 240 | // Create relocation iterators for the two code objects. |
| 241 | if (reloc_iterator_ != NULL) delete reloc_iterator_; |
| 242 | if (reloc_iterator_original_ != NULL) delete reloc_iterator_original_; |
| 243 | reloc_iterator_ = new RelocIterator(debug_info_->code()); |
| 244 | reloc_iterator_original_ = new RelocIterator(debug_info_->original_code()); |
| 245 | |
| 246 | // Position at the first break point. |
| 247 | break_point_ = -1; |
| 248 | position_ = 1; |
| 249 | statement_position_ = 1; |
| 250 | Next(); |
| 251 | } |
| 252 | |
| 253 | |
| 254 | bool BreakLocationIterator::Done() const { |
| 255 | return RinfoDone(); |
| 256 | } |
| 257 | |
| 258 | |
| 259 | void BreakLocationIterator::SetBreakPoint(Handle<Object> break_point_object) { |
| 260 | // If there is not already a real break point here patch code with debug |
| 261 | // break. |
| 262 | if (!HasBreakPoint()) { |
| 263 | SetDebugBreak(); |
| 264 | } |
| 265 | ASSERT(IsDebugBreak() || IsDebuggerStatement()); |
| 266 | // Set the break point information. |
| 267 | DebugInfo::SetBreakPoint(debug_info_, code_position(), |
| 268 | position(), statement_position(), |
| 269 | break_point_object); |
| 270 | } |
| 271 | |
| 272 | |
| 273 | void BreakLocationIterator::ClearBreakPoint(Handle<Object> break_point_object) { |
| 274 | // Clear the break point information. |
| 275 | DebugInfo::ClearBreakPoint(debug_info_, code_position(), break_point_object); |
| 276 | // If there are no more break points here remove the debug break. |
| 277 | if (!HasBreakPoint()) { |
| 278 | ClearDebugBreak(); |
| 279 | ASSERT(!IsDebugBreak()); |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | |
| 284 | void BreakLocationIterator::SetOneShot() { |
| 285 | // Debugger statement always calls debugger. No need to modify it. |
| 286 | if (IsDebuggerStatement()) { |
| 287 | return; |
| 288 | } |
| 289 | |
| 290 | // If there is a real break point here no more to do. |
| 291 | if (HasBreakPoint()) { |
| 292 | ASSERT(IsDebugBreak()); |
| 293 | return; |
| 294 | } |
| 295 | |
| 296 | // Patch code with debug break. |
| 297 | SetDebugBreak(); |
| 298 | } |
| 299 | |
| 300 | |
| 301 | void BreakLocationIterator::ClearOneShot() { |
| 302 | // Debugger statement always calls debugger. No need to modify it. |
| 303 | if (IsDebuggerStatement()) { |
| 304 | return; |
| 305 | } |
| 306 | |
| 307 | // If there is a real break point here no more to do. |
| 308 | if (HasBreakPoint()) { |
| 309 | ASSERT(IsDebugBreak()); |
| 310 | return; |
| 311 | } |
| 312 | |
| 313 | // Patch code removing debug break. |
| 314 | ClearDebugBreak(); |
| 315 | ASSERT(!IsDebugBreak()); |
| 316 | } |
| 317 | |
| 318 | |
| 319 | void BreakLocationIterator::SetDebugBreak() { |
| 320 | // Debugger statement always calls debugger. No need to modify it. |
| 321 | if (IsDebuggerStatement()) { |
| 322 | return; |
| 323 | } |
| 324 | |
| 325 | // If there is already a break point here just return. This might happen if |
| 326 | // the same code is flooded with break points twice. Flooding the same |
| 327 | // function twice might happen when stepping in a function with an exception |
| 328 | // handler as the handler and the function is the same. |
| 329 | if (IsDebugBreak()) { |
| 330 | return; |
| 331 | } |
| 332 | |
| 333 | if (RelocInfo::IsJSReturn(rmode())) { |
| 334 | // Patch the frame exit code with a break point. |
| 335 | SetDebugBreakAtReturn(); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 336 | } else if (IsDebugBreakSlot()) { |
| 337 | // Patch the code in the break slot. |
| 338 | SetDebugBreakAtSlot(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 339 | } else { |
| 340 | // Patch the IC call. |
| 341 | SetDebugBreakAtIC(); |
| 342 | } |
| 343 | ASSERT(IsDebugBreak()); |
| 344 | } |
| 345 | |
| 346 | |
| 347 | void BreakLocationIterator::ClearDebugBreak() { |
| 348 | // Debugger statement always calls debugger. No need to modify it. |
| 349 | if (IsDebuggerStatement()) { |
| 350 | return; |
| 351 | } |
| 352 | |
| 353 | if (RelocInfo::IsJSReturn(rmode())) { |
| 354 | // Restore the frame exit code. |
| 355 | ClearDebugBreakAtReturn(); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 356 | } else if (IsDebugBreakSlot()) { |
| 357 | // Restore the code in the break slot. |
| 358 | ClearDebugBreakAtSlot(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 359 | } else { |
| 360 | // Patch the IC call. |
| 361 | ClearDebugBreakAtIC(); |
| 362 | } |
| 363 | ASSERT(!IsDebugBreak()); |
| 364 | } |
| 365 | |
| 366 | |
| 367 | void BreakLocationIterator::PrepareStepIn() { |
| 368 | HandleScope scope; |
| 369 | |
| 370 | // Step in can only be prepared if currently positioned on an IC call, |
| 371 | // construct call or CallFunction stub call. |
| 372 | Address target = rinfo()->target_address(); |
| 373 | Handle<Code> code(Code::GetCodeFromTargetAddress(target)); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 374 | if (code->is_call_stub() || code->is_keyed_call_stub()) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 375 | // Step in through IC call is handled by the runtime system. Therefore make |
| 376 | // sure that the any current IC is cleared and the runtime system is |
| 377 | // called. If the executing code has a debug break at the location change |
| 378 | // the call in the original code as it is the code there that will be |
| 379 | // executed in place of the debug break call. |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 380 | Handle<Code> stub = ComputeCallDebugPrepareStepIn(code->arguments_count(), |
| 381 | code->kind()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 382 | if (IsDebugBreak()) { |
| 383 | original_rinfo()->set_target_address(stub->entry()); |
| 384 | } else { |
| 385 | rinfo()->set_target_address(stub->entry()); |
| 386 | } |
| 387 | } else { |
| 388 | #ifdef DEBUG |
| 389 | // All the following stuff is needed only for assertion checks so the code |
| 390 | // is wrapped in ifdef. |
| 391 | Handle<Code> maybe_call_function_stub = code; |
| 392 | if (IsDebugBreak()) { |
| 393 | Address original_target = original_rinfo()->target_address(); |
| 394 | maybe_call_function_stub = |
| 395 | Handle<Code>(Code::GetCodeFromTargetAddress(original_target)); |
| 396 | } |
| 397 | bool is_call_function_stub = |
| 398 | (maybe_call_function_stub->kind() == Code::STUB && |
| 399 | maybe_call_function_stub->major_key() == CodeStub::CallFunction); |
| 400 | |
| 401 | // Step in through construct call requires no changes to the running code. |
| 402 | // Step in through getters/setters should already be prepared as well |
| 403 | // because caller of this function (Debug::PrepareStep) is expected to |
| 404 | // flood the top frame's function with one shot breakpoints. |
| 405 | // Step in through CallFunction stub should also be prepared by caller of |
| 406 | // this function (Debug::PrepareStep) which should flood target function |
| 407 | // with breakpoints. |
| 408 | ASSERT(RelocInfo::IsConstructCall(rmode()) || code->is_inline_cache_stub() |
| 409 | || is_call_function_stub); |
| 410 | #endif |
| 411 | } |
| 412 | } |
| 413 | |
| 414 | |
| 415 | // Check whether the break point is at a position which will exit the function. |
| 416 | bool BreakLocationIterator::IsExit() const { |
| 417 | return (RelocInfo::IsJSReturn(rmode())); |
| 418 | } |
| 419 | |
| 420 | |
| 421 | bool BreakLocationIterator::HasBreakPoint() { |
| 422 | return debug_info_->HasBreakPoint(code_position()); |
| 423 | } |
| 424 | |
| 425 | |
| 426 | // Check whether there is a debug break at the current position. |
| 427 | bool BreakLocationIterator::IsDebugBreak() { |
| 428 | if (RelocInfo::IsJSReturn(rmode())) { |
| 429 | return IsDebugBreakAtReturn(); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 430 | } else if (IsDebugBreakSlot()) { |
| 431 | return IsDebugBreakAtSlot(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 432 | } else { |
| 433 | return Debug::IsDebugBreak(rinfo()->target_address()); |
| 434 | } |
| 435 | } |
| 436 | |
| 437 | |
| 438 | void BreakLocationIterator::SetDebugBreakAtIC() { |
| 439 | // Patch the original code with the current address as the current address |
| 440 | // might have changed by the inline caching since the code was copied. |
| 441 | original_rinfo()->set_target_address(rinfo()->target_address()); |
| 442 | |
| 443 | RelocInfo::Mode mode = rmode(); |
| 444 | if (RelocInfo::IsCodeTarget(mode)) { |
| 445 | Address target = rinfo()->target_address(); |
| 446 | Handle<Code> code(Code::GetCodeFromTargetAddress(target)); |
| 447 | |
| 448 | // Patch the code to invoke the builtin debug break function matching the |
| 449 | // calling convention used by the call site. |
| 450 | Handle<Code> dbgbrk_code(Debug::FindDebugBreak(code, mode)); |
| 451 | rinfo()->set_target_address(dbgbrk_code->entry()); |
| 452 | |
| 453 | // For stubs that refer back to an inlined version clear the cached map for |
| 454 | // the inlined case to always go through the IC. As long as the break point |
| 455 | // is set the patching performed by the runtime system will take place in |
| 456 | // the code copy and will therefore have no effect on the running code |
| 457 | // keeping it from using the inlined code. |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 458 | if (code->is_keyed_load_stub()) { |
| 459 | KeyedLoadIC::ClearInlinedVersion(pc()); |
| 460 | } else if (code->is_keyed_store_stub()) { |
| 461 | KeyedStoreIC::ClearInlinedVersion(pc()); |
| 462 | } else if (code->is_load_stub()) { |
| 463 | LoadIC::ClearInlinedVersion(pc()); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 464 | } else if (code->is_store_stub()) { |
| 465 | StoreIC::ClearInlinedVersion(pc()); |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 466 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 467 | } |
| 468 | } |
| 469 | |
| 470 | |
| 471 | void BreakLocationIterator::ClearDebugBreakAtIC() { |
| 472 | // Patch the code to the original invoke. |
| 473 | rinfo()->set_target_address(original_rinfo()->target_address()); |
| 474 | |
| 475 | RelocInfo::Mode mode = rmode(); |
| 476 | if (RelocInfo::IsCodeTarget(mode)) { |
Leon Clarke | ac95265 | 2010-07-15 11:15:24 +0100 | [diff] [blame] | 477 | AssertNoAllocation nogc; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 478 | Address target = original_rinfo()->target_address(); |
Leon Clarke | ac95265 | 2010-07-15 11:15:24 +0100 | [diff] [blame] | 479 | Code* code = Code::GetCodeFromTargetAddress(target); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 480 | |
| 481 | // Restore the inlined version of keyed stores to get back to the |
| 482 | // fast case. We need to patch back the keyed store because no |
| 483 | // patching happens when running normally. For keyed loads, the |
| 484 | // map check will get patched back when running normally after ICs |
| 485 | // have been cleared at GC. |
| 486 | if (code->is_keyed_store_stub()) KeyedStoreIC::RestoreInlinedVersion(pc()); |
| 487 | } |
| 488 | } |
| 489 | |
| 490 | |
| 491 | bool BreakLocationIterator::IsDebuggerStatement() { |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 492 | return RelocInfo::DEBUG_BREAK == rmode(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 493 | } |
| 494 | |
| 495 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 496 | bool BreakLocationIterator::IsDebugBreakSlot() { |
| 497 | return RelocInfo::DEBUG_BREAK_SLOT == rmode(); |
| 498 | } |
| 499 | |
| 500 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 501 | Object* BreakLocationIterator::BreakPointObjects() { |
| 502 | return debug_info_->GetBreakPointObjects(code_position()); |
| 503 | } |
| 504 | |
| 505 | |
| 506 | // Clear out all the debug break code. This is ONLY supposed to be used when |
| 507 | // shutting down the debugger as it will leave the break point information in |
| 508 | // DebugInfo even though the code is patched back to the non break point state. |
| 509 | void BreakLocationIterator::ClearAllDebugBreak() { |
| 510 | while (!Done()) { |
| 511 | ClearDebugBreak(); |
| 512 | Next(); |
| 513 | } |
| 514 | } |
| 515 | |
| 516 | |
| 517 | bool BreakLocationIterator::RinfoDone() const { |
| 518 | ASSERT(reloc_iterator_->done() == reloc_iterator_original_->done()); |
| 519 | return reloc_iterator_->done(); |
| 520 | } |
| 521 | |
| 522 | |
| 523 | void BreakLocationIterator::RinfoNext() { |
| 524 | reloc_iterator_->next(); |
| 525 | reloc_iterator_original_->next(); |
| 526 | #ifdef DEBUG |
| 527 | ASSERT(reloc_iterator_->done() == reloc_iterator_original_->done()); |
| 528 | if (!reloc_iterator_->done()) { |
| 529 | ASSERT(rmode() == original_rmode()); |
| 530 | } |
| 531 | #endif |
| 532 | } |
| 533 | |
| 534 | |
| 535 | bool Debug::has_break_points_ = false; |
| 536 | ScriptCache* Debug::script_cache_ = NULL; |
| 537 | DebugInfoListNode* Debug::debug_info_list_ = NULL; |
| 538 | |
| 539 | |
| 540 | // Threading support. |
| 541 | void Debug::ThreadInit() { |
| 542 | thread_local_.break_count_ = 0; |
| 543 | thread_local_.break_id_ = 0; |
| 544 | thread_local_.break_frame_id_ = StackFrame::NO_ID; |
| 545 | thread_local_.last_step_action_ = StepNone; |
| 546 | thread_local_.last_statement_position_ = RelocInfo::kNoPosition; |
| 547 | thread_local_.step_count_ = 0; |
| 548 | thread_local_.last_fp_ = 0; |
| 549 | thread_local_.step_into_fp_ = 0; |
| 550 | thread_local_.step_out_fp_ = 0; |
| 551 | thread_local_.after_break_target_ = 0; |
| 552 | thread_local_.debugger_entry_ = NULL; |
| 553 | thread_local_.pending_interrupts_ = 0; |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 554 | thread_local_.restarter_frame_function_pointer_ = NULL; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 555 | } |
| 556 | |
| 557 | |
| 558 | JSCallerSavedBuffer Debug::registers_; |
| 559 | Debug::ThreadLocal Debug::thread_local_; |
| 560 | |
| 561 | |
| 562 | char* Debug::ArchiveDebug(char* storage) { |
| 563 | char* to = storage; |
| 564 | memcpy(to, reinterpret_cast<char*>(&thread_local_), sizeof(ThreadLocal)); |
| 565 | to += sizeof(ThreadLocal); |
| 566 | memcpy(to, reinterpret_cast<char*>(®isters_), sizeof(registers_)); |
| 567 | ThreadInit(); |
| 568 | ASSERT(to <= storage + ArchiveSpacePerThread()); |
| 569 | return storage + ArchiveSpacePerThread(); |
| 570 | } |
| 571 | |
| 572 | |
| 573 | char* Debug::RestoreDebug(char* storage) { |
| 574 | char* from = storage; |
| 575 | memcpy(reinterpret_cast<char*>(&thread_local_), from, sizeof(ThreadLocal)); |
| 576 | from += sizeof(ThreadLocal); |
| 577 | memcpy(reinterpret_cast<char*>(®isters_), from, sizeof(registers_)); |
| 578 | ASSERT(from <= storage + ArchiveSpacePerThread()); |
| 579 | return storage + ArchiveSpacePerThread(); |
| 580 | } |
| 581 | |
| 582 | |
| 583 | int Debug::ArchiveSpacePerThread() { |
| 584 | return sizeof(ThreadLocal) + sizeof(registers_); |
| 585 | } |
| 586 | |
| 587 | |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 588 | // Frame structure (conforms InternalFrame structure): |
| 589 | // -- code |
| 590 | // -- SMI maker |
| 591 | // -- function (slot is called "context") |
| 592 | // -- frame base |
| 593 | Object** Debug::SetUpFrameDropperFrame(StackFrame* bottom_js_frame, |
| 594 | Handle<Code> code) { |
| 595 | ASSERT(bottom_js_frame->is_java_script()); |
| 596 | |
| 597 | Address fp = bottom_js_frame->fp(); |
| 598 | |
| 599 | // Move function pointer into "context" slot. |
| 600 | Memory::Object_at(fp + StandardFrameConstants::kContextOffset) = |
| 601 | Memory::Object_at(fp + JavaScriptFrameConstants::kFunctionOffset); |
| 602 | |
| 603 | Memory::Object_at(fp + InternalFrameConstants::kCodeOffset) = *code; |
| 604 | Memory::Object_at(fp + StandardFrameConstants::kMarkerOffset) = |
| 605 | Smi::FromInt(StackFrame::INTERNAL); |
| 606 | |
| 607 | return reinterpret_cast<Object**>(&Memory::Object_at( |
| 608 | fp + StandardFrameConstants::kContextOffset)); |
| 609 | } |
| 610 | |
| 611 | const int Debug::kFrameDropperFrameSize = 4; |
| 612 | |
| 613 | |
| 614 | |
| 615 | |
| 616 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 617 | // Default break enabled. |
| 618 | bool Debug::disable_break_ = false; |
| 619 | |
| 620 | // Default call debugger on uncaught exception. |
| 621 | bool Debug::break_on_exception_ = false; |
| 622 | bool Debug::break_on_uncaught_exception_ = true; |
| 623 | |
| 624 | Handle<Context> Debug::debug_context_ = Handle<Context>(); |
| 625 | Code* Debug::debug_break_return_ = NULL; |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 626 | Code* Debug::debug_break_slot_ = NULL; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 627 | |
| 628 | |
| 629 | void ScriptCache::Add(Handle<Script> script) { |
| 630 | // Create an entry in the hash map for the script. |
| 631 | int id = Smi::cast(script->id())->value(); |
| 632 | HashMap::Entry* entry = |
| 633 | HashMap::Lookup(reinterpret_cast<void*>(id), Hash(id), true); |
| 634 | if (entry->value != NULL) { |
| 635 | ASSERT(*script == *reinterpret_cast<Script**>(entry->value)); |
| 636 | return; |
| 637 | } |
| 638 | |
| 639 | // Globalize the script object, make it weak and use the location of the |
| 640 | // global handle as the value in the hash map. |
| 641 | Handle<Script> script_ = |
| 642 | Handle<Script>::cast((GlobalHandles::Create(*script))); |
| 643 | GlobalHandles::MakeWeak(reinterpret_cast<Object**>(script_.location()), |
| 644 | this, ScriptCache::HandleWeakScript); |
| 645 | entry->value = script_.location(); |
| 646 | } |
| 647 | |
| 648 | |
| 649 | Handle<FixedArray> ScriptCache::GetScripts() { |
| 650 | Handle<FixedArray> instances = Factory::NewFixedArray(occupancy()); |
| 651 | int count = 0; |
| 652 | for (HashMap::Entry* entry = Start(); entry != NULL; entry = Next(entry)) { |
| 653 | ASSERT(entry->value != NULL); |
| 654 | if (entry->value != NULL) { |
| 655 | instances->set(count, *reinterpret_cast<Script**>(entry->value)); |
| 656 | count++; |
| 657 | } |
| 658 | } |
| 659 | return instances; |
| 660 | } |
| 661 | |
| 662 | |
| 663 | void ScriptCache::ProcessCollectedScripts() { |
| 664 | for (int i = 0; i < collected_scripts_.length(); i++) { |
| 665 | Debugger::OnScriptCollected(collected_scripts_[i]); |
| 666 | } |
| 667 | collected_scripts_.Clear(); |
| 668 | } |
| 669 | |
| 670 | |
| 671 | void ScriptCache::Clear() { |
| 672 | // Iterate the script cache to get rid of all the weak handles. |
| 673 | for (HashMap::Entry* entry = Start(); entry != NULL; entry = Next(entry)) { |
| 674 | ASSERT(entry != NULL); |
| 675 | Object** location = reinterpret_cast<Object**>(entry->value); |
| 676 | ASSERT((*location)->IsScript()); |
| 677 | GlobalHandles::ClearWeakness(location); |
| 678 | GlobalHandles::Destroy(location); |
| 679 | } |
| 680 | // Clear the content of the hash map. |
| 681 | HashMap::Clear(); |
| 682 | } |
| 683 | |
| 684 | |
| 685 | void ScriptCache::HandleWeakScript(v8::Persistent<v8::Value> obj, void* data) { |
| 686 | ScriptCache* script_cache = reinterpret_cast<ScriptCache*>(data); |
| 687 | // Find the location of the global handle. |
| 688 | Script** location = |
| 689 | reinterpret_cast<Script**>(Utils::OpenHandle(*obj).location()); |
| 690 | ASSERT((*location)->IsScript()); |
| 691 | |
| 692 | // Remove the entry from the cache. |
| 693 | int id = Smi::cast((*location)->id())->value(); |
| 694 | script_cache->Remove(reinterpret_cast<void*>(id), Hash(id)); |
| 695 | script_cache->collected_scripts_.Add(id); |
| 696 | |
| 697 | // Clear the weak handle. |
| 698 | obj.Dispose(); |
| 699 | obj.Clear(); |
| 700 | } |
| 701 | |
| 702 | |
| 703 | void Debug::Setup(bool create_heap_objects) { |
| 704 | ThreadInit(); |
| 705 | if (create_heap_objects) { |
| 706 | // Get code to handle debug break on return. |
| 707 | debug_break_return_ = |
| 708 | Builtins::builtin(Builtins::Return_DebugBreak); |
| 709 | ASSERT(debug_break_return_->IsCode()); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 710 | // Get code to handle debug break in debug break slots. |
| 711 | debug_break_slot_ = |
| 712 | Builtins::builtin(Builtins::Slot_DebugBreak); |
| 713 | ASSERT(debug_break_slot_->IsCode()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 714 | } |
| 715 | } |
| 716 | |
| 717 | |
| 718 | void Debug::HandleWeakDebugInfo(v8::Persistent<v8::Value> obj, void* data) { |
| 719 | DebugInfoListNode* node = reinterpret_cast<DebugInfoListNode*>(data); |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 720 | // We need to clear all breakpoints associated with the function to restore |
| 721 | // original code and avoid patching the code twice later because |
| 722 | // the function will live in the heap until next gc, and can be found by |
| 723 | // Runtime::FindSharedFunctionInfoInScript. |
| 724 | BreakLocationIterator it(node->debug_info(), ALL_BREAK_LOCATIONS); |
| 725 | it.ClearAllDebugBreak(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 726 | RemoveDebugInfo(node->debug_info()); |
| 727 | #ifdef DEBUG |
| 728 | node = Debug::debug_info_list_; |
| 729 | while (node != NULL) { |
| 730 | ASSERT(node != reinterpret_cast<DebugInfoListNode*>(data)); |
| 731 | node = node->next(); |
| 732 | } |
| 733 | #endif |
| 734 | } |
| 735 | |
| 736 | |
| 737 | DebugInfoListNode::DebugInfoListNode(DebugInfo* debug_info): next_(NULL) { |
| 738 | // Globalize the request debug info object and make it weak. |
| 739 | debug_info_ = Handle<DebugInfo>::cast((GlobalHandles::Create(debug_info))); |
| 740 | GlobalHandles::MakeWeak(reinterpret_cast<Object**>(debug_info_.location()), |
| 741 | this, Debug::HandleWeakDebugInfo); |
| 742 | } |
| 743 | |
| 744 | |
| 745 | DebugInfoListNode::~DebugInfoListNode() { |
| 746 | GlobalHandles::Destroy(reinterpret_cast<Object**>(debug_info_.location())); |
| 747 | } |
| 748 | |
| 749 | |
| 750 | bool Debug::CompileDebuggerScript(int index) { |
| 751 | HandleScope scope; |
| 752 | |
| 753 | // Bail out if the index is invalid. |
| 754 | if (index == -1) { |
| 755 | return false; |
| 756 | } |
| 757 | |
| 758 | // Find source and name for the requested script. |
| 759 | Handle<String> source_code = Bootstrapper::NativesSourceLookup(index); |
| 760 | Vector<const char> name = Natives::GetScriptName(index); |
| 761 | Handle<String> script_name = Factory::NewStringFromAscii(name); |
| 762 | |
| 763 | // Compile the script. |
| 764 | bool allow_natives_syntax = FLAG_allow_natives_syntax; |
| 765 | FLAG_allow_natives_syntax = true; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 766 | Handle<SharedFunctionInfo> function_info; |
| 767 | function_info = Compiler::Compile(source_code, |
| 768 | script_name, |
| 769 | 0, 0, NULL, NULL, |
| 770 | Handle<String>::null(), |
| 771 | NATIVES_CODE); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 772 | FLAG_allow_natives_syntax = allow_natives_syntax; |
| 773 | |
| 774 | // Silently ignore stack overflows during compilation. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 775 | if (function_info.is_null()) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 776 | ASSERT(Top::has_pending_exception()); |
| 777 | Top::clear_pending_exception(); |
| 778 | return false; |
| 779 | } |
| 780 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 781 | // Execute the shared function in the debugger context. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 782 | Handle<Context> context = Top::global_context(); |
| 783 | bool caught_exception = false; |
| 784 | Handle<JSFunction> function = |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 785 | Factory::NewFunctionFromSharedFunctionInfo(function_info, context); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 786 | Handle<Object> result = |
| 787 | Execution::TryCall(function, Handle<Object>(context->global()), |
| 788 | 0, NULL, &caught_exception); |
| 789 | |
| 790 | // Check for caught exceptions. |
| 791 | if (caught_exception) { |
| 792 | Handle<Object> message = MessageHandler::MakeMessageObject( |
| 793 | "error_loading_debugger", NULL, Vector<Handle<Object> >::empty(), |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 794 | Handle<String>(), Handle<JSArray>()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 795 | MessageHandler::ReportMessage(NULL, message); |
| 796 | return false; |
| 797 | } |
| 798 | |
| 799 | // Mark this script as native and return successfully. |
| 800 | Handle<Script> script(Script::cast(function->shared()->script())); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 801 | script->set_type(Smi::FromInt(Script::TYPE_NATIVE)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 802 | return true; |
| 803 | } |
| 804 | |
| 805 | |
| 806 | bool Debug::Load() { |
| 807 | // Return if debugger is already loaded. |
| 808 | if (IsLoaded()) return true; |
| 809 | |
| 810 | // Bail out if we're already in the process of compiling the native |
| 811 | // JavaScript source code for the debugger. |
| 812 | if (Debugger::compiling_natives() || Debugger::is_loading_debugger()) |
| 813 | return false; |
| 814 | Debugger::set_loading_debugger(true); |
| 815 | |
| 816 | // Disable breakpoints and interrupts while compiling and running the |
| 817 | // debugger scripts including the context creation code. |
| 818 | DisableBreak disable(true); |
| 819 | PostponeInterruptsScope postpone; |
| 820 | |
| 821 | // Create the debugger context. |
| 822 | HandleScope scope; |
| 823 | Handle<Context> context = |
| 824 | Bootstrapper::CreateEnvironment(Handle<Object>::null(), |
| 825 | v8::Handle<ObjectTemplate>(), |
| 826 | NULL); |
| 827 | |
| 828 | // Use the debugger context. |
| 829 | SaveContext save; |
| 830 | Top::set_context(*context); |
| 831 | |
| 832 | // Expose the builtins object in the debugger context. |
| 833 | Handle<String> key = Factory::LookupAsciiSymbol("builtins"); |
| 834 | Handle<GlobalObject> global = Handle<GlobalObject>(context->global()); |
| 835 | SetProperty(global, key, Handle<Object>(global->builtins()), NONE); |
| 836 | |
| 837 | // Compile the JavaScript for the debugger in the debugger context. |
| 838 | Debugger::set_compiling_natives(true); |
| 839 | bool caught_exception = |
| 840 | !CompileDebuggerScript(Natives::GetIndex("mirror")) || |
| 841 | !CompileDebuggerScript(Natives::GetIndex("debug")); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 842 | |
| 843 | if (FLAG_enable_liveedit) { |
| 844 | caught_exception = caught_exception || |
| 845 | !CompileDebuggerScript(Natives::GetIndex("liveedit")); |
| 846 | } |
| 847 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 848 | Debugger::set_compiling_natives(false); |
| 849 | |
| 850 | // Make sure we mark the debugger as not loading before we might |
| 851 | // return. |
| 852 | Debugger::set_loading_debugger(false); |
| 853 | |
| 854 | // Check for caught exceptions. |
| 855 | if (caught_exception) return false; |
| 856 | |
| 857 | // Debugger loaded. |
| 858 | debug_context_ = Handle<Context>::cast(GlobalHandles::Create(*context)); |
| 859 | |
| 860 | return true; |
| 861 | } |
| 862 | |
| 863 | |
| 864 | void Debug::Unload() { |
| 865 | // Return debugger is not loaded. |
| 866 | if (!IsLoaded()) { |
| 867 | return; |
| 868 | } |
| 869 | |
| 870 | // Clear the script cache. |
| 871 | DestroyScriptCache(); |
| 872 | |
| 873 | // Clear debugger context global handle. |
| 874 | GlobalHandles::Destroy(reinterpret_cast<Object**>(debug_context_.location())); |
| 875 | debug_context_ = Handle<Context>(); |
| 876 | } |
| 877 | |
| 878 | |
| 879 | // Set the flag indicating that preemption happened during debugging. |
| 880 | void Debug::PreemptionWhileInDebugger() { |
| 881 | ASSERT(InDebugger()); |
| 882 | Debug::set_interrupts_pending(PREEMPT); |
| 883 | } |
| 884 | |
| 885 | |
| 886 | void Debug::Iterate(ObjectVisitor* v) { |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 887 | v->VisitPointer(BitCast<Object**>(&(debug_break_return_))); |
| 888 | v->VisitPointer(BitCast<Object**>(&(debug_break_slot_))); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 889 | } |
| 890 | |
| 891 | |
| 892 | Object* Debug::Break(Arguments args) { |
| 893 | HandleScope scope; |
| 894 | ASSERT(args.length() == 0); |
| 895 | |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 896 | thread_local_.frame_drop_mode_ = FRAMES_UNTOUCHED; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 897 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 898 | // Get the top-most JavaScript frame. |
| 899 | JavaScriptFrameIterator it; |
| 900 | JavaScriptFrame* frame = it.frame(); |
| 901 | |
| 902 | // Just continue if breaks are disabled or debugger cannot be loaded. |
| 903 | if (disable_break() || !Load()) { |
| 904 | SetAfterBreakTarget(frame); |
| 905 | return Heap::undefined_value(); |
| 906 | } |
| 907 | |
| 908 | // Enter the debugger. |
| 909 | EnterDebugger debugger; |
| 910 | if (debugger.FailedToEnter()) { |
| 911 | return Heap::undefined_value(); |
| 912 | } |
| 913 | |
| 914 | // Postpone interrupt during breakpoint processing. |
| 915 | PostponeInterruptsScope postpone; |
| 916 | |
| 917 | // Get the debug info (create it if it does not exist). |
| 918 | Handle<SharedFunctionInfo> shared = |
| 919 | Handle<SharedFunctionInfo>(JSFunction::cast(frame->function())->shared()); |
| 920 | Handle<DebugInfo> debug_info = GetDebugInfo(shared); |
| 921 | |
| 922 | // Find the break point where execution has stopped. |
| 923 | BreakLocationIterator break_location_iterator(debug_info, |
| 924 | ALL_BREAK_LOCATIONS); |
| 925 | break_location_iterator.FindBreakLocationFromAddress(frame->pc()); |
| 926 | |
| 927 | // Check whether step next reached a new statement. |
| 928 | if (!StepNextContinue(&break_location_iterator, frame)) { |
| 929 | // Decrease steps left if performing multiple steps. |
| 930 | if (thread_local_.step_count_ > 0) { |
| 931 | thread_local_.step_count_--; |
| 932 | } |
| 933 | } |
| 934 | |
| 935 | // If there is one or more real break points check whether any of these are |
| 936 | // triggered. |
| 937 | Handle<Object> break_points_hit(Heap::undefined_value()); |
| 938 | if (break_location_iterator.HasBreakPoint()) { |
| 939 | Handle<Object> break_point_objects = |
| 940 | Handle<Object>(break_location_iterator.BreakPointObjects()); |
| 941 | break_points_hit = CheckBreakPoints(break_point_objects); |
| 942 | } |
| 943 | |
| 944 | // If step out is active skip everything until the frame where we need to step |
| 945 | // out to is reached, unless real breakpoint is hit. |
| 946 | if (Debug::StepOutActive() && frame->fp() != Debug::step_out_fp() && |
| 947 | break_points_hit->IsUndefined() ) { |
| 948 | // Step count should always be 0 for StepOut. |
| 949 | ASSERT(thread_local_.step_count_ == 0); |
| 950 | } else if (!break_points_hit->IsUndefined() || |
| 951 | (thread_local_.last_step_action_ != StepNone && |
| 952 | thread_local_.step_count_ == 0)) { |
| 953 | // Notify debugger if a real break point is triggered or if performing |
| 954 | // single stepping with no more steps to perform. Otherwise do another step. |
| 955 | |
| 956 | // Clear all current stepping setup. |
| 957 | ClearStepping(); |
| 958 | |
| 959 | // Notify the debug event listeners. |
| 960 | Debugger::OnDebugBreak(break_points_hit, false); |
| 961 | } else if (thread_local_.last_step_action_ != StepNone) { |
| 962 | // Hold on to last step action as it is cleared by the call to |
| 963 | // ClearStepping. |
| 964 | StepAction step_action = thread_local_.last_step_action_; |
| 965 | int step_count = thread_local_.step_count_; |
| 966 | |
| 967 | // Clear all current stepping setup. |
| 968 | ClearStepping(); |
| 969 | |
| 970 | // Set up for the remaining steps. |
| 971 | PrepareStep(step_action, step_count); |
| 972 | } |
| 973 | |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 974 | if (thread_local_.frame_drop_mode_ == FRAMES_UNTOUCHED) { |
| 975 | SetAfterBreakTarget(frame); |
| 976 | } else if (thread_local_.frame_drop_mode_ == FRAME_DROPPED_IN_IC_CALL) { |
| 977 | // We must have been calling IC stub. Do not go there anymore. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 978 | Code* plain_return = Builtins::builtin(Builtins::PlainReturn_LiveEdit); |
| 979 | thread_local_.after_break_target_ = plain_return->entry(); |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 980 | } else if (thread_local_.frame_drop_mode_ == |
| 981 | FRAME_DROPPED_IN_DEBUG_SLOT_CALL) { |
| 982 | // Debug break slot stub does not return normally, instead it manually |
| 983 | // cleans the stack and jumps. We should patch the jump address. |
| 984 | Code* plain_return = Builtins::builtin(Builtins::FrameDropper_LiveEdit); |
| 985 | thread_local_.after_break_target_ = plain_return->entry(); |
| 986 | } else if (thread_local_.frame_drop_mode_ == FRAME_DROPPED_IN_DIRECT_CALL) { |
| 987 | // Nothing to do, after_break_target is not used here. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 988 | } else { |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 989 | UNREACHABLE(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 990 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 991 | |
| 992 | return Heap::undefined_value(); |
| 993 | } |
| 994 | |
| 995 | |
| 996 | // Check the break point objects for whether one or more are actually |
| 997 | // triggered. This function returns a JSArray with the break point objects |
| 998 | // which is triggered. |
| 999 | Handle<Object> Debug::CheckBreakPoints(Handle<Object> break_point_objects) { |
| 1000 | int break_points_hit_count = 0; |
| 1001 | Handle<JSArray> break_points_hit = Factory::NewJSArray(1); |
| 1002 | |
| 1003 | // If there are multiple break points they are in a FixedArray. |
| 1004 | ASSERT(!break_point_objects->IsUndefined()); |
| 1005 | if (break_point_objects->IsFixedArray()) { |
| 1006 | Handle<FixedArray> array(FixedArray::cast(*break_point_objects)); |
| 1007 | for (int i = 0; i < array->length(); i++) { |
| 1008 | Handle<Object> o(array->get(i)); |
| 1009 | if (CheckBreakPoint(o)) { |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 1010 | SetElement(break_points_hit, break_points_hit_count++, o); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1011 | } |
| 1012 | } |
| 1013 | } else { |
| 1014 | if (CheckBreakPoint(break_point_objects)) { |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 1015 | SetElement(break_points_hit, |
| 1016 | break_points_hit_count++, |
| 1017 | break_point_objects); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1018 | } |
| 1019 | } |
| 1020 | |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 1021 | // Return undefined if no break points were triggered. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1022 | if (break_points_hit_count == 0) { |
| 1023 | return Factory::undefined_value(); |
| 1024 | } |
| 1025 | return break_points_hit; |
| 1026 | } |
| 1027 | |
| 1028 | |
| 1029 | // Check whether a single break point object is triggered. |
| 1030 | bool Debug::CheckBreakPoint(Handle<Object> break_point_object) { |
| 1031 | HandleScope scope; |
| 1032 | |
| 1033 | // Ignore check if break point object is not a JSObject. |
| 1034 | if (!break_point_object->IsJSObject()) return true; |
| 1035 | |
| 1036 | // Get the function CheckBreakPoint (defined in debug.js). |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 1037 | Handle<String> is_break_point_triggered_symbol = |
| 1038 | Factory::LookupAsciiSymbol("IsBreakPointTriggered"); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1039 | Handle<JSFunction> check_break_point = |
| 1040 | Handle<JSFunction>(JSFunction::cast( |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 1041 | debug_context()->global()->GetPropertyNoExceptionThrown( |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 1042 | *is_break_point_triggered_symbol))); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1043 | |
| 1044 | // Get the break id as an object. |
| 1045 | Handle<Object> break_id = Factory::NewNumberFromInt(Debug::break_id()); |
| 1046 | |
| 1047 | // Call HandleBreakPointx. |
| 1048 | bool caught_exception = false; |
| 1049 | const int argc = 2; |
| 1050 | Object** argv[argc] = { |
| 1051 | break_id.location(), |
| 1052 | reinterpret_cast<Object**>(break_point_object.location()) |
| 1053 | }; |
| 1054 | Handle<Object> result = Execution::TryCall(check_break_point, |
| 1055 | Top::builtins(), argc, argv, |
| 1056 | &caught_exception); |
| 1057 | |
| 1058 | // If exception or non boolean result handle as not triggered |
| 1059 | if (caught_exception || !result->IsBoolean()) { |
| 1060 | return false; |
| 1061 | } |
| 1062 | |
| 1063 | // Return whether the break point is triggered. |
| 1064 | return *result == Heap::true_value(); |
| 1065 | } |
| 1066 | |
| 1067 | |
| 1068 | // Check whether the function has debug information. |
| 1069 | bool Debug::HasDebugInfo(Handle<SharedFunctionInfo> shared) { |
| 1070 | return !shared->debug_info()->IsUndefined(); |
| 1071 | } |
| 1072 | |
| 1073 | |
| 1074 | // Return the debug info for this function. EnsureDebugInfo must be called |
| 1075 | // prior to ensure the debug info has been generated for shared. |
| 1076 | Handle<DebugInfo> Debug::GetDebugInfo(Handle<SharedFunctionInfo> shared) { |
| 1077 | ASSERT(HasDebugInfo(shared)); |
| 1078 | return Handle<DebugInfo>(DebugInfo::cast(shared->debug_info())); |
| 1079 | } |
| 1080 | |
| 1081 | |
| 1082 | void Debug::SetBreakPoint(Handle<SharedFunctionInfo> shared, |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 1083 | Handle<Object> break_point_object, |
| 1084 | int* source_position) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1085 | HandleScope scope; |
| 1086 | |
| 1087 | if (!EnsureDebugInfo(shared)) { |
| 1088 | // Return if retrieving debug info failed. |
| 1089 | return; |
| 1090 | } |
| 1091 | |
| 1092 | Handle<DebugInfo> debug_info = GetDebugInfo(shared); |
| 1093 | // Source positions starts with zero. |
| 1094 | ASSERT(source_position >= 0); |
| 1095 | |
| 1096 | // Find the break point and change it. |
| 1097 | BreakLocationIterator it(debug_info, SOURCE_BREAK_LOCATIONS); |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 1098 | it.FindBreakLocationFromPosition(*source_position); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1099 | it.SetBreakPoint(break_point_object); |
| 1100 | |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 1101 | *source_position = it.position(); |
| 1102 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1103 | // At least one active break point now. |
| 1104 | ASSERT(debug_info->GetBreakPointCount() > 0); |
| 1105 | } |
| 1106 | |
| 1107 | |
| 1108 | void Debug::ClearBreakPoint(Handle<Object> break_point_object) { |
| 1109 | HandleScope scope; |
| 1110 | |
| 1111 | DebugInfoListNode* node = debug_info_list_; |
| 1112 | while (node != NULL) { |
| 1113 | Object* result = DebugInfo::FindBreakPointInfo(node->debug_info(), |
| 1114 | break_point_object); |
| 1115 | if (!result->IsUndefined()) { |
| 1116 | // Get information in the break point. |
| 1117 | BreakPointInfo* break_point_info = BreakPointInfo::cast(result); |
| 1118 | Handle<DebugInfo> debug_info = node->debug_info(); |
| 1119 | Handle<SharedFunctionInfo> shared(debug_info->shared()); |
| 1120 | int source_position = break_point_info->statement_position()->value(); |
| 1121 | |
| 1122 | // Source positions starts with zero. |
| 1123 | ASSERT(source_position >= 0); |
| 1124 | |
| 1125 | // Find the break point and clear it. |
| 1126 | BreakLocationIterator it(debug_info, SOURCE_BREAK_LOCATIONS); |
| 1127 | it.FindBreakLocationFromPosition(source_position); |
| 1128 | it.ClearBreakPoint(break_point_object); |
| 1129 | |
| 1130 | // If there are no more break points left remove the debug info for this |
| 1131 | // function. |
| 1132 | if (debug_info->GetBreakPointCount() == 0) { |
| 1133 | RemoveDebugInfo(debug_info); |
| 1134 | } |
| 1135 | |
| 1136 | return; |
| 1137 | } |
| 1138 | node = node->next(); |
| 1139 | } |
| 1140 | } |
| 1141 | |
| 1142 | |
| 1143 | void Debug::ClearAllBreakPoints() { |
| 1144 | DebugInfoListNode* node = debug_info_list_; |
| 1145 | while (node != NULL) { |
| 1146 | // Remove all debug break code. |
| 1147 | BreakLocationIterator it(node->debug_info(), ALL_BREAK_LOCATIONS); |
| 1148 | it.ClearAllDebugBreak(); |
| 1149 | node = node->next(); |
| 1150 | } |
| 1151 | |
| 1152 | // Remove all debug info. |
| 1153 | while (debug_info_list_ != NULL) { |
| 1154 | RemoveDebugInfo(debug_info_list_->debug_info()); |
| 1155 | } |
| 1156 | } |
| 1157 | |
| 1158 | |
| 1159 | void Debug::FloodWithOneShot(Handle<SharedFunctionInfo> shared) { |
| 1160 | // Make sure the function has setup the debug info. |
| 1161 | if (!EnsureDebugInfo(shared)) { |
| 1162 | // Return if we failed to retrieve the debug info. |
| 1163 | return; |
| 1164 | } |
| 1165 | |
| 1166 | // Flood the function with break points. |
| 1167 | BreakLocationIterator it(GetDebugInfo(shared), ALL_BREAK_LOCATIONS); |
| 1168 | while (!it.Done()) { |
| 1169 | it.SetOneShot(); |
| 1170 | it.Next(); |
| 1171 | } |
| 1172 | } |
| 1173 | |
| 1174 | |
| 1175 | void Debug::FloodHandlerWithOneShot() { |
| 1176 | // Iterate through the JavaScript stack looking for handlers. |
| 1177 | StackFrame::Id id = break_frame_id(); |
| 1178 | if (id == StackFrame::NO_ID) { |
| 1179 | // If there is no JavaScript stack don't do anything. |
| 1180 | return; |
| 1181 | } |
| 1182 | for (JavaScriptFrameIterator it(id); !it.done(); it.Advance()) { |
| 1183 | JavaScriptFrame* frame = it.frame(); |
| 1184 | if (frame->HasHandler()) { |
| 1185 | Handle<SharedFunctionInfo> shared = |
| 1186 | Handle<SharedFunctionInfo>( |
| 1187 | JSFunction::cast(frame->function())->shared()); |
| 1188 | // Flood the function with the catch block with break points |
| 1189 | FloodWithOneShot(shared); |
| 1190 | return; |
| 1191 | } |
| 1192 | } |
| 1193 | } |
| 1194 | |
| 1195 | |
| 1196 | void Debug::ChangeBreakOnException(ExceptionBreakType type, bool enable) { |
| 1197 | if (type == BreakUncaughtException) { |
| 1198 | break_on_uncaught_exception_ = enable; |
| 1199 | } else { |
| 1200 | break_on_exception_ = enable; |
| 1201 | } |
| 1202 | } |
| 1203 | |
| 1204 | |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 1205 | bool Debug::IsBreakOnException(ExceptionBreakType type) { |
| 1206 | if (type == BreakUncaughtException) { |
| 1207 | return break_on_uncaught_exception_; |
| 1208 | } else { |
| 1209 | return break_on_exception_; |
| 1210 | } |
| 1211 | } |
| 1212 | |
| 1213 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1214 | void Debug::PrepareStep(StepAction step_action, int step_count) { |
| 1215 | HandleScope scope; |
| 1216 | ASSERT(Debug::InDebugger()); |
| 1217 | |
| 1218 | // Remember this step action and count. |
| 1219 | thread_local_.last_step_action_ = step_action; |
| 1220 | if (step_action == StepOut) { |
| 1221 | // For step out target frame will be found on the stack so there is no need |
| 1222 | // to set step counter for it. It's expected to always be 0 for StepOut. |
| 1223 | thread_local_.step_count_ = 0; |
| 1224 | } else { |
| 1225 | thread_local_.step_count_ = step_count; |
| 1226 | } |
| 1227 | |
| 1228 | // Get the frame where the execution has stopped and skip the debug frame if |
| 1229 | // any. The debug frame will only be present if execution was stopped due to |
| 1230 | // hitting a break point. In other situations (e.g. unhandled exception) the |
| 1231 | // debug frame is not present. |
| 1232 | StackFrame::Id id = break_frame_id(); |
| 1233 | if (id == StackFrame::NO_ID) { |
| 1234 | // If there is no JavaScript stack don't do anything. |
| 1235 | return; |
| 1236 | } |
| 1237 | JavaScriptFrameIterator frames_it(id); |
| 1238 | JavaScriptFrame* frame = frames_it.frame(); |
| 1239 | |
| 1240 | // First of all ensure there is one-shot break points in the top handler |
| 1241 | // if any. |
| 1242 | FloodHandlerWithOneShot(); |
| 1243 | |
| 1244 | // If the function on the top frame is unresolved perform step out. This will |
| 1245 | // be the case when calling unknown functions and having the debugger stopped |
| 1246 | // in an unhandled exception. |
| 1247 | if (!frame->function()->IsJSFunction()) { |
| 1248 | // Step out: Find the calling JavaScript frame and flood it with |
| 1249 | // breakpoints. |
| 1250 | frames_it.Advance(); |
| 1251 | // Fill the function to return to with one-shot break points. |
| 1252 | JSFunction* function = JSFunction::cast(frames_it.frame()->function()); |
| 1253 | FloodWithOneShot(Handle<SharedFunctionInfo>(function->shared())); |
| 1254 | return; |
| 1255 | } |
| 1256 | |
| 1257 | // Get the debug info (create it if it does not exist). |
| 1258 | Handle<SharedFunctionInfo> shared = |
| 1259 | Handle<SharedFunctionInfo>(JSFunction::cast(frame->function())->shared()); |
| 1260 | if (!EnsureDebugInfo(shared)) { |
| 1261 | // Return if ensuring debug info failed. |
| 1262 | return; |
| 1263 | } |
| 1264 | Handle<DebugInfo> debug_info = GetDebugInfo(shared); |
| 1265 | |
| 1266 | // Find the break location where execution has stopped. |
| 1267 | BreakLocationIterator it(debug_info, ALL_BREAK_LOCATIONS); |
| 1268 | it.FindBreakLocationFromAddress(frame->pc()); |
| 1269 | |
| 1270 | // Compute whether or not the target is a call target. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1271 | bool is_load_or_store = false; |
| 1272 | bool is_inline_cache_stub = false; |
Ben Murdoch | bb769b2 | 2010-08-11 14:56:33 +0100 | [diff] [blame] | 1273 | bool is_at_restarted_function = false; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1274 | Handle<Code> call_function_stub; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1275 | |
Ben Murdoch | bb769b2 | 2010-08-11 14:56:33 +0100 | [diff] [blame] | 1276 | if (thread_local_.restarter_frame_function_pointer_ == NULL) { |
| 1277 | if (RelocInfo::IsCodeTarget(it.rinfo()->rmode())) { |
| 1278 | bool is_call_target = false; |
| 1279 | Address target = it.rinfo()->target_address(); |
| 1280 | Code* code = Code::GetCodeFromTargetAddress(target); |
| 1281 | if (code->is_call_stub() || code->is_keyed_call_stub()) { |
| 1282 | is_call_target = true; |
| 1283 | } |
| 1284 | if (code->is_inline_cache_stub()) { |
| 1285 | is_inline_cache_stub = true; |
| 1286 | is_load_or_store = !is_call_target; |
| 1287 | } |
| 1288 | |
| 1289 | // Check if target code is CallFunction stub. |
| 1290 | Code* maybe_call_function_stub = code; |
| 1291 | // If there is a breakpoint at this line look at the original code to |
| 1292 | // check if it is a CallFunction stub. |
| 1293 | if (it.IsDebugBreak()) { |
| 1294 | Address original_target = it.original_rinfo()->target_address(); |
| 1295 | maybe_call_function_stub = |
| 1296 | Code::GetCodeFromTargetAddress(original_target); |
| 1297 | } |
| 1298 | if (maybe_call_function_stub->kind() == Code::STUB && |
| 1299 | maybe_call_function_stub->major_key() == CodeStub::CallFunction) { |
| 1300 | // Save reference to the code as we may need it to find out arguments |
| 1301 | // count for 'step in' later. |
| 1302 | call_function_stub = Handle<Code>(maybe_call_function_stub); |
| 1303 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1304 | } |
Ben Murdoch | bb769b2 | 2010-08-11 14:56:33 +0100 | [diff] [blame] | 1305 | } else { |
| 1306 | is_at_restarted_function = true; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1307 | } |
| 1308 | |
| 1309 | // If this is the last break code target step out is the only possibility. |
| 1310 | if (it.IsExit() || step_action == StepOut) { |
| 1311 | if (step_action == StepOut) { |
| 1312 | // Skip step_count frames starting with the current one. |
| 1313 | while (step_count-- > 0 && !frames_it.done()) { |
| 1314 | frames_it.Advance(); |
| 1315 | } |
| 1316 | } else { |
| 1317 | ASSERT(it.IsExit()); |
| 1318 | frames_it.Advance(); |
| 1319 | } |
| 1320 | // Skip builtin functions on the stack. |
| 1321 | while (!frames_it.done() && |
| 1322 | JSFunction::cast(frames_it.frame()->function())->IsBuiltin()) { |
| 1323 | frames_it.Advance(); |
| 1324 | } |
| 1325 | // Step out: If there is a JavaScript caller frame, we need to |
| 1326 | // flood it with breakpoints. |
| 1327 | if (!frames_it.done()) { |
| 1328 | // Fill the function to return to with one-shot break points. |
| 1329 | JSFunction* function = JSFunction::cast(frames_it.frame()->function()); |
| 1330 | FloodWithOneShot(Handle<SharedFunctionInfo>(function->shared())); |
| 1331 | // Set target frame pointer. |
| 1332 | ActivateStepOut(frames_it.frame()); |
| 1333 | } |
| 1334 | } else if (!(is_inline_cache_stub || RelocInfo::IsConstructCall(it.rmode()) || |
Ben Murdoch | bb769b2 | 2010-08-11 14:56:33 +0100 | [diff] [blame] | 1335 | !call_function_stub.is_null() || is_at_restarted_function) |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1336 | || step_action == StepNext || step_action == StepMin) { |
| 1337 | // Step next or step min. |
| 1338 | |
| 1339 | // Fill the current function with one-shot break points. |
| 1340 | FloodWithOneShot(shared); |
| 1341 | |
| 1342 | // Remember source position and frame to handle step next. |
| 1343 | thread_local_.last_statement_position_ = |
| 1344 | debug_info->code()->SourceStatementPosition(frame->pc()); |
| 1345 | thread_local_.last_fp_ = frame->fp(); |
| 1346 | } else { |
Ben Murdoch | bb769b2 | 2010-08-11 14:56:33 +0100 | [diff] [blame] | 1347 | // If there's restarter frame on top of the stack, just get the pointer |
| 1348 | // to function which is going to be restarted. |
| 1349 | if (is_at_restarted_function) { |
| 1350 | Handle<JSFunction> restarted_function( |
| 1351 | JSFunction::cast(*thread_local_.restarter_frame_function_pointer_)); |
| 1352 | Handle<SharedFunctionInfo> restarted_shared( |
| 1353 | restarted_function->shared()); |
| 1354 | FloodWithOneShot(restarted_shared); |
| 1355 | } else if (!call_function_stub.is_null()) { |
| 1356 | // If it's CallFunction stub ensure target function is compiled and flood |
| 1357 | // it with one shot breakpoints. |
| 1358 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1359 | // Find out number of arguments from the stub minor key. |
| 1360 | // Reverse lookup required as the minor key cannot be retrieved |
| 1361 | // from the code object. |
| 1362 | Handle<Object> obj( |
| 1363 | Heap::code_stubs()->SlowReverseLookup(*call_function_stub)); |
| 1364 | ASSERT(*obj != Heap::undefined_value()); |
| 1365 | ASSERT(obj->IsSmi()); |
| 1366 | // Get the STUB key and extract major and minor key. |
| 1367 | uint32_t key = Smi::cast(*obj)->value(); |
| 1368 | // Argc in the stub is the number of arguments passed - not the |
| 1369 | // expected arguments of the called function. |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 1370 | int call_function_arg_count = |
| 1371 | CallFunctionStub::ExtractArgcFromMinorKey( |
| 1372 | CodeStub::MinorKeyFromKey(key)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1373 | ASSERT(call_function_stub->major_key() == |
| 1374 | CodeStub::MajorKeyFromKey(key)); |
| 1375 | |
| 1376 | // Find target function on the expression stack. |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 1377 | // Expression stack looks like this (top to bottom): |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1378 | // argN |
| 1379 | // ... |
| 1380 | // arg0 |
| 1381 | // Receiver |
| 1382 | // Function to call |
| 1383 | int expressions_count = frame->ComputeExpressionsCount(); |
| 1384 | ASSERT(expressions_count - 2 - call_function_arg_count >= 0); |
| 1385 | Object* fun = frame->GetExpression( |
| 1386 | expressions_count - 2 - call_function_arg_count); |
| 1387 | if (fun->IsJSFunction()) { |
| 1388 | Handle<JSFunction> js_function(JSFunction::cast(fun)); |
| 1389 | // Don't step into builtins. |
| 1390 | if (!js_function->IsBuiltin()) { |
| 1391 | // It will also compile target function if it's not compiled yet. |
| 1392 | FloodWithOneShot(Handle<SharedFunctionInfo>(js_function->shared())); |
| 1393 | } |
| 1394 | } |
| 1395 | } |
| 1396 | |
| 1397 | // Fill the current function with one-shot break points even for step in on |
| 1398 | // a call target as the function called might be a native function for |
| 1399 | // which step in will not stop. It also prepares for stepping in |
| 1400 | // getters/setters. |
| 1401 | FloodWithOneShot(shared); |
| 1402 | |
| 1403 | if (is_load_or_store) { |
| 1404 | // Remember source position and frame to handle step in getter/setter. If |
| 1405 | // there is a custom getter/setter it will be handled in |
| 1406 | // Object::Get/SetPropertyWithCallback, otherwise the step action will be |
| 1407 | // propagated on the next Debug::Break. |
| 1408 | thread_local_.last_statement_position_ = |
| 1409 | debug_info->code()->SourceStatementPosition(frame->pc()); |
| 1410 | thread_local_.last_fp_ = frame->fp(); |
| 1411 | } |
| 1412 | |
| 1413 | // Step in or Step in min |
| 1414 | it.PrepareStepIn(); |
| 1415 | ActivateStepIn(frame); |
| 1416 | } |
| 1417 | } |
| 1418 | |
| 1419 | |
| 1420 | // Check whether the current debug break should be reported to the debugger. It |
| 1421 | // is used to have step next and step in only report break back to the debugger |
| 1422 | // if on a different frame or in a different statement. In some situations |
| 1423 | // there will be several break points in the same statement when the code is |
| 1424 | // flooded with one-shot break points. This function helps to perform several |
| 1425 | // steps before reporting break back to the debugger. |
| 1426 | bool Debug::StepNextContinue(BreakLocationIterator* break_location_iterator, |
| 1427 | JavaScriptFrame* frame) { |
| 1428 | // If the step last action was step next or step in make sure that a new |
| 1429 | // statement is hit. |
| 1430 | if (thread_local_.last_step_action_ == StepNext || |
| 1431 | thread_local_.last_step_action_ == StepIn) { |
| 1432 | // Never continue if returning from function. |
| 1433 | if (break_location_iterator->IsExit()) return false; |
| 1434 | |
| 1435 | // Continue if we are still on the same frame and in the same statement. |
| 1436 | int current_statement_position = |
| 1437 | break_location_iterator->code()->SourceStatementPosition(frame->pc()); |
| 1438 | return thread_local_.last_fp_ == frame->fp() && |
| 1439 | thread_local_.last_statement_position_ == current_statement_position; |
| 1440 | } |
| 1441 | |
| 1442 | // No step next action - don't continue. |
| 1443 | return false; |
| 1444 | } |
| 1445 | |
| 1446 | |
| 1447 | // Check whether the code object at the specified address is a debug break code |
| 1448 | // object. |
| 1449 | bool Debug::IsDebugBreak(Address addr) { |
| 1450 | Code* code = Code::GetCodeFromTargetAddress(addr); |
| 1451 | return code->ic_state() == DEBUG_BREAK; |
| 1452 | } |
| 1453 | |
| 1454 | |
| 1455 | // Check whether a code stub with the specified major key is a possible break |
| 1456 | // point location when looking for source break locations. |
| 1457 | bool Debug::IsSourceBreakStub(Code* code) { |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 1458 | CodeStub::Major major_key = CodeStub::GetMajorKey(code); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1459 | return major_key == CodeStub::CallFunction; |
| 1460 | } |
| 1461 | |
| 1462 | |
| 1463 | // Check whether a code stub with the specified major key is a possible break |
| 1464 | // location. |
| 1465 | bool Debug::IsBreakStub(Code* code) { |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 1466 | CodeStub::Major major_key = CodeStub::GetMajorKey(code); |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 1467 | return major_key == CodeStub::CallFunction; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1468 | } |
| 1469 | |
| 1470 | |
| 1471 | // Find the builtin to use for invoking the debug break |
| 1472 | Handle<Code> Debug::FindDebugBreak(Handle<Code> code, RelocInfo::Mode mode) { |
| 1473 | // Find the builtin debug break function matching the calling convention |
| 1474 | // used by the call site. |
| 1475 | if (code->is_inline_cache_stub()) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1476 | switch (code->kind()) { |
| 1477 | case Code::CALL_IC: |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 1478 | case Code::KEYED_CALL_IC: |
| 1479 | return ComputeCallDebugBreak(code->arguments_count(), code->kind()); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1480 | |
| 1481 | case Code::LOAD_IC: |
| 1482 | return Handle<Code>(Builtins::builtin(Builtins::LoadIC_DebugBreak)); |
| 1483 | |
| 1484 | case Code::STORE_IC: |
| 1485 | return Handle<Code>(Builtins::builtin(Builtins::StoreIC_DebugBreak)); |
| 1486 | |
| 1487 | case Code::KEYED_LOAD_IC: |
| 1488 | return Handle<Code>( |
| 1489 | Builtins::builtin(Builtins::KeyedLoadIC_DebugBreak)); |
| 1490 | |
| 1491 | case Code::KEYED_STORE_IC: |
| 1492 | return Handle<Code>( |
| 1493 | Builtins::builtin(Builtins::KeyedStoreIC_DebugBreak)); |
| 1494 | |
| 1495 | default: |
| 1496 | UNREACHABLE(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1497 | } |
| 1498 | } |
| 1499 | if (RelocInfo::IsConstructCall(mode)) { |
| 1500 | Handle<Code> result = |
| 1501 | Handle<Code>(Builtins::builtin(Builtins::ConstructCall_DebugBreak)); |
| 1502 | return result; |
| 1503 | } |
| 1504 | if (code->kind() == Code::STUB) { |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 1505 | ASSERT(code->major_key() == CodeStub::CallFunction); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1506 | Handle<Code> result = |
| 1507 | Handle<Code>(Builtins::builtin(Builtins::StubNoRegisters_DebugBreak)); |
| 1508 | return result; |
| 1509 | } |
| 1510 | |
| 1511 | UNREACHABLE(); |
| 1512 | return Handle<Code>::null(); |
| 1513 | } |
| 1514 | |
| 1515 | |
| 1516 | // Simple function for returning the source positions for active break points. |
| 1517 | Handle<Object> Debug::GetSourceBreakLocations( |
| 1518 | Handle<SharedFunctionInfo> shared) { |
| 1519 | if (!HasDebugInfo(shared)) return Handle<Object>(Heap::undefined_value()); |
| 1520 | Handle<DebugInfo> debug_info = GetDebugInfo(shared); |
| 1521 | if (debug_info->GetBreakPointCount() == 0) { |
| 1522 | return Handle<Object>(Heap::undefined_value()); |
| 1523 | } |
| 1524 | Handle<FixedArray> locations = |
| 1525 | Factory::NewFixedArray(debug_info->GetBreakPointCount()); |
| 1526 | int count = 0; |
| 1527 | for (int i = 0; i < debug_info->break_points()->length(); i++) { |
| 1528 | if (!debug_info->break_points()->get(i)->IsUndefined()) { |
| 1529 | BreakPointInfo* break_point_info = |
| 1530 | BreakPointInfo::cast(debug_info->break_points()->get(i)); |
| 1531 | if (break_point_info->GetBreakPointCount() > 0) { |
| 1532 | locations->set(count++, break_point_info->statement_position()); |
| 1533 | } |
| 1534 | } |
| 1535 | } |
| 1536 | return locations; |
| 1537 | } |
| 1538 | |
| 1539 | |
| 1540 | void Debug::NewBreak(StackFrame::Id break_frame_id) { |
| 1541 | thread_local_.break_frame_id_ = break_frame_id; |
| 1542 | thread_local_.break_id_ = ++thread_local_.break_count_; |
| 1543 | } |
| 1544 | |
| 1545 | |
| 1546 | void Debug::SetBreak(StackFrame::Id break_frame_id, int break_id) { |
| 1547 | thread_local_.break_frame_id_ = break_frame_id; |
| 1548 | thread_local_.break_id_ = break_id; |
| 1549 | } |
| 1550 | |
| 1551 | |
| 1552 | // Handle stepping into a function. |
| 1553 | void Debug::HandleStepIn(Handle<JSFunction> function, |
| 1554 | Handle<Object> holder, |
| 1555 | Address fp, |
| 1556 | bool is_constructor) { |
| 1557 | // If the frame pointer is not supplied by the caller find it. |
| 1558 | if (fp == 0) { |
| 1559 | StackFrameIterator it; |
| 1560 | it.Advance(); |
| 1561 | // For constructor functions skip another frame. |
| 1562 | if (is_constructor) { |
| 1563 | ASSERT(it.frame()->is_construct()); |
| 1564 | it.Advance(); |
| 1565 | } |
| 1566 | fp = it.frame()->fp(); |
| 1567 | } |
| 1568 | |
| 1569 | // Flood the function with one-shot break points if it is called from where |
| 1570 | // step into was requested. |
| 1571 | if (fp == Debug::step_in_fp()) { |
| 1572 | // Don't allow step into functions in the native context. |
| 1573 | if (!function->IsBuiltin()) { |
| 1574 | if (function->shared()->code() == |
| 1575 | Builtins::builtin(Builtins::FunctionApply) || |
| 1576 | function->shared()->code() == |
| 1577 | Builtins::builtin(Builtins::FunctionCall)) { |
| 1578 | // Handle function.apply and function.call separately to flood the |
| 1579 | // function to be called and not the code for Builtins::FunctionApply or |
| 1580 | // Builtins::FunctionCall. The receiver of call/apply is the target |
| 1581 | // function. |
| 1582 | if (!holder.is_null() && holder->IsJSFunction() && |
| 1583 | !JSFunction::cast(*holder)->IsBuiltin()) { |
| 1584 | Handle<SharedFunctionInfo> shared_info( |
| 1585 | JSFunction::cast(*holder)->shared()); |
| 1586 | Debug::FloodWithOneShot(shared_info); |
| 1587 | } |
| 1588 | } else { |
| 1589 | Debug::FloodWithOneShot(Handle<SharedFunctionInfo>(function->shared())); |
| 1590 | } |
| 1591 | } |
| 1592 | } |
| 1593 | } |
| 1594 | |
| 1595 | |
| 1596 | void Debug::ClearStepping() { |
| 1597 | // Clear the various stepping setup. |
| 1598 | ClearOneShot(); |
| 1599 | ClearStepIn(); |
| 1600 | ClearStepOut(); |
| 1601 | ClearStepNext(); |
| 1602 | |
| 1603 | // Clear multiple step counter. |
| 1604 | thread_local_.step_count_ = 0; |
| 1605 | } |
| 1606 | |
| 1607 | // Clears all the one-shot break points that are currently set. Normally this |
| 1608 | // function is called each time a break point is hit as one shot break points |
| 1609 | // are used to support stepping. |
| 1610 | void Debug::ClearOneShot() { |
| 1611 | // The current implementation just runs through all the breakpoints. When the |
| 1612 | // last break point for a function is removed that function is automatically |
| 1613 | // removed from the list. |
| 1614 | |
| 1615 | DebugInfoListNode* node = debug_info_list_; |
| 1616 | while (node != NULL) { |
| 1617 | BreakLocationIterator it(node->debug_info(), ALL_BREAK_LOCATIONS); |
| 1618 | while (!it.Done()) { |
| 1619 | it.ClearOneShot(); |
| 1620 | it.Next(); |
| 1621 | } |
| 1622 | node = node->next(); |
| 1623 | } |
| 1624 | } |
| 1625 | |
| 1626 | |
| 1627 | void Debug::ActivateStepIn(StackFrame* frame) { |
| 1628 | ASSERT(!StepOutActive()); |
| 1629 | thread_local_.step_into_fp_ = frame->fp(); |
| 1630 | } |
| 1631 | |
| 1632 | |
| 1633 | void Debug::ClearStepIn() { |
| 1634 | thread_local_.step_into_fp_ = 0; |
| 1635 | } |
| 1636 | |
| 1637 | |
| 1638 | void Debug::ActivateStepOut(StackFrame* frame) { |
| 1639 | ASSERT(!StepInActive()); |
| 1640 | thread_local_.step_out_fp_ = frame->fp(); |
| 1641 | } |
| 1642 | |
| 1643 | |
| 1644 | void Debug::ClearStepOut() { |
| 1645 | thread_local_.step_out_fp_ = 0; |
| 1646 | } |
| 1647 | |
| 1648 | |
| 1649 | void Debug::ClearStepNext() { |
| 1650 | thread_local_.last_step_action_ = StepNone; |
| 1651 | thread_local_.last_statement_position_ = RelocInfo::kNoPosition; |
| 1652 | thread_local_.last_fp_ = 0; |
| 1653 | } |
| 1654 | |
| 1655 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1656 | // Ensures the debug information is present for shared. |
| 1657 | bool Debug::EnsureDebugInfo(Handle<SharedFunctionInfo> shared) { |
| 1658 | // Return if we already have the debug info for shared. |
| 1659 | if (HasDebugInfo(shared)) return true; |
| 1660 | |
| 1661 | // Ensure shared in compiled. Return false if this failed. |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 1662 | if (!EnsureCompiled(shared, CLEAR_EXCEPTION)) return false; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1663 | |
| 1664 | // Create the debug info object. |
| 1665 | Handle<DebugInfo> debug_info = Factory::NewDebugInfo(shared); |
| 1666 | |
| 1667 | // Add debug info to the list. |
| 1668 | DebugInfoListNode* node = new DebugInfoListNode(*debug_info); |
| 1669 | node->set_next(debug_info_list_); |
| 1670 | debug_info_list_ = node; |
| 1671 | |
| 1672 | // Now there is at least one break point. |
| 1673 | has_break_points_ = true; |
| 1674 | |
| 1675 | return true; |
| 1676 | } |
| 1677 | |
| 1678 | |
| 1679 | void Debug::RemoveDebugInfo(Handle<DebugInfo> debug_info) { |
| 1680 | ASSERT(debug_info_list_ != NULL); |
| 1681 | // Run through the debug info objects to find this one and remove it. |
| 1682 | DebugInfoListNode* prev = NULL; |
| 1683 | DebugInfoListNode* current = debug_info_list_; |
| 1684 | while (current != NULL) { |
| 1685 | if (*current->debug_info() == *debug_info) { |
| 1686 | // Unlink from list. If prev is NULL we are looking at the first element. |
| 1687 | if (prev == NULL) { |
| 1688 | debug_info_list_ = current->next(); |
| 1689 | } else { |
| 1690 | prev->set_next(current->next()); |
| 1691 | } |
| 1692 | current->debug_info()->shared()->set_debug_info(Heap::undefined_value()); |
| 1693 | delete current; |
| 1694 | |
| 1695 | // If there are no more debug info objects there are not more break |
| 1696 | // points. |
| 1697 | has_break_points_ = debug_info_list_ != NULL; |
| 1698 | |
| 1699 | return; |
| 1700 | } |
| 1701 | // Move to next in list. |
| 1702 | prev = current; |
| 1703 | current = current->next(); |
| 1704 | } |
| 1705 | UNREACHABLE(); |
| 1706 | } |
| 1707 | |
| 1708 | |
| 1709 | void Debug::SetAfterBreakTarget(JavaScriptFrame* frame) { |
| 1710 | HandleScope scope; |
| 1711 | |
| 1712 | // Get the executing function in which the debug break occurred. |
| 1713 | Handle<SharedFunctionInfo> shared = |
| 1714 | Handle<SharedFunctionInfo>(JSFunction::cast(frame->function())->shared()); |
| 1715 | if (!EnsureDebugInfo(shared)) { |
| 1716 | // Return if we failed to retrieve the debug info. |
| 1717 | return; |
| 1718 | } |
| 1719 | Handle<DebugInfo> debug_info = GetDebugInfo(shared); |
| 1720 | Handle<Code> code(debug_info->code()); |
| 1721 | Handle<Code> original_code(debug_info->original_code()); |
| 1722 | #ifdef DEBUG |
| 1723 | // Get the code which is actually executing. |
| 1724 | Handle<Code> frame_code(frame->code()); |
| 1725 | ASSERT(frame_code.is_identical_to(code)); |
| 1726 | #endif |
| 1727 | |
| 1728 | // Find the call address in the running code. This address holds the call to |
| 1729 | // either a DebugBreakXXX or to the debug break return entry code if the |
| 1730 | // break point is still active after processing the break point. |
| 1731 | Address addr = frame->pc() - Assembler::kCallTargetAddressOffset; |
| 1732 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 1733 | // Check if the location is at JS exit or debug break slot. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1734 | bool at_js_return = false; |
| 1735 | bool break_at_js_return_active = false; |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 1736 | bool at_debug_break_slot = false; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1737 | RelocIterator it(debug_info->code()); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 1738 | while (!it.done() && !at_js_return && !at_debug_break_slot) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1739 | if (RelocInfo::IsJSReturn(it.rinfo()->rmode())) { |
| 1740 | at_js_return = (it.rinfo()->pc() == |
| 1741 | addr - Assembler::kPatchReturnSequenceAddressOffset); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 1742 | break_at_js_return_active = it.rinfo()->IsPatchedReturnSequence(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1743 | } |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 1744 | if (RelocInfo::IsDebugBreakSlot(it.rinfo()->rmode())) { |
| 1745 | at_debug_break_slot = (it.rinfo()->pc() == |
| 1746 | addr - Assembler::kPatchDebugBreakSlotAddressOffset); |
| 1747 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1748 | it.next(); |
| 1749 | } |
| 1750 | |
| 1751 | // Handle the jump to continue execution after break point depending on the |
| 1752 | // break location. |
| 1753 | if (at_js_return) { |
| 1754 | // If the break point as return is still active jump to the corresponding |
| 1755 | // place in the original code. If not the break point was removed during |
| 1756 | // break point processing. |
| 1757 | if (break_at_js_return_active) { |
| 1758 | addr += original_code->instruction_start() - code->instruction_start(); |
| 1759 | } |
| 1760 | |
| 1761 | // Move back to where the call instruction sequence started. |
| 1762 | thread_local_.after_break_target_ = |
| 1763 | addr - Assembler::kPatchReturnSequenceAddressOffset; |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 1764 | } else if (at_debug_break_slot) { |
| 1765 | // Address of where the debug break slot starts. |
| 1766 | addr = addr - Assembler::kPatchDebugBreakSlotAddressOffset; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1767 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 1768 | // Continue just after the slot. |
| 1769 | thread_local_.after_break_target_ = addr + Assembler::kDebugBreakSlotLength; |
| 1770 | } else if (IsDebugBreak(Assembler::target_address_at(addr))) { |
| 1771 | // We now know that there is still a debug break call at the target address, |
| 1772 | // so the break point is still there and the original code will hold the |
| 1773 | // address to jump to in order to complete the call which is replaced by a |
| 1774 | // call to DebugBreakXXX. |
| 1775 | |
| 1776 | // Find the corresponding address in the original code. |
| 1777 | addr += original_code->instruction_start() - code->instruction_start(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1778 | |
| 1779 | // Install jump to the call address in the original code. This will be the |
| 1780 | // call which was overwritten by the call to DebugBreakXXX. |
| 1781 | thread_local_.after_break_target_ = Assembler::target_address_at(addr); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 1782 | } else { |
| 1783 | // There is no longer a break point present. Don't try to look in the |
| 1784 | // original code as the running code will have the right address. This takes |
| 1785 | // care of the case where the last break point is removed from the function |
| 1786 | // and therefore no "original code" is available. |
| 1787 | thread_local_.after_break_target_ = Assembler::target_address_at(addr); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1788 | } |
| 1789 | } |
| 1790 | |
| 1791 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 1792 | bool Debug::IsBreakAtReturn(JavaScriptFrame* frame) { |
| 1793 | HandleScope scope; |
| 1794 | |
| 1795 | // Get the executing function in which the debug break occurred. |
| 1796 | Handle<SharedFunctionInfo> shared = |
| 1797 | Handle<SharedFunctionInfo>(JSFunction::cast(frame->function())->shared()); |
| 1798 | if (!EnsureDebugInfo(shared)) { |
| 1799 | // Return if we failed to retrieve the debug info. |
| 1800 | return false; |
| 1801 | } |
| 1802 | Handle<DebugInfo> debug_info = GetDebugInfo(shared); |
| 1803 | Handle<Code> code(debug_info->code()); |
| 1804 | #ifdef DEBUG |
| 1805 | // Get the code which is actually executing. |
| 1806 | Handle<Code> frame_code(frame->code()); |
| 1807 | ASSERT(frame_code.is_identical_to(code)); |
| 1808 | #endif |
| 1809 | |
| 1810 | // Find the call address in the running code. |
| 1811 | Address addr = frame->pc() - Assembler::kCallTargetAddressOffset; |
| 1812 | |
| 1813 | // Check if the location is at JS return. |
| 1814 | RelocIterator it(debug_info->code()); |
| 1815 | while (!it.done()) { |
| 1816 | if (RelocInfo::IsJSReturn(it.rinfo()->rmode())) { |
| 1817 | return (it.rinfo()->pc() == |
| 1818 | addr - Assembler::kPatchReturnSequenceAddressOffset); |
| 1819 | } |
| 1820 | it.next(); |
| 1821 | } |
| 1822 | return false; |
| 1823 | } |
| 1824 | |
| 1825 | |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 1826 | void Debug::FramesHaveBeenDropped(StackFrame::Id new_break_frame_id, |
Ben Murdoch | bb769b2 | 2010-08-11 14:56:33 +0100 | [diff] [blame] | 1827 | FrameDropMode mode, |
| 1828 | Object** restarter_frame_function_pointer) { |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 1829 | thread_local_.frame_drop_mode_ = mode; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1830 | thread_local_.break_frame_id_ = new_break_frame_id; |
Ben Murdoch | bb769b2 | 2010-08-11 14:56:33 +0100 | [diff] [blame] | 1831 | thread_local_.restarter_frame_function_pointer_ = |
| 1832 | restarter_frame_function_pointer; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1833 | } |
| 1834 | |
| 1835 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1836 | bool Debug::IsDebugGlobal(GlobalObject* global) { |
| 1837 | return IsLoaded() && global == Debug::debug_context()->global(); |
| 1838 | } |
| 1839 | |
| 1840 | |
| 1841 | void Debug::ClearMirrorCache() { |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 1842 | PostponeInterruptsScope postpone; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1843 | HandleScope scope; |
| 1844 | ASSERT(Top::context() == *Debug::debug_context()); |
| 1845 | |
| 1846 | // Clear the mirror cache. |
| 1847 | Handle<String> function_name = |
| 1848 | Factory::LookupSymbol(CStrVector("ClearMirrorCache")); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 1849 | Handle<Object> fun(Top::global()->GetPropertyNoExceptionThrown( |
| 1850 | *function_name)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1851 | ASSERT(fun->IsJSFunction()); |
| 1852 | bool caught_exception; |
| 1853 | Handle<Object> js_object = Execution::TryCall( |
| 1854 | Handle<JSFunction>::cast(fun), |
| 1855 | Handle<JSObject>(Debug::debug_context()->global()), |
| 1856 | 0, NULL, &caught_exception); |
| 1857 | } |
| 1858 | |
| 1859 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1860 | void Debug::CreateScriptCache() { |
| 1861 | HandleScope scope; |
| 1862 | |
| 1863 | // Perform two GCs to get rid of all unreferenced scripts. The first GC gets |
| 1864 | // rid of all the cached script wrappers and the second gets rid of the |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 1865 | // scripts which are no longer referenced. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1866 | Heap::CollectAllGarbage(false); |
| 1867 | Heap::CollectAllGarbage(false); |
| 1868 | |
| 1869 | ASSERT(script_cache_ == NULL); |
| 1870 | script_cache_ = new ScriptCache(); |
| 1871 | |
| 1872 | // Scan heap for Script objects. |
| 1873 | int count = 0; |
| 1874 | HeapIterator iterator; |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 1875 | for (HeapObject* obj = iterator.next(); obj != NULL; obj = iterator.next()) { |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 1876 | if (obj->IsScript() && Script::cast(obj)->HasValidSource()) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1877 | script_cache_->Add(Handle<Script>(Script::cast(obj))); |
| 1878 | count++; |
| 1879 | } |
| 1880 | } |
| 1881 | } |
| 1882 | |
| 1883 | |
| 1884 | void Debug::DestroyScriptCache() { |
| 1885 | // Get rid of the script cache if it was created. |
| 1886 | if (script_cache_ != NULL) { |
| 1887 | delete script_cache_; |
| 1888 | script_cache_ = NULL; |
| 1889 | } |
| 1890 | } |
| 1891 | |
| 1892 | |
| 1893 | void Debug::AddScriptToScriptCache(Handle<Script> script) { |
| 1894 | if (script_cache_ != NULL) { |
| 1895 | script_cache_->Add(script); |
| 1896 | } |
| 1897 | } |
| 1898 | |
| 1899 | |
| 1900 | Handle<FixedArray> Debug::GetLoadedScripts() { |
| 1901 | // Create and fill the script cache when the loaded scripts is requested for |
| 1902 | // the first time. |
| 1903 | if (script_cache_ == NULL) { |
| 1904 | CreateScriptCache(); |
| 1905 | } |
| 1906 | |
| 1907 | // If the script cache is not active just return an empty array. |
| 1908 | ASSERT(script_cache_ != NULL); |
| 1909 | if (script_cache_ == NULL) { |
| 1910 | Factory::NewFixedArray(0); |
| 1911 | } |
| 1912 | |
| 1913 | // Perform GC to get unreferenced scripts evicted from the cache before |
| 1914 | // returning the content. |
| 1915 | Heap::CollectAllGarbage(false); |
| 1916 | |
| 1917 | // Get the scripts from the cache. |
| 1918 | return script_cache_->GetScripts(); |
| 1919 | } |
| 1920 | |
| 1921 | |
| 1922 | void Debug::AfterGarbageCollection() { |
| 1923 | // Generate events for collected scripts. |
| 1924 | if (script_cache_ != NULL) { |
| 1925 | script_cache_->ProcessCollectedScripts(); |
| 1926 | } |
| 1927 | } |
| 1928 | |
| 1929 | |
| 1930 | Mutex* Debugger::debugger_access_ = OS::CreateMutex(); |
| 1931 | Handle<Object> Debugger::event_listener_ = Handle<Object>(); |
| 1932 | Handle<Object> Debugger::event_listener_data_ = Handle<Object>(); |
| 1933 | bool Debugger::compiling_natives_ = false; |
| 1934 | bool Debugger::is_loading_debugger_ = false; |
| 1935 | bool Debugger::never_unload_debugger_ = false; |
| 1936 | v8::Debug::MessageHandler2 Debugger::message_handler_ = NULL; |
| 1937 | bool Debugger::debugger_unload_pending_ = false; |
| 1938 | v8::Debug::HostDispatchHandler Debugger::host_dispatch_handler_ = NULL; |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 1939 | Mutex* Debugger::dispatch_handler_access_ = OS::CreateMutex(); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 1940 | v8::Debug::DebugMessageDispatchHandler |
| 1941 | Debugger::debug_message_dispatch_handler_ = NULL; |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 1942 | MessageDispatchHelperThread* Debugger::message_dispatch_helper_thread_ = NULL; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1943 | int Debugger::host_dispatch_micros_ = 100 * 1000; |
| 1944 | DebuggerAgent* Debugger::agent_ = NULL; |
| 1945 | LockingCommandMessageQueue Debugger::command_queue_(kQueueInitialSize); |
| 1946 | Semaphore* Debugger::command_received_ = OS::CreateSemaphore(0); |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 1947 | LockingCommandMessageQueue Debugger::event_command_queue_(kQueueInitialSize); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1948 | |
| 1949 | |
| 1950 | Handle<Object> Debugger::MakeJSObject(Vector<const char> constructor_name, |
| 1951 | int argc, Object*** argv, |
| 1952 | bool* caught_exception) { |
| 1953 | ASSERT(Top::context() == *Debug::debug_context()); |
| 1954 | |
| 1955 | // Create the execution state object. |
| 1956 | Handle<String> constructor_str = Factory::LookupSymbol(constructor_name); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 1957 | Handle<Object> constructor(Top::global()->GetPropertyNoExceptionThrown( |
| 1958 | *constructor_str)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1959 | ASSERT(constructor->IsJSFunction()); |
| 1960 | if (!constructor->IsJSFunction()) { |
| 1961 | *caught_exception = true; |
| 1962 | return Factory::undefined_value(); |
| 1963 | } |
| 1964 | Handle<Object> js_object = Execution::TryCall( |
| 1965 | Handle<JSFunction>::cast(constructor), |
| 1966 | Handle<JSObject>(Debug::debug_context()->global()), argc, argv, |
| 1967 | caught_exception); |
| 1968 | return js_object; |
| 1969 | } |
| 1970 | |
| 1971 | |
| 1972 | Handle<Object> Debugger::MakeExecutionState(bool* caught_exception) { |
| 1973 | // Create the execution state object. |
| 1974 | Handle<Object> break_id = Factory::NewNumberFromInt(Debug::break_id()); |
| 1975 | const int argc = 1; |
| 1976 | Object** argv[argc] = { break_id.location() }; |
| 1977 | return MakeJSObject(CStrVector("MakeExecutionState"), |
| 1978 | argc, argv, caught_exception); |
| 1979 | } |
| 1980 | |
| 1981 | |
| 1982 | Handle<Object> Debugger::MakeBreakEvent(Handle<Object> exec_state, |
| 1983 | Handle<Object> break_points_hit, |
| 1984 | bool* caught_exception) { |
| 1985 | // Create the new break event object. |
| 1986 | const int argc = 2; |
| 1987 | Object** argv[argc] = { exec_state.location(), |
| 1988 | break_points_hit.location() }; |
| 1989 | return MakeJSObject(CStrVector("MakeBreakEvent"), |
| 1990 | argc, |
| 1991 | argv, |
| 1992 | caught_exception); |
| 1993 | } |
| 1994 | |
| 1995 | |
| 1996 | Handle<Object> Debugger::MakeExceptionEvent(Handle<Object> exec_state, |
| 1997 | Handle<Object> exception, |
| 1998 | bool uncaught, |
| 1999 | bool* caught_exception) { |
| 2000 | // Create the new exception event object. |
| 2001 | const int argc = 3; |
| 2002 | Object** argv[argc] = { exec_state.location(), |
| 2003 | exception.location(), |
| 2004 | uncaught ? Factory::true_value().location() : |
| 2005 | Factory::false_value().location()}; |
| 2006 | return MakeJSObject(CStrVector("MakeExceptionEvent"), |
| 2007 | argc, argv, caught_exception); |
| 2008 | } |
| 2009 | |
| 2010 | |
| 2011 | Handle<Object> Debugger::MakeNewFunctionEvent(Handle<Object> function, |
| 2012 | bool* caught_exception) { |
| 2013 | // Create the new function event object. |
| 2014 | const int argc = 1; |
| 2015 | Object** argv[argc] = { function.location() }; |
| 2016 | return MakeJSObject(CStrVector("MakeNewFunctionEvent"), |
| 2017 | argc, argv, caught_exception); |
| 2018 | } |
| 2019 | |
| 2020 | |
| 2021 | Handle<Object> Debugger::MakeCompileEvent(Handle<Script> script, |
| 2022 | bool before, |
| 2023 | bool* caught_exception) { |
| 2024 | // Create the compile event object. |
| 2025 | Handle<Object> exec_state = MakeExecutionState(caught_exception); |
| 2026 | Handle<Object> script_wrapper = GetScriptWrapper(script); |
| 2027 | const int argc = 3; |
| 2028 | Object** argv[argc] = { exec_state.location(), |
| 2029 | script_wrapper.location(), |
| 2030 | before ? Factory::true_value().location() : |
| 2031 | Factory::false_value().location() }; |
| 2032 | |
| 2033 | return MakeJSObject(CStrVector("MakeCompileEvent"), |
| 2034 | argc, |
| 2035 | argv, |
| 2036 | caught_exception); |
| 2037 | } |
| 2038 | |
| 2039 | |
| 2040 | Handle<Object> Debugger::MakeScriptCollectedEvent(int id, |
| 2041 | bool* caught_exception) { |
| 2042 | // Create the script collected event object. |
| 2043 | Handle<Object> exec_state = MakeExecutionState(caught_exception); |
| 2044 | Handle<Object> id_object = Handle<Smi>(Smi::FromInt(id)); |
| 2045 | const int argc = 2; |
| 2046 | Object** argv[argc] = { exec_state.location(), id_object.location() }; |
| 2047 | |
| 2048 | return MakeJSObject(CStrVector("MakeScriptCollectedEvent"), |
| 2049 | argc, |
| 2050 | argv, |
| 2051 | caught_exception); |
| 2052 | } |
| 2053 | |
| 2054 | |
| 2055 | void Debugger::OnException(Handle<Object> exception, bool uncaught) { |
| 2056 | HandleScope scope; |
| 2057 | |
| 2058 | // Bail out based on state or if there is no listener for this event |
| 2059 | if (Debug::InDebugger()) return; |
| 2060 | if (!Debugger::EventActive(v8::Exception)) return; |
| 2061 | |
| 2062 | // Bail out if exception breaks are not active |
| 2063 | if (uncaught) { |
| 2064 | // Uncaught exceptions are reported by either flags. |
| 2065 | if (!(Debug::break_on_uncaught_exception() || |
| 2066 | Debug::break_on_exception())) return; |
| 2067 | } else { |
| 2068 | // Caught exceptions are reported is activated. |
| 2069 | if (!Debug::break_on_exception()) return; |
| 2070 | } |
| 2071 | |
| 2072 | // Enter the debugger. |
| 2073 | EnterDebugger debugger; |
| 2074 | if (debugger.FailedToEnter()) return; |
| 2075 | |
| 2076 | // Clear all current stepping setup. |
| 2077 | Debug::ClearStepping(); |
| 2078 | // Create the event data object. |
| 2079 | bool caught_exception = false; |
| 2080 | Handle<Object> exec_state = MakeExecutionState(&caught_exception); |
| 2081 | Handle<Object> event_data; |
| 2082 | if (!caught_exception) { |
| 2083 | event_data = MakeExceptionEvent(exec_state, exception, uncaught, |
| 2084 | &caught_exception); |
| 2085 | } |
| 2086 | // Bail out and don't call debugger if exception. |
| 2087 | if (caught_exception) { |
| 2088 | return; |
| 2089 | } |
| 2090 | |
| 2091 | // Process debug event. |
| 2092 | ProcessDebugEvent(v8::Exception, Handle<JSObject>::cast(event_data), false); |
| 2093 | // Return to continue execution from where the exception was thrown. |
| 2094 | } |
| 2095 | |
| 2096 | |
| 2097 | void Debugger::OnDebugBreak(Handle<Object> break_points_hit, |
| 2098 | bool auto_continue) { |
| 2099 | HandleScope scope; |
| 2100 | |
| 2101 | // Debugger has already been entered by caller. |
| 2102 | ASSERT(Top::context() == *Debug::debug_context()); |
| 2103 | |
| 2104 | // Bail out if there is no listener for this event |
| 2105 | if (!Debugger::EventActive(v8::Break)) return; |
| 2106 | |
| 2107 | // Debugger must be entered in advance. |
| 2108 | ASSERT(Top::context() == *Debug::debug_context()); |
| 2109 | |
| 2110 | // Create the event data object. |
| 2111 | bool caught_exception = false; |
| 2112 | Handle<Object> exec_state = MakeExecutionState(&caught_exception); |
| 2113 | Handle<Object> event_data; |
| 2114 | if (!caught_exception) { |
| 2115 | event_data = MakeBreakEvent(exec_state, break_points_hit, |
| 2116 | &caught_exception); |
| 2117 | } |
| 2118 | // Bail out and don't call debugger if exception. |
| 2119 | if (caught_exception) { |
| 2120 | return; |
| 2121 | } |
| 2122 | |
| 2123 | // Process debug event. |
| 2124 | ProcessDebugEvent(v8::Break, |
| 2125 | Handle<JSObject>::cast(event_data), |
| 2126 | auto_continue); |
| 2127 | } |
| 2128 | |
| 2129 | |
| 2130 | void Debugger::OnBeforeCompile(Handle<Script> script) { |
| 2131 | HandleScope scope; |
| 2132 | |
| 2133 | // Bail out based on state or if there is no listener for this event |
| 2134 | if (Debug::InDebugger()) return; |
| 2135 | if (compiling_natives()) return; |
| 2136 | if (!EventActive(v8::BeforeCompile)) return; |
| 2137 | |
| 2138 | // Enter the debugger. |
| 2139 | EnterDebugger debugger; |
| 2140 | if (debugger.FailedToEnter()) return; |
| 2141 | |
| 2142 | // Create the event data object. |
| 2143 | bool caught_exception = false; |
| 2144 | Handle<Object> event_data = MakeCompileEvent(script, true, &caught_exception); |
| 2145 | // Bail out and don't call debugger if exception. |
| 2146 | if (caught_exception) { |
| 2147 | return; |
| 2148 | } |
| 2149 | |
| 2150 | // Process debug event. |
| 2151 | ProcessDebugEvent(v8::BeforeCompile, |
| 2152 | Handle<JSObject>::cast(event_data), |
| 2153 | true); |
| 2154 | } |
| 2155 | |
| 2156 | |
| 2157 | // Handle debugger actions when a new script is compiled. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2158 | void Debugger::OnAfterCompile(Handle<Script> script, |
| 2159 | AfterCompileFlags after_compile_flags) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2160 | HandleScope scope; |
| 2161 | |
| 2162 | // Add the newly compiled script to the script cache. |
| 2163 | Debug::AddScriptToScriptCache(script); |
| 2164 | |
| 2165 | // No more to do if not debugging. |
| 2166 | if (!IsDebuggerActive()) return; |
| 2167 | |
| 2168 | // No compile events while compiling natives. |
| 2169 | if (compiling_natives()) return; |
| 2170 | |
| 2171 | // Store whether in debugger before entering debugger. |
| 2172 | bool in_debugger = Debug::InDebugger(); |
| 2173 | |
| 2174 | // Enter the debugger. |
| 2175 | EnterDebugger debugger; |
| 2176 | if (debugger.FailedToEnter()) return; |
| 2177 | |
| 2178 | // If debugging there might be script break points registered for this |
| 2179 | // script. Make sure that these break points are set. |
| 2180 | |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 2181 | // Get the function UpdateScriptBreakPoints (defined in debug-debugger.js). |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 2182 | Handle<String> update_script_break_points_symbol = |
| 2183 | Factory::LookupAsciiSymbol("UpdateScriptBreakPoints"); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2184 | Handle<Object> update_script_break_points = |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2185 | Handle<Object>(Debug::debug_context()->global()-> |
| 2186 | GetPropertyNoExceptionThrown(*update_script_break_points_symbol)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2187 | if (!update_script_break_points->IsJSFunction()) { |
| 2188 | return; |
| 2189 | } |
| 2190 | ASSERT(update_script_break_points->IsJSFunction()); |
| 2191 | |
| 2192 | // Wrap the script object in a proper JS object before passing it |
| 2193 | // to JavaScript. |
| 2194 | Handle<JSValue> wrapper = GetScriptWrapper(script); |
| 2195 | |
| 2196 | // Call UpdateScriptBreakPoints expect no exceptions. |
| 2197 | bool caught_exception = false; |
| 2198 | const int argc = 1; |
| 2199 | Object** argv[argc] = { reinterpret_cast<Object**>(wrapper.location()) }; |
| 2200 | Handle<Object> result = Execution::TryCall( |
| 2201 | Handle<JSFunction>::cast(update_script_break_points), |
| 2202 | Top::builtins(), argc, argv, |
| 2203 | &caught_exception); |
| 2204 | if (caught_exception) { |
| 2205 | return; |
| 2206 | } |
| 2207 | // Bail out based on state or if there is no listener for this event |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2208 | if (in_debugger && (after_compile_flags & SEND_WHEN_DEBUGGING) == 0) return; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2209 | if (!Debugger::EventActive(v8::AfterCompile)) return; |
| 2210 | |
| 2211 | // Create the compile state object. |
| 2212 | Handle<Object> event_data = MakeCompileEvent(script, |
| 2213 | false, |
| 2214 | &caught_exception); |
| 2215 | // Bail out and don't call debugger if exception. |
| 2216 | if (caught_exception) { |
| 2217 | return; |
| 2218 | } |
| 2219 | // Process debug event. |
| 2220 | ProcessDebugEvent(v8::AfterCompile, |
| 2221 | Handle<JSObject>::cast(event_data), |
| 2222 | true); |
| 2223 | } |
| 2224 | |
| 2225 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2226 | void Debugger::OnScriptCollected(int id) { |
| 2227 | HandleScope scope; |
| 2228 | |
| 2229 | // No more to do if not debugging. |
| 2230 | if (!IsDebuggerActive()) return; |
| 2231 | if (!Debugger::EventActive(v8::ScriptCollected)) return; |
| 2232 | |
| 2233 | // Enter the debugger. |
| 2234 | EnterDebugger debugger; |
| 2235 | if (debugger.FailedToEnter()) return; |
| 2236 | |
| 2237 | // Create the script collected state object. |
| 2238 | bool caught_exception = false; |
| 2239 | Handle<Object> event_data = MakeScriptCollectedEvent(id, |
| 2240 | &caught_exception); |
| 2241 | // Bail out and don't call debugger if exception. |
| 2242 | if (caught_exception) { |
| 2243 | return; |
| 2244 | } |
| 2245 | |
| 2246 | // Process debug event. |
| 2247 | ProcessDebugEvent(v8::ScriptCollected, |
| 2248 | Handle<JSObject>::cast(event_data), |
| 2249 | true); |
| 2250 | } |
| 2251 | |
| 2252 | |
| 2253 | void Debugger::ProcessDebugEvent(v8::DebugEvent event, |
| 2254 | Handle<JSObject> event_data, |
| 2255 | bool auto_continue) { |
| 2256 | HandleScope scope; |
| 2257 | |
| 2258 | // Clear any pending debug break if this is a real break. |
| 2259 | if (!auto_continue) { |
| 2260 | Debug::clear_interrupt_pending(DEBUGBREAK); |
| 2261 | } |
| 2262 | |
| 2263 | // Create the execution state. |
| 2264 | bool caught_exception = false; |
| 2265 | Handle<Object> exec_state = MakeExecutionState(&caught_exception); |
| 2266 | if (caught_exception) { |
| 2267 | return; |
| 2268 | } |
| 2269 | // First notify the message handler if any. |
| 2270 | if (message_handler_ != NULL) { |
| 2271 | NotifyMessageHandler(event, |
| 2272 | Handle<JSObject>::cast(exec_state), |
| 2273 | event_data, |
| 2274 | auto_continue); |
| 2275 | } |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 2276 | // Notify registered debug event listener. This can be either a C or |
| 2277 | // a JavaScript function. Don't call event listener for v8::Break |
| 2278 | // here, if it's only a debug command -- they will be processed later. |
| 2279 | if ((event != v8::Break || !auto_continue) && !event_listener_.is_null()) { |
| 2280 | CallEventCallback(event, exec_state, event_data, NULL); |
| 2281 | } |
| 2282 | // Process pending debug commands. |
| 2283 | if (event == v8::Break) { |
| 2284 | while (!event_command_queue_.IsEmpty()) { |
| 2285 | CommandMessage command = event_command_queue_.Get(); |
| 2286 | if (!event_listener_.is_null()) { |
| 2287 | CallEventCallback(v8::BreakForCommand, |
| 2288 | exec_state, |
| 2289 | event_data, |
| 2290 | command.client_data()); |
| 2291 | } |
| 2292 | command.Dispose(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2293 | } |
| 2294 | } |
| 2295 | } |
| 2296 | |
| 2297 | |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 2298 | void Debugger::CallEventCallback(v8::DebugEvent event, |
| 2299 | Handle<Object> exec_state, |
| 2300 | Handle<Object> event_data, |
| 2301 | v8::Debug::ClientData* client_data) { |
| 2302 | if (event_listener_->IsProxy()) { |
| 2303 | CallCEventCallback(event, exec_state, event_data, client_data); |
| 2304 | } else { |
| 2305 | CallJSEventCallback(event, exec_state, event_data); |
| 2306 | } |
| 2307 | } |
| 2308 | |
| 2309 | |
| 2310 | void Debugger::CallCEventCallback(v8::DebugEvent event, |
| 2311 | Handle<Object> exec_state, |
| 2312 | Handle<Object> event_data, |
| 2313 | v8::Debug::ClientData* client_data) { |
| 2314 | Handle<Proxy> callback_obj(Handle<Proxy>::cast(event_listener_)); |
| 2315 | v8::Debug::EventCallback2 callback = |
| 2316 | FUNCTION_CAST<v8::Debug::EventCallback2>(callback_obj->proxy()); |
| 2317 | EventDetailsImpl event_details( |
| 2318 | event, |
| 2319 | Handle<JSObject>::cast(exec_state), |
| 2320 | Handle<JSObject>::cast(event_data), |
| 2321 | event_listener_data_, |
| 2322 | client_data); |
| 2323 | callback(event_details); |
| 2324 | } |
| 2325 | |
| 2326 | |
| 2327 | void Debugger::CallJSEventCallback(v8::DebugEvent event, |
| 2328 | Handle<Object> exec_state, |
| 2329 | Handle<Object> event_data) { |
| 2330 | ASSERT(event_listener_->IsJSFunction()); |
| 2331 | Handle<JSFunction> fun(Handle<JSFunction>::cast(event_listener_)); |
| 2332 | |
| 2333 | // Invoke the JavaScript debug event listener. |
| 2334 | const int argc = 4; |
| 2335 | Object** argv[argc] = { Handle<Object>(Smi::FromInt(event)).location(), |
| 2336 | exec_state.location(), |
| 2337 | Handle<Object>::cast(event_data).location(), |
| 2338 | event_listener_data_.location() }; |
| 2339 | bool caught_exception = false; |
| 2340 | Execution::TryCall(fun, Top::global(), argc, argv, &caught_exception); |
| 2341 | // Silently ignore exceptions from debug event listeners. |
| 2342 | } |
| 2343 | |
| 2344 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2345 | Handle<Context> Debugger::GetDebugContext() { |
| 2346 | never_unload_debugger_ = true; |
| 2347 | EnterDebugger debugger; |
| 2348 | return Debug::debug_context(); |
| 2349 | } |
| 2350 | |
| 2351 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2352 | void Debugger::UnloadDebugger() { |
| 2353 | // Make sure that there are no breakpoints left. |
| 2354 | Debug::ClearAllBreakPoints(); |
| 2355 | |
| 2356 | // Unload the debugger if feasible. |
| 2357 | if (!never_unload_debugger_) { |
| 2358 | Debug::Unload(); |
| 2359 | } |
| 2360 | |
| 2361 | // Clear the flag indicating that the debugger should be unloaded. |
| 2362 | debugger_unload_pending_ = false; |
| 2363 | } |
| 2364 | |
| 2365 | |
| 2366 | void Debugger::NotifyMessageHandler(v8::DebugEvent event, |
| 2367 | Handle<JSObject> exec_state, |
| 2368 | Handle<JSObject> event_data, |
| 2369 | bool auto_continue) { |
| 2370 | HandleScope scope; |
| 2371 | |
| 2372 | if (!Debug::Load()) return; |
| 2373 | |
| 2374 | // Process the individual events. |
| 2375 | bool sendEventMessage = false; |
| 2376 | switch (event) { |
| 2377 | case v8::Break: |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 2378 | case v8::BreakForCommand: |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2379 | sendEventMessage = !auto_continue; |
| 2380 | break; |
| 2381 | case v8::Exception: |
| 2382 | sendEventMessage = true; |
| 2383 | break; |
| 2384 | case v8::BeforeCompile: |
| 2385 | break; |
| 2386 | case v8::AfterCompile: |
| 2387 | sendEventMessage = true; |
| 2388 | break; |
| 2389 | case v8::ScriptCollected: |
| 2390 | sendEventMessage = true; |
| 2391 | break; |
| 2392 | case v8::NewFunction: |
| 2393 | break; |
| 2394 | default: |
| 2395 | UNREACHABLE(); |
| 2396 | } |
| 2397 | |
| 2398 | // The debug command interrupt flag might have been set when the command was |
| 2399 | // added. It should be enough to clear the flag only once while we are in the |
| 2400 | // debugger. |
| 2401 | ASSERT(Debug::InDebugger()); |
| 2402 | StackGuard::Continue(DEBUGCOMMAND); |
| 2403 | |
| 2404 | // Notify the debugger that a debug event has occurred unless auto continue is |
| 2405 | // active in which case no event is send. |
| 2406 | if (sendEventMessage) { |
| 2407 | MessageImpl message = MessageImpl::NewEvent( |
| 2408 | event, |
| 2409 | auto_continue, |
| 2410 | Handle<JSObject>::cast(exec_state), |
| 2411 | Handle<JSObject>::cast(event_data)); |
| 2412 | InvokeMessageHandler(message); |
| 2413 | } |
| 2414 | |
| 2415 | // If auto continue don't make the event cause a break, but process messages |
| 2416 | // in the queue if any. For script collected events don't even process |
| 2417 | // messages in the queue as the execution state might not be what is expected |
| 2418 | // by the client. |
| 2419 | if ((auto_continue && !HasCommands()) || event == v8::ScriptCollected) { |
| 2420 | return; |
| 2421 | } |
| 2422 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2423 | v8::TryCatch try_catch; |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 2424 | |
| 2425 | // DebugCommandProcessor goes here. |
| 2426 | v8::Local<v8::Object> cmd_processor; |
| 2427 | { |
| 2428 | v8::Local<v8::Object> api_exec_state = |
| 2429 | v8::Utils::ToLocal(Handle<JSObject>::cast(exec_state)); |
| 2430 | v8::Local<v8::String> fun_name = |
| 2431 | v8::String::New("debugCommandProcessor"); |
| 2432 | v8::Local<v8::Function> fun = |
| 2433 | v8::Function::Cast(*api_exec_state->Get(fun_name)); |
| 2434 | |
| 2435 | v8::Handle<v8::Boolean> running = |
| 2436 | auto_continue ? v8::True() : v8::False(); |
| 2437 | static const int kArgc = 1; |
| 2438 | v8::Handle<Value> argv[kArgc] = { running }; |
| 2439 | cmd_processor = v8::Object::Cast(*fun->Call(api_exec_state, kArgc, argv)); |
| 2440 | if (try_catch.HasCaught()) { |
| 2441 | PrintLn(try_catch.Exception()); |
| 2442 | return; |
| 2443 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2444 | } |
| 2445 | |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 2446 | bool running = auto_continue; |
| 2447 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2448 | // Process requests from the debugger. |
| 2449 | while (true) { |
| 2450 | // Wait for new command in the queue. |
| 2451 | if (Debugger::host_dispatch_handler_) { |
| 2452 | // In case there is a host dispatch - do periodic dispatches. |
| 2453 | if (!command_received_->Wait(host_dispatch_micros_)) { |
| 2454 | // Timout expired, do the dispatch. |
| 2455 | Debugger::host_dispatch_handler_(); |
| 2456 | continue; |
| 2457 | } |
| 2458 | } else { |
| 2459 | // In case there is no host dispatch - just wait. |
| 2460 | command_received_->Wait(); |
| 2461 | } |
| 2462 | |
| 2463 | // Get the command from the queue. |
| 2464 | CommandMessage command = command_queue_.Get(); |
| 2465 | Logger::DebugTag("Got request from command queue, in interactive loop."); |
| 2466 | if (!Debugger::IsDebuggerActive()) { |
| 2467 | // Delete command text and user data. |
| 2468 | command.Dispose(); |
| 2469 | return; |
| 2470 | } |
| 2471 | |
| 2472 | // Invoke JavaScript to process the debug request. |
| 2473 | v8::Local<v8::String> fun_name; |
| 2474 | v8::Local<v8::Function> fun; |
| 2475 | v8::Local<v8::Value> request; |
| 2476 | v8::TryCatch try_catch; |
| 2477 | fun_name = v8::String::New("processDebugRequest"); |
| 2478 | fun = v8::Function::Cast(*cmd_processor->Get(fun_name)); |
| 2479 | |
| 2480 | request = v8::String::New(command.text().start(), |
| 2481 | command.text().length()); |
| 2482 | static const int kArgc = 1; |
| 2483 | v8::Handle<Value> argv[kArgc] = { request }; |
| 2484 | v8::Local<v8::Value> response_val = fun->Call(cmd_processor, kArgc, argv); |
| 2485 | |
| 2486 | // Get the response. |
| 2487 | v8::Local<v8::String> response; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2488 | if (!try_catch.HasCaught()) { |
| 2489 | // Get response string. |
| 2490 | if (!response_val->IsUndefined()) { |
| 2491 | response = v8::String::Cast(*response_val); |
| 2492 | } else { |
| 2493 | response = v8::String::New(""); |
| 2494 | } |
| 2495 | |
| 2496 | // Log the JSON request/response. |
| 2497 | if (FLAG_trace_debug_json) { |
| 2498 | PrintLn(request); |
| 2499 | PrintLn(response); |
| 2500 | } |
| 2501 | |
| 2502 | // Get the running state. |
| 2503 | fun_name = v8::String::New("isRunning"); |
| 2504 | fun = v8::Function::Cast(*cmd_processor->Get(fun_name)); |
| 2505 | static const int kArgc = 1; |
| 2506 | v8::Handle<Value> argv[kArgc] = { response }; |
| 2507 | v8::Local<v8::Value> running_val = fun->Call(cmd_processor, kArgc, argv); |
| 2508 | if (!try_catch.HasCaught()) { |
| 2509 | running = running_val->ToBoolean()->Value(); |
| 2510 | } |
| 2511 | } else { |
| 2512 | // In case of failure the result text is the exception text. |
| 2513 | response = try_catch.Exception()->ToString(); |
| 2514 | } |
| 2515 | |
| 2516 | // Return the result. |
| 2517 | MessageImpl message = MessageImpl::NewResponse( |
| 2518 | event, |
| 2519 | running, |
| 2520 | Handle<JSObject>::cast(exec_state), |
| 2521 | Handle<JSObject>::cast(event_data), |
| 2522 | Handle<String>(Utils::OpenHandle(*response)), |
| 2523 | command.client_data()); |
| 2524 | InvokeMessageHandler(message); |
| 2525 | command.Dispose(); |
| 2526 | |
| 2527 | // Return from debug event processing if either the VM is put into the |
| 2528 | // runnning state (through a continue command) or auto continue is active |
| 2529 | // and there are no more commands queued. |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 2530 | if (running && !HasCommands()) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2531 | return; |
| 2532 | } |
| 2533 | } |
| 2534 | } |
| 2535 | |
| 2536 | |
| 2537 | void Debugger::SetEventListener(Handle<Object> callback, |
| 2538 | Handle<Object> data) { |
| 2539 | HandleScope scope; |
| 2540 | |
| 2541 | // Clear the global handles for the event listener and the event listener data |
| 2542 | // object. |
| 2543 | if (!event_listener_.is_null()) { |
| 2544 | GlobalHandles::Destroy( |
| 2545 | reinterpret_cast<Object**>(event_listener_.location())); |
| 2546 | event_listener_ = Handle<Object>(); |
| 2547 | } |
| 2548 | if (!event_listener_data_.is_null()) { |
| 2549 | GlobalHandles::Destroy( |
| 2550 | reinterpret_cast<Object**>(event_listener_data_.location())); |
| 2551 | event_listener_data_ = Handle<Object>(); |
| 2552 | } |
| 2553 | |
| 2554 | // If there is a new debug event listener register it together with its data |
| 2555 | // object. |
| 2556 | if (!callback->IsUndefined() && !callback->IsNull()) { |
| 2557 | event_listener_ = Handle<Object>::cast(GlobalHandles::Create(*callback)); |
| 2558 | if (data.is_null()) { |
| 2559 | data = Factory::undefined_value(); |
| 2560 | } |
| 2561 | event_listener_data_ = Handle<Object>::cast(GlobalHandles::Create(*data)); |
| 2562 | } |
| 2563 | |
| 2564 | ListenersChanged(); |
| 2565 | } |
| 2566 | |
| 2567 | |
| 2568 | void Debugger::SetMessageHandler(v8::Debug::MessageHandler2 handler) { |
| 2569 | ScopedLock with(debugger_access_); |
| 2570 | |
| 2571 | message_handler_ = handler; |
| 2572 | ListenersChanged(); |
| 2573 | if (handler == NULL) { |
| 2574 | // Send an empty command to the debugger if in a break to make JavaScript |
| 2575 | // run again if the debugger is closed. |
| 2576 | if (Debug::InDebugger()) { |
| 2577 | ProcessCommand(Vector<const uint16_t>::empty()); |
| 2578 | } |
| 2579 | } |
| 2580 | } |
| 2581 | |
| 2582 | |
| 2583 | void Debugger::ListenersChanged() { |
| 2584 | if (IsDebuggerActive()) { |
| 2585 | // Disable the compilation cache when the debugger is active. |
| 2586 | CompilationCache::Disable(); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 2587 | debugger_unload_pending_ = false; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2588 | } else { |
| 2589 | CompilationCache::Enable(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2590 | // Unload the debugger if event listener and message handler cleared. |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 2591 | // Schedule this for later, because we may be in non-V8 thread. |
| 2592 | debugger_unload_pending_ = true; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2593 | } |
| 2594 | } |
| 2595 | |
| 2596 | |
| 2597 | void Debugger::SetHostDispatchHandler(v8::Debug::HostDispatchHandler handler, |
| 2598 | int period) { |
| 2599 | host_dispatch_handler_ = handler; |
| 2600 | host_dispatch_micros_ = period * 1000; |
| 2601 | } |
| 2602 | |
| 2603 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 2604 | void Debugger::SetDebugMessageDispatchHandler( |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 2605 | v8::Debug::DebugMessageDispatchHandler handler, bool provide_locker) { |
| 2606 | ScopedLock with(dispatch_handler_access_); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 2607 | debug_message_dispatch_handler_ = handler; |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 2608 | |
| 2609 | if (provide_locker && message_dispatch_helper_thread_ == NULL) { |
| 2610 | message_dispatch_helper_thread_ = new MessageDispatchHelperThread; |
| 2611 | message_dispatch_helper_thread_->Start(); |
| 2612 | } |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 2613 | } |
| 2614 | |
| 2615 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2616 | // Calls the registered debug message handler. This callback is part of the |
| 2617 | // public API. |
| 2618 | void Debugger::InvokeMessageHandler(MessageImpl message) { |
| 2619 | ScopedLock with(debugger_access_); |
| 2620 | |
| 2621 | if (message_handler_ != NULL) { |
| 2622 | message_handler_(message); |
| 2623 | } |
| 2624 | } |
| 2625 | |
| 2626 | |
| 2627 | // Puts a command coming from the public API on the queue. Creates |
| 2628 | // a copy of the command string managed by the debugger. Up to this |
| 2629 | // point, the command data was managed by the API client. Called |
| 2630 | // by the API client thread. |
| 2631 | void Debugger::ProcessCommand(Vector<const uint16_t> command, |
| 2632 | v8::Debug::ClientData* client_data) { |
| 2633 | // Need to cast away const. |
| 2634 | CommandMessage message = CommandMessage::New( |
| 2635 | Vector<uint16_t>(const_cast<uint16_t*>(command.start()), |
| 2636 | command.length()), |
| 2637 | client_data); |
| 2638 | Logger::DebugTag("Put command on command_queue."); |
| 2639 | command_queue_.Put(message); |
| 2640 | command_received_->Signal(); |
| 2641 | |
| 2642 | // Set the debug command break flag to have the command processed. |
| 2643 | if (!Debug::InDebugger()) { |
| 2644 | StackGuard::DebugCommand(); |
| 2645 | } |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 2646 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 2647 | MessageDispatchHelperThread* dispatch_thread; |
| 2648 | { |
| 2649 | ScopedLock with(dispatch_handler_access_); |
| 2650 | dispatch_thread = message_dispatch_helper_thread_; |
| 2651 | } |
| 2652 | |
| 2653 | if (dispatch_thread == NULL) { |
| 2654 | CallMessageDispatchHandler(); |
| 2655 | } else { |
| 2656 | dispatch_thread->Schedule(); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 2657 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2658 | } |
| 2659 | |
| 2660 | |
| 2661 | bool Debugger::HasCommands() { |
| 2662 | return !command_queue_.IsEmpty(); |
| 2663 | } |
| 2664 | |
| 2665 | |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 2666 | void Debugger::EnqueueDebugCommand(v8::Debug::ClientData* client_data) { |
| 2667 | CommandMessage message = CommandMessage::New(Vector<uint16_t>(), client_data); |
| 2668 | event_command_queue_.Put(message); |
| 2669 | |
| 2670 | // Set the debug command break flag to have the command processed. |
| 2671 | if (!Debug::InDebugger()) { |
| 2672 | StackGuard::DebugCommand(); |
| 2673 | } |
| 2674 | } |
| 2675 | |
| 2676 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2677 | bool Debugger::IsDebuggerActive() { |
| 2678 | ScopedLock with(debugger_access_); |
| 2679 | |
| 2680 | return message_handler_ != NULL || !event_listener_.is_null(); |
| 2681 | } |
| 2682 | |
| 2683 | |
| 2684 | Handle<Object> Debugger::Call(Handle<JSFunction> fun, |
| 2685 | Handle<Object> data, |
| 2686 | bool* pending_exception) { |
| 2687 | // When calling functions in the debugger prevent it from beeing unloaded. |
| 2688 | Debugger::never_unload_debugger_ = true; |
| 2689 | |
| 2690 | // Enter the debugger. |
| 2691 | EnterDebugger debugger; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2692 | if (debugger.FailedToEnter()) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2693 | return Factory::undefined_value(); |
| 2694 | } |
| 2695 | |
| 2696 | // Create the execution state. |
| 2697 | bool caught_exception = false; |
| 2698 | Handle<Object> exec_state = MakeExecutionState(&caught_exception); |
| 2699 | if (caught_exception) { |
| 2700 | return Factory::undefined_value(); |
| 2701 | } |
| 2702 | |
| 2703 | static const int kArgc = 2; |
| 2704 | Object** argv[kArgc] = { exec_state.location(), data.location() }; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2705 | Handle<Object> result = Execution::Call( |
| 2706 | fun, |
| 2707 | Handle<Object>(Debug::debug_context_->global_proxy()), |
| 2708 | kArgc, |
| 2709 | argv, |
| 2710 | pending_exception); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2711 | return result; |
| 2712 | } |
| 2713 | |
| 2714 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 2715 | static void StubMessageHandler2(const v8::Debug::Message& message) { |
| 2716 | // Simply ignore message. |
| 2717 | } |
| 2718 | |
| 2719 | |
| 2720 | bool Debugger::StartAgent(const char* name, int port, |
| 2721 | bool wait_for_connection) { |
| 2722 | if (wait_for_connection) { |
| 2723 | // Suspend V8 if it is already running or set V8 to suspend whenever |
| 2724 | // it starts. |
| 2725 | // Provide stub message handler; V8 auto-continues each suspend |
| 2726 | // when there is no message handler; we doesn't need it. |
| 2727 | // Once become suspended, V8 will stay so indefinitely long, until remote |
| 2728 | // debugger connects and issues "continue" command. |
| 2729 | Debugger::message_handler_ = StubMessageHandler2; |
| 2730 | v8::Debug::DebugBreak(); |
| 2731 | } |
| 2732 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2733 | if (Socket::Setup()) { |
| 2734 | agent_ = new DebuggerAgent(name, port); |
| 2735 | agent_->Start(); |
| 2736 | return true; |
| 2737 | } |
| 2738 | |
| 2739 | return false; |
| 2740 | } |
| 2741 | |
| 2742 | |
| 2743 | void Debugger::StopAgent() { |
| 2744 | if (agent_ != NULL) { |
| 2745 | agent_->Shutdown(); |
| 2746 | agent_->Join(); |
| 2747 | delete agent_; |
| 2748 | agent_ = NULL; |
| 2749 | } |
| 2750 | } |
| 2751 | |
| 2752 | |
| 2753 | void Debugger::WaitForAgent() { |
| 2754 | if (agent_ != NULL) |
| 2755 | agent_->WaitUntilListening(); |
| 2756 | } |
| 2757 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 2758 | |
| 2759 | void Debugger::CallMessageDispatchHandler() { |
| 2760 | v8::Debug::DebugMessageDispatchHandler handler; |
| 2761 | { |
| 2762 | ScopedLock with(dispatch_handler_access_); |
| 2763 | handler = Debugger::debug_message_dispatch_handler_; |
| 2764 | } |
| 2765 | if (handler != NULL) { |
| 2766 | handler(); |
| 2767 | } |
| 2768 | } |
| 2769 | |
| 2770 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2771 | MessageImpl MessageImpl::NewEvent(DebugEvent event, |
| 2772 | bool running, |
| 2773 | Handle<JSObject> exec_state, |
| 2774 | Handle<JSObject> event_data) { |
| 2775 | MessageImpl message(true, event, running, |
| 2776 | exec_state, event_data, Handle<String>(), NULL); |
| 2777 | return message; |
| 2778 | } |
| 2779 | |
| 2780 | |
| 2781 | MessageImpl MessageImpl::NewResponse(DebugEvent event, |
| 2782 | bool running, |
| 2783 | Handle<JSObject> exec_state, |
| 2784 | Handle<JSObject> event_data, |
| 2785 | Handle<String> response_json, |
| 2786 | v8::Debug::ClientData* client_data) { |
| 2787 | MessageImpl message(false, event, running, |
| 2788 | exec_state, event_data, response_json, client_data); |
| 2789 | return message; |
| 2790 | } |
| 2791 | |
| 2792 | |
| 2793 | MessageImpl::MessageImpl(bool is_event, |
| 2794 | DebugEvent event, |
| 2795 | bool running, |
| 2796 | Handle<JSObject> exec_state, |
| 2797 | Handle<JSObject> event_data, |
| 2798 | Handle<String> response_json, |
| 2799 | v8::Debug::ClientData* client_data) |
| 2800 | : is_event_(is_event), |
| 2801 | event_(event), |
| 2802 | running_(running), |
| 2803 | exec_state_(exec_state), |
| 2804 | event_data_(event_data), |
| 2805 | response_json_(response_json), |
| 2806 | client_data_(client_data) {} |
| 2807 | |
| 2808 | |
| 2809 | bool MessageImpl::IsEvent() const { |
| 2810 | return is_event_; |
| 2811 | } |
| 2812 | |
| 2813 | |
| 2814 | bool MessageImpl::IsResponse() const { |
| 2815 | return !is_event_; |
| 2816 | } |
| 2817 | |
| 2818 | |
| 2819 | DebugEvent MessageImpl::GetEvent() const { |
| 2820 | return event_; |
| 2821 | } |
| 2822 | |
| 2823 | |
| 2824 | bool MessageImpl::WillStartRunning() const { |
| 2825 | return running_; |
| 2826 | } |
| 2827 | |
| 2828 | |
| 2829 | v8::Handle<v8::Object> MessageImpl::GetExecutionState() const { |
| 2830 | return v8::Utils::ToLocal(exec_state_); |
| 2831 | } |
| 2832 | |
| 2833 | |
| 2834 | v8::Handle<v8::Object> MessageImpl::GetEventData() const { |
| 2835 | return v8::Utils::ToLocal(event_data_); |
| 2836 | } |
| 2837 | |
| 2838 | |
| 2839 | v8::Handle<v8::String> MessageImpl::GetJSON() const { |
| 2840 | v8::HandleScope scope; |
| 2841 | |
| 2842 | if (IsEvent()) { |
| 2843 | // Call toJSONProtocol on the debug event object. |
| 2844 | Handle<Object> fun = GetProperty(event_data_, "toJSONProtocol"); |
| 2845 | if (!fun->IsJSFunction()) { |
| 2846 | return v8::Handle<v8::String>(); |
| 2847 | } |
| 2848 | bool caught_exception; |
| 2849 | Handle<Object> json = Execution::TryCall(Handle<JSFunction>::cast(fun), |
| 2850 | event_data_, |
| 2851 | 0, NULL, &caught_exception); |
| 2852 | if (caught_exception || !json->IsString()) { |
| 2853 | return v8::Handle<v8::String>(); |
| 2854 | } |
| 2855 | return scope.Close(v8::Utils::ToLocal(Handle<String>::cast(json))); |
| 2856 | } else { |
| 2857 | return v8::Utils::ToLocal(response_json_); |
| 2858 | } |
| 2859 | } |
| 2860 | |
| 2861 | |
| 2862 | v8::Handle<v8::Context> MessageImpl::GetEventContext() const { |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 2863 | v8::Handle<v8::Context> context = GetDebugEventContext(); |
| 2864 | // Top::context() may be NULL when "script collected" event occures. |
| 2865 | ASSERT(!context.IsEmpty() || event_ == v8::ScriptCollected); |
| 2866 | return GetDebugEventContext(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2867 | } |
| 2868 | |
| 2869 | |
| 2870 | v8::Debug::ClientData* MessageImpl::GetClientData() const { |
| 2871 | return client_data_; |
| 2872 | } |
| 2873 | |
| 2874 | |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 2875 | EventDetailsImpl::EventDetailsImpl(DebugEvent event, |
| 2876 | Handle<JSObject> exec_state, |
| 2877 | Handle<JSObject> event_data, |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 2878 | Handle<Object> callback_data, |
| 2879 | v8::Debug::ClientData* client_data) |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 2880 | : event_(event), |
| 2881 | exec_state_(exec_state), |
| 2882 | event_data_(event_data), |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 2883 | callback_data_(callback_data), |
| 2884 | client_data_(client_data) {} |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 2885 | |
| 2886 | |
| 2887 | DebugEvent EventDetailsImpl::GetEvent() const { |
| 2888 | return event_; |
| 2889 | } |
| 2890 | |
| 2891 | |
| 2892 | v8::Handle<v8::Object> EventDetailsImpl::GetExecutionState() const { |
| 2893 | return v8::Utils::ToLocal(exec_state_); |
| 2894 | } |
| 2895 | |
| 2896 | |
| 2897 | v8::Handle<v8::Object> EventDetailsImpl::GetEventData() const { |
| 2898 | return v8::Utils::ToLocal(event_data_); |
| 2899 | } |
| 2900 | |
| 2901 | |
| 2902 | v8::Handle<v8::Context> EventDetailsImpl::GetEventContext() const { |
| 2903 | return GetDebugEventContext(); |
| 2904 | } |
| 2905 | |
| 2906 | |
| 2907 | v8::Handle<v8::Value> EventDetailsImpl::GetCallbackData() const { |
| 2908 | return v8::Utils::ToLocal(callback_data_); |
| 2909 | } |
| 2910 | |
| 2911 | |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 2912 | v8::Debug::ClientData* EventDetailsImpl::GetClientData() const { |
| 2913 | return client_data_; |
| 2914 | } |
| 2915 | |
| 2916 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2917 | CommandMessage::CommandMessage() : text_(Vector<uint16_t>::empty()), |
| 2918 | client_data_(NULL) { |
| 2919 | } |
| 2920 | |
| 2921 | |
| 2922 | CommandMessage::CommandMessage(const Vector<uint16_t>& text, |
| 2923 | v8::Debug::ClientData* data) |
| 2924 | : text_(text), |
| 2925 | client_data_(data) { |
| 2926 | } |
| 2927 | |
| 2928 | |
| 2929 | CommandMessage::~CommandMessage() { |
| 2930 | } |
| 2931 | |
| 2932 | |
| 2933 | void CommandMessage::Dispose() { |
| 2934 | text_.Dispose(); |
| 2935 | delete client_data_; |
| 2936 | client_data_ = NULL; |
| 2937 | } |
| 2938 | |
| 2939 | |
| 2940 | CommandMessage CommandMessage::New(const Vector<uint16_t>& command, |
| 2941 | v8::Debug::ClientData* data) { |
| 2942 | return CommandMessage(command.Clone(), data); |
| 2943 | } |
| 2944 | |
| 2945 | |
| 2946 | CommandMessageQueue::CommandMessageQueue(int size) : start_(0), end_(0), |
| 2947 | size_(size) { |
| 2948 | messages_ = NewArray<CommandMessage>(size); |
| 2949 | } |
| 2950 | |
| 2951 | |
| 2952 | CommandMessageQueue::~CommandMessageQueue() { |
| 2953 | while (!IsEmpty()) { |
| 2954 | CommandMessage m = Get(); |
| 2955 | m.Dispose(); |
| 2956 | } |
| 2957 | DeleteArray(messages_); |
| 2958 | } |
| 2959 | |
| 2960 | |
| 2961 | CommandMessage CommandMessageQueue::Get() { |
| 2962 | ASSERT(!IsEmpty()); |
| 2963 | int result = start_; |
| 2964 | start_ = (start_ + 1) % size_; |
| 2965 | return messages_[result]; |
| 2966 | } |
| 2967 | |
| 2968 | |
| 2969 | void CommandMessageQueue::Put(const CommandMessage& message) { |
| 2970 | if ((end_ + 1) % size_ == start_) { |
| 2971 | Expand(); |
| 2972 | } |
| 2973 | messages_[end_] = message; |
| 2974 | end_ = (end_ + 1) % size_; |
| 2975 | } |
| 2976 | |
| 2977 | |
| 2978 | void CommandMessageQueue::Expand() { |
| 2979 | CommandMessageQueue new_queue(size_ * 2); |
| 2980 | while (!IsEmpty()) { |
| 2981 | new_queue.Put(Get()); |
| 2982 | } |
| 2983 | CommandMessage* array_to_free = messages_; |
| 2984 | *this = new_queue; |
| 2985 | new_queue.messages_ = array_to_free; |
| 2986 | // Make the new_queue empty so that it doesn't call Dispose on any messages. |
| 2987 | new_queue.start_ = new_queue.end_; |
| 2988 | // Automatic destructor called on new_queue, freeing array_to_free. |
| 2989 | } |
| 2990 | |
| 2991 | |
| 2992 | LockingCommandMessageQueue::LockingCommandMessageQueue(int size) |
| 2993 | : queue_(size) { |
| 2994 | lock_ = OS::CreateMutex(); |
| 2995 | } |
| 2996 | |
| 2997 | |
| 2998 | LockingCommandMessageQueue::~LockingCommandMessageQueue() { |
| 2999 | delete lock_; |
| 3000 | } |
| 3001 | |
| 3002 | |
| 3003 | bool LockingCommandMessageQueue::IsEmpty() const { |
| 3004 | ScopedLock sl(lock_); |
| 3005 | return queue_.IsEmpty(); |
| 3006 | } |
| 3007 | |
| 3008 | |
| 3009 | CommandMessage LockingCommandMessageQueue::Get() { |
| 3010 | ScopedLock sl(lock_); |
| 3011 | CommandMessage result = queue_.Get(); |
| 3012 | Logger::DebugEvent("Get", result.text()); |
| 3013 | return result; |
| 3014 | } |
| 3015 | |
| 3016 | |
| 3017 | void LockingCommandMessageQueue::Put(const CommandMessage& message) { |
| 3018 | ScopedLock sl(lock_); |
| 3019 | queue_.Put(message); |
| 3020 | Logger::DebugEvent("Put", message.text()); |
| 3021 | } |
| 3022 | |
| 3023 | |
| 3024 | void LockingCommandMessageQueue::Clear() { |
| 3025 | ScopedLock sl(lock_); |
| 3026 | queue_.Clear(); |
| 3027 | } |
| 3028 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 3029 | |
| 3030 | MessageDispatchHelperThread::MessageDispatchHelperThread() |
| 3031 | : sem_(OS::CreateSemaphore(0)), mutex_(OS::CreateMutex()), |
| 3032 | already_signalled_(false) { |
| 3033 | } |
| 3034 | |
| 3035 | |
| 3036 | MessageDispatchHelperThread::~MessageDispatchHelperThread() { |
| 3037 | delete mutex_; |
| 3038 | delete sem_; |
| 3039 | } |
| 3040 | |
| 3041 | |
| 3042 | void MessageDispatchHelperThread::Schedule() { |
| 3043 | { |
| 3044 | ScopedLock lock(mutex_); |
| 3045 | if (already_signalled_) { |
| 3046 | return; |
| 3047 | } |
| 3048 | already_signalled_ = true; |
| 3049 | } |
| 3050 | sem_->Signal(); |
| 3051 | } |
| 3052 | |
| 3053 | |
| 3054 | void MessageDispatchHelperThread::Run() { |
| 3055 | while (true) { |
| 3056 | sem_->Wait(); |
| 3057 | { |
| 3058 | ScopedLock lock(mutex_); |
| 3059 | already_signalled_ = false; |
| 3060 | } |
| 3061 | { |
| 3062 | Locker locker; |
| 3063 | Debugger::CallMessageDispatchHandler(); |
| 3064 | } |
| 3065 | } |
| 3066 | } |
| 3067 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3068 | #endif // ENABLE_DEBUGGER_SUPPORT |
| 3069 | |
| 3070 | } } // namespace v8::internal |