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