blob: 01c4dba3cfeba41cc3a629e1e7c614c6355183ed [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
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000685void Debug::SetUp(bool create_heap_objects) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000686 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_,
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000830 JSReceiver::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.
danno@chromium.orgbf0c8202011-12-27 10:09:42 +00001149 ASSERT(*source_position >= 0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001150
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();
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001216 // Make sure the function has set up the debug info.
kasper.lundbd3ec4e2008-07-09 11:06:54 +00001217 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
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001761static void CollectActiveFunctionsFromThread(
1762 Isolate* isolate,
1763 ThreadLocalTop* top,
1764 List<Handle<JSFunction> >* active_functions,
1765 Object* active_code_marker) {
1766 // Find all non-optimized code functions with activation frames
1767 // on the stack. This includes functions which have optimized
1768 // activations (including inlined functions) on the stack as the
1769 // non-optimized code is needed for the lazy deoptimization.
1770 for (JavaScriptFrameIterator it(isolate, top); !it.done(); it.Advance()) {
1771 JavaScriptFrame* frame = it.frame();
1772 if (frame->is_optimized()) {
1773 List<JSFunction*> functions(Compiler::kMaxInliningLevels + 1);
1774 frame->GetFunctions(&functions);
1775 for (int i = 0; i < functions.length(); i++) {
1776 JSFunction* function = functions[i];
1777 active_functions->Add(Handle<JSFunction>(function));
1778 function->shared()->code()->set_gc_metadata(active_code_marker);
1779 }
1780 } else if (frame->function()->IsJSFunction()) {
1781 JSFunction* function = JSFunction::cast(frame->function());
1782 ASSERT(frame->LookupCode()->kind() == Code::FUNCTION);
1783 active_functions->Add(Handle<JSFunction>(function));
1784 function->shared()->code()->set_gc_metadata(active_code_marker);
1785 }
1786 }
1787}
1788
1789
1790static void RedirectActivationsToRecompiledCodeOnThread(
1791 Isolate* isolate,
1792 ThreadLocalTop* top) {
1793 for (JavaScriptFrameIterator it(isolate, top); !it.done(); it.Advance()) {
1794 JavaScriptFrame* frame = it.frame();
1795
1796 if (frame->is_optimized() || !frame->function()->IsJSFunction()) continue;
1797
1798 JSFunction* function = JSFunction::cast(frame->function());
1799
1800 ASSERT(frame->LookupCode()->kind() == Code::FUNCTION);
1801
1802 Handle<Code> frame_code(frame->LookupCode());
1803 if (frame_code->has_debug_break_slots()) continue;
1804
1805 Handle<Code> new_code(function->shared()->code());
1806 if (new_code->kind() != Code::FUNCTION ||
1807 !new_code->has_debug_break_slots()) {
1808 continue;
1809 }
1810
1811 intptr_t delta = frame->pc() - frame_code->instruction_start();
1812 int debug_break_slot_count = 0;
1813 int mask = RelocInfo::ModeMask(RelocInfo::DEBUG_BREAK_SLOT);
1814 for (RelocIterator it(*new_code, mask); !it.done(); it.next()) {
1815 // Check if the pc in the new code with debug break
1816 // slots is before this slot.
1817 RelocInfo* info = it.rinfo();
1818 int debug_break_slot_bytes =
1819 debug_break_slot_count * Assembler::kDebugBreakSlotLength;
1820 intptr_t new_delta =
1821 info->pc() -
1822 new_code->instruction_start() -
1823 debug_break_slot_bytes;
1824 if (new_delta > delta) {
1825 break;
1826 }
1827
1828 // Passed a debug break slot in the full code with debug
1829 // break slots.
1830 debug_break_slot_count++;
1831 }
1832 int debug_break_slot_bytes =
1833 debug_break_slot_count * Assembler::kDebugBreakSlotLength;
1834 if (FLAG_trace_deopt) {
1835 PrintF("Replacing code %08" V8PRIxPTR " - %08" V8PRIxPTR " (%d) "
1836 "with %08" V8PRIxPTR " - %08" V8PRIxPTR " (%d) "
1837 "for debugging, "
1838 "changing pc from %08" V8PRIxPTR " to %08" V8PRIxPTR "\n",
1839 reinterpret_cast<intptr_t>(
1840 frame_code->instruction_start()),
1841 reinterpret_cast<intptr_t>(
1842 frame_code->instruction_start()) +
1843 frame_code->instruction_size(),
1844 frame_code->instruction_size(),
1845 reinterpret_cast<intptr_t>(new_code->instruction_start()),
1846 reinterpret_cast<intptr_t>(new_code->instruction_start()) +
1847 new_code->instruction_size(),
1848 new_code->instruction_size(),
1849 reinterpret_cast<intptr_t>(frame->pc()),
1850 reinterpret_cast<intptr_t>(new_code->instruction_start()) +
1851 delta + debug_break_slot_bytes);
1852 }
1853
1854 // Patch the return address to return into the code with
1855 // debug break slots.
1856 frame->set_pc(
1857 new_code->instruction_start() + delta + debug_break_slot_bytes);
1858 }
1859}
1860
1861
1862class ActiveFunctionsCollector : public ThreadVisitor {
1863 public:
1864 explicit ActiveFunctionsCollector(List<Handle<JSFunction> >* active_functions,
1865 Object* active_code_marker)
1866 : active_functions_(active_functions),
1867 active_code_marker_(active_code_marker) { }
1868
1869 void VisitThread(Isolate* isolate, ThreadLocalTop* top) {
1870 CollectActiveFunctionsFromThread(isolate,
1871 top,
1872 active_functions_,
1873 active_code_marker_);
1874 }
1875
1876 private:
1877 List<Handle<JSFunction> >* active_functions_;
1878 Object* active_code_marker_;
1879};
1880
1881
1882class ActiveFunctionsRedirector : public ThreadVisitor {
1883 public:
1884 void VisitThread(Isolate* isolate, ThreadLocalTop* top) {
1885 RedirectActivationsToRecompiledCodeOnThread(isolate, top);
1886 }
1887};
1888
1889
lrn@chromium.org34e60782011-09-15 07:25:40 +00001890void Debug::PrepareForBreakPoints() {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001891 // If preparing for the first break point make sure to deoptimize all
1892 // functions as debugging does not work with optimized code.
1893 if (!has_break_points_) {
1894 Deoptimizer::DeoptimizeAll();
lrn@chromium.org34e60782011-09-15 07:25:40 +00001895
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001896 Handle<Code> lazy_compile =
1897 Handle<Code>(isolate_->builtins()->builtin(Builtins::kLazyCompile));
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001898
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001899 // Keep the list of activated functions in a handlified list as it
1900 // is used both in GC and non-GC code.
1901 List<Handle<JSFunction> > active_functions(100);
lrn@chromium.org34e60782011-09-15 07:25:40 +00001902
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001903 {
1904 // We are going to iterate heap to find all functions without
1905 // debug break slots.
1906 isolate_->heap()->CollectAllGarbage(Heap::kMakeHeapIterableMask);
1907
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001908 // Ensure no GC in this scope as we are going to use gc_metadata
1909 // field in the Code object to mark active functions.
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001910 AssertNoAllocation no_allocation;
1911
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001912 Object* active_code_marker = isolate_->heap()->the_hole_value();
svenpanne@chromium.orgecb9dd62011-12-01 08:22:35 +00001913
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001914 CollectActiveFunctionsFromThread(isolate_,
1915 isolate_->thread_local_top(),
1916 &active_functions,
1917 active_code_marker);
1918 ActiveFunctionsCollector active_functions_collector(&active_functions,
1919 active_code_marker);
1920 isolate_->thread_manager()->IterateArchivedThreads(
1921 &active_functions_collector);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001922
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001923 // Scan the heap for all non-optimized functions which have no
1924 // debug break slots and are not active or inlined into an active
1925 // function and mark them for lazy compilation.
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001926 HeapIterator iterator;
1927 HeapObject* obj = NULL;
1928 while (((obj = iterator.next()) != NULL)) {
1929 if (obj->IsJSFunction()) {
1930 JSFunction* function = JSFunction::cast(obj);
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001931 SharedFunctionInfo* shared = function->shared();
1932 if (shared->allows_lazy_compilation() &&
1933 shared->script()->IsScript() &&
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001934 function->code()->kind() == Code::FUNCTION &&
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001935 !function->code()->has_debug_break_slots() &&
1936 shared->code()->gc_metadata() != active_code_marker) {
1937 function->set_code(*lazy_compile);
1938 function->shared()->set_code(*lazy_compile);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001939 }
1940 }
lrn@chromium.org34e60782011-09-15 07:25:40 +00001941 }
lrn@chromium.org34e60782011-09-15 07:25:40 +00001942
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001943 // Clear gc_metadata field.
1944 for (int i = 0; i < active_functions.length(); i++) {
1945 Handle<JSFunction> function = active_functions[i];
1946 function->shared()->code()->set_gc_metadata(Smi::FromInt(0));
1947 }
1948 }
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001949
1950 // Now recompile all functions with activation frames and and
1951 // patch the return address to run in the new compiled code.
1952 for (int i = 0; i < active_functions.length(); i++) {
1953 Handle<JSFunction> function = active_functions[i];
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001954
1955 if (function->code()->kind() == Code::FUNCTION &&
1956 function->code()->has_debug_break_slots()) {
1957 // Nothing to do. Function code already had debug break slots.
1958 continue;
1959 }
1960
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001961 Handle<SharedFunctionInfo> shared(function->shared());
1962 // If recompilation is not possible just skip it.
1963 if (shared->is_toplevel() ||
1964 !shared->allows_lazy_compilation() ||
1965 shared->code()->kind() == Code::BUILTIN) {
1966 continue;
1967 }
1968
1969 // Make sure that the shared full code is compiled with debug
1970 // break slots.
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00001971 if (!shared->code()->has_debug_break_slots()) {
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001972 // Try to compile the full code with debug break slots. If it
1973 // fails just keep the current code.
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00001974 Handle<Code> current_code(function->shared()->code());
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001975 ZoneScope zone_scope(isolate_, DELETE_ON_EXIT);
1976 shared->set_code(*lazy_compile);
1977 bool prev_force_debugger_active =
1978 isolate_->debugger()->force_debugger_active();
1979 isolate_->debugger()->set_force_debugger_active(true);
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00001980 ASSERT(current_code->kind() == Code::FUNCTION);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001981 CompileFullCodeForDebugging(shared, current_code);
1982 isolate_->debugger()->set_force_debugger_active(
1983 prev_force_debugger_active);
1984 if (!shared->is_compiled()) {
1985 shared->set_code(*current_code);
1986 continue;
1987 }
1988 }
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001989
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001990 // Keep function code in sync with shared function info.
1991 function->set_code(shared->code());
lrn@chromium.org34e60782011-09-15 07:25:40 +00001992 }
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001993
1994 RedirectActivationsToRecompiledCodeOnThread(isolate_,
1995 isolate_->thread_local_top());
1996
1997 ActiveFunctionsRedirector active_functions_redirector;
1998 isolate_->thread_manager()->IterateArchivedThreads(
1999 &active_functions_redirector);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002000 }
lrn@chromium.org34e60782011-09-15 07:25:40 +00002001}
2002
2003
2004// Ensures the debug information is present for shared.
2005bool Debug::EnsureDebugInfo(Handle<SharedFunctionInfo> shared) {
2006 // Return if we already have the debug info for shared.
2007 if (HasDebugInfo(shared)) {
2008 ASSERT(shared->is_compiled());
2009 return true;
2010 }
2011
2012 // Ensure shared in compiled. Return false if this failed.
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002013 if (!SharedFunctionInfo::EnsureCompiled(shared, CLEAR_EXCEPTION)) {
2014 return false;
2015 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002016
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002017 // Create the debug info object.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002018 Handle<DebugInfo> debug_info = FACTORY->NewDebugInfo(shared);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002019
2020 // Add debug info to the list.
2021 DebugInfoListNode* node = new DebugInfoListNode(*debug_info);
2022 node->set_next(debug_info_list_);
2023 debug_info_list_ = node;
2024
2025 // Now there is at least one break point.
2026 has_break_points_ = true;
2027
kasper.lundbd3ec4e2008-07-09 11:06:54 +00002028 return true;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002029}
2030
2031
2032void Debug::RemoveDebugInfo(Handle<DebugInfo> debug_info) {
2033 ASSERT(debug_info_list_ != NULL);
2034 // Run through the debug info objects to find this one and remove it.
2035 DebugInfoListNode* prev = NULL;
2036 DebugInfoListNode* current = debug_info_list_;
2037 while (current != NULL) {
2038 if (*current->debug_info() == *debug_info) {
2039 // Unlink from list. If prev is NULL we are looking at the first element.
2040 if (prev == NULL) {
2041 debug_info_list_ = current->next();
2042 } else {
2043 prev->set_next(current->next());
2044 }
lrn@chromium.org7516f052011-03-30 08:52:27 +00002045 current->debug_info()->shared()->set_debug_info(
2046 isolate_->heap()->undefined_value());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002047 delete current;
2048
2049 // If there are no more debug info objects there are not more break
2050 // points.
2051 has_break_points_ = debug_info_list_ != NULL;
2052
2053 return;
2054 }
2055 // Move to next in list.
2056 prev = current;
2057 current = current->next();
2058 }
2059 UNREACHABLE();
2060}
2061
2062
2063void Debug::SetAfterBreakTarget(JavaScriptFrame* frame) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002064 HandleScope scope(isolate_);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002065
lrn@chromium.org34e60782011-09-15 07:25:40 +00002066 PrepareForBreakPoints();
2067
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002068 // Get the executing function in which the debug break occurred.
2069 Handle<SharedFunctionInfo> shared =
2070 Handle<SharedFunctionInfo>(JSFunction::cast(frame->function())->shared());
kasper.lundbd3ec4e2008-07-09 11:06:54 +00002071 if (!EnsureDebugInfo(shared)) {
2072 // Return if we failed to retrieve the debug info.
2073 return;
2074 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002075 Handle<DebugInfo> debug_info = GetDebugInfo(shared);
2076 Handle<Code> code(debug_info->code());
2077 Handle<Code> original_code(debug_info->original_code());
2078#ifdef DEBUG
2079 // Get the code which is actually executing.
vegorov@chromium.org74f333b2011-04-06 11:17:46 +00002080 Handle<Code> frame_code(frame->LookupCode());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002081 ASSERT(frame_code.is_identical_to(code));
2082#endif
2083
2084 // Find the call address in the running code. This address holds the call to
2085 // either a DebugBreakXXX or to the debug break return entry code if the
2086 // break point is still active after processing the break point.
ager@chromium.org4af710e2009-09-15 12:20:11 +00002087 Address addr = frame->pc() - Assembler::kCallTargetAddressOffset;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002088
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002089 // Check if the location is at JS exit or debug break slot.
ager@chromium.orga1645e22009-09-09 19:27:10 +00002090 bool at_js_return = false;
2091 bool break_at_js_return_active = false;
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002092 bool at_debug_break_slot = false;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002093 RelocIterator it(debug_info->code());
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002094 while (!it.done() && !at_js_return && !at_debug_break_slot) {
ager@chromium.org236ad962008-09-25 09:45:57 +00002095 if (RelocInfo::IsJSReturn(it.rinfo()->rmode())) {
ager@chromium.orga1645e22009-09-09 19:27:10 +00002096 at_js_return = (it.rinfo()->pc() ==
2097 addr - Assembler::kPatchReturnSequenceAddressOffset);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002098 break_at_js_return_active = it.rinfo()->IsPatchedReturnSequence();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002099 }
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002100 if (RelocInfo::IsDebugBreakSlot(it.rinfo()->rmode())) {
2101 at_debug_break_slot = (it.rinfo()->pc() ==
2102 addr - Assembler::kPatchDebugBreakSlotAddressOffset);
2103 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002104 it.next();
2105 }
2106
2107 // Handle the jump to continue execution after break point depending on the
2108 // break location.
ager@chromium.orga1645e22009-09-09 19:27:10 +00002109 if (at_js_return) {
2110 // If the break point as return is still active jump to the corresponding
2111 // place in the original code. If not the break point was removed during
2112 // break point processing.
2113 if (break_at_js_return_active) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002114 addr += original_code->instruction_start() - code->instruction_start();
2115 }
2116
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00002117 // Move back to where the call instruction sequence started.
2118 thread_local_.after_break_target_ =
2119 addr - Assembler::kPatchReturnSequenceAddressOffset;
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002120 } else if (at_debug_break_slot) {
2121 // Address of where the debug break slot starts.
2122 addr = addr - Assembler::kPatchDebugBreakSlotAddressOffset;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002123
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002124 // Continue just after the slot.
2125 thread_local_.after_break_target_ = addr + Assembler::kDebugBreakSlotLength;
2126 } else if (IsDebugBreak(Assembler::target_address_at(addr))) {
2127 // We now know that there is still a debug break call at the target address,
2128 // so the break point is still there and the original code will hold the
2129 // address to jump to in order to complete the call which is replaced by a
2130 // call to DebugBreakXXX.
2131
2132 // Find the corresponding address in the original code.
2133 addr += original_code->instruction_start() - code->instruction_start();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002134
2135 // Install jump to the call address in the original code. This will be the
2136 // call which was overwritten by the call to DebugBreakXXX.
2137 thread_local_.after_break_target_ = Assembler::target_address_at(addr);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002138 } else {
2139 // There is no longer a break point present. Don't try to look in the
2140 // original code as the running code will have the right address. This takes
2141 // care of the case where the last break point is removed from the function
2142 // and therefore no "original code" is available.
2143 thread_local_.after_break_target_ = Assembler::target_address_at(addr);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002144 }
2145}
2146
2147
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00002148bool Debug::IsBreakAtReturn(JavaScriptFrame* frame) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002149 HandleScope scope(isolate_);
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00002150
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00002151 // If there are no break points this cannot be break at return, as
2152 // the debugger statement and stack guard bebug break cannot be at
2153 // return.
2154 if (!has_break_points_) {
2155 return false;
2156 }
2157
lrn@chromium.org34e60782011-09-15 07:25:40 +00002158 PrepareForBreakPoints();
2159
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00002160 // Get the executing function in which the debug break occurred.
2161 Handle<SharedFunctionInfo> shared =
2162 Handle<SharedFunctionInfo>(JSFunction::cast(frame->function())->shared());
2163 if (!EnsureDebugInfo(shared)) {
2164 // Return if we failed to retrieve the debug info.
2165 return false;
2166 }
2167 Handle<DebugInfo> debug_info = GetDebugInfo(shared);
2168 Handle<Code> code(debug_info->code());
2169#ifdef DEBUG
2170 // Get the code which is actually executing.
vegorov@chromium.org74f333b2011-04-06 11:17:46 +00002171 Handle<Code> frame_code(frame->LookupCode());
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00002172 ASSERT(frame_code.is_identical_to(code));
2173#endif
2174
2175 // Find the call address in the running code.
2176 Address addr = frame->pc() - Assembler::kCallTargetAddressOffset;
2177
2178 // Check if the location is at JS return.
2179 RelocIterator it(debug_info->code());
2180 while (!it.done()) {
2181 if (RelocInfo::IsJSReturn(it.rinfo()->rmode())) {
2182 return (it.rinfo()->pc() ==
2183 addr - Assembler::kPatchReturnSequenceAddressOffset);
2184 }
2185 it.next();
2186 }
2187 return false;
2188}
2189
2190
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00002191void Debug::FramesHaveBeenDropped(StackFrame::Id new_break_frame_id,
whesse@chromium.orge90029b2010-08-02 11:52:17 +00002192 FrameDropMode mode,
2193 Object** restarter_frame_function_pointer) {
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00002194 thread_local_.frame_drop_mode_ = mode;
ager@chromium.org357bf652010-04-12 11:30:10 +00002195 thread_local_.break_frame_id_ = new_break_frame_id;
whesse@chromium.orge90029b2010-08-02 11:52:17 +00002196 thread_local_.restarter_frame_function_pointer_ =
2197 restarter_frame_function_pointer;
ager@chromium.org357bf652010-04-12 11:30:10 +00002198}
2199
2200
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002201bool Debug::IsDebugGlobal(GlobalObject* global) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002202 return IsLoaded() && global == debug_context()->global();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002203}
2204
2205
ager@chromium.org32912102009-01-16 10:38:43 +00002206void Debug::ClearMirrorCache() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002207 PostponeInterruptsScope postpone(isolate_);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002208 HandleScope scope(isolate_);
2209 ASSERT(isolate_->context() == *Debug::debug_context());
ager@chromium.org32912102009-01-16 10:38:43 +00002210
2211 // Clear the mirror cache.
2212 Handle<String> function_name =
lrn@chromium.org7516f052011-03-30 08:52:27 +00002213 isolate_->factory()->LookupSymbol(CStrVector("ClearMirrorCache"));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002214 Handle<Object> fun(Isolate::Current()->global()->GetPropertyNoExceptionThrown(
lrn@chromium.org303ada72010-10-27 09:33:13 +00002215 *function_name));
ager@chromium.org32912102009-01-16 10:38:43 +00002216 ASSERT(fun->IsJSFunction());
2217 bool caught_exception;
rossberg@chromium.org717967f2011-07-20 13:44:42 +00002218 Execution::TryCall(Handle<JSFunction>::cast(fun),
ager@chromium.org32912102009-01-16 10:38:43 +00002219 Handle<JSObject>(Debug::debug_context()->global()),
2220 0, NULL, &caught_exception);
2221}
2222
2223
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002224void Debug::CreateScriptCache() {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002225 Heap* heap = isolate_->heap();
2226 HandleScope scope(isolate_);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002227
2228 // Perform two GCs to get rid of all unreferenced scripts. The first GC gets
2229 // rid of all the cached script wrappers and the second gets rid of the
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002230 // scripts which are no longer referenced. The second also sweeps precisely,
2231 // which saves us doing yet another GC to make the heap iterable.
2232 heap->CollectAllGarbage(Heap::kNoGCFlags);
2233 heap->CollectAllGarbage(Heap::kMakeHeapIterableMask);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002234
2235 ASSERT(script_cache_ == NULL);
2236 script_cache_ = new ScriptCache();
2237
2238 // Scan heap for Script objects.
2239 int count = 0;
2240 HeapIterator iterator;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002241 AssertNoAllocation no_allocation;
2242
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002243 for (HeapObject* obj = iterator.next(); obj != NULL; obj = iterator.next()) {
sgjesse@chromium.org152a0b02009-10-07 13:50:16 +00002244 if (obj->IsScript() && Script::cast(obj)->HasValidSource()) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002245 script_cache_->Add(Handle<Script>(Script::cast(obj)));
2246 count++;
2247 }
2248 }
2249}
2250
2251
2252void Debug::DestroyScriptCache() {
2253 // Get rid of the script cache if it was created.
2254 if (script_cache_ != NULL) {
2255 delete script_cache_;
2256 script_cache_ = NULL;
2257 }
2258}
2259
2260
2261void Debug::AddScriptToScriptCache(Handle<Script> script) {
2262 if (script_cache_ != NULL) {
2263 script_cache_->Add(script);
2264 }
2265}
2266
2267
2268Handle<FixedArray> Debug::GetLoadedScripts() {
2269 // Create and fill the script cache when the loaded scripts is requested for
2270 // the first time.
2271 if (script_cache_ == NULL) {
2272 CreateScriptCache();
2273 }
2274
2275 // If the script cache is not active just return an empty array.
2276 ASSERT(script_cache_ != NULL);
2277 if (script_cache_ == NULL) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002278 isolate_->factory()->NewFixedArray(0);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002279 }
2280
2281 // Perform GC to get unreferenced scripts evicted from the cache before
2282 // returning the content.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002283 isolate_->heap()->CollectAllGarbage(Heap::kNoGCFlags);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002284
2285 // Get the scripts from the cache.
2286 return script_cache_->GetScripts();
2287}
2288
2289
2290void Debug::AfterGarbageCollection() {
2291 // Generate events for collected scripts.
2292 if (script_cache_ != NULL) {
2293 script_cache_->ProcessCollectedScripts();
2294 }
2295}
2296
2297
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00002298Debugger::Debugger(Isolate* isolate)
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +00002299 : debugger_access_(isolate->debugger_access()),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002300 event_listener_(Handle<Object>()),
2301 event_listener_data_(Handle<Object>()),
2302 compiling_natives_(false),
2303 is_loading_debugger_(false),
2304 never_unload_debugger_(false),
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002305 force_debugger_active_(false),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002306 message_handler_(NULL),
2307 debugger_unload_pending_(false),
2308 host_dispatch_handler_(NULL),
2309 dispatch_handler_access_(OS::CreateMutex()),
2310 debug_message_dispatch_handler_(NULL),
2311 message_dispatch_helper_thread_(NULL),
2312 host_dispatch_micros_(100 * 1000),
2313 agent_(NULL),
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00002314 command_queue_(isolate->logger(), kQueueInitialSize),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002315 command_received_(OS::CreateSemaphore(0)),
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00002316 event_command_queue_(isolate->logger(), kQueueInitialSize),
2317 isolate_(isolate) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002318}
2319
2320
2321Debugger::~Debugger() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002322 delete dispatch_handler_access_;
2323 dispatch_handler_access_ = 0;
2324 delete command_received_;
2325 command_received_ = 0;
2326}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002327
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002328
2329Handle<Object> Debugger::MakeJSObject(Vector<const char> constructor_name,
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002330 int argc,
2331 Handle<Object> argv[],
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002332 bool* caught_exception) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002333 ASSERT(isolate_->context() == *isolate_->debug()->debug_context());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002334
2335 // Create the execution state object.
lrn@chromium.org7516f052011-03-30 08:52:27 +00002336 Handle<String> constructor_str =
2337 isolate_->factory()->LookupSymbol(constructor_name);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002338 Handle<Object> constructor(
2339 isolate_->global()->GetPropertyNoExceptionThrown(*constructor_str));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002340 ASSERT(constructor->IsJSFunction());
2341 if (!constructor->IsJSFunction()) {
2342 *caught_exception = true;
lrn@chromium.org7516f052011-03-30 08:52:27 +00002343 return isolate_->factory()->undefined_value();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002344 }
2345 Handle<Object> js_object = Execution::TryCall(
2346 Handle<JSFunction>::cast(constructor),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002347 Handle<JSObject>(isolate_->debug()->debug_context()->global()),
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002348 argc,
2349 argv,
2350 caught_exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002351 return js_object;
2352}
2353
2354
2355Handle<Object> Debugger::MakeExecutionState(bool* caught_exception) {
2356 // Create the execution state object.
lrn@chromium.org7516f052011-03-30 08:52:27 +00002357 Handle<Object> break_id = isolate_->factory()->NewNumberFromInt(
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002358 isolate_->debug()->break_id());
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002359 Handle<Object> argv[] = { break_id };
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002360 return MakeJSObject(CStrVector("MakeExecutionState"),
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002361 ARRAY_SIZE(argv),
2362 argv,
2363 caught_exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002364}
2365
2366
2367Handle<Object> Debugger::MakeBreakEvent(Handle<Object> exec_state,
2368 Handle<Object> break_points_hit,
2369 bool* caught_exception) {
2370 // Create the new break event object.
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002371 Handle<Object> argv[] = { exec_state, break_points_hit };
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002372 return MakeJSObject(CStrVector("MakeBreakEvent"),
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002373 ARRAY_SIZE(argv),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002374 argv,
2375 caught_exception);
2376}
2377
2378
2379Handle<Object> Debugger::MakeExceptionEvent(Handle<Object> exec_state,
2380 Handle<Object> exception,
2381 bool uncaught,
2382 bool* caught_exception) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002383 Factory* factory = isolate_->factory();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002384 // Create the new exception event object.
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002385 Handle<Object> argv[] = { exec_state,
2386 exception,
2387 factory->ToBoolean(uncaught) };
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002388 return MakeJSObject(CStrVector("MakeExceptionEvent"),
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002389 ARRAY_SIZE(argv),
2390 argv,
2391 caught_exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002392}
2393
2394
2395Handle<Object> Debugger::MakeNewFunctionEvent(Handle<Object> function,
2396 bool* caught_exception) {
2397 // Create the new function event object.
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002398 Handle<Object> argv[] = { function };
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002399 return MakeJSObject(CStrVector("MakeNewFunctionEvent"),
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002400 ARRAY_SIZE(argv),
2401 argv,
2402 caught_exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002403}
2404
2405
2406Handle<Object> Debugger::MakeCompileEvent(Handle<Script> script,
iposva@chromium.org245aa852009-02-10 00:49:54 +00002407 bool before,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002408 bool* caught_exception) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002409 Factory* factory = isolate_->factory();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002410 // Create the compile event object.
2411 Handle<Object> exec_state = MakeExecutionState(caught_exception);
iposva@chromium.org245aa852009-02-10 00:49:54 +00002412 Handle<Object> script_wrapper = GetScriptWrapper(script);
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002413 Handle<Object> argv[] = { exec_state,
2414 script_wrapper,
2415 factory->ToBoolean(before) };
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002416 return MakeJSObject(CStrVector("MakeCompileEvent"),
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002417 ARRAY_SIZE(argv),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002418 argv,
2419 caught_exception);
2420}
2421
2422
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002423Handle<Object> Debugger::MakeScriptCollectedEvent(int id,
2424 bool* caught_exception) {
2425 // Create the script collected event object.
2426 Handle<Object> exec_state = MakeExecutionState(caught_exception);
2427 Handle<Object> id_object = Handle<Smi>(Smi::FromInt(id));
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002428 Handle<Object> argv[] = { exec_state, id_object };
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002429
2430 return MakeJSObject(CStrVector("MakeScriptCollectedEvent"),
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002431 ARRAY_SIZE(argv),
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002432 argv,
2433 caught_exception);
2434}
2435
2436
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002437void Debugger::OnException(Handle<Object> exception, bool uncaught) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002438 HandleScope scope(isolate_);
2439 Debug* debug = isolate_->debug();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002440
2441 // Bail out based on state or if there is no listener for this event
lrn@chromium.org7516f052011-03-30 08:52:27 +00002442 if (debug->InDebugger()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002443 if (!Debugger::EventActive(v8::Exception)) return;
2444
2445 // Bail out if exception breaks are not active
2446 if (uncaught) {
2447 // Uncaught exceptions are reported by either flags.
lrn@chromium.org7516f052011-03-30 08:52:27 +00002448 if (!(debug->break_on_uncaught_exception() ||
2449 debug->break_on_exception())) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002450 } else {
2451 // Caught exceptions are reported is activated.
lrn@chromium.org7516f052011-03-30 08:52:27 +00002452 if (!debug->break_on_exception()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002453 }
2454
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002455 // Enter the debugger.
2456 EnterDebugger debugger;
2457 if (debugger.FailedToEnter()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002458
2459 // Clear all current stepping setup.
lrn@chromium.org7516f052011-03-30 08:52:27 +00002460 debug->ClearStepping();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002461 // Create the event data object.
2462 bool caught_exception = false;
2463 Handle<Object> exec_state = MakeExecutionState(&caught_exception);
2464 Handle<Object> event_data;
2465 if (!caught_exception) {
2466 event_data = MakeExceptionEvent(exec_state, exception, uncaught,
2467 &caught_exception);
2468 }
2469 // Bail out and don't call debugger if exception.
2470 if (caught_exception) {
2471 return;
2472 }
2473
ager@chromium.org5ec48922009-05-05 07:25:34 +00002474 // Process debug event.
2475 ProcessDebugEvent(v8::Exception, Handle<JSObject>::cast(event_data), false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002476 // Return to continue execution from where the exception was thrown.
2477}
2478
2479
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002480void Debugger::OnDebugBreak(Handle<Object> break_points_hit,
2481 bool auto_continue) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002482 HandleScope scope(isolate_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002483
kasper.lund212ac232008-07-16 07:07:30 +00002484 // Debugger has already been entered by caller.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002485 ASSERT(isolate_->context() == *isolate_->debug()->debug_context());
kasper.lund212ac232008-07-16 07:07:30 +00002486
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002487 // Bail out if there is no listener for this event
2488 if (!Debugger::EventActive(v8::Break)) return;
2489
2490 // Debugger must be entered in advance.
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00002491 ASSERT(isolate_->context() == *isolate_->debug()->debug_context());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002492
2493 // Create the event data object.
2494 bool caught_exception = false;
2495 Handle<Object> exec_state = MakeExecutionState(&caught_exception);
2496 Handle<Object> event_data;
2497 if (!caught_exception) {
2498 event_data = MakeBreakEvent(exec_state, break_points_hit,
2499 &caught_exception);
2500 }
2501 // Bail out and don't call debugger if exception.
2502 if (caught_exception) {
2503 return;
2504 }
2505
ager@chromium.org5ec48922009-05-05 07:25:34 +00002506 // Process debug event.
2507 ProcessDebugEvent(v8::Break,
2508 Handle<JSObject>::cast(event_data),
2509 auto_continue);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002510}
2511
2512
2513void Debugger::OnBeforeCompile(Handle<Script> script) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002514 HandleScope scope(isolate_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002515
2516 // Bail out based on state or if there is no listener for this event
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002517 if (isolate_->debug()->InDebugger()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002518 if (compiling_natives()) return;
2519 if (!EventActive(v8::BeforeCompile)) return;
2520
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002521 // Enter the debugger.
2522 EnterDebugger debugger;
2523 if (debugger.FailedToEnter()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002524
2525 // Create the event data object.
2526 bool caught_exception = false;
iposva@chromium.org245aa852009-02-10 00:49:54 +00002527 Handle<Object> event_data = MakeCompileEvent(script, true, &caught_exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002528 // Bail out and don't call debugger if exception.
2529 if (caught_exception) {
2530 return;
2531 }
2532
ager@chromium.org5ec48922009-05-05 07:25:34 +00002533 // Process debug event.
2534 ProcessDebugEvent(v8::BeforeCompile,
2535 Handle<JSObject>::cast(event_data),
2536 true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002537}
2538
2539
2540// Handle debugger actions when a new script is compiled.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00002541void Debugger::OnAfterCompile(Handle<Script> script,
2542 AfterCompileFlags after_compile_flags) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002543 HandleScope scope(isolate_);
2544 Debug* debug = isolate_->debug();
kasper.lund212ac232008-07-16 07:07:30 +00002545
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002546 // Add the newly compiled script to the script cache.
lrn@chromium.org7516f052011-03-30 08:52:27 +00002547 debug->AddScriptToScriptCache(script);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002548
2549 // No more to do if not debugging.
ager@chromium.org71daaf62009-04-01 07:22:49 +00002550 if (!IsDebuggerActive()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002551
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002552 // No compile events while compiling natives.
2553 if (compiling_natives()) return;
2554
iposva@chromium.org245aa852009-02-10 00:49:54 +00002555 // Store whether in debugger before entering debugger.
lrn@chromium.org7516f052011-03-30 08:52:27 +00002556 bool in_debugger = debug->InDebugger();
iposva@chromium.org245aa852009-02-10 00:49:54 +00002557
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002558 // Enter the debugger.
2559 EnterDebugger debugger;
2560 if (debugger.FailedToEnter()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002561
2562 // If debugging there might be script break points registered for this
2563 // script. Make sure that these break points are set.
2564
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002565 // Get the function UpdateScriptBreakPoints (defined in debug-debugger.js).
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002566 Handle<String> update_script_break_points_symbol =
lrn@chromium.org7516f052011-03-30 08:52:27 +00002567 isolate_->factory()->LookupAsciiSymbol("UpdateScriptBreakPoints");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002568 Handle<Object> update_script_break_points =
lrn@chromium.org7516f052011-03-30 08:52:27 +00002569 Handle<Object>(debug->debug_context()->global()->
lrn@chromium.org303ada72010-10-27 09:33:13 +00002570 GetPropertyNoExceptionThrown(*update_script_break_points_symbol));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002571 if (!update_script_break_points->IsJSFunction()) {
2572 return;
2573 }
2574 ASSERT(update_script_break_points->IsJSFunction());
2575
2576 // Wrap the script object in a proper JS object before passing it
2577 // to JavaScript.
2578 Handle<JSValue> wrapper = GetScriptWrapper(script);
2579
2580 // Call UpdateScriptBreakPoints expect no exceptions.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002581 bool caught_exception;
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002582 Handle<Object> argv[] = { wrapper };
rossberg@chromium.org717967f2011-07-20 13:44:42 +00002583 Execution::TryCall(Handle<JSFunction>::cast(update_script_break_points),
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002584 Isolate::Current()->js_builtins_object(),
2585 ARRAY_SIZE(argv),
2586 argv,
2587 &caught_exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002588 if (caught_exception) {
2589 return;
2590 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002591 // Bail out based on state or if there is no listener for this event
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00002592 if (in_debugger && (after_compile_flags & SEND_WHEN_DEBUGGING) == 0) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002593 if (!Debugger::EventActive(v8::AfterCompile)) return;
2594
2595 // Create the compile state object.
2596 Handle<Object> event_data = MakeCompileEvent(script,
iposva@chromium.org245aa852009-02-10 00:49:54 +00002597 false,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002598 &caught_exception);
2599 // Bail out and don't call debugger if exception.
2600 if (caught_exception) {
2601 return;
2602 }
ager@chromium.org5ec48922009-05-05 07:25:34 +00002603 // Process debug event.
2604 ProcessDebugEvent(v8::AfterCompile,
2605 Handle<JSObject>::cast(event_data),
2606 true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002607}
2608
2609
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002610void Debugger::OnScriptCollected(int id) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002611 HandleScope scope(isolate_);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002612
2613 // No more to do if not debugging.
2614 if (!IsDebuggerActive()) return;
2615 if (!Debugger::EventActive(v8::ScriptCollected)) return;
2616
2617 // Enter the debugger.
2618 EnterDebugger debugger;
2619 if (debugger.FailedToEnter()) return;
2620
2621 // Create the script collected state object.
2622 bool caught_exception = false;
2623 Handle<Object> event_data = MakeScriptCollectedEvent(id,
2624 &caught_exception);
2625 // Bail out and don't call debugger if exception.
2626 if (caught_exception) {
2627 return;
2628 }
2629
2630 // Process debug event.
2631 ProcessDebugEvent(v8::ScriptCollected,
2632 Handle<JSObject>::cast(event_data),
2633 true);
2634}
2635
2636
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002637void Debugger::ProcessDebugEvent(v8::DebugEvent event,
ager@chromium.org5ec48922009-05-05 07:25:34 +00002638 Handle<JSObject> event_data,
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002639 bool auto_continue) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002640 HandleScope scope(isolate_);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002641
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +00002642 // Clear any pending debug break if this is a real break.
2643 if (!auto_continue) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002644 isolate_->debug()->clear_interrupt_pending(DEBUGBREAK);
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +00002645 }
2646
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002647 // Create the execution state.
2648 bool caught_exception = false;
2649 Handle<Object> exec_state = MakeExecutionState(&caught_exception);
2650 if (caught_exception) {
2651 return;
2652 }
ager@chromium.org41826e72009-03-30 13:30:57 +00002653 // First notify the message handler if any.
2654 if (message_handler_ != NULL) {
ager@chromium.org5ec48922009-05-05 07:25:34 +00002655 NotifyMessageHandler(event,
2656 Handle<JSObject>::cast(exec_state),
2657 event_data,
2658 auto_continue);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002659 }
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00002660 // Notify registered debug event listener. This can be either a C or
2661 // a JavaScript function. Don't call event listener for v8::Break
2662 // here, if it's only a debug command -- they will be processed later.
2663 if ((event != v8::Break || !auto_continue) && !event_listener_.is_null()) {
2664 CallEventCallback(event, exec_state, event_data, NULL);
2665 }
2666 // Process pending debug commands.
2667 if (event == v8::Break) {
2668 while (!event_command_queue_.IsEmpty()) {
2669 CommandMessage command = event_command_queue_.Get();
2670 if (!event_listener_.is_null()) {
2671 CallEventCallback(v8::BreakForCommand,
2672 exec_state,
2673 event_data,
2674 command.client_data());
2675 }
2676 command.Dispose();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002677 }
2678 }
2679}
2680
2681
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00002682void Debugger::CallEventCallback(v8::DebugEvent event,
2683 Handle<Object> exec_state,
2684 Handle<Object> event_data,
2685 v8::Debug::ClientData* client_data) {
ager@chromium.orgea91cc52011-05-23 06:06:11 +00002686 if (event_listener_->IsForeign()) {
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00002687 CallCEventCallback(event, exec_state, event_data, client_data);
2688 } else {
2689 CallJSEventCallback(event, exec_state, event_data);
2690 }
2691}
2692
2693
2694void Debugger::CallCEventCallback(v8::DebugEvent event,
2695 Handle<Object> exec_state,
2696 Handle<Object> event_data,
2697 v8::Debug::ClientData* client_data) {
ager@chromium.orgea91cc52011-05-23 06:06:11 +00002698 Handle<Foreign> callback_obj(Handle<Foreign>::cast(event_listener_));
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00002699 v8::Debug::EventCallback2 callback =
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002700 FUNCTION_CAST<v8::Debug::EventCallback2>(
2701 callback_obj->foreign_address());
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00002702 EventDetailsImpl event_details(
2703 event,
2704 Handle<JSObject>::cast(exec_state),
2705 Handle<JSObject>::cast(event_data),
2706 event_listener_data_,
2707 client_data);
2708 callback(event_details);
2709}
2710
2711
2712void Debugger::CallJSEventCallback(v8::DebugEvent event,
2713 Handle<Object> exec_state,
2714 Handle<Object> event_data) {
2715 ASSERT(event_listener_->IsJSFunction());
2716 Handle<JSFunction> fun(Handle<JSFunction>::cast(event_listener_));
2717
2718 // Invoke the JavaScript debug event listener.
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002719 Handle<Object> argv[] = { Handle<Object>(Smi::FromInt(event)),
2720 exec_state,
2721 event_data,
2722 event_listener_data_ };
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002723 bool caught_exception;
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002724 Execution::TryCall(fun,
2725 isolate_->global(),
2726 ARRAY_SIZE(argv),
2727 argv,
2728 &caught_exception);
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00002729 // Silently ignore exceptions from debug event listeners.
2730}
2731
2732
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00002733Handle<Context> Debugger::GetDebugContext() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002734 never_unload_debugger_ = true;
2735 EnterDebugger debugger;
2736 return isolate_->debug()->debug_context();
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00002737}
2738
2739
ager@chromium.org71daaf62009-04-01 07:22:49 +00002740void Debugger::UnloadDebugger() {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002741 Debug* debug = isolate_->debug();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002742
ager@chromium.org71daaf62009-04-01 07:22:49 +00002743 // Make sure that there are no breakpoints left.
lrn@chromium.org7516f052011-03-30 08:52:27 +00002744 debug->ClearAllBreakPoints();
ager@chromium.org71daaf62009-04-01 07:22:49 +00002745
2746 // Unload the debugger if feasible.
2747 if (!never_unload_debugger_) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002748 debug->Unload();
ager@chromium.org71daaf62009-04-01 07:22:49 +00002749 }
2750
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002751 // Clear the flag indicating that the debugger should be unloaded.
2752 debugger_unload_pending_ = false;
ager@chromium.org71daaf62009-04-01 07:22:49 +00002753}
2754
2755
ager@chromium.org41826e72009-03-30 13:30:57 +00002756void Debugger::NotifyMessageHandler(v8::DebugEvent event,
ager@chromium.org5ec48922009-05-05 07:25:34 +00002757 Handle<JSObject> exec_state,
2758 Handle<JSObject> event_data,
ager@chromium.org41826e72009-03-30 13:30:57 +00002759 bool auto_continue) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002760 HandleScope scope(isolate_);
ager@chromium.org41826e72009-03-30 13:30:57 +00002761
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002762 if (!isolate_->debug()->Load()) return;
ager@chromium.org41826e72009-03-30 13:30:57 +00002763
2764 // Process the individual events.
ager@chromium.org5ec48922009-05-05 07:25:34 +00002765 bool sendEventMessage = false;
ager@chromium.org41826e72009-03-30 13:30:57 +00002766 switch (event) {
2767 case v8::Break:
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00002768 case v8::BreakForCommand:
ager@chromium.org5ec48922009-05-05 07:25:34 +00002769 sendEventMessage = !auto_continue;
ager@chromium.org41826e72009-03-30 13:30:57 +00002770 break;
2771 case v8::Exception:
ager@chromium.org5ec48922009-05-05 07:25:34 +00002772 sendEventMessage = true;
ager@chromium.org41826e72009-03-30 13:30:57 +00002773 break;
2774 case v8::BeforeCompile:
2775 break;
2776 case v8::AfterCompile:
ager@chromium.org5ec48922009-05-05 07:25:34 +00002777 sendEventMessage = true;
ager@chromium.org41826e72009-03-30 13:30:57 +00002778 break;
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002779 case v8::ScriptCollected:
2780 sendEventMessage = true;
2781 break;
ager@chromium.org41826e72009-03-30 13:30:57 +00002782 case v8::NewFunction:
2783 break;
2784 default:
2785 UNREACHABLE();
2786 }
2787
ager@chromium.org5ec48922009-05-05 07:25:34 +00002788 // The debug command interrupt flag might have been set when the command was
2789 // added. It should be enough to clear the flag only once while we are in the
2790 // debugger.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002791 ASSERT(isolate_->debug()->InDebugger());
2792 isolate_->stack_guard()->Continue(DEBUGCOMMAND);
ager@chromium.org5ec48922009-05-05 07:25:34 +00002793
2794 // Notify the debugger that a debug event has occurred unless auto continue is
2795 // active in which case no event is send.
2796 if (sendEventMessage) {
2797 MessageImpl message = MessageImpl::NewEvent(
2798 event,
2799 auto_continue,
2800 Handle<JSObject>::cast(exec_state),
2801 Handle<JSObject>::cast(event_data));
2802 InvokeMessageHandler(message);
2803 }
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +00002804
2805 // If auto continue don't make the event cause a break, but process messages
2806 // in the queue if any. For script collected events don't even process
2807 // messages in the queue as the execution state might not be what is expected
2808 // by the client.
ager@chromium.org6ffc2172009-05-29 19:20:16 +00002809 if ((auto_continue && !HasCommands()) || event == v8::ScriptCollected) {
ager@chromium.org5ec48922009-05-05 07:25:34 +00002810 return;
2811 }
ager@chromium.org41826e72009-03-30 13:30:57 +00002812
ager@chromium.org41826e72009-03-30 13:30:57 +00002813 v8::TryCatch try_catch;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002814
2815 // DebugCommandProcessor goes here.
2816 v8::Local<v8::Object> cmd_processor;
2817 {
2818 v8::Local<v8::Object> api_exec_state =
2819 v8::Utils::ToLocal(Handle<JSObject>::cast(exec_state));
2820 v8::Local<v8::String> fun_name =
2821 v8::String::New("debugCommandProcessor");
2822 v8::Local<v8::Function> fun =
2823 v8::Function::Cast(*api_exec_state->Get(fun_name));
2824
2825 v8::Handle<v8::Boolean> running =
2826 auto_continue ? v8::True() : v8::False();
2827 static const int kArgc = 1;
2828 v8::Handle<Value> argv[kArgc] = { running };
2829 cmd_processor = v8::Object::Cast(*fun->Call(api_exec_state, kArgc, argv));
2830 if (try_catch.HasCaught()) {
2831 PrintLn(try_catch.Exception());
2832 return;
2833 }
ager@chromium.org41826e72009-03-30 13:30:57 +00002834 }
2835
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002836 bool running = auto_continue;
2837
ager@chromium.org41826e72009-03-30 13:30:57 +00002838 // Process requests from the debugger.
2839 while (true) {
2840 // Wait for new command in the queue.
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002841 if (Debugger::host_dispatch_handler_) {
2842 // In case there is a host dispatch - do periodic dispatches.
2843 if (!command_received_->Wait(host_dispatch_micros_)) {
2844 // Timout expired, do the dispatch.
2845 Debugger::host_dispatch_handler_();
2846 continue;
2847 }
2848 } else {
2849 // In case there is no host dispatch - just wait.
2850 command_received_->Wait();
2851 }
ager@chromium.org41826e72009-03-30 13:30:57 +00002852
ager@chromium.org41826e72009-03-30 13:30:57 +00002853 // Get the command from the queue.
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00002854 CommandMessage command = command_queue_.Get();
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00002855 isolate_->logger()->DebugTag(
2856 "Got request from command queue, in interactive loop.");
ager@chromium.org71daaf62009-04-01 07:22:49 +00002857 if (!Debugger::IsDebuggerActive()) {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002858 // Delete command text and user data.
2859 command.Dispose();
ager@chromium.org41826e72009-03-30 13:30:57 +00002860 return;
2861 }
2862
ager@chromium.org41826e72009-03-30 13:30:57 +00002863 // Invoke JavaScript to process the debug request.
2864 v8::Local<v8::String> fun_name;
2865 v8::Local<v8::Function> fun;
2866 v8::Local<v8::Value> request;
2867 v8::TryCatch try_catch;
2868 fun_name = v8::String::New("processDebugRequest");
2869 fun = v8::Function::Cast(*cmd_processor->Get(fun_name));
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002870
2871 request = v8::String::New(command.text().start(),
2872 command.text().length());
ager@chromium.org41826e72009-03-30 13:30:57 +00002873 static const int kArgc = 1;
2874 v8::Handle<Value> argv[kArgc] = { request };
2875 v8::Local<v8::Value> response_val = fun->Call(cmd_processor, kArgc, argv);
2876
2877 // Get the response.
2878 v8::Local<v8::String> response;
ager@chromium.org41826e72009-03-30 13:30:57 +00002879 if (!try_catch.HasCaught()) {
2880 // Get response string.
2881 if (!response_val->IsUndefined()) {
2882 response = v8::String::Cast(*response_val);
2883 } else {
2884 response = v8::String::New("");
2885 }
2886
2887 // Log the JSON request/response.
2888 if (FLAG_trace_debug_json) {
2889 PrintLn(request);
2890 PrintLn(response);
2891 }
2892
2893 // Get the running state.
2894 fun_name = v8::String::New("isRunning");
2895 fun = v8::Function::Cast(*cmd_processor->Get(fun_name));
2896 static const int kArgc = 1;
2897 v8::Handle<Value> argv[kArgc] = { response };
2898 v8::Local<v8::Value> running_val = fun->Call(cmd_processor, kArgc, argv);
2899 if (!try_catch.HasCaught()) {
2900 running = running_val->ToBoolean()->Value();
2901 }
2902 } else {
2903 // In case of failure the result text is the exception text.
2904 response = try_catch.Exception()->ToString();
2905 }
2906
ager@chromium.org41826e72009-03-30 13:30:57 +00002907 // Return the result.
ager@chromium.org5ec48922009-05-05 07:25:34 +00002908 MessageImpl message = MessageImpl::NewResponse(
2909 event,
2910 running,
2911 Handle<JSObject>::cast(exec_state),
2912 Handle<JSObject>::cast(event_data),
2913 Handle<String>(Utils::OpenHandle(*response)),
2914 command.client_data());
2915 InvokeMessageHandler(message);
2916 command.Dispose();
ager@chromium.org41826e72009-03-30 13:30:57 +00002917
2918 // Return from debug event processing if either the VM is put into the
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00002919 // running state (through a continue command) or auto continue is active
ager@chromium.org41826e72009-03-30 13:30:57 +00002920 // and there are no more commands queued.
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002921 if (running && !HasCommands()) {
ager@chromium.org41826e72009-03-30 13:30:57 +00002922 return;
2923 }
2924 }
2925}
2926
2927
iposva@chromium.org245aa852009-02-10 00:49:54 +00002928void Debugger::SetEventListener(Handle<Object> callback,
2929 Handle<Object> data) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002930 HandleScope scope(isolate_);
2931 GlobalHandles* global_handles = isolate_->global_handles();
iposva@chromium.org245aa852009-02-10 00:49:54 +00002932
2933 // Clear the global handles for the event listener and the event listener data
2934 // object.
2935 if (!event_listener_.is_null()) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002936 global_handles->Destroy(
iposva@chromium.org245aa852009-02-10 00:49:54 +00002937 reinterpret_cast<Object**>(event_listener_.location()));
2938 event_listener_ = Handle<Object>();
2939 }
2940 if (!event_listener_data_.is_null()) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002941 global_handles->Destroy(
iposva@chromium.org245aa852009-02-10 00:49:54 +00002942 reinterpret_cast<Object**>(event_listener_data_.location()));
2943 event_listener_data_ = Handle<Object>();
2944 }
2945
2946 // If there is a new debug event listener register it together with its data
2947 // object.
2948 if (!callback->IsUndefined() && !callback->IsNull()) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002949 event_listener_ = Handle<Object>::cast(
lrn@chromium.org7516f052011-03-30 08:52:27 +00002950 global_handles->Create(*callback));
iposva@chromium.org245aa852009-02-10 00:49:54 +00002951 if (data.is_null()) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002952 data = isolate_->factory()->undefined_value();
iposva@chromium.org245aa852009-02-10 00:49:54 +00002953 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002954 event_listener_data_ = Handle<Object>::cast(
lrn@chromium.org7516f052011-03-30 08:52:27 +00002955 global_handles->Create(*data));
iposva@chromium.org245aa852009-02-10 00:49:54 +00002956 }
2957
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002958 ListenersChanged();
iposva@chromium.org245aa852009-02-10 00:49:54 +00002959}
2960
2961
ager@chromium.org5ec48922009-05-05 07:25:34 +00002962void Debugger::SetMessageHandler(v8::Debug::MessageHandler2 handler) {
ager@chromium.org71daaf62009-04-01 07:22:49 +00002963 ScopedLock with(debugger_access_);
2964
ager@chromium.org381abbb2009-02-25 13:23:22 +00002965 message_handler_ = handler;
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002966 ListenersChanged();
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00002967 if (handler == NULL) {
ager@chromium.org71daaf62009-04-01 07:22:49 +00002968 // Send an empty command to the debugger if in a break to make JavaScript
2969 // run again if the debugger is closed.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002970 if (isolate_->debug()->InDebugger()) {
ager@chromium.org71daaf62009-04-01 07:22:49 +00002971 ProcessCommand(Vector<const uint16_t>::empty());
2972 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002973 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002974}
2975
2976
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002977void Debugger::ListenersChanged() {
2978 if (IsDebuggerActive()) {
2979 // Disable the compilation cache when the debugger is active.
lrn@chromium.org7516f052011-03-30 08:52:27 +00002980 isolate_->compilation_cache()->Disable();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002981 debugger_unload_pending_ = false;
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002982 } else {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002983 isolate_->compilation_cache()->Enable();
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002984 // Unload the debugger if event listener and message handler cleared.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002985 // Schedule this for later, because we may be in non-V8 thread.
2986 debugger_unload_pending_ = true;
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002987 }
2988}
2989
2990
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002991void Debugger::SetHostDispatchHandler(v8::Debug::HostDispatchHandler handler,
2992 int period) {
ager@chromium.org381abbb2009-02-25 13:23:22 +00002993 host_dispatch_handler_ = handler;
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002994 host_dispatch_micros_ = period * 1000;
ager@chromium.org381abbb2009-02-25 13:23:22 +00002995}
2996
2997
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002998void Debugger::SetDebugMessageDispatchHandler(
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002999 v8::Debug::DebugMessageDispatchHandler handler, bool provide_locker) {
3000 ScopedLock with(dispatch_handler_access_);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003001 debug_message_dispatch_handler_ = handler;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003002
3003 if (provide_locker && message_dispatch_helper_thread_ == NULL) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003004 message_dispatch_helper_thread_ = new MessageDispatchHelperThread(isolate_);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003005 message_dispatch_helper_thread_->Start();
3006 }
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003007}
3008
3009
ager@chromium.org41826e72009-03-30 13:30:57 +00003010// Calls the registered debug message handler. This callback is part of the
ager@chromium.org5ec48922009-05-05 07:25:34 +00003011// public API.
3012void Debugger::InvokeMessageHandler(MessageImpl message) {
ager@chromium.org71daaf62009-04-01 07:22:49 +00003013 ScopedLock with(debugger_access_);
3014
ager@chromium.org381abbb2009-02-25 13:23:22 +00003015 if (message_handler_ != NULL) {
ager@chromium.org5ec48922009-05-05 07:25:34 +00003016 message_handler_(message);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003017 }
ager@chromium.org41826e72009-03-30 13:30:57 +00003018}
3019
3020
3021// Puts a command coming from the public API on the queue. Creates
3022// a copy of the command string managed by the debugger. Up to this
3023// point, the command data was managed by the API client. Called
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003024// by the API client thread.
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003025void Debugger::ProcessCommand(Vector<const uint16_t> command,
3026 v8::Debug::ClientData* client_data) {
3027 // Need to cast away const.
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003028 CommandMessage message = CommandMessage::New(
ager@chromium.org41826e72009-03-30 13:30:57 +00003029 Vector<uint16_t>(const_cast<uint16_t*>(command.start()),
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003030 command.length()),
3031 client_data);
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00003032 isolate_->logger()->DebugTag("Put command on command_queue.");
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003033 command_queue_.Put(message);
ager@chromium.org41826e72009-03-30 13:30:57 +00003034 command_received_->Signal();
sgjesse@chromium.org3afc1582009-04-16 22:31:44 +00003035
3036 // Set the debug command break flag to have the command processed.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003037 if (!isolate_->debug()->InDebugger()) {
3038 isolate_->stack_guard()->DebugCommand();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003039 }
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003040
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003041 MessageDispatchHelperThread* dispatch_thread;
3042 {
3043 ScopedLock with(dispatch_handler_access_);
3044 dispatch_thread = message_dispatch_helper_thread_;
3045 }
3046
3047 if (dispatch_thread == NULL) {
3048 CallMessageDispatchHandler();
3049 } else {
3050 dispatch_thread->Schedule();
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003051 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003052}
3053
3054
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003055bool Debugger::HasCommands() {
ager@chromium.org41826e72009-03-30 13:30:57 +00003056 return !command_queue_.IsEmpty();
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003057}
3058
3059
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00003060void Debugger::EnqueueDebugCommand(v8::Debug::ClientData* client_data) {
3061 CommandMessage message = CommandMessage::New(Vector<uint16_t>(), client_data);
3062 event_command_queue_.Put(message);
3063
3064 // Set the debug command break flag to have the command processed.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003065 if (!isolate_->debug()->InDebugger()) {
3066 isolate_->stack_guard()->DebugCommand();
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00003067 }
3068}
3069
3070
ager@chromium.org71daaf62009-04-01 07:22:49 +00003071bool Debugger::IsDebuggerActive() {
3072 ScopedLock with(debugger_access_);
3073
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00003074 return message_handler_ != NULL ||
3075 !event_listener_.is_null() ||
3076 force_debugger_active_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003077}
3078
3079
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003080Handle<Object> Debugger::Call(Handle<JSFunction> fun,
3081 Handle<Object> data,
3082 bool* pending_exception) {
ager@chromium.org71daaf62009-04-01 07:22:49 +00003083 // When calling functions in the debugger prevent it from beeing unloaded.
3084 Debugger::never_unload_debugger_ = true;
3085
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003086 // Enter the debugger.
3087 EnterDebugger debugger;
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +00003088 if (debugger.FailedToEnter()) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00003089 return isolate_->factory()->undefined_value();
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003090 }
3091
3092 // Create the execution state.
3093 bool caught_exception = false;
3094 Handle<Object> exec_state = MakeExecutionState(&caught_exception);
3095 if (caught_exception) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00003096 return isolate_->factory()->undefined_value();
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003097 }
3098
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00003099 Handle<Object> argv[] = { exec_state, data };
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +00003100 Handle<Object> result = Execution::Call(
3101 fun,
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003102 Handle<Object>(isolate_->debug()->debug_context_->global_proxy()),
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00003103 ARRAY_SIZE(argv),
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +00003104 argv,
3105 pending_exception);
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003106 return result;
3107}
3108
3109
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003110static void StubMessageHandler2(const v8::Debug::Message& message) {
3111 // Simply ignore message.
3112}
3113
3114
3115bool Debugger::StartAgent(const char* name, int port,
3116 bool wait_for_connection) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003117 ASSERT(Isolate::Current() == isolate_);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003118 if (wait_for_connection) {
3119 // Suspend V8 if it is already running or set V8 to suspend whenever
3120 // it starts.
3121 // Provide stub message handler; V8 auto-continues each suspend
3122 // when there is no message handler; we doesn't need it.
3123 // Once become suspended, V8 will stay so indefinitely long, until remote
3124 // debugger connects and issues "continue" command.
3125 Debugger::message_handler_ = StubMessageHandler2;
3126 v8::Debug::DebugBreak();
3127 }
3128
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00003129 if (Socket::SetUp()) {
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00003130 if (agent_ == NULL) {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00003131 agent_ = new DebuggerAgent(name, port);
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00003132 agent_->Start();
3133 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003134 return true;
3135 }
3136
3137 return false;
3138}
3139
3140
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003141void Debugger::StopAgent() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003142 ASSERT(Isolate::Current() == isolate_);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003143 if (agent_ != NULL) {
3144 agent_->Shutdown();
3145 agent_->Join();
3146 delete agent_;
3147 agent_ = NULL;
3148 }
3149}
3150
3151
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003152void Debugger::WaitForAgent() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003153 ASSERT(Isolate::Current() == isolate_);
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003154 if (agent_ != NULL)
3155 agent_->WaitUntilListening();
3156}
3157
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003158
3159void Debugger::CallMessageDispatchHandler() {
3160 v8::Debug::DebugMessageDispatchHandler handler;
3161 {
3162 ScopedLock with(dispatch_handler_access_);
3163 handler = Debugger::debug_message_dispatch_handler_;
3164 }
3165 if (handler != NULL) {
3166 handler();
3167 }
3168}
3169
3170
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003171EnterDebugger::EnterDebugger()
3172 : isolate_(Isolate::Current()),
3173 prev_(isolate_->debug()->debugger_entry()),
3174 it_(isolate_),
3175 has_js_frames_(!it_.done()),
3176 save_(isolate_) {
3177 Debug* debug = isolate_->debug();
3178 ASSERT(prev_ != NULL || !debug->is_interrupt_pending(PREEMPT));
3179 ASSERT(prev_ != NULL || !debug->is_interrupt_pending(DEBUGBREAK));
3180
3181 // Link recursive debugger entry.
3182 debug->set_debugger_entry(this);
3183
3184 // Store the previous break id and frame id.
3185 break_id_ = debug->break_id();
3186 break_frame_id_ = debug->break_frame_id();
3187
3188 // Create the new break info. If there is no JavaScript frames there is no
3189 // break frame id.
3190 if (has_js_frames_) {
3191 debug->NewBreak(it_.frame()->id());
3192 } else {
3193 debug->NewBreak(StackFrame::NO_ID);
3194 }
3195
3196 // Make sure that debugger is loaded and enter the debugger context.
3197 load_failed_ = !debug->Load();
3198 if (!load_failed_) {
3199 // NOTE the member variable save which saves the previous context before
3200 // this change.
3201 isolate_->set_context(*debug->debug_context());
3202 }
3203}
3204
3205
3206EnterDebugger::~EnterDebugger() {
3207 ASSERT(Isolate::Current() == isolate_);
3208 Debug* debug = isolate_->debug();
3209
3210 // Restore to the previous break state.
3211 debug->SetBreak(break_frame_id_, break_id_);
3212
3213 // Check for leaving the debugger.
3214 if (prev_ == NULL) {
3215 // Clear mirror cache when leaving the debugger. Skip this if there is a
3216 // pending exception as clearing the mirror cache calls back into
3217 // JavaScript. This can happen if the v8::Debug::Call is used in which
3218 // case the exception should end up in the calling code.
3219 if (!isolate_->has_pending_exception()) {
3220 // Try to avoid any pending debug break breaking in the clear mirror
3221 // cache JavaScript code.
3222 if (isolate_->stack_guard()->IsDebugBreak()) {
3223 debug->set_interrupts_pending(DEBUGBREAK);
3224 isolate_->stack_guard()->Continue(DEBUGBREAK);
3225 }
3226 debug->ClearMirrorCache();
3227 }
3228
3229 // Request preemption and debug break when leaving the last debugger entry
3230 // if any of these where recorded while debugging.
3231 if (debug->is_interrupt_pending(PREEMPT)) {
3232 // This re-scheduling of preemption is to avoid starvation in some
3233 // debugging scenarios.
3234 debug->clear_interrupt_pending(PREEMPT);
3235 isolate_->stack_guard()->Preempt();
3236 }
3237 if (debug->is_interrupt_pending(DEBUGBREAK)) {
3238 debug->clear_interrupt_pending(DEBUGBREAK);
3239 isolate_->stack_guard()->DebugBreak();
3240 }
3241
3242 // If there are commands in the queue when leaving the debugger request
3243 // that these commands are processed.
3244 if (isolate_->debugger()->HasCommands()) {
3245 isolate_->stack_guard()->DebugCommand();
3246 }
3247
3248 // If leaving the debugger with the debugger no longer active unload it.
3249 if (!isolate_->debugger()->IsDebuggerActive()) {
3250 isolate_->debugger()->UnloadDebugger();
3251 }
3252 }
3253
3254 // Leaving this debugger entry.
3255 debug->set_debugger_entry(prev_);
3256}
3257
3258
ager@chromium.org5ec48922009-05-05 07:25:34 +00003259MessageImpl MessageImpl::NewEvent(DebugEvent event,
3260 bool running,
3261 Handle<JSObject> exec_state,
3262 Handle<JSObject> event_data) {
3263 MessageImpl message(true, event, running,
3264 exec_state, event_data, Handle<String>(), NULL);
3265 return message;
3266}
3267
3268
3269MessageImpl MessageImpl::NewResponse(DebugEvent event,
3270 bool running,
3271 Handle<JSObject> exec_state,
3272 Handle<JSObject> event_data,
3273 Handle<String> response_json,
3274 v8::Debug::ClientData* client_data) {
3275 MessageImpl message(false, event, running,
3276 exec_state, event_data, response_json, client_data);
3277 return message;
3278}
3279
3280
3281MessageImpl::MessageImpl(bool is_event,
3282 DebugEvent event,
3283 bool running,
3284 Handle<JSObject> exec_state,
3285 Handle<JSObject> event_data,
3286 Handle<String> response_json,
3287 v8::Debug::ClientData* client_data)
3288 : is_event_(is_event),
3289 event_(event),
3290 running_(running),
3291 exec_state_(exec_state),
3292 event_data_(event_data),
3293 response_json_(response_json),
3294 client_data_(client_data) {}
3295
3296
3297bool MessageImpl::IsEvent() const {
3298 return is_event_;
3299}
3300
3301
3302bool MessageImpl::IsResponse() const {
3303 return !is_event_;
3304}
3305
3306
3307DebugEvent MessageImpl::GetEvent() const {
3308 return event_;
3309}
3310
3311
3312bool MessageImpl::WillStartRunning() const {
3313 return running_;
3314}
3315
3316
3317v8::Handle<v8::Object> MessageImpl::GetExecutionState() const {
3318 return v8::Utils::ToLocal(exec_state_);
3319}
3320
3321
3322v8::Handle<v8::Object> MessageImpl::GetEventData() const {
3323 return v8::Utils::ToLocal(event_data_);
3324}
3325
3326
3327v8::Handle<v8::String> MessageImpl::GetJSON() const {
3328 v8::HandleScope scope;
3329
3330 if (IsEvent()) {
3331 // Call toJSONProtocol on the debug event object.
3332 Handle<Object> fun = GetProperty(event_data_, "toJSONProtocol");
3333 if (!fun->IsJSFunction()) {
3334 return v8::Handle<v8::String>();
3335 }
3336 bool caught_exception;
3337 Handle<Object> json = Execution::TryCall(Handle<JSFunction>::cast(fun),
3338 event_data_,
3339 0, NULL, &caught_exception);
3340 if (caught_exception || !json->IsString()) {
3341 return v8::Handle<v8::String>();
3342 }
3343 return scope.Close(v8::Utils::ToLocal(Handle<String>::cast(json)));
3344 } else {
3345 return v8::Utils::ToLocal(response_json_);
3346 }
3347}
3348
3349
3350v8::Handle<v8::Context> MessageImpl::GetEventContext() const {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003351 Isolate* isolate = Isolate::Current();
3352 v8::Handle<v8::Context> context = GetDebugEventContext(isolate);
3353 // Isolate::context() may be NULL when "script collected" event occures.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003354 ASSERT(!context.IsEmpty() || event_ == v8::ScriptCollected);
rossberg@chromium.org717967f2011-07-20 13:44:42 +00003355 return context;
ager@chromium.org5ec48922009-05-05 07:25:34 +00003356}
3357
3358
3359v8::Debug::ClientData* MessageImpl::GetClientData() const {
3360 return client_data_;
3361}
3362
3363
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003364EventDetailsImpl::EventDetailsImpl(DebugEvent event,
3365 Handle<JSObject> exec_state,
3366 Handle<JSObject> event_data,
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00003367 Handle<Object> callback_data,
3368 v8::Debug::ClientData* client_data)
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003369 : event_(event),
3370 exec_state_(exec_state),
3371 event_data_(event_data),
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00003372 callback_data_(callback_data),
3373 client_data_(client_data) {}
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003374
3375
3376DebugEvent EventDetailsImpl::GetEvent() const {
3377 return event_;
3378}
3379
3380
3381v8::Handle<v8::Object> EventDetailsImpl::GetExecutionState() const {
3382 return v8::Utils::ToLocal(exec_state_);
3383}
3384
3385
3386v8::Handle<v8::Object> EventDetailsImpl::GetEventData() const {
3387 return v8::Utils::ToLocal(event_data_);
3388}
3389
3390
3391v8::Handle<v8::Context> EventDetailsImpl::GetEventContext() const {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003392 return GetDebugEventContext(Isolate::Current());
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003393}
3394
3395
3396v8::Handle<v8::Value> EventDetailsImpl::GetCallbackData() const {
3397 return v8::Utils::ToLocal(callback_data_);
3398}
3399
3400
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00003401v8::Debug::ClientData* EventDetailsImpl::GetClientData() const {
3402 return client_data_;
3403}
3404
3405
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003406CommandMessage::CommandMessage() : text_(Vector<uint16_t>::empty()),
3407 client_data_(NULL) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003408}
3409
3410
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003411CommandMessage::CommandMessage(const Vector<uint16_t>& text,
3412 v8::Debug::ClientData* data)
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003413 : text_(text),
3414 client_data_(data) {
3415}
3416
3417
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003418CommandMessage::~CommandMessage() {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003419}
3420
3421
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003422void CommandMessage::Dispose() {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003423 text_.Dispose();
3424 delete client_data_;
3425 client_data_ = NULL;
3426}
3427
3428
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003429CommandMessage CommandMessage::New(const Vector<uint16_t>& command,
3430 v8::Debug::ClientData* data) {
3431 return CommandMessage(command.Clone(), data);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003432}
3433
3434
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003435CommandMessageQueue::CommandMessageQueue(int size) : start_(0), end_(0),
3436 size_(size) {
3437 messages_ = NewArray<CommandMessage>(size);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003438}
3439
3440
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003441CommandMessageQueue::~CommandMessageQueue() {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003442 while (!IsEmpty()) {
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003443 CommandMessage m = Get();
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003444 m.Dispose();
3445 }
kasper.lund7276f142008-07-30 08:49:36 +00003446 DeleteArray(messages_);
3447}
3448
3449
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003450CommandMessage CommandMessageQueue::Get() {
kasper.lund7276f142008-07-30 08:49:36 +00003451 ASSERT(!IsEmpty());
3452 int result = start_;
3453 start_ = (start_ + 1) % size_;
3454 return messages_[result];
3455}
3456
3457
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003458void CommandMessageQueue::Put(const CommandMessage& message) {
kasper.lund7276f142008-07-30 08:49:36 +00003459 if ((end_ + 1) % size_ == start_) {
3460 Expand();
3461 }
3462 messages_[end_] = message;
3463 end_ = (end_ + 1) % size_;
3464}
3465
3466
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003467void CommandMessageQueue::Expand() {
3468 CommandMessageQueue new_queue(size_ * 2);
kasper.lund7276f142008-07-30 08:49:36 +00003469 while (!IsEmpty()) {
3470 new_queue.Put(Get());
3471 }
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003472 CommandMessage* array_to_free = messages_;
kasper.lund7276f142008-07-30 08:49:36 +00003473 *this = new_queue;
3474 new_queue.messages_ = array_to_free;
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003475 // Make the new_queue empty so that it doesn't call Dispose on any messages.
3476 new_queue.start_ = new_queue.end_;
kasper.lund7276f142008-07-30 08:49:36 +00003477 // Automatic destructor called on new_queue, freeing array_to_free.
3478}
3479
3480
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00003481LockingCommandMessageQueue::LockingCommandMessageQueue(Logger* logger, int size)
3482 : logger_(logger), queue_(size) {
kasper.lund7276f142008-07-30 08:49:36 +00003483 lock_ = OS::CreateMutex();
3484}
3485
3486
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003487LockingCommandMessageQueue::~LockingCommandMessageQueue() {
kasper.lund7276f142008-07-30 08:49:36 +00003488 delete lock_;
3489}
3490
3491
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003492bool LockingCommandMessageQueue::IsEmpty() const {
kasper.lund7276f142008-07-30 08:49:36 +00003493 ScopedLock sl(lock_);
3494 return queue_.IsEmpty();
3495}
3496
3497
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003498CommandMessage LockingCommandMessageQueue::Get() {
kasper.lund7276f142008-07-30 08:49:36 +00003499 ScopedLock sl(lock_);
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003500 CommandMessage result = queue_.Get();
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00003501 logger_->DebugEvent("Get", result.text());
kasper.lund7276f142008-07-30 08:49:36 +00003502 return result;
3503}
3504
3505
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003506void LockingCommandMessageQueue::Put(const CommandMessage& message) {
kasper.lund7276f142008-07-30 08:49:36 +00003507 ScopedLock sl(lock_);
3508 queue_.Put(message);
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00003509 logger_->DebugEvent("Put", message.text());
kasper.lund7276f142008-07-30 08:49:36 +00003510}
3511
3512
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003513void LockingCommandMessageQueue::Clear() {
kasper.lund7276f142008-07-30 08:49:36 +00003514 ScopedLock sl(lock_);
3515 queue_.Clear();
3516}
3517
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003518
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003519MessageDispatchHelperThread::MessageDispatchHelperThread(Isolate* isolate)
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00003520 : Thread("v8:MsgDispHelpr"),
lrn@chromium.org5d00b602011-01-05 09:51:43 +00003521 sem_(OS::CreateSemaphore(0)), mutex_(OS::CreateMutex()),
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003522 already_signalled_(false) {
3523}
3524
3525
3526MessageDispatchHelperThread::~MessageDispatchHelperThread() {
3527 delete mutex_;
3528 delete sem_;
3529}
3530
3531
3532void MessageDispatchHelperThread::Schedule() {
3533 {
3534 ScopedLock lock(mutex_);
3535 if (already_signalled_) {
3536 return;
3537 }
3538 already_signalled_ = true;
3539 }
3540 sem_->Signal();
3541}
3542
3543
3544void MessageDispatchHelperThread::Run() {
3545 while (true) {
3546 sem_->Wait();
3547 {
3548 ScopedLock lock(mutex_);
3549 already_signalled_ = false;
3550 }
3551 {
3552 Locker locker;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003553 Isolate::Current()->debugger()->CallMessageDispatchHandler();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003554 }
3555 }
3556}
3557
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003558#endif // ENABLE_DEBUGGER_SUPPORT
kasper.lund7276f142008-07-30 08:49:36 +00003559
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003560} } // namespace v8::internal