blob: 17897dd7f031854baee8ba3db4218899c560397c [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_;
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +0000264 reloc_iterator_ = new RelocIterator(
265 debug_info_->code(),
266 ~RelocInfo::ModeMask(RelocInfo::CODE_AGE_SEQUENCE));
267 reloc_iterator_original_ = new RelocIterator(
268 debug_info_->original_code(),
269 ~RelocInfo::ModeMask(RelocInfo::CODE_AGE_SEQUENCE));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000270
271 // Position at the first break point.
272 break_point_ = -1;
273 position_ = 1;
274 statement_position_ = 1;
275 Next();
276}
277
278
279bool BreakLocationIterator::Done() const {
280 return RinfoDone();
281}
282
283
284void BreakLocationIterator::SetBreakPoint(Handle<Object> break_point_object) {
285 // If there is not already a real break point here patch code with debug
286 // break.
287 if (!HasBreakPoint()) {
288 SetDebugBreak();
289 }
ager@chromium.orga1645e22009-09-09 19:27:10 +0000290 ASSERT(IsDebugBreak() || IsDebuggerStatement());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000291 // Set the break point information.
292 DebugInfo::SetBreakPoint(debug_info_, code_position(),
293 position(), statement_position(),
294 break_point_object);
295}
296
297
298void BreakLocationIterator::ClearBreakPoint(Handle<Object> break_point_object) {
299 // Clear the break point information.
300 DebugInfo::ClearBreakPoint(debug_info_, code_position(), break_point_object);
301 // If there are no more break points here remove the debug break.
302 if (!HasBreakPoint()) {
303 ClearDebugBreak();
304 ASSERT(!IsDebugBreak());
305 }
306}
307
308
309void BreakLocationIterator::SetOneShot() {
ager@chromium.orga1645e22009-09-09 19:27:10 +0000310 // Debugger statement always calls debugger. No need to modify it.
311 if (IsDebuggerStatement()) {
312 return;
313 }
314
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000315 // If there is a real break point here no more to do.
316 if (HasBreakPoint()) {
317 ASSERT(IsDebugBreak());
318 return;
319 }
320
321 // Patch code with debug break.
322 SetDebugBreak();
323}
324
325
326void BreakLocationIterator::ClearOneShot() {
ager@chromium.orga1645e22009-09-09 19:27:10 +0000327 // Debugger statement always calls debugger. No need to modify it.
328 if (IsDebuggerStatement()) {
329 return;
330 }
331
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000332 // If there is a real break point here no more to do.
333 if (HasBreakPoint()) {
334 ASSERT(IsDebugBreak());
335 return;
336 }
337
338 // Patch code removing debug break.
339 ClearDebugBreak();
340 ASSERT(!IsDebugBreak());
341}
342
343
344void BreakLocationIterator::SetDebugBreak() {
ager@chromium.orga1645e22009-09-09 19:27:10 +0000345 // Debugger statement always calls debugger. No need to modify it.
346 if (IsDebuggerStatement()) {
347 return;
348 }
349
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000350 // If there is already a break point here just return. This might happen if
v8.team.kasperl727e9952008-09-02 14:56:44 +0000351 // the same code is flooded with break points twice. Flooding the same
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000352 // function twice might happen when stepping in a function with an exception
353 // handler as the handler and the function is the same.
354 if (IsDebugBreak()) {
355 return;
356 }
357
ager@chromium.org236ad962008-09-25 09:45:57 +0000358 if (RelocInfo::IsJSReturn(rmode())) {
iposva@chromium.org245aa852009-02-10 00:49:54 +0000359 // Patch the frame exit code with a break point.
360 SetDebugBreakAtReturn();
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000361 } else if (IsDebugBreakSlot()) {
362 // Patch the code in the break slot.
363 SetDebugBreakAtSlot();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000364 } else {
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000365 // Patch the IC call.
366 SetDebugBreakAtIC();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000367 }
368 ASSERT(IsDebugBreak());
369}
370
371
372void BreakLocationIterator::ClearDebugBreak() {
ager@chromium.orga1645e22009-09-09 19:27:10 +0000373 // Debugger statement always calls debugger. No need to modify it.
374 if (IsDebuggerStatement()) {
375 return;
376 }
377
ager@chromium.org236ad962008-09-25 09:45:57 +0000378 if (RelocInfo::IsJSReturn(rmode())) {
iposva@chromium.org245aa852009-02-10 00:49:54 +0000379 // Restore the frame exit code.
380 ClearDebugBreakAtReturn();
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000381 } else if (IsDebugBreakSlot()) {
382 // Restore the code in the break slot.
383 ClearDebugBreakAtSlot();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000384 } else {
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000385 // Patch the IC call.
386 ClearDebugBreakAtIC();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000387 }
388 ASSERT(!IsDebugBreak());
389}
390
391
392void BreakLocationIterator::PrepareStepIn() {
ager@chromium.org381abbb2009-02-25 13:23:22 +0000393 HandleScope scope;
394
ager@chromium.orga1645e22009-09-09 19:27:10 +0000395 // Step in can only be prepared if currently positioned on an IC call,
396 // construct call or CallFunction stub call.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000397 Address target = rinfo()->target_address();
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000398 Handle<Code> target_code(Code::GetCodeFromTargetAddress(target));
399 if (target_code->is_call_stub() || target_code->is_keyed_call_stub()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000400 // Step in through IC call is handled by the runtime system. Therefore make
401 // sure that the any current IC is cleared and the runtime system is
402 // called. If the executing code has a debug break at the location change
403 // the call in the original code as it is the code there that will be
404 // executed in place of the debug break call.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000405 Handle<Code> stub = ComputeCallDebugPrepareStepIn(
406 target_code->arguments_count(), target_code->kind());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000407 if (IsDebugBreak()) {
408 original_rinfo()->set_target_address(stub->entry());
409 } else {
410 rinfo()->set_target_address(stub->entry());
411 }
412 } else {
ager@chromium.orga1645e22009-09-09 19:27:10 +0000413#ifdef DEBUG
414 // All the following stuff is needed only for assertion checks so the code
415 // is wrapped in ifdef.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000416 Handle<Code> maybe_call_function_stub = target_code;
ager@chromium.orga1645e22009-09-09 19:27:10 +0000417 if (IsDebugBreak()) {
418 Address original_target = original_rinfo()->target_address();
419 maybe_call_function_stub =
420 Handle<Code>(Code::GetCodeFromTargetAddress(original_target));
421 }
422 bool is_call_function_stub =
423 (maybe_call_function_stub->kind() == Code::STUB &&
424 maybe_call_function_stub->major_key() == CodeStub::CallFunction);
425
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000426 // Step in through construct call requires no changes to the running code.
427 // Step in through getters/setters should already be prepared as well
428 // because caller of this function (Debug::PrepareStep) is expected to
429 // flood the top frame's function with one shot breakpoints.
ager@chromium.orga1645e22009-09-09 19:27:10 +0000430 // Step in through CallFunction stub should also be prepared by caller of
431 // this function (Debug::PrepareStep) which should flood target function
432 // with breakpoints.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000433 ASSERT(RelocInfo::IsConstructCall(rmode()) ||
434 target_code->is_inline_cache_stub() ||
435 is_call_function_stub);
ager@chromium.orga1645e22009-09-09 19:27:10 +0000436#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000437 }
438}
439
440
441// Check whether the break point is at a position which will exit the function.
442bool BreakLocationIterator::IsExit() const {
ager@chromium.org236ad962008-09-25 09:45:57 +0000443 return (RelocInfo::IsJSReturn(rmode()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000444}
445
446
447bool BreakLocationIterator::HasBreakPoint() {
448 return debug_info_->HasBreakPoint(code_position());
449}
450
451
452// Check whether there is a debug break at the current position.
453bool BreakLocationIterator::IsDebugBreak() {
ager@chromium.org236ad962008-09-25 09:45:57 +0000454 if (RelocInfo::IsJSReturn(rmode())) {
iposva@chromium.org245aa852009-02-10 00:49:54 +0000455 return IsDebugBreakAtReturn();
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000456 } else if (IsDebugBreakSlot()) {
457 return IsDebugBreakAtSlot();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000458 } else {
459 return Debug::IsDebugBreak(rinfo()->target_address());
460 }
461}
462
463
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000464void BreakLocationIterator::SetDebugBreakAtIC() {
465 // Patch the original code with the current address as the current address
466 // might have changed by the inline caching since the code was copied.
467 original_rinfo()->set_target_address(rinfo()->target_address());
468
469 RelocInfo::Mode mode = rmode();
470 if (RelocInfo::IsCodeTarget(mode)) {
471 Address target = rinfo()->target_address();
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000472 Handle<Code> target_code(Code::GetCodeFromTargetAddress(target));
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000473
474 // Patch the code to invoke the builtin debug break function matching the
475 // calling convention used by the call site.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000476 Handle<Code> dbgbrk_code(Debug::FindDebugBreak(target_code, mode));
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000477 rinfo()->set_target_address(dbgbrk_code->entry());
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000478 }
479}
480
481
482void BreakLocationIterator::ClearDebugBreakAtIC() {
483 // Patch the code to the original invoke.
484 rinfo()->set_target_address(original_rinfo()->target_address());
485}
486
487
ager@chromium.orga1645e22009-09-09 19:27:10 +0000488bool BreakLocationIterator::IsDebuggerStatement() {
ager@chromium.org5c838252010-02-19 08:53:10 +0000489 return RelocInfo::DEBUG_BREAK == rmode();
ager@chromium.orga1645e22009-09-09 19:27:10 +0000490}
491
492
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000493bool BreakLocationIterator::IsDebugBreakSlot() {
494 return RelocInfo::DEBUG_BREAK_SLOT == rmode();
495}
496
497
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000498Object* BreakLocationIterator::BreakPointObjects() {
499 return debug_info_->GetBreakPointObjects(code_position());
500}
501
502
ager@chromium.org381abbb2009-02-25 13:23:22 +0000503// Clear out all the debug break code. This is ONLY supposed to be used when
504// shutting down the debugger as it will leave the break point information in
505// DebugInfo even though the code is patched back to the non break point state.
506void BreakLocationIterator::ClearAllDebugBreak() {
507 while (!Done()) {
508 ClearDebugBreak();
509 Next();
510 }
511}
512
513
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000514bool BreakLocationIterator::RinfoDone() const {
515 ASSERT(reloc_iterator_->done() == reloc_iterator_original_->done());
516 return reloc_iterator_->done();
517}
518
519
520void BreakLocationIterator::RinfoNext() {
521 reloc_iterator_->next();
522 reloc_iterator_original_->next();
523#ifdef DEBUG
524 ASSERT(reloc_iterator_->done() == reloc_iterator_original_->done());
525 if (!reloc_iterator_->done()) {
526 ASSERT(rmode() == original_rmode());
527 }
528#endif
529}
530
531
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000532// Threading support.
533void Debug::ThreadInit() {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000534 thread_local_.break_count_ = 0;
535 thread_local_.break_id_ = 0;
536 thread_local_.break_frame_id_ = StackFrame::NO_ID;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000537 thread_local_.last_step_action_ = StepNone;
ager@chromium.org236ad962008-09-25 09:45:57 +0000538 thread_local_.last_statement_position_ = RelocInfo::kNoPosition;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000539 thread_local_.step_count_ = 0;
540 thread_local_.last_fp_ = 0;
lrn@chromium.org34e60782011-09-15 07:25:40 +0000541 thread_local_.queued_step_count_ = 0;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000542 thread_local_.step_into_fp_ = 0;
ager@chromium.orga1645e22009-09-09 19:27:10 +0000543 thread_local_.step_out_fp_ = 0;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000544 thread_local_.after_break_target_ = 0;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000545 // TODO(isolates): frames_are_dropped_?
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000546 thread_local_.debugger_entry_ = NULL;
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000547 thread_local_.pending_interrupts_ = 0;
ricow@chromium.org0b9f8502010-08-18 07:45:01 +0000548 thread_local_.restarter_frame_function_pointer_ = NULL;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000549}
550
551
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000552char* Debug::ArchiveDebug(char* storage) {
553 char* to = storage;
554 memcpy(to, reinterpret_cast<char*>(&thread_local_), sizeof(ThreadLocal));
555 to += sizeof(ThreadLocal);
556 memcpy(to, reinterpret_cast<char*>(&registers_), sizeof(registers_));
557 ThreadInit();
558 ASSERT(to <= storage + ArchiveSpacePerThread());
559 return storage + ArchiveSpacePerThread();
560}
561
562
563char* Debug::RestoreDebug(char* storage) {
564 char* from = storage;
565 memcpy(reinterpret_cast<char*>(&thread_local_), from, sizeof(ThreadLocal));
566 from += sizeof(ThreadLocal);
567 memcpy(reinterpret_cast<char*>(&registers_), from, sizeof(registers_));
568 ASSERT(from <= storage + ArchiveSpacePerThread());
569 return storage + ArchiveSpacePerThread();
570}
571
572
573int Debug::ArchiveSpacePerThread() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000574 return sizeof(ThreadLocal) + sizeof(JSCallerSavedBuffer);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000575}
576
577
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000578// Frame structure (conforms InternalFrame structure):
579// -- code
580// -- SMI maker
581// -- function (slot is called "context")
582// -- frame base
583Object** Debug::SetUpFrameDropperFrame(StackFrame* bottom_js_frame,
584 Handle<Code> code) {
585 ASSERT(bottom_js_frame->is_java_script());
586
587 Address fp = bottom_js_frame->fp();
588
589 // Move function pointer into "context" slot.
590 Memory::Object_at(fp + StandardFrameConstants::kContextOffset) =
591 Memory::Object_at(fp + JavaScriptFrameConstants::kFunctionOffset);
592
593 Memory::Object_at(fp + InternalFrameConstants::kCodeOffset) = *code;
594 Memory::Object_at(fp + StandardFrameConstants::kMarkerOffset) =
595 Smi::FromInt(StackFrame::INTERNAL);
596
597 return reinterpret_cast<Object**>(&Memory::Object_at(
598 fp + StandardFrameConstants::kContextOffset));
599}
600
601const int Debug::kFrameDropperFrameSize = 4;
602
603
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000604void ScriptCache::Add(Handle<Script> script) {
lrn@chromium.org7516f052011-03-30 08:52:27 +0000605 GlobalHandles* global_handles = Isolate::Current()->global_handles();
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000606 // Create an entry in the hash map for the script.
607 int id = Smi::cast(script->id())->value();
608 HashMap::Entry* entry =
609 HashMap::Lookup(reinterpret_cast<void*>(id), Hash(id), true);
610 if (entry->value != NULL) {
611 ASSERT(*script == *reinterpret_cast<Script**>(entry->value));
612 return;
613 }
614
615 // Globalize the script object, make it weak and use the location of the
616 // global handle as the value in the hash map.
617 Handle<Script> script_ =
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000618 Handle<Script>::cast(
lrn@chromium.org7516f052011-03-30 08:52:27 +0000619 (global_handles->Create(*script)));
620 global_handles->MakeWeak(
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000621 reinterpret_cast<Object**>(script_.location()),
622 this,
623 ScriptCache::HandleWeakScript);
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000624 entry->value = script_.location();
625}
626
627
628Handle<FixedArray> ScriptCache::GetScripts() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000629 Handle<FixedArray> instances = FACTORY->NewFixedArray(occupancy());
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000630 int count = 0;
631 for (HashMap::Entry* entry = Start(); entry != NULL; entry = Next(entry)) {
632 ASSERT(entry->value != NULL);
633 if (entry->value != NULL) {
634 instances->set(count, *reinterpret_cast<Script**>(entry->value));
635 count++;
636 }
637 }
638 return instances;
639}
640
641
642void ScriptCache::ProcessCollectedScripts() {
lrn@chromium.org7516f052011-03-30 08:52:27 +0000643 Debugger* debugger = Isolate::Current()->debugger();
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000644 for (int i = 0; i < collected_scripts_.length(); i++) {
lrn@chromium.org7516f052011-03-30 08:52:27 +0000645 debugger->OnScriptCollected(collected_scripts_[i]);
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000646 }
647 collected_scripts_.Clear();
648}
649
650
651void ScriptCache::Clear() {
lrn@chromium.org7516f052011-03-30 08:52:27 +0000652 GlobalHandles* global_handles = Isolate::Current()->global_handles();
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000653 // Iterate the script cache to get rid of all the weak handles.
654 for (HashMap::Entry* entry = Start(); entry != NULL; entry = Next(entry)) {
655 ASSERT(entry != NULL);
656 Object** location = reinterpret_cast<Object**>(entry->value);
657 ASSERT((*location)->IsScript());
lrn@chromium.org7516f052011-03-30 08:52:27 +0000658 global_handles->ClearWeakness(location);
659 global_handles->Destroy(location);
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000660 }
661 // Clear the content of the hash map.
662 HashMap::Clear();
663}
664
665
666void ScriptCache::HandleWeakScript(v8::Persistent<v8::Value> obj, void* data) {
667 ScriptCache* script_cache = reinterpret_cast<ScriptCache*>(data);
668 // Find the location of the global handle.
669 Script** location =
670 reinterpret_cast<Script**>(Utils::OpenHandle(*obj).location());
671 ASSERT((*location)->IsScript());
672
673 // Remove the entry from the cache.
674 int id = Smi::cast((*location)->id())->value();
675 script_cache->Remove(reinterpret_cast<void*>(id), Hash(id));
676 script_cache->collected_scripts_.Add(id);
677
678 // Clear the weak handle.
679 obj.Dispose();
680 obj.Clear();
681}
682
683
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000684void Debug::SetUp(bool create_heap_objects) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000685 ThreadInit();
686 if (create_heap_objects) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000687 // Get code to handle debug break on return.
688 debug_break_return_ =
lrn@chromium.org7516f052011-03-30 08:52:27 +0000689 isolate_->builtins()->builtin(Builtins::kReturn_DebugBreak);
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000690 ASSERT(debug_break_return_->IsCode());
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000691 // Get code to handle debug break in debug break slots.
692 debug_break_slot_ =
lrn@chromium.org7516f052011-03-30 08:52:27 +0000693 isolate_->builtins()->builtin(Builtins::kSlot_DebugBreak);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000694 ASSERT(debug_break_slot_->IsCode());
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000695 }
696}
697
698
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000699void Debug::HandleWeakDebugInfo(v8::Persistent<v8::Value> obj, void* data) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000700 Debug* debug = Isolate::Current()->debug();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000701 DebugInfoListNode* node = reinterpret_cast<DebugInfoListNode*>(data);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000702 // We need to clear all breakpoints associated with the function to restore
703 // original code and avoid patching the code twice later because
704 // the function will live in the heap until next gc, and can be found by
jkummerow@chromium.org78502a92012-09-06 13:50:42 +0000705 // Debug::FindSharedFunctionInfoInScript.
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000706 BreakLocationIterator it(node->debug_info(), ALL_BREAK_LOCATIONS);
707 it.ClearAllDebugBreak();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000708 debug->RemoveDebugInfo(node->debug_info());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000709#ifdef DEBUG
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000710 node = debug->debug_info_list_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000711 while (node != NULL) {
712 ASSERT(node != reinterpret_cast<DebugInfoListNode*>(data));
713 node = node->next();
714 }
715#endif
716}
717
718
719DebugInfoListNode::DebugInfoListNode(DebugInfo* debug_info): next_(NULL) {
lrn@chromium.org7516f052011-03-30 08:52:27 +0000720 GlobalHandles* global_handles = Isolate::Current()->global_handles();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000721 // Globalize the request debug info object and make it weak.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000722 debug_info_ = Handle<DebugInfo>::cast(
lrn@chromium.org7516f052011-03-30 08:52:27 +0000723 (global_handles->Create(debug_info)));
724 global_handles->MakeWeak(
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000725 reinterpret_cast<Object**>(debug_info_.location()),
726 this,
727 Debug::HandleWeakDebugInfo);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000728}
729
730
731DebugInfoListNode::~DebugInfoListNode() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000732 Isolate::Current()->global_handles()->Destroy(
733 reinterpret_cast<Object**>(debug_info_.location()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000734}
735
736
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000737bool Debug::CompileDebuggerScript(int index) {
lrn@chromium.org7516f052011-03-30 08:52:27 +0000738 Isolate* isolate = Isolate::Current();
739 Factory* factory = isolate->factory();
740 HandleScope scope(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000741
kasper.lund44510672008-07-25 07:37:58 +0000742 // Bail out if the index is invalid.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000743 if (index == -1) {
744 return false;
745 }
kasper.lund44510672008-07-25 07:37:58 +0000746
747 // Find source and name for the requested script.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000748 Handle<String> source_code =
lrn@chromium.org7516f052011-03-30 08:52:27 +0000749 isolate->bootstrapper()->NativesSourceLookup(index);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000750 Vector<const char> name = Natives::GetScriptName(index);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000751 Handle<String> script_name = factory->NewStringFromAscii(name);
yangguo@chromium.org355cfd12012-08-29 15:32:24 +0000752 Handle<Context> context = isolate->native_context();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000753
754 // Compile the script.
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000755 Handle<SharedFunctionInfo> function_info;
756 function_info = Compiler::Compile(source_code,
757 script_name,
yangguo@chromium.org355cfd12012-08-29 15:32:24 +0000758 0, 0,
759 context,
760 NULL, NULL,
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000761 Handle<String>::null(),
762 NATIVES_CODE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000763
764 // Silently ignore stack overflows during compilation.
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000765 if (function_info.is_null()) {
lrn@chromium.org7516f052011-03-30 08:52:27 +0000766 ASSERT(isolate->has_pending_exception());
767 isolate->clear_pending_exception();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000768 return false;
769 }
770
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000771 // Execute the shared function in the debugger context.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000772 bool caught_exception;
kasper.lund44510672008-07-25 07:37:58 +0000773 Handle<JSFunction> function =
lrn@chromium.org7516f052011-03-30 08:52:27 +0000774 factory->NewFunctionFromSharedFunctionInfo(function_info, context);
rossberg@chromium.org717967f2011-07-20 13:44:42 +0000775
fschneider@chromium.org7d10be52012-04-10 12:30:14 +0000776 Handle<Object> exception =
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000777 Execution::TryCall(function, Handle<Object>(context->global_object()),
fschneider@chromium.org7d10be52012-04-10 12:30:14 +0000778 0, NULL, &caught_exception);
kasper.lund44510672008-07-25 07:37:58 +0000779
780 // Check for caught exceptions.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000781 if (caught_exception) {
fschneider@chromium.org7d10be52012-04-10 12:30:14 +0000782 ASSERT(!isolate->has_pending_exception());
783 MessageLocation computed_location;
784 isolate->ComputeLocation(&computed_location);
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000785 Handle<Object> message = MessageHandler::MakeMessageObject(
fschneider@chromium.org7d10be52012-04-10 12:30:14 +0000786 "error_loading_debugger", &computed_location,
787 Vector<Handle<Object> >::empty(), Handle<String>(), Handle<JSArray>());
788 ASSERT(!isolate->has_pending_exception());
mmassi@chromium.org49a44672012-12-04 13:52:03 +0000789 if (!exception.is_null()) {
790 isolate->set_pending_exception(*exception);
791 MessageHandler::ReportMessage(Isolate::Current(), NULL, message);
792 isolate->clear_pending_exception();
793 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000794 return false;
795 }
796
kasper.lund44510672008-07-25 07:37:58 +0000797 // Mark this script as native and return successfully.
798 Handle<Script> script(Script::cast(function->shared()->script()));
ager@chromium.orge2902be2009-06-08 12:21:35 +0000799 script->set_type(Smi::FromInt(Script::TYPE_NATIVE));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000800 return true;
801}
802
803
804bool Debug::Load() {
805 // Return if debugger is already loaded.
kasper.lund212ac232008-07-16 07:07:30 +0000806 if (IsLoaded()) return true;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000807
lrn@chromium.org7516f052011-03-30 08:52:27 +0000808 Debugger* debugger = isolate_->debugger();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000809
kasper.lund44510672008-07-25 07:37:58 +0000810 // Bail out if we're already in the process of compiling the native
811 // JavaScript source code for the debugger.
lrn@chromium.org7516f052011-03-30 08:52:27 +0000812 if (debugger->compiling_natives() ||
813 debugger->is_loading_debugger())
mads.s.agercbaa0602008-08-14 13:41:48 +0000814 return false;
lrn@chromium.org7516f052011-03-30 08:52:27 +0000815 debugger->set_loading_debugger(true);
kasper.lund44510672008-07-25 07:37:58 +0000816
817 // Disable breakpoints and interrupts while compiling and running the
818 // debugger scripts including the context creation code.
819 DisableBreak disable(true);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000820 PostponeInterruptsScope postpone(isolate_);
kasper.lund44510672008-07-25 07:37:58 +0000821
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000822 // Create the debugger context.
lrn@chromium.org7516f052011-03-30 08:52:27 +0000823 HandleScope scope(isolate_);
kasper.lund44510672008-07-25 07:37:58 +0000824 Handle<Context> context =
lrn@chromium.org7516f052011-03-30 08:52:27 +0000825 isolate_->bootstrapper()->CreateEnvironment(
danno@chromium.org160a7b02011-04-18 15:51:38 +0000826 isolate_,
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000827 Handle<Object>::null(),
828 v8::Handle<ObjectTemplate>(),
829 NULL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000830
fschneider@chromium.org7d10be52012-04-10 12:30:14 +0000831 // Fail if no context could be created.
832 if (context.is_null()) return false;
833
kasper.lund44510672008-07-25 07:37:58 +0000834 // Use the debugger context.
lrn@chromium.org7516f052011-03-30 08:52:27 +0000835 SaveContext save(isolate_);
836 isolate_->set_context(*context);
kasper.lund44510672008-07-25 07:37:58 +0000837
838 // Expose the builtins object in the debugger context.
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +0000839 Handle<String> key = isolate_->factory()->LookupOneByteSymbol(
840 STATIC_ASCII_VECTOR("builtins"));
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000841 Handle<GlobalObject> global = Handle<GlobalObject>(context->global_object());
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +0000842 RETURN_IF_EMPTY_HANDLE_VALUE(
lrn@chromium.org7516f052011-03-30 08:52:27 +0000843 isolate_,
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000844 JSReceiver::SetProperty(global, key, Handle<Object>(global->builtins()),
845 NONE, kNonStrictMode),
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +0000846 false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000847
848 // Compile the JavaScript for the debugger in the debugger context.
lrn@chromium.org7516f052011-03-30 08:52:27 +0000849 debugger->set_compiling_natives(true);
kasper.lund44510672008-07-25 07:37:58 +0000850 bool caught_exception =
851 !CompileDebuggerScript(Natives::GetIndex("mirror")) ||
852 !CompileDebuggerScript(Natives::GetIndex("debug"));
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000853
854 if (FLAG_enable_liveedit) {
855 caught_exception = caught_exception ||
856 !CompileDebuggerScript(Natives::GetIndex("liveedit"));
857 }
858
lrn@chromium.org7516f052011-03-30 08:52:27 +0000859 debugger->set_compiling_natives(false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000860
mads.s.agercbaa0602008-08-14 13:41:48 +0000861 // Make sure we mark the debugger as not loading before we might
862 // return.
lrn@chromium.org7516f052011-03-30 08:52:27 +0000863 debugger->set_loading_debugger(false);
mads.s.agercbaa0602008-08-14 13:41:48 +0000864
kasper.lund44510672008-07-25 07:37:58 +0000865 // Check for caught exceptions.
866 if (caught_exception) return false;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000867
868 // Debugger loaded.
whesse@chromium.org023421e2010-12-21 12:19:12 +0000869 debug_context_ = context;
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000870
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000871 return true;
872}
873
874
875void Debug::Unload() {
876 // Return debugger is not loaded.
877 if (!IsLoaded()) {
878 return;
879 }
880
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000881 // Clear the script cache.
882 DestroyScriptCache();
883
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000884 // Clear debugger context global handle.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000885 Isolate::Current()->global_handles()->Destroy(
886 reinterpret_cast<Object**>(debug_context_.location()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000887 debug_context_ = Handle<Context>();
888}
889
890
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000891// Set the flag indicating that preemption happened during debugging.
892void Debug::PreemptionWhileInDebugger() {
893 ASSERT(InDebugger());
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000894 Debug::set_interrupts_pending(PREEMPT);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000895}
896
897
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000898void Debug::Iterate(ObjectVisitor* v) {
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000899 v->VisitPointer(BitCast<Object**>(&(debug_break_return_)));
900 v->VisitPointer(BitCast<Object**>(&(debug_break_slot_)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000901}
902
903
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000904Object* Debug::Break(Arguments args) {
905 Heap* heap = isolate_->heap();
906 HandleScope scope(isolate_);
mads.s.ager31e71382008-08-13 09:32:07 +0000907 ASSERT(args.length() == 0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000908
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000909 thread_local_.frame_drop_mode_ = FRAMES_UNTOUCHED;
ager@chromium.org357bf652010-04-12 11:30:10 +0000910
kasper.lundbd3ec4e2008-07-09 11:06:54 +0000911 // Get the top-most JavaScript frame.
vegorov@chromium.org74f333b2011-04-06 11:17:46 +0000912 JavaScriptFrameIterator it(isolate_);
kasper.lundbd3ec4e2008-07-09 11:06:54 +0000913 JavaScriptFrame* frame = it.frame();
914
915 // Just continue if breaks are disabled or debugger cannot be loaded.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000916 if (disable_break() || !Load()) {
917 SetAfterBreakTarget(frame);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000918 return heap->undefined_value();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000919 }
920
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000921 // Enter the debugger.
922 EnterDebugger debugger;
923 if (debugger.FailedToEnter()) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000924 return heap->undefined_value();
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000925 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000926
kasper.lund44510672008-07-25 07:37:58 +0000927 // Postpone interrupt during breakpoint processing.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000928 PostponeInterruptsScope postpone(isolate_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000929
930 // Get the debug info (create it if it does not exist).
931 Handle<SharedFunctionInfo> shared =
932 Handle<SharedFunctionInfo>(JSFunction::cast(frame->function())->shared());
933 Handle<DebugInfo> debug_info = GetDebugInfo(shared);
934
935 // Find the break point where execution has stopped.
936 BreakLocationIterator break_location_iterator(debug_info,
937 ALL_BREAK_LOCATIONS);
938 break_location_iterator.FindBreakLocationFromAddress(frame->pc());
939
940 // Check whether step next reached a new statement.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000941 if (!StepNextContinue(&break_location_iterator, frame)) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000942 // Decrease steps left if performing multiple steps.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000943 if (thread_local_.step_count_ > 0) {
944 thread_local_.step_count_--;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000945 }
946 }
947
948 // If there is one or more real break points check whether any of these are
949 // triggered.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000950 Handle<Object> break_points_hit(heap->undefined_value());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000951 if (break_location_iterator.HasBreakPoint()) {
952 Handle<Object> break_point_objects =
953 Handle<Object>(break_location_iterator.BreakPointObjects());
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000954 break_points_hit = CheckBreakPoints(break_point_objects);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000955 }
956
ager@chromium.orga1645e22009-09-09 19:27:10 +0000957 // If step out is active skip everything until the frame where we need to step
958 // out to is reached, unless real breakpoint is hit.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000959 if (StepOutActive() && frame->fp() != step_out_fp() &&
ager@chromium.orga1645e22009-09-09 19:27:10 +0000960 break_points_hit->IsUndefined() ) {
961 // Step count should always be 0 for StepOut.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000962 ASSERT(thread_local_.step_count_ == 0);
ager@chromium.orga1645e22009-09-09 19:27:10 +0000963 } else if (!break_points_hit->IsUndefined() ||
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000964 (thread_local_.last_step_action_ != StepNone &&
965 thread_local_.step_count_ == 0)) {
ager@chromium.orga1645e22009-09-09 19:27:10 +0000966 // Notify debugger if a real break point is triggered or if performing
967 // single stepping with no more steps to perform. Otherwise do another step.
968
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000969 // Clear all current stepping setup.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000970 ClearStepping();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000971
lrn@chromium.org34e60782011-09-15 07:25:40 +0000972 if (thread_local_.queued_step_count_ > 0) {
973 // Perform queued steps
974 int step_count = thread_local_.queued_step_count_;
975
976 // Clear queue
977 thread_local_.queued_step_count_ = 0;
978
979 PrepareStep(StepNext, step_count);
980 } else {
981 // Notify the debug event listeners.
982 isolate_->debugger()->OnDebugBreak(break_points_hit, false);
983 }
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000984 } else if (thread_local_.last_step_action_ != StepNone) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000985 // Hold on to last step action as it is cleared by the call to
986 // ClearStepping.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000987 StepAction step_action = thread_local_.last_step_action_;
988 int step_count = thread_local_.step_count_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000989
lrn@chromium.org34e60782011-09-15 07:25:40 +0000990 // If StepNext goes deeper in code, StepOut until original frame
991 // and keep step count queued up in the meantime.
992 if (step_action == StepNext && frame->fp() < thread_local_.last_fp_) {
993 // Count frames until target frame
994 int count = 0;
995 JavaScriptFrameIterator it(isolate_);
jkummerow@chromium.org212d9642012-05-11 15:02:09 +0000996 while (!it.done() && it.frame()->fp() < thread_local_.last_fp_) {
lrn@chromium.org34e60782011-09-15 07:25:40 +0000997 count++;
998 it.Advance();
999 }
1000
danno@chromium.org81cac2b2012-07-10 11:28:27 +00001001 // Check that we indeed found the frame we are looking for.
1002 CHECK(!it.done() && (it.frame()->fp() == thread_local_.last_fp_));
1003 if (step_count > 1) {
1004 // Save old count and action to continue stepping after StepOut.
1005 thread_local_.queued_step_count_ = step_count - 1;
jkummerow@chromium.org212d9642012-05-11 15:02:09 +00001006 }
1007
danno@chromium.org81cac2b2012-07-10 11:28:27 +00001008 // Set up for StepOut to reach target frame.
1009 step_action = StepOut;
1010 step_count = count;
lrn@chromium.org34e60782011-09-15 07:25:40 +00001011 }
1012
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001013 // Clear all current stepping setup.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001014 ClearStepping();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001015
1016 // Set up for the remaining steps.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001017 PrepareStep(step_action, step_count);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001018 }
1019
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001020 if (thread_local_.frame_drop_mode_ == FRAMES_UNTOUCHED) {
1021 SetAfterBreakTarget(frame);
1022 } else if (thread_local_.frame_drop_mode_ ==
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001023 FRAME_DROPPED_IN_IC_CALL) {
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001024 // We must have been calling IC stub. Do not go there anymore.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001025 Code* plain_return = isolate_->builtins()->builtin(
1026 Builtins::kPlainReturn_LiveEdit);
1027 thread_local_.after_break_target_ = plain_return->entry();
1028 } else if (thread_local_.frame_drop_mode_ ==
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001029 FRAME_DROPPED_IN_DEBUG_SLOT_CALL) {
1030 // Debug break slot stub does not return normally, instead it manually
1031 // cleans the stack and jumps. We should patch the jump address.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001032 Code* plain_return = isolate_->builtins()->builtin(
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00001033 Builtins::kFrameDropper_LiveEdit);
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001034 thread_local_.after_break_target_ = plain_return->entry();
1035 } else if (thread_local_.frame_drop_mode_ ==
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001036 FRAME_DROPPED_IN_DIRECT_CALL) {
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001037 // Nothing to do, after_break_target is not used here.
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00001038 } else if (thread_local_.frame_drop_mode_ ==
1039 FRAME_DROPPED_IN_RETURN_CALL) {
1040 Code* plain_return = isolate_->builtins()->builtin(
1041 Builtins::kFrameDropper_LiveEdit);
1042 thread_local_.after_break_target_ = plain_return->entry();
ager@chromium.org357bf652010-04-12 11:30:10 +00001043 } else {
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001044 UNREACHABLE();
ager@chromium.org357bf652010-04-12 11:30:10 +00001045 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001046
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001047 return heap->undefined_value();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001048}
1049
1050
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001051RUNTIME_FUNCTION(Object*, Debug_Break) {
1052 return isolate->debug()->Break(args);
1053}
1054
1055
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001056// Check the break point objects for whether one or more are actually
1057// triggered. This function returns a JSArray with the break point objects
1058// which is triggered.
1059Handle<Object> Debug::CheckBreakPoints(Handle<Object> break_point_objects) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00001060 Factory* factory = isolate_->factory();
1061
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001062 // Count the number of break points hit. If there are multiple break points
1063 // they are in a FixedArray.
1064 Handle<FixedArray> break_points_hit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001065 int break_points_hit_count = 0;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001066 ASSERT(!break_point_objects->IsUndefined());
1067 if (break_point_objects->IsFixedArray()) {
1068 Handle<FixedArray> array(FixedArray::cast(*break_point_objects));
lrn@chromium.org7516f052011-03-30 08:52:27 +00001069 break_points_hit = factory->NewFixedArray(array->length());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001070 for (int i = 0; i < array->length(); i++) {
1071 Handle<Object> o(array->get(i));
1072 if (CheckBreakPoint(o)) {
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001073 break_points_hit->set(break_points_hit_count++, *o);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001074 }
1075 }
1076 } else {
lrn@chromium.org7516f052011-03-30 08:52:27 +00001077 break_points_hit = factory->NewFixedArray(1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001078 if (CheckBreakPoint(break_point_objects)) {
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001079 break_points_hit->set(break_points_hit_count++, *break_point_objects);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001080 }
1081 }
1082
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001083 // Return undefined if no break points were triggered.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001084 if (break_points_hit_count == 0) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00001085 return factory->undefined_value();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001086 }
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001087 // Return break points hit as a JSArray.
lrn@chromium.org7516f052011-03-30 08:52:27 +00001088 Handle<JSArray> result = factory->NewJSArrayWithElements(break_points_hit);
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001089 result->set_length(Smi::FromInt(break_points_hit_count));
1090 return result;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001091}
1092
1093
1094// Check whether a single break point object is triggered.
1095bool Debug::CheckBreakPoint(Handle<Object> break_point_object) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00001096 Factory* factory = isolate_->factory();
1097 HandleScope scope(isolate_);
ager@chromium.org381abbb2009-02-25 13:23:22 +00001098
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001099 // Ignore check if break point object is not a JSObject.
1100 if (!break_point_object->IsJSObject()) return true;
1101
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001102 // Get the function IsBreakPointTriggered (defined in debug-debugger.js).
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001103 Handle<String> is_break_point_triggered_symbol =
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00001104 factory->LookupOneByteSymbol(
1105 STATIC_ASCII_VECTOR("IsBreakPointTriggered"));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001106 Handle<JSFunction> check_break_point =
1107 Handle<JSFunction>(JSFunction::cast(
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00001108 debug_context()->global_object()->GetPropertyNoExceptionThrown(
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001109 *is_break_point_triggered_symbol)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001110
1111 // Get the break id as an object.
lrn@chromium.org7516f052011-03-30 08:52:27 +00001112 Handle<Object> break_id = factory->NewNumberFromInt(Debug::break_id());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001113
1114 // Call HandleBreakPointx.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001115 bool caught_exception;
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00001116 Handle<Object> argv[] = { break_id, break_point_object };
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001117 Handle<Object> result = Execution::TryCall(check_break_point,
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00001118 isolate_->js_builtins_object(),
1119 ARRAY_SIZE(argv),
1120 argv,
1121 &caught_exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001122
1123 // If exception or non boolean result handle as not triggered
1124 if (caught_exception || !result->IsBoolean()) {
1125 return false;
1126 }
1127
1128 // Return whether the break point is triggered.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001129 ASSERT(!result.is_null());
1130 return (*result)->IsTrue();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001131}
1132
1133
1134// Check whether the function has debug information.
1135bool Debug::HasDebugInfo(Handle<SharedFunctionInfo> shared) {
1136 return !shared->debug_info()->IsUndefined();
1137}
1138
1139
kasper.lundbd3ec4e2008-07-09 11:06:54 +00001140// Return the debug info for this function. EnsureDebugInfo must be called
1141// prior to ensure the debug info has been generated for shared.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001142Handle<DebugInfo> Debug::GetDebugInfo(Handle<SharedFunctionInfo> shared) {
kasper.lundbd3ec4e2008-07-09 11:06:54 +00001143 ASSERT(HasDebugInfo(shared));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001144 return Handle<DebugInfo>(DebugInfo::cast(shared->debug_info()));
1145}
1146
1147
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001148void Debug::SetBreakPoint(Handle<JSFunction> function,
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00001149 Handle<Object> break_point_object,
1150 int* source_position) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00001151 HandleScope scope(isolate_);
ager@chromium.org381abbb2009-02-25 13:23:22 +00001152
lrn@chromium.org34e60782011-09-15 07:25:40 +00001153 PrepareForBreakPoints();
1154
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001155 // Make sure the function is compiled and has set up the debug info.
1156 Handle<SharedFunctionInfo> shared(function->shared());
1157 if (!EnsureDebugInfo(shared, function)) {
kasper.lundbd3ec4e2008-07-09 11:06:54 +00001158 // Return if retrieving debug info failed.
1159 return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001160 }
1161
kasper.lundbd3ec4e2008-07-09 11:06:54 +00001162 Handle<DebugInfo> debug_info = GetDebugInfo(shared);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001163 // Source positions starts with zero.
danno@chromium.orgbf0c8202011-12-27 10:09:42 +00001164 ASSERT(*source_position >= 0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001165
1166 // Find the break point and change it.
1167 BreakLocationIterator it(debug_info, SOURCE_BREAK_LOCATIONS);
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00001168 it.FindBreakLocationFromPosition(*source_position);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001169 it.SetBreakPoint(break_point_object);
1170
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00001171 *source_position = it.position();
1172
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001173 // At least one active break point now.
1174 ASSERT(debug_info->GetBreakPointCount() > 0);
1175}
1176
1177
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001178bool Debug::SetBreakPointForScript(Handle<Script> script,
1179 Handle<Object> break_point_object,
1180 int* source_position) {
1181 HandleScope scope(isolate_);
1182
jkummerow@chromium.org78502a92012-09-06 13:50:42 +00001183 PrepareForBreakPoints();
1184
1185 // Obtain shared function info for the function.
1186 Object* result = FindSharedFunctionInfoInScript(script, *source_position);
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001187 if (result->IsUndefined()) return false;
1188
1189 // Make sure the function has set up the debug info.
1190 Handle<SharedFunctionInfo> shared(SharedFunctionInfo::cast(result));
1191 if (!EnsureDebugInfo(shared, Handle<JSFunction>::null())) {
1192 // Return if retrieving debug info failed.
1193 return false;
1194 }
1195
1196 // Find position within function. The script position might be before the
1197 // source position of the first function.
1198 int position;
1199 if (shared->start_position() > *source_position) {
1200 position = 0;
1201 } else {
1202 position = *source_position - shared->start_position();
1203 }
1204
1205 Handle<DebugInfo> debug_info = GetDebugInfo(shared);
1206 // Source positions starts with zero.
1207 ASSERT(position >= 0);
1208
1209 // Find the break point and change it.
1210 BreakLocationIterator it(debug_info, SOURCE_BREAK_LOCATIONS);
1211 it.FindBreakLocationFromPosition(position);
1212 it.SetBreakPoint(break_point_object);
1213
1214 *source_position = it.position() + shared->start_position();
1215
1216 // At least one active break point now.
1217 ASSERT(debug_info->GetBreakPointCount() > 0);
1218 return true;
1219}
1220
1221
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001222void Debug::ClearBreakPoint(Handle<Object> break_point_object) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00001223 HandleScope scope(isolate_);
ager@chromium.org381abbb2009-02-25 13:23:22 +00001224
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001225 DebugInfoListNode* node = debug_info_list_;
1226 while (node != NULL) {
1227 Object* result = DebugInfo::FindBreakPointInfo(node->debug_info(),
1228 break_point_object);
1229 if (!result->IsUndefined()) {
1230 // Get information in the break point.
1231 BreakPointInfo* break_point_info = BreakPointInfo::cast(result);
1232 Handle<DebugInfo> debug_info = node->debug_info();
1233 Handle<SharedFunctionInfo> shared(debug_info->shared());
1234 int source_position = break_point_info->statement_position()->value();
1235
1236 // Source positions starts with zero.
1237 ASSERT(source_position >= 0);
1238
1239 // Find the break point and clear it.
1240 BreakLocationIterator it(debug_info, SOURCE_BREAK_LOCATIONS);
1241 it.FindBreakLocationFromPosition(source_position);
1242 it.ClearBreakPoint(break_point_object);
1243
1244 // If there are no more break points left remove the debug info for this
1245 // function.
1246 if (debug_info->GetBreakPointCount() == 0) {
1247 RemoveDebugInfo(debug_info);
1248 }
1249
1250 return;
1251 }
1252 node = node->next();
1253 }
1254}
1255
1256
ager@chromium.org381abbb2009-02-25 13:23:22 +00001257void Debug::ClearAllBreakPoints() {
1258 DebugInfoListNode* node = debug_info_list_;
1259 while (node != NULL) {
1260 // Remove all debug break code.
1261 BreakLocationIterator it(node->debug_info(), ALL_BREAK_LOCATIONS);
1262 it.ClearAllDebugBreak();
1263 node = node->next();
1264 }
1265
1266 // Remove all debug info.
1267 while (debug_info_list_ != NULL) {
1268 RemoveDebugInfo(debug_info_list_->debug_info());
1269 }
1270}
1271
1272
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001273void Debug::FloodWithOneShot(Handle<JSFunction> function) {
lrn@chromium.org34e60782011-09-15 07:25:40 +00001274 PrepareForBreakPoints();
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001275
1276 // Make sure the function is compiled and has set up the debug info.
1277 Handle<SharedFunctionInfo> shared(function->shared());
1278 if (!EnsureDebugInfo(shared, function)) {
kasper.lundbd3ec4e2008-07-09 11:06:54 +00001279 // Return if we failed to retrieve the debug info.
1280 return;
1281 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001282
1283 // Flood the function with break points.
kasper.lundbd3ec4e2008-07-09 11:06:54 +00001284 BreakLocationIterator it(GetDebugInfo(shared), ALL_BREAK_LOCATIONS);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001285 while (!it.Done()) {
1286 it.SetOneShot();
1287 it.Next();
1288 }
1289}
1290
1291
rossberg@chromium.org2c067b12012-03-19 11:01:52 +00001292void Debug::FloodBoundFunctionWithOneShot(Handle<JSFunction> function) {
1293 Handle<FixedArray> new_bindings(function->function_bindings());
1294 Handle<Object> bindee(new_bindings->get(JSFunction::kBoundFunctionIndex));
1295
1296 if (!bindee.is_null() && bindee->IsJSFunction() &&
1297 !JSFunction::cast(*bindee)->IsBuiltin()) {
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001298 Handle<JSFunction> bindee_function(JSFunction::cast(*bindee));
1299 Debug::FloodWithOneShot(bindee_function);
rossberg@chromium.org2c067b12012-03-19 11:01:52 +00001300 }
1301}
1302
1303
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001304void Debug::FloodHandlerWithOneShot() {
ager@chromium.org8bb60582008-12-11 12:02:20 +00001305 // Iterate through the JavaScript stack looking for handlers.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001306 StackFrame::Id id = break_frame_id();
ager@chromium.org8bb60582008-12-11 12:02:20 +00001307 if (id == StackFrame::NO_ID) {
1308 // If there is no JavaScript stack don't do anything.
1309 return;
1310 }
vegorov@chromium.org74f333b2011-04-06 11:17:46 +00001311 for (JavaScriptFrameIterator it(isolate_, id); !it.done(); it.Advance()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001312 JavaScriptFrame* frame = it.frame();
1313 if (frame->HasHandler()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001314 // Flood the function with the catch block with break points
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001315 JSFunction* function = JSFunction::cast(frame->function());
1316 FloodWithOneShot(Handle<JSFunction>(function));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001317 return;
1318 }
1319 }
1320}
1321
1322
1323void Debug::ChangeBreakOnException(ExceptionBreakType type, bool enable) {
1324 if (type == BreakUncaughtException) {
1325 break_on_uncaught_exception_ = enable;
1326 } else {
1327 break_on_exception_ = enable;
1328 }
1329}
1330
1331
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00001332bool Debug::IsBreakOnException(ExceptionBreakType type) {
1333 if (type == BreakUncaughtException) {
1334 return break_on_uncaught_exception_;
1335 } else {
1336 return break_on_exception_;
1337 }
1338}
1339
1340
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001341void Debug::PrepareStep(StepAction step_action, int step_count) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00001342 HandleScope scope(isolate_);
lrn@chromium.org34e60782011-09-15 07:25:40 +00001343
1344 PrepareForBreakPoints();
1345
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001346 ASSERT(Debug::InDebugger());
1347
1348 // Remember this step action and count.
1349 thread_local_.last_step_action_ = step_action;
ager@chromium.orga1645e22009-09-09 19:27:10 +00001350 if (step_action == StepOut) {
1351 // For step out target frame will be found on the stack so there is no need
1352 // to set step counter for it. It's expected to always be 0 for StepOut.
1353 thread_local_.step_count_ = 0;
1354 } else {
1355 thread_local_.step_count_ = step_count;
1356 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001357
1358 // Get the frame where the execution has stopped and skip the debug frame if
1359 // any. The debug frame will only be present if execution was stopped due to
1360 // hitting a break point. In other situations (e.g. unhandled exception) the
1361 // debug frame is not present.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001362 StackFrame::Id id = break_frame_id();
ager@chromium.org8bb60582008-12-11 12:02:20 +00001363 if (id == StackFrame::NO_ID) {
1364 // If there is no JavaScript stack don't do anything.
1365 return;
1366 }
vegorov@chromium.org74f333b2011-04-06 11:17:46 +00001367 JavaScriptFrameIterator frames_it(isolate_, id);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001368 JavaScriptFrame* frame = frames_it.frame();
1369
1370 // First of all ensure there is one-shot break points in the top handler
1371 // if any.
1372 FloodHandlerWithOneShot();
1373
1374 // If the function on the top frame is unresolved perform step out. This will
1375 // be the case when calling unknown functions and having the debugger stopped
1376 // in an unhandled exception.
1377 if (!frame->function()->IsJSFunction()) {
1378 // Step out: Find the calling JavaScript frame and flood it with
1379 // breakpoints.
1380 frames_it.Advance();
1381 // Fill the function to return to with one-shot break points.
1382 JSFunction* function = JSFunction::cast(frames_it.frame()->function());
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001383 FloodWithOneShot(Handle<JSFunction>(function));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001384 return;
1385 }
1386
1387 // Get the debug info (create it if it does not exist).
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001388 Handle<JSFunction> function(JSFunction::cast(frame->function()));
1389 Handle<SharedFunctionInfo> shared(function->shared());
1390 if (!EnsureDebugInfo(shared, function)) {
kasper.lundbd3ec4e2008-07-09 11:06:54 +00001391 // Return if ensuring debug info failed.
1392 return;
1393 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001394 Handle<DebugInfo> debug_info = GetDebugInfo(shared);
1395
1396 // Find the break location where execution has stopped.
1397 BreakLocationIterator it(debug_info, ALL_BREAK_LOCATIONS);
1398 it.FindBreakLocationFromAddress(frame->pc());
1399
1400 // Compute whether or not the target is a call target.
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001401 bool is_load_or_store = false;
1402 bool is_inline_cache_stub = false;
whesse@chromium.orge90029b2010-08-02 11:52:17 +00001403 bool is_at_restarted_function = false;
ager@chromium.orga1645e22009-09-09 19:27:10 +00001404 Handle<Code> call_function_stub;
ager@chromium.orga1645e22009-09-09 19:27:10 +00001405
whesse@chromium.orge90029b2010-08-02 11:52:17 +00001406 if (thread_local_.restarter_frame_function_pointer_ == NULL) {
1407 if (RelocInfo::IsCodeTarget(it.rinfo()->rmode())) {
1408 bool is_call_target = false;
1409 Address target = it.rinfo()->target_address();
1410 Code* code = Code::GetCodeFromTargetAddress(target);
1411 if (code->is_call_stub() || code->is_keyed_call_stub()) {
1412 is_call_target = true;
1413 }
1414 if (code->is_inline_cache_stub()) {
1415 is_inline_cache_stub = true;
1416 is_load_or_store = !is_call_target;
1417 }
1418
1419 // Check if target code is CallFunction stub.
1420 Code* maybe_call_function_stub = code;
1421 // If there is a breakpoint at this line look at the original code to
1422 // check if it is a CallFunction stub.
1423 if (it.IsDebugBreak()) {
1424 Address original_target = it.original_rinfo()->target_address();
1425 maybe_call_function_stub =
1426 Code::GetCodeFromTargetAddress(original_target);
1427 }
1428 if (maybe_call_function_stub->kind() == Code::STUB &&
1429 maybe_call_function_stub->major_key() == CodeStub::CallFunction) {
1430 // Save reference to the code as we may need it to find out arguments
1431 // count for 'step in' later.
1432 call_function_stub = Handle<Code>(maybe_call_function_stub);
1433 }
ager@chromium.orga1645e22009-09-09 19:27:10 +00001434 }
whesse@chromium.orge90029b2010-08-02 11:52:17 +00001435 } else {
1436 is_at_restarted_function = true;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001437 }
1438
v8.team.kasperl727e9952008-09-02 14:56:44 +00001439 // If this is the last break code target step out is the only possibility.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001440 if (it.IsExit() || step_action == StepOut) {
ager@chromium.orga1645e22009-09-09 19:27:10 +00001441 if (step_action == StepOut) {
1442 // Skip step_count frames starting with the current one.
1443 while (step_count-- > 0 && !frames_it.done()) {
1444 frames_it.Advance();
1445 }
1446 } else {
1447 ASSERT(it.IsExit());
1448 frames_it.Advance();
1449 }
1450 // Skip builtin functions on the stack.
1451 while (!frames_it.done() &&
1452 JSFunction::cast(frames_it.frame()->function())->IsBuiltin()) {
1453 frames_it.Advance();
1454 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001455 // Step out: If there is a JavaScript caller frame, we need to
1456 // flood it with breakpoints.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001457 if (!frames_it.done()) {
1458 // Fill the function to return to with one-shot break points.
1459 JSFunction* function = JSFunction::cast(frames_it.frame()->function());
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001460 FloodWithOneShot(Handle<JSFunction>(function));
ager@chromium.orga1645e22009-09-09 19:27:10 +00001461 // Set target frame pointer.
1462 ActivateStepOut(frames_it.frame());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001463 }
ager@chromium.orga1645e22009-09-09 19:27:10 +00001464 } else if (!(is_inline_cache_stub || RelocInfo::IsConstructCall(it.rmode()) ||
whesse@chromium.orge90029b2010-08-02 11:52:17 +00001465 !call_function_stub.is_null() || is_at_restarted_function)
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001466 || step_action == StepNext || step_action == StepMin) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001467 // Step next or step min.
1468
1469 // Fill the current function with one-shot break points.
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001470 FloodWithOneShot(function);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001471
1472 // Remember source position and frame to handle step next.
1473 thread_local_.last_statement_position_ =
1474 debug_info->code()->SourceStatementPosition(frame->pc());
mmassi@chromium.org7028c052012-06-13 11:51:58 +00001475 thread_local_.last_fp_ = frame->UnpaddedFP();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001476 } else {
whesse@chromium.orge90029b2010-08-02 11:52:17 +00001477 // If there's restarter frame on top of the stack, just get the pointer
1478 // to function which is going to be restarted.
1479 if (is_at_restarted_function) {
1480 Handle<JSFunction> restarted_function(
1481 JSFunction::cast(*thread_local_.restarter_frame_function_pointer_));
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001482 FloodWithOneShot(restarted_function);
whesse@chromium.orge90029b2010-08-02 11:52:17 +00001483 } else if (!call_function_stub.is_null()) {
1484 // If it's CallFunction stub ensure target function is compiled and flood
1485 // it with one shot breakpoints.
1486
ager@chromium.orga1645e22009-09-09 19:27:10 +00001487 // Find out number of arguments from the stub minor key.
1488 // Reverse lookup required as the minor key cannot be retrieved
1489 // from the code object.
1490 Handle<Object> obj(
lrn@chromium.org7516f052011-03-30 08:52:27 +00001491 isolate_->heap()->code_stubs()->SlowReverseLookup(
1492 *call_function_stub));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001493 ASSERT(!obj.is_null());
1494 ASSERT(!(*obj)->IsUndefined());
ager@chromium.orga1645e22009-09-09 19:27:10 +00001495 ASSERT(obj->IsSmi());
1496 // Get the STUB key and extract major and minor key.
1497 uint32_t key = Smi::cast(*obj)->value();
1498 // Argc in the stub is the number of arguments passed - not the
1499 // expected arguments of the called function.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001500 int call_function_arg_count =
1501 CallFunctionStub::ExtractArgcFromMinorKey(
1502 CodeStub::MinorKeyFromKey(key));
ager@chromium.orga1645e22009-09-09 19:27:10 +00001503 ASSERT(call_function_stub->major_key() ==
1504 CodeStub::MajorKeyFromKey(key));
1505
1506 // Find target function on the expression stack.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001507 // Expression stack looks like this (top to bottom):
ager@chromium.orga1645e22009-09-09 19:27:10 +00001508 // argN
1509 // ...
1510 // arg0
1511 // Receiver
1512 // Function to call
1513 int expressions_count = frame->ComputeExpressionsCount();
1514 ASSERT(expressions_count - 2 - call_function_arg_count >= 0);
1515 Object* fun = frame->GetExpression(
1516 expressions_count - 2 - call_function_arg_count);
1517 if (fun->IsJSFunction()) {
1518 Handle<JSFunction> js_function(JSFunction::cast(fun));
rossberg@chromium.org2c067b12012-03-19 11:01:52 +00001519 if (js_function->shared()->bound()) {
1520 Debug::FloodBoundFunctionWithOneShot(js_function);
1521 } else if (!js_function->IsBuiltin()) {
1522 // Don't step into builtins.
ager@chromium.orga1645e22009-09-09 19:27:10 +00001523 // It will also compile target function if it's not compiled yet.
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001524 FloodWithOneShot(js_function);
ager@chromium.orga1645e22009-09-09 19:27:10 +00001525 }
1526 }
1527 }
1528
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001529 // Fill the current function with one-shot break points even for step in on
1530 // a call target as the function called might be a native function for
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001531 // which step in will not stop. It also prepares for stepping in
1532 // getters/setters.
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001533 FloodWithOneShot(function);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001534
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001535 if (is_load_or_store) {
1536 // Remember source position and frame to handle step in getter/setter. If
1537 // there is a custom getter/setter it will be handled in
1538 // Object::Get/SetPropertyWithCallback, otherwise the step action will be
1539 // propagated on the next Debug::Break.
1540 thread_local_.last_statement_position_ =
1541 debug_info->code()->SourceStatementPosition(frame->pc());
mmassi@chromium.org7028c052012-06-13 11:51:58 +00001542 thread_local_.last_fp_ = frame->UnpaddedFP();
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001543 }
1544
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001545 // Step in or Step in min
1546 it.PrepareStepIn();
1547 ActivateStepIn(frame);
1548 }
1549}
1550
1551
1552// Check whether the current debug break should be reported to the debugger. It
1553// is used to have step next and step in only report break back to the debugger
1554// if on a different frame or in a different statement. In some situations
1555// there will be several break points in the same statement when the code is
v8.team.kasperl727e9952008-09-02 14:56:44 +00001556// flooded with one-shot break points. This function helps to perform several
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001557// steps before reporting break back to the debugger.
1558bool Debug::StepNextContinue(BreakLocationIterator* break_location_iterator,
1559 JavaScriptFrame* frame) {
lrn@chromium.org34e60782011-09-15 07:25:40 +00001560 // StepNext and StepOut shouldn't bring us deeper in code, so last frame
1561 // shouldn't be a parent of current frame.
1562 if (thread_local_.last_step_action_ == StepNext ||
1563 thread_local_.last_step_action_ == StepOut) {
1564 if (frame->fp() < thread_local_.last_fp_) return true;
1565 }
1566
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001567 // If the step last action was step next or step in make sure that a new
1568 // statement is hit.
1569 if (thread_local_.last_step_action_ == StepNext ||
1570 thread_local_.last_step_action_ == StepIn) {
1571 // Never continue if returning from function.
1572 if (break_location_iterator->IsExit()) return false;
1573
1574 // Continue if we are still on the same frame and in the same statement.
1575 int current_statement_position =
1576 break_location_iterator->code()->SourceStatementPosition(frame->pc());
mmassi@chromium.org7028c052012-06-13 11:51:58 +00001577 return thread_local_.last_fp_ == frame->UnpaddedFP() &&
ager@chromium.org236ad962008-09-25 09:45:57 +00001578 thread_local_.last_statement_position_ == current_statement_position;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001579 }
1580
1581 // No step next action - don't continue.
1582 return false;
1583}
1584
1585
1586// Check whether the code object at the specified address is a debug break code
1587// object.
1588bool Debug::IsDebugBreak(Address addr) {
ager@chromium.org8bb60582008-12-11 12:02:20 +00001589 Code* code = Code::GetCodeFromTargetAddress(addr);
kasper.lund7276f142008-07-30 08:49:36 +00001590 return code->ic_state() == DEBUG_BREAK;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001591}
1592
1593
1594// Check whether a code stub with the specified major key is a possible break
1595// point location when looking for source break locations.
1596bool Debug::IsSourceBreakStub(Code* code) {
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001597 CodeStub::Major major_key = CodeStub::GetMajorKey(code);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001598 return major_key == CodeStub::CallFunction;
1599}
1600
1601
1602// Check whether a code stub with the specified major key is a possible break
1603// location.
1604bool Debug::IsBreakStub(Code* code) {
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001605 CodeStub::Major major_key = CodeStub::GetMajorKey(code);
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00001606 return major_key == CodeStub::CallFunction;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001607}
1608
1609
1610// Find the builtin to use for invoking the debug break
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001611Handle<Code> Debug::FindDebugBreak(Handle<Code> code, RelocInfo::Mode mode) {
danno@chromium.orgfa458e42012-02-01 10:48:36 +00001612 Isolate* isolate = Isolate::Current();
1613
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001614 // Find the builtin debug break function matching the calling convention
1615 // used by the call site.
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001616 if (code->is_inline_cache_stub()) {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001617 switch (code->kind()) {
1618 case Code::CALL_IC:
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001619 case Code::KEYED_CALL_IC:
danno@chromium.orgfa458e42012-02-01 10:48:36 +00001620 return isolate->stub_cache()->ComputeCallDebugBreak(
1621 code->arguments_count(), code->kind());
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001622
1623 case Code::LOAD_IC:
danno@chromium.orgfa458e42012-02-01 10:48:36 +00001624 return isolate->builtins()->LoadIC_DebugBreak();
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001625
1626 case Code::STORE_IC:
danno@chromium.orgfa458e42012-02-01 10:48:36 +00001627 return isolate->builtins()->StoreIC_DebugBreak();
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001628
1629 case Code::KEYED_LOAD_IC:
danno@chromium.orgfa458e42012-02-01 10:48:36 +00001630 return isolate->builtins()->KeyedLoadIC_DebugBreak();
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001631
1632 case Code::KEYED_STORE_IC:
danno@chromium.orgfa458e42012-02-01 10:48:36 +00001633 return isolate->builtins()->KeyedStoreIC_DebugBreak();
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001634
1635 default:
1636 UNREACHABLE();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001637 }
1638 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001639 if (RelocInfo::IsConstructCall(mode)) {
danno@chromium.orgfa458e42012-02-01 10:48:36 +00001640 if (code->has_function_cache()) {
1641 return isolate->builtins()->CallConstructStub_Recording_DebugBreak();
1642 } else {
1643 return isolate->builtins()->CallConstructStub_DebugBreak();
1644 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001645 }
1646 if (code->kind() == Code::STUB) {
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00001647 ASSERT(code->major_key() == CodeStub::CallFunction);
danno@chromium.orgfa458e42012-02-01 10:48:36 +00001648 if (code->has_function_cache()) {
1649 return isolate->builtins()->CallFunctionStub_Recording_DebugBreak();
1650 } else {
1651 return isolate->builtins()->CallFunctionStub_DebugBreak();
1652 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001653 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001654
1655 UNREACHABLE();
1656 return Handle<Code>::null();
1657}
1658
1659
1660// Simple function for returning the source positions for active break points.
1661Handle<Object> Debug::GetSourceBreakLocations(
1662 Handle<SharedFunctionInfo> shared) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00001663 Isolate* isolate = Isolate::Current();
1664 Heap* heap = isolate->heap();
1665 if (!HasDebugInfo(shared)) return Handle<Object>(heap->undefined_value());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001666 Handle<DebugInfo> debug_info = GetDebugInfo(shared);
1667 if (debug_info->GetBreakPointCount() == 0) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00001668 return Handle<Object>(heap->undefined_value());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001669 }
1670 Handle<FixedArray> locations =
lrn@chromium.org7516f052011-03-30 08:52:27 +00001671 isolate->factory()->NewFixedArray(debug_info->GetBreakPointCount());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001672 int count = 0;
1673 for (int i = 0; i < debug_info->break_points()->length(); i++) {
1674 if (!debug_info->break_points()->get(i)->IsUndefined()) {
1675 BreakPointInfo* break_point_info =
1676 BreakPointInfo::cast(debug_info->break_points()->get(i));
1677 if (break_point_info->GetBreakPointCount() > 0) {
1678 locations->set(count++, break_point_info->statement_position());
1679 }
1680 }
1681 }
1682 return locations;
1683}
1684
1685
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001686void Debug::NewBreak(StackFrame::Id break_frame_id) {
1687 thread_local_.break_frame_id_ = break_frame_id;
1688 thread_local_.break_id_ = ++thread_local_.break_count_;
1689}
1690
1691
1692void Debug::SetBreak(StackFrame::Id break_frame_id, int break_id) {
1693 thread_local_.break_frame_id_ = break_frame_id;
1694 thread_local_.break_id_ = break_id;
1695}
1696
1697
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001698// Handle stepping into a function.
1699void Debug::HandleStepIn(Handle<JSFunction> function,
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001700 Handle<Object> holder,
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001701 Address fp,
1702 bool is_constructor) {
1703 // If the frame pointer is not supplied by the caller find it.
1704 if (fp == 0) {
1705 StackFrameIterator it;
1706 it.Advance();
1707 // For constructor functions skip another frame.
1708 if (is_constructor) {
1709 ASSERT(it.frame()->is_construct());
1710 it.Advance();
1711 }
1712 fp = it.frame()->fp();
1713 }
1714
1715 // Flood the function with one-shot break points if it is called from where
1716 // step into was requested.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001717 if (fp == step_in_fp()) {
rossberg@chromium.org2c067b12012-03-19 11:01:52 +00001718 if (function->shared()->bound()) {
1719 // Handle Function.prototype.bind
1720 Debug::FloodBoundFunctionWithOneShot(function);
1721 } else if (!function->IsBuiltin()) {
1722 // Don't allow step into functions in the native context.
kasperl@chromium.orgacae3782009-04-11 09:17:08 +00001723 if (function->shared()->code() ==
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00001724 Isolate::Current()->builtins()->builtin(Builtins::kFunctionApply) ||
kasperl@chromium.orgacae3782009-04-11 09:17:08 +00001725 function->shared()->code() ==
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00001726 Isolate::Current()->builtins()->builtin(Builtins::kFunctionCall)) {
kasperl@chromium.orgacae3782009-04-11 09:17:08 +00001727 // Handle function.apply and function.call separately to flood the
1728 // function to be called and not the code for Builtins::FunctionApply or
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001729 // Builtins::FunctionCall. The receiver of call/apply is the target
1730 // function.
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001731 if (!holder.is_null() && holder->IsJSFunction() &&
1732 !JSFunction::cast(*holder)->IsBuiltin()) {
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001733 Handle<JSFunction> js_function = Handle<JSFunction>::cast(holder);
1734 Debug::FloodWithOneShot(js_function);
kasperl@chromium.orgacae3782009-04-11 09:17:08 +00001735 }
1736 } else {
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001737 Debug::FloodWithOneShot(function);
kasperl@chromium.orgacae3782009-04-11 09:17:08 +00001738 }
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001739 }
1740 }
1741}
1742
1743
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001744void Debug::ClearStepping() {
1745 // Clear the various stepping setup.
1746 ClearOneShot();
1747 ClearStepIn();
ager@chromium.orga1645e22009-09-09 19:27:10 +00001748 ClearStepOut();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001749 ClearStepNext();
1750
1751 // Clear multiple step counter.
1752 thread_local_.step_count_ = 0;
1753}
1754
1755// Clears all the one-shot break points that are currently set. Normally this
1756// function is called each time a break point is hit as one shot break points
1757// are used to support stepping.
1758void Debug::ClearOneShot() {
1759 // The current implementation just runs through all the breakpoints. When the
v8.team.kasperl727e9952008-09-02 14:56:44 +00001760 // last break point for a function is removed that function is automatically
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001761 // removed from the list.
1762
1763 DebugInfoListNode* node = debug_info_list_;
1764 while (node != NULL) {
1765 BreakLocationIterator it(node->debug_info(), ALL_BREAK_LOCATIONS);
1766 while (!it.Done()) {
1767 it.ClearOneShot();
1768 it.Next();
1769 }
1770 node = node->next();
1771 }
1772}
1773
1774
1775void Debug::ActivateStepIn(StackFrame* frame) {
ager@chromium.orga1645e22009-09-09 19:27:10 +00001776 ASSERT(!StepOutActive());
mmassi@chromium.org7028c052012-06-13 11:51:58 +00001777 thread_local_.step_into_fp_ = frame->UnpaddedFP();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001778}
1779
1780
1781void Debug::ClearStepIn() {
1782 thread_local_.step_into_fp_ = 0;
1783}
1784
1785
ager@chromium.orga1645e22009-09-09 19:27:10 +00001786void Debug::ActivateStepOut(StackFrame* frame) {
1787 ASSERT(!StepInActive());
mmassi@chromium.org7028c052012-06-13 11:51:58 +00001788 thread_local_.step_out_fp_ = frame->UnpaddedFP();
ager@chromium.orga1645e22009-09-09 19:27:10 +00001789}
1790
1791
1792void Debug::ClearStepOut() {
1793 thread_local_.step_out_fp_ = 0;
1794}
1795
1796
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001797void Debug::ClearStepNext() {
1798 thread_local_.last_step_action_ = StepNone;
ager@chromium.org236ad962008-09-25 09:45:57 +00001799 thread_local_.last_statement_position_ = RelocInfo::kNoPosition;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001800 thread_local_.last_fp_ = 0;
1801}
1802
1803
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001804// Helper function to compile full code for debugging. This code will
mmassi@chromium.org7028c052012-06-13 11:51:58 +00001805// have debug break slots and deoptimization information. Deoptimization
1806// information is required in case that an optimized version of this
1807// function is still activated on the stack. It will also make sure that
1808// the full code is compiled with the same flags as the previous version,
1809// that is flags which can change the code generated. The current method
1810// of mapping from already compiled full code without debug break slots
1811// to full code with debug break slots depends on the generated code is
1812// otherwise exactly the same.
1813static bool CompileFullCodeForDebugging(Handle<JSFunction> function,
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001814 Handle<Code> current_code) {
1815 ASSERT(!current_code->has_debug_break_slots());
1816
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001817 CompilationInfoWithZone info(function);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001818 info.MarkCompilingForDebugging(current_code);
1819 ASSERT(!info.shared_info()->is_compiled());
1820 ASSERT(!info.isolate()->has_pending_exception());
1821
1822 // Use compile lazy which will end up compiling the full code in the
1823 // configuration configured above.
1824 bool result = Compiler::CompileLazy(&info);
1825 ASSERT(result != Isolate::Current()->has_pending_exception());
1826 info.isolate()->clear_pending_exception();
1827#if DEBUG
1828 if (result) {
mmassi@chromium.org7028c052012-06-13 11:51:58 +00001829 Handle<Code> new_code(function->shared()->code());
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001830 ASSERT(new_code->has_debug_break_slots());
1831 ASSERT(current_code->is_compiled_optimizable() ==
1832 new_code->is_compiled_optimizable());
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001833 }
1834#endif
1835 return result;
1836}
1837
1838
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001839static void CollectActiveFunctionsFromThread(
1840 Isolate* isolate,
1841 ThreadLocalTop* top,
1842 List<Handle<JSFunction> >* active_functions,
1843 Object* active_code_marker) {
1844 // Find all non-optimized code functions with activation frames
1845 // on the stack. This includes functions which have optimized
1846 // activations (including inlined functions) on the stack as the
1847 // non-optimized code is needed for the lazy deoptimization.
1848 for (JavaScriptFrameIterator it(isolate, top); !it.done(); it.Advance()) {
1849 JavaScriptFrame* frame = it.frame();
1850 if (frame->is_optimized()) {
1851 List<JSFunction*> functions(Compiler::kMaxInliningLevels + 1);
1852 frame->GetFunctions(&functions);
1853 for (int i = 0; i < functions.length(); i++) {
1854 JSFunction* function = functions[i];
1855 active_functions->Add(Handle<JSFunction>(function));
1856 function->shared()->code()->set_gc_metadata(active_code_marker);
1857 }
1858 } else if (frame->function()->IsJSFunction()) {
1859 JSFunction* function = JSFunction::cast(frame->function());
1860 ASSERT(frame->LookupCode()->kind() == Code::FUNCTION);
1861 active_functions->Add(Handle<JSFunction>(function));
1862 function->shared()->code()->set_gc_metadata(active_code_marker);
1863 }
1864 }
1865}
1866
1867
1868static void RedirectActivationsToRecompiledCodeOnThread(
1869 Isolate* isolate,
1870 ThreadLocalTop* top) {
1871 for (JavaScriptFrameIterator it(isolate, top); !it.done(); it.Advance()) {
1872 JavaScriptFrame* frame = it.frame();
1873
1874 if (frame->is_optimized() || !frame->function()->IsJSFunction()) continue;
1875
1876 JSFunction* function = JSFunction::cast(frame->function());
1877
1878 ASSERT(frame->LookupCode()->kind() == Code::FUNCTION);
1879
1880 Handle<Code> frame_code(frame->LookupCode());
1881 if (frame_code->has_debug_break_slots()) continue;
1882
1883 Handle<Code> new_code(function->shared()->code());
1884 if (new_code->kind() != Code::FUNCTION ||
1885 !new_code->has_debug_break_slots()) {
1886 continue;
1887 }
1888
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001889 // Iterate over the RelocInfo in the original code to compute the sum of the
1890 // constant pools sizes. (See Assembler::CheckConstPool())
1891 // Note that this is only useful for architectures using constant pools.
1892 int constpool_mask = RelocInfo::ModeMask(RelocInfo::CONST_POOL);
1893 int frame_const_pool_size = 0;
1894 for (RelocIterator it(*frame_code, constpool_mask); !it.done(); it.next()) {
1895 RelocInfo* info = it.rinfo();
1896 if (info->pc() >= frame->pc()) break;
1897 frame_const_pool_size += static_cast<int>(info->data());
1898 }
1899 intptr_t frame_offset =
1900 frame->pc() - frame_code->instruction_start() - frame_const_pool_size;
1901
1902 // Iterate over the RelocInfo for new code to find the number of bytes
1903 // generated for debug slots and constant pools.
1904 int debug_break_slot_bytes = 0;
1905 int new_code_const_pool_size = 0;
1906 int mask = RelocInfo::ModeMask(RelocInfo::DEBUG_BREAK_SLOT) |
1907 RelocInfo::ModeMask(RelocInfo::CONST_POOL);
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001908 for (RelocIterator it(*new_code, mask); !it.done(); it.next()) {
1909 // Check if the pc in the new code with debug break
1910 // slots is before this slot.
1911 RelocInfo* info = it.rinfo();
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001912 intptr_t new_offset = info->pc() - new_code->instruction_start() -
1913 new_code_const_pool_size - debug_break_slot_bytes;
1914 if (new_offset >= frame_offset) {
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001915 break;
1916 }
1917
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001918 if (RelocInfo::IsDebugBreakSlot(info->rmode())) {
1919 debug_break_slot_bytes += Assembler::kDebugBreakSlotLength;
1920 } else {
1921 ASSERT(RelocInfo::IsConstPool(info->rmode()));
1922 // The size of the constant pool is encoded in the data.
1923 new_code_const_pool_size += static_cast<int>(info->data());
1924 }
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001925 }
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001926
1927 // Compute the equivalent pc in the new code.
1928 byte* new_pc = new_code->instruction_start() + frame_offset +
1929 debug_break_slot_bytes + new_code_const_pool_size;
1930
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001931 if (FLAG_trace_deopt) {
1932 PrintF("Replacing code %08" V8PRIxPTR " - %08" V8PRIxPTR " (%d) "
1933 "with %08" V8PRIxPTR " - %08" V8PRIxPTR " (%d) "
1934 "for debugging, "
1935 "changing pc from %08" V8PRIxPTR " to %08" V8PRIxPTR "\n",
1936 reinterpret_cast<intptr_t>(
1937 frame_code->instruction_start()),
1938 reinterpret_cast<intptr_t>(
1939 frame_code->instruction_start()) +
1940 frame_code->instruction_size(),
1941 frame_code->instruction_size(),
1942 reinterpret_cast<intptr_t>(new_code->instruction_start()),
1943 reinterpret_cast<intptr_t>(new_code->instruction_start()) +
1944 new_code->instruction_size(),
1945 new_code->instruction_size(),
1946 reinterpret_cast<intptr_t>(frame->pc()),
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001947 reinterpret_cast<intptr_t>(new_pc));
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001948 }
1949
1950 // Patch the return address to return into the code with
1951 // debug break slots.
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001952 frame->set_pc(new_pc);
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001953 }
1954}
1955
1956
1957class ActiveFunctionsCollector : public ThreadVisitor {
1958 public:
1959 explicit ActiveFunctionsCollector(List<Handle<JSFunction> >* active_functions,
1960 Object* active_code_marker)
1961 : active_functions_(active_functions),
1962 active_code_marker_(active_code_marker) { }
1963
1964 void VisitThread(Isolate* isolate, ThreadLocalTop* top) {
1965 CollectActiveFunctionsFromThread(isolate,
1966 top,
1967 active_functions_,
1968 active_code_marker_);
1969 }
1970
1971 private:
1972 List<Handle<JSFunction> >* active_functions_;
1973 Object* active_code_marker_;
1974};
1975
1976
1977class ActiveFunctionsRedirector : public ThreadVisitor {
1978 public:
1979 void VisitThread(Isolate* isolate, ThreadLocalTop* top) {
1980 RedirectActivationsToRecompiledCodeOnThread(isolate, top);
1981 }
1982};
1983
1984
lrn@chromium.org34e60782011-09-15 07:25:40 +00001985void Debug::PrepareForBreakPoints() {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001986 // If preparing for the first break point make sure to deoptimize all
1987 // functions as debugging does not work with optimized code.
1988 if (!has_break_points_) {
1989 Deoptimizer::DeoptimizeAll();
lrn@chromium.org34e60782011-09-15 07:25:40 +00001990
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001991 Handle<Code> lazy_compile =
1992 Handle<Code>(isolate_->builtins()->builtin(Builtins::kLazyCompile));
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001993
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001994 // There will be at least one break point when we are done.
1995 has_break_points_ = true;
1996
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001997 // Keep the list of activated functions in a handlified list as it
1998 // is used both in GC and non-GC code.
1999 List<Handle<JSFunction> > active_functions(100);
lrn@chromium.org34e60782011-09-15 07:25:40 +00002000
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002001 {
2002 // We are going to iterate heap to find all functions without
2003 // debug break slots.
rossberg@chromium.org994edf62012-02-06 10:12:55 +00002004 isolate_->heap()->CollectAllGarbage(Heap::kMakeHeapIterableMask,
2005 "preparing for breakpoints");
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002006
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002007 // Ensure no GC in this scope as we are going to use gc_metadata
2008 // field in the Code object to mark active functions.
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002009 AssertNoAllocation no_allocation;
2010
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002011 Object* active_code_marker = isolate_->heap()->the_hole_value();
svenpanne@chromium.orgecb9dd62011-12-01 08:22:35 +00002012
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002013 CollectActiveFunctionsFromThread(isolate_,
2014 isolate_->thread_local_top(),
2015 &active_functions,
2016 active_code_marker);
2017 ActiveFunctionsCollector active_functions_collector(&active_functions,
2018 active_code_marker);
2019 isolate_->thread_manager()->IterateArchivedThreads(
2020 &active_functions_collector);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002021
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002022 // Scan the heap for all non-optimized functions which have no
2023 // debug break slots and are not active or inlined into an active
2024 // function and mark them for lazy compilation.
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002025 HeapIterator iterator;
2026 HeapObject* obj = NULL;
2027 while (((obj = iterator.next()) != NULL)) {
2028 if (obj->IsJSFunction()) {
2029 JSFunction* function = JSFunction::cast(obj);
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002030 SharedFunctionInfo* shared = function->shared();
2031 if (shared->allows_lazy_compilation() &&
2032 shared->script()->IsScript() &&
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002033 function->code()->kind() == Code::FUNCTION &&
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002034 !function->code()->has_debug_break_slots() &&
2035 shared->code()->gc_metadata() != active_code_marker) {
2036 function->set_code(*lazy_compile);
2037 function->shared()->set_code(*lazy_compile);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002038 }
2039 }
lrn@chromium.org34e60782011-09-15 07:25:40 +00002040 }
lrn@chromium.org34e60782011-09-15 07:25:40 +00002041
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002042 // Clear gc_metadata field.
2043 for (int i = 0; i < active_functions.length(); i++) {
2044 Handle<JSFunction> function = active_functions[i];
2045 function->shared()->code()->set_gc_metadata(Smi::FromInt(0));
2046 }
2047 }
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002048
2049 // Now recompile all functions with activation frames and and
2050 // patch the return address to run in the new compiled code.
2051 for (int i = 0; i < active_functions.length(); i++) {
2052 Handle<JSFunction> function = active_functions[i];
mmassi@chromium.org7028c052012-06-13 11:51:58 +00002053 Handle<SharedFunctionInfo> shared(function->shared());
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002054
2055 if (function->code()->kind() == Code::FUNCTION &&
2056 function->code()->has_debug_break_slots()) {
2057 // Nothing to do. Function code already had debug break slots.
2058 continue;
2059 }
2060
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002061 // If recompilation is not possible just skip it.
2062 if (shared->is_toplevel() ||
2063 !shared->allows_lazy_compilation() ||
2064 shared->code()->kind() == Code::BUILTIN) {
2065 continue;
2066 }
2067
2068 // Make sure that the shared full code is compiled with debug
2069 // break slots.
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00002070 if (!shared->code()->has_debug_break_slots()) {
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002071 // Try to compile the full code with debug break slots. If it
2072 // fails just keep the current code.
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00002073 Handle<Code> current_code(function->shared()->code());
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002074 shared->set_code(*lazy_compile);
2075 bool prev_force_debugger_active =
2076 isolate_->debugger()->force_debugger_active();
2077 isolate_->debugger()->set_force_debugger_active(true);
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00002078 ASSERT(current_code->kind() == Code::FUNCTION);
mmassi@chromium.org7028c052012-06-13 11:51:58 +00002079 CompileFullCodeForDebugging(function, current_code);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002080 isolate_->debugger()->set_force_debugger_active(
2081 prev_force_debugger_active);
2082 if (!shared->is_compiled()) {
2083 shared->set_code(*current_code);
2084 continue;
2085 }
2086 }
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002087
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002088 // Keep function code in sync with shared function info.
2089 function->set_code(shared->code());
lrn@chromium.org34e60782011-09-15 07:25:40 +00002090 }
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002091
2092 RedirectActivationsToRecompiledCodeOnThread(isolate_,
2093 isolate_->thread_local_top());
2094
2095 ActiveFunctionsRedirector active_functions_redirector;
2096 isolate_->thread_manager()->IterateArchivedThreads(
2097 &active_functions_redirector);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002098 }
lrn@chromium.org34e60782011-09-15 07:25:40 +00002099}
2100
2101
jkummerow@chromium.org78502a92012-09-06 13:50:42 +00002102Object* Debug::FindSharedFunctionInfoInScript(Handle<Script> script,
2103 int position) {
2104 // Iterate the heap looking for SharedFunctionInfo generated from the
2105 // script. The inner most SharedFunctionInfo containing the source position
2106 // for the requested break point is found.
2107 // NOTE: This might require several heap iterations. If the SharedFunctionInfo
2108 // which is found is not compiled it is compiled and the heap is iterated
2109 // again as the compilation might create inner functions from the newly
2110 // compiled function and the actual requested break point might be in one of
2111 // these functions.
2112 // NOTE: The below fix-point iteration depends on all functions that cannot be
2113 // compiled lazily without a context to not be compiled at all. Compilation
2114 // will be triggered at points where we do not need a context.
2115 bool done = false;
2116 // The current candidate for the source position:
2117 int target_start_position = RelocInfo::kNoPosition;
2118 Handle<JSFunction> target_function;
2119 Handle<SharedFunctionInfo> target;
2120 while (!done) {
2121 { // Extra scope for iterator and no-allocation.
2122 isolate_->heap()->EnsureHeapIsIterable();
2123 AssertNoAllocation no_alloc_during_heap_iteration;
2124 HeapIterator iterator;
2125 for (HeapObject* obj = iterator.next();
2126 obj != NULL; obj = iterator.next()) {
2127 bool found_next_candidate = false;
2128 Handle<JSFunction> function;
2129 Handle<SharedFunctionInfo> shared;
2130 if (obj->IsJSFunction()) {
2131 function = Handle<JSFunction>(JSFunction::cast(obj));
2132 shared = Handle<SharedFunctionInfo>(function->shared());
2133 ASSERT(shared->allows_lazy_compilation() || shared->is_compiled());
2134 found_next_candidate = true;
2135 } else if (obj->IsSharedFunctionInfo()) {
2136 shared = Handle<SharedFunctionInfo>(SharedFunctionInfo::cast(obj));
2137 // Skip functions that we cannot compile lazily without a context,
2138 // which is not available here, because there is no closure.
2139 found_next_candidate = shared->is_compiled() ||
2140 shared->allows_lazy_compilation_without_context();
2141 }
2142 if (!found_next_candidate) continue;
2143 if (shared->script() == *script) {
2144 // If the SharedFunctionInfo found has the requested script data and
2145 // contains the source position it is a candidate.
2146 int start_position = shared->function_token_position();
2147 if (start_position == RelocInfo::kNoPosition) {
2148 start_position = shared->start_position();
2149 }
2150 if (start_position <= position &&
2151 position <= shared->end_position()) {
2152 // If there is no candidate or this function is within the current
2153 // candidate this is the new candidate.
2154 if (target.is_null()) {
2155 target_start_position = start_position;
2156 target_function = function;
2157 target = shared;
2158 } else {
2159 if (target_start_position == start_position &&
2160 shared->end_position() == target->end_position()) {
2161 // If a top-level function contains only one function
2162 // declaration the source for the top-level and the function
2163 // is the same. In that case prefer the non top-level function.
2164 if (!shared->is_toplevel()) {
2165 target_start_position = start_position;
2166 target_function = function;
2167 target = shared;
2168 }
2169 } else if (target_start_position <= start_position &&
2170 shared->end_position() <= target->end_position()) {
2171 // This containment check includes equality as a function
2172 // inside a top-level function can share either start or end
2173 // position with the top-level function.
2174 target_start_position = start_position;
2175 target_function = function;
2176 target = shared;
2177 }
2178 }
2179 }
2180 }
2181 } // End for loop.
2182 } // End no-allocation scope.
2183
2184 if (target.is_null()) {
2185 return isolate_->heap()->undefined_value();
2186 }
2187
2188 // There will be at least one break point when we are done.
2189 has_break_points_ = true;
2190
2191 // If the candidate found is compiled we are done.
2192 done = target->is_compiled();
2193 if (!done) {
2194 // If the candidate is not compiled, compile it to reveal any inner
2195 // functions which might contain the requested source position. This
2196 // will compile all inner functions that cannot be compiled without a
2197 // context, because Compiler::BuildFunctionInfo checks whether the
2198 // debugger is active.
2199 if (target_function.is_null()) {
2200 SharedFunctionInfo::CompileLazy(target, KEEP_EXCEPTION);
2201 } else {
2202 JSFunction::CompileLazy(target_function, KEEP_EXCEPTION);
2203 }
2204 }
2205 } // End while loop.
2206
2207 return *target;
2208}
2209
2210
lrn@chromium.org34e60782011-09-15 07:25:40 +00002211// Ensures the debug information is present for shared.
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00002212bool Debug::EnsureDebugInfo(Handle<SharedFunctionInfo> shared,
2213 Handle<JSFunction> function) {
lrn@chromium.org34e60782011-09-15 07:25:40 +00002214 // Return if we already have the debug info for shared.
2215 if (HasDebugInfo(shared)) {
2216 ASSERT(shared->is_compiled());
2217 return true;
2218 }
2219
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00002220 // There will be at least one break point when we are done.
2221 has_break_points_ = true;
2222
2223 // Ensure function is compiled. Return false if this failed.
2224 if (!function.is_null() &&
2225 !JSFunction::EnsureCompiled(function, CLEAR_EXCEPTION)) {
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002226 return false;
2227 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002228
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002229 // Create the debug info object.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002230 Handle<DebugInfo> debug_info = FACTORY->NewDebugInfo(shared);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002231
2232 // Add debug info to the list.
2233 DebugInfoListNode* node = new DebugInfoListNode(*debug_info);
2234 node->set_next(debug_info_list_);
2235 debug_info_list_ = node;
2236
kasper.lundbd3ec4e2008-07-09 11:06:54 +00002237 return true;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002238}
2239
2240
2241void Debug::RemoveDebugInfo(Handle<DebugInfo> debug_info) {
2242 ASSERT(debug_info_list_ != NULL);
2243 // Run through the debug info objects to find this one and remove it.
2244 DebugInfoListNode* prev = NULL;
2245 DebugInfoListNode* current = debug_info_list_;
2246 while (current != NULL) {
2247 if (*current->debug_info() == *debug_info) {
2248 // Unlink from list. If prev is NULL we are looking at the first element.
2249 if (prev == NULL) {
2250 debug_info_list_ = current->next();
2251 } else {
2252 prev->set_next(current->next());
2253 }
lrn@chromium.org7516f052011-03-30 08:52:27 +00002254 current->debug_info()->shared()->set_debug_info(
2255 isolate_->heap()->undefined_value());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002256 delete current;
2257
2258 // If there are no more debug info objects there are not more break
2259 // points.
2260 has_break_points_ = debug_info_list_ != NULL;
2261
2262 return;
2263 }
2264 // Move to next in list.
2265 prev = current;
2266 current = current->next();
2267 }
2268 UNREACHABLE();
2269}
2270
2271
2272void Debug::SetAfterBreakTarget(JavaScriptFrame* frame) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002273 HandleScope scope(isolate_);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002274
lrn@chromium.org34e60782011-09-15 07:25:40 +00002275 PrepareForBreakPoints();
2276
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002277 // Get the executing function in which the debug break occurred.
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00002278 Handle<JSFunction> function(JSFunction::cast(frame->function()));
2279 Handle<SharedFunctionInfo> shared(function->shared());
2280 if (!EnsureDebugInfo(shared, function)) {
kasper.lundbd3ec4e2008-07-09 11:06:54 +00002281 // Return if we failed to retrieve the debug info.
2282 return;
2283 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002284 Handle<DebugInfo> debug_info = GetDebugInfo(shared);
2285 Handle<Code> code(debug_info->code());
2286 Handle<Code> original_code(debug_info->original_code());
2287#ifdef DEBUG
2288 // Get the code which is actually executing.
vegorov@chromium.org74f333b2011-04-06 11:17:46 +00002289 Handle<Code> frame_code(frame->LookupCode());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002290 ASSERT(frame_code.is_identical_to(code));
2291#endif
2292
2293 // Find the call address in the running code. This address holds the call to
2294 // either a DebugBreakXXX or to the debug break return entry code if the
2295 // break point is still active after processing the break point.
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00002296 Address addr = frame->pc() - Assembler::kPatchDebugBreakSlotReturnOffset;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002297
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002298 // Check if the location is at JS exit or debug break slot.
ager@chromium.orga1645e22009-09-09 19:27:10 +00002299 bool at_js_return = false;
2300 bool break_at_js_return_active = false;
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002301 bool at_debug_break_slot = false;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002302 RelocIterator it(debug_info->code());
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002303 while (!it.done() && !at_js_return && !at_debug_break_slot) {
ager@chromium.org236ad962008-09-25 09:45:57 +00002304 if (RelocInfo::IsJSReturn(it.rinfo()->rmode())) {
ager@chromium.orga1645e22009-09-09 19:27:10 +00002305 at_js_return = (it.rinfo()->pc() ==
2306 addr - Assembler::kPatchReturnSequenceAddressOffset);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002307 break_at_js_return_active = it.rinfo()->IsPatchedReturnSequence();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002308 }
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002309 if (RelocInfo::IsDebugBreakSlot(it.rinfo()->rmode())) {
2310 at_debug_break_slot = (it.rinfo()->pc() ==
2311 addr - Assembler::kPatchDebugBreakSlotAddressOffset);
2312 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002313 it.next();
2314 }
2315
2316 // Handle the jump to continue execution after break point depending on the
2317 // break location.
ager@chromium.orga1645e22009-09-09 19:27:10 +00002318 if (at_js_return) {
2319 // If the break point as return is still active jump to the corresponding
2320 // place in the original code. If not the break point was removed during
2321 // break point processing.
2322 if (break_at_js_return_active) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002323 addr += original_code->instruction_start() - code->instruction_start();
2324 }
2325
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00002326 // Move back to where the call instruction sequence started.
2327 thread_local_.after_break_target_ =
2328 addr - Assembler::kPatchReturnSequenceAddressOffset;
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002329 } else if (at_debug_break_slot) {
2330 // Address of where the debug break slot starts.
2331 addr = addr - Assembler::kPatchDebugBreakSlotAddressOffset;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002332
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002333 // Continue just after the slot.
2334 thread_local_.after_break_target_ = addr + Assembler::kDebugBreakSlotLength;
2335 } else if (IsDebugBreak(Assembler::target_address_at(addr))) {
2336 // We now know that there is still a debug break call at the target address,
2337 // so the break point is still there and the original code will hold the
2338 // address to jump to in order to complete the call which is replaced by a
2339 // call to DebugBreakXXX.
2340
2341 // Find the corresponding address in the original code.
2342 addr += original_code->instruction_start() - code->instruction_start();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002343
2344 // Install jump to the call address in the original code. This will be the
2345 // call which was overwritten by the call to DebugBreakXXX.
2346 thread_local_.after_break_target_ = Assembler::target_address_at(addr);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002347 } else {
2348 // There is no longer a break point present. Don't try to look in the
2349 // original code as the running code will have the right address. This takes
2350 // care of the case where the last break point is removed from the function
2351 // and therefore no "original code" is available.
2352 thread_local_.after_break_target_ = Assembler::target_address_at(addr);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002353 }
2354}
2355
2356
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00002357bool Debug::IsBreakAtReturn(JavaScriptFrame* frame) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002358 HandleScope scope(isolate_);
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00002359
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00002360 // If there are no break points this cannot be break at return, as
2361 // the debugger statement and stack guard bebug break cannot be at
2362 // return.
2363 if (!has_break_points_) {
2364 return false;
2365 }
2366
lrn@chromium.org34e60782011-09-15 07:25:40 +00002367 PrepareForBreakPoints();
2368
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00002369 // Get the executing function in which the debug break occurred.
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00002370 Handle<JSFunction> function(JSFunction::cast(frame->function()));
2371 Handle<SharedFunctionInfo> shared(function->shared());
2372 if (!EnsureDebugInfo(shared, function)) {
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00002373 // Return if we failed to retrieve the debug info.
2374 return false;
2375 }
2376 Handle<DebugInfo> debug_info = GetDebugInfo(shared);
2377 Handle<Code> code(debug_info->code());
2378#ifdef DEBUG
2379 // Get the code which is actually executing.
vegorov@chromium.org74f333b2011-04-06 11:17:46 +00002380 Handle<Code> frame_code(frame->LookupCode());
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00002381 ASSERT(frame_code.is_identical_to(code));
2382#endif
2383
2384 // Find the call address in the running code.
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00002385 Address addr = frame->pc() - Assembler::kPatchDebugBreakSlotReturnOffset;
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00002386
2387 // Check if the location is at JS return.
2388 RelocIterator it(debug_info->code());
2389 while (!it.done()) {
2390 if (RelocInfo::IsJSReturn(it.rinfo()->rmode())) {
2391 return (it.rinfo()->pc() ==
2392 addr - Assembler::kPatchReturnSequenceAddressOffset);
2393 }
2394 it.next();
2395 }
2396 return false;
2397}
2398
2399
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00002400void Debug::FramesHaveBeenDropped(StackFrame::Id new_break_frame_id,
whesse@chromium.orge90029b2010-08-02 11:52:17 +00002401 FrameDropMode mode,
2402 Object** restarter_frame_function_pointer) {
ulan@chromium.orgd9e468a2012-06-25 09:47:40 +00002403 if (mode != CURRENTLY_SET_MODE) {
2404 thread_local_.frame_drop_mode_ = mode;
2405 }
ager@chromium.org357bf652010-04-12 11:30:10 +00002406 thread_local_.break_frame_id_ = new_break_frame_id;
whesse@chromium.orge90029b2010-08-02 11:52:17 +00002407 thread_local_.restarter_frame_function_pointer_ =
2408 restarter_frame_function_pointer;
ager@chromium.org357bf652010-04-12 11:30:10 +00002409}
2410
2411
jkummerow@chromium.org212d9642012-05-11 15:02:09 +00002412const int Debug::FramePaddingLayout::kInitialSize = 1;
2413
2414
2415// Any even value bigger than kInitialSize as needed for stack scanning.
2416const int Debug::FramePaddingLayout::kPaddingValue = kInitialSize + 1;
2417
2418
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002419bool Debug::IsDebugGlobal(GlobalObject* global) {
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00002420 return IsLoaded() && global == debug_context()->global_object();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002421}
2422
2423
ager@chromium.org32912102009-01-16 10:38:43 +00002424void Debug::ClearMirrorCache() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002425 PostponeInterruptsScope postpone(isolate_);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002426 HandleScope scope(isolate_);
2427 ASSERT(isolate_->context() == *Debug::debug_context());
ager@chromium.org32912102009-01-16 10:38:43 +00002428
2429 // Clear the mirror cache.
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00002430 Handle<String> function_name = isolate_->factory()->LookupOneByteSymbol(
2431 STATIC_ASCII_VECTOR("ClearMirrorCache"));
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00002432 Handle<Object> fun(
2433 Isolate::Current()->global_object()->GetPropertyNoExceptionThrown(
lrn@chromium.org303ada72010-10-27 09:33:13 +00002434 *function_name));
ager@chromium.org32912102009-01-16 10:38:43 +00002435 ASSERT(fun->IsJSFunction());
2436 bool caught_exception;
rossberg@chromium.org717967f2011-07-20 13:44:42 +00002437 Execution::TryCall(Handle<JSFunction>::cast(fun),
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00002438 Handle<JSObject>(Debug::debug_context()->global_object()),
ager@chromium.org32912102009-01-16 10:38:43 +00002439 0, NULL, &caught_exception);
2440}
2441
2442
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002443void Debug::CreateScriptCache() {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002444 Heap* heap = isolate_->heap();
2445 HandleScope scope(isolate_);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002446
2447 // Perform two GCs to get rid of all unreferenced scripts. The first GC gets
2448 // rid of all the cached script wrappers and the second gets rid of the
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002449 // scripts which are no longer referenced. The second also sweeps precisely,
2450 // which saves us doing yet another GC to make the heap iterable.
rossberg@chromium.org994edf62012-02-06 10:12:55 +00002451 heap->CollectAllGarbage(Heap::kNoGCFlags, "Debug::CreateScriptCache");
2452 heap->CollectAllGarbage(Heap::kMakeHeapIterableMask,
2453 "Debug::CreateScriptCache");
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002454
2455 ASSERT(script_cache_ == NULL);
2456 script_cache_ = new ScriptCache();
2457
2458 // Scan heap for Script objects.
2459 int count = 0;
2460 HeapIterator iterator;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002461 AssertNoAllocation no_allocation;
2462
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002463 for (HeapObject* obj = iterator.next(); obj != NULL; obj = iterator.next()) {
sgjesse@chromium.org152a0b02009-10-07 13:50:16 +00002464 if (obj->IsScript() && Script::cast(obj)->HasValidSource()) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002465 script_cache_->Add(Handle<Script>(Script::cast(obj)));
2466 count++;
2467 }
2468 }
2469}
2470
2471
2472void Debug::DestroyScriptCache() {
2473 // Get rid of the script cache if it was created.
2474 if (script_cache_ != NULL) {
2475 delete script_cache_;
2476 script_cache_ = NULL;
2477 }
2478}
2479
2480
2481void Debug::AddScriptToScriptCache(Handle<Script> script) {
2482 if (script_cache_ != NULL) {
2483 script_cache_->Add(script);
2484 }
2485}
2486
2487
2488Handle<FixedArray> Debug::GetLoadedScripts() {
2489 // Create and fill the script cache when the loaded scripts is requested for
2490 // the first time.
2491 if (script_cache_ == NULL) {
2492 CreateScriptCache();
2493 }
2494
2495 // If the script cache is not active just return an empty array.
2496 ASSERT(script_cache_ != NULL);
2497 if (script_cache_ == NULL) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002498 isolate_->factory()->NewFixedArray(0);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002499 }
2500
2501 // Perform GC to get unreferenced scripts evicted from the cache before
2502 // returning the content.
rossberg@chromium.org994edf62012-02-06 10:12:55 +00002503 isolate_->heap()->CollectAllGarbage(Heap::kNoGCFlags,
2504 "Debug::GetLoadedScripts");
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002505
2506 // Get the scripts from the cache.
2507 return script_cache_->GetScripts();
2508}
2509
2510
2511void Debug::AfterGarbageCollection() {
2512 // Generate events for collected scripts.
2513 if (script_cache_ != NULL) {
2514 script_cache_->ProcessCollectedScripts();
2515 }
2516}
2517
2518
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00002519Debugger::Debugger(Isolate* isolate)
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +00002520 : debugger_access_(isolate->debugger_access()),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002521 event_listener_(Handle<Object>()),
2522 event_listener_data_(Handle<Object>()),
2523 compiling_natives_(false),
2524 is_loading_debugger_(false),
mstarzinger@chromium.orgde886792012-09-11 13:22:37 +00002525 live_edit_enabled_(true),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002526 never_unload_debugger_(false),
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002527 force_debugger_active_(false),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002528 message_handler_(NULL),
2529 debugger_unload_pending_(false),
2530 host_dispatch_handler_(NULL),
2531 dispatch_handler_access_(OS::CreateMutex()),
2532 debug_message_dispatch_handler_(NULL),
2533 message_dispatch_helper_thread_(NULL),
2534 host_dispatch_micros_(100 * 1000),
2535 agent_(NULL),
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00002536 command_queue_(isolate->logger(), kQueueInitialSize),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002537 command_received_(OS::CreateSemaphore(0)),
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00002538 event_command_queue_(isolate->logger(), kQueueInitialSize),
2539 isolate_(isolate) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002540}
2541
2542
2543Debugger::~Debugger() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002544 delete dispatch_handler_access_;
2545 dispatch_handler_access_ = 0;
2546 delete command_received_;
2547 command_received_ = 0;
2548}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002549
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002550
2551Handle<Object> Debugger::MakeJSObject(Vector<const char> constructor_name,
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002552 int argc,
2553 Handle<Object> argv[],
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002554 bool* caught_exception) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002555 ASSERT(isolate_->context() == *isolate_->debug()->debug_context());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002556
2557 // Create the execution state object.
lrn@chromium.org7516f052011-03-30 08:52:27 +00002558 Handle<String> constructor_str =
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00002559 isolate_->factory()->LookupUtf8Symbol(constructor_name);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002560 Handle<Object> constructor(
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00002561 isolate_->global_object()->GetPropertyNoExceptionThrown(
2562 *constructor_str));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002563 ASSERT(constructor->IsJSFunction());
2564 if (!constructor->IsJSFunction()) {
2565 *caught_exception = true;
lrn@chromium.org7516f052011-03-30 08:52:27 +00002566 return isolate_->factory()->undefined_value();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002567 }
2568 Handle<Object> js_object = Execution::TryCall(
2569 Handle<JSFunction>::cast(constructor),
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00002570 Handle<JSObject>(isolate_->debug()->debug_context()->global_object()),
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002571 argc,
2572 argv,
2573 caught_exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002574 return js_object;
2575}
2576
2577
2578Handle<Object> Debugger::MakeExecutionState(bool* caught_exception) {
2579 // Create the execution state object.
lrn@chromium.org7516f052011-03-30 08:52:27 +00002580 Handle<Object> break_id = isolate_->factory()->NewNumberFromInt(
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002581 isolate_->debug()->break_id());
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002582 Handle<Object> argv[] = { break_id };
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002583 return MakeJSObject(CStrVector("MakeExecutionState"),
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002584 ARRAY_SIZE(argv),
2585 argv,
2586 caught_exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002587}
2588
2589
2590Handle<Object> Debugger::MakeBreakEvent(Handle<Object> exec_state,
2591 Handle<Object> break_points_hit,
2592 bool* caught_exception) {
2593 // Create the new break event object.
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002594 Handle<Object> argv[] = { exec_state, break_points_hit };
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002595 return MakeJSObject(CStrVector("MakeBreakEvent"),
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002596 ARRAY_SIZE(argv),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002597 argv,
2598 caught_exception);
2599}
2600
2601
2602Handle<Object> Debugger::MakeExceptionEvent(Handle<Object> exec_state,
2603 Handle<Object> exception,
2604 bool uncaught,
2605 bool* caught_exception) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002606 Factory* factory = isolate_->factory();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002607 // Create the new exception event object.
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002608 Handle<Object> argv[] = { exec_state,
2609 exception,
2610 factory->ToBoolean(uncaught) };
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002611 return MakeJSObject(CStrVector("MakeExceptionEvent"),
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002612 ARRAY_SIZE(argv),
2613 argv,
2614 caught_exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002615}
2616
2617
2618Handle<Object> Debugger::MakeNewFunctionEvent(Handle<Object> function,
2619 bool* caught_exception) {
2620 // Create the new function event object.
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002621 Handle<Object> argv[] = { function };
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002622 return MakeJSObject(CStrVector("MakeNewFunctionEvent"),
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002623 ARRAY_SIZE(argv),
2624 argv,
2625 caught_exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002626}
2627
2628
2629Handle<Object> Debugger::MakeCompileEvent(Handle<Script> script,
iposva@chromium.org245aa852009-02-10 00:49:54 +00002630 bool before,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002631 bool* caught_exception) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002632 Factory* factory = isolate_->factory();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002633 // Create the compile event object.
2634 Handle<Object> exec_state = MakeExecutionState(caught_exception);
iposva@chromium.org245aa852009-02-10 00:49:54 +00002635 Handle<Object> script_wrapper = GetScriptWrapper(script);
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002636 Handle<Object> argv[] = { exec_state,
2637 script_wrapper,
2638 factory->ToBoolean(before) };
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002639 return MakeJSObject(CStrVector("MakeCompileEvent"),
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002640 ARRAY_SIZE(argv),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002641 argv,
2642 caught_exception);
2643}
2644
2645
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002646Handle<Object> Debugger::MakeScriptCollectedEvent(int id,
2647 bool* caught_exception) {
2648 // Create the script collected event object.
2649 Handle<Object> exec_state = MakeExecutionState(caught_exception);
2650 Handle<Object> id_object = Handle<Smi>(Smi::FromInt(id));
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002651 Handle<Object> argv[] = { exec_state, id_object };
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002652
2653 return MakeJSObject(CStrVector("MakeScriptCollectedEvent"),
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002654 ARRAY_SIZE(argv),
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002655 argv,
2656 caught_exception);
2657}
2658
2659
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002660void Debugger::OnException(Handle<Object> exception, bool uncaught) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002661 HandleScope scope(isolate_);
2662 Debug* debug = isolate_->debug();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002663
2664 // Bail out based on state or if there is no listener for this event
lrn@chromium.org7516f052011-03-30 08:52:27 +00002665 if (debug->InDebugger()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002666 if (!Debugger::EventActive(v8::Exception)) return;
2667
2668 // Bail out if exception breaks are not active
2669 if (uncaught) {
2670 // Uncaught exceptions are reported by either flags.
lrn@chromium.org7516f052011-03-30 08:52:27 +00002671 if (!(debug->break_on_uncaught_exception() ||
2672 debug->break_on_exception())) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002673 } else {
2674 // Caught exceptions are reported is activated.
lrn@chromium.org7516f052011-03-30 08:52:27 +00002675 if (!debug->break_on_exception()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002676 }
2677
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002678 // Enter the debugger.
2679 EnterDebugger debugger;
2680 if (debugger.FailedToEnter()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002681
2682 // Clear all current stepping setup.
lrn@chromium.org7516f052011-03-30 08:52:27 +00002683 debug->ClearStepping();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002684 // Create the event data object.
2685 bool caught_exception = false;
2686 Handle<Object> exec_state = MakeExecutionState(&caught_exception);
2687 Handle<Object> event_data;
2688 if (!caught_exception) {
2689 event_data = MakeExceptionEvent(exec_state, exception, uncaught,
2690 &caught_exception);
2691 }
2692 // Bail out and don't call debugger if exception.
2693 if (caught_exception) {
2694 return;
2695 }
2696
ager@chromium.org5ec48922009-05-05 07:25:34 +00002697 // Process debug event.
2698 ProcessDebugEvent(v8::Exception, Handle<JSObject>::cast(event_data), false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002699 // Return to continue execution from where the exception was thrown.
2700}
2701
2702
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002703void Debugger::OnDebugBreak(Handle<Object> break_points_hit,
2704 bool auto_continue) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002705 HandleScope scope(isolate_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002706
kasper.lund212ac232008-07-16 07:07:30 +00002707 // Debugger has already been entered by caller.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002708 ASSERT(isolate_->context() == *isolate_->debug()->debug_context());
kasper.lund212ac232008-07-16 07:07:30 +00002709
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002710 // Bail out if there is no listener for this event
2711 if (!Debugger::EventActive(v8::Break)) return;
2712
2713 // Debugger must be entered in advance.
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00002714 ASSERT(isolate_->context() == *isolate_->debug()->debug_context());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002715
2716 // Create the event data object.
2717 bool caught_exception = false;
2718 Handle<Object> exec_state = MakeExecutionState(&caught_exception);
2719 Handle<Object> event_data;
2720 if (!caught_exception) {
2721 event_data = MakeBreakEvent(exec_state, break_points_hit,
2722 &caught_exception);
2723 }
2724 // Bail out and don't call debugger if exception.
2725 if (caught_exception) {
2726 return;
2727 }
2728
ager@chromium.org5ec48922009-05-05 07:25:34 +00002729 // Process debug event.
2730 ProcessDebugEvent(v8::Break,
2731 Handle<JSObject>::cast(event_data),
2732 auto_continue);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002733}
2734
2735
2736void Debugger::OnBeforeCompile(Handle<Script> script) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002737 HandleScope scope(isolate_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002738
2739 // Bail out based on state or if there is no listener for this event
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002740 if (isolate_->debug()->InDebugger()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002741 if (compiling_natives()) return;
2742 if (!EventActive(v8::BeforeCompile)) return;
2743
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002744 // Enter the debugger.
2745 EnterDebugger debugger;
2746 if (debugger.FailedToEnter()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002747
2748 // Create the event data object.
2749 bool caught_exception = false;
iposva@chromium.org245aa852009-02-10 00:49:54 +00002750 Handle<Object> event_data = MakeCompileEvent(script, true, &caught_exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002751 // Bail out and don't call debugger if exception.
2752 if (caught_exception) {
2753 return;
2754 }
2755
ager@chromium.org5ec48922009-05-05 07:25:34 +00002756 // Process debug event.
2757 ProcessDebugEvent(v8::BeforeCompile,
2758 Handle<JSObject>::cast(event_data),
2759 true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002760}
2761
2762
2763// Handle debugger actions when a new script is compiled.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00002764void Debugger::OnAfterCompile(Handle<Script> script,
2765 AfterCompileFlags after_compile_flags) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002766 HandleScope scope(isolate_);
2767 Debug* debug = isolate_->debug();
kasper.lund212ac232008-07-16 07:07:30 +00002768
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002769 // Add the newly compiled script to the script cache.
lrn@chromium.org7516f052011-03-30 08:52:27 +00002770 debug->AddScriptToScriptCache(script);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002771
2772 // No more to do if not debugging.
ager@chromium.org71daaf62009-04-01 07:22:49 +00002773 if (!IsDebuggerActive()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002774
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002775 // No compile events while compiling natives.
2776 if (compiling_natives()) return;
2777
iposva@chromium.org245aa852009-02-10 00:49:54 +00002778 // Store whether in debugger before entering debugger.
lrn@chromium.org7516f052011-03-30 08:52:27 +00002779 bool in_debugger = debug->InDebugger();
iposva@chromium.org245aa852009-02-10 00:49:54 +00002780
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002781 // Enter the debugger.
2782 EnterDebugger debugger;
2783 if (debugger.FailedToEnter()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002784
2785 // If debugging there might be script break points registered for this
2786 // script. Make sure that these break points are set.
2787
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002788 // Get the function UpdateScriptBreakPoints (defined in debug-debugger.js).
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002789 Handle<String> update_script_break_points_symbol =
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00002790 isolate_->factory()->LookupOneByteSymbol(
2791 STATIC_ASCII_VECTOR("UpdateScriptBreakPoints"));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002792 Handle<Object> update_script_break_points =
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00002793 Handle<Object>(debug->debug_context()->global_object()->
lrn@chromium.org303ada72010-10-27 09:33:13 +00002794 GetPropertyNoExceptionThrown(*update_script_break_points_symbol));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002795 if (!update_script_break_points->IsJSFunction()) {
2796 return;
2797 }
2798 ASSERT(update_script_break_points->IsJSFunction());
2799
2800 // Wrap the script object in a proper JS object before passing it
2801 // to JavaScript.
2802 Handle<JSValue> wrapper = GetScriptWrapper(script);
2803
2804 // Call UpdateScriptBreakPoints expect no exceptions.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002805 bool caught_exception;
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002806 Handle<Object> argv[] = { wrapper };
rossberg@chromium.org717967f2011-07-20 13:44:42 +00002807 Execution::TryCall(Handle<JSFunction>::cast(update_script_break_points),
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002808 Isolate::Current()->js_builtins_object(),
2809 ARRAY_SIZE(argv),
2810 argv,
2811 &caught_exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002812 if (caught_exception) {
2813 return;
2814 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002815 // Bail out based on state or if there is no listener for this event
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00002816 if (in_debugger && (after_compile_flags & SEND_WHEN_DEBUGGING) == 0) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002817 if (!Debugger::EventActive(v8::AfterCompile)) return;
2818
2819 // Create the compile state object.
2820 Handle<Object> event_data = MakeCompileEvent(script,
iposva@chromium.org245aa852009-02-10 00:49:54 +00002821 false,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002822 &caught_exception);
2823 // Bail out and don't call debugger if exception.
2824 if (caught_exception) {
2825 return;
2826 }
ager@chromium.org5ec48922009-05-05 07:25:34 +00002827 // Process debug event.
2828 ProcessDebugEvent(v8::AfterCompile,
2829 Handle<JSObject>::cast(event_data),
2830 true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002831}
2832
2833
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002834void Debugger::OnScriptCollected(int id) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002835 HandleScope scope(isolate_);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002836
2837 // No more to do if not debugging.
ulan@chromium.org56c14af2012-09-20 12:51:09 +00002838 if (isolate_->debug()->InDebugger()) return;
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002839 if (!IsDebuggerActive()) return;
2840 if (!Debugger::EventActive(v8::ScriptCollected)) return;
2841
2842 // Enter the debugger.
2843 EnterDebugger debugger;
2844 if (debugger.FailedToEnter()) return;
2845
2846 // Create the script collected state object.
2847 bool caught_exception = false;
2848 Handle<Object> event_data = MakeScriptCollectedEvent(id,
2849 &caught_exception);
2850 // Bail out and don't call debugger if exception.
2851 if (caught_exception) {
2852 return;
2853 }
2854
2855 // Process debug event.
2856 ProcessDebugEvent(v8::ScriptCollected,
2857 Handle<JSObject>::cast(event_data),
2858 true);
2859}
2860
2861
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002862void Debugger::ProcessDebugEvent(v8::DebugEvent event,
ager@chromium.org5ec48922009-05-05 07:25:34 +00002863 Handle<JSObject> event_data,
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002864 bool auto_continue) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002865 HandleScope scope(isolate_);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002866
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +00002867 // Clear any pending debug break if this is a real break.
2868 if (!auto_continue) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002869 isolate_->debug()->clear_interrupt_pending(DEBUGBREAK);
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +00002870 }
2871
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002872 // Create the execution state.
2873 bool caught_exception = false;
2874 Handle<Object> exec_state = MakeExecutionState(&caught_exception);
2875 if (caught_exception) {
2876 return;
2877 }
ager@chromium.org41826e72009-03-30 13:30:57 +00002878 // First notify the message handler if any.
2879 if (message_handler_ != NULL) {
ager@chromium.org5ec48922009-05-05 07:25:34 +00002880 NotifyMessageHandler(event,
2881 Handle<JSObject>::cast(exec_state),
2882 event_data,
2883 auto_continue);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002884 }
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00002885 // Notify registered debug event listener. This can be either a C or
2886 // a JavaScript function. Don't call event listener for v8::Break
2887 // here, if it's only a debug command -- they will be processed later.
2888 if ((event != v8::Break || !auto_continue) && !event_listener_.is_null()) {
2889 CallEventCallback(event, exec_state, event_data, NULL);
2890 }
2891 // Process pending debug commands.
2892 if (event == v8::Break) {
2893 while (!event_command_queue_.IsEmpty()) {
2894 CommandMessage command = event_command_queue_.Get();
2895 if (!event_listener_.is_null()) {
2896 CallEventCallback(v8::BreakForCommand,
2897 exec_state,
2898 event_data,
2899 command.client_data());
2900 }
2901 command.Dispose();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002902 }
2903 }
2904}
2905
2906
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00002907void Debugger::CallEventCallback(v8::DebugEvent event,
2908 Handle<Object> exec_state,
2909 Handle<Object> event_data,
2910 v8::Debug::ClientData* client_data) {
ager@chromium.orgea91cc52011-05-23 06:06:11 +00002911 if (event_listener_->IsForeign()) {
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00002912 CallCEventCallback(event, exec_state, event_data, client_data);
2913 } else {
2914 CallJSEventCallback(event, exec_state, event_data);
2915 }
2916}
2917
2918
2919void Debugger::CallCEventCallback(v8::DebugEvent event,
2920 Handle<Object> exec_state,
2921 Handle<Object> event_data,
2922 v8::Debug::ClientData* client_data) {
ager@chromium.orgea91cc52011-05-23 06:06:11 +00002923 Handle<Foreign> callback_obj(Handle<Foreign>::cast(event_listener_));
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00002924 v8::Debug::EventCallback2 callback =
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002925 FUNCTION_CAST<v8::Debug::EventCallback2>(
2926 callback_obj->foreign_address());
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00002927 EventDetailsImpl event_details(
2928 event,
2929 Handle<JSObject>::cast(exec_state),
2930 Handle<JSObject>::cast(event_data),
2931 event_listener_data_,
2932 client_data);
2933 callback(event_details);
2934}
2935
2936
2937void Debugger::CallJSEventCallback(v8::DebugEvent event,
2938 Handle<Object> exec_state,
2939 Handle<Object> event_data) {
2940 ASSERT(event_listener_->IsJSFunction());
2941 Handle<JSFunction> fun(Handle<JSFunction>::cast(event_listener_));
2942
2943 // Invoke the JavaScript debug event listener.
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002944 Handle<Object> argv[] = { Handle<Object>(Smi::FromInt(event)),
2945 exec_state,
2946 event_data,
2947 event_listener_data_ };
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002948 bool caught_exception;
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002949 Execution::TryCall(fun,
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00002950 isolate_->global_object(),
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002951 ARRAY_SIZE(argv),
2952 argv,
2953 &caught_exception);
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00002954 // Silently ignore exceptions from debug event listeners.
2955}
2956
2957
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00002958Handle<Context> Debugger::GetDebugContext() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002959 never_unload_debugger_ = true;
2960 EnterDebugger debugger;
2961 return isolate_->debug()->debug_context();
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00002962}
2963
2964
ager@chromium.org71daaf62009-04-01 07:22:49 +00002965void Debugger::UnloadDebugger() {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002966 Debug* debug = isolate_->debug();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002967
ager@chromium.org71daaf62009-04-01 07:22:49 +00002968 // Make sure that there are no breakpoints left.
lrn@chromium.org7516f052011-03-30 08:52:27 +00002969 debug->ClearAllBreakPoints();
ager@chromium.org71daaf62009-04-01 07:22:49 +00002970
2971 // Unload the debugger if feasible.
2972 if (!never_unload_debugger_) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002973 debug->Unload();
ager@chromium.org71daaf62009-04-01 07:22:49 +00002974 }
2975
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002976 // Clear the flag indicating that the debugger should be unloaded.
2977 debugger_unload_pending_ = false;
ager@chromium.org71daaf62009-04-01 07:22:49 +00002978}
2979
2980
ager@chromium.org41826e72009-03-30 13:30:57 +00002981void Debugger::NotifyMessageHandler(v8::DebugEvent event,
ager@chromium.org5ec48922009-05-05 07:25:34 +00002982 Handle<JSObject> exec_state,
2983 Handle<JSObject> event_data,
ager@chromium.org41826e72009-03-30 13:30:57 +00002984 bool auto_continue) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002985 HandleScope scope(isolate_);
ager@chromium.org41826e72009-03-30 13:30:57 +00002986
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002987 if (!isolate_->debug()->Load()) return;
ager@chromium.org41826e72009-03-30 13:30:57 +00002988
2989 // Process the individual events.
ager@chromium.org5ec48922009-05-05 07:25:34 +00002990 bool sendEventMessage = false;
ager@chromium.org41826e72009-03-30 13:30:57 +00002991 switch (event) {
2992 case v8::Break:
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00002993 case v8::BreakForCommand:
ager@chromium.org5ec48922009-05-05 07:25:34 +00002994 sendEventMessage = !auto_continue;
ager@chromium.org41826e72009-03-30 13:30:57 +00002995 break;
2996 case v8::Exception:
ager@chromium.org5ec48922009-05-05 07:25:34 +00002997 sendEventMessage = true;
ager@chromium.org41826e72009-03-30 13:30:57 +00002998 break;
2999 case v8::BeforeCompile:
3000 break;
3001 case v8::AfterCompile:
ager@chromium.org5ec48922009-05-05 07:25:34 +00003002 sendEventMessage = true;
ager@chromium.org41826e72009-03-30 13:30:57 +00003003 break;
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003004 case v8::ScriptCollected:
3005 sendEventMessage = true;
3006 break;
ager@chromium.org41826e72009-03-30 13:30:57 +00003007 case v8::NewFunction:
3008 break;
3009 default:
3010 UNREACHABLE();
3011 }
3012
ager@chromium.org5ec48922009-05-05 07:25:34 +00003013 // The debug command interrupt flag might have been set when the command was
3014 // added. It should be enough to clear the flag only once while we are in the
3015 // debugger.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003016 ASSERT(isolate_->debug()->InDebugger());
3017 isolate_->stack_guard()->Continue(DEBUGCOMMAND);
ager@chromium.org5ec48922009-05-05 07:25:34 +00003018
3019 // Notify the debugger that a debug event has occurred unless auto continue is
3020 // active in which case no event is send.
3021 if (sendEventMessage) {
3022 MessageImpl message = MessageImpl::NewEvent(
3023 event,
3024 auto_continue,
3025 Handle<JSObject>::cast(exec_state),
3026 Handle<JSObject>::cast(event_data));
3027 InvokeMessageHandler(message);
3028 }
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +00003029
3030 // If auto continue don't make the event cause a break, but process messages
3031 // in the queue if any. For script collected events don't even process
3032 // messages in the queue as the execution state might not be what is expected
3033 // by the client.
ager@chromium.org6ffc2172009-05-29 19:20:16 +00003034 if ((auto_continue && !HasCommands()) || event == v8::ScriptCollected) {
ager@chromium.org5ec48922009-05-05 07:25:34 +00003035 return;
3036 }
ager@chromium.org41826e72009-03-30 13:30:57 +00003037
ager@chromium.org41826e72009-03-30 13:30:57 +00003038 v8::TryCatch try_catch;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003039
3040 // DebugCommandProcessor goes here.
3041 v8::Local<v8::Object> cmd_processor;
3042 {
3043 v8::Local<v8::Object> api_exec_state =
3044 v8::Utils::ToLocal(Handle<JSObject>::cast(exec_state));
3045 v8::Local<v8::String> fun_name =
3046 v8::String::New("debugCommandProcessor");
3047 v8::Local<v8::Function> fun =
3048 v8::Function::Cast(*api_exec_state->Get(fun_name));
3049
3050 v8::Handle<v8::Boolean> running =
3051 auto_continue ? v8::True() : v8::False();
3052 static const int kArgc = 1;
3053 v8::Handle<Value> argv[kArgc] = { running };
3054 cmd_processor = v8::Object::Cast(*fun->Call(api_exec_state, kArgc, argv));
3055 if (try_catch.HasCaught()) {
3056 PrintLn(try_catch.Exception());
3057 return;
3058 }
ager@chromium.org41826e72009-03-30 13:30:57 +00003059 }
3060
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003061 bool running = auto_continue;
3062
ager@chromium.org41826e72009-03-30 13:30:57 +00003063 // Process requests from the debugger.
3064 while (true) {
3065 // Wait for new command in the queue.
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003066 if (Debugger::host_dispatch_handler_) {
3067 // In case there is a host dispatch - do periodic dispatches.
3068 if (!command_received_->Wait(host_dispatch_micros_)) {
3069 // Timout expired, do the dispatch.
3070 Debugger::host_dispatch_handler_();
3071 continue;
3072 }
3073 } else {
3074 // In case there is no host dispatch - just wait.
3075 command_received_->Wait();
3076 }
ager@chromium.org41826e72009-03-30 13:30:57 +00003077
ager@chromium.org41826e72009-03-30 13:30:57 +00003078 // Get the command from the queue.
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003079 CommandMessage command = command_queue_.Get();
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00003080 isolate_->logger()->DebugTag(
3081 "Got request from command queue, in interactive loop.");
ager@chromium.org71daaf62009-04-01 07:22:49 +00003082 if (!Debugger::IsDebuggerActive()) {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003083 // Delete command text and user data.
3084 command.Dispose();
ager@chromium.org41826e72009-03-30 13:30:57 +00003085 return;
3086 }
3087
ager@chromium.org41826e72009-03-30 13:30:57 +00003088 // Invoke JavaScript to process the debug request.
3089 v8::Local<v8::String> fun_name;
3090 v8::Local<v8::Function> fun;
3091 v8::Local<v8::Value> request;
3092 v8::TryCatch try_catch;
3093 fun_name = v8::String::New("processDebugRequest");
3094 fun = v8::Function::Cast(*cmd_processor->Get(fun_name));
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003095
3096 request = v8::String::New(command.text().start(),
3097 command.text().length());
ager@chromium.org41826e72009-03-30 13:30:57 +00003098 static const int kArgc = 1;
3099 v8::Handle<Value> argv[kArgc] = { request };
3100 v8::Local<v8::Value> response_val = fun->Call(cmd_processor, kArgc, argv);
3101
3102 // Get the response.
3103 v8::Local<v8::String> response;
ager@chromium.org41826e72009-03-30 13:30:57 +00003104 if (!try_catch.HasCaught()) {
3105 // Get response string.
3106 if (!response_val->IsUndefined()) {
3107 response = v8::String::Cast(*response_val);
3108 } else {
3109 response = v8::String::New("");
3110 }
3111
3112 // Log the JSON request/response.
3113 if (FLAG_trace_debug_json) {
3114 PrintLn(request);
3115 PrintLn(response);
3116 }
3117
3118 // Get the running state.
3119 fun_name = v8::String::New("isRunning");
3120 fun = v8::Function::Cast(*cmd_processor->Get(fun_name));
3121 static const int kArgc = 1;
3122 v8::Handle<Value> argv[kArgc] = { response };
3123 v8::Local<v8::Value> running_val = fun->Call(cmd_processor, kArgc, argv);
3124 if (!try_catch.HasCaught()) {
3125 running = running_val->ToBoolean()->Value();
3126 }
3127 } else {
3128 // In case of failure the result text is the exception text.
3129 response = try_catch.Exception()->ToString();
3130 }
3131
ager@chromium.org41826e72009-03-30 13:30:57 +00003132 // Return the result.
ager@chromium.org5ec48922009-05-05 07:25:34 +00003133 MessageImpl message = MessageImpl::NewResponse(
3134 event,
3135 running,
3136 Handle<JSObject>::cast(exec_state),
3137 Handle<JSObject>::cast(event_data),
3138 Handle<String>(Utils::OpenHandle(*response)),
3139 command.client_data());
3140 InvokeMessageHandler(message);
3141 command.Dispose();
ager@chromium.org41826e72009-03-30 13:30:57 +00003142
3143 // Return from debug event processing if either the VM is put into the
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00003144 // running state (through a continue command) or auto continue is active
ager@chromium.org41826e72009-03-30 13:30:57 +00003145 // and there are no more commands queued.
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003146 if (running && !HasCommands()) {
ager@chromium.org41826e72009-03-30 13:30:57 +00003147 return;
3148 }
3149 }
3150}
3151
3152
iposva@chromium.org245aa852009-02-10 00:49:54 +00003153void Debugger::SetEventListener(Handle<Object> callback,
3154 Handle<Object> data) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00003155 HandleScope scope(isolate_);
3156 GlobalHandles* global_handles = isolate_->global_handles();
iposva@chromium.org245aa852009-02-10 00:49:54 +00003157
3158 // Clear the global handles for the event listener and the event listener data
3159 // object.
3160 if (!event_listener_.is_null()) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00003161 global_handles->Destroy(
iposva@chromium.org245aa852009-02-10 00:49:54 +00003162 reinterpret_cast<Object**>(event_listener_.location()));
3163 event_listener_ = Handle<Object>();
3164 }
3165 if (!event_listener_data_.is_null()) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00003166 global_handles->Destroy(
iposva@chromium.org245aa852009-02-10 00:49:54 +00003167 reinterpret_cast<Object**>(event_listener_data_.location()));
3168 event_listener_data_ = Handle<Object>();
3169 }
3170
3171 // If there is a new debug event listener register it together with its data
3172 // object.
3173 if (!callback->IsUndefined() && !callback->IsNull()) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003174 event_listener_ = Handle<Object>::cast(
lrn@chromium.org7516f052011-03-30 08:52:27 +00003175 global_handles->Create(*callback));
iposva@chromium.org245aa852009-02-10 00:49:54 +00003176 if (data.is_null()) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00003177 data = isolate_->factory()->undefined_value();
iposva@chromium.org245aa852009-02-10 00:49:54 +00003178 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003179 event_listener_data_ = Handle<Object>::cast(
lrn@chromium.org7516f052011-03-30 08:52:27 +00003180 global_handles->Create(*data));
iposva@chromium.org245aa852009-02-10 00:49:54 +00003181 }
3182
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003183 ListenersChanged();
iposva@chromium.org245aa852009-02-10 00:49:54 +00003184}
3185
3186
ager@chromium.org5ec48922009-05-05 07:25:34 +00003187void Debugger::SetMessageHandler(v8::Debug::MessageHandler2 handler) {
ager@chromium.org71daaf62009-04-01 07:22:49 +00003188 ScopedLock with(debugger_access_);
3189
ager@chromium.org381abbb2009-02-25 13:23:22 +00003190 message_handler_ = handler;
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003191 ListenersChanged();
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003192 if (handler == NULL) {
ager@chromium.org71daaf62009-04-01 07:22:49 +00003193 // Send an empty command to the debugger if in a break to make JavaScript
3194 // run again if the debugger is closed.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003195 if (isolate_->debug()->InDebugger()) {
ager@chromium.org71daaf62009-04-01 07:22:49 +00003196 ProcessCommand(Vector<const uint16_t>::empty());
3197 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003198 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003199}
3200
3201
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003202void Debugger::ListenersChanged() {
3203 if (IsDebuggerActive()) {
3204 // Disable the compilation cache when the debugger is active.
lrn@chromium.org7516f052011-03-30 08:52:27 +00003205 isolate_->compilation_cache()->Disable();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003206 debugger_unload_pending_ = false;
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003207 } else {
lrn@chromium.org7516f052011-03-30 08:52:27 +00003208 isolate_->compilation_cache()->Enable();
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003209 // Unload the debugger if event listener and message handler cleared.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003210 // Schedule this for later, because we may be in non-V8 thread.
3211 debugger_unload_pending_ = true;
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003212 }
3213}
3214
3215
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003216void Debugger::SetHostDispatchHandler(v8::Debug::HostDispatchHandler handler,
3217 int period) {
ager@chromium.org381abbb2009-02-25 13:23:22 +00003218 host_dispatch_handler_ = handler;
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003219 host_dispatch_micros_ = period * 1000;
ager@chromium.org381abbb2009-02-25 13:23:22 +00003220}
3221
3222
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003223void Debugger::SetDebugMessageDispatchHandler(
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003224 v8::Debug::DebugMessageDispatchHandler handler, bool provide_locker) {
3225 ScopedLock with(dispatch_handler_access_);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003226 debug_message_dispatch_handler_ = handler;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003227
3228 if (provide_locker && message_dispatch_helper_thread_ == NULL) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003229 message_dispatch_helper_thread_ = new MessageDispatchHelperThread(isolate_);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003230 message_dispatch_helper_thread_->Start();
3231 }
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003232}
3233
3234
ager@chromium.org41826e72009-03-30 13:30:57 +00003235// Calls the registered debug message handler. This callback is part of the
ager@chromium.org5ec48922009-05-05 07:25:34 +00003236// public API.
3237void Debugger::InvokeMessageHandler(MessageImpl message) {
ager@chromium.org71daaf62009-04-01 07:22:49 +00003238 ScopedLock with(debugger_access_);
3239
ager@chromium.org381abbb2009-02-25 13:23:22 +00003240 if (message_handler_ != NULL) {
ager@chromium.org5ec48922009-05-05 07:25:34 +00003241 message_handler_(message);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003242 }
ager@chromium.org41826e72009-03-30 13:30:57 +00003243}
3244
3245
3246// Puts a command coming from the public API on the queue. Creates
3247// a copy of the command string managed by the debugger. Up to this
3248// point, the command data was managed by the API client. Called
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003249// by the API client thread.
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003250void Debugger::ProcessCommand(Vector<const uint16_t> command,
3251 v8::Debug::ClientData* client_data) {
3252 // Need to cast away const.
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003253 CommandMessage message = CommandMessage::New(
ager@chromium.org41826e72009-03-30 13:30:57 +00003254 Vector<uint16_t>(const_cast<uint16_t*>(command.start()),
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003255 command.length()),
3256 client_data);
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00003257 isolate_->logger()->DebugTag("Put command on command_queue.");
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003258 command_queue_.Put(message);
ager@chromium.org41826e72009-03-30 13:30:57 +00003259 command_received_->Signal();
sgjesse@chromium.org3afc1582009-04-16 22:31:44 +00003260
3261 // Set the debug command break flag to have the command processed.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003262 if (!isolate_->debug()->InDebugger()) {
3263 isolate_->stack_guard()->DebugCommand();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003264 }
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003265
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003266 MessageDispatchHelperThread* dispatch_thread;
3267 {
3268 ScopedLock with(dispatch_handler_access_);
3269 dispatch_thread = message_dispatch_helper_thread_;
3270 }
3271
3272 if (dispatch_thread == NULL) {
3273 CallMessageDispatchHandler();
3274 } else {
3275 dispatch_thread->Schedule();
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003276 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003277}
3278
3279
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003280bool Debugger::HasCommands() {
ager@chromium.org41826e72009-03-30 13:30:57 +00003281 return !command_queue_.IsEmpty();
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003282}
3283
3284
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00003285void Debugger::EnqueueDebugCommand(v8::Debug::ClientData* client_data) {
3286 CommandMessage message = CommandMessage::New(Vector<uint16_t>(), client_data);
3287 event_command_queue_.Put(message);
3288
3289 // Set the debug command break flag to have the command processed.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003290 if (!isolate_->debug()->InDebugger()) {
3291 isolate_->stack_guard()->DebugCommand();
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00003292 }
3293}
3294
3295
ager@chromium.org71daaf62009-04-01 07:22:49 +00003296bool Debugger::IsDebuggerActive() {
3297 ScopedLock with(debugger_access_);
3298
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00003299 return message_handler_ != NULL ||
3300 !event_listener_.is_null() ||
3301 force_debugger_active_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003302}
3303
3304
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003305Handle<Object> Debugger::Call(Handle<JSFunction> fun,
3306 Handle<Object> data,
3307 bool* pending_exception) {
ager@chromium.org71daaf62009-04-01 07:22:49 +00003308 // When calling functions in the debugger prevent it from beeing unloaded.
3309 Debugger::never_unload_debugger_ = true;
3310
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003311 // Enter the debugger.
3312 EnterDebugger debugger;
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +00003313 if (debugger.FailedToEnter()) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00003314 return isolate_->factory()->undefined_value();
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003315 }
3316
3317 // Create the execution state.
3318 bool caught_exception = false;
3319 Handle<Object> exec_state = MakeExecutionState(&caught_exception);
3320 if (caught_exception) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00003321 return isolate_->factory()->undefined_value();
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003322 }
3323
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00003324 Handle<Object> argv[] = { exec_state, data };
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +00003325 Handle<Object> result = Execution::Call(
3326 fun,
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003327 Handle<Object>(isolate_->debug()->debug_context_->global_proxy()),
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00003328 ARRAY_SIZE(argv),
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +00003329 argv,
3330 pending_exception);
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003331 return result;
3332}
3333
3334
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003335static void StubMessageHandler2(const v8::Debug::Message& message) {
3336 // Simply ignore message.
3337}
3338
3339
3340bool Debugger::StartAgent(const char* name, int port,
3341 bool wait_for_connection) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003342 ASSERT(Isolate::Current() == isolate_);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003343 if (wait_for_connection) {
3344 // Suspend V8 if it is already running or set V8 to suspend whenever
3345 // it starts.
3346 // Provide stub message handler; V8 auto-continues each suspend
3347 // when there is no message handler; we doesn't need it.
3348 // Once become suspended, V8 will stay so indefinitely long, until remote
3349 // debugger connects and issues "continue" command.
3350 Debugger::message_handler_ = StubMessageHandler2;
3351 v8::Debug::DebugBreak();
3352 }
3353
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00003354 if (Socket::SetUp()) {
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00003355 if (agent_ == NULL) {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00003356 agent_ = new DebuggerAgent(name, port);
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00003357 agent_->Start();
3358 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003359 return true;
3360 }
3361
3362 return false;
3363}
3364
3365
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003366void Debugger::StopAgent() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003367 ASSERT(Isolate::Current() == isolate_);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003368 if (agent_ != NULL) {
3369 agent_->Shutdown();
3370 agent_->Join();
3371 delete agent_;
3372 agent_ = NULL;
3373 }
3374}
3375
3376
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003377void Debugger::WaitForAgent() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003378 ASSERT(Isolate::Current() == isolate_);
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003379 if (agent_ != NULL)
3380 agent_->WaitUntilListening();
3381}
3382
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003383
3384void Debugger::CallMessageDispatchHandler() {
3385 v8::Debug::DebugMessageDispatchHandler handler;
3386 {
3387 ScopedLock with(dispatch_handler_access_);
3388 handler = Debugger::debug_message_dispatch_handler_;
3389 }
3390 if (handler != NULL) {
3391 handler();
3392 }
3393}
3394
3395
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003396EnterDebugger::EnterDebugger()
3397 : isolate_(Isolate::Current()),
3398 prev_(isolate_->debug()->debugger_entry()),
3399 it_(isolate_),
3400 has_js_frames_(!it_.done()),
3401 save_(isolate_) {
3402 Debug* debug = isolate_->debug();
3403 ASSERT(prev_ != NULL || !debug->is_interrupt_pending(PREEMPT));
3404 ASSERT(prev_ != NULL || !debug->is_interrupt_pending(DEBUGBREAK));
3405
3406 // Link recursive debugger entry.
3407 debug->set_debugger_entry(this);
3408
3409 // Store the previous break id and frame id.
3410 break_id_ = debug->break_id();
3411 break_frame_id_ = debug->break_frame_id();
3412
3413 // Create the new break info. If there is no JavaScript frames there is no
3414 // break frame id.
3415 if (has_js_frames_) {
3416 debug->NewBreak(it_.frame()->id());
3417 } else {
3418 debug->NewBreak(StackFrame::NO_ID);
3419 }
3420
3421 // Make sure that debugger is loaded and enter the debugger context.
3422 load_failed_ = !debug->Load();
3423 if (!load_failed_) {
3424 // NOTE the member variable save which saves the previous context before
3425 // this change.
3426 isolate_->set_context(*debug->debug_context());
3427 }
3428}
3429
3430
3431EnterDebugger::~EnterDebugger() {
3432 ASSERT(Isolate::Current() == isolate_);
3433 Debug* debug = isolate_->debug();
3434
3435 // Restore to the previous break state.
3436 debug->SetBreak(break_frame_id_, break_id_);
3437
3438 // Check for leaving the debugger.
fschneider@chromium.org7d10be52012-04-10 12:30:14 +00003439 if (!load_failed_ && prev_ == NULL) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003440 // Clear mirror cache when leaving the debugger. Skip this if there is a
3441 // pending exception as clearing the mirror cache calls back into
3442 // JavaScript. This can happen if the v8::Debug::Call is used in which
3443 // case the exception should end up in the calling code.
3444 if (!isolate_->has_pending_exception()) {
3445 // Try to avoid any pending debug break breaking in the clear mirror
3446 // cache JavaScript code.
3447 if (isolate_->stack_guard()->IsDebugBreak()) {
3448 debug->set_interrupts_pending(DEBUGBREAK);
3449 isolate_->stack_guard()->Continue(DEBUGBREAK);
3450 }
3451 debug->ClearMirrorCache();
3452 }
3453
3454 // Request preemption and debug break when leaving the last debugger entry
3455 // if any of these where recorded while debugging.
3456 if (debug->is_interrupt_pending(PREEMPT)) {
3457 // This re-scheduling of preemption is to avoid starvation in some
3458 // debugging scenarios.
3459 debug->clear_interrupt_pending(PREEMPT);
3460 isolate_->stack_guard()->Preempt();
3461 }
3462 if (debug->is_interrupt_pending(DEBUGBREAK)) {
3463 debug->clear_interrupt_pending(DEBUGBREAK);
3464 isolate_->stack_guard()->DebugBreak();
3465 }
3466
3467 // If there are commands in the queue when leaving the debugger request
3468 // that these commands are processed.
3469 if (isolate_->debugger()->HasCommands()) {
3470 isolate_->stack_guard()->DebugCommand();
3471 }
3472
3473 // If leaving the debugger with the debugger no longer active unload it.
3474 if (!isolate_->debugger()->IsDebuggerActive()) {
3475 isolate_->debugger()->UnloadDebugger();
3476 }
3477 }
3478
3479 // Leaving this debugger entry.
3480 debug->set_debugger_entry(prev_);
3481}
3482
3483
ager@chromium.org5ec48922009-05-05 07:25:34 +00003484MessageImpl MessageImpl::NewEvent(DebugEvent event,
3485 bool running,
3486 Handle<JSObject> exec_state,
3487 Handle<JSObject> event_data) {
3488 MessageImpl message(true, event, running,
3489 exec_state, event_data, Handle<String>(), NULL);
3490 return message;
3491}
3492
3493
3494MessageImpl MessageImpl::NewResponse(DebugEvent event,
3495 bool running,
3496 Handle<JSObject> exec_state,
3497 Handle<JSObject> event_data,
3498 Handle<String> response_json,
3499 v8::Debug::ClientData* client_data) {
3500 MessageImpl message(false, event, running,
3501 exec_state, event_data, response_json, client_data);
3502 return message;
3503}
3504
3505
3506MessageImpl::MessageImpl(bool is_event,
3507 DebugEvent event,
3508 bool running,
3509 Handle<JSObject> exec_state,
3510 Handle<JSObject> event_data,
3511 Handle<String> response_json,
3512 v8::Debug::ClientData* client_data)
3513 : is_event_(is_event),
3514 event_(event),
3515 running_(running),
3516 exec_state_(exec_state),
3517 event_data_(event_data),
3518 response_json_(response_json),
3519 client_data_(client_data) {}
3520
3521
3522bool MessageImpl::IsEvent() const {
3523 return is_event_;
3524}
3525
3526
3527bool MessageImpl::IsResponse() const {
3528 return !is_event_;
3529}
3530
3531
3532DebugEvent MessageImpl::GetEvent() const {
3533 return event_;
3534}
3535
3536
3537bool MessageImpl::WillStartRunning() const {
3538 return running_;
3539}
3540
3541
3542v8::Handle<v8::Object> MessageImpl::GetExecutionState() const {
3543 return v8::Utils::ToLocal(exec_state_);
3544}
3545
3546
3547v8::Handle<v8::Object> MessageImpl::GetEventData() const {
3548 return v8::Utils::ToLocal(event_data_);
3549}
3550
3551
3552v8::Handle<v8::String> MessageImpl::GetJSON() const {
3553 v8::HandleScope scope;
3554
3555 if (IsEvent()) {
3556 // Call toJSONProtocol on the debug event object.
3557 Handle<Object> fun = GetProperty(event_data_, "toJSONProtocol");
3558 if (!fun->IsJSFunction()) {
3559 return v8::Handle<v8::String>();
3560 }
3561 bool caught_exception;
3562 Handle<Object> json = Execution::TryCall(Handle<JSFunction>::cast(fun),
3563 event_data_,
3564 0, NULL, &caught_exception);
3565 if (caught_exception || !json->IsString()) {
3566 return v8::Handle<v8::String>();
3567 }
3568 return scope.Close(v8::Utils::ToLocal(Handle<String>::cast(json)));
3569 } else {
3570 return v8::Utils::ToLocal(response_json_);
3571 }
3572}
3573
3574
3575v8::Handle<v8::Context> MessageImpl::GetEventContext() const {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003576 Isolate* isolate = Isolate::Current();
3577 v8::Handle<v8::Context> context = GetDebugEventContext(isolate);
3578 // Isolate::context() may be NULL when "script collected" event occures.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003579 ASSERT(!context.IsEmpty() || event_ == v8::ScriptCollected);
rossberg@chromium.org717967f2011-07-20 13:44:42 +00003580 return context;
ager@chromium.org5ec48922009-05-05 07:25:34 +00003581}
3582
3583
3584v8::Debug::ClientData* MessageImpl::GetClientData() const {
3585 return client_data_;
3586}
3587
3588
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003589EventDetailsImpl::EventDetailsImpl(DebugEvent event,
3590 Handle<JSObject> exec_state,
3591 Handle<JSObject> event_data,
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00003592 Handle<Object> callback_data,
3593 v8::Debug::ClientData* client_data)
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003594 : event_(event),
3595 exec_state_(exec_state),
3596 event_data_(event_data),
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00003597 callback_data_(callback_data),
3598 client_data_(client_data) {}
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003599
3600
3601DebugEvent EventDetailsImpl::GetEvent() const {
3602 return event_;
3603}
3604
3605
3606v8::Handle<v8::Object> EventDetailsImpl::GetExecutionState() const {
3607 return v8::Utils::ToLocal(exec_state_);
3608}
3609
3610
3611v8::Handle<v8::Object> EventDetailsImpl::GetEventData() const {
3612 return v8::Utils::ToLocal(event_data_);
3613}
3614
3615
3616v8::Handle<v8::Context> EventDetailsImpl::GetEventContext() const {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003617 return GetDebugEventContext(Isolate::Current());
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003618}
3619
3620
3621v8::Handle<v8::Value> EventDetailsImpl::GetCallbackData() const {
3622 return v8::Utils::ToLocal(callback_data_);
3623}
3624
3625
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00003626v8::Debug::ClientData* EventDetailsImpl::GetClientData() const {
3627 return client_data_;
3628}
3629
3630
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003631CommandMessage::CommandMessage() : text_(Vector<uint16_t>::empty()),
3632 client_data_(NULL) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003633}
3634
3635
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003636CommandMessage::CommandMessage(const Vector<uint16_t>& text,
3637 v8::Debug::ClientData* data)
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003638 : text_(text),
3639 client_data_(data) {
3640}
3641
3642
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003643CommandMessage::~CommandMessage() {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003644}
3645
3646
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003647void CommandMessage::Dispose() {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003648 text_.Dispose();
3649 delete client_data_;
3650 client_data_ = NULL;
3651}
3652
3653
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003654CommandMessage CommandMessage::New(const Vector<uint16_t>& command,
3655 v8::Debug::ClientData* data) {
3656 return CommandMessage(command.Clone(), data);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003657}
3658
3659
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003660CommandMessageQueue::CommandMessageQueue(int size) : start_(0), end_(0),
3661 size_(size) {
3662 messages_ = NewArray<CommandMessage>(size);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003663}
3664
3665
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003666CommandMessageQueue::~CommandMessageQueue() {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003667 while (!IsEmpty()) {
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003668 CommandMessage m = Get();
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003669 m.Dispose();
3670 }
kasper.lund7276f142008-07-30 08:49:36 +00003671 DeleteArray(messages_);
3672}
3673
3674
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003675CommandMessage CommandMessageQueue::Get() {
kasper.lund7276f142008-07-30 08:49:36 +00003676 ASSERT(!IsEmpty());
3677 int result = start_;
3678 start_ = (start_ + 1) % size_;
3679 return messages_[result];
3680}
3681
3682
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003683void CommandMessageQueue::Put(const CommandMessage& message) {
kasper.lund7276f142008-07-30 08:49:36 +00003684 if ((end_ + 1) % size_ == start_) {
3685 Expand();
3686 }
3687 messages_[end_] = message;
3688 end_ = (end_ + 1) % size_;
3689}
3690
3691
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003692void CommandMessageQueue::Expand() {
3693 CommandMessageQueue new_queue(size_ * 2);
kasper.lund7276f142008-07-30 08:49:36 +00003694 while (!IsEmpty()) {
3695 new_queue.Put(Get());
3696 }
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003697 CommandMessage* array_to_free = messages_;
kasper.lund7276f142008-07-30 08:49:36 +00003698 *this = new_queue;
3699 new_queue.messages_ = array_to_free;
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003700 // Make the new_queue empty so that it doesn't call Dispose on any messages.
3701 new_queue.start_ = new_queue.end_;
kasper.lund7276f142008-07-30 08:49:36 +00003702 // Automatic destructor called on new_queue, freeing array_to_free.
3703}
3704
3705
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00003706LockingCommandMessageQueue::LockingCommandMessageQueue(Logger* logger, int size)
3707 : logger_(logger), queue_(size) {
kasper.lund7276f142008-07-30 08:49:36 +00003708 lock_ = OS::CreateMutex();
3709}
3710
3711
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003712LockingCommandMessageQueue::~LockingCommandMessageQueue() {
kasper.lund7276f142008-07-30 08:49:36 +00003713 delete lock_;
3714}
3715
3716
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003717bool LockingCommandMessageQueue::IsEmpty() const {
kasper.lund7276f142008-07-30 08:49:36 +00003718 ScopedLock sl(lock_);
3719 return queue_.IsEmpty();
3720}
3721
3722
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003723CommandMessage LockingCommandMessageQueue::Get() {
kasper.lund7276f142008-07-30 08:49:36 +00003724 ScopedLock sl(lock_);
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003725 CommandMessage result = queue_.Get();
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00003726 logger_->DebugEvent("Get", result.text());
kasper.lund7276f142008-07-30 08:49:36 +00003727 return result;
3728}
3729
3730
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003731void LockingCommandMessageQueue::Put(const CommandMessage& message) {
kasper.lund7276f142008-07-30 08:49:36 +00003732 ScopedLock sl(lock_);
3733 queue_.Put(message);
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00003734 logger_->DebugEvent("Put", message.text());
kasper.lund7276f142008-07-30 08:49:36 +00003735}
3736
3737
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003738void LockingCommandMessageQueue::Clear() {
kasper.lund7276f142008-07-30 08:49:36 +00003739 ScopedLock sl(lock_);
3740 queue_.Clear();
3741}
3742
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003743
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003744MessageDispatchHelperThread::MessageDispatchHelperThread(Isolate* isolate)
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00003745 : Thread("v8:MsgDispHelpr"),
lrn@chromium.org5d00b602011-01-05 09:51:43 +00003746 sem_(OS::CreateSemaphore(0)), mutex_(OS::CreateMutex()),
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003747 already_signalled_(false) {
3748}
3749
3750
3751MessageDispatchHelperThread::~MessageDispatchHelperThread() {
3752 delete mutex_;
3753 delete sem_;
3754}
3755
3756
3757void MessageDispatchHelperThread::Schedule() {
3758 {
3759 ScopedLock lock(mutex_);
3760 if (already_signalled_) {
3761 return;
3762 }
3763 already_signalled_ = true;
3764 }
3765 sem_->Signal();
3766}
3767
3768
3769void MessageDispatchHelperThread::Run() {
3770 while (true) {
3771 sem_->Wait();
3772 {
3773 ScopedLock lock(mutex_);
3774 already_signalled_ = false;
3775 }
3776 {
3777 Locker locker;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003778 Isolate::Current()->debugger()->CallMessageDispatchHandler();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003779 }
3780 }
3781}
3782
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003783#endif // ENABLE_DEBUGGER_SUPPORT
kasper.lund7276f142008-07-30 08:49:36 +00003784
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003785} } // namespace v8::internal