blob: 6e5a51f7df1d2a22c4b188fad0f4f5e7ea815c1c [file] [log] [blame]
danno@chromium.org40cb8782011-05-25 07:58:50 +00001// Copyright 2011 the V8 project authors. All rights reserved.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002// 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.org5c838252010-02-19 08:53:10 +000034#include "codegen.h"
kasperl@chromium.org71affb52009-05-26 05:44:31 +000035#include "compilation-cache.h"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000036#include "compiler.h"
37#include "debug.h"
kasperl@chromium.orga5551262010-12-07 12:49:48 +000038#include "deoptimizer.h"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000039#include "execution.h"
40#include "global-handles.h"
ager@chromium.org65dad4b2009-04-23 08:48:43 +000041#include "ic.h"
42#include "ic-inl.h"
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +000043#include "isolate-inl.h"
lrn@chromium.org34e60782011-09-15 07:25:40 +000044#include "list.h"
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000045#include "messages.h"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000046#include "natives.h"
47#include "stub-cache.h"
kasper.lund7276f142008-07-30 08:49:36 +000048#include "log.h"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000049
ager@chromium.org5ec48922009-05-05 07:25:34 +000050#include "../include/v8-debug.h"
51
kasperl@chromium.org71affb52009-05-26 05:44:31 +000052namespace v8 {
53namespace internal {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000054
ager@chromium.org65dad4b2009-04-23 08:48:43 +000055#ifdef ENABLE_DEBUGGER_SUPPORT
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000056
57
58Debug::Debug(Isolate* isolate)
59 : has_break_points_(false),
60 script_cache_(NULL),
61 debug_info_list_(NULL),
62 disable_break_(false),
63 break_on_exception_(false),
64 break_on_uncaught_exception_(false),
65 debug_break_return_(NULL),
66 debug_break_slot_(NULL),
67 isolate_(isolate) {
68 memset(registers_, 0, sizeof(JSCallerSavedBuffer));
69}
70
71
72Debug::~Debug() {
73}
74
75
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000076static void PrintLn(v8::Local<v8::Value> value) {
77 v8::Local<v8::String> s = value->ToString();
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +000078 ScopedVector<char> data(s->Length() + 1);
79 if (data.start() == NULL) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000080 V8::FatalProcessOutOfMemory("PrintLn");
81 return;
82 }
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +000083 s->WriteAscii(data.start());
84 PrintF("%s\n", data.start());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000085}
86
87
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +000088static Handle<Code> ComputeCallDebugBreak(int argc, Code::Kind kind) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000089 Isolate* isolate = Isolate::Current();
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +000090 return isolate->stub_cache()->ComputeCallDebugBreak(argc, kind);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000091}
92
93
danno@chromium.org40cb8782011-05-25 07:58:50 +000094static Handle<Code> ComputeCallDebugPrepareStepIn(int argc, Code::Kind kind) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000095 Isolate* isolate = Isolate::Current();
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +000096 return isolate->stub_cache()->ComputeCallDebugPrepareStepIn(argc, kind);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000097}
98
99
100static v8::Handle<v8::Context> GetDebugEventContext(Isolate* isolate) {
101 Handle<Context> context = isolate->debug()->debugger_entry()->GetContext();
102 // Isolate::context() may have been NULL when "script collected" event
103 // occured.
104 if (context.is_null()) return v8::Local<v8::Context>();
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000105 Handle<Context> global_context(context->global_context());
106 return v8::Utils::ToLocal(global_context);
107}
108
109
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000110BreakLocationIterator::BreakLocationIterator(Handle<DebugInfo> debug_info,
111 BreakLocatorType type) {
112 debug_info_ = debug_info;
113 type_ = type;
114 reloc_iterator_ = NULL;
115 reloc_iterator_original_ = NULL;
116 Reset(); // Initialize the rest of the member variables.
117}
118
119
120BreakLocationIterator::~BreakLocationIterator() {
121 ASSERT(reloc_iterator_ != NULL);
122 ASSERT(reloc_iterator_original_ != NULL);
123 delete reloc_iterator_;
124 delete reloc_iterator_original_;
125}
126
127
128void BreakLocationIterator::Next() {
129 AssertNoAllocation nogc;
130 ASSERT(!RinfoDone());
131
132 // Iterate through reloc info for code and original code stopping at each
133 // breakable code target.
134 bool first = break_point_ == -1;
135 while (!RinfoDone()) {
136 if (!first) RinfoNext();
137 first = false;
138 if (RinfoDone()) return;
139
ager@chromium.org236ad962008-09-25 09:45:57 +0000140 // Whenever a statement position or (plain) position is passed update the
141 // current value of these.
142 if (RelocInfo::IsPosition(rmode())) {
143 if (RelocInfo::IsStatementPosition(rmode())) {
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000144 statement_position_ = static_cast<int>(
145 rinfo()->data() - debug_info_->shared()->start_position());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000146 }
ager@chromium.org236ad962008-09-25 09:45:57 +0000147 // Always update the position as we don't want that to be before the
148 // statement position.
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000149 position_ = static_cast<int>(
150 rinfo()->data() - debug_info_->shared()->start_position());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000151 ASSERT(position_ >= 0);
152 ASSERT(statement_position_ >= 0);
153 }
154
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000155 if (IsDebugBreakSlot()) {
156 // There is always a possible break point at a debug break slot.
157 break_point_++;
158 return;
159 } else if (RelocInfo::IsCodeTarget(rmode())) {
160 // Check for breakable code target. Look in the original code as setting
161 // break points can cause the code targets in the running (debugged) code
162 // to be of a different kind than in the original code.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000163 Address target = original_rinfo()->target_address();
ager@chromium.org8bb60582008-12-11 12:02:20 +0000164 Code* code = Code::GetCodeFromTargetAddress(target);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000165 if ((code->is_inline_cache_stub() &&
danno@chromium.org40cb8782011-05-25 07:58:50 +0000166 !code->is_binary_op_stub() &&
167 !code->is_unary_op_stub() &&
ricow@chromium.org9fa09672011-07-25 11:05:35 +0000168 !code->is_compare_ic_stub() &&
169 !code->is_to_boolean_ic_stub()) ||
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000170 RelocInfo::IsConstructCall(rmode())) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000171 break_point_++;
172 return;
173 }
174 if (code->kind() == Code::STUB) {
ager@chromium.orga1645e22009-09-09 19:27:10 +0000175 if (IsDebuggerStatement()) {
176 break_point_++;
177 return;
178 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000179 if (type_ == ALL_BREAK_LOCATIONS) {
180 if (Debug::IsBreakStub(code)) {
181 break_point_++;
182 return;
183 }
184 } else {
185 ASSERT(type_ == SOURCE_BREAK_LOCATIONS);
186 if (Debug::IsSourceBreakStub(code)) {
187 break_point_++;
188 return;
189 }
190 }
191 }
192 }
193
194 // Check for break at return.
ager@chromium.org236ad962008-09-25 09:45:57 +0000195 if (RelocInfo::IsJSReturn(rmode())) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000196 // Set the positions to the end of the function.
197 if (debug_info_->shared()->HasSourceCode()) {
198 position_ = debug_info_->shared()->end_position() -
whesse@chromium.orge90029b2010-08-02 11:52:17 +0000199 debug_info_->shared()->start_position() - 1;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000200 } else {
201 position_ = 0;
202 }
203 statement_position_ = position_;
204 break_point_++;
205 return;
206 }
207 }
208}
209
210
211void BreakLocationIterator::Next(int count) {
212 while (count > 0) {
213 Next();
214 count--;
215 }
216}
217
218
219// Find the break point closest to the supplied address.
220void BreakLocationIterator::FindBreakLocationFromAddress(Address pc) {
221 // Run through all break points to locate the one closest to the address.
222 int closest_break_point = 0;
223 int distance = kMaxInt;
224 while (!Done()) {
225 // Check if this break point is closer that what was previously found.
226 if (this->pc() < pc && pc - this->pc() < distance) {
227 closest_break_point = break_point();
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000228 distance = static_cast<int>(pc - this->pc());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000229 // Check whether we can't get any closer.
230 if (distance == 0) break;
231 }
232 Next();
233 }
234
235 // Move to the break point found.
236 Reset();
237 Next(closest_break_point);
238}
239
240
241// Find the break point closest to the supplied source position.
242void BreakLocationIterator::FindBreakLocationFromPosition(int position) {
243 // Run through all break points to locate the one closest to the source
244 // position.
245 int closest_break_point = 0;
246 int distance = kMaxInt;
247 while (!Done()) {
248 // Check if this break point is closer that what was previously found.
249 if (position <= statement_position() &&
250 statement_position() - position < distance) {
251 closest_break_point = break_point();
252 distance = statement_position() - position;
253 // Check whether we can't get any closer.
254 if (distance == 0) break;
255 }
256 Next();
257 }
258
259 // Move to the break point found.
260 Reset();
261 Next(closest_break_point);
262}
263
264
265void BreakLocationIterator::Reset() {
266 // Create relocation iterators for the two code objects.
267 if (reloc_iterator_ != NULL) delete reloc_iterator_;
268 if (reloc_iterator_original_ != NULL) delete reloc_iterator_original_;
269 reloc_iterator_ = new RelocIterator(debug_info_->code());
270 reloc_iterator_original_ = new RelocIterator(debug_info_->original_code());
271
272 // Position at the first break point.
273 break_point_ = -1;
274 position_ = 1;
275 statement_position_ = 1;
276 Next();
277}
278
279
280bool BreakLocationIterator::Done() const {
281 return RinfoDone();
282}
283
284
285void BreakLocationIterator::SetBreakPoint(Handle<Object> break_point_object) {
286 // If there is not already a real break point here patch code with debug
287 // break.
288 if (!HasBreakPoint()) {
289 SetDebugBreak();
290 }
ager@chromium.orga1645e22009-09-09 19:27:10 +0000291 ASSERT(IsDebugBreak() || IsDebuggerStatement());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000292 // Set the break point information.
293 DebugInfo::SetBreakPoint(debug_info_, code_position(),
294 position(), statement_position(),
295 break_point_object);
296}
297
298
299void BreakLocationIterator::ClearBreakPoint(Handle<Object> break_point_object) {
300 // Clear the break point information.
301 DebugInfo::ClearBreakPoint(debug_info_, code_position(), break_point_object);
302 // If there are no more break points here remove the debug break.
303 if (!HasBreakPoint()) {
304 ClearDebugBreak();
305 ASSERT(!IsDebugBreak());
306 }
307}
308
309
310void BreakLocationIterator::SetOneShot() {
ager@chromium.orga1645e22009-09-09 19:27:10 +0000311 // Debugger statement always calls debugger. No need to modify it.
312 if (IsDebuggerStatement()) {
313 return;
314 }
315
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000316 // If there is a real break point here no more to do.
317 if (HasBreakPoint()) {
318 ASSERT(IsDebugBreak());
319 return;
320 }
321
322 // Patch code with debug break.
323 SetDebugBreak();
324}
325
326
327void BreakLocationIterator::ClearOneShot() {
ager@chromium.orga1645e22009-09-09 19:27:10 +0000328 // Debugger statement always calls debugger. No need to modify it.
329 if (IsDebuggerStatement()) {
330 return;
331 }
332
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000333 // If there is a real break point here no more to do.
334 if (HasBreakPoint()) {
335 ASSERT(IsDebugBreak());
336 return;
337 }
338
339 // Patch code removing debug break.
340 ClearDebugBreak();
341 ASSERT(!IsDebugBreak());
342}
343
344
345void BreakLocationIterator::SetDebugBreak() {
ager@chromium.orga1645e22009-09-09 19:27:10 +0000346 // Debugger statement always calls debugger. No need to modify it.
347 if (IsDebuggerStatement()) {
348 return;
349 }
350
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000351 // If there is already a break point here just return. This might happen if
v8.team.kasperl727e9952008-09-02 14:56:44 +0000352 // the same code is flooded with break points twice. Flooding the same
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000353 // function twice might happen when stepping in a function with an exception
354 // handler as the handler and the function is the same.
355 if (IsDebugBreak()) {
356 return;
357 }
358
ager@chromium.org236ad962008-09-25 09:45:57 +0000359 if (RelocInfo::IsJSReturn(rmode())) {
iposva@chromium.org245aa852009-02-10 00:49:54 +0000360 // Patch the frame exit code with a break point.
361 SetDebugBreakAtReturn();
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000362 } else if (IsDebugBreakSlot()) {
363 // Patch the code in the break slot.
364 SetDebugBreakAtSlot();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000365 } else {
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000366 // Patch the IC call.
367 SetDebugBreakAtIC();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000368 }
369 ASSERT(IsDebugBreak());
370}
371
372
373void BreakLocationIterator::ClearDebugBreak() {
ager@chromium.orga1645e22009-09-09 19:27:10 +0000374 // Debugger statement always calls debugger. No need to modify it.
375 if (IsDebuggerStatement()) {
376 return;
377 }
378
ager@chromium.org236ad962008-09-25 09:45:57 +0000379 if (RelocInfo::IsJSReturn(rmode())) {
iposva@chromium.org245aa852009-02-10 00:49:54 +0000380 // Restore the frame exit code.
381 ClearDebugBreakAtReturn();
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000382 } else if (IsDebugBreakSlot()) {
383 // Restore the code in the break slot.
384 ClearDebugBreakAtSlot();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000385 } else {
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000386 // Patch the IC call.
387 ClearDebugBreakAtIC();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000388 }
389 ASSERT(!IsDebugBreak());
390}
391
392
393void BreakLocationIterator::PrepareStepIn() {
ager@chromium.org381abbb2009-02-25 13:23:22 +0000394 HandleScope scope;
395
ager@chromium.orga1645e22009-09-09 19:27:10 +0000396 // Step in can only be prepared if currently positioned on an IC call,
397 // construct call or CallFunction stub call.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000398 Address target = rinfo()->target_address();
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000399 Handle<Code> target_code(Code::GetCodeFromTargetAddress(target));
400 if (target_code->is_call_stub() || target_code->is_keyed_call_stub()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000401 // Step in through IC call is handled by the runtime system. Therefore make
402 // sure that the any current IC is cleared and the runtime system is
403 // called. If the executing code has a debug break at the location change
404 // the call in the original code as it is the code there that will be
405 // executed in place of the debug break call.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000406 Handle<Code> stub = ComputeCallDebugPrepareStepIn(
407 target_code->arguments_count(), target_code->kind());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000408 if (IsDebugBreak()) {
409 original_rinfo()->set_target_address(stub->entry());
410 } else {
411 rinfo()->set_target_address(stub->entry());
412 }
413 } else {
ager@chromium.orga1645e22009-09-09 19:27:10 +0000414#ifdef DEBUG
415 // All the following stuff is needed only for assertion checks so the code
416 // is wrapped in ifdef.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000417 Handle<Code> maybe_call_function_stub = target_code;
ager@chromium.orga1645e22009-09-09 19:27:10 +0000418 if (IsDebugBreak()) {
419 Address original_target = original_rinfo()->target_address();
420 maybe_call_function_stub =
421 Handle<Code>(Code::GetCodeFromTargetAddress(original_target));
422 }
423 bool is_call_function_stub =
424 (maybe_call_function_stub->kind() == Code::STUB &&
425 maybe_call_function_stub->major_key() == CodeStub::CallFunction);
426
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000427 // Step in through construct call requires no changes to the running code.
428 // Step in through getters/setters should already be prepared as well
429 // because caller of this function (Debug::PrepareStep) is expected to
430 // flood the top frame's function with one shot breakpoints.
ager@chromium.orga1645e22009-09-09 19:27:10 +0000431 // Step in through CallFunction stub should also be prepared by caller of
432 // this function (Debug::PrepareStep) which should flood target function
433 // with breakpoints.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000434 ASSERT(RelocInfo::IsConstructCall(rmode()) ||
435 target_code->is_inline_cache_stub() ||
436 is_call_function_stub);
ager@chromium.orga1645e22009-09-09 19:27:10 +0000437#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000438 }
439}
440
441
442// Check whether the break point is at a position which will exit the function.
443bool BreakLocationIterator::IsExit() const {
ager@chromium.org236ad962008-09-25 09:45:57 +0000444 return (RelocInfo::IsJSReturn(rmode()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000445}
446
447
448bool BreakLocationIterator::HasBreakPoint() {
449 return debug_info_->HasBreakPoint(code_position());
450}
451
452
453// Check whether there is a debug break at the current position.
454bool BreakLocationIterator::IsDebugBreak() {
ager@chromium.org236ad962008-09-25 09:45:57 +0000455 if (RelocInfo::IsJSReturn(rmode())) {
iposva@chromium.org245aa852009-02-10 00:49:54 +0000456 return IsDebugBreakAtReturn();
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000457 } else if (IsDebugBreakSlot()) {
458 return IsDebugBreakAtSlot();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000459 } else {
460 return Debug::IsDebugBreak(rinfo()->target_address());
461 }
462}
463
464
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000465void BreakLocationIterator::SetDebugBreakAtIC() {
466 // Patch the original code with the current address as the current address
467 // might have changed by the inline caching since the code was copied.
468 original_rinfo()->set_target_address(rinfo()->target_address());
469
470 RelocInfo::Mode mode = rmode();
471 if (RelocInfo::IsCodeTarget(mode)) {
472 Address target = rinfo()->target_address();
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000473 Handle<Code> target_code(Code::GetCodeFromTargetAddress(target));
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000474
475 // Patch the code to invoke the builtin debug break function matching the
476 // calling convention used by the call site.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000477 Handle<Code> dbgbrk_code(Debug::FindDebugBreak(target_code, mode));
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000478 rinfo()->set_target_address(dbgbrk_code->entry());
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000479 }
480}
481
482
483void BreakLocationIterator::ClearDebugBreakAtIC() {
484 // Patch the code to the original invoke.
485 rinfo()->set_target_address(original_rinfo()->target_address());
486}
487
488
ager@chromium.orga1645e22009-09-09 19:27:10 +0000489bool BreakLocationIterator::IsDebuggerStatement() {
ager@chromium.org5c838252010-02-19 08:53:10 +0000490 return RelocInfo::DEBUG_BREAK == rmode();
ager@chromium.orga1645e22009-09-09 19:27:10 +0000491}
492
493
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000494bool BreakLocationIterator::IsDebugBreakSlot() {
495 return RelocInfo::DEBUG_BREAK_SLOT == rmode();
496}
497
498
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000499Object* BreakLocationIterator::BreakPointObjects() {
500 return debug_info_->GetBreakPointObjects(code_position());
501}
502
503
ager@chromium.org381abbb2009-02-25 13:23:22 +0000504// 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.
507void BreakLocationIterator::ClearAllDebugBreak() {
508 while (!Done()) {
509 ClearDebugBreak();
510 Next();
511 }
512}
513
514
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000515bool BreakLocationIterator::RinfoDone() const {
516 ASSERT(reloc_iterator_->done() == reloc_iterator_original_->done());
517 return reloc_iterator_->done();
518}
519
520
521void BreakLocationIterator::RinfoNext() {
522 reloc_iterator_->next();
523 reloc_iterator_original_->next();
524#ifdef DEBUG
525 ASSERT(reloc_iterator_->done() == reloc_iterator_original_->done());
526 if (!reloc_iterator_->done()) {
527 ASSERT(rmode() == original_rmode());
528 }
529#endif
530}
531
532
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000533// Threading support.
534void Debug::ThreadInit() {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000535 thread_local_.break_count_ = 0;
536 thread_local_.break_id_ = 0;
537 thread_local_.break_frame_id_ = StackFrame::NO_ID;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000538 thread_local_.last_step_action_ = StepNone;
ager@chromium.org236ad962008-09-25 09:45:57 +0000539 thread_local_.last_statement_position_ = RelocInfo::kNoPosition;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000540 thread_local_.step_count_ = 0;
541 thread_local_.last_fp_ = 0;
lrn@chromium.org34e60782011-09-15 07:25:40 +0000542 thread_local_.queued_step_count_ = 0;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000543 thread_local_.step_into_fp_ = 0;
ager@chromium.orga1645e22009-09-09 19:27:10 +0000544 thread_local_.step_out_fp_ = 0;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000545 thread_local_.after_break_target_ = 0;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000546 // TODO(isolates): frames_are_dropped_?
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000547 thread_local_.debugger_entry_ = NULL;
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000548 thread_local_.pending_interrupts_ = 0;
ricow@chromium.org0b9f8502010-08-18 07:45:01 +0000549 thread_local_.restarter_frame_function_pointer_ = NULL;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000550}
551
552
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000553char* Debug::ArchiveDebug(char* storage) {
554 char* to = storage;
555 memcpy(to, reinterpret_cast<char*>(&thread_local_), sizeof(ThreadLocal));
556 to += sizeof(ThreadLocal);
557 memcpy(to, reinterpret_cast<char*>(&registers_), sizeof(registers_));
558 ThreadInit();
559 ASSERT(to <= storage + ArchiveSpacePerThread());
560 return storage + ArchiveSpacePerThread();
561}
562
563
564char* Debug::RestoreDebug(char* storage) {
565 char* from = storage;
566 memcpy(reinterpret_cast<char*>(&thread_local_), from, sizeof(ThreadLocal));
567 from += sizeof(ThreadLocal);
568 memcpy(reinterpret_cast<char*>(&registers_), from, sizeof(registers_));
569 ASSERT(from <= storage + ArchiveSpacePerThread());
570 return storage + ArchiveSpacePerThread();
571}
572
573
574int Debug::ArchiveSpacePerThread() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000575 return sizeof(ThreadLocal) + sizeof(JSCallerSavedBuffer);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000576}
577
578
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000579// Frame structure (conforms InternalFrame structure):
580// -- code
581// -- SMI maker
582// -- function (slot is called "context")
583// -- frame base
584Object** Debug::SetUpFrameDropperFrame(StackFrame* bottom_js_frame,
585 Handle<Code> code) {
586 ASSERT(bottom_js_frame->is_java_script());
587
588 Address fp = bottom_js_frame->fp();
589
590 // Move function pointer into "context" slot.
591 Memory::Object_at(fp + StandardFrameConstants::kContextOffset) =
592 Memory::Object_at(fp + JavaScriptFrameConstants::kFunctionOffset);
593
594 Memory::Object_at(fp + InternalFrameConstants::kCodeOffset) = *code;
595 Memory::Object_at(fp + StandardFrameConstants::kMarkerOffset) =
596 Smi::FromInt(StackFrame::INTERNAL);
597
598 return reinterpret_cast<Object**>(&Memory::Object_at(
599 fp + StandardFrameConstants::kContextOffset));
600}
601
602const int Debug::kFrameDropperFrameSize = 4;
603
604
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000605void ScriptCache::Add(Handle<Script> script) {
lrn@chromium.org7516f052011-03-30 08:52:27 +0000606 GlobalHandles* global_handles = Isolate::Current()->global_handles();
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000607 // Create an entry in the hash map for the script.
608 int id = Smi::cast(script->id())->value();
609 HashMap::Entry* entry =
610 HashMap::Lookup(reinterpret_cast<void*>(id), Hash(id), true);
611 if (entry->value != NULL) {
612 ASSERT(*script == *reinterpret_cast<Script**>(entry->value));
613 return;
614 }
615
616 // Globalize the script object, make it weak and use the location of the
617 // global handle as the value in the hash map.
618 Handle<Script> script_ =
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000619 Handle<Script>::cast(
lrn@chromium.org7516f052011-03-30 08:52:27 +0000620 (global_handles->Create(*script)));
621 global_handles->MakeWeak(
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000622 reinterpret_cast<Object**>(script_.location()),
623 this,
624 ScriptCache::HandleWeakScript);
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000625 entry->value = script_.location();
626}
627
628
629Handle<FixedArray> ScriptCache::GetScripts() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000630 Handle<FixedArray> instances = FACTORY->NewFixedArray(occupancy());
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000631 int count = 0;
632 for (HashMap::Entry* entry = Start(); entry != NULL; entry = Next(entry)) {
633 ASSERT(entry->value != NULL);
634 if (entry->value != NULL) {
635 instances->set(count, *reinterpret_cast<Script**>(entry->value));
636 count++;
637 }
638 }
639 return instances;
640}
641
642
643void ScriptCache::ProcessCollectedScripts() {
lrn@chromium.org7516f052011-03-30 08:52:27 +0000644 Debugger* debugger = Isolate::Current()->debugger();
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000645 for (int i = 0; i < collected_scripts_.length(); i++) {
lrn@chromium.org7516f052011-03-30 08:52:27 +0000646 debugger->OnScriptCollected(collected_scripts_[i]);
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000647 }
648 collected_scripts_.Clear();
649}
650
651
652void ScriptCache::Clear() {
lrn@chromium.org7516f052011-03-30 08:52:27 +0000653 GlobalHandles* global_handles = Isolate::Current()->global_handles();
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000654 // Iterate the script cache to get rid of all the weak handles.
655 for (HashMap::Entry* entry = Start(); entry != NULL; entry = Next(entry)) {
656 ASSERT(entry != NULL);
657 Object** location = reinterpret_cast<Object**>(entry->value);
658 ASSERT((*location)->IsScript());
lrn@chromium.org7516f052011-03-30 08:52:27 +0000659 global_handles->ClearWeakness(location);
660 global_handles->Destroy(location);
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000661 }
662 // Clear the content of the hash map.
663 HashMap::Clear();
664}
665
666
667void ScriptCache::HandleWeakScript(v8::Persistent<v8::Value> obj, void* data) {
668 ScriptCache* script_cache = reinterpret_cast<ScriptCache*>(data);
669 // Find the location of the global handle.
670 Script** location =
671 reinterpret_cast<Script**>(Utils::OpenHandle(*obj).location());
672 ASSERT((*location)->IsScript());
673
674 // Remove the entry from the cache.
675 int id = Smi::cast((*location)->id())->value();
676 script_cache->Remove(reinterpret_cast<void*>(id), Hash(id));
677 script_cache->collected_scripts_.Add(id);
678
679 // Clear the weak handle.
680 obj.Dispose();
681 obj.Clear();
682}
683
684
685void Debug::Setup(bool create_heap_objects) {
686 ThreadInit();
687 if (create_heap_objects) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000688 // Get code to handle debug break on return.
689 debug_break_return_ =
lrn@chromium.org7516f052011-03-30 08:52:27 +0000690 isolate_->builtins()->builtin(Builtins::kReturn_DebugBreak);
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000691 ASSERT(debug_break_return_->IsCode());
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000692 // Get code to handle debug break in debug break slots.
693 debug_break_slot_ =
lrn@chromium.org7516f052011-03-30 08:52:27 +0000694 isolate_->builtins()->builtin(Builtins::kSlot_DebugBreak);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000695 ASSERT(debug_break_slot_->IsCode());
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000696 }
697}
698
699
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000700void Debug::HandleWeakDebugInfo(v8::Persistent<v8::Value> obj, void* data) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000701 Debug* debug = Isolate::Current()->debug();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000702 DebugInfoListNode* node = reinterpret_cast<DebugInfoListNode*>(data);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000703 // We need to clear all breakpoints associated with the function to restore
704 // original code and avoid patching the code twice later because
705 // the function will live in the heap until next gc, and can be found by
706 // Runtime::FindSharedFunctionInfoInScript.
707 BreakLocationIterator it(node->debug_info(), ALL_BREAK_LOCATIONS);
708 it.ClearAllDebugBreak();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000709 debug->RemoveDebugInfo(node->debug_info());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000710#ifdef DEBUG
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000711 node = debug->debug_info_list_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000712 while (node != NULL) {
713 ASSERT(node != reinterpret_cast<DebugInfoListNode*>(data));
714 node = node->next();
715 }
716#endif
717}
718
719
720DebugInfoListNode::DebugInfoListNode(DebugInfo* debug_info): next_(NULL) {
lrn@chromium.org7516f052011-03-30 08:52:27 +0000721 GlobalHandles* global_handles = Isolate::Current()->global_handles();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000722 // Globalize the request debug info object and make it weak.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000723 debug_info_ = Handle<DebugInfo>::cast(
lrn@chromium.org7516f052011-03-30 08:52:27 +0000724 (global_handles->Create(debug_info)));
725 global_handles->MakeWeak(
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000726 reinterpret_cast<Object**>(debug_info_.location()),
727 this,
728 Debug::HandleWeakDebugInfo);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000729}
730
731
732DebugInfoListNode::~DebugInfoListNode() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000733 Isolate::Current()->global_handles()->Destroy(
734 reinterpret_cast<Object**>(debug_info_.location()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000735}
736
737
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000738bool Debug::CompileDebuggerScript(int index) {
lrn@chromium.org7516f052011-03-30 08:52:27 +0000739 Isolate* isolate = Isolate::Current();
740 Factory* factory = isolate->factory();
741 HandleScope scope(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000742
kasper.lund44510672008-07-25 07:37:58 +0000743 // Bail out if the index is invalid.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000744 if (index == -1) {
745 return false;
746 }
kasper.lund44510672008-07-25 07:37:58 +0000747
748 // Find source and name for the requested script.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000749 Handle<String> source_code =
lrn@chromium.org7516f052011-03-30 08:52:27 +0000750 isolate->bootstrapper()->NativesSourceLookup(index);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000751 Vector<const char> name = Natives::GetScriptName(index);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000752 Handle<String> script_name = factory->NewStringFromAscii(name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000753
754 // Compile the script.
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000755 Handle<SharedFunctionInfo> function_info;
756 function_info = Compiler::Compile(source_code,
757 script_name,
758 0, 0, NULL, NULL,
759 Handle<String>::null(),
760 NATIVES_CODE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000761
762 // Silently ignore stack overflows during compilation.
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000763 if (function_info.is_null()) {
lrn@chromium.org7516f052011-03-30 08:52:27 +0000764 ASSERT(isolate->has_pending_exception());
765 isolate->clear_pending_exception();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000766 return false;
767 }
768
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000769 // Execute the shared function in the debugger context.
lrn@chromium.org7516f052011-03-30 08:52:27 +0000770 Handle<Context> context = isolate->global_context();
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000771 bool caught_exception;
kasper.lund44510672008-07-25 07:37:58 +0000772 Handle<JSFunction> function =
lrn@chromium.org7516f052011-03-30 08:52:27 +0000773 factory->NewFunctionFromSharedFunctionInfo(function_info, context);
rossberg@chromium.org717967f2011-07-20 13:44:42 +0000774
775 Execution::TryCall(function, Handle<Object>(context->global()),
776 0, NULL, &caught_exception);
kasper.lund44510672008-07-25 07:37:58 +0000777
778 // Check for caught exceptions.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000779 if (caught_exception) {
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000780 Handle<Object> message = MessageHandler::MakeMessageObject(
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000781 "error_loading_debugger", NULL, Vector<Handle<Object> >::empty(),
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +0000782 Handle<String>(), Handle<JSArray>());
karlklose@chromium.org44bc7082011-04-11 12:33:05 +0000783 MessageHandler::ReportMessage(Isolate::Current(), NULL, message);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000784 return false;
785 }
786
kasper.lund44510672008-07-25 07:37:58 +0000787 // Mark this script as native and return successfully.
788 Handle<Script> script(Script::cast(function->shared()->script()));
ager@chromium.orge2902be2009-06-08 12:21:35 +0000789 script->set_type(Smi::FromInt(Script::TYPE_NATIVE));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000790 return true;
791}
792
793
794bool Debug::Load() {
795 // Return if debugger is already loaded.
kasper.lund212ac232008-07-16 07:07:30 +0000796 if (IsLoaded()) return true;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000797
lrn@chromium.org7516f052011-03-30 08:52:27 +0000798 Debugger* debugger = isolate_->debugger();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000799
kasper.lund44510672008-07-25 07:37:58 +0000800 // Bail out if we're already in the process of compiling the native
801 // JavaScript source code for the debugger.
lrn@chromium.org7516f052011-03-30 08:52:27 +0000802 if (debugger->compiling_natives() ||
803 debugger->is_loading_debugger())
mads.s.agercbaa0602008-08-14 13:41:48 +0000804 return false;
lrn@chromium.org7516f052011-03-30 08:52:27 +0000805 debugger->set_loading_debugger(true);
kasper.lund44510672008-07-25 07:37:58 +0000806
807 // Disable breakpoints and interrupts while compiling and running the
808 // debugger scripts including the context creation code.
809 DisableBreak disable(true);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000810 PostponeInterruptsScope postpone(isolate_);
kasper.lund44510672008-07-25 07:37:58 +0000811
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000812 // Create the debugger context.
lrn@chromium.org7516f052011-03-30 08:52:27 +0000813 HandleScope scope(isolate_);
kasper.lund44510672008-07-25 07:37:58 +0000814 Handle<Context> context =
lrn@chromium.org7516f052011-03-30 08:52:27 +0000815 isolate_->bootstrapper()->CreateEnvironment(
danno@chromium.org160a7b02011-04-18 15:51:38 +0000816 isolate_,
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000817 Handle<Object>::null(),
818 v8::Handle<ObjectTemplate>(),
819 NULL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000820
kasper.lund44510672008-07-25 07:37:58 +0000821 // Use the debugger context.
lrn@chromium.org7516f052011-03-30 08:52:27 +0000822 SaveContext save(isolate_);
823 isolate_->set_context(*context);
kasper.lund44510672008-07-25 07:37:58 +0000824
825 // Expose the builtins object in the debugger context.
lrn@chromium.org7516f052011-03-30 08:52:27 +0000826 Handle<String> key = isolate_->factory()->LookupAsciiSymbol("builtins");
kasper.lund44510672008-07-25 07:37:58 +0000827 Handle<GlobalObject> global = Handle<GlobalObject>(context->global());
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +0000828 RETURN_IF_EMPTY_HANDLE_VALUE(
lrn@chromium.org7516f052011-03-30 08:52:27 +0000829 isolate_,
ager@chromium.org9ee27ae2011-03-02 13:43:26 +0000830 SetProperty(global, key, Handle<Object>(global->builtins()),
831 NONE, kNonStrictMode),
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +0000832 false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000833
834 // Compile the JavaScript for the debugger in the debugger context.
lrn@chromium.org7516f052011-03-30 08:52:27 +0000835 debugger->set_compiling_natives(true);
kasper.lund44510672008-07-25 07:37:58 +0000836 bool caught_exception =
837 !CompileDebuggerScript(Natives::GetIndex("mirror")) ||
838 !CompileDebuggerScript(Natives::GetIndex("debug"));
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000839
840 if (FLAG_enable_liveedit) {
841 caught_exception = caught_exception ||
842 !CompileDebuggerScript(Natives::GetIndex("liveedit"));
843 }
844
lrn@chromium.org7516f052011-03-30 08:52:27 +0000845 debugger->set_compiling_natives(false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000846
mads.s.agercbaa0602008-08-14 13:41:48 +0000847 // Make sure we mark the debugger as not loading before we might
848 // return.
lrn@chromium.org7516f052011-03-30 08:52:27 +0000849 debugger->set_loading_debugger(false);
mads.s.agercbaa0602008-08-14 13:41:48 +0000850
kasper.lund44510672008-07-25 07:37:58 +0000851 // Check for caught exceptions.
852 if (caught_exception) return false;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000853
854 // Debugger loaded.
whesse@chromium.org023421e2010-12-21 12:19:12 +0000855 debug_context_ = context;
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000856
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000857 return true;
858}
859
860
861void Debug::Unload() {
862 // Return debugger is not loaded.
863 if (!IsLoaded()) {
864 return;
865 }
866
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000867 // Clear the script cache.
868 DestroyScriptCache();
869
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000870 // Clear debugger context global handle.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000871 Isolate::Current()->global_handles()->Destroy(
872 reinterpret_cast<Object**>(debug_context_.location()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000873 debug_context_ = Handle<Context>();
874}
875
876
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000877// Set the flag indicating that preemption happened during debugging.
878void Debug::PreemptionWhileInDebugger() {
879 ASSERT(InDebugger());
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000880 Debug::set_interrupts_pending(PREEMPT);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000881}
882
883
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000884void Debug::Iterate(ObjectVisitor* v) {
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000885 v->VisitPointer(BitCast<Object**>(&(debug_break_return_)));
886 v->VisitPointer(BitCast<Object**>(&(debug_break_slot_)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000887}
888
889
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000890Object* Debug::Break(Arguments args) {
891 Heap* heap = isolate_->heap();
892 HandleScope scope(isolate_);
mads.s.ager31e71382008-08-13 09:32:07 +0000893 ASSERT(args.length() == 0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000894
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000895 thread_local_.frame_drop_mode_ = FRAMES_UNTOUCHED;
ager@chromium.org357bf652010-04-12 11:30:10 +0000896
kasper.lundbd3ec4e2008-07-09 11:06:54 +0000897 // Get the top-most JavaScript frame.
vegorov@chromium.org74f333b2011-04-06 11:17:46 +0000898 JavaScriptFrameIterator it(isolate_);
kasper.lundbd3ec4e2008-07-09 11:06:54 +0000899 JavaScriptFrame* frame = it.frame();
900
901 // Just continue if breaks are disabled or debugger cannot be loaded.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000902 if (disable_break() || !Load()) {
903 SetAfterBreakTarget(frame);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000904 return heap->undefined_value();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000905 }
906
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000907 // Enter the debugger.
908 EnterDebugger debugger;
909 if (debugger.FailedToEnter()) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000910 return heap->undefined_value();
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000911 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000912
kasper.lund44510672008-07-25 07:37:58 +0000913 // Postpone interrupt during breakpoint processing.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000914 PostponeInterruptsScope postpone(isolate_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000915
916 // Get the debug info (create it if it does not exist).
917 Handle<SharedFunctionInfo> shared =
918 Handle<SharedFunctionInfo>(JSFunction::cast(frame->function())->shared());
919 Handle<DebugInfo> debug_info = GetDebugInfo(shared);
920
921 // Find the break point where execution has stopped.
922 BreakLocationIterator break_location_iterator(debug_info,
923 ALL_BREAK_LOCATIONS);
924 break_location_iterator.FindBreakLocationFromAddress(frame->pc());
925
926 // Check whether step next reached a new statement.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000927 if (!StepNextContinue(&break_location_iterator, frame)) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000928 // Decrease steps left if performing multiple steps.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000929 if (thread_local_.step_count_ > 0) {
930 thread_local_.step_count_--;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000931 }
932 }
933
934 // If there is one or more real break points check whether any of these are
935 // triggered.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000936 Handle<Object> break_points_hit(heap->undefined_value());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000937 if (break_location_iterator.HasBreakPoint()) {
938 Handle<Object> break_point_objects =
939 Handle<Object>(break_location_iterator.BreakPointObjects());
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000940 break_points_hit = CheckBreakPoints(break_point_objects);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000941 }
942
ager@chromium.orga1645e22009-09-09 19:27:10 +0000943 // If step out is active skip everything until the frame where we need to step
944 // out to is reached, unless real breakpoint is hit.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000945 if (StepOutActive() && frame->fp() != step_out_fp() &&
ager@chromium.orga1645e22009-09-09 19:27:10 +0000946 break_points_hit->IsUndefined() ) {
947 // Step count should always be 0 for StepOut.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000948 ASSERT(thread_local_.step_count_ == 0);
ager@chromium.orga1645e22009-09-09 19:27:10 +0000949 } else if (!break_points_hit->IsUndefined() ||
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000950 (thread_local_.last_step_action_ != StepNone &&
951 thread_local_.step_count_ == 0)) {
ager@chromium.orga1645e22009-09-09 19:27:10 +0000952 // Notify debugger if a real break point is triggered or if performing
953 // single stepping with no more steps to perform. Otherwise do another step.
954
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000955 // Clear all current stepping setup.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000956 ClearStepping();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000957
lrn@chromium.org34e60782011-09-15 07:25:40 +0000958 if (thread_local_.queued_step_count_ > 0) {
959 // Perform queued steps
960 int step_count = thread_local_.queued_step_count_;
961
962 // Clear queue
963 thread_local_.queued_step_count_ = 0;
964
965 PrepareStep(StepNext, step_count);
966 } else {
967 // Notify the debug event listeners.
968 isolate_->debugger()->OnDebugBreak(break_points_hit, false);
969 }
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000970 } else if (thread_local_.last_step_action_ != StepNone) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000971 // Hold on to last step action as it is cleared by the call to
972 // ClearStepping.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000973 StepAction step_action = thread_local_.last_step_action_;
974 int step_count = thread_local_.step_count_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000975
lrn@chromium.org34e60782011-09-15 07:25:40 +0000976 // If StepNext goes deeper in code, StepOut until original frame
977 // and keep step count queued up in the meantime.
978 if (step_action == StepNext && frame->fp() < thread_local_.last_fp_) {
979 // Count frames until target frame
980 int count = 0;
981 JavaScriptFrameIterator it(isolate_);
982 while (!it.done() && it.frame()->fp() != thread_local_.last_fp_) {
983 count++;
984 it.Advance();
985 }
986
987 // If we found original frame
988 if (it.frame()->fp() == thread_local_.last_fp_) {
989 if (step_count > 1) {
990 // Save old count and action to continue stepping after
991 // StepOut
992 thread_local_.queued_step_count_ = step_count - 1;
993 }
994
995 // Set up for StepOut to reach target frame
996 step_action = StepOut;
997 step_count = count;
998 }
999 }
1000
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001001 // Clear all current stepping setup.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001002 ClearStepping();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001003
1004 // Set up for the remaining steps.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001005 PrepareStep(step_action, step_count);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001006 }
1007
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001008 if (thread_local_.frame_drop_mode_ == FRAMES_UNTOUCHED) {
1009 SetAfterBreakTarget(frame);
1010 } else if (thread_local_.frame_drop_mode_ ==
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001011 FRAME_DROPPED_IN_IC_CALL) {
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001012 // We must have been calling IC stub. Do not go there anymore.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001013 Code* plain_return = isolate_->builtins()->builtin(
1014 Builtins::kPlainReturn_LiveEdit);
1015 thread_local_.after_break_target_ = plain_return->entry();
1016 } else if (thread_local_.frame_drop_mode_ ==
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001017 FRAME_DROPPED_IN_DEBUG_SLOT_CALL) {
1018 // Debug break slot stub does not return normally, instead it manually
1019 // cleans the stack and jumps. We should patch the jump address.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001020 Code* plain_return = isolate_->builtins()->builtin(
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00001021 Builtins::kFrameDropper_LiveEdit);
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001022 thread_local_.after_break_target_ = plain_return->entry();
1023 } else if (thread_local_.frame_drop_mode_ ==
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001024 FRAME_DROPPED_IN_DIRECT_CALL) {
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001025 // Nothing to do, after_break_target is not used here.
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00001026 } else if (thread_local_.frame_drop_mode_ ==
1027 FRAME_DROPPED_IN_RETURN_CALL) {
1028 Code* plain_return = isolate_->builtins()->builtin(
1029 Builtins::kFrameDropper_LiveEdit);
1030 thread_local_.after_break_target_ = plain_return->entry();
ager@chromium.org357bf652010-04-12 11:30:10 +00001031 } else {
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001032 UNREACHABLE();
ager@chromium.org357bf652010-04-12 11:30:10 +00001033 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001034
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001035 return heap->undefined_value();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001036}
1037
1038
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001039RUNTIME_FUNCTION(Object*, Debug_Break) {
1040 return isolate->debug()->Break(args);
1041}
1042
1043
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001044// Check the break point objects for whether one or more are actually
1045// triggered. This function returns a JSArray with the break point objects
1046// which is triggered.
1047Handle<Object> Debug::CheckBreakPoints(Handle<Object> break_point_objects) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00001048 Factory* factory = isolate_->factory();
1049
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001050 // Count the number of break points hit. If there are multiple break points
1051 // they are in a FixedArray.
1052 Handle<FixedArray> break_points_hit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001053 int break_points_hit_count = 0;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001054 ASSERT(!break_point_objects->IsUndefined());
1055 if (break_point_objects->IsFixedArray()) {
1056 Handle<FixedArray> array(FixedArray::cast(*break_point_objects));
lrn@chromium.org7516f052011-03-30 08:52:27 +00001057 break_points_hit = factory->NewFixedArray(array->length());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001058 for (int i = 0; i < array->length(); i++) {
1059 Handle<Object> o(array->get(i));
1060 if (CheckBreakPoint(o)) {
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001061 break_points_hit->set(break_points_hit_count++, *o);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001062 }
1063 }
1064 } else {
lrn@chromium.org7516f052011-03-30 08:52:27 +00001065 break_points_hit = factory->NewFixedArray(1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001066 if (CheckBreakPoint(break_point_objects)) {
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001067 break_points_hit->set(break_points_hit_count++, *break_point_objects);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001068 }
1069 }
1070
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001071 // Return undefined if no break points were triggered.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001072 if (break_points_hit_count == 0) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00001073 return factory->undefined_value();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001074 }
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001075 // Return break points hit as a JSArray.
lrn@chromium.org7516f052011-03-30 08:52:27 +00001076 Handle<JSArray> result = factory->NewJSArrayWithElements(break_points_hit);
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001077 result->set_length(Smi::FromInt(break_points_hit_count));
1078 return result;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001079}
1080
1081
1082// Check whether a single break point object is triggered.
1083bool Debug::CheckBreakPoint(Handle<Object> break_point_object) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00001084 Factory* factory = isolate_->factory();
1085 HandleScope scope(isolate_);
ager@chromium.org381abbb2009-02-25 13:23:22 +00001086
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001087 // Ignore check if break point object is not a JSObject.
1088 if (!break_point_object->IsJSObject()) return true;
1089
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001090 // Get the function IsBreakPointTriggered (defined in debug-debugger.js).
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001091 Handle<String> is_break_point_triggered_symbol =
lrn@chromium.org7516f052011-03-30 08:52:27 +00001092 factory->LookupAsciiSymbol("IsBreakPointTriggered");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001093 Handle<JSFunction> check_break_point =
1094 Handle<JSFunction>(JSFunction::cast(
lrn@chromium.org303ada72010-10-27 09:33:13 +00001095 debug_context()->global()->GetPropertyNoExceptionThrown(
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001096 *is_break_point_triggered_symbol)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001097
1098 // Get the break id as an object.
lrn@chromium.org7516f052011-03-30 08:52:27 +00001099 Handle<Object> break_id = factory->NewNumberFromInt(Debug::break_id());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001100
1101 // Call HandleBreakPointx.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001102 bool caught_exception;
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00001103 Handle<Object> argv[] = { break_id, break_point_object };
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001104 Handle<Object> result = Execution::TryCall(check_break_point,
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00001105 isolate_->js_builtins_object(),
1106 ARRAY_SIZE(argv),
1107 argv,
1108 &caught_exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001109
1110 // If exception or non boolean result handle as not triggered
1111 if (caught_exception || !result->IsBoolean()) {
1112 return false;
1113 }
1114
1115 // Return whether the break point is triggered.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001116 ASSERT(!result.is_null());
1117 return (*result)->IsTrue();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001118}
1119
1120
1121// Check whether the function has debug information.
1122bool Debug::HasDebugInfo(Handle<SharedFunctionInfo> shared) {
1123 return !shared->debug_info()->IsUndefined();
1124}
1125
1126
kasper.lundbd3ec4e2008-07-09 11:06:54 +00001127// Return the debug info for this function. EnsureDebugInfo must be called
1128// prior to ensure the debug info has been generated for shared.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001129Handle<DebugInfo> Debug::GetDebugInfo(Handle<SharedFunctionInfo> shared) {
kasper.lundbd3ec4e2008-07-09 11:06:54 +00001130 ASSERT(HasDebugInfo(shared));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001131 return Handle<DebugInfo>(DebugInfo::cast(shared->debug_info()));
1132}
1133
1134
1135void Debug::SetBreakPoint(Handle<SharedFunctionInfo> shared,
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00001136 Handle<Object> break_point_object,
1137 int* source_position) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00001138 HandleScope scope(isolate_);
ager@chromium.org381abbb2009-02-25 13:23:22 +00001139
lrn@chromium.org34e60782011-09-15 07:25:40 +00001140 PrepareForBreakPoints();
1141
kasper.lundbd3ec4e2008-07-09 11:06:54 +00001142 if (!EnsureDebugInfo(shared)) {
1143 // Return if retrieving debug info failed.
1144 return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001145 }
1146
kasper.lundbd3ec4e2008-07-09 11:06:54 +00001147 Handle<DebugInfo> debug_info = GetDebugInfo(shared);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001148 // Source positions starts with zero.
1149 ASSERT(source_position >= 0);
1150
1151 // Find the break point and change it.
1152 BreakLocationIterator it(debug_info, SOURCE_BREAK_LOCATIONS);
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00001153 it.FindBreakLocationFromPosition(*source_position);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001154 it.SetBreakPoint(break_point_object);
1155
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00001156 *source_position = it.position();
1157
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001158 // At least one active break point now.
1159 ASSERT(debug_info->GetBreakPointCount() > 0);
1160}
1161
1162
1163void Debug::ClearBreakPoint(Handle<Object> break_point_object) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00001164 HandleScope scope(isolate_);
ager@chromium.org381abbb2009-02-25 13:23:22 +00001165
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001166 DebugInfoListNode* node = debug_info_list_;
1167 while (node != NULL) {
1168 Object* result = DebugInfo::FindBreakPointInfo(node->debug_info(),
1169 break_point_object);
1170 if (!result->IsUndefined()) {
1171 // Get information in the break point.
1172 BreakPointInfo* break_point_info = BreakPointInfo::cast(result);
1173 Handle<DebugInfo> debug_info = node->debug_info();
1174 Handle<SharedFunctionInfo> shared(debug_info->shared());
1175 int source_position = break_point_info->statement_position()->value();
1176
1177 // Source positions starts with zero.
1178 ASSERT(source_position >= 0);
1179
1180 // Find the break point and clear it.
1181 BreakLocationIterator it(debug_info, SOURCE_BREAK_LOCATIONS);
1182 it.FindBreakLocationFromPosition(source_position);
1183 it.ClearBreakPoint(break_point_object);
1184
1185 // If there are no more break points left remove the debug info for this
1186 // function.
1187 if (debug_info->GetBreakPointCount() == 0) {
1188 RemoveDebugInfo(debug_info);
1189 }
1190
1191 return;
1192 }
1193 node = node->next();
1194 }
1195}
1196
1197
ager@chromium.org381abbb2009-02-25 13:23:22 +00001198void Debug::ClearAllBreakPoints() {
1199 DebugInfoListNode* node = debug_info_list_;
1200 while (node != NULL) {
1201 // Remove all debug break code.
1202 BreakLocationIterator it(node->debug_info(), ALL_BREAK_LOCATIONS);
1203 it.ClearAllDebugBreak();
1204 node = node->next();
1205 }
1206
1207 // Remove all debug info.
1208 while (debug_info_list_ != NULL) {
1209 RemoveDebugInfo(debug_info_list_->debug_info());
1210 }
1211}
1212
1213
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001214void Debug::FloodWithOneShot(Handle<SharedFunctionInfo> shared) {
lrn@chromium.org34e60782011-09-15 07:25:40 +00001215 PrepareForBreakPoints();
kasper.lundbd3ec4e2008-07-09 11:06:54 +00001216 // Make sure the function has setup the debug info.
1217 if (!EnsureDebugInfo(shared)) {
1218 // Return if we failed to retrieve the debug info.
1219 return;
1220 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001221
1222 // Flood the function with break points.
kasper.lundbd3ec4e2008-07-09 11:06:54 +00001223 BreakLocationIterator it(GetDebugInfo(shared), ALL_BREAK_LOCATIONS);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001224 while (!it.Done()) {
1225 it.SetOneShot();
1226 it.Next();
1227 }
1228}
1229
1230
1231void Debug::FloodHandlerWithOneShot() {
ager@chromium.org8bb60582008-12-11 12:02:20 +00001232 // Iterate through the JavaScript stack looking for handlers.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001233 StackFrame::Id id = break_frame_id();
ager@chromium.org8bb60582008-12-11 12:02:20 +00001234 if (id == StackFrame::NO_ID) {
1235 // If there is no JavaScript stack don't do anything.
1236 return;
1237 }
vegorov@chromium.org74f333b2011-04-06 11:17:46 +00001238 for (JavaScriptFrameIterator it(isolate_, id); !it.done(); it.Advance()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001239 JavaScriptFrame* frame = it.frame();
1240 if (frame->HasHandler()) {
1241 Handle<SharedFunctionInfo> shared =
1242 Handle<SharedFunctionInfo>(
1243 JSFunction::cast(frame->function())->shared());
1244 // Flood the function with the catch block with break points
1245 FloodWithOneShot(shared);
1246 return;
1247 }
1248 }
1249}
1250
1251
1252void Debug::ChangeBreakOnException(ExceptionBreakType type, bool enable) {
1253 if (type == BreakUncaughtException) {
1254 break_on_uncaught_exception_ = enable;
1255 } else {
1256 break_on_exception_ = enable;
1257 }
1258}
1259
1260
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00001261bool Debug::IsBreakOnException(ExceptionBreakType type) {
1262 if (type == BreakUncaughtException) {
1263 return break_on_uncaught_exception_;
1264 } else {
1265 return break_on_exception_;
1266 }
1267}
1268
1269
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001270void Debug::PrepareStep(StepAction step_action, int step_count) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00001271 HandleScope scope(isolate_);
lrn@chromium.org34e60782011-09-15 07:25:40 +00001272
1273 PrepareForBreakPoints();
1274
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001275 ASSERT(Debug::InDebugger());
1276
1277 // Remember this step action and count.
1278 thread_local_.last_step_action_ = step_action;
ager@chromium.orga1645e22009-09-09 19:27:10 +00001279 if (step_action == StepOut) {
1280 // For step out target frame will be found on the stack so there is no need
1281 // to set step counter for it. It's expected to always be 0 for StepOut.
1282 thread_local_.step_count_ = 0;
1283 } else {
1284 thread_local_.step_count_ = step_count;
1285 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001286
1287 // Get the frame where the execution has stopped and skip the debug frame if
1288 // any. The debug frame will only be present if execution was stopped due to
1289 // hitting a break point. In other situations (e.g. unhandled exception) the
1290 // debug frame is not present.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001291 StackFrame::Id id = break_frame_id();
ager@chromium.org8bb60582008-12-11 12:02:20 +00001292 if (id == StackFrame::NO_ID) {
1293 // If there is no JavaScript stack don't do anything.
1294 return;
1295 }
vegorov@chromium.org74f333b2011-04-06 11:17:46 +00001296 JavaScriptFrameIterator frames_it(isolate_, id);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001297 JavaScriptFrame* frame = frames_it.frame();
1298
1299 // First of all ensure there is one-shot break points in the top handler
1300 // if any.
1301 FloodHandlerWithOneShot();
1302
1303 // If the function on the top frame is unresolved perform step out. This will
1304 // be the case when calling unknown functions and having the debugger stopped
1305 // in an unhandled exception.
1306 if (!frame->function()->IsJSFunction()) {
1307 // Step out: Find the calling JavaScript frame and flood it with
1308 // breakpoints.
1309 frames_it.Advance();
1310 // Fill the function to return to with one-shot break points.
1311 JSFunction* function = JSFunction::cast(frames_it.frame()->function());
1312 FloodWithOneShot(Handle<SharedFunctionInfo>(function->shared()));
1313 return;
1314 }
1315
1316 // Get the debug info (create it if it does not exist).
1317 Handle<SharedFunctionInfo> shared =
1318 Handle<SharedFunctionInfo>(JSFunction::cast(frame->function())->shared());
kasper.lundbd3ec4e2008-07-09 11:06:54 +00001319 if (!EnsureDebugInfo(shared)) {
1320 // Return if ensuring debug info failed.
1321 return;
1322 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001323 Handle<DebugInfo> debug_info = GetDebugInfo(shared);
1324
1325 // Find the break location where execution has stopped.
1326 BreakLocationIterator it(debug_info, ALL_BREAK_LOCATIONS);
1327 it.FindBreakLocationFromAddress(frame->pc());
1328
1329 // Compute whether or not the target is a call target.
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001330 bool is_load_or_store = false;
1331 bool is_inline_cache_stub = false;
whesse@chromium.orge90029b2010-08-02 11:52:17 +00001332 bool is_at_restarted_function = false;
ager@chromium.orga1645e22009-09-09 19:27:10 +00001333 Handle<Code> call_function_stub;
ager@chromium.orga1645e22009-09-09 19:27:10 +00001334
whesse@chromium.orge90029b2010-08-02 11:52:17 +00001335 if (thread_local_.restarter_frame_function_pointer_ == NULL) {
1336 if (RelocInfo::IsCodeTarget(it.rinfo()->rmode())) {
1337 bool is_call_target = false;
1338 Address target = it.rinfo()->target_address();
1339 Code* code = Code::GetCodeFromTargetAddress(target);
1340 if (code->is_call_stub() || code->is_keyed_call_stub()) {
1341 is_call_target = true;
1342 }
1343 if (code->is_inline_cache_stub()) {
1344 is_inline_cache_stub = true;
1345 is_load_or_store = !is_call_target;
1346 }
1347
1348 // Check if target code is CallFunction stub.
1349 Code* maybe_call_function_stub = code;
1350 // If there is a breakpoint at this line look at the original code to
1351 // check if it is a CallFunction stub.
1352 if (it.IsDebugBreak()) {
1353 Address original_target = it.original_rinfo()->target_address();
1354 maybe_call_function_stub =
1355 Code::GetCodeFromTargetAddress(original_target);
1356 }
1357 if (maybe_call_function_stub->kind() == Code::STUB &&
1358 maybe_call_function_stub->major_key() == CodeStub::CallFunction) {
1359 // Save reference to the code as we may need it to find out arguments
1360 // count for 'step in' later.
1361 call_function_stub = Handle<Code>(maybe_call_function_stub);
1362 }
ager@chromium.orga1645e22009-09-09 19:27:10 +00001363 }
whesse@chromium.orge90029b2010-08-02 11:52:17 +00001364 } else {
1365 is_at_restarted_function = true;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001366 }
1367
v8.team.kasperl727e9952008-09-02 14:56:44 +00001368 // If this is the last break code target step out is the only possibility.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001369 if (it.IsExit() || step_action == StepOut) {
ager@chromium.orga1645e22009-09-09 19:27:10 +00001370 if (step_action == StepOut) {
1371 // Skip step_count frames starting with the current one.
1372 while (step_count-- > 0 && !frames_it.done()) {
1373 frames_it.Advance();
1374 }
1375 } else {
1376 ASSERT(it.IsExit());
1377 frames_it.Advance();
1378 }
1379 // Skip builtin functions on the stack.
1380 while (!frames_it.done() &&
1381 JSFunction::cast(frames_it.frame()->function())->IsBuiltin()) {
1382 frames_it.Advance();
1383 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001384 // Step out: If there is a JavaScript caller frame, we need to
1385 // flood it with breakpoints.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001386 if (!frames_it.done()) {
1387 // Fill the function to return to with one-shot break points.
1388 JSFunction* function = JSFunction::cast(frames_it.frame()->function());
1389 FloodWithOneShot(Handle<SharedFunctionInfo>(function->shared()));
ager@chromium.orga1645e22009-09-09 19:27:10 +00001390 // Set target frame pointer.
1391 ActivateStepOut(frames_it.frame());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001392 }
ager@chromium.orga1645e22009-09-09 19:27:10 +00001393 } else if (!(is_inline_cache_stub || RelocInfo::IsConstructCall(it.rmode()) ||
whesse@chromium.orge90029b2010-08-02 11:52:17 +00001394 !call_function_stub.is_null() || is_at_restarted_function)
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001395 || step_action == StepNext || step_action == StepMin) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001396 // Step next or step min.
1397
1398 // Fill the current function with one-shot break points.
1399 FloodWithOneShot(shared);
1400
1401 // Remember source position and frame to handle step next.
1402 thread_local_.last_statement_position_ =
1403 debug_info->code()->SourceStatementPosition(frame->pc());
1404 thread_local_.last_fp_ = frame->fp();
1405 } else {
whesse@chromium.orge90029b2010-08-02 11:52:17 +00001406 // If there's restarter frame on top of the stack, just get the pointer
1407 // to function which is going to be restarted.
1408 if (is_at_restarted_function) {
1409 Handle<JSFunction> restarted_function(
1410 JSFunction::cast(*thread_local_.restarter_frame_function_pointer_));
1411 Handle<SharedFunctionInfo> restarted_shared(
1412 restarted_function->shared());
1413 FloodWithOneShot(restarted_shared);
1414 } else if (!call_function_stub.is_null()) {
1415 // If it's CallFunction stub ensure target function is compiled and flood
1416 // it with one shot breakpoints.
1417
ager@chromium.orga1645e22009-09-09 19:27:10 +00001418 // Find out number of arguments from the stub minor key.
1419 // Reverse lookup required as the minor key cannot be retrieved
1420 // from the code object.
1421 Handle<Object> obj(
lrn@chromium.org7516f052011-03-30 08:52:27 +00001422 isolate_->heap()->code_stubs()->SlowReverseLookup(
1423 *call_function_stub));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001424 ASSERT(!obj.is_null());
1425 ASSERT(!(*obj)->IsUndefined());
ager@chromium.orga1645e22009-09-09 19:27:10 +00001426 ASSERT(obj->IsSmi());
1427 // Get the STUB key and extract major and minor key.
1428 uint32_t key = Smi::cast(*obj)->value();
1429 // Argc in the stub is the number of arguments passed - not the
1430 // expected arguments of the called function.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001431 int call_function_arg_count =
1432 CallFunctionStub::ExtractArgcFromMinorKey(
1433 CodeStub::MinorKeyFromKey(key));
ager@chromium.orga1645e22009-09-09 19:27:10 +00001434 ASSERT(call_function_stub->major_key() ==
1435 CodeStub::MajorKeyFromKey(key));
1436
1437 // Find target function on the expression stack.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001438 // Expression stack looks like this (top to bottom):
ager@chromium.orga1645e22009-09-09 19:27:10 +00001439 // argN
1440 // ...
1441 // arg0
1442 // Receiver
1443 // Function to call
1444 int expressions_count = frame->ComputeExpressionsCount();
1445 ASSERT(expressions_count - 2 - call_function_arg_count >= 0);
1446 Object* fun = frame->GetExpression(
1447 expressions_count - 2 - call_function_arg_count);
1448 if (fun->IsJSFunction()) {
1449 Handle<JSFunction> js_function(JSFunction::cast(fun));
1450 // Don't step into builtins.
1451 if (!js_function->IsBuiltin()) {
1452 // It will also compile target function if it's not compiled yet.
1453 FloodWithOneShot(Handle<SharedFunctionInfo>(js_function->shared()));
1454 }
1455 }
1456 }
1457
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001458 // Fill the current function with one-shot break points even for step in on
1459 // a call target as the function called might be a native function for
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001460 // which step in will not stop. It also prepares for stepping in
1461 // getters/setters.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001462 FloodWithOneShot(shared);
1463
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001464 if (is_load_or_store) {
1465 // Remember source position and frame to handle step in getter/setter. If
1466 // there is a custom getter/setter it will be handled in
1467 // Object::Get/SetPropertyWithCallback, otherwise the step action will be
1468 // propagated on the next Debug::Break.
1469 thread_local_.last_statement_position_ =
1470 debug_info->code()->SourceStatementPosition(frame->pc());
1471 thread_local_.last_fp_ = frame->fp();
1472 }
1473
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001474 // Step in or Step in min
1475 it.PrepareStepIn();
1476 ActivateStepIn(frame);
1477 }
1478}
1479
1480
1481// Check whether the current debug break should be reported to the debugger. It
1482// is used to have step next and step in only report break back to the debugger
1483// if on a different frame or in a different statement. In some situations
1484// there will be several break points in the same statement when the code is
v8.team.kasperl727e9952008-09-02 14:56:44 +00001485// flooded with one-shot break points. This function helps to perform several
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001486// steps before reporting break back to the debugger.
1487bool Debug::StepNextContinue(BreakLocationIterator* break_location_iterator,
1488 JavaScriptFrame* frame) {
lrn@chromium.org34e60782011-09-15 07:25:40 +00001489 // StepNext and StepOut shouldn't bring us deeper in code, so last frame
1490 // shouldn't be a parent of current frame.
1491 if (thread_local_.last_step_action_ == StepNext ||
1492 thread_local_.last_step_action_ == StepOut) {
1493 if (frame->fp() < thread_local_.last_fp_) return true;
1494 }
1495
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001496 // If the step last action was step next or step in make sure that a new
1497 // statement is hit.
1498 if (thread_local_.last_step_action_ == StepNext ||
1499 thread_local_.last_step_action_ == StepIn) {
1500 // Never continue if returning from function.
1501 if (break_location_iterator->IsExit()) return false;
1502
1503 // Continue if we are still on the same frame and in the same statement.
1504 int current_statement_position =
1505 break_location_iterator->code()->SourceStatementPosition(frame->pc());
1506 return thread_local_.last_fp_ == frame->fp() &&
ager@chromium.org236ad962008-09-25 09:45:57 +00001507 thread_local_.last_statement_position_ == current_statement_position;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001508 }
1509
1510 // No step next action - don't continue.
1511 return false;
1512}
1513
1514
1515// Check whether the code object at the specified address is a debug break code
1516// object.
1517bool Debug::IsDebugBreak(Address addr) {
ager@chromium.org8bb60582008-12-11 12:02:20 +00001518 Code* code = Code::GetCodeFromTargetAddress(addr);
kasper.lund7276f142008-07-30 08:49:36 +00001519 return code->ic_state() == DEBUG_BREAK;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001520}
1521
1522
1523// Check whether a code stub with the specified major key is a possible break
1524// point location when looking for source break locations.
1525bool Debug::IsSourceBreakStub(Code* code) {
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001526 CodeStub::Major major_key = CodeStub::GetMajorKey(code);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001527 return major_key == CodeStub::CallFunction;
1528}
1529
1530
1531// Check whether a code stub with the specified major key is a possible break
1532// location.
1533bool Debug::IsBreakStub(Code* code) {
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001534 CodeStub::Major major_key = CodeStub::GetMajorKey(code);
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00001535 return major_key == CodeStub::CallFunction;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001536}
1537
1538
1539// Find the builtin to use for invoking the debug break
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001540Handle<Code> Debug::FindDebugBreak(Handle<Code> code, RelocInfo::Mode mode) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001541 // Find the builtin debug break function matching the calling convention
1542 // used by the call site.
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001543 if (code->is_inline_cache_stub()) {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001544 switch (code->kind()) {
1545 case Code::CALL_IC:
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001546 case Code::KEYED_CALL_IC:
1547 return ComputeCallDebugBreak(code->arguments_count(), code->kind());
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001548
1549 case Code::LOAD_IC:
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00001550 return Isolate::Current()->builtins()->LoadIC_DebugBreak();
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001551
1552 case Code::STORE_IC:
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00001553 return Isolate::Current()->builtins()->StoreIC_DebugBreak();
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001554
1555 case Code::KEYED_LOAD_IC:
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00001556 return Isolate::Current()->builtins()->KeyedLoadIC_DebugBreak();
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001557
1558 case Code::KEYED_STORE_IC:
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00001559 return Isolate::Current()->builtins()->KeyedStoreIC_DebugBreak();
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001560
1561 default:
1562 UNREACHABLE();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001563 }
1564 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001565 if (RelocInfo::IsConstructCall(mode)) {
1566 Handle<Code> result =
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00001567 Isolate::Current()->builtins()->ConstructCall_DebugBreak();
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001568 return result;
1569 }
1570 if (code->kind() == Code::STUB) {
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00001571 ASSERT(code->major_key() == CodeStub::CallFunction);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001572 Handle<Code> result =
danno@chromium.orgc612e022011-11-10 11:38:15 +00001573 Isolate::Current()->builtins()->CallFunctionStub_DebugBreak();
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001574 return result;
1575 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001576
1577 UNREACHABLE();
1578 return Handle<Code>::null();
1579}
1580
1581
1582// Simple function for returning the source positions for active break points.
1583Handle<Object> Debug::GetSourceBreakLocations(
1584 Handle<SharedFunctionInfo> shared) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00001585 Isolate* isolate = Isolate::Current();
1586 Heap* heap = isolate->heap();
1587 if (!HasDebugInfo(shared)) return Handle<Object>(heap->undefined_value());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001588 Handle<DebugInfo> debug_info = GetDebugInfo(shared);
1589 if (debug_info->GetBreakPointCount() == 0) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00001590 return Handle<Object>(heap->undefined_value());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001591 }
1592 Handle<FixedArray> locations =
lrn@chromium.org7516f052011-03-30 08:52:27 +00001593 isolate->factory()->NewFixedArray(debug_info->GetBreakPointCount());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001594 int count = 0;
1595 for (int i = 0; i < debug_info->break_points()->length(); i++) {
1596 if (!debug_info->break_points()->get(i)->IsUndefined()) {
1597 BreakPointInfo* break_point_info =
1598 BreakPointInfo::cast(debug_info->break_points()->get(i));
1599 if (break_point_info->GetBreakPointCount() > 0) {
1600 locations->set(count++, break_point_info->statement_position());
1601 }
1602 }
1603 }
1604 return locations;
1605}
1606
1607
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001608void Debug::NewBreak(StackFrame::Id break_frame_id) {
1609 thread_local_.break_frame_id_ = break_frame_id;
1610 thread_local_.break_id_ = ++thread_local_.break_count_;
1611}
1612
1613
1614void Debug::SetBreak(StackFrame::Id break_frame_id, int break_id) {
1615 thread_local_.break_frame_id_ = break_frame_id;
1616 thread_local_.break_id_ = break_id;
1617}
1618
1619
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001620// Handle stepping into a function.
1621void Debug::HandleStepIn(Handle<JSFunction> function,
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001622 Handle<Object> holder,
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001623 Address fp,
1624 bool is_constructor) {
1625 // If the frame pointer is not supplied by the caller find it.
1626 if (fp == 0) {
1627 StackFrameIterator it;
1628 it.Advance();
1629 // For constructor functions skip another frame.
1630 if (is_constructor) {
1631 ASSERT(it.frame()->is_construct());
1632 it.Advance();
1633 }
1634 fp = it.frame()->fp();
1635 }
1636
1637 // Flood the function with one-shot break points if it is called from where
1638 // step into was requested.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001639 if (fp == step_in_fp()) {
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001640 // Don't allow step into functions in the native context.
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001641 if (!function->IsBuiltin()) {
kasperl@chromium.orgacae3782009-04-11 09:17:08 +00001642 if (function->shared()->code() ==
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00001643 Isolate::Current()->builtins()->builtin(Builtins::kFunctionApply) ||
kasperl@chromium.orgacae3782009-04-11 09:17:08 +00001644 function->shared()->code() ==
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00001645 Isolate::Current()->builtins()->builtin(Builtins::kFunctionCall)) {
kasperl@chromium.orgacae3782009-04-11 09:17:08 +00001646 // Handle function.apply and function.call separately to flood the
1647 // function to be called and not the code for Builtins::FunctionApply or
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001648 // Builtins::FunctionCall. The receiver of call/apply is the target
1649 // function.
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001650 if (!holder.is_null() && holder->IsJSFunction() &&
1651 !JSFunction::cast(*holder)->IsBuiltin()) {
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001652 Handle<SharedFunctionInfo> shared_info(
1653 JSFunction::cast(*holder)->shared());
1654 Debug::FloodWithOneShot(shared_info);
kasperl@chromium.orgacae3782009-04-11 09:17:08 +00001655 }
1656 } else {
1657 Debug::FloodWithOneShot(Handle<SharedFunctionInfo>(function->shared()));
1658 }
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001659 }
1660 }
1661}
1662
1663
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001664void Debug::ClearStepping() {
1665 // Clear the various stepping setup.
1666 ClearOneShot();
1667 ClearStepIn();
ager@chromium.orga1645e22009-09-09 19:27:10 +00001668 ClearStepOut();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001669 ClearStepNext();
1670
1671 // Clear multiple step counter.
1672 thread_local_.step_count_ = 0;
1673}
1674
1675// Clears all the one-shot break points that are currently set. Normally this
1676// function is called each time a break point is hit as one shot break points
1677// are used to support stepping.
1678void Debug::ClearOneShot() {
1679 // The current implementation just runs through all the breakpoints. When the
v8.team.kasperl727e9952008-09-02 14:56:44 +00001680 // last break point for a function is removed that function is automatically
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001681 // removed from the list.
1682
1683 DebugInfoListNode* node = debug_info_list_;
1684 while (node != NULL) {
1685 BreakLocationIterator it(node->debug_info(), ALL_BREAK_LOCATIONS);
1686 while (!it.Done()) {
1687 it.ClearOneShot();
1688 it.Next();
1689 }
1690 node = node->next();
1691 }
1692}
1693
1694
1695void Debug::ActivateStepIn(StackFrame* frame) {
ager@chromium.orga1645e22009-09-09 19:27:10 +00001696 ASSERT(!StepOutActive());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001697 thread_local_.step_into_fp_ = frame->fp();
1698}
1699
1700
1701void Debug::ClearStepIn() {
1702 thread_local_.step_into_fp_ = 0;
1703}
1704
1705
ager@chromium.orga1645e22009-09-09 19:27:10 +00001706void Debug::ActivateStepOut(StackFrame* frame) {
1707 ASSERT(!StepInActive());
1708 thread_local_.step_out_fp_ = frame->fp();
1709}
1710
1711
1712void Debug::ClearStepOut() {
1713 thread_local_.step_out_fp_ = 0;
1714}
1715
1716
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001717void Debug::ClearStepNext() {
1718 thread_local_.last_step_action_ = StepNone;
ager@chromium.org236ad962008-09-25 09:45:57 +00001719 thread_local_.last_statement_position_ = RelocInfo::kNoPosition;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001720 thread_local_.last_fp_ = 0;
1721}
1722
1723
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001724// Helper function to compile full code for debugging. This code will
1725// have debug break slots and deoptimization
1726// information. Deoptimization information is required in case that an
1727// optimized version of this function is still activated on the
1728// stack. It will also make sure that the full code is compiled with
1729// the same flags as the previous version - that is flags which can
1730// change the code generated. The current method of mapping from
1731// already compiled full code without debug break slots to full code
1732// with debug break slots depends on the generated code is otherwise
1733// exactly the same.
1734static bool CompileFullCodeForDebugging(Handle<SharedFunctionInfo> shared,
1735 Handle<Code> current_code) {
1736 ASSERT(!current_code->has_debug_break_slots());
1737
1738 CompilationInfo info(shared);
1739 info.MarkCompilingForDebugging(current_code);
1740 ASSERT(!info.shared_info()->is_compiled());
1741 ASSERT(!info.isolate()->has_pending_exception());
1742
1743 // Use compile lazy which will end up compiling the full code in the
1744 // configuration configured above.
1745 bool result = Compiler::CompileLazy(&info);
1746 ASSERT(result != Isolate::Current()->has_pending_exception());
1747 info.isolate()->clear_pending_exception();
1748#if DEBUG
1749 if (result) {
1750 Handle<Code> new_code(shared->code());
1751 ASSERT(new_code->has_debug_break_slots());
1752 ASSERT(current_code->is_compiled_optimizable() ==
1753 new_code->is_compiled_optimizable());
1754 ASSERT(current_code->instruction_size() <= new_code->instruction_size());
1755 }
1756#endif
1757 return result;
1758}
1759
1760
lrn@chromium.org34e60782011-09-15 07:25:40 +00001761void Debug::PrepareForBreakPoints() {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001762 // If preparing for the first break point make sure to deoptimize all
1763 // functions as debugging does not work with optimized code.
1764 if (!has_break_points_) {
1765 Deoptimizer::DeoptimizeAll();
lrn@chromium.org34e60782011-09-15 07:25:40 +00001766
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001767 Handle<Code> lazy_compile =
1768 Handle<Code>(isolate_->builtins()->builtin(Builtins::kLazyCompile));
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001769
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001770 // Keep the list of activated functions in a handlified list as it
1771 // is used both in GC and non-GC code.
1772 List<Handle<JSFunction> > active_functions(100);
lrn@chromium.org34e60782011-09-15 07:25:40 +00001773
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001774 {
1775 // We are going to iterate heap to find all functions without
1776 // debug break slots.
1777 isolate_->heap()->CollectAllGarbage(Heap::kMakeHeapIterableMask);
1778
1779 // Ensure no GC in this scope as we are comparing raw pointer
1780 // values and performing a heap iteration.
1781 AssertNoAllocation no_allocation;
1782
1783 // Find all non-optimized code functions with activation frames on
1784 // the stack.
1785 for (JavaScriptFrameIterator it(isolate_); !it.done(); it.Advance()) {
1786 JavaScriptFrame* frame = it.frame();
1787 if (frame->function()->IsJSFunction()) {
1788 JSFunction* function = JSFunction::cast(frame->function());
1789 if (function->code()->kind() == Code::FUNCTION &&
1790 !function->code()->has_debug_break_slots())
1791 active_functions.Add(Handle<JSFunction>(function));
1792 }
1793 }
1794 // Sort the functions on the object pointer value to prepare for
1795 // the binary search below.
1796 active_functions.Sort(HandleObjectPointerCompare<JSFunction>);
1797
1798 // Scan the heap for all non-optimized functions which has no
1799 // debug break slots.
1800 HeapIterator iterator;
1801 HeapObject* obj = NULL;
1802 while (((obj = iterator.next()) != NULL)) {
1803 if (obj->IsJSFunction()) {
1804 JSFunction* function = JSFunction::cast(obj);
1805 if (function->shared()->allows_lazy_compilation() &&
1806 function->shared()->script()->IsScript() &&
1807 function->code()->kind() == Code::FUNCTION &&
1808 !function->code()->has_debug_break_slots()) {
1809 bool has_activation =
1810 SortedListBSearch<Handle<JSFunction> >(
1811 active_functions,
1812 Handle<JSFunction>(function),
1813 HandleObjectPointerCompare<JSFunction>) != -1;
1814 if (!has_activation) {
1815 function->set_code(*lazy_compile);
1816 function->shared()->set_code(*lazy_compile);
1817 }
1818 }
1819 }
lrn@chromium.org34e60782011-09-15 07:25:40 +00001820 }
1821 }
lrn@chromium.org34e60782011-09-15 07:25:40 +00001822
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001823 // Now the non-GC scope is left, and the sorting of the functions
1824 // in active_function is not ensured any more. The code below does
1825 // not rely on it.
1826
1827 // Now recompile all functions with activation frames and and
1828 // patch the return address to run in the new compiled code.
1829 for (int i = 0; i < active_functions.length(); i++) {
1830 Handle<JSFunction> function = active_functions[i];
1831 Handle<SharedFunctionInfo> shared(function->shared());
1832 // If recompilation is not possible just skip it.
1833 if (shared->is_toplevel() ||
1834 !shared->allows_lazy_compilation() ||
1835 shared->code()->kind() == Code::BUILTIN) {
1836 continue;
1837 }
1838
1839 // Make sure that the shared full code is compiled with debug
1840 // break slots.
1841 Handle<Code> current_code(function->code());
1842 if (shared->code()->has_debug_break_slots()) {
1843 // if the code is already recompiled to have break slots skip
1844 // recompilation.
1845 ASSERT(!function->code()->has_debug_break_slots());
1846 } else {
1847 // Try to compile the full code with debug break slots. If it
1848 // fails just keep the current code.
1849 ASSERT(shared->code() == *current_code);
1850 ZoneScope zone_scope(isolate_, DELETE_ON_EXIT);
1851 shared->set_code(*lazy_compile);
1852 bool prev_force_debugger_active =
1853 isolate_->debugger()->force_debugger_active();
1854 isolate_->debugger()->set_force_debugger_active(true);
1855 CompileFullCodeForDebugging(shared, current_code);
1856 isolate_->debugger()->set_force_debugger_active(
1857 prev_force_debugger_active);
1858 if (!shared->is_compiled()) {
1859 shared->set_code(*current_code);
1860 continue;
1861 }
1862 }
1863 Handle<Code> new_code(shared->code());
1864
1865 // Find the function and patch return address.
1866 for (JavaScriptFrameIterator it(isolate_); !it.done(); it.Advance()) {
1867 JavaScriptFrame* frame = it.frame();
1868 // If the current frame is for this function in its
1869 // non-optimized form rewrite the return address to continue
1870 // in the newly compiled full code with debug break slots.
1871 if (frame->function()->IsJSFunction() &&
1872 frame->function() == *function &&
1873 frame->LookupCode()->kind() == Code::FUNCTION) {
1874 intptr_t delta = frame->pc() - current_code->instruction_start();
1875 int debug_break_slot_count = 0;
1876 int mask = RelocInfo::ModeMask(RelocInfo::DEBUG_BREAK_SLOT);
1877 for (RelocIterator it(*new_code, mask); !it.done(); it.next()) {
1878 // Check if the pc in the new code with debug break
1879 // slots is before this slot.
1880 RelocInfo* info = it.rinfo();
1881 int debug_break_slot_bytes =
1882 debug_break_slot_count * Assembler::kDebugBreakSlotLength;
1883 intptr_t new_delta =
1884 info->pc() -
1885 new_code->instruction_start() -
1886 debug_break_slot_bytes;
1887 if (new_delta > delta) {
1888 break;
1889 }
1890
1891 // Passed a debug break slot in the full code with debug
1892 // break slots.
1893 debug_break_slot_count++;
lrn@chromium.org34e60782011-09-15 07:25:40 +00001894 }
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001895 int debug_break_slot_bytes =
1896 debug_break_slot_count * Assembler::kDebugBreakSlotLength;
1897 if (FLAG_trace_deopt) {
1898 PrintF("Replacing code %08" V8PRIxPTR " - %08" V8PRIxPTR " (%d) "
1899 "with %08" V8PRIxPTR " - %08" V8PRIxPTR " (%d) "
1900 "for debugging, "
1901 "changing pc from %08" V8PRIxPTR " to %08" V8PRIxPTR "\n",
1902 reinterpret_cast<intptr_t>(
1903 current_code->instruction_start()),
1904 reinterpret_cast<intptr_t>(
1905 current_code->instruction_start()) +
1906 current_code->instruction_size(),
1907 current_code->instruction_size(),
1908 reinterpret_cast<intptr_t>(new_code->instruction_start()),
1909 reinterpret_cast<intptr_t>(new_code->instruction_start()) +
1910 new_code->instruction_size(),
1911 new_code->instruction_size(),
1912 reinterpret_cast<intptr_t>(frame->pc()),
1913 reinterpret_cast<intptr_t>(new_code->instruction_start()) +
1914 delta + debug_break_slot_bytes);
1915 }
1916
1917 // Patch the return address to return into the code with
1918 // debug break slots.
1919 frame->set_pc(
1920 new_code->instruction_start() + delta + debug_break_slot_bytes);
lrn@chromium.org34e60782011-09-15 07:25:40 +00001921 }
1922 }
1923 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001924 }
lrn@chromium.org34e60782011-09-15 07:25:40 +00001925}
1926
1927
1928// Ensures the debug information is present for shared.
1929bool Debug::EnsureDebugInfo(Handle<SharedFunctionInfo> shared) {
1930 // Return if we already have the debug info for shared.
1931 if (HasDebugInfo(shared)) {
1932 ASSERT(shared->is_compiled());
1933 return true;
1934 }
1935
1936 // Ensure shared in compiled. Return false if this failed.
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001937 if (!SharedFunctionInfo::EnsureCompiled(shared, CLEAR_EXCEPTION)) {
1938 return false;
1939 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001940
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001941 // Create the debug info object.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001942 Handle<DebugInfo> debug_info = FACTORY->NewDebugInfo(shared);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001943
1944 // Add debug info to the list.
1945 DebugInfoListNode* node = new DebugInfoListNode(*debug_info);
1946 node->set_next(debug_info_list_);
1947 debug_info_list_ = node;
1948
1949 // Now there is at least one break point.
1950 has_break_points_ = true;
1951
kasper.lundbd3ec4e2008-07-09 11:06:54 +00001952 return true;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001953}
1954
1955
1956void Debug::RemoveDebugInfo(Handle<DebugInfo> debug_info) {
1957 ASSERT(debug_info_list_ != NULL);
1958 // Run through the debug info objects to find this one and remove it.
1959 DebugInfoListNode* prev = NULL;
1960 DebugInfoListNode* current = debug_info_list_;
1961 while (current != NULL) {
1962 if (*current->debug_info() == *debug_info) {
1963 // Unlink from list. If prev is NULL we are looking at the first element.
1964 if (prev == NULL) {
1965 debug_info_list_ = current->next();
1966 } else {
1967 prev->set_next(current->next());
1968 }
lrn@chromium.org7516f052011-03-30 08:52:27 +00001969 current->debug_info()->shared()->set_debug_info(
1970 isolate_->heap()->undefined_value());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001971 delete current;
1972
1973 // If there are no more debug info objects there are not more break
1974 // points.
1975 has_break_points_ = debug_info_list_ != NULL;
1976
1977 return;
1978 }
1979 // Move to next in list.
1980 prev = current;
1981 current = current->next();
1982 }
1983 UNREACHABLE();
1984}
1985
1986
1987void Debug::SetAfterBreakTarget(JavaScriptFrame* frame) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00001988 HandleScope scope(isolate_);
ager@chromium.org381abbb2009-02-25 13:23:22 +00001989
lrn@chromium.org34e60782011-09-15 07:25:40 +00001990 PrepareForBreakPoints();
1991
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001992 // Get the executing function in which the debug break occurred.
1993 Handle<SharedFunctionInfo> shared =
1994 Handle<SharedFunctionInfo>(JSFunction::cast(frame->function())->shared());
kasper.lundbd3ec4e2008-07-09 11:06:54 +00001995 if (!EnsureDebugInfo(shared)) {
1996 // Return if we failed to retrieve the debug info.
1997 return;
1998 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001999 Handle<DebugInfo> debug_info = GetDebugInfo(shared);
2000 Handle<Code> code(debug_info->code());
2001 Handle<Code> original_code(debug_info->original_code());
2002#ifdef DEBUG
2003 // Get the code which is actually executing.
vegorov@chromium.org74f333b2011-04-06 11:17:46 +00002004 Handle<Code> frame_code(frame->LookupCode());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002005 ASSERT(frame_code.is_identical_to(code));
2006#endif
2007
2008 // Find the call address in the running code. This address holds the call to
2009 // either a DebugBreakXXX or to the debug break return entry code if the
2010 // break point is still active after processing the break point.
ager@chromium.org4af710e2009-09-15 12:20:11 +00002011 Address addr = frame->pc() - Assembler::kCallTargetAddressOffset;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002012
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002013 // Check if the location is at JS exit or debug break slot.
ager@chromium.orga1645e22009-09-09 19:27:10 +00002014 bool at_js_return = false;
2015 bool break_at_js_return_active = false;
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002016 bool at_debug_break_slot = false;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002017 RelocIterator it(debug_info->code());
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002018 while (!it.done() && !at_js_return && !at_debug_break_slot) {
ager@chromium.org236ad962008-09-25 09:45:57 +00002019 if (RelocInfo::IsJSReturn(it.rinfo()->rmode())) {
ager@chromium.orga1645e22009-09-09 19:27:10 +00002020 at_js_return = (it.rinfo()->pc() ==
2021 addr - Assembler::kPatchReturnSequenceAddressOffset);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002022 break_at_js_return_active = it.rinfo()->IsPatchedReturnSequence();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002023 }
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002024 if (RelocInfo::IsDebugBreakSlot(it.rinfo()->rmode())) {
2025 at_debug_break_slot = (it.rinfo()->pc() ==
2026 addr - Assembler::kPatchDebugBreakSlotAddressOffset);
2027 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002028 it.next();
2029 }
2030
2031 // Handle the jump to continue execution after break point depending on the
2032 // break location.
ager@chromium.orga1645e22009-09-09 19:27:10 +00002033 if (at_js_return) {
2034 // If the break point as return is still active jump to the corresponding
2035 // place in the original code. If not the break point was removed during
2036 // break point processing.
2037 if (break_at_js_return_active) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002038 addr += original_code->instruction_start() - code->instruction_start();
2039 }
2040
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00002041 // Move back to where the call instruction sequence started.
2042 thread_local_.after_break_target_ =
2043 addr - Assembler::kPatchReturnSequenceAddressOffset;
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002044 } else if (at_debug_break_slot) {
2045 // Address of where the debug break slot starts.
2046 addr = addr - Assembler::kPatchDebugBreakSlotAddressOffset;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002047
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002048 // Continue just after the slot.
2049 thread_local_.after_break_target_ = addr + Assembler::kDebugBreakSlotLength;
2050 } else if (IsDebugBreak(Assembler::target_address_at(addr))) {
2051 // We now know that there is still a debug break call at the target address,
2052 // so the break point is still there and the original code will hold the
2053 // address to jump to in order to complete the call which is replaced by a
2054 // call to DebugBreakXXX.
2055
2056 // Find the corresponding address in the original code.
2057 addr += original_code->instruction_start() - code->instruction_start();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002058
2059 // Install jump to the call address in the original code. This will be the
2060 // call which was overwritten by the call to DebugBreakXXX.
2061 thread_local_.after_break_target_ = Assembler::target_address_at(addr);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002062 } else {
2063 // There is no longer a break point present. Don't try to look in the
2064 // original code as the running code will have the right address. This takes
2065 // care of the case where the last break point is removed from the function
2066 // and therefore no "original code" is available.
2067 thread_local_.after_break_target_ = Assembler::target_address_at(addr);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002068 }
2069}
2070
2071
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00002072bool Debug::IsBreakAtReturn(JavaScriptFrame* frame) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002073 HandleScope scope(isolate_);
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00002074
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00002075 // If there are no break points this cannot be break at return, as
2076 // the debugger statement and stack guard bebug break cannot be at
2077 // return.
2078 if (!has_break_points_) {
2079 return false;
2080 }
2081
lrn@chromium.org34e60782011-09-15 07:25:40 +00002082 PrepareForBreakPoints();
2083
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00002084 // Get the executing function in which the debug break occurred.
2085 Handle<SharedFunctionInfo> shared =
2086 Handle<SharedFunctionInfo>(JSFunction::cast(frame->function())->shared());
2087 if (!EnsureDebugInfo(shared)) {
2088 // Return if we failed to retrieve the debug info.
2089 return false;
2090 }
2091 Handle<DebugInfo> debug_info = GetDebugInfo(shared);
2092 Handle<Code> code(debug_info->code());
2093#ifdef DEBUG
2094 // Get the code which is actually executing.
vegorov@chromium.org74f333b2011-04-06 11:17:46 +00002095 Handle<Code> frame_code(frame->LookupCode());
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00002096 ASSERT(frame_code.is_identical_to(code));
2097#endif
2098
2099 // Find the call address in the running code.
2100 Address addr = frame->pc() - Assembler::kCallTargetAddressOffset;
2101
2102 // Check if the location is at JS return.
2103 RelocIterator it(debug_info->code());
2104 while (!it.done()) {
2105 if (RelocInfo::IsJSReturn(it.rinfo()->rmode())) {
2106 return (it.rinfo()->pc() ==
2107 addr - Assembler::kPatchReturnSequenceAddressOffset);
2108 }
2109 it.next();
2110 }
2111 return false;
2112}
2113
2114
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00002115void Debug::FramesHaveBeenDropped(StackFrame::Id new_break_frame_id,
whesse@chromium.orge90029b2010-08-02 11:52:17 +00002116 FrameDropMode mode,
2117 Object** restarter_frame_function_pointer) {
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00002118 thread_local_.frame_drop_mode_ = mode;
ager@chromium.org357bf652010-04-12 11:30:10 +00002119 thread_local_.break_frame_id_ = new_break_frame_id;
whesse@chromium.orge90029b2010-08-02 11:52:17 +00002120 thread_local_.restarter_frame_function_pointer_ =
2121 restarter_frame_function_pointer;
ager@chromium.org357bf652010-04-12 11:30:10 +00002122}
2123
2124
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002125bool Debug::IsDebugGlobal(GlobalObject* global) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002126 return IsLoaded() && global == debug_context()->global();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002127}
2128
2129
ager@chromium.org32912102009-01-16 10:38:43 +00002130void Debug::ClearMirrorCache() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002131 PostponeInterruptsScope postpone(isolate_);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002132 HandleScope scope(isolate_);
2133 ASSERT(isolate_->context() == *Debug::debug_context());
ager@chromium.org32912102009-01-16 10:38:43 +00002134
2135 // Clear the mirror cache.
2136 Handle<String> function_name =
lrn@chromium.org7516f052011-03-30 08:52:27 +00002137 isolate_->factory()->LookupSymbol(CStrVector("ClearMirrorCache"));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002138 Handle<Object> fun(Isolate::Current()->global()->GetPropertyNoExceptionThrown(
lrn@chromium.org303ada72010-10-27 09:33:13 +00002139 *function_name));
ager@chromium.org32912102009-01-16 10:38:43 +00002140 ASSERT(fun->IsJSFunction());
2141 bool caught_exception;
rossberg@chromium.org717967f2011-07-20 13:44:42 +00002142 Execution::TryCall(Handle<JSFunction>::cast(fun),
ager@chromium.org32912102009-01-16 10:38:43 +00002143 Handle<JSObject>(Debug::debug_context()->global()),
2144 0, NULL, &caught_exception);
2145}
2146
2147
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002148void Debug::CreateScriptCache() {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002149 Heap* heap = isolate_->heap();
2150 HandleScope scope(isolate_);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002151
2152 // Perform two GCs to get rid of all unreferenced scripts. The first GC gets
2153 // rid of all the cached script wrappers and the second gets rid of the
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002154 // scripts which are no longer referenced. The second also sweeps precisely,
2155 // which saves us doing yet another GC to make the heap iterable.
2156 heap->CollectAllGarbage(Heap::kNoGCFlags);
2157 heap->CollectAllGarbage(Heap::kMakeHeapIterableMask);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002158
2159 ASSERT(script_cache_ == NULL);
2160 script_cache_ = new ScriptCache();
2161
2162 // Scan heap for Script objects.
2163 int count = 0;
2164 HeapIterator iterator;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002165 AssertNoAllocation no_allocation;
2166
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002167 for (HeapObject* obj = iterator.next(); obj != NULL; obj = iterator.next()) {
sgjesse@chromium.org152a0b02009-10-07 13:50:16 +00002168 if (obj->IsScript() && Script::cast(obj)->HasValidSource()) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002169 script_cache_->Add(Handle<Script>(Script::cast(obj)));
2170 count++;
2171 }
2172 }
2173}
2174
2175
2176void Debug::DestroyScriptCache() {
2177 // Get rid of the script cache if it was created.
2178 if (script_cache_ != NULL) {
2179 delete script_cache_;
2180 script_cache_ = NULL;
2181 }
2182}
2183
2184
2185void Debug::AddScriptToScriptCache(Handle<Script> script) {
2186 if (script_cache_ != NULL) {
2187 script_cache_->Add(script);
2188 }
2189}
2190
2191
2192Handle<FixedArray> Debug::GetLoadedScripts() {
2193 // Create and fill the script cache when the loaded scripts is requested for
2194 // the first time.
2195 if (script_cache_ == NULL) {
2196 CreateScriptCache();
2197 }
2198
2199 // If the script cache is not active just return an empty array.
2200 ASSERT(script_cache_ != NULL);
2201 if (script_cache_ == NULL) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002202 isolate_->factory()->NewFixedArray(0);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002203 }
2204
2205 // Perform GC to get unreferenced scripts evicted from the cache before
2206 // returning the content.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002207 isolate_->heap()->CollectAllGarbage(Heap::kNoGCFlags);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002208
2209 // Get the scripts from the cache.
2210 return script_cache_->GetScripts();
2211}
2212
2213
2214void Debug::AfterGarbageCollection() {
2215 // Generate events for collected scripts.
2216 if (script_cache_ != NULL) {
2217 script_cache_->ProcessCollectedScripts();
2218 }
2219}
2220
2221
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00002222Debugger::Debugger(Isolate* isolate)
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +00002223 : debugger_access_(isolate->debugger_access()),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002224 event_listener_(Handle<Object>()),
2225 event_listener_data_(Handle<Object>()),
2226 compiling_natives_(false),
2227 is_loading_debugger_(false),
2228 never_unload_debugger_(false),
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002229 force_debugger_active_(false),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002230 message_handler_(NULL),
2231 debugger_unload_pending_(false),
2232 host_dispatch_handler_(NULL),
2233 dispatch_handler_access_(OS::CreateMutex()),
2234 debug_message_dispatch_handler_(NULL),
2235 message_dispatch_helper_thread_(NULL),
2236 host_dispatch_micros_(100 * 1000),
2237 agent_(NULL),
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00002238 command_queue_(isolate->logger(), kQueueInitialSize),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002239 command_received_(OS::CreateSemaphore(0)),
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00002240 event_command_queue_(isolate->logger(), kQueueInitialSize),
2241 isolate_(isolate) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002242}
2243
2244
2245Debugger::~Debugger() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002246 delete dispatch_handler_access_;
2247 dispatch_handler_access_ = 0;
2248 delete command_received_;
2249 command_received_ = 0;
2250}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002251
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002252
2253Handle<Object> Debugger::MakeJSObject(Vector<const char> constructor_name,
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002254 int argc,
2255 Handle<Object> argv[],
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002256 bool* caught_exception) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002257 ASSERT(isolate_->context() == *isolate_->debug()->debug_context());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002258
2259 // Create the execution state object.
lrn@chromium.org7516f052011-03-30 08:52:27 +00002260 Handle<String> constructor_str =
2261 isolate_->factory()->LookupSymbol(constructor_name);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002262 Handle<Object> constructor(
2263 isolate_->global()->GetPropertyNoExceptionThrown(*constructor_str));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002264 ASSERT(constructor->IsJSFunction());
2265 if (!constructor->IsJSFunction()) {
2266 *caught_exception = true;
lrn@chromium.org7516f052011-03-30 08:52:27 +00002267 return isolate_->factory()->undefined_value();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002268 }
2269 Handle<Object> js_object = Execution::TryCall(
2270 Handle<JSFunction>::cast(constructor),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002271 Handle<JSObject>(isolate_->debug()->debug_context()->global()),
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002272 argc,
2273 argv,
2274 caught_exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002275 return js_object;
2276}
2277
2278
2279Handle<Object> Debugger::MakeExecutionState(bool* caught_exception) {
2280 // Create the execution state object.
lrn@chromium.org7516f052011-03-30 08:52:27 +00002281 Handle<Object> break_id = isolate_->factory()->NewNumberFromInt(
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002282 isolate_->debug()->break_id());
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002283 Handle<Object> argv[] = { break_id };
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002284 return MakeJSObject(CStrVector("MakeExecutionState"),
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002285 ARRAY_SIZE(argv),
2286 argv,
2287 caught_exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002288}
2289
2290
2291Handle<Object> Debugger::MakeBreakEvent(Handle<Object> exec_state,
2292 Handle<Object> break_points_hit,
2293 bool* caught_exception) {
2294 // Create the new break event object.
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002295 Handle<Object> argv[] = { exec_state, break_points_hit };
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002296 return MakeJSObject(CStrVector("MakeBreakEvent"),
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002297 ARRAY_SIZE(argv),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002298 argv,
2299 caught_exception);
2300}
2301
2302
2303Handle<Object> Debugger::MakeExceptionEvent(Handle<Object> exec_state,
2304 Handle<Object> exception,
2305 bool uncaught,
2306 bool* caught_exception) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002307 Factory* factory = isolate_->factory();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002308 // Create the new exception event object.
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002309 Handle<Object> argv[] = { exec_state,
2310 exception,
2311 factory->ToBoolean(uncaught) };
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002312 return MakeJSObject(CStrVector("MakeExceptionEvent"),
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002313 ARRAY_SIZE(argv),
2314 argv,
2315 caught_exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002316}
2317
2318
2319Handle<Object> Debugger::MakeNewFunctionEvent(Handle<Object> function,
2320 bool* caught_exception) {
2321 // Create the new function event object.
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002322 Handle<Object> argv[] = { function };
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002323 return MakeJSObject(CStrVector("MakeNewFunctionEvent"),
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002324 ARRAY_SIZE(argv),
2325 argv,
2326 caught_exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002327}
2328
2329
2330Handle<Object> Debugger::MakeCompileEvent(Handle<Script> script,
iposva@chromium.org245aa852009-02-10 00:49:54 +00002331 bool before,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002332 bool* caught_exception) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002333 Factory* factory = isolate_->factory();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002334 // Create the compile event object.
2335 Handle<Object> exec_state = MakeExecutionState(caught_exception);
iposva@chromium.org245aa852009-02-10 00:49:54 +00002336 Handle<Object> script_wrapper = GetScriptWrapper(script);
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002337 Handle<Object> argv[] = { exec_state,
2338 script_wrapper,
2339 factory->ToBoolean(before) };
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002340 return MakeJSObject(CStrVector("MakeCompileEvent"),
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002341 ARRAY_SIZE(argv),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002342 argv,
2343 caught_exception);
2344}
2345
2346
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002347Handle<Object> Debugger::MakeScriptCollectedEvent(int id,
2348 bool* caught_exception) {
2349 // Create the script collected event object.
2350 Handle<Object> exec_state = MakeExecutionState(caught_exception);
2351 Handle<Object> id_object = Handle<Smi>(Smi::FromInt(id));
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002352 Handle<Object> argv[] = { exec_state, id_object };
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002353
2354 return MakeJSObject(CStrVector("MakeScriptCollectedEvent"),
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002355 ARRAY_SIZE(argv),
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002356 argv,
2357 caught_exception);
2358}
2359
2360
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002361void Debugger::OnException(Handle<Object> exception, bool uncaught) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002362 HandleScope scope(isolate_);
2363 Debug* debug = isolate_->debug();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002364
2365 // Bail out based on state or if there is no listener for this event
lrn@chromium.org7516f052011-03-30 08:52:27 +00002366 if (debug->InDebugger()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002367 if (!Debugger::EventActive(v8::Exception)) return;
2368
2369 // Bail out if exception breaks are not active
2370 if (uncaught) {
2371 // Uncaught exceptions are reported by either flags.
lrn@chromium.org7516f052011-03-30 08:52:27 +00002372 if (!(debug->break_on_uncaught_exception() ||
2373 debug->break_on_exception())) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002374 } else {
2375 // Caught exceptions are reported is activated.
lrn@chromium.org7516f052011-03-30 08:52:27 +00002376 if (!debug->break_on_exception()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002377 }
2378
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002379 // Enter the debugger.
2380 EnterDebugger debugger;
2381 if (debugger.FailedToEnter()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002382
2383 // Clear all current stepping setup.
lrn@chromium.org7516f052011-03-30 08:52:27 +00002384 debug->ClearStepping();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002385 // Create the event data object.
2386 bool caught_exception = false;
2387 Handle<Object> exec_state = MakeExecutionState(&caught_exception);
2388 Handle<Object> event_data;
2389 if (!caught_exception) {
2390 event_data = MakeExceptionEvent(exec_state, exception, uncaught,
2391 &caught_exception);
2392 }
2393 // Bail out and don't call debugger if exception.
2394 if (caught_exception) {
2395 return;
2396 }
2397
ager@chromium.org5ec48922009-05-05 07:25:34 +00002398 // Process debug event.
2399 ProcessDebugEvent(v8::Exception, Handle<JSObject>::cast(event_data), false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002400 // Return to continue execution from where the exception was thrown.
2401}
2402
2403
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002404void Debugger::OnDebugBreak(Handle<Object> break_points_hit,
2405 bool auto_continue) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002406 HandleScope scope(isolate_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002407
kasper.lund212ac232008-07-16 07:07:30 +00002408 // Debugger has already been entered by caller.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002409 ASSERT(isolate_->context() == *isolate_->debug()->debug_context());
kasper.lund212ac232008-07-16 07:07:30 +00002410
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002411 // Bail out if there is no listener for this event
2412 if (!Debugger::EventActive(v8::Break)) return;
2413
2414 // Debugger must be entered in advance.
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00002415 ASSERT(isolate_->context() == *isolate_->debug()->debug_context());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002416
2417 // Create the event data object.
2418 bool caught_exception = false;
2419 Handle<Object> exec_state = MakeExecutionState(&caught_exception);
2420 Handle<Object> event_data;
2421 if (!caught_exception) {
2422 event_data = MakeBreakEvent(exec_state, break_points_hit,
2423 &caught_exception);
2424 }
2425 // Bail out and don't call debugger if exception.
2426 if (caught_exception) {
2427 return;
2428 }
2429
ager@chromium.org5ec48922009-05-05 07:25:34 +00002430 // Process debug event.
2431 ProcessDebugEvent(v8::Break,
2432 Handle<JSObject>::cast(event_data),
2433 auto_continue);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002434}
2435
2436
2437void Debugger::OnBeforeCompile(Handle<Script> script) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002438 HandleScope scope(isolate_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002439
2440 // Bail out based on state or if there is no listener for this event
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002441 if (isolate_->debug()->InDebugger()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002442 if (compiling_natives()) return;
2443 if (!EventActive(v8::BeforeCompile)) return;
2444
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002445 // Enter the debugger.
2446 EnterDebugger debugger;
2447 if (debugger.FailedToEnter()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002448
2449 // Create the event data object.
2450 bool caught_exception = false;
iposva@chromium.org245aa852009-02-10 00:49:54 +00002451 Handle<Object> event_data = MakeCompileEvent(script, true, &caught_exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002452 // Bail out and don't call debugger if exception.
2453 if (caught_exception) {
2454 return;
2455 }
2456
ager@chromium.org5ec48922009-05-05 07:25:34 +00002457 // Process debug event.
2458 ProcessDebugEvent(v8::BeforeCompile,
2459 Handle<JSObject>::cast(event_data),
2460 true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002461}
2462
2463
2464// Handle debugger actions when a new script is compiled.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00002465void Debugger::OnAfterCompile(Handle<Script> script,
2466 AfterCompileFlags after_compile_flags) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002467 HandleScope scope(isolate_);
2468 Debug* debug = isolate_->debug();
kasper.lund212ac232008-07-16 07:07:30 +00002469
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002470 // Add the newly compiled script to the script cache.
lrn@chromium.org7516f052011-03-30 08:52:27 +00002471 debug->AddScriptToScriptCache(script);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002472
2473 // No more to do if not debugging.
ager@chromium.org71daaf62009-04-01 07:22:49 +00002474 if (!IsDebuggerActive()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002475
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002476 // No compile events while compiling natives.
2477 if (compiling_natives()) return;
2478
iposva@chromium.org245aa852009-02-10 00:49:54 +00002479 // Store whether in debugger before entering debugger.
lrn@chromium.org7516f052011-03-30 08:52:27 +00002480 bool in_debugger = debug->InDebugger();
iposva@chromium.org245aa852009-02-10 00:49:54 +00002481
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002482 // Enter the debugger.
2483 EnterDebugger debugger;
2484 if (debugger.FailedToEnter()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002485
2486 // If debugging there might be script break points registered for this
2487 // script. Make sure that these break points are set.
2488
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002489 // Get the function UpdateScriptBreakPoints (defined in debug-debugger.js).
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002490 Handle<String> update_script_break_points_symbol =
lrn@chromium.org7516f052011-03-30 08:52:27 +00002491 isolate_->factory()->LookupAsciiSymbol("UpdateScriptBreakPoints");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002492 Handle<Object> update_script_break_points =
lrn@chromium.org7516f052011-03-30 08:52:27 +00002493 Handle<Object>(debug->debug_context()->global()->
lrn@chromium.org303ada72010-10-27 09:33:13 +00002494 GetPropertyNoExceptionThrown(*update_script_break_points_symbol));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002495 if (!update_script_break_points->IsJSFunction()) {
2496 return;
2497 }
2498 ASSERT(update_script_break_points->IsJSFunction());
2499
2500 // Wrap the script object in a proper JS object before passing it
2501 // to JavaScript.
2502 Handle<JSValue> wrapper = GetScriptWrapper(script);
2503
2504 // Call UpdateScriptBreakPoints expect no exceptions.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002505 bool caught_exception;
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002506 Handle<Object> argv[] = { wrapper };
rossberg@chromium.org717967f2011-07-20 13:44:42 +00002507 Execution::TryCall(Handle<JSFunction>::cast(update_script_break_points),
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002508 Isolate::Current()->js_builtins_object(),
2509 ARRAY_SIZE(argv),
2510 argv,
2511 &caught_exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002512 if (caught_exception) {
2513 return;
2514 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002515 // Bail out based on state or if there is no listener for this event
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00002516 if (in_debugger && (after_compile_flags & SEND_WHEN_DEBUGGING) == 0) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002517 if (!Debugger::EventActive(v8::AfterCompile)) return;
2518
2519 // Create the compile state object.
2520 Handle<Object> event_data = MakeCompileEvent(script,
iposva@chromium.org245aa852009-02-10 00:49:54 +00002521 false,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002522 &caught_exception);
2523 // Bail out and don't call debugger if exception.
2524 if (caught_exception) {
2525 return;
2526 }
ager@chromium.org5ec48922009-05-05 07:25:34 +00002527 // Process debug event.
2528 ProcessDebugEvent(v8::AfterCompile,
2529 Handle<JSObject>::cast(event_data),
2530 true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002531}
2532
2533
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002534void Debugger::OnScriptCollected(int id) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002535 HandleScope scope(isolate_);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002536
2537 // No more to do if not debugging.
2538 if (!IsDebuggerActive()) return;
2539 if (!Debugger::EventActive(v8::ScriptCollected)) return;
2540
2541 // Enter the debugger.
2542 EnterDebugger debugger;
2543 if (debugger.FailedToEnter()) return;
2544
2545 // Create the script collected state object.
2546 bool caught_exception = false;
2547 Handle<Object> event_data = MakeScriptCollectedEvent(id,
2548 &caught_exception);
2549 // Bail out and don't call debugger if exception.
2550 if (caught_exception) {
2551 return;
2552 }
2553
2554 // Process debug event.
2555 ProcessDebugEvent(v8::ScriptCollected,
2556 Handle<JSObject>::cast(event_data),
2557 true);
2558}
2559
2560
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002561void Debugger::ProcessDebugEvent(v8::DebugEvent event,
ager@chromium.org5ec48922009-05-05 07:25:34 +00002562 Handle<JSObject> event_data,
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002563 bool auto_continue) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002564 HandleScope scope(isolate_);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002565
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +00002566 // Clear any pending debug break if this is a real break.
2567 if (!auto_continue) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002568 isolate_->debug()->clear_interrupt_pending(DEBUGBREAK);
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +00002569 }
2570
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002571 // Create the execution state.
2572 bool caught_exception = false;
2573 Handle<Object> exec_state = MakeExecutionState(&caught_exception);
2574 if (caught_exception) {
2575 return;
2576 }
ager@chromium.org41826e72009-03-30 13:30:57 +00002577 // First notify the message handler if any.
2578 if (message_handler_ != NULL) {
ager@chromium.org5ec48922009-05-05 07:25:34 +00002579 NotifyMessageHandler(event,
2580 Handle<JSObject>::cast(exec_state),
2581 event_data,
2582 auto_continue);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002583 }
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00002584 // Notify registered debug event listener. This can be either a C or
2585 // a JavaScript function. Don't call event listener for v8::Break
2586 // here, if it's only a debug command -- they will be processed later.
2587 if ((event != v8::Break || !auto_continue) && !event_listener_.is_null()) {
2588 CallEventCallback(event, exec_state, event_data, NULL);
2589 }
2590 // Process pending debug commands.
2591 if (event == v8::Break) {
2592 while (!event_command_queue_.IsEmpty()) {
2593 CommandMessage command = event_command_queue_.Get();
2594 if (!event_listener_.is_null()) {
2595 CallEventCallback(v8::BreakForCommand,
2596 exec_state,
2597 event_data,
2598 command.client_data());
2599 }
2600 command.Dispose();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002601 }
2602 }
2603}
2604
2605
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00002606void Debugger::CallEventCallback(v8::DebugEvent event,
2607 Handle<Object> exec_state,
2608 Handle<Object> event_data,
2609 v8::Debug::ClientData* client_data) {
ager@chromium.orgea91cc52011-05-23 06:06:11 +00002610 if (event_listener_->IsForeign()) {
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00002611 CallCEventCallback(event, exec_state, event_data, client_data);
2612 } else {
2613 CallJSEventCallback(event, exec_state, event_data);
2614 }
2615}
2616
2617
2618void Debugger::CallCEventCallback(v8::DebugEvent event,
2619 Handle<Object> exec_state,
2620 Handle<Object> event_data,
2621 v8::Debug::ClientData* client_data) {
ager@chromium.orgea91cc52011-05-23 06:06:11 +00002622 Handle<Foreign> callback_obj(Handle<Foreign>::cast(event_listener_));
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00002623 v8::Debug::EventCallback2 callback =
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002624 FUNCTION_CAST<v8::Debug::EventCallback2>(
2625 callback_obj->foreign_address());
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00002626 EventDetailsImpl event_details(
2627 event,
2628 Handle<JSObject>::cast(exec_state),
2629 Handle<JSObject>::cast(event_data),
2630 event_listener_data_,
2631 client_data);
2632 callback(event_details);
2633}
2634
2635
2636void Debugger::CallJSEventCallback(v8::DebugEvent event,
2637 Handle<Object> exec_state,
2638 Handle<Object> event_data) {
2639 ASSERT(event_listener_->IsJSFunction());
2640 Handle<JSFunction> fun(Handle<JSFunction>::cast(event_listener_));
2641
2642 // Invoke the JavaScript debug event listener.
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002643 Handle<Object> argv[] = { Handle<Object>(Smi::FromInt(event)),
2644 exec_state,
2645 event_data,
2646 event_listener_data_ };
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002647 bool caught_exception;
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002648 Execution::TryCall(fun,
2649 isolate_->global(),
2650 ARRAY_SIZE(argv),
2651 argv,
2652 &caught_exception);
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00002653 // Silently ignore exceptions from debug event listeners.
2654}
2655
2656
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00002657Handle<Context> Debugger::GetDebugContext() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002658 never_unload_debugger_ = true;
2659 EnterDebugger debugger;
2660 return isolate_->debug()->debug_context();
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00002661}
2662
2663
ager@chromium.org71daaf62009-04-01 07:22:49 +00002664void Debugger::UnloadDebugger() {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002665 Debug* debug = isolate_->debug();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002666
ager@chromium.org71daaf62009-04-01 07:22:49 +00002667 // Make sure that there are no breakpoints left.
lrn@chromium.org7516f052011-03-30 08:52:27 +00002668 debug->ClearAllBreakPoints();
ager@chromium.org71daaf62009-04-01 07:22:49 +00002669
2670 // Unload the debugger if feasible.
2671 if (!never_unload_debugger_) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002672 debug->Unload();
ager@chromium.org71daaf62009-04-01 07:22:49 +00002673 }
2674
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002675 // Clear the flag indicating that the debugger should be unloaded.
2676 debugger_unload_pending_ = false;
ager@chromium.org71daaf62009-04-01 07:22:49 +00002677}
2678
2679
ager@chromium.org41826e72009-03-30 13:30:57 +00002680void Debugger::NotifyMessageHandler(v8::DebugEvent event,
ager@chromium.org5ec48922009-05-05 07:25:34 +00002681 Handle<JSObject> exec_state,
2682 Handle<JSObject> event_data,
ager@chromium.org41826e72009-03-30 13:30:57 +00002683 bool auto_continue) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002684 HandleScope scope(isolate_);
ager@chromium.org41826e72009-03-30 13:30:57 +00002685
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002686 if (!isolate_->debug()->Load()) return;
ager@chromium.org41826e72009-03-30 13:30:57 +00002687
2688 // Process the individual events.
ager@chromium.org5ec48922009-05-05 07:25:34 +00002689 bool sendEventMessage = false;
ager@chromium.org41826e72009-03-30 13:30:57 +00002690 switch (event) {
2691 case v8::Break:
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00002692 case v8::BreakForCommand:
ager@chromium.org5ec48922009-05-05 07:25:34 +00002693 sendEventMessage = !auto_continue;
ager@chromium.org41826e72009-03-30 13:30:57 +00002694 break;
2695 case v8::Exception:
ager@chromium.org5ec48922009-05-05 07:25:34 +00002696 sendEventMessage = true;
ager@chromium.org41826e72009-03-30 13:30:57 +00002697 break;
2698 case v8::BeforeCompile:
2699 break;
2700 case v8::AfterCompile:
ager@chromium.org5ec48922009-05-05 07:25:34 +00002701 sendEventMessage = true;
ager@chromium.org41826e72009-03-30 13:30:57 +00002702 break;
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002703 case v8::ScriptCollected:
2704 sendEventMessage = true;
2705 break;
ager@chromium.org41826e72009-03-30 13:30:57 +00002706 case v8::NewFunction:
2707 break;
2708 default:
2709 UNREACHABLE();
2710 }
2711
ager@chromium.org5ec48922009-05-05 07:25:34 +00002712 // The debug command interrupt flag might have been set when the command was
2713 // added. It should be enough to clear the flag only once while we are in the
2714 // debugger.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002715 ASSERT(isolate_->debug()->InDebugger());
2716 isolate_->stack_guard()->Continue(DEBUGCOMMAND);
ager@chromium.org5ec48922009-05-05 07:25:34 +00002717
2718 // Notify the debugger that a debug event has occurred unless auto continue is
2719 // active in which case no event is send.
2720 if (sendEventMessage) {
2721 MessageImpl message = MessageImpl::NewEvent(
2722 event,
2723 auto_continue,
2724 Handle<JSObject>::cast(exec_state),
2725 Handle<JSObject>::cast(event_data));
2726 InvokeMessageHandler(message);
2727 }
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +00002728
2729 // If auto continue don't make the event cause a break, but process messages
2730 // in the queue if any. For script collected events don't even process
2731 // messages in the queue as the execution state might not be what is expected
2732 // by the client.
ager@chromium.org6ffc2172009-05-29 19:20:16 +00002733 if ((auto_continue && !HasCommands()) || event == v8::ScriptCollected) {
ager@chromium.org5ec48922009-05-05 07:25:34 +00002734 return;
2735 }
ager@chromium.org41826e72009-03-30 13:30:57 +00002736
ager@chromium.org41826e72009-03-30 13:30:57 +00002737 v8::TryCatch try_catch;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002738
2739 // DebugCommandProcessor goes here.
2740 v8::Local<v8::Object> cmd_processor;
2741 {
2742 v8::Local<v8::Object> api_exec_state =
2743 v8::Utils::ToLocal(Handle<JSObject>::cast(exec_state));
2744 v8::Local<v8::String> fun_name =
2745 v8::String::New("debugCommandProcessor");
2746 v8::Local<v8::Function> fun =
2747 v8::Function::Cast(*api_exec_state->Get(fun_name));
2748
2749 v8::Handle<v8::Boolean> running =
2750 auto_continue ? v8::True() : v8::False();
2751 static const int kArgc = 1;
2752 v8::Handle<Value> argv[kArgc] = { running };
2753 cmd_processor = v8::Object::Cast(*fun->Call(api_exec_state, kArgc, argv));
2754 if (try_catch.HasCaught()) {
2755 PrintLn(try_catch.Exception());
2756 return;
2757 }
ager@chromium.org41826e72009-03-30 13:30:57 +00002758 }
2759
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002760 bool running = auto_continue;
2761
ager@chromium.org41826e72009-03-30 13:30:57 +00002762 // Process requests from the debugger.
2763 while (true) {
2764 // Wait for new command in the queue.
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002765 if (Debugger::host_dispatch_handler_) {
2766 // In case there is a host dispatch - do periodic dispatches.
2767 if (!command_received_->Wait(host_dispatch_micros_)) {
2768 // Timout expired, do the dispatch.
2769 Debugger::host_dispatch_handler_();
2770 continue;
2771 }
2772 } else {
2773 // In case there is no host dispatch - just wait.
2774 command_received_->Wait();
2775 }
ager@chromium.org41826e72009-03-30 13:30:57 +00002776
ager@chromium.org41826e72009-03-30 13:30:57 +00002777 // Get the command from the queue.
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00002778 CommandMessage command = command_queue_.Get();
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00002779 isolate_->logger()->DebugTag(
2780 "Got request from command queue, in interactive loop.");
ager@chromium.org71daaf62009-04-01 07:22:49 +00002781 if (!Debugger::IsDebuggerActive()) {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002782 // Delete command text and user data.
2783 command.Dispose();
ager@chromium.org41826e72009-03-30 13:30:57 +00002784 return;
2785 }
2786
ager@chromium.org41826e72009-03-30 13:30:57 +00002787 // Invoke JavaScript to process the debug request.
2788 v8::Local<v8::String> fun_name;
2789 v8::Local<v8::Function> fun;
2790 v8::Local<v8::Value> request;
2791 v8::TryCatch try_catch;
2792 fun_name = v8::String::New("processDebugRequest");
2793 fun = v8::Function::Cast(*cmd_processor->Get(fun_name));
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002794
2795 request = v8::String::New(command.text().start(),
2796 command.text().length());
ager@chromium.org41826e72009-03-30 13:30:57 +00002797 static const int kArgc = 1;
2798 v8::Handle<Value> argv[kArgc] = { request };
2799 v8::Local<v8::Value> response_val = fun->Call(cmd_processor, kArgc, argv);
2800
2801 // Get the response.
2802 v8::Local<v8::String> response;
ager@chromium.org41826e72009-03-30 13:30:57 +00002803 if (!try_catch.HasCaught()) {
2804 // Get response string.
2805 if (!response_val->IsUndefined()) {
2806 response = v8::String::Cast(*response_val);
2807 } else {
2808 response = v8::String::New("");
2809 }
2810
2811 // Log the JSON request/response.
2812 if (FLAG_trace_debug_json) {
2813 PrintLn(request);
2814 PrintLn(response);
2815 }
2816
2817 // Get the running state.
2818 fun_name = v8::String::New("isRunning");
2819 fun = v8::Function::Cast(*cmd_processor->Get(fun_name));
2820 static const int kArgc = 1;
2821 v8::Handle<Value> argv[kArgc] = { response };
2822 v8::Local<v8::Value> running_val = fun->Call(cmd_processor, kArgc, argv);
2823 if (!try_catch.HasCaught()) {
2824 running = running_val->ToBoolean()->Value();
2825 }
2826 } else {
2827 // In case of failure the result text is the exception text.
2828 response = try_catch.Exception()->ToString();
2829 }
2830
ager@chromium.org41826e72009-03-30 13:30:57 +00002831 // Return the result.
ager@chromium.org5ec48922009-05-05 07:25:34 +00002832 MessageImpl message = MessageImpl::NewResponse(
2833 event,
2834 running,
2835 Handle<JSObject>::cast(exec_state),
2836 Handle<JSObject>::cast(event_data),
2837 Handle<String>(Utils::OpenHandle(*response)),
2838 command.client_data());
2839 InvokeMessageHandler(message);
2840 command.Dispose();
ager@chromium.org41826e72009-03-30 13:30:57 +00002841
2842 // Return from debug event processing if either the VM is put into the
2843 // runnning state (through a continue command) or auto continue is active
2844 // and there are no more commands queued.
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002845 if (running && !HasCommands()) {
ager@chromium.org41826e72009-03-30 13:30:57 +00002846 return;
2847 }
2848 }
2849}
2850
2851
iposva@chromium.org245aa852009-02-10 00:49:54 +00002852void Debugger::SetEventListener(Handle<Object> callback,
2853 Handle<Object> data) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002854 HandleScope scope(isolate_);
2855 GlobalHandles* global_handles = isolate_->global_handles();
iposva@chromium.org245aa852009-02-10 00:49:54 +00002856
2857 // Clear the global handles for the event listener and the event listener data
2858 // object.
2859 if (!event_listener_.is_null()) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002860 global_handles->Destroy(
iposva@chromium.org245aa852009-02-10 00:49:54 +00002861 reinterpret_cast<Object**>(event_listener_.location()));
2862 event_listener_ = Handle<Object>();
2863 }
2864 if (!event_listener_data_.is_null()) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002865 global_handles->Destroy(
iposva@chromium.org245aa852009-02-10 00:49:54 +00002866 reinterpret_cast<Object**>(event_listener_data_.location()));
2867 event_listener_data_ = Handle<Object>();
2868 }
2869
2870 // If there is a new debug event listener register it together with its data
2871 // object.
2872 if (!callback->IsUndefined() && !callback->IsNull()) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002873 event_listener_ = Handle<Object>::cast(
lrn@chromium.org7516f052011-03-30 08:52:27 +00002874 global_handles->Create(*callback));
iposva@chromium.org245aa852009-02-10 00:49:54 +00002875 if (data.is_null()) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002876 data = isolate_->factory()->undefined_value();
iposva@chromium.org245aa852009-02-10 00:49:54 +00002877 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002878 event_listener_data_ = Handle<Object>::cast(
lrn@chromium.org7516f052011-03-30 08:52:27 +00002879 global_handles->Create(*data));
iposva@chromium.org245aa852009-02-10 00:49:54 +00002880 }
2881
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002882 ListenersChanged();
iposva@chromium.org245aa852009-02-10 00:49:54 +00002883}
2884
2885
ager@chromium.org5ec48922009-05-05 07:25:34 +00002886void Debugger::SetMessageHandler(v8::Debug::MessageHandler2 handler) {
ager@chromium.org71daaf62009-04-01 07:22:49 +00002887 ScopedLock with(debugger_access_);
2888
ager@chromium.org381abbb2009-02-25 13:23:22 +00002889 message_handler_ = handler;
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002890 ListenersChanged();
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00002891 if (handler == NULL) {
ager@chromium.org71daaf62009-04-01 07:22:49 +00002892 // Send an empty command to the debugger if in a break to make JavaScript
2893 // run again if the debugger is closed.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002894 if (isolate_->debug()->InDebugger()) {
ager@chromium.org71daaf62009-04-01 07:22:49 +00002895 ProcessCommand(Vector<const uint16_t>::empty());
2896 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002897 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002898}
2899
2900
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002901void Debugger::ListenersChanged() {
2902 if (IsDebuggerActive()) {
2903 // Disable the compilation cache when the debugger is active.
lrn@chromium.org7516f052011-03-30 08:52:27 +00002904 isolate_->compilation_cache()->Disable();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002905 debugger_unload_pending_ = false;
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002906 } else {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002907 isolate_->compilation_cache()->Enable();
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002908 // Unload the debugger if event listener and message handler cleared.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002909 // Schedule this for later, because we may be in non-V8 thread.
2910 debugger_unload_pending_ = true;
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002911 }
2912}
2913
2914
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002915void Debugger::SetHostDispatchHandler(v8::Debug::HostDispatchHandler handler,
2916 int period) {
ager@chromium.org381abbb2009-02-25 13:23:22 +00002917 host_dispatch_handler_ = handler;
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002918 host_dispatch_micros_ = period * 1000;
ager@chromium.org381abbb2009-02-25 13:23:22 +00002919}
2920
2921
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002922void Debugger::SetDebugMessageDispatchHandler(
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002923 v8::Debug::DebugMessageDispatchHandler handler, bool provide_locker) {
2924 ScopedLock with(dispatch_handler_access_);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002925 debug_message_dispatch_handler_ = handler;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002926
2927 if (provide_locker && message_dispatch_helper_thread_ == NULL) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002928 message_dispatch_helper_thread_ = new MessageDispatchHelperThread(isolate_);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002929 message_dispatch_helper_thread_->Start();
2930 }
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002931}
2932
2933
ager@chromium.org41826e72009-03-30 13:30:57 +00002934// Calls the registered debug message handler. This callback is part of the
ager@chromium.org5ec48922009-05-05 07:25:34 +00002935// public API.
2936void Debugger::InvokeMessageHandler(MessageImpl message) {
ager@chromium.org71daaf62009-04-01 07:22:49 +00002937 ScopedLock with(debugger_access_);
2938
ager@chromium.org381abbb2009-02-25 13:23:22 +00002939 if (message_handler_ != NULL) {
ager@chromium.org5ec48922009-05-05 07:25:34 +00002940 message_handler_(message);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002941 }
ager@chromium.org41826e72009-03-30 13:30:57 +00002942}
2943
2944
2945// Puts a command coming from the public API on the queue. Creates
2946// a copy of the command string managed by the debugger. Up to this
2947// point, the command data was managed by the API client. Called
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00002948// by the API client thread.
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002949void Debugger::ProcessCommand(Vector<const uint16_t> command,
2950 v8::Debug::ClientData* client_data) {
2951 // Need to cast away const.
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00002952 CommandMessage message = CommandMessage::New(
ager@chromium.org41826e72009-03-30 13:30:57 +00002953 Vector<uint16_t>(const_cast<uint16_t*>(command.start()),
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002954 command.length()),
2955 client_data);
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00002956 isolate_->logger()->DebugTag("Put command on command_queue.");
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002957 command_queue_.Put(message);
ager@chromium.org41826e72009-03-30 13:30:57 +00002958 command_received_->Signal();
sgjesse@chromium.org3afc1582009-04-16 22:31:44 +00002959
2960 // Set the debug command break flag to have the command processed.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002961 if (!isolate_->debug()->InDebugger()) {
2962 isolate_->stack_guard()->DebugCommand();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002963 }
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002964
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002965 MessageDispatchHelperThread* dispatch_thread;
2966 {
2967 ScopedLock with(dispatch_handler_access_);
2968 dispatch_thread = message_dispatch_helper_thread_;
2969 }
2970
2971 if (dispatch_thread == NULL) {
2972 CallMessageDispatchHandler();
2973 } else {
2974 dispatch_thread->Schedule();
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002975 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002976}
2977
2978
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002979bool Debugger::HasCommands() {
ager@chromium.org41826e72009-03-30 13:30:57 +00002980 return !command_queue_.IsEmpty();
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002981}
2982
2983
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00002984void Debugger::EnqueueDebugCommand(v8::Debug::ClientData* client_data) {
2985 CommandMessage message = CommandMessage::New(Vector<uint16_t>(), client_data);
2986 event_command_queue_.Put(message);
2987
2988 // Set the debug command break flag to have the command processed.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002989 if (!isolate_->debug()->InDebugger()) {
2990 isolate_->stack_guard()->DebugCommand();
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00002991 }
2992}
2993
2994
ager@chromium.org71daaf62009-04-01 07:22:49 +00002995bool Debugger::IsDebuggerActive() {
2996 ScopedLock with(debugger_access_);
2997
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002998 return message_handler_ != NULL ||
2999 !event_listener_.is_null() ||
3000 force_debugger_active_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003001}
3002
3003
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003004Handle<Object> Debugger::Call(Handle<JSFunction> fun,
3005 Handle<Object> data,
3006 bool* pending_exception) {
ager@chromium.org71daaf62009-04-01 07:22:49 +00003007 // When calling functions in the debugger prevent it from beeing unloaded.
3008 Debugger::never_unload_debugger_ = true;
3009
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003010 // Enter the debugger.
3011 EnterDebugger debugger;
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +00003012 if (debugger.FailedToEnter()) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00003013 return isolate_->factory()->undefined_value();
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003014 }
3015
3016 // Create the execution state.
3017 bool caught_exception = false;
3018 Handle<Object> exec_state = MakeExecutionState(&caught_exception);
3019 if (caught_exception) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00003020 return isolate_->factory()->undefined_value();
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003021 }
3022
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00003023 Handle<Object> argv[] = { exec_state, data };
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +00003024 Handle<Object> result = Execution::Call(
3025 fun,
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003026 Handle<Object>(isolate_->debug()->debug_context_->global_proxy()),
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00003027 ARRAY_SIZE(argv),
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +00003028 argv,
3029 pending_exception);
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003030 return result;
3031}
3032
3033
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003034static void StubMessageHandler2(const v8::Debug::Message& message) {
3035 // Simply ignore message.
3036}
3037
3038
3039bool Debugger::StartAgent(const char* name, int port,
3040 bool wait_for_connection) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003041 ASSERT(Isolate::Current() == isolate_);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003042 if (wait_for_connection) {
3043 // Suspend V8 if it is already running or set V8 to suspend whenever
3044 // it starts.
3045 // Provide stub message handler; V8 auto-continues each suspend
3046 // when there is no message handler; we doesn't need it.
3047 // Once become suspended, V8 will stay so indefinitely long, until remote
3048 // debugger connects and issues "continue" command.
3049 Debugger::message_handler_ = StubMessageHandler2;
3050 v8::Debug::DebugBreak();
3051 }
3052
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003053 if (Socket::Setup()) {
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00003054 if (agent_ == NULL) {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00003055 agent_ = new DebuggerAgent(name, port);
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00003056 agent_->Start();
3057 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003058 return true;
3059 }
3060
3061 return false;
3062}
3063
3064
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003065void Debugger::StopAgent() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003066 ASSERT(Isolate::Current() == isolate_);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003067 if (agent_ != NULL) {
3068 agent_->Shutdown();
3069 agent_->Join();
3070 delete agent_;
3071 agent_ = NULL;
3072 }
3073}
3074
3075
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003076void Debugger::WaitForAgent() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003077 ASSERT(Isolate::Current() == isolate_);
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003078 if (agent_ != NULL)
3079 agent_->WaitUntilListening();
3080}
3081
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003082
3083void Debugger::CallMessageDispatchHandler() {
3084 v8::Debug::DebugMessageDispatchHandler handler;
3085 {
3086 ScopedLock with(dispatch_handler_access_);
3087 handler = Debugger::debug_message_dispatch_handler_;
3088 }
3089 if (handler != NULL) {
3090 handler();
3091 }
3092}
3093
3094
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003095EnterDebugger::EnterDebugger()
3096 : isolate_(Isolate::Current()),
3097 prev_(isolate_->debug()->debugger_entry()),
3098 it_(isolate_),
3099 has_js_frames_(!it_.done()),
3100 save_(isolate_) {
3101 Debug* debug = isolate_->debug();
3102 ASSERT(prev_ != NULL || !debug->is_interrupt_pending(PREEMPT));
3103 ASSERT(prev_ != NULL || !debug->is_interrupt_pending(DEBUGBREAK));
3104
3105 // Link recursive debugger entry.
3106 debug->set_debugger_entry(this);
3107
3108 // Store the previous break id and frame id.
3109 break_id_ = debug->break_id();
3110 break_frame_id_ = debug->break_frame_id();
3111
3112 // Create the new break info. If there is no JavaScript frames there is no
3113 // break frame id.
3114 if (has_js_frames_) {
3115 debug->NewBreak(it_.frame()->id());
3116 } else {
3117 debug->NewBreak(StackFrame::NO_ID);
3118 }
3119
3120 // Make sure that debugger is loaded and enter the debugger context.
3121 load_failed_ = !debug->Load();
3122 if (!load_failed_) {
3123 // NOTE the member variable save which saves the previous context before
3124 // this change.
3125 isolate_->set_context(*debug->debug_context());
3126 }
3127}
3128
3129
3130EnterDebugger::~EnterDebugger() {
3131 ASSERT(Isolate::Current() == isolate_);
3132 Debug* debug = isolate_->debug();
3133
3134 // Restore to the previous break state.
3135 debug->SetBreak(break_frame_id_, break_id_);
3136
3137 // Check for leaving the debugger.
3138 if (prev_ == NULL) {
3139 // Clear mirror cache when leaving the debugger. Skip this if there is a
3140 // pending exception as clearing the mirror cache calls back into
3141 // JavaScript. This can happen if the v8::Debug::Call is used in which
3142 // case the exception should end up in the calling code.
3143 if (!isolate_->has_pending_exception()) {
3144 // Try to avoid any pending debug break breaking in the clear mirror
3145 // cache JavaScript code.
3146 if (isolate_->stack_guard()->IsDebugBreak()) {
3147 debug->set_interrupts_pending(DEBUGBREAK);
3148 isolate_->stack_guard()->Continue(DEBUGBREAK);
3149 }
3150 debug->ClearMirrorCache();
3151 }
3152
3153 // Request preemption and debug break when leaving the last debugger entry
3154 // if any of these where recorded while debugging.
3155 if (debug->is_interrupt_pending(PREEMPT)) {
3156 // This re-scheduling of preemption is to avoid starvation in some
3157 // debugging scenarios.
3158 debug->clear_interrupt_pending(PREEMPT);
3159 isolate_->stack_guard()->Preempt();
3160 }
3161 if (debug->is_interrupt_pending(DEBUGBREAK)) {
3162 debug->clear_interrupt_pending(DEBUGBREAK);
3163 isolate_->stack_guard()->DebugBreak();
3164 }
3165
3166 // If there are commands in the queue when leaving the debugger request
3167 // that these commands are processed.
3168 if (isolate_->debugger()->HasCommands()) {
3169 isolate_->stack_guard()->DebugCommand();
3170 }
3171
3172 // If leaving the debugger with the debugger no longer active unload it.
3173 if (!isolate_->debugger()->IsDebuggerActive()) {
3174 isolate_->debugger()->UnloadDebugger();
3175 }
3176 }
3177
3178 // Leaving this debugger entry.
3179 debug->set_debugger_entry(prev_);
3180}
3181
3182
ager@chromium.org5ec48922009-05-05 07:25:34 +00003183MessageImpl MessageImpl::NewEvent(DebugEvent event,
3184 bool running,
3185 Handle<JSObject> exec_state,
3186 Handle<JSObject> event_data) {
3187 MessageImpl message(true, event, running,
3188 exec_state, event_data, Handle<String>(), NULL);
3189 return message;
3190}
3191
3192
3193MessageImpl MessageImpl::NewResponse(DebugEvent event,
3194 bool running,
3195 Handle<JSObject> exec_state,
3196 Handle<JSObject> event_data,
3197 Handle<String> response_json,
3198 v8::Debug::ClientData* client_data) {
3199 MessageImpl message(false, event, running,
3200 exec_state, event_data, response_json, client_data);
3201 return message;
3202}
3203
3204
3205MessageImpl::MessageImpl(bool is_event,
3206 DebugEvent event,
3207 bool running,
3208 Handle<JSObject> exec_state,
3209 Handle<JSObject> event_data,
3210 Handle<String> response_json,
3211 v8::Debug::ClientData* client_data)
3212 : is_event_(is_event),
3213 event_(event),
3214 running_(running),
3215 exec_state_(exec_state),
3216 event_data_(event_data),
3217 response_json_(response_json),
3218 client_data_(client_data) {}
3219
3220
3221bool MessageImpl::IsEvent() const {
3222 return is_event_;
3223}
3224
3225
3226bool MessageImpl::IsResponse() const {
3227 return !is_event_;
3228}
3229
3230
3231DebugEvent MessageImpl::GetEvent() const {
3232 return event_;
3233}
3234
3235
3236bool MessageImpl::WillStartRunning() const {
3237 return running_;
3238}
3239
3240
3241v8::Handle<v8::Object> MessageImpl::GetExecutionState() const {
3242 return v8::Utils::ToLocal(exec_state_);
3243}
3244
3245
3246v8::Handle<v8::Object> MessageImpl::GetEventData() const {
3247 return v8::Utils::ToLocal(event_data_);
3248}
3249
3250
3251v8::Handle<v8::String> MessageImpl::GetJSON() const {
3252 v8::HandleScope scope;
3253
3254 if (IsEvent()) {
3255 // Call toJSONProtocol on the debug event object.
3256 Handle<Object> fun = GetProperty(event_data_, "toJSONProtocol");
3257 if (!fun->IsJSFunction()) {
3258 return v8::Handle<v8::String>();
3259 }
3260 bool caught_exception;
3261 Handle<Object> json = Execution::TryCall(Handle<JSFunction>::cast(fun),
3262 event_data_,
3263 0, NULL, &caught_exception);
3264 if (caught_exception || !json->IsString()) {
3265 return v8::Handle<v8::String>();
3266 }
3267 return scope.Close(v8::Utils::ToLocal(Handle<String>::cast(json)));
3268 } else {
3269 return v8::Utils::ToLocal(response_json_);
3270 }
3271}
3272
3273
3274v8::Handle<v8::Context> MessageImpl::GetEventContext() const {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003275 Isolate* isolate = Isolate::Current();
3276 v8::Handle<v8::Context> context = GetDebugEventContext(isolate);
3277 // Isolate::context() may be NULL when "script collected" event occures.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003278 ASSERT(!context.IsEmpty() || event_ == v8::ScriptCollected);
rossberg@chromium.org717967f2011-07-20 13:44:42 +00003279 return context;
ager@chromium.org5ec48922009-05-05 07:25:34 +00003280}
3281
3282
3283v8::Debug::ClientData* MessageImpl::GetClientData() const {
3284 return client_data_;
3285}
3286
3287
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003288EventDetailsImpl::EventDetailsImpl(DebugEvent event,
3289 Handle<JSObject> exec_state,
3290 Handle<JSObject> event_data,
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00003291 Handle<Object> callback_data,
3292 v8::Debug::ClientData* client_data)
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003293 : event_(event),
3294 exec_state_(exec_state),
3295 event_data_(event_data),
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00003296 callback_data_(callback_data),
3297 client_data_(client_data) {}
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003298
3299
3300DebugEvent EventDetailsImpl::GetEvent() const {
3301 return event_;
3302}
3303
3304
3305v8::Handle<v8::Object> EventDetailsImpl::GetExecutionState() const {
3306 return v8::Utils::ToLocal(exec_state_);
3307}
3308
3309
3310v8::Handle<v8::Object> EventDetailsImpl::GetEventData() const {
3311 return v8::Utils::ToLocal(event_data_);
3312}
3313
3314
3315v8::Handle<v8::Context> EventDetailsImpl::GetEventContext() const {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003316 return GetDebugEventContext(Isolate::Current());
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003317}
3318
3319
3320v8::Handle<v8::Value> EventDetailsImpl::GetCallbackData() const {
3321 return v8::Utils::ToLocal(callback_data_);
3322}
3323
3324
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00003325v8::Debug::ClientData* EventDetailsImpl::GetClientData() const {
3326 return client_data_;
3327}
3328
3329
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003330CommandMessage::CommandMessage() : text_(Vector<uint16_t>::empty()),
3331 client_data_(NULL) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003332}
3333
3334
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003335CommandMessage::CommandMessage(const Vector<uint16_t>& text,
3336 v8::Debug::ClientData* data)
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003337 : text_(text),
3338 client_data_(data) {
3339}
3340
3341
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003342CommandMessage::~CommandMessage() {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003343}
3344
3345
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003346void CommandMessage::Dispose() {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003347 text_.Dispose();
3348 delete client_data_;
3349 client_data_ = NULL;
3350}
3351
3352
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003353CommandMessage CommandMessage::New(const Vector<uint16_t>& command,
3354 v8::Debug::ClientData* data) {
3355 return CommandMessage(command.Clone(), data);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003356}
3357
3358
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003359CommandMessageQueue::CommandMessageQueue(int size) : start_(0), end_(0),
3360 size_(size) {
3361 messages_ = NewArray<CommandMessage>(size);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003362}
3363
3364
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003365CommandMessageQueue::~CommandMessageQueue() {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003366 while (!IsEmpty()) {
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003367 CommandMessage m = Get();
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003368 m.Dispose();
3369 }
kasper.lund7276f142008-07-30 08:49:36 +00003370 DeleteArray(messages_);
3371}
3372
3373
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003374CommandMessage CommandMessageQueue::Get() {
kasper.lund7276f142008-07-30 08:49:36 +00003375 ASSERT(!IsEmpty());
3376 int result = start_;
3377 start_ = (start_ + 1) % size_;
3378 return messages_[result];
3379}
3380
3381
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003382void CommandMessageQueue::Put(const CommandMessage& message) {
kasper.lund7276f142008-07-30 08:49:36 +00003383 if ((end_ + 1) % size_ == start_) {
3384 Expand();
3385 }
3386 messages_[end_] = message;
3387 end_ = (end_ + 1) % size_;
3388}
3389
3390
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003391void CommandMessageQueue::Expand() {
3392 CommandMessageQueue new_queue(size_ * 2);
kasper.lund7276f142008-07-30 08:49:36 +00003393 while (!IsEmpty()) {
3394 new_queue.Put(Get());
3395 }
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003396 CommandMessage* array_to_free = messages_;
kasper.lund7276f142008-07-30 08:49:36 +00003397 *this = new_queue;
3398 new_queue.messages_ = array_to_free;
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003399 // Make the new_queue empty so that it doesn't call Dispose on any messages.
3400 new_queue.start_ = new_queue.end_;
kasper.lund7276f142008-07-30 08:49:36 +00003401 // Automatic destructor called on new_queue, freeing array_to_free.
3402}
3403
3404
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00003405LockingCommandMessageQueue::LockingCommandMessageQueue(Logger* logger, int size)
3406 : logger_(logger), queue_(size) {
kasper.lund7276f142008-07-30 08:49:36 +00003407 lock_ = OS::CreateMutex();
3408}
3409
3410
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003411LockingCommandMessageQueue::~LockingCommandMessageQueue() {
kasper.lund7276f142008-07-30 08:49:36 +00003412 delete lock_;
3413}
3414
3415
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003416bool LockingCommandMessageQueue::IsEmpty() const {
kasper.lund7276f142008-07-30 08:49:36 +00003417 ScopedLock sl(lock_);
3418 return queue_.IsEmpty();
3419}
3420
3421
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003422CommandMessage LockingCommandMessageQueue::Get() {
kasper.lund7276f142008-07-30 08:49:36 +00003423 ScopedLock sl(lock_);
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003424 CommandMessage result = queue_.Get();
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00003425 logger_->DebugEvent("Get", result.text());
kasper.lund7276f142008-07-30 08:49:36 +00003426 return result;
3427}
3428
3429
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003430void LockingCommandMessageQueue::Put(const CommandMessage& message) {
kasper.lund7276f142008-07-30 08:49:36 +00003431 ScopedLock sl(lock_);
3432 queue_.Put(message);
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00003433 logger_->DebugEvent("Put", message.text());
kasper.lund7276f142008-07-30 08:49:36 +00003434}
3435
3436
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003437void LockingCommandMessageQueue::Clear() {
kasper.lund7276f142008-07-30 08:49:36 +00003438 ScopedLock sl(lock_);
3439 queue_.Clear();
3440}
3441
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003442
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003443MessageDispatchHelperThread::MessageDispatchHelperThread(Isolate* isolate)
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00003444 : Thread("v8:MsgDispHelpr"),
lrn@chromium.org5d00b602011-01-05 09:51:43 +00003445 sem_(OS::CreateSemaphore(0)), mutex_(OS::CreateMutex()),
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003446 already_signalled_(false) {
3447}
3448
3449
3450MessageDispatchHelperThread::~MessageDispatchHelperThread() {
3451 delete mutex_;
3452 delete sem_;
3453}
3454
3455
3456void MessageDispatchHelperThread::Schedule() {
3457 {
3458 ScopedLock lock(mutex_);
3459 if (already_signalled_) {
3460 return;
3461 }
3462 already_signalled_ = true;
3463 }
3464 sem_->Signal();
3465}
3466
3467
3468void MessageDispatchHelperThread::Run() {
3469 while (true) {
3470 sem_->Wait();
3471 {
3472 ScopedLock lock(mutex_);
3473 already_signalled_ = false;
3474 }
3475 {
3476 Locker locker;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003477 Isolate::Current()->debugger()->CallMessageDispatchHandler();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003478 }
3479 }
3480}
3481
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003482#endif // ENABLE_DEBUGGER_SUPPORT
kasper.lund7276f142008-07-30 08:49:36 +00003483
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003484} } // namespace v8::internal