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