blob: 777e23dac81a476d16ea77fbb4558be98ae98d43 [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>();
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000100 Handle<Context> global_context(context->global_context());
101 return v8::Utils::ToLocal(global_context);
102}
103
104
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000105BreakLocationIterator::BreakLocationIterator(Handle<DebugInfo> debug_info,
106 BreakLocatorType type) {
107 debug_info_ = debug_info;
108 type_ = type;
109 reloc_iterator_ = NULL;
110 reloc_iterator_original_ = NULL;
111 Reset(); // Initialize the rest of the member variables.
112}
113
114
115BreakLocationIterator::~BreakLocationIterator() {
116 ASSERT(reloc_iterator_ != NULL);
117 ASSERT(reloc_iterator_original_ != NULL);
118 delete reloc_iterator_;
119 delete reloc_iterator_original_;
120}
121
122
123void BreakLocationIterator::Next() {
124 AssertNoAllocation nogc;
125 ASSERT(!RinfoDone());
126
127 // Iterate through reloc info for code and original code stopping at each
128 // breakable code target.
129 bool first = break_point_ == -1;
130 while (!RinfoDone()) {
131 if (!first) RinfoNext();
132 first = false;
133 if (RinfoDone()) return;
134
ager@chromium.org236ad962008-09-25 09:45:57 +0000135 // Whenever a statement position or (plain) position is passed update the
136 // current value of these.
137 if (RelocInfo::IsPosition(rmode())) {
138 if (RelocInfo::IsStatementPosition(rmode())) {
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000139 statement_position_ = static_cast<int>(
140 rinfo()->data() - debug_info_->shared()->start_position());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000141 }
ager@chromium.org236ad962008-09-25 09:45:57 +0000142 // Always update the position as we don't want that to be before the
143 // statement position.
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000144 position_ = static_cast<int>(
145 rinfo()->data() - debug_info_->shared()->start_position());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000146 ASSERT(position_ >= 0);
147 ASSERT(statement_position_ >= 0);
148 }
149
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000150 if (IsDebugBreakSlot()) {
151 // There is always a possible break point at a debug break slot.
152 break_point_++;
153 return;
154 } else if (RelocInfo::IsCodeTarget(rmode())) {
155 // Check for breakable code target. Look in the original code as setting
156 // break points can cause the code targets in the running (debugged) code
157 // to be of a different kind than in the original code.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000158 Address target = original_rinfo()->target_address();
ager@chromium.org8bb60582008-12-11 12:02:20 +0000159 Code* code = Code::GetCodeFromTargetAddress(target);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000160 if ((code->is_inline_cache_stub() &&
danno@chromium.org40cb8782011-05-25 07:58:50 +0000161 !code->is_binary_op_stub() &&
162 !code->is_unary_op_stub() &&
ricow@chromium.org9fa09672011-07-25 11:05:35 +0000163 !code->is_compare_ic_stub() &&
164 !code->is_to_boolean_ic_stub()) ||
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000165 RelocInfo::IsConstructCall(rmode())) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000166 break_point_++;
167 return;
168 }
169 if (code->kind() == Code::STUB) {
ager@chromium.orga1645e22009-09-09 19:27:10 +0000170 if (IsDebuggerStatement()) {
171 break_point_++;
172 return;
173 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000174 if (type_ == ALL_BREAK_LOCATIONS) {
175 if (Debug::IsBreakStub(code)) {
176 break_point_++;
177 return;
178 }
179 } else {
180 ASSERT(type_ == SOURCE_BREAK_LOCATIONS);
181 if (Debug::IsSourceBreakStub(code)) {
182 break_point_++;
183 return;
184 }
185 }
186 }
187 }
188
189 // Check for break at return.
ager@chromium.org236ad962008-09-25 09:45:57 +0000190 if (RelocInfo::IsJSReturn(rmode())) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000191 // Set the positions to the end of the function.
192 if (debug_info_->shared()->HasSourceCode()) {
193 position_ = debug_info_->shared()->end_position() -
whesse@chromium.orge90029b2010-08-02 11:52:17 +0000194 debug_info_->shared()->start_position() - 1;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000195 } else {
196 position_ = 0;
197 }
198 statement_position_ = position_;
199 break_point_++;
200 return;
201 }
202 }
203}
204
205
206void BreakLocationIterator::Next(int count) {
207 while (count > 0) {
208 Next();
209 count--;
210 }
211}
212
213
214// Find the break point closest to the supplied address.
215void BreakLocationIterator::FindBreakLocationFromAddress(Address pc) {
216 // Run through all break points to locate the one closest to the address.
217 int closest_break_point = 0;
218 int distance = kMaxInt;
219 while (!Done()) {
220 // Check if this break point is closer that what was previously found.
221 if (this->pc() < pc && pc - this->pc() < distance) {
222 closest_break_point = break_point();
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000223 distance = static_cast<int>(pc - this->pc());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000224 // Check whether we can't get any closer.
225 if (distance == 0) break;
226 }
227 Next();
228 }
229
230 // Move to the break point found.
231 Reset();
232 Next(closest_break_point);
233}
234
235
236// Find the break point closest to the supplied source position.
237void BreakLocationIterator::FindBreakLocationFromPosition(int position) {
238 // Run through all break points to locate the one closest to the source
239 // position.
240 int closest_break_point = 0;
241 int distance = kMaxInt;
242 while (!Done()) {
243 // Check if this break point is closer that what was previously found.
244 if (position <= statement_position() &&
245 statement_position() - position < distance) {
246 closest_break_point = break_point();
247 distance = statement_position() - position;
248 // Check whether we can't get any closer.
249 if (distance == 0) break;
250 }
251 Next();
252 }
253
254 // Move to the break point found.
255 Reset();
256 Next(closest_break_point);
257}
258
259
260void BreakLocationIterator::Reset() {
261 // Create relocation iterators for the two code objects.
262 if (reloc_iterator_ != NULL) delete reloc_iterator_;
263 if (reloc_iterator_original_ != NULL) delete reloc_iterator_original_;
264 reloc_iterator_ = new RelocIterator(debug_info_->code());
265 reloc_iterator_original_ = new RelocIterator(debug_info_->original_code());
266
267 // Position at the first break point.
268 break_point_ = -1;
269 position_ = 1;
270 statement_position_ = 1;
271 Next();
272}
273
274
275bool BreakLocationIterator::Done() const {
276 return RinfoDone();
277}
278
279
280void BreakLocationIterator::SetBreakPoint(Handle<Object> break_point_object) {
281 // If there is not already a real break point here patch code with debug
282 // break.
283 if (!HasBreakPoint()) {
284 SetDebugBreak();
285 }
ager@chromium.orga1645e22009-09-09 19:27:10 +0000286 ASSERT(IsDebugBreak() || IsDebuggerStatement());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000287 // Set the break point information.
288 DebugInfo::SetBreakPoint(debug_info_, code_position(),
289 position(), statement_position(),
290 break_point_object);
291}
292
293
294void BreakLocationIterator::ClearBreakPoint(Handle<Object> break_point_object) {
295 // Clear the break point information.
296 DebugInfo::ClearBreakPoint(debug_info_, code_position(), break_point_object);
297 // If there are no more break points here remove the debug break.
298 if (!HasBreakPoint()) {
299 ClearDebugBreak();
300 ASSERT(!IsDebugBreak());
301 }
302}
303
304
305void BreakLocationIterator::SetOneShot() {
ager@chromium.orga1645e22009-09-09 19:27:10 +0000306 // Debugger statement always calls debugger. No need to modify it.
307 if (IsDebuggerStatement()) {
308 return;
309 }
310
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000311 // If there is a real break point here no more to do.
312 if (HasBreakPoint()) {
313 ASSERT(IsDebugBreak());
314 return;
315 }
316
317 // Patch code with debug break.
318 SetDebugBreak();
319}
320
321
322void BreakLocationIterator::ClearOneShot() {
ager@chromium.orga1645e22009-09-09 19:27:10 +0000323 // Debugger statement always calls debugger. No need to modify it.
324 if (IsDebuggerStatement()) {
325 return;
326 }
327
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000328 // If there is a real break point here no more to do.
329 if (HasBreakPoint()) {
330 ASSERT(IsDebugBreak());
331 return;
332 }
333
334 // Patch code removing debug break.
335 ClearDebugBreak();
336 ASSERT(!IsDebugBreak());
337}
338
339
340void BreakLocationIterator::SetDebugBreak() {
ager@chromium.orga1645e22009-09-09 19:27:10 +0000341 // Debugger statement always calls debugger. No need to modify it.
342 if (IsDebuggerStatement()) {
343 return;
344 }
345
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000346 // If there is already a break point here just return. This might happen if
v8.team.kasperl727e9952008-09-02 14:56:44 +0000347 // the same code is flooded with break points twice. Flooding the same
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000348 // function twice might happen when stepping in a function with an exception
349 // handler as the handler and the function is the same.
350 if (IsDebugBreak()) {
351 return;
352 }
353
ager@chromium.org236ad962008-09-25 09:45:57 +0000354 if (RelocInfo::IsJSReturn(rmode())) {
iposva@chromium.org245aa852009-02-10 00:49:54 +0000355 // Patch the frame exit code with a break point.
356 SetDebugBreakAtReturn();
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000357 } else if (IsDebugBreakSlot()) {
358 // Patch the code in the break slot.
359 SetDebugBreakAtSlot();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000360 } else {
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000361 // Patch the IC call.
362 SetDebugBreakAtIC();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000363 }
364 ASSERT(IsDebugBreak());
365}
366
367
368void BreakLocationIterator::ClearDebugBreak() {
ager@chromium.orga1645e22009-09-09 19:27:10 +0000369 // Debugger statement always calls debugger. No need to modify it.
370 if (IsDebuggerStatement()) {
371 return;
372 }
373
ager@chromium.org236ad962008-09-25 09:45:57 +0000374 if (RelocInfo::IsJSReturn(rmode())) {
iposva@chromium.org245aa852009-02-10 00:49:54 +0000375 // Restore the frame exit code.
376 ClearDebugBreakAtReturn();
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000377 } else if (IsDebugBreakSlot()) {
378 // Restore the code in the break slot.
379 ClearDebugBreakAtSlot();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000380 } else {
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000381 // Patch the IC call.
382 ClearDebugBreakAtIC();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000383 }
384 ASSERT(!IsDebugBreak());
385}
386
387
388void BreakLocationIterator::PrepareStepIn() {
ager@chromium.org381abbb2009-02-25 13:23:22 +0000389 HandleScope scope;
390
ager@chromium.orga1645e22009-09-09 19:27:10 +0000391 // Step in can only be prepared if currently positioned on an IC call,
392 // construct call or CallFunction stub call.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000393 Address target = rinfo()->target_address();
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000394 Handle<Code> target_code(Code::GetCodeFromTargetAddress(target));
395 if (target_code->is_call_stub() || target_code->is_keyed_call_stub()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000396 // Step in through IC call is handled by the runtime system. Therefore make
397 // sure that the any current IC is cleared and the runtime system is
398 // called. If the executing code has a debug break at the location change
399 // the call in the original code as it is the code there that will be
400 // executed in place of the debug break call.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000401 Handle<Code> stub = ComputeCallDebugPrepareStepIn(
402 target_code->arguments_count(), target_code->kind());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000403 if (IsDebugBreak()) {
404 original_rinfo()->set_target_address(stub->entry());
405 } else {
406 rinfo()->set_target_address(stub->entry());
407 }
408 } else {
ager@chromium.orga1645e22009-09-09 19:27:10 +0000409#ifdef DEBUG
410 // All the following stuff is needed only for assertion checks so the code
411 // is wrapped in ifdef.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000412 Handle<Code> maybe_call_function_stub = target_code;
ager@chromium.orga1645e22009-09-09 19:27:10 +0000413 if (IsDebugBreak()) {
414 Address original_target = original_rinfo()->target_address();
415 maybe_call_function_stub =
416 Handle<Code>(Code::GetCodeFromTargetAddress(original_target));
417 }
418 bool is_call_function_stub =
419 (maybe_call_function_stub->kind() == Code::STUB &&
420 maybe_call_function_stub->major_key() == CodeStub::CallFunction);
421
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000422 // Step in through construct call requires no changes to the running code.
423 // Step in through getters/setters should already be prepared as well
424 // because caller of this function (Debug::PrepareStep) is expected to
425 // flood the top frame's function with one shot breakpoints.
ager@chromium.orga1645e22009-09-09 19:27:10 +0000426 // Step in through CallFunction stub should also be prepared by caller of
427 // this function (Debug::PrepareStep) which should flood target function
428 // with breakpoints.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000429 ASSERT(RelocInfo::IsConstructCall(rmode()) ||
430 target_code->is_inline_cache_stub() ||
431 is_call_function_stub);
ager@chromium.orga1645e22009-09-09 19:27:10 +0000432#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000433 }
434}
435
436
437// Check whether the break point is at a position which will exit the function.
438bool BreakLocationIterator::IsExit() const {
ager@chromium.org236ad962008-09-25 09:45:57 +0000439 return (RelocInfo::IsJSReturn(rmode()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000440}
441
442
443bool BreakLocationIterator::HasBreakPoint() {
444 return debug_info_->HasBreakPoint(code_position());
445}
446
447
448// Check whether there is a debug break at the current position.
449bool BreakLocationIterator::IsDebugBreak() {
ager@chromium.org236ad962008-09-25 09:45:57 +0000450 if (RelocInfo::IsJSReturn(rmode())) {
iposva@chromium.org245aa852009-02-10 00:49:54 +0000451 return IsDebugBreakAtReturn();
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000452 } else if (IsDebugBreakSlot()) {
453 return IsDebugBreakAtSlot();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000454 } else {
455 return Debug::IsDebugBreak(rinfo()->target_address());
456 }
457}
458
459
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000460void BreakLocationIterator::SetDebugBreakAtIC() {
461 // Patch the original code with the current address as the current address
462 // might have changed by the inline caching since the code was copied.
463 original_rinfo()->set_target_address(rinfo()->target_address());
464
465 RelocInfo::Mode mode = rmode();
466 if (RelocInfo::IsCodeTarget(mode)) {
467 Address target = rinfo()->target_address();
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000468 Handle<Code> target_code(Code::GetCodeFromTargetAddress(target));
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000469
470 // Patch the code to invoke the builtin debug break function matching the
471 // calling convention used by the call site.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000472 Handle<Code> dbgbrk_code(Debug::FindDebugBreak(target_code, mode));
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000473 rinfo()->set_target_address(dbgbrk_code->entry());
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000474 }
475}
476
477
478void BreakLocationIterator::ClearDebugBreakAtIC() {
479 // Patch the code to the original invoke.
480 rinfo()->set_target_address(original_rinfo()->target_address());
481}
482
483
ager@chromium.orga1645e22009-09-09 19:27:10 +0000484bool BreakLocationIterator::IsDebuggerStatement() {
ager@chromium.org5c838252010-02-19 08:53:10 +0000485 return RelocInfo::DEBUG_BREAK == rmode();
ager@chromium.orga1645e22009-09-09 19:27:10 +0000486}
487
488
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000489bool BreakLocationIterator::IsDebugBreakSlot() {
490 return RelocInfo::DEBUG_BREAK_SLOT == rmode();
491}
492
493
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000494Object* BreakLocationIterator::BreakPointObjects() {
495 return debug_info_->GetBreakPointObjects(code_position());
496}
497
498
ager@chromium.org381abbb2009-02-25 13:23:22 +0000499// Clear out all the debug break code. This is ONLY supposed to be used when
500// shutting down the debugger as it will leave the break point information in
501// DebugInfo even though the code is patched back to the non break point state.
502void BreakLocationIterator::ClearAllDebugBreak() {
503 while (!Done()) {
504 ClearDebugBreak();
505 Next();
506 }
507}
508
509
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000510bool BreakLocationIterator::RinfoDone() const {
511 ASSERT(reloc_iterator_->done() == reloc_iterator_original_->done());
512 return reloc_iterator_->done();
513}
514
515
516void BreakLocationIterator::RinfoNext() {
517 reloc_iterator_->next();
518 reloc_iterator_original_->next();
519#ifdef DEBUG
520 ASSERT(reloc_iterator_->done() == reloc_iterator_original_->done());
521 if (!reloc_iterator_->done()) {
522 ASSERT(rmode() == original_rmode());
523 }
524#endif
525}
526
527
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000528// Threading support.
529void Debug::ThreadInit() {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000530 thread_local_.break_count_ = 0;
531 thread_local_.break_id_ = 0;
532 thread_local_.break_frame_id_ = StackFrame::NO_ID;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000533 thread_local_.last_step_action_ = StepNone;
ager@chromium.org236ad962008-09-25 09:45:57 +0000534 thread_local_.last_statement_position_ = RelocInfo::kNoPosition;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000535 thread_local_.step_count_ = 0;
536 thread_local_.last_fp_ = 0;
lrn@chromium.org34e60782011-09-15 07:25:40 +0000537 thread_local_.queued_step_count_ = 0;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000538 thread_local_.step_into_fp_ = 0;
ager@chromium.orga1645e22009-09-09 19:27:10 +0000539 thread_local_.step_out_fp_ = 0;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000540 thread_local_.after_break_target_ = 0;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000541 // TODO(isolates): frames_are_dropped_?
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000542 thread_local_.debugger_entry_ = NULL;
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000543 thread_local_.pending_interrupts_ = 0;
ricow@chromium.org0b9f8502010-08-18 07:45:01 +0000544 thread_local_.restarter_frame_function_pointer_ = NULL;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000545}
546
547
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000548char* Debug::ArchiveDebug(char* storage) {
549 char* to = storage;
550 memcpy(to, reinterpret_cast<char*>(&thread_local_), sizeof(ThreadLocal));
551 to += sizeof(ThreadLocal);
552 memcpy(to, reinterpret_cast<char*>(&registers_), sizeof(registers_));
553 ThreadInit();
554 ASSERT(to <= storage + ArchiveSpacePerThread());
555 return storage + ArchiveSpacePerThread();
556}
557
558
559char* Debug::RestoreDebug(char* storage) {
560 char* from = storage;
561 memcpy(reinterpret_cast<char*>(&thread_local_), from, sizeof(ThreadLocal));
562 from += sizeof(ThreadLocal);
563 memcpy(reinterpret_cast<char*>(&registers_), from, sizeof(registers_));
564 ASSERT(from <= storage + ArchiveSpacePerThread());
565 return storage + ArchiveSpacePerThread();
566}
567
568
569int Debug::ArchiveSpacePerThread() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000570 return sizeof(ThreadLocal) + sizeof(JSCallerSavedBuffer);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000571}
572
573
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000574// Frame structure (conforms InternalFrame structure):
575// -- code
576// -- SMI maker
577// -- function (slot is called "context")
578// -- frame base
579Object** Debug::SetUpFrameDropperFrame(StackFrame* bottom_js_frame,
580 Handle<Code> code) {
581 ASSERT(bottom_js_frame->is_java_script());
582
583 Address fp = bottom_js_frame->fp();
584
585 // Move function pointer into "context" slot.
586 Memory::Object_at(fp + StandardFrameConstants::kContextOffset) =
587 Memory::Object_at(fp + JavaScriptFrameConstants::kFunctionOffset);
588
589 Memory::Object_at(fp + InternalFrameConstants::kCodeOffset) = *code;
590 Memory::Object_at(fp + StandardFrameConstants::kMarkerOffset) =
591 Smi::FromInt(StackFrame::INTERNAL);
592
593 return reinterpret_cast<Object**>(&Memory::Object_at(
594 fp + StandardFrameConstants::kContextOffset));
595}
596
597const int Debug::kFrameDropperFrameSize = 4;
598
599
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000600void ScriptCache::Add(Handle<Script> script) {
lrn@chromium.org7516f052011-03-30 08:52:27 +0000601 GlobalHandles* global_handles = Isolate::Current()->global_handles();
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000602 // Create an entry in the hash map for the script.
603 int id = Smi::cast(script->id())->value();
604 HashMap::Entry* entry =
605 HashMap::Lookup(reinterpret_cast<void*>(id), Hash(id), true);
606 if (entry->value != NULL) {
607 ASSERT(*script == *reinterpret_cast<Script**>(entry->value));
608 return;
609 }
610
611 // Globalize the script object, make it weak and use the location of the
612 // global handle as the value in the hash map.
613 Handle<Script> script_ =
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000614 Handle<Script>::cast(
lrn@chromium.org7516f052011-03-30 08:52:27 +0000615 (global_handles->Create(*script)));
616 global_handles->MakeWeak(
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000617 reinterpret_cast<Object**>(script_.location()),
618 this,
619 ScriptCache::HandleWeakScript);
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000620 entry->value = script_.location();
621}
622
623
624Handle<FixedArray> ScriptCache::GetScripts() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000625 Handle<FixedArray> instances = FACTORY->NewFixedArray(occupancy());
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000626 int count = 0;
627 for (HashMap::Entry* entry = Start(); entry != NULL; entry = Next(entry)) {
628 ASSERT(entry->value != NULL);
629 if (entry->value != NULL) {
630 instances->set(count, *reinterpret_cast<Script**>(entry->value));
631 count++;
632 }
633 }
634 return instances;
635}
636
637
638void ScriptCache::ProcessCollectedScripts() {
lrn@chromium.org7516f052011-03-30 08:52:27 +0000639 Debugger* debugger = Isolate::Current()->debugger();
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000640 for (int i = 0; i < collected_scripts_.length(); i++) {
lrn@chromium.org7516f052011-03-30 08:52:27 +0000641 debugger->OnScriptCollected(collected_scripts_[i]);
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000642 }
643 collected_scripts_.Clear();
644}
645
646
647void ScriptCache::Clear() {
lrn@chromium.org7516f052011-03-30 08:52:27 +0000648 GlobalHandles* global_handles = Isolate::Current()->global_handles();
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000649 // Iterate the script cache to get rid of all the weak handles.
650 for (HashMap::Entry* entry = Start(); entry != NULL; entry = Next(entry)) {
651 ASSERT(entry != NULL);
652 Object** location = reinterpret_cast<Object**>(entry->value);
653 ASSERT((*location)->IsScript());
lrn@chromium.org7516f052011-03-30 08:52:27 +0000654 global_handles->ClearWeakness(location);
655 global_handles->Destroy(location);
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000656 }
657 // Clear the content of the hash map.
658 HashMap::Clear();
659}
660
661
662void ScriptCache::HandleWeakScript(v8::Persistent<v8::Value> obj, void* data) {
663 ScriptCache* script_cache = reinterpret_cast<ScriptCache*>(data);
664 // Find the location of the global handle.
665 Script** location =
666 reinterpret_cast<Script**>(Utils::OpenHandle(*obj).location());
667 ASSERT((*location)->IsScript());
668
669 // Remove the entry from the cache.
670 int id = Smi::cast((*location)->id())->value();
671 script_cache->Remove(reinterpret_cast<void*>(id), Hash(id));
672 script_cache->collected_scripts_.Add(id);
673
674 // Clear the weak handle.
675 obj.Dispose();
676 obj.Clear();
677}
678
679
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000680void Debug::SetUp(bool create_heap_objects) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000681 ThreadInit();
682 if (create_heap_objects) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000683 // Get code to handle debug break on return.
684 debug_break_return_ =
lrn@chromium.org7516f052011-03-30 08:52:27 +0000685 isolate_->builtins()->builtin(Builtins::kReturn_DebugBreak);
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000686 ASSERT(debug_break_return_->IsCode());
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000687 // Get code to handle debug break in debug break slots.
688 debug_break_slot_ =
lrn@chromium.org7516f052011-03-30 08:52:27 +0000689 isolate_->builtins()->builtin(Builtins::kSlot_DebugBreak);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000690 ASSERT(debug_break_slot_->IsCode());
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000691 }
692}
693
694
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000695void Debug::HandleWeakDebugInfo(v8::Persistent<v8::Value> obj, void* data) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000696 Debug* debug = Isolate::Current()->debug();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000697 DebugInfoListNode* node = reinterpret_cast<DebugInfoListNode*>(data);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000698 // We need to clear all breakpoints associated with the function to restore
699 // original code and avoid patching the code twice later because
700 // the function will live in the heap until next gc, and can be found by
701 // Runtime::FindSharedFunctionInfoInScript.
702 BreakLocationIterator it(node->debug_info(), ALL_BREAK_LOCATIONS);
703 it.ClearAllDebugBreak();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000704 debug->RemoveDebugInfo(node->debug_info());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000705#ifdef DEBUG
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000706 node = debug->debug_info_list_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000707 while (node != NULL) {
708 ASSERT(node != reinterpret_cast<DebugInfoListNode*>(data));
709 node = node->next();
710 }
711#endif
712}
713
714
715DebugInfoListNode::DebugInfoListNode(DebugInfo* debug_info): next_(NULL) {
lrn@chromium.org7516f052011-03-30 08:52:27 +0000716 GlobalHandles* global_handles = Isolate::Current()->global_handles();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000717 // Globalize the request debug info object and make it weak.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000718 debug_info_ = Handle<DebugInfo>::cast(
lrn@chromium.org7516f052011-03-30 08:52:27 +0000719 (global_handles->Create(debug_info)));
720 global_handles->MakeWeak(
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000721 reinterpret_cast<Object**>(debug_info_.location()),
722 this,
723 Debug::HandleWeakDebugInfo);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000724}
725
726
727DebugInfoListNode::~DebugInfoListNode() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000728 Isolate::Current()->global_handles()->Destroy(
729 reinterpret_cast<Object**>(debug_info_.location()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000730}
731
732
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000733bool Debug::CompileDebuggerScript(int index) {
lrn@chromium.org7516f052011-03-30 08:52:27 +0000734 Isolate* isolate = Isolate::Current();
735 Factory* factory = isolate->factory();
736 HandleScope scope(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000737
kasper.lund44510672008-07-25 07:37:58 +0000738 // Bail out if the index is invalid.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000739 if (index == -1) {
740 return false;
741 }
kasper.lund44510672008-07-25 07:37:58 +0000742
743 // Find source and name for the requested script.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000744 Handle<String> source_code =
lrn@chromium.org7516f052011-03-30 08:52:27 +0000745 isolate->bootstrapper()->NativesSourceLookup(index);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000746 Vector<const char> name = Natives::GetScriptName(index);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000747 Handle<String> script_name = factory->NewStringFromAscii(name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000748
749 // Compile the script.
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000750 Handle<SharedFunctionInfo> function_info;
751 function_info = Compiler::Compile(source_code,
752 script_name,
753 0, 0, NULL, NULL,
754 Handle<String>::null(),
755 NATIVES_CODE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000756
757 // Silently ignore stack overflows during compilation.
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000758 if (function_info.is_null()) {
lrn@chromium.org7516f052011-03-30 08:52:27 +0000759 ASSERT(isolate->has_pending_exception());
760 isolate->clear_pending_exception();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000761 return false;
762 }
763
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000764 // Execute the shared function in the debugger context.
lrn@chromium.org7516f052011-03-30 08:52:27 +0000765 Handle<Context> context = isolate->global_context();
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000766 bool caught_exception;
kasper.lund44510672008-07-25 07:37:58 +0000767 Handle<JSFunction> function =
lrn@chromium.org7516f052011-03-30 08:52:27 +0000768 factory->NewFunctionFromSharedFunctionInfo(function_info, context);
rossberg@chromium.org717967f2011-07-20 13:44:42 +0000769
fschneider@chromium.org7d10be52012-04-10 12:30:14 +0000770 Handle<Object> exception =
771 Execution::TryCall(function, Handle<Object>(context->global()),
772 0, NULL, &caught_exception);
kasper.lund44510672008-07-25 07:37:58 +0000773
774 // Check for caught exceptions.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000775 if (caught_exception) {
fschneider@chromium.org7d10be52012-04-10 12:30:14 +0000776 ASSERT(!isolate->has_pending_exception());
777 MessageLocation computed_location;
778 isolate->ComputeLocation(&computed_location);
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000779 Handle<Object> message = MessageHandler::MakeMessageObject(
fschneider@chromium.org7d10be52012-04-10 12:30:14 +0000780 "error_loading_debugger", &computed_location,
781 Vector<Handle<Object> >::empty(), Handle<String>(), Handle<JSArray>());
782 ASSERT(!isolate->has_pending_exception());
783 isolate->set_pending_exception(*exception);
karlklose@chromium.org44bc7082011-04-11 12:33:05 +0000784 MessageHandler::ReportMessage(Isolate::Current(), NULL, message);
fschneider@chromium.org7d10be52012-04-10 12:30:14 +0000785 isolate->clear_pending_exception();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000786 return false;
787 }
788
kasper.lund44510672008-07-25 07:37:58 +0000789 // Mark this script as native and return successfully.
790 Handle<Script> script(Script::cast(function->shared()->script()));
ager@chromium.orge2902be2009-06-08 12:21:35 +0000791 script->set_type(Smi::FromInt(Script::TYPE_NATIVE));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000792 return true;
793}
794
795
796bool Debug::Load() {
797 // Return if debugger is already loaded.
kasper.lund212ac232008-07-16 07:07:30 +0000798 if (IsLoaded()) return true;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000799
lrn@chromium.org7516f052011-03-30 08:52:27 +0000800 Debugger* debugger = isolate_->debugger();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000801
kasper.lund44510672008-07-25 07:37:58 +0000802 // Bail out if we're already in the process of compiling the native
803 // JavaScript source code for the debugger.
lrn@chromium.org7516f052011-03-30 08:52:27 +0000804 if (debugger->compiling_natives() ||
805 debugger->is_loading_debugger())
mads.s.agercbaa0602008-08-14 13:41:48 +0000806 return false;
lrn@chromium.org7516f052011-03-30 08:52:27 +0000807 debugger->set_loading_debugger(true);
kasper.lund44510672008-07-25 07:37:58 +0000808
809 // Disable breakpoints and interrupts while compiling and running the
810 // debugger scripts including the context creation code.
811 DisableBreak disable(true);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000812 PostponeInterruptsScope postpone(isolate_);
kasper.lund44510672008-07-25 07:37:58 +0000813
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000814 // Create the debugger context.
lrn@chromium.org7516f052011-03-30 08:52:27 +0000815 HandleScope scope(isolate_);
kasper.lund44510672008-07-25 07:37:58 +0000816 Handle<Context> context =
lrn@chromium.org7516f052011-03-30 08:52:27 +0000817 isolate_->bootstrapper()->CreateEnvironment(
danno@chromium.org160a7b02011-04-18 15:51:38 +0000818 isolate_,
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000819 Handle<Object>::null(),
820 v8::Handle<ObjectTemplate>(),
821 NULL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000822
fschneider@chromium.org7d10be52012-04-10 12:30:14 +0000823 // Fail if no context could be created.
824 if (context.is_null()) return false;
825
kasper.lund44510672008-07-25 07:37:58 +0000826 // Use the debugger context.
lrn@chromium.org7516f052011-03-30 08:52:27 +0000827 SaveContext save(isolate_);
828 isolate_->set_context(*context);
kasper.lund44510672008-07-25 07:37:58 +0000829
830 // Expose the builtins object in the debugger context.
lrn@chromium.org7516f052011-03-30 08:52:27 +0000831 Handle<String> key = isolate_->factory()->LookupAsciiSymbol("builtins");
kasper.lund44510672008-07-25 07:37:58 +0000832 Handle<GlobalObject> global = Handle<GlobalObject>(context->global());
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +0000833 RETURN_IF_EMPTY_HANDLE_VALUE(
lrn@chromium.org7516f052011-03-30 08:52:27 +0000834 isolate_,
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000835 JSReceiver::SetProperty(global, key, Handle<Object>(global->builtins()),
836 NONE, kNonStrictMode),
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +0000837 false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000838
839 // Compile the JavaScript for the debugger in the debugger context.
lrn@chromium.org7516f052011-03-30 08:52:27 +0000840 debugger->set_compiling_natives(true);
kasper.lund44510672008-07-25 07:37:58 +0000841 bool caught_exception =
842 !CompileDebuggerScript(Natives::GetIndex("mirror")) ||
843 !CompileDebuggerScript(Natives::GetIndex("debug"));
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000844
845 if (FLAG_enable_liveedit) {
846 caught_exception = caught_exception ||
847 !CompileDebuggerScript(Natives::GetIndex("liveedit"));
848 }
849
lrn@chromium.org7516f052011-03-30 08:52:27 +0000850 debugger->set_compiling_natives(false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000851
mads.s.agercbaa0602008-08-14 13:41:48 +0000852 // Make sure we mark the debugger as not loading before we might
853 // return.
lrn@chromium.org7516f052011-03-30 08:52:27 +0000854 debugger->set_loading_debugger(false);
mads.s.agercbaa0602008-08-14 13:41:48 +0000855
kasper.lund44510672008-07-25 07:37:58 +0000856 // Check for caught exceptions.
857 if (caught_exception) return false;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000858
859 // Debugger loaded.
whesse@chromium.org023421e2010-12-21 12:19:12 +0000860 debug_context_ = context;
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000861
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000862 return true;
863}
864
865
866void Debug::Unload() {
867 // Return debugger is not loaded.
868 if (!IsLoaded()) {
869 return;
870 }
871
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000872 // Clear the script cache.
873 DestroyScriptCache();
874
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000875 // Clear debugger context global handle.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000876 Isolate::Current()->global_handles()->Destroy(
877 reinterpret_cast<Object**>(debug_context_.location()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000878 debug_context_ = Handle<Context>();
879}
880
881
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000882// Set the flag indicating that preemption happened during debugging.
883void Debug::PreemptionWhileInDebugger() {
884 ASSERT(InDebugger());
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000885 Debug::set_interrupts_pending(PREEMPT);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000886}
887
888
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000889void Debug::Iterate(ObjectVisitor* v) {
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000890 v->VisitPointer(BitCast<Object**>(&(debug_break_return_)));
891 v->VisitPointer(BitCast<Object**>(&(debug_break_slot_)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000892}
893
894
jkummerow@chromium.org212d9642012-05-11 15:02:09 +0000895void Debug::PutValuesOnStackAndDie(int start,
896 Address c_entry_fp,
897 Address last_fp,
898 Address larger_fp,
899 int count,
yangguo@chromium.orgc74d6742012-06-29 15:15:45 +0000900 char* stack,
jkummerow@chromium.org212d9642012-05-11 15:02:09 +0000901 int end) {
yangguo@chromium.orgde0db002012-06-22 13:44:28 +0000902 OS::PrintError("start: %d\n", start);
903 OS::PrintError("c_entry_fp: %p\n", static_cast<void*>(c_entry_fp));
904 OS::PrintError("last_fp: %p\n", static_cast<void*>(last_fp));
905 OS::PrintError("larger_fp: %p\n", static_cast<void*>(larger_fp));
yangguo@chromium.orgde0db002012-06-22 13:44:28 +0000906 OS::PrintError("count: %d\n", count);
yangguo@chromium.orgc74d6742012-06-29 15:15:45 +0000907 if (stack != NULL) {
908 OS::PrintError("stack: %s\n", stack);
909 }
yangguo@chromium.orgde0db002012-06-22 13:44:28 +0000910 OS::PrintError("end: %d\n", end);
jkummerow@chromium.org212d9642012-05-11 15:02:09 +0000911 OS::Abort();
912}
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.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000961 Handle<Object> break_points_hit(heap->undefined_value());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000962 if (break_location_iterator.HasBreakPoint()) {
963 Handle<Object> break_point_objects =
964 Handle<Object>(break_location_iterator.BreakPointObjects());
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
jkummerow@chromium.org212d9642012-05-11 15:02:09 +00001012 // Catch the cases that would lead to crashes and capture
1013 // - C entry FP at which to start stack crawl.
1014 // - FP of the frame at which we plan to stop stepping out (last FP).
1015 // - current FP that's larger than last FP.
1016 // - Counter for the number of steps to step out.
yangguo@chromium.orgc74d6742012-06-29 15:15:45 +00001017 // - stack trace string.
jkummerow@chromium.org212d9642012-05-11 15:02:09 +00001018 if (it.done()) {
1019 // We crawled the entire stack, never reaching last_fp_.
1020 PutValuesOnStackAndDie(0xBEEEEEEE,
1021 frame->fp(),
1022 thread_local_.last_fp_,
yangguo@chromium.orgc74d6742012-06-29 15:15:45 +00001023 reinterpret_cast<Address>(0xDEADDEAD),
jkummerow@chromium.org212d9642012-05-11 15:02:09 +00001024 count,
yangguo@chromium.orgc74d6742012-06-29 15:15:45 +00001025 NULL,
1026 0xCEEEEEEE);
jkummerow@chromium.org212d9642012-05-11 15:02:09 +00001027 } else if (it.frame()->fp() != thread_local_.last_fp_) {
1028 // We crawled over last_fp_, without getting a match.
yangguo@chromium.orgc74d6742012-06-29 15:15:45 +00001029 Handle<String> stack = isolate_->StackTraceString();
svenpanne@chromium.org619781a2012-07-05 08:22:44 +00001030 char buffer[8192];
1031 String::WriteToFlat(*stack, buffer, 0, 8191);
yangguo@chromium.orgc74d6742012-06-29 15:15:45 +00001032 PutValuesOnStackAndDie(0xDEEEEEEE,
jkummerow@chromium.org212d9642012-05-11 15:02:09 +00001033 frame->fp(),
1034 thread_local_.last_fp_,
1035 it.frame()->fp(),
1036 count,
yangguo@chromium.orgc74d6742012-06-29 15:15:45 +00001037 buffer,
jkummerow@chromium.org212d9642012-05-11 15:02:09 +00001038 0xFEEEEEEE);
1039 }
1040
lrn@chromium.org34e60782011-09-15 07:25:40 +00001041 // If we found original frame
1042 if (it.frame()->fp() == thread_local_.last_fp_) {
1043 if (step_count > 1) {
1044 // Save old count and action to continue stepping after
1045 // StepOut
1046 thread_local_.queued_step_count_ = step_count - 1;
1047 }
1048
1049 // Set up for StepOut to reach target frame
1050 step_action = StepOut;
1051 step_count = count;
1052 }
1053 }
1054
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001055 // Clear all current stepping setup.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001056 ClearStepping();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001057
1058 // Set up for the remaining steps.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001059 PrepareStep(step_action, step_count);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001060 }
1061
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001062 if (thread_local_.frame_drop_mode_ == FRAMES_UNTOUCHED) {
1063 SetAfterBreakTarget(frame);
1064 } else if (thread_local_.frame_drop_mode_ ==
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001065 FRAME_DROPPED_IN_IC_CALL) {
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001066 // We must have been calling IC stub. Do not go there anymore.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001067 Code* plain_return = isolate_->builtins()->builtin(
1068 Builtins::kPlainReturn_LiveEdit);
1069 thread_local_.after_break_target_ = plain_return->entry();
1070 } else if (thread_local_.frame_drop_mode_ ==
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001071 FRAME_DROPPED_IN_DEBUG_SLOT_CALL) {
1072 // Debug break slot stub does not return normally, instead it manually
1073 // cleans the stack and jumps. We should patch the jump address.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001074 Code* plain_return = isolate_->builtins()->builtin(
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00001075 Builtins::kFrameDropper_LiveEdit);
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001076 thread_local_.after_break_target_ = plain_return->entry();
1077 } else if (thread_local_.frame_drop_mode_ ==
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001078 FRAME_DROPPED_IN_DIRECT_CALL) {
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001079 // Nothing to do, after_break_target is not used here.
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00001080 } else if (thread_local_.frame_drop_mode_ ==
1081 FRAME_DROPPED_IN_RETURN_CALL) {
1082 Code* plain_return = isolate_->builtins()->builtin(
1083 Builtins::kFrameDropper_LiveEdit);
1084 thread_local_.after_break_target_ = plain_return->entry();
ager@chromium.org357bf652010-04-12 11:30:10 +00001085 } else {
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001086 UNREACHABLE();
ager@chromium.org357bf652010-04-12 11:30:10 +00001087 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001088
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001089 return heap->undefined_value();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001090}
1091
1092
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001093RUNTIME_FUNCTION(Object*, Debug_Break) {
1094 return isolate->debug()->Break(args);
1095}
1096
1097
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001098// Check the break point objects for whether one or more are actually
1099// triggered. This function returns a JSArray with the break point objects
1100// which is triggered.
1101Handle<Object> Debug::CheckBreakPoints(Handle<Object> break_point_objects) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00001102 Factory* factory = isolate_->factory();
1103
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001104 // Count the number of break points hit. If there are multiple break points
1105 // they are in a FixedArray.
1106 Handle<FixedArray> break_points_hit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001107 int break_points_hit_count = 0;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001108 ASSERT(!break_point_objects->IsUndefined());
1109 if (break_point_objects->IsFixedArray()) {
1110 Handle<FixedArray> array(FixedArray::cast(*break_point_objects));
lrn@chromium.org7516f052011-03-30 08:52:27 +00001111 break_points_hit = factory->NewFixedArray(array->length());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001112 for (int i = 0; i < array->length(); i++) {
1113 Handle<Object> o(array->get(i));
1114 if (CheckBreakPoint(o)) {
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001115 break_points_hit->set(break_points_hit_count++, *o);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001116 }
1117 }
1118 } else {
lrn@chromium.org7516f052011-03-30 08:52:27 +00001119 break_points_hit = factory->NewFixedArray(1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001120 if (CheckBreakPoint(break_point_objects)) {
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001121 break_points_hit->set(break_points_hit_count++, *break_point_objects);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001122 }
1123 }
1124
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001125 // Return undefined if no break points were triggered.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001126 if (break_points_hit_count == 0) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00001127 return factory->undefined_value();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001128 }
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001129 // Return break points hit as a JSArray.
lrn@chromium.org7516f052011-03-30 08:52:27 +00001130 Handle<JSArray> result = factory->NewJSArrayWithElements(break_points_hit);
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001131 result->set_length(Smi::FromInt(break_points_hit_count));
1132 return result;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001133}
1134
1135
1136// Check whether a single break point object is triggered.
1137bool Debug::CheckBreakPoint(Handle<Object> break_point_object) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00001138 Factory* factory = isolate_->factory();
1139 HandleScope scope(isolate_);
ager@chromium.org381abbb2009-02-25 13:23:22 +00001140
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001141 // Ignore check if break point object is not a JSObject.
1142 if (!break_point_object->IsJSObject()) return true;
1143
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001144 // Get the function IsBreakPointTriggered (defined in debug-debugger.js).
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001145 Handle<String> is_break_point_triggered_symbol =
lrn@chromium.org7516f052011-03-30 08:52:27 +00001146 factory->LookupAsciiSymbol("IsBreakPointTriggered");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001147 Handle<JSFunction> check_break_point =
1148 Handle<JSFunction>(JSFunction::cast(
lrn@chromium.org303ada72010-10-27 09:33:13 +00001149 debug_context()->global()->GetPropertyNoExceptionThrown(
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001150 *is_break_point_triggered_symbol)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001151
1152 // Get the break id as an object.
lrn@chromium.org7516f052011-03-30 08:52:27 +00001153 Handle<Object> break_id = factory->NewNumberFromInt(Debug::break_id());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001154
1155 // Call HandleBreakPointx.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001156 bool caught_exception;
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00001157 Handle<Object> argv[] = { break_id, break_point_object };
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001158 Handle<Object> result = Execution::TryCall(check_break_point,
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00001159 isolate_->js_builtins_object(),
1160 ARRAY_SIZE(argv),
1161 argv,
1162 &caught_exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001163
1164 // If exception or non boolean result handle as not triggered
1165 if (caught_exception || !result->IsBoolean()) {
1166 return false;
1167 }
1168
1169 // Return whether the break point is triggered.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001170 ASSERT(!result.is_null());
1171 return (*result)->IsTrue();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001172}
1173
1174
1175// Check whether the function has debug information.
1176bool Debug::HasDebugInfo(Handle<SharedFunctionInfo> shared) {
1177 return !shared->debug_info()->IsUndefined();
1178}
1179
1180
kasper.lundbd3ec4e2008-07-09 11:06:54 +00001181// Return the debug info for this function. EnsureDebugInfo must be called
1182// prior to ensure the debug info has been generated for shared.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001183Handle<DebugInfo> Debug::GetDebugInfo(Handle<SharedFunctionInfo> shared) {
kasper.lundbd3ec4e2008-07-09 11:06:54 +00001184 ASSERT(HasDebugInfo(shared));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001185 return Handle<DebugInfo>(DebugInfo::cast(shared->debug_info()));
1186}
1187
1188
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001189void Debug::SetBreakPoint(Handle<JSFunction> function,
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00001190 Handle<Object> break_point_object,
1191 int* source_position) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00001192 HandleScope scope(isolate_);
ager@chromium.org381abbb2009-02-25 13:23:22 +00001193
lrn@chromium.org34e60782011-09-15 07:25:40 +00001194 PrepareForBreakPoints();
1195
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001196 // Make sure the function is compiled and has set up the debug info.
1197 Handle<SharedFunctionInfo> shared(function->shared());
1198 if (!EnsureDebugInfo(shared, function)) {
kasper.lundbd3ec4e2008-07-09 11:06:54 +00001199 // Return if retrieving debug info failed.
1200 return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001201 }
1202
kasper.lundbd3ec4e2008-07-09 11:06:54 +00001203 Handle<DebugInfo> debug_info = GetDebugInfo(shared);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001204 // Source positions starts with zero.
danno@chromium.orgbf0c8202011-12-27 10:09:42 +00001205 ASSERT(*source_position >= 0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001206
1207 // Find the break point and change it.
1208 BreakLocationIterator it(debug_info, SOURCE_BREAK_LOCATIONS);
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00001209 it.FindBreakLocationFromPosition(*source_position);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001210 it.SetBreakPoint(break_point_object);
1211
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00001212 *source_position = it.position();
1213
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001214 // At least one active break point now.
1215 ASSERT(debug_info->GetBreakPointCount() > 0);
1216}
1217
1218
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001219bool Debug::SetBreakPointForScript(Handle<Script> script,
1220 Handle<Object> break_point_object,
1221 int* source_position) {
1222 HandleScope scope(isolate_);
1223
1224 // No need to call PrepareForBreakPoints because it will be called
1225 // implicitly by Runtime::FindSharedFunctionInfoInScript.
1226 Object* result = Runtime::FindSharedFunctionInfoInScript(isolate_,
1227 script,
1228 *source_position);
1229 if (result->IsUndefined()) return false;
1230
1231 // Make sure the function has set up the debug info.
1232 Handle<SharedFunctionInfo> shared(SharedFunctionInfo::cast(result));
1233 if (!EnsureDebugInfo(shared, Handle<JSFunction>::null())) {
1234 // Return if retrieving debug info failed.
1235 return false;
1236 }
1237
1238 // Find position within function. The script position might be before the
1239 // source position of the first function.
1240 int position;
1241 if (shared->start_position() > *source_position) {
1242 position = 0;
1243 } else {
1244 position = *source_position - shared->start_position();
1245 }
1246
1247 Handle<DebugInfo> debug_info = GetDebugInfo(shared);
1248 // Source positions starts with zero.
1249 ASSERT(position >= 0);
1250
1251 // Find the break point and change it.
1252 BreakLocationIterator it(debug_info, SOURCE_BREAK_LOCATIONS);
1253 it.FindBreakLocationFromPosition(position);
1254 it.SetBreakPoint(break_point_object);
1255
1256 *source_position = it.position() + shared->start_position();
1257
1258 // At least one active break point now.
1259 ASSERT(debug_info->GetBreakPointCount() > 0);
1260 return true;
1261}
1262
1263
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001264void Debug::ClearBreakPoint(Handle<Object> break_point_object) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00001265 HandleScope scope(isolate_);
ager@chromium.org381abbb2009-02-25 13:23:22 +00001266
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001267 DebugInfoListNode* node = debug_info_list_;
1268 while (node != NULL) {
1269 Object* result = DebugInfo::FindBreakPointInfo(node->debug_info(),
1270 break_point_object);
1271 if (!result->IsUndefined()) {
1272 // Get information in the break point.
1273 BreakPointInfo* break_point_info = BreakPointInfo::cast(result);
1274 Handle<DebugInfo> debug_info = node->debug_info();
1275 Handle<SharedFunctionInfo> shared(debug_info->shared());
1276 int source_position = break_point_info->statement_position()->value();
1277
1278 // Source positions starts with zero.
1279 ASSERT(source_position >= 0);
1280
1281 // Find the break point and clear it.
1282 BreakLocationIterator it(debug_info, SOURCE_BREAK_LOCATIONS);
1283 it.FindBreakLocationFromPosition(source_position);
1284 it.ClearBreakPoint(break_point_object);
1285
1286 // If there are no more break points left remove the debug info for this
1287 // function.
1288 if (debug_info->GetBreakPointCount() == 0) {
1289 RemoveDebugInfo(debug_info);
1290 }
1291
1292 return;
1293 }
1294 node = node->next();
1295 }
1296}
1297
1298
ager@chromium.org381abbb2009-02-25 13:23:22 +00001299void Debug::ClearAllBreakPoints() {
1300 DebugInfoListNode* node = debug_info_list_;
1301 while (node != NULL) {
1302 // Remove all debug break code.
1303 BreakLocationIterator it(node->debug_info(), ALL_BREAK_LOCATIONS);
1304 it.ClearAllDebugBreak();
1305 node = node->next();
1306 }
1307
1308 // Remove all debug info.
1309 while (debug_info_list_ != NULL) {
1310 RemoveDebugInfo(debug_info_list_->debug_info());
1311 }
1312}
1313
1314
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001315void Debug::FloodWithOneShot(Handle<JSFunction> function) {
lrn@chromium.org34e60782011-09-15 07:25:40 +00001316 PrepareForBreakPoints();
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001317
1318 // Make sure the function is compiled and has set up the debug info.
1319 Handle<SharedFunctionInfo> shared(function->shared());
1320 if (!EnsureDebugInfo(shared, function)) {
kasper.lundbd3ec4e2008-07-09 11:06:54 +00001321 // Return if we failed to retrieve the debug info.
1322 return;
1323 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001324
1325 // Flood the function with break points.
kasper.lundbd3ec4e2008-07-09 11:06:54 +00001326 BreakLocationIterator it(GetDebugInfo(shared), ALL_BREAK_LOCATIONS);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001327 while (!it.Done()) {
1328 it.SetOneShot();
1329 it.Next();
1330 }
1331}
1332
1333
rossberg@chromium.org2c067b12012-03-19 11:01:52 +00001334void Debug::FloodBoundFunctionWithOneShot(Handle<JSFunction> function) {
1335 Handle<FixedArray> new_bindings(function->function_bindings());
1336 Handle<Object> bindee(new_bindings->get(JSFunction::kBoundFunctionIndex));
1337
1338 if (!bindee.is_null() && bindee->IsJSFunction() &&
1339 !JSFunction::cast(*bindee)->IsBuiltin()) {
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001340 Handle<JSFunction> bindee_function(JSFunction::cast(*bindee));
1341 Debug::FloodWithOneShot(bindee_function);
rossberg@chromium.org2c067b12012-03-19 11:01:52 +00001342 }
1343}
1344
1345
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001346void Debug::FloodHandlerWithOneShot() {
ager@chromium.org8bb60582008-12-11 12:02:20 +00001347 // Iterate through the JavaScript stack looking for handlers.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001348 StackFrame::Id id = break_frame_id();
ager@chromium.org8bb60582008-12-11 12:02:20 +00001349 if (id == StackFrame::NO_ID) {
1350 // If there is no JavaScript stack don't do anything.
1351 return;
1352 }
vegorov@chromium.org74f333b2011-04-06 11:17:46 +00001353 for (JavaScriptFrameIterator it(isolate_, id); !it.done(); it.Advance()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001354 JavaScriptFrame* frame = it.frame();
1355 if (frame->HasHandler()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001356 // Flood the function with the catch block with break points
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001357 JSFunction* function = JSFunction::cast(frame->function());
1358 FloodWithOneShot(Handle<JSFunction>(function));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001359 return;
1360 }
1361 }
1362}
1363
1364
1365void Debug::ChangeBreakOnException(ExceptionBreakType type, bool enable) {
1366 if (type == BreakUncaughtException) {
1367 break_on_uncaught_exception_ = enable;
1368 } else {
1369 break_on_exception_ = enable;
1370 }
1371}
1372
1373
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00001374bool Debug::IsBreakOnException(ExceptionBreakType type) {
1375 if (type == BreakUncaughtException) {
1376 return break_on_uncaught_exception_;
1377 } else {
1378 return break_on_exception_;
1379 }
1380}
1381
1382
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001383void Debug::PrepareStep(StepAction step_action, int step_count) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00001384 HandleScope scope(isolate_);
lrn@chromium.org34e60782011-09-15 07:25:40 +00001385
1386 PrepareForBreakPoints();
1387
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001388 ASSERT(Debug::InDebugger());
1389
1390 // Remember this step action and count.
1391 thread_local_.last_step_action_ = step_action;
ager@chromium.orga1645e22009-09-09 19:27:10 +00001392 if (step_action == StepOut) {
1393 // For step out target frame will be found on the stack so there is no need
1394 // to set step counter for it. It's expected to always be 0 for StepOut.
1395 thread_local_.step_count_ = 0;
1396 } else {
1397 thread_local_.step_count_ = step_count;
1398 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001399
1400 // Get the frame where the execution has stopped and skip the debug frame if
1401 // any. The debug frame will only be present if execution was stopped due to
1402 // hitting a break point. In other situations (e.g. unhandled exception) the
1403 // debug frame is not present.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001404 StackFrame::Id id = break_frame_id();
ager@chromium.org8bb60582008-12-11 12:02:20 +00001405 if (id == StackFrame::NO_ID) {
1406 // If there is no JavaScript stack don't do anything.
1407 return;
1408 }
vegorov@chromium.org74f333b2011-04-06 11:17:46 +00001409 JavaScriptFrameIterator frames_it(isolate_, id);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001410 JavaScriptFrame* frame = frames_it.frame();
1411
1412 // First of all ensure there is one-shot break points in the top handler
1413 // if any.
1414 FloodHandlerWithOneShot();
1415
1416 // If the function on the top frame is unresolved perform step out. This will
1417 // be the case when calling unknown functions and having the debugger stopped
1418 // in an unhandled exception.
1419 if (!frame->function()->IsJSFunction()) {
1420 // Step out: Find the calling JavaScript frame and flood it with
1421 // breakpoints.
1422 frames_it.Advance();
1423 // Fill the function to return to with one-shot break points.
1424 JSFunction* function = JSFunction::cast(frames_it.frame()->function());
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001425 FloodWithOneShot(Handle<JSFunction>(function));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001426 return;
1427 }
1428
1429 // Get the debug info (create it if it does not exist).
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001430 Handle<JSFunction> function(JSFunction::cast(frame->function()));
1431 Handle<SharedFunctionInfo> shared(function->shared());
1432 if (!EnsureDebugInfo(shared, function)) {
kasper.lundbd3ec4e2008-07-09 11:06:54 +00001433 // Return if ensuring debug info failed.
1434 return;
1435 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001436 Handle<DebugInfo> debug_info = GetDebugInfo(shared);
1437
1438 // Find the break location where execution has stopped.
1439 BreakLocationIterator it(debug_info, ALL_BREAK_LOCATIONS);
1440 it.FindBreakLocationFromAddress(frame->pc());
1441
1442 // Compute whether or not the target is a call target.
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001443 bool is_load_or_store = false;
1444 bool is_inline_cache_stub = false;
whesse@chromium.orge90029b2010-08-02 11:52:17 +00001445 bool is_at_restarted_function = false;
ager@chromium.orga1645e22009-09-09 19:27:10 +00001446 Handle<Code> call_function_stub;
ager@chromium.orga1645e22009-09-09 19:27:10 +00001447
whesse@chromium.orge90029b2010-08-02 11:52:17 +00001448 if (thread_local_.restarter_frame_function_pointer_ == NULL) {
1449 if (RelocInfo::IsCodeTarget(it.rinfo()->rmode())) {
1450 bool is_call_target = false;
1451 Address target = it.rinfo()->target_address();
1452 Code* code = Code::GetCodeFromTargetAddress(target);
1453 if (code->is_call_stub() || code->is_keyed_call_stub()) {
1454 is_call_target = true;
1455 }
1456 if (code->is_inline_cache_stub()) {
1457 is_inline_cache_stub = true;
1458 is_load_or_store = !is_call_target;
1459 }
1460
1461 // Check if target code is CallFunction stub.
1462 Code* maybe_call_function_stub = code;
1463 // If there is a breakpoint at this line look at the original code to
1464 // check if it is a CallFunction stub.
1465 if (it.IsDebugBreak()) {
1466 Address original_target = it.original_rinfo()->target_address();
1467 maybe_call_function_stub =
1468 Code::GetCodeFromTargetAddress(original_target);
1469 }
1470 if (maybe_call_function_stub->kind() == Code::STUB &&
1471 maybe_call_function_stub->major_key() == CodeStub::CallFunction) {
1472 // Save reference to the code as we may need it to find out arguments
1473 // count for 'step in' later.
1474 call_function_stub = Handle<Code>(maybe_call_function_stub);
1475 }
ager@chromium.orga1645e22009-09-09 19:27:10 +00001476 }
whesse@chromium.orge90029b2010-08-02 11:52:17 +00001477 } else {
1478 is_at_restarted_function = true;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001479 }
1480
v8.team.kasperl727e9952008-09-02 14:56:44 +00001481 // If this is the last break code target step out is the only possibility.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001482 if (it.IsExit() || step_action == StepOut) {
ager@chromium.orga1645e22009-09-09 19:27:10 +00001483 if (step_action == StepOut) {
1484 // Skip step_count frames starting with the current one.
1485 while (step_count-- > 0 && !frames_it.done()) {
1486 frames_it.Advance();
1487 }
1488 } else {
1489 ASSERT(it.IsExit());
1490 frames_it.Advance();
1491 }
1492 // Skip builtin functions on the stack.
1493 while (!frames_it.done() &&
1494 JSFunction::cast(frames_it.frame()->function())->IsBuiltin()) {
1495 frames_it.Advance();
1496 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001497 // Step out: If there is a JavaScript caller frame, we need to
1498 // flood it with breakpoints.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001499 if (!frames_it.done()) {
1500 // Fill the function to return to with one-shot break points.
1501 JSFunction* function = JSFunction::cast(frames_it.frame()->function());
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001502 FloodWithOneShot(Handle<JSFunction>(function));
ager@chromium.orga1645e22009-09-09 19:27:10 +00001503 // Set target frame pointer.
1504 ActivateStepOut(frames_it.frame());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001505 }
ager@chromium.orga1645e22009-09-09 19:27:10 +00001506 } else if (!(is_inline_cache_stub || RelocInfo::IsConstructCall(it.rmode()) ||
whesse@chromium.orge90029b2010-08-02 11:52:17 +00001507 !call_function_stub.is_null() || is_at_restarted_function)
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001508 || step_action == StepNext || step_action == StepMin) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001509 // Step next or step min.
1510
1511 // Fill the current function with one-shot break points.
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001512 FloodWithOneShot(function);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001513
1514 // Remember source position and frame to handle step next.
1515 thread_local_.last_statement_position_ =
1516 debug_info->code()->SourceStatementPosition(frame->pc());
mmassi@chromium.org7028c052012-06-13 11:51:58 +00001517 thread_local_.last_fp_ = frame->UnpaddedFP();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001518 } else {
whesse@chromium.orge90029b2010-08-02 11:52:17 +00001519 // If there's restarter frame on top of the stack, just get the pointer
1520 // to function which is going to be restarted.
1521 if (is_at_restarted_function) {
1522 Handle<JSFunction> restarted_function(
1523 JSFunction::cast(*thread_local_.restarter_frame_function_pointer_));
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001524 FloodWithOneShot(restarted_function);
whesse@chromium.orge90029b2010-08-02 11:52:17 +00001525 } else if (!call_function_stub.is_null()) {
1526 // If it's CallFunction stub ensure target function is compiled and flood
1527 // it with one shot breakpoints.
1528
ager@chromium.orga1645e22009-09-09 19:27:10 +00001529 // Find out number of arguments from the stub minor key.
1530 // Reverse lookup required as the minor key cannot be retrieved
1531 // from the code object.
1532 Handle<Object> obj(
lrn@chromium.org7516f052011-03-30 08:52:27 +00001533 isolate_->heap()->code_stubs()->SlowReverseLookup(
1534 *call_function_stub));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001535 ASSERT(!obj.is_null());
1536 ASSERT(!(*obj)->IsUndefined());
ager@chromium.orga1645e22009-09-09 19:27:10 +00001537 ASSERT(obj->IsSmi());
1538 // Get the STUB key and extract major and minor key.
1539 uint32_t key = Smi::cast(*obj)->value();
1540 // Argc in the stub is the number of arguments passed - not the
1541 // expected arguments of the called function.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001542 int call_function_arg_count =
1543 CallFunctionStub::ExtractArgcFromMinorKey(
1544 CodeStub::MinorKeyFromKey(key));
ager@chromium.orga1645e22009-09-09 19:27:10 +00001545 ASSERT(call_function_stub->major_key() ==
1546 CodeStub::MajorKeyFromKey(key));
1547
1548 // Find target function on the expression stack.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001549 // Expression stack looks like this (top to bottom):
ager@chromium.orga1645e22009-09-09 19:27:10 +00001550 // argN
1551 // ...
1552 // arg0
1553 // Receiver
1554 // Function to call
1555 int expressions_count = frame->ComputeExpressionsCount();
1556 ASSERT(expressions_count - 2 - call_function_arg_count >= 0);
1557 Object* fun = frame->GetExpression(
1558 expressions_count - 2 - call_function_arg_count);
1559 if (fun->IsJSFunction()) {
1560 Handle<JSFunction> js_function(JSFunction::cast(fun));
rossberg@chromium.org2c067b12012-03-19 11:01:52 +00001561 if (js_function->shared()->bound()) {
1562 Debug::FloodBoundFunctionWithOneShot(js_function);
1563 } else if (!js_function->IsBuiltin()) {
1564 // Don't step into builtins.
ager@chromium.orga1645e22009-09-09 19:27:10 +00001565 // It will also compile target function if it's not compiled yet.
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001566 FloodWithOneShot(js_function);
ager@chromium.orga1645e22009-09-09 19:27:10 +00001567 }
1568 }
1569 }
1570
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001571 // Fill the current function with one-shot break points even for step in on
1572 // a call target as the function called might be a native function for
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001573 // which step in will not stop. It also prepares for stepping in
1574 // getters/setters.
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001575 FloodWithOneShot(function);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001576
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001577 if (is_load_or_store) {
1578 // Remember source position and frame to handle step in getter/setter. If
1579 // there is a custom getter/setter it will be handled in
1580 // Object::Get/SetPropertyWithCallback, otherwise the step action will be
1581 // propagated on the next Debug::Break.
1582 thread_local_.last_statement_position_ =
1583 debug_info->code()->SourceStatementPosition(frame->pc());
mmassi@chromium.org7028c052012-06-13 11:51:58 +00001584 thread_local_.last_fp_ = frame->UnpaddedFP();
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001585 }
1586
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001587 // Step in or Step in min
1588 it.PrepareStepIn();
1589 ActivateStepIn(frame);
1590 }
1591}
1592
1593
1594// Check whether the current debug break should be reported to the debugger. It
1595// is used to have step next and step in only report break back to the debugger
1596// if on a different frame or in a different statement. In some situations
1597// there will be several break points in the same statement when the code is
v8.team.kasperl727e9952008-09-02 14:56:44 +00001598// flooded with one-shot break points. This function helps to perform several
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001599// steps before reporting break back to the debugger.
1600bool Debug::StepNextContinue(BreakLocationIterator* break_location_iterator,
1601 JavaScriptFrame* frame) {
lrn@chromium.org34e60782011-09-15 07:25:40 +00001602 // StepNext and StepOut shouldn't bring us deeper in code, so last frame
1603 // shouldn't be a parent of current frame.
1604 if (thread_local_.last_step_action_ == StepNext ||
1605 thread_local_.last_step_action_ == StepOut) {
1606 if (frame->fp() < thread_local_.last_fp_) return true;
1607 }
1608
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001609 // If the step last action was step next or step in make sure that a new
1610 // statement is hit.
1611 if (thread_local_.last_step_action_ == StepNext ||
1612 thread_local_.last_step_action_ == StepIn) {
1613 // Never continue if returning from function.
1614 if (break_location_iterator->IsExit()) return false;
1615
1616 // Continue if we are still on the same frame and in the same statement.
1617 int current_statement_position =
1618 break_location_iterator->code()->SourceStatementPosition(frame->pc());
mmassi@chromium.org7028c052012-06-13 11:51:58 +00001619 return thread_local_.last_fp_ == frame->UnpaddedFP() &&
ager@chromium.org236ad962008-09-25 09:45:57 +00001620 thread_local_.last_statement_position_ == current_statement_position;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001621 }
1622
1623 // No step next action - don't continue.
1624 return false;
1625}
1626
1627
1628// Check whether the code object at the specified address is a debug break code
1629// object.
1630bool Debug::IsDebugBreak(Address addr) {
ager@chromium.org8bb60582008-12-11 12:02:20 +00001631 Code* code = Code::GetCodeFromTargetAddress(addr);
kasper.lund7276f142008-07-30 08:49:36 +00001632 return code->ic_state() == DEBUG_BREAK;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001633}
1634
1635
1636// Check whether a code stub with the specified major key is a possible break
1637// point location when looking for source break locations.
1638bool Debug::IsSourceBreakStub(Code* code) {
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001639 CodeStub::Major major_key = CodeStub::GetMajorKey(code);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001640 return major_key == CodeStub::CallFunction;
1641}
1642
1643
1644// Check whether a code stub with the specified major key is a possible break
1645// location.
1646bool Debug::IsBreakStub(Code* code) {
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001647 CodeStub::Major major_key = CodeStub::GetMajorKey(code);
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00001648 return major_key == CodeStub::CallFunction;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001649}
1650
1651
1652// Find the builtin to use for invoking the debug break
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001653Handle<Code> Debug::FindDebugBreak(Handle<Code> code, RelocInfo::Mode mode) {
danno@chromium.orgfa458e42012-02-01 10:48:36 +00001654 Isolate* isolate = Isolate::Current();
1655
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001656 // Find the builtin debug break function matching the calling convention
1657 // used by the call site.
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001658 if (code->is_inline_cache_stub()) {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001659 switch (code->kind()) {
1660 case Code::CALL_IC:
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001661 case Code::KEYED_CALL_IC:
danno@chromium.orgfa458e42012-02-01 10:48:36 +00001662 return isolate->stub_cache()->ComputeCallDebugBreak(
1663 code->arguments_count(), code->kind());
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001664
1665 case Code::LOAD_IC:
danno@chromium.orgfa458e42012-02-01 10:48:36 +00001666 return isolate->builtins()->LoadIC_DebugBreak();
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001667
1668 case Code::STORE_IC:
danno@chromium.orgfa458e42012-02-01 10:48:36 +00001669 return isolate->builtins()->StoreIC_DebugBreak();
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001670
1671 case Code::KEYED_LOAD_IC:
danno@chromium.orgfa458e42012-02-01 10:48:36 +00001672 return isolate->builtins()->KeyedLoadIC_DebugBreak();
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001673
1674 case Code::KEYED_STORE_IC:
danno@chromium.orgfa458e42012-02-01 10:48:36 +00001675 return isolate->builtins()->KeyedStoreIC_DebugBreak();
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001676
1677 default:
1678 UNREACHABLE();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001679 }
1680 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001681 if (RelocInfo::IsConstructCall(mode)) {
danno@chromium.orgfa458e42012-02-01 10:48:36 +00001682 if (code->has_function_cache()) {
1683 return isolate->builtins()->CallConstructStub_Recording_DebugBreak();
1684 } else {
1685 return isolate->builtins()->CallConstructStub_DebugBreak();
1686 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001687 }
1688 if (code->kind() == Code::STUB) {
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00001689 ASSERT(code->major_key() == CodeStub::CallFunction);
danno@chromium.orgfa458e42012-02-01 10:48:36 +00001690 if (code->has_function_cache()) {
1691 return isolate->builtins()->CallFunctionStub_Recording_DebugBreak();
1692 } else {
1693 return isolate->builtins()->CallFunctionStub_DebugBreak();
1694 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001695 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001696
1697 UNREACHABLE();
1698 return Handle<Code>::null();
1699}
1700
1701
1702// Simple function for returning the source positions for active break points.
1703Handle<Object> Debug::GetSourceBreakLocations(
1704 Handle<SharedFunctionInfo> shared) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00001705 Isolate* isolate = Isolate::Current();
1706 Heap* heap = isolate->heap();
1707 if (!HasDebugInfo(shared)) return Handle<Object>(heap->undefined_value());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001708 Handle<DebugInfo> debug_info = GetDebugInfo(shared);
1709 if (debug_info->GetBreakPointCount() == 0) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00001710 return Handle<Object>(heap->undefined_value());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001711 }
1712 Handle<FixedArray> locations =
lrn@chromium.org7516f052011-03-30 08:52:27 +00001713 isolate->factory()->NewFixedArray(debug_info->GetBreakPointCount());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001714 int count = 0;
1715 for (int i = 0; i < debug_info->break_points()->length(); i++) {
1716 if (!debug_info->break_points()->get(i)->IsUndefined()) {
1717 BreakPointInfo* break_point_info =
1718 BreakPointInfo::cast(debug_info->break_points()->get(i));
1719 if (break_point_info->GetBreakPointCount() > 0) {
1720 locations->set(count++, break_point_info->statement_position());
1721 }
1722 }
1723 }
1724 return locations;
1725}
1726
1727
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001728void Debug::NewBreak(StackFrame::Id break_frame_id) {
1729 thread_local_.break_frame_id_ = break_frame_id;
1730 thread_local_.break_id_ = ++thread_local_.break_count_;
1731}
1732
1733
1734void Debug::SetBreak(StackFrame::Id break_frame_id, int break_id) {
1735 thread_local_.break_frame_id_ = break_frame_id;
1736 thread_local_.break_id_ = break_id;
1737}
1738
1739
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001740// Handle stepping into a function.
1741void Debug::HandleStepIn(Handle<JSFunction> function,
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001742 Handle<Object> holder,
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001743 Address fp,
1744 bool is_constructor) {
1745 // If the frame pointer is not supplied by the caller find it.
1746 if (fp == 0) {
1747 StackFrameIterator it;
1748 it.Advance();
1749 // For constructor functions skip another frame.
1750 if (is_constructor) {
1751 ASSERT(it.frame()->is_construct());
1752 it.Advance();
1753 }
1754 fp = it.frame()->fp();
1755 }
1756
1757 // Flood the function with one-shot break points if it is called from where
1758 // step into was requested.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001759 if (fp == step_in_fp()) {
rossberg@chromium.org2c067b12012-03-19 11:01:52 +00001760 if (function->shared()->bound()) {
1761 // Handle Function.prototype.bind
1762 Debug::FloodBoundFunctionWithOneShot(function);
1763 } else if (!function->IsBuiltin()) {
1764 // Don't allow step into functions in the native context.
kasperl@chromium.orgacae3782009-04-11 09:17:08 +00001765 if (function->shared()->code() ==
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00001766 Isolate::Current()->builtins()->builtin(Builtins::kFunctionApply) ||
kasperl@chromium.orgacae3782009-04-11 09:17:08 +00001767 function->shared()->code() ==
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00001768 Isolate::Current()->builtins()->builtin(Builtins::kFunctionCall)) {
kasperl@chromium.orgacae3782009-04-11 09:17:08 +00001769 // Handle function.apply and function.call separately to flood the
1770 // function to be called and not the code for Builtins::FunctionApply or
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001771 // Builtins::FunctionCall. The receiver of call/apply is the target
1772 // function.
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001773 if (!holder.is_null() && holder->IsJSFunction() &&
1774 !JSFunction::cast(*holder)->IsBuiltin()) {
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001775 Handle<JSFunction> js_function = Handle<JSFunction>::cast(holder);
1776 Debug::FloodWithOneShot(js_function);
kasperl@chromium.orgacae3782009-04-11 09:17:08 +00001777 }
1778 } else {
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001779 Debug::FloodWithOneShot(function);
kasperl@chromium.orgacae3782009-04-11 09:17:08 +00001780 }
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001781 }
1782 }
1783}
1784
1785
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001786void Debug::ClearStepping() {
1787 // Clear the various stepping setup.
1788 ClearOneShot();
1789 ClearStepIn();
ager@chromium.orga1645e22009-09-09 19:27:10 +00001790 ClearStepOut();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001791 ClearStepNext();
1792
1793 // Clear multiple step counter.
1794 thread_local_.step_count_ = 0;
1795}
1796
1797// Clears all the one-shot break points that are currently set. Normally this
1798// function is called each time a break point is hit as one shot break points
1799// are used to support stepping.
1800void Debug::ClearOneShot() {
1801 // The current implementation just runs through all the breakpoints. When the
v8.team.kasperl727e9952008-09-02 14:56:44 +00001802 // last break point for a function is removed that function is automatically
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001803 // removed from the list.
1804
1805 DebugInfoListNode* node = debug_info_list_;
1806 while (node != NULL) {
1807 BreakLocationIterator it(node->debug_info(), ALL_BREAK_LOCATIONS);
1808 while (!it.Done()) {
1809 it.ClearOneShot();
1810 it.Next();
1811 }
1812 node = node->next();
1813 }
1814}
1815
1816
1817void Debug::ActivateStepIn(StackFrame* frame) {
ager@chromium.orga1645e22009-09-09 19:27:10 +00001818 ASSERT(!StepOutActive());
mmassi@chromium.org7028c052012-06-13 11:51:58 +00001819 thread_local_.step_into_fp_ = frame->UnpaddedFP();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001820}
1821
1822
1823void Debug::ClearStepIn() {
1824 thread_local_.step_into_fp_ = 0;
1825}
1826
1827
ager@chromium.orga1645e22009-09-09 19:27:10 +00001828void Debug::ActivateStepOut(StackFrame* frame) {
1829 ASSERT(!StepInActive());
mmassi@chromium.org7028c052012-06-13 11:51:58 +00001830 thread_local_.step_out_fp_ = frame->UnpaddedFP();
ager@chromium.orga1645e22009-09-09 19:27:10 +00001831}
1832
1833
1834void Debug::ClearStepOut() {
1835 thread_local_.step_out_fp_ = 0;
1836}
1837
1838
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001839void Debug::ClearStepNext() {
1840 thread_local_.last_step_action_ = StepNone;
ager@chromium.org236ad962008-09-25 09:45:57 +00001841 thread_local_.last_statement_position_ = RelocInfo::kNoPosition;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001842 thread_local_.last_fp_ = 0;
1843}
1844
1845
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001846// Helper function to compile full code for debugging. This code will
mmassi@chromium.org7028c052012-06-13 11:51:58 +00001847// have debug break slots and deoptimization information. Deoptimization
1848// information is required in case that an optimized version of this
1849// function is still activated on the stack. It will also make sure that
1850// the full code is compiled with the same flags as the previous version,
1851// that is flags which can change the code generated. The current method
1852// of mapping from already compiled full code without debug break slots
1853// to full code with debug break slots depends on the generated code is
1854// otherwise exactly the same.
1855static bool CompileFullCodeForDebugging(Handle<JSFunction> function,
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001856 Handle<Code> current_code) {
1857 ASSERT(!current_code->has_debug_break_slots());
1858
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001859 CompilationInfoWithZone info(function);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001860 info.MarkCompilingForDebugging(current_code);
1861 ASSERT(!info.shared_info()->is_compiled());
1862 ASSERT(!info.isolate()->has_pending_exception());
1863
1864 // Use compile lazy which will end up compiling the full code in the
1865 // configuration configured above.
1866 bool result = Compiler::CompileLazy(&info);
1867 ASSERT(result != Isolate::Current()->has_pending_exception());
1868 info.isolate()->clear_pending_exception();
1869#if DEBUG
1870 if (result) {
mmassi@chromium.org7028c052012-06-13 11:51:58 +00001871 Handle<Code> new_code(function->shared()->code());
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001872 ASSERT(new_code->has_debug_break_slots());
1873 ASSERT(current_code->is_compiled_optimizable() ==
1874 new_code->is_compiled_optimizable());
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001875 }
1876#endif
1877 return result;
1878}
1879
1880
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001881static void CollectActiveFunctionsFromThread(
1882 Isolate* isolate,
1883 ThreadLocalTop* top,
1884 List<Handle<JSFunction> >* active_functions,
1885 Object* active_code_marker) {
1886 // Find all non-optimized code functions with activation frames
1887 // on the stack. This includes functions which have optimized
1888 // activations (including inlined functions) on the stack as the
1889 // non-optimized code is needed for the lazy deoptimization.
1890 for (JavaScriptFrameIterator it(isolate, top); !it.done(); it.Advance()) {
1891 JavaScriptFrame* frame = it.frame();
1892 if (frame->is_optimized()) {
1893 List<JSFunction*> functions(Compiler::kMaxInliningLevels + 1);
1894 frame->GetFunctions(&functions);
1895 for (int i = 0; i < functions.length(); i++) {
1896 JSFunction* function = functions[i];
1897 active_functions->Add(Handle<JSFunction>(function));
1898 function->shared()->code()->set_gc_metadata(active_code_marker);
1899 }
1900 } else if (frame->function()->IsJSFunction()) {
1901 JSFunction* function = JSFunction::cast(frame->function());
1902 ASSERT(frame->LookupCode()->kind() == Code::FUNCTION);
1903 active_functions->Add(Handle<JSFunction>(function));
1904 function->shared()->code()->set_gc_metadata(active_code_marker);
1905 }
1906 }
1907}
1908
1909
1910static void RedirectActivationsToRecompiledCodeOnThread(
1911 Isolate* isolate,
1912 ThreadLocalTop* top) {
1913 for (JavaScriptFrameIterator it(isolate, top); !it.done(); it.Advance()) {
1914 JavaScriptFrame* frame = it.frame();
1915
1916 if (frame->is_optimized() || !frame->function()->IsJSFunction()) continue;
1917
1918 JSFunction* function = JSFunction::cast(frame->function());
1919
1920 ASSERT(frame->LookupCode()->kind() == Code::FUNCTION);
1921
1922 Handle<Code> frame_code(frame->LookupCode());
1923 if (frame_code->has_debug_break_slots()) continue;
1924
1925 Handle<Code> new_code(function->shared()->code());
1926 if (new_code->kind() != Code::FUNCTION ||
1927 !new_code->has_debug_break_slots()) {
1928 continue;
1929 }
1930
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001931 // Iterate over the RelocInfo in the original code to compute the sum of the
1932 // constant pools sizes. (See Assembler::CheckConstPool())
1933 // Note that this is only useful for architectures using constant pools.
1934 int constpool_mask = RelocInfo::ModeMask(RelocInfo::CONST_POOL);
1935 int frame_const_pool_size = 0;
1936 for (RelocIterator it(*frame_code, constpool_mask); !it.done(); it.next()) {
1937 RelocInfo* info = it.rinfo();
1938 if (info->pc() >= frame->pc()) break;
1939 frame_const_pool_size += static_cast<int>(info->data());
1940 }
1941 intptr_t frame_offset =
1942 frame->pc() - frame_code->instruction_start() - frame_const_pool_size;
1943
1944 // Iterate over the RelocInfo for new code to find the number of bytes
1945 // generated for debug slots and constant pools.
1946 int debug_break_slot_bytes = 0;
1947 int new_code_const_pool_size = 0;
1948 int mask = RelocInfo::ModeMask(RelocInfo::DEBUG_BREAK_SLOT) |
1949 RelocInfo::ModeMask(RelocInfo::CONST_POOL);
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001950 for (RelocIterator it(*new_code, mask); !it.done(); it.next()) {
1951 // Check if the pc in the new code with debug break
1952 // slots is before this slot.
1953 RelocInfo* info = it.rinfo();
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001954 intptr_t new_offset = info->pc() - new_code->instruction_start() -
1955 new_code_const_pool_size - debug_break_slot_bytes;
1956 if (new_offset >= frame_offset) {
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001957 break;
1958 }
1959
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001960 if (RelocInfo::IsDebugBreakSlot(info->rmode())) {
1961 debug_break_slot_bytes += Assembler::kDebugBreakSlotLength;
1962 } else {
1963 ASSERT(RelocInfo::IsConstPool(info->rmode()));
1964 // The size of the constant pool is encoded in the data.
1965 new_code_const_pool_size += static_cast<int>(info->data());
1966 }
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001967 }
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001968
1969 // Compute the equivalent pc in the new code.
1970 byte* new_pc = new_code->instruction_start() + frame_offset +
1971 debug_break_slot_bytes + new_code_const_pool_size;
1972
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001973 if (FLAG_trace_deopt) {
1974 PrintF("Replacing code %08" V8PRIxPTR " - %08" V8PRIxPTR " (%d) "
1975 "with %08" V8PRIxPTR " - %08" V8PRIxPTR " (%d) "
1976 "for debugging, "
1977 "changing pc from %08" V8PRIxPTR " to %08" V8PRIxPTR "\n",
1978 reinterpret_cast<intptr_t>(
1979 frame_code->instruction_start()),
1980 reinterpret_cast<intptr_t>(
1981 frame_code->instruction_start()) +
1982 frame_code->instruction_size(),
1983 frame_code->instruction_size(),
1984 reinterpret_cast<intptr_t>(new_code->instruction_start()),
1985 reinterpret_cast<intptr_t>(new_code->instruction_start()) +
1986 new_code->instruction_size(),
1987 new_code->instruction_size(),
1988 reinterpret_cast<intptr_t>(frame->pc()),
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001989 reinterpret_cast<intptr_t>(new_pc));
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001990 }
1991
1992 // Patch the return address to return into the code with
1993 // debug break slots.
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001994 frame->set_pc(new_pc);
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001995 }
1996}
1997
1998
1999class ActiveFunctionsCollector : public ThreadVisitor {
2000 public:
2001 explicit ActiveFunctionsCollector(List<Handle<JSFunction> >* active_functions,
2002 Object* active_code_marker)
2003 : active_functions_(active_functions),
2004 active_code_marker_(active_code_marker) { }
2005
2006 void VisitThread(Isolate* isolate, ThreadLocalTop* top) {
2007 CollectActiveFunctionsFromThread(isolate,
2008 top,
2009 active_functions_,
2010 active_code_marker_);
2011 }
2012
2013 private:
2014 List<Handle<JSFunction> >* active_functions_;
2015 Object* active_code_marker_;
2016};
2017
2018
2019class ActiveFunctionsRedirector : public ThreadVisitor {
2020 public:
2021 void VisitThread(Isolate* isolate, ThreadLocalTop* top) {
2022 RedirectActivationsToRecompiledCodeOnThread(isolate, top);
2023 }
2024};
2025
2026
lrn@chromium.org34e60782011-09-15 07:25:40 +00002027void Debug::PrepareForBreakPoints() {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002028 // If preparing for the first break point make sure to deoptimize all
2029 // functions as debugging does not work with optimized code.
2030 if (!has_break_points_) {
2031 Deoptimizer::DeoptimizeAll();
lrn@chromium.org34e60782011-09-15 07:25:40 +00002032
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002033 Handle<Code> lazy_compile =
2034 Handle<Code>(isolate_->builtins()->builtin(Builtins::kLazyCompile));
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002035
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00002036 // There will be at least one break point when we are done.
2037 has_break_points_ = true;
2038
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002039 // Keep the list of activated functions in a handlified list as it
2040 // is used both in GC and non-GC code.
2041 List<Handle<JSFunction> > active_functions(100);
lrn@chromium.org34e60782011-09-15 07:25:40 +00002042
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002043 {
2044 // We are going to iterate heap to find all functions without
2045 // debug break slots.
rossberg@chromium.org994edf62012-02-06 10:12:55 +00002046 isolate_->heap()->CollectAllGarbage(Heap::kMakeHeapIterableMask,
2047 "preparing for breakpoints");
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002048
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002049 // Ensure no GC in this scope as we are going to use gc_metadata
2050 // field in the Code object to mark active functions.
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002051 AssertNoAllocation no_allocation;
2052
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002053 Object* active_code_marker = isolate_->heap()->the_hole_value();
svenpanne@chromium.orgecb9dd62011-12-01 08:22:35 +00002054
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002055 CollectActiveFunctionsFromThread(isolate_,
2056 isolate_->thread_local_top(),
2057 &active_functions,
2058 active_code_marker);
2059 ActiveFunctionsCollector active_functions_collector(&active_functions,
2060 active_code_marker);
2061 isolate_->thread_manager()->IterateArchivedThreads(
2062 &active_functions_collector);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002063
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002064 // Scan the heap for all non-optimized functions which have no
2065 // debug break slots and are not active or inlined into an active
2066 // function and mark them for lazy compilation.
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002067 HeapIterator iterator;
2068 HeapObject* obj = NULL;
2069 while (((obj = iterator.next()) != NULL)) {
2070 if (obj->IsJSFunction()) {
2071 JSFunction* function = JSFunction::cast(obj);
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002072 SharedFunctionInfo* shared = function->shared();
2073 if (shared->allows_lazy_compilation() &&
2074 shared->script()->IsScript() &&
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002075 function->code()->kind() == Code::FUNCTION &&
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002076 !function->code()->has_debug_break_slots() &&
2077 shared->code()->gc_metadata() != active_code_marker) {
2078 function->set_code(*lazy_compile);
2079 function->shared()->set_code(*lazy_compile);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002080 }
2081 }
lrn@chromium.org34e60782011-09-15 07:25:40 +00002082 }
lrn@chromium.org34e60782011-09-15 07:25:40 +00002083
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002084 // Clear gc_metadata field.
2085 for (int i = 0; i < active_functions.length(); i++) {
2086 Handle<JSFunction> function = active_functions[i];
2087 function->shared()->code()->set_gc_metadata(Smi::FromInt(0));
2088 }
2089 }
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002090
2091 // Now recompile all functions with activation frames and and
2092 // patch the return address to run in the new compiled code.
2093 for (int i = 0; i < active_functions.length(); i++) {
2094 Handle<JSFunction> function = active_functions[i];
mmassi@chromium.org7028c052012-06-13 11:51:58 +00002095 Handle<SharedFunctionInfo> shared(function->shared());
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002096
2097 if (function->code()->kind() == Code::FUNCTION &&
2098 function->code()->has_debug_break_slots()) {
2099 // Nothing to do. Function code already had debug break slots.
2100 continue;
2101 }
2102
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002103 // If recompilation is not possible just skip it.
2104 if (shared->is_toplevel() ||
2105 !shared->allows_lazy_compilation() ||
2106 shared->code()->kind() == Code::BUILTIN) {
2107 continue;
2108 }
2109
2110 // Make sure that the shared full code is compiled with debug
2111 // break slots.
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00002112 if (!shared->code()->has_debug_break_slots()) {
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002113 // Try to compile the full code with debug break slots. If it
2114 // fails just keep the current code.
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00002115 Handle<Code> current_code(function->shared()->code());
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002116 shared->set_code(*lazy_compile);
2117 bool prev_force_debugger_active =
2118 isolate_->debugger()->force_debugger_active();
2119 isolate_->debugger()->set_force_debugger_active(true);
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00002120 ASSERT(current_code->kind() == Code::FUNCTION);
mmassi@chromium.org7028c052012-06-13 11:51:58 +00002121 CompileFullCodeForDebugging(function, current_code);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002122 isolate_->debugger()->set_force_debugger_active(
2123 prev_force_debugger_active);
2124 if (!shared->is_compiled()) {
2125 shared->set_code(*current_code);
2126 continue;
2127 }
2128 }
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002129
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002130 // Keep function code in sync with shared function info.
2131 function->set_code(shared->code());
lrn@chromium.org34e60782011-09-15 07:25:40 +00002132 }
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002133
2134 RedirectActivationsToRecompiledCodeOnThread(isolate_,
2135 isolate_->thread_local_top());
2136
2137 ActiveFunctionsRedirector active_functions_redirector;
2138 isolate_->thread_manager()->IterateArchivedThreads(
2139 &active_functions_redirector);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002140 }
lrn@chromium.org34e60782011-09-15 07:25:40 +00002141}
2142
2143
2144// Ensures the debug information is present for shared.
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00002145bool Debug::EnsureDebugInfo(Handle<SharedFunctionInfo> shared,
2146 Handle<JSFunction> function) {
lrn@chromium.org34e60782011-09-15 07:25:40 +00002147 // Return if we already have the debug info for shared.
2148 if (HasDebugInfo(shared)) {
2149 ASSERT(shared->is_compiled());
2150 return true;
2151 }
2152
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00002153 // There will be at least one break point when we are done.
2154 has_break_points_ = true;
2155
2156 // Ensure function is compiled. Return false if this failed.
2157 if (!function.is_null() &&
2158 !JSFunction::EnsureCompiled(function, CLEAR_EXCEPTION)) {
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002159 return false;
2160 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002161
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002162 // Create the debug info object.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002163 Handle<DebugInfo> debug_info = FACTORY->NewDebugInfo(shared);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002164
2165 // Add debug info to the list.
2166 DebugInfoListNode* node = new DebugInfoListNode(*debug_info);
2167 node->set_next(debug_info_list_);
2168 debug_info_list_ = node;
2169
kasper.lundbd3ec4e2008-07-09 11:06:54 +00002170 return true;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002171}
2172
2173
2174void Debug::RemoveDebugInfo(Handle<DebugInfo> debug_info) {
2175 ASSERT(debug_info_list_ != NULL);
2176 // Run through the debug info objects to find this one and remove it.
2177 DebugInfoListNode* prev = NULL;
2178 DebugInfoListNode* current = debug_info_list_;
2179 while (current != NULL) {
2180 if (*current->debug_info() == *debug_info) {
2181 // Unlink from list. If prev is NULL we are looking at the first element.
2182 if (prev == NULL) {
2183 debug_info_list_ = current->next();
2184 } else {
2185 prev->set_next(current->next());
2186 }
lrn@chromium.org7516f052011-03-30 08:52:27 +00002187 current->debug_info()->shared()->set_debug_info(
2188 isolate_->heap()->undefined_value());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002189 delete current;
2190
2191 // If there are no more debug info objects there are not more break
2192 // points.
2193 has_break_points_ = debug_info_list_ != NULL;
2194
2195 return;
2196 }
2197 // Move to next in list.
2198 prev = current;
2199 current = current->next();
2200 }
2201 UNREACHABLE();
2202}
2203
2204
2205void Debug::SetAfterBreakTarget(JavaScriptFrame* frame) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002206 HandleScope scope(isolate_);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002207
lrn@chromium.org34e60782011-09-15 07:25:40 +00002208 PrepareForBreakPoints();
2209
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002210 // Get the executing function in which the debug break occurred.
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00002211 Handle<JSFunction> function(JSFunction::cast(frame->function()));
2212 Handle<SharedFunctionInfo> shared(function->shared());
2213 if (!EnsureDebugInfo(shared, function)) {
kasper.lundbd3ec4e2008-07-09 11:06:54 +00002214 // Return if we failed to retrieve the debug info.
2215 return;
2216 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002217 Handle<DebugInfo> debug_info = GetDebugInfo(shared);
2218 Handle<Code> code(debug_info->code());
2219 Handle<Code> original_code(debug_info->original_code());
2220#ifdef DEBUG
2221 // Get the code which is actually executing.
vegorov@chromium.org74f333b2011-04-06 11:17:46 +00002222 Handle<Code> frame_code(frame->LookupCode());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002223 ASSERT(frame_code.is_identical_to(code));
2224#endif
2225
2226 // Find the call address in the running code. This address holds the call to
2227 // either a DebugBreakXXX or to the debug break return entry code if the
2228 // break point is still active after processing the break point.
ager@chromium.org4af710e2009-09-15 12:20:11 +00002229 Address addr = frame->pc() - Assembler::kCallTargetAddressOffset;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002230
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002231 // Check if the location is at JS exit or debug break slot.
ager@chromium.orga1645e22009-09-09 19:27:10 +00002232 bool at_js_return = false;
2233 bool break_at_js_return_active = false;
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002234 bool at_debug_break_slot = false;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002235 RelocIterator it(debug_info->code());
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002236 while (!it.done() && !at_js_return && !at_debug_break_slot) {
ager@chromium.org236ad962008-09-25 09:45:57 +00002237 if (RelocInfo::IsJSReturn(it.rinfo()->rmode())) {
ager@chromium.orga1645e22009-09-09 19:27:10 +00002238 at_js_return = (it.rinfo()->pc() ==
2239 addr - Assembler::kPatchReturnSequenceAddressOffset);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002240 break_at_js_return_active = it.rinfo()->IsPatchedReturnSequence();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002241 }
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002242 if (RelocInfo::IsDebugBreakSlot(it.rinfo()->rmode())) {
2243 at_debug_break_slot = (it.rinfo()->pc() ==
2244 addr - Assembler::kPatchDebugBreakSlotAddressOffset);
2245 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002246 it.next();
2247 }
2248
2249 // Handle the jump to continue execution after break point depending on the
2250 // break location.
ager@chromium.orga1645e22009-09-09 19:27:10 +00002251 if (at_js_return) {
2252 // If the break point as return is still active jump to the corresponding
2253 // place in the original code. If not the break point was removed during
2254 // break point processing.
2255 if (break_at_js_return_active) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002256 addr += original_code->instruction_start() - code->instruction_start();
2257 }
2258
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00002259 // Move back to where the call instruction sequence started.
2260 thread_local_.after_break_target_ =
2261 addr - Assembler::kPatchReturnSequenceAddressOffset;
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002262 } else if (at_debug_break_slot) {
2263 // Address of where the debug break slot starts.
2264 addr = addr - Assembler::kPatchDebugBreakSlotAddressOffset;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002265
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002266 // Continue just after the slot.
2267 thread_local_.after_break_target_ = addr + Assembler::kDebugBreakSlotLength;
2268 } else if (IsDebugBreak(Assembler::target_address_at(addr))) {
2269 // We now know that there is still a debug break call at the target address,
2270 // so the break point is still there and the original code will hold the
2271 // address to jump to in order to complete the call which is replaced by a
2272 // call to DebugBreakXXX.
2273
2274 // Find the corresponding address in the original code.
2275 addr += original_code->instruction_start() - code->instruction_start();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002276
2277 // Install jump to the call address in the original code. This will be the
2278 // call which was overwritten by the call to DebugBreakXXX.
2279 thread_local_.after_break_target_ = Assembler::target_address_at(addr);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002280 } else {
2281 // There is no longer a break point present. Don't try to look in the
2282 // original code as the running code will have the right address. This takes
2283 // care of the case where the last break point is removed from the function
2284 // and therefore no "original code" is available.
2285 thread_local_.after_break_target_ = Assembler::target_address_at(addr);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002286 }
2287}
2288
2289
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00002290bool Debug::IsBreakAtReturn(JavaScriptFrame* frame) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002291 HandleScope scope(isolate_);
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00002292
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00002293 // If there are no break points this cannot be break at return, as
2294 // the debugger statement and stack guard bebug break cannot be at
2295 // return.
2296 if (!has_break_points_) {
2297 return false;
2298 }
2299
lrn@chromium.org34e60782011-09-15 07:25:40 +00002300 PrepareForBreakPoints();
2301
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00002302 // Get the executing function in which the debug break occurred.
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00002303 Handle<JSFunction> function(JSFunction::cast(frame->function()));
2304 Handle<SharedFunctionInfo> shared(function->shared());
2305 if (!EnsureDebugInfo(shared, function)) {
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00002306 // Return if we failed to retrieve the debug info.
2307 return false;
2308 }
2309 Handle<DebugInfo> debug_info = GetDebugInfo(shared);
2310 Handle<Code> code(debug_info->code());
2311#ifdef DEBUG
2312 // Get the code which is actually executing.
vegorov@chromium.org74f333b2011-04-06 11:17:46 +00002313 Handle<Code> frame_code(frame->LookupCode());
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00002314 ASSERT(frame_code.is_identical_to(code));
2315#endif
2316
2317 // Find the call address in the running code.
2318 Address addr = frame->pc() - Assembler::kCallTargetAddressOffset;
2319
2320 // Check if the location is at JS return.
2321 RelocIterator it(debug_info->code());
2322 while (!it.done()) {
2323 if (RelocInfo::IsJSReturn(it.rinfo()->rmode())) {
2324 return (it.rinfo()->pc() ==
2325 addr - Assembler::kPatchReturnSequenceAddressOffset);
2326 }
2327 it.next();
2328 }
2329 return false;
2330}
2331
2332
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00002333void Debug::FramesHaveBeenDropped(StackFrame::Id new_break_frame_id,
whesse@chromium.orge90029b2010-08-02 11:52:17 +00002334 FrameDropMode mode,
2335 Object** restarter_frame_function_pointer) {
ulan@chromium.orgd9e468a2012-06-25 09:47:40 +00002336 if (mode != CURRENTLY_SET_MODE) {
2337 thread_local_.frame_drop_mode_ = mode;
2338 }
ager@chromium.org357bf652010-04-12 11:30:10 +00002339 thread_local_.break_frame_id_ = new_break_frame_id;
whesse@chromium.orge90029b2010-08-02 11:52:17 +00002340 thread_local_.restarter_frame_function_pointer_ =
2341 restarter_frame_function_pointer;
ager@chromium.org357bf652010-04-12 11:30:10 +00002342}
2343
2344
jkummerow@chromium.org212d9642012-05-11 15:02:09 +00002345const int Debug::FramePaddingLayout::kInitialSize = 1;
2346
2347
2348// Any even value bigger than kInitialSize as needed for stack scanning.
2349const int Debug::FramePaddingLayout::kPaddingValue = kInitialSize + 1;
2350
2351
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002352bool Debug::IsDebugGlobal(GlobalObject* global) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002353 return IsLoaded() && global == debug_context()->global();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002354}
2355
2356
ager@chromium.org32912102009-01-16 10:38:43 +00002357void Debug::ClearMirrorCache() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002358 PostponeInterruptsScope postpone(isolate_);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002359 HandleScope scope(isolate_);
2360 ASSERT(isolate_->context() == *Debug::debug_context());
ager@chromium.org32912102009-01-16 10:38:43 +00002361
2362 // Clear the mirror cache.
2363 Handle<String> function_name =
lrn@chromium.org7516f052011-03-30 08:52:27 +00002364 isolate_->factory()->LookupSymbol(CStrVector("ClearMirrorCache"));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002365 Handle<Object> fun(Isolate::Current()->global()->GetPropertyNoExceptionThrown(
lrn@chromium.org303ada72010-10-27 09:33:13 +00002366 *function_name));
ager@chromium.org32912102009-01-16 10:38:43 +00002367 ASSERT(fun->IsJSFunction());
2368 bool caught_exception;
rossberg@chromium.org717967f2011-07-20 13:44:42 +00002369 Execution::TryCall(Handle<JSFunction>::cast(fun),
ager@chromium.org32912102009-01-16 10:38:43 +00002370 Handle<JSObject>(Debug::debug_context()->global()),
2371 0, NULL, &caught_exception);
2372}
2373
2374
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002375void Debug::CreateScriptCache() {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002376 Heap* heap = isolate_->heap();
2377 HandleScope scope(isolate_);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002378
2379 // Perform two GCs to get rid of all unreferenced scripts. The first GC gets
2380 // rid of all the cached script wrappers and the second gets rid of the
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002381 // scripts which are no longer referenced. The second also sweeps precisely,
2382 // which saves us doing yet another GC to make the heap iterable.
rossberg@chromium.org994edf62012-02-06 10:12:55 +00002383 heap->CollectAllGarbage(Heap::kNoGCFlags, "Debug::CreateScriptCache");
2384 heap->CollectAllGarbage(Heap::kMakeHeapIterableMask,
2385 "Debug::CreateScriptCache");
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002386
2387 ASSERT(script_cache_ == NULL);
2388 script_cache_ = new ScriptCache();
2389
2390 // Scan heap for Script objects.
2391 int count = 0;
2392 HeapIterator iterator;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002393 AssertNoAllocation no_allocation;
2394
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002395 for (HeapObject* obj = iterator.next(); obj != NULL; obj = iterator.next()) {
sgjesse@chromium.org152a0b02009-10-07 13:50:16 +00002396 if (obj->IsScript() && Script::cast(obj)->HasValidSource()) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002397 script_cache_->Add(Handle<Script>(Script::cast(obj)));
2398 count++;
2399 }
2400 }
2401}
2402
2403
2404void Debug::DestroyScriptCache() {
2405 // Get rid of the script cache if it was created.
2406 if (script_cache_ != NULL) {
2407 delete script_cache_;
2408 script_cache_ = NULL;
2409 }
2410}
2411
2412
2413void Debug::AddScriptToScriptCache(Handle<Script> script) {
2414 if (script_cache_ != NULL) {
2415 script_cache_->Add(script);
2416 }
2417}
2418
2419
2420Handle<FixedArray> Debug::GetLoadedScripts() {
2421 // Create and fill the script cache when the loaded scripts is requested for
2422 // the first time.
2423 if (script_cache_ == NULL) {
2424 CreateScriptCache();
2425 }
2426
2427 // If the script cache is not active just return an empty array.
2428 ASSERT(script_cache_ != NULL);
2429 if (script_cache_ == NULL) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002430 isolate_->factory()->NewFixedArray(0);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002431 }
2432
2433 // Perform GC to get unreferenced scripts evicted from the cache before
2434 // returning the content.
rossberg@chromium.org994edf62012-02-06 10:12:55 +00002435 isolate_->heap()->CollectAllGarbage(Heap::kNoGCFlags,
2436 "Debug::GetLoadedScripts");
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002437
2438 // Get the scripts from the cache.
2439 return script_cache_->GetScripts();
2440}
2441
2442
2443void Debug::AfterGarbageCollection() {
2444 // Generate events for collected scripts.
2445 if (script_cache_ != NULL) {
2446 script_cache_->ProcessCollectedScripts();
2447 }
2448}
2449
2450
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00002451Debugger::Debugger(Isolate* isolate)
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +00002452 : debugger_access_(isolate->debugger_access()),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002453 event_listener_(Handle<Object>()),
2454 event_listener_data_(Handle<Object>()),
2455 compiling_natives_(false),
2456 is_loading_debugger_(false),
2457 never_unload_debugger_(false),
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002458 force_debugger_active_(false),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002459 message_handler_(NULL),
2460 debugger_unload_pending_(false),
2461 host_dispatch_handler_(NULL),
2462 dispatch_handler_access_(OS::CreateMutex()),
2463 debug_message_dispatch_handler_(NULL),
2464 message_dispatch_helper_thread_(NULL),
2465 host_dispatch_micros_(100 * 1000),
2466 agent_(NULL),
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00002467 command_queue_(isolate->logger(), kQueueInitialSize),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002468 command_received_(OS::CreateSemaphore(0)),
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00002469 event_command_queue_(isolate->logger(), kQueueInitialSize),
2470 isolate_(isolate) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002471}
2472
2473
2474Debugger::~Debugger() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002475 delete dispatch_handler_access_;
2476 dispatch_handler_access_ = 0;
2477 delete command_received_;
2478 command_received_ = 0;
2479}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002480
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002481
2482Handle<Object> Debugger::MakeJSObject(Vector<const char> constructor_name,
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002483 int argc,
2484 Handle<Object> argv[],
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002485 bool* caught_exception) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002486 ASSERT(isolate_->context() == *isolate_->debug()->debug_context());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002487
2488 // Create the execution state object.
lrn@chromium.org7516f052011-03-30 08:52:27 +00002489 Handle<String> constructor_str =
2490 isolate_->factory()->LookupSymbol(constructor_name);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002491 Handle<Object> constructor(
2492 isolate_->global()->GetPropertyNoExceptionThrown(*constructor_str));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002493 ASSERT(constructor->IsJSFunction());
2494 if (!constructor->IsJSFunction()) {
2495 *caught_exception = true;
lrn@chromium.org7516f052011-03-30 08:52:27 +00002496 return isolate_->factory()->undefined_value();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002497 }
2498 Handle<Object> js_object = Execution::TryCall(
2499 Handle<JSFunction>::cast(constructor),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002500 Handle<JSObject>(isolate_->debug()->debug_context()->global()),
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002501 argc,
2502 argv,
2503 caught_exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002504 return js_object;
2505}
2506
2507
2508Handle<Object> Debugger::MakeExecutionState(bool* caught_exception) {
2509 // Create the execution state object.
lrn@chromium.org7516f052011-03-30 08:52:27 +00002510 Handle<Object> break_id = isolate_->factory()->NewNumberFromInt(
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002511 isolate_->debug()->break_id());
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002512 Handle<Object> argv[] = { break_id };
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002513 return MakeJSObject(CStrVector("MakeExecutionState"),
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002514 ARRAY_SIZE(argv),
2515 argv,
2516 caught_exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002517}
2518
2519
2520Handle<Object> Debugger::MakeBreakEvent(Handle<Object> exec_state,
2521 Handle<Object> break_points_hit,
2522 bool* caught_exception) {
2523 // Create the new break event object.
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002524 Handle<Object> argv[] = { exec_state, break_points_hit };
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002525 return MakeJSObject(CStrVector("MakeBreakEvent"),
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002526 ARRAY_SIZE(argv),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002527 argv,
2528 caught_exception);
2529}
2530
2531
2532Handle<Object> Debugger::MakeExceptionEvent(Handle<Object> exec_state,
2533 Handle<Object> exception,
2534 bool uncaught,
2535 bool* caught_exception) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002536 Factory* factory = isolate_->factory();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002537 // Create the new exception event object.
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002538 Handle<Object> argv[] = { exec_state,
2539 exception,
2540 factory->ToBoolean(uncaught) };
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002541 return MakeJSObject(CStrVector("MakeExceptionEvent"),
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002542 ARRAY_SIZE(argv),
2543 argv,
2544 caught_exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002545}
2546
2547
2548Handle<Object> Debugger::MakeNewFunctionEvent(Handle<Object> function,
2549 bool* caught_exception) {
2550 // Create the new function event object.
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002551 Handle<Object> argv[] = { function };
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002552 return MakeJSObject(CStrVector("MakeNewFunctionEvent"),
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002553 ARRAY_SIZE(argv),
2554 argv,
2555 caught_exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002556}
2557
2558
2559Handle<Object> Debugger::MakeCompileEvent(Handle<Script> script,
iposva@chromium.org245aa852009-02-10 00:49:54 +00002560 bool before,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002561 bool* caught_exception) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002562 Factory* factory = isolate_->factory();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002563 // Create the compile event object.
2564 Handle<Object> exec_state = MakeExecutionState(caught_exception);
iposva@chromium.org245aa852009-02-10 00:49:54 +00002565 Handle<Object> script_wrapper = GetScriptWrapper(script);
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002566 Handle<Object> argv[] = { exec_state,
2567 script_wrapper,
2568 factory->ToBoolean(before) };
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002569 return MakeJSObject(CStrVector("MakeCompileEvent"),
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002570 ARRAY_SIZE(argv),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002571 argv,
2572 caught_exception);
2573}
2574
2575
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002576Handle<Object> Debugger::MakeScriptCollectedEvent(int id,
2577 bool* caught_exception) {
2578 // Create the script collected event object.
2579 Handle<Object> exec_state = MakeExecutionState(caught_exception);
2580 Handle<Object> id_object = Handle<Smi>(Smi::FromInt(id));
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002581 Handle<Object> argv[] = { exec_state, id_object };
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002582
2583 return MakeJSObject(CStrVector("MakeScriptCollectedEvent"),
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002584 ARRAY_SIZE(argv),
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002585 argv,
2586 caught_exception);
2587}
2588
2589
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002590void Debugger::OnException(Handle<Object> exception, bool uncaught) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002591 HandleScope scope(isolate_);
2592 Debug* debug = isolate_->debug();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002593
2594 // Bail out based on state or if there is no listener for this event
lrn@chromium.org7516f052011-03-30 08:52:27 +00002595 if (debug->InDebugger()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002596 if (!Debugger::EventActive(v8::Exception)) return;
2597
2598 // Bail out if exception breaks are not active
2599 if (uncaught) {
2600 // Uncaught exceptions are reported by either flags.
lrn@chromium.org7516f052011-03-30 08:52:27 +00002601 if (!(debug->break_on_uncaught_exception() ||
2602 debug->break_on_exception())) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002603 } else {
2604 // Caught exceptions are reported is activated.
lrn@chromium.org7516f052011-03-30 08:52:27 +00002605 if (!debug->break_on_exception()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002606 }
2607
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002608 // Enter the debugger.
2609 EnterDebugger debugger;
2610 if (debugger.FailedToEnter()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002611
2612 // Clear all current stepping setup.
lrn@chromium.org7516f052011-03-30 08:52:27 +00002613 debug->ClearStepping();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002614 // Create the event data object.
2615 bool caught_exception = false;
2616 Handle<Object> exec_state = MakeExecutionState(&caught_exception);
2617 Handle<Object> event_data;
2618 if (!caught_exception) {
2619 event_data = MakeExceptionEvent(exec_state, exception, uncaught,
2620 &caught_exception);
2621 }
2622 // Bail out and don't call debugger if exception.
2623 if (caught_exception) {
2624 return;
2625 }
2626
ager@chromium.org5ec48922009-05-05 07:25:34 +00002627 // Process debug event.
2628 ProcessDebugEvent(v8::Exception, Handle<JSObject>::cast(event_data), false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002629 // Return to continue execution from where the exception was thrown.
2630}
2631
2632
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002633void Debugger::OnDebugBreak(Handle<Object> break_points_hit,
2634 bool auto_continue) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002635 HandleScope scope(isolate_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002636
kasper.lund212ac232008-07-16 07:07:30 +00002637 // Debugger has already been entered by caller.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002638 ASSERT(isolate_->context() == *isolate_->debug()->debug_context());
kasper.lund212ac232008-07-16 07:07:30 +00002639
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002640 // Bail out if there is no listener for this event
2641 if (!Debugger::EventActive(v8::Break)) return;
2642
2643 // Debugger must be entered in advance.
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00002644 ASSERT(isolate_->context() == *isolate_->debug()->debug_context());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002645
2646 // Create the event data object.
2647 bool caught_exception = false;
2648 Handle<Object> exec_state = MakeExecutionState(&caught_exception);
2649 Handle<Object> event_data;
2650 if (!caught_exception) {
2651 event_data = MakeBreakEvent(exec_state, break_points_hit,
2652 &caught_exception);
2653 }
2654 // Bail out and don't call debugger if exception.
2655 if (caught_exception) {
2656 return;
2657 }
2658
ager@chromium.org5ec48922009-05-05 07:25:34 +00002659 // Process debug event.
2660 ProcessDebugEvent(v8::Break,
2661 Handle<JSObject>::cast(event_data),
2662 auto_continue);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002663}
2664
2665
2666void Debugger::OnBeforeCompile(Handle<Script> script) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002667 HandleScope scope(isolate_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002668
2669 // Bail out based on state or if there is no listener for this event
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002670 if (isolate_->debug()->InDebugger()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002671 if (compiling_natives()) return;
2672 if (!EventActive(v8::BeforeCompile)) return;
2673
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002674 // Enter the debugger.
2675 EnterDebugger debugger;
2676 if (debugger.FailedToEnter()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002677
2678 // Create the event data object.
2679 bool caught_exception = false;
iposva@chromium.org245aa852009-02-10 00:49:54 +00002680 Handle<Object> event_data = MakeCompileEvent(script, true, &caught_exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002681 // Bail out and don't call debugger if exception.
2682 if (caught_exception) {
2683 return;
2684 }
2685
ager@chromium.org5ec48922009-05-05 07:25:34 +00002686 // Process debug event.
2687 ProcessDebugEvent(v8::BeforeCompile,
2688 Handle<JSObject>::cast(event_data),
2689 true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002690}
2691
2692
2693// Handle debugger actions when a new script is compiled.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00002694void Debugger::OnAfterCompile(Handle<Script> script,
2695 AfterCompileFlags after_compile_flags) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002696 HandleScope scope(isolate_);
2697 Debug* debug = isolate_->debug();
kasper.lund212ac232008-07-16 07:07:30 +00002698
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002699 // Add the newly compiled script to the script cache.
lrn@chromium.org7516f052011-03-30 08:52:27 +00002700 debug->AddScriptToScriptCache(script);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002701
2702 // No more to do if not debugging.
ager@chromium.org71daaf62009-04-01 07:22:49 +00002703 if (!IsDebuggerActive()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002704
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002705 // No compile events while compiling natives.
2706 if (compiling_natives()) return;
2707
iposva@chromium.org245aa852009-02-10 00:49:54 +00002708 // Store whether in debugger before entering debugger.
lrn@chromium.org7516f052011-03-30 08:52:27 +00002709 bool in_debugger = debug->InDebugger();
iposva@chromium.org245aa852009-02-10 00:49:54 +00002710
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002711 // Enter the debugger.
2712 EnterDebugger debugger;
2713 if (debugger.FailedToEnter()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002714
2715 // If debugging there might be script break points registered for this
2716 // script. Make sure that these break points are set.
2717
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002718 // Get the function UpdateScriptBreakPoints (defined in debug-debugger.js).
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002719 Handle<String> update_script_break_points_symbol =
lrn@chromium.org7516f052011-03-30 08:52:27 +00002720 isolate_->factory()->LookupAsciiSymbol("UpdateScriptBreakPoints");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002721 Handle<Object> update_script_break_points =
lrn@chromium.org7516f052011-03-30 08:52:27 +00002722 Handle<Object>(debug->debug_context()->global()->
lrn@chromium.org303ada72010-10-27 09:33:13 +00002723 GetPropertyNoExceptionThrown(*update_script_break_points_symbol));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002724 if (!update_script_break_points->IsJSFunction()) {
2725 return;
2726 }
2727 ASSERT(update_script_break_points->IsJSFunction());
2728
2729 // Wrap the script object in a proper JS object before passing it
2730 // to JavaScript.
2731 Handle<JSValue> wrapper = GetScriptWrapper(script);
2732
2733 // Call UpdateScriptBreakPoints expect no exceptions.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002734 bool caught_exception;
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002735 Handle<Object> argv[] = { wrapper };
rossberg@chromium.org717967f2011-07-20 13:44:42 +00002736 Execution::TryCall(Handle<JSFunction>::cast(update_script_break_points),
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002737 Isolate::Current()->js_builtins_object(),
2738 ARRAY_SIZE(argv),
2739 argv,
2740 &caught_exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002741 if (caught_exception) {
2742 return;
2743 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002744 // Bail out based on state or if there is no listener for this event
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00002745 if (in_debugger && (after_compile_flags & SEND_WHEN_DEBUGGING) == 0) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002746 if (!Debugger::EventActive(v8::AfterCompile)) return;
2747
2748 // Create the compile state object.
2749 Handle<Object> event_data = MakeCompileEvent(script,
iposva@chromium.org245aa852009-02-10 00:49:54 +00002750 false,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002751 &caught_exception);
2752 // Bail out and don't call debugger if exception.
2753 if (caught_exception) {
2754 return;
2755 }
ager@chromium.org5ec48922009-05-05 07:25:34 +00002756 // Process debug event.
2757 ProcessDebugEvent(v8::AfterCompile,
2758 Handle<JSObject>::cast(event_data),
2759 true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002760}
2761
2762
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002763void Debugger::OnScriptCollected(int id) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002764 HandleScope scope(isolate_);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002765
2766 // No more to do if not debugging.
2767 if (!IsDebuggerActive()) return;
2768 if (!Debugger::EventActive(v8::ScriptCollected)) return;
2769
2770 // Enter the debugger.
2771 EnterDebugger debugger;
2772 if (debugger.FailedToEnter()) return;
2773
2774 // Create the script collected state object.
2775 bool caught_exception = false;
2776 Handle<Object> event_data = MakeScriptCollectedEvent(id,
2777 &caught_exception);
2778 // Bail out and don't call debugger if exception.
2779 if (caught_exception) {
2780 return;
2781 }
2782
2783 // Process debug event.
2784 ProcessDebugEvent(v8::ScriptCollected,
2785 Handle<JSObject>::cast(event_data),
2786 true);
2787}
2788
2789
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002790void Debugger::ProcessDebugEvent(v8::DebugEvent event,
ager@chromium.org5ec48922009-05-05 07:25:34 +00002791 Handle<JSObject> event_data,
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002792 bool auto_continue) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002793 HandleScope scope(isolate_);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002794
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +00002795 // Clear any pending debug break if this is a real break.
2796 if (!auto_continue) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002797 isolate_->debug()->clear_interrupt_pending(DEBUGBREAK);
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +00002798 }
2799
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002800 // Create the execution state.
2801 bool caught_exception = false;
2802 Handle<Object> exec_state = MakeExecutionState(&caught_exception);
2803 if (caught_exception) {
2804 return;
2805 }
ager@chromium.org41826e72009-03-30 13:30:57 +00002806 // First notify the message handler if any.
2807 if (message_handler_ != NULL) {
ager@chromium.org5ec48922009-05-05 07:25:34 +00002808 NotifyMessageHandler(event,
2809 Handle<JSObject>::cast(exec_state),
2810 event_data,
2811 auto_continue);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002812 }
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00002813 // Notify registered debug event listener. This can be either a C or
2814 // a JavaScript function. Don't call event listener for v8::Break
2815 // here, if it's only a debug command -- they will be processed later.
2816 if ((event != v8::Break || !auto_continue) && !event_listener_.is_null()) {
2817 CallEventCallback(event, exec_state, event_data, NULL);
2818 }
2819 // Process pending debug commands.
2820 if (event == v8::Break) {
2821 while (!event_command_queue_.IsEmpty()) {
2822 CommandMessage command = event_command_queue_.Get();
2823 if (!event_listener_.is_null()) {
2824 CallEventCallback(v8::BreakForCommand,
2825 exec_state,
2826 event_data,
2827 command.client_data());
2828 }
2829 command.Dispose();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002830 }
2831 }
2832}
2833
2834
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00002835void Debugger::CallEventCallback(v8::DebugEvent event,
2836 Handle<Object> exec_state,
2837 Handle<Object> event_data,
2838 v8::Debug::ClientData* client_data) {
ager@chromium.orgea91cc52011-05-23 06:06:11 +00002839 if (event_listener_->IsForeign()) {
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00002840 CallCEventCallback(event, exec_state, event_data, client_data);
2841 } else {
2842 CallJSEventCallback(event, exec_state, event_data);
2843 }
2844}
2845
2846
2847void Debugger::CallCEventCallback(v8::DebugEvent event,
2848 Handle<Object> exec_state,
2849 Handle<Object> event_data,
2850 v8::Debug::ClientData* client_data) {
ager@chromium.orgea91cc52011-05-23 06:06:11 +00002851 Handle<Foreign> callback_obj(Handle<Foreign>::cast(event_listener_));
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00002852 v8::Debug::EventCallback2 callback =
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002853 FUNCTION_CAST<v8::Debug::EventCallback2>(
2854 callback_obj->foreign_address());
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00002855 EventDetailsImpl event_details(
2856 event,
2857 Handle<JSObject>::cast(exec_state),
2858 Handle<JSObject>::cast(event_data),
2859 event_listener_data_,
2860 client_data);
2861 callback(event_details);
2862}
2863
2864
2865void Debugger::CallJSEventCallback(v8::DebugEvent event,
2866 Handle<Object> exec_state,
2867 Handle<Object> event_data) {
2868 ASSERT(event_listener_->IsJSFunction());
2869 Handle<JSFunction> fun(Handle<JSFunction>::cast(event_listener_));
2870
2871 // Invoke the JavaScript debug event listener.
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002872 Handle<Object> argv[] = { Handle<Object>(Smi::FromInt(event)),
2873 exec_state,
2874 event_data,
2875 event_listener_data_ };
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002876 bool caught_exception;
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002877 Execution::TryCall(fun,
2878 isolate_->global(),
2879 ARRAY_SIZE(argv),
2880 argv,
2881 &caught_exception);
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00002882 // Silently ignore exceptions from debug event listeners.
2883}
2884
2885
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00002886Handle<Context> Debugger::GetDebugContext() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002887 never_unload_debugger_ = true;
2888 EnterDebugger debugger;
2889 return isolate_->debug()->debug_context();
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00002890}
2891
2892
ager@chromium.org71daaf62009-04-01 07:22:49 +00002893void Debugger::UnloadDebugger() {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002894 Debug* debug = isolate_->debug();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002895
ager@chromium.org71daaf62009-04-01 07:22:49 +00002896 // Make sure that there are no breakpoints left.
lrn@chromium.org7516f052011-03-30 08:52:27 +00002897 debug->ClearAllBreakPoints();
ager@chromium.org71daaf62009-04-01 07:22:49 +00002898
2899 // Unload the debugger if feasible.
2900 if (!never_unload_debugger_) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002901 debug->Unload();
ager@chromium.org71daaf62009-04-01 07:22:49 +00002902 }
2903
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002904 // Clear the flag indicating that the debugger should be unloaded.
2905 debugger_unload_pending_ = false;
ager@chromium.org71daaf62009-04-01 07:22:49 +00002906}
2907
2908
ager@chromium.org41826e72009-03-30 13:30:57 +00002909void Debugger::NotifyMessageHandler(v8::DebugEvent event,
ager@chromium.org5ec48922009-05-05 07:25:34 +00002910 Handle<JSObject> exec_state,
2911 Handle<JSObject> event_data,
ager@chromium.org41826e72009-03-30 13:30:57 +00002912 bool auto_continue) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002913 HandleScope scope(isolate_);
ager@chromium.org41826e72009-03-30 13:30:57 +00002914
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002915 if (!isolate_->debug()->Load()) return;
ager@chromium.org41826e72009-03-30 13:30:57 +00002916
2917 // Process the individual events.
ager@chromium.org5ec48922009-05-05 07:25:34 +00002918 bool sendEventMessage = false;
ager@chromium.org41826e72009-03-30 13:30:57 +00002919 switch (event) {
2920 case v8::Break:
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00002921 case v8::BreakForCommand:
ager@chromium.org5ec48922009-05-05 07:25:34 +00002922 sendEventMessage = !auto_continue;
ager@chromium.org41826e72009-03-30 13:30:57 +00002923 break;
2924 case v8::Exception:
ager@chromium.org5ec48922009-05-05 07:25:34 +00002925 sendEventMessage = true;
ager@chromium.org41826e72009-03-30 13:30:57 +00002926 break;
2927 case v8::BeforeCompile:
2928 break;
2929 case v8::AfterCompile:
ager@chromium.org5ec48922009-05-05 07:25:34 +00002930 sendEventMessage = true;
ager@chromium.org41826e72009-03-30 13:30:57 +00002931 break;
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002932 case v8::ScriptCollected:
2933 sendEventMessage = true;
2934 break;
ager@chromium.org41826e72009-03-30 13:30:57 +00002935 case v8::NewFunction:
2936 break;
2937 default:
2938 UNREACHABLE();
2939 }
2940
ager@chromium.org5ec48922009-05-05 07:25:34 +00002941 // The debug command interrupt flag might have been set when the command was
2942 // added. It should be enough to clear the flag only once while we are in the
2943 // debugger.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002944 ASSERT(isolate_->debug()->InDebugger());
2945 isolate_->stack_guard()->Continue(DEBUGCOMMAND);
ager@chromium.org5ec48922009-05-05 07:25:34 +00002946
2947 // Notify the debugger that a debug event has occurred unless auto continue is
2948 // active in which case no event is send.
2949 if (sendEventMessage) {
2950 MessageImpl message = MessageImpl::NewEvent(
2951 event,
2952 auto_continue,
2953 Handle<JSObject>::cast(exec_state),
2954 Handle<JSObject>::cast(event_data));
2955 InvokeMessageHandler(message);
2956 }
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +00002957
2958 // If auto continue don't make the event cause a break, but process messages
2959 // in the queue if any. For script collected events don't even process
2960 // messages in the queue as the execution state might not be what is expected
2961 // by the client.
ager@chromium.org6ffc2172009-05-29 19:20:16 +00002962 if ((auto_continue && !HasCommands()) || event == v8::ScriptCollected) {
ager@chromium.org5ec48922009-05-05 07:25:34 +00002963 return;
2964 }
ager@chromium.org41826e72009-03-30 13:30:57 +00002965
ager@chromium.org41826e72009-03-30 13:30:57 +00002966 v8::TryCatch try_catch;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002967
2968 // DebugCommandProcessor goes here.
2969 v8::Local<v8::Object> cmd_processor;
2970 {
2971 v8::Local<v8::Object> api_exec_state =
2972 v8::Utils::ToLocal(Handle<JSObject>::cast(exec_state));
2973 v8::Local<v8::String> fun_name =
2974 v8::String::New("debugCommandProcessor");
2975 v8::Local<v8::Function> fun =
2976 v8::Function::Cast(*api_exec_state->Get(fun_name));
2977
2978 v8::Handle<v8::Boolean> running =
2979 auto_continue ? v8::True() : v8::False();
2980 static const int kArgc = 1;
2981 v8::Handle<Value> argv[kArgc] = { running };
2982 cmd_processor = v8::Object::Cast(*fun->Call(api_exec_state, kArgc, argv));
2983 if (try_catch.HasCaught()) {
2984 PrintLn(try_catch.Exception());
2985 return;
2986 }
ager@chromium.org41826e72009-03-30 13:30:57 +00002987 }
2988
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002989 bool running = auto_continue;
2990
ager@chromium.org41826e72009-03-30 13:30:57 +00002991 // Process requests from the debugger.
2992 while (true) {
2993 // Wait for new command in the queue.
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002994 if (Debugger::host_dispatch_handler_) {
2995 // In case there is a host dispatch - do periodic dispatches.
2996 if (!command_received_->Wait(host_dispatch_micros_)) {
2997 // Timout expired, do the dispatch.
2998 Debugger::host_dispatch_handler_();
2999 continue;
3000 }
3001 } else {
3002 // In case there is no host dispatch - just wait.
3003 command_received_->Wait();
3004 }
ager@chromium.org41826e72009-03-30 13:30:57 +00003005
ager@chromium.org41826e72009-03-30 13:30:57 +00003006 // Get the command from the queue.
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003007 CommandMessage command = command_queue_.Get();
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00003008 isolate_->logger()->DebugTag(
3009 "Got request from command queue, in interactive loop.");
ager@chromium.org71daaf62009-04-01 07:22:49 +00003010 if (!Debugger::IsDebuggerActive()) {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003011 // Delete command text and user data.
3012 command.Dispose();
ager@chromium.org41826e72009-03-30 13:30:57 +00003013 return;
3014 }
3015
ager@chromium.org41826e72009-03-30 13:30:57 +00003016 // Invoke JavaScript to process the debug request.
3017 v8::Local<v8::String> fun_name;
3018 v8::Local<v8::Function> fun;
3019 v8::Local<v8::Value> request;
3020 v8::TryCatch try_catch;
3021 fun_name = v8::String::New("processDebugRequest");
3022 fun = v8::Function::Cast(*cmd_processor->Get(fun_name));
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003023
3024 request = v8::String::New(command.text().start(),
3025 command.text().length());
ager@chromium.org41826e72009-03-30 13:30:57 +00003026 static const int kArgc = 1;
3027 v8::Handle<Value> argv[kArgc] = { request };
3028 v8::Local<v8::Value> response_val = fun->Call(cmd_processor, kArgc, argv);
3029
3030 // Get the response.
3031 v8::Local<v8::String> response;
ager@chromium.org41826e72009-03-30 13:30:57 +00003032 if (!try_catch.HasCaught()) {
3033 // Get response string.
3034 if (!response_val->IsUndefined()) {
3035 response = v8::String::Cast(*response_val);
3036 } else {
3037 response = v8::String::New("");
3038 }
3039
3040 // Log the JSON request/response.
3041 if (FLAG_trace_debug_json) {
3042 PrintLn(request);
3043 PrintLn(response);
3044 }
3045
3046 // Get the running state.
3047 fun_name = v8::String::New("isRunning");
3048 fun = v8::Function::Cast(*cmd_processor->Get(fun_name));
3049 static const int kArgc = 1;
3050 v8::Handle<Value> argv[kArgc] = { response };
3051 v8::Local<v8::Value> running_val = fun->Call(cmd_processor, kArgc, argv);
3052 if (!try_catch.HasCaught()) {
3053 running = running_val->ToBoolean()->Value();
3054 }
3055 } else {
3056 // In case of failure the result text is the exception text.
3057 response = try_catch.Exception()->ToString();
3058 }
3059
ager@chromium.org41826e72009-03-30 13:30:57 +00003060 // Return the result.
ager@chromium.org5ec48922009-05-05 07:25:34 +00003061 MessageImpl message = MessageImpl::NewResponse(
3062 event,
3063 running,
3064 Handle<JSObject>::cast(exec_state),
3065 Handle<JSObject>::cast(event_data),
3066 Handle<String>(Utils::OpenHandle(*response)),
3067 command.client_data());
3068 InvokeMessageHandler(message);
3069 command.Dispose();
ager@chromium.org41826e72009-03-30 13:30:57 +00003070
3071 // Return from debug event processing if either the VM is put into the
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00003072 // running state (through a continue command) or auto continue is active
ager@chromium.org41826e72009-03-30 13:30:57 +00003073 // and there are no more commands queued.
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003074 if (running && !HasCommands()) {
ager@chromium.org41826e72009-03-30 13:30:57 +00003075 return;
3076 }
3077 }
3078}
3079
3080
iposva@chromium.org245aa852009-02-10 00:49:54 +00003081void Debugger::SetEventListener(Handle<Object> callback,
3082 Handle<Object> data) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00003083 HandleScope scope(isolate_);
3084 GlobalHandles* global_handles = isolate_->global_handles();
iposva@chromium.org245aa852009-02-10 00:49:54 +00003085
3086 // Clear the global handles for the event listener and the event listener data
3087 // object.
3088 if (!event_listener_.is_null()) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00003089 global_handles->Destroy(
iposva@chromium.org245aa852009-02-10 00:49:54 +00003090 reinterpret_cast<Object**>(event_listener_.location()));
3091 event_listener_ = Handle<Object>();
3092 }
3093 if (!event_listener_data_.is_null()) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00003094 global_handles->Destroy(
iposva@chromium.org245aa852009-02-10 00:49:54 +00003095 reinterpret_cast<Object**>(event_listener_data_.location()));
3096 event_listener_data_ = Handle<Object>();
3097 }
3098
3099 // If there is a new debug event listener register it together with its data
3100 // object.
3101 if (!callback->IsUndefined() && !callback->IsNull()) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003102 event_listener_ = Handle<Object>::cast(
lrn@chromium.org7516f052011-03-30 08:52:27 +00003103 global_handles->Create(*callback));
iposva@chromium.org245aa852009-02-10 00:49:54 +00003104 if (data.is_null()) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00003105 data = isolate_->factory()->undefined_value();
iposva@chromium.org245aa852009-02-10 00:49:54 +00003106 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003107 event_listener_data_ = Handle<Object>::cast(
lrn@chromium.org7516f052011-03-30 08:52:27 +00003108 global_handles->Create(*data));
iposva@chromium.org245aa852009-02-10 00:49:54 +00003109 }
3110
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003111 ListenersChanged();
iposva@chromium.org245aa852009-02-10 00:49:54 +00003112}
3113
3114
ager@chromium.org5ec48922009-05-05 07:25:34 +00003115void Debugger::SetMessageHandler(v8::Debug::MessageHandler2 handler) {
ager@chromium.org71daaf62009-04-01 07:22:49 +00003116 ScopedLock with(debugger_access_);
3117
ager@chromium.org381abbb2009-02-25 13:23:22 +00003118 message_handler_ = handler;
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003119 ListenersChanged();
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003120 if (handler == NULL) {
ager@chromium.org71daaf62009-04-01 07:22:49 +00003121 // Send an empty command to the debugger if in a break to make JavaScript
3122 // run again if the debugger is closed.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003123 if (isolate_->debug()->InDebugger()) {
ager@chromium.org71daaf62009-04-01 07:22:49 +00003124 ProcessCommand(Vector<const uint16_t>::empty());
3125 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003126 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003127}
3128
3129
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003130void Debugger::ListenersChanged() {
3131 if (IsDebuggerActive()) {
3132 // Disable the compilation cache when the debugger is active.
lrn@chromium.org7516f052011-03-30 08:52:27 +00003133 isolate_->compilation_cache()->Disable();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003134 debugger_unload_pending_ = false;
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003135 } else {
lrn@chromium.org7516f052011-03-30 08:52:27 +00003136 isolate_->compilation_cache()->Enable();
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003137 // Unload the debugger if event listener and message handler cleared.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003138 // Schedule this for later, because we may be in non-V8 thread.
3139 debugger_unload_pending_ = true;
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003140 }
3141}
3142
3143
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003144void Debugger::SetHostDispatchHandler(v8::Debug::HostDispatchHandler handler,
3145 int period) {
ager@chromium.org381abbb2009-02-25 13:23:22 +00003146 host_dispatch_handler_ = handler;
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003147 host_dispatch_micros_ = period * 1000;
ager@chromium.org381abbb2009-02-25 13:23:22 +00003148}
3149
3150
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003151void Debugger::SetDebugMessageDispatchHandler(
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003152 v8::Debug::DebugMessageDispatchHandler handler, bool provide_locker) {
3153 ScopedLock with(dispatch_handler_access_);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003154 debug_message_dispatch_handler_ = handler;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003155
3156 if (provide_locker && message_dispatch_helper_thread_ == NULL) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003157 message_dispatch_helper_thread_ = new MessageDispatchHelperThread(isolate_);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003158 message_dispatch_helper_thread_->Start();
3159 }
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003160}
3161
3162
ager@chromium.org41826e72009-03-30 13:30:57 +00003163// Calls the registered debug message handler. This callback is part of the
ager@chromium.org5ec48922009-05-05 07:25:34 +00003164// public API.
3165void Debugger::InvokeMessageHandler(MessageImpl message) {
ager@chromium.org71daaf62009-04-01 07:22:49 +00003166 ScopedLock with(debugger_access_);
3167
ager@chromium.org381abbb2009-02-25 13:23:22 +00003168 if (message_handler_ != NULL) {
ager@chromium.org5ec48922009-05-05 07:25:34 +00003169 message_handler_(message);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003170 }
ager@chromium.org41826e72009-03-30 13:30:57 +00003171}
3172
3173
3174// Puts a command coming from the public API on the queue. Creates
3175// a copy of the command string managed by the debugger. Up to this
3176// point, the command data was managed by the API client. Called
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003177// by the API client thread.
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003178void Debugger::ProcessCommand(Vector<const uint16_t> command,
3179 v8::Debug::ClientData* client_data) {
3180 // Need to cast away const.
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003181 CommandMessage message = CommandMessage::New(
ager@chromium.org41826e72009-03-30 13:30:57 +00003182 Vector<uint16_t>(const_cast<uint16_t*>(command.start()),
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003183 command.length()),
3184 client_data);
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00003185 isolate_->logger()->DebugTag("Put command on command_queue.");
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003186 command_queue_.Put(message);
ager@chromium.org41826e72009-03-30 13:30:57 +00003187 command_received_->Signal();
sgjesse@chromium.org3afc1582009-04-16 22:31:44 +00003188
3189 // Set the debug command break flag to have the command processed.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003190 if (!isolate_->debug()->InDebugger()) {
3191 isolate_->stack_guard()->DebugCommand();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003192 }
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003193
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003194 MessageDispatchHelperThread* dispatch_thread;
3195 {
3196 ScopedLock with(dispatch_handler_access_);
3197 dispatch_thread = message_dispatch_helper_thread_;
3198 }
3199
3200 if (dispatch_thread == NULL) {
3201 CallMessageDispatchHandler();
3202 } else {
3203 dispatch_thread->Schedule();
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003204 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003205}
3206
3207
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003208bool Debugger::HasCommands() {
ager@chromium.org41826e72009-03-30 13:30:57 +00003209 return !command_queue_.IsEmpty();
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003210}
3211
3212
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00003213void Debugger::EnqueueDebugCommand(v8::Debug::ClientData* client_data) {
3214 CommandMessage message = CommandMessage::New(Vector<uint16_t>(), client_data);
3215 event_command_queue_.Put(message);
3216
3217 // Set the debug command break flag to have the command processed.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003218 if (!isolate_->debug()->InDebugger()) {
3219 isolate_->stack_guard()->DebugCommand();
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00003220 }
3221}
3222
3223
ager@chromium.org71daaf62009-04-01 07:22:49 +00003224bool Debugger::IsDebuggerActive() {
3225 ScopedLock with(debugger_access_);
3226
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00003227 return message_handler_ != NULL ||
3228 !event_listener_.is_null() ||
3229 force_debugger_active_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003230}
3231
3232
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003233Handle<Object> Debugger::Call(Handle<JSFunction> fun,
3234 Handle<Object> data,
3235 bool* pending_exception) {
ager@chromium.org71daaf62009-04-01 07:22:49 +00003236 // When calling functions in the debugger prevent it from beeing unloaded.
3237 Debugger::never_unload_debugger_ = true;
3238
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003239 // Enter the debugger.
3240 EnterDebugger debugger;
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +00003241 if (debugger.FailedToEnter()) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00003242 return isolate_->factory()->undefined_value();
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003243 }
3244
3245 // Create the execution state.
3246 bool caught_exception = false;
3247 Handle<Object> exec_state = MakeExecutionState(&caught_exception);
3248 if (caught_exception) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00003249 return isolate_->factory()->undefined_value();
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003250 }
3251
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00003252 Handle<Object> argv[] = { exec_state, data };
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +00003253 Handle<Object> result = Execution::Call(
3254 fun,
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003255 Handle<Object>(isolate_->debug()->debug_context_->global_proxy()),
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00003256 ARRAY_SIZE(argv),
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +00003257 argv,
3258 pending_exception);
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003259 return result;
3260}
3261
3262
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003263static void StubMessageHandler2(const v8::Debug::Message& message) {
3264 // Simply ignore message.
3265}
3266
3267
3268bool Debugger::StartAgent(const char* name, int port,
3269 bool wait_for_connection) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003270 ASSERT(Isolate::Current() == isolate_);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003271 if (wait_for_connection) {
3272 // Suspend V8 if it is already running or set V8 to suspend whenever
3273 // it starts.
3274 // Provide stub message handler; V8 auto-continues each suspend
3275 // when there is no message handler; we doesn't need it.
3276 // Once become suspended, V8 will stay so indefinitely long, until remote
3277 // debugger connects and issues "continue" command.
3278 Debugger::message_handler_ = StubMessageHandler2;
3279 v8::Debug::DebugBreak();
3280 }
3281
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00003282 if (Socket::SetUp()) {
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00003283 if (agent_ == NULL) {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00003284 agent_ = new DebuggerAgent(name, port);
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00003285 agent_->Start();
3286 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003287 return true;
3288 }
3289
3290 return false;
3291}
3292
3293
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003294void Debugger::StopAgent() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003295 ASSERT(Isolate::Current() == isolate_);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003296 if (agent_ != NULL) {
3297 agent_->Shutdown();
3298 agent_->Join();
3299 delete agent_;
3300 agent_ = NULL;
3301 }
3302}
3303
3304
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003305void Debugger::WaitForAgent() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003306 ASSERT(Isolate::Current() == isolate_);
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003307 if (agent_ != NULL)
3308 agent_->WaitUntilListening();
3309}
3310
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003311
3312void Debugger::CallMessageDispatchHandler() {
3313 v8::Debug::DebugMessageDispatchHandler handler;
3314 {
3315 ScopedLock with(dispatch_handler_access_);
3316 handler = Debugger::debug_message_dispatch_handler_;
3317 }
3318 if (handler != NULL) {
3319 handler();
3320 }
3321}
3322
3323
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003324EnterDebugger::EnterDebugger()
3325 : isolate_(Isolate::Current()),
3326 prev_(isolate_->debug()->debugger_entry()),
3327 it_(isolate_),
3328 has_js_frames_(!it_.done()),
3329 save_(isolate_) {
3330 Debug* debug = isolate_->debug();
3331 ASSERT(prev_ != NULL || !debug->is_interrupt_pending(PREEMPT));
3332 ASSERT(prev_ != NULL || !debug->is_interrupt_pending(DEBUGBREAK));
3333
3334 // Link recursive debugger entry.
3335 debug->set_debugger_entry(this);
3336
3337 // Store the previous break id and frame id.
3338 break_id_ = debug->break_id();
3339 break_frame_id_ = debug->break_frame_id();
3340
3341 // Create the new break info. If there is no JavaScript frames there is no
3342 // break frame id.
3343 if (has_js_frames_) {
3344 debug->NewBreak(it_.frame()->id());
3345 } else {
3346 debug->NewBreak(StackFrame::NO_ID);
3347 }
3348
3349 // Make sure that debugger is loaded and enter the debugger context.
3350 load_failed_ = !debug->Load();
3351 if (!load_failed_) {
3352 // NOTE the member variable save which saves the previous context before
3353 // this change.
3354 isolate_->set_context(*debug->debug_context());
3355 }
3356}
3357
3358
3359EnterDebugger::~EnterDebugger() {
3360 ASSERT(Isolate::Current() == isolate_);
3361 Debug* debug = isolate_->debug();
3362
3363 // Restore to the previous break state.
3364 debug->SetBreak(break_frame_id_, break_id_);
3365
3366 // Check for leaving the debugger.
fschneider@chromium.org7d10be52012-04-10 12:30:14 +00003367 if (!load_failed_ && prev_ == NULL) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003368 // Clear mirror cache when leaving the debugger. Skip this if there is a
3369 // pending exception as clearing the mirror cache calls back into
3370 // JavaScript. This can happen if the v8::Debug::Call is used in which
3371 // case the exception should end up in the calling code.
3372 if (!isolate_->has_pending_exception()) {
3373 // Try to avoid any pending debug break breaking in the clear mirror
3374 // cache JavaScript code.
3375 if (isolate_->stack_guard()->IsDebugBreak()) {
3376 debug->set_interrupts_pending(DEBUGBREAK);
3377 isolate_->stack_guard()->Continue(DEBUGBREAK);
3378 }
3379 debug->ClearMirrorCache();
3380 }
3381
3382 // Request preemption and debug break when leaving the last debugger entry
3383 // if any of these where recorded while debugging.
3384 if (debug->is_interrupt_pending(PREEMPT)) {
3385 // This re-scheduling of preemption is to avoid starvation in some
3386 // debugging scenarios.
3387 debug->clear_interrupt_pending(PREEMPT);
3388 isolate_->stack_guard()->Preempt();
3389 }
3390 if (debug->is_interrupt_pending(DEBUGBREAK)) {
3391 debug->clear_interrupt_pending(DEBUGBREAK);
3392 isolate_->stack_guard()->DebugBreak();
3393 }
3394
3395 // If there are commands in the queue when leaving the debugger request
3396 // that these commands are processed.
3397 if (isolate_->debugger()->HasCommands()) {
3398 isolate_->stack_guard()->DebugCommand();
3399 }
3400
3401 // If leaving the debugger with the debugger no longer active unload it.
3402 if (!isolate_->debugger()->IsDebuggerActive()) {
3403 isolate_->debugger()->UnloadDebugger();
3404 }
3405 }
3406
3407 // Leaving this debugger entry.
3408 debug->set_debugger_entry(prev_);
3409}
3410
3411
ager@chromium.org5ec48922009-05-05 07:25:34 +00003412MessageImpl MessageImpl::NewEvent(DebugEvent event,
3413 bool running,
3414 Handle<JSObject> exec_state,
3415 Handle<JSObject> event_data) {
3416 MessageImpl message(true, event, running,
3417 exec_state, event_data, Handle<String>(), NULL);
3418 return message;
3419}
3420
3421
3422MessageImpl MessageImpl::NewResponse(DebugEvent event,
3423 bool running,
3424 Handle<JSObject> exec_state,
3425 Handle<JSObject> event_data,
3426 Handle<String> response_json,
3427 v8::Debug::ClientData* client_data) {
3428 MessageImpl message(false, event, running,
3429 exec_state, event_data, response_json, client_data);
3430 return message;
3431}
3432
3433
3434MessageImpl::MessageImpl(bool is_event,
3435 DebugEvent event,
3436 bool running,
3437 Handle<JSObject> exec_state,
3438 Handle<JSObject> event_data,
3439 Handle<String> response_json,
3440 v8::Debug::ClientData* client_data)
3441 : is_event_(is_event),
3442 event_(event),
3443 running_(running),
3444 exec_state_(exec_state),
3445 event_data_(event_data),
3446 response_json_(response_json),
3447 client_data_(client_data) {}
3448
3449
3450bool MessageImpl::IsEvent() const {
3451 return is_event_;
3452}
3453
3454
3455bool MessageImpl::IsResponse() const {
3456 return !is_event_;
3457}
3458
3459
3460DebugEvent MessageImpl::GetEvent() const {
3461 return event_;
3462}
3463
3464
3465bool MessageImpl::WillStartRunning() const {
3466 return running_;
3467}
3468
3469
3470v8::Handle<v8::Object> MessageImpl::GetExecutionState() const {
3471 return v8::Utils::ToLocal(exec_state_);
3472}
3473
3474
3475v8::Handle<v8::Object> MessageImpl::GetEventData() const {
3476 return v8::Utils::ToLocal(event_data_);
3477}
3478
3479
3480v8::Handle<v8::String> MessageImpl::GetJSON() const {
3481 v8::HandleScope scope;
3482
3483 if (IsEvent()) {
3484 // Call toJSONProtocol on the debug event object.
3485 Handle<Object> fun = GetProperty(event_data_, "toJSONProtocol");
3486 if (!fun->IsJSFunction()) {
3487 return v8::Handle<v8::String>();
3488 }
3489 bool caught_exception;
3490 Handle<Object> json = Execution::TryCall(Handle<JSFunction>::cast(fun),
3491 event_data_,
3492 0, NULL, &caught_exception);
3493 if (caught_exception || !json->IsString()) {
3494 return v8::Handle<v8::String>();
3495 }
3496 return scope.Close(v8::Utils::ToLocal(Handle<String>::cast(json)));
3497 } else {
3498 return v8::Utils::ToLocal(response_json_);
3499 }
3500}
3501
3502
3503v8::Handle<v8::Context> MessageImpl::GetEventContext() const {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003504 Isolate* isolate = Isolate::Current();
3505 v8::Handle<v8::Context> context = GetDebugEventContext(isolate);
3506 // Isolate::context() may be NULL when "script collected" event occures.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003507 ASSERT(!context.IsEmpty() || event_ == v8::ScriptCollected);
rossberg@chromium.org717967f2011-07-20 13:44:42 +00003508 return context;
ager@chromium.org5ec48922009-05-05 07:25:34 +00003509}
3510
3511
3512v8::Debug::ClientData* MessageImpl::GetClientData() const {
3513 return client_data_;
3514}
3515
3516
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003517EventDetailsImpl::EventDetailsImpl(DebugEvent event,
3518 Handle<JSObject> exec_state,
3519 Handle<JSObject> event_data,
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00003520 Handle<Object> callback_data,
3521 v8::Debug::ClientData* client_data)
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003522 : event_(event),
3523 exec_state_(exec_state),
3524 event_data_(event_data),
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00003525 callback_data_(callback_data),
3526 client_data_(client_data) {}
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003527
3528
3529DebugEvent EventDetailsImpl::GetEvent() const {
3530 return event_;
3531}
3532
3533
3534v8::Handle<v8::Object> EventDetailsImpl::GetExecutionState() const {
3535 return v8::Utils::ToLocal(exec_state_);
3536}
3537
3538
3539v8::Handle<v8::Object> EventDetailsImpl::GetEventData() const {
3540 return v8::Utils::ToLocal(event_data_);
3541}
3542
3543
3544v8::Handle<v8::Context> EventDetailsImpl::GetEventContext() const {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003545 return GetDebugEventContext(Isolate::Current());
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003546}
3547
3548
3549v8::Handle<v8::Value> EventDetailsImpl::GetCallbackData() const {
3550 return v8::Utils::ToLocal(callback_data_);
3551}
3552
3553
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00003554v8::Debug::ClientData* EventDetailsImpl::GetClientData() const {
3555 return client_data_;
3556}
3557
3558
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003559CommandMessage::CommandMessage() : text_(Vector<uint16_t>::empty()),
3560 client_data_(NULL) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003561}
3562
3563
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003564CommandMessage::CommandMessage(const Vector<uint16_t>& text,
3565 v8::Debug::ClientData* data)
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003566 : text_(text),
3567 client_data_(data) {
3568}
3569
3570
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003571CommandMessage::~CommandMessage() {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003572}
3573
3574
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003575void CommandMessage::Dispose() {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003576 text_.Dispose();
3577 delete client_data_;
3578 client_data_ = NULL;
3579}
3580
3581
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003582CommandMessage CommandMessage::New(const Vector<uint16_t>& command,
3583 v8::Debug::ClientData* data) {
3584 return CommandMessage(command.Clone(), data);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003585}
3586
3587
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003588CommandMessageQueue::CommandMessageQueue(int size) : start_(0), end_(0),
3589 size_(size) {
3590 messages_ = NewArray<CommandMessage>(size);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003591}
3592
3593
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003594CommandMessageQueue::~CommandMessageQueue() {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003595 while (!IsEmpty()) {
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003596 CommandMessage m = Get();
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003597 m.Dispose();
3598 }
kasper.lund7276f142008-07-30 08:49:36 +00003599 DeleteArray(messages_);
3600}
3601
3602
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003603CommandMessage CommandMessageQueue::Get() {
kasper.lund7276f142008-07-30 08:49:36 +00003604 ASSERT(!IsEmpty());
3605 int result = start_;
3606 start_ = (start_ + 1) % size_;
3607 return messages_[result];
3608}
3609
3610
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003611void CommandMessageQueue::Put(const CommandMessage& message) {
kasper.lund7276f142008-07-30 08:49:36 +00003612 if ((end_ + 1) % size_ == start_) {
3613 Expand();
3614 }
3615 messages_[end_] = message;
3616 end_ = (end_ + 1) % size_;
3617}
3618
3619
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003620void CommandMessageQueue::Expand() {
3621 CommandMessageQueue new_queue(size_ * 2);
kasper.lund7276f142008-07-30 08:49:36 +00003622 while (!IsEmpty()) {
3623 new_queue.Put(Get());
3624 }
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003625 CommandMessage* array_to_free = messages_;
kasper.lund7276f142008-07-30 08:49:36 +00003626 *this = new_queue;
3627 new_queue.messages_ = array_to_free;
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003628 // Make the new_queue empty so that it doesn't call Dispose on any messages.
3629 new_queue.start_ = new_queue.end_;
kasper.lund7276f142008-07-30 08:49:36 +00003630 // Automatic destructor called on new_queue, freeing array_to_free.
3631}
3632
3633
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00003634LockingCommandMessageQueue::LockingCommandMessageQueue(Logger* logger, int size)
3635 : logger_(logger), queue_(size) {
kasper.lund7276f142008-07-30 08:49:36 +00003636 lock_ = OS::CreateMutex();
3637}
3638
3639
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003640LockingCommandMessageQueue::~LockingCommandMessageQueue() {
kasper.lund7276f142008-07-30 08:49:36 +00003641 delete lock_;
3642}
3643
3644
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003645bool LockingCommandMessageQueue::IsEmpty() const {
kasper.lund7276f142008-07-30 08:49:36 +00003646 ScopedLock sl(lock_);
3647 return queue_.IsEmpty();
3648}
3649
3650
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003651CommandMessage LockingCommandMessageQueue::Get() {
kasper.lund7276f142008-07-30 08:49:36 +00003652 ScopedLock sl(lock_);
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003653 CommandMessage result = queue_.Get();
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00003654 logger_->DebugEvent("Get", result.text());
kasper.lund7276f142008-07-30 08:49:36 +00003655 return result;
3656}
3657
3658
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003659void LockingCommandMessageQueue::Put(const CommandMessage& message) {
kasper.lund7276f142008-07-30 08:49:36 +00003660 ScopedLock sl(lock_);
3661 queue_.Put(message);
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00003662 logger_->DebugEvent("Put", message.text());
kasper.lund7276f142008-07-30 08:49:36 +00003663}
3664
3665
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003666void LockingCommandMessageQueue::Clear() {
kasper.lund7276f142008-07-30 08:49:36 +00003667 ScopedLock sl(lock_);
3668 queue_.Clear();
3669}
3670
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003671
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003672MessageDispatchHelperThread::MessageDispatchHelperThread(Isolate* isolate)
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00003673 : Thread("v8:MsgDispHelpr"),
lrn@chromium.org5d00b602011-01-05 09:51:43 +00003674 sem_(OS::CreateSemaphore(0)), mutex_(OS::CreateMutex()),
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003675 already_signalled_(false) {
3676}
3677
3678
3679MessageDispatchHelperThread::~MessageDispatchHelperThread() {
3680 delete mutex_;
3681 delete sem_;
3682}
3683
3684
3685void MessageDispatchHelperThread::Schedule() {
3686 {
3687 ScopedLock lock(mutex_);
3688 if (already_signalled_) {
3689 return;
3690 }
3691 already_signalled_ = true;
3692 }
3693 sem_->Signal();
3694}
3695
3696
3697void MessageDispatchHelperThread::Run() {
3698 while (true) {
3699 sem_->Wait();
3700 {
3701 ScopedLock lock(mutex_);
3702 already_signalled_ = false;
3703 }
3704 {
3705 Locker locker;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003706 Isolate::Current()->debugger()->CallMessageDispatchHandler();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003707 }
3708 }
3709}
3710
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003711#endif // ENABLE_DEBUGGER_SUPPORT
kasper.lund7276f142008-07-30 08:49:36 +00003712
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003713} } // namespace v8::internal