blob: 4af2194ea5f7455a6c0e7b2166b53755cc294130 [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;
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000554 OS::MemCopy(to, reinterpret_cast<char*>(&thread_local_), sizeof(ThreadLocal));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000555 to += sizeof(ThreadLocal);
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000556 OS::MemCopy(to, reinterpret_cast<char*>(&registers_), sizeof(registers_));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000557 ThreadInit();
558 ASSERT(to <= storage + ArchiveSpacePerThread());
559 return storage + ArchiveSpacePerThread();
560}
561
562
563char* Debug::RestoreDebug(char* storage) {
564 char* from = storage;
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000565 OS::MemCopy(
566 reinterpret_cast<char*>(&thread_local_), from, sizeof(ThreadLocal));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000567 from += sizeof(ThreadLocal);
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000568 OS::MemCopy(reinterpret_cast<char*>(&registers_), from, sizeof(registers_));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000569 ASSERT(from <= storage + ArchiveSpacePerThread());
570 return storage + ArchiveSpacePerThread();
571}
572
573
574int Debug::ArchiveSpacePerThread() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000575 return sizeof(ThreadLocal) + sizeof(JSCallerSavedBuffer);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000576}
577
578
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000579// Frame structure (conforms InternalFrame structure):
580// -- code
581// -- SMI maker
582// -- function (slot is called "context")
583// -- frame base
584Object** Debug::SetUpFrameDropperFrame(StackFrame* bottom_js_frame,
585 Handle<Code> code) {
586 ASSERT(bottom_js_frame->is_java_script());
587
588 Address fp = bottom_js_frame->fp();
589
590 // Move function pointer into "context" slot.
591 Memory::Object_at(fp + StandardFrameConstants::kContextOffset) =
592 Memory::Object_at(fp + JavaScriptFrameConstants::kFunctionOffset);
593
594 Memory::Object_at(fp + InternalFrameConstants::kCodeOffset) = *code;
595 Memory::Object_at(fp + StandardFrameConstants::kMarkerOffset) =
596 Smi::FromInt(StackFrame::INTERNAL);
597
598 return reinterpret_cast<Object**>(&Memory::Object_at(
599 fp + StandardFrameConstants::kContextOffset));
600}
601
602const int Debug::kFrameDropperFrameSize = 4;
603
604
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000605void ScriptCache::Add(Handle<Script> script) {
lrn@chromium.org7516f052011-03-30 08:52:27 +0000606 GlobalHandles* global_handles = Isolate::Current()->global_handles();
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000607 // Create an entry in the hash map for the script.
608 int id = Smi::cast(script->id())->value();
609 HashMap::Entry* entry =
610 HashMap::Lookup(reinterpret_cast<void*>(id), Hash(id), true);
611 if (entry->value != NULL) {
612 ASSERT(*script == *reinterpret_cast<Script**>(entry->value));
613 return;
614 }
615
616 // Globalize the script object, make it weak and use the location of the
617 // global handle as the value in the hash map.
618 Handle<Script> script_ =
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000619 Handle<Script>::cast(
lrn@chromium.org7516f052011-03-30 08:52:27 +0000620 (global_handles->Create(*script)));
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000621 global_handles->MakeWeak(reinterpret_cast<Object**>(script_.location()),
622 this,
623 NULL,
624 ScriptCache::HandleWeakScript);
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000625 entry->value = script_.location();
626}
627
628
629Handle<FixedArray> ScriptCache::GetScripts() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000630 Handle<FixedArray> instances = FACTORY->NewFixedArray(occupancy());
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000631 int count = 0;
632 for (HashMap::Entry* entry = Start(); entry != NULL; entry = Next(entry)) {
633 ASSERT(entry->value != NULL);
634 if (entry->value != NULL) {
635 instances->set(count, *reinterpret_cast<Script**>(entry->value));
636 count++;
637 }
638 }
639 return instances;
640}
641
642
643void ScriptCache::ProcessCollectedScripts() {
lrn@chromium.org7516f052011-03-30 08:52:27 +0000644 Debugger* debugger = Isolate::Current()->debugger();
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000645 for (int i = 0; i < collected_scripts_.length(); i++) {
lrn@chromium.org7516f052011-03-30 08:52:27 +0000646 debugger->OnScriptCollected(collected_scripts_[i]);
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000647 }
648 collected_scripts_.Clear();
649}
650
651
652void ScriptCache::Clear() {
lrn@chromium.org7516f052011-03-30 08:52:27 +0000653 GlobalHandles* global_handles = Isolate::Current()->global_handles();
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000654 // Iterate the script cache to get rid of all the weak handles.
655 for (HashMap::Entry* entry = Start(); entry != NULL; entry = Next(entry)) {
656 ASSERT(entry != NULL);
657 Object** location = reinterpret_cast<Object**>(entry->value);
658 ASSERT((*location)->IsScript());
lrn@chromium.org7516f052011-03-30 08:52:27 +0000659 global_handles->ClearWeakness(location);
660 global_handles->Destroy(location);
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000661 }
662 // Clear the content of the hash map.
663 HashMap::Clear();
664}
665
666
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000667void ScriptCache::HandleWeakScript(v8::Isolate* isolate,
668 v8::Persistent<v8::Value> obj,
669 void* data) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000670 ScriptCache* script_cache = reinterpret_cast<ScriptCache*>(data);
671 // Find the location of the global handle.
672 Script** location =
673 reinterpret_cast<Script**>(Utils::OpenHandle(*obj).location());
674 ASSERT((*location)->IsScript());
675
676 // Remove the entry from the cache.
677 int id = Smi::cast((*location)->id())->value();
678 script_cache->Remove(reinterpret_cast<void*>(id), Hash(id));
679 script_cache->collected_scripts_.Add(id);
680
681 // Clear the weak handle.
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000682 obj.Dispose(isolate);
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000683 obj.Clear();
684}
685
686
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000687void Debug::SetUp(bool create_heap_objects) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000688 ThreadInit();
689 if (create_heap_objects) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000690 // Get code to handle debug break on return.
691 debug_break_return_ =
lrn@chromium.org7516f052011-03-30 08:52:27 +0000692 isolate_->builtins()->builtin(Builtins::kReturn_DebugBreak);
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000693 ASSERT(debug_break_return_->IsCode());
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000694 // Get code to handle debug break in debug break slots.
695 debug_break_slot_ =
lrn@chromium.org7516f052011-03-30 08:52:27 +0000696 isolate_->builtins()->builtin(Builtins::kSlot_DebugBreak);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000697 ASSERT(debug_break_slot_->IsCode());
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000698 }
699}
700
701
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000702void Debug::HandleWeakDebugInfo(v8::Isolate* isolate,
703 v8::Persistent<v8::Value> obj,
704 void* data) {
705 Debug* debug = reinterpret_cast<Isolate*>(isolate)->debug();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000706 DebugInfoListNode* node = reinterpret_cast<DebugInfoListNode*>(data);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000707 // We need to clear all breakpoints associated with the function to restore
708 // original code and avoid patching the code twice later because
709 // the function will live in the heap until next gc, and can be found by
jkummerow@chromium.org78502a92012-09-06 13:50:42 +0000710 // Debug::FindSharedFunctionInfoInScript.
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000711 BreakLocationIterator it(node->debug_info(), ALL_BREAK_LOCATIONS);
712 it.ClearAllDebugBreak();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000713 debug->RemoveDebugInfo(node->debug_info());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000714#ifdef DEBUG
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000715 node = debug->debug_info_list_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000716 while (node != NULL) {
717 ASSERT(node != reinterpret_cast<DebugInfoListNode*>(data));
718 node = node->next();
719 }
720#endif
721}
722
723
724DebugInfoListNode::DebugInfoListNode(DebugInfo* debug_info): next_(NULL) {
lrn@chromium.org7516f052011-03-30 08:52:27 +0000725 GlobalHandles* global_handles = Isolate::Current()->global_handles();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000726 // Globalize the request debug info object and make it weak.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000727 debug_info_ = Handle<DebugInfo>::cast(
lrn@chromium.org7516f052011-03-30 08:52:27 +0000728 (global_handles->Create(debug_info)));
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000729 global_handles->MakeWeak(reinterpret_cast<Object**>(debug_info_.location()),
730 this,
731 NULL,
732 Debug::HandleWeakDebugInfo);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000733}
734
735
736DebugInfoListNode::~DebugInfoListNode() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000737 Isolate::Current()->global_handles()->Destroy(
738 reinterpret_cast<Object**>(debug_info_.location()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000739}
740
741
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000742bool Debug::CompileDebuggerScript(int index) {
lrn@chromium.org7516f052011-03-30 08:52:27 +0000743 Isolate* isolate = Isolate::Current();
744 Factory* factory = isolate->factory();
745 HandleScope scope(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000746
kasper.lund44510672008-07-25 07:37:58 +0000747 // Bail out if the index is invalid.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000748 if (index == -1) {
749 return false;
750 }
kasper.lund44510672008-07-25 07:37:58 +0000751
752 // Find source and name for the requested script.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000753 Handle<String> source_code =
lrn@chromium.org7516f052011-03-30 08:52:27 +0000754 isolate->bootstrapper()->NativesSourceLookup(index);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000755 Vector<const char> name = Natives::GetScriptName(index);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000756 Handle<String> script_name = factory->NewStringFromAscii(name);
yangguo@chromium.org355cfd12012-08-29 15:32:24 +0000757 Handle<Context> context = isolate->native_context();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000758
759 // Compile the script.
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000760 Handle<SharedFunctionInfo> function_info;
761 function_info = Compiler::Compile(source_code,
762 script_name,
yangguo@chromium.org355cfd12012-08-29 15:32:24 +0000763 0, 0,
764 context,
765 NULL, NULL,
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000766 Handle<String>::null(),
767 NATIVES_CODE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000768
769 // Silently ignore stack overflows during compilation.
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000770 if (function_info.is_null()) {
lrn@chromium.org7516f052011-03-30 08:52:27 +0000771 ASSERT(isolate->has_pending_exception());
772 isolate->clear_pending_exception();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000773 return false;
774 }
775
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000776 // Execute the shared function in the debugger context.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000777 bool caught_exception;
kasper.lund44510672008-07-25 07:37:58 +0000778 Handle<JSFunction> function =
lrn@chromium.org7516f052011-03-30 08:52:27 +0000779 factory->NewFunctionFromSharedFunctionInfo(function_info, context);
rossberg@chromium.org717967f2011-07-20 13:44:42 +0000780
fschneider@chromium.org7d10be52012-04-10 12:30:14 +0000781 Handle<Object> exception =
ulan@chromium.org09d7ab52013-02-25 15:50:35 +0000782 Execution::TryCall(function,
783 Handle<Object>(context->global_object(), isolate),
784 0,
785 NULL,
786 &caught_exception);
kasper.lund44510672008-07-25 07:37:58 +0000787
788 // Check for caught exceptions.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000789 if (caught_exception) {
fschneider@chromium.org7d10be52012-04-10 12:30:14 +0000790 ASSERT(!isolate->has_pending_exception());
791 MessageLocation computed_location;
792 isolate->ComputeLocation(&computed_location);
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000793 Handle<Object> message = MessageHandler::MakeMessageObject(
fschneider@chromium.org7d10be52012-04-10 12:30:14 +0000794 "error_loading_debugger", &computed_location,
795 Vector<Handle<Object> >::empty(), Handle<String>(), Handle<JSArray>());
796 ASSERT(!isolate->has_pending_exception());
mmassi@chromium.org49a44672012-12-04 13:52:03 +0000797 if (!exception.is_null()) {
798 isolate->set_pending_exception(*exception);
799 MessageHandler::ReportMessage(Isolate::Current(), NULL, message);
800 isolate->clear_pending_exception();
801 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000802 return false;
803 }
804
kasper.lund44510672008-07-25 07:37:58 +0000805 // Mark this script as native and return successfully.
806 Handle<Script> script(Script::cast(function->shared()->script()));
ager@chromium.orge2902be2009-06-08 12:21:35 +0000807 script->set_type(Smi::FromInt(Script::TYPE_NATIVE));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000808 return true;
809}
810
811
812bool Debug::Load() {
813 // Return if debugger is already loaded.
kasper.lund212ac232008-07-16 07:07:30 +0000814 if (IsLoaded()) return true;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000815
lrn@chromium.org7516f052011-03-30 08:52:27 +0000816 Debugger* debugger = isolate_->debugger();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000817
kasper.lund44510672008-07-25 07:37:58 +0000818 // Bail out if we're already in the process of compiling the native
819 // JavaScript source code for the debugger.
lrn@chromium.org7516f052011-03-30 08:52:27 +0000820 if (debugger->compiling_natives() ||
821 debugger->is_loading_debugger())
mads.s.agercbaa0602008-08-14 13:41:48 +0000822 return false;
lrn@chromium.org7516f052011-03-30 08:52:27 +0000823 debugger->set_loading_debugger(true);
kasper.lund44510672008-07-25 07:37:58 +0000824
825 // Disable breakpoints and interrupts while compiling and running the
826 // debugger scripts including the context creation code.
827 DisableBreak disable(true);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000828 PostponeInterruptsScope postpone(isolate_);
kasper.lund44510672008-07-25 07:37:58 +0000829
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000830 // Create the debugger context.
lrn@chromium.org7516f052011-03-30 08:52:27 +0000831 HandleScope scope(isolate_);
kasper.lund44510672008-07-25 07:37:58 +0000832 Handle<Context> context =
lrn@chromium.org7516f052011-03-30 08:52:27 +0000833 isolate_->bootstrapper()->CreateEnvironment(
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000834 Handle<Object>::null(),
835 v8::Handle<ObjectTemplate>(),
836 NULL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000837
fschneider@chromium.org7d10be52012-04-10 12:30:14 +0000838 // Fail if no context could be created.
839 if (context.is_null()) return false;
840
kasper.lund44510672008-07-25 07:37:58 +0000841 // Use the debugger context.
lrn@chromium.org7516f052011-03-30 08:52:27 +0000842 SaveContext save(isolate_);
843 isolate_->set_context(*context);
kasper.lund44510672008-07-25 07:37:58 +0000844
845 // Expose the builtins object in the debugger context.
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000846 Handle<String> key = isolate_->factory()->InternalizeOneByteString(
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +0000847 STATIC_ASCII_VECTOR("builtins"));
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000848 Handle<GlobalObject> global = Handle<GlobalObject>(context->global_object());
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +0000849 RETURN_IF_EMPTY_HANDLE_VALUE(
lrn@chromium.org7516f052011-03-30 08:52:27 +0000850 isolate_,
ulan@chromium.org09d7ab52013-02-25 15:50:35 +0000851 JSReceiver::SetProperty(global,
852 key,
853 Handle<Object>(global->builtins(), isolate_),
854 NONE,
855 kNonStrictMode),
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +0000856 false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000857
858 // Compile the JavaScript for the debugger in the debugger context.
lrn@chromium.org7516f052011-03-30 08:52:27 +0000859 debugger->set_compiling_natives(true);
kasper.lund44510672008-07-25 07:37:58 +0000860 bool caught_exception =
861 !CompileDebuggerScript(Natives::GetIndex("mirror")) ||
862 !CompileDebuggerScript(Natives::GetIndex("debug"));
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000863
864 if (FLAG_enable_liveedit) {
865 caught_exception = caught_exception ||
866 !CompileDebuggerScript(Natives::GetIndex("liveedit"));
867 }
868
lrn@chromium.org7516f052011-03-30 08:52:27 +0000869 debugger->set_compiling_natives(false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000870
mads.s.agercbaa0602008-08-14 13:41:48 +0000871 // Make sure we mark the debugger as not loading before we might
872 // return.
lrn@chromium.org7516f052011-03-30 08:52:27 +0000873 debugger->set_loading_debugger(false);
mads.s.agercbaa0602008-08-14 13:41:48 +0000874
kasper.lund44510672008-07-25 07:37:58 +0000875 // Check for caught exceptions.
876 if (caught_exception) return false;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000877
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000878 // Debugger loaded, create debugger context global handle.
879 debug_context_ = Handle<Context>::cast(
880 isolate_->global_handles()->Create(*context));
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000881
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000882 return true;
883}
884
885
886void Debug::Unload() {
887 // Return debugger is not loaded.
888 if (!IsLoaded()) {
889 return;
890 }
891
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000892 // Clear the script cache.
893 DestroyScriptCache();
894
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000895 // Clear debugger context global handle.
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000896 isolate_->global_handles()->Destroy(
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000897 reinterpret_cast<Object**>(debug_context_.location()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000898 debug_context_ = Handle<Context>();
899}
900
901
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000902// Set the flag indicating that preemption happened during debugging.
903void Debug::PreemptionWhileInDebugger() {
904 ASSERT(InDebugger());
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000905 Debug::set_interrupts_pending(PREEMPT);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000906}
907
908
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000909void Debug::Iterate(ObjectVisitor* v) {
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000910 v->VisitPointer(BitCast<Object**>(&(debug_break_return_)));
911 v->VisitPointer(BitCast<Object**>(&(debug_break_slot_)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000912}
913
914
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000915Object* Debug::Break(Arguments args) {
916 Heap* heap = isolate_->heap();
917 HandleScope scope(isolate_);
mads.s.ager31e71382008-08-13 09:32:07 +0000918 ASSERT(args.length() == 0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000919
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000920 thread_local_.frame_drop_mode_ = FRAMES_UNTOUCHED;
ager@chromium.org357bf652010-04-12 11:30:10 +0000921
kasper.lundbd3ec4e2008-07-09 11:06:54 +0000922 // Get the top-most JavaScript frame.
vegorov@chromium.org74f333b2011-04-06 11:17:46 +0000923 JavaScriptFrameIterator it(isolate_);
kasper.lundbd3ec4e2008-07-09 11:06:54 +0000924 JavaScriptFrame* frame = it.frame();
925
926 // Just continue if breaks are disabled or debugger cannot be loaded.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000927 if (disable_break() || !Load()) {
928 SetAfterBreakTarget(frame);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000929 return heap->undefined_value();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000930 }
931
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000932 // Enter the debugger.
933 EnterDebugger debugger;
934 if (debugger.FailedToEnter()) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000935 return heap->undefined_value();
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000936 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000937
kasper.lund44510672008-07-25 07:37:58 +0000938 // Postpone interrupt during breakpoint processing.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000939 PostponeInterruptsScope postpone(isolate_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000940
941 // Get the debug info (create it if it does not exist).
942 Handle<SharedFunctionInfo> shared =
943 Handle<SharedFunctionInfo>(JSFunction::cast(frame->function())->shared());
944 Handle<DebugInfo> debug_info = GetDebugInfo(shared);
945
946 // Find the break point where execution has stopped.
947 BreakLocationIterator break_location_iterator(debug_info,
948 ALL_BREAK_LOCATIONS);
949 break_location_iterator.FindBreakLocationFromAddress(frame->pc());
950
951 // Check whether step next reached a new statement.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000952 if (!StepNextContinue(&break_location_iterator, frame)) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000953 // Decrease steps left if performing multiple steps.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000954 if (thread_local_.step_count_ > 0) {
955 thread_local_.step_count_--;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000956 }
957 }
958
959 // If there is one or more real break points check whether any of these are
960 // triggered.
ulan@chromium.org09d7ab52013-02-25 15:50:35 +0000961 Handle<Object> break_points_hit(heap->undefined_value(), isolate_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000962 if (break_location_iterator.HasBreakPoint()) {
963 Handle<Object> break_point_objects =
ulan@chromium.org09d7ab52013-02-25 15:50:35 +0000964 Handle<Object>(break_location_iterator.BreakPointObjects(), isolate_);
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000965 break_points_hit = CheckBreakPoints(break_point_objects);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000966 }
967
ager@chromium.orga1645e22009-09-09 19:27:10 +0000968 // If step out is active skip everything until the frame where we need to step
969 // out to is reached, unless real breakpoint is hit.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000970 if (StepOutActive() && frame->fp() != step_out_fp() &&
ager@chromium.orga1645e22009-09-09 19:27:10 +0000971 break_points_hit->IsUndefined() ) {
972 // Step count should always be 0 for StepOut.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000973 ASSERT(thread_local_.step_count_ == 0);
ager@chromium.orga1645e22009-09-09 19:27:10 +0000974 } else if (!break_points_hit->IsUndefined() ||
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000975 (thread_local_.last_step_action_ != StepNone &&
976 thread_local_.step_count_ == 0)) {
ager@chromium.orga1645e22009-09-09 19:27:10 +0000977 // Notify debugger if a real break point is triggered or if performing
978 // single stepping with no more steps to perform. Otherwise do another step.
979
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000980 // Clear all current stepping setup.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000981 ClearStepping();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000982
lrn@chromium.org34e60782011-09-15 07:25:40 +0000983 if (thread_local_.queued_step_count_ > 0) {
984 // Perform queued steps
985 int step_count = thread_local_.queued_step_count_;
986
987 // Clear queue
988 thread_local_.queued_step_count_ = 0;
989
990 PrepareStep(StepNext, step_count);
991 } else {
992 // Notify the debug event listeners.
993 isolate_->debugger()->OnDebugBreak(break_points_hit, false);
994 }
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000995 } else if (thread_local_.last_step_action_ != StepNone) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000996 // Hold on to last step action as it is cleared by the call to
997 // ClearStepping.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000998 StepAction step_action = thread_local_.last_step_action_;
999 int step_count = thread_local_.step_count_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001000
lrn@chromium.org34e60782011-09-15 07:25:40 +00001001 // If StepNext goes deeper in code, StepOut until original frame
1002 // and keep step count queued up in the meantime.
1003 if (step_action == StepNext && frame->fp() < thread_local_.last_fp_) {
1004 // Count frames until target frame
1005 int count = 0;
1006 JavaScriptFrameIterator it(isolate_);
jkummerow@chromium.org212d9642012-05-11 15:02:09 +00001007 while (!it.done() && it.frame()->fp() < thread_local_.last_fp_) {
lrn@chromium.org34e60782011-09-15 07:25:40 +00001008 count++;
1009 it.Advance();
1010 }
1011
danno@chromium.org81cac2b2012-07-10 11:28:27 +00001012 // Check that we indeed found the frame we are looking for.
1013 CHECK(!it.done() && (it.frame()->fp() == thread_local_.last_fp_));
1014 if (step_count > 1) {
1015 // Save old count and action to continue stepping after StepOut.
1016 thread_local_.queued_step_count_ = step_count - 1;
jkummerow@chromium.org212d9642012-05-11 15:02:09 +00001017 }
1018
danno@chromium.org81cac2b2012-07-10 11:28:27 +00001019 // Set up for StepOut to reach target frame.
1020 step_action = StepOut;
1021 step_count = count;
lrn@chromium.org34e60782011-09-15 07:25:40 +00001022 }
1023
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001024 // Clear all current stepping setup.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001025 ClearStepping();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001026
1027 // Set up for the remaining steps.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001028 PrepareStep(step_action, step_count);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001029 }
1030
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001031 if (thread_local_.frame_drop_mode_ == FRAMES_UNTOUCHED) {
1032 SetAfterBreakTarget(frame);
1033 } else if (thread_local_.frame_drop_mode_ ==
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001034 FRAME_DROPPED_IN_IC_CALL) {
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001035 // We must have been calling IC stub. Do not go there anymore.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001036 Code* plain_return = isolate_->builtins()->builtin(
1037 Builtins::kPlainReturn_LiveEdit);
1038 thread_local_.after_break_target_ = plain_return->entry();
1039 } else if (thread_local_.frame_drop_mode_ ==
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001040 FRAME_DROPPED_IN_DEBUG_SLOT_CALL) {
1041 // Debug break slot stub does not return normally, instead it manually
1042 // cleans the stack and jumps. We should patch the jump address.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001043 Code* plain_return = isolate_->builtins()->builtin(
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00001044 Builtins::kFrameDropper_LiveEdit);
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001045 thread_local_.after_break_target_ = plain_return->entry();
1046 } else if (thread_local_.frame_drop_mode_ ==
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001047 FRAME_DROPPED_IN_DIRECT_CALL) {
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001048 // Nothing to do, after_break_target is not used here.
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00001049 } else if (thread_local_.frame_drop_mode_ ==
1050 FRAME_DROPPED_IN_RETURN_CALL) {
1051 Code* plain_return = isolate_->builtins()->builtin(
1052 Builtins::kFrameDropper_LiveEdit);
1053 thread_local_.after_break_target_ = plain_return->entry();
ager@chromium.org357bf652010-04-12 11:30:10 +00001054 } else {
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001055 UNREACHABLE();
ager@chromium.org357bf652010-04-12 11:30:10 +00001056 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001057
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001058 return heap->undefined_value();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001059}
1060
1061
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001062RUNTIME_FUNCTION(Object*, Debug_Break) {
1063 return isolate->debug()->Break(args);
1064}
1065
1066
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001067// Check the break point objects for whether one or more are actually
1068// triggered. This function returns a JSArray with the break point objects
1069// which is triggered.
1070Handle<Object> Debug::CheckBreakPoints(Handle<Object> break_point_objects) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00001071 Factory* factory = isolate_->factory();
1072
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001073 // Count the number of break points hit. If there are multiple break points
1074 // they are in a FixedArray.
1075 Handle<FixedArray> break_points_hit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001076 int break_points_hit_count = 0;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001077 ASSERT(!break_point_objects->IsUndefined());
1078 if (break_point_objects->IsFixedArray()) {
1079 Handle<FixedArray> array(FixedArray::cast(*break_point_objects));
lrn@chromium.org7516f052011-03-30 08:52:27 +00001080 break_points_hit = factory->NewFixedArray(array->length());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001081 for (int i = 0; i < array->length(); i++) {
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00001082 Handle<Object> o(array->get(i), isolate_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001083 if (CheckBreakPoint(o)) {
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001084 break_points_hit->set(break_points_hit_count++, *o);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001085 }
1086 }
1087 } else {
lrn@chromium.org7516f052011-03-30 08:52:27 +00001088 break_points_hit = factory->NewFixedArray(1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001089 if (CheckBreakPoint(break_point_objects)) {
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001090 break_points_hit->set(break_points_hit_count++, *break_point_objects);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001091 }
1092 }
1093
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001094 // Return undefined if no break points were triggered.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001095 if (break_points_hit_count == 0) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00001096 return factory->undefined_value();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001097 }
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001098 // Return break points hit as a JSArray.
lrn@chromium.org7516f052011-03-30 08:52:27 +00001099 Handle<JSArray> result = factory->NewJSArrayWithElements(break_points_hit);
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001100 result->set_length(Smi::FromInt(break_points_hit_count));
1101 return result;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001102}
1103
1104
1105// Check whether a single break point object is triggered.
1106bool Debug::CheckBreakPoint(Handle<Object> break_point_object) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00001107 Factory* factory = isolate_->factory();
1108 HandleScope scope(isolate_);
ager@chromium.org381abbb2009-02-25 13:23:22 +00001109
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001110 // Ignore check if break point object is not a JSObject.
1111 if (!break_point_object->IsJSObject()) return true;
1112
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001113 // Get the function IsBreakPointTriggered (defined in debug-debugger.js).
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001114 Handle<String> is_break_point_triggered_string =
1115 factory->InternalizeOneByteString(
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00001116 STATIC_ASCII_VECTOR("IsBreakPointTriggered"));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001117 Handle<JSFunction> check_break_point =
1118 Handle<JSFunction>(JSFunction::cast(
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00001119 debug_context()->global_object()->GetPropertyNoExceptionThrown(
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001120 *is_break_point_triggered_string)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001121
1122 // Get the break id as an object.
lrn@chromium.org7516f052011-03-30 08:52:27 +00001123 Handle<Object> break_id = factory->NewNumberFromInt(Debug::break_id());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001124
1125 // Call HandleBreakPointx.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001126 bool caught_exception;
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00001127 Handle<Object> argv[] = { break_id, break_point_object };
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001128 Handle<Object> result = Execution::TryCall(check_break_point,
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00001129 isolate_->js_builtins_object(),
1130 ARRAY_SIZE(argv),
1131 argv,
1132 &caught_exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001133
1134 // If exception or non boolean result handle as not triggered
1135 if (caught_exception || !result->IsBoolean()) {
1136 return false;
1137 }
1138
1139 // Return whether the break point is triggered.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001140 ASSERT(!result.is_null());
1141 return (*result)->IsTrue();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001142}
1143
1144
1145// Check whether the function has debug information.
1146bool Debug::HasDebugInfo(Handle<SharedFunctionInfo> shared) {
1147 return !shared->debug_info()->IsUndefined();
1148}
1149
1150
kasper.lundbd3ec4e2008-07-09 11:06:54 +00001151// Return the debug info for this function. EnsureDebugInfo must be called
1152// prior to ensure the debug info has been generated for shared.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001153Handle<DebugInfo> Debug::GetDebugInfo(Handle<SharedFunctionInfo> shared) {
kasper.lundbd3ec4e2008-07-09 11:06:54 +00001154 ASSERT(HasDebugInfo(shared));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001155 return Handle<DebugInfo>(DebugInfo::cast(shared->debug_info()));
1156}
1157
1158
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001159void Debug::SetBreakPoint(Handle<JSFunction> function,
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00001160 Handle<Object> break_point_object,
1161 int* source_position) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00001162 HandleScope scope(isolate_);
ager@chromium.org381abbb2009-02-25 13:23:22 +00001163
lrn@chromium.org34e60782011-09-15 07:25:40 +00001164 PrepareForBreakPoints();
1165
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001166 // Make sure the function is compiled and has set up the debug info.
1167 Handle<SharedFunctionInfo> shared(function->shared());
1168 if (!EnsureDebugInfo(shared, function)) {
kasper.lundbd3ec4e2008-07-09 11:06:54 +00001169 // Return if retrieving debug info failed.
1170 return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001171 }
1172
kasper.lundbd3ec4e2008-07-09 11:06:54 +00001173 Handle<DebugInfo> debug_info = GetDebugInfo(shared);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001174 // Source positions starts with zero.
danno@chromium.orgbf0c8202011-12-27 10:09:42 +00001175 ASSERT(*source_position >= 0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001176
1177 // Find the break point and change it.
1178 BreakLocationIterator it(debug_info, SOURCE_BREAK_LOCATIONS);
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00001179 it.FindBreakLocationFromPosition(*source_position);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001180 it.SetBreakPoint(break_point_object);
1181
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00001182 *source_position = it.position();
1183
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001184 // At least one active break point now.
1185 ASSERT(debug_info->GetBreakPointCount() > 0);
1186}
1187
1188
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001189bool Debug::SetBreakPointForScript(Handle<Script> script,
1190 Handle<Object> break_point_object,
1191 int* source_position) {
1192 HandleScope scope(isolate_);
1193
jkummerow@chromium.org78502a92012-09-06 13:50:42 +00001194 PrepareForBreakPoints();
1195
1196 // Obtain shared function info for the function.
1197 Object* result = FindSharedFunctionInfoInScript(script, *source_position);
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001198 if (result->IsUndefined()) return false;
1199
1200 // Make sure the function has set up the debug info.
1201 Handle<SharedFunctionInfo> shared(SharedFunctionInfo::cast(result));
1202 if (!EnsureDebugInfo(shared, Handle<JSFunction>::null())) {
1203 // Return if retrieving debug info failed.
1204 return false;
1205 }
1206
1207 // Find position within function. The script position might be before the
1208 // source position of the first function.
1209 int position;
1210 if (shared->start_position() > *source_position) {
1211 position = 0;
1212 } else {
1213 position = *source_position - shared->start_position();
1214 }
1215
1216 Handle<DebugInfo> debug_info = GetDebugInfo(shared);
1217 // Source positions starts with zero.
1218 ASSERT(position >= 0);
1219
1220 // Find the break point and change it.
1221 BreakLocationIterator it(debug_info, SOURCE_BREAK_LOCATIONS);
1222 it.FindBreakLocationFromPosition(position);
1223 it.SetBreakPoint(break_point_object);
1224
1225 *source_position = it.position() + shared->start_position();
1226
1227 // At least one active break point now.
1228 ASSERT(debug_info->GetBreakPointCount() > 0);
1229 return true;
1230}
1231
1232
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001233void Debug::ClearBreakPoint(Handle<Object> break_point_object) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00001234 HandleScope scope(isolate_);
ager@chromium.org381abbb2009-02-25 13:23:22 +00001235
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001236 DebugInfoListNode* node = debug_info_list_;
1237 while (node != NULL) {
1238 Object* result = DebugInfo::FindBreakPointInfo(node->debug_info(),
1239 break_point_object);
1240 if (!result->IsUndefined()) {
1241 // Get information in the break point.
1242 BreakPointInfo* break_point_info = BreakPointInfo::cast(result);
1243 Handle<DebugInfo> debug_info = node->debug_info();
1244 Handle<SharedFunctionInfo> shared(debug_info->shared());
1245 int source_position = break_point_info->statement_position()->value();
1246
1247 // Source positions starts with zero.
1248 ASSERT(source_position >= 0);
1249
1250 // Find the break point and clear it.
1251 BreakLocationIterator it(debug_info, SOURCE_BREAK_LOCATIONS);
1252 it.FindBreakLocationFromPosition(source_position);
1253 it.ClearBreakPoint(break_point_object);
1254
1255 // If there are no more break points left remove the debug info for this
1256 // function.
1257 if (debug_info->GetBreakPointCount() == 0) {
1258 RemoveDebugInfo(debug_info);
1259 }
1260
1261 return;
1262 }
1263 node = node->next();
1264 }
1265}
1266
1267
ager@chromium.org381abbb2009-02-25 13:23:22 +00001268void Debug::ClearAllBreakPoints() {
1269 DebugInfoListNode* node = debug_info_list_;
1270 while (node != NULL) {
1271 // Remove all debug break code.
1272 BreakLocationIterator it(node->debug_info(), ALL_BREAK_LOCATIONS);
1273 it.ClearAllDebugBreak();
1274 node = node->next();
1275 }
1276
1277 // Remove all debug info.
1278 while (debug_info_list_ != NULL) {
1279 RemoveDebugInfo(debug_info_list_->debug_info());
1280 }
1281}
1282
1283
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001284void Debug::FloodWithOneShot(Handle<JSFunction> function) {
lrn@chromium.org34e60782011-09-15 07:25:40 +00001285 PrepareForBreakPoints();
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001286
1287 // Make sure the function is compiled and has set up the debug info.
1288 Handle<SharedFunctionInfo> shared(function->shared());
1289 if (!EnsureDebugInfo(shared, function)) {
kasper.lundbd3ec4e2008-07-09 11:06:54 +00001290 // Return if we failed to retrieve the debug info.
1291 return;
1292 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001293
1294 // Flood the function with break points.
kasper.lundbd3ec4e2008-07-09 11:06:54 +00001295 BreakLocationIterator it(GetDebugInfo(shared), ALL_BREAK_LOCATIONS);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001296 while (!it.Done()) {
1297 it.SetOneShot();
1298 it.Next();
1299 }
1300}
1301
1302
rossberg@chromium.org2c067b12012-03-19 11:01:52 +00001303void Debug::FloodBoundFunctionWithOneShot(Handle<JSFunction> function) {
1304 Handle<FixedArray> new_bindings(function->function_bindings());
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00001305 Handle<Object> bindee(new_bindings->get(JSFunction::kBoundFunctionIndex),
1306 isolate_);
rossberg@chromium.org2c067b12012-03-19 11:01:52 +00001307
1308 if (!bindee.is_null() && bindee->IsJSFunction() &&
1309 !JSFunction::cast(*bindee)->IsBuiltin()) {
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001310 Handle<JSFunction> bindee_function(JSFunction::cast(*bindee));
1311 Debug::FloodWithOneShot(bindee_function);
rossberg@chromium.org2c067b12012-03-19 11:01:52 +00001312 }
1313}
1314
1315
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001316void Debug::FloodHandlerWithOneShot() {
ager@chromium.org8bb60582008-12-11 12:02:20 +00001317 // Iterate through the JavaScript stack looking for handlers.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001318 StackFrame::Id id = break_frame_id();
ager@chromium.org8bb60582008-12-11 12:02:20 +00001319 if (id == StackFrame::NO_ID) {
1320 // If there is no JavaScript stack don't do anything.
1321 return;
1322 }
vegorov@chromium.org74f333b2011-04-06 11:17:46 +00001323 for (JavaScriptFrameIterator it(isolate_, id); !it.done(); it.Advance()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001324 JavaScriptFrame* frame = it.frame();
1325 if (frame->HasHandler()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001326 // Flood the function with the catch block with break points
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001327 JSFunction* function = JSFunction::cast(frame->function());
1328 FloodWithOneShot(Handle<JSFunction>(function));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001329 return;
1330 }
1331 }
1332}
1333
1334
1335void Debug::ChangeBreakOnException(ExceptionBreakType type, bool enable) {
1336 if (type == BreakUncaughtException) {
1337 break_on_uncaught_exception_ = enable;
1338 } else {
1339 break_on_exception_ = enable;
1340 }
1341}
1342
1343
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00001344bool Debug::IsBreakOnException(ExceptionBreakType type) {
1345 if (type == BreakUncaughtException) {
1346 return break_on_uncaught_exception_;
1347 } else {
1348 return break_on_exception_;
1349 }
1350}
1351
1352
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001353void Debug::PrepareStep(StepAction step_action, int step_count) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00001354 HandleScope scope(isolate_);
lrn@chromium.org34e60782011-09-15 07:25:40 +00001355
1356 PrepareForBreakPoints();
1357
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001358 ASSERT(Debug::InDebugger());
1359
1360 // Remember this step action and count.
1361 thread_local_.last_step_action_ = step_action;
ager@chromium.orga1645e22009-09-09 19:27:10 +00001362 if (step_action == StepOut) {
1363 // For step out target frame will be found on the stack so there is no need
1364 // to set step counter for it. It's expected to always be 0 for StepOut.
1365 thread_local_.step_count_ = 0;
1366 } else {
1367 thread_local_.step_count_ = step_count;
1368 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001369
1370 // Get the frame where the execution has stopped and skip the debug frame if
1371 // any. The debug frame will only be present if execution was stopped due to
1372 // hitting a break point. In other situations (e.g. unhandled exception) the
1373 // debug frame is not present.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001374 StackFrame::Id id = break_frame_id();
ager@chromium.org8bb60582008-12-11 12:02:20 +00001375 if (id == StackFrame::NO_ID) {
1376 // If there is no JavaScript stack don't do anything.
1377 return;
1378 }
vegorov@chromium.org74f333b2011-04-06 11:17:46 +00001379 JavaScriptFrameIterator frames_it(isolate_, id);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001380 JavaScriptFrame* frame = frames_it.frame();
1381
1382 // First of all ensure there is one-shot break points in the top handler
1383 // if any.
1384 FloodHandlerWithOneShot();
1385
1386 // If the function on the top frame is unresolved perform step out. This will
1387 // be the case when calling unknown functions and having the debugger stopped
1388 // in an unhandled exception.
1389 if (!frame->function()->IsJSFunction()) {
1390 // Step out: Find the calling JavaScript frame and flood it with
1391 // breakpoints.
1392 frames_it.Advance();
1393 // Fill the function to return to with one-shot break points.
1394 JSFunction* function = JSFunction::cast(frames_it.frame()->function());
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001395 FloodWithOneShot(Handle<JSFunction>(function));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001396 return;
1397 }
1398
1399 // Get the debug info (create it if it does not exist).
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001400 Handle<JSFunction> function(JSFunction::cast(frame->function()));
1401 Handle<SharedFunctionInfo> shared(function->shared());
1402 if (!EnsureDebugInfo(shared, function)) {
kasper.lundbd3ec4e2008-07-09 11:06:54 +00001403 // Return if ensuring debug info failed.
1404 return;
1405 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001406 Handle<DebugInfo> debug_info = GetDebugInfo(shared);
1407
1408 // Find the break location where execution has stopped.
1409 BreakLocationIterator it(debug_info, ALL_BREAK_LOCATIONS);
1410 it.FindBreakLocationFromAddress(frame->pc());
1411
1412 // Compute whether or not the target is a call target.
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001413 bool is_load_or_store = false;
1414 bool is_inline_cache_stub = false;
whesse@chromium.orge90029b2010-08-02 11:52:17 +00001415 bool is_at_restarted_function = false;
ager@chromium.orga1645e22009-09-09 19:27:10 +00001416 Handle<Code> call_function_stub;
ager@chromium.orga1645e22009-09-09 19:27:10 +00001417
whesse@chromium.orge90029b2010-08-02 11:52:17 +00001418 if (thread_local_.restarter_frame_function_pointer_ == NULL) {
1419 if (RelocInfo::IsCodeTarget(it.rinfo()->rmode())) {
1420 bool is_call_target = false;
1421 Address target = it.rinfo()->target_address();
1422 Code* code = Code::GetCodeFromTargetAddress(target);
1423 if (code->is_call_stub() || code->is_keyed_call_stub()) {
1424 is_call_target = true;
1425 }
1426 if (code->is_inline_cache_stub()) {
1427 is_inline_cache_stub = true;
1428 is_load_or_store = !is_call_target;
1429 }
1430
1431 // Check if target code is CallFunction stub.
1432 Code* maybe_call_function_stub = code;
1433 // If there is a breakpoint at this line look at the original code to
1434 // check if it is a CallFunction stub.
1435 if (it.IsDebugBreak()) {
1436 Address original_target = it.original_rinfo()->target_address();
1437 maybe_call_function_stub =
1438 Code::GetCodeFromTargetAddress(original_target);
1439 }
1440 if (maybe_call_function_stub->kind() == Code::STUB &&
1441 maybe_call_function_stub->major_key() == CodeStub::CallFunction) {
1442 // Save reference to the code as we may need it to find out arguments
1443 // count for 'step in' later.
1444 call_function_stub = Handle<Code>(maybe_call_function_stub);
1445 }
ager@chromium.orga1645e22009-09-09 19:27:10 +00001446 }
whesse@chromium.orge90029b2010-08-02 11:52:17 +00001447 } else {
1448 is_at_restarted_function = true;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001449 }
1450
v8.team.kasperl727e9952008-09-02 14:56:44 +00001451 // If this is the last break code target step out is the only possibility.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001452 if (it.IsExit() || step_action == StepOut) {
ager@chromium.orga1645e22009-09-09 19:27:10 +00001453 if (step_action == StepOut) {
1454 // Skip step_count frames starting with the current one.
1455 while (step_count-- > 0 && !frames_it.done()) {
1456 frames_it.Advance();
1457 }
1458 } else {
1459 ASSERT(it.IsExit());
1460 frames_it.Advance();
1461 }
1462 // Skip builtin functions on the stack.
1463 while (!frames_it.done() &&
1464 JSFunction::cast(frames_it.frame()->function())->IsBuiltin()) {
1465 frames_it.Advance();
1466 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001467 // Step out: If there is a JavaScript caller frame, we need to
1468 // flood it with breakpoints.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001469 if (!frames_it.done()) {
1470 // Fill the function to return to with one-shot break points.
1471 JSFunction* function = JSFunction::cast(frames_it.frame()->function());
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001472 FloodWithOneShot(Handle<JSFunction>(function));
ager@chromium.orga1645e22009-09-09 19:27:10 +00001473 // Set target frame pointer.
1474 ActivateStepOut(frames_it.frame());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001475 }
ager@chromium.orga1645e22009-09-09 19:27:10 +00001476 } else if (!(is_inline_cache_stub || RelocInfo::IsConstructCall(it.rmode()) ||
whesse@chromium.orge90029b2010-08-02 11:52:17 +00001477 !call_function_stub.is_null() || is_at_restarted_function)
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001478 || step_action == StepNext || step_action == StepMin) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001479 // Step next or step min.
1480
1481 // Fill the current function with one-shot break points.
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001482 FloodWithOneShot(function);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001483
1484 // Remember source position and frame to handle step next.
1485 thread_local_.last_statement_position_ =
1486 debug_info->code()->SourceStatementPosition(frame->pc());
mmassi@chromium.org7028c052012-06-13 11:51:58 +00001487 thread_local_.last_fp_ = frame->UnpaddedFP();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001488 } else {
whesse@chromium.orge90029b2010-08-02 11:52:17 +00001489 // If there's restarter frame on top of the stack, just get the pointer
1490 // to function which is going to be restarted.
1491 if (is_at_restarted_function) {
1492 Handle<JSFunction> restarted_function(
1493 JSFunction::cast(*thread_local_.restarter_frame_function_pointer_));
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001494 FloodWithOneShot(restarted_function);
whesse@chromium.orge90029b2010-08-02 11:52:17 +00001495 } else if (!call_function_stub.is_null()) {
1496 // If it's CallFunction stub ensure target function is compiled and flood
1497 // it with one shot breakpoints.
1498
ager@chromium.orga1645e22009-09-09 19:27:10 +00001499 // Find out number of arguments from the stub minor key.
1500 // Reverse lookup required as the minor key cannot be retrieved
1501 // from the code object.
1502 Handle<Object> obj(
lrn@chromium.org7516f052011-03-30 08:52:27 +00001503 isolate_->heap()->code_stubs()->SlowReverseLookup(
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00001504 *call_function_stub),
1505 isolate_);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001506 ASSERT(!obj.is_null());
1507 ASSERT(!(*obj)->IsUndefined());
ager@chromium.orga1645e22009-09-09 19:27:10 +00001508 ASSERT(obj->IsSmi());
1509 // Get the STUB key and extract major and minor key.
1510 uint32_t key = Smi::cast(*obj)->value();
1511 // Argc in the stub is the number of arguments passed - not the
1512 // expected arguments of the called function.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001513 int call_function_arg_count =
1514 CallFunctionStub::ExtractArgcFromMinorKey(
1515 CodeStub::MinorKeyFromKey(key));
ager@chromium.orga1645e22009-09-09 19:27:10 +00001516 ASSERT(call_function_stub->major_key() ==
1517 CodeStub::MajorKeyFromKey(key));
1518
1519 // Find target function on the expression stack.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001520 // Expression stack looks like this (top to bottom):
ager@chromium.orga1645e22009-09-09 19:27:10 +00001521 // argN
1522 // ...
1523 // arg0
1524 // Receiver
1525 // Function to call
1526 int expressions_count = frame->ComputeExpressionsCount();
1527 ASSERT(expressions_count - 2 - call_function_arg_count >= 0);
1528 Object* fun = frame->GetExpression(
1529 expressions_count - 2 - call_function_arg_count);
1530 if (fun->IsJSFunction()) {
1531 Handle<JSFunction> js_function(JSFunction::cast(fun));
rossberg@chromium.org2c067b12012-03-19 11:01:52 +00001532 if (js_function->shared()->bound()) {
1533 Debug::FloodBoundFunctionWithOneShot(js_function);
1534 } else if (!js_function->IsBuiltin()) {
1535 // Don't step into builtins.
ager@chromium.orga1645e22009-09-09 19:27:10 +00001536 // It will also compile target function if it's not compiled yet.
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001537 FloodWithOneShot(js_function);
ager@chromium.orga1645e22009-09-09 19:27:10 +00001538 }
1539 }
1540 }
1541
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001542 // Fill the current function with one-shot break points even for step in on
1543 // a call target as the function called might be a native function for
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001544 // which step in will not stop. It also prepares for stepping in
1545 // getters/setters.
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001546 FloodWithOneShot(function);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001547
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001548 if (is_load_or_store) {
1549 // Remember source position and frame to handle step in getter/setter. If
1550 // there is a custom getter/setter it will be handled in
1551 // Object::Get/SetPropertyWithCallback, otherwise the step action will be
1552 // propagated on the next Debug::Break.
1553 thread_local_.last_statement_position_ =
1554 debug_info->code()->SourceStatementPosition(frame->pc());
mmassi@chromium.org7028c052012-06-13 11:51:58 +00001555 thread_local_.last_fp_ = frame->UnpaddedFP();
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001556 }
1557
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001558 // Step in or Step in min
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00001559 it.PrepareStepIn(isolate_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001560 ActivateStepIn(frame);
1561 }
1562}
1563
1564
1565// Check whether the current debug break should be reported to the debugger. It
1566// is used to have step next and step in only report break back to the debugger
1567// if on a different frame or in a different statement. In some situations
1568// there will be several break points in the same statement when the code is
v8.team.kasperl727e9952008-09-02 14:56:44 +00001569// flooded with one-shot break points. This function helps to perform several
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001570// steps before reporting break back to the debugger.
1571bool Debug::StepNextContinue(BreakLocationIterator* break_location_iterator,
1572 JavaScriptFrame* frame) {
lrn@chromium.org34e60782011-09-15 07:25:40 +00001573 // StepNext and StepOut shouldn't bring us deeper in code, so last frame
1574 // shouldn't be a parent of current frame.
1575 if (thread_local_.last_step_action_ == StepNext ||
1576 thread_local_.last_step_action_ == StepOut) {
1577 if (frame->fp() < thread_local_.last_fp_) return true;
1578 }
1579
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001580 // If the step last action was step next or step in make sure that a new
1581 // statement is hit.
1582 if (thread_local_.last_step_action_ == StepNext ||
1583 thread_local_.last_step_action_ == StepIn) {
1584 // Never continue if returning from function.
1585 if (break_location_iterator->IsExit()) return false;
1586
1587 // Continue if we are still on the same frame and in the same statement.
1588 int current_statement_position =
1589 break_location_iterator->code()->SourceStatementPosition(frame->pc());
mmassi@chromium.org7028c052012-06-13 11:51:58 +00001590 return thread_local_.last_fp_ == frame->UnpaddedFP() &&
ager@chromium.org236ad962008-09-25 09:45:57 +00001591 thread_local_.last_statement_position_ == current_statement_position;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001592 }
1593
1594 // No step next action - don't continue.
1595 return false;
1596}
1597
1598
1599// Check whether the code object at the specified address is a debug break code
1600// object.
1601bool Debug::IsDebugBreak(Address addr) {
ager@chromium.org8bb60582008-12-11 12:02:20 +00001602 Code* code = Code::GetCodeFromTargetAddress(addr);
yangguo@chromium.org9768bf12013-01-11 14:51:07 +00001603 return code->is_debug_break();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001604}
1605
1606
1607// Check whether a code stub with the specified major key is a possible break
1608// point location when looking for source break locations.
1609bool Debug::IsSourceBreakStub(Code* code) {
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001610 CodeStub::Major major_key = CodeStub::GetMajorKey(code);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001611 return major_key == CodeStub::CallFunction;
1612}
1613
1614
1615// Check whether a code stub with the specified major key is a possible break
1616// location.
1617bool Debug::IsBreakStub(Code* code) {
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001618 CodeStub::Major major_key = CodeStub::GetMajorKey(code);
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00001619 return major_key == CodeStub::CallFunction;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001620}
1621
1622
1623// Find the builtin to use for invoking the debug break
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001624Handle<Code> Debug::FindDebugBreak(Handle<Code> code, RelocInfo::Mode mode) {
danno@chromium.orgfa458e42012-02-01 10:48:36 +00001625 Isolate* isolate = Isolate::Current();
1626
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001627 // Find the builtin debug break function matching the calling convention
1628 // used by the call site.
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001629 if (code->is_inline_cache_stub()) {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001630 switch (code->kind()) {
1631 case Code::CALL_IC:
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001632 case Code::KEYED_CALL_IC:
danno@chromium.orgfa458e42012-02-01 10:48:36 +00001633 return isolate->stub_cache()->ComputeCallDebugBreak(
1634 code->arguments_count(), code->kind());
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001635
1636 case Code::LOAD_IC:
danno@chromium.orgfa458e42012-02-01 10:48:36 +00001637 return isolate->builtins()->LoadIC_DebugBreak();
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001638
1639 case Code::STORE_IC:
danno@chromium.orgfa458e42012-02-01 10:48:36 +00001640 return isolate->builtins()->StoreIC_DebugBreak();
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001641
1642 case Code::KEYED_LOAD_IC:
danno@chromium.orgfa458e42012-02-01 10:48:36 +00001643 return isolate->builtins()->KeyedLoadIC_DebugBreak();
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001644
1645 case Code::KEYED_STORE_IC:
danno@chromium.orgfa458e42012-02-01 10:48:36 +00001646 return isolate->builtins()->KeyedStoreIC_DebugBreak();
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001647
1648 default:
1649 UNREACHABLE();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001650 }
1651 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001652 if (RelocInfo::IsConstructCall(mode)) {
danno@chromium.orgfa458e42012-02-01 10:48:36 +00001653 if (code->has_function_cache()) {
1654 return isolate->builtins()->CallConstructStub_Recording_DebugBreak();
1655 } else {
1656 return isolate->builtins()->CallConstructStub_DebugBreak();
1657 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001658 }
1659 if (code->kind() == Code::STUB) {
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00001660 ASSERT(code->major_key() == CodeStub::CallFunction);
danno@chromium.orgfa458e42012-02-01 10:48:36 +00001661 if (code->has_function_cache()) {
1662 return isolate->builtins()->CallFunctionStub_Recording_DebugBreak();
1663 } else {
1664 return isolate->builtins()->CallFunctionStub_DebugBreak();
1665 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001666 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001667
1668 UNREACHABLE();
1669 return Handle<Code>::null();
1670}
1671
1672
1673// Simple function for returning the source positions for active break points.
1674Handle<Object> Debug::GetSourceBreakLocations(
1675 Handle<SharedFunctionInfo> shared) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00001676 Isolate* isolate = Isolate::Current();
1677 Heap* heap = isolate->heap();
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00001678 if (!HasDebugInfo(shared)) {
1679 return Handle<Object>(heap->undefined_value(), isolate);
1680 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001681 Handle<DebugInfo> debug_info = GetDebugInfo(shared);
1682 if (debug_info->GetBreakPointCount() == 0) {
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00001683 return Handle<Object>(heap->undefined_value(), isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001684 }
1685 Handle<FixedArray> locations =
lrn@chromium.org7516f052011-03-30 08:52:27 +00001686 isolate->factory()->NewFixedArray(debug_info->GetBreakPointCount());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001687 int count = 0;
1688 for (int i = 0; i < debug_info->break_points()->length(); i++) {
1689 if (!debug_info->break_points()->get(i)->IsUndefined()) {
1690 BreakPointInfo* break_point_info =
1691 BreakPointInfo::cast(debug_info->break_points()->get(i));
1692 if (break_point_info->GetBreakPointCount() > 0) {
1693 locations->set(count++, break_point_info->statement_position());
1694 }
1695 }
1696 }
1697 return locations;
1698}
1699
1700
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001701void Debug::NewBreak(StackFrame::Id break_frame_id) {
1702 thread_local_.break_frame_id_ = break_frame_id;
1703 thread_local_.break_id_ = ++thread_local_.break_count_;
1704}
1705
1706
1707void Debug::SetBreak(StackFrame::Id break_frame_id, int break_id) {
1708 thread_local_.break_frame_id_ = break_frame_id;
1709 thread_local_.break_id_ = break_id;
1710}
1711
1712
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001713// Handle stepping into a function.
1714void Debug::HandleStepIn(Handle<JSFunction> function,
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001715 Handle<Object> holder,
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001716 Address fp,
1717 bool is_constructor) {
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00001718 Isolate* isolate = function->GetIsolate();
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001719 // If the frame pointer is not supplied by the caller find it.
1720 if (fp == 0) {
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00001721 StackFrameIterator it(isolate);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001722 it.Advance();
1723 // For constructor functions skip another frame.
1724 if (is_constructor) {
1725 ASSERT(it.frame()->is_construct());
1726 it.Advance();
1727 }
1728 fp = it.frame()->fp();
1729 }
1730
1731 // Flood the function with one-shot break points if it is called from where
1732 // step into was requested.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001733 if (fp == step_in_fp()) {
rossberg@chromium.org2c067b12012-03-19 11:01:52 +00001734 if (function->shared()->bound()) {
1735 // Handle Function.prototype.bind
1736 Debug::FloodBoundFunctionWithOneShot(function);
1737 } else if (!function->IsBuiltin()) {
1738 // Don't allow step into functions in the native context.
kasperl@chromium.orgacae3782009-04-11 09:17:08 +00001739 if (function->shared()->code() ==
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00001740 isolate->builtins()->builtin(Builtins::kFunctionApply) ||
kasperl@chromium.orgacae3782009-04-11 09:17:08 +00001741 function->shared()->code() ==
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00001742 isolate->builtins()->builtin(Builtins::kFunctionCall)) {
kasperl@chromium.orgacae3782009-04-11 09:17:08 +00001743 // Handle function.apply and function.call separately to flood the
1744 // function to be called and not the code for Builtins::FunctionApply or
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001745 // Builtins::FunctionCall. The receiver of call/apply is the target
1746 // function.
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001747 if (!holder.is_null() && holder->IsJSFunction() &&
1748 !JSFunction::cast(*holder)->IsBuiltin()) {
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001749 Handle<JSFunction> js_function = Handle<JSFunction>::cast(holder);
1750 Debug::FloodWithOneShot(js_function);
kasperl@chromium.orgacae3782009-04-11 09:17:08 +00001751 }
1752 } else {
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001753 Debug::FloodWithOneShot(function);
kasperl@chromium.orgacae3782009-04-11 09:17:08 +00001754 }
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001755 }
1756 }
1757}
1758
1759
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001760void Debug::ClearStepping() {
1761 // Clear the various stepping setup.
1762 ClearOneShot();
1763 ClearStepIn();
ager@chromium.orga1645e22009-09-09 19:27:10 +00001764 ClearStepOut();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001765 ClearStepNext();
1766
1767 // Clear multiple step counter.
1768 thread_local_.step_count_ = 0;
1769}
1770
1771// Clears all the one-shot break points that are currently set. Normally this
1772// function is called each time a break point is hit as one shot break points
1773// are used to support stepping.
1774void Debug::ClearOneShot() {
1775 // The current implementation just runs through all the breakpoints. When the
v8.team.kasperl727e9952008-09-02 14:56:44 +00001776 // last break point for a function is removed that function is automatically
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001777 // removed from the list.
1778
1779 DebugInfoListNode* node = debug_info_list_;
1780 while (node != NULL) {
1781 BreakLocationIterator it(node->debug_info(), ALL_BREAK_LOCATIONS);
1782 while (!it.Done()) {
1783 it.ClearOneShot();
1784 it.Next();
1785 }
1786 node = node->next();
1787 }
1788}
1789
1790
1791void Debug::ActivateStepIn(StackFrame* frame) {
ager@chromium.orga1645e22009-09-09 19:27:10 +00001792 ASSERT(!StepOutActive());
mmassi@chromium.org7028c052012-06-13 11:51:58 +00001793 thread_local_.step_into_fp_ = frame->UnpaddedFP();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001794}
1795
1796
1797void Debug::ClearStepIn() {
1798 thread_local_.step_into_fp_ = 0;
1799}
1800
1801
ager@chromium.orga1645e22009-09-09 19:27:10 +00001802void Debug::ActivateStepOut(StackFrame* frame) {
1803 ASSERT(!StepInActive());
mmassi@chromium.org7028c052012-06-13 11:51:58 +00001804 thread_local_.step_out_fp_ = frame->UnpaddedFP();
ager@chromium.orga1645e22009-09-09 19:27:10 +00001805}
1806
1807
1808void Debug::ClearStepOut() {
1809 thread_local_.step_out_fp_ = 0;
1810}
1811
1812
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001813void Debug::ClearStepNext() {
1814 thread_local_.last_step_action_ = StepNone;
ager@chromium.org236ad962008-09-25 09:45:57 +00001815 thread_local_.last_statement_position_ = RelocInfo::kNoPosition;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001816 thread_local_.last_fp_ = 0;
1817}
1818
1819
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001820// Helper function to compile full code for debugging. This code will
mmassi@chromium.org7028c052012-06-13 11:51:58 +00001821// have debug break slots and deoptimization information. Deoptimization
1822// information is required in case that an optimized version of this
1823// function is still activated on the stack. It will also make sure that
1824// the full code is compiled with the same flags as the previous version,
1825// that is flags which can change the code generated. The current method
1826// of mapping from already compiled full code without debug break slots
1827// to full code with debug break slots depends on the generated code is
1828// otherwise exactly the same.
1829static bool CompileFullCodeForDebugging(Handle<JSFunction> function,
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001830 Handle<Code> current_code) {
1831 ASSERT(!current_code->has_debug_break_slots());
1832
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001833 CompilationInfoWithZone info(function);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001834 info.MarkCompilingForDebugging(current_code);
1835 ASSERT(!info.shared_info()->is_compiled());
1836 ASSERT(!info.isolate()->has_pending_exception());
1837
1838 // Use compile lazy which will end up compiling the full code in the
1839 // configuration configured above.
1840 bool result = Compiler::CompileLazy(&info);
1841 ASSERT(result != Isolate::Current()->has_pending_exception());
1842 info.isolate()->clear_pending_exception();
1843#if DEBUG
1844 if (result) {
mmassi@chromium.org7028c052012-06-13 11:51:58 +00001845 Handle<Code> new_code(function->shared()->code());
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001846 ASSERT(new_code->has_debug_break_slots());
1847 ASSERT(current_code->is_compiled_optimizable() ==
1848 new_code->is_compiled_optimizable());
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001849 }
1850#endif
1851 return result;
1852}
1853
1854
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001855static void CollectActiveFunctionsFromThread(
1856 Isolate* isolate,
1857 ThreadLocalTop* top,
1858 List<Handle<JSFunction> >* active_functions,
1859 Object* active_code_marker) {
1860 // Find all non-optimized code functions with activation frames
1861 // on the stack. This includes functions which have optimized
1862 // activations (including inlined functions) on the stack as the
1863 // non-optimized code is needed for the lazy deoptimization.
1864 for (JavaScriptFrameIterator it(isolate, top); !it.done(); it.Advance()) {
1865 JavaScriptFrame* frame = it.frame();
1866 if (frame->is_optimized()) {
1867 List<JSFunction*> functions(Compiler::kMaxInliningLevels + 1);
1868 frame->GetFunctions(&functions);
1869 for (int i = 0; i < functions.length(); i++) {
1870 JSFunction* function = functions[i];
1871 active_functions->Add(Handle<JSFunction>(function));
1872 function->shared()->code()->set_gc_metadata(active_code_marker);
1873 }
1874 } else if (frame->function()->IsJSFunction()) {
1875 JSFunction* function = JSFunction::cast(frame->function());
1876 ASSERT(frame->LookupCode()->kind() == Code::FUNCTION);
1877 active_functions->Add(Handle<JSFunction>(function));
1878 function->shared()->code()->set_gc_metadata(active_code_marker);
1879 }
1880 }
1881}
1882
1883
1884static void RedirectActivationsToRecompiledCodeOnThread(
1885 Isolate* isolate,
1886 ThreadLocalTop* top) {
1887 for (JavaScriptFrameIterator it(isolate, top); !it.done(); it.Advance()) {
1888 JavaScriptFrame* frame = it.frame();
1889
1890 if (frame->is_optimized() || !frame->function()->IsJSFunction()) continue;
1891
1892 JSFunction* function = JSFunction::cast(frame->function());
1893
1894 ASSERT(frame->LookupCode()->kind() == Code::FUNCTION);
1895
1896 Handle<Code> frame_code(frame->LookupCode());
1897 if (frame_code->has_debug_break_slots()) continue;
1898
1899 Handle<Code> new_code(function->shared()->code());
1900 if (new_code->kind() != Code::FUNCTION ||
1901 !new_code->has_debug_break_slots()) {
1902 continue;
1903 }
1904
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001905 // Iterate over the RelocInfo in the original code to compute the sum of the
1906 // constant pools sizes. (See Assembler::CheckConstPool())
1907 // Note that this is only useful for architectures using constant pools.
1908 int constpool_mask = RelocInfo::ModeMask(RelocInfo::CONST_POOL);
1909 int frame_const_pool_size = 0;
1910 for (RelocIterator it(*frame_code, constpool_mask); !it.done(); it.next()) {
1911 RelocInfo* info = it.rinfo();
1912 if (info->pc() >= frame->pc()) break;
1913 frame_const_pool_size += static_cast<int>(info->data());
1914 }
1915 intptr_t frame_offset =
1916 frame->pc() - frame_code->instruction_start() - frame_const_pool_size;
1917
1918 // Iterate over the RelocInfo for new code to find the number of bytes
1919 // generated for debug slots and constant pools.
1920 int debug_break_slot_bytes = 0;
1921 int new_code_const_pool_size = 0;
1922 int mask = RelocInfo::ModeMask(RelocInfo::DEBUG_BREAK_SLOT) |
1923 RelocInfo::ModeMask(RelocInfo::CONST_POOL);
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001924 for (RelocIterator it(*new_code, mask); !it.done(); it.next()) {
1925 // Check if the pc in the new code with debug break
1926 // slots is before this slot.
1927 RelocInfo* info = it.rinfo();
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001928 intptr_t new_offset = info->pc() - new_code->instruction_start() -
1929 new_code_const_pool_size - debug_break_slot_bytes;
1930 if (new_offset >= frame_offset) {
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001931 break;
1932 }
1933
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001934 if (RelocInfo::IsDebugBreakSlot(info->rmode())) {
1935 debug_break_slot_bytes += Assembler::kDebugBreakSlotLength;
1936 } else {
1937 ASSERT(RelocInfo::IsConstPool(info->rmode()));
1938 // The size of the constant pool is encoded in the data.
1939 new_code_const_pool_size += static_cast<int>(info->data());
1940 }
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001941 }
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001942
1943 // Compute the equivalent pc in the new code.
1944 byte* new_pc = new_code->instruction_start() + frame_offset +
1945 debug_break_slot_bytes + new_code_const_pool_size;
1946
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001947 if (FLAG_trace_deopt) {
1948 PrintF("Replacing code %08" V8PRIxPTR " - %08" V8PRIxPTR " (%d) "
1949 "with %08" V8PRIxPTR " - %08" V8PRIxPTR " (%d) "
1950 "for debugging, "
1951 "changing pc from %08" V8PRIxPTR " to %08" V8PRIxPTR "\n",
1952 reinterpret_cast<intptr_t>(
1953 frame_code->instruction_start()),
1954 reinterpret_cast<intptr_t>(
1955 frame_code->instruction_start()) +
1956 frame_code->instruction_size(),
1957 frame_code->instruction_size(),
1958 reinterpret_cast<intptr_t>(new_code->instruction_start()),
1959 reinterpret_cast<intptr_t>(new_code->instruction_start()) +
1960 new_code->instruction_size(),
1961 new_code->instruction_size(),
1962 reinterpret_cast<intptr_t>(frame->pc()),
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001963 reinterpret_cast<intptr_t>(new_pc));
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001964 }
1965
1966 // Patch the return address to return into the code with
1967 // debug break slots.
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001968 frame->set_pc(new_pc);
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001969 }
1970}
1971
1972
1973class ActiveFunctionsCollector : public ThreadVisitor {
1974 public:
1975 explicit ActiveFunctionsCollector(List<Handle<JSFunction> >* active_functions,
1976 Object* active_code_marker)
1977 : active_functions_(active_functions),
1978 active_code_marker_(active_code_marker) { }
1979
1980 void VisitThread(Isolate* isolate, ThreadLocalTop* top) {
1981 CollectActiveFunctionsFromThread(isolate,
1982 top,
1983 active_functions_,
1984 active_code_marker_);
1985 }
1986
1987 private:
1988 List<Handle<JSFunction> >* active_functions_;
1989 Object* active_code_marker_;
1990};
1991
1992
1993class ActiveFunctionsRedirector : public ThreadVisitor {
1994 public:
1995 void VisitThread(Isolate* isolate, ThreadLocalTop* top) {
1996 RedirectActivationsToRecompiledCodeOnThread(isolate, top);
1997 }
1998};
1999
2000
lrn@chromium.org34e60782011-09-15 07:25:40 +00002001void Debug::PrepareForBreakPoints() {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002002 // If preparing for the first break point make sure to deoptimize all
2003 // functions as debugging does not work with optimized code.
2004 if (!has_break_points_) {
svenpanne@chromium.org876cca82013-03-18 14:43:20 +00002005 Deoptimizer::DeoptimizeAll(isolate_);
lrn@chromium.org34e60782011-09-15 07:25:40 +00002006
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002007 Handle<Code> lazy_compile =
2008 Handle<Code>(isolate_->builtins()->builtin(Builtins::kLazyCompile));
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002009
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00002010 // There will be at least one break point when we are done.
2011 has_break_points_ = true;
2012
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002013 // Keep the list of activated functions in a handlified list as it
2014 // is used both in GC and non-GC code.
2015 List<Handle<JSFunction> > active_functions(100);
lrn@chromium.org34e60782011-09-15 07:25:40 +00002016
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002017 {
2018 // We are going to iterate heap to find all functions without
2019 // debug break slots.
hpayer@chromium.org7c3372b2013-02-13 17:26:04 +00002020 Heap* heap = isolate_->heap();
2021 heap->CollectAllGarbage(Heap::kMakeHeapIterableMask,
2022 "preparing for breakpoints");
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002023
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002024 // Ensure no GC in this scope as we are going to use gc_metadata
2025 // field in the Code object to mark active functions.
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002026 AssertNoAllocation no_allocation;
2027
hpayer@chromium.org7c3372b2013-02-13 17:26:04 +00002028 Object* active_code_marker = heap->the_hole_value();
svenpanne@chromium.orgecb9dd62011-12-01 08:22:35 +00002029
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002030 CollectActiveFunctionsFromThread(isolate_,
2031 isolate_->thread_local_top(),
2032 &active_functions,
2033 active_code_marker);
2034 ActiveFunctionsCollector active_functions_collector(&active_functions,
2035 active_code_marker);
2036 isolate_->thread_manager()->IterateArchivedThreads(
2037 &active_functions_collector);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002038
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002039 // Scan the heap for all non-optimized functions which have no
2040 // debug break slots and are not active or inlined into an active
2041 // function and mark them for lazy compilation.
hpayer@chromium.org7c3372b2013-02-13 17:26:04 +00002042 HeapIterator iterator(heap);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002043 HeapObject* obj = NULL;
2044 while (((obj = iterator.next()) != NULL)) {
2045 if (obj->IsJSFunction()) {
2046 JSFunction* function = JSFunction::cast(obj);
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002047 SharedFunctionInfo* shared = function->shared();
2048 if (shared->allows_lazy_compilation() &&
2049 shared->script()->IsScript() &&
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002050 function->code()->kind() == Code::FUNCTION &&
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002051 !function->code()->has_debug_break_slots() &&
2052 shared->code()->gc_metadata() != active_code_marker) {
2053 function->set_code(*lazy_compile);
2054 function->shared()->set_code(*lazy_compile);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002055 }
2056 }
lrn@chromium.org34e60782011-09-15 07:25:40 +00002057 }
lrn@chromium.org34e60782011-09-15 07:25:40 +00002058
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002059 // Clear gc_metadata field.
2060 for (int i = 0; i < active_functions.length(); i++) {
2061 Handle<JSFunction> function = active_functions[i];
2062 function->shared()->code()->set_gc_metadata(Smi::FromInt(0));
2063 }
2064 }
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002065
2066 // Now recompile all functions with activation frames and and
2067 // patch the return address to run in the new compiled code.
2068 for (int i = 0; i < active_functions.length(); i++) {
2069 Handle<JSFunction> function = active_functions[i];
mmassi@chromium.org7028c052012-06-13 11:51:58 +00002070 Handle<SharedFunctionInfo> shared(function->shared());
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002071
2072 if (function->code()->kind() == Code::FUNCTION &&
2073 function->code()->has_debug_break_slots()) {
2074 // Nothing to do. Function code already had debug break slots.
2075 continue;
2076 }
2077
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002078 // If recompilation is not possible just skip it.
2079 if (shared->is_toplevel() ||
2080 !shared->allows_lazy_compilation() ||
2081 shared->code()->kind() == Code::BUILTIN) {
2082 continue;
2083 }
2084
2085 // Make sure that the shared full code is compiled with debug
2086 // break slots.
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00002087 if (!shared->code()->has_debug_break_slots()) {
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002088 // Try to compile the full code with debug break slots. If it
2089 // fails just keep the current code.
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00002090 Handle<Code> current_code(function->shared()->code());
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002091 shared->set_code(*lazy_compile);
2092 bool prev_force_debugger_active =
2093 isolate_->debugger()->force_debugger_active();
2094 isolate_->debugger()->set_force_debugger_active(true);
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00002095 ASSERT(current_code->kind() == Code::FUNCTION);
mmassi@chromium.org7028c052012-06-13 11:51:58 +00002096 CompileFullCodeForDebugging(function, current_code);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002097 isolate_->debugger()->set_force_debugger_active(
2098 prev_force_debugger_active);
2099 if (!shared->is_compiled()) {
2100 shared->set_code(*current_code);
2101 continue;
2102 }
2103 }
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002104
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002105 // Keep function code in sync with shared function info.
2106 function->set_code(shared->code());
lrn@chromium.org34e60782011-09-15 07:25:40 +00002107 }
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002108
2109 RedirectActivationsToRecompiledCodeOnThread(isolate_,
2110 isolate_->thread_local_top());
2111
2112 ActiveFunctionsRedirector active_functions_redirector;
2113 isolate_->thread_manager()->IterateArchivedThreads(
2114 &active_functions_redirector);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002115 }
lrn@chromium.org34e60782011-09-15 07:25:40 +00002116}
2117
2118
jkummerow@chromium.org78502a92012-09-06 13:50:42 +00002119Object* Debug::FindSharedFunctionInfoInScript(Handle<Script> script,
2120 int position) {
2121 // Iterate the heap looking for SharedFunctionInfo generated from the
2122 // script. The inner most SharedFunctionInfo containing the source position
2123 // for the requested break point is found.
2124 // NOTE: This might require several heap iterations. If the SharedFunctionInfo
2125 // which is found is not compiled it is compiled and the heap is iterated
2126 // again as the compilation might create inner functions from the newly
2127 // compiled function and the actual requested break point might be in one of
2128 // these functions.
2129 // NOTE: The below fix-point iteration depends on all functions that cannot be
2130 // compiled lazily without a context to not be compiled at all. Compilation
2131 // will be triggered at points where we do not need a context.
2132 bool done = false;
2133 // The current candidate for the source position:
2134 int target_start_position = RelocInfo::kNoPosition;
2135 Handle<JSFunction> target_function;
2136 Handle<SharedFunctionInfo> target;
hpayer@chromium.org7c3372b2013-02-13 17:26:04 +00002137 Heap* heap = isolate_->heap();
jkummerow@chromium.org78502a92012-09-06 13:50:42 +00002138 while (!done) {
2139 { // Extra scope for iterator and no-allocation.
hpayer@chromium.org7c3372b2013-02-13 17:26:04 +00002140 heap->EnsureHeapIsIterable();
jkummerow@chromium.org78502a92012-09-06 13:50:42 +00002141 AssertNoAllocation no_alloc_during_heap_iteration;
hpayer@chromium.org7c3372b2013-02-13 17:26:04 +00002142 HeapIterator iterator(heap);
jkummerow@chromium.org78502a92012-09-06 13:50:42 +00002143 for (HeapObject* obj = iterator.next();
2144 obj != NULL; obj = iterator.next()) {
2145 bool found_next_candidate = false;
2146 Handle<JSFunction> function;
2147 Handle<SharedFunctionInfo> shared;
2148 if (obj->IsJSFunction()) {
2149 function = Handle<JSFunction>(JSFunction::cast(obj));
2150 shared = Handle<SharedFunctionInfo>(function->shared());
2151 ASSERT(shared->allows_lazy_compilation() || shared->is_compiled());
2152 found_next_candidate = true;
2153 } else if (obj->IsSharedFunctionInfo()) {
2154 shared = Handle<SharedFunctionInfo>(SharedFunctionInfo::cast(obj));
2155 // Skip functions that we cannot compile lazily without a context,
2156 // which is not available here, because there is no closure.
2157 found_next_candidate = shared->is_compiled() ||
2158 shared->allows_lazy_compilation_without_context();
2159 }
2160 if (!found_next_candidate) continue;
2161 if (shared->script() == *script) {
2162 // If the SharedFunctionInfo found has the requested script data and
2163 // contains the source position it is a candidate.
2164 int start_position = shared->function_token_position();
2165 if (start_position == RelocInfo::kNoPosition) {
2166 start_position = shared->start_position();
2167 }
2168 if (start_position <= position &&
2169 position <= shared->end_position()) {
2170 // If there is no candidate or this function is within the current
2171 // candidate this is the new candidate.
2172 if (target.is_null()) {
2173 target_start_position = start_position;
2174 target_function = function;
2175 target = shared;
2176 } else {
2177 if (target_start_position == start_position &&
2178 shared->end_position() == target->end_position()) {
2179 // If a top-level function contains only one function
2180 // declaration the source for the top-level and the function
2181 // is the same. In that case prefer the non top-level function.
2182 if (!shared->is_toplevel()) {
2183 target_start_position = start_position;
2184 target_function = function;
2185 target = shared;
2186 }
2187 } else if (target_start_position <= start_position &&
2188 shared->end_position() <= target->end_position()) {
2189 // This containment check includes equality as a function
2190 // inside a top-level function can share either start or end
2191 // position with the top-level function.
2192 target_start_position = start_position;
2193 target_function = function;
2194 target = shared;
2195 }
2196 }
2197 }
2198 }
2199 } // End for loop.
2200 } // End no-allocation scope.
2201
hpayer@chromium.org7c3372b2013-02-13 17:26:04 +00002202 if (target.is_null()) return heap->undefined_value();
jkummerow@chromium.org78502a92012-09-06 13:50:42 +00002203
2204 // There will be at least one break point when we are done.
2205 has_break_points_ = true;
2206
2207 // If the candidate found is compiled we are done.
2208 done = target->is_compiled();
2209 if (!done) {
2210 // If the candidate is not compiled, compile it to reveal any inner
2211 // functions which might contain the requested source position. This
2212 // will compile all inner functions that cannot be compiled without a
2213 // context, because Compiler::BuildFunctionInfo checks whether the
2214 // debugger is active.
2215 if (target_function.is_null()) {
2216 SharedFunctionInfo::CompileLazy(target, KEEP_EXCEPTION);
2217 } else {
2218 JSFunction::CompileLazy(target_function, KEEP_EXCEPTION);
2219 }
2220 }
2221 } // End while loop.
2222
2223 return *target;
2224}
2225
2226
lrn@chromium.org34e60782011-09-15 07:25:40 +00002227// Ensures the debug information is present for shared.
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00002228bool Debug::EnsureDebugInfo(Handle<SharedFunctionInfo> shared,
2229 Handle<JSFunction> function) {
lrn@chromium.org34e60782011-09-15 07:25:40 +00002230 // Return if we already have the debug info for shared.
2231 if (HasDebugInfo(shared)) {
2232 ASSERT(shared->is_compiled());
2233 return true;
2234 }
2235
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00002236 // There will be at least one break point when we are done.
2237 has_break_points_ = true;
2238
2239 // Ensure function is compiled. Return false if this failed.
2240 if (!function.is_null() &&
2241 !JSFunction::EnsureCompiled(function, CLEAR_EXCEPTION)) {
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002242 return false;
2243 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002244
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002245 // Create the debug info object.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002246 Handle<DebugInfo> debug_info = FACTORY->NewDebugInfo(shared);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002247
2248 // Add debug info to the list.
2249 DebugInfoListNode* node = new DebugInfoListNode(*debug_info);
2250 node->set_next(debug_info_list_);
2251 debug_info_list_ = node;
2252
kasper.lundbd3ec4e2008-07-09 11:06:54 +00002253 return true;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002254}
2255
2256
2257void Debug::RemoveDebugInfo(Handle<DebugInfo> debug_info) {
2258 ASSERT(debug_info_list_ != NULL);
2259 // Run through the debug info objects to find this one and remove it.
2260 DebugInfoListNode* prev = NULL;
2261 DebugInfoListNode* current = debug_info_list_;
2262 while (current != NULL) {
2263 if (*current->debug_info() == *debug_info) {
2264 // Unlink from list. If prev is NULL we are looking at the first element.
2265 if (prev == NULL) {
2266 debug_info_list_ = current->next();
2267 } else {
2268 prev->set_next(current->next());
2269 }
lrn@chromium.org7516f052011-03-30 08:52:27 +00002270 current->debug_info()->shared()->set_debug_info(
2271 isolate_->heap()->undefined_value());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002272 delete current;
2273
2274 // If there are no more debug info objects there are not more break
2275 // points.
2276 has_break_points_ = debug_info_list_ != NULL;
2277
2278 return;
2279 }
2280 // Move to next in list.
2281 prev = current;
2282 current = current->next();
2283 }
2284 UNREACHABLE();
2285}
2286
2287
2288void Debug::SetAfterBreakTarget(JavaScriptFrame* frame) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002289 HandleScope scope(isolate_);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002290
lrn@chromium.org34e60782011-09-15 07:25:40 +00002291 PrepareForBreakPoints();
2292
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002293 // Get the executing function in which the debug break occurred.
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00002294 Handle<JSFunction> function(JSFunction::cast(frame->function()));
2295 Handle<SharedFunctionInfo> shared(function->shared());
2296 if (!EnsureDebugInfo(shared, function)) {
kasper.lundbd3ec4e2008-07-09 11:06:54 +00002297 // Return if we failed to retrieve the debug info.
2298 return;
2299 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002300 Handle<DebugInfo> debug_info = GetDebugInfo(shared);
2301 Handle<Code> code(debug_info->code());
2302 Handle<Code> original_code(debug_info->original_code());
2303#ifdef DEBUG
2304 // Get the code which is actually executing.
vegorov@chromium.org74f333b2011-04-06 11:17:46 +00002305 Handle<Code> frame_code(frame->LookupCode());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002306 ASSERT(frame_code.is_identical_to(code));
2307#endif
2308
2309 // Find the call address in the running code. This address holds the call to
2310 // either a DebugBreakXXX or to the debug break return entry code if the
2311 // break point is still active after processing the break point.
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00002312 Address addr = frame->pc() - Assembler::kPatchDebugBreakSlotReturnOffset;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002313
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002314 // Check if the location is at JS exit or debug break slot.
ager@chromium.orga1645e22009-09-09 19:27:10 +00002315 bool at_js_return = false;
2316 bool break_at_js_return_active = false;
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002317 bool at_debug_break_slot = false;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002318 RelocIterator it(debug_info->code());
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002319 while (!it.done() && !at_js_return && !at_debug_break_slot) {
ager@chromium.org236ad962008-09-25 09:45:57 +00002320 if (RelocInfo::IsJSReturn(it.rinfo()->rmode())) {
ager@chromium.orga1645e22009-09-09 19:27:10 +00002321 at_js_return = (it.rinfo()->pc() ==
2322 addr - Assembler::kPatchReturnSequenceAddressOffset);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002323 break_at_js_return_active = it.rinfo()->IsPatchedReturnSequence();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002324 }
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002325 if (RelocInfo::IsDebugBreakSlot(it.rinfo()->rmode())) {
2326 at_debug_break_slot = (it.rinfo()->pc() ==
2327 addr - Assembler::kPatchDebugBreakSlotAddressOffset);
2328 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002329 it.next();
2330 }
2331
2332 // Handle the jump to continue execution after break point depending on the
2333 // break location.
ager@chromium.orga1645e22009-09-09 19:27:10 +00002334 if (at_js_return) {
2335 // If the break point as return is still active jump to the corresponding
2336 // place in the original code. If not the break point was removed during
2337 // break point processing.
2338 if (break_at_js_return_active) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002339 addr += original_code->instruction_start() - code->instruction_start();
2340 }
2341
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00002342 // Move back to where the call instruction sequence started.
2343 thread_local_.after_break_target_ =
2344 addr - Assembler::kPatchReturnSequenceAddressOffset;
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002345 } else if (at_debug_break_slot) {
2346 // Address of where the debug break slot starts.
2347 addr = addr - Assembler::kPatchDebugBreakSlotAddressOffset;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002348
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002349 // Continue just after the slot.
2350 thread_local_.after_break_target_ = addr + Assembler::kDebugBreakSlotLength;
2351 } else if (IsDebugBreak(Assembler::target_address_at(addr))) {
2352 // We now know that there is still a debug break call at the target address,
2353 // so the break point is still there and the original code will hold the
2354 // address to jump to in order to complete the call which is replaced by a
2355 // call to DebugBreakXXX.
2356
2357 // Find the corresponding address in the original code.
2358 addr += original_code->instruction_start() - code->instruction_start();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002359
2360 // Install jump to the call address in the original code. This will be the
2361 // call which was overwritten by the call to DebugBreakXXX.
2362 thread_local_.after_break_target_ = Assembler::target_address_at(addr);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002363 } else {
2364 // There is no longer a break point present. Don't try to look in the
2365 // original code as the running code will have the right address. This takes
2366 // care of the case where the last break point is removed from the function
2367 // and therefore no "original code" is available.
2368 thread_local_.after_break_target_ = Assembler::target_address_at(addr);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002369 }
2370}
2371
2372
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00002373bool Debug::IsBreakAtReturn(JavaScriptFrame* frame) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002374 HandleScope scope(isolate_);
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00002375
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00002376 // If there are no break points this cannot be break at return, as
2377 // the debugger statement and stack guard bebug break cannot be at
2378 // return.
2379 if (!has_break_points_) {
2380 return false;
2381 }
2382
lrn@chromium.org34e60782011-09-15 07:25:40 +00002383 PrepareForBreakPoints();
2384
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00002385 // Get the executing function in which the debug break occurred.
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00002386 Handle<JSFunction> function(JSFunction::cast(frame->function()));
2387 Handle<SharedFunctionInfo> shared(function->shared());
2388 if (!EnsureDebugInfo(shared, function)) {
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00002389 // Return if we failed to retrieve the debug info.
2390 return false;
2391 }
2392 Handle<DebugInfo> debug_info = GetDebugInfo(shared);
2393 Handle<Code> code(debug_info->code());
2394#ifdef DEBUG
2395 // Get the code which is actually executing.
vegorov@chromium.org74f333b2011-04-06 11:17:46 +00002396 Handle<Code> frame_code(frame->LookupCode());
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00002397 ASSERT(frame_code.is_identical_to(code));
2398#endif
2399
2400 // Find the call address in the running code.
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00002401 Address addr = frame->pc() - Assembler::kPatchDebugBreakSlotReturnOffset;
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00002402
2403 // Check if the location is at JS return.
2404 RelocIterator it(debug_info->code());
2405 while (!it.done()) {
2406 if (RelocInfo::IsJSReturn(it.rinfo()->rmode())) {
2407 return (it.rinfo()->pc() ==
2408 addr - Assembler::kPatchReturnSequenceAddressOffset);
2409 }
2410 it.next();
2411 }
2412 return false;
2413}
2414
2415
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00002416void Debug::FramesHaveBeenDropped(StackFrame::Id new_break_frame_id,
whesse@chromium.orge90029b2010-08-02 11:52:17 +00002417 FrameDropMode mode,
2418 Object** restarter_frame_function_pointer) {
ulan@chromium.orgd9e468a2012-06-25 09:47:40 +00002419 if (mode != CURRENTLY_SET_MODE) {
2420 thread_local_.frame_drop_mode_ = mode;
2421 }
ager@chromium.org357bf652010-04-12 11:30:10 +00002422 thread_local_.break_frame_id_ = new_break_frame_id;
whesse@chromium.orge90029b2010-08-02 11:52:17 +00002423 thread_local_.restarter_frame_function_pointer_ =
2424 restarter_frame_function_pointer;
ager@chromium.org357bf652010-04-12 11:30:10 +00002425}
2426
2427
jkummerow@chromium.org212d9642012-05-11 15:02:09 +00002428const int Debug::FramePaddingLayout::kInitialSize = 1;
2429
2430
2431// Any even value bigger than kInitialSize as needed for stack scanning.
2432const int Debug::FramePaddingLayout::kPaddingValue = kInitialSize + 1;
2433
2434
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002435bool Debug::IsDebugGlobal(GlobalObject* global) {
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00002436 return IsLoaded() && global == debug_context()->global_object();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002437}
2438
2439
ager@chromium.org32912102009-01-16 10:38:43 +00002440void Debug::ClearMirrorCache() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002441 PostponeInterruptsScope postpone(isolate_);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002442 HandleScope scope(isolate_);
2443 ASSERT(isolate_->context() == *Debug::debug_context());
ager@chromium.org32912102009-01-16 10:38:43 +00002444
2445 // Clear the mirror cache.
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00002446 Handle<String> function_name = isolate_->factory()->InternalizeOneByteString(
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00002447 STATIC_ASCII_VECTOR("ClearMirrorCache"));
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00002448 Handle<Object> fun(
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00002449 isolate_->global_object()->GetPropertyNoExceptionThrown(*function_name),
2450 isolate_);
ager@chromium.org32912102009-01-16 10:38:43 +00002451 ASSERT(fun->IsJSFunction());
2452 bool caught_exception;
rossberg@chromium.org717967f2011-07-20 13:44:42 +00002453 Execution::TryCall(Handle<JSFunction>::cast(fun),
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00002454 Handle<JSObject>(Debug::debug_context()->global_object()),
ager@chromium.org32912102009-01-16 10:38:43 +00002455 0, NULL, &caught_exception);
2456}
2457
2458
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002459void Debug::CreateScriptCache() {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002460 Heap* heap = isolate_->heap();
2461 HandleScope scope(isolate_);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002462
2463 // Perform two GCs to get rid of all unreferenced scripts. The first GC gets
2464 // rid of all the cached script wrappers and the second gets rid of the
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002465 // scripts which are no longer referenced. The second also sweeps precisely,
2466 // which saves us doing yet another GC to make the heap iterable.
rossberg@chromium.org994edf62012-02-06 10:12:55 +00002467 heap->CollectAllGarbage(Heap::kNoGCFlags, "Debug::CreateScriptCache");
2468 heap->CollectAllGarbage(Heap::kMakeHeapIterableMask,
2469 "Debug::CreateScriptCache");
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002470
2471 ASSERT(script_cache_ == NULL);
2472 script_cache_ = new ScriptCache();
2473
2474 // Scan heap for Script objects.
2475 int count = 0;
hpayer@chromium.org7c3372b2013-02-13 17:26:04 +00002476 HeapIterator iterator(heap);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002477 AssertNoAllocation no_allocation;
2478
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002479 for (HeapObject* obj = iterator.next(); obj != NULL; obj = iterator.next()) {
sgjesse@chromium.org152a0b02009-10-07 13:50:16 +00002480 if (obj->IsScript() && Script::cast(obj)->HasValidSource()) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002481 script_cache_->Add(Handle<Script>(Script::cast(obj)));
2482 count++;
2483 }
2484 }
2485}
2486
2487
2488void Debug::DestroyScriptCache() {
2489 // Get rid of the script cache if it was created.
2490 if (script_cache_ != NULL) {
2491 delete script_cache_;
2492 script_cache_ = NULL;
2493 }
2494}
2495
2496
2497void Debug::AddScriptToScriptCache(Handle<Script> script) {
2498 if (script_cache_ != NULL) {
2499 script_cache_->Add(script);
2500 }
2501}
2502
2503
2504Handle<FixedArray> Debug::GetLoadedScripts() {
2505 // Create and fill the script cache when the loaded scripts is requested for
2506 // the first time.
2507 if (script_cache_ == NULL) {
2508 CreateScriptCache();
2509 }
2510
2511 // If the script cache is not active just return an empty array.
2512 ASSERT(script_cache_ != NULL);
2513 if (script_cache_ == NULL) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002514 isolate_->factory()->NewFixedArray(0);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002515 }
2516
2517 // Perform GC to get unreferenced scripts evicted from the cache before
2518 // returning the content.
rossberg@chromium.org994edf62012-02-06 10:12:55 +00002519 isolate_->heap()->CollectAllGarbage(Heap::kNoGCFlags,
2520 "Debug::GetLoadedScripts");
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002521
2522 // Get the scripts from the cache.
2523 return script_cache_->GetScripts();
2524}
2525
2526
2527void Debug::AfterGarbageCollection() {
2528 // Generate events for collected scripts.
2529 if (script_cache_ != NULL) {
2530 script_cache_->ProcessCollectedScripts();
2531 }
2532}
2533
2534
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00002535Debugger::Debugger(Isolate* isolate)
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +00002536 : debugger_access_(isolate->debugger_access()),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002537 event_listener_(Handle<Object>()),
2538 event_listener_data_(Handle<Object>()),
2539 compiling_natives_(false),
2540 is_loading_debugger_(false),
mstarzinger@chromium.orgde886792012-09-11 13:22:37 +00002541 live_edit_enabled_(true),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002542 never_unload_debugger_(false),
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002543 force_debugger_active_(false),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002544 message_handler_(NULL),
2545 debugger_unload_pending_(false),
2546 host_dispatch_handler_(NULL),
2547 dispatch_handler_access_(OS::CreateMutex()),
2548 debug_message_dispatch_handler_(NULL),
2549 message_dispatch_helper_thread_(NULL),
2550 host_dispatch_micros_(100 * 1000),
2551 agent_(NULL),
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00002552 command_queue_(isolate->logger(), kQueueInitialSize),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002553 command_received_(OS::CreateSemaphore(0)),
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00002554 event_command_queue_(isolate->logger(), kQueueInitialSize),
2555 isolate_(isolate) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002556}
2557
2558
2559Debugger::~Debugger() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002560 delete dispatch_handler_access_;
2561 dispatch_handler_access_ = 0;
2562 delete command_received_;
2563 command_received_ = 0;
2564}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002565
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002566
2567Handle<Object> Debugger::MakeJSObject(Vector<const char> constructor_name,
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002568 int argc,
2569 Handle<Object> argv[],
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002570 bool* caught_exception) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002571 ASSERT(isolate_->context() == *isolate_->debug()->debug_context());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002572
2573 // Create the execution state object.
lrn@chromium.org7516f052011-03-30 08:52:27 +00002574 Handle<String> constructor_str =
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00002575 isolate_->factory()->InternalizeUtf8String(constructor_name);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002576 Handle<Object> constructor(
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00002577 isolate_->global_object()->GetPropertyNoExceptionThrown(*constructor_str),
2578 isolate_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002579 ASSERT(constructor->IsJSFunction());
2580 if (!constructor->IsJSFunction()) {
2581 *caught_exception = true;
lrn@chromium.org7516f052011-03-30 08:52:27 +00002582 return isolate_->factory()->undefined_value();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002583 }
2584 Handle<Object> js_object = Execution::TryCall(
2585 Handle<JSFunction>::cast(constructor),
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00002586 Handle<JSObject>(isolate_->debug()->debug_context()->global_object()),
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002587 argc,
2588 argv,
2589 caught_exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002590 return js_object;
2591}
2592
2593
2594Handle<Object> Debugger::MakeExecutionState(bool* caught_exception) {
2595 // Create the execution state object.
lrn@chromium.org7516f052011-03-30 08:52:27 +00002596 Handle<Object> break_id = isolate_->factory()->NewNumberFromInt(
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002597 isolate_->debug()->break_id());
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002598 Handle<Object> argv[] = { break_id };
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002599 return MakeJSObject(CStrVector("MakeExecutionState"),
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002600 ARRAY_SIZE(argv),
2601 argv,
2602 caught_exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002603}
2604
2605
2606Handle<Object> Debugger::MakeBreakEvent(Handle<Object> exec_state,
2607 Handle<Object> break_points_hit,
2608 bool* caught_exception) {
2609 // Create the new break event object.
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002610 Handle<Object> argv[] = { exec_state, break_points_hit };
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002611 return MakeJSObject(CStrVector("MakeBreakEvent"),
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002612 ARRAY_SIZE(argv),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002613 argv,
2614 caught_exception);
2615}
2616
2617
2618Handle<Object> Debugger::MakeExceptionEvent(Handle<Object> exec_state,
2619 Handle<Object> exception,
2620 bool uncaught,
2621 bool* caught_exception) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002622 Factory* factory = isolate_->factory();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002623 // Create the new exception event object.
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002624 Handle<Object> argv[] = { exec_state,
2625 exception,
2626 factory->ToBoolean(uncaught) };
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002627 return MakeJSObject(CStrVector("MakeExceptionEvent"),
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002628 ARRAY_SIZE(argv),
2629 argv,
2630 caught_exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002631}
2632
2633
2634Handle<Object> Debugger::MakeNewFunctionEvent(Handle<Object> function,
2635 bool* caught_exception) {
2636 // Create the new function event object.
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002637 Handle<Object> argv[] = { function };
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002638 return MakeJSObject(CStrVector("MakeNewFunctionEvent"),
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002639 ARRAY_SIZE(argv),
2640 argv,
2641 caught_exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002642}
2643
2644
2645Handle<Object> Debugger::MakeCompileEvent(Handle<Script> script,
iposva@chromium.org245aa852009-02-10 00:49:54 +00002646 bool before,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002647 bool* caught_exception) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002648 Factory* factory = isolate_->factory();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002649 // Create the compile event object.
2650 Handle<Object> exec_state = MakeExecutionState(caught_exception);
iposva@chromium.org245aa852009-02-10 00:49:54 +00002651 Handle<Object> script_wrapper = GetScriptWrapper(script);
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002652 Handle<Object> argv[] = { exec_state,
2653 script_wrapper,
2654 factory->ToBoolean(before) };
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002655 return MakeJSObject(CStrVector("MakeCompileEvent"),
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002656 ARRAY_SIZE(argv),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002657 argv,
2658 caught_exception);
2659}
2660
2661
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002662Handle<Object> Debugger::MakeScriptCollectedEvent(int id,
2663 bool* caught_exception) {
2664 // Create the script collected event object.
2665 Handle<Object> exec_state = MakeExecutionState(caught_exception);
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00002666 Handle<Object> id_object = Handle<Smi>(Smi::FromInt(id), isolate_);
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002667 Handle<Object> argv[] = { exec_state, id_object };
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002668
2669 return MakeJSObject(CStrVector("MakeScriptCollectedEvent"),
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002670 ARRAY_SIZE(argv),
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002671 argv,
2672 caught_exception);
2673}
2674
2675
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002676void Debugger::OnException(Handle<Object> exception, bool uncaught) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002677 HandleScope scope(isolate_);
2678 Debug* debug = isolate_->debug();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002679
2680 // Bail out based on state or if there is no listener for this event
lrn@chromium.org7516f052011-03-30 08:52:27 +00002681 if (debug->InDebugger()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002682 if (!Debugger::EventActive(v8::Exception)) return;
2683
2684 // Bail out if exception breaks are not active
2685 if (uncaught) {
2686 // Uncaught exceptions are reported by either flags.
lrn@chromium.org7516f052011-03-30 08:52:27 +00002687 if (!(debug->break_on_uncaught_exception() ||
2688 debug->break_on_exception())) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002689 } else {
2690 // Caught exceptions are reported is activated.
lrn@chromium.org7516f052011-03-30 08:52:27 +00002691 if (!debug->break_on_exception()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002692 }
2693
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002694 // Enter the debugger.
2695 EnterDebugger debugger;
2696 if (debugger.FailedToEnter()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002697
2698 // Clear all current stepping setup.
lrn@chromium.org7516f052011-03-30 08:52:27 +00002699 debug->ClearStepping();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002700 // Create the event data object.
2701 bool caught_exception = false;
2702 Handle<Object> exec_state = MakeExecutionState(&caught_exception);
2703 Handle<Object> event_data;
2704 if (!caught_exception) {
2705 event_data = MakeExceptionEvent(exec_state, exception, uncaught,
2706 &caught_exception);
2707 }
2708 // Bail out and don't call debugger if exception.
2709 if (caught_exception) {
2710 return;
2711 }
2712
ager@chromium.org5ec48922009-05-05 07:25:34 +00002713 // Process debug event.
2714 ProcessDebugEvent(v8::Exception, Handle<JSObject>::cast(event_data), false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002715 // Return to continue execution from where the exception was thrown.
2716}
2717
2718
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002719void Debugger::OnDebugBreak(Handle<Object> break_points_hit,
2720 bool auto_continue) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002721 HandleScope scope(isolate_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002722
kasper.lund212ac232008-07-16 07:07:30 +00002723 // Debugger has already been entered by caller.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002724 ASSERT(isolate_->context() == *isolate_->debug()->debug_context());
kasper.lund212ac232008-07-16 07:07:30 +00002725
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002726 // Bail out if there is no listener for this event
2727 if (!Debugger::EventActive(v8::Break)) return;
2728
2729 // Debugger must be entered in advance.
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00002730 ASSERT(isolate_->context() == *isolate_->debug()->debug_context());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002731
2732 // Create the event data object.
2733 bool caught_exception = false;
2734 Handle<Object> exec_state = MakeExecutionState(&caught_exception);
2735 Handle<Object> event_data;
2736 if (!caught_exception) {
2737 event_data = MakeBreakEvent(exec_state, break_points_hit,
2738 &caught_exception);
2739 }
2740 // Bail out and don't call debugger if exception.
2741 if (caught_exception) {
2742 return;
2743 }
2744
ager@chromium.org5ec48922009-05-05 07:25:34 +00002745 // Process debug event.
2746 ProcessDebugEvent(v8::Break,
2747 Handle<JSObject>::cast(event_data),
2748 auto_continue);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002749}
2750
2751
2752void Debugger::OnBeforeCompile(Handle<Script> script) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002753 HandleScope scope(isolate_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002754
2755 // Bail out based on state or if there is no listener for this event
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002756 if (isolate_->debug()->InDebugger()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002757 if (compiling_natives()) return;
2758 if (!EventActive(v8::BeforeCompile)) return;
2759
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002760 // Enter the debugger.
2761 EnterDebugger debugger;
2762 if (debugger.FailedToEnter()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002763
2764 // Create the event data object.
2765 bool caught_exception = false;
iposva@chromium.org245aa852009-02-10 00:49:54 +00002766 Handle<Object> event_data = MakeCompileEvent(script, true, &caught_exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002767 // Bail out and don't call debugger if exception.
2768 if (caught_exception) {
2769 return;
2770 }
2771
ager@chromium.org5ec48922009-05-05 07:25:34 +00002772 // Process debug event.
2773 ProcessDebugEvent(v8::BeforeCompile,
2774 Handle<JSObject>::cast(event_data),
2775 true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002776}
2777
2778
2779// Handle debugger actions when a new script is compiled.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00002780void Debugger::OnAfterCompile(Handle<Script> script,
2781 AfterCompileFlags after_compile_flags) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002782 HandleScope scope(isolate_);
2783 Debug* debug = isolate_->debug();
kasper.lund212ac232008-07-16 07:07:30 +00002784
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002785 // Add the newly compiled script to the script cache.
lrn@chromium.org7516f052011-03-30 08:52:27 +00002786 debug->AddScriptToScriptCache(script);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002787
2788 // No more to do if not debugging.
ager@chromium.org71daaf62009-04-01 07:22:49 +00002789 if (!IsDebuggerActive()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002790
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002791 // No compile events while compiling natives.
2792 if (compiling_natives()) return;
2793
iposva@chromium.org245aa852009-02-10 00:49:54 +00002794 // Store whether in debugger before entering debugger.
lrn@chromium.org7516f052011-03-30 08:52:27 +00002795 bool in_debugger = debug->InDebugger();
iposva@chromium.org245aa852009-02-10 00:49:54 +00002796
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002797 // Enter the debugger.
2798 EnterDebugger debugger;
2799 if (debugger.FailedToEnter()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002800
2801 // If debugging there might be script break points registered for this
2802 // script. Make sure that these break points are set.
2803
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002804 // Get the function UpdateScriptBreakPoints (defined in debug-debugger.js).
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00002805 Handle<String> update_script_break_points_string =
2806 isolate_->factory()->InternalizeOneByteString(
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00002807 STATIC_ASCII_VECTOR("UpdateScriptBreakPoints"));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002808 Handle<Object> update_script_break_points =
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00002809 Handle<Object>(
2810 debug->debug_context()->global_object()->GetPropertyNoExceptionThrown(
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00002811 *update_script_break_points_string),
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00002812 isolate_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002813 if (!update_script_break_points->IsJSFunction()) {
2814 return;
2815 }
2816 ASSERT(update_script_break_points->IsJSFunction());
2817
2818 // Wrap the script object in a proper JS object before passing it
2819 // to JavaScript.
2820 Handle<JSValue> wrapper = GetScriptWrapper(script);
2821
2822 // Call UpdateScriptBreakPoints expect no exceptions.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002823 bool caught_exception;
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002824 Handle<Object> argv[] = { wrapper };
rossberg@chromium.org717967f2011-07-20 13:44:42 +00002825 Execution::TryCall(Handle<JSFunction>::cast(update_script_break_points),
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002826 Isolate::Current()->js_builtins_object(),
2827 ARRAY_SIZE(argv),
2828 argv,
2829 &caught_exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002830 if (caught_exception) {
2831 return;
2832 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002833 // Bail out based on state or if there is no listener for this event
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00002834 if (in_debugger && (after_compile_flags & SEND_WHEN_DEBUGGING) == 0) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002835 if (!Debugger::EventActive(v8::AfterCompile)) return;
2836
2837 // Create the compile state object.
2838 Handle<Object> event_data = MakeCompileEvent(script,
iposva@chromium.org245aa852009-02-10 00:49:54 +00002839 false,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002840 &caught_exception);
2841 // Bail out and don't call debugger if exception.
2842 if (caught_exception) {
2843 return;
2844 }
ager@chromium.org5ec48922009-05-05 07:25:34 +00002845 // Process debug event.
2846 ProcessDebugEvent(v8::AfterCompile,
2847 Handle<JSObject>::cast(event_data),
2848 true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002849}
2850
2851
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002852void Debugger::OnScriptCollected(int id) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002853 HandleScope scope(isolate_);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002854
2855 // No more to do if not debugging.
ulan@chromium.org56c14af2012-09-20 12:51:09 +00002856 if (isolate_->debug()->InDebugger()) return;
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002857 if (!IsDebuggerActive()) return;
2858 if (!Debugger::EventActive(v8::ScriptCollected)) return;
2859
2860 // Enter the debugger.
2861 EnterDebugger debugger;
2862 if (debugger.FailedToEnter()) return;
2863
2864 // Create the script collected state object.
2865 bool caught_exception = false;
2866 Handle<Object> event_data = MakeScriptCollectedEvent(id,
2867 &caught_exception);
2868 // Bail out and don't call debugger if exception.
2869 if (caught_exception) {
2870 return;
2871 }
2872
2873 // Process debug event.
2874 ProcessDebugEvent(v8::ScriptCollected,
2875 Handle<JSObject>::cast(event_data),
2876 true);
2877}
2878
2879
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002880void Debugger::ProcessDebugEvent(v8::DebugEvent event,
ager@chromium.org5ec48922009-05-05 07:25:34 +00002881 Handle<JSObject> event_data,
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002882 bool auto_continue) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002883 HandleScope scope(isolate_);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002884
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +00002885 // Clear any pending debug break if this is a real break.
2886 if (!auto_continue) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002887 isolate_->debug()->clear_interrupt_pending(DEBUGBREAK);
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +00002888 }
2889
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002890 // Create the execution state.
2891 bool caught_exception = false;
2892 Handle<Object> exec_state = MakeExecutionState(&caught_exception);
2893 if (caught_exception) {
2894 return;
2895 }
ager@chromium.org41826e72009-03-30 13:30:57 +00002896 // First notify the message handler if any.
2897 if (message_handler_ != NULL) {
ager@chromium.org5ec48922009-05-05 07:25:34 +00002898 NotifyMessageHandler(event,
2899 Handle<JSObject>::cast(exec_state),
2900 event_data,
2901 auto_continue);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002902 }
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00002903 // Notify registered debug event listener. This can be either a C or
2904 // a JavaScript function. Don't call event listener for v8::Break
2905 // here, if it's only a debug command -- they will be processed later.
2906 if ((event != v8::Break || !auto_continue) && !event_listener_.is_null()) {
2907 CallEventCallback(event, exec_state, event_data, NULL);
2908 }
2909 // Process pending debug commands.
2910 if (event == v8::Break) {
2911 while (!event_command_queue_.IsEmpty()) {
2912 CommandMessage command = event_command_queue_.Get();
2913 if (!event_listener_.is_null()) {
2914 CallEventCallback(v8::BreakForCommand,
2915 exec_state,
2916 event_data,
2917 command.client_data());
2918 }
2919 command.Dispose();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002920 }
2921 }
2922}
2923
2924
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00002925void Debugger::CallEventCallback(v8::DebugEvent event,
2926 Handle<Object> exec_state,
2927 Handle<Object> event_data,
2928 v8::Debug::ClientData* client_data) {
ager@chromium.orgea91cc52011-05-23 06:06:11 +00002929 if (event_listener_->IsForeign()) {
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00002930 CallCEventCallback(event, exec_state, event_data, client_data);
2931 } else {
2932 CallJSEventCallback(event, exec_state, event_data);
2933 }
2934}
2935
2936
2937void Debugger::CallCEventCallback(v8::DebugEvent event,
2938 Handle<Object> exec_state,
2939 Handle<Object> event_data,
2940 v8::Debug::ClientData* client_data) {
ager@chromium.orgea91cc52011-05-23 06:06:11 +00002941 Handle<Foreign> callback_obj(Handle<Foreign>::cast(event_listener_));
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00002942 v8::Debug::EventCallback2 callback =
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002943 FUNCTION_CAST<v8::Debug::EventCallback2>(
2944 callback_obj->foreign_address());
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00002945 EventDetailsImpl event_details(
2946 event,
2947 Handle<JSObject>::cast(exec_state),
2948 Handle<JSObject>::cast(event_data),
2949 event_listener_data_,
2950 client_data);
2951 callback(event_details);
2952}
2953
2954
2955void Debugger::CallJSEventCallback(v8::DebugEvent event,
2956 Handle<Object> exec_state,
2957 Handle<Object> event_data) {
2958 ASSERT(event_listener_->IsJSFunction());
2959 Handle<JSFunction> fun(Handle<JSFunction>::cast(event_listener_));
2960
2961 // Invoke the JavaScript debug event listener.
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00002962 Handle<Object> argv[] = { Handle<Object>(Smi::FromInt(event), isolate_),
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002963 exec_state,
2964 event_data,
2965 event_listener_data_ };
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002966 bool caught_exception;
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002967 Execution::TryCall(fun,
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00002968 isolate_->global_object(),
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002969 ARRAY_SIZE(argv),
2970 argv,
2971 &caught_exception);
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00002972 // Silently ignore exceptions from debug event listeners.
2973}
2974
2975
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00002976Handle<Context> Debugger::GetDebugContext() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002977 never_unload_debugger_ = true;
2978 EnterDebugger debugger;
2979 return isolate_->debug()->debug_context();
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00002980}
2981
2982
ager@chromium.org71daaf62009-04-01 07:22:49 +00002983void Debugger::UnloadDebugger() {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002984 Debug* debug = isolate_->debug();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002985
ager@chromium.org71daaf62009-04-01 07:22:49 +00002986 // Make sure that there are no breakpoints left.
lrn@chromium.org7516f052011-03-30 08:52:27 +00002987 debug->ClearAllBreakPoints();
ager@chromium.org71daaf62009-04-01 07:22:49 +00002988
2989 // Unload the debugger if feasible.
2990 if (!never_unload_debugger_) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002991 debug->Unload();
ager@chromium.org71daaf62009-04-01 07:22:49 +00002992 }
2993
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002994 // Clear the flag indicating that the debugger should be unloaded.
2995 debugger_unload_pending_ = false;
ager@chromium.org71daaf62009-04-01 07:22:49 +00002996}
2997
2998
ager@chromium.org41826e72009-03-30 13:30:57 +00002999void Debugger::NotifyMessageHandler(v8::DebugEvent event,
ager@chromium.org5ec48922009-05-05 07:25:34 +00003000 Handle<JSObject> exec_state,
3001 Handle<JSObject> event_data,
ager@chromium.org41826e72009-03-30 13:30:57 +00003002 bool auto_continue) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00003003 HandleScope scope(isolate_);
ager@chromium.org41826e72009-03-30 13:30:57 +00003004
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003005 if (!isolate_->debug()->Load()) return;
ager@chromium.org41826e72009-03-30 13:30:57 +00003006
3007 // Process the individual events.
ager@chromium.org5ec48922009-05-05 07:25:34 +00003008 bool sendEventMessage = false;
ager@chromium.org41826e72009-03-30 13:30:57 +00003009 switch (event) {
3010 case v8::Break:
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00003011 case v8::BreakForCommand:
ager@chromium.org5ec48922009-05-05 07:25:34 +00003012 sendEventMessage = !auto_continue;
ager@chromium.org41826e72009-03-30 13:30:57 +00003013 break;
3014 case v8::Exception:
ager@chromium.org5ec48922009-05-05 07:25:34 +00003015 sendEventMessage = true;
ager@chromium.org41826e72009-03-30 13:30:57 +00003016 break;
3017 case v8::BeforeCompile:
3018 break;
3019 case v8::AfterCompile:
ager@chromium.org5ec48922009-05-05 07:25:34 +00003020 sendEventMessage = true;
ager@chromium.org41826e72009-03-30 13:30:57 +00003021 break;
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003022 case v8::ScriptCollected:
3023 sendEventMessage = true;
3024 break;
ager@chromium.org41826e72009-03-30 13:30:57 +00003025 case v8::NewFunction:
3026 break;
3027 default:
3028 UNREACHABLE();
3029 }
3030
ager@chromium.org5ec48922009-05-05 07:25:34 +00003031 // The debug command interrupt flag might have been set when the command was
3032 // added. It should be enough to clear the flag only once while we are in the
3033 // debugger.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003034 ASSERT(isolate_->debug()->InDebugger());
3035 isolate_->stack_guard()->Continue(DEBUGCOMMAND);
ager@chromium.org5ec48922009-05-05 07:25:34 +00003036
3037 // Notify the debugger that a debug event has occurred unless auto continue is
3038 // active in which case no event is send.
3039 if (sendEventMessage) {
3040 MessageImpl message = MessageImpl::NewEvent(
3041 event,
3042 auto_continue,
3043 Handle<JSObject>::cast(exec_state),
3044 Handle<JSObject>::cast(event_data));
3045 InvokeMessageHandler(message);
3046 }
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +00003047
3048 // If auto continue don't make the event cause a break, but process messages
3049 // in the queue if any. For script collected events don't even process
3050 // messages in the queue as the execution state might not be what is expected
3051 // by the client.
ager@chromium.org6ffc2172009-05-29 19:20:16 +00003052 if ((auto_continue && !HasCommands()) || event == v8::ScriptCollected) {
ager@chromium.org5ec48922009-05-05 07:25:34 +00003053 return;
3054 }
ager@chromium.org41826e72009-03-30 13:30:57 +00003055
ager@chromium.org41826e72009-03-30 13:30:57 +00003056 v8::TryCatch try_catch;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003057
3058 // DebugCommandProcessor goes here.
3059 v8::Local<v8::Object> cmd_processor;
3060 {
3061 v8::Local<v8::Object> api_exec_state =
3062 v8::Utils::ToLocal(Handle<JSObject>::cast(exec_state));
3063 v8::Local<v8::String> fun_name =
3064 v8::String::New("debugCommandProcessor");
3065 v8::Local<v8::Function> fun =
3066 v8::Function::Cast(*api_exec_state->Get(fun_name));
3067
3068 v8::Handle<v8::Boolean> running =
3069 auto_continue ? v8::True() : v8::False();
3070 static const int kArgc = 1;
3071 v8::Handle<Value> argv[kArgc] = { running };
3072 cmd_processor = v8::Object::Cast(*fun->Call(api_exec_state, kArgc, argv));
3073 if (try_catch.HasCaught()) {
3074 PrintLn(try_catch.Exception());
3075 return;
3076 }
ager@chromium.org41826e72009-03-30 13:30:57 +00003077 }
3078
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003079 bool running = auto_continue;
3080
ager@chromium.org41826e72009-03-30 13:30:57 +00003081 // Process requests from the debugger.
3082 while (true) {
3083 // Wait for new command in the queue.
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003084 if (Debugger::host_dispatch_handler_) {
3085 // In case there is a host dispatch - do periodic dispatches.
3086 if (!command_received_->Wait(host_dispatch_micros_)) {
3087 // Timout expired, do the dispatch.
3088 Debugger::host_dispatch_handler_();
3089 continue;
3090 }
3091 } else {
3092 // In case there is no host dispatch - just wait.
3093 command_received_->Wait();
3094 }
ager@chromium.org41826e72009-03-30 13:30:57 +00003095
ager@chromium.org41826e72009-03-30 13:30:57 +00003096 // Get the command from the queue.
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003097 CommandMessage command = command_queue_.Get();
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00003098 isolate_->logger()->DebugTag(
3099 "Got request from command queue, in interactive loop.");
ager@chromium.org71daaf62009-04-01 07:22:49 +00003100 if (!Debugger::IsDebuggerActive()) {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003101 // Delete command text and user data.
3102 command.Dispose();
ager@chromium.org41826e72009-03-30 13:30:57 +00003103 return;
3104 }
3105
ager@chromium.org41826e72009-03-30 13:30:57 +00003106 // Invoke JavaScript to process the debug request.
3107 v8::Local<v8::String> fun_name;
3108 v8::Local<v8::Function> fun;
3109 v8::Local<v8::Value> request;
3110 v8::TryCatch try_catch;
3111 fun_name = v8::String::New("processDebugRequest");
3112 fun = v8::Function::Cast(*cmd_processor->Get(fun_name));
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003113
3114 request = v8::String::New(command.text().start(),
3115 command.text().length());
ager@chromium.org41826e72009-03-30 13:30:57 +00003116 static const int kArgc = 1;
3117 v8::Handle<Value> argv[kArgc] = { request };
3118 v8::Local<v8::Value> response_val = fun->Call(cmd_processor, kArgc, argv);
3119
3120 // Get the response.
3121 v8::Local<v8::String> response;
ager@chromium.org41826e72009-03-30 13:30:57 +00003122 if (!try_catch.HasCaught()) {
3123 // Get response string.
3124 if (!response_val->IsUndefined()) {
3125 response = v8::String::Cast(*response_val);
3126 } else {
3127 response = v8::String::New("");
3128 }
3129
3130 // Log the JSON request/response.
3131 if (FLAG_trace_debug_json) {
3132 PrintLn(request);
3133 PrintLn(response);
3134 }
3135
3136 // Get the running state.
3137 fun_name = v8::String::New("isRunning");
3138 fun = v8::Function::Cast(*cmd_processor->Get(fun_name));
3139 static const int kArgc = 1;
3140 v8::Handle<Value> argv[kArgc] = { response };
3141 v8::Local<v8::Value> running_val = fun->Call(cmd_processor, kArgc, argv);
3142 if (!try_catch.HasCaught()) {
3143 running = running_val->ToBoolean()->Value();
3144 }
3145 } else {
3146 // In case of failure the result text is the exception text.
3147 response = try_catch.Exception()->ToString();
3148 }
3149
ager@chromium.org41826e72009-03-30 13:30:57 +00003150 // Return the result.
ager@chromium.org5ec48922009-05-05 07:25:34 +00003151 MessageImpl message = MessageImpl::NewResponse(
3152 event,
3153 running,
3154 Handle<JSObject>::cast(exec_state),
3155 Handle<JSObject>::cast(event_data),
3156 Handle<String>(Utils::OpenHandle(*response)),
3157 command.client_data());
3158 InvokeMessageHandler(message);
3159 command.Dispose();
ager@chromium.org41826e72009-03-30 13:30:57 +00003160
3161 // Return from debug event processing if either the VM is put into the
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00003162 // running state (through a continue command) or auto continue is active
ager@chromium.org41826e72009-03-30 13:30:57 +00003163 // and there are no more commands queued.
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003164 if (running && !HasCommands()) {
ager@chromium.org41826e72009-03-30 13:30:57 +00003165 return;
3166 }
3167 }
3168}
3169
3170
iposva@chromium.org245aa852009-02-10 00:49:54 +00003171void Debugger::SetEventListener(Handle<Object> callback,
3172 Handle<Object> data) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00003173 HandleScope scope(isolate_);
3174 GlobalHandles* global_handles = isolate_->global_handles();
iposva@chromium.org245aa852009-02-10 00:49:54 +00003175
3176 // Clear the global handles for the event listener and the event listener data
3177 // object.
3178 if (!event_listener_.is_null()) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00003179 global_handles->Destroy(
iposva@chromium.org245aa852009-02-10 00:49:54 +00003180 reinterpret_cast<Object**>(event_listener_.location()));
3181 event_listener_ = Handle<Object>();
3182 }
3183 if (!event_listener_data_.is_null()) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00003184 global_handles->Destroy(
iposva@chromium.org245aa852009-02-10 00:49:54 +00003185 reinterpret_cast<Object**>(event_listener_data_.location()));
3186 event_listener_data_ = Handle<Object>();
3187 }
3188
3189 // If there is a new debug event listener register it together with its data
3190 // object.
3191 if (!callback->IsUndefined() && !callback->IsNull()) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003192 event_listener_ = Handle<Object>::cast(
lrn@chromium.org7516f052011-03-30 08:52:27 +00003193 global_handles->Create(*callback));
iposva@chromium.org245aa852009-02-10 00:49:54 +00003194 if (data.is_null()) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00003195 data = isolate_->factory()->undefined_value();
iposva@chromium.org245aa852009-02-10 00:49:54 +00003196 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003197 event_listener_data_ = Handle<Object>::cast(
lrn@chromium.org7516f052011-03-30 08:52:27 +00003198 global_handles->Create(*data));
iposva@chromium.org245aa852009-02-10 00:49:54 +00003199 }
3200
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003201 ListenersChanged();
iposva@chromium.org245aa852009-02-10 00:49:54 +00003202}
3203
3204
ager@chromium.org5ec48922009-05-05 07:25:34 +00003205void Debugger::SetMessageHandler(v8::Debug::MessageHandler2 handler) {
ager@chromium.org71daaf62009-04-01 07:22:49 +00003206 ScopedLock with(debugger_access_);
3207
ager@chromium.org381abbb2009-02-25 13:23:22 +00003208 message_handler_ = handler;
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003209 ListenersChanged();
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003210 if (handler == NULL) {
ager@chromium.org71daaf62009-04-01 07:22:49 +00003211 // Send an empty command to the debugger if in a break to make JavaScript
3212 // run again if the debugger is closed.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003213 if (isolate_->debug()->InDebugger()) {
ager@chromium.org71daaf62009-04-01 07:22:49 +00003214 ProcessCommand(Vector<const uint16_t>::empty());
3215 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003216 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003217}
3218
3219
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003220void Debugger::ListenersChanged() {
3221 if (IsDebuggerActive()) {
3222 // Disable the compilation cache when the debugger is active.
lrn@chromium.org7516f052011-03-30 08:52:27 +00003223 isolate_->compilation_cache()->Disable();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003224 debugger_unload_pending_ = false;
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003225 } else {
lrn@chromium.org7516f052011-03-30 08:52:27 +00003226 isolate_->compilation_cache()->Enable();
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003227 // Unload the debugger if event listener and message handler cleared.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003228 // Schedule this for later, because we may be in non-V8 thread.
3229 debugger_unload_pending_ = true;
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003230 }
3231}
3232
3233
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003234void Debugger::SetHostDispatchHandler(v8::Debug::HostDispatchHandler handler,
3235 int period) {
ager@chromium.org381abbb2009-02-25 13:23:22 +00003236 host_dispatch_handler_ = handler;
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003237 host_dispatch_micros_ = period * 1000;
ager@chromium.org381abbb2009-02-25 13:23:22 +00003238}
3239
3240
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003241void Debugger::SetDebugMessageDispatchHandler(
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003242 v8::Debug::DebugMessageDispatchHandler handler, bool provide_locker) {
3243 ScopedLock with(dispatch_handler_access_);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003244 debug_message_dispatch_handler_ = handler;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003245
3246 if (provide_locker && message_dispatch_helper_thread_ == NULL) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003247 message_dispatch_helper_thread_ = new MessageDispatchHelperThread(isolate_);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003248 message_dispatch_helper_thread_->Start();
3249 }
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003250}
3251
3252
ager@chromium.org41826e72009-03-30 13:30:57 +00003253// Calls the registered debug message handler. This callback is part of the
ager@chromium.org5ec48922009-05-05 07:25:34 +00003254// public API.
3255void Debugger::InvokeMessageHandler(MessageImpl message) {
ager@chromium.org71daaf62009-04-01 07:22:49 +00003256 ScopedLock with(debugger_access_);
3257
ager@chromium.org381abbb2009-02-25 13:23:22 +00003258 if (message_handler_ != NULL) {
ager@chromium.org5ec48922009-05-05 07:25:34 +00003259 message_handler_(message);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003260 }
ager@chromium.org41826e72009-03-30 13:30:57 +00003261}
3262
3263
3264// Puts a command coming from the public API on the queue. Creates
3265// a copy of the command string managed by the debugger. Up to this
3266// point, the command data was managed by the API client. Called
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003267// by the API client thread.
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003268void Debugger::ProcessCommand(Vector<const uint16_t> command,
3269 v8::Debug::ClientData* client_data) {
3270 // Need to cast away const.
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003271 CommandMessage message = CommandMessage::New(
ager@chromium.org41826e72009-03-30 13:30:57 +00003272 Vector<uint16_t>(const_cast<uint16_t*>(command.start()),
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003273 command.length()),
3274 client_data);
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00003275 isolate_->logger()->DebugTag("Put command on command_queue.");
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003276 command_queue_.Put(message);
ager@chromium.org41826e72009-03-30 13:30:57 +00003277 command_received_->Signal();
sgjesse@chromium.org3afc1582009-04-16 22:31:44 +00003278
3279 // Set the debug command break flag to have the command processed.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003280 if (!isolate_->debug()->InDebugger()) {
3281 isolate_->stack_guard()->DebugCommand();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003282 }
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003283
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003284 MessageDispatchHelperThread* dispatch_thread;
3285 {
3286 ScopedLock with(dispatch_handler_access_);
3287 dispatch_thread = message_dispatch_helper_thread_;
3288 }
3289
3290 if (dispatch_thread == NULL) {
3291 CallMessageDispatchHandler();
3292 } else {
3293 dispatch_thread->Schedule();
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003294 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003295}
3296
3297
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003298bool Debugger::HasCommands() {
ager@chromium.org41826e72009-03-30 13:30:57 +00003299 return !command_queue_.IsEmpty();
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003300}
3301
3302
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00003303void Debugger::EnqueueDebugCommand(v8::Debug::ClientData* client_data) {
3304 CommandMessage message = CommandMessage::New(Vector<uint16_t>(), client_data);
3305 event_command_queue_.Put(message);
3306
3307 // Set the debug command break flag to have the command processed.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003308 if (!isolate_->debug()->InDebugger()) {
3309 isolate_->stack_guard()->DebugCommand();
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00003310 }
3311}
3312
3313
ager@chromium.org71daaf62009-04-01 07:22:49 +00003314bool Debugger::IsDebuggerActive() {
3315 ScopedLock with(debugger_access_);
3316
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00003317 return message_handler_ != NULL ||
3318 !event_listener_.is_null() ||
3319 force_debugger_active_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003320}
3321
3322
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003323Handle<Object> Debugger::Call(Handle<JSFunction> fun,
3324 Handle<Object> data,
3325 bool* pending_exception) {
ager@chromium.org71daaf62009-04-01 07:22:49 +00003326 // When calling functions in the debugger prevent it from beeing unloaded.
3327 Debugger::never_unload_debugger_ = true;
3328
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003329 // Enter the debugger.
3330 EnterDebugger debugger;
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +00003331 if (debugger.FailedToEnter()) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00003332 return isolate_->factory()->undefined_value();
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003333 }
3334
3335 // Create the execution state.
3336 bool caught_exception = false;
3337 Handle<Object> exec_state = MakeExecutionState(&caught_exception);
3338 if (caught_exception) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00003339 return isolate_->factory()->undefined_value();
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003340 }
3341
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00003342 Handle<Object> argv[] = { exec_state, data };
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +00003343 Handle<Object> result = Execution::Call(
3344 fun,
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00003345 Handle<Object>(isolate_->debug()->debug_context_->global_proxy(),
3346 isolate_),
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00003347 ARRAY_SIZE(argv),
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +00003348 argv,
3349 pending_exception);
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003350 return result;
3351}
3352
3353
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003354static void StubMessageHandler2(const v8::Debug::Message& message) {
3355 // Simply ignore message.
3356}
3357
3358
3359bool Debugger::StartAgent(const char* name, int port,
3360 bool wait_for_connection) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003361 ASSERT(Isolate::Current() == isolate_);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003362 if (wait_for_connection) {
3363 // Suspend V8 if it is already running or set V8 to suspend whenever
3364 // it starts.
3365 // Provide stub message handler; V8 auto-continues each suspend
3366 // when there is no message handler; we doesn't need it.
3367 // Once become suspended, V8 will stay so indefinitely long, until remote
3368 // debugger connects and issues "continue" command.
3369 Debugger::message_handler_ = StubMessageHandler2;
3370 v8::Debug::DebugBreak();
3371 }
3372
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00003373 if (Socket::SetUp()) {
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00003374 if (agent_ == NULL) {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00003375 agent_ = new DebuggerAgent(name, port);
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00003376 agent_->Start();
3377 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003378 return true;
3379 }
3380
3381 return false;
3382}
3383
3384
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003385void Debugger::StopAgent() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003386 ASSERT(Isolate::Current() == isolate_);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003387 if (agent_ != NULL) {
3388 agent_->Shutdown();
3389 agent_->Join();
3390 delete agent_;
3391 agent_ = NULL;
3392 }
3393}
3394
3395
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003396void Debugger::WaitForAgent() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003397 ASSERT(Isolate::Current() == isolate_);
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003398 if (agent_ != NULL)
3399 agent_->WaitUntilListening();
3400}
3401
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003402
3403void Debugger::CallMessageDispatchHandler() {
3404 v8::Debug::DebugMessageDispatchHandler handler;
3405 {
3406 ScopedLock with(dispatch_handler_access_);
3407 handler = Debugger::debug_message_dispatch_handler_;
3408 }
3409 if (handler != NULL) {
3410 handler();
3411 }
3412}
3413
3414
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003415EnterDebugger::EnterDebugger()
3416 : isolate_(Isolate::Current()),
3417 prev_(isolate_->debug()->debugger_entry()),
3418 it_(isolate_),
3419 has_js_frames_(!it_.done()),
3420 save_(isolate_) {
3421 Debug* debug = isolate_->debug();
3422 ASSERT(prev_ != NULL || !debug->is_interrupt_pending(PREEMPT));
3423 ASSERT(prev_ != NULL || !debug->is_interrupt_pending(DEBUGBREAK));
3424
3425 // Link recursive debugger entry.
3426 debug->set_debugger_entry(this);
3427
3428 // Store the previous break id and frame id.
3429 break_id_ = debug->break_id();
3430 break_frame_id_ = debug->break_frame_id();
3431
3432 // Create the new break info. If there is no JavaScript frames there is no
3433 // break frame id.
3434 if (has_js_frames_) {
3435 debug->NewBreak(it_.frame()->id());
3436 } else {
3437 debug->NewBreak(StackFrame::NO_ID);
3438 }
3439
3440 // Make sure that debugger is loaded and enter the debugger context.
3441 load_failed_ = !debug->Load();
3442 if (!load_failed_) {
3443 // NOTE the member variable save which saves the previous context before
3444 // this change.
3445 isolate_->set_context(*debug->debug_context());
3446 }
3447}
3448
3449
3450EnterDebugger::~EnterDebugger() {
3451 ASSERT(Isolate::Current() == isolate_);
3452 Debug* debug = isolate_->debug();
3453
3454 // Restore to the previous break state.
3455 debug->SetBreak(break_frame_id_, break_id_);
3456
3457 // Check for leaving the debugger.
fschneider@chromium.org7d10be52012-04-10 12:30:14 +00003458 if (!load_failed_ && prev_ == NULL) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003459 // Clear mirror cache when leaving the debugger. Skip this if there is a
3460 // pending exception as clearing the mirror cache calls back into
3461 // JavaScript. This can happen if the v8::Debug::Call is used in which
3462 // case the exception should end up in the calling code.
3463 if (!isolate_->has_pending_exception()) {
3464 // Try to avoid any pending debug break breaking in the clear mirror
3465 // cache JavaScript code.
3466 if (isolate_->stack_guard()->IsDebugBreak()) {
3467 debug->set_interrupts_pending(DEBUGBREAK);
3468 isolate_->stack_guard()->Continue(DEBUGBREAK);
3469 }
3470 debug->ClearMirrorCache();
3471 }
3472
3473 // Request preemption and debug break when leaving the last debugger entry
3474 // if any of these where recorded while debugging.
3475 if (debug->is_interrupt_pending(PREEMPT)) {
3476 // This re-scheduling of preemption is to avoid starvation in some
3477 // debugging scenarios.
3478 debug->clear_interrupt_pending(PREEMPT);
3479 isolate_->stack_guard()->Preempt();
3480 }
3481 if (debug->is_interrupt_pending(DEBUGBREAK)) {
3482 debug->clear_interrupt_pending(DEBUGBREAK);
3483 isolate_->stack_guard()->DebugBreak();
3484 }
3485
3486 // If there are commands in the queue when leaving the debugger request
3487 // that these commands are processed.
3488 if (isolate_->debugger()->HasCommands()) {
3489 isolate_->stack_guard()->DebugCommand();
3490 }
3491
3492 // If leaving the debugger with the debugger no longer active unload it.
3493 if (!isolate_->debugger()->IsDebuggerActive()) {
3494 isolate_->debugger()->UnloadDebugger();
3495 }
3496 }
3497
3498 // Leaving this debugger entry.
3499 debug->set_debugger_entry(prev_);
3500}
3501
3502
ager@chromium.org5ec48922009-05-05 07:25:34 +00003503MessageImpl MessageImpl::NewEvent(DebugEvent event,
3504 bool running,
3505 Handle<JSObject> exec_state,
3506 Handle<JSObject> event_data) {
3507 MessageImpl message(true, event, running,
3508 exec_state, event_data, Handle<String>(), NULL);
3509 return message;
3510}
3511
3512
3513MessageImpl MessageImpl::NewResponse(DebugEvent event,
3514 bool running,
3515 Handle<JSObject> exec_state,
3516 Handle<JSObject> event_data,
3517 Handle<String> response_json,
3518 v8::Debug::ClientData* client_data) {
3519 MessageImpl message(false, event, running,
3520 exec_state, event_data, response_json, client_data);
3521 return message;
3522}
3523
3524
3525MessageImpl::MessageImpl(bool is_event,
3526 DebugEvent event,
3527 bool running,
3528 Handle<JSObject> exec_state,
3529 Handle<JSObject> event_data,
3530 Handle<String> response_json,
3531 v8::Debug::ClientData* client_data)
3532 : is_event_(is_event),
3533 event_(event),
3534 running_(running),
3535 exec_state_(exec_state),
3536 event_data_(event_data),
3537 response_json_(response_json),
3538 client_data_(client_data) {}
3539
3540
3541bool MessageImpl::IsEvent() const {
3542 return is_event_;
3543}
3544
3545
3546bool MessageImpl::IsResponse() const {
3547 return !is_event_;
3548}
3549
3550
3551DebugEvent MessageImpl::GetEvent() const {
3552 return event_;
3553}
3554
3555
3556bool MessageImpl::WillStartRunning() const {
3557 return running_;
3558}
3559
3560
3561v8::Handle<v8::Object> MessageImpl::GetExecutionState() const {
3562 return v8::Utils::ToLocal(exec_state_);
3563}
3564
3565
3566v8::Handle<v8::Object> MessageImpl::GetEventData() const {
3567 return v8::Utils::ToLocal(event_data_);
3568}
3569
3570
3571v8::Handle<v8::String> MessageImpl::GetJSON() const {
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +00003572 v8::HandleScope scope(
3573 reinterpret_cast<v8::Isolate*>(event_data_->GetIsolate()));
ager@chromium.org5ec48922009-05-05 07:25:34 +00003574
3575 if (IsEvent()) {
3576 // Call toJSONProtocol on the debug event object.
3577 Handle<Object> fun = GetProperty(event_data_, "toJSONProtocol");
3578 if (!fun->IsJSFunction()) {
3579 return v8::Handle<v8::String>();
3580 }
3581 bool caught_exception;
3582 Handle<Object> json = Execution::TryCall(Handle<JSFunction>::cast(fun),
3583 event_data_,
3584 0, NULL, &caught_exception);
3585 if (caught_exception || !json->IsString()) {
3586 return v8::Handle<v8::String>();
3587 }
3588 return scope.Close(v8::Utils::ToLocal(Handle<String>::cast(json)));
3589 } else {
3590 return v8::Utils::ToLocal(response_json_);
3591 }
3592}
3593
3594
3595v8::Handle<v8::Context> MessageImpl::GetEventContext() const {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003596 Isolate* isolate = Isolate::Current();
3597 v8::Handle<v8::Context> context = GetDebugEventContext(isolate);
3598 // Isolate::context() may be NULL when "script collected" event occures.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003599 ASSERT(!context.IsEmpty() || event_ == v8::ScriptCollected);
rossberg@chromium.org717967f2011-07-20 13:44:42 +00003600 return context;
ager@chromium.org5ec48922009-05-05 07:25:34 +00003601}
3602
3603
3604v8::Debug::ClientData* MessageImpl::GetClientData() const {
3605 return client_data_;
3606}
3607
3608
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003609EventDetailsImpl::EventDetailsImpl(DebugEvent event,
3610 Handle<JSObject> exec_state,
3611 Handle<JSObject> event_data,
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00003612 Handle<Object> callback_data,
3613 v8::Debug::ClientData* client_data)
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003614 : event_(event),
3615 exec_state_(exec_state),
3616 event_data_(event_data),
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00003617 callback_data_(callback_data),
3618 client_data_(client_data) {}
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003619
3620
3621DebugEvent EventDetailsImpl::GetEvent() const {
3622 return event_;
3623}
3624
3625
3626v8::Handle<v8::Object> EventDetailsImpl::GetExecutionState() const {
3627 return v8::Utils::ToLocal(exec_state_);
3628}
3629
3630
3631v8::Handle<v8::Object> EventDetailsImpl::GetEventData() const {
3632 return v8::Utils::ToLocal(event_data_);
3633}
3634
3635
3636v8::Handle<v8::Context> EventDetailsImpl::GetEventContext() const {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003637 return GetDebugEventContext(Isolate::Current());
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003638}
3639
3640
3641v8::Handle<v8::Value> EventDetailsImpl::GetCallbackData() const {
3642 return v8::Utils::ToLocal(callback_data_);
3643}
3644
3645
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00003646v8::Debug::ClientData* EventDetailsImpl::GetClientData() const {
3647 return client_data_;
3648}
3649
3650
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003651CommandMessage::CommandMessage() : text_(Vector<uint16_t>::empty()),
3652 client_data_(NULL) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003653}
3654
3655
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003656CommandMessage::CommandMessage(const Vector<uint16_t>& text,
3657 v8::Debug::ClientData* data)
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003658 : text_(text),
3659 client_data_(data) {
3660}
3661
3662
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003663CommandMessage::~CommandMessage() {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003664}
3665
3666
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003667void CommandMessage::Dispose() {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003668 text_.Dispose();
3669 delete client_data_;
3670 client_data_ = NULL;
3671}
3672
3673
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003674CommandMessage CommandMessage::New(const Vector<uint16_t>& command,
3675 v8::Debug::ClientData* data) {
3676 return CommandMessage(command.Clone(), data);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003677}
3678
3679
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003680CommandMessageQueue::CommandMessageQueue(int size) : start_(0), end_(0),
3681 size_(size) {
3682 messages_ = NewArray<CommandMessage>(size);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003683}
3684
3685
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003686CommandMessageQueue::~CommandMessageQueue() {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003687 while (!IsEmpty()) {
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003688 CommandMessage m = Get();
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003689 m.Dispose();
3690 }
kasper.lund7276f142008-07-30 08:49:36 +00003691 DeleteArray(messages_);
3692}
3693
3694
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003695CommandMessage CommandMessageQueue::Get() {
kasper.lund7276f142008-07-30 08:49:36 +00003696 ASSERT(!IsEmpty());
3697 int result = start_;
3698 start_ = (start_ + 1) % size_;
3699 return messages_[result];
3700}
3701
3702
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003703void CommandMessageQueue::Put(const CommandMessage& message) {
kasper.lund7276f142008-07-30 08:49:36 +00003704 if ((end_ + 1) % size_ == start_) {
3705 Expand();
3706 }
3707 messages_[end_] = message;
3708 end_ = (end_ + 1) % size_;
3709}
3710
3711
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003712void CommandMessageQueue::Expand() {
3713 CommandMessageQueue new_queue(size_ * 2);
kasper.lund7276f142008-07-30 08:49:36 +00003714 while (!IsEmpty()) {
3715 new_queue.Put(Get());
3716 }
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003717 CommandMessage* array_to_free = messages_;
kasper.lund7276f142008-07-30 08:49:36 +00003718 *this = new_queue;
3719 new_queue.messages_ = array_to_free;
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003720 // Make the new_queue empty so that it doesn't call Dispose on any messages.
3721 new_queue.start_ = new_queue.end_;
kasper.lund7276f142008-07-30 08:49:36 +00003722 // Automatic destructor called on new_queue, freeing array_to_free.
3723}
3724
3725
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00003726LockingCommandMessageQueue::LockingCommandMessageQueue(Logger* logger, int size)
3727 : logger_(logger), queue_(size) {
kasper.lund7276f142008-07-30 08:49:36 +00003728 lock_ = OS::CreateMutex();
3729}
3730
3731
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003732LockingCommandMessageQueue::~LockingCommandMessageQueue() {
kasper.lund7276f142008-07-30 08:49:36 +00003733 delete lock_;
3734}
3735
3736
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003737bool LockingCommandMessageQueue::IsEmpty() const {
kasper.lund7276f142008-07-30 08:49:36 +00003738 ScopedLock sl(lock_);
3739 return queue_.IsEmpty();
3740}
3741
3742
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003743CommandMessage LockingCommandMessageQueue::Get() {
kasper.lund7276f142008-07-30 08:49:36 +00003744 ScopedLock sl(lock_);
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003745 CommandMessage result = queue_.Get();
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00003746 logger_->DebugEvent("Get", result.text());
kasper.lund7276f142008-07-30 08:49:36 +00003747 return result;
3748}
3749
3750
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003751void LockingCommandMessageQueue::Put(const CommandMessage& message) {
kasper.lund7276f142008-07-30 08:49:36 +00003752 ScopedLock sl(lock_);
3753 queue_.Put(message);
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00003754 logger_->DebugEvent("Put", message.text());
kasper.lund7276f142008-07-30 08:49:36 +00003755}
3756
3757
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003758void LockingCommandMessageQueue::Clear() {
kasper.lund7276f142008-07-30 08:49:36 +00003759 ScopedLock sl(lock_);
3760 queue_.Clear();
3761}
3762
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003763
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003764MessageDispatchHelperThread::MessageDispatchHelperThread(Isolate* isolate)
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00003765 : Thread("v8:MsgDispHelpr"),
mstarzinger@chromium.orgf705b502013-04-04 11:38:09 +00003766 isolate_(isolate), sem_(OS::CreateSemaphore(0)),
3767 mutex_(OS::CreateMutex()), already_signalled_(false) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003768}
3769
3770
3771MessageDispatchHelperThread::~MessageDispatchHelperThread() {
3772 delete mutex_;
3773 delete sem_;
3774}
3775
3776
3777void MessageDispatchHelperThread::Schedule() {
3778 {
3779 ScopedLock lock(mutex_);
3780 if (already_signalled_) {
3781 return;
3782 }
3783 already_signalled_ = true;
3784 }
3785 sem_->Signal();
3786}
3787
3788
3789void MessageDispatchHelperThread::Run() {
3790 while (true) {
3791 sem_->Wait();
3792 {
3793 ScopedLock lock(mutex_);
3794 already_signalled_ = false;
3795 }
3796 {
mstarzinger@chromium.orgf705b502013-04-04 11:38:09 +00003797 Locker locker(reinterpret_cast<v8::Isolate*>(isolate_));
3798 isolate_->debugger()->CallMessageDispatchHandler();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003799 }
3800 }
3801}
3802
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003803#endif // ENABLE_DEBUGGER_SUPPORT
kasper.lund7276f142008-07-30 08:49:36 +00003804
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003805} } // namespace v8::internal