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