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