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