blob: 1fd3e4023d8a16ec8364d0d42ba5dd3a915f5e38 [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
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +0000392void BreakLocationIterator::PrepareStepIn(Isolate* isolate) {
393 HandleScope scope(isolate);
ager@chromium.org381abbb2009-02-25 13:23:22 +0000394
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)));
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000620 global_handles->MakeWeak(reinterpret_cast<Object**>(script_.location()),
621 this,
622 NULL,
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
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000666void ScriptCache::HandleWeakScript(v8::Isolate* isolate,
667 v8::Persistent<v8::Value> obj,
668 void* data) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000669 ScriptCache* script_cache = reinterpret_cast<ScriptCache*>(data);
670 // Find the location of the global handle.
671 Script** location =
672 reinterpret_cast<Script**>(Utils::OpenHandle(*obj).location());
673 ASSERT((*location)->IsScript());
674
675 // Remove the entry from the cache.
676 int id = Smi::cast((*location)->id())->value();
677 script_cache->Remove(reinterpret_cast<void*>(id), Hash(id));
678 script_cache->collected_scripts_.Add(id);
679
680 // Clear the weak handle.
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000681 obj.Dispose(isolate);
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000682 obj.Clear();
683}
684
685
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000686void Debug::SetUp(bool create_heap_objects) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000687 ThreadInit();
688 if (create_heap_objects) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000689 // Get code to handle debug break on return.
690 debug_break_return_ =
lrn@chromium.org7516f052011-03-30 08:52:27 +0000691 isolate_->builtins()->builtin(Builtins::kReturn_DebugBreak);
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000692 ASSERT(debug_break_return_->IsCode());
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000693 // Get code to handle debug break in debug break slots.
694 debug_break_slot_ =
lrn@chromium.org7516f052011-03-30 08:52:27 +0000695 isolate_->builtins()->builtin(Builtins::kSlot_DebugBreak);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000696 ASSERT(debug_break_slot_->IsCode());
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000697 }
698}
699
700
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000701void Debug::HandleWeakDebugInfo(v8::Isolate* isolate,
702 v8::Persistent<v8::Value> obj,
703 void* data) {
704 Debug* debug = reinterpret_cast<Isolate*>(isolate)->debug();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000705 DebugInfoListNode* node = reinterpret_cast<DebugInfoListNode*>(data);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000706 // We need to clear all breakpoints associated with the function to restore
707 // original code and avoid patching the code twice later because
708 // the function will live in the heap until next gc, and can be found by
jkummerow@chromium.org78502a92012-09-06 13:50:42 +0000709 // Debug::FindSharedFunctionInfoInScript.
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000710 BreakLocationIterator it(node->debug_info(), ALL_BREAK_LOCATIONS);
711 it.ClearAllDebugBreak();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000712 debug->RemoveDebugInfo(node->debug_info());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000713#ifdef DEBUG
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000714 node = debug->debug_info_list_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000715 while (node != NULL) {
716 ASSERT(node != reinterpret_cast<DebugInfoListNode*>(data));
717 node = node->next();
718 }
719#endif
720}
721
722
723DebugInfoListNode::DebugInfoListNode(DebugInfo* debug_info): next_(NULL) {
lrn@chromium.org7516f052011-03-30 08:52:27 +0000724 GlobalHandles* global_handles = Isolate::Current()->global_handles();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000725 // Globalize the request debug info object and make it weak.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000726 debug_info_ = Handle<DebugInfo>::cast(
lrn@chromium.org7516f052011-03-30 08:52:27 +0000727 (global_handles->Create(debug_info)));
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000728 global_handles->MakeWeak(reinterpret_cast<Object**>(debug_info_.location()),
729 this,
730 NULL,
731 Debug::HandleWeakDebugInfo);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000732}
733
734
735DebugInfoListNode::~DebugInfoListNode() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000736 Isolate::Current()->global_handles()->Destroy(
737 reinterpret_cast<Object**>(debug_info_.location()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000738}
739
740
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000741bool Debug::CompileDebuggerScript(int index) {
lrn@chromium.org7516f052011-03-30 08:52:27 +0000742 Isolate* isolate = Isolate::Current();
743 Factory* factory = isolate->factory();
744 HandleScope scope(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000745
kasper.lund44510672008-07-25 07:37:58 +0000746 // Bail out if the index is invalid.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000747 if (index == -1) {
748 return false;
749 }
kasper.lund44510672008-07-25 07:37:58 +0000750
751 // Find source and name for the requested script.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000752 Handle<String> source_code =
lrn@chromium.org7516f052011-03-30 08:52:27 +0000753 isolate->bootstrapper()->NativesSourceLookup(index);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000754 Vector<const char> name = Natives::GetScriptName(index);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000755 Handle<String> script_name = factory->NewStringFromAscii(name);
yangguo@chromium.org355cfd12012-08-29 15:32:24 +0000756 Handle<Context> context = isolate->native_context();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000757
758 // Compile the script.
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000759 Handle<SharedFunctionInfo> function_info;
760 function_info = Compiler::Compile(source_code,
761 script_name,
yangguo@chromium.org355cfd12012-08-29 15:32:24 +0000762 0, 0,
763 context,
764 NULL, NULL,
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000765 Handle<String>::null(),
766 NATIVES_CODE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000767
768 // Silently ignore stack overflows during compilation.
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000769 if (function_info.is_null()) {
lrn@chromium.org7516f052011-03-30 08:52:27 +0000770 ASSERT(isolate->has_pending_exception());
771 isolate->clear_pending_exception();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000772 return false;
773 }
774
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000775 // Execute the shared function in the debugger context.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000776 bool caught_exception;
kasper.lund44510672008-07-25 07:37:58 +0000777 Handle<JSFunction> function =
lrn@chromium.org7516f052011-03-30 08:52:27 +0000778 factory->NewFunctionFromSharedFunctionInfo(function_info, context);
rossberg@chromium.org717967f2011-07-20 13:44:42 +0000779
fschneider@chromium.org7d10be52012-04-10 12:30:14 +0000780 Handle<Object> exception =
ulan@chromium.org09d7ab52013-02-25 15:50:35 +0000781 Execution::TryCall(function,
782 Handle<Object>(context->global_object(), isolate),
783 0,
784 NULL,
785 &caught_exception);
kasper.lund44510672008-07-25 07:37:58 +0000786
787 // Check for caught exceptions.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000788 if (caught_exception) {
fschneider@chromium.org7d10be52012-04-10 12:30:14 +0000789 ASSERT(!isolate->has_pending_exception());
790 MessageLocation computed_location;
791 isolate->ComputeLocation(&computed_location);
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000792 Handle<Object> message = MessageHandler::MakeMessageObject(
fschneider@chromium.org7d10be52012-04-10 12:30:14 +0000793 "error_loading_debugger", &computed_location,
794 Vector<Handle<Object> >::empty(), Handle<String>(), Handle<JSArray>());
795 ASSERT(!isolate->has_pending_exception());
mmassi@chromium.org49a44672012-12-04 13:52:03 +0000796 if (!exception.is_null()) {
797 isolate->set_pending_exception(*exception);
798 MessageHandler::ReportMessage(Isolate::Current(), NULL, message);
799 isolate->clear_pending_exception();
800 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000801 return false;
802 }
803
kasper.lund44510672008-07-25 07:37:58 +0000804 // Mark this script as native and return successfully.
805 Handle<Script> script(Script::cast(function->shared()->script()));
ager@chromium.orge2902be2009-06-08 12:21:35 +0000806 script->set_type(Smi::FromInt(Script::TYPE_NATIVE));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000807 return true;
808}
809
810
811bool Debug::Load() {
812 // Return if debugger is already loaded.
kasper.lund212ac232008-07-16 07:07:30 +0000813 if (IsLoaded()) return true;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000814
lrn@chromium.org7516f052011-03-30 08:52:27 +0000815 Debugger* debugger = isolate_->debugger();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000816
kasper.lund44510672008-07-25 07:37:58 +0000817 // Bail out if we're already in the process of compiling the native
818 // JavaScript source code for the debugger.
lrn@chromium.org7516f052011-03-30 08:52:27 +0000819 if (debugger->compiling_natives() ||
820 debugger->is_loading_debugger())
mads.s.agercbaa0602008-08-14 13:41:48 +0000821 return false;
lrn@chromium.org7516f052011-03-30 08:52:27 +0000822 debugger->set_loading_debugger(true);
kasper.lund44510672008-07-25 07:37:58 +0000823
824 // Disable breakpoints and interrupts while compiling and running the
825 // debugger scripts including the context creation code.
826 DisableBreak disable(true);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000827 PostponeInterruptsScope postpone(isolate_);
kasper.lund44510672008-07-25 07:37:58 +0000828
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000829 // Create the debugger context.
lrn@chromium.org7516f052011-03-30 08:52:27 +0000830 HandleScope scope(isolate_);
kasper.lund44510672008-07-25 07:37:58 +0000831 Handle<Context> context =
lrn@chromium.org7516f052011-03-30 08:52:27 +0000832 isolate_->bootstrapper()->CreateEnvironment(
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000833 Handle<Object>::null(),
834 v8::Handle<ObjectTemplate>(),
835 NULL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000836
fschneider@chromium.org7d10be52012-04-10 12:30:14 +0000837 // Fail if no context could be created.
838 if (context.is_null()) return false;
839
kasper.lund44510672008-07-25 07:37:58 +0000840 // Use the debugger context.
lrn@chromium.org7516f052011-03-30 08:52:27 +0000841 SaveContext save(isolate_);
842 isolate_->set_context(*context);
kasper.lund44510672008-07-25 07:37:58 +0000843
844 // Expose the builtins object in the debugger context.
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000845 Handle<String> key = isolate_->factory()->InternalizeOneByteString(
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +0000846 STATIC_ASCII_VECTOR("builtins"));
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000847 Handle<GlobalObject> global = Handle<GlobalObject>(context->global_object());
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +0000848 RETURN_IF_EMPTY_HANDLE_VALUE(
lrn@chromium.org7516f052011-03-30 08:52:27 +0000849 isolate_,
ulan@chromium.org09d7ab52013-02-25 15:50:35 +0000850 JSReceiver::SetProperty(global,
851 key,
852 Handle<Object>(global->builtins(), isolate_),
853 NONE,
854 kNonStrictMode),
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +0000855 false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000856
857 // Compile the JavaScript for the debugger in the debugger context.
lrn@chromium.org7516f052011-03-30 08:52:27 +0000858 debugger->set_compiling_natives(true);
kasper.lund44510672008-07-25 07:37:58 +0000859 bool caught_exception =
860 !CompileDebuggerScript(Natives::GetIndex("mirror")) ||
861 !CompileDebuggerScript(Natives::GetIndex("debug"));
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000862
863 if (FLAG_enable_liveedit) {
864 caught_exception = caught_exception ||
865 !CompileDebuggerScript(Natives::GetIndex("liveedit"));
866 }
867
lrn@chromium.org7516f052011-03-30 08:52:27 +0000868 debugger->set_compiling_natives(false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000869
mads.s.agercbaa0602008-08-14 13:41:48 +0000870 // Make sure we mark the debugger as not loading before we might
871 // return.
lrn@chromium.org7516f052011-03-30 08:52:27 +0000872 debugger->set_loading_debugger(false);
mads.s.agercbaa0602008-08-14 13:41:48 +0000873
kasper.lund44510672008-07-25 07:37:58 +0000874 // Check for caught exceptions.
875 if (caught_exception) return false;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000876
877 // Debugger loaded.
whesse@chromium.org023421e2010-12-21 12:19:12 +0000878 debug_context_ = context;
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000879
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000880 return true;
881}
882
883
884void Debug::Unload() {
885 // Return debugger is not loaded.
886 if (!IsLoaded()) {
887 return;
888 }
889
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000890 // Clear the script cache.
891 DestroyScriptCache();
892
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000893 // Clear debugger context global handle.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000894 Isolate::Current()->global_handles()->Destroy(
895 reinterpret_cast<Object**>(debug_context_.location()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000896 debug_context_ = Handle<Context>();
897}
898
899
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000900// Set the flag indicating that preemption happened during debugging.
901void Debug::PreemptionWhileInDebugger() {
902 ASSERT(InDebugger());
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000903 Debug::set_interrupts_pending(PREEMPT);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000904}
905
906
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000907void Debug::Iterate(ObjectVisitor* v) {
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000908 v->VisitPointer(BitCast<Object**>(&(debug_break_return_)));
909 v->VisitPointer(BitCast<Object**>(&(debug_break_slot_)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000910}
911
912
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000913Object* Debug::Break(Arguments args) {
914 Heap* heap = isolate_->heap();
915 HandleScope scope(isolate_);
mads.s.ager31e71382008-08-13 09:32:07 +0000916 ASSERT(args.length() == 0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000917
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000918 thread_local_.frame_drop_mode_ = FRAMES_UNTOUCHED;
ager@chromium.org357bf652010-04-12 11:30:10 +0000919
kasper.lundbd3ec4e2008-07-09 11:06:54 +0000920 // Get the top-most JavaScript frame.
vegorov@chromium.org74f333b2011-04-06 11:17:46 +0000921 JavaScriptFrameIterator it(isolate_);
kasper.lundbd3ec4e2008-07-09 11:06:54 +0000922 JavaScriptFrame* frame = it.frame();
923
924 // Just continue if breaks are disabled or debugger cannot be loaded.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000925 if (disable_break() || !Load()) {
926 SetAfterBreakTarget(frame);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000927 return heap->undefined_value();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000928 }
929
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000930 // Enter the debugger.
931 EnterDebugger debugger;
932 if (debugger.FailedToEnter()) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000933 return heap->undefined_value();
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000934 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000935
kasper.lund44510672008-07-25 07:37:58 +0000936 // Postpone interrupt during breakpoint processing.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000937 PostponeInterruptsScope postpone(isolate_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000938
939 // Get the debug info (create it if it does not exist).
940 Handle<SharedFunctionInfo> shared =
941 Handle<SharedFunctionInfo>(JSFunction::cast(frame->function())->shared());
942 Handle<DebugInfo> debug_info = GetDebugInfo(shared);
943
944 // Find the break point where execution has stopped.
945 BreakLocationIterator break_location_iterator(debug_info,
946 ALL_BREAK_LOCATIONS);
947 break_location_iterator.FindBreakLocationFromAddress(frame->pc());
948
949 // Check whether step next reached a new statement.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000950 if (!StepNextContinue(&break_location_iterator, frame)) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000951 // Decrease steps left if performing multiple steps.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000952 if (thread_local_.step_count_ > 0) {
953 thread_local_.step_count_--;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000954 }
955 }
956
957 // If there is one or more real break points check whether any of these are
958 // triggered.
ulan@chromium.org09d7ab52013-02-25 15:50:35 +0000959 Handle<Object> break_points_hit(heap->undefined_value(), isolate_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000960 if (break_location_iterator.HasBreakPoint()) {
961 Handle<Object> break_point_objects =
ulan@chromium.org09d7ab52013-02-25 15:50:35 +0000962 Handle<Object>(break_location_iterator.BreakPointObjects(), isolate_);
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000963 break_points_hit = CheckBreakPoints(break_point_objects);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000964 }
965
ager@chromium.orga1645e22009-09-09 19:27:10 +0000966 // If step out is active skip everything until the frame where we need to step
967 // out to is reached, unless real breakpoint is hit.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000968 if (StepOutActive() && frame->fp() != step_out_fp() &&
ager@chromium.orga1645e22009-09-09 19:27:10 +0000969 break_points_hit->IsUndefined() ) {
970 // Step count should always be 0 for StepOut.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000971 ASSERT(thread_local_.step_count_ == 0);
ager@chromium.orga1645e22009-09-09 19:27:10 +0000972 } else if (!break_points_hit->IsUndefined() ||
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000973 (thread_local_.last_step_action_ != StepNone &&
974 thread_local_.step_count_ == 0)) {
ager@chromium.orga1645e22009-09-09 19:27:10 +0000975 // Notify debugger if a real break point is triggered or if performing
976 // single stepping with no more steps to perform. Otherwise do another step.
977
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000978 // Clear all current stepping setup.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000979 ClearStepping();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000980
lrn@chromium.org34e60782011-09-15 07:25:40 +0000981 if (thread_local_.queued_step_count_ > 0) {
982 // Perform queued steps
983 int step_count = thread_local_.queued_step_count_;
984
985 // Clear queue
986 thread_local_.queued_step_count_ = 0;
987
988 PrepareStep(StepNext, step_count);
989 } else {
990 // Notify the debug event listeners.
991 isolate_->debugger()->OnDebugBreak(break_points_hit, false);
992 }
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000993 } else if (thread_local_.last_step_action_ != StepNone) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000994 // Hold on to last step action as it is cleared by the call to
995 // ClearStepping.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000996 StepAction step_action = thread_local_.last_step_action_;
997 int step_count = thread_local_.step_count_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000998
lrn@chromium.org34e60782011-09-15 07:25:40 +0000999 // If StepNext goes deeper in code, StepOut until original frame
1000 // and keep step count queued up in the meantime.
1001 if (step_action == StepNext && frame->fp() < thread_local_.last_fp_) {
1002 // Count frames until target frame
1003 int count = 0;
1004 JavaScriptFrameIterator it(isolate_);
jkummerow@chromium.org212d9642012-05-11 15:02:09 +00001005 while (!it.done() && it.frame()->fp() < thread_local_.last_fp_) {
lrn@chromium.org34e60782011-09-15 07:25:40 +00001006 count++;
1007 it.Advance();
1008 }
1009
danno@chromium.org81cac2b2012-07-10 11:28:27 +00001010 // Check that we indeed found the frame we are looking for.
1011 CHECK(!it.done() && (it.frame()->fp() == thread_local_.last_fp_));
1012 if (step_count > 1) {
1013 // Save old count and action to continue stepping after StepOut.
1014 thread_local_.queued_step_count_ = step_count - 1;
jkummerow@chromium.org212d9642012-05-11 15:02:09 +00001015 }
1016
danno@chromium.org81cac2b2012-07-10 11:28:27 +00001017 // Set up for StepOut to reach target frame.
1018 step_action = StepOut;
1019 step_count = count;
lrn@chromium.org34e60782011-09-15 07:25:40 +00001020 }
1021
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001022 // Clear all current stepping setup.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001023 ClearStepping();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001024
1025 // Set up for the remaining steps.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001026 PrepareStep(step_action, step_count);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001027 }
1028
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001029 if (thread_local_.frame_drop_mode_ == FRAMES_UNTOUCHED) {
1030 SetAfterBreakTarget(frame);
1031 } else if (thread_local_.frame_drop_mode_ ==
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001032 FRAME_DROPPED_IN_IC_CALL) {
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001033 // We must have been calling IC stub. Do not go there anymore.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001034 Code* plain_return = isolate_->builtins()->builtin(
1035 Builtins::kPlainReturn_LiveEdit);
1036 thread_local_.after_break_target_ = plain_return->entry();
1037 } else if (thread_local_.frame_drop_mode_ ==
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001038 FRAME_DROPPED_IN_DEBUG_SLOT_CALL) {
1039 // Debug break slot stub does not return normally, instead it manually
1040 // cleans the stack and jumps. We should patch the jump address.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001041 Code* plain_return = isolate_->builtins()->builtin(
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00001042 Builtins::kFrameDropper_LiveEdit);
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001043 thread_local_.after_break_target_ = plain_return->entry();
1044 } else if (thread_local_.frame_drop_mode_ ==
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001045 FRAME_DROPPED_IN_DIRECT_CALL) {
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001046 // Nothing to do, after_break_target is not used here.
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00001047 } else if (thread_local_.frame_drop_mode_ ==
1048 FRAME_DROPPED_IN_RETURN_CALL) {
1049 Code* plain_return = isolate_->builtins()->builtin(
1050 Builtins::kFrameDropper_LiveEdit);
1051 thread_local_.after_break_target_ = plain_return->entry();
ager@chromium.org357bf652010-04-12 11:30:10 +00001052 } else {
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001053 UNREACHABLE();
ager@chromium.org357bf652010-04-12 11:30:10 +00001054 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001055
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001056 return heap->undefined_value();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001057}
1058
1059
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001060RUNTIME_FUNCTION(Object*, Debug_Break) {
1061 return isolate->debug()->Break(args);
1062}
1063
1064
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001065// Check the break point objects for whether one or more are actually
1066// triggered. This function returns a JSArray with the break point objects
1067// which is triggered.
1068Handle<Object> Debug::CheckBreakPoints(Handle<Object> break_point_objects) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00001069 Factory* factory = isolate_->factory();
1070
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001071 // Count the number of break points hit. If there are multiple break points
1072 // they are in a FixedArray.
1073 Handle<FixedArray> break_points_hit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001074 int break_points_hit_count = 0;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001075 ASSERT(!break_point_objects->IsUndefined());
1076 if (break_point_objects->IsFixedArray()) {
1077 Handle<FixedArray> array(FixedArray::cast(*break_point_objects));
lrn@chromium.org7516f052011-03-30 08:52:27 +00001078 break_points_hit = factory->NewFixedArray(array->length());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001079 for (int i = 0; i < array->length(); i++) {
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00001080 Handle<Object> o(array->get(i), isolate_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001081 if (CheckBreakPoint(o)) {
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001082 break_points_hit->set(break_points_hit_count++, *o);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001083 }
1084 }
1085 } else {
lrn@chromium.org7516f052011-03-30 08:52:27 +00001086 break_points_hit = factory->NewFixedArray(1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001087 if (CheckBreakPoint(break_point_objects)) {
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001088 break_points_hit->set(break_points_hit_count++, *break_point_objects);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001089 }
1090 }
1091
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001092 // Return undefined if no break points were triggered.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001093 if (break_points_hit_count == 0) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00001094 return factory->undefined_value();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001095 }
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001096 // Return break points hit as a JSArray.
lrn@chromium.org7516f052011-03-30 08:52:27 +00001097 Handle<JSArray> result = factory->NewJSArrayWithElements(break_points_hit);
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001098 result->set_length(Smi::FromInt(break_points_hit_count));
1099 return result;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001100}
1101
1102
1103// Check whether a single break point object is triggered.
1104bool Debug::CheckBreakPoint(Handle<Object> break_point_object) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00001105 Factory* factory = isolate_->factory();
1106 HandleScope scope(isolate_);
ager@chromium.org381abbb2009-02-25 13:23:22 +00001107
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001108 // Ignore check if break point object is not a JSObject.
1109 if (!break_point_object->IsJSObject()) return true;
1110
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001111 // Get the function IsBreakPointTriggered (defined in debug-debugger.js).
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001112 Handle<String> is_break_point_triggered_string =
1113 factory->InternalizeOneByteString(
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00001114 STATIC_ASCII_VECTOR("IsBreakPointTriggered"));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001115 Handle<JSFunction> check_break_point =
1116 Handle<JSFunction>(JSFunction::cast(
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00001117 debug_context()->global_object()->GetPropertyNoExceptionThrown(
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001118 *is_break_point_triggered_string)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001119
1120 // Get the break id as an object.
lrn@chromium.org7516f052011-03-30 08:52:27 +00001121 Handle<Object> break_id = factory->NewNumberFromInt(Debug::break_id());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001122
1123 // Call HandleBreakPointx.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001124 bool caught_exception;
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00001125 Handle<Object> argv[] = { break_id, break_point_object };
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001126 Handle<Object> result = Execution::TryCall(check_break_point,
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00001127 isolate_->js_builtins_object(),
1128 ARRAY_SIZE(argv),
1129 argv,
1130 &caught_exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001131
1132 // If exception or non boolean result handle as not triggered
1133 if (caught_exception || !result->IsBoolean()) {
1134 return false;
1135 }
1136
1137 // Return whether the break point is triggered.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001138 ASSERT(!result.is_null());
1139 return (*result)->IsTrue();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001140}
1141
1142
1143// Check whether the function has debug information.
1144bool Debug::HasDebugInfo(Handle<SharedFunctionInfo> shared) {
1145 return !shared->debug_info()->IsUndefined();
1146}
1147
1148
kasper.lundbd3ec4e2008-07-09 11:06:54 +00001149// Return the debug info for this function. EnsureDebugInfo must be called
1150// prior to ensure the debug info has been generated for shared.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001151Handle<DebugInfo> Debug::GetDebugInfo(Handle<SharedFunctionInfo> shared) {
kasper.lundbd3ec4e2008-07-09 11:06:54 +00001152 ASSERT(HasDebugInfo(shared));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001153 return Handle<DebugInfo>(DebugInfo::cast(shared->debug_info()));
1154}
1155
1156
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001157void Debug::SetBreakPoint(Handle<JSFunction> function,
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00001158 Handle<Object> break_point_object,
1159 int* source_position) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00001160 HandleScope scope(isolate_);
ager@chromium.org381abbb2009-02-25 13:23:22 +00001161
lrn@chromium.org34e60782011-09-15 07:25:40 +00001162 PrepareForBreakPoints();
1163
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001164 // Make sure the function is compiled and has set up the debug info.
1165 Handle<SharedFunctionInfo> shared(function->shared());
1166 if (!EnsureDebugInfo(shared, function)) {
kasper.lundbd3ec4e2008-07-09 11:06:54 +00001167 // Return if retrieving debug info failed.
1168 return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001169 }
1170
kasper.lundbd3ec4e2008-07-09 11:06:54 +00001171 Handle<DebugInfo> debug_info = GetDebugInfo(shared);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001172 // Source positions starts with zero.
danno@chromium.orgbf0c8202011-12-27 10:09:42 +00001173 ASSERT(*source_position >= 0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001174
1175 // Find the break point and change it.
1176 BreakLocationIterator it(debug_info, SOURCE_BREAK_LOCATIONS);
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00001177 it.FindBreakLocationFromPosition(*source_position);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001178 it.SetBreakPoint(break_point_object);
1179
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00001180 *source_position = it.position();
1181
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001182 // At least one active break point now.
1183 ASSERT(debug_info->GetBreakPointCount() > 0);
1184}
1185
1186
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001187bool Debug::SetBreakPointForScript(Handle<Script> script,
1188 Handle<Object> break_point_object,
1189 int* source_position) {
1190 HandleScope scope(isolate_);
1191
jkummerow@chromium.org78502a92012-09-06 13:50:42 +00001192 PrepareForBreakPoints();
1193
1194 // Obtain shared function info for the function.
1195 Object* result = FindSharedFunctionInfoInScript(script, *source_position);
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001196 if (result->IsUndefined()) return false;
1197
1198 // Make sure the function has set up the debug info.
1199 Handle<SharedFunctionInfo> shared(SharedFunctionInfo::cast(result));
1200 if (!EnsureDebugInfo(shared, Handle<JSFunction>::null())) {
1201 // Return if retrieving debug info failed.
1202 return false;
1203 }
1204
1205 // Find position within function. The script position might be before the
1206 // source position of the first function.
1207 int position;
1208 if (shared->start_position() > *source_position) {
1209 position = 0;
1210 } else {
1211 position = *source_position - shared->start_position();
1212 }
1213
1214 Handle<DebugInfo> debug_info = GetDebugInfo(shared);
1215 // Source positions starts with zero.
1216 ASSERT(position >= 0);
1217
1218 // Find the break point and change it.
1219 BreakLocationIterator it(debug_info, SOURCE_BREAK_LOCATIONS);
1220 it.FindBreakLocationFromPosition(position);
1221 it.SetBreakPoint(break_point_object);
1222
1223 *source_position = it.position() + shared->start_position();
1224
1225 // At least one active break point now.
1226 ASSERT(debug_info->GetBreakPointCount() > 0);
1227 return true;
1228}
1229
1230
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001231void Debug::ClearBreakPoint(Handle<Object> break_point_object) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00001232 HandleScope scope(isolate_);
ager@chromium.org381abbb2009-02-25 13:23:22 +00001233
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001234 DebugInfoListNode* node = debug_info_list_;
1235 while (node != NULL) {
1236 Object* result = DebugInfo::FindBreakPointInfo(node->debug_info(),
1237 break_point_object);
1238 if (!result->IsUndefined()) {
1239 // Get information in the break point.
1240 BreakPointInfo* break_point_info = BreakPointInfo::cast(result);
1241 Handle<DebugInfo> debug_info = node->debug_info();
1242 Handle<SharedFunctionInfo> shared(debug_info->shared());
1243 int source_position = break_point_info->statement_position()->value();
1244
1245 // Source positions starts with zero.
1246 ASSERT(source_position >= 0);
1247
1248 // Find the break point and clear it.
1249 BreakLocationIterator it(debug_info, SOURCE_BREAK_LOCATIONS);
1250 it.FindBreakLocationFromPosition(source_position);
1251 it.ClearBreakPoint(break_point_object);
1252
1253 // If there are no more break points left remove the debug info for this
1254 // function.
1255 if (debug_info->GetBreakPointCount() == 0) {
1256 RemoveDebugInfo(debug_info);
1257 }
1258
1259 return;
1260 }
1261 node = node->next();
1262 }
1263}
1264
1265
ager@chromium.org381abbb2009-02-25 13:23:22 +00001266void Debug::ClearAllBreakPoints() {
1267 DebugInfoListNode* node = debug_info_list_;
1268 while (node != NULL) {
1269 // Remove all debug break code.
1270 BreakLocationIterator it(node->debug_info(), ALL_BREAK_LOCATIONS);
1271 it.ClearAllDebugBreak();
1272 node = node->next();
1273 }
1274
1275 // Remove all debug info.
1276 while (debug_info_list_ != NULL) {
1277 RemoveDebugInfo(debug_info_list_->debug_info());
1278 }
1279}
1280
1281
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001282void Debug::FloodWithOneShot(Handle<JSFunction> function) {
lrn@chromium.org34e60782011-09-15 07:25:40 +00001283 PrepareForBreakPoints();
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001284
1285 // Make sure the function is compiled and has set up the debug info.
1286 Handle<SharedFunctionInfo> shared(function->shared());
1287 if (!EnsureDebugInfo(shared, function)) {
kasper.lundbd3ec4e2008-07-09 11:06:54 +00001288 // Return if we failed to retrieve the debug info.
1289 return;
1290 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001291
1292 // Flood the function with break points.
kasper.lundbd3ec4e2008-07-09 11:06:54 +00001293 BreakLocationIterator it(GetDebugInfo(shared), ALL_BREAK_LOCATIONS);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001294 while (!it.Done()) {
1295 it.SetOneShot();
1296 it.Next();
1297 }
1298}
1299
1300
rossberg@chromium.org2c067b12012-03-19 11:01:52 +00001301void Debug::FloodBoundFunctionWithOneShot(Handle<JSFunction> function) {
1302 Handle<FixedArray> new_bindings(function->function_bindings());
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00001303 Handle<Object> bindee(new_bindings->get(JSFunction::kBoundFunctionIndex),
1304 isolate_);
rossberg@chromium.org2c067b12012-03-19 11:01:52 +00001305
1306 if (!bindee.is_null() && bindee->IsJSFunction() &&
1307 !JSFunction::cast(*bindee)->IsBuiltin()) {
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001308 Handle<JSFunction> bindee_function(JSFunction::cast(*bindee));
1309 Debug::FloodWithOneShot(bindee_function);
rossberg@chromium.org2c067b12012-03-19 11:01:52 +00001310 }
1311}
1312
1313
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001314void Debug::FloodHandlerWithOneShot() {
ager@chromium.org8bb60582008-12-11 12:02:20 +00001315 // Iterate through the JavaScript stack looking for handlers.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001316 StackFrame::Id id = break_frame_id();
ager@chromium.org8bb60582008-12-11 12:02:20 +00001317 if (id == StackFrame::NO_ID) {
1318 // If there is no JavaScript stack don't do anything.
1319 return;
1320 }
vegorov@chromium.org74f333b2011-04-06 11:17:46 +00001321 for (JavaScriptFrameIterator it(isolate_, id); !it.done(); it.Advance()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001322 JavaScriptFrame* frame = it.frame();
1323 if (frame->HasHandler()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001324 // Flood the function with the catch block with break points
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001325 JSFunction* function = JSFunction::cast(frame->function());
1326 FloodWithOneShot(Handle<JSFunction>(function));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001327 return;
1328 }
1329 }
1330}
1331
1332
1333void Debug::ChangeBreakOnException(ExceptionBreakType type, bool enable) {
1334 if (type == BreakUncaughtException) {
1335 break_on_uncaught_exception_ = enable;
1336 } else {
1337 break_on_exception_ = enable;
1338 }
1339}
1340
1341
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00001342bool Debug::IsBreakOnException(ExceptionBreakType type) {
1343 if (type == BreakUncaughtException) {
1344 return break_on_uncaught_exception_;
1345 } else {
1346 return break_on_exception_;
1347 }
1348}
1349
1350
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001351void Debug::PrepareStep(StepAction step_action, int step_count) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00001352 HandleScope scope(isolate_);
lrn@chromium.org34e60782011-09-15 07:25:40 +00001353
1354 PrepareForBreakPoints();
1355
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001356 ASSERT(Debug::InDebugger());
1357
1358 // Remember this step action and count.
1359 thread_local_.last_step_action_ = step_action;
ager@chromium.orga1645e22009-09-09 19:27:10 +00001360 if (step_action == StepOut) {
1361 // For step out target frame will be found on the stack so there is no need
1362 // to set step counter for it. It's expected to always be 0 for StepOut.
1363 thread_local_.step_count_ = 0;
1364 } else {
1365 thread_local_.step_count_ = step_count;
1366 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001367
1368 // Get the frame where the execution has stopped and skip the debug frame if
1369 // any. The debug frame will only be present if execution was stopped due to
1370 // hitting a break point. In other situations (e.g. unhandled exception) the
1371 // debug frame is not present.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001372 StackFrame::Id id = break_frame_id();
ager@chromium.org8bb60582008-12-11 12:02:20 +00001373 if (id == StackFrame::NO_ID) {
1374 // If there is no JavaScript stack don't do anything.
1375 return;
1376 }
vegorov@chromium.org74f333b2011-04-06 11:17:46 +00001377 JavaScriptFrameIterator frames_it(isolate_, id);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001378 JavaScriptFrame* frame = frames_it.frame();
1379
1380 // First of all ensure there is one-shot break points in the top handler
1381 // if any.
1382 FloodHandlerWithOneShot();
1383
1384 // If the function on the top frame is unresolved perform step out. This will
1385 // be the case when calling unknown functions and having the debugger stopped
1386 // in an unhandled exception.
1387 if (!frame->function()->IsJSFunction()) {
1388 // Step out: Find the calling JavaScript frame and flood it with
1389 // breakpoints.
1390 frames_it.Advance();
1391 // Fill the function to return to with one-shot break points.
1392 JSFunction* function = JSFunction::cast(frames_it.frame()->function());
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001393 FloodWithOneShot(Handle<JSFunction>(function));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001394 return;
1395 }
1396
1397 // Get the debug info (create it if it does not exist).
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001398 Handle<JSFunction> function(JSFunction::cast(frame->function()));
1399 Handle<SharedFunctionInfo> shared(function->shared());
1400 if (!EnsureDebugInfo(shared, function)) {
kasper.lundbd3ec4e2008-07-09 11:06:54 +00001401 // Return if ensuring debug info failed.
1402 return;
1403 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001404 Handle<DebugInfo> debug_info = GetDebugInfo(shared);
1405
1406 // Find the break location where execution has stopped.
1407 BreakLocationIterator it(debug_info, ALL_BREAK_LOCATIONS);
1408 it.FindBreakLocationFromAddress(frame->pc());
1409
1410 // Compute whether or not the target is a call target.
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001411 bool is_load_or_store = false;
1412 bool is_inline_cache_stub = false;
whesse@chromium.orge90029b2010-08-02 11:52:17 +00001413 bool is_at_restarted_function = false;
ager@chromium.orga1645e22009-09-09 19:27:10 +00001414 Handle<Code> call_function_stub;
ager@chromium.orga1645e22009-09-09 19:27:10 +00001415
whesse@chromium.orge90029b2010-08-02 11:52:17 +00001416 if (thread_local_.restarter_frame_function_pointer_ == NULL) {
1417 if (RelocInfo::IsCodeTarget(it.rinfo()->rmode())) {
1418 bool is_call_target = false;
1419 Address target = it.rinfo()->target_address();
1420 Code* code = Code::GetCodeFromTargetAddress(target);
1421 if (code->is_call_stub() || code->is_keyed_call_stub()) {
1422 is_call_target = true;
1423 }
1424 if (code->is_inline_cache_stub()) {
1425 is_inline_cache_stub = true;
1426 is_load_or_store = !is_call_target;
1427 }
1428
1429 // Check if target code is CallFunction stub.
1430 Code* maybe_call_function_stub = code;
1431 // If there is a breakpoint at this line look at the original code to
1432 // check if it is a CallFunction stub.
1433 if (it.IsDebugBreak()) {
1434 Address original_target = it.original_rinfo()->target_address();
1435 maybe_call_function_stub =
1436 Code::GetCodeFromTargetAddress(original_target);
1437 }
1438 if (maybe_call_function_stub->kind() == Code::STUB &&
1439 maybe_call_function_stub->major_key() == CodeStub::CallFunction) {
1440 // Save reference to the code as we may need it to find out arguments
1441 // count for 'step in' later.
1442 call_function_stub = Handle<Code>(maybe_call_function_stub);
1443 }
ager@chromium.orga1645e22009-09-09 19:27:10 +00001444 }
whesse@chromium.orge90029b2010-08-02 11:52:17 +00001445 } else {
1446 is_at_restarted_function = true;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001447 }
1448
v8.team.kasperl727e9952008-09-02 14:56:44 +00001449 // If this is the last break code target step out is the only possibility.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001450 if (it.IsExit() || step_action == StepOut) {
ager@chromium.orga1645e22009-09-09 19:27:10 +00001451 if (step_action == StepOut) {
1452 // Skip step_count frames starting with the current one.
1453 while (step_count-- > 0 && !frames_it.done()) {
1454 frames_it.Advance();
1455 }
1456 } else {
1457 ASSERT(it.IsExit());
1458 frames_it.Advance();
1459 }
1460 // Skip builtin functions on the stack.
1461 while (!frames_it.done() &&
1462 JSFunction::cast(frames_it.frame()->function())->IsBuiltin()) {
1463 frames_it.Advance();
1464 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001465 // Step out: If there is a JavaScript caller frame, we need to
1466 // flood it with breakpoints.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001467 if (!frames_it.done()) {
1468 // Fill the function to return to with one-shot break points.
1469 JSFunction* function = JSFunction::cast(frames_it.frame()->function());
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001470 FloodWithOneShot(Handle<JSFunction>(function));
ager@chromium.orga1645e22009-09-09 19:27:10 +00001471 // Set target frame pointer.
1472 ActivateStepOut(frames_it.frame());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001473 }
ager@chromium.orga1645e22009-09-09 19:27:10 +00001474 } else if (!(is_inline_cache_stub || RelocInfo::IsConstructCall(it.rmode()) ||
whesse@chromium.orge90029b2010-08-02 11:52:17 +00001475 !call_function_stub.is_null() || is_at_restarted_function)
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001476 || step_action == StepNext || step_action == StepMin) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001477 // Step next or step min.
1478
1479 // Fill the current function with one-shot break points.
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001480 FloodWithOneShot(function);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001481
1482 // Remember source position and frame to handle step next.
1483 thread_local_.last_statement_position_ =
1484 debug_info->code()->SourceStatementPosition(frame->pc());
mmassi@chromium.org7028c052012-06-13 11:51:58 +00001485 thread_local_.last_fp_ = frame->UnpaddedFP();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001486 } else {
whesse@chromium.orge90029b2010-08-02 11:52:17 +00001487 // If there's restarter frame on top of the stack, just get the pointer
1488 // to function which is going to be restarted.
1489 if (is_at_restarted_function) {
1490 Handle<JSFunction> restarted_function(
1491 JSFunction::cast(*thread_local_.restarter_frame_function_pointer_));
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001492 FloodWithOneShot(restarted_function);
whesse@chromium.orge90029b2010-08-02 11:52:17 +00001493 } else if (!call_function_stub.is_null()) {
1494 // If it's CallFunction stub ensure target function is compiled and flood
1495 // it with one shot breakpoints.
1496
ager@chromium.orga1645e22009-09-09 19:27:10 +00001497 // Find out number of arguments from the stub minor key.
1498 // Reverse lookup required as the minor key cannot be retrieved
1499 // from the code object.
1500 Handle<Object> obj(
lrn@chromium.org7516f052011-03-30 08:52:27 +00001501 isolate_->heap()->code_stubs()->SlowReverseLookup(
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00001502 *call_function_stub),
1503 isolate_);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001504 ASSERT(!obj.is_null());
1505 ASSERT(!(*obj)->IsUndefined());
ager@chromium.orga1645e22009-09-09 19:27:10 +00001506 ASSERT(obj->IsSmi());
1507 // Get the STUB key and extract major and minor key.
1508 uint32_t key = Smi::cast(*obj)->value();
1509 // Argc in the stub is the number of arguments passed - not the
1510 // expected arguments of the called function.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001511 int call_function_arg_count =
1512 CallFunctionStub::ExtractArgcFromMinorKey(
1513 CodeStub::MinorKeyFromKey(key));
ager@chromium.orga1645e22009-09-09 19:27:10 +00001514 ASSERT(call_function_stub->major_key() ==
1515 CodeStub::MajorKeyFromKey(key));
1516
1517 // Find target function on the expression stack.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001518 // Expression stack looks like this (top to bottom):
ager@chromium.orga1645e22009-09-09 19:27:10 +00001519 // argN
1520 // ...
1521 // arg0
1522 // Receiver
1523 // Function to call
1524 int expressions_count = frame->ComputeExpressionsCount();
1525 ASSERT(expressions_count - 2 - call_function_arg_count >= 0);
1526 Object* fun = frame->GetExpression(
1527 expressions_count - 2 - call_function_arg_count);
1528 if (fun->IsJSFunction()) {
1529 Handle<JSFunction> js_function(JSFunction::cast(fun));
rossberg@chromium.org2c067b12012-03-19 11:01:52 +00001530 if (js_function->shared()->bound()) {
1531 Debug::FloodBoundFunctionWithOneShot(js_function);
1532 } else if (!js_function->IsBuiltin()) {
1533 // Don't step into builtins.
ager@chromium.orga1645e22009-09-09 19:27:10 +00001534 // It will also compile target function if it's not compiled yet.
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001535 FloodWithOneShot(js_function);
ager@chromium.orga1645e22009-09-09 19:27:10 +00001536 }
1537 }
1538 }
1539
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001540 // Fill the current function with one-shot break points even for step in on
1541 // a call target as the function called might be a native function for
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001542 // which step in will not stop. It also prepares for stepping in
1543 // getters/setters.
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001544 FloodWithOneShot(function);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001545
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001546 if (is_load_or_store) {
1547 // Remember source position and frame to handle step in getter/setter. If
1548 // there is a custom getter/setter it will be handled in
1549 // Object::Get/SetPropertyWithCallback, otherwise the step action will be
1550 // propagated on the next Debug::Break.
1551 thread_local_.last_statement_position_ =
1552 debug_info->code()->SourceStatementPosition(frame->pc());
mmassi@chromium.org7028c052012-06-13 11:51:58 +00001553 thread_local_.last_fp_ = frame->UnpaddedFP();
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001554 }
1555
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001556 // Step in or Step in min
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00001557 it.PrepareStepIn(isolate_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001558 ActivateStepIn(frame);
1559 }
1560}
1561
1562
1563// Check whether the current debug break should be reported to the debugger. It
1564// is used to have step next and step in only report break back to the debugger
1565// if on a different frame or in a different statement. In some situations
1566// there will be several break points in the same statement when the code is
v8.team.kasperl727e9952008-09-02 14:56:44 +00001567// flooded with one-shot break points. This function helps to perform several
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001568// steps before reporting break back to the debugger.
1569bool Debug::StepNextContinue(BreakLocationIterator* break_location_iterator,
1570 JavaScriptFrame* frame) {
lrn@chromium.org34e60782011-09-15 07:25:40 +00001571 // StepNext and StepOut shouldn't bring us deeper in code, so last frame
1572 // shouldn't be a parent of current frame.
1573 if (thread_local_.last_step_action_ == StepNext ||
1574 thread_local_.last_step_action_ == StepOut) {
1575 if (frame->fp() < thread_local_.last_fp_) return true;
1576 }
1577
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001578 // If the step last action was step next or step in make sure that a new
1579 // statement is hit.
1580 if (thread_local_.last_step_action_ == StepNext ||
1581 thread_local_.last_step_action_ == StepIn) {
1582 // Never continue if returning from function.
1583 if (break_location_iterator->IsExit()) return false;
1584
1585 // Continue if we are still on the same frame and in the same statement.
1586 int current_statement_position =
1587 break_location_iterator->code()->SourceStatementPosition(frame->pc());
mmassi@chromium.org7028c052012-06-13 11:51:58 +00001588 return thread_local_.last_fp_ == frame->UnpaddedFP() &&
ager@chromium.org236ad962008-09-25 09:45:57 +00001589 thread_local_.last_statement_position_ == current_statement_position;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001590 }
1591
1592 // No step next action - don't continue.
1593 return false;
1594}
1595
1596
1597// Check whether the code object at the specified address is a debug break code
1598// object.
1599bool Debug::IsDebugBreak(Address addr) {
ager@chromium.org8bb60582008-12-11 12:02:20 +00001600 Code* code = Code::GetCodeFromTargetAddress(addr);
yangguo@chromium.org9768bf12013-01-11 14:51:07 +00001601 return code->is_debug_break();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001602}
1603
1604
1605// Check whether a code stub with the specified major key is a possible break
1606// point location when looking for source break locations.
1607bool Debug::IsSourceBreakStub(Code* code) {
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001608 CodeStub::Major major_key = CodeStub::GetMajorKey(code);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001609 return major_key == CodeStub::CallFunction;
1610}
1611
1612
1613// Check whether a code stub with the specified major key is a possible break
1614// location.
1615bool Debug::IsBreakStub(Code* code) {
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001616 CodeStub::Major major_key = CodeStub::GetMajorKey(code);
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00001617 return major_key == CodeStub::CallFunction;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001618}
1619
1620
1621// Find the builtin to use for invoking the debug break
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001622Handle<Code> Debug::FindDebugBreak(Handle<Code> code, RelocInfo::Mode mode) {
danno@chromium.orgfa458e42012-02-01 10:48:36 +00001623 Isolate* isolate = Isolate::Current();
1624
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001625 // Find the builtin debug break function matching the calling convention
1626 // used by the call site.
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001627 if (code->is_inline_cache_stub()) {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001628 switch (code->kind()) {
1629 case Code::CALL_IC:
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001630 case Code::KEYED_CALL_IC:
danno@chromium.orgfa458e42012-02-01 10:48:36 +00001631 return isolate->stub_cache()->ComputeCallDebugBreak(
1632 code->arguments_count(), code->kind());
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001633
1634 case Code::LOAD_IC:
danno@chromium.orgfa458e42012-02-01 10:48:36 +00001635 return isolate->builtins()->LoadIC_DebugBreak();
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001636
1637 case Code::STORE_IC:
danno@chromium.orgfa458e42012-02-01 10:48:36 +00001638 return isolate->builtins()->StoreIC_DebugBreak();
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001639
1640 case Code::KEYED_LOAD_IC:
danno@chromium.orgfa458e42012-02-01 10:48:36 +00001641 return isolate->builtins()->KeyedLoadIC_DebugBreak();
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001642
1643 case Code::KEYED_STORE_IC:
danno@chromium.orgfa458e42012-02-01 10:48:36 +00001644 return isolate->builtins()->KeyedStoreIC_DebugBreak();
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001645
1646 default:
1647 UNREACHABLE();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001648 }
1649 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001650 if (RelocInfo::IsConstructCall(mode)) {
danno@chromium.orgfa458e42012-02-01 10:48:36 +00001651 if (code->has_function_cache()) {
1652 return isolate->builtins()->CallConstructStub_Recording_DebugBreak();
1653 } else {
1654 return isolate->builtins()->CallConstructStub_DebugBreak();
1655 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001656 }
1657 if (code->kind() == Code::STUB) {
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00001658 ASSERT(code->major_key() == CodeStub::CallFunction);
danno@chromium.orgfa458e42012-02-01 10:48:36 +00001659 if (code->has_function_cache()) {
1660 return isolate->builtins()->CallFunctionStub_Recording_DebugBreak();
1661 } else {
1662 return isolate->builtins()->CallFunctionStub_DebugBreak();
1663 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001664 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001665
1666 UNREACHABLE();
1667 return Handle<Code>::null();
1668}
1669
1670
1671// Simple function for returning the source positions for active break points.
1672Handle<Object> Debug::GetSourceBreakLocations(
1673 Handle<SharedFunctionInfo> shared) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00001674 Isolate* isolate = Isolate::Current();
1675 Heap* heap = isolate->heap();
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00001676 if (!HasDebugInfo(shared)) {
1677 return Handle<Object>(heap->undefined_value(), isolate);
1678 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001679 Handle<DebugInfo> debug_info = GetDebugInfo(shared);
1680 if (debug_info->GetBreakPointCount() == 0) {
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00001681 return Handle<Object>(heap->undefined_value(), isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001682 }
1683 Handle<FixedArray> locations =
lrn@chromium.org7516f052011-03-30 08:52:27 +00001684 isolate->factory()->NewFixedArray(debug_info->GetBreakPointCount());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001685 int count = 0;
1686 for (int i = 0; i < debug_info->break_points()->length(); i++) {
1687 if (!debug_info->break_points()->get(i)->IsUndefined()) {
1688 BreakPointInfo* break_point_info =
1689 BreakPointInfo::cast(debug_info->break_points()->get(i));
1690 if (break_point_info->GetBreakPointCount() > 0) {
1691 locations->set(count++, break_point_info->statement_position());
1692 }
1693 }
1694 }
1695 return locations;
1696}
1697
1698
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001699void Debug::NewBreak(StackFrame::Id break_frame_id) {
1700 thread_local_.break_frame_id_ = break_frame_id;
1701 thread_local_.break_id_ = ++thread_local_.break_count_;
1702}
1703
1704
1705void Debug::SetBreak(StackFrame::Id break_frame_id, int break_id) {
1706 thread_local_.break_frame_id_ = break_frame_id;
1707 thread_local_.break_id_ = break_id;
1708}
1709
1710
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001711// Handle stepping into a function.
1712void Debug::HandleStepIn(Handle<JSFunction> function,
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001713 Handle<Object> holder,
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001714 Address fp,
1715 bool is_constructor) {
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00001716 Isolate* isolate = function->GetIsolate();
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001717 // If the frame pointer is not supplied by the caller find it.
1718 if (fp == 0) {
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00001719 StackFrameIterator it(isolate);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001720 it.Advance();
1721 // For constructor functions skip another frame.
1722 if (is_constructor) {
1723 ASSERT(it.frame()->is_construct());
1724 it.Advance();
1725 }
1726 fp = it.frame()->fp();
1727 }
1728
1729 // Flood the function with one-shot break points if it is called from where
1730 // step into was requested.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001731 if (fp == step_in_fp()) {
rossberg@chromium.org2c067b12012-03-19 11:01:52 +00001732 if (function->shared()->bound()) {
1733 // Handle Function.prototype.bind
1734 Debug::FloodBoundFunctionWithOneShot(function);
1735 } else if (!function->IsBuiltin()) {
1736 // Don't allow step into functions in the native context.
kasperl@chromium.orgacae3782009-04-11 09:17:08 +00001737 if (function->shared()->code() ==
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00001738 isolate->builtins()->builtin(Builtins::kFunctionApply) ||
kasperl@chromium.orgacae3782009-04-11 09:17:08 +00001739 function->shared()->code() ==
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00001740 isolate->builtins()->builtin(Builtins::kFunctionCall)) {
kasperl@chromium.orgacae3782009-04-11 09:17:08 +00001741 // Handle function.apply and function.call separately to flood the
1742 // function to be called and not the code for Builtins::FunctionApply or
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001743 // Builtins::FunctionCall. The receiver of call/apply is the target
1744 // function.
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001745 if (!holder.is_null() && holder->IsJSFunction() &&
1746 !JSFunction::cast(*holder)->IsBuiltin()) {
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001747 Handle<JSFunction> js_function = Handle<JSFunction>::cast(holder);
1748 Debug::FloodWithOneShot(js_function);
kasperl@chromium.orgacae3782009-04-11 09:17:08 +00001749 }
1750 } else {
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001751 Debug::FloodWithOneShot(function);
kasperl@chromium.orgacae3782009-04-11 09:17:08 +00001752 }
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001753 }
1754 }
1755}
1756
1757
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001758void Debug::ClearStepping() {
1759 // Clear the various stepping setup.
1760 ClearOneShot();
1761 ClearStepIn();
ager@chromium.orga1645e22009-09-09 19:27:10 +00001762 ClearStepOut();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001763 ClearStepNext();
1764
1765 // Clear multiple step counter.
1766 thread_local_.step_count_ = 0;
1767}
1768
1769// Clears all the one-shot break points that are currently set. Normally this
1770// function is called each time a break point is hit as one shot break points
1771// are used to support stepping.
1772void Debug::ClearOneShot() {
1773 // The current implementation just runs through all the breakpoints. When the
v8.team.kasperl727e9952008-09-02 14:56:44 +00001774 // last break point for a function is removed that function is automatically
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001775 // removed from the list.
1776
1777 DebugInfoListNode* node = debug_info_list_;
1778 while (node != NULL) {
1779 BreakLocationIterator it(node->debug_info(), ALL_BREAK_LOCATIONS);
1780 while (!it.Done()) {
1781 it.ClearOneShot();
1782 it.Next();
1783 }
1784 node = node->next();
1785 }
1786}
1787
1788
1789void Debug::ActivateStepIn(StackFrame* frame) {
ager@chromium.orga1645e22009-09-09 19:27:10 +00001790 ASSERT(!StepOutActive());
mmassi@chromium.org7028c052012-06-13 11:51:58 +00001791 thread_local_.step_into_fp_ = frame->UnpaddedFP();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001792}
1793
1794
1795void Debug::ClearStepIn() {
1796 thread_local_.step_into_fp_ = 0;
1797}
1798
1799
ager@chromium.orga1645e22009-09-09 19:27:10 +00001800void Debug::ActivateStepOut(StackFrame* frame) {
1801 ASSERT(!StepInActive());
mmassi@chromium.org7028c052012-06-13 11:51:58 +00001802 thread_local_.step_out_fp_ = frame->UnpaddedFP();
ager@chromium.orga1645e22009-09-09 19:27:10 +00001803}
1804
1805
1806void Debug::ClearStepOut() {
1807 thread_local_.step_out_fp_ = 0;
1808}
1809
1810
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001811void Debug::ClearStepNext() {
1812 thread_local_.last_step_action_ = StepNone;
ager@chromium.org236ad962008-09-25 09:45:57 +00001813 thread_local_.last_statement_position_ = RelocInfo::kNoPosition;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001814 thread_local_.last_fp_ = 0;
1815}
1816
1817
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001818// Helper function to compile full code for debugging. This code will
mmassi@chromium.org7028c052012-06-13 11:51:58 +00001819// have debug break slots and deoptimization information. Deoptimization
1820// information is required in case that an optimized version of this
1821// function is still activated on the stack. It will also make sure that
1822// the full code is compiled with the same flags as the previous version,
1823// that is flags which can change the code generated. The current method
1824// of mapping from already compiled full code without debug break slots
1825// to full code with debug break slots depends on the generated code is
1826// otherwise exactly the same.
1827static bool CompileFullCodeForDebugging(Handle<JSFunction> function,
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001828 Handle<Code> current_code) {
1829 ASSERT(!current_code->has_debug_break_slots());
1830
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001831 CompilationInfoWithZone info(function);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001832 info.MarkCompilingForDebugging(current_code);
1833 ASSERT(!info.shared_info()->is_compiled());
1834 ASSERT(!info.isolate()->has_pending_exception());
1835
1836 // Use compile lazy which will end up compiling the full code in the
1837 // configuration configured above.
1838 bool result = Compiler::CompileLazy(&info);
1839 ASSERT(result != Isolate::Current()->has_pending_exception());
1840 info.isolate()->clear_pending_exception();
1841#if DEBUG
1842 if (result) {
mmassi@chromium.org7028c052012-06-13 11:51:58 +00001843 Handle<Code> new_code(function->shared()->code());
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001844 ASSERT(new_code->has_debug_break_slots());
1845 ASSERT(current_code->is_compiled_optimizable() ==
1846 new_code->is_compiled_optimizable());
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001847 }
1848#endif
1849 return result;
1850}
1851
1852
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001853static void CollectActiveFunctionsFromThread(
1854 Isolate* isolate,
1855 ThreadLocalTop* top,
1856 List<Handle<JSFunction> >* active_functions,
1857 Object* active_code_marker) {
1858 // Find all non-optimized code functions with activation frames
1859 // on the stack. This includes functions which have optimized
1860 // activations (including inlined functions) on the stack as the
1861 // non-optimized code is needed for the lazy deoptimization.
1862 for (JavaScriptFrameIterator it(isolate, top); !it.done(); it.Advance()) {
1863 JavaScriptFrame* frame = it.frame();
1864 if (frame->is_optimized()) {
1865 List<JSFunction*> functions(Compiler::kMaxInliningLevels + 1);
1866 frame->GetFunctions(&functions);
1867 for (int i = 0; i < functions.length(); i++) {
1868 JSFunction* function = functions[i];
1869 active_functions->Add(Handle<JSFunction>(function));
1870 function->shared()->code()->set_gc_metadata(active_code_marker);
1871 }
1872 } else if (frame->function()->IsJSFunction()) {
1873 JSFunction* function = JSFunction::cast(frame->function());
1874 ASSERT(frame->LookupCode()->kind() == Code::FUNCTION);
1875 active_functions->Add(Handle<JSFunction>(function));
1876 function->shared()->code()->set_gc_metadata(active_code_marker);
1877 }
1878 }
1879}
1880
1881
1882static void RedirectActivationsToRecompiledCodeOnThread(
1883 Isolate* isolate,
1884 ThreadLocalTop* top) {
1885 for (JavaScriptFrameIterator it(isolate, top); !it.done(); it.Advance()) {
1886 JavaScriptFrame* frame = it.frame();
1887
1888 if (frame->is_optimized() || !frame->function()->IsJSFunction()) continue;
1889
1890 JSFunction* function = JSFunction::cast(frame->function());
1891
1892 ASSERT(frame->LookupCode()->kind() == Code::FUNCTION);
1893
1894 Handle<Code> frame_code(frame->LookupCode());
1895 if (frame_code->has_debug_break_slots()) continue;
1896
1897 Handle<Code> new_code(function->shared()->code());
1898 if (new_code->kind() != Code::FUNCTION ||
1899 !new_code->has_debug_break_slots()) {
1900 continue;
1901 }
1902
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001903 // Iterate over the RelocInfo in the original code to compute the sum of the
1904 // constant pools sizes. (See Assembler::CheckConstPool())
1905 // Note that this is only useful for architectures using constant pools.
1906 int constpool_mask = RelocInfo::ModeMask(RelocInfo::CONST_POOL);
1907 int frame_const_pool_size = 0;
1908 for (RelocIterator it(*frame_code, constpool_mask); !it.done(); it.next()) {
1909 RelocInfo* info = it.rinfo();
1910 if (info->pc() >= frame->pc()) break;
1911 frame_const_pool_size += static_cast<int>(info->data());
1912 }
1913 intptr_t frame_offset =
1914 frame->pc() - frame_code->instruction_start() - frame_const_pool_size;
1915
1916 // Iterate over the RelocInfo for new code to find the number of bytes
1917 // generated for debug slots and constant pools.
1918 int debug_break_slot_bytes = 0;
1919 int new_code_const_pool_size = 0;
1920 int mask = RelocInfo::ModeMask(RelocInfo::DEBUG_BREAK_SLOT) |
1921 RelocInfo::ModeMask(RelocInfo::CONST_POOL);
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001922 for (RelocIterator it(*new_code, mask); !it.done(); it.next()) {
1923 // Check if the pc in the new code with debug break
1924 // slots is before this slot.
1925 RelocInfo* info = it.rinfo();
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001926 intptr_t new_offset = info->pc() - new_code->instruction_start() -
1927 new_code_const_pool_size - debug_break_slot_bytes;
1928 if (new_offset >= frame_offset) {
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001929 break;
1930 }
1931
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001932 if (RelocInfo::IsDebugBreakSlot(info->rmode())) {
1933 debug_break_slot_bytes += Assembler::kDebugBreakSlotLength;
1934 } else {
1935 ASSERT(RelocInfo::IsConstPool(info->rmode()));
1936 // The size of the constant pool is encoded in the data.
1937 new_code_const_pool_size += static_cast<int>(info->data());
1938 }
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001939 }
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001940
1941 // Compute the equivalent pc in the new code.
1942 byte* new_pc = new_code->instruction_start() + frame_offset +
1943 debug_break_slot_bytes + new_code_const_pool_size;
1944
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001945 if (FLAG_trace_deopt) {
1946 PrintF("Replacing code %08" V8PRIxPTR " - %08" V8PRIxPTR " (%d) "
1947 "with %08" V8PRIxPTR " - %08" V8PRIxPTR " (%d) "
1948 "for debugging, "
1949 "changing pc from %08" V8PRIxPTR " to %08" V8PRIxPTR "\n",
1950 reinterpret_cast<intptr_t>(
1951 frame_code->instruction_start()),
1952 reinterpret_cast<intptr_t>(
1953 frame_code->instruction_start()) +
1954 frame_code->instruction_size(),
1955 frame_code->instruction_size(),
1956 reinterpret_cast<intptr_t>(new_code->instruction_start()),
1957 reinterpret_cast<intptr_t>(new_code->instruction_start()) +
1958 new_code->instruction_size(),
1959 new_code->instruction_size(),
1960 reinterpret_cast<intptr_t>(frame->pc()),
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001961 reinterpret_cast<intptr_t>(new_pc));
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001962 }
1963
1964 // Patch the return address to return into the code with
1965 // debug break slots.
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001966 frame->set_pc(new_pc);
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001967 }
1968}
1969
1970
1971class ActiveFunctionsCollector : public ThreadVisitor {
1972 public:
1973 explicit ActiveFunctionsCollector(List<Handle<JSFunction> >* active_functions,
1974 Object* active_code_marker)
1975 : active_functions_(active_functions),
1976 active_code_marker_(active_code_marker) { }
1977
1978 void VisitThread(Isolate* isolate, ThreadLocalTop* top) {
1979 CollectActiveFunctionsFromThread(isolate,
1980 top,
1981 active_functions_,
1982 active_code_marker_);
1983 }
1984
1985 private:
1986 List<Handle<JSFunction> >* active_functions_;
1987 Object* active_code_marker_;
1988};
1989
1990
1991class ActiveFunctionsRedirector : public ThreadVisitor {
1992 public:
1993 void VisitThread(Isolate* isolate, ThreadLocalTop* top) {
1994 RedirectActivationsToRecompiledCodeOnThread(isolate, top);
1995 }
1996};
1997
1998
lrn@chromium.org34e60782011-09-15 07:25:40 +00001999void Debug::PrepareForBreakPoints() {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002000 // If preparing for the first break point make sure to deoptimize all
2001 // functions as debugging does not work with optimized code.
2002 if (!has_break_points_) {
2003 Deoptimizer::DeoptimizeAll();
lrn@chromium.org34e60782011-09-15 07:25:40 +00002004
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002005 Handle<Code> lazy_compile =
2006 Handle<Code>(isolate_->builtins()->builtin(Builtins::kLazyCompile));
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002007
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00002008 // There will be at least one break point when we are done.
2009 has_break_points_ = true;
2010
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002011 // Keep the list of activated functions in a handlified list as it
2012 // is used both in GC and non-GC code.
2013 List<Handle<JSFunction> > active_functions(100);
lrn@chromium.org34e60782011-09-15 07:25:40 +00002014
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002015 {
2016 // We are going to iterate heap to find all functions without
2017 // debug break slots.
hpayer@chromium.org7c3372b2013-02-13 17:26:04 +00002018 Heap* heap = isolate_->heap();
2019 heap->CollectAllGarbage(Heap::kMakeHeapIterableMask,
2020 "preparing for breakpoints");
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002021
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002022 // Ensure no GC in this scope as we are going to use gc_metadata
2023 // field in the Code object to mark active functions.
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002024 AssertNoAllocation no_allocation;
2025
hpayer@chromium.org7c3372b2013-02-13 17:26:04 +00002026 Object* active_code_marker = heap->the_hole_value();
svenpanne@chromium.orgecb9dd62011-12-01 08:22:35 +00002027
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002028 CollectActiveFunctionsFromThread(isolate_,
2029 isolate_->thread_local_top(),
2030 &active_functions,
2031 active_code_marker);
2032 ActiveFunctionsCollector active_functions_collector(&active_functions,
2033 active_code_marker);
2034 isolate_->thread_manager()->IterateArchivedThreads(
2035 &active_functions_collector);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002036
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002037 // Scan the heap for all non-optimized functions which have no
2038 // debug break slots and are not active or inlined into an active
2039 // function and mark them for lazy compilation.
hpayer@chromium.org7c3372b2013-02-13 17:26:04 +00002040 HeapIterator iterator(heap);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002041 HeapObject* obj = NULL;
2042 while (((obj = iterator.next()) != NULL)) {
2043 if (obj->IsJSFunction()) {
2044 JSFunction* function = JSFunction::cast(obj);
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002045 SharedFunctionInfo* shared = function->shared();
2046 if (shared->allows_lazy_compilation() &&
2047 shared->script()->IsScript() &&
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002048 function->code()->kind() == Code::FUNCTION &&
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002049 !function->code()->has_debug_break_slots() &&
2050 shared->code()->gc_metadata() != active_code_marker) {
2051 function->set_code(*lazy_compile);
2052 function->shared()->set_code(*lazy_compile);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002053 }
2054 }
lrn@chromium.org34e60782011-09-15 07:25:40 +00002055 }
lrn@chromium.org34e60782011-09-15 07:25:40 +00002056
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002057 // Clear gc_metadata field.
2058 for (int i = 0; i < active_functions.length(); i++) {
2059 Handle<JSFunction> function = active_functions[i];
2060 function->shared()->code()->set_gc_metadata(Smi::FromInt(0));
2061 }
2062 }
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002063
2064 // Now recompile all functions with activation frames and and
2065 // patch the return address to run in the new compiled code.
2066 for (int i = 0; i < active_functions.length(); i++) {
2067 Handle<JSFunction> function = active_functions[i];
mmassi@chromium.org7028c052012-06-13 11:51:58 +00002068 Handle<SharedFunctionInfo> shared(function->shared());
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002069
2070 if (function->code()->kind() == Code::FUNCTION &&
2071 function->code()->has_debug_break_slots()) {
2072 // Nothing to do. Function code already had debug break slots.
2073 continue;
2074 }
2075
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002076 // If recompilation is not possible just skip it.
2077 if (shared->is_toplevel() ||
2078 !shared->allows_lazy_compilation() ||
2079 shared->code()->kind() == Code::BUILTIN) {
2080 continue;
2081 }
2082
2083 // Make sure that the shared full code is compiled with debug
2084 // break slots.
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00002085 if (!shared->code()->has_debug_break_slots()) {
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002086 // Try to compile the full code with debug break slots. If it
2087 // fails just keep the current code.
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00002088 Handle<Code> current_code(function->shared()->code());
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002089 shared->set_code(*lazy_compile);
2090 bool prev_force_debugger_active =
2091 isolate_->debugger()->force_debugger_active();
2092 isolate_->debugger()->set_force_debugger_active(true);
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00002093 ASSERT(current_code->kind() == Code::FUNCTION);
mmassi@chromium.org7028c052012-06-13 11:51:58 +00002094 CompileFullCodeForDebugging(function, current_code);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002095 isolate_->debugger()->set_force_debugger_active(
2096 prev_force_debugger_active);
2097 if (!shared->is_compiled()) {
2098 shared->set_code(*current_code);
2099 continue;
2100 }
2101 }
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002102
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002103 // Keep function code in sync with shared function info.
2104 function->set_code(shared->code());
lrn@chromium.org34e60782011-09-15 07:25:40 +00002105 }
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002106
2107 RedirectActivationsToRecompiledCodeOnThread(isolate_,
2108 isolate_->thread_local_top());
2109
2110 ActiveFunctionsRedirector active_functions_redirector;
2111 isolate_->thread_manager()->IterateArchivedThreads(
2112 &active_functions_redirector);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002113 }
lrn@chromium.org34e60782011-09-15 07:25:40 +00002114}
2115
2116
jkummerow@chromium.org78502a92012-09-06 13:50:42 +00002117Object* Debug::FindSharedFunctionInfoInScript(Handle<Script> script,
2118 int position) {
2119 // Iterate the heap looking for SharedFunctionInfo generated from the
2120 // script. The inner most SharedFunctionInfo containing the source position
2121 // for the requested break point is found.
2122 // NOTE: This might require several heap iterations. If the SharedFunctionInfo
2123 // which is found is not compiled it is compiled and the heap is iterated
2124 // again as the compilation might create inner functions from the newly
2125 // compiled function and the actual requested break point might be in one of
2126 // these functions.
2127 // NOTE: The below fix-point iteration depends on all functions that cannot be
2128 // compiled lazily without a context to not be compiled at all. Compilation
2129 // will be triggered at points where we do not need a context.
2130 bool done = false;
2131 // The current candidate for the source position:
2132 int target_start_position = RelocInfo::kNoPosition;
2133 Handle<JSFunction> target_function;
2134 Handle<SharedFunctionInfo> target;
hpayer@chromium.org7c3372b2013-02-13 17:26:04 +00002135 Heap* heap = isolate_->heap();
jkummerow@chromium.org78502a92012-09-06 13:50:42 +00002136 while (!done) {
2137 { // Extra scope for iterator and no-allocation.
hpayer@chromium.org7c3372b2013-02-13 17:26:04 +00002138 heap->EnsureHeapIsIterable();
jkummerow@chromium.org78502a92012-09-06 13:50:42 +00002139 AssertNoAllocation no_alloc_during_heap_iteration;
hpayer@chromium.org7c3372b2013-02-13 17:26:04 +00002140 HeapIterator iterator(heap);
jkummerow@chromium.org78502a92012-09-06 13:50:42 +00002141 for (HeapObject* obj = iterator.next();
2142 obj != NULL; obj = iterator.next()) {
2143 bool found_next_candidate = false;
2144 Handle<JSFunction> function;
2145 Handle<SharedFunctionInfo> shared;
2146 if (obj->IsJSFunction()) {
2147 function = Handle<JSFunction>(JSFunction::cast(obj));
2148 shared = Handle<SharedFunctionInfo>(function->shared());
2149 ASSERT(shared->allows_lazy_compilation() || shared->is_compiled());
2150 found_next_candidate = true;
2151 } else if (obj->IsSharedFunctionInfo()) {
2152 shared = Handle<SharedFunctionInfo>(SharedFunctionInfo::cast(obj));
2153 // Skip functions that we cannot compile lazily without a context,
2154 // which is not available here, because there is no closure.
2155 found_next_candidate = shared->is_compiled() ||
2156 shared->allows_lazy_compilation_without_context();
2157 }
2158 if (!found_next_candidate) continue;
2159 if (shared->script() == *script) {
2160 // If the SharedFunctionInfo found has the requested script data and
2161 // contains the source position it is a candidate.
2162 int start_position = shared->function_token_position();
2163 if (start_position == RelocInfo::kNoPosition) {
2164 start_position = shared->start_position();
2165 }
2166 if (start_position <= position &&
2167 position <= shared->end_position()) {
2168 // If there is no candidate or this function is within the current
2169 // candidate this is the new candidate.
2170 if (target.is_null()) {
2171 target_start_position = start_position;
2172 target_function = function;
2173 target = shared;
2174 } else {
2175 if (target_start_position == start_position &&
2176 shared->end_position() == target->end_position()) {
2177 // If a top-level function contains only one function
2178 // declaration the source for the top-level and the function
2179 // is the same. In that case prefer the non top-level function.
2180 if (!shared->is_toplevel()) {
2181 target_start_position = start_position;
2182 target_function = function;
2183 target = shared;
2184 }
2185 } else if (target_start_position <= start_position &&
2186 shared->end_position() <= target->end_position()) {
2187 // This containment check includes equality as a function
2188 // inside a top-level function can share either start or end
2189 // position with the top-level function.
2190 target_start_position = start_position;
2191 target_function = function;
2192 target = shared;
2193 }
2194 }
2195 }
2196 }
2197 } // End for loop.
2198 } // End no-allocation scope.
2199
hpayer@chromium.org7c3372b2013-02-13 17:26:04 +00002200 if (target.is_null()) return heap->undefined_value();
jkummerow@chromium.org78502a92012-09-06 13:50:42 +00002201
2202 // There will be at least one break point when we are done.
2203 has_break_points_ = true;
2204
2205 // If the candidate found is compiled we are done.
2206 done = target->is_compiled();
2207 if (!done) {
2208 // If the candidate is not compiled, compile it to reveal any inner
2209 // functions which might contain the requested source position. This
2210 // will compile all inner functions that cannot be compiled without a
2211 // context, because Compiler::BuildFunctionInfo checks whether the
2212 // debugger is active.
2213 if (target_function.is_null()) {
2214 SharedFunctionInfo::CompileLazy(target, KEEP_EXCEPTION);
2215 } else {
2216 JSFunction::CompileLazy(target_function, KEEP_EXCEPTION);
2217 }
2218 }
2219 } // End while loop.
2220
2221 return *target;
2222}
2223
2224
lrn@chromium.org34e60782011-09-15 07:25:40 +00002225// Ensures the debug information is present for shared.
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00002226bool Debug::EnsureDebugInfo(Handle<SharedFunctionInfo> shared,
2227 Handle<JSFunction> function) {
lrn@chromium.org34e60782011-09-15 07:25:40 +00002228 // Return if we already have the debug info for shared.
2229 if (HasDebugInfo(shared)) {
2230 ASSERT(shared->is_compiled());
2231 return true;
2232 }
2233
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00002234 // There will be at least one break point when we are done.
2235 has_break_points_ = true;
2236
2237 // Ensure function is compiled. Return false if this failed.
2238 if (!function.is_null() &&
2239 !JSFunction::EnsureCompiled(function, CLEAR_EXCEPTION)) {
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002240 return false;
2241 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002242
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002243 // Create the debug info object.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002244 Handle<DebugInfo> debug_info = FACTORY->NewDebugInfo(shared);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002245
2246 // Add debug info to the list.
2247 DebugInfoListNode* node = new DebugInfoListNode(*debug_info);
2248 node->set_next(debug_info_list_);
2249 debug_info_list_ = node;
2250
kasper.lundbd3ec4e2008-07-09 11:06:54 +00002251 return true;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002252}
2253
2254
2255void Debug::RemoveDebugInfo(Handle<DebugInfo> debug_info) {
2256 ASSERT(debug_info_list_ != NULL);
2257 // Run through the debug info objects to find this one and remove it.
2258 DebugInfoListNode* prev = NULL;
2259 DebugInfoListNode* current = debug_info_list_;
2260 while (current != NULL) {
2261 if (*current->debug_info() == *debug_info) {
2262 // Unlink from list. If prev is NULL we are looking at the first element.
2263 if (prev == NULL) {
2264 debug_info_list_ = current->next();
2265 } else {
2266 prev->set_next(current->next());
2267 }
lrn@chromium.org7516f052011-03-30 08:52:27 +00002268 current->debug_info()->shared()->set_debug_info(
2269 isolate_->heap()->undefined_value());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002270 delete current;
2271
2272 // If there are no more debug info objects there are not more break
2273 // points.
2274 has_break_points_ = debug_info_list_ != NULL;
2275
2276 return;
2277 }
2278 // Move to next in list.
2279 prev = current;
2280 current = current->next();
2281 }
2282 UNREACHABLE();
2283}
2284
2285
2286void Debug::SetAfterBreakTarget(JavaScriptFrame* frame) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002287 HandleScope scope(isolate_);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002288
lrn@chromium.org34e60782011-09-15 07:25:40 +00002289 PrepareForBreakPoints();
2290
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002291 // Get the executing function in which the debug break occurred.
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00002292 Handle<JSFunction> function(JSFunction::cast(frame->function()));
2293 Handle<SharedFunctionInfo> shared(function->shared());
2294 if (!EnsureDebugInfo(shared, function)) {
kasper.lundbd3ec4e2008-07-09 11:06:54 +00002295 // Return if we failed to retrieve the debug info.
2296 return;
2297 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002298 Handle<DebugInfo> debug_info = GetDebugInfo(shared);
2299 Handle<Code> code(debug_info->code());
2300 Handle<Code> original_code(debug_info->original_code());
2301#ifdef DEBUG
2302 // Get the code which is actually executing.
vegorov@chromium.org74f333b2011-04-06 11:17:46 +00002303 Handle<Code> frame_code(frame->LookupCode());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002304 ASSERT(frame_code.is_identical_to(code));
2305#endif
2306
2307 // Find the call address in the running code. This address holds the call to
2308 // either a DebugBreakXXX or to the debug break return entry code if the
2309 // break point is still active after processing the break point.
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00002310 Address addr = frame->pc() - Assembler::kPatchDebugBreakSlotReturnOffset;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002311
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002312 // Check if the location is at JS exit or debug break slot.
ager@chromium.orga1645e22009-09-09 19:27:10 +00002313 bool at_js_return = false;
2314 bool break_at_js_return_active = false;
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002315 bool at_debug_break_slot = false;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002316 RelocIterator it(debug_info->code());
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002317 while (!it.done() && !at_js_return && !at_debug_break_slot) {
ager@chromium.org236ad962008-09-25 09:45:57 +00002318 if (RelocInfo::IsJSReturn(it.rinfo()->rmode())) {
ager@chromium.orga1645e22009-09-09 19:27:10 +00002319 at_js_return = (it.rinfo()->pc() ==
2320 addr - Assembler::kPatchReturnSequenceAddressOffset);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002321 break_at_js_return_active = it.rinfo()->IsPatchedReturnSequence();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002322 }
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002323 if (RelocInfo::IsDebugBreakSlot(it.rinfo()->rmode())) {
2324 at_debug_break_slot = (it.rinfo()->pc() ==
2325 addr - Assembler::kPatchDebugBreakSlotAddressOffset);
2326 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002327 it.next();
2328 }
2329
2330 // Handle the jump to continue execution after break point depending on the
2331 // break location.
ager@chromium.orga1645e22009-09-09 19:27:10 +00002332 if (at_js_return) {
2333 // If the break point as return is still active jump to the corresponding
2334 // place in the original code. If not the break point was removed during
2335 // break point processing.
2336 if (break_at_js_return_active) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002337 addr += original_code->instruction_start() - code->instruction_start();
2338 }
2339
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00002340 // Move back to where the call instruction sequence started.
2341 thread_local_.after_break_target_ =
2342 addr - Assembler::kPatchReturnSequenceAddressOffset;
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002343 } else if (at_debug_break_slot) {
2344 // Address of where the debug break slot starts.
2345 addr = addr - Assembler::kPatchDebugBreakSlotAddressOffset;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002346
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002347 // Continue just after the slot.
2348 thread_local_.after_break_target_ = addr + Assembler::kDebugBreakSlotLength;
2349 } else if (IsDebugBreak(Assembler::target_address_at(addr))) {
2350 // We now know that there is still a debug break call at the target address,
2351 // so the break point is still there and the original code will hold the
2352 // address to jump to in order to complete the call which is replaced by a
2353 // call to DebugBreakXXX.
2354
2355 // Find the corresponding address in the original code.
2356 addr += original_code->instruction_start() - code->instruction_start();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002357
2358 // Install jump to the call address in the original code. This will be the
2359 // call which was overwritten by the call to DebugBreakXXX.
2360 thread_local_.after_break_target_ = Assembler::target_address_at(addr);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002361 } else {
2362 // There is no longer a break point present. Don't try to look in the
2363 // original code as the running code will have the right address. This takes
2364 // care of the case where the last break point is removed from the function
2365 // and therefore no "original code" is available.
2366 thread_local_.after_break_target_ = Assembler::target_address_at(addr);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002367 }
2368}
2369
2370
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00002371bool Debug::IsBreakAtReturn(JavaScriptFrame* frame) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002372 HandleScope scope(isolate_);
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00002373
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00002374 // If there are no break points this cannot be break at return, as
2375 // the debugger statement and stack guard bebug break cannot be at
2376 // return.
2377 if (!has_break_points_) {
2378 return false;
2379 }
2380
lrn@chromium.org34e60782011-09-15 07:25:40 +00002381 PrepareForBreakPoints();
2382
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00002383 // Get the executing function in which the debug break occurred.
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00002384 Handle<JSFunction> function(JSFunction::cast(frame->function()));
2385 Handle<SharedFunctionInfo> shared(function->shared());
2386 if (!EnsureDebugInfo(shared, function)) {
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00002387 // Return if we failed to retrieve the debug info.
2388 return false;
2389 }
2390 Handle<DebugInfo> debug_info = GetDebugInfo(shared);
2391 Handle<Code> code(debug_info->code());
2392#ifdef DEBUG
2393 // Get the code which is actually executing.
vegorov@chromium.org74f333b2011-04-06 11:17:46 +00002394 Handle<Code> frame_code(frame->LookupCode());
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00002395 ASSERT(frame_code.is_identical_to(code));
2396#endif
2397
2398 // Find the call address in the running code.
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00002399 Address addr = frame->pc() - Assembler::kPatchDebugBreakSlotReturnOffset;
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00002400
2401 // Check if the location is at JS return.
2402 RelocIterator it(debug_info->code());
2403 while (!it.done()) {
2404 if (RelocInfo::IsJSReturn(it.rinfo()->rmode())) {
2405 return (it.rinfo()->pc() ==
2406 addr - Assembler::kPatchReturnSequenceAddressOffset);
2407 }
2408 it.next();
2409 }
2410 return false;
2411}
2412
2413
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00002414void Debug::FramesHaveBeenDropped(StackFrame::Id new_break_frame_id,
whesse@chromium.orge90029b2010-08-02 11:52:17 +00002415 FrameDropMode mode,
2416 Object** restarter_frame_function_pointer) {
ulan@chromium.orgd9e468a2012-06-25 09:47:40 +00002417 if (mode != CURRENTLY_SET_MODE) {
2418 thread_local_.frame_drop_mode_ = mode;
2419 }
ager@chromium.org357bf652010-04-12 11:30:10 +00002420 thread_local_.break_frame_id_ = new_break_frame_id;
whesse@chromium.orge90029b2010-08-02 11:52:17 +00002421 thread_local_.restarter_frame_function_pointer_ =
2422 restarter_frame_function_pointer;
ager@chromium.org357bf652010-04-12 11:30:10 +00002423}
2424
2425
jkummerow@chromium.org212d9642012-05-11 15:02:09 +00002426const int Debug::FramePaddingLayout::kInitialSize = 1;
2427
2428
2429// Any even value bigger than kInitialSize as needed for stack scanning.
2430const int Debug::FramePaddingLayout::kPaddingValue = kInitialSize + 1;
2431
2432
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002433bool Debug::IsDebugGlobal(GlobalObject* global) {
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00002434 return IsLoaded() && global == debug_context()->global_object();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002435}
2436
2437
ager@chromium.org32912102009-01-16 10:38:43 +00002438void Debug::ClearMirrorCache() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002439 PostponeInterruptsScope postpone(isolate_);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002440 HandleScope scope(isolate_);
2441 ASSERT(isolate_->context() == *Debug::debug_context());
ager@chromium.org32912102009-01-16 10:38:43 +00002442
2443 // Clear the mirror cache.
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00002444 Handle<String> function_name = isolate_->factory()->InternalizeOneByteString(
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00002445 STATIC_ASCII_VECTOR("ClearMirrorCache"));
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00002446 Handle<Object> fun(
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00002447 isolate_->global_object()->GetPropertyNoExceptionThrown(*function_name),
2448 isolate_);
ager@chromium.org32912102009-01-16 10:38:43 +00002449 ASSERT(fun->IsJSFunction());
2450 bool caught_exception;
rossberg@chromium.org717967f2011-07-20 13:44:42 +00002451 Execution::TryCall(Handle<JSFunction>::cast(fun),
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00002452 Handle<JSObject>(Debug::debug_context()->global_object()),
ager@chromium.org32912102009-01-16 10:38:43 +00002453 0, NULL, &caught_exception);
2454}
2455
2456
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002457void Debug::CreateScriptCache() {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002458 Heap* heap = isolate_->heap();
2459 HandleScope scope(isolate_);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002460
2461 // Perform two GCs to get rid of all unreferenced scripts. The first GC gets
2462 // rid of all the cached script wrappers and the second gets rid of the
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002463 // scripts which are no longer referenced. The second also sweeps precisely,
2464 // which saves us doing yet another GC to make the heap iterable.
rossberg@chromium.org994edf62012-02-06 10:12:55 +00002465 heap->CollectAllGarbage(Heap::kNoGCFlags, "Debug::CreateScriptCache");
2466 heap->CollectAllGarbage(Heap::kMakeHeapIterableMask,
2467 "Debug::CreateScriptCache");
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002468
2469 ASSERT(script_cache_ == NULL);
2470 script_cache_ = new ScriptCache();
2471
2472 // Scan heap for Script objects.
2473 int count = 0;
hpayer@chromium.org7c3372b2013-02-13 17:26:04 +00002474 HeapIterator iterator(heap);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002475 AssertNoAllocation no_allocation;
2476
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002477 for (HeapObject* obj = iterator.next(); obj != NULL; obj = iterator.next()) {
sgjesse@chromium.org152a0b02009-10-07 13:50:16 +00002478 if (obj->IsScript() && Script::cast(obj)->HasValidSource()) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002479 script_cache_->Add(Handle<Script>(Script::cast(obj)));
2480 count++;
2481 }
2482 }
2483}
2484
2485
2486void Debug::DestroyScriptCache() {
2487 // Get rid of the script cache if it was created.
2488 if (script_cache_ != NULL) {
2489 delete script_cache_;
2490 script_cache_ = NULL;
2491 }
2492}
2493
2494
2495void Debug::AddScriptToScriptCache(Handle<Script> script) {
2496 if (script_cache_ != NULL) {
2497 script_cache_->Add(script);
2498 }
2499}
2500
2501
2502Handle<FixedArray> Debug::GetLoadedScripts() {
2503 // Create and fill the script cache when the loaded scripts is requested for
2504 // the first time.
2505 if (script_cache_ == NULL) {
2506 CreateScriptCache();
2507 }
2508
2509 // If the script cache is not active just return an empty array.
2510 ASSERT(script_cache_ != NULL);
2511 if (script_cache_ == NULL) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002512 isolate_->factory()->NewFixedArray(0);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002513 }
2514
2515 // Perform GC to get unreferenced scripts evicted from the cache before
2516 // returning the content.
rossberg@chromium.org994edf62012-02-06 10:12:55 +00002517 isolate_->heap()->CollectAllGarbage(Heap::kNoGCFlags,
2518 "Debug::GetLoadedScripts");
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002519
2520 // Get the scripts from the cache.
2521 return script_cache_->GetScripts();
2522}
2523
2524
2525void Debug::AfterGarbageCollection() {
2526 // Generate events for collected scripts.
2527 if (script_cache_ != NULL) {
2528 script_cache_->ProcessCollectedScripts();
2529 }
2530}
2531
2532
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00002533Debugger::Debugger(Isolate* isolate)
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +00002534 : debugger_access_(isolate->debugger_access()),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002535 event_listener_(Handle<Object>()),
2536 event_listener_data_(Handle<Object>()),
2537 compiling_natives_(false),
2538 is_loading_debugger_(false),
mstarzinger@chromium.orgde886792012-09-11 13:22:37 +00002539 live_edit_enabled_(true),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002540 never_unload_debugger_(false),
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002541 force_debugger_active_(false),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002542 message_handler_(NULL),
2543 debugger_unload_pending_(false),
2544 host_dispatch_handler_(NULL),
2545 dispatch_handler_access_(OS::CreateMutex()),
2546 debug_message_dispatch_handler_(NULL),
2547 message_dispatch_helper_thread_(NULL),
2548 host_dispatch_micros_(100 * 1000),
2549 agent_(NULL),
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00002550 command_queue_(isolate->logger(), kQueueInitialSize),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002551 command_received_(OS::CreateSemaphore(0)),
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00002552 event_command_queue_(isolate->logger(), kQueueInitialSize),
2553 isolate_(isolate) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002554}
2555
2556
2557Debugger::~Debugger() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002558 delete dispatch_handler_access_;
2559 dispatch_handler_access_ = 0;
2560 delete command_received_;
2561 command_received_ = 0;
2562}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002563
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002564
2565Handle<Object> Debugger::MakeJSObject(Vector<const char> constructor_name,
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002566 int argc,
2567 Handle<Object> argv[],
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002568 bool* caught_exception) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002569 ASSERT(isolate_->context() == *isolate_->debug()->debug_context());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002570
2571 // Create the execution state object.
lrn@chromium.org7516f052011-03-30 08:52:27 +00002572 Handle<String> constructor_str =
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00002573 isolate_->factory()->InternalizeUtf8String(constructor_name);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002574 Handle<Object> constructor(
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00002575 isolate_->global_object()->GetPropertyNoExceptionThrown(*constructor_str),
2576 isolate_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002577 ASSERT(constructor->IsJSFunction());
2578 if (!constructor->IsJSFunction()) {
2579 *caught_exception = true;
lrn@chromium.org7516f052011-03-30 08:52:27 +00002580 return isolate_->factory()->undefined_value();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002581 }
2582 Handle<Object> js_object = Execution::TryCall(
2583 Handle<JSFunction>::cast(constructor),
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00002584 Handle<JSObject>(isolate_->debug()->debug_context()->global_object()),
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002585 argc,
2586 argv,
2587 caught_exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002588 return js_object;
2589}
2590
2591
2592Handle<Object> Debugger::MakeExecutionState(bool* caught_exception) {
2593 // Create the execution state object.
lrn@chromium.org7516f052011-03-30 08:52:27 +00002594 Handle<Object> break_id = isolate_->factory()->NewNumberFromInt(
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002595 isolate_->debug()->break_id());
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002596 Handle<Object> argv[] = { break_id };
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002597 return MakeJSObject(CStrVector("MakeExecutionState"),
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002598 ARRAY_SIZE(argv),
2599 argv,
2600 caught_exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002601}
2602
2603
2604Handle<Object> Debugger::MakeBreakEvent(Handle<Object> exec_state,
2605 Handle<Object> break_points_hit,
2606 bool* caught_exception) {
2607 // Create the new break event object.
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002608 Handle<Object> argv[] = { exec_state, break_points_hit };
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002609 return MakeJSObject(CStrVector("MakeBreakEvent"),
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002610 ARRAY_SIZE(argv),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002611 argv,
2612 caught_exception);
2613}
2614
2615
2616Handle<Object> Debugger::MakeExceptionEvent(Handle<Object> exec_state,
2617 Handle<Object> exception,
2618 bool uncaught,
2619 bool* caught_exception) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002620 Factory* factory = isolate_->factory();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002621 // Create the new exception event object.
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002622 Handle<Object> argv[] = { exec_state,
2623 exception,
2624 factory->ToBoolean(uncaught) };
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002625 return MakeJSObject(CStrVector("MakeExceptionEvent"),
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002626 ARRAY_SIZE(argv),
2627 argv,
2628 caught_exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002629}
2630
2631
2632Handle<Object> Debugger::MakeNewFunctionEvent(Handle<Object> function,
2633 bool* caught_exception) {
2634 // Create the new function event object.
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002635 Handle<Object> argv[] = { function };
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002636 return MakeJSObject(CStrVector("MakeNewFunctionEvent"),
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002637 ARRAY_SIZE(argv),
2638 argv,
2639 caught_exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002640}
2641
2642
2643Handle<Object> Debugger::MakeCompileEvent(Handle<Script> script,
iposva@chromium.org245aa852009-02-10 00:49:54 +00002644 bool before,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002645 bool* caught_exception) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002646 Factory* factory = isolate_->factory();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002647 // Create the compile event object.
2648 Handle<Object> exec_state = MakeExecutionState(caught_exception);
iposva@chromium.org245aa852009-02-10 00:49:54 +00002649 Handle<Object> script_wrapper = GetScriptWrapper(script);
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002650 Handle<Object> argv[] = { exec_state,
2651 script_wrapper,
2652 factory->ToBoolean(before) };
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002653 return MakeJSObject(CStrVector("MakeCompileEvent"),
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002654 ARRAY_SIZE(argv),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002655 argv,
2656 caught_exception);
2657}
2658
2659
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002660Handle<Object> Debugger::MakeScriptCollectedEvent(int id,
2661 bool* caught_exception) {
2662 // Create the script collected event object.
2663 Handle<Object> exec_state = MakeExecutionState(caught_exception);
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00002664 Handle<Object> id_object = Handle<Smi>(Smi::FromInt(id), isolate_);
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002665 Handle<Object> argv[] = { exec_state, id_object };
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002666
2667 return MakeJSObject(CStrVector("MakeScriptCollectedEvent"),
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002668 ARRAY_SIZE(argv),
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002669 argv,
2670 caught_exception);
2671}
2672
2673
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002674void Debugger::OnException(Handle<Object> exception, bool uncaught) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002675 HandleScope scope(isolate_);
2676 Debug* debug = isolate_->debug();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002677
2678 // Bail out based on state or if there is no listener for this event
lrn@chromium.org7516f052011-03-30 08:52:27 +00002679 if (debug->InDebugger()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002680 if (!Debugger::EventActive(v8::Exception)) return;
2681
2682 // Bail out if exception breaks are not active
2683 if (uncaught) {
2684 // Uncaught exceptions are reported by either flags.
lrn@chromium.org7516f052011-03-30 08:52:27 +00002685 if (!(debug->break_on_uncaught_exception() ||
2686 debug->break_on_exception())) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002687 } else {
2688 // Caught exceptions are reported is activated.
lrn@chromium.org7516f052011-03-30 08:52:27 +00002689 if (!debug->break_on_exception()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002690 }
2691
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002692 // Enter the debugger.
2693 EnterDebugger debugger;
2694 if (debugger.FailedToEnter()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002695
2696 // Clear all current stepping setup.
lrn@chromium.org7516f052011-03-30 08:52:27 +00002697 debug->ClearStepping();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002698 // Create the event data object.
2699 bool caught_exception = false;
2700 Handle<Object> exec_state = MakeExecutionState(&caught_exception);
2701 Handle<Object> event_data;
2702 if (!caught_exception) {
2703 event_data = MakeExceptionEvent(exec_state, exception, uncaught,
2704 &caught_exception);
2705 }
2706 // Bail out and don't call debugger if exception.
2707 if (caught_exception) {
2708 return;
2709 }
2710
ager@chromium.org5ec48922009-05-05 07:25:34 +00002711 // Process debug event.
2712 ProcessDebugEvent(v8::Exception, Handle<JSObject>::cast(event_data), false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002713 // Return to continue execution from where the exception was thrown.
2714}
2715
2716
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002717void Debugger::OnDebugBreak(Handle<Object> break_points_hit,
2718 bool auto_continue) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002719 HandleScope scope(isolate_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002720
kasper.lund212ac232008-07-16 07:07:30 +00002721 // Debugger has already been entered by caller.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002722 ASSERT(isolate_->context() == *isolate_->debug()->debug_context());
kasper.lund212ac232008-07-16 07:07:30 +00002723
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002724 // Bail out if there is no listener for this event
2725 if (!Debugger::EventActive(v8::Break)) return;
2726
2727 // Debugger must be entered in advance.
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00002728 ASSERT(isolate_->context() == *isolate_->debug()->debug_context());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002729
2730 // Create the event data object.
2731 bool caught_exception = false;
2732 Handle<Object> exec_state = MakeExecutionState(&caught_exception);
2733 Handle<Object> event_data;
2734 if (!caught_exception) {
2735 event_data = MakeBreakEvent(exec_state, break_points_hit,
2736 &caught_exception);
2737 }
2738 // Bail out and don't call debugger if exception.
2739 if (caught_exception) {
2740 return;
2741 }
2742
ager@chromium.org5ec48922009-05-05 07:25:34 +00002743 // Process debug event.
2744 ProcessDebugEvent(v8::Break,
2745 Handle<JSObject>::cast(event_data),
2746 auto_continue);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002747}
2748
2749
2750void Debugger::OnBeforeCompile(Handle<Script> script) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002751 HandleScope scope(isolate_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002752
2753 // Bail out based on state or if there is no listener for this event
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002754 if (isolate_->debug()->InDebugger()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002755 if (compiling_natives()) return;
2756 if (!EventActive(v8::BeforeCompile)) return;
2757
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002758 // Enter the debugger.
2759 EnterDebugger debugger;
2760 if (debugger.FailedToEnter()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002761
2762 // Create the event data object.
2763 bool caught_exception = false;
iposva@chromium.org245aa852009-02-10 00:49:54 +00002764 Handle<Object> event_data = MakeCompileEvent(script, true, &caught_exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002765 // Bail out and don't call debugger if exception.
2766 if (caught_exception) {
2767 return;
2768 }
2769
ager@chromium.org5ec48922009-05-05 07:25:34 +00002770 // Process debug event.
2771 ProcessDebugEvent(v8::BeforeCompile,
2772 Handle<JSObject>::cast(event_data),
2773 true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002774}
2775
2776
2777// Handle debugger actions when a new script is compiled.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00002778void Debugger::OnAfterCompile(Handle<Script> script,
2779 AfterCompileFlags after_compile_flags) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002780 HandleScope scope(isolate_);
2781 Debug* debug = isolate_->debug();
kasper.lund212ac232008-07-16 07:07:30 +00002782
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002783 // Add the newly compiled script to the script cache.
lrn@chromium.org7516f052011-03-30 08:52:27 +00002784 debug->AddScriptToScriptCache(script);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002785
2786 // No more to do if not debugging.
ager@chromium.org71daaf62009-04-01 07:22:49 +00002787 if (!IsDebuggerActive()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002788
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002789 // No compile events while compiling natives.
2790 if (compiling_natives()) return;
2791
iposva@chromium.org245aa852009-02-10 00:49:54 +00002792 // Store whether in debugger before entering debugger.
lrn@chromium.org7516f052011-03-30 08:52:27 +00002793 bool in_debugger = debug->InDebugger();
iposva@chromium.org245aa852009-02-10 00:49:54 +00002794
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002795 // Enter the debugger.
2796 EnterDebugger debugger;
2797 if (debugger.FailedToEnter()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002798
2799 // If debugging there might be script break points registered for this
2800 // script. Make sure that these break points are set.
2801
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002802 // Get the function UpdateScriptBreakPoints (defined in debug-debugger.js).
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00002803 Handle<String> update_script_break_points_string =
2804 isolate_->factory()->InternalizeOneByteString(
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00002805 STATIC_ASCII_VECTOR("UpdateScriptBreakPoints"));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002806 Handle<Object> update_script_break_points =
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00002807 Handle<Object>(
2808 debug->debug_context()->global_object()->GetPropertyNoExceptionThrown(
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00002809 *update_script_break_points_string),
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00002810 isolate_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002811 if (!update_script_break_points->IsJSFunction()) {
2812 return;
2813 }
2814 ASSERT(update_script_break_points->IsJSFunction());
2815
2816 // Wrap the script object in a proper JS object before passing it
2817 // to JavaScript.
2818 Handle<JSValue> wrapper = GetScriptWrapper(script);
2819
2820 // Call UpdateScriptBreakPoints expect no exceptions.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002821 bool caught_exception;
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002822 Handle<Object> argv[] = { wrapper };
rossberg@chromium.org717967f2011-07-20 13:44:42 +00002823 Execution::TryCall(Handle<JSFunction>::cast(update_script_break_points),
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002824 Isolate::Current()->js_builtins_object(),
2825 ARRAY_SIZE(argv),
2826 argv,
2827 &caught_exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002828 if (caught_exception) {
2829 return;
2830 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002831 // Bail out based on state or if there is no listener for this event
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00002832 if (in_debugger && (after_compile_flags & SEND_WHEN_DEBUGGING) == 0) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002833 if (!Debugger::EventActive(v8::AfterCompile)) return;
2834
2835 // Create the compile state object.
2836 Handle<Object> event_data = MakeCompileEvent(script,
iposva@chromium.org245aa852009-02-10 00:49:54 +00002837 false,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002838 &caught_exception);
2839 // Bail out and don't call debugger if exception.
2840 if (caught_exception) {
2841 return;
2842 }
ager@chromium.org5ec48922009-05-05 07:25:34 +00002843 // Process debug event.
2844 ProcessDebugEvent(v8::AfterCompile,
2845 Handle<JSObject>::cast(event_data),
2846 true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002847}
2848
2849
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002850void Debugger::OnScriptCollected(int id) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002851 HandleScope scope(isolate_);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002852
2853 // No more to do if not debugging.
ulan@chromium.org56c14af2012-09-20 12:51:09 +00002854 if (isolate_->debug()->InDebugger()) return;
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002855 if (!IsDebuggerActive()) return;
2856 if (!Debugger::EventActive(v8::ScriptCollected)) return;
2857
2858 // Enter the debugger.
2859 EnterDebugger debugger;
2860 if (debugger.FailedToEnter()) return;
2861
2862 // Create the script collected state object.
2863 bool caught_exception = false;
2864 Handle<Object> event_data = MakeScriptCollectedEvent(id,
2865 &caught_exception);
2866 // Bail out and don't call debugger if exception.
2867 if (caught_exception) {
2868 return;
2869 }
2870
2871 // Process debug event.
2872 ProcessDebugEvent(v8::ScriptCollected,
2873 Handle<JSObject>::cast(event_data),
2874 true);
2875}
2876
2877
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002878void Debugger::ProcessDebugEvent(v8::DebugEvent event,
ager@chromium.org5ec48922009-05-05 07:25:34 +00002879 Handle<JSObject> event_data,
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002880 bool auto_continue) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002881 HandleScope scope(isolate_);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002882
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +00002883 // Clear any pending debug break if this is a real break.
2884 if (!auto_continue) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002885 isolate_->debug()->clear_interrupt_pending(DEBUGBREAK);
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +00002886 }
2887
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002888 // Create the execution state.
2889 bool caught_exception = false;
2890 Handle<Object> exec_state = MakeExecutionState(&caught_exception);
2891 if (caught_exception) {
2892 return;
2893 }
ager@chromium.org41826e72009-03-30 13:30:57 +00002894 // First notify the message handler if any.
2895 if (message_handler_ != NULL) {
ager@chromium.org5ec48922009-05-05 07:25:34 +00002896 NotifyMessageHandler(event,
2897 Handle<JSObject>::cast(exec_state),
2898 event_data,
2899 auto_continue);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002900 }
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00002901 // Notify registered debug event listener. This can be either a C or
2902 // a JavaScript function. Don't call event listener for v8::Break
2903 // here, if it's only a debug command -- they will be processed later.
2904 if ((event != v8::Break || !auto_continue) && !event_listener_.is_null()) {
2905 CallEventCallback(event, exec_state, event_data, NULL);
2906 }
2907 // Process pending debug commands.
2908 if (event == v8::Break) {
2909 while (!event_command_queue_.IsEmpty()) {
2910 CommandMessage command = event_command_queue_.Get();
2911 if (!event_listener_.is_null()) {
2912 CallEventCallback(v8::BreakForCommand,
2913 exec_state,
2914 event_data,
2915 command.client_data());
2916 }
2917 command.Dispose();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002918 }
2919 }
2920}
2921
2922
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00002923void Debugger::CallEventCallback(v8::DebugEvent event,
2924 Handle<Object> exec_state,
2925 Handle<Object> event_data,
2926 v8::Debug::ClientData* client_data) {
ager@chromium.orgea91cc52011-05-23 06:06:11 +00002927 if (event_listener_->IsForeign()) {
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00002928 CallCEventCallback(event, exec_state, event_data, client_data);
2929 } else {
2930 CallJSEventCallback(event, exec_state, event_data);
2931 }
2932}
2933
2934
2935void Debugger::CallCEventCallback(v8::DebugEvent event,
2936 Handle<Object> exec_state,
2937 Handle<Object> event_data,
2938 v8::Debug::ClientData* client_data) {
ager@chromium.orgea91cc52011-05-23 06:06:11 +00002939 Handle<Foreign> callback_obj(Handle<Foreign>::cast(event_listener_));
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00002940 v8::Debug::EventCallback2 callback =
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002941 FUNCTION_CAST<v8::Debug::EventCallback2>(
2942 callback_obj->foreign_address());
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00002943 EventDetailsImpl event_details(
2944 event,
2945 Handle<JSObject>::cast(exec_state),
2946 Handle<JSObject>::cast(event_data),
2947 event_listener_data_,
2948 client_data);
2949 callback(event_details);
2950}
2951
2952
2953void Debugger::CallJSEventCallback(v8::DebugEvent event,
2954 Handle<Object> exec_state,
2955 Handle<Object> event_data) {
2956 ASSERT(event_listener_->IsJSFunction());
2957 Handle<JSFunction> fun(Handle<JSFunction>::cast(event_listener_));
2958
2959 // Invoke the JavaScript debug event listener.
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00002960 Handle<Object> argv[] = { Handle<Object>(Smi::FromInt(event), isolate_),
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002961 exec_state,
2962 event_data,
2963 event_listener_data_ };
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002964 bool caught_exception;
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002965 Execution::TryCall(fun,
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00002966 isolate_->global_object(),
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002967 ARRAY_SIZE(argv),
2968 argv,
2969 &caught_exception);
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00002970 // Silently ignore exceptions from debug event listeners.
2971}
2972
2973
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00002974Handle<Context> Debugger::GetDebugContext() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002975 never_unload_debugger_ = true;
2976 EnterDebugger debugger;
2977 return isolate_->debug()->debug_context();
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00002978}
2979
2980
ager@chromium.org71daaf62009-04-01 07:22:49 +00002981void Debugger::UnloadDebugger() {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002982 Debug* debug = isolate_->debug();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002983
ager@chromium.org71daaf62009-04-01 07:22:49 +00002984 // Make sure that there are no breakpoints left.
lrn@chromium.org7516f052011-03-30 08:52:27 +00002985 debug->ClearAllBreakPoints();
ager@chromium.org71daaf62009-04-01 07:22:49 +00002986
2987 // Unload the debugger if feasible.
2988 if (!never_unload_debugger_) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002989 debug->Unload();
ager@chromium.org71daaf62009-04-01 07:22:49 +00002990 }
2991
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002992 // Clear the flag indicating that the debugger should be unloaded.
2993 debugger_unload_pending_ = false;
ager@chromium.org71daaf62009-04-01 07:22:49 +00002994}
2995
2996
ager@chromium.org41826e72009-03-30 13:30:57 +00002997void Debugger::NotifyMessageHandler(v8::DebugEvent event,
ager@chromium.org5ec48922009-05-05 07:25:34 +00002998 Handle<JSObject> exec_state,
2999 Handle<JSObject> event_data,
ager@chromium.org41826e72009-03-30 13:30:57 +00003000 bool auto_continue) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00003001 HandleScope scope(isolate_);
ager@chromium.org41826e72009-03-30 13:30:57 +00003002
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003003 if (!isolate_->debug()->Load()) return;
ager@chromium.org41826e72009-03-30 13:30:57 +00003004
3005 // Process the individual events.
ager@chromium.org5ec48922009-05-05 07:25:34 +00003006 bool sendEventMessage = false;
ager@chromium.org41826e72009-03-30 13:30:57 +00003007 switch (event) {
3008 case v8::Break:
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00003009 case v8::BreakForCommand:
ager@chromium.org5ec48922009-05-05 07:25:34 +00003010 sendEventMessage = !auto_continue;
ager@chromium.org41826e72009-03-30 13:30:57 +00003011 break;
3012 case v8::Exception:
ager@chromium.org5ec48922009-05-05 07:25:34 +00003013 sendEventMessage = true;
ager@chromium.org41826e72009-03-30 13:30:57 +00003014 break;
3015 case v8::BeforeCompile:
3016 break;
3017 case v8::AfterCompile:
ager@chromium.org5ec48922009-05-05 07:25:34 +00003018 sendEventMessage = true;
ager@chromium.org41826e72009-03-30 13:30:57 +00003019 break;
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003020 case v8::ScriptCollected:
3021 sendEventMessage = true;
3022 break;
ager@chromium.org41826e72009-03-30 13:30:57 +00003023 case v8::NewFunction:
3024 break;
3025 default:
3026 UNREACHABLE();
3027 }
3028
ager@chromium.org5ec48922009-05-05 07:25:34 +00003029 // The debug command interrupt flag might have been set when the command was
3030 // added. It should be enough to clear the flag only once while we are in the
3031 // debugger.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003032 ASSERT(isolate_->debug()->InDebugger());
3033 isolate_->stack_guard()->Continue(DEBUGCOMMAND);
ager@chromium.org5ec48922009-05-05 07:25:34 +00003034
3035 // Notify the debugger that a debug event has occurred unless auto continue is
3036 // active in which case no event is send.
3037 if (sendEventMessage) {
3038 MessageImpl message = MessageImpl::NewEvent(
3039 event,
3040 auto_continue,
3041 Handle<JSObject>::cast(exec_state),
3042 Handle<JSObject>::cast(event_data));
3043 InvokeMessageHandler(message);
3044 }
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +00003045
3046 // If auto continue don't make the event cause a break, but process messages
3047 // in the queue if any. For script collected events don't even process
3048 // messages in the queue as the execution state might not be what is expected
3049 // by the client.
ager@chromium.org6ffc2172009-05-29 19:20:16 +00003050 if ((auto_continue && !HasCommands()) || event == v8::ScriptCollected) {
ager@chromium.org5ec48922009-05-05 07:25:34 +00003051 return;
3052 }
ager@chromium.org41826e72009-03-30 13:30:57 +00003053
ager@chromium.org41826e72009-03-30 13:30:57 +00003054 v8::TryCatch try_catch;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003055
3056 // DebugCommandProcessor goes here.
3057 v8::Local<v8::Object> cmd_processor;
3058 {
3059 v8::Local<v8::Object> api_exec_state =
3060 v8::Utils::ToLocal(Handle<JSObject>::cast(exec_state));
3061 v8::Local<v8::String> fun_name =
3062 v8::String::New("debugCommandProcessor");
3063 v8::Local<v8::Function> fun =
3064 v8::Function::Cast(*api_exec_state->Get(fun_name));
3065
3066 v8::Handle<v8::Boolean> running =
3067 auto_continue ? v8::True() : v8::False();
3068 static const int kArgc = 1;
3069 v8::Handle<Value> argv[kArgc] = { running };
3070 cmd_processor = v8::Object::Cast(*fun->Call(api_exec_state, kArgc, argv));
3071 if (try_catch.HasCaught()) {
3072 PrintLn(try_catch.Exception());
3073 return;
3074 }
ager@chromium.org41826e72009-03-30 13:30:57 +00003075 }
3076
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003077 bool running = auto_continue;
3078
ager@chromium.org41826e72009-03-30 13:30:57 +00003079 // Process requests from the debugger.
3080 while (true) {
3081 // Wait for new command in the queue.
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003082 if (Debugger::host_dispatch_handler_) {
3083 // In case there is a host dispatch - do periodic dispatches.
3084 if (!command_received_->Wait(host_dispatch_micros_)) {
3085 // Timout expired, do the dispatch.
3086 Debugger::host_dispatch_handler_();
3087 continue;
3088 }
3089 } else {
3090 // In case there is no host dispatch - just wait.
3091 command_received_->Wait();
3092 }
ager@chromium.org41826e72009-03-30 13:30:57 +00003093
ager@chromium.org41826e72009-03-30 13:30:57 +00003094 // Get the command from the queue.
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003095 CommandMessage command = command_queue_.Get();
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00003096 isolate_->logger()->DebugTag(
3097 "Got request from command queue, in interactive loop.");
ager@chromium.org71daaf62009-04-01 07:22:49 +00003098 if (!Debugger::IsDebuggerActive()) {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003099 // Delete command text and user data.
3100 command.Dispose();
ager@chromium.org41826e72009-03-30 13:30:57 +00003101 return;
3102 }
3103
ager@chromium.org41826e72009-03-30 13:30:57 +00003104 // Invoke JavaScript to process the debug request.
3105 v8::Local<v8::String> fun_name;
3106 v8::Local<v8::Function> fun;
3107 v8::Local<v8::Value> request;
3108 v8::TryCatch try_catch;
3109 fun_name = v8::String::New("processDebugRequest");
3110 fun = v8::Function::Cast(*cmd_processor->Get(fun_name));
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003111
3112 request = v8::String::New(command.text().start(),
3113 command.text().length());
ager@chromium.org41826e72009-03-30 13:30:57 +00003114 static const int kArgc = 1;
3115 v8::Handle<Value> argv[kArgc] = { request };
3116 v8::Local<v8::Value> response_val = fun->Call(cmd_processor, kArgc, argv);
3117
3118 // Get the response.
3119 v8::Local<v8::String> response;
ager@chromium.org41826e72009-03-30 13:30:57 +00003120 if (!try_catch.HasCaught()) {
3121 // Get response string.
3122 if (!response_val->IsUndefined()) {
3123 response = v8::String::Cast(*response_val);
3124 } else {
3125 response = v8::String::New("");
3126 }
3127
3128 // Log the JSON request/response.
3129 if (FLAG_trace_debug_json) {
3130 PrintLn(request);
3131 PrintLn(response);
3132 }
3133
3134 // Get the running state.
3135 fun_name = v8::String::New("isRunning");
3136 fun = v8::Function::Cast(*cmd_processor->Get(fun_name));
3137 static const int kArgc = 1;
3138 v8::Handle<Value> argv[kArgc] = { response };
3139 v8::Local<v8::Value> running_val = fun->Call(cmd_processor, kArgc, argv);
3140 if (!try_catch.HasCaught()) {
3141 running = running_val->ToBoolean()->Value();
3142 }
3143 } else {
3144 // In case of failure the result text is the exception text.
3145 response = try_catch.Exception()->ToString();
3146 }
3147
ager@chromium.org41826e72009-03-30 13:30:57 +00003148 // Return the result.
ager@chromium.org5ec48922009-05-05 07:25:34 +00003149 MessageImpl message = MessageImpl::NewResponse(
3150 event,
3151 running,
3152 Handle<JSObject>::cast(exec_state),
3153 Handle<JSObject>::cast(event_data),
3154 Handle<String>(Utils::OpenHandle(*response)),
3155 command.client_data());
3156 InvokeMessageHandler(message);
3157 command.Dispose();
ager@chromium.org41826e72009-03-30 13:30:57 +00003158
3159 // Return from debug event processing if either the VM is put into the
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00003160 // running state (through a continue command) or auto continue is active
ager@chromium.org41826e72009-03-30 13:30:57 +00003161 // and there are no more commands queued.
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003162 if (running && !HasCommands()) {
ager@chromium.org41826e72009-03-30 13:30:57 +00003163 return;
3164 }
3165 }
3166}
3167
3168
iposva@chromium.org245aa852009-02-10 00:49:54 +00003169void Debugger::SetEventListener(Handle<Object> callback,
3170 Handle<Object> data) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00003171 HandleScope scope(isolate_);
3172 GlobalHandles* global_handles = isolate_->global_handles();
iposva@chromium.org245aa852009-02-10 00:49:54 +00003173
3174 // Clear the global handles for the event listener and the event listener data
3175 // object.
3176 if (!event_listener_.is_null()) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00003177 global_handles->Destroy(
iposva@chromium.org245aa852009-02-10 00:49:54 +00003178 reinterpret_cast<Object**>(event_listener_.location()));
3179 event_listener_ = Handle<Object>();
3180 }
3181 if (!event_listener_data_.is_null()) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00003182 global_handles->Destroy(
iposva@chromium.org245aa852009-02-10 00:49:54 +00003183 reinterpret_cast<Object**>(event_listener_data_.location()));
3184 event_listener_data_ = Handle<Object>();
3185 }
3186
3187 // If there is a new debug event listener register it together with its data
3188 // object.
3189 if (!callback->IsUndefined() && !callback->IsNull()) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003190 event_listener_ = Handle<Object>::cast(
lrn@chromium.org7516f052011-03-30 08:52:27 +00003191 global_handles->Create(*callback));
iposva@chromium.org245aa852009-02-10 00:49:54 +00003192 if (data.is_null()) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00003193 data = isolate_->factory()->undefined_value();
iposva@chromium.org245aa852009-02-10 00:49:54 +00003194 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003195 event_listener_data_ = Handle<Object>::cast(
lrn@chromium.org7516f052011-03-30 08:52:27 +00003196 global_handles->Create(*data));
iposva@chromium.org245aa852009-02-10 00:49:54 +00003197 }
3198
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003199 ListenersChanged();
iposva@chromium.org245aa852009-02-10 00:49:54 +00003200}
3201
3202
ager@chromium.org5ec48922009-05-05 07:25:34 +00003203void Debugger::SetMessageHandler(v8::Debug::MessageHandler2 handler) {
ager@chromium.org71daaf62009-04-01 07:22:49 +00003204 ScopedLock with(debugger_access_);
3205
ager@chromium.org381abbb2009-02-25 13:23:22 +00003206 message_handler_ = handler;
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003207 ListenersChanged();
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003208 if (handler == NULL) {
ager@chromium.org71daaf62009-04-01 07:22:49 +00003209 // Send an empty command to the debugger if in a break to make JavaScript
3210 // run again if the debugger is closed.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003211 if (isolate_->debug()->InDebugger()) {
ager@chromium.org71daaf62009-04-01 07:22:49 +00003212 ProcessCommand(Vector<const uint16_t>::empty());
3213 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003214 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003215}
3216
3217
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003218void Debugger::ListenersChanged() {
3219 if (IsDebuggerActive()) {
3220 // Disable the compilation cache when the debugger is active.
lrn@chromium.org7516f052011-03-30 08:52:27 +00003221 isolate_->compilation_cache()->Disable();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003222 debugger_unload_pending_ = false;
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003223 } else {
lrn@chromium.org7516f052011-03-30 08:52:27 +00003224 isolate_->compilation_cache()->Enable();
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003225 // Unload the debugger if event listener and message handler cleared.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003226 // Schedule this for later, because we may be in non-V8 thread.
3227 debugger_unload_pending_ = true;
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003228 }
3229}
3230
3231
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003232void Debugger::SetHostDispatchHandler(v8::Debug::HostDispatchHandler handler,
3233 int period) {
ager@chromium.org381abbb2009-02-25 13:23:22 +00003234 host_dispatch_handler_ = handler;
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003235 host_dispatch_micros_ = period * 1000;
ager@chromium.org381abbb2009-02-25 13:23:22 +00003236}
3237
3238
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003239void Debugger::SetDebugMessageDispatchHandler(
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003240 v8::Debug::DebugMessageDispatchHandler handler, bool provide_locker) {
3241 ScopedLock with(dispatch_handler_access_);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003242 debug_message_dispatch_handler_ = handler;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003243
3244 if (provide_locker && message_dispatch_helper_thread_ == NULL) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003245 message_dispatch_helper_thread_ = new MessageDispatchHelperThread(isolate_);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003246 message_dispatch_helper_thread_->Start();
3247 }
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003248}
3249
3250
ager@chromium.org41826e72009-03-30 13:30:57 +00003251// Calls the registered debug message handler. This callback is part of the
ager@chromium.org5ec48922009-05-05 07:25:34 +00003252// public API.
3253void Debugger::InvokeMessageHandler(MessageImpl message) {
ager@chromium.org71daaf62009-04-01 07:22:49 +00003254 ScopedLock with(debugger_access_);
3255
ager@chromium.org381abbb2009-02-25 13:23:22 +00003256 if (message_handler_ != NULL) {
ager@chromium.org5ec48922009-05-05 07:25:34 +00003257 message_handler_(message);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003258 }
ager@chromium.org41826e72009-03-30 13:30:57 +00003259}
3260
3261
3262// Puts a command coming from the public API on the queue. Creates
3263// a copy of the command string managed by the debugger. Up to this
3264// point, the command data was managed by the API client. Called
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003265// by the API client thread.
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003266void Debugger::ProcessCommand(Vector<const uint16_t> command,
3267 v8::Debug::ClientData* client_data) {
3268 // Need to cast away const.
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003269 CommandMessage message = CommandMessage::New(
ager@chromium.org41826e72009-03-30 13:30:57 +00003270 Vector<uint16_t>(const_cast<uint16_t*>(command.start()),
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003271 command.length()),
3272 client_data);
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00003273 isolate_->logger()->DebugTag("Put command on command_queue.");
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003274 command_queue_.Put(message);
ager@chromium.org41826e72009-03-30 13:30:57 +00003275 command_received_->Signal();
sgjesse@chromium.org3afc1582009-04-16 22:31:44 +00003276
3277 // Set the debug command break flag to have the command processed.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003278 if (!isolate_->debug()->InDebugger()) {
3279 isolate_->stack_guard()->DebugCommand();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003280 }
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003281
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003282 MessageDispatchHelperThread* dispatch_thread;
3283 {
3284 ScopedLock with(dispatch_handler_access_);
3285 dispatch_thread = message_dispatch_helper_thread_;
3286 }
3287
3288 if (dispatch_thread == NULL) {
3289 CallMessageDispatchHandler();
3290 } else {
3291 dispatch_thread->Schedule();
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003292 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003293}
3294
3295
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003296bool Debugger::HasCommands() {
ager@chromium.org41826e72009-03-30 13:30:57 +00003297 return !command_queue_.IsEmpty();
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003298}
3299
3300
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00003301void Debugger::EnqueueDebugCommand(v8::Debug::ClientData* client_data) {
3302 CommandMessage message = CommandMessage::New(Vector<uint16_t>(), client_data);
3303 event_command_queue_.Put(message);
3304
3305 // Set the debug command break flag to have the command processed.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003306 if (!isolate_->debug()->InDebugger()) {
3307 isolate_->stack_guard()->DebugCommand();
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00003308 }
3309}
3310
3311
ager@chromium.org71daaf62009-04-01 07:22:49 +00003312bool Debugger::IsDebuggerActive() {
3313 ScopedLock with(debugger_access_);
3314
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00003315 return message_handler_ != NULL ||
3316 !event_listener_.is_null() ||
3317 force_debugger_active_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003318}
3319
3320
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003321Handle<Object> Debugger::Call(Handle<JSFunction> fun,
3322 Handle<Object> data,
3323 bool* pending_exception) {
ager@chromium.org71daaf62009-04-01 07:22:49 +00003324 // When calling functions in the debugger prevent it from beeing unloaded.
3325 Debugger::never_unload_debugger_ = true;
3326
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003327 // Enter the debugger.
3328 EnterDebugger debugger;
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +00003329 if (debugger.FailedToEnter()) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00003330 return isolate_->factory()->undefined_value();
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003331 }
3332
3333 // Create the execution state.
3334 bool caught_exception = false;
3335 Handle<Object> exec_state = MakeExecutionState(&caught_exception);
3336 if (caught_exception) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00003337 return isolate_->factory()->undefined_value();
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003338 }
3339
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00003340 Handle<Object> argv[] = { exec_state, data };
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +00003341 Handle<Object> result = Execution::Call(
3342 fun,
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00003343 Handle<Object>(isolate_->debug()->debug_context_->global_proxy(),
3344 isolate_),
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00003345 ARRAY_SIZE(argv),
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +00003346 argv,
3347 pending_exception);
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003348 return result;
3349}
3350
3351
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003352static void StubMessageHandler2(const v8::Debug::Message& message) {
3353 // Simply ignore message.
3354}
3355
3356
3357bool Debugger::StartAgent(const char* name, int port,
3358 bool wait_for_connection) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003359 ASSERT(Isolate::Current() == isolate_);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003360 if (wait_for_connection) {
3361 // Suspend V8 if it is already running or set V8 to suspend whenever
3362 // it starts.
3363 // Provide stub message handler; V8 auto-continues each suspend
3364 // when there is no message handler; we doesn't need it.
3365 // Once become suspended, V8 will stay so indefinitely long, until remote
3366 // debugger connects and issues "continue" command.
3367 Debugger::message_handler_ = StubMessageHandler2;
3368 v8::Debug::DebugBreak();
3369 }
3370
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00003371 if (Socket::SetUp()) {
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00003372 if (agent_ == NULL) {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00003373 agent_ = new DebuggerAgent(name, port);
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00003374 agent_->Start();
3375 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003376 return true;
3377 }
3378
3379 return false;
3380}
3381
3382
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003383void Debugger::StopAgent() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003384 ASSERT(Isolate::Current() == isolate_);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003385 if (agent_ != NULL) {
3386 agent_->Shutdown();
3387 agent_->Join();
3388 delete agent_;
3389 agent_ = NULL;
3390 }
3391}
3392
3393
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003394void Debugger::WaitForAgent() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003395 ASSERT(Isolate::Current() == isolate_);
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003396 if (agent_ != NULL)
3397 agent_->WaitUntilListening();
3398}
3399
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003400
3401void Debugger::CallMessageDispatchHandler() {
3402 v8::Debug::DebugMessageDispatchHandler handler;
3403 {
3404 ScopedLock with(dispatch_handler_access_);
3405 handler = Debugger::debug_message_dispatch_handler_;
3406 }
3407 if (handler != NULL) {
3408 handler();
3409 }
3410}
3411
3412
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003413EnterDebugger::EnterDebugger()
3414 : isolate_(Isolate::Current()),
3415 prev_(isolate_->debug()->debugger_entry()),
3416 it_(isolate_),
3417 has_js_frames_(!it_.done()),
3418 save_(isolate_) {
3419 Debug* debug = isolate_->debug();
3420 ASSERT(prev_ != NULL || !debug->is_interrupt_pending(PREEMPT));
3421 ASSERT(prev_ != NULL || !debug->is_interrupt_pending(DEBUGBREAK));
3422
3423 // Link recursive debugger entry.
3424 debug->set_debugger_entry(this);
3425
3426 // Store the previous break id and frame id.
3427 break_id_ = debug->break_id();
3428 break_frame_id_ = debug->break_frame_id();
3429
3430 // Create the new break info. If there is no JavaScript frames there is no
3431 // break frame id.
3432 if (has_js_frames_) {
3433 debug->NewBreak(it_.frame()->id());
3434 } else {
3435 debug->NewBreak(StackFrame::NO_ID);
3436 }
3437
3438 // Make sure that debugger is loaded and enter the debugger context.
3439 load_failed_ = !debug->Load();
3440 if (!load_failed_) {
3441 // NOTE the member variable save which saves the previous context before
3442 // this change.
3443 isolate_->set_context(*debug->debug_context());
3444 }
3445}
3446
3447
3448EnterDebugger::~EnterDebugger() {
3449 ASSERT(Isolate::Current() == isolate_);
3450 Debug* debug = isolate_->debug();
3451
3452 // Restore to the previous break state.
3453 debug->SetBreak(break_frame_id_, break_id_);
3454
3455 // Check for leaving the debugger.
fschneider@chromium.org7d10be52012-04-10 12:30:14 +00003456 if (!load_failed_ && prev_ == NULL) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003457 // Clear mirror cache when leaving the debugger. Skip this if there is a
3458 // pending exception as clearing the mirror cache calls back into
3459 // JavaScript. This can happen if the v8::Debug::Call is used in which
3460 // case the exception should end up in the calling code.
3461 if (!isolate_->has_pending_exception()) {
3462 // Try to avoid any pending debug break breaking in the clear mirror
3463 // cache JavaScript code.
3464 if (isolate_->stack_guard()->IsDebugBreak()) {
3465 debug->set_interrupts_pending(DEBUGBREAK);
3466 isolate_->stack_guard()->Continue(DEBUGBREAK);
3467 }
3468 debug->ClearMirrorCache();
3469 }
3470
3471 // Request preemption and debug break when leaving the last debugger entry
3472 // if any of these where recorded while debugging.
3473 if (debug->is_interrupt_pending(PREEMPT)) {
3474 // This re-scheduling of preemption is to avoid starvation in some
3475 // debugging scenarios.
3476 debug->clear_interrupt_pending(PREEMPT);
3477 isolate_->stack_guard()->Preempt();
3478 }
3479 if (debug->is_interrupt_pending(DEBUGBREAK)) {
3480 debug->clear_interrupt_pending(DEBUGBREAK);
3481 isolate_->stack_guard()->DebugBreak();
3482 }
3483
3484 // If there are commands in the queue when leaving the debugger request
3485 // that these commands are processed.
3486 if (isolate_->debugger()->HasCommands()) {
3487 isolate_->stack_guard()->DebugCommand();
3488 }
3489
3490 // If leaving the debugger with the debugger no longer active unload it.
3491 if (!isolate_->debugger()->IsDebuggerActive()) {
3492 isolate_->debugger()->UnloadDebugger();
3493 }
3494 }
3495
3496 // Leaving this debugger entry.
3497 debug->set_debugger_entry(prev_);
3498}
3499
3500
ager@chromium.org5ec48922009-05-05 07:25:34 +00003501MessageImpl MessageImpl::NewEvent(DebugEvent event,
3502 bool running,
3503 Handle<JSObject> exec_state,
3504 Handle<JSObject> event_data) {
3505 MessageImpl message(true, event, running,
3506 exec_state, event_data, Handle<String>(), NULL);
3507 return message;
3508}
3509
3510
3511MessageImpl MessageImpl::NewResponse(DebugEvent event,
3512 bool running,
3513 Handle<JSObject> exec_state,
3514 Handle<JSObject> event_data,
3515 Handle<String> response_json,
3516 v8::Debug::ClientData* client_data) {
3517 MessageImpl message(false, event, running,
3518 exec_state, event_data, response_json, client_data);
3519 return message;
3520}
3521
3522
3523MessageImpl::MessageImpl(bool is_event,
3524 DebugEvent event,
3525 bool running,
3526 Handle<JSObject> exec_state,
3527 Handle<JSObject> event_data,
3528 Handle<String> response_json,
3529 v8::Debug::ClientData* client_data)
3530 : is_event_(is_event),
3531 event_(event),
3532 running_(running),
3533 exec_state_(exec_state),
3534 event_data_(event_data),
3535 response_json_(response_json),
3536 client_data_(client_data) {}
3537
3538
3539bool MessageImpl::IsEvent() const {
3540 return is_event_;
3541}
3542
3543
3544bool MessageImpl::IsResponse() const {
3545 return !is_event_;
3546}
3547
3548
3549DebugEvent MessageImpl::GetEvent() const {
3550 return event_;
3551}
3552
3553
3554bool MessageImpl::WillStartRunning() const {
3555 return running_;
3556}
3557
3558
3559v8::Handle<v8::Object> MessageImpl::GetExecutionState() const {
3560 return v8::Utils::ToLocal(exec_state_);
3561}
3562
3563
3564v8::Handle<v8::Object> MessageImpl::GetEventData() const {
3565 return v8::Utils::ToLocal(event_data_);
3566}
3567
3568
3569v8::Handle<v8::String> MessageImpl::GetJSON() const {
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +00003570 v8::HandleScope scope(
3571 reinterpret_cast<v8::Isolate*>(event_data_->GetIsolate()));
ager@chromium.org5ec48922009-05-05 07:25:34 +00003572
3573 if (IsEvent()) {
3574 // Call toJSONProtocol on the debug event object.
3575 Handle<Object> fun = GetProperty(event_data_, "toJSONProtocol");
3576 if (!fun->IsJSFunction()) {
3577 return v8::Handle<v8::String>();
3578 }
3579 bool caught_exception;
3580 Handle<Object> json = Execution::TryCall(Handle<JSFunction>::cast(fun),
3581 event_data_,
3582 0, NULL, &caught_exception);
3583 if (caught_exception || !json->IsString()) {
3584 return v8::Handle<v8::String>();
3585 }
3586 return scope.Close(v8::Utils::ToLocal(Handle<String>::cast(json)));
3587 } else {
3588 return v8::Utils::ToLocal(response_json_);
3589 }
3590}
3591
3592
3593v8::Handle<v8::Context> MessageImpl::GetEventContext() const {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003594 Isolate* isolate = Isolate::Current();
3595 v8::Handle<v8::Context> context = GetDebugEventContext(isolate);
3596 // Isolate::context() may be NULL when "script collected" event occures.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003597 ASSERT(!context.IsEmpty() || event_ == v8::ScriptCollected);
rossberg@chromium.org717967f2011-07-20 13:44:42 +00003598 return context;
ager@chromium.org5ec48922009-05-05 07:25:34 +00003599}
3600
3601
3602v8::Debug::ClientData* MessageImpl::GetClientData() const {
3603 return client_data_;
3604}
3605
3606
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003607EventDetailsImpl::EventDetailsImpl(DebugEvent event,
3608 Handle<JSObject> exec_state,
3609 Handle<JSObject> event_data,
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00003610 Handle<Object> callback_data,
3611 v8::Debug::ClientData* client_data)
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003612 : event_(event),
3613 exec_state_(exec_state),
3614 event_data_(event_data),
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00003615 callback_data_(callback_data),
3616 client_data_(client_data) {}
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003617
3618
3619DebugEvent EventDetailsImpl::GetEvent() const {
3620 return event_;
3621}
3622
3623
3624v8::Handle<v8::Object> EventDetailsImpl::GetExecutionState() const {
3625 return v8::Utils::ToLocal(exec_state_);
3626}
3627
3628
3629v8::Handle<v8::Object> EventDetailsImpl::GetEventData() const {
3630 return v8::Utils::ToLocal(event_data_);
3631}
3632
3633
3634v8::Handle<v8::Context> EventDetailsImpl::GetEventContext() const {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003635 return GetDebugEventContext(Isolate::Current());
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003636}
3637
3638
3639v8::Handle<v8::Value> EventDetailsImpl::GetCallbackData() const {
3640 return v8::Utils::ToLocal(callback_data_);
3641}
3642
3643
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00003644v8::Debug::ClientData* EventDetailsImpl::GetClientData() const {
3645 return client_data_;
3646}
3647
3648
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003649CommandMessage::CommandMessage() : text_(Vector<uint16_t>::empty()),
3650 client_data_(NULL) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003651}
3652
3653
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003654CommandMessage::CommandMessage(const Vector<uint16_t>& text,
3655 v8::Debug::ClientData* data)
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003656 : text_(text),
3657 client_data_(data) {
3658}
3659
3660
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003661CommandMessage::~CommandMessage() {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003662}
3663
3664
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003665void CommandMessage::Dispose() {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003666 text_.Dispose();
3667 delete client_data_;
3668 client_data_ = NULL;
3669}
3670
3671
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003672CommandMessage CommandMessage::New(const Vector<uint16_t>& command,
3673 v8::Debug::ClientData* data) {
3674 return CommandMessage(command.Clone(), data);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003675}
3676
3677
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003678CommandMessageQueue::CommandMessageQueue(int size) : start_(0), end_(0),
3679 size_(size) {
3680 messages_ = NewArray<CommandMessage>(size);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003681}
3682
3683
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003684CommandMessageQueue::~CommandMessageQueue() {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003685 while (!IsEmpty()) {
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003686 CommandMessage m = Get();
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003687 m.Dispose();
3688 }
kasper.lund7276f142008-07-30 08:49:36 +00003689 DeleteArray(messages_);
3690}
3691
3692
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003693CommandMessage CommandMessageQueue::Get() {
kasper.lund7276f142008-07-30 08:49:36 +00003694 ASSERT(!IsEmpty());
3695 int result = start_;
3696 start_ = (start_ + 1) % size_;
3697 return messages_[result];
3698}
3699
3700
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003701void CommandMessageQueue::Put(const CommandMessage& message) {
kasper.lund7276f142008-07-30 08:49:36 +00003702 if ((end_ + 1) % size_ == start_) {
3703 Expand();
3704 }
3705 messages_[end_] = message;
3706 end_ = (end_ + 1) % size_;
3707}
3708
3709
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003710void CommandMessageQueue::Expand() {
3711 CommandMessageQueue new_queue(size_ * 2);
kasper.lund7276f142008-07-30 08:49:36 +00003712 while (!IsEmpty()) {
3713 new_queue.Put(Get());
3714 }
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003715 CommandMessage* array_to_free = messages_;
kasper.lund7276f142008-07-30 08:49:36 +00003716 *this = new_queue;
3717 new_queue.messages_ = array_to_free;
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003718 // Make the new_queue empty so that it doesn't call Dispose on any messages.
3719 new_queue.start_ = new_queue.end_;
kasper.lund7276f142008-07-30 08:49:36 +00003720 // Automatic destructor called on new_queue, freeing array_to_free.
3721}
3722
3723
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00003724LockingCommandMessageQueue::LockingCommandMessageQueue(Logger* logger, int size)
3725 : logger_(logger), queue_(size) {
kasper.lund7276f142008-07-30 08:49:36 +00003726 lock_ = OS::CreateMutex();
3727}
3728
3729
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003730LockingCommandMessageQueue::~LockingCommandMessageQueue() {
kasper.lund7276f142008-07-30 08:49:36 +00003731 delete lock_;
3732}
3733
3734
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003735bool LockingCommandMessageQueue::IsEmpty() const {
kasper.lund7276f142008-07-30 08:49:36 +00003736 ScopedLock sl(lock_);
3737 return queue_.IsEmpty();
3738}
3739
3740
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003741CommandMessage LockingCommandMessageQueue::Get() {
kasper.lund7276f142008-07-30 08:49:36 +00003742 ScopedLock sl(lock_);
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003743 CommandMessage result = queue_.Get();
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00003744 logger_->DebugEvent("Get", result.text());
kasper.lund7276f142008-07-30 08:49:36 +00003745 return result;
3746}
3747
3748
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003749void LockingCommandMessageQueue::Put(const CommandMessage& message) {
kasper.lund7276f142008-07-30 08:49:36 +00003750 ScopedLock sl(lock_);
3751 queue_.Put(message);
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00003752 logger_->DebugEvent("Put", message.text());
kasper.lund7276f142008-07-30 08:49:36 +00003753}
3754
3755
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003756void LockingCommandMessageQueue::Clear() {
kasper.lund7276f142008-07-30 08:49:36 +00003757 ScopedLock sl(lock_);
3758 queue_.Clear();
3759}
3760
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003761
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003762MessageDispatchHelperThread::MessageDispatchHelperThread(Isolate* isolate)
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00003763 : Thread("v8:MsgDispHelpr"),
lrn@chromium.org5d00b602011-01-05 09:51:43 +00003764 sem_(OS::CreateSemaphore(0)), mutex_(OS::CreateMutex()),
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003765 already_signalled_(false) {
3766}
3767
3768
3769MessageDispatchHelperThread::~MessageDispatchHelperThread() {
3770 delete mutex_;
3771 delete sem_;
3772}
3773
3774
3775void MessageDispatchHelperThread::Schedule() {
3776 {
3777 ScopedLock lock(mutex_);
3778 if (already_signalled_) {
3779 return;
3780 }
3781 already_signalled_ = true;
3782 }
3783 sem_->Signal();
3784}
3785
3786
3787void MessageDispatchHelperThread::Run() {
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00003788 Isolate* isolate = Isolate::Current();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003789 while (true) {
3790 sem_->Wait();
3791 {
3792 ScopedLock lock(mutex_);
3793 already_signalled_ = false;
3794 }
3795 {
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00003796 Locker locker(reinterpret_cast<v8::Isolate*>(isolate));
3797 isolate->debugger()->CallMessageDispatchHandler();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003798 }
3799 }
3800}
3801
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003802#endif // ENABLE_DEBUGGER_SUPPORT
kasper.lund7276f142008-07-30 08:49:36 +00003803
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003804} } // namespace v8::internal