blob: c70b834f7abdebb75f34a8fcd695607732f89e51 [file] [log] [blame]
fschneider@chromium.org7d10be52012-04-10 12:30:14 +00001// Copyright 2012 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"
yangguo@chromium.orga7d3df92012-02-27 11:46:55 +000040#include "full-codegen.h"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000041#include "global-handles.h"
ager@chromium.org65dad4b2009-04-23 08:48:43 +000042#include "ic.h"
43#include "ic-inl.h"
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +000044#include "isolate-inl.h"
lrn@chromium.org34e60782011-09-15 07:25:40 +000045#include "list.h"
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000046#include "messages.h"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000047#include "natives.h"
48#include "stub-cache.h"
kasper.lund7276f142008-07-30 08:49:36 +000049#include "log.h"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000050
ager@chromium.org5ec48922009-05-05 07:25:34 +000051#include "../include/v8-debug.h"
52
kasperl@chromium.org71affb52009-05-26 05:44:31 +000053namespace v8 {
54namespace internal {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000055
ager@chromium.org65dad4b2009-04-23 08:48:43 +000056#ifdef ENABLE_DEBUGGER_SUPPORT
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000057
58
59Debug::Debug(Isolate* isolate)
60 : has_break_points_(false),
61 script_cache_(NULL),
62 debug_info_list_(NULL),
63 disable_break_(false),
64 break_on_exception_(false),
65 break_on_uncaught_exception_(false),
66 debug_break_return_(NULL),
67 debug_break_slot_(NULL),
68 isolate_(isolate) {
69 memset(registers_, 0, sizeof(JSCallerSavedBuffer));
70}
71
72
73Debug::~Debug() {
74}
75
76
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000077static void PrintLn(v8::Local<v8::Value> value) {
78 v8::Local<v8::String> s = value->ToString();
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +000079 ScopedVector<char> data(s->Length() + 1);
80 if (data.start() == NULL) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000081 V8::FatalProcessOutOfMemory("PrintLn");
82 return;
83 }
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +000084 s->WriteAscii(data.start());
85 PrintF("%s\n", data.start());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000086}
87
88
danno@chromium.org40cb8782011-05-25 07:58:50 +000089static Handle<Code> ComputeCallDebugPrepareStepIn(int argc, Code::Kind kind) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000090 Isolate* isolate = Isolate::Current();
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +000091 return isolate->stub_cache()->ComputeCallDebugPrepareStepIn(argc, kind);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000092}
93
94
95static v8::Handle<v8::Context> GetDebugEventContext(Isolate* isolate) {
96 Handle<Context> context = isolate->debug()->debugger_entry()->GetContext();
97 // Isolate::context() may have been NULL when "script collected" event
98 // occured.
99 if (context.is_null()) return v8::Local<v8::Context>();
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000100 Handle<Context> native_context(context->native_context());
101 return v8::Utils::ToLocal(native_context);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000102}
103
104
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000105BreakLocationIterator::BreakLocationIterator(Handle<DebugInfo> debug_info,
106 BreakLocatorType type) {
107 debug_info_ = debug_info;
108 type_ = type;
109 reloc_iterator_ = NULL;
110 reloc_iterator_original_ = NULL;
111 Reset(); // Initialize the rest of the member variables.
112}
113
114
115BreakLocationIterator::~BreakLocationIterator() {
116 ASSERT(reloc_iterator_ != NULL);
117 ASSERT(reloc_iterator_original_ != NULL);
118 delete reloc_iterator_;
119 delete reloc_iterator_original_;
120}
121
122
123void BreakLocationIterator::Next() {
124 AssertNoAllocation nogc;
125 ASSERT(!RinfoDone());
126
127 // Iterate through reloc info for code and original code stopping at each
128 // breakable code target.
129 bool first = break_point_ == -1;
130 while (!RinfoDone()) {
131 if (!first) RinfoNext();
132 first = false;
133 if (RinfoDone()) return;
134
ager@chromium.org236ad962008-09-25 09:45:57 +0000135 // Whenever a statement position or (plain) position is passed update the
136 // current value of these.
137 if (RelocInfo::IsPosition(rmode())) {
138 if (RelocInfo::IsStatementPosition(rmode())) {
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000139 statement_position_ = static_cast<int>(
140 rinfo()->data() - debug_info_->shared()->start_position());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000141 }
ager@chromium.org236ad962008-09-25 09:45:57 +0000142 // Always update the position as we don't want that to be before the
143 // statement position.
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000144 position_ = static_cast<int>(
145 rinfo()->data() - debug_info_->shared()->start_position());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000146 ASSERT(position_ >= 0);
147 ASSERT(statement_position_ >= 0);
148 }
149
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000150 if (IsDebugBreakSlot()) {
151 // There is always a possible break point at a debug break slot.
152 break_point_++;
153 return;
154 } else if (RelocInfo::IsCodeTarget(rmode())) {
155 // Check for breakable code target. Look in the original code as setting
156 // break points can cause the code targets in the running (debugged) code
157 // to be of a different kind than in the original code.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000158 Address target = original_rinfo()->target_address();
ager@chromium.org8bb60582008-12-11 12:02:20 +0000159 Code* code = Code::GetCodeFromTargetAddress(target);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000160 if ((code->is_inline_cache_stub() &&
danno@chromium.org40cb8782011-05-25 07:58:50 +0000161 !code->is_binary_op_stub() &&
162 !code->is_unary_op_stub() &&
ricow@chromium.org9fa09672011-07-25 11:05:35 +0000163 !code->is_compare_ic_stub() &&
164 !code->is_to_boolean_ic_stub()) ||
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000165 RelocInfo::IsConstructCall(rmode())) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000166 break_point_++;
167 return;
168 }
169 if (code->kind() == Code::STUB) {
ager@chromium.orga1645e22009-09-09 19:27:10 +0000170 if (IsDebuggerStatement()) {
171 break_point_++;
172 return;
173 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000174 if (type_ == ALL_BREAK_LOCATIONS) {
175 if (Debug::IsBreakStub(code)) {
176 break_point_++;
177 return;
178 }
179 } else {
180 ASSERT(type_ == SOURCE_BREAK_LOCATIONS);
181 if (Debug::IsSourceBreakStub(code)) {
182 break_point_++;
183 return;
184 }
185 }
186 }
187 }
188
189 // Check for break at return.
ager@chromium.org236ad962008-09-25 09:45:57 +0000190 if (RelocInfo::IsJSReturn(rmode())) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000191 // Set the positions to the end of the function.
192 if (debug_info_->shared()->HasSourceCode()) {
193 position_ = debug_info_->shared()->end_position() -
whesse@chromium.orge90029b2010-08-02 11:52:17 +0000194 debug_info_->shared()->start_position() - 1;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000195 } else {
196 position_ = 0;
197 }
198 statement_position_ = position_;
199 break_point_++;
200 return;
201 }
202 }
203}
204
205
206void BreakLocationIterator::Next(int count) {
207 while (count > 0) {
208 Next();
209 count--;
210 }
211}
212
213
214// Find the break point closest to the supplied address.
215void BreakLocationIterator::FindBreakLocationFromAddress(Address pc) {
216 // Run through all break points to locate the one closest to the address.
217 int closest_break_point = 0;
218 int distance = kMaxInt;
219 while (!Done()) {
220 // Check if this break point is closer that what was previously found.
221 if (this->pc() < pc && pc - this->pc() < distance) {
222 closest_break_point = break_point();
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000223 distance = static_cast<int>(pc - this->pc());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000224 // Check whether we can't get any closer.
225 if (distance == 0) break;
226 }
227 Next();
228 }
229
230 // Move to the break point found.
231 Reset();
232 Next(closest_break_point);
233}
234
235
236// Find the break point closest to the supplied source position.
237void BreakLocationIterator::FindBreakLocationFromPosition(int position) {
238 // Run through all break points to locate the one closest to the source
239 // position.
240 int closest_break_point = 0;
241 int distance = kMaxInt;
242 while (!Done()) {
243 // Check if this break point is closer that what was previously found.
244 if (position <= statement_position() &&
245 statement_position() - position < distance) {
246 closest_break_point = break_point();
247 distance = statement_position() - position;
248 // Check whether we can't get any closer.
249 if (distance == 0) break;
250 }
251 Next();
252 }
253
254 // Move to the break point found.
255 Reset();
256 Next(closest_break_point);
257}
258
259
260void BreakLocationIterator::Reset() {
261 // Create relocation iterators for the two code objects.
262 if (reloc_iterator_ != NULL) delete reloc_iterator_;
263 if (reloc_iterator_original_ != NULL) delete reloc_iterator_original_;
264 reloc_iterator_ = new RelocIterator(debug_info_->code());
265 reloc_iterator_original_ = new RelocIterator(debug_info_->original_code());
266
267 // Position at the first break point.
268 break_point_ = -1;
269 position_ = 1;
270 statement_position_ = 1;
271 Next();
272}
273
274
275bool BreakLocationIterator::Done() const {
276 return RinfoDone();
277}
278
279
280void BreakLocationIterator::SetBreakPoint(Handle<Object> break_point_object) {
281 // If there is not already a real break point here patch code with debug
282 // break.
283 if (!HasBreakPoint()) {
284 SetDebugBreak();
285 }
ager@chromium.orga1645e22009-09-09 19:27:10 +0000286 ASSERT(IsDebugBreak() || IsDebuggerStatement());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000287 // Set the break point information.
288 DebugInfo::SetBreakPoint(debug_info_, code_position(),
289 position(), statement_position(),
290 break_point_object);
291}
292
293
294void BreakLocationIterator::ClearBreakPoint(Handle<Object> break_point_object) {
295 // Clear the break point information.
296 DebugInfo::ClearBreakPoint(debug_info_, code_position(), break_point_object);
297 // If there are no more break points here remove the debug break.
298 if (!HasBreakPoint()) {
299 ClearDebugBreak();
300 ASSERT(!IsDebugBreak());
301 }
302}
303
304
305void BreakLocationIterator::SetOneShot() {
ager@chromium.orga1645e22009-09-09 19:27:10 +0000306 // Debugger statement always calls debugger. No need to modify it.
307 if (IsDebuggerStatement()) {
308 return;
309 }
310
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000311 // If there is a real break point here no more to do.
312 if (HasBreakPoint()) {
313 ASSERT(IsDebugBreak());
314 return;
315 }
316
317 // Patch code with debug break.
318 SetDebugBreak();
319}
320
321
322void BreakLocationIterator::ClearOneShot() {
ager@chromium.orga1645e22009-09-09 19:27:10 +0000323 // Debugger statement always calls debugger. No need to modify it.
324 if (IsDebuggerStatement()) {
325 return;
326 }
327
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000328 // If there is a real break point here no more to do.
329 if (HasBreakPoint()) {
330 ASSERT(IsDebugBreak());
331 return;
332 }
333
334 // Patch code removing debug break.
335 ClearDebugBreak();
336 ASSERT(!IsDebugBreak());
337}
338
339
340void BreakLocationIterator::SetDebugBreak() {
ager@chromium.orga1645e22009-09-09 19:27:10 +0000341 // Debugger statement always calls debugger. No need to modify it.
342 if (IsDebuggerStatement()) {
343 return;
344 }
345
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000346 // If there is already a break point here just return. This might happen if
v8.team.kasperl727e9952008-09-02 14:56:44 +0000347 // the same code is flooded with break points twice. Flooding the same
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000348 // function twice might happen when stepping in a function with an exception
349 // handler as the handler and the function is the same.
350 if (IsDebugBreak()) {
351 return;
352 }
353
ager@chromium.org236ad962008-09-25 09:45:57 +0000354 if (RelocInfo::IsJSReturn(rmode())) {
iposva@chromium.org245aa852009-02-10 00:49:54 +0000355 // Patch the frame exit code with a break point.
356 SetDebugBreakAtReturn();
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000357 } else if (IsDebugBreakSlot()) {
358 // Patch the code in the break slot.
359 SetDebugBreakAtSlot();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000360 } else {
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000361 // Patch the IC call.
362 SetDebugBreakAtIC();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000363 }
364 ASSERT(IsDebugBreak());
365}
366
367
368void BreakLocationIterator::ClearDebugBreak() {
ager@chromium.orga1645e22009-09-09 19:27:10 +0000369 // Debugger statement always calls debugger. No need to modify it.
370 if (IsDebuggerStatement()) {
371 return;
372 }
373
ager@chromium.org236ad962008-09-25 09:45:57 +0000374 if (RelocInfo::IsJSReturn(rmode())) {
iposva@chromium.org245aa852009-02-10 00:49:54 +0000375 // Restore the frame exit code.
376 ClearDebugBreakAtReturn();
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000377 } else if (IsDebugBreakSlot()) {
378 // Restore the code in the break slot.
379 ClearDebugBreakAtSlot();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000380 } else {
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000381 // Patch the IC call.
382 ClearDebugBreakAtIC();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000383 }
384 ASSERT(!IsDebugBreak());
385}
386
387
388void BreakLocationIterator::PrepareStepIn() {
ager@chromium.org381abbb2009-02-25 13:23:22 +0000389 HandleScope scope;
390
ager@chromium.orga1645e22009-09-09 19:27:10 +0000391 // Step in can only be prepared if currently positioned on an IC call,
392 // construct call or CallFunction stub call.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000393 Address target = rinfo()->target_address();
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000394 Handle<Code> target_code(Code::GetCodeFromTargetAddress(target));
395 if (target_code->is_call_stub() || target_code->is_keyed_call_stub()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000396 // Step in through IC call is handled by the runtime system. Therefore make
397 // sure that the any current IC is cleared and the runtime system is
398 // called. If the executing code has a debug break at the location change
399 // the call in the original code as it is the code there that will be
400 // executed in place of the debug break call.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000401 Handle<Code> stub = ComputeCallDebugPrepareStepIn(
402 target_code->arguments_count(), target_code->kind());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000403 if (IsDebugBreak()) {
404 original_rinfo()->set_target_address(stub->entry());
405 } else {
406 rinfo()->set_target_address(stub->entry());
407 }
408 } else {
ager@chromium.orga1645e22009-09-09 19:27:10 +0000409#ifdef DEBUG
410 // All the following stuff is needed only for assertion checks so the code
411 // is wrapped in ifdef.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000412 Handle<Code> maybe_call_function_stub = target_code;
ager@chromium.orga1645e22009-09-09 19:27:10 +0000413 if (IsDebugBreak()) {
414 Address original_target = original_rinfo()->target_address();
415 maybe_call_function_stub =
416 Handle<Code>(Code::GetCodeFromTargetAddress(original_target));
417 }
418 bool is_call_function_stub =
419 (maybe_call_function_stub->kind() == Code::STUB &&
420 maybe_call_function_stub->major_key() == CodeStub::CallFunction);
421
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000422 // Step in through construct call requires no changes to the running code.
423 // Step in through getters/setters should already be prepared as well
424 // because caller of this function (Debug::PrepareStep) is expected to
425 // flood the top frame's function with one shot breakpoints.
ager@chromium.orga1645e22009-09-09 19:27:10 +0000426 // Step in through CallFunction stub should also be prepared by caller of
427 // this function (Debug::PrepareStep) which should flood target function
428 // with breakpoints.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000429 ASSERT(RelocInfo::IsConstructCall(rmode()) ||
430 target_code->is_inline_cache_stub() ||
431 is_call_function_stub);
ager@chromium.orga1645e22009-09-09 19:27:10 +0000432#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000433 }
434}
435
436
437// Check whether the break point is at a position which will exit the function.
438bool BreakLocationIterator::IsExit() const {
ager@chromium.org236ad962008-09-25 09:45:57 +0000439 return (RelocInfo::IsJSReturn(rmode()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000440}
441
442
443bool BreakLocationIterator::HasBreakPoint() {
444 return debug_info_->HasBreakPoint(code_position());
445}
446
447
448// Check whether there is a debug break at the current position.
449bool BreakLocationIterator::IsDebugBreak() {
ager@chromium.org236ad962008-09-25 09:45:57 +0000450 if (RelocInfo::IsJSReturn(rmode())) {
iposva@chromium.org245aa852009-02-10 00:49:54 +0000451 return IsDebugBreakAtReturn();
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000452 } else if (IsDebugBreakSlot()) {
453 return IsDebugBreakAtSlot();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000454 } else {
455 return Debug::IsDebugBreak(rinfo()->target_address());
456 }
457}
458
459
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000460void BreakLocationIterator::SetDebugBreakAtIC() {
461 // Patch the original code with the current address as the current address
462 // might have changed by the inline caching since the code was copied.
463 original_rinfo()->set_target_address(rinfo()->target_address());
464
465 RelocInfo::Mode mode = rmode();
466 if (RelocInfo::IsCodeTarget(mode)) {
467 Address target = rinfo()->target_address();
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000468 Handle<Code> target_code(Code::GetCodeFromTargetAddress(target));
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000469
470 // Patch the code to invoke the builtin debug break function matching the
471 // calling convention used by the call site.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000472 Handle<Code> dbgbrk_code(Debug::FindDebugBreak(target_code, mode));
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000473 rinfo()->set_target_address(dbgbrk_code->entry());
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000474 }
475}
476
477
478void BreakLocationIterator::ClearDebugBreakAtIC() {
479 // Patch the code to the original invoke.
480 rinfo()->set_target_address(original_rinfo()->target_address());
481}
482
483
ager@chromium.orga1645e22009-09-09 19:27:10 +0000484bool BreakLocationIterator::IsDebuggerStatement() {
ager@chromium.org5c838252010-02-19 08:53:10 +0000485 return RelocInfo::DEBUG_BREAK == rmode();
ager@chromium.orga1645e22009-09-09 19:27:10 +0000486}
487
488
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000489bool BreakLocationIterator::IsDebugBreakSlot() {
490 return RelocInfo::DEBUG_BREAK_SLOT == rmode();
491}
492
493
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000494Object* BreakLocationIterator::BreakPointObjects() {
495 return debug_info_->GetBreakPointObjects(code_position());
496}
497
498
ager@chromium.org381abbb2009-02-25 13:23:22 +0000499// Clear out all the debug break code. This is ONLY supposed to be used when
500// shutting down the debugger as it will leave the break point information in
501// DebugInfo even though the code is patched back to the non break point state.
502void BreakLocationIterator::ClearAllDebugBreak() {
503 while (!Done()) {
504 ClearDebugBreak();
505 Next();
506 }
507}
508
509
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000510bool BreakLocationIterator::RinfoDone() const {
511 ASSERT(reloc_iterator_->done() == reloc_iterator_original_->done());
512 return reloc_iterator_->done();
513}
514
515
516void BreakLocationIterator::RinfoNext() {
517 reloc_iterator_->next();
518 reloc_iterator_original_->next();
519#ifdef DEBUG
520 ASSERT(reloc_iterator_->done() == reloc_iterator_original_->done());
521 if (!reloc_iterator_->done()) {
522 ASSERT(rmode() == original_rmode());
523 }
524#endif
525}
526
527
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000528// Threading support.
529void Debug::ThreadInit() {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000530 thread_local_.break_count_ = 0;
531 thread_local_.break_id_ = 0;
532 thread_local_.break_frame_id_ = StackFrame::NO_ID;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000533 thread_local_.last_step_action_ = StepNone;
ager@chromium.org236ad962008-09-25 09:45:57 +0000534 thread_local_.last_statement_position_ = RelocInfo::kNoPosition;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000535 thread_local_.step_count_ = 0;
536 thread_local_.last_fp_ = 0;
lrn@chromium.org34e60782011-09-15 07:25:40 +0000537 thread_local_.queued_step_count_ = 0;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000538 thread_local_.step_into_fp_ = 0;
ager@chromium.orga1645e22009-09-09 19:27:10 +0000539 thread_local_.step_out_fp_ = 0;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000540 thread_local_.after_break_target_ = 0;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000541 // TODO(isolates): frames_are_dropped_?
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000542 thread_local_.debugger_entry_ = NULL;
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000543 thread_local_.pending_interrupts_ = 0;
ricow@chromium.org0b9f8502010-08-18 07:45:01 +0000544 thread_local_.restarter_frame_function_pointer_ = NULL;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000545}
546
547
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000548char* Debug::ArchiveDebug(char* storage) {
549 char* to = storage;
550 memcpy(to, reinterpret_cast<char*>(&thread_local_), sizeof(ThreadLocal));
551 to += sizeof(ThreadLocal);
552 memcpy(to, reinterpret_cast<char*>(&registers_), sizeof(registers_));
553 ThreadInit();
554 ASSERT(to <= storage + ArchiveSpacePerThread());
555 return storage + ArchiveSpacePerThread();
556}
557
558
559char* Debug::RestoreDebug(char* storage) {
560 char* from = storage;
561 memcpy(reinterpret_cast<char*>(&thread_local_), from, sizeof(ThreadLocal));
562 from += sizeof(ThreadLocal);
563 memcpy(reinterpret_cast<char*>(&registers_), from, sizeof(registers_));
564 ASSERT(from <= storage + ArchiveSpacePerThread());
565 return storage + ArchiveSpacePerThread();
566}
567
568
569int Debug::ArchiveSpacePerThread() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000570 return sizeof(ThreadLocal) + sizeof(JSCallerSavedBuffer);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000571}
572
573
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000574// Frame structure (conforms InternalFrame structure):
575// -- code
576// -- SMI maker
577// -- function (slot is called "context")
578// -- frame base
579Object** Debug::SetUpFrameDropperFrame(StackFrame* bottom_js_frame,
580 Handle<Code> code) {
581 ASSERT(bottom_js_frame->is_java_script());
582
583 Address fp = bottom_js_frame->fp();
584
585 // Move function pointer into "context" slot.
586 Memory::Object_at(fp + StandardFrameConstants::kContextOffset) =
587 Memory::Object_at(fp + JavaScriptFrameConstants::kFunctionOffset);
588
589 Memory::Object_at(fp + InternalFrameConstants::kCodeOffset) = *code;
590 Memory::Object_at(fp + StandardFrameConstants::kMarkerOffset) =
591 Smi::FromInt(StackFrame::INTERNAL);
592
593 return reinterpret_cast<Object**>(&Memory::Object_at(
594 fp + StandardFrameConstants::kContextOffset));
595}
596
597const int Debug::kFrameDropperFrameSize = 4;
598
599
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000600void ScriptCache::Add(Handle<Script> script) {
lrn@chromium.org7516f052011-03-30 08:52:27 +0000601 GlobalHandles* global_handles = Isolate::Current()->global_handles();
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000602 // Create an entry in the hash map for the script.
603 int id = Smi::cast(script->id())->value();
604 HashMap::Entry* entry =
605 HashMap::Lookup(reinterpret_cast<void*>(id), Hash(id), true);
606 if (entry->value != NULL) {
607 ASSERT(*script == *reinterpret_cast<Script**>(entry->value));
608 return;
609 }
610
611 // Globalize the script object, make it weak and use the location of the
612 // global handle as the value in the hash map.
613 Handle<Script> script_ =
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000614 Handle<Script>::cast(
lrn@chromium.org7516f052011-03-30 08:52:27 +0000615 (global_handles->Create(*script)));
616 global_handles->MakeWeak(
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000617 reinterpret_cast<Object**>(script_.location()),
618 this,
619 ScriptCache::HandleWeakScript);
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000620 entry->value = script_.location();
621}
622
623
624Handle<FixedArray> ScriptCache::GetScripts() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000625 Handle<FixedArray> instances = FACTORY->NewFixedArray(occupancy());
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000626 int count = 0;
627 for (HashMap::Entry* entry = Start(); entry != NULL; entry = Next(entry)) {
628 ASSERT(entry->value != NULL);
629 if (entry->value != NULL) {
630 instances->set(count, *reinterpret_cast<Script**>(entry->value));
631 count++;
632 }
633 }
634 return instances;
635}
636
637
638void ScriptCache::ProcessCollectedScripts() {
lrn@chromium.org7516f052011-03-30 08:52:27 +0000639 Debugger* debugger = Isolate::Current()->debugger();
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000640 for (int i = 0; i < collected_scripts_.length(); i++) {
lrn@chromium.org7516f052011-03-30 08:52:27 +0000641 debugger->OnScriptCollected(collected_scripts_[i]);
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000642 }
643 collected_scripts_.Clear();
644}
645
646
647void ScriptCache::Clear() {
lrn@chromium.org7516f052011-03-30 08:52:27 +0000648 GlobalHandles* global_handles = Isolate::Current()->global_handles();
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000649 // Iterate the script cache to get rid of all the weak handles.
650 for (HashMap::Entry* entry = Start(); entry != NULL; entry = Next(entry)) {
651 ASSERT(entry != NULL);
652 Object** location = reinterpret_cast<Object**>(entry->value);
653 ASSERT((*location)->IsScript());
lrn@chromium.org7516f052011-03-30 08:52:27 +0000654 global_handles->ClearWeakness(location);
655 global_handles->Destroy(location);
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000656 }
657 // Clear the content of the hash map.
658 HashMap::Clear();
659}
660
661
662void ScriptCache::HandleWeakScript(v8::Persistent<v8::Value> obj, void* data) {
663 ScriptCache* script_cache = reinterpret_cast<ScriptCache*>(data);
664 // Find the location of the global handle.
665 Script** location =
666 reinterpret_cast<Script**>(Utils::OpenHandle(*obj).location());
667 ASSERT((*location)->IsScript());
668
669 // Remove the entry from the cache.
670 int id = Smi::cast((*location)->id())->value();
671 script_cache->Remove(reinterpret_cast<void*>(id), Hash(id));
672 script_cache->collected_scripts_.Add(id);
673
674 // Clear the weak handle.
675 obj.Dispose();
676 obj.Clear();
677}
678
679
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000680void Debug::SetUp(bool create_heap_objects) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000681 ThreadInit();
682 if (create_heap_objects) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000683 // Get code to handle debug break on return.
684 debug_break_return_ =
lrn@chromium.org7516f052011-03-30 08:52:27 +0000685 isolate_->builtins()->builtin(Builtins::kReturn_DebugBreak);
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000686 ASSERT(debug_break_return_->IsCode());
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000687 // Get code to handle debug break in debug break slots.
688 debug_break_slot_ =
lrn@chromium.org7516f052011-03-30 08:52:27 +0000689 isolate_->builtins()->builtin(Builtins::kSlot_DebugBreak);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000690 ASSERT(debug_break_slot_->IsCode());
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000691 }
692}
693
694
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000695void Debug::HandleWeakDebugInfo(v8::Persistent<v8::Value> obj, void* data) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000696 Debug* debug = Isolate::Current()->debug();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000697 DebugInfoListNode* node = reinterpret_cast<DebugInfoListNode*>(data);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000698 // We need to clear all breakpoints associated with the function to restore
699 // original code and avoid patching the code twice later because
700 // the function will live in the heap until next gc, and can be found by
701 // Runtime::FindSharedFunctionInfoInScript.
702 BreakLocationIterator it(node->debug_info(), ALL_BREAK_LOCATIONS);
703 it.ClearAllDebugBreak();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000704 debug->RemoveDebugInfo(node->debug_info());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000705#ifdef DEBUG
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000706 node = debug->debug_info_list_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000707 while (node != NULL) {
708 ASSERT(node != reinterpret_cast<DebugInfoListNode*>(data));
709 node = node->next();
710 }
711#endif
712}
713
714
715DebugInfoListNode::DebugInfoListNode(DebugInfo* debug_info): next_(NULL) {
lrn@chromium.org7516f052011-03-30 08:52:27 +0000716 GlobalHandles* global_handles = Isolate::Current()->global_handles();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000717 // Globalize the request debug info object and make it weak.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000718 debug_info_ = Handle<DebugInfo>::cast(
lrn@chromium.org7516f052011-03-30 08:52:27 +0000719 (global_handles->Create(debug_info)));
720 global_handles->MakeWeak(
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000721 reinterpret_cast<Object**>(debug_info_.location()),
722 this,
723 Debug::HandleWeakDebugInfo);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000724}
725
726
727DebugInfoListNode::~DebugInfoListNode() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000728 Isolate::Current()->global_handles()->Destroy(
729 reinterpret_cast<Object**>(debug_info_.location()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000730}
731
732
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000733bool Debug::CompileDebuggerScript(int index) {
lrn@chromium.org7516f052011-03-30 08:52:27 +0000734 Isolate* isolate = Isolate::Current();
735 Factory* factory = isolate->factory();
736 HandleScope scope(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000737
kasper.lund44510672008-07-25 07:37:58 +0000738 // Bail out if the index is invalid.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000739 if (index == -1) {
740 return false;
741 }
kasper.lund44510672008-07-25 07:37:58 +0000742
743 // Find source and name for the requested script.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000744 Handle<String> source_code =
lrn@chromium.org7516f052011-03-30 08:52:27 +0000745 isolate->bootstrapper()->NativesSourceLookup(index);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000746 Vector<const char> name = Natives::GetScriptName(index);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000747 Handle<String> script_name = factory->NewStringFromAscii(name);
yangguo@chromium.org355cfd12012-08-29 15:32:24 +0000748 Handle<Context> context = isolate->native_context();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000749
750 // Compile the script.
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000751 Handle<SharedFunctionInfo> function_info;
752 function_info = Compiler::Compile(source_code,
753 script_name,
yangguo@chromium.org355cfd12012-08-29 15:32:24 +0000754 0, 0,
755 context,
756 NULL, NULL,
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000757 Handle<String>::null(),
758 NATIVES_CODE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000759
760 // Silently ignore stack overflows during compilation.
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000761 if (function_info.is_null()) {
lrn@chromium.org7516f052011-03-30 08:52:27 +0000762 ASSERT(isolate->has_pending_exception());
763 isolate->clear_pending_exception();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000764 return false;
765 }
766
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000767 // Execute the shared function in the debugger context.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000768 bool caught_exception;
kasper.lund44510672008-07-25 07:37:58 +0000769 Handle<JSFunction> function =
lrn@chromium.org7516f052011-03-30 08:52:27 +0000770 factory->NewFunctionFromSharedFunctionInfo(function_info, context);
rossberg@chromium.org717967f2011-07-20 13:44:42 +0000771
fschneider@chromium.org7d10be52012-04-10 12:30:14 +0000772 Handle<Object> exception =
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000773 Execution::TryCall(function, Handle<Object>(context->global_object()),
fschneider@chromium.org7d10be52012-04-10 12:30:14 +0000774 0, NULL, &caught_exception);
kasper.lund44510672008-07-25 07:37:58 +0000775
776 // Check for caught exceptions.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000777 if (caught_exception) {
fschneider@chromium.org7d10be52012-04-10 12:30:14 +0000778 ASSERT(!isolate->has_pending_exception());
779 MessageLocation computed_location;
780 isolate->ComputeLocation(&computed_location);
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000781 Handle<Object> message = MessageHandler::MakeMessageObject(
fschneider@chromium.org7d10be52012-04-10 12:30:14 +0000782 "error_loading_debugger", &computed_location,
783 Vector<Handle<Object> >::empty(), Handle<String>(), Handle<JSArray>());
784 ASSERT(!isolate->has_pending_exception());
785 isolate->set_pending_exception(*exception);
karlklose@chromium.org44bc7082011-04-11 12:33:05 +0000786 MessageHandler::ReportMessage(Isolate::Current(), NULL, message);
fschneider@chromium.org7d10be52012-04-10 12:30:14 +0000787 isolate->clear_pending_exception();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000788 return false;
789 }
790
kasper.lund44510672008-07-25 07:37:58 +0000791 // Mark this script as native and return successfully.
792 Handle<Script> script(Script::cast(function->shared()->script()));
ager@chromium.orge2902be2009-06-08 12:21:35 +0000793 script->set_type(Smi::FromInt(Script::TYPE_NATIVE));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000794 return true;
795}
796
797
798bool Debug::Load() {
799 // Return if debugger is already loaded.
kasper.lund212ac232008-07-16 07:07:30 +0000800 if (IsLoaded()) return true;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000801
lrn@chromium.org7516f052011-03-30 08:52:27 +0000802 Debugger* debugger = isolate_->debugger();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000803
kasper.lund44510672008-07-25 07:37:58 +0000804 // Bail out if we're already in the process of compiling the native
805 // JavaScript source code for the debugger.
lrn@chromium.org7516f052011-03-30 08:52:27 +0000806 if (debugger->compiling_natives() ||
807 debugger->is_loading_debugger())
mads.s.agercbaa0602008-08-14 13:41:48 +0000808 return false;
lrn@chromium.org7516f052011-03-30 08:52:27 +0000809 debugger->set_loading_debugger(true);
kasper.lund44510672008-07-25 07:37:58 +0000810
811 // Disable breakpoints and interrupts while compiling and running the
812 // debugger scripts including the context creation code.
813 DisableBreak disable(true);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000814 PostponeInterruptsScope postpone(isolate_);
kasper.lund44510672008-07-25 07:37:58 +0000815
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000816 // Create the debugger context.
lrn@chromium.org7516f052011-03-30 08:52:27 +0000817 HandleScope scope(isolate_);
kasper.lund44510672008-07-25 07:37:58 +0000818 Handle<Context> context =
lrn@chromium.org7516f052011-03-30 08:52:27 +0000819 isolate_->bootstrapper()->CreateEnvironment(
danno@chromium.org160a7b02011-04-18 15:51:38 +0000820 isolate_,
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000821 Handle<Object>::null(),
822 v8::Handle<ObjectTemplate>(),
823 NULL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000824
fschneider@chromium.org7d10be52012-04-10 12:30:14 +0000825 // Fail if no context could be created.
826 if (context.is_null()) return false;
827
kasper.lund44510672008-07-25 07:37:58 +0000828 // Use the debugger context.
lrn@chromium.org7516f052011-03-30 08:52:27 +0000829 SaveContext save(isolate_);
830 isolate_->set_context(*context);
kasper.lund44510672008-07-25 07:37:58 +0000831
832 // Expose the builtins object in the debugger context.
lrn@chromium.org7516f052011-03-30 08:52:27 +0000833 Handle<String> key = isolate_->factory()->LookupAsciiSymbol("builtins");
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000834 Handle<GlobalObject> global = Handle<GlobalObject>(context->global_object());
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +0000835 RETURN_IF_EMPTY_HANDLE_VALUE(
lrn@chromium.org7516f052011-03-30 08:52:27 +0000836 isolate_,
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000837 JSReceiver::SetProperty(global, key, Handle<Object>(global->builtins()),
838 NONE, kNonStrictMode),
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +0000839 false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000840
841 // Compile the JavaScript for the debugger in the debugger context.
lrn@chromium.org7516f052011-03-30 08:52:27 +0000842 debugger->set_compiling_natives(true);
kasper.lund44510672008-07-25 07:37:58 +0000843 bool caught_exception =
844 !CompileDebuggerScript(Natives::GetIndex("mirror")) ||
845 !CompileDebuggerScript(Natives::GetIndex("debug"));
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000846
847 if (FLAG_enable_liveedit) {
848 caught_exception = caught_exception ||
849 !CompileDebuggerScript(Natives::GetIndex("liveedit"));
850 }
851
lrn@chromium.org7516f052011-03-30 08:52:27 +0000852 debugger->set_compiling_natives(false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000853
mads.s.agercbaa0602008-08-14 13:41:48 +0000854 // Make sure we mark the debugger as not loading before we might
855 // return.
lrn@chromium.org7516f052011-03-30 08:52:27 +0000856 debugger->set_loading_debugger(false);
mads.s.agercbaa0602008-08-14 13:41:48 +0000857
kasper.lund44510672008-07-25 07:37:58 +0000858 // Check for caught exceptions.
859 if (caught_exception) return false;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000860
861 // Debugger loaded.
whesse@chromium.org023421e2010-12-21 12:19:12 +0000862 debug_context_ = context;
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000863
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000864 return true;
865}
866
867
868void Debug::Unload() {
869 // Return debugger is not loaded.
870 if (!IsLoaded()) {
871 return;
872 }
873
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000874 // Clear the script cache.
875 DestroyScriptCache();
876
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000877 // Clear debugger context global handle.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000878 Isolate::Current()->global_handles()->Destroy(
879 reinterpret_cast<Object**>(debug_context_.location()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000880 debug_context_ = Handle<Context>();
881}
882
883
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000884// Set the flag indicating that preemption happened during debugging.
885void Debug::PreemptionWhileInDebugger() {
886 ASSERT(InDebugger());
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000887 Debug::set_interrupts_pending(PREEMPT);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000888}
889
890
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000891void Debug::Iterate(ObjectVisitor* v) {
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000892 v->VisitPointer(BitCast<Object**>(&(debug_break_return_)));
893 v->VisitPointer(BitCast<Object**>(&(debug_break_slot_)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000894}
895
896
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000897Object* Debug::Break(Arguments args) {
898 Heap* heap = isolate_->heap();
899 HandleScope scope(isolate_);
mads.s.ager31e71382008-08-13 09:32:07 +0000900 ASSERT(args.length() == 0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000901
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000902 thread_local_.frame_drop_mode_ = FRAMES_UNTOUCHED;
ager@chromium.org357bf652010-04-12 11:30:10 +0000903
kasper.lundbd3ec4e2008-07-09 11:06:54 +0000904 // Get the top-most JavaScript frame.
vegorov@chromium.org74f333b2011-04-06 11:17:46 +0000905 JavaScriptFrameIterator it(isolate_);
kasper.lundbd3ec4e2008-07-09 11:06:54 +0000906 JavaScriptFrame* frame = it.frame();
907
908 // Just continue if breaks are disabled or debugger cannot be loaded.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000909 if (disable_break() || !Load()) {
910 SetAfterBreakTarget(frame);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000911 return heap->undefined_value();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000912 }
913
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000914 // Enter the debugger.
915 EnterDebugger debugger;
916 if (debugger.FailedToEnter()) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000917 return heap->undefined_value();
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000918 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000919
kasper.lund44510672008-07-25 07:37:58 +0000920 // Postpone interrupt during breakpoint processing.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000921 PostponeInterruptsScope postpone(isolate_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000922
923 // Get the debug info (create it if it does not exist).
924 Handle<SharedFunctionInfo> shared =
925 Handle<SharedFunctionInfo>(JSFunction::cast(frame->function())->shared());
926 Handle<DebugInfo> debug_info = GetDebugInfo(shared);
927
928 // Find the break point where execution has stopped.
929 BreakLocationIterator break_location_iterator(debug_info,
930 ALL_BREAK_LOCATIONS);
931 break_location_iterator.FindBreakLocationFromAddress(frame->pc());
932
933 // Check whether step next reached a new statement.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000934 if (!StepNextContinue(&break_location_iterator, frame)) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000935 // Decrease steps left if performing multiple steps.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000936 if (thread_local_.step_count_ > 0) {
937 thread_local_.step_count_--;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000938 }
939 }
940
941 // If there is one or more real break points check whether any of these are
942 // triggered.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000943 Handle<Object> break_points_hit(heap->undefined_value());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000944 if (break_location_iterator.HasBreakPoint()) {
945 Handle<Object> break_point_objects =
946 Handle<Object>(break_location_iterator.BreakPointObjects());
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000947 break_points_hit = CheckBreakPoints(break_point_objects);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000948 }
949
ager@chromium.orga1645e22009-09-09 19:27:10 +0000950 // If step out is active skip everything until the frame where we need to step
951 // out to is reached, unless real breakpoint is hit.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000952 if (StepOutActive() && frame->fp() != step_out_fp() &&
ager@chromium.orga1645e22009-09-09 19:27:10 +0000953 break_points_hit->IsUndefined() ) {
954 // Step count should always be 0 for StepOut.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000955 ASSERT(thread_local_.step_count_ == 0);
ager@chromium.orga1645e22009-09-09 19:27:10 +0000956 } else if (!break_points_hit->IsUndefined() ||
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000957 (thread_local_.last_step_action_ != StepNone &&
958 thread_local_.step_count_ == 0)) {
ager@chromium.orga1645e22009-09-09 19:27:10 +0000959 // Notify debugger if a real break point is triggered or if performing
960 // single stepping with no more steps to perform. Otherwise do another step.
961
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000962 // Clear all current stepping setup.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000963 ClearStepping();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000964
lrn@chromium.org34e60782011-09-15 07:25:40 +0000965 if (thread_local_.queued_step_count_ > 0) {
966 // Perform queued steps
967 int step_count = thread_local_.queued_step_count_;
968
969 // Clear queue
970 thread_local_.queued_step_count_ = 0;
971
972 PrepareStep(StepNext, step_count);
973 } else {
974 // Notify the debug event listeners.
975 isolate_->debugger()->OnDebugBreak(break_points_hit, false);
976 }
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000977 } else if (thread_local_.last_step_action_ != StepNone) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000978 // Hold on to last step action as it is cleared by the call to
979 // ClearStepping.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000980 StepAction step_action = thread_local_.last_step_action_;
981 int step_count = thread_local_.step_count_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000982
lrn@chromium.org34e60782011-09-15 07:25:40 +0000983 // If StepNext goes deeper in code, StepOut until original frame
984 // and keep step count queued up in the meantime.
985 if (step_action == StepNext && frame->fp() < thread_local_.last_fp_) {
986 // Count frames until target frame
987 int count = 0;
988 JavaScriptFrameIterator it(isolate_);
jkummerow@chromium.org212d9642012-05-11 15:02:09 +0000989 while (!it.done() && it.frame()->fp() < thread_local_.last_fp_) {
lrn@chromium.org34e60782011-09-15 07:25:40 +0000990 count++;
991 it.Advance();
992 }
993
danno@chromium.org81cac2b2012-07-10 11:28:27 +0000994 // Check that we indeed found the frame we are looking for.
995 CHECK(!it.done() && (it.frame()->fp() == thread_local_.last_fp_));
996 if (step_count > 1) {
997 // Save old count and action to continue stepping after StepOut.
998 thread_local_.queued_step_count_ = step_count - 1;
jkummerow@chromium.org212d9642012-05-11 15:02:09 +0000999 }
1000
danno@chromium.org81cac2b2012-07-10 11:28:27 +00001001 // Set up for StepOut to reach target frame.
1002 step_action = StepOut;
1003 step_count = count;
lrn@chromium.org34e60782011-09-15 07:25:40 +00001004 }
1005
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001006 // Clear all current stepping setup.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001007 ClearStepping();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001008
1009 // Set up for the remaining steps.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001010 PrepareStep(step_action, step_count);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001011 }
1012
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001013 if (thread_local_.frame_drop_mode_ == FRAMES_UNTOUCHED) {
1014 SetAfterBreakTarget(frame);
1015 } else if (thread_local_.frame_drop_mode_ ==
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001016 FRAME_DROPPED_IN_IC_CALL) {
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001017 // We must have been calling IC stub. Do not go there anymore.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001018 Code* plain_return = isolate_->builtins()->builtin(
1019 Builtins::kPlainReturn_LiveEdit);
1020 thread_local_.after_break_target_ = plain_return->entry();
1021 } else if (thread_local_.frame_drop_mode_ ==
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001022 FRAME_DROPPED_IN_DEBUG_SLOT_CALL) {
1023 // Debug break slot stub does not return normally, instead it manually
1024 // cleans the stack and jumps. We should patch the jump address.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001025 Code* plain_return = isolate_->builtins()->builtin(
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00001026 Builtins::kFrameDropper_LiveEdit);
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001027 thread_local_.after_break_target_ = plain_return->entry();
1028 } else if (thread_local_.frame_drop_mode_ ==
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001029 FRAME_DROPPED_IN_DIRECT_CALL) {
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001030 // Nothing to do, after_break_target is not used here.
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00001031 } else if (thread_local_.frame_drop_mode_ ==
1032 FRAME_DROPPED_IN_RETURN_CALL) {
1033 Code* plain_return = isolate_->builtins()->builtin(
1034 Builtins::kFrameDropper_LiveEdit);
1035 thread_local_.after_break_target_ = plain_return->entry();
ager@chromium.org357bf652010-04-12 11:30:10 +00001036 } else {
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001037 UNREACHABLE();
ager@chromium.org357bf652010-04-12 11:30:10 +00001038 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001039
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001040 return heap->undefined_value();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001041}
1042
1043
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001044RUNTIME_FUNCTION(Object*, Debug_Break) {
1045 return isolate->debug()->Break(args);
1046}
1047
1048
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001049// Check the break point objects for whether one or more are actually
1050// triggered. This function returns a JSArray with the break point objects
1051// which is triggered.
1052Handle<Object> Debug::CheckBreakPoints(Handle<Object> break_point_objects) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00001053 Factory* factory = isolate_->factory();
1054
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001055 // Count the number of break points hit. If there are multiple break points
1056 // they are in a FixedArray.
1057 Handle<FixedArray> break_points_hit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001058 int break_points_hit_count = 0;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001059 ASSERT(!break_point_objects->IsUndefined());
1060 if (break_point_objects->IsFixedArray()) {
1061 Handle<FixedArray> array(FixedArray::cast(*break_point_objects));
lrn@chromium.org7516f052011-03-30 08:52:27 +00001062 break_points_hit = factory->NewFixedArray(array->length());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001063 for (int i = 0; i < array->length(); i++) {
1064 Handle<Object> o(array->get(i));
1065 if (CheckBreakPoint(o)) {
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001066 break_points_hit->set(break_points_hit_count++, *o);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001067 }
1068 }
1069 } else {
lrn@chromium.org7516f052011-03-30 08:52:27 +00001070 break_points_hit = factory->NewFixedArray(1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001071 if (CheckBreakPoint(break_point_objects)) {
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001072 break_points_hit->set(break_points_hit_count++, *break_point_objects);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001073 }
1074 }
1075
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001076 // Return undefined if no break points were triggered.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001077 if (break_points_hit_count == 0) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00001078 return factory->undefined_value();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001079 }
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001080 // Return break points hit as a JSArray.
lrn@chromium.org7516f052011-03-30 08:52:27 +00001081 Handle<JSArray> result = factory->NewJSArrayWithElements(break_points_hit);
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001082 result->set_length(Smi::FromInt(break_points_hit_count));
1083 return result;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001084}
1085
1086
1087// Check whether a single break point object is triggered.
1088bool Debug::CheckBreakPoint(Handle<Object> break_point_object) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00001089 Factory* factory = isolate_->factory();
1090 HandleScope scope(isolate_);
ager@chromium.org381abbb2009-02-25 13:23:22 +00001091
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001092 // Ignore check if break point object is not a JSObject.
1093 if (!break_point_object->IsJSObject()) return true;
1094
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001095 // Get the function IsBreakPointTriggered (defined in debug-debugger.js).
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001096 Handle<String> is_break_point_triggered_symbol =
lrn@chromium.org7516f052011-03-30 08:52:27 +00001097 factory->LookupAsciiSymbol("IsBreakPointTriggered");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001098 Handle<JSFunction> check_break_point =
1099 Handle<JSFunction>(JSFunction::cast(
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00001100 debug_context()->global_object()->GetPropertyNoExceptionThrown(
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001101 *is_break_point_triggered_symbol)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001102
1103 // Get the break id as an object.
lrn@chromium.org7516f052011-03-30 08:52:27 +00001104 Handle<Object> break_id = factory->NewNumberFromInt(Debug::break_id());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001105
1106 // Call HandleBreakPointx.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001107 bool caught_exception;
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00001108 Handle<Object> argv[] = { break_id, break_point_object };
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001109 Handle<Object> result = Execution::TryCall(check_break_point,
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00001110 isolate_->js_builtins_object(),
1111 ARRAY_SIZE(argv),
1112 argv,
1113 &caught_exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001114
1115 // If exception or non boolean result handle as not triggered
1116 if (caught_exception || !result->IsBoolean()) {
1117 return false;
1118 }
1119
1120 // Return whether the break point is triggered.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001121 ASSERT(!result.is_null());
1122 return (*result)->IsTrue();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001123}
1124
1125
1126// Check whether the function has debug information.
1127bool Debug::HasDebugInfo(Handle<SharedFunctionInfo> shared) {
1128 return !shared->debug_info()->IsUndefined();
1129}
1130
1131
kasper.lundbd3ec4e2008-07-09 11:06:54 +00001132// Return the debug info for this function. EnsureDebugInfo must be called
1133// prior to ensure the debug info has been generated for shared.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001134Handle<DebugInfo> Debug::GetDebugInfo(Handle<SharedFunctionInfo> shared) {
kasper.lundbd3ec4e2008-07-09 11:06:54 +00001135 ASSERT(HasDebugInfo(shared));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001136 return Handle<DebugInfo>(DebugInfo::cast(shared->debug_info()));
1137}
1138
1139
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001140void Debug::SetBreakPoint(Handle<JSFunction> function,
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00001141 Handle<Object> break_point_object,
1142 int* source_position) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00001143 HandleScope scope(isolate_);
ager@chromium.org381abbb2009-02-25 13:23:22 +00001144
lrn@chromium.org34e60782011-09-15 07:25:40 +00001145 PrepareForBreakPoints();
1146
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001147 // Make sure the function is compiled and has set up the debug info.
1148 Handle<SharedFunctionInfo> shared(function->shared());
1149 if (!EnsureDebugInfo(shared, function)) {
kasper.lundbd3ec4e2008-07-09 11:06:54 +00001150 // Return if retrieving debug info failed.
1151 return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001152 }
1153
kasper.lundbd3ec4e2008-07-09 11:06:54 +00001154 Handle<DebugInfo> debug_info = GetDebugInfo(shared);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001155 // Source positions starts with zero.
danno@chromium.orgbf0c8202011-12-27 10:09:42 +00001156 ASSERT(*source_position >= 0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001157
1158 // Find the break point and change it.
1159 BreakLocationIterator it(debug_info, SOURCE_BREAK_LOCATIONS);
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00001160 it.FindBreakLocationFromPosition(*source_position);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001161 it.SetBreakPoint(break_point_object);
1162
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00001163 *source_position = it.position();
1164
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001165 // At least one active break point now.
1166 ASSERT(debug_info->GetBreakPointCount() > 0);
1167}
1168
1169
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001170bool Debug::SetBreakPointForScript(Handle<Script> script,
1171 Handle<Object> break_point_object,
1172 int* source_position) {
1173 HandleScope scope(isolate_);
1174
1175 // No need to call PrepareForBreakPoints because it will be called
1176 // implicitly by Runtime::FindSharedFunctionInfoInScript.
1177 Object* result = Runtime::FindSharedFunctionInfoInScript(isolate_,
1178 script,
1179 *source_position);
1180 if (result->IsUndefined()) return false;
1181
1182 // Make sure the function has set up the debug info.
1183 Handle<SharedFunctionInfo> shared(SharedFunctionInfo::cast(result));
1184 if (!EnsureDebugInfo(shared, Handle<JSFunction>::null())) {
1185 // Return if retrieving debug info failed.
1186 return false;
1187 }
1188
1189 // Find position within function. The script position might be before the
1190 // source position of the first function.
1191 int position;
1192 if (shared->start_position() > *source_position) {
1193 position = 0;
1194 } else {
1195 position = *source_position - shared->start_position();
1196 }
1197
1198 Handle<DebugInfo> debug_info = GetDebugInfo(shared);
1199 // Source positions starts with zero.
1200 ASSERT(position >= 0);
1201
1202 // Find the break point and change it.
1203 BreakLocationIterator it(debug_info, SOURCE_BREAK_LOCATIONS);
1204 it.FindBreakLocationFromPosition(position);
1205 it.SetBreakPoint(break_point_object);
1206
1207 *source_position = it.position() + shared->start_position();
1208
1209 // At least one active break point now.
1210 ASSERT(debug_info->GetBreakPointCount() > 0);
1211 return true;
1212}
1213
1214
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001215void Debug::ClearBreakPoint(Handle<Object> break_point_object) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00001216 HandleScope scope(isolate_);
ager@chromium.org381abbb2009-02-25 13:23:22 +00001217
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001218 DebugInfoListNode* node = debug_info_list_;
1219 while (node != NULL) {
1220 Object* result = DebugInfo::FindBreakPointInfo(node->debug_info(),
1221 break_point_object);
1222 if (!result->IsUndefined()) {
1223 // Get information in the break point.
1224 BreakPointInfo* break_point_info = BreakPointInfo::cast(result);
1225 Handle<DebugInfo> debug_info = node->debug_info();
1226 Handle<SharedFunctionInfo> shared(debug_info->shared());
1227 int source_position = break_point_info->statement_position()->value();
1228
1229 // Source positions starts with zero.
1230 ASSERT(source_position >= 0);
1231
1232 // Find the break point and clear it.
1233 BreakLocationIterator it(debug_info, SOURCE_BREAK_LOCATIONS);
1234 it.FindBreakLocationFromPosition(source_position);
1235 it.ClearBreakPoint(break_point_object);
1236
1237 // If there are no more break points left remove the debug info for this
1238 // function.
1239 if (debug_info->GetBreakPointCount() == 0) {
1240 RemoveDebugInfo(debug_info);
1241 }
1242
1243 return;
1244 }
1245 node = node->next();
1246 }
1247}
1248
1249
ager@chromium.org381abbb2009-02-25 13:23:22 +00001250void Debug::ClearAllBreakPoints() {
1251 DebugInfoListNode* node = debug_info_list_;
1252 while (node != NULL) {
1253 // Remove all debug break code.
1254 BreakLocationIterator it(node->debug_info(), ALL_BREAK_LOCATIONS);
1255 it.ClearAllDebugBreak();
1256 node = node->next();
1257 }
1258
1259 // Remove all debug info.
1260 while (debug_info_list_ != NULL) {
1261 RemoveDebugInfo(debug_info_list_->debug_info());
1262 }
1263}
1264
1265
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001266void Debug::FloodWithOneShot(Handle<JSFunction> function) {
lrn@chromium.org34e60782011-09-15 07:25:40 +00001267 PrepareForBreakPoints();
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001268
1269 // Make sure the function is compiled and has set up the debug info.
1270 Handle<SharedFunctionInfo> shared(function->shared());
1271 if (!EnsureDebugInfo(shared, function)) {
kasper.lundbd3ec4e2008-07-09 11:06:54 +00001272 // Return if we failed to retrieve the debug info.
1273 return;
1274 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001275
1276 // Flood the function with break points.
kasper.lundbd3ec4e2008-07-09 11:06:54 +00001277 BreakLocationIterator it(GetDebugInfo(shared), ALL_BREAK_LOCATIONS);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001278 while (!it.Done()) {
1279 it.SetOneShot();
1280 it.Next();
1281 }
1282}
1283
1284
rossberg@chromium.org2c067b12012-03-19 11:01:52 +00001285void Debug::FloodBoundFunctionWithOneShot(Handle<JSFunction> function) {
1286 Handle<FixedArray> new_bindings(function->function_bindings());
1287 Handle<Object> bindee(new_bindings->get(JSFunction::kBoundFunctionIndex));
1288
1289 if (!bindee.is_null() && bindee->IsJSFunction() &&
1290 !JSFunction::cast(*bindee)->IsBuiltin()) {
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001291 Handle<JSFunction> bindee_function(JSFunction::cast(*bindee));
1292 Debug::FloodWithOneShot(bindee_function);
rossberg@chromium.org2c067b12012-03-19 11:01:52 +00001293 }
1294}
1295
1296
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001297void Debug::FloodHandlerWithOneShot() {
ager@chromium.org8bb60582008-12-11 12:02:20 +00001298 // Iterate through the JavaScript stack looking for handlers.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001299 StackFrame::Id id = break_frame_id();
ager@chromium.org8bb60582008-12-11 12:02:20 +00001300 if (id == StackFrame::NO_ID) {
1301 // If there is no JavaScript stack don't do anything.
1302 return;
1303 }
vegorov@chromium.org74f333b2011-04-06 11:17:46 +00001304 for (JavaScriptFrameIterator it(isolate_, id); !it.done(); it.Advance()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001305 JavaScriptFrame* frame = it.frame();
1306 if (frame->HasHandler()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001307 // Flood the function with the catch block with break points
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001308 JSFunction* function = JSFunction::cast(frame->function());
1309 FloodWithOneShot(Handle<JSFunction>(function));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001310 return;
1311 }
1312 }
1313}
1314
1315
1316void Debug::ChangeBreakOnException(ExceptionBreakType type, bool enable) {
1317 if (type == BreakUncaughtException) {
1318 break_on_uncaught_exception_ = enable;
1319 } else {
1320 break_on_exception_ = enable;
1321 }
1322}
1323
1324
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00001325bool Debug::IsBreakOnException(ExceptionBreakType type) {
1326 if (type == BreakUncaughtException) {
1327 return break_on_uncaught_exception_;
1328 } else {
1329 return break_on_exception_;
1330 }
1331}
1332
1333
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001334void Debug::PrepareStep(StepAction step_action, int step_count) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00001335 HandleScope scope(isolate_);
lrn@chromium.org34e60782011-09-15 07:25:40 +00001336
1337 PrepareForBreakPoints();
1338
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001339 ASSERT(Debug::InDebugger());
1340
1341 // Remember this step action and count.
1342 thread_local_.last_step_action_ = step_action;
ager@chromium.orga1645e22009-09-09 19:27:10 +00001343 if (step_action == StepOut) {
1344 // For step out target frame will be found on the stack so there is no need
1345 // to set step counter for it. It's expected to always be 0 for StepOut.
1346 thread_local_.step_count_ = 0;
1347 } else {
1348 thread_local_.step_count_ = step_count;
1349 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001350
1351 // Get the frame where the execution has stopped and skip the debug frame if
1352 // any. The debug frame will only be present if execution was stopped due to
1353 // hitting a break point. In other situations (e.g. unhandled exception) the
1354 // debug frame is not present.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001355 StackFrame::Id id = break_frame_id();
ager@chromium.org8bb60582008-12-11 12:02:20 +00001356 if (id == StackFrame::NO_ID) {
1357 // If there is no JavaScript stack don't do anything.
1358 return;
1359 }
vegorov@chromium.org74f333b2011-04-06 11:17:46 +00001360 JavaScriptFrameIterator frames_it(isolate_, id);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001361 JavaScriptFrame* frame = frames_it.frame();
1362
1363 // First of all ensure there is one-shot break points in the top handler
1364 // if any.
1365 FloodHandlerWithOneShot();
1366
1367 // If the function on the top frame is unresolved perform step out. This will
1368 // be the case when calling unknown functions and having the debugger stopped
1369 // in an unhandled exception.
1370 if (!frame->function()->IsJSFunction()) {
1371 // Step out: Find the calling JavaScript frame and flood it with
1372 // breakpoints.
1373 frames_it.Advance();
1374 // Fill the function to return to with one-shot break points.
1375 JSFunction* function = JSFunction::cast(frames_it.frame()->function());
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001376 FloodWithOneShot(Handle<JSFunction>(function));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001377 return;
1378 }
1379
1380 // Get the debug info (create it if it does not exist).
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001381 Handle<JSFunction> function(JSFunction::cast(frame->function()));
1382 Handle<SharedFunctionInfo> shared(function->shared());
1383 if (!EnsureDebugInfo(shared, function)) {
kasper.lundbd3ec4e2008-07-09 11:06:54 +00001384 // Return if ensuring debug info failed.
1385 return;
1386 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001387 Handle<DebugInfo> debug_info = GetDebugInfo(shared);
1388
1389 // Find the break location where execution has stopped.
1390 BreakLocationIterator it(debug_info, ALL_BREAK_LOCATIONS);
1391 it.FindBreakLocationFromAddress(frame->pc());
1392
1393 // Compute whether or not the target is a call target.
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001394 bool is_load_or_store = false;
1395 bool is_inline_cache_stub = false;
whesse@chromium.orge90029b2010-08-02 11:52:17 +00001396 bool is_at_restarted_function = false;
ager@chromium.orga1645e22009-09-09 19:27:10 +00001397 Handle<Code> call_function_stub;
ager@chromium.orga1645e22009-09-09 19:27:10 +00001398
whesse@chromium.orge90029b2010-08-02 11:52:17 +00001399 if (thread_local_.restarter_frame_function_pointer_ == NULL) {
1400 if (RelocInfo::IsCodeTarget(it.rinfo()->rmode())) {
1401 bool is_call_target = false;
1402 Address target = it.rinfo()->target_address();
1403 Code* code = Code::GetCodeFromTargetAddress(target);
1404 if (code->is_call_stub() || code->is_keyed_call_stub()) {
1405 is_call_target = true;
1406 }
1407 if (code->is_inline_cache_stub()) {
1408 is_inline_cache_stub = true;
1409 is_load_or_store = !is_call_target;
1410 }
1411
1412 // Check if target code is CallFunction stub.
1413 Code* maybe_call_function_stub = code;
1414 // If there is a breakpoint at this line look at the original code to
1415 // check if it is a CallFunction stub.
1416 if (it.IsDebugBreak()) {
1417 Address original_target = it.original_rinfo()->target_address();
1418 maybe_call_function_stub =
1419 Code::GetCodeFromTargetAddress(original_target);
1420 }
1421 if (maybe_call_function_stub->kind() == Code::STUB &&
1422 maybe_call_function_stub->major_key() == CodeStub::CallFunction) {
1423 // Save reference to the code as we may need it to find out arguments
1424 // count for 'step in' later.
1425 call_function_stub = Handle<Code>(maybe_call_function_stub);
1426 }
ager@chromium.orga1645e22009-09-09 19:27:10 +00001427 }
whesse@chromium.orge90029b2010-08-02 11:52:17 +00001428 } else {
1429 is_at_restarted_function = true;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001430 }
1431
v8.team.kasperl727e9952008-09-02 14:56:44 +00001432 // If this is the last break code target step out is the only possibility.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001433 if (it.IsExit() || step_action == StepOut) {
ager@chromium.orga1645e22009-09-09 19:27:10 +00001434 if (step_action == StepOut) {
1435 // Skip step_count frames starting with the current one.
1436 while (step_count-- > 0 && !frames_it.done()) {
1437 frames_it.Advance();
1438 }
1439 } else {
1440 ASSERT(it.IsExit());
1441 frames_it.Advance();
1442 }
1443 // Skip builtin functions on the stack.
1444 while (!frames_it.done() &&
1445 JSFunction::cast(frames_it.frame()->function())->IsBuiltin()) {
1446 frames_it.Advance();
1447 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001448 // Step out: If there is a JavaScript caller frame, we need to
1449 // flood it with breakpoints.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001450 if (!frames_it.done()) {
1451 // Fill the function to return to with one-shot break points.
1452 JSFunction* function = JSFunction::cast(frames_it.frame()->function());
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001453 FloodWithOneShot(Handle<JSFunction>(function));
ager@chromium.orga1645e22009-09-09 19:27:10 +00001454 // Set target frame pointer.
1455 ActivateStepOut(frames_it.frame());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001456 }
ager@chromium.orga1645e22009-09-09 19:27:10 +00001457 } else if (!(is_inline_cache_stub || RelocInfo::IsConstructCall(it.rmode()) ||
whesse@chromium.orge90029b2010-08-02 11:52:17 +00001458 !call_function_stub.is_null() || is_at_restarted_function)
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001459 || step_action == StepNext || step_action == StepMin) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001460 // Step next or step min.
1461
1462 // Fill the current function with one-shot break points.
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001463 FloodWithOneShot(function);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001464
1465 // Remember source position and frame to handle step next.
1466 thread_local_.last_statement_position_ =
1467 debug_info->code()->SourceStatementPosition(frame->pc());
mmassi@chromium.org7028c052012-06-13 11:51:58 +00001468 thread_local_.last_fp_ = frame->UnpaddedFP();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001469 } else {
whesse@chromium.orge90029b2010-08-02 11:52:17 +00001470 // If there's restarter frame on top of the stack, just get the pointer
1471 // to function which is going to be restarted.
1472 if (is_at_restarted_function) {
1473 Handle<JSFunction> restarted_function(
1474 JSFunction::cast(*thread_local_.restarter_frame_function_pointer_));
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001475 FloodWithOneShot(restarted_function);
whesse@chromium.orge90029b2010-08-02 11:52:17 +00001476 } else if (!call_function_stub.is_null()) {
1477 // If it's CallFunction stub ensure target function is compiled and flood
1478 // it with one shot breakpoints.
1479
ager@chromium.orga1645e22009-09-09 19:27:10 +00001480 // Find out number of arguments from the stub minor key.
1481 // Reverse lookup required as the minor key cannot be retrieved
1482 // from the code object.
1483 Handle<Object> obj(
lrn@chromium.org7516f052011-03-30 08:52:27 +00001484 isolate_->heap()->code_stubs()->SlowReverseLookup(
1485 *call_function_stub));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001486 ASSERT(!obj.is_null());
1487 ASSERT(!(*obj)->IsUndefined());
ager@chromium.orga1645e22009-09-09 19:27:10 +00001488 ASSERT(obj->IsSmi());
1489 // Get the STUB key and extract major and minor key.
1490 uint32_t key = Smi::cast(*obj)->value();
1491 // Argc in the stub is the number of arguments passed - not the
1492 // expected arguments of the called function.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001493 int call_function_arg_count =
1494 CallFunctionStub::ExtractArgcFromMinorKey(
1495 CodeStub::MinorKeyFromKey(key));
ager@chromium.orga1645e22009-09-09 19:27:10 +00001496 ASSERT(call_function_stub->major_key() ==
1497 CodeStub::MajorKeyFromKey(key));
1498
1499 // Find target function on the expression stack.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001500 // Expression stack looks like this (top to bottom):
ager@chromium.orga1645e22009-09-09 19:27:10 +00001501 // argN
1502 // ...
1503 // arg0
1504 // Receiver
1505 // Function to call
1506 int expressions_count = frame->ComputeExpressionsCount();
1507 ASSERT(expressions_count - 2 - call_function_arg_count >= 0);
1508 Object* fun = frame->GetExpression(
1509 expressions_count - 2 - call_function_arg_count);
1510 if (fun->IsJSFunction()) {
1511 Handle<JSFunction> js_function(JSFunction::cast(fun));
rossberg@chromium.org2c067b12012-03-19 11:01:52 +00001512 if (js_function->shared()->bound()) {
1513 Debug::FloodBoundFunctionWithOneShot(js_function);
1514 } else if (!js_function->IsBuiltin()) {
1515 // Don't step into builtins.
ager@chromium.orga1645e22009-09-09 19:27:10 +00001516 // It will also compile target function if it's not compiled yet.
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001517 FloodWithOneShot(js_function);
ager@chromium.orga1645e22009-09-09 19:27:10 +00001518 }
1519 }
1520 }
1521
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001522 // Fill the current function with one-shot break points even for step in on
1523 // a call target as the function called might be a native function for
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001524 // which step in will not stop. It also prepares for stepping in
1525 // getters/setters.
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001526 FloodWithOneShot(function);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001527
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001528 if (is_load_or_store) {
1529 // Remember source position and frame to handle step in getter/setter. If
1530 // there is a custom getter/setter it will be handled in
1531 // Object::Get/SetPropertyWithCallback, otherwise the step action will be
1532 // propagated on the next Debug::Break.
1533 thread_local_.last_statement_position_ =
1534 debug_info->code()->SourceStatementPosition(frame->pc());
mmassi@chromium.org7028c052012-06-13 11:51:58 +00001535 thread_local_.last_fp_ = frame->UnpaddedFP();
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001536 }
1537
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001538 // Step in or Step in min
1539 it.PrepareStepIn();
1540 ActivateStepIn(frame);
1541 }
1542}
1543
1544
1545// Check whether the current debug break should be reported to the debugger. It
1546// is used to have step next and step in only report break back to the debugger
1547// if on a different frame or in a different statement. In some situations
1548// there will be several break points in the same statement when the code is
v8.team.kasperl727e9952008-09-02 14:56:44 +00001549// flooded with one-shot break points. This function helps to perform several
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001550// steps before reporting break back to the debugger.
1551bool Debug::StepNextContinue(BreakLocationIterator* break_location_iterator,
1552 JavaScriptFrame* frame) {
lrn@chromium.org34e60782011-09-15 07:25:40 +00001553 // StepNext and StepOut shouldn't bring us deeper in code, so last frame
1554 // shouldn't be a parent of current frame.
1555 if (thread_local_.last_step_action_ == StepNext ||
1556 thread_local_.last_step_action_ == StepOut) {
1557 if (frame->fp() < thread_local_.last_fp_) return true;
1558 }
1559
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001560 // If the step last action was step next or step in make sure that a new
1561 // statement is hit.
1562 if (thread_local_.last_step_action_ == StepNext ||
1563 thread_local_.last_step_action_ == StepIn) {
1564 // Never continue if returning from function.
1565 if (break_location_iterator->IsExit()) return false;
1566
1567 // Continue if we are still on the same frame and in the same statement.
1568 int current_statement_position =
1569 break_location_iterator->code()->SourceStatementPosition(frame->pc());
mmassi@chromium.org7028c052012-06-13 11:51:58 +00001570 return thread_local_.last_fp_ == frame->UnpaddedFP() &&
ager@chromium.org236ad962008-09-25 09:45:57 +00001571 thread_local_.last_statement_position_ == current_statement_position;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001572 }
1573
1574 // No step next action - don't continue.
1575 return false;
1576}
1577
1578
1579// Check whether the code object at the specified address is a debug break code
1580// object.
1581bool Debug::IsDebugBreak(Address addr) {
ager@chromium.org8bb60582008-12-11 12:02:20 +00001582 Code* code = Code::GetCodeFromTargetAddress(addr);
kasper.lund7276f142008-07-30 08:49:36 +00001583 return code->ic_state() == DEBUG_BREAK;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001584}
1585
1586
1587// Check whether a code stub with the specified major key is a possible break
1588// point location when looking for source break locations.
1589bool Debug::IsSourceBreakStub(Code* code) {
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001590 CodeStub::Major major_key = CodeStub::GetMajorKey(code);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001591 return major_key == CodeStub::CallFunction;
1592}
1593
1594
1595// Check whether a code stub with the specified major key is a possible break
1596// location.
1597bool Debug::IsBreakStub(Code* code) {
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001598 CodeStub::Major major_key = CodeStub::GetMajorKey(code);
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00001599 return major_key == CodeStub::CallFunction;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001600}
1601
1602
1603// Find the builtin to use for invoking the debug break
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001604Handle<Code> Debug::FindDebugBreak(Handle<Code> code, RelocInfo::Mode mode) {
danno@chromium.orgfa458e42012-02-01 10:48:36 +00001605 Isolate* isolate = Isolate::Current();
1606
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001607 // Find the builtin debug break function matching the calling convention
1608 // used by the call site.
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001609 if (code->is_inline_cache_stub()) {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001610 switch (code->kind()) {
1611 case Code::CALL_IC:
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001612 case Code::KEYED_CALL_IC:
danno@chromium.orgfa458e42012-02-01 10:48:36 +00001613 return isolate->stub_cache()->ComputeCallDebugBreak(
1614 code->arguments_count(), code->kind());
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001615
1616 case Code::LOAD_IC:
danno@chromium.orgfa458e42012-02-01 10:48:36 +00001617 return isolate->builtins()->LoadIC_DebugBreak();
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001618
1619 case Code::STORE_IC:
danno@chromium.orgfa458e42012-02-01 10:48:36 +00001620 return isolate->builtins()->StoreIC_DebugBreak();
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001621
1622 case Code::KEYED_LOAD_IC:
danno@chromium.orgfa458e42012-02-01 10:48:36 +00001623 return isolate->builtins()->KeyedLoadIC_DebugBreak();
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001624
1625 case Code::KEYED_STORE_IC:
danno@chromium.orgfa458e42012-02-01 10:48:36 +00001626 return isolate->builtins()->KeyedStoreIC_DebugBreak();
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001627
1628 default:
1629 UNREACHABLE();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001630 }
1631 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001632 if (RelocInfo::IsConstructCall(mode)) {
danno@chromium.orgfa458e42012-02-01 10:48:36 +00001633 if (code->has_function_cache()) {
1634 return isolate->builtins()->CallConstructStub_Recording_DebugBreak();
1635 } else {
1636 return isolate->builtins()->CallConstructStub_DebugBreak();
1637 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001638 }
1639 if (code->kind() == Code::STUB) {
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00001640 ASSERT(code->major_key() == CodeStub::CallFunction);
danno@chromium.orgfa458e42012-02-01 10:48:36 +00001641 if (code->has_function_cache()) {
1642 return isolate->builtins()->CallFunctionStub_Recording_DebugBreak();
1643 } else {
1644 return isolate->builtins()->CallFunctionStub_DebugBreak();
1645 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001646 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001647
1648 UNREACHABLE();
1649 return Handle<Code>::null();
1650}
1651
1652
1653// Simple function for returning the source positions for active break points.
1654Handle<Object> Debug::GetSourceBreakLocations(
1655 Handle<SharedFunctionInfo> shared) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00001656 Isolate* isolate = Isolate::Current();
1657 Heap* heap = isolate->heap();
1658 if (!HasDebugInfo(shared)) return Handle<Object>(heap->undefined_value());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001659 Handle<DebugInfo> debug_info = GetDebugInfo(shared);
1660 if (debug_info->GetBreakPointCount() == 0) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00001661 return Handle<Object>(heap->undefined_value());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001662 }
1663 Handle<FixedArray> locations =
lrn@chromium.org7516f052011-03-30 08:52:27 +00001664 isolate->factory()->NewFixedArray(debug_info->GetBreakPointCount());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001665 int count = 0;
1666 for (int i = 0; i < debug_info->break_points()->length(); i++) {
1667 if (!debug_info->break_points()->get(i)->IsUndefined()) {
1668 BreakPointInfo* break_point_info =
1669 BreakPointInfo::cast(debug_info->break_points()->get(i));
1670 if (break_point_info->GetBreakPointCount() > 0) {
1671 locations->set(count++, break_point_info->statement_position());
1672 }
1673 }
1674 }
1675 return locations;
1676}
1677
1678
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001679void Debug::NewBreak(StackFrame::Id break_frame_id) {
1680 thread_local_.break_frame_id_ = break_frame_id;
1681 thread_local_.break_id_ = ++thread_local_.break_count_;
1682}
1683
1684
1685void Debug::SetBreak(StackFrame::Id break_frame_id, int break_id) {
1686 thread_local_.break_frame_id_ = break_frame_id;
1687 thread_local_.break_id_ = break_id;
1688}
1689
1690
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001691// Handle stepping into a function.
1692void Debug::HandleStepIn(Handle<JSFunction> function,
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001693 Handle<Object> holder,
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001694 Address fp,
1695 bool is_constructor) {
1696 // If the frame pointer is not supplied by the caller find it.
1697 if (fp == 0) {
1698 StackFrameIterator it;
1699 it.Advance();
1700 // For constructor functions skip another frame.
1701 if (is_constructor) {
1702 ASSERT(it.frame()->is_construct());
1703 it.Advance();
1704 }
1705 fp = it.frame()->fp();
1706 }
1707
1708 // Flood the function with one-shot break points if it is called from where
1709 // step into was requested.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001710 if (fp == step_in_fp()) {
rossberg@chromium.org2c067b12012-03-19 11:01:52 +00001711 if (function->shared()->bound()) {
1712 // Handle Function.prototype.bind
1713 Debug::FloodBoundFunctionWithOneShot(function);
1714 } else if (!function->IsBuiltin()) {
1715 // Don't allow step into functions in the native context.
kasperl@chromium.orgacae3782009-04-11 09:17:08 +00001716 if (function->shared()->code() ==
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00001717 Isolate::Current()->builtins()->builtin(Builtins::kFunctionApply) ||
kasperl@chromium.orgacae3782009-04-11 09:17:08 +00001718 function->shared()->code() ==
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00001719 Isolate::Current()->builtins()->builtin(Builtins::kFunctionCall)) {
kasperl@chromium.orgacae3782009-04-11 09:17:08 +00001720 // Handle function.apply and function.call separately to flood the
1721 // function to be called and not the code for Builtins::FunctionApply or
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001722 // Builtins::FunctionCall. The receiver of call/apply is the target
1723 // function.
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001724 if (!holder.is_null() && holder->IsJSFunction() &&
1725 !JSFunction::cast(*holder)->IsBuiltin()) {
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001726 Handle<JSFunction> js_function = Handle<JSFunction>::cast(holder);
1727 Debug::FloodWithOneShot(js_function);
kasperl@chromium.orgacae3782009-04-11 09:17:08 +00001728 }
1729 } else {
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001730 Debug::FloodWithOneShot(function);
kasperl@chromium.orgacae3782009-04-11 09:17:08 +00001731 }
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001732 }
1733 }
1734}
1735
1736
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001737void Debug::ClearStepping() {
1738 // Clear the various stepping setup.
1739 ClearOneShot();
1740 ClearStepIn();
ager@chromium.orga1645e22009-09-09 19:27:10 +00001741 ClearStepOut();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001742 ClearStepNext();
1743
1744 // Clear multiple step counter.
1745 thread_local_.step_count_ = 0;
1746}
1747
1748// Clears all the one-shot break points that are currently set. Normally this
1749// function is called each time a break point is hit as one shot break points
1750// are used to support stepping.
1751void Debug::ClearOneShot() {
1752 // The current implementation just runs through all the breakpoints. When the
v8.team.kasperl727e9952008-09-02 14:56:44 +00001753 // last break point for a function is removed that function is automatically
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001754 // removed from the list.
1755
1756 DebugInfoListNode* node = debug_info_list_;
1757 while (node != NULL) {
1758 BreakLocationIterator it(node->debug_info(), ALL_BREAK_LOCATIONS);
1759 while (!it.Done()) {
1760 it.ClearOneShot();
1761 it.Next();
1762 }
1763 node = node->next();
1764 }
1765}
1766
1767
1768void Debug::ActivateStepIn(StackFrame* frame) {
ager@chromium.orga1645e22009-09-09 19:27:10 +00001769 ASSERT(!StepOutActive());
mmassi@chromium.org7028c052012-06-13 11:51:58 +00001770 thread_local_.step_into_fp_ = frame->UnpaddedFP();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001771}
1772
1773
1774void Debug::ClearStepIn() {
1775 thread_local_.step_into_fp_ = 0;
1776}
1777
1778
ager@chromium.orga1645e22009-09-09 19:27:10 +00001779void Debug::ActivateStepOut(StackFrame* frame) {
1780 ASSERT(!StepInActive());
mmassi@chromium.org7028c052012-06-13 11:51:58 +00001781 thread_local_.step_out_fp_ = frame->UnpaddedFP();
ager@chromium.orga1645e22009-09-09 19:27:10 +00001782}
1783
1784
1785void Debug::ClearStepOut() {
1786 thread_local_.step_out_fp_ = 0;
1787}
1788
1789
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001790void Debug::ClearStepNext() {
1791 thread_local_.last_step_action_ = StepNone;
ager@chromium.org236ad962008-09-25 09:45:57 +00001792 thread_local_.last_statement_position_ = RelocInfo::kNoPosition;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001793 thread_local_.last_fp_ = 0;
1794}
1795
1796
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001797// Helper function to compile full code for debugging. This code will
mmassi@chromium.org7028c052012-06-13 11:51:58 +00001798// have debug break slots and deoptimization information. Deoptimization
1799// information is required in case that an optimized version of this
1800// function is still activated on the stack. It will also make sure that
1801// the full code is compiled with the same flags as the previous version,
1802// that is flags which can change the code generated. The current method
1803// of mapping from already compiled full code without debug break slots
1804// to full code with debug break slots depends on the generated code is
1805// otherwise exactly the same.
1806static bool CompileFullCodeForDebugging(Handle<JSFunction> function,
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001807 Handle<Code> current_code) {
1808 ASSERT(!current_code->has_debug_break_slots());
1809
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001810 CompilationInfoWithZone info(function);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001811 info.MarkCompilingForDebugging(current_code);
1812 ASSERT(!info.shared_info()->is_compiled());
1813 ASSERT(!info.isolate()->has_pending_exception());
1814
1815 // Use compile lazy which will end up compiling the full code in the
1816 // configuration configured above.
1817 bool result = Compiler::CompileLazy(&info);
1818 ASSERT(result != Isolate::Current()->has_pending_exception());
1819 info.isolate()->clear_pending_exception();
1820#if DEBUG
1821 if (result) {
mmassi@chromium.org7028c052012-06-13 11:51:58 +00001822 Handle<Code> new_code(function->shared()->code());
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001823 ASSERT(new_code->has_debug_break_slots());
1824 ASSERT(current_code->is_compiled_optimizable() ==
1825 new_code->is_compiled_optimizable());
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001826 }
1827#endif
1828 return result;
1829}
1830
1831
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001832static void CollectActiveFunctionsFromThread(
1833 Isolate* isolate,
1834 ThreadLocalTop* top,
1835 List<Handle<JSFunction> >* active_functions,
1836 Object* active_code_marker) {
1837 // Find all non-optimized code functions with activation frames
1838 // on the stack. This includes functions which have optimized
1839 // activations (including inlined functions) on the stack as the
1840 // non-optimized code is needed for the lazy deoptimization.
1841 for (JavaScriptFrameIterator it(isolate, top); !it.done(); it.Advance()) {
1842 JavaScriptFrame* frame = it.frame();
1843 if (frame->is_optimized()) {
1844 List<JSFunction*> functions(Compiler::kMaxInliningLevels + 1);
1845 frame->GetFunctions(&functions);
1846 for (int i = 0; i < functions.length(); i++) {
1847 JSFunction* function = functions[i];
1848 active_functions->Add(Handle<JSFunction>(function));
1849 function->shared()->code()->set_gc_metadata(active_code_marker);
1850 }
1851 } else if (frame->function()->IsJSFunction()) {
1852 JSFunction* function = JSFunction::cast(frame->function());
1853 ASSERT(frame->LookupCode()->kind() == Code::FUNCTION);
1854 active_functions->Add(Handle<JSFunction>(function));
1855 function->shared()->code()->set_gc_metadata(active_code_marker);
1856 }
1857 }
1858}
1859
1860
1861static void RedirectActivationsToRecompiledCodeOnThread(
1862 Isolate* isolate,
1863 ThreadLocalTop* top) {
1864 for (JavaScriptFrameIterator it(isolate, top); !it.done(); it.Advance()) {
1865 JavaScriptFrame* frame = it.frame();
1866
1867 if (frame->is_optimized() || !frame->function()->IsJSFunction()) continue;
1868
1869 JSFunction* function = JSFunction::cast(frame->function());
1870
1871 ASSERT(frame->LookupCode()->kind() == Code::FUNCTION);
1872
1873 Handle<Code> frame_code(frame->LookupCode());
1874 if (frame_code->has_debug_break_slots()) continue;
1875
1876 Handle<Code> new_code(function->shared()->code());
1877 if (new_code->kind() != Code::FUNCTION ||
1878 !new_code->has_debug_break_slots()) {
1879 continue;
1880 }
1881
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001882 // Iterate over the RelocInfo in the original code to compute the sum of the
1883 // constant pools sizes. (See Assembler::CheckConstPool())
1884 // Note that this is only useful for architectures using constant pools.
1885 int constpool_mask = RelocInfo::ModeMask(RelocInfo::CONST_POOL);
1886 int frame_const_pool_size = 0;
1887 for (RelocIterator it(*frame_code, constpool_mask); !it.done(); it.next()) {
1888 RelocInfo* info = it.rinfo();
1889 if (info->pc() >= frame->pc()) break;
1890 frame_const_pool_size += static_cast<int>(info->data());
1891 }
1892 intptr_t frame_offset =
1893 frame->pc() - frame_code->instruction_start() - frame_const_pool_size;
1894
1895 // Iterate over the RelocInfo for new code to find the number of bytes
1896 // generated for debug slots and constant pools.
1897 int debug_break_slot_bytes = 0;
1898 int new_code_const_pool_size = 0;
1899 int mask = RelocInfo::ModeMask(RelocInfo::DEBUG_BREAK_SLOT) |
1900 RelocInfo::ModeMask(RelocInfo::CONST_POOL);
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001901 for (RelocIterator it(*new_code, mask); !it.done(); it.next()) {
1902 // Check if the pc in the new code with debug break
1903 // slots is before this slot.
1904 RelocInfo* info = it.rinfo();
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001905 intptr_t new_offset = info->pc() - new_code->instruction_start() -
1906 new_code_const_pool_size - debug_break_slot_bytes;
1907 if (new_offset >= frame_offset) {
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001908 break;
1909 }
1910
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001911 if (RelocInfo::IsDebugBreakSlot(info->rmode())) {
1912 debug_break_slot_bytes += Assembler::kDebugBreakSlotLength;
1913 } else {
1914 ASSERT(RelocInfo::IsConstPool(info->rmode()));
1915 // The size of the constant pool is encoded in the data.
1916 new_code_const_pool_size += static_cast<int>(info->data());
1917 }
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001918 }
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001919
1920 // Compute the equivalent pc in the new code.
1921 byte* new_pc = new_code->instruction_start() + frame_offset +
1922 debug_break_slot_bytes + new_code_const_pool_size;
1923
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001924 if (FLAG_trace_deopt) {
1925 PrintF("Replacing code %08" V8PRIxPTR " - %08" V8PRIxPTR " (%d) "
1926 "with %08" V8PRIxPTR " - %08" V8PRIxPTR " (%d) "
1927 "for debugging, "
1928 "changing pc from %08" V8PRIxPTR " to %08" V8PRIxPTR "\n",
1929 reinterpret_cast<intptr_t>(
1930 frame_code->instruction_start()),
1931 reinterpret_cast<intptr_t>(
1932 frame_code->instruction_start()) +
1933 frame_code->instruction_size(),
1934 frame_code->instruction_size(),
1935 reinterpret_cast<intptr_t>(new_code->instruction_start()),
1936 reinterpret_cast<intptr_t>(new_code->instruction_start()) +
1937 new_code->instruction_size(),
1938 new_code->instruction_size(),
1939 reinterpret_cast<intptr_t>(frame->pc()),
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001940 reinterpret_cast<intptr_t>(new_pc));
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001941 }
1942
1943 // Patch the return address to return into the code with
1944 // debug break slots.
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001945 frame->set_pc(new_pc);
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001946 }
1947}
1948
1949
1950class ActiveFunctionsCollector : public ThreadVisitor {
1951 public:
1952 explicit ActiveFunctionsCollector(List<Handle<JSFunction> >* active_functions,
1953 Object* active_code_marker)
1954 : active_functions_(active_functions),
1955 active_code_marker_(active_code_marker) { }
1956
1957 void VisitThread(Isolate* isolate, ThreadLocalTop* top) {
1958 CollectActiveFunctionsFromThread(isolate,
1959 top,
1960 active_functions_,
1961 active_code_marker_);
1962 }
1963
1964 private:
1965 List<Handle<JSFunction> >* active_functions_;
1966 Object* active_code_marker_;
1967};
1968
1969
1970class ActiveFunctionsRedirector : public ThreadVisitor {
1971 public:
1972 void VisitThread(Isolate* isolate, ThreadLocalTop* top) {
1973 RedirectActivationsToRecompiledCodeOnThread(isolate, top);
1974 }
1975};
1976
1977
lrn@chromium.org34e60782011-09-15 07:25:40 +00001978void Debug::PrepareForBreakPoints() {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001979 // If preparing for the first break point make sure to deoptimize all
1980 // functions as debugging does not work with optimized code.
1981 if (!has_break_points_) {
1982 Deoptimizer::DeoptimizeAll();
lrn@chromium.org34e60782011-09-15 07:25:40 +00001983
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001984 Handle<Code> lazy_compile =
1985 Handle<Code>(isolate_->builtins()->builtin(Builtins::kLazyCompile));
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001986
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001987 // There will be at least one break point when we are done.
1988 has_break_points_ = true;
1989
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001990 // Keep the list of activated functions in a handlified list as it
1991 // is used both in GC and non-GC code.
1992 List<Handle<JSFunction> > active_functions(100);
lrn@chromium.org34e60782011-09-15 07:25:40 +00001993
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001994 {
1995 // We are going to iterate heap to find all functions without
1996 // debug break slots.
rossberg@chromium.org994edf62012-02-06 10:12:55 +00001997 isolate_->heap()->CollectAllGarbage(Heap::kMakeHeapIterableMask,
1998 "preparing for breakpoints");
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001999
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002000 // Ensure no GC in this scope as we are going to use gc_metadata
2001 // field in the Code object to mark active functions.
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002002 AssertNoAllocation no_allocation;
2003
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002004 Object* active_code_marker = isolate_->heap()->the_hole_value();
svenpanne@chromium.orgecb9dd62011-12-01 08:22:35 +00002005
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002006 CollectActiveFunctionsFromThread(isolate_,
2007 isolate_->thread_local_top(),
2008 &active_functions,
2009 active_code_marker);
2010 ActiveFunctionsCollector active_functions_collector(&active_functions,
2011 active_code_marker);
2012 isolate_->thread_manager()->IterateArchivedThreads(
2013 &active_functions_collector);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002014
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002015 // Scan the heap for all non-optimized functions which have no
2016 // debug break slots and are not active or inlined into an active
2017 // function and mark them for lazy compilation.
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002018 HeapIterator iterator;
2019 HeapObject* obj = NULL;
2020 while (((obj = iterator.next()) != NULL)) {
2021 if (obj->IsJSFunction()) {
2022 JSFunction* function = JSFunction::cast(obj);
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002023 SharedFunctionInfo* shared = function->shared();
2024 if (shared->allows_lazy_compilation() &&
2025 shared->script()->IsScript() &&
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002026 function->code()->kind() == Code::FUNCTION &&
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002027 !function->code()->has_debug_break_slots() &&
2028 shared->code()->gc_metadata() != active_code_marker) {
2029 function->set_code(*lazy_compile);
2030 function->shared()->set_code(*lazy_compile);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002031 }
2032 }
lrn@chromium.org34e60782011-09-15 07:25:40 +00002033 }
lrn@chromium.org34e60782011-09-15 07:25:40 +00002034
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002035 // Clear gc_metadata field.
2036 for (int i = 0; i < active_functions.length(); i++) {
2037 Handle<JSFunction> function = active_functions[i];
2038 function->shared()->code()->set_gc_metadata(Smi::FromInt(0));
2039 }
2040 }
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002041
2042 // Now recompile all functions with activation frames and and
2043 // patch the return address to run in the new compiled code.
2044 for (int i = 0; i < active_functions.length(); i++) {
2045 Handle<JSFunction> function = active_functions[i];
mmassi@chromium.org7028c052012-06-13 11:51:58 +00002046 Handle<SharedFunctionInfo> shared(function->shared());
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002047
2048 if (function->code()->kind() == Code::FUNCTION &&
2049 function->code()->has_debug_break_slots()) {
2050 // Nothing to do. Function code already had debug break slots.
2051 continue;
2052 }
2053
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002054 // If recompilation is not possible just skip it.
2055 if (shared->is_toplevel() ||
2056 !shared->allows_lazy_compilation() ||
2057 shared->code()->kind() == Code::BUILTIN) {
2058 continue;
2059 }
2060
2061 // Make sure that the shared full code is compiled with debug
2062 // break slots.
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00002063 if (!shared->code()->has_debug_break_slots()) {
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002064 // Try to compile the full code with debug break slots. If it
2065 // fails just keep the current code.
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00002066 Handle<Code> current_code(function->shared()->code());
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002067 shared->set_code(*lazy_compile);
2068 bool prev_force_debugger_active =
2069 isolate_->debugger()->force_debugger_active();
2070 isolate_->debugger()->set_force_debugger_active(true);
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00002071 ASSERT(current_code->kind() == Code::FUNCTION);
mmassi@chromium.org7028c052012-06-13 11:51:58 +00002072 CompileFullCodeForDebugging(function, current_code);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002073 isolate_->debugger()->set_force_debugger_active(
2074 prev_force_debugger_active);
2075 if (!shared->is_compiled()) {
2076 shared->set_code(*current_code);
2077 continue;
2078 }
2079 }
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002080
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002081 // Keep function code in sync with shared function info.
2082 function->set_code(shared->code());
lrn@chromium.org34e60782011-09-15 07:25:40 +00002083 }
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002084
2085 RedirectActivationsToRecompiledCodeOnThread(isolate_,
2086 isolate_->thread_local_top());
2087
2088 ActiveFunctionsRedirector active_functions_redirector;
2089 isolate_->thread_manager()->IterateArchivedThreads(
2090 &active_functions_redirector);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002091 }
lrn@chromium.org34e60782011-09-15 07:25:40 +00002092}
2093
2094
2095// Ensures the debug information is present for shared.
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00002096bool Debug::EnsureDebugInfo(Handle<SharedFunctionInfo> shared,
2097 Handle<JSFunction> function) {
lrn@chromium.org34e60782011-09-15 07:25:40 +00002098 // Return if we already have the debug info for shared.
2099 if (HasDebugInfo(shared)) {
2100 ASSERT(shared->is_compiled());
2101 return true;
2102 }
2103
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00002104 // There will be at least one break point when we are done.
2105 has_break_points_ = true;
2106
2107 // Ensure function is compiled. Return false if this failed.
2108 if (!function.is_null() &&
2109 !JSFunction::EnsureCompiled(function, CLEAR_EXCEPTION)) {
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002110 return false;
2111 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002112
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002113 // Create the debug info object.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002114 Handle<DebugInfo> debug_info = FACTORY->NewDebugInfo(shared);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002115
2116 // Add debug info to the list.
2117 DebugInfoListNode* node = new DebugInfoListNode(*debug_info);
2118 node->set_next(debug_info_list_);
2119 debug_info_list_ = node;
2120
kasper.lundbd3ec4e2008-07-09 11:06:54 +00002121 return true;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002122}
2123
2124
2125void Debug::RemoveDebugInfo(Handle<DebugInfo> debug_info) {
2126 ASSERT(debug_info_list_ != NULL);
2127 // Run through the debug info objects to find this one and remove it.
2128 DebugInfoListNode* prev = NULL;
2129 DebugInfoListNode* current = debug_info_list_;
2130 while (current != NULL) {
2131 if (*current->debug_info() == *debug_info) {
2132 // Unlink from list. If prev is NULL we are looking at the first element.
2133 if (prev == NULL) {
2134 debug_info_list_ = current->next();
2135 } else {
2136 prev->set_next(current->next());
2137 }
lrn@chromium.org7516f052011-03-30 08:52:27 +00002138 current->debug_info()->shared()->set_debug_info(
2139 isolate_->heap()->undefined_value());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002140 delete current;
2141
2142 // If there are no more debug info objects there are not more break
2143 // points.
2144 has_break_points_ = debug_info_list_ != NULL;
2145
2146 return;
2147 }
2148 // Move to next in list.
2149 prev = current;
2150 current = current->next();
2151 }
2152 UNREACHABLE();
2153}
2154
2155
2156void Debug::SetAfterBreakTarget(JavaScriptFrame* frame) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002157 HandleScope scope(isolate_);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002158
lrn@chromium.org34e60782011-09-15 07:25:40 +00002159 PrepareForBreakPoints();
2160
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002161 // Get the executing function in which the debug break occurred.
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00002162 Handle<JSFunction> function(JSFunction::cast(frame->function()));
2163 Handle<SharedFunctionInfo> shared(function->shared());
2164 if (!EnsureDebugInfo(shared, function)) {
kasper.lundbd3ec4e2008-07-09 11:06:54 +00002165 // Return if we failed to retrieve the debug info.
2166 return;
2167 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002168 Handle<DebugInfo> debug_info = GetDebugInfo(shared);
2169 Handle<Code> code(debug_info->code());
2170 Handle<Code> original_code(debug_info->original_code());
2171#ifdef DEBUG
2172 // Get the code which is actually executing.
vegorov@chromium.org74f333b2011-04-06 11:17:46 +00002173 Handle<Code> frame_code(frame->LookupCode());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002174 ASSERT(frame_code.is_identical_to(code));
2175#endif
2176
2177 // Find the call address in the running code. This address holds the call to
2178 // either a DebugBreakXXX or to the debug break return entry code if the
2179 // break point is still active after processing the break point.
ager@chromium.org4af710e2009-09-15 12:20:11 +00002180 Address addr = frame->pc() - Assembler::kCallTargetAddressOffset;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002181
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002182 // Check if the location is at JS exit or debug break slot.
ager@chromium.orga1645e22009-09-09 19:27:10 +00002183 bool at_js_return = false;
2184 bool break_at_js_return_active = false;
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002185 bool at_debug_break_slot = false;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002186 RelocIterator it(debug_info->code());
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002187 while (!it.done() && !at_js_return && !at_debug_break_slot) {
ager@chromium.org236ad962008-09-25 09:45:57 +00002188 if (RelocInfo::IsJSReturn(it.rinfo()->rmode())) {
ager@chromium.orga1645e22009-09-09 19:27:10 +00002189 at_js_return = (it.rinfo()->pc() ==
2190 addr - Assembler::kPatchReturnSequenceAddressOffset);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002191 break_at_js_return_active = it.rinfo()->IsPatchedReturnSequence();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002192 }
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002193 if (RelocInfo::IsDebugBreakSlot(it.rinfo()->rmode())) {
2194 at_debug_break_slot = (it.rinfo()->pc() ==
2195 addr - Assembler::kPatchDebugBreakSlotAddressOffset);
2196 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002197 it.next();
2198 }
2199
2200 // Handle the jump to continue execution after break point depending on the
2201 // break location.
ager@chromium.orga1645e22009-09-09 19:27:10 +00002202 if (at_js_return) {
2203 // If the break point as return is still active jump to the corresponding
2204 // place in the original code. If not the break point was removed during
2205 // break point processing.
2206 if (break_at_js_return_active) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002207 addr += original_code->instruction_start() - code->instruction_start();
2208 }
2209
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00002210 // Move back to where the call instruction sequence started.
2211 thread_local_.after_break_target_ =
2212 addr - Assembler::kPatchReturnSequenceAddressOffset;
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002213 } else if (at_debug_break_slot) {
2214 // Address of where the debug break slot starts.
2215 addr = addr - Assembler::kPatchDebugBreakSlotAddressOffset;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002216
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002217 // Continue just after the slot.
2218 thread_local_.after_break_target_ = addr + Assembler::kDebugBreakSlotLength;
2219 } else if (IsDebugBreak(Assembler::target_address_at(addr))) {
2220 // We now know that there is still a debug break call at the target address,
2221 // so the break point is still there and the original code will hold the
2222 // address to jump to in order to complete the call which is replaced by a
2223 // call to DebugBreakXXX.
2224
2225 // Find the corresponding address in the original code.
2226 addr += original_code->instruction_start() - code->instruction_start();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002227
2228 // Install jump to the call address in the original code. This will be the
2229 // call which was overwritten by the call to DebugBreakXXX.
2230 thread_local_.after_break_target_ = Assembler::target_address_at(addr);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002231 } else {
2232 // There is no longer a break point present. Don't try to look in the
2233 // original code as the running code will have the right address. This takes
2234 // care of the case where the last break point is removed from the function
2235 // and therefore no "original code" is available.
2236 thread_local_.after_break_target_ = Assembler::target_address_at(addr);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002237 }
2238}
2239
2240
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00002241bool Debug::IsBreakAtReturn(JavaScriptFrame* frame) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002242 HandleScope scope(isolate_);
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00002243
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00002244 // If there are no break points this cannot be break at return, as
2245 // the debugger statement and stack guard bebug break cannot be at
2246 // return.
2247 if (!has_break_points_) {
2248 return false;
2249 }
2250
lrn@chromium.org34e60782011-09-15 07:25:40 +00002251 PrepareForBreakPoints();
2252
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00002253 // Get the executing function in which the debug break occurred.
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00002254 Handle<JSFunction> function(JSFunction::cast(frame->function()));
2255 Handle<SharedFunctionInfo> shared(function->shared());
2256 if (!EnsureDebugInfo(shared, function)) {
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00002257 // Return if we failed to retrieve the debug info.
2258 return false;
2259 }
2260 Handle<DebugInfo> debug_info = GetDebugInfo(shared);
2261 Handle<Code> code(debug_info->code());
2262#ifdef DEBUG
2263 // Get the code which is actually executing.
vegorov@chromium.org74f333b2011-04-06 11:17:46 +00002264 Handle<Code> frame_code(frame->LookupCode());
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00002265 ASSERT(frame_code.is_identical_to(code));
2266#endif
2267
2268 // Find the call address in the running code.
2269 Address addr = frame->pc() - Assembler::kCallTargetAddressOffset;
2270
2271 // Check if the location is at JS return.
2272 RelocIterator it(debug_info->code());
2273 while (!it.done()) {
2274 if (RelocInfo::IsJSReturn(it.rinfo()->rmode())) {
2275 return (it.rinfo()->pc() ==
2276 addr - Assembler::kPatchReturnSequenceAddressOffset);
2277 }
2278 it.next();
2279 }
2280 return false;
2281}
2282
2283
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00002284void Debug::FramesHaveBeenDropped(StackFrame::Id new_break_frame_id,
whesse@chromium.orge90029b2010-08-02 11:52:17 +00002285 FrameDropMode mode,
2286 Object** restarter_frame_function_pointer) {
ulan@chromium.orgd9e468a2012-06-25 09:47:40 +00002287 if (mode != CURRENTLY_SET_MODE) {
2288 thread_local_.frame_drop_mode_ = mode;
2289 }
ager@chromium.org357bf652010-04-12 11:30:10 +00002290 thread_local_.break_frame_id_ = new_break_frame_id;
whesse@chromium.orge90029b2010-08-02 11:52:17 +00002291 thread_local_.restarter_frame_function_pointer_ =
2292 restarter_frame_function_pointer;
ager@chromium.org357bf652010-04-12 11:30:10 +00002293}
2294
2295
jkummerow@chromium.org212d9642012-05-11 15:02:09 +00002296const int Debug::FramePaddingLayout::kInitialSize = 1;
2297
2298
2299// Any even value bigger than kInitialSize as needed for stack scanning.
2300const int Debug::FramePaddingLayout::kPaddingValue = kInitialSize + 1;
2301
2302
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002303bool Debug::IsDebugGlobal(GlobalObject* global) {
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00002304 return IsLoaded() && global == debug_context()->global_object();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002305}
2306
2307
ager@chromium.org32912102009-01-16 10:38:43 +00002308void Debug::ClearMirrorCache() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002309 PostponeInterruptsScope postpone(isolate_);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002310 HandleScope scope(isolate_);
2311 ASSERT(isolate_->context() == *Debug::debug_context());
ager@chromium.org32912102009-01-16 10:38:43 +00002312
2313 // Clear the mirror cache.
2314 Handle<String> function_name =
lrn@chromium.org7516f052011-03-30 08:52:27 +00002315 isolate_->factory()->LookupSymbol(CStrVector("ClearMirrorCache"));
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00002316 Handle<Object> fun(
2317 Isolate::Current()->global_object()->GetPropertyNoExceptionThrown(
lrn@chromium.org303ada72010-10-27 09:33:13 +00002318 *function_name));
ager@chromium.org32912102009-01-16 10:38:43 +00002319 ASSERT(fun->IsJSFunction());
2320 bool caught_exception;
rossberg@chromium.org717967f2011-07-20 13:44:42 +00002321 Execution::TryCall(Handle<JSFunction>::cast(fun),
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00002322 Handle<JSObject>(Debug::debug_context()->global_object()),
ager@chromium.org32912102009-01-16 10:38:43 +00002323 0, NULL, &caught_exception);
2324}
2325
2326
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002327void Debug::CreateScriptCache() {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002328 Heap* heap = isolate_->heap();
2329 HandleScope scope(isolate_);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002330
2331 // Perform two GCs to get rid of all unreferenced scripts. The first GC gets
2332 // rid of all the cached script wrappers and the second gets rid of the
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002333 // scripts which are no longer referenced. The second also sweeps precisely,
2334 // which saves us doing yet another GC to make the heap iterable.
rossberg@chromium.org994edf62012-02-06 10:12:55 +00002335 heap->CollectAllGarbage(Heap::kNoGCFlags, "Debug::CreateScriptCache");
2336 heap->CollectAllGarbage(Heap::kMakeHeapIterableMask,
2337 "Debug::CreateScriptCache");
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002338
2339 ASSERT(script_cache_ == NULL);
2340 script_cache_ = new ScriptCache();
2341
2342 // Scan heap for Script objects.
2343 int count = 0;
2344 HeapIterator iterator;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002345 AssertNoAllocation no_allocation;
2346
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002347 for (HeapObject* obj = iterator.next(); obj != NULL; obj = iterator.next()) {
sgjesse@chromium.org152a0b02009-10-07 13:50:16 +00002348 if (obj->IsScript() && Script::cast(obj)->HasValidSource()) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002349 script_cache_->Add(Handle<Script>(Script::cast(obj)));
2350 count++;
2351 }
2352 }
2353}
2354
2355
2356void Debug::DestroyScriptCache() {
2357 // Get rid of the script cache if it was created.
2358 if (script_cache_ != NULL) {
2359 delete script_cache_;
2360 script_cache_ = NULL;
2361 }
2362}
2363
2364
2365void Debug::AddScriptToScriptCache(Handle<Script> script) {
2366 if (script_cache_ != NULL) {
2367 script_cache_->Add(script);
2368 }
2369}
2370
2371
2372Handle<FixedArray> Debug::GetLoadedScripts() {
2373 // Create and fill the script cache when the loaded scripts is requested for
2374 // the first time.
2375 if (script_cache_ == NULL) {
2376 CreateScriptCache();
2377 }
2378
2379 // If the script cache is not active just return an empty array.
2380 ASSERT(script_cache_ != NULL);
2381 if (script_cache_ == NULL) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002382 isolate_->factory()->NewFixedArray(0);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002383 }
2384
2385 // Perform GC to get unreferenced scripts evicted from the cache before
2386 // returning the content.
rossberg@chromium.org994edf62012-02-06 10:12:55 +00002387 isolate_->heap()->CollectAllGarbage(Heap::kNoGCFlags,
2388 "Debug::GetLoadedScripts");
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002389
2390 // Get the scripts from the cache.
2391 return script_cache_->GetScripts();
2392}
2393
2394
2395void Debug::AfterGarbageCollection() {
2396 // Generate events for collected scripts.
2397 if (script_cache_ != NULL) {
2398 script_cache_->ProcessCollectedScripts();
2399 }
2400}
2401
2402
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00002403Debugger::Debugger(Isolate* isolate)
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +00002404 : debugger_access_(isolate->debugger_access()),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002405 event_listener_(Handle<Object>()),
2406 event_listener_data_(Handle<Object>()),
2407 compiling_natives_(false),
2408 is_loading_debugger_(false),
2409 never_unload_debugger_(false),
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002410 force_debugger_active_(false),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002411 message_handler_(NULL),
2412 debugger_unload_pending_(false),
2413 host_dispatch_handler_(NULL),
2414 dispatch_handler_access_(OS::CreateMutex()),
2415 debug_message_dispatch_handler_(NULL),
2416 message_dispatch_helper_thread_(NULL),
2417 host_dispatch_micros_(100 * 1000),
2418 agent_(NULL),
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00002419 command_queue_(isolate->logger(), kQueueInitialSize),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002420 command_received_(OS::CreateSemaphore(0)),
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00002421 event_command_queue_(isolate->logger(), kQueueInitialSize),
2422 isolate_(isolate) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002423}
2424
2425
2426Debugger::~Debugger() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002427 delete dispatch_handler_access_;
2428 dispatch_handler_access_ = 0;
2429 delete command_received_;
2430 command_received_ = 0;
2431}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002432
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002433
2434Handle<Object> Debugger::MakeJSObject(Vector<const char> constructor_name,
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002435 int argc,
2436 Handle<Object> argv[],
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002437 bool* caught_exception) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002438 ASSERT(isolate_->context() == *isolate_->debug()->debug_context());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002439
2440 // Create the execution state object.
lrn@chromium.org7516f052011-03-30 08:52:27 +00002441 Handle<String> constructor_str =
2442 isolate_->factory()->LookupSymbol(constructor_name);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002443 Handle<Object> constructor(
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00002444 isolate_->global_object()->GetPropertyNoExceptionThrown(
2445 *constructor_str));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002446 ASSERT(constructor->IsJSFunction());
2447 if (!constructor->IsJSFunction()) {
2448 *caught_exception = true;
lrn@chromium.org7516f052011-03-30 08:52:27 +00002449 return isolate_->factory()->undefined_value();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002450 }
2451 Handle<Object> js_object = Execution::TryCall(
2452 Handle<JSFunction>::cast(constructor),
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00002453 Handle<JSObject>(isolate_->debug()->debug_context()->global_object()),
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002454 argc,
2455 argv,
2456 caught_exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002457 return js_object;
2458}
2459
2460
2461Handle<Object> Debugger::MakeExecutionState(bool* caught_exception) {
2462 // Create the execution state object.
lrn@chromium.org7516f052011-03-30 08:52:27 +00002463 Handle<Object> break_id = isolate_->factory()->NewNumberFromInt(
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002464 isolate_->debug()->break_id());
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002465 Handle<Object> argv[] = { break_id };
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002466 return MakeJSObject(CStrVector("MakeExecutionState"),
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002467 ARRAY_SIZE(argv),
2468 argv,
2469 caught_exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002470}
2471
2472
2473Handle<Object> Debugger::MakeBreakEvent(Handle<Object> exec_state,
2474 Handle<Object> break_points_hit,
2475 bool* caught_exception) {
2476 // Create the new break event object.
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002477 Handle<Object> argv[] = { exec_state, break_points_hit };
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002478 return MakeJSObject(CStrVector("MakeBreakEvent"),
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002479 ARRAY_SIZE(argv),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002480 argv,
2481 caught_exception);
2482}
2483
2484
2485Handle<Object> Debugger::MakeExceptionEvent(Handle<Object> exec_state,
2486 Handle<Object> exception,
2487 bool uncaught,
2488 bool* caught_exception) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002489 Factory* factory = isolate_->factory();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002490 // Create the new exception event object.
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002491 Handle<Object> argv[] = { exec_state,
2492 exception,
2493 factory->ToBoolean(uncaught) };
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002494 return MakeJSObject(CStrVector("MakeExceptionEvent"),
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002495 ARRAY_SIZE(argv),
2496 argv,
2497 caught_exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002498}
2499
2500
2501Handle<Object> Debugger::MakeNewFunctionEvent(Handle<Object> function,
2502 bool* caught_exception) {
2503 // Create the new function event object.
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002504 Handle<Object> argv[] = { function };
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002505 return MakeJSObject(CStrVector("MakeNewFunctionEvent"),
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002506 ARRAY_SIZE(argv),
2507 argv,
2508 caught_exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002509}
2510
2511
2512Handle<Object> Debugger::MakeCompileEvent(Handle<Script> script,
iposva@chromium.org245aa852009-02-10 00:49:54 +00002513 bool before,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002514 bool* caught_exception) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002515 Factory* factory = isolate_->factory();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002516 // Create the compile event object.
2517 Handle<Object> exec_state = MakeExecutionState(caught_exception);
iposva@chromium.org245aa852009-02-10 00:49:54 +00002518 Handle<Object> script_wrapper = GetScriptWrapper(script);
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002519 Handle<Object> argv[] = { exec_state,
2520 script_wrapper,
2521 factory->ToBoolean(before) };
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002522 return MakeJSObject(CStrVector("MakeCompileEvent"),
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002523 ARRAY_SIZE(argv),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002524 argv,
2525 caught_exception);
2526}
2527
2528
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002529Handle<Object> Debugger::MakeScriptCollectedEvent(int id,
2530 bool* caught_exception) {
2531 // Create the script collected event object.
2532 Handle<Object> exec_state = MakeExecutionState(caught_exception);
2533 Handle<Object> id_object = Handle<Smi>(Smi::FromInt(id));
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002534 Handle<Object> argv[] = { exec_state, id_object };
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002535
2536 return MakeJSObject(CStrVector("MakeScriptCollectedEvent"),
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002537 ARRAY_SIZE(argv),
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002538 argv,
2539 caught_exception);
2540}
2541
2542
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002543void Debugger::OnException(Handle<Object> exception, bool uncaught) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002544 HandleScope scope(isolate_);
2545 Debug* debug = isolate_->debug();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002546
2547 // Bail out based on state or if there is no listener for this event
lrn@chromium.org7516f052011-03-30 08:52:27 +00002548 if (debug->InDebugger()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002549 if (!Debugger::EventActive(v8::Exception)) return;
2550
2551 // Bail out if exception breaks are not active
2552 if (uncaught) {
2553 // Uncaught exceptions are reported by either flags.
lrn@chromium.org7516f052011-03-30 08:52:27 +00002554 if (!(debug->break_on_uncaught_exception() ||
2555 debug->break_on_exception())) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002556 } else {
2557 // Caught exceptions are reported is activated.
lrn@chromium.org7516f052011-03-30 08:52:27 +00002558 if (!debug->break_on_exception()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002559 }
2560
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002561 // Enter the debugger.
2562 EnterDebugger debugger;
2563 if (debugger.FailedToEnter()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002564
2565 // Clear all current stepping setup.
lrn@chromium.org7516f052011-03-30 08:52:27 +00002566 debug->ClearStepping();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002567 // Create the event data object.
2568 bool caught_exception = false;
2569 Handle<Object> exec_state = MakeExecutionState(&caught_exception);
2570 Handle<Object> event_data;
2571 if (!caught_exception) {
2572 event_data = MakeExceptionEvent(exec_state, exception, uncaught,
2573 &caught_exception);
2574 }
2575 // Bail out and don't call debugger if exception.
2576 if (caught_exception) {
2577 return;
2578 }
2579
ager@chromium.org5ec48922009-05-05 07:25:34 +00002580 // Process debug event.
2581 ProcessDebugEvent(v8::Exception, Handle<JSObject>::cast(event_data), false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002582 // Return to continue execution from where the exception was thrown.
2583}
2584
2585
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002586void Debugger::OnDebugBreak(Handle<Object> break_points_hit,
2587 bool auto_continue) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002588 HandleScope scope(isolate_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002589
kasper.lund212ac232008-07-16 07:07:30 +00002590 // Debugger has already been entered by caller.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002591 ASSERT(isolate_->context() == *isolate_->debug()->debug_context());
kasper.lund212ac232008-07-16 07:07:30 +00002592
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002593 // Bail out if there is no listener for this event
2594 if (!Debugger::EventActive(v8::Break)) return;
2595
2596 // Debugger must be entered in advance.
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00002597 ASSERT(isolate_->context() == *isolate_->debug()->debug_context());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002598
2599 // Create the event data object.
2600 bool caught_exception = false;
2601 Handle<Object> exec_state = MakeExecutionState(&caught_exception);
2602 Handle<Object> event_data;
2603 if (!caught_exception) {
2604 event_data = MakeBreakEvent(exec_state, break_points_hit,
2605 &caught_exception);
2606 }
2607 // Bail out and don't call debugger if exception.
2608 if (caught_exception) {
2609 return;
2610 }
2611
ager@chromium.org5ec48922009-05-05 07:25:34 +00002612 // Process debug event.
2613 ProcessDebugEvent(v8::Break,
2614 Handle<JSObject>::cast(event_data),
2615 auto_continue);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002616}
2617
2618
2619void Debugger::OnBeforeCompile(Handle<Script> script) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002620 HandleScope scope(isolate_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002621
2622 // Bail out based on state or if there is no listener for this event
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002623 if (isolate_->debug()->InDebugger()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002624 if (compiling_natives()) return;
2625 if (!EventActive(v8::BeforeCompile)) return;
2626
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002627 // Enter the debugger.
2628 EnterDebugger debugger;
2629 if (debugger.FailedToEnter()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002630
2631 // Create the event data object.
2632 bool caught_exception = false;
iposva@chromium.org245aa852009-02-10 00:49:54 +00002633 Handle<Object> event_data = MakeCompileEvent(script, true, &caught_exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002634 // Bail out and don't call debugger if exception.
2635 if (caught_exception) {
2636 return;
2637 }
2638
ager@chromium.org5ec48922009-05-05 07:25:34 +00002639 // Process debug event.
2640 ProcessDebugEvent(v8::BeforeCompile,
2641 Handle<JSObject>::cast(event_data),
2642 true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002643}
2644
2645
2646// Handle debugger actions when a new script is compiled.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00002647void Debugger::OnAfterCompile(Handle<Script> script,
2648 AfterCompileFlags after_compile_flags) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002649 HandleScope scope(isolate_);
2650 Debug* debug = isolate_->debug();
kasper.lund212ac232008-07-16 07:07:30 +00002651
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002652 // Add the newly compiled script to the script cache.
lrn@chromium.org7516f052011-03-30 08:52:27 +00002653 debug->AddScriptToScriptCache(script);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002654
2655 // No more to do if not debugging.
ager@chromium.org71daaf62009-04-01 07:22:49 +00002656 if (!IsDebuggerActive()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002657
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002658 // No compile events while compiling natives.
2659 if (compiling_natives()) return;
2660
iposva@chromium.org245aa852009-02-10 00:49:54 +00002661 // Store whether in debugger before entering debugger.
lrn@chromium.org7516f052011-03-30 08:52:27 +00002662 bool in_debugger = debug->InDebugger();
iposva@chromium.org245aa852009-02-10 00:49:54 +00002663
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002664 // Enter the debugger.
2665 EnterDebugger debugger;
2666 if (debugger.FailedToEnter()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002667
2668 // If debugging there might be script break points registered for this
2669 // script. Make sure that these break points are set.
2670
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002671 // Get the function UpdateScriptBreakPoints (defined in debug-debugger.js).
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002672 Handle<String> update_script_break_points_symbol =
lrn@chromium.org7516f052011-03-30 08:52:27 +00002673 isolate_->factory()->LookupAsciiSymbol("UpdateScriptBreakPoints");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002674 Handle<Object> update_script_break_points =
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00002675 Handle<Object>(debug->debug_context()->global_object()->
lrn@chromium.org303ada72010-10-27 09:33:13 +00002676 GetPropertyNoExceptionThrown(*update_script_break_points_symbol));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002677 if (!update_script_break_points->IsJSFunction()) {
2678 return;
2679 }
2680 ASSERT(update_script_break_points->IsJSFunction());
2681
2682 // Wrap the script object in a proper JS object before passing it
2683 // to JavaScript.
2684 Handle<JSValue> wrapper = GetScriptWrapper(script);
2685
2686 // Call UpdateScriptBreakPoints expect no exceptions.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002687 bool caught_exception;
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002688 Handle<Object> argv[] = { wrapper };
rossberg@chromium.org717967f2011-07-20 13:44:42 +00002689 Execution::TryCall(Handle<JSFunction>::cast(update_script_break_points),
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002690 Isolate::Current()->js_builtins_object(),
2691 ARRAY_SIZE(argv),
2692 argv,
2693 &caught_exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002694 if (caught_exception) {
2695 return;
2696 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002697 // Bail out based on state or if there is no listener for this event
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00002698 if (in_debugger && (after_compile_flags & SEND_WHEN_DEBUGGING) == 0) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002699 if (!Debugger::EventActive(v8::AfterCompile)) return;
2700
2701 // Create the compile state object.
2702 Handle<Object> event_data = MakeCompileEvent(script,
iposva@chromium.org245aa852009-02-10 00:49:54 +00002703 false,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002704 &caught_exception);
2705 // Bail out and don't call debugger if exception.
2706 if (caught_exception) {
2707 return;
2708 }
ager@chromium.org5ec48922009-05-05 07:25:34 +00002709 // Process debug event.
2710 ProcessDebugEvent(v8::AfterCompile,
2711 Handle<JSObject>::cast(event_data),
2712 true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002713}
2714
2715
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002716void Debugger::OnScriptCollected(int id) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002717 HandleScope scope(isolate_);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002718
2719 // No more to do if not debugging.
2720 if (!IsDebuggerActive()) return;
2721 if (!Debugger::EventActive(v8::ScriptCollected)) return;
2722
2723 // Enter the debugger.
2724 EnterDebugger debugger;
2725 if (debugger.FailedToEnter()) return;
2726
2727 // Create the script collected state object.
2728 bool caught_exception = false;
2729 Handle<Object> event_data = MakeScriptCollectedEvent(id,
2730 &caught_exception);
2731 // Bail out and don't call debugger if exception.
2732 if (caught_exception) {
2733 return;
2734 }
2735
2736 // Process debug event.
2737 ProcessDebugEvent(v8::ScriptCollected,
2738 Handle<JSObject>::cast(event_data),
2739 true);
2740}
2741
2742
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002743void Debugger::ProcessDebugEvent(v8::DebugEvent event,
ager@chromium.org5ec48922009-05-05 07:25:34 +00002744 Handle<JSObject> event_data,
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002745 bool auto_continue) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002746 HandleScope scope(isolate_);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002747
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +00002748 // Clear any pending debug break if this is a real break.
2749 if (!auto_continue) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002750 isolate_->debug()->clear_interrupt_pending(DEBUGBREAK);
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +00002751 }
2752
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002753 // Create the execution state.
2754 bool caught_exception = false;
2755 Handle<Object> exec_state = MakeExecutionState(&caught_exception);
2756 if (caught_exception) {
2757 return;
2758 }
ager@chromium.org41826e72009-03-30 13:30:57 +00002759 // First notify the message handler if any.
2760 if (message_handler_ != NULL) {
ager@chromium.org5ec48922009-05-05 07:25:34 +00002761 NotifyMessageHandler(event,
2762 Handle<JSObject>::cast(exec_state),
2763 event_data,
2764 auto_continue);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002765 }
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00002766 // Notify registered debug event listener. This can be either a C or
2767 // a JavaScript function. Don't call event listener for v8::Break
2768 // here, if it's only a debug command -- they will be processed later.
2769 if ((event != v8::Break || !auto_continue) && !event_listener_.is_null()) {
2770 CallEventCallback(event, exec_state, event_data, NULL);
2771 }
2772 // Process pending debug commands.
2773 if (event == v8::Break) {
2774 while (!event_command_queue_.IsEmpty()) {
2775 CommandMessage command = event_command_queue_.Get();
2776 if (!event_listener_.is_null()) {
2777 CallEventCallback(v8::BreakForCommand,
2778 exec_state,
2779 event_data,
2780 command.client_data());
2781 }
2782 command.Dispose();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002783 }
2784 }
2785}
2786
2787
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00002788void Debugger::CallEventCallback(v8::DebugEvent event,
2789 Handle<Object> exec_state,
2790 Handle<Object> event_data,
2791 v8::Debug::ClientData* client_data) {
ager@chromium.orgea91cc52011-05-23 06:06:11 +00002792 if (event_listener_->IsForeign()) {
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00002793 CallCEventCallback(event, exec_state, event_data, client_data);
2794 } else {
2795 CallJSEventCallback(event, exec_state, event_data);
2796 }
2797}
2798
2799
2800void Debugger::CallCEventCallback(v8::DebugEvent event,
2801 Handle<Object> exec_state,
2802 Handle<Object> event_data,
2803 v8::Debug::ClientData* client_data) {
ager@chromium.orgea91cc52011-05-23 06:06:11 +00002804 Handle<Foreign> callback_obj(Handle<Foreign>::cast(event_listener_));
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00002805 v8::Debug::EventCallback2 callback =
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002806 FUNCTION_CAST<v8::Debug::EventCallback2>(
2807 callback_obj->foreign_address());
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00002808 EventDetailsImpl event_details(
2809 event,
2810 Handle<JSObject>::cast(exec_state),
2811 Handle<JSObject>::cast(event_data),
2812 event_listener_data_,
2813 client_data);
2814 callback(event_details);
2815}
2816
2817
2818void Debugger::CallJSEventCallback(v8::DebugEvent event,
2819 Handle<Object> exec_state,
2820 Handle<Object> event_data) {
2821 ASSERT(event_listener_->IsJSFunction());
2822 Handle<JSFunction> fun(Handle<JSFunction>::cast(event_listener_));
2823
2824 // Invoke the JavaScript debug event listener.
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002825 Handle<Object> argv[] = { Handle<Object>(Smi::FromInt(event)),
2826 exec_state,
2827 event_data,
2828 event_listener_data_ };
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002829 bool caught_exception;
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002830 Execution::TryCall(fun,
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00002831 isolate_->global_object(),
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002832 ARRAY_SIZE(argv),
2833 argv,
2834 &caught_exception);
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00002835 // Silently ignore exceptions from debug event listeners.
2836}
2837
2838
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00002839Handle<Context> Debugger::GetDebugContext() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002840 never_unload_debugger_ = true;
2841 EnterDebugger debugger;
2842 return isolate_->debug()->debug_context();
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00002843}
2844
2845
ager@chromium.org71daaf62009-04-01 07:22:49 +00002846void Debugger::UnloadDebugger() {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002847 Debug* debug = isolate_->debug();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002848
ager@chromium.org71daaf62009-04-01 07:22:49 +00002849 // Make sure that there are no breakpoints left.
lrn@chromium.org7516f052011-03-30 08:52:27 +00002850 debug->ClearAllBreakPoints();
ager@chromium.org71daaf62009-04-01 07:22:49 +00002851
2852 // Unload the debugger if feasible.
2853 if (!never_unload_debugger_) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002854 debug->Unload();
ager@chromium.org71daaf62009-04-01 07:22:49 +00002855 }
2856
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002857 // Clear the flag indicating that the debugger should be unloaded.
2858 debugger_unload_pending_ = false;
ager@chromium.org71daaf62009-04-01 07:22:49 +00002859}
2860
2861
ager@chromium.org41826e72009-03-30 13:30:57 +00002862void Debugger::NotifyMessageHandler(v8::DebugEvent event,
ager@chromium.org5ec48922009-05-05 07:25:34 +00002863 Handle<JSObject> exec_state,
2864 Handle<JSObject> event_data,
ager@chromium.org41826e72009-03-30 13:30:57 +00002865 bool auto_continue) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002866 HandleScope scope(isolate_);
ager@chromium.org41826e72009-03-30 13:30:57 +00002867
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002868 if (!isolate_->debug()->Load()) return;
ager@chromium.org41826e72009-03-30 13:30:57 +00002869
2870 // Process the individual events.
ager@chromium.org5ec48922009-05-05 07:25:34 +00002871 bool sendEventMessage = false;
ager@chromium.org41826e72009-03-30 13:30:57 +00002872 switch (event) {
2873 case v8::Break:
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00002874 case v8::BreakForCommand:
ager@chromium.org5ec48922009-05-05 07:25:34 +00002875 sendEventMessage = !auto_continue;
ager@chromium.org41826e72009-03-30 13:30:57 +00002876 break;
2877 case v8::Exception:
ager@chromium.org5ec48922009-05-05 07:25:34 +00002878 sendEventMessage = true;
ager@chromium.org41826e72009-03-30 13:30:57 +00002879 break;
2880 case v8::BeforeCompile:
2881 break;
2882 case v8::AfterCompile:
ager@chromium.org5ec48922009-05-05 07:25:34 +00002883 sendEventMessage = true;
ager@chromium.org41826e72009-03-30 13:30:57 +00002884 break;
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002885 case v8::ScriptCollected:
2886 sendEventMessage = true;
2887 break;
ager@chromium.org41826e72009-03-30 13:30:57 +00002888 case v8::NewFunction:
2889 break;
2890 default:
2891 UNREACHABLE();
2892 }
2893
ager@chromium.org5ec48922009-05-05 07:25:34 +00002894 // The debug command interrupt flag might have been set when the command was
2895 // added. It should be enough to clear the flag only once while we are in the
2896 // debugger.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002897 ASSERT(isolate_->debug()->InDebugger());
2898 isolate_->stack_guard()->Continue(DEBUGCOMMAND);
ager@chromium.org5ec48922009-05-05 07:25:34 +00002899
2900 // Notify the debugger that a debug event has occurred unless auto continue is
2901 // active in which case no event is send.
2902 if (sendEventMessage) {
2903 MessageImpl message = MessageImpl::NewEvent(
2904 event,
2905 auto_continue,
2906 Handle<JSObject>::cast(exec_state),
2907 Handle<JSObject>::cast(event_data));
2908 InvokeMessageHandler(message);
2909 }
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +00002910
2911 // If auto continue don't make the event cause a break, but process messages
2912 // in the queue if any. For script collected events don't even process
2913 // messages in the queue as the execution state might not be what is expected
2914 // by the client.
ager@chromium.org6ffc2172009-05-29 19:20:16 +00002915 if ((auto_continue && !HasCommands()) || event == v8::ScriptCollected) {
ager@chromium.org5ec48922009-05-05 07:25:34 +00002916 return;
2917 }
ager@chromium.org41826e72009-03-30 13:30:57 +00002918
ager@chromium.org41826e72009-03-30 13:30:57 +00002919 v8::TryCatch try_catch;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002920
2921 // DebugCommandProcessor goes here.
2922 v8::Local<v8::Object> cmd_processor;
2923 {
2924 v8::Local<v8::Object> api_exec_state =
2925 v8::Utils::ToLocal(Handle<JSObject>::cast(exec_state));
2926 v8::Local<v8::String> fun_name =
2927 v8::String::New("debugCommandProcessor");
2928 v8::Local<v8::Function> fun =
2929 v8::Function::Cast(*api_exec_state->Get(fun_name));
2930
2931 v8::Handle<v8::Boolean> running =
2932 auto_continue ? v8::True() : v8::False();
2933 static const int kArgc = 1;
2934 v8::Handle<Value> argv[kArgc] = { running };
2935 cmd_processor = v8::Object::Cast(*fun->Call(api_exec_state, kArgc, argv));
2936 if (try_catch.HasCaught()) {
2937 PrintLn(try_catch.Exception());
2938 return;
2939 }
ager@chromium.org41826e72009-03-30 13:30:57 +00002940 }
2941
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002942 bool running = auto_continue;
2943
ager@chromium.org41826e72009-03-30 13:30:57 +00002944 // Process requests from the debugger.
2945 while (true) {
2946 // Wait for new command in the queue.
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002947 if (Debugger::host_dispatch_handler_) {
2948 // In case there is a host dispatch - do periodic dispatches.
2949 if (!command_received_->Wait(host_dispatch_micros_)) {
2950 // Timout expired, do the dispatch.
2951 Debugger::host_dispatch_handler_();
2952 continue;
2953 }
2954 } else {
2955 // In case there is no host dispatch - just wait.
2956 command_received_->Wait();
2957 }
ager@chromium.org41826e72009-03-30 13:30:57 +00002958
ager@chromium.org41826e72009-03-30 13:30:57 +00002959 // Get the command from the queue.
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00002960 CommandMessage command = command_queue_.Get();
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00002961 isolate_->logger()->DebugTag(
2962 "Got request from command queue, in interactive loop.");
ager@chromium.org71daaf62009-04-01 07:22:49 +00002963 if (!Debugger::IsDebuggerActive()) {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002964 // Delete command text and user data.
2965 command.Dispose();
ager@chromium.org41826e72009-03-30 13:30:57 +00002966 return;
2967 }
2968
ager@chromium.org41826e72009-03-30 13:30:57 +00002969 // Invoke JavaScript to process the debug request.
2970 v8::Local<v8::String> fun_name;
2971 v8::Local<v8::Function> fun;
2972 v8::Local<v8::Value> request;
2973 v8::TryCatch try_catch;
2974 fun_name = v8::String::New("processDebugRequest");
2975 fun = v8::Function::Cast(*cmd_processor->Get(fun_name));
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002976
2977 request = v8::String::New(command.text().start(),
2978 command.text().length());
ager@chromium.org41826e72009-03-30 13:30:57 +00002979 static const int kArgc = 1;
2980 v8::Handle<Value> argv[kArgc] = { request };
2981 v8::Local<v8::Value> response_val = fun->Call(cmd_processor, kArgc, argv);
2982
2983 // Get the response.
2984 v8::Local<v8::String> response;
ager@chromium.org41826e72009-03-30 13:30:57 +00002985 if (!try_catch.HasCaught()) {
2986 // Get response string.
2987 if (!response_val->IsUndefined()) {
2988 response = v8::String::Cast(*response_val);
2989 } else {
2990 response = v8::String::New("");
2991 }
2992
2993 // Log the JSON request/response.
2994 if (FLAG_trace_debug_json) {
2995 PrintLn(request);
2996 PrintLn(response);
2997 }
2998
2999 // Get the running state.
3000 fun_name = v8::String::New("isRunning");
3001 fun = v8::Function::Cast(*cmd_processor->Get(fun_name));
3002 static const int kArgc = 1;
3003 v8::Handle<Value> argv[kArgc] = { response };
3004 v8::Local<v8::Value> running_val = fun->Call(cmd_processor, kArgc, argv);
3005 if (!try_catch.HasCaught()) {
3006 running = running_val->ToBoolean()->Value();
3007 }
3008 } else {
3009 // In case of failure the result text is the exception text.
3010 response = try_catch.Exception()->ToString();
3011 }
3012
ager@chromium.org41826e72009-03-30 13:30:57 +00003013 // Return the result.
ager@chromium.org5ec48922009-05-05 07:25:34 +00003014 MessageImpl message = MessageImpl::NewResponse(
3015 event,
3016 running,
3017 Handle<JSObject>::cast(exec_state),
3018 Handle<JSObject>::cast(event_data),
3019 Handle<String>(Utils::OpenHandle(*response)),
3020 command.client_data());
3021 InvokeMessageHandler(message);
3022 command.Dispose();
ager@chromium.org41826e72009-03-30 13:30:57 +00003023
3024 // Return from debug event processing if either the VM is put into the
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00003025 // running state (through a continue command) or auto continue is active
ager@chromium.org41826e72009-03-30 13:30:57 +00003026 // and there are no more commands queued.
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003027 if (running && !HasCommands()) {
ager@chromium.org41826e72009-03-30 13:30:57 +00003028 return;
3029 }
3030 }
3031}
3032
3033
iposva@chromium.org245aa852009-02-10 00:49:54 +00003034void Debugger::SetEventListener(Handle<Object> callback,
3035 Handle<Object> data) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00003036 HandleScope scope(isolate_);
3037 GlobalHandles* global_handles = isolate_->global_handles();
iposva@chromium.org245aa852009-02-10 00:49:54 +00003038
3039 // Clear the global handles for the event listener and the event listener data
3040 // object.
3041 if (!event_listener_.is_null()) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00003042 global_handles->Destroy(
iposva@chromium.org245aa852009-02-10 00:49:54 +00003043 reinterpret_cast<Object**>(event_listener_.location()));
3044 event_listener_ = Handle<Object>();
3045 }
3046 if (!event_listener_data_.is_null()) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00003047 global_handles->Destroy(
iposva@chromium.org245aa852009-02-10 00:49:54 +00003048 reinterpret_cast<Object**>(event_listener_data_.location()));
3049 event_listener_data_ = Handle<Object>();
3050 }
3051
3052 // If there is a new debug event listener register it together with its data
3053 // object.
3054 if (!callback->IsUndefined() && !callback->IsNull()) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003055 event_listener_ = Handle<Object>::cast(
lrn@chromium.org7516f052011-03-30 08:52:27 +00003056 global_handles->Create(*callback));
iposva@chromium.org245aa852009-02-10 00:49:54 +00003057 if (data.is_null()) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00003058 data = isolate_->factory()->undefined_value();
iposva@chromium.org245aa852009-02-10 00:49:54 +00003059 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003060 event_listener_data_ = Handle<Object>::cast(
lrn@chromium.org7516f052011-03-30 08:52:27 +00003061 global_handles->Create(*data));
iposva@chromium.org245aa852009-02-10 00:49:54 +00003062 }
3063
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003064 ListenersChanged();
iposva@chromium.org245aa852009-02-10 00:49:54 +00003065}
3066
3067
ager@chromium.org5ec48922009-05-05 07:25:34 +00003068void Debugger::SetMessageHandler(v8::Debug::MessageHandler2 handler) {
ager@chromium.org71daaf62009-04-01 07:22:49 +00003069 ScopedLock with(debugger_access_);
3070
ager@chromium.org381abbb2009-02-25 13:23:22 +00003071 message_handler_ = handler;
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003072 ListenersChanged();
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003073 if (handler == NULL) {
ager@chromium.org71daaf62009-04-01 07:22:49 +00003074 // Send an empty command to the debugger if in a break to make JavaScript
3075 // run again if the debugger is closed.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003076 if (isolate_->debug()->InDebugger()) {
ager@chromium.org71daaf62009-04-01 07:22:49 +00003077 ProcessCommand(Vector<const uint16_t>::empty());
3078 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003079 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003080}
3081
3082
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003083void Debugger::ListenersChanged() {
3084 if (IsDebuggerActive()) {
3085 // Disable the compilation cache when the debugger is active.
lrn@chromium.org7516f052011-03-30 08:52:27 +00003086 isolate_->compilation_cache()->Disable();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003087 debugger_unload_pending_ = false;
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003088 } else {
lrn@chromium.org7516f052011-03-30 08:52:27 +00003089 isolate_->compilation_cache()->Enable();
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003090 // Unload the debugger if event listener and message handler cleared.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003091 // Schedule this for later, because we may be in non-V8 thread.
3092 debugger_unload_pending_ = true;
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003093 }
3094}
3095
3096
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003097void Debugger::SetHostDispatchHandler(v8::Debug::HostDispatchHandler handler,
3098 int period) {
ager@chromium.org381abbb2009-02-25 13:23:22 +00003099 host_dispatch_handler_ = handler;
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003100 host_dispatch_micros_ = period * 1000;
ager@chromium.org381abbb2009-02-25 13:23:22 +00003101}
3102
3103
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003104void Debugger::SetDebugMessageDispatchHandler(
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003105 v8::Debug::DebugMessageDispatchHandler handler, bool provide_locker) {
3106 ScopedLock with(dispatch_handler_access_);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003107 debug_message_dispatch_handler_ = handler;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003108
3109 if (provide_locker && message_dispatch_helper_thread_ == NULL) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003110 message_dispatch_helper_thread_ = new MessageDispatchHelperThread(isolate_);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003111 message_dispatch_helper_thread_->Start();
3112 }
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003113}
3114
3115
ager@chromium.org41826e72009-03-30 13:30:57 +00003116// Calls the registered debug message handler. This callback is part of the
ager@chromium.org5ec48922009-05-05 07:25:34 +00003117// public API.
3118void Debugger::InvokeMessageHandler(MessageImpl message) {
ager@chromium.org71daaf62009-04-01 07:22:49 +00003119 ScopedLock with(debugger_access_);
3120
ager@chromium.org381abbb2009-02-25 13:23:22 +00003121 if (message_handler_ != NULL) {
ager@chromium.org5ec48922009-05-05 07:25:34 +00003122 message_handler_(message);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003123 }
ager@chromium.org41826e72009-03-30 13:30:57 +00003124}
3125
3126
3127// Puts a command coming from the public API on the queue. Creates
3128// a copy of the command string managed by the debugger. Up to this
3129// point, the command data was managed by the API client. Called
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003130// by the API client thread.
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003131void Debugger::ProcessCommand(Vector<const uint16_t> command,
3132 v8::Debug::ClientData* client_data) {
3133 // Need to cast away const.
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003134 CommandMessage message = CommandMessage::New(
ager@chromium.org41826e72009-03-30 13:30:57 +00003135 Vector<uint16_t>(const_cast<uint16_t*>(command.start()),
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003136 command.length()),
3137 client_data);
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00003138 isolate_->logger()->DebugTag("Put command on command_queue.");
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003139 command_queue_.Put(message);
ager@chromium.org41826e72009-03-30 13:30:57 +00003140 command_received_->Signal();
sgjesse@chromium.org3afc1582009-04-16 22:31:44 +00003141
3142 // Set the debug command break flag to have the command processed.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003143 if (!isolate_->debug()->InDebugger()) {
3144 isolate_->stack_guard()->DebugCommand();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003145 }
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003146
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003147 MessageDispatchHelperThread* dispatch_thread;
3148 {
3149 ScopedLock with(dispatch_handler_access_);
3150 dispatch_thread = message_dispatch_helper_thread_;
3151 }
3152
3153 if (dispatch_thread == NULL) {
3154 CallMessageDispatchHandler();
3155 } else {
3156 dispatch_thread->Schedule();
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003157 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003158}
3159
3160
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003161bool Debugger::HasCommands() {
ager@chromium.org41826e72009-03-30 13:30:57 +00003162 return !command_queue_.IsEmpty();
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003163}
3164
3165
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00003166void Debugger::EnqueueDebugCommand(v8::Debug::ClientData* client_data) {
3167 CommandMessage message = CommandMessage::New(Vector<uint16_t>(), client_data);
3168 event_command_queue_.Put(message);
3169
3170 // Set the debug command break flag to have the command processed.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003171 if (!isolate_->debug()->InDebugger()) {
3172 isolate_->stack_guard()->DebugCommand();
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00003173 }
3174}
3175
3176
ager@chromium.org71daaf62009-04-01 07:22:49 +00003177bool Debugger::IsDebuggerActive() {
3178 ScopedLock with(debugger_access_);
3179
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00003180 return message_handler_ != NULL ||
3181 !event_listener_.is_null() ||
3182 force_debugger_active_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003183}
3184
3185
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003186Handle<Object> Debugger::Call(Handle<JSFunction> fun,
3187 Handle<Object> data,
3188 bool* pending_exception) {
ager@chromium.org71daaf62009-04-01 07:22:49 +00003189 // When calling functions in the debugger prevent it from beeing unloaded.
3190 Debugger::never_unload_debugger_ = true;
3191
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003192 // Enter the debugger.
3193 EnterDebugger debugger;
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +00003194 if (debugger.FailedToEnter()) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00003195 return isolate_->factory()->undefined_value();
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003196 }
3197
3198 // Create the execution state.
3199 bool caught_exception = false;
3200 Handle<Object> exec_state = MakeExecutionState(&caught_exception);
3201 if (caught_exception) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00003202 return isolate_->factory()->undefined_value();
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003203 }
3204
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00003205 Handle<Object> argv[] = { exec_state, data };
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +00003206 Handle<Object> result = Execution::Call(
3207 fun,
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003208 Handle<Object>(isolate_->debug()->debug_context_->global_proxy()),
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00003209 ARRAY_SIZE(argv),
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +00003210 argv,
3211 pending_exception);
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003212 return result;
3213}
3214
3215
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003216static void StubMessageHandler2(const v8::Debug::Message& message) {
3217 // Simply ignore message.
3218}
3219
3220
3221bool Debugger::StartAgent(const char* name, int port,
3222 bool wait_for_connection) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003223 ASSERT(Isolate::Current() == isolate_);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003224 if (wait_for_connection) {
3225 // Suspend V8 if it is already running or set V8 to suspend whenever
3226 // it starts.
3227 // Provide stub message handler; V8 auto-continues each suspend
3228 // when there is no message handler; we doesn't need it.
3229 // Once become suspended, V8 will stay so indefinitely long, until remote
3230 // debugger connects and issues "continue" command.
3231 Debugger::message_handler_ = StubMessageHandler2;
3232 v8::Debug::DebugBreak();
3233 }
3234
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00003235 if (Socket::SetUp()) {
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00003236 if (agent_ == NULL) {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00003237 agent_ = new DebuggerAgent(name, port);
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00003238 agent_->Start();
3239 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003240 return true;
3241 }
3242
3243 return false;
3244}
3245
3246
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003247void Debugger::StopAgent() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003248 ASSERT(Isolate::Current() == isolate_);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003249 if (agent_ != NULL) {
3250 agent_->Shutdown();
3251 agent_->Join();
3252 delete agent_;
3253 agent_ = NULL;
3254 }
3255}
3256
3257
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003258void Debugger::WaitForAgent() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003259 ASSERT(Isolate::Current() == isolate_);
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003260 if (agent_ != NULL)
3261 agent_->WaitUntilListening();
3262}
3263
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003264
3265void Debugger::CallMessageDispatchHandler() {
3266 v8::Debug::DebugMessageDispatchHandler handler;
3267 {
3268 ScopedLock with(dispatch_handler_access_);
3269 handler = Debugger::debug_message_dispatch_handler_;
3270 }
3271 if (handler != NULL) {
3272 handler();
3273 }
3274}
3275
3276
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003277EnterDebugger::EnterDebugger()
3278 : isolate_(Isolate::Current()),
3279 prev_(isolate_->debug()->debugger_entry()),
3280 it_(isolate_),
3281 has_js_frames_(!it_.done()),
3282 save_(isolate_) {
3283 Debug* debug = isolate_->debug();
3284 ASSERT(prev_ != NULL || !debug->is_interrupt_pending(PREEMPT));
3285 ASSERT(prev_ != NULL || !debug->is_interrupt_pending(DEBUGBREAK));
3286
3287 // Link recursive debugger entry.
3288 debug->set_debugger_entry(this);
3289
3290 // Store the previous break id and frame id.
3291 break_id_ = debug->break_id();
3292 break_frame_id_ = debug->break_frame_id();
3293
3294 // Create the new break info. If there is no JavaScript frames there is no
3295 // break frame id.
3296 if (has_js_frames_) {
3297 debug->NewBreak(it_.frame()->id());
3298 } else {
3299 debug->NewBreak(StackFrame::NO_ID);
3300 }
3301
3302 // Make sure that debugger is loaded and enter the debugger context.
3303 load_failed_ = !debug->Load();
3304 if (!load_failed_) {
3305 // NOTE the member variable save which saves the previous context before
3306 // this change.
3307 isolate_->set_context(*debug->debug_context());
3308 }
3309}
3310
3311
3312EnterDebugger::~EnterDebugger() {
3313 ASSERT(Isolate::Current() == isolate_);
3314 Debug* debug = isolate_->debug();
3315
3316 // Restore to the previous break state.
3317 debug->SetBreak(break_frame_id_, break_id_);
3318
3319 // Check for leaving the debugger.
fschneider@chromium.org7d10be52012-04-10 12:30:14 +00003320 if (!load_failed_ && prev_ == NULL) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003321 // Clear mirror cache when leaving the debugger. Skip this if there is a
3322 // pending exception as clearing the mirror cache calls back into
3323 // JavaScript. This can happen if the v8::Debug::Call is used in which
3324 // case the exception should end up in the calling code.
3325 if (!isolate_->has_pending_exception()) {
3326 // Try to avoid any pending debug break breaking in the clear mirror
3327 // cache JavaScript code.
3328 if (isolate_->stack_guard()->IsDebugBreak()) {
3329 debug->set_interrupts_pending(DEBUGBREAK);
3330 isolate_->stack_guard()->Continue(DEBUGBREAK);
3331 }
3332 debug->ClearMirrorCache();
3333 }
3334
3335 // Request preemption and debug break when leaving the last debugger entry
3336 // if any of these where recorded while debugging.
3337 if (debug->is_interrupt_pending(PREEMPT)) {
3338 // This re-scheduling of preemption is to avoid starvation in some
3339 // debugging scenarios.
3340 debug->clear_interrupt_pending(PREEMPT);
3341 isolate_->stack_guard()->Preempt();
3342 }
3343 if (debug->is_interrupt_pending(DEBUGBREAK)) {
3344 debug->clear_interrupt_pending(DEBUGBREAK);
3345 isolate_->stack_guard()->DebugBreak();
3346 }
3347
3348 // If there are commands in the queue when leaving the debugger request
3349 // that these commands are processed.
3350 if (isolate_->debugger()->HasCommands()) {
3351 isolate_->stack_guard()->DebugCommand();
3352 }
3353
3354 // If leaving the debugger with the debugger no longer active unload it.
3355 if (!isolate_->debugger()->IsDebuggerActive()) {
3356 isolate_->debugger()->UnloadDebugger();
3357 }
3358 }
3359
3360 // Leaving this debugger entry.
3361 debug->set_debugger_entry(prev_);
3362}
3363
3364
ager@chromium.org5ec48922009-05-05 07:25:34 +00003365MessageImpl MessageImpl::NewEvent(DebugEvent event,
3366 bool running,
3367 Handle<JSObject> exec_state,
3368 Handle<JSObject> event_data) {
3369 MessageImpl message(true, event, running,
3370 exec_state, event_data, Handle<String>(), NULL);
3371 return message;
3372}
3373
3374
3375MessageImpl MessageImpl::NewResponse(DebugEvent event,
3376 bool running,
3377 Handle<JSObject> exec_state,
3378 Handle<JSObject> event_data,
3379 Handle<String> response_json,
3380 v8::Debug::ClientData* client_data) {
3381 MessageImpl message(false, event, running,
3382 exec_state, event_data, response_json, client_data);
3383 return message;
3384}
3385
3386
3387MessageImpl::MessageImpl(bool is_event,
3388 DebugEvent event,
3389 bool running,
3390 Handle<JSObject> exec_state,
3391 Handle<JSObject> event_data,
3392 Handle<String> response_json,
3393 v8::Debug::ClientData* client_data)
3394 : is_event_(is_event),
3395 event_(event),
3396 running_(running),
3397 exec_state_(exec_state),
3398 event_data_(event_data),
3399 response_json_(response_json),
3400 client_data_(client_data) {}
3401
3402
3403bool MessageImpl::IsEvent() const {
3404 return is_event_;
3405}
3406
3407
3408bool MessageImpl::IsResponse() const {
3409 return !is_event_;
3410}
3411
3412
3413DebugEvent MessageImpl::GetEvent() const {
3414 return event_;
3415}
3416
3417
3418bool MessageImpl::WillStartRunning() const {
3419 return running_;
3420}
3421
3422
3423v8::Handle<v8::Object> MessageImpl::GetExecutionState() const {
3424 return v8::Utils::ToLocal(exec_state_);
3425}
3426
3427
3428v8::Handle<v8::Object> MessageImpl::GetEventData() const {
3429 return v8::Utils::ToLocal(event_data_);
3430}
3431
3432
3433v8::Handle<v8::String> MessageImpl::GetJSON() const {
3434 v8::HandleScope scope;
3435
3436 if (IsEvent()) {
3437 // Call toJSONProtocol on the debug event object.
3438 Handle<Object> fun = GetProperty(event_data_, "toJSONProtocol");
3439 if (!fun->IsJSFunction()) {
3440 return v8::Handle<v8::String>();
3441 }
3442 bool caught_exception;
3443 Handle<Object> json = Execution::TryCall(Handle<JSFunction>::cast(fun),
3444 event_data_,
3445 0, NULL, &caught_exception);
3446 if (caught_exception || !json->IsString()) {
3447 return v8::Handle<v8::String>();
3448 }
3449 return scope.Close(v8::Utils::ToLocal(Handle<String>::cast(json)));
3450 } else {
3451 return v8::Utils::ToLocal(response_json_);
3452 }
3453}
3454
3455
3456v8::Handle<v8::Context> MessageImpl::GetEventContext() const {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003457 Isolate* isolate = Isolate::Current();
3458 v8::Handle<v8::Context> context = GetDebugEventContext(isolate);
3459 // Isolate::context() may be NULL when "script collected" event occures.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003460 ASSERT(!context.IsEmpty() || event_ == v8::ScriptCollected);
rossberg@chromium.org717967f2011-07-20 13:44:42 +00003461 return context;
ager@chromium.org5ec48922009-05-05 07:25:34 +00003462}
3463
3464
3465v8::Debug::ClientData* MessageImpl::GetClientData() const {
3466 return client_data_;
3467}
3468
3469
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003470EventDetailsImpl::EventDetailsImpl(DebugEvent event,
3471 Handle<JSObject> exec_state,
3472 Handle<JSObject> event_data,
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00003473 Handle<Object> callback_data,
3474 v8::Debug::ClientData* client_data)
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003475 : event_(event),
3476 exec_state_(exec_state),
3477 event_data_(event_data),
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00003478 callback_data_(callback_data),
3479 client_data_(client_data) {}
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003480
3481
3482DebugEvent EventDetailsImpl::GetEvent() const {
3483 return event_;
3484}
3485
3486
3487v8::Handle<v8::Object> EventDetailsImpl::GetExecutionState() const {
3488 return v8::Utils::ToLocal(exec_state_);
3489}
3490
3491
3492v8::Handle<v8::Object> EventDetailsImpl::GetEventData() const {
3493 return v8::Utils::ToLocal(event_data_);
3494}
3495
3496
3497v8::Handle<v8::Context> EventDetailsImpl::GetEventContext() const {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003498 return GetDebugEventContext(Isolate::Current());
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003499}
3500
3501
3502v8::Handle<v8::Value> EventDetailsImpl::GetCallbackData() const {
3503 return v8::Utils::ToLocal(callback_data_);
3504}
3505
3506
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00003507v8::Debug::ClientData* EventDetailsImpl::GetClientData() const {
3508 return client_data_;
3509}
3510
3511
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003512CommandMessage::CommandMessage() : text_(Vector<uint16_t>::empty()),
3513 client_data_(NULL) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003514}
3515
3516
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003517CommandMessage::CommandMessage(const Vector<uint16_t>& text,
3518 v8::Debug::ClientData* data)
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003519 : text_(text),
3520 client_data_(data) {
3521}
3522
3523
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003524CommandMessage::~CommandMessage() {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003525}
3526
3527
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003528void CommandMessage::Dispose() {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003529 text_.Dispose();
3530 delete client_data_;
3531 client_data_ = NULL;
3532}
3533
3534
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003535CommandMessage CommandMessage::New(const Vector<uint16_t>& command,
3536 v8::Debug::ClientData* data) {
3537 return CommandMessage(command.Clone(), data);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003538}
3539
3540
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003541CommandMessageQueue::CommandMessageQueue(int size) : start_(0), end_(0),
3542 size_(size) {
3543 messages_ = NewArray<CommandMessage>(size);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003544}
3545
3546
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003547CommandMessageQueue::~CommandMessageQueue() {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003548 while (!IsEmpty()) {
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003549 CommandMessage m = Get();
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003550 m.Dispose();
3551 }
kasper.lund7276f142008-07-30 08:49:36 +00003552 DeleteArray(messages_);
3553}
3554
3555
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003556CommandMessage CommandMessageQueue::Get() {
kasper.lund7276f142008-07-30 08:49:36 +00003557 ASSERT(!IsEmpty());
3558 int result = start_;
3559 start_ = (start_ + 1) % size_;
3560 return messages_[result];
3561}
3562
3563
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003564void CommandMessageQueue::Put(const CommandMessage& message) {
kasper.lund7276f142008-07-30 08:49:36 +00003565 if ((end_ + 1) % size_ == start_) {
3566 Expand();
3567 }
3568 messages_[end_] = message;
3569 end_ = (end_ + 1) % size_;
3570}
3571
3572
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003573void CommandMessageQueue::Expand() {
3574 CommandMessageQueue new_queue(size_ * 2);
kasper.lund7276f142008-07-30 08:49:36 +00003575 while (!IsEmpty()) {
3576 new_queue.Put(Get());
3577 }
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003578 CommandMessage* array_to_free = messages_;
kasper.lund7276f142008-07-30 08:49:36 +00003579 *this = new_queue;
3580 new_queue.messages_ = array_to_free;
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003581 // Make the new_queue empty so that it doesn't call Dispose on any messages.
3582 new_queue.start_ = new_queue.end_;
kasper.lund7276f142008-07-30 08:49:36 +00003583 // Automatic destructor called on new_queue, freeing array_to_free.
3584}
3585
3586
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00003587LockingCommandMessageQueue::LockingCommandMessageQueue(Logger* logger, int size)
3588 : logger_(logger), queue_(size) {
kasper.lund7276f142008-07-30 08:49:36 +00003589 lock_ = OS::CreateMutex();
3590}
3591
3592
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003593LockingCommandMessageQueue::~LockingCommandMessageQueue() {
kasper.lund7276f142008-07-30 08:49:36 +00003594 delete lock_;
3595}
3596
3597
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003598bool LockingCommandMessageQueue::IsEmpty() const {
kasper.lund7276f142008-07-30 08:49:36 +00003599 ScopedLock sl(lock_);
3600 return queue_.IsEmpty();
3601}
3602
3603
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003604CommandMessage LockingCommandMessageQueue::Get() {
kasper.lund7276f142008-07-30 08:49:36 +00003605 ScopedLock sl(lock_);
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003606 CommandMessage result = queue_.Get();
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00003607 logger_->DebugEvent("Get", result.text());
kasper.lund7276f142008-07-30 08:49:36 +00003608 return result;
3609}
3610
3611
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003612void LockingCommandMessageQueue::Put(const CommandMessage& message) {
kasper.lund7276f142008-07-30 08:49:36 +00003613 ScopedLock sl(lock_);
3614 queue_.Put(message);
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00003615 logger_->DebugEvent("Put", message.text());
kasper.lund7276f142008-07-30 08:49:36 +00003616}
3617
3618
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003619void LockingCommandMessageQueue::Clear() {
kasper.lund7276f142008-07-30 08:49:36 +00003620 ScopedLock sl(lock_);
3621 queue_.Clear();
3622}
3623
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003624
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003625MessageDispatchHelperThread::MessageDispatchHelperThread(Isolate* isolate)
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00003626 : Thread("v8:MsgDispHelpr"),
lrn@chromium.org5d00b602011-01-05 09:51:43 +00003627 sem_(OS::CreateSemaphore(0)), mutex_(OS::CreateMutex()),
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003628 already_signalled_(false) {
3629}
3630
3631
3632MessageDispatchHelperThread::~MessageDispatchHelperThread() {
3633 delete mutex_;
3634 delete sem_;
3635}
3636
3637
3638void MessageDispatchHelperThread::Schedule() {
3639 {
3640 ScopedLock lock(mutex_);
3641 if (already_signalled_) {
3642 return;
3643 }
3644 already_signalled_ = true;
3645 }
3646 sem_->Signal();
3647}
3648
3649
3650void MessageDispatchHelperThread::Run() {
3651 while (true) {
3652 sem_->Wait();
3653 {
3654 ScopedLock lock(mutex_);
3655 already_signalled_ = false;
3656 }
3657 {
3658 Locker locker;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003659 Isolate::Current()->debugger()->CallMessageDispatchHandler();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003660 }
3661 }
3662}
3663
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003664#endif // ENABLE_DEBUGGER_SUPPORT
kasper.lund7276f142008-07-30 08:49:36 +00003665
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003666} } // namespace v8::internal