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