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