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