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