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