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