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