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