blob: 8e1cf43d85a45c3e70483b0c3375fbc220c2533a [file] [log] [blame]
fschneider@chromium.org7d10be52012-04-10 12:30:14 +00001// Copyright 2012 the V8 project authors. All rights reserved.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28#include "v8.h"
29
30#include "api.h"
31#include "arguments.h"
32#include "bootstrapper.h"
33#include "code-stubs.h"
ager@chromium.org5c838252010-02-19 08:53:10 +000034#include "codegen.h"
kasperl@chromium.org71affb52009-05-26 05:44:31 +000035#include "compilation-cache.h"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000036#include "compiler.h"
37#include "debug.h"
kasperl@chromium.orga5551262010-12-07 12:49:48 +000038#include "deoptimizer.h"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000039#include "execution.h"
yangguo@chromium.orga7d3df92012-02-27 11:46:55 +000040#include "full-codegen.h"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000041#include "global-handles.h"
ager@chromium.org65dad4b2009-04-23 08:48:43 +000042#include "ic.h"
43#include "ic-inl.h"
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +000044#include "isolate-inl.h"
lrn@chromium.org34e60782011-09-15 07:25:40 +000045#include "list.h"
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000046#include "messages.h"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000047#include "natives.h"
48#include "stub-cache.h"
kasper.lund7276f142008-07-30 08:49:36 +000049#include "log.h"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000050
ager@chromium.org5ec48922009-05-05 07:25:34 +000051#include "../include/v8-debug.h"
52
kasperl@chromium.org71affb52009-05-26 05:44:31 +000053namespace v8 {
54namespace internal {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000055
ager@chromium.org65dad4b2009-04-23 08:48:43 +000056#ifdef ENABLE_DEBUGGER_SUPPORT
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000057
58
59Debug::Debug(Isolate* isolate)
60 : has_break_points_(false),
61 script_cache_(NULL),
62 debug_info_list_(NULL),
63 disable_break_(false),
64 break_on_exception_(false),
65 break_on_uncaught_exception_(false),
66 debug_break_return_(NULL),
67 debug_break_slot_(NULL),
68 isolate_(isolate) {
69 memset(registers_, 0, sizeof(JSCallerSavedBuffer));
70}
71
72
73Debug::~Debug() {
74}
75
76
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000077static void PrintLn(v8::Local<v8::Value> value) {
78 v8::Local<v8::String> s = value->ToString();
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +000079 ScopedVector<char> data(s->Length() + 1);
80 if (data.start() == NULL) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000081 V8::FatalProcessOutOfMemory("PrintLn");
82 return;
83 }
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +000084 s->WriteAscii(data.start());
85 PrintF("%s\n", data.start());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000086}
87
88
danno@chromium.org40cb8782011-05-25 07:58:50 +000089static Handle<Code> ComputeCallDebugPrepareStepIn(int argc, Code::Kind kind) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000090 Isolate* isolate = Isolate::Current();
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +000091 return isolate->stub_cache()->ComputeCallDebugPrepareStepIn(argc, kind);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000092}
93
94
95static v8::Handle<v8::Context> GetDebugEventContext(Isolate* isolate) {
96 Handle<Context> context = isolate->debug()->debugger_entry()->GetContext();
97 // Isolate::context() may have been NULL when "script collected" event
98 // occured.
99 if (context.is_null()) return v8::Local<v8::Context>();
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000100 Handle<Context> native_context(context->native_context());
101 return v8::Utils::ToLocal(native_context);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000102}
103
104
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000105BreakLocationIterator::BreakLocationIterator(Handle<DebugInfo> debug_info,
106 BreakLocatorType type) {
107 debug_info_ = debug_info;
108 type_ = type;
109 reloc_iterator_ = NULL;
110 reloc_iterator_original_ = NULL;
111 Reset(); // Initialize the rest of the member variables.
112}
113
114
115BreakLocationIterator::~BreakLocationIterator() {
116 ASSERT(reloc_iterator_ != NULL);
117 ASSERT(reloc_iterator_original_ != NULL);
118 delete reloc_iterator_;
119 delete reloc_iterator_original_;
120}
121
122
123void BreakLocationIterator::Next() {
124 AssertNoAllocation nogc;
125 ASSERT(!RinfoDone());
126
127 // Iterate through reloc info for code and original code stopping at each
128 // breakable code target.
129 bool first = break_point_ == -1;
130 while (!RinfoDone()) {
131 if (!first) RinfoNext();
132 first = false;
133 if (RinfoDone()) return;
134
ager@chromium.org236ad962008-09-25 09:45:57 +0000135 // Whenever a statement position or (plain) position is passed update the
136 // current value of these.
137 if (RelocInfo::IsPosition(rmode())) {
138 if (RelocInfo::IsStatementPosition(rmode())) {
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000139 statement_position_ = static_cast<int>(
140 rinfo()->data() - debug_info_->shared()->start_position());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000141 }
ager@chromium.org236ad962008-09-25 09:45:57 +0000142 // Always update the position as we don't want that to be before the
143 // statement position.
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000144 position_ = static_cast<int>(
145 rinfo()->data() - debug_info_->shared()->start_position());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000146 ASSERT(position_ >= 0);
147 ASSERT(statement_position_ >= 0);
148 }
149
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000150 if (IsDebugBreakSlot()) {
151 // There is always a possible break point at a debug break slot.
152 break_point_++;
153 return;
154 } else if (RelocInfo::IsCodeTarget(rmode())) {
155 // Check for breakable code target. Look in the original code as setting
156 // break points can cause the code targets in the running (debugged) code
157 // to be of a different kind than in the original code.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000158 Address target = original_rinfo()->target_address();
ager@chromium.org8bb60582008-12-11 12:02:20 +0000159 Code* code = Code::GetCodeFromTargetAddress(target);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000160 if ((code->is_inline_cache_stub() &&
danno@chromium.org40cb8782011-05-25 07:58:50 +0000161 !code->is_binary_op_stub() &&
162 !code->is_unary_op_stub() &&
ricow@chromium.org9fa09672011-07-25 11:05:35 +0000163 !code->is_compare_ic_stub() &&
164 !code->is_to_boolean_ic_stub()) ||
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000165 RelocInfo::IsConstructCall(rmode())) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000166 break_point_++;
167 return;
168 }
169 if (code->kind() == Code::STUB) {
ager@chromium.orga1645e22009-09-09 19:27:10 +0000170 if (IsDebuggerStatement()) {
171 break_point_++;
172 return;
173 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000174 if (type_ == ALL_BREAK_LOCATIONS) {
175 if (Debug::IsBreakStub(code)) {
176 break_point_++;
177 return;
178 }
179 } else {
180 ASSERT(type_ == SOURCE_BREAK_LOCATIONS);
181 if (Debug::IsSourceBreakStub(code)) {
182 break_point_++;
183 return;
184 }
185 }
186 }
187 }
188
189 // Check for break at return.
ager@chromium.org236ad962008-09-25 09:45:57 +0000190 if (RelocInfo::IsJSReturn(rmode())) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000191 // Set the positions to the end of the function.
192 if (debug_info_->shared()->HasSourceCode()) {
193 position_ = debug_info_->shared()->end_position() -
whesse@chromium.orge90029b2010-08-02 11:52:17 +0000194 debug_info_->shared()->start_position() - 1;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000195 } else {
196 position_ = 0;
197 }
198 statement_position_ = position_;
199 break_point_++;
200 return;
201 }
202 }
203}
204
205
206void BreakLocationIterator::Next(int count) {
207 while (count > 0) {
208 Next();
209 count--;
210 }
211}
212
213
214// Find the break point closest to the supplied address.
215void BreakLocationIterator::FindBreakLocationFromAddress(Address pc) {
216 // Run through all break points to locate the one closest to the address.
217 int closest_break_point = 0;
218 int distance = kMaxInt;
219 while (!Done()) {
220 // Check if this break point is closer that what was previously found.
221 if (this->pc() < pc && pc - this->pc() < distance) {
222 closest_break_point = break_point();
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000223 distance = static_cast<int>(pc - this->pc());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000224 // Check whether we can't get any closer.
225 if (distance == 0) break;
226 }
227 Next();
228 }
229
230 // Move to the break point found.
231 Reset();
232 Next(closest_break_point);
233}
234
235
236// Find the break point closest to the supplied source position.
237void BreakLocationIterator::FindBreakLocationFromPosition(int position) {
238 // Run through all break points to locate the one closest to the source
239 // position.
240 int closest_break_point = 0;
241 int distance = kMaxInt;
242 while (!Done()) {
243 // Check if this break point is closer that what was previously found.
244 if (position <= statement_position() &&
245 statement_position() - position < distance) {
246 closest_break_point = break_point();
247 distance = statement_position() - position;
248 // Check whether we can't get any closer.
249 if (distance == 0) break;
250 }
251 Next();
252 }
253
254 // Move to the break point found.
255 Reset();
256 Next(closest_break_point);
257}
258
259
260void BreakLocationIterator::Reset() {
261 // Create relocation iterators for the two code objects.
262 if (reloc_iterator_ != NULL) delete reloc_iterator_;
263 if (reloc_iterator_original_ != NULL) delete reloc_iterator_original_;
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +0000264 reloc_iterator_ = new RelocIterator(
265 debug_info_->code(),
266 ~RelocInfo::ModeMask(RelocInfo::CODE_AGE_SEQUENCE));
267 reloc_iterator_original_ = new RelocIterator(
268 debug_info_->original_code(),
269 ~RelocInfo::ModeMask(RelocInfo::CODE_AGE_SEQUENCE));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000270
271 // Position at the first break point.
272 break_point_ = -1;
273 position_ = 1;
274 statement_position_ = 1;
275 Next();
276}
277
278
279bool BreakLocationIterator::Done() const {
280 return RinfoDone();
281}
282
283
284void BreakLocationIterator::SetBreakPoint(Handle<Object> break_point_object) {
285 // If there is not already a real break point here patch code with debug
286 // break.
287 if (!HasBreakPoint()) {
288 SetDebugBreak();
289 }
ager@chromium.orga1645e22009-09-09 19:27:10 +0000290 ASSERT(IsDebugBreak() || IsDebuggerStatement());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000291 // Set the break point information.
292 DebugInfo::SetBreakPoint(debug_info_, code_position(),
293 position(), statement_position(),
294 break_point_object);
295}
296
297
298void BreakLocationIterator::ClearBreakPoint(Handle<Object> break_point_object) {
299 // Clear the break point information.
300 DebugInfo::ClearBreakPoint(debug_info_, code_position(), break_point_object);
301 // If there are no more break points here remove the debug break.
302 if (!HasBreakPoint()) {
303 ClearDebugBreak();
304 ASSERT(!IsDebugBreak());
305 }
306}
307
308
309void BreakLocationIterator::SetOneShot() {
ager@chromium.orga1645e22009-09-09 19:27:10 +0000310 // Debugger statement always calls debugger. No need to modify it.
311 if (IsDebuggerStatement()) {
312 return;
313 }
314
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000315 // If there is a real break point here no more to do.
316 if (HasBreakPoint()) {
317 ASSERT(IsDebugBreak());
318 return;
319 }
320
321 // Patch code with debug break.
322 SetDebugBreak();
323}
324
325
326void BreakLocationIterator::ClearOneShot() {
ager@chromium.orga1645e22009-09-09 19:27:10 +0000327 // Debugger statement always calls debugger. No need to modify it.
328 if (IsDebuggerStatement()) {
329 return;
330 }
331
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000332 // If there is a real break point here no more to do.
333 if (HasBreakPoint()) {
334 ASSERT(IsDebugBreak());
335 return;
336 }
337
338 // Patch code removing debug break.
339 ClearDebugBreak();
340 ASSERT(!IsDebugBreak());
341}
342
343
344void BreakLocationIterator::SetDebugBreak() {
ager@chromium.orga1645e22009-09-09 19:27:10 +0000345 // Debugger statement always calls debugger. No need to modify it.
346 if (IsDebuggerStatement()) {
347 return;
348 }
349
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000350 // If there is already a break point here just return. This might happen if
v8.team.kasperl727e9952008-09-02 14:56:44 +0000351 // the same code is flooded with break points twice. Flooding the same
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000352 // function twice might happen when stepping in a function with an exception
353 // handler as the handler and the function is the same.
354 if (IsDebugBreak()) {
355 return;
356 }
357
ager@chromium.org236ad962008-09-25 09:45:57 +0000358 if (RelocInfo::IsJSReturn(rmode())) {
iposva@chromium.org245aa852009-02-10 00:49:54 +0000359 // Patch the frame exit code with a break point.
360 SetDebugBreakAtReturn();
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000361 } else if (IsDebugBreakSlot()) {
362 // Patch the code in the break slot.
363 SetDebugBreakAtSlot();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000364 } else {
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000365 // Patch the IC call.
366 SetDebugBreakAtIC();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000367 }
368 ASSERT(IsDebugBreak());
369}
370
371
372void BreakLocationIterator::ClearDebugBreak() {
ager@chromium.orga1645e22009-09-09 19:27:10 +0000373 // Debugger statement always calls debugger. No need to modify it.
374 if (IsDebuggerStatement()) {
375 return;
376 }
377
ager@chromium.org236ad962008-09-25 09:45:57 +0000378 if (RelocInfo::IsJSReturn(rmode())) {
iposva@chromium.org245aa852009-02-10 00:49:54 +0000379 // Restore the frame exit code.
380 ClearDebugBreakAtReturn();
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000381 } else if (IsDebugBreakSlot()) {
382 // Restore the code in the break slot.
383 ClearDebugBreakAtSlot();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000384 } else {
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000385 // Patch the IC call.
386 ClearDebugBreakAtIC();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000387 }
388 ASSERT(!IsDebugBreak());
389}
390
391
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +0000392void BreakLocationIterator::PrepareStepIn(Isolate* isolate) {
393 HandleScope scope(isolate);
ager@chromium.org381abbb2009-02-25 13:23:22 +0000394
ager@chromium.orga1645e22009-09-09 19:27:10 +0000395 // Step in can only be prepared if currently positioned on an IC call,
396 // construct call or CallFunction stub call.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000397 Address target = rinfo()->target_address();
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000398 Handle<Code> target_code(Code::GetCodeFromTargetAddress(target));
399 if (target_code->is_call_stub() || target_code->is_keyed_call_stub()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000400 // Step in through IC call is handled by the runtime system. Therefore make
401 // sure that the any current IC is cleared and the runtime system is
402 // called. If the executing code has a debug break at the location change
403 // the call in the original code as it is the code there that will be
404 // executed in place of the debug break call.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000405 Handle<Code> stub = ComputeCallDebugPrepareStepIn(
406 target_code->arguments_count(), target_code->kind());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000407 if (IsDebugBreak()) {
408 original_rinfo()->set_target_address(stub->entry());
409 } else {
410 rinfo()->set_target_address(stub->entry());
411 }
412 } else {
ager@chromium.orga1645e22009-09-09 19:27:10 +0000413#ifdef DEBUG
414 // All the following stuff is needed only for assertion checks so the code
415 // is wrapped in ifdef.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000416 Handle<Code> maybe_call_function_stub = target_code;
ager@chromium.orga1645e22009-09-09 19:27:10 +0000417 if (IsDebugBreak()) {
418 Address original_target = original_rinfo()->target_address();
419 maybe_call_function_stub =
420 Handle<Code>(Code::GetCodeFromTargetAddress(original_target));
421 }
422 bool is_call_function_stub =
423 (maybe_call_function_stub->kind() == Code::STUB &&
424 maybe_call_function_stub->major_key() == CodeStub::CallFunction);
425
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000426 // Step in through construct call requires no changes to the running code.
427 // Step in through getters/setters should already be prepared as well
428 // because caller of this function (Debug::PrepareStep) is expected to
429 // flood the top frame's function with one shot breakpoints.
ager@chromium.orga1645e22009-09-09 19:27:10 +0000430 // Step in through CallFunction stub should also be prepared by caller of
431 // this function (Debug::PrepareStep) which should flood target function
432 // with breakpoints.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000433 ASSERT(RelocInfo::IsConstructCall(rmode()) ||
434 target_code->is_inline_cache_stub() ||
435 is_call_function_stub);
ager@chromium.orga1645e22009-09-09 19:27:10 +0000436#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000437 }
438}
439
440
441// Check whether the break point is at a position which will exit the function.
442bool BreakLocationIterator::IsExit() const {
ager@chromium.org236ad962008-09-25 09:45:57 +0000443 return (RelocInfo::IsJSReturn(rmode()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000444}
445
446
447bool BreakLocationIterator::HasBreakPoint() {
448 return debug_info_->HasBreakPoint(code_position());
449}
450
451
452// Check whether there is a debug break at the current position.
453bool BreakLocationIterator::IsDebugBreak() {
ager@chromium.org236ad962008-09-25 09:45:57 +0000454 if (RelocInfo::IsJSReturn(rmode())) {
iposva@chromium.org245aa852009-02-10 00:49:54 +0000455 return IsDebugBreakAtReturn();
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000456 } else if (IsDebugBreakSlot()) {
457 return IsDebugBreakAtSlot();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000458 } else {
459 return Debug::IsDebugBreak(rinfo()->target_address());
460 }
461}
462
463
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000464void BreakLocationIterator::SetDebugBreakAtIC() {
465 // Patch the original code with the current address as the current address
466 // might have changed by the inline caching since the code was copied.
467 original_rinfo()->set_target_address(rinfo()->target_address());
468
469 RelocInfo::Mode mode = rmode();
470 if (RelocInfo::IsCodeTarget(mode)) {
471 Address target = rinfo()->target_address();
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000472 Handle<Code> target_code(Code::GetCodeFromTargetAddress(target));
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000473
474 // Patch the code to invoke the builtin debug break function matching the
475 // calling convention used by the call site.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000476 Handle<Code> dbgbrk_code(Debug::FindDebugBreak(target_code, mode));
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000477 rinfo()->set_target_address(dbgbrk_code->entry());
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000478 }
479}
480
481
482void BreakLocationIterator::ClearDebugBreakAtIC() {
483 // Patch the code to the original invoke.
484 rinfo()->set_target_address(original_rinfo()->target_address());
485}
486
487
ager@chromium.orga1645e22009-09-09 19:27:10 +0000488bool BreakLocationIterator::IsDebuggerStatement() {
ager@chromium.org5c838252010-02-19 08:53:10 +0000489 return RelocInfo::DEBUG_BREAK == rmode();
ager@chromium.orga1645e22009-09-09 19:27:10 +0000490}
491
492
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000493bool BreakLocationIterator::IsDebugBreakSlot() {
494 return RelocInfo::DEBUG_BREAK_SLOT == rmode();
495}
496
497
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000498Object* BreakLocationIterator::BreakPointObjects() {
499 return debug_info_->GetBreakPointObjects(code_position());
500}
501
502
ager@chromium.org381abbb2009-02-25 13:23:22 +0000503// Clear out all the debug break code. This is ONLY supposed to be used when
504// shutting down the debugger as it will leave the break point information in
505// DebugInfo even though the code is patched back to the non break point state.
506void BreakLocationIterator::ClearAllDebugBreak() {
507 while (!Done()) {
508 ClearDebugBreak();
509 Next();
510 }
511}
512
513
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000514bool BreakLocationIterator::RinfoDone() const {
515 ASSERT(reloc_iterator_->done() == reloc_iterator_original_->done());
516 return reloc_iterator_->done();
517}
518
519
520void BreakLocationIterator::RinfoNext() {
521 reloc_iterator_->next();
522 reloc_iterator_original_->next();
523#ifdef DEBUG
524 ASSERT(reloc_iterator_->done() == reloc_iterator_original_->done());
525 if (!reloc_iterator_->done()) {
526 ASSERT(rmode() == original_rmode());
527 }
528#endif
529}
530
531
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000532// Threading support.
533void Debug::ThreadInit() {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000534 thread_local_.break_count_ = 0;
535 thread_local_.break_id_ = 0;
536 thread_local_.break_frame_id_ = StackFrame::NO_ID;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000537 thread_local_.last_step_action_ = StepNone;
ager@chromium.org236ad962008-09-25 09:45:57 +0000538 thread_local_.last_statement_position_ = RelocInfo::kNoPosition;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000539 thread_local_.step_count_ = 0;
540 thread_local_.last_fp_ = 0;
lrn@chromium.org34e60782011-09-15 07:25:40 +0000541 thread_local_.queued_step_count_ = 0;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000542 thread_local_.step_into_fp_ = 0;
ager@chromium.orga1645e22009-09-09 19:27:10 +0000543 thread_local_.step_out_fp_ = 0;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000544 thread_local_.after_break_target_ = 0;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000545 // TODO(isolates): frames_are_dropped_?
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000546 thread_local_.debugger_entry_ = NULL;
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000547 thread_local_.pending_interrupts_ = 0;
ricow@chromium.org0b9f8502010-08-18 07:45:01 +0000548 thread_local_.restarter_frame_function_pointer_ = NULL;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000549}
550
551
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000552char* Debug::ArchiveDebug(char* storage) {
553 char* to = storage;
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000554 OS::MemCopy(to, reinterpret_cast<char*>(&thread_local_), sizeof(ThreadLocal));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000555 to += sizeof(ThreadLocal);
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000556 OS::MemCopy(to, reinterpret_cast<char*>(&registers_), sizeof(registers_));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000557 ThreadInit();
558 ASSERT(to <= storage + ArchiveSpacePerThread());
559 return storage + ArchiveSpacePerThread();
560}
561
562
563char* Debug::RestoreDebug(char* storage) {
564 char* from = storage;
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000565 OS::MemCopy(
566 reinterpret_cast<char*>(&thread_local_), from, sizeof(ThreadLocal));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000567 from += sizeof(ThreadLocal);
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000568 OS::MemCopy(reinterpret_cast<char*>(&registers_), from, sizeof(registers_));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000569 ASSERT(from <= storage + ArchiveSpacePerThread());
570 return storage + ArchiveSpacePerThread();
571}
572
573
574int Debug::ArchiveSpacePerThread() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000575 return sizeof(ThreadLocal) + sizeof(JSCallerSavedBuffer);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000576}
577
578
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000579// Frame structure (conforms InternalFrame structure):
580// -- code
581// -- SMI maker
582// -- function (slot is called "context")
583// -- frame base
584Object** Debug::SetUpFrameDropperFrame(StackFrame* bottom_js_frame,
585 Handle<Code> code) {
586 ASSERT(bottom_js_frame->is_java_script());
587
588 Address fp = bottom_js_frame->fp();
589
590 // Move function pointer into "context" slot.
591 Memory::Object_at(fp + StandardFrameConstants::kContextOffset) =
592 Memory::Object_at(fp + JavaScriptFrameConstants::kFunctionOffset);
593
594 Memory::Object_at(fp + InternalFrameConstants::kCodeOffset) = *code;
595 Memory::Object_at(fp + StandardFrameConstants::kMarkerOffset) =
596 Smi::FromInt(StackFrame::INTERNAL);
597
598 return reinterpret_cast<Object**>(&Memory::Object_at(
599 fp + StandardFrameConstants::kContextOffset));
600}
601
602const int Debug::kFrameDropperFrameSize = 4;
603
604
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000605void ScriptCache::Add(Handle<Script> script) {
lrn@chromium.org7516f052011-03-30 08:52:27 +0000606 GlobalHandles* global_handles = Isolate::Current()->global_handles();
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000607 // Create an entry in the hash map for the script.
608 int id = Smi::cast(script->id())->value();
609 HashMap::Entry* entry =
610 HashMap::Lookup(reinterpret_cast<void*>(id), Hash(id), true);
611 if (entry->value != NULL) {
612 ASSERT(*script == *reinterpret_cast<Script**>(entry->value));
613 return;
614 }
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000615 // Globalize the script object, make it weak and use the location of the
616 // global handle as the value in the hash map.
617 Handle<Script> script_ =
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000618 Handle<Script>::cast(
lrn@chromium.org7516f052011-03-30 08:52:27 +0000619 (global_handles->Create(*script)));
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000620 global_handles->MakeWeak(reinterpret_cast<Object**>(script_.location()),
621 this,
622 NULL,
623 ScriptCache::HandleWeakScript);
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000624 entry->value = script_.location();
625}
626
627
628Handle<FixedArray> ScriptCache::GetScripts() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000629 Handle<FixedArray> instances = FACTORY->NewFixedArray(occupancy());
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000630 int count = 0;
631 for (HashMap::Entry* entry = Start(); entry != NULL; entry = Next(entry)) {
632 ASSERT(entry->value != NULL);
633 if (entry->value != NULL) {
634 instances->set(count, *reinterpret_cast<Script**>(entry->value));
635 count++;
636 }
637 }
638 return instances;
639}
640
641
642void ScriptCache::ProcessCollectedScripts() {
lrn@chromium.org7516f052011-03-30 08:52:27 +0000643 Debugger* debugger = Isolate::Current()->debugger();
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000644 for (int i = 0; i < collected_scripts_.length(); i++) {
lrn@chromium.org7516f052011-03-30 08:52:27 +0000645 debugger->OnScriptCollected(collected_scripts_[i]);
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000646 }
647 collected_scripts_.Clear();
648}
649
650
651void ScriptCache::Clear() {
lrn@chromium.org7516f052011-03-30 08:52:27 +0000652 GlobalHandles* global_handles = Isolate::Current()->global_handles();
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000653 // Iterate the script cache to get rid of all the weak handles.
654 for (HashMap::Entry* entry = Start(); entry != NULL; entry = Next(entry)) {
655 ASSERT(entry != NULL);
656 Object** location = reinterpret_cast<Object**>(entry->value);
657 ASSERT((*location)->IsScript());
lrn@chromium.org7516f052011-03-30 08:52:27 +0000658 global_handles->ClearWeakness(location);
659 global_handles->Destroy(location);
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000660 }
661 // Clear the content of the hash map.
662 HashMap::Clear();
663}
664
665
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000666void ScriptCache::HandleWeakScript(v8::Isolate* isolate,
667 v8::Persistent<v8::Value> obj,
668 void* data) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000669 ScriptCache* script_cache = reinterpret_cast<ScriptCache*>(data);
670 // Find the location of the global handle.
671 Script** location =
672 reinterpret_cast<Script**>(Utils::OpenHandle(*obj).location());
673 ASSERT((*location)->IsScript());
674
675 // Remove the entry from the cache.
676 int id = Smi::cast((*location)->id())->value();
677 script_cache->Remove(reinterpret_cast<void*>(id), Hash(id));
678 script_cache->collected_scripts_.Add(id);
679
680 // Clear the weak handle.
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000681 obj.Dispose(isolate);
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000682 obj.Clear();
683}
684
685
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000686void Debug::SetUp(bool create_heap_objects) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000687 ThreadInit();
688 if (create_heap_objects) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000689 // Get code to handle debug break on return.
690 debug_break_return_ =
lrn@chromium.org7516f052011-03-30 08:52:27 +0000691 isolate_->builtins()->builtin(Builtins::kReturn_DebugBreak);
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000692 ASSERT(debug_break_return_->IsCode());
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000693 // Get code to handle debug break in debug break slots.
694 debug_break_slot_ =
lrn@chromium.org7516f052011-03-30 08:52:27 +0000695 isolate_->builtins()->builtin(Builtins::kSlot_DebugBreak);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000696 ASSERT(debug_break_slot_->IsCode());
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000697 }
698}
699
700
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000701void Debug::HandleWeakDebugInfo(v8::Isolate* isolate,
702 v8::Persistent<v8::Value> obj,
703 void* data) {
704 Debug* debug = reinterpret_cast<Isolate*>(isolate)->debug();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000705 DebugInfoListNode* node = reinterpret_cast<DebugInfoListNode*>(data);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000706 // We need to clear all breakpoints associated with the function to restore
707 // original code and avoid patching the code twice later because
708 // the function will live in the heap until next gc, and can be found by
jkummerow@chromium.org78502a92012-09-06 13:50:42 +0000709 // Debug::FindSharedFunctionInfoInScript.
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000710 BreakLocationIterator it(node->debug_info(), ALL_BREAK_LOCATIONS);
711 it.ClearAllDebugBreak();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000712 debug->RemoveDebugInfo(node->debug_info());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000713#ifdef DEBUG
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000714 node = debug->debug_info_list_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000715 while (node != NULL) {
716 ASSERT(node != reinterpret_cast<DebugInfoListNode*>(data));
717 node = node->next();
718 }
719#endif
720}
721
722
723DebugInfoListNode::DebugInfoListNode(DebugInfo* debug_info): next_(NULL) {
lrn@chromium.org7516f052011-03-30 08:52:27 +0000724 GlobalHandles* global_handles = Isolate::Current()->global_handles();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000725 // Globalize the request debug info object and make it weak.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000726 debug_info_ = Handle<DebugInfo>::cast(
lrn@chromium.org7516f052011-03-30 08:52:27 +0000727 (global_handles->Create(debug_info)));
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000728 global_handles->MakeWeak(reinterpret_cast<Object**>(debug_info_.location()),
729 this,
730 NULL,
731 Debug::HandleWeakDebugInfo);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000732}
733
734
735DebugInfoListNode::~DebugInfoListNode() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000736 Isolate::Current()->global_handles()->Destroy(
737 reinterpret_cast<Object**>(debug_info_.location()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000738}
739
740
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000741bool Debug::CompileDebuggerScript(int index) {
lrn@chromium.org7516f052011-03-30 08:52:27 +0000742 Isolate* isolate = Isolate::Current();
743 Factory* factory = isolate->factory();
744 HandleScope scope(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000745
kasper.lund44510672008-07-25 07:37:58 +0000746 // Bail out if the index is invalid.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000747 if (index == -1) {
748 return false;
749 }
kasper.lund44510672008-07-25 07:37:58 +0000750
751 // Find source and name for the requested script.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000752 Handle<String> source_code =
lrn@chromium.org7516f052011-03-30 08:52:27 +0000753 isolate->bootstrapper()->NativesSourceLookup(index);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000754 Vector<const char> name = Natives::GetScriptName(index);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000755 Handle<String> script_name = factory->NewStringFromAscii(name);
yangguo@chromium.org355cfd12012-08-29 15:32:24 +0000756 Handle<Context> context = isolate->native_context();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000757
758 // Compile the script.
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000759 Handle<SharedFunctionInfo> function_info;
760 function_info = Compiler::Compile(source_code,
761 script_name,
yangguo@chromium.org355cfd12012-08-29 15:32:24 +0000762 0, 0,
763 context,
764 NULL, NULL,
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000765 Handle<String>::null(),
766 NATIVES_CODE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000767
768 // Silently ignore stack overflows during compilation.
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000769 if (function_info.is_null()) {
lrn@chromium.org7516f052011-03-30 08:52:27 +0000770 ASSERT(isolate->has_pending_exception());
771 isolate->clear_pending_exception();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000772 return false;
773 }
774
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000775 // Execute the shared function in the debugger context.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000776 bool caught_exception;
kasper.lund44510672008-07-25 07:37:58 +0000777 Handle<JSFunction> function =
lrn@chromium.org7516f052011-03-30 08:52:27 +0000778 factory->NewFunctionFromSharedFunctionInfo(function_info, context);
rossberg@chromium.org717967f2011-07-20 13:44:42 +0000779
fschneider@chromium.org7d10be52012-04-10 12:30:14 +0000780 Handle<Object> exception =
ulan@chromium.org09d7ab52013-02-25 15:50:35 +0000781 Execution::TryCall(function,
782 Handle<Object>(context->global_object(), isolate),
783 0,
784 NULL,
785 &caught_exception);
kasper.lund44510672008-07-25 07:37:58 +0000786
787 // Check for caught exceptions.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000788 if (caught_exception) {
fschneider@chromium.org7d10be52012-04-10 12:30:14 +0000789 ASSERT(!isolate->has_pending_exception());
790 MessageLocation computed_location;
791 isolate->ComputeLocation(&computed_location);
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000792 Handle<Object> message = MessageHandler::MakeMessageObject(
fschneider@chromium.org7d10be52012-04-10 12:30:14 +0000793 "error_loading_debugger", &computed_location,
794 Vector<Handle<Object> >::empty(), Handle<String>(), Handle<JSArray>());
795 ASSERT(!isolate->has_pending_exception());
mmassi@chromium.org49a44672012-12-04 13:52:03 +0000796 if (!exception.is_null()) {
797 isolate->set_pending_exception(*exception);
798 MessageHandler::ReportMessage(Isolate::Current(), NULL, message);
799 isolate->clear_pending_exception();
800 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000801 return false;
802 }
803
kasper.lund44510672008-07-25 07:37:58 +0000804 // Mark this script as native and return successfully.
805 Handle<Script> script(Script::cast(function->shared()->script()));
ager@chromium.orge2902be2009-06-08 12:21:35 +0000806 script->set_type(Smi::FromInt(Script::TYPE_NATIVE));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000807 return true;
808}
809
810
811bool Debug::Load() {
812 // Return if debugger is already loaded.
kasper.lund212ac232008-07-16 07:07:30 +0000813 if (IsLoaded()) return true;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000814
lrn@chromium.org7516f052011-03-30 08:52:27 +0000815 Debugger* debugger = isolate_->debugger();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000816
kasper.lund44510672008-07-25 07:37:58 +0000817 // Bail out if we're already in the process of compiling the native
818 // JavaScript source code for the debugger.
lrn@chromium.org7516f052011-03-30 08:52:27 +0000819 if (debugger->compiling_natives() ||
820 debugger->is_loading_debugger())
mads.s.agercbaa0602008-08-14 13:41:48 +0000821 return false;
lrn@chromium.org7516f052011-03-30 08:52:27 +0000822 debugger->set_loading_debugger(true);
kasper.lund44510672008-07-25 07:37:58 +0000823
824 // Disable breakpoints and interrupts while compiling and running the
825 // debugger scripts including the context creation code.
826 DisableBreak disable(true);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000827 PostponeInterruptsScope postpone(isolate_);
kasper.lund44510672008-07-25 07:37:58 +0000828
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000829 // Create the debugger context.
lrn@chromium.org7516f052011-03-30 08:52:27 +0000830 HandleScope scope(isolate_);
kasper.lund44510672008-07-25 07:37:58 +0000831 Handle<Context> context =
lrn@chromium.org7516f052011-03-30 08:52:27 +0000832 isolate_->bootstrapper()->CreateEnvironment(
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000833 Handle<Object>::null(),
834 v8::Handle<ObjectTemplate>(),
835 NULL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000836
fschneider@chromium.org7d10be52012-04-10 12:30:14 +0000837 // Fail if no context could be created.
838 if (context.is_null()) return false;
839
kasper.lund44510672008-07-25 07:37:58 +0000840 // Use the debugger context.
lrn@chromium.org7516f052011-03-30 08:52:27 +0000841 SaveContext save(isolate_);
842 isolate_->set_context(*context);
kasper.lund44510672008-07-25 07:37:58 +0000843
844 // Expose the builtins object in the debugger context.
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000845 Handle<String> key = isolate_->factory()->InternalizeOneByteString(
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +0000846 STATIC_ASCII_VECTOR("builtins"));
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000847 Handle<GlobalObject> global = Handle<GlobalObject>(context->global_object());
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +0000848 RETURN_IF_EMPTY_HANDLE_VALUE(
lrn@chromium.org7516f052011-03-30 08:52:27 +0000849 isolate_,
ulan@chromium.org09d7ab52013-02-25 15:50:35 +0000850 JSReceiver::SetProperty(global,
851 key,
852 Handle<Object>(global->builtins(), isolate_),
853 NONE,
854 kNonStrictMode),
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +0000855 false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000856
857 // Compile the JavaScript for the debugger in the debugger context.
lrn@chromium.org7516f052011-03-30 08:52:27 +0000858 debugger->set_compiling_natives(true);
kasper.lund44510672008-07-25 07:37:58 +0000859 bool caught_exception =
860 !CompileDebuggerScript(Natives::GetIndex("mirror")) ||
861 !CompileDebuggerScript(Natives::GetIndex("debug"));
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000862
863 if (FLAG_enable_liveedit) {
864 caught_exception = caught_exception ||
865 !CompileDebuggerScript(Natives::GetIndex("liveedit"));
866 }
867
lrn@chromium.org7516f052011-03-30 08:52:27 +0000868 debugger->set_compiling_natives(false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000869
mads.s.agercbaa0602008-08-14 13:41:48 +0000870 // Make sure we mark the debugger as not loading before we might
871 // return.
lrn@chromium.org7516f052011-03-30 08:52:27 +0000872 debugger->set_loading_debugger(false);
mads.s.agercbaa0602008-08-14 13:41:48 +0000873
kasper.lund44510672008-07-25 07:37:58 +0000874 // Check for caught exceptions.
875 if (caught_exception) return false;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000876
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000877 // Debugger loaded, create debugger context global handle.
878 debug_context_ = Handle<Context>::cast(
879 isolate_->global_handles()->Create(*context));
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000880
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000881 return true;
882}
883
884
885void Debug::Unload() {
886 // Return debugger is not loaded.
887 if (!IsLoaded()) {
888 return;
889 }
890
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000891 // Clear the script cache.
892 DestroyScriptCache();
893
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000894 // Clear debugger context global handle.
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000895 isolate_->global_handles()->Destroy(
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000896 reinterpret_cast<Object**>(debug_context_.location()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000897 debug_context_ = Handle<Context>();
898}
899
900
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000901// Set the flag indicating that preemption happened during debugging.
902void Debug::PreemptionWhileInDebugger() {
903 ASSERT(InDebugger());
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000904 Debug::set_interrupts_pending(PREEMPT);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000905}
906
907
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000908void Debug::Iterate(ObjectVisitor* v) {
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000909 v->VisitPointer(BitCast<Object**>(&(debug_break_return_)));
910 v->VisitPointer(BitCast<Object**>(&(debug_break_slot_)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000911}
912
913
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000914Object* Debug::Break(Arguments args) {
915 Heap* heap = isolate_->heap();
916 HandleScope scope(isolate_);
mads.s.ager31e71382008-08-13 09:32:07 +0000917 ASSERT(args.length() == 0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000918
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000919 thread_local_.frame_drop_mode_ = FRAMES_UNTOUCHED;
ager@chromium.org357bf652010-04-12 11:30:10 +0000920
kasper.lundbd3ec4e2008-07-09 11:06:54 +0000921 // Get the top-most JavaScript frame.
vegorov@chromium.org74f333b2011-04-06 11:17:46 +0000922 JavaScriptFrameIterator it(isolate_);
kasper.lundbd3ec4e2008-07-09 11:06:54 +0000923 JavaScriptFrame* frame = it.frame();
924
925 // Just continue if breaks are disabled or debugger cannot be loaded.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000926 if (disable_break() || !Load()) {
927 SetAfterBreakTarget(frame);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000928 return heap->undefined_value();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000929 }
930
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000931 // Enter the debugger.
932 EnterDebugger debugger;
933 if (debugger.FailedToEnter()) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000934 return heap->undefined_value();
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000935 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000936
kasper.lund44510672008-07-25 07:37:58 +0000937 // Postpone interrupt during breakpoint processing.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000938 PostponeInterruptsScope postpone(isolate_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000939
940 // Get the debug info (create it if it does not exist).
941 Handle<SharedFunctionInfo> shared =
942 Handle<SharedFunctionInfo>(JSFunction::cast(frame->function())->shared());
943 Handle<DebugInfo> debug_info = GetDebugInfo(shared);
944
945 // Find the break point where execution has stopped.
946 BreakLocationIterator break_location_iterator(debug_info,
947 ALL_BREAK_LOCATIONS);
948 break_location_iterator.FindBreakLocationFromAddress(frame->pc());
949
950 // Check whether step next reached a new statement.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000951 if (!StepNextContinue(&break_location_iterator, frame)) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000952 // Decrease steps left if performing multiple steps.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000953 if (thread_local_.step_count_ > 0) {
954 thread_local_.step_count_--;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000955 }
956 }
957
958 // If there is one or more real break points check whether any of these are
959 // triggered.
ulan@chromium.org09d7ab52013-02-25 15:50:35 +0000960 Handle<Object> break_points_hit(heap->undefined_value(), isolate_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000961 if (break_location_iterator.HasBreakPoint()) {
962 Handle<Object> break_point_objects =
ulan@chromium.org09d7ab52013-02-25 15:50:35 +0000963 Handle<Object>(break_location_iterator.BreakPointObjects(), isolate_);
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000964 break_points_hit = CheckBreakPoints(break_point_objects);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000965 }
966
ager@chromium.orga1645e22009-09-09 19:27:10 +0000967 // If step out is active skip everything until the frame where we need to step
968 // out to is reached, unless real breakpoint is hit.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000969 if (StepOutActive() && frame->fp() != step_out_fp() &&
ager@chromium.orga1645e22009-09-09 19:27:10 +0000970 break_points_hit->IsUndefined() ) {
971 // Step count should always be 0 for StepOut.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000972 ASSERT(thread_local_.step_count_ == 0);
ager@chromium.orga1645e22009-09-09 19:27:10 +0000973 } else if (!break_points_hit->IsUndefined() ||
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000974 (thread_local_.last_step_action_ != StepNone &&
975 thread_local_.step_count_ == 0)) {
ager@chromium.orga1645e22009-09-09 19:27:10 +0000976 // Notify debugger if a real break point is triggered or if performing
977 // single stepping with no more steps to perform. Otherwise do another step.
978
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000979 // Clear all current stepping setup.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000980 ClearStepping();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000981
lrn@chromium.org34e60782011-09-15 07:25:40 +0000982 if (thread_local_.queued_step_count_ > 0) {
983 // Perform queued steps
984 int step_count = thread_local_.queued_step_count_;
985
986 // Clear queue
987 thread_local_.queued_step_count_ = 0;
988
989 PrepareStep(StepNext, step_count);
990 } else {
991 // Notify the debug event listeners.
992 isolate_->debugger()->OnDebugBreak(break_points_hit, false);
993 }
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000994 } else if (thread_local_.last_step_action_ != StepNone) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000995 // Hold on to last step action as it is cleared by the call to
996 // ClearStepping.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000997 StepAction step_action = thread_local_.last_step_action_;
998 int step_count = thread_local_.step_count_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000999
lrn@chromium.org34e60782011-09-15 07:25:40 +00001000 // If StepNext goes deeper in code, StepOut until original frame
1001 // and keep step count queued up in the meantime.
1002 if (step_action == StepNext && frame->fp() < thread_local_.last_fp_) {
1003 // Count frames until target frame
1004 int count = 0;
1005 JavaScriptFrameIterator it(isolate_);
jkummerow@chromium.org212d9642012-05-11 15:02:09 +00001006 while (!it.done() && it.frame()->fp() < thread_local_.last_fp_) {
lrn@chromium.org34e60782011-09-15 07:25:40 +00001007 count++;
1008 it.Advance();
1009 }
1010
danno@chromium.org81cac2b2012-07-10 11:28:27 +00001011 // Check that we indeed found the frame we are looking for.
1012 CHECK(!it.done() && (it.frame()->fp() == thread_local_.last_fp_));
1013 if (step_count > 1) {
1014 // Save old count and action to continue stepping after StepOut.
1015 thread_local_.queued_step_count_ = step_count - 1;
jkummerow@chromium.org212d9642012-05-11 15:02:09 +00001016 }
1017
danno@chromium.org81cac2b2012-07-10 11:28:27 +00001018 // Set up for StepOut to reach target frame.
1019 step_action = StepOut;
1020 step_count = count;
lrn@chromium.org34e60782011-09-15 07:25:40 +00001021 }
1022
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001023 // Clear all current stepping setup.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001024 ClearStepping();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001025
1026 // Set up for the remaining steps.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001027 PrepareStep(step_action, step_count);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001028 }
1029
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001030 if (thread_local_.frame_drop_mode_ == FRAMES_UNTOUCHED) {
1031 SetAfterBreakTarget(frame);
1032 } else if (thread_local_.frame_drop_mode_ ==
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001033 FRAME_DROPPED_IN_IC_CALL) {
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001034 // We must have been calling IC stub. Do not go there anymore.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001035 Code* plain_return = isolate_->builtins()->builtin(
1036 Builtins::kPlainReturn_LiveEdit);
1037 thread_local_.after_break_target_ = plain_return->entry();
1038 } else if (thread_local_.frame_drop_mode_ ==
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001039 FRAME_DROPPED_IN_DEBUG_SLOT_CALL) {
1040 // Debug break slot stub does not return normally, instead it manually
1041 // cleans the stack and jumps. We should patch the jump address.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001042 Code* plain_return = isolate_->builtins()->builtin(
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00001043 Builtins::kFrameDropper_LiveEdit);
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001044 thread_local_.after_break_target_ = plain_return->entry();
1045 } else if (thread_local_.frame_drop_mode_ ==
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001046 FRAME_DROPPED_IN_DIRECT_CALL) {
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001047 // Nothing to do, after_break_target is not used here.
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00001048 } else if (thread_local_.frame_drop_mode_ ==
1049 FRAME_DROPPED_IN_RETURN_CALL) {
1050 Code* plain_return = isolate_->builtins()->builtin(
1051 Builtins::kFrameDropper_LiveEdit);
1052 thread_local_.after_break_target_ = plain_return->entry();
ager@chromium.org357bf652010-04-12 11:30:10 +00001053 } else {
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001054 UNREACHABLE();
ager@chromium.org357bf652010-04-12 11:30:10 +00001055 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001056
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001057 return heap->undefined_value();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001058}
1059
1060
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001061RUNTIME_FUNCTION(Object*, Debug_Break) {
1062 return isolate->debug()->Break(args);
1063}
1064
1065
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001066// Check the break point objects for whether one or more are actually
1067// triggered. This function returns a JSArray with the break point objects
1068// which is triggered.
1069Handle<Object> Debug::CheckBreakPoints(Handle<Object> break_point_objects) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00001070 Factory* factory = isolate_->factory();
1071
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001072 // Count the number of break points hit. If there are multiple break points
1073 // they are in a FixedArray.
1074 Handle<FixedArray> break_points_hit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001075 int break_points_hit_count = 0;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001076 ASSERT(!break_point_objects->IsUndefined());
1077 if (break_point_objects->IsFixedArray()) {
1078 Handle<FixedArray> array(FixedArray::cast(*break_point_objects));
lrn@chromium.org7516f052011-03-30 08:52:27 +00001079 break_points_hit = factory->NewFixedArray(array->length());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001080 for (int i = 0; i < array->length(); i++) {
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00001081 Handle<Object> o(array->get(i), isolate_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001082 if (CheckBreakPoint(o)) {
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001083 break_points_hit->set(break_points_hit_count++, *o);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001084 }
1085 }
1086 } else {
lrn@chromium.org7516f052011-03-30 08:52:27 +00001087 break_points_hit = factory->NewFixedArray(1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001088 if (CheckBreakPoint(break_point_objects)) {
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001089 break_points_hit->set(break_points_hit_count++, *break_point_objects);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001090 }
1091 }
1092
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001093 // Return undefined if no break points were triggered.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001094 if (break_points_hit_count == 0) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00001095 return factory->undefined_value();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001096 }
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001097 // Return break points hit as a JSArray.
lrn@chromium.org7516f052011-03-30 08:52:27 +00001098 Handle<JSArray> result = factory->NewJSArrayWithElements(break_points_hit);
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001099 result->set_length(Smi::FromInt(break_points_hit_count));
1100 return result;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001101}
1102
1103
1104// Check whether a single break point object is triggered.
1105bool Debug::CheckBreakPoint(Handle<Object> break_point_object) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00001106 Factory* factory = isolate_->factory();
1107 HandleScope scope(isolate_);
ager@chromium.org381abbb2009-02-25 13:23:22 +00001108
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001109 // Ignore check if break point object is not a JSObject.
1110 if (!break_point_object->IsJSObject()) return true;
1111
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001112 // Get the function IsBreakPointTriggered (defined in debug-debugger.js).
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001113 Handle<String> is_break_point_triggered_string =
1114 factory->InternalizeOneByteString(
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00001115 STATIC_ASCII_VECTOR("IsBreakPointTriggered"));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001116 Handle<JSFunction> check_break_point =
1117 Handle<JSFunction>(JSFunction::cast(
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00001118 debug_context()->global_object()->GetPropertyNoExceptionThrown(
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001119 *is_break_point_triggered_string)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001120
1121 // Get the break id as an object.
lrn@chromium.org7516f052011-03-30 08:52:27 +00001122 Handle<Object> break_id = factory->NewNumberFromInt(Debug::break_id());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001123
1124 // Call HandleBreakPointx.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001125 bool caught_exception;
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00001126 Handle<Object> argv[] = { break_id, break_point_object };
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001127 Handle<Object> result = Execution::TryCall(check_break_point,
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00001128 isolate_->js_builtins_object(),
1129 ARRAY_SIZE(argv),
1130 argv,
1131 &caught_exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001132
1133 // If exception or non boolean result handle as not triggered
1134 if (caught_exception || !result->IsBoolean()) {
1135 return false;
1136 }
1137
1138 // Return whether the break point is triggered.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001139 ASSERT(!result.is_null());
1140 return (*result)->IsTrue();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001141}
1142
1143
1144// Check whether the function has debug information.
1145bool Debug::HasDebugInfo(Handle<SharedFunctionInfo> shared) {
1146 return !shared->debug_info()->IsUndefined();
1147}
1148
1149
kasper.lundbd3ec4e2008-07-09 11:06:54 +00001150// Return the debug info for this function. EnsureDebugInfo must be called
1151// prior to ensure the debug info has been generated for shared.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001152Handle<DebugInfo> Debug::GetDebugInfo(Handle<SharedFunctionInfo> shared) {
kasper.lundbd3ec4e2008-07-09 11:06:54 +00001153 ASSERT(HasDebugInfo(shared));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001154 return Handle<DebugInfo>(DebugInfo::cast(shared->debug_info()));
1155}
1156
1157
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001158void Debug::SetBreakPoint(Handle<JSFunction> function,
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00001159 Handle<Object> break_point_object,
1160 int* source_position) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00001161 HandleScope scope(isolate_);
ager@chromium.org381abbb2009-02-25 13:23:22 +00001162
lrn@chromium.org34e60782011-09-15 07:25:40 +00001163 PrepareForBreakPoints();
1164
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001165 // Make sure the function is compiled and has set up the debug info.
1166 Handle<SharedFunctionInfo> shared(function->shared());
1167 if (!EnsureDebugInfo(shared, function)) {
kasper.lundbd3ec4e2008-07-09 11:06:54 +00001168 // Return if retrieving debug info failed.
1169 return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001170 }
1171
kasper.lundbd3ec4e2008-07-09 11:06:54 +00001172 Handle<DebugInfo> debug_info = GetDebugInfo(shared);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001173 // Source positions starts with zero.
danno@chromium.orgbf0c8202011-12-27 10:09:42 +00001174 ASSERT(*source_position >= 0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001175
1176 // Find the break point and change it.
1177 BreakLocationIterator it(debug_info, SOURCE_BREAK_LOCATIONS);
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00001178 it.FindBreakLocationFromPosition(*source_position);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001179 it.SetBreakPoint(break_point_object);
1180
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00001181 *source_position = it.position();
1182
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001183 // At least one active break point now.
1184 ASSERT(debug_info->GetBreakPointCount() > 0);
1185}
1186
1187
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001188bool Debug::SetBreakPointForScript(Handle<Script> script,
1189 Handle<Object> break_point_object,
1190 int* source_position) {
1191 HandleScope scope(isolate_);
1192
jkummerow@chromium.org78502a92012-09-06 13:50:42 +00001193 PrepareForBreakPoints();
1194
1195 // Obtain shared function info for the function.
1196 Object* result = FindSharedFunctionInfoInScript(script, *source_position);
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001197 if (result->IsUndefined()) return false;
1198
1199 // Make sure the function has set up the debug info.
1200 Handle<SharedFunctionInfo> shared(SharedFunctionInfo::cast(result));
1201 if (!EnsureDebugInfo(shared, Handle<JSFunction>::null())) {
1202 // Return if retrieving debug info failed.
1203 return false;
1204 }
1205
1206 // Find position within function. The script position might be before the
1207 // source position of the first function.
1208 int position;
1209 if (shared->start_position() > *source_position) {
1210 position = 0;
1211 } else {
1212 position = *source_position - shared->start_position();
1213 }
1214
1215 Handle<DebugInfo> debug_info = GetDebugInfo(shared);
1216 // Source positions starts with zero.
1217 ASSERT(position >= 0);
1218
1219 // Find the break point and change it.
1220 BreakLocationIterator it(debug_info, SOURCE_BREAK_LOCATIONS);
1221 it.FindBreakLocationFromPosition(position);
1222 it.SetBreakPoint(break_point_object);
1223
1224 *source_position = it.position() + shared->start_position();
1225
1226 // At least one active break point now.
1227 ASSERT(debug_info->GetBreakPointCount() > 0);
1228 return true;
1229}
1230
1231
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001232void Debug::ClearBreakPoint(Handle<Object> break_point_object) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00001233 HandleScope scope(isolate_);
ager@chromium.org381abbb2009-02-25 13:23:22 +00001234
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001235 DebugInfoListNode* node = debug_info_list_;
1236 while (node != NULL) {
1237 Object* result = DebugInfo::FindBreakPointInfo(node->debug_info(),
1238 break_point_object);
1239 if (!result->IsUndefined()) {
1240 // Get information in the break point.
1241 BreakPointInfo* break_point_info = BreakPointInfo::cast(result);
1242 Handle<DebugInfo> debug_info = node->debug_info();
1243 Handle<SharedFunctionInfo> shared(debug_info->shared());
1244 int source_position = break_point_info->statement_position()->value();
1245
1246 // Source positions starts with zero.
1247 ASSERT(source_position >= 0);
1248
1249 // Find the break point and clear it.
1250 BreakLocationIterator it(debug_info, SOURCE_BREAK_LOCATIONS);
1251 it.FindBreakLocationFromPosition(source_position);
1252 it.ClearBreakPoint(break_point_object);
1253
1254 // If there are no more break points left remove the debug info for this
1255 // function.
1256 if (debug_info->GetBreakPointCount() == 0) {
1257 RemoveDebugInfo(debug_info);
1258 }
1259
1260 return;
1261 }
1262 node = node->next();
1263 }
1264}
1265
1266
ager@chromium.org381abbb2009-02-25 13:23:22 +00001267void Debug::ClearAllBreakPoints() {
1268 DebugInfoListNode* node = debug_info_list_;
1269 while (node != NULL) {
1270 // Remove all debug break code.
1271 BreakLocationIterator it(node->debug_info(), ALL_BREAK_LOCATIONS);
1272 it.ClearAllDebugBreak();
1273 node = node->next();
1274 }
1275
1276 // Remove all debug info.
1277 while (debug_info_list_ != NULL) {
1278 RemoveDebugInfo(debug_info_list_->debug_info());
1279 }
1280}
1281
1282
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001283void Debug::FloodWithOneShot(Handle<JSFunction> function) {
lrn@chromium.org34e60782011-09-15 07:25:40 +00001284 PrepareForBreakPoints();
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001285
1286 // Make sure the function is compiled and has set up the debug info.
1287 Handle<SharedFunctionInfo> shared(function->shared());
1288 if (!EnsureDebugInfo(shared, function)) {
kasper.lundbd3ec4e2008-07-09 11:06:54 +00001289 // Return if we failed to retrieve the debug info.
1290 return;
1291 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001292
1293 // Flood the function with break points.
kasper.lundbd3ec4e2008-07-09 11:06:54 +00001294 BreakLocationIterator it(GetDebugInfo(shared), ALL_BREAK_LOCATIONS);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001295 while (!it.Done()) {
1296 it.SetOneShot();
1297 it.Next();
1298 }
1299}
1300
1301
rossberg@chromium.org2c067b12012-03-19 11:01:52 +00001302void Debug::FloodBoundFunctionWithOneShot(Handle<JSFunction> function) {
1303 Handle<FixedArray> new_bindings(function->function_bindings());
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00001304 Handle<Object> bindee(new_bindings->get(JSFunction::kBoundFunctionIndex),
1305 isolate_);
rossberg@chromium.org2c067b12012-03-19 11:01:52 +00001306
1307 if (!bindee.is_null() && bindee->IsJSFunction() &&
1308 !JSFunction::cast(*bindee)->IsBuiltin()) {
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001309 Handle<JSFunction> bindee_function(JSFunction::cast(*bindee));
1310 Debug::FloodWithOneShot(bindee_function);
rossberg@chromium.org2c067b12012-03-19 11:01:52 +00001311 }
1312}
1313
1314
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001315void Debug::FloodHandlerWithOneShot() {
ager@chromium.org8bb60582008-12-11 12:02:20 +00001316 // Iterate through the JavaScript stack looking for handlers.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001317 StackFrame::Id id = break_frame_id();
ager@chromium.org8bb60582008-12-11 12:02:20 +00001318 if (id == StackFrame::NO_ID) {
1319 // If there is no JavaScript stack don't do anything.
1320 return;
1321 }
vegorov@chromium.org74f333b2011-04-06 11:17:46 +00001322 for (JavaScriptFrameIterator it(isolate_, id); !it.done(); it.Advance()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001323 JavaScriptFrame* frame = it.frame();
1324 if (frame->HasHandler()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001325 // Flood the function with the catch block with break points
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001326 JSFunction* function = JSFunction::cast(frame->function());
1327 FloodWithOneShot(Handle<JSFunction>(function));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001328 return;
1329 }
1330 }
1331}
1332
1333
1334void Debug::ChangeBreakOnException(ExceptionBreakType type, bool enable) {
1335 if (type == BreakUncaughtException) {
1336 break_on_uncaught_exception_ = enable;
1337 } else {
1338 break_on_exception_ = enable;
1339 }
1340}
1341
1342
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00001343bool Debug::IsBreakOnException(ExceptionBreakType type) {
1344 if (type == BreakUncaughtException) {
1345 return break_on_uncaught_exception_;
1346 } else {
1347 return break_on_exception_;
1348 }
1349}
1350
1351
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001352void Debug::PrepareStep(StepAction step_action, int step_count) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00001353 HandleScope scope(isolate_);
lrn@chromium.org34e60782011-09-15 07:25:40 +00001354
1355 PrepareForBreakPoints();
1356
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001357 ASSERT(Debug::InDebugger());
1358
1359 // Remember this step action and count.
1360 thread_local_.last_step_action_ = step_action;
ager@chromium.orga1645e22009-09-09 19:27:10 +00001361 if (step_action == StepOut) {
1362 // For step out target frame will be found on the stack so there is no need
1363 // to set step counter for it. It's expected to always be 0 for StepOut.
1364 thread_local_.step_count_ = 0;
1365 } else {
1366 thread_local_.step_count_ = step_count;
1367 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001368
1369 // Get the frame where the execution has stopped and skip the debug frame if
1370 // any. The debug frame will only be present if execution was stopped due to
1371 // hitting a break point. In other situations (e.g. unhandled exception) the
1372 // debug frame is not present.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001373 StackFrame::Id id = break_frame_id();
ager@chromium.org8bb60582008-12-11 12:02:20 +00001374 if (id == StackFrame::NO_ID) {
1375 // If there is no JavaScript stack don't do anything.
1376 return;
1377 }
vegorov@chromium.org74f333b2011-04-06 11:17:46 +00001378 JavaScriptFrameIterator frames_it(isolate_, id);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001379 JavaScriptFrame* frame = frames_it.frame();
1380
1381 // First of all ensure there is one-shot break points in the top handler
1382 // if any.
1383 FloodHandlerWithOneShot();
1384
1385 // If the function on the top frame is unresolved perform step out. This will
1386 // be the case when calling unknown functions and having the debugger stopped
1387 // in an unhandled exception.
1388 if (!frame->function()->IsJSFunction()) {
1389 // Step out: Find the calling JavaScript frame and flood it with
1390 // breakpoints.
1391 frames_it.Advance();
1392 // Fill the function to return to with one-shot break points.
1393 JSFunction* function = JSFunction::cast(frames_it.frame()->function());
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001394 FloodWithOneShot(Handle<JSFunction>(function));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001395 return;
1396 }
1397
1398 // Get the debug info (create it if it does not exist).
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001399 Handle<JSFunction> function(JSFunction::cast(frame->function()));
1400 Handle<SharedFunctionInfo> shared(function->shared());
1401 if (!EnsureDebugInfo(shared, function)) {
kasper.lundbd3ec4e2008-07-09 11:06:54 +00001402 // Return if ensuring debug info failed.
1403 return;
1404 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001405 Handle<DebugInfo> debug_info = GetDebugInfo(shared);
1406
1407 // Find the break location where execution has stopped.
1408 BreakLocationIterator it(debug_info, ALL_BREAK_LOCATIONS);
1409 it.FindBreakLocationFromAddress(frame->pc());
1410
1411 // Compute whether or not the target is a call target.
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001412 bool is_load_or_store = false;
1413 bool is_inline_cache_stub = false;
whesse@chromium.orge90029b2010-08-02 11:52:17 +00001414 bool is_at_restarted_function = false;
ager@chromium.orga1645e22009-09-09 19:27:10 +00001415 Handle<Code> call_function_stub;
ager@chromium.orga1645e22009-09-09 19:27:10 +00001416
whesse@chromium.orge90029b2010-08-02 11:52:17 +00001417 if (thread_local_.restarter_frame_function_pointer_ == NULL) {
1418 if (RelocInfo::IsCodeTarget(it.rinfo()->rmode())) {
1419 bool is_call_target = false;
1420 Address target = it.rinfo()->target_address();
1421 Code* code = Code::GetCodeFromTargetAddress(target);
1422 if (code->is_call_stub() || code->is_keyed_call_stub()) {
1423 is_call_target = true;
1424 }
1425 if (code->is_inline_cache_stub()) {
1426 is_inline_cache_stub = true;
1427 is_load_or_store = !is_call_target;
1428 }
1429
1430 // Check if target code is CallFunction stub.
1431 Code* maybe_call_function_stub = code;
1432 // If there is a breakpoint at this line look at the original code to
1433 // check if it is a CallFunction stub.
1434 if (it.IsDebugBreak()) {
1435 Address original_target = it.original_rinfo()->target_address();
1436 maybe_call_function_stub =
1437 Code::GetCodeFromTargetAddress(original_target);
1438 }
1439 if (maybe_call_function_stub->kind() == Code::STUB &&
1440 maybe_call_function_stub->major_key() == CodeStub::CallFunction) {
1441 // Save reference to the code as we may need it to find out arguments
1442 // count for 'step in' later.
1443 call_function_stub = Handle<Code>(maybe_call_function_stub);
1444 }
ager@chromium.orga1645e22009-09-09 19:27:10 +00001445 }
whesse@chromium.orge90029b2010-08-02 11:52:17 +00001446 } else {
1447 is_at_restarted_function = true;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001448 }
1449
v8.team.kasperl727e9952008-09-02 14:56:44 +00001450 // If this is the last break code target step out is the only possibility.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001451 if (it.IsExit() || step_action == StepOut) {
ager@chromium.orga1645e22009-09-09 19:27:10 +00001452 if (step_action == StepOut) {
1453 // Skip step_count frames starting with the current one.
1454 while (step_count-- > 0 && !frames_it.done()) {
1455 frames_it.Advance();
1456 }
1457 } else {
1458 ASSERT(it.IsExit());
1459 frames_it.Advance();
1460 }
1461 // Skip builtin functions on the stack.
1462 while (!frames_it.done() &&
1463 JSFunction::cast(frames_it.frame()->function())->IsBuiltin()) {
1464 frames_it.Advance();
1465 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001466 // Step out: If there is a JavaScript caller frame, we need to
1467 // flood it with breakpoints.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001468 if (!frames_it.done()) {
1469 // Fill the function to return to with one-shot break points.
1470 JSFunction* function = JSFunction::cast(frames_it.frame()->function());
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001471 FloodWithOneShot(Handle<JSFunction>(function));
ager@chromium.orga1645e22009-09-09 19:27:10 +00001472 // Set target frame pointer.
1473 ActivateStepOut(frames_it.frame());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001474 }
ager@chromium.orga1645e22009-09-09 19:27:10 +00001475 } else if (!(is_inline_cache_stub || RelocInfo::IsConstructCall(it.rmode()) ||
whesse@chromium.orge90029b2010-08-02 11:52:17 +00001476 !call_function_stub.is_null() || is_at_restarted_function)
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001477 || step_action == StepNext || step_action == StepMin) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001478 // Step next or step min.
1479
1480 // Fill the current function with one-shot break points.
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001481 FloodWithOneShot(function);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001482
1483 // Remember source position and frame to handle step next.
1484 thread_local_.last_statement_position_ =
1485 debug_info->code()->SourceStatementPosition(frame->pc());
mmassi@chromium.org7028c052012-06-13 11:51:58 +00001486 thread_local_.last_fp_ = frame->UnpaddedFP();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001487 } else {
whesse@chromium.orge90029b2010-08-02 11:52:17 +00001488 // If there's restarter frame on top of the stack, just get the pointer
1489 // to function which is going to be restarted.
1490 if (is_at_restarted_function) {
1491 Handle<JSFunction> restarted_function(
1492 JSFunction::cast(*thread_local_.restarter_frame_function_pointer_));
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001493 FloodWithOneShot(restarted_function);
whesse@chromium.orge90029b2010-08-02 11:52:17 +00001494 } else if (!call_function_stub.is_null()) {
1495 // If it's CallFunction stub ensure target function is compiled and flood
1496 // it with one shot breakpoints.
1497
ager@chromium.orga1645e22009-09-09 19:27:10 +00001498 // Find out number of arguments from the stub minor key.
1499 // Reverse lookup required as the minor key cannot be retrieved
1500 // from the code object.
1501 Handle<Object> obj(
lrn@chromium.org7516f052011-03-30 08:52:27 +00001502 isolate_->heap()->code_stubs()->SlowReverseLookup(
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00001503 *call_function_stub),
1504 isolate_);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001505 ASSERT(!obj.is_null());
1506 ASSERT(!(*obj)->IsUndefined());
ager@chromium.orga1645e22009-09-09 19:27:10 +00001507 ASSERT(obj->IsSmi());
1508 // Get the STUB key and extract major and minor key.
1509 uint32_t key = Smi::cast(*obj)->value();
1510 // Argc in the stub is the number of arguments passed - not the
1511 // expected arguments of the called function.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001512 int call_function_arg_count =
1513 CallFunctionStub::ExtractArgcFromMinorKey(
1514 CodeStub::MinorKeyFromKey(key));
ager@chromium.orga1645e22009-09-09 19:27:10 +00001515 ASSERT(call_function_stub->major_key() ==
1516 CodeStub::MajorKeyFromKey(key));
1517
1518 // Find target function on the expression stack.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001519 // Expression stack looks like this (top to bottom):
ager@chromium.orga1645e22009-09-09 19:27:10 +00001520 // argN
1521 // ...
1522 // arg0
1523 // Receiver
1524 // Function to call
1525 int expressions_count = frame->ComputeExpressionsCount();
1526 ASSERT(expressions_count - 2 - call_function_arg_count >= 0);
1527 Object* fun = frame->GetExpression(
1528 expressions_count - 2 - call_function_arg_count);
1529 if (fun->IsJSFunction()) {
1530 Handle<JSFunction> js_function(JSFunction::cast(fun));
rossberg@chromium.org2c067b12012-03-19 11:01:52 +00001531 if (js_function->shared()->bound()) {
1532 Debug::FloodBoundFunctionWithOneShot(js_function);
1533 } else if (!js_function->IsBuiltin()) {
1534 // Don't step into builtins.
ager@chromium.orga1645e22009-09-09 19:27:10 +00001535 // It will also compile target function if it's not compiled yet.
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001536 FloodWithOneShot(js_function);
ager@chromium.orga1645e22009-09-09 19:27:10 +00001537 }
1538 }
1539 }
1540
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001541 // Fill the current function with one-shot break points even for step in on
1542 // a call target as the function called might be a native function for
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001543 // which step in will not stop. It also prepares for stepping in
1544 // getters/setters.
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001545 FloodWithOneShot(function);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001546
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001547 if (is_load_or_store) {
1548 // Remember source position and frame to handle step in getter/setter. If
1549 // there is a custom getter/setter it will be handled in
1550 // Object::Get/SetPropertyWithCallback, otherwise the step action will be
1551 // propagated on the next Debug::Break.
1552 thread_local_.last_statement_position_ =
1553 debug_info->code()->SourceStatementPosition(frame->pc());
mmassi@chromium.org7028c052012-06-13 11:51:58 +00001554 thread_local_.last_fp_ = frame->UnpaddedFP();
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001555 }
1556
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001557 // Step in or Step in min
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00001558 it.PrepareStepIn(isolate_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001559 ActivateStepIn(frame);
1560 }
1561}
1562
1563
1564// Check whether the current debug break should be reported to the debugger. It
1565// is used to have step next and step in only report break back to the debugger
1566// if on a different frame or in a different statement. In some situations
1567// there will be several break points in the same statement when the code is
v8.team.kasperl727e9952008-09-02 14:56:44 +00001568// flooded with one-shot break points. This function helps to perform several
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001569// steps before reporting break back to the debugger.
1570bool Debug::StepNextContinue(BreakLocationIterator* break_location_iterator,
1571 JavaScriptFrame* frame) {
lrn@chromium.org34e60782011-09-15 07:25:40 +00001572 // StepNext and StepOut shouldn't bring us deeper in code, so last frame
1573 // shouldn't be a parent of current frame.
1574 if (thread_local_.last_step_action_ == StepNext ||
1575 thread_local_.last_step_action_ == StepOut) {
1576 if (frame->fp() < thread_local_.last_fp_) return true;
1577 }
1578
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001579 // If the step last action was step next or step in make sure that a new
1580 // statement is hit.
1581 if (thread_local_.last_step_action_ == StepNext ||
1582 thread_local_.last_step_action_ == StepIn) {
1583 // Never continue if returning from function.
1584 if (break_location_iterator->IsExit()) return false;
1585
1586 // Continue if we are still on the same frame and in the same statement.
1587 int current_statement_position =
1588 break_location_iterator->code()->SourceStatementPosition(frame->pc());
mmassi@chromium.org7028c052012-06-13 11:51:58 +00001589 return thread_local_.last_fp_ == frame->UnpaddedFP() &&
ager@chromium.org236ad962008-09-25 09:45:57 +00001590 thread_local_.last_statement_position_ == current_statement_position;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001591 }
1592
1593 // No step next action - don't continue.
1594 return false;
1595}
1596
1597
1598// Check whether the code object at the specified address is a debug break code
1599// object.
1600bool Debug::IsDebugBreak(Address addr) {
ager@chromium.org8bb60582008-12-11 12:02:20 +00001601 Code* code = Code::GetCodeFromTargetAddress(addr);
yangguo@chromium.org9768bf12013-01-11 14:51:07 +00001602 return code->is_debug_break();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001603}
1604
1605
1606// Check whether a code stub with the specified major key is a possible break
1607// point location when looking for source break locations.
1608bool Debug::IsSourceBreakStub(Code* code) {
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001609 CodeStub::Major major_key = CodeStub::GetMajorKey(code);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001610 return major_key == CodeStub::CallFunction;
1611}
1612
1613
1614// Check whether a code stub with the specified major key is a possible break
1615// location.
1616bool Debug::IsBreakStub(Code* code) {
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001617 CodeStub::Major major_key = CodeStub::GetMajorKey(code);
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00001618 return major_key == CodeStub::CallFunction;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001619}
1620
1621
1622// Find the builtin to use for invoking the debug break
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001623Handle<Code> Debug::FindDebugBreak(Handle<Code> code, RelocInfo::Mode mode) {
danno@chromium.orgfa458e42012-02-01 10:48:36 +00001624 Isolate* isolate = Isolate::Current();
1625
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001626 // Find the builtin debug break function matching the calling convention
1627 // used by the call site.
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001628 if (code->is_inline_cache_stub()) {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001629 switch (code->kind()) {
1630 case Code::CALL_IC:
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001631 case Code::KEYED_CALL_IC:
danno@chromium.orgfa458e42012-02-01 10:48:36 +00001632 return isolate->stub_cache()->ComputeCallDebugBreak(
1633 code->arguments_count(), code->kind());
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001634
1635 case Code::LOAD_IC:
danno@chromium.orgfa458e42012-02-01 10:48:36 +00001636 return isolate->builtins()->LoadIC_DebugBreak();
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001637
1638 case Code::STORE_IC:
danno@chromium.orgfa458e42012-02-01 10:48:36 +00001639 return isolate->builtins()->StoreIC_DebugBreak();
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001640
1641 case Code::KEYED_LOAD_IC:
danno@chromium.orgfa458e42012-02-01 10:48:36 +00001642 return isolate->builtins()->KeyedLoadIC_DebugBreak();
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001643
1644 case Code::KEYED_STORE_IC:
danno@chromium.orgfa458e42012-02-01 10:48:36 +00001645 return isolate->builtins()->KeyedStoreIC_DebugBreak();
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001646
danno@chromium.orgf005df62013-04-30 16:36:45 +00001647 case Code::COMPARE_NIL_IC:
1648 return isolate->builtins()->CompareNilIC_DebugBreak();
1649
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001650 default:
1651 UNREACHABLE();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001652 }
1653 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001654 if (RelocInfo::IsConstructCall(mode)) {
danno@chromium.orgfa458e42012-02-01 10:48:36 +00001655 if (code->has_function_cache()) {
1656 return isolate->builtins()->CallConstructStub_Recording_DebugBreak();
1657 } else {
1658 return isolate->builtins()->CallConstructStub_DebugBreak();
1659 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001660 }
1661 if (code->kind() == Code::STUB) {
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00001662 ASSERT(code->major_key() == CodeStub::CallFunction);
danno@chromium.orgfa458e42012-02-01 10:48:36 +00001663 if (code->has_function_cache()) {
1664 return isolate->builtins()->CallFunctionStub_Recording_DebugBreak();
1665 } else {
1666 return isolate->builtins()->CallFunctionStub_DebugBreak();
1667 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001668 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001669
1670 UNREACHABLE();
1671 return Handle<Code>::null();
1672}
1673
1674
1675// Simple function for returning the source positions for active break points.
1676Handle<Object> Debug::GetSourceBreakLocations(
1677 Handle<SharedFunctionInfo> shared) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00001678 Isolate* isolate = Isolate::Current();
1679 Heap* heap = isolate->heap();
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00001680 if (!HasDebugInfo(shared)) {
1681 return Handle<Object>(heap->undefined_value(), isolate);
1682 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001683 Handle<DebugInfo> debug_info = GetDebugInfo(shared);
1684 if (debug_info->GetBreakPointCount() == 0) {
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00001685 return Handle<Object>(heap->undefined_value(), isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001686 }
1687 Handle<FixedArray> locations =
lrn@chromium.org7516f052011-03-30 08:52:27 +00001688 isolate->factory()->NewFixedArray(debug_info->GetBreakPointCount());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001689 int count = 0;
1690 for (int i = 0; i < debug_info->break_points()->length(); i++) {
1691 if (!debug_info->break_points()->get(i)->IsUndefined()) {
1692 BreakPointInfo* break_point_info =
1693 BreakPointInfo::cast(debug_info->break_points()->get(i));
1694 if (break_point_info->GetBreakPointCount() > 0) {
1695 locations->set(count++, break_point_info->statement_position());
1696 }
1697 }
1698 }
1699 return locations;
1700}
1701
1702
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001703void Debug::NewBreak(StackFrame::Id break_frame_id) {
1704 thread_local_.break_frame_id_ = break_frame_id;
1705 thread_local_.break_id_ = ++thread_local_.break_count_;
1706}
1707
1708
1709void Debug::SetBreak(StackFrame::Id break_frame_id, int break_id) {
1710 thread_local_.break_frame_id_ = break_frame_id;
1711 thread_local_.break_id_ = break_id;
1712}
1713
1714
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001715// Handle stepping into a function.
1716void Debug::HandleStepIn(Handle<JSFunction> function,
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001717 Handle<Object> holder,
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001718 Address fp,
1719 bool is_constructor) {
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00001720 Isolate* isolate = function->GetIsolate();
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001721 // If the frame pointer is not supplied by the caller find it.
1722 if (fp == 0) {
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00001723 StackFrameIterator it(isolate);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001724 it.Advance();
1725 // For constructor functions skip another frame.
1726 if (is_constructor) {
1727 ASSERT(it.frame()->is_construct());
1728 it.Advance();
1729 }
1730 fp = it.frame()->fp();
1731 }
1732
1733 // Flood the function with one-shot break points if it is called from where
1734 // step into was requested.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001735 if (fp == step_in_fp()) {
rossberg@chromium.org2c067b12012-03-19 11:01:52 +00001736 if (function->shared()->bound()) {
1737 // Handle Function.prototype.bind
1738 Debug::FloodBoundFunctionWithOneShot(function);
1739 } else if (!function->IsBuiltin()) {
1740 // Don't allow step into functions in the native context.
kasperl@chromium.orgacae3782009-04-11 09:17:08 +00001741 if (function->shared()->code() ==
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00001742 isolate->builtins()->builtin(Builtins::kFunctionApply) ||
kasperl@chromium.orgacae3782009-04-11 09:17:08 +00001743 function->shared()->code() ==
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00001744 isolate->builtins()->builtin(Builtins::kFunctionCall)) {
kasperl@chromium.orgacae3782009-04-11 09:17:08 +00001745 // Handle function.apply and function.call separately to flood the
1746 // function to be called and not the code for Builtins::FunctionApply or
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001747 // Builtins::FunctionCall. The receiver of call/apply is the target
1748 // function.
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001749 if (!holder.is_null() && holder->IsJSFunction() &&
1750 !JSFunction::cast(*holder)->IsBuiltin()) {
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001751 Handle<JSFunction> js_function = Handle<JSFunction>::cast(holder);
1752 Debug::FloodWithOneShot(js_function);
kasperl@chromium.orgacae3782009-04-11 09:17:08 +00001753 }
1754 } else {
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001755 Debug::FloodWithOneShot(function);
kasperl@chromium.orgacae3782009-04-11 09:17:08 +00001756 }
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001757 }
1758 }
1759}
1760
1761
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001762void Debug::ClearStepping() {
1763 // Clear the various stepping setup.
1764 ClearOneShot();
1765 ClearStepIn();
ager@chromium.orga1645e22009-09-09 19:27:10 +00001766 ClearStepOut();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001767 ClearStepNext();
1768
1769 // Clear multiple step counter.
1770 thread_local_.step_count_ = 0;
1771}
1772
1773// Clears all the one-shot break points that are currently set. Normally this
1774// function is called each time a break point is hit as one shot break points
1775// are used to support stepping.
1776void Debug::ClearOneShot() {
1777 // The current implementation just runs through all the breakpoints. When the
v8.team.kasperl727e9952008-09-02 14:56:44 +00001778 // last break point for a function is removed that function is automatically
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001779 // removed from the list.
1780
1781 DebugInfoListNode* node = debug_info_list_;
1782 while (node != NULL) {
1783 BreakLocationIterator it(node->debug_info(), ALL_BREAK_LOCATIONS);
1784 while (!it.Done()) {
1785 it.ClearOneShot();
1786 it.Next();
1787 }
1788 node = node->next();
1789 }
1790}
1791
1792
1793void Debug::ActivateStepIn(StackFrame* frame) {
ager@chromium.orga1645e22009-09-09 19:27:10 +00001794 ASSERT(!StepOutActive());
mmassi@chromium.org7028c052012-06-13 11:51:58 +00001795 thread_local_.step_into_fp_ = frame->UnpaddedFP();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001796}
1797
1798
1799void Debug::ClearStepIn() {
1800 thread_local_.step_into_fp_ = 0;
1801}
1802
1803
ager@chromium.orga1645e22009-09-09 19:27:10 +00001804void Debug::ActivateStepOut(StackFrame* frame) {
1805 ASSERT(!StepInActive());
mmassi@chromium.org7028c052012-06-13 11:51:58 +00001806 thread_local_.step_out_fp_ = frame->UnpaddedFP();
ager@chromium.orga1645e22009-09-09 19:27:10 +00001807}
1808
1809
1810void Debug::ClearStepOut() {
1811 thread_local_.step_out_fp_ = 0;
1812}
1813
1814
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001815void Debug::ClearStepNext() {
1816 thread_local_.last_step_action_ = StepNone;
ager@chromium.org236ad962008-09-25 09:45:57 +00001817 thread_local_.last_statement_position_ = RelocInfo::kNoPosition;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001818 thread_local_.last_fp_ = 0;
1819}
1820
1821
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001822// Helper function to compile full code for debugging. This code will
mmassi@chromium.org7028c052012-06-13 11:51:58 +00001823// have debug break slots and deoptimization information. Deoptimization
1824// information is required in case that an optimized version of this
1825// function is still activated on the stack. It will also make sure that
1826// the full code is compiled with the same flags as the previous version,
1827// that is flags which can change the code generated. The current method
1828// of mapping from already compiled full code without debug break slots
1829// to full code with debug break slots depends on the generated code is
1830// otherwise exactly the same.
1831static bool CompileFullCodeForDebugging(Handle<JSFunction> function,
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001832 Handle<Code> current_code) {
1833 ASSERT(!current_code->has_debug_break_slots());
1834
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001835 CompilationInfoWithZone info(function);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001836 info.MarkCompilingForDebugging(current_code);
1837 ASSERT(!info.shared_info()->is_compiled());
1838 ASSERT(!info.isolate()->has_pending_exception());
1839
1840 // Use compile lazy which will end up compiling the full code in the
1841 // configuration configured above.
1842 bool result = Compiler::CompileLazy(&info);
1843 ASSERT(result != Isolate::Current()->has_pending_exception());
1844 info.isolate()->clear_pending_exception();
1845#if DEBUG
1846 if (result) {
mmassi@chromium.org7028c052012-06-13 11:51:58 +00001847 Handle<Code> new_code(function->shared()->code());
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001848 ASSERT(new_code->has_debug_break_slots());
1849 ASSERT(current_code->is_compiled_optimizable() ==
1850 new_code->is_compiled_optimizable());
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001851 }
1852#endif
1853 return result;
1854}
1855
1856
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001857static void CollectActiveFunctionsFromThread(
1858 Isolate* isolate,
1859 ThreadLocalTop* top,
1860 List<Handle<JSFunction> >* active_functions,
1861 Object* active_code_marker) {
1862 // Find all non-optimized code functions with activation frames
1863 // on the stack. This includes functions which have optimized
1864 // activations (including inlined functions) on the stack as the
1865 // non-optimized code is needed for the lazy deoptimization.
1866 for (JavaScriptFrameIterator it(isolate, top); !it.done(); it.Advance()) {
1867 JavaScriptFrame* frame = it.frame();
1868 if (frame->is_optimized()) {
1869 List<JSFunction*> functions(Compiler::kMaxInliningLevels + 1);
1870 frame->GetFunctions(&functions);
1871 for (int i = 0; i < functions.length(); i++) {
1872 JSFunction* function = functions[i];
1873 active_functions->Add(Handle<JSFunction>(function));
1874 function->shared()->code()->set_gc_metadata(active_code_marker);
1875 }
1876 } else if (frame->function()->IsJSFunction()) {
1877 JSFunction* function = JSFunction::cast(frame->function());
1878 ASSERT(frame->LookupCode()->kind() == Code::FUNCTION);
1879 active_functions->Add(Handle<JSFunction>(function));
1880 function->shared()->code()->set_gc_metadata(active_code_marker);
1881 }
1882 }
1883}
1884
1885
1886static void RedirectActivationsToRecompiledCodeOnThread(
1887 Isolate* isolate,
1888 ThreadLocalTop* top) {
1889 for (JavaScriptFrameIterator it(isolate, top); !it.done(); it.Advance()) {
1890 JavaScriptFrame* frame = it.frame();
1891
1892 if (frame->is_optimized() || !frame->function()->IsJSFunction()) continue;
1893
1894 JSFunction* function = JSFunction::cast(frame->function());
1895
1896 ASSERT(frame->LookupCode()->kind() == Code::FUNCTION);
1897
1898 Handle<Code> frame_code(frame->LookupCode());
1899 if (frame_code->has_debug_break_slots()) continue;
1900
1901 Handle<Code> new_code(function->shared()->code());
1902 if (new_code->kind() != Code::FUNCTION ||
1903 !new_code->has_debug_break_slots()) {
1904 continue;
1905 }
1906
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001907 // Iterate over the RelocInfo in the original code to compute the sum of the
1908 // constant pools sizes. (See Assembler::CheckConstPool())
1909 // Note that this is only useful for architectures using constant pools.
1910 int constpool_mask = RelocInfo::ModeMask(RelocInfo::CONST_POOL);
1911 int frame_const_pool_size = 0;
1912 for (RelocIterator it(*frame_code, constpool_mask); !it.done(); it.next()) {
1913 RelocInfo* info = it.rinfo();
1914 if (info->pc() >= frame->pc()) break;
1915 frame_const_pool_size += static_cast<int>(info->data());
1916 }
1917 intptr_t frame_offset =
1918 frame->pc() - frame_code->instruction_start() - frame_const_pool_size;
1919
1920 // Iterate over the RelocInfo for new code to find the number of bytes
1921 // generated for debug slots and constant pools.
1922 int debug_break_slot_bytes = 0;
1923 int new_code_const_pool_size = 0;
1924 int mask = RelocInfo::ModeMask(RelocInfo::DEBUG_BREAK_SLOT) |
1925 RelocInfo::ModeMask(RelocInfo::CONST_POOL);
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001926 for (RelocIterator it(*new_code, mask); !it.done(); it.next()) {
1927 // Check if the pc in the new code with debug break
1928 // slots is before this slot.
1929 RelocInfo* info = it.rinfo();
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001930 intptr_t new_offset = info->pc() - new_code->instruction_start() -
1931 new_code_const_pool_size - debug_break_slot_bytes;
1932 if (new_offset >= frame_offset) {
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001933 break;
1934 }
1935
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001936 if (RelocInfo::IsDebugBreakSlot(info->rmode())) {
1937 debug_break_slot_bytes += Assembler::kDebugBreakSlotLength;
1938 } else {
1939 ASSERT(RelocInfo::IsConstPool(info->rmode()));
1940 // The size of the constant pool is encoded in the data.
1941 new_code_const_pool_size += static_cast<int>(info->data());
1942 }
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001943 }
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001944
1945 // Compute the equivalent pc in the new code.
1946 byte* new_pc = new_code->instruction_start() + frame_offset +
1947 debug_break_slot_bytes + new_code_const_pool_size;
1948
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001949 if (FLAG_trace_deopt) {
1950 PrintF("Replacing code %08" V8PRIxPTR " - %08" V8PRIxPTR " (%d) "
1951 "with %08" V8PRIxPTR " - %08" V8PRIxPTR " (%d) "
1952 "for debugging, "
1953 "changing pc from %08" V8PRIxPTR " to %08" V8PRIxPTR "\n",
1954 reinterpret_cast<intptr_t>(
1955 frame_code->instruction_start()),
1956 reinterpret_cast<intptr_t>(
1957 frame_code->instruction_start()) +
1958 frame_code->instruction_size(),
1959 frame_code->instruction_size(),
1960 reinterpret_cast<intptr_t>(new_code->instruction_start()),
1961 reinterpret_cast<intptr_t>(new_code->instruction_start()) +
1962 new_code->instruction_size(),
1963 new_code->instruction_size(),
1964 reinterpret_cast<intptr_t>(frame->pc()),
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001965 reinterpret_cast<intptr_t>(new_pc));
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001966 }
1967
1968 // Patch the return address to return into the code with
1969 // debug break slots.
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001970 frame->set_pc(new_pc);
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001971 }
1972}
1973
1974
1975class ActiveFunctionsCollector : public ThreadVisitor {
1976 public:
1977 explicit ActiveFunctionsCollector(List<Handle<JSFunction> >* active_functions,
1978 Object* active_code_marker)
1979 : active_functions_(active_functions),
1980 active_code_marker_(active_code_marker) { }
1981
1982 void VisitThread(Isolate* isolate, ThreadLocalTop* top) {
1983 CollectActiveFunctionsFromThread(isolate,
1984 top,
1985 active_functions_,
1986 active_code_marker_);
1987 }
1988
1989 private:
1990 List<Handle<JSFunction> >* active_functions_;
1991 Object* active_code_marker_;
1992};
1993
1994
1995class ActiveFunctionsRedirector : public ThreadVisitor {
1996 public:
1997 void VisitThread(Isolate* isolate, ThreadLocalTop* top) {
1998 RedirectActivationsToRecompiledCodeOnThread(isolate, top);
1999 }
2000};
2001
2002
lrn@chromium.org34e60782011-09-15 07:25:40 +00002003void Debug::PrepareForBreakPoints() {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002004 // If preparing for the first break point make sure to deoptimize all
2005 // functions as debugging does not work with optimized code.
2006 if (!has_break_points_) {
svenpanne@chromium.org876cca82013-03-18 14:43:20 +00002007 Deoptimizer::DeoptimizeAll(isolate_);
lrn@chromium.org34e60782011-09-15 07:25:40 +00002008
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002009 Handle<Code> lazy_compile =
2010 Handle<Code>(isolate_->builtins()->builtin(Builtins::kLazyCompile));
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002011
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00002012 // There will be at least one break point when we are done.
2013 has_break_points_ = true;
2014
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002015 // Keep the list of activated functions in a handlified list as it
2016 // is used both in GC and non-GC code.
2017 List<Handle<JSFunction> > active_functions(100);
lrn@chromium.org34e60782011-09-15 07:25:40 +00002018
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002019 {
2020 // We are going to iterate heap to find all functions without
2021 // debug break slots.
hpayer@chromium.org7c3372b2013-02-13 17:26:04 +00002022 Heap* heap = isolate_->heap();
2023 heap->CollectAllGarbage(Heap::kMakeHeapIterableMask,
2024 "preparing for breakpoints");
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002025
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002026 // Ensure no GC in this scope as we are going to use gc_metadata
2027 // field in the Code object to mark active functions.
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002028 AssertNoAllocation no_allocation;
2029
hpayer@chromium.org7c3372b2013-02-13 17:26:04 +00002030 Object* active_code_marker = heap->the_hole_value();
svenpanne@chromium.orgecb9dd62011-12-01 08:22:35 +00002031
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002032 CollectActiveFunctionsFromThread(isolate_,
2033 isolate_->thread_local_top(),
2034 &active_functions,
2035 active_code_marker);
2036 ActiveFunctionsCollector active_functions_collector(&active_functions,
2037 active_code_marker);
2038 isolate_->thread_manager()->IterateArchivedThreads(
2039 &active_functions_collector);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002040
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002041 // Scan the heap for all non-optimized functions which have no
2042 // debug break slots and are not active or inlined into an active
2043 // function and mark them for lazy compilation.
hpayer@chromium.org7c3372b2013-02-13 17:26:04 +00002044 HeapIterator iterator(heap);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002045 HeapObject* obj = NULL;
2046 while (((obj = iterator.next()) != NULL)) {
2047 if (obj->IsJSFunction()) {
2048 JSFunction* function = JSFunction::cast(obj);
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002049 SharedFunctionInfo* shared = function->shared();
2050 if (shared->allows_lazy_compilation() &&
2051 shared->script()->IsScript() &&
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002052 function->code()->kind() == Code::FUNCTION &&
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002053 !function->code()->has_debug_break_slots() &&
2054 shared->code()->gc_metadata() != active_code_marker) {
2055 function->set_code(*lazy_compile);
2056 function->shared()->set_code(*lazy_compile);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002057 }
2058 }
lrn@chromium.org34e60782011-09-15 07:25:40 +00002059 }
lrn@chromium.org34e60782011-09-15 07:25:40 +00002060
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002061 // Clear gc_metadata field.
2062 for (int i = 0; i < active_functions.length(); i++) {
2063 Handle<JSFunction> function = active_functions[i];
2064 function->shared()->code()->set_gc_metadata(Smi::FromInt(0));
2065 }
2066 }
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002067
2068 // Now recompile all functions with activation frames and and
2069 // patch the return address to run in the new compiled code.
2070 for (int i = 0; i < active_functions.length(); i++) {
2071 Handle<JSFunction> function = active_functions[i];
mmassi@chromium.org7028c052012-06-13 11:51:58 +00002072 Handle<SharedFunctionInfo> shared(function->shared());
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002073
2074 if (function->code()->kind() == Code::FUNCTION &&
2075 function->code()->has_debug_break_slots()) {
2076 // Nothing to do. Function code already had debug break slots.
2077 continue;
2078 }
2079
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002080 // If recompilation is not possible just skip it.
2081 if (shared->is_toplevel() ||
2082 !shared->allows_lazy_compilation() ||
2083 shared->code()->kind() == Code::BUILTIN) {
2084 continue;
2085 }
2086
2087 // Make sure that the shared full code is compiled with debug
2088 // break slots.
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00002089 if (!shared->code()->has_debug_break_slots()) {
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002090 // Try to compile the full code with debug break slots. If it
2091 // fails just keep the current code.
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00002092 Handle<Code> current_code(function->shared()->code());
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002093 shared->set_code(*lazy_compile);
2094 bool prev_force_debugger_active =
2095 isolate_->debugger()->force_debugger_active();
2096 isolate_->debugger()->set_force_debugger_active(true);
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00002097 ASSERT(current_code->kind() == Code::FUNCTION);
mmassi@chromium.org7028c052012-06-13 11:51:58 +00002098 CompileFullCodeForDebugging(function, current_code);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002099 isolate_->debugger()->set_force_debugger_active(
2100 prev_force_debugger_active);
2101 if (!shared->is_compiled()) {
2102 shared->set_code(*current_code);
2103 continue;
2104 }
2105 }
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002106
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002107 // Keep function code in sync with shared function info.
2108 function->set_code(shared->code());
lrn@chromium.org34e60782011-09-15 07:25:40 +00002109 }
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002110
2111 RedirectActivationsToRecompiledCodeOnThread(isolate_,
2112 isolate_->thread_local_top());
2113
2114 ActiveFunctionsRedirector active_functions_redirector;
2115 isolate_->thread_manager()->IterateArchivedThreads(
2116 &active_functions_redirector);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002117 }
lrn@chromium.org34e60782011-09-15 07:25:40 +00002118}
2119
2120
jkummerow@chromium.org78502a92012-09-06 13:50:42 +00002121Object* Debug::FindSharedFunctionInfoInScript(Handle<Script> script,
2122 int position) {
2123 // Iterate the heap looking for SharedFunctionInfo generated from the
2124 // script. The inner most SharedFunctionInfo containing the source position
2125 // for the requested break point is found.
2126 // NOTE: This might require several heap iterations. If the SharedFunctionInfo
2127 // which is found is not compiled it is compiled and the heap is iterated
2128 // again as the compilation might create inner functions from the newly
2129 // compiled function and the actual requested break point might be in one of
2130 // these functions.
2131 // NOTE: The below fix-point iteration depends on all functions that cannot be
2132 // compiled lazily without a context to not be compiled at all. Compilation
2133 // will be triggered at points where we do not need a context.
2134 bool done = false;
2135 // The current candidate for the source position:
2136 int target_start_position = RelocInfo::kNoPosition;
2137 Handle<JSFunction> target_function;
2138 Handle<SharedFunctionInfo> target;
hpayer@chromium.org7c3372b2013-02-13 17:26:04 +00002139 Heap* heap = isolate_->heap();
jkummerow@chromium.org78502a92012-09-06 13:50:42 +00002140 while (!done) {
2141 { // Extra scope for iterator and no-allocation.
hpayer@chromium.org7c3372b2013-02-13 17:26:04 +00002142 heap->EnsureHeapIsIterable();
jkummerow@chromium.org78502a92012-09-06 13:50:42 +00002143 AssertNoAllocation no_alloc_during_heap_iteration;
hpayer@chromium.org7c3372b2013-02-13 17:26:04 +00002144 HeapIterator iterator(heap);
jkummerow@chromium.org78502a92012-09-06 13:50:42 +00002145 for (HeapObject* obj = iterator.next();
2146 obj != NULL; obj = iterator.next()) {
2147 bool found_next_candidate = false;
2148 Handle<JSFunction> function;
2149 Handle<SharedFunctionInfo> shared;
2150 if (obj->IsJSFunction()) {
2151 function = Handle<JSFunction>(JSFunction::cast(obj));
2152 shared = Handle<SharedFunctionInfo>(function->shared());
2153 ASSERT(shared->allows_lazy_compilation() || shared->is_compiled());
2154 found_next_candidate = true;
2155 } else if (obj->IsSharedFunctionInfo()) {
2156 shared = Handle<SharedFunctionInfo>(SharedFunctionInfo::cast(obj));
2157 // Skip functions that we cannot compile lazily without a context,
2158 // which is not available here, because there is no closure.
2159 found_next_candidate = shared->is_compiled() ||
2160 shared->allows_lazy_compilation_without_context();
2161 }
2162 if (!found_next_candidate) continue;
2163 if (shared->script() == *script) {
2164 // If the SharedFunctionInfo found has the requested script data and
2165 // contains the source position it is a candidate.
2166 int start_position = shared->function_token_position();
2167 if (start_position == RelocInfo::kNoPosition) {
2168 start_position = shared->start_position();
2169 }
2170 if (start_position <= position &&
2171 position <= shared->end_position()) {
2172 // If there is no candidate or this function is within the current
2173 // candidate this is the new candidate.
2174 if (target.is_null()) {
2175 target_start_position = start_position;
2176 target_function = function;
2177 target = shared;
2178 } else {
2179 if (target_start_position == start_position &&
2180 shared->end_position() == target->end_position()) {
2181 // If a top-level function contains only one function
2182 // declaration the source for the top-level and the function
2183 // is the same. In that case prefer the non top-level function.
2184 if (!shared->is_toplevel()) {
2185 target_start_position = start_position;
2186 target_function = function;
2187 target = shared;
2188 }
2189 } else if (target_start_position <= start_position &&
2190 shared->end_position() <= target->end_position()) {
2191 // This containment check includes equality as a function
2192 // inside a top-level function can share either start or end
2193 // position with the top-level function.
2194 target_start_position = start_position;
2195 target_function = function;
2196 target = shared;
2197 }
2198 }
2199 }
2200 }
2201 } // End for loop.
2202 } // End no-allocation scope.
2203
hpayer@chromium.org7c3372b2013-02-13 17:26:04 +00002204 if (target.is_null()) return heap->undefined_value();
jkummerow@chromium.org78502a92012-09-06 13:50:42 +00002205
2206 // There will be at least one break point when we are done.
2207 has_break_points_ = true;
2208
2209 // If the candidate found is compiled we are done.
2210 done = target->is_compiled();
2211 if (!done) {
2212 // If the candidate is not compiled, compile it to reveal any inner
2213 // functions which might contain the requested source position. This
2214 // will compile all inner functions that cannot be compiled without a
2215 // context, because Compiler::BuildFunctionInfo checks whether the
2216 // debugger is active.
2217 if (target_function.is_null()) {
2218 SharedFunctionInfo::CompileLazy(target, KEEP_EXCEPTION);
2219 } else {
2220 JSFunction::CompileLazy(target_function, KEEP_EXCEPTION);
2221 }
2222 }
2223 } // End while loop.
2224
2225 return *target;
2226}
2227
2228
lrn@chromium.org34e60782011-09-15 07:25:40 +00002229// Ensures the debug information is present for shared.
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00002230bool Debug::EnsureDebugInfo(Handle<SharedFunctionInfo> shared,
2231 Handle<JSFunction> function) {
lrn@chromium.org34e60782011-09-15 07:25:40 +00002232 // Return if we already have the debug info for shared.
2233 if (HasDebugInfo(shared)) {
2234 ASSERT(shared->is_compiled());
2235 return true;
2236 }
2237
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00002238 // There will be at least one break point when we are done.
2239 has_break_points_ = true;
2240
2241 // Ensure function is compiled. Return false if this failed.
2242 if (!function.is_null() &&
2243 !JSFunction::EnsureCompiled(function, CLEAR_EXCEPTION)) {
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002244 return false;
2245 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002246
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002247 // Create the debug info object.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002248 Handle<DebugInfo> debug_info = FACTORY->NewDebugInfo(shared);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002249
2250 // Add debug info to the list.
2251 DebugInfoListNode* node = new DebugInfoListNode(*debug_info);
2252 node->set_next(debug_info_list_);
2253 debug_info_list_ = node;
2254
kasper.lundbd3ec4e2008-07-09 11:06:54 +00002255 return true;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002256}
2257
2258
2259void Debug::RemoveDebugInfo(Handle<DebugInfo> debug_info) {
2260 ASSERT(debug_info_list_ != NULL);
2261 // Run through the debug info objects to find this one and remove it.
2262 DebugInfoListNode* prev = NULL;
2263 DebugInfoListNode* current = debug_info_list_;
2264 while (current != NULL) {
2265 if (*current->debug_info() == *debug_info) {
2266 // Unlink from list. If prev is NULL we are looking at the first element.
2267 if (prev == NULL) {
2268 debug_info_list_ = current->next();
2269 } else {
2270 prev->set_next(current->next());
2271 }
lrn@chromium.org7516f052011-03-30 08:52:27 +00002272 current->debug_info()->shared()->set_debug_info(
2273 isolate_->heap()->undefined_value());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002274 delete current;
2275
2276 // If there are no more debug info objects there are not more break
2277 // points.
2278 has_break_points_ = debug_info_list_ != NULL;
2279
2280 return;
2281 }
2282 // Move to next in list.
2283 prev = current;
2284 current = current->next();
2285 }
2286 UNREACHABLE();
2287}
2288
2289
2290void Debug::SetAfterBreakTarget(JavaScriptFrame* frame) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002291 HandleScope scope(isolate_);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002292
lrn@chromium.org34e60782011-09-15 07:25:40 +00002293 PrepareForBreakPoints();
2294
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002295 // Get the executing function in which the debug break occurred.
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00002296 Handle<JSFunction> function(JSFunction::cast(frame->function()));
2297 Handle<SharedFunctionInfo> shared(function->shared());
2298 if (!EnsureDebugInfo(shared, function)) {
kasper.lundbd3ec4e2008-07-09 11:06:54 +00002299 // Return if we failed to retrieve the debug info.
2300 return;
2301 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002302 Handle<DebugInfo> debug_info = GetDebugInfo(shared);
2303 Handle<Code> code(debug_info->code());
2304 Handle<Code> original_code(debug_info->original_code());
2305#ifdef DEBUG
2306 // Get the code which is actually executing.
vegorov@chromium.org74f333b2011-04-06 11:17:46 +00002307 Handle<Code> frame_code(frame->LookupCode());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002308 ASSERT(frame_code.is_identical_to(code));
2309#endif
2310
2311 // Find the call address in the running code. This address holds the call to
2312 // either a DebugBreakXXX or to the debug break return entry code if the
2313 // break point is still active after processing the break point.
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00002314 Address addr = frame->pc() - Assembler::kPatchDebugBreakSlotReturnOffset;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002315
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002316 // Check if the location is at JS exit or debug break slot.
ager@chromium.orga1645e22009-09-09 19:27:10 +00002317 bool at_js_return = false;
2318 bool break_at_js_return_active = false;
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002319 bool at_debug_break_slot = false;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002320 RelocIterator it(debug_info->code());
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002321 while (!it.done() && !at_js_return && !at_debug_break_slot) {
ager@chromium.org236ad962008-09-25 09:45:57 +00002322 if (RelocInfo::IsJSReturn(it.rinfo()->rmode())) {
ager@chromium.orga1645e22009-09-09 19:27:10 +00002323 at_js_return = (it.rinfo()->pc() ==
2324 addr - Assembler::kPatchReturnSequenceAddressOffset);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002325 break_at_js_return_active = it.rinfo()->IsPatchedReturnSequence();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002326 }
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002327 if (RelocInfo::IsDebugBreakSlot(it.rinfo()->rmode())) {
2328 at_debug_break_slot = (it.rinfo()->pc() ==
2329 addr - Assembler::kPatchDebugBreakSlotAddressOffset);
2330 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002331 it.next();
2332 }
2333
2334 // Handle the jump to continue execution after break point depending on the
2335 // break location.
ager@chromium.orga1645e22009-09-09 19:27:10 +00002336 if (at_js_return) {
2337 // If the break point as return is still active jump to the corresponding
2338 // place in the original code. If not the break point was removed during
2339 // break point processing.
2340 if (break_at_js_return_active) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002341 addr += original_code->instruction_start() - code->instruction_start();
2342 }
2343
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00002344 // Move back to where the call instruction sequence started.
2345 thread_local_.after_break_target_ =
2346 addr - Assembler::kPatchReturnSequenceAddressOffset;
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002347 } else if (at_debug_break_slot) {
2348 // Address of where the debug break slot starts.
2349 addr = addr - Assembler::kPatchDebugBreakSlotAddressOffset;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002350
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002351 // Continue just after the slot.
2352 thread_local_.after_break_target_ = addr + Assembler::kDebugBreakSlotLength;
2353 } else if (IsDebugBreak(Assembler::target_address_at(addr))) {
2354 // We now know that there is still a debug break call at the target address,
2355 // so the break point is still there and the original code will hold the
2356 // address to jump to in order to complete the call which is replaced by a
2357 // call to DebugBreakXXX.
2358
2359 // Find the corresponding address in the original code.
2360 addr += original_code->instruction_start() - code->instruction_start();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002361
2362 // Install jump to the call address in the original code. This will be the
2363 // call which was overwritten by the call to DebugBreakXXX.
2364 thread_local_.after_break_target_ = Assembler::target_address_at(addr);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002365 } else {
2366 // There is no longer a break point present. Don't try to look in the
2367 // original code as the running code will have the right address. This takes
2368 // care of the case where the last break point is removed from the function
2369 // and therefore no "original code" is available.
2370 thread_local_.after_break_target_ = Assembler::target_address_at(addr);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002371 }
2372}
2373
2374
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00002375bool Debug::IsBreakAtReturn(JavaScriptFrame* frame) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002376 HandleScope scope(isolate_);
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00002377
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00002378 // If there are no break points this cannot be break at return, as
2379 // the debugger statement and stack guard bebug break cannot be at
2380 // return.
2381 if (!has_break_points_) {
2382 return false;
2383 }
2384
lrn@chromium.org34e60782011-09-15 07:25:40 +00002385 PrepareForBreakPoints();
2386
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00002387 // Get the executing function in which the debug break occurred.
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00002388 Handle<JSFunction> function(JSFunction::cast(frame->function()));
2389 Handle<SharedFunctionInfo> shared(function->shared());
2390 if (!EnsureDebugInfo(shared, function)) {
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00002391 // Return if we failed to retrieve the debug info.
2392 return false;
2393 }
2394 Handle<DebugInfo> debug_info = GetDebugInfo(shared);
2395 Handle<Code> code(debug_info->code());
2396#ifdef DEBUG
2397 // Get the code which is actually executing.
vegorov@chromium.org74f333b2011-04-06 11:17:46 +00002398 Handle<Code> frame_code(frame->LookupCode());
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00002399 ASSERT(frame_code.is_identical_to(code));
2400#endif
2401
2402 // Find the call address in the running code.
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00002403 Address addr = frame->pc() - Assembler::kPatchDebugBreakSlotReturnOffset;
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00002404
2405 // Check if the location is at JS return.
2406 RelocIterator it(debug_info->code());
2407 while (!it.done()) {
2408 if (RelocInfo::IsJSReturn(it.rinfo()->rmode())) {
2409 return (it.rinfo()->pc() ==
2410 addr - Assembler::kPatchReturnSequenceAddressOffset);
2411 }
2412 it.next();
2413 }
2414 return false;
2415}
2416
2417
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00002418void Debug::FramesHaveBeenDropped(StackFrame::Id new_break_frame_id,
whesse@chromium.orge90029b2010-08-02 11:52:17 +00002419 FrameDropMode mode,
2420 Object** restarter_frame_function_pointer) {
ulan@chromium.orgd9e468a2012-06-25 09:47:40 +00002421 if (mode != CURRENTLY_SET_MODE) {
2422 thread_local_.frame_drop_mode_ = mode;
2423 }
ager@chromium.org357bf652010-04-12 11:30:10 +00002424 thread_local_.break_frame_id_ = new_break_frame_id;
whesse@chromium.orge90029b2010-08-02 11:52:17 +00002425 thread_local_.restarter_frame_function_pointer_ =
2426 restarter_frame_function_pointer;
ager@chromium.org357bf652010-04-12 11:30:10 +00002427}
2428
2429
jkummerow@chromium.org212d9642012-05-11 15:02:09 +00002430const int Debug::FramePaddingLayout::kInitialSize = 1;
2431
2432
2433// Any even value bigger than kInitialSize as needed for stack scanning.
2434const int Debug::FramePaddingLayout::kPaddingValue = kInitialSize + 1;
2435
2436
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002437bool Debug::IsDebugGlobal(GlobalObject* global) {
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00002438 return IsLoaded() && global == debug_context()->global_object();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002439}
2440
2441
ager@chromium.org32912102009-01-16 10:38:43 +00002442void Debug::ClearMirrorCache() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002443 PostponeInterruptsScope postpone(isolate_);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002444 HandleScope scope(isolate_);
2445 ASSERT(isolate_->context() == *Debug::debug_context());
ager@chromium.org32912102009-01-16 10:38:43 +00002446
2447 // Clear the mirror cache.
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00002448 Handle<String> function_name = isolate_->factory()->InternalizeOneByteString(
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00002449 STATIC_ASCII_VECTOR("ClearMirrorCache"));
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00002450 Handle<Object> fun(
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00002451 isolate_->global_object()->GetPropertyNoExceptionThrown(*function_name),
2452 isolate_);
ager@chromium.org32912102009-01-16 10:38:43 +00002453 ASSERT(fun->IsJSFunction());
2454 bool caught_exception;
rossberg@chromium.org717967f2011-07-20 13:44:42 +00002455 Execution::TryCall(Handle<JSFunction>::cast(fun),
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00002456 Handle<JSObject>(Debug::debug_context()->global_object()),
ager@chromium.org32912102009-01-16 10:38:43 +00002457 0, NULL, &caught_exception);
2458}
2459
2460
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002461void Debug::CreateScriptCache() {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002462 Heap* heap = isolate_->heap();
2463 HandleScope scope(isolate_);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002464
2465 // Perform two GCs to get rid of all unreferenced scripts. The first GC gets
2466 // rid of all the cached script wrappers and the second gets rid of the
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002467 // scripts which are no longer referenced. The second also sweeps precisely,
2468 // which saves us doing yet another GC to make the heap iterable.
rossberg@chromium.org994edf62012-02-06 10:12:55 +00002469 heap->CollectAllGarbage(Heap::kNoGCFlags, "Debug::CreateScriptCache");
2470 heap->CollectAllGarbage(Heap::kMakeHeapIterableMask,
2471 "Debug::CreateScriptCache");
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002472
2473 ASSERT(script_cache_ == NULL);
2474 script_cache_ = new ScriptCache();
2475
2476 // Scan heap for Script objects.
2477 int count = 0;
hpayer@chromium.org7c3372b2013-02-13 17:26:04 +00002478 HeapIterator iterator(heap);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002479 AssertNoAllocation no_allocation;
2480
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002481 for (HeapObject* obj = iterator.next(); obj != NULL; obj = iterator.next()) {
sgjesse@chromium.org152a0b02009-10-07 13:50:16 +00002482 if (obj->IsScript() && Script::cast(obj)->HasValidSource()) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002483 script_cache_->Add(Handle<Script>(Script::cast(obj)));
2484 count++;
2485 }
2486 }
2487}
2488
2489
2490void Debug::DestroyScriptCache() {
2491 // Get rid of the script cache if it was created.
2492 if (script_cache_ != NULL) {
2493 delete script_cache_;
2494 script_cache_ = NULL;
2495 }
2496}
2497
2498
2499void Debug::AddScriptToScriptCache(Handle<Script> script) {
2500 if (script_cache_ != NULL) {
2501 script_cache_->Add(script);
2502 }
2503}
2504
2505
2506Handle<FixedArray> Debug::GetLoadedScripts() {
2507 // Create and fill the script cache when the loaded scripts is requested for
2508 // the first time.
2509 if (script_cache_ == NULL) {
2510 CreateScriptCache();
2511 }
2512
2513 // If the script cache is not active just return an empty array.
2514 ASSERT(script_cache_ != NULL);
2515 if (script_cache_ == NULL) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002516 isolate_->factory()->NewFixedArray(0);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002517 }
2518
2519 // Perform GC to get unreferenced scripts evicted from the cache before
2520 // returning the content.
rossberg@chromium.org994edf62012-02-06 10:12:55 +00002521 isolate_->heap()->CollectAllGarbage(Heap::kNoGCFlags,
2522 "Debug::GetLoadedScripts");
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002523
2524 // Get the scripts from the cache.
2525 return script_cache_->GetScripts();
2526}
2527
2528
2529void Debug::AfterGarbageCollection() {
2530 // Generate events for collected scripts.
2531 if (script_cache_ != NULL) {
2532 script_cache_->ProcessCollectedScripts();
2533 }
2534}
2535
2536
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00002537Debugger::Debugger(Isolate* isolate)
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +00002538 : debugger_access_(isolate->debugger_access()),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002539 event_listener_(Handle<Object>()),
2540 event_listener_data_(Handle<Object>()),
2541 compiling_natives_(false),
2542 is_loading_debugger_(false),
mstarzinger@chromium.orgde886792012-09-11 13:22:37 +00002543 live_edit_enabled_(true),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002544 never_unload_debugger_(false),
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002545 force_debugger_active_(false),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002546 message_handler_(NULL),
2547 debugger_unload_pending_(false),
2548 host_dispatch_handler_(NULL),
2549 dispatch_handler_access_(OS::CreateMutex()),
2550 debug_message_dispatch_handler_(NULL),
2551 message_dispatch_helper_thread_(NULL),
2552 host_dispatch_micros_(100 * 1000),
2553 agent_(NULL),
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00002554 command_queue_(isolate->logger(), kQueueInitialSize),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002555 command_received_(OS::CreateSemaphore(0)),
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00002556 event_command_queue_(isolate->logger(), kQueueInitialSize),
2557 isolate_(isolate) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002558}
2559
2560
2561Debugger::~Debugger() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002562 delete dispatch_handler_access_;
2563 dispatch_handler_access_ = 0;
2564 delete command_received_;
2565 command_received_ = 0;
2566}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002567
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002568
2569Handle<Object> Debugger::MakeJSObject(Vector<const char> constructor_name,
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002570 int argc,
2571 Handle<Object> argv[],
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002572 bool* caught_exception) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002573 ASSERT(isolate_->context() == *isolate_->debug()->debug_context());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002574
2575 // Create the execution state object.
lrn@chromium.org7516f052011-03-30 08:52:27 +00002576 Handle<String> constructor_str =
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00002577 isolate_->factory()->InternalizeUtf8String(constructor_name);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002578 Handle<Object> constructor(
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00002579 isolate_->global_object()->GetPropertyNoExceptionThrown(*constructor_str),
2580 isolate_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002581 ASSERT(constructor->IsJSFunction());
2582 if (!constructor->IsJSFunction()) {
2583 *caught_exception = true;
lrn@chromium.org7516f052011-03-30 08:52:27 +00002584 return isolate_->factory()->undefined_value();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002585 }
2586 Handle<Object> js_object = Execution::TryCall(
2587 Handle<JSFunction>::cast(constructor),
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00002588 Handle<JSObject>(isolate_->debug()->debug_context()->global_object()),
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002589 argc,
2590 argv,
2591 caught_exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002592 return js_object;
2593}
2594
2595
2596Handle<Object> Debugger::MakeExecutionState(bool* caught_exception) {
2597 // Create the execution state object.
lrn@chromium.org7516f052011-03-30 08:52:27 +00002598 Handle<Object> break_id = isolate_->factory()->NewNumberFromInt(
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002599 isolate_->debug()->break_id());
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002600 Handle<Object> argv[] = { break_id };
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002601 return MakeJSObject(CStrVector("MakeExecutionState"),
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002602 ARRAY_SIZE(argv),
2603 argv,
2604 caught_exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002605}
2606
2607
2608Handle<Object> Debugger::MakeBreakEvent(Handle<Object> exec_state,
2609 Handle<Object> break_points_hit,
2610 bool* caught_exception) {
2611 // Create the new break event object.
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002612 Handle<Object> argv[] = { exec_state, break_points_hit };
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002613 return MakeJSObject(CStrVector("MakeBreakEvent"),
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002614 ARRAY_SIZE(argv),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002615 argv,
2616 caught_exception);
2617}
2618
2619
2620Handle<Object> Debugger::MakeExceptionEvent(Handle<Object> exec_state,
2621 Handle<Object> exception,
2622 bool uncaught,
2623 bool* caught_exception) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002624 Factory* factory = isolate_->factory();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002625 // Create the new exception event object.
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002626 Handle<Object> argv[] = { exec_state,
2627 exception,
2628 factory->ToBoolean(uncaught) };
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002629 return MakeJSObject(CStrVector("MakeExceptionEvent"),
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002630 ARRAY_SIZE(argv),
2631 argv,
2632 caught_exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002633}
2634
2635
2636Handle<Object> Debugger::MakeNewFunctionEvent(Handle<Object> function,
2637 bool* caught_exception) {
2638 // Create the new function event object.
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002639 Handle<Object> argv[] = { function };
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002640 return MakeJSObject(CStrVector("MakeNewFunctionEvent"),
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002641 ARRAY_SIZE(argv),
2642 argv,
2643 caught_exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002644}
2645
2646
2647Handle<Object> Debugger::MakeCompileEvent(Handle<Script> script,
iposva@chromium.org245aa852009-02-10 00:49:54 +00002648 bool before,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002649 bool* caught_exception) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002650 Factory* factory = isolate_->factory();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002651 // Create the compile event object.
2652 Handle<Object> exec_state = MakeExecutionState(caught_exception);
iposva@chromium.org245aa852009-02-10 00:49:54 +00002653 Handle<Object> script_wrapper = GetScriptWrapper(script);
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002654 Handle<Object> argv[] = { exec_state,
2655 script_wrapper,
2656 factory->ToBoolean(before) };
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002657 return MakeJSObject(CStrVector("MakeCompileEvent"),
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002658 ARRAY_SIZE(argv),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002659 argv,
2660 caught_exception);
2661}
2662
2663
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002664Handle<Object> Debugger::MakeScriptCollectedEvent(int id,
2665 bool* caught_exception) {
2666 // Create the script collected event object.
2667 Handle<Object> exec_state = MakeExecutionState(caught_exception);
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00002668 Handle<Object> id_object = Handle<Smi>(Smi::FromInt(id), isolate_);
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002669 Handle<Object> argv[] = { exec_state, id_object };
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002670
2671 return MakeJSObject(CStrVector("MakeScriptCollectedEvent"),
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002672 ARRAY_SIZE(argv),
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002673 argv,
2674 caught_exception);
2675}
2676
2677
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002678void Debugger::OnException(Handle<Object> exception, bool uncaught) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002679 HandleScope scope(isolate_);
2680 Debug* debug = isolate_->debug();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002681
2682 // Bail out based on state or if there is no listener for this event
lrn@chromium.org7516f052011-03-30 08:52:27 +00002683 if (debug->InDebugger()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002684 if (!Debugger::EventActive(v8::Exception)) return;
2685
2686 // Bail out if exception breaks are not active
2687 if (uncaught) {
2688 // Uncaught exceptions are reported by either flags.
lrn@chromium.org7516f052011-03-30 08:52:27 +00002689 if (!(debug->break_on_uncaught_exception() ||
2690 debug->break_on_exception())) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002691 } else {
2692 // Caught exceptions are reported is activated.
lrn@chromium.org7516f052011-03-30 08:52:27 +00002693 if (!debug->break_on_exception()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002694 }
2695
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002696 // Enter the debugger.
2697 EnterDebugger debugger;
2698 if (debugger.FailedToEnter()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002699
2700 // Clear all current stepping setup.
lrn@chromium.org7516f052011-03-30 08:52:27 +00002701 debug->ClearStepping();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002702 // Create the event data object.
2703 bool caught_exception = false;
2704 Handle<Object> exec_state = MakeExecutionState(&caught_exception);
2705 Handle<Object> event_data;
2706 if (!caught_exception) {
2707 event_data = MakeExceptionEvent(exec_state, exception, uncaught,
2708 &caught_exception);
2709 }
2710 // Bail out and don't call debugger if exception.
2711 if (caught_exception) {
2712 return;
2713 }
2714
ager@chromium.org5ec48922009-05-05 07:25:34 +00002715 // Process debug event.
2716 ProcessDebugEvent(v8::Exception, Handle<JSObject>::cast(event_data), false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002717 // Return to continue execution from where the exception was thrown.
2718}
2719
2720
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002721void Debugger::OnDebugBreak(Handle<Object> break_points_hit,
2722 bool auto_continue) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002723 HandleScope scope(isolate_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002724
kasper.lund212ac232008-07-16 07:07:30 +00002725 // Debugger has already been entered by caller.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002726 ASSERT(isolate_->context() == *isolate_->debug()->debug_context());
kasper.lund212ac232008-07-16 07:07:30 +00002727
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002728 // Bail out if there is no listener for this event
2729 if (!Debugger::EventActive(v8::Break)) return;
2730
2731 // Debugger must be entered in advance.
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00002732 ASSERT(isolate_->context() == *isolate_->debug()->debug_context());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002733
2734 // Create the event data object.
2735 bool caught_exception = false;
2736 Handle<Object> exec_state = MakeExecutionState(&caught_exception);
2737 Handle<Object> event_data;
2738 if (!caught_exception) {
2739 event_data = MakeBreakEvent(exec_state, break_points_hit,
2740 &caught_exception);
2741 }
2742 // Bail out and don't call debugger if exception.
2743 if (caught_exception) {
2744 return;
2745 }
2746
ager@chromium.org5ec48922009-05-05 07:25:34 +00002747 // Process debug event.
2748 ProcessDebugEvent(v8::Break,
2749 Handle<JSObject>::cast(event_data),
2750 auto_continue);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002751}
2752
2753
2754void Debugger::OnBeforeCompile(Handle<Script> script) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002755 HandleScope scope(isolate_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002756
2757 // Bail out based on state or if there is no listener for this event
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002758 if (isolate_->debug()->InDebugger()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002759 if (compiling_natives()) return;
2760 if (!EventActive(v8::BeforeCompile)) return;
2761
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002762 // Enter the debugger.
2763 EnterDebugger debugger;
2764 if (debugger.FailedToEnter()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002765
2766 // Create the event data object.
2767 bool caught_exception = false;
iposva@chromium.org245aa852009-02-10 00:49:54 +00002768 Handle<Object> event_data = MakeCompileEvent(script, true, &caught_exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002769 // Bail out and don't call debugger if exception.
2770 if (caught_exception) {
2771 return;
2772 }
2773
ager@chromium.org5ec48922009-05-05 07:25:34 +00002774 // Process debug event.
2775 ProcessDebugEvent(v8::BeforeCompile,
2776 Handle<JSObject>::cast(event_data),
2777 true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002778}
2779
2780
2781// Handle debugger actions when a new script is compiled.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00002782void Debugger::OnAfterCompile(Handle<Script> script,
2783 AfterCompileFlags after_compile_flags) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002784 HandleScope scope(isolate_);
2785 Debug* debug = isolate_->debug();
kasper.lund212ac232008-07-16 07:07:30 +00002786
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002787 // Add the newly compiled script to the script cache.
lrn@chromium.org7516f052011-03-30 08:52:27 +00002788 debug->AddScriptToScriptCache(script);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002789
2790 // No more to do if not debugging.
ager@chromium.org71daaf62009-04-01 07:22:49 +00002791 if (!IsDebuggerActive()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002792
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002793 // No compile events while compiling natives.
2794 if (compiling_natives()) return;
2795
iposva@chromium.org245aa852009-02-10 00:49:54 +00002796 // Store whether in debugger before entering debugger.
lrn@chromium.org7516f052011-03-30 08:52:27 +00002797 bool in_debugger = debug->InDebugger();
iposva@chromium.org245aa852009-02-10 00:49:54 +00002798
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002799 // Enter the debugger.
2800 EnterDebugger debugger;
2801 if (debugger.FailedToEnter()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002802
2803 // If debugging there might be script break points registered for this
2804 // script. Make sure that these break points are set.
2805
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002806 // Get the function UpdateScriptBreakPoints (defined in debug-debugger.js).
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00002807 Handle<String> update_script_break_points_string =
2808 isolate_->factory()->InternalizeOneByteString(
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00002809 STATIC_ASCII_VECTOR("UpdateScriptBreakPoints"));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002810 Handle<Object> update_script_break_points =
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00002811 Handle<Object>(
2812 debug->debug_context()->global_object()->GetPropertyNoExceptionThrown(
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00002813 *update_script_break_points_string),
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00002814 isolate_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002815 if (!update_script_break_points->IsJSFunction()) {
2816 return;
2817 }
2818 ASSERT(update_script_break_points->IsJSFunction());
2819
2820 // Wrap the script object in a proper JS object before passing it
2821 // to JavaScript.
2822 Handle<JSValue> wrapper = GetScriptWrapper(script);
2823
2824 // Call UpdateScriptBreakPoints expect no exceptions.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002825 bool caught_exception;
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002826 Handle<Object> argv[] = { wrapper };
rossberg@chromium.org717967f2011-07-20 13:44:42 +00002827 Execution::TryCall(Handle<JSFunction>::cast(update_script_break_points),
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002828 Isolate::Current()->js_builtins_object(),
2829 ARRAY_SIZE(argv),
2830 argv,
2831 &caught_exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002832 if (caught_exception) {
2833 return;
2834 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002835 // Bail out based on state or if there is no listener for this event
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00002836 if (in_debugger && (after_compile_flags & SEND_WHEN_DEBUGGING) == 0) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002837 if (!Debugger::EventActive(v8::AfterCompile)) return;
2838
2839 // Create the compile state object.
2840 Handle<Object> event_data = MakeCompileEvent(script,
iposva@chromium.org245aa852009-02-10 00:49:54 +00002841 false,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002842 &caught_exception);
2843 // Bail out and don't call debugger if exception.
2844 if (caught_exception) {
2845 return;
2846 }
ager@chromium.org5ec48922009-05-05 07:25:34 +00002847 // Process debug event.
2848 ProcessDebugEvent(v8::AfterCompile,
2849 Handle<JSObject>::cast(event_data),
2850 true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002851}
2852
2853
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002854void Debugger::OnScriptCollected(int id) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002855 HandleScope scope(isolate_);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002856
2857 // No more to do if not debugging.
ulan@chromium.org56c14af2012-09-20 12:51:09 +00002858 if (isolate_->debug()->InDebugger()) return;
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002859 if (!IsDebuggerActive()) return;
2860 if (!Debugger::EventActive(v8::ScriptCollected)) return;
2861
2862 // Enter the debugger.
2863 EnterDebugger debugger;
2864 if (debugger.FailedToEnter()) return;
2865
2866 // Create the script collected state object.
2867 bool caught_exception = false;
2868 Handle<Object> event_data = MakeScriptCollectedEvent(id,
2869 &caught_exception);
2870 // Bail out and don't call debugger if exception.
2871 if (caught_exception) {
2872 return;
2873 }
2874
2875 // Process debug event.
2876 ProcessDebugEvent(v8::ScriptCollected,
2877 Handle<JSObject>::cast(event_data),
2878 true);
2879}
2880
2881
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002882void Debugger::ProcessDebugEvent(v8::DebugEvent event,
ager@chromium.org5ec48922009-05-05 07:25:34 +00002883 Handle<JSObject> event_data,
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002884 bool auto_continue) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002885 HandleScope scope(isolate_);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002886
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +00002887 // Clear any pending debug break if this is a real break.
2888 if (!auto_continue) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002889 isolate_->debug()->clear_interrupt_pending(DEBUGBREAK);
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +00002890 }
2891
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002892 // Create the execution state.
2893 bool caught_exception = false;
2894 Handle<Object> exec_state = MakeExecutionState(&caught_exception);
2895 if (caught_exception) {
2896 return;
2897 }
ager@chromium.org41826e72009-03-30 13:30:57 +00002898 // First notify the message handler if any.
2899 if (message_handler_ != NULL) {
ager@chromium.org5ec48922009-05-05 07:25:34 +00002900 NotifyMessageHandler(event,
2901 Handle<JSObject>::cast(exec_state),
2902 event_data,
2903 auto_continue);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002904 }
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00002905 // Notify registered debug event listener. This can be either a C or
2906 // a JavaScript function. Don't call event listener for v8::Break
2907 // here, if it's only a debug command -- they will be processed later.
2908 if ((event != v8::Break || !auto_continue) && !event_listener_.is_null()) {
2909 CallEventCallback(event, exec_state, event_data, NULL);
2910 }
2911 // Process pending debug commands.
2912 if (event == v8::Break) {
2913 while (!event_command_queue_.IsEmpty()) {
2914 CommandMessage command = event_command_queue_.Get();
2915 if (!event_listener_.is_null()) {
2916 CallEventCallback(v8::BreakForCommand,
2917 exec_state,
2918 event_data,
2919 command.client_data());
2920 }
2921 command.Dispose();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002922 }
2923 }
2924}
2925
2926
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00002927void Debugger::CallEventCallback(v8::DebugEvent event,
2928 Handle<Object> exec_state,
2929 Handle<Object> event_data,
2930 v8::Debug::ClientData* client_data) {
ager@chromium.orgea91cc52011-05-23 06:06:11 +00002931 if (event_listener_->IsForeign()) {
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00002932 CallCEventCallback(event, exec_state, event_data, client_data);
2933 } else {
2934 CallJSEventCallback(event, exec_state, event_data);
2935 }
2936}
2937
2938
2939void Debugger::CallCEventCallback(v8::DebugEvent event,
2940 Handle<Object> exec_state,
2941 Handle<Object> event_data,
2942 v8::Debug::ClientData* client_data) {
ager@chromium.orgea91cc52011-05-23 06:06:11 +00002943 Handle<Foreign> callback_obj(Handle<Foreign>::cast(event_listener_));
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00002944 v8::Debug::EventCallback2 callback =
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002945 FUNCTION_CAST<v8::Debug::EventCallback2>(
2946 callback_obj->foreign_address());
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00002947 EventDetailsImpl event_details(
2948 event,
2949 Handle<JSObject>::cast(exec_state),
2950 Handle<JSObject>::cast(event_data),
2951 event_listener_data_,
2952 client_data);
2953 callback(event_details);
2954}
2955
2956
2957void Debugger::CallJSEventCallback(v8::DebugEvent event,
2958 Handle<Object> exec_state,
2959 Handle<Object> event_data) {
2960 ASSERT(event_listener_->IsJSFunction());
2961 Handle<JSFunction> fun(Handle<JSFunction>::cast(event_listener_));
2962
2963 // Invoke the JavaScript debug event listener.
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00002964 Handle<Object> argv[] = { Handle<Object>(Smi::FromInt(event), isolate_),
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002965 exec_state,
2966 event_data,
2967 event_listener_data_ };
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002968 bool caught_exception;
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002969 Execution::TryCall(fun,
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00002970 isolate_->global_object(),
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002971 ARRAY_SIZE(argv),
2972 argv,
2973 &caught_exception);
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00002974 // Silently ignore exceptions from debug event listeners.
2975}
2976
2977
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00002978Handle<Context> Debugger::GetDebugContext() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002979 never_unload_debugger_ = true;
2980 EnterDebugger debugger;
2981 return isolate_->debug()->debug_context();
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00002982}
2983
2984
ager@chromium.org71daaf62009-04-01 07:22:49 +00002985void Debugger::UnloadDebugger() {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002986 Debug* debug = isolate_->debug();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002987
ager@chromium.org71daaf62009-04-01 07:22:49 +00002988 // Make sure that there are no breakpoints left.
lrn@chromium.org7516f052011-03-30 08:52:27 +00002989 debug->ClearAllBreakPoints();
ager@chromium.org71daaf62009-04-01 07:22:49 +00002990
2991 // Unload the debugger if feasible.
2992 if (!never_unload_debugger_) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002993 debug->Unload();
ager@chromium.org71daaf62009-04-01 07:22:49 +00002994 }
2995
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002996 // Clear the flag indicating that the debugger should be unloaded.
2997 debugger_unload_pending_ = false;
ager@chromium.org71daaf62009-04-01 07:22:49 +00002998}
2999
3000
ager@chromium.org41826e72009-03-30 13:30:57 +00003001void Debugger::NotifyMessageHandler(v8::DebugEvent event,
ager@chromium.org5ec48922009-05-05 07:25:34 +00003002 Handle<JSObject> exec_state,
3003 Handle<JSObject> event_data,
ager@chromium.org41826e72009-03-30 13:30:57 +00003004 bool auto_continue) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00003005 HandleScope scope(isolate_);
ager@chromium.org41826e72009-03-30 13:30:57 +00003006
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003007 if (!isolate_->debug()->Load()) return;
ager@chromium.org41826e72009-03-30 13:30:57 +00003008
3009 // Process the individual events.
ager@chromium.org5ec48922009-05-05 07:25:34 +00003010 bool sendEventMessage = false;
ager@chromium.org41826e72009-03-30 13:30:57 +00003011 switch (event) {
3012 case v8::Break:
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00003013 case v8::BreakForCommand:
ager@chromium.org5ec48922009-05-05 07:25:34 +00003014 sendEventMessage = !auto_continue;
ager@chromium.org41826e72009-03-30 13:30:57 +00003015 break;
3016 case v8::Exception:
ager@chromium.org5ec48922009-05-05 07:25:34 +00003017 sendEventMessage = true;
ager@chromium.org41826e72009-03-30 13:30:57 +00003018 break;
3019 case v8::BeforeCompile:
3020 break;
3021 case v8::AfterCompile:
ager@chromium.org5ec48922009-05-05 07:25:34 +00003022 sendEventMessage = true;
ager@chromium.org41826e72009-03-30 13:30:57 +00003023 break;
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003024 case v8::ScriptCollected:
3025 sendEventMessage = true;
3026 break;
ager@chromium.org41826e72009-03-30 13:30:57 +00003027 case v8::NewFunction:
3028 break;
3029 default:
3030 UNREACHABLE();
3031 }
3032
ager@chromium.org5ec48922009-05-05 07:25:34 +00003033 // The debug command interrupt flag might have been set when the command was
3034 // added. It should be enough to clear the flag only once while we are in the
3035 // debugger.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003036 ASSERT(isolate_->debug()->InDebugger());
3037 isolate_->stack_guard()->Continue(DEBUGCOMMAND);
ager@chromium.org5ec48922009-05-05 07:25:34 +00003038
3039 // Notify the debugger that a debug event has occurred unless auto continue is
3040 // active in which case no event is send.
3041 if (sendEventMessage) {
3042 MessageImpl message = MessageImpl::NewEvent(
3043 event,
3044 auto_continue,
3045 Handle<JSObject>::cast(exec_state),
3046 Handle<JSObject>::cast(event_data));
3047 InvokeMessageHandler(message);
3048 }
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +00003049
3050 // If auto continue don't make the event cause a break, but process messages
3051 // in the queue if any. For script collected events don't even process
3052 // messages in the queue as the execution state might not be what is expected
3053 // by the client.
ager@chromium.org6ffc2172009-05-29 19:20:16 +00003054 if ((auto_continue && !HasCommands()) || event == v8::ScriptCollected) {
ager@chromium.org5ec48922009-05-05 07:25:34 +00003055 return;
3056 }
ager@chromium.org41826e72009-03-30 13:30:57 +00003057
ager@chromium.org41826e72009-03-30 13:30:57 +00003058 v8::TryCatch try_catch;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003059
3060 // DebugCommandProcessor goes here.
3061 v8::Local<v8::Object> cmd_processor;
3062 {
3063 v8::Local<v8::Object> api_exec_state =
3064 v8::Utils::ToLocal(Handle<JSObject>::cast(exec_state));
3065 v8::Local<v8::String> fun_name =
3066 v8::String::New("debugCommandProcessor");
3067 v8::Local<v8::Function> fun =
3068 v8::Function::Cast(*api_exec_state->Get(fun_name));
3069
3070 v8::Handle<v8::Boolean> running =
3071 auto_continue ? v8::True() : v8::False();
3072 static const int kArgc = 1;
3073 v8::Handle<Value> argv[kArgc] = { running };
3074 cmd_processor = v8::Object::Cast(*fun->Call(api_exec_state, kArgc, argv));
3075 if (try_catch.HasCaught()) {
3076 PrintLn(try_catch.Exception());
3077 return;
3078 }
ager@chromium.org41826e72009-03-30 13:30:57 +00003079 }
3080
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003081 bool running = auto_continue;
3082
ager@chromium.org41826e72009-03-30 13:30:57 +00003083 // Process requests from the debugger.
3084 while (true) {
3085 // Wait for new command in the queue.
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003086 if (Debugger::host_dispatch_handler_) {
3087 // In case there is a host dispatch - do periodic dispatches.
3088 if (!command_received_->Wait(host_dispatch_micros_)) {
3089 // Timout expired, do the dispatch.
3090 Debugger::host_dispatch_handler_();
3091 continue;
3092 }
3093 } else {
3094 // In case there is no host dispatch - just wait.
3095 command_received_->Wait();
3096 }
ager@chromium.org41826e72009-03-30 13:30:57 +00003097
ager@chromium.org41826e72009-03-30 13:30:57 +00003098 // Get the command from the queue.
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003099 CommandMessage command = command_queue_.Get();
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00003100 isolate_->logger()->DebugTag(
3101 "Got request from command queue, in interactive loop.");
ager@chromium.org71daaf62009-04-01 07:22:49 +00003102 if (!Debugger::IsDebuggerActive()) {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003103 // Delete command text and user data.
3104 command.Dispose();
ager@chromium.org41826e72009-03-30 13:30:57 +00003105 return;
3106 }
3107
ager@chromium.org41826e72009-03-30 13:30:57 +00003108 // Invoke JavaScript to process the debug request.
3109 v8::Local<v8::String> fun_name;
3110 v8::Local<v8::Function> fun;
3111 v8::Local<v8::Value> request;
3112 v8::TryCatch try_catch;
3113 fun_name = v8::String::New("processDebugRequest");
3114 fun = v8::Function::Cast(*cmd_processor->Get(fun_name));
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003115
3116 request = v8::String::New(command.text().start(),
3117 command.text().length());
ager@chromium.org41826e72009-03-30 13:30:57 +00003118 static const int kArgc = 1;
3119 v8::Handle<Value> argv[kArgc] = { request };
3120 v8::Local<v8::Value> response_val = fun->Call(cmd_processor, kArgc, argv);
3121
3122 // Get the response.
3123 v8::Local<v8::String> response;
ager@chromium.org41826e72009-03-30 13:30:57 +00003124 if (!try_catch.HasCaught()) {
3125 // Get response string.
3126 if (!response_val->IsUndefined()) {
3127 response = v8::String::Cast(*response_val);
3128 } else {
3129 response = v8::String::New("");
3130 }
3131
3132 // Log the JSON request/response.
3133 if (FLAG_trace_debug_json) {
3134 PrintLn(request);
3135 PrintLn(response);
3136 }
3137
3138 // Get the running state.
3139 fun_name = v8::String::New("isRunning");
3140 fun = v8::Function::Cast(*cmd_processor->Get(fun_name));
3141 static const int kArgc = 1;
3142 v8::Handle<Value> argv[kArgc] = { response };
3143 v8::Local<v8::Value> running_val = fun->Call(cmd_processor, kArgc, argv);
3144 if (!try_catch.HasCaught()) {
3145 running = running_val->ToBoolean()->Value();
3146 }
3147 } else {
3148 // In case of failure the result text is the exception text.
3149 response = try_catch.Exception()->ToString();
3150 }
3151
ager@chromium.org41826e72009-03-30 13:30:57 +00003152 // Return the result.
ager@chromium.org5ec48922009-05-05 07:25:34 +00003153 MessageImpl message = MessageImpl::NewResponse(
3154 event,
3155 running,
3156 Handle<JSObject>::cast(exec_state),
3157 Handle<JSObject>::cast(event_data),
3158 Handle<String>(Utils::OpenHandle(*response)),
3159 command.client_data());
3160 InvokeMessageHandler(message);
3161 command.Dispose();
ager@chromium.org41826e72009-03-30 13:30:57 +00003162
3163 // Return from debug event processing if either the VM is put into the
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00003164 // running state (through a continue command) or auto continue is active
ager@chromium.org41826e72009-03-30 13:30:57 +00003165 // and there are no more commands queued.
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003166 if (running && !HasCommands()) {
ager@chromium.org41826e72009-03-30 13:30:57 +00003167 return;
3168 }
3169 }
3170}
3171
3172
iposva@chromium.org245aa852009-02-10 00:49:54 +00003173void Debugger::SetEventListener(Handle<Object> callback,
3174 Handle<Object> data) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00003175 HandleScope scope(isolate_);
3176 GlobalHandles* global_handles = isolate_->global_handles();
iposva@chromium.org245aa852009-02-10 00:49:54 +00003177
3178 // Clear the global handles for the event listener and the event listener data
3179 // object.
3180 if (!event_listener_.is_null()) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00003181 global_handles->Destroy(
iposva@chromium.org245aa852009-02-10 00:49:54 +00003182 reinterpret_cast<Object**>(event_listener_.location()));
3183 event_listener_ = Handle<Object>();
3184 }
3185 if (!event_listener_data_.is_null()) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00003186 global_handles->Destroy(
iposva@chromium.org245aa852009-02-10 00:49:54 +00003187 reinterpret_cast<Object**>(event_listener_data_.location()));
3188 event_listener_data_ = Handle<Object>();
3189 }
3190
3191 // If there is a new debug event listener register it together with its data
3192 // object.
3193 if (!callback->IsUndefined() && !callback->IsNull()) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003194 event_listener_ = Handle<Object>::cast(
lrn@chromium.org7516f052011-03-30 08:52:27 +00003195 global_handles->Create(*callback));
iposva@chromium.org245aa852009-02-10 00:49:54 +00003196 if (data.is_null()) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00003197 data = isolate_->factory()->undefined_value();
iposva@chromium.org245aa852009-02-10 00:49:54 +00003198 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003199 event_listener_data_ = Handle<Object>::cast(
lrn@chromium.org7516f052011-03-30 08:52:27 +00003200 global_handles->Create(*data));
iposva@chromium.org245aa852009-02-10 00:49:54 +00003201 }
3202
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003203 ListenersChanged();
iposva@chromium.org245aa852009-02-10 00:49:54 +00003204}
3205
3206
ager@chromium.org5ec48922009-05-05 07:25:34 +00003207void Debugger::SetMessageHandler(v8::Debug::MessageHandler2 handler) {
ager@chromium.org71daaf62009-04-01 07:22:49 +00003208 ScopedLock with(debugger_access_);
3209
ager@chromium.org381abbb2009-02-25 13:23:22 +00003210 message_handler_ = handler;
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003211 ListenersChanged();
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003212 if (handler == NULL) {
ager@chromium.org71daaf62009-04-01 07:22:49 +00003213 // Send an empty command to the debugger if in a break to make JavaScript
3214 // run again if the debugger is closed.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003215 if (isolate_->debug()->InDebugger()) {
ager@chromium.org71daaf62009-04-01 07:22:49 +00003216 ProcessCommand(Vector<const uint16_t>::empty());
3217 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003218 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003219}
3220
3221
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003222void Debugger::ListenersChanged() {
3223 if (IsDebuggerActive()) {
3224 // Disable the compilation cache when the debugger is active.
lrn@chromium.org7516f052011-03-30 08:52:27 +00003225 isolate_->compilation_cache()->Disable();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003226 debugger_unload_pending_ = false;
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003227 } else {
lrn@chromium.org7516f052011-03-30 08:52:27 +00003228 isolate_->compilation_cache()->Enable();
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003229 // Unload the debugger if event listener and message handler cleared.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003230 // Schedule this for later, because we may be in non-V8 thread.
3231 debugger_unload_pending_ = true;
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003232 }
3233}
3234
3235
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003236void Debugger::SetHostDispatchHandler(v8::Debug::HostDispatchHandler handler,
3237 int period) {
ager@chromium.org381abbb2009-02-25 13:23:22 +00003238 host_dispatch_handler_ = handler;
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003239 host_dispatch_micros_ = period * 1000;
ager@chromium.org381abbb2009-02-25 13:23:22 +00003240}
3241
3242
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003243void Debugger::SetDebugMessageDispatchHandler(
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003244 v8::Debug::DebugMessageDispatchHandler handler, bool provide_locker) {
3245 ScopedLock with(dispatch_handler_access_);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003246 debug_message_dispatch_handler_ = handler;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003247
3248 if (provide_locker && message_dispatch_helper_thread_ == NULL) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003249 message_dispatch_helper_thread_ = new MessageDispatchHelperThread(isolate_);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003250 message_dispatch_helper_thread_->Start();
3251 }
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003252}
3253
3254
ager@chromium.org41826e72009-03-30 13:30:57 +00003255// Calls the registered debug message handler. This callback is part of the
ager@chromium.org5ec48922009-05-05 07:25:34 +00003256// public API.
3257void Debugger::InvokeMessageHandler(MessageImpl message) {
ager@chromium.org71daaf62009-04-01 07:22:49 +00003258 ScopedLock with(debugger_access_);
3259
ager@chromium.org381abbb2009-02-25 13:23:22 +00003260 if (message_handler_ != NULL) {
ager@chromium.org5ec48922009-05-05 07:25:34 +00003261 message_handler_(message);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003262 }
ager@chromium.org41826e72009-03-30 13:30:57 +00003263}
3264
3265
3266// Puts a command coming from the public API on the queue. Creates
3267// a copy of the command string managed by the debugger. Up to this
3268// point, the command data was managed by the API client. Called
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003269// by the API client thread.
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003270void Debugger::ProcessCommand(Vector<const uint16_t> command,
3271 v8::Debug::ClientData* client_data) {
3272 // Need to cast away const.
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003273 CommandMessage message = CommandMessage::New(
ager@chromium.org41826e72009-03-30 13:30:57 +00003274 Vector<uint16_t>(const_cast<uint16_t*>(command.start()),
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003275 command.length()),
3276 client_data);
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00003277 isolate_->logger()->DebugTag("Put command on command_queue.");
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003278 command_queue_.Put(message);
ager@chromium.org41826e72009-03-30 13:30:57 +00003279 command_received_->Signal();
sgjesse@chromium.org3afc1582009-04-16 22:31:44 +00003280
3281 // Set the debug command break flag to have the command processed.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003282 if (!isolate_->debug()->InDebugger()) {
3283 isolate_->stack_guard()->DebugCommand();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003284 }
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003285
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003286 MessageDispatchHelperThread* dispatch_thread;
3287 {
3288 ScopedLock with(dispatch_handler_access_);
3289 dispatch_thread = message_dispatch_helper_thread_;
3290 }
3291
3292 if (dispatch_thread == NULL) {
3293 CallMessageDispatchHandler();
3294 } else {
3295 dispatch_thread->Schedule();
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003296 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003297}
3298
3299
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003300bool Debugger::HasCommands() {
ager@chromium.org41826e72009-03-30 13:30:57 +00003301 return !command_queue_.IsEmpty();
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003302}
3303
3304
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00003305void Debugger::EnqueueDebugCommand(v8::Debug::ClientData* client_data) {
3306 CommandMessage message = CommandMessage::New(Vector<uint16_t>(), client_data);
3307 event_command_queue_.Put(message);
3308
3309 // Set the debug command break flag to have the command processed.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003310 if (!isolate_->debug()->InDebugger()) {
3311 isolate_->stack_guard()->DebugCommand();
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00003312 }
3313}
3314
3315
ager@chromium.org71daaf62009-04-01 07:22:49 +00003316bool Debugger::IsDebuggerActive() {
3317 ScopedLock with(debugger_access_);
3318
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00003319 return message_handler_ != NULL ||
3320 !event_listener_.is_null() ||
3321 force_debugger_active_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003322}
3323
3324
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003325Handle<Object> Debugger::Call(Handle<JSFunction> fun,
3326 Handle<Object> data,
3327 bool* pending_exception) {
ager@chromium.org71daaf62009-04-01 07:22:49 +00003328 // When calling functions in the debugger prevent it from beeing unloaded.
3329 Debugger::never_unload_debugger_ = true;
3330
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003331 // Enter the debugger.
3332 EnterDebugger debugger;
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +00003333 if (debugger.FailedToEnter()) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00003334 return isolate_->factory()->undefined_value();
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003335 }
3336
3337 // Create the execution state.
3338 bool caught_exception = false;
3339 Handle<Object> exec_state = MakeExecutionState(&caught_exception);
3340 if (caught_exception) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00003341 return isolate_->factory()->undefined_value();
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003342 }
3343
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00003344 Handle<Object> argv[] = { exec_state, data };
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +00003345 Handle<Object> result = Execution::Call(
3346 fun,
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00003347 Handle<Object>(isolate_->debug()->debug_context_->global_proxy(),
3348 isolate_),
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00003349 ARRAY_SIZE(argv),
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +00003350 argv,
3351 pending_exception);
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003352 return result;
3353}
3354
3355
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003356static void StubMessageHandler2(const v8::Debug::Message& message) {
3357 // Simply ignore message.
3358}
3359
3360
3361bool Debugger::StartAgent(const char* name, int port,
3362 bool wait_for_connection) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003363 ASSERT(Isolate::Current() == isolate_);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003364 if (wait_for_connection) {
3365 // Suspend V8 if it is already running or set V8 to suspend whenever
3366 // it starts.
3367 // Provide stub message handler; V8 auto-continues each suspend
3368 // when there is no message handler; we doesn't need it.
3369 // Once become suspended, V8 will stay so indefinitely long, until remote
3370 // debugger connects and issues "continue" command.
3371 Debugger::message_handler_ = StubMessageHandler2;
3372 v8::Debug::DebugBreak();
3373 }
3374
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00003375 if (Socket::SetUp()) {
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00003376 if (agent_ == NULL) {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00003377 agent_ = new DebuggerAgent(name, port);
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00003378 agent_->Start();
3379 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003380 return true;
3381 }
3382
3383 return false;
3384}
3385
3386
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003387void Debugger::StopAgent() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003388 ASSERT(Isolate::Current() == isolate_);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003389 if (agent_ != NULL) {
3390 agent_->Shutdown();
3391 agent_->Join();
3392 delete agent_;
3393 agent_ = NULL;
3394 }
3395}
3396
3397
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003398void Debugger::WaitForAgent() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003399 ASSERT(Isolate::Current() == isolate_);
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003400 if (agent_ != NULL)
3401 agent_->WaitUntilListening();
3402}
3403
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003404
3405void Debugger::CallMessageDispatchHandler() {
3406 v8::Debug::DebugMessageDispatchHandler handler;
3407 {
3408 ScopedLock with(dispatch_handler_access_);
3409 handler = Debugger::debug_message_dispatch_handler_;
3410 }
3411 if (handler != NULL) {
3412 handler();
3413 }
3414}
3415
3416
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003417EnterDebugger::EnterDebugger()
3418 : isolate_(Isolate::Current()),
3419 prev_(isolate_->debug()->debugger_entry()),
3420 it_(isolate_),
3421 has_js_frames_(!it_.done()),
3422 save_(isolate_) {
3423 Debug* debug = isolate_->debug();
3424 ASSERT(prev_ != NULL || !debug->is_interrupt_pending(PREEMPT));
3425 ASSERT(prev_ != NULL || !debug->is_interrupt_pending(DEBUGBREAK));
3426
3427 // Link recursive debugger entry.
3428 debug->set_debugger_entry(this);
3429
3430 // Store the previous break id and frame id.
3431 break_id_ = debug->break_id();
3432 break_frame_id_ = debug->break_frame_id();
3433
3434 // Create the new break info. If there is no JavaScript frames there is no
3435 // break frame id.
3436 if (has_js_frames_) {
3437 debug->NewBreak(it_.frame()->id());
3438 } else {
3439 debug->NewBreak(StackFrame::NO_ID);
3440 }
3441
3442 // Make sure that debugger is loaded and enter the debugger context.
3443 load_failed_ = !debug->Load();
3444 if (!load_failed_) {
3445 // NOTE the member variable save which saves the previous context before
3446 // this change.
3447 isolate_->set_context(*debug->debug_context());
3448 }
3449}
3450
3451
3452EnterDebugger::~EnterDebugger() {
3453 ASSERT(Isolate::Current() == isolate_);
3454 Debug* debug = isolate_->debug();
3455
3456 // Restore to the previous break state.
3457 debug->SetBreak(break_frame_id_, break_id_);
3458
3459 // Check for leaving the debugger.
fschneider@chromium.org7d10be52012-04-10 12:30:14 +00003460 if (!load_failed_ && prev_ == NULL) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003461 // Clear mirror cache when leaving the debugger. Skip this if there is a
3462 // pending exception as clearing the mirror cache calls back into
3463 // JavaScript. This can happen if the v8::Debug::Call is used in which
3464 // case the exception should end up in the calling code.
3465 if (!isolate_->has_pending_exception()) {
3466 // Try to avoid any pending debug break breaking in the clear mirror
3467 // cache JavaScript code.
3468 if (isolate_->stack_guard()->IsDebugBreak()) {
3469 debug->set_interrupts_pending(DEBUGBREAK);
3470 isolate_->stack_guard()->Continue(DEBUGBREAK);
3471 }
3472 debug->ClearMirrorCache();
3473 }
3474
3475 // Request preemption and debug break when leaving the last debugger entry
3476 // if any of these where recorded while debugging.
3477 if (debug->is_interrupt_pending(PREEMPT)) {
3478 // This re-scheduling of preemption is to avoid starvation in some
3479 // debugging scenarios.
3480 debug->clear_interrupt_pending(PREEMPT);
3481 isolate_->stack_guard()->Preempt();
3482 }
3483 if (debug->is_interrupt_pending(DEBUGBREAK)) {
3484 debug->clear_interrupt_pending(DEBUGBREAK);
3485 isolate_->stack_guard()->DebugBreak();
3486 }
3487
3488 // If there are commands in the queue when leaving the debugger request
3489 // that these commands are processed.
3490 if (isolate_->debugger()->HasCommands()) {
3491 isolate_->stack_guard()->DebugCommand();
3492 }
3493
3494 // If leaving the debugger with the debugger no longer active unload it.
3495 if (!isolate_->debugger()->IsDebuggerActive()) {
3496 isolate_->debugger()->UnloadDebugger();
3497 }
3498 }
3499
3500 // Leaving this debugger entry.
3501 debug->set_debugger_entry(prev_);
3502}
3503
3504
ager@chromium.org5ec48922009-05-05 07:25:34 +00003505MessageImpl MessageImpl::NewEvent(DebugEvent event,
3506 bool running,
3507 Handle<JSObject> exec_state,
3508 Handle<JSObject> event_data) {
3509 MessageImpl message(true, event, running,
3510 exec_state, event_data, Handle<String>(), NULL);
3511 return message;
3512}
3513
3514
3515MessageImpl MessageImpl::NewResponse(DebugEvent event,
3516 bool running,
3517 Handle<JSObject> exec_state,
3518 Handle<JSObject> event_data,
3519 Handle<String> response_json,
3520 v8::Debug::ClientData* client_data) {
3521 MessageImpl message(false, event, running,
3522 exec_state, event_data, response_json, client_data);
3523 return message;
3524}
3525
3526
3527MessageImpl::MessageImpl(bool is_event,
3528 DebugEvent event,
3529 bool running,
3530 Handle<JSObject> exec_state,
3531 Handle<JSObject> event_data,
3532 Handle<String> response_json,
3533 v8::Debug::ClientData* client_data)
3534 : is_event_(is_event),
3535 event_(event),
3536 running_(running),
3537 exec_state_(exec_state),
3538 event_data_(event_data),
3539 response_json_(response_json),
3540 client_data_(client_data) {}
3541
3542
3543bool MessageImpl::IsEvent() const {
3544 return is_event_;
3545}
3546
3547
3548bool MessageImpl::IsResponse() const {
3549 return !is_event_;
3550}
3551
3552
3553DebugEvent MessageImpl::GetEvent() const {
3554 return event_;
3555}
3556
3557
3558bool MessageImpl::WillStartRunning() const {
3559 return running_;
3560}
3561
3562
3563v8::Handle<v8::Object> MessageImpl::GetExecutionState() const {
3564 return v8::Utils::ToLocal(exec_state_);
3565}
3566
3567
3568v8::Handle<v8::Object> MessageImpl::GetEventData() const {
3569 return v8::Utils::ToLocal(event_data_);
3570}
3571
3572
3573v8::Handle<v8::String> MessageImpl::GetJSON() const {
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +00003574 v8::HandleScope scope(
3575 reinterpret_cast<v8::Isolate*>(event_data_->GetIsolate()));
ager@chromium.org5ec48922009-05-05 07:25:34 +00003576
3577 if (IsEvent()) {
3578 // Call toJSONProtocol on the debug event object.
3579 Handle<Object> fun = GetProperty(event_data_, "toJSONProtocol");
3580 if (!fun->IsJSFunction()) {
3581 return v8::Handle<v8::String>();
3582 }
3583 bool caught_exception;
3584 Handle<Object> json = Execution::TryCall(Handle<JSFunction>::cast(fun),
3585 event_data_,
3586 0, NULL, &caught_exception);
3587 if (caught_exception || !json->IsString()) {
3588 return v8::Handle<v8::String>();
3589 }
3590 return scope.Close(v8::Utils::ToLocal(Handle<String>::cast(json)));
3591 } else {
3592 return v8::Utils::ToLocal(response_json_);
3593 }
3594}
3595
3596
3597v8::Handle<v8::Context> MessageImpl::GetEventContext() const {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003598 Isolate* isolate = Isolate::Current();
3599 v8::Handle<v8::Context> context = GetDebugEventContext(isolate);
3600 // Isolate::context() may be NULL when "script collected" event occures.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003601 ASSERT(!context.IsEmpty() || event_ == v8::ScriptCollected);
rossberg@chromium.org717967f2011-07-20 13:44:42 +00003602 return context;
ager@chromium.org5ec48922009-05-05 07:25:34 +00003603}
3604
3605
3606v8::Debug::ClientData* MessageImpl::GetClientData() const {
3607 return client_data_;
3608}
3609
3610
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003611EventDetailsImpl::EventDetailsImpl(DebugEvent event,
3612 Handle<JSObject> exec_state,
3613 Handle<JSObject> event_data,
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00003614 Handle<Object> callback_data,
3615 v8::Debug::ClientData* client_data)
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003616 : event_(event),
3617 exec_state_(exec_state),
3618 event_data_(event_data),
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00003619 callback_data_(callback_data),
3620 client_data_(client_data) {}
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003621
3622
3623DebugEvent EventDetailsImpl::GetEvent() const {
3624 return event_;
3625}
3626
3627
3628v8::Handle<v8::Object> EventDetailsImpl::GetExecutionState() const {
3629 return v8::Utils::ToLocal(exec_state_);
3630}
3631
3632
3633v8::Handle<v8::Object> EventDetailsImpl::GetEventData() const {
3634 return v8::Utils::ToLocal(event_data_);
3635}
3636
3637
3638v8::Handle<v8::Context> EventDetailsImpl::GetEventContext() const {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003639 return GetDebugEventContext(Isolate::Current());
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003640}
3641
3642
3643v8::Handle<v8::Value> EventDetailsImpl::GetCallbackData() const {
3644 return v8::Utils::ToLocal(callback_data_);
3645}
3646
3647
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00003648v8::Debug::ClientData* EventDetailsImpl::GetClientData() const {
3649 return client_data_;
3650}
3651
3652
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003653CommandMessage::CommandMessage() : text_(Vector<uint16_t>::empty()),
3654 client_data_(NULL) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003655}
3656
3657
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003658CommandMessage::CommandMessage(const Vector<uint16_t>& text,
3659 v8::Debug::ClientData* data)
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003660 : text_(text),
3661 client_data_(data) {
3662}
3663
3664
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003665CommandMessage::~CommandMessage() {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003666}
3667
3668
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003669void CommandMessage::Dispose() {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003670 text_.Dispose();
3671 delete client_data_;
3672 client_data_ = NULL;
3673}
3674
3675
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003676CommandMessage CommandMessage::New(const Vector<uint16_t>& command,
3677 v8::Debug::ClientData* data) {
3678 return CommandMessage(command.Clone(), data);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003679}
3680
3681
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003682CommandMessageQueue::CommandMessageQueue(int size) : start_(0), end_(0),
3683 size_(size) {
3684 messages_ = NewArray<CommandMessage>(size);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003685}
3686
3687
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003688CommandMessageQueue::~CommandMessageQueue() {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003689 while (!IsEmpty()) {
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003690 CommandMessage m = Get();
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003691 m.Dispose();
3692 }
kasper.lund7276f142008-07-30 08:49:36 +00003693 DeleteArray(messages_);
3694}
3695
3696
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003697CommandMessage CommandMessageQueue::Get() {
kasper.lund7276f142008-07-30 08:49:36 +00003698 ASSERT(!IsEmpty());
3699 int result = start_;
3700 start_ = (start_ + 1) % size_;
3701 return messages_[result];
3702}
3703
3704
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003705void CommandMessageQueue::Put(const CommandMessage& message) {
kasper.lund7276f142008-07-30 08:49:36 +00003706 if ((end_ + 1) % size_ == start_) {
3707 Expand();
3708 }
3709 messages_[end_] = message;
3710 end_ = (end_ + 1) % size_;
3711}
3712
3713
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003714void CommandMessageQueue::Expand() {
3715 CommandMessageQueue new_queue(size_ * 2);
kasper.lund7276f142008-07-30 08:49:36 +00003716 while (!IsEmpty()) {
3717 new_queue.Put(Get());
3718 }
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003719 CommandMessage* array_to_free = messages_;
kasper.lund7276f142008-07-30 08:49:36 +00003720 *this = new_queue;
3721 new_queue.messages_ = array_to_free;
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003722 // Make the new_queue empty so that it doesn't call Dispose on any messages.
3723 new_queue.start_ = new_queue.end_;
kasper.lund7276f142008-07-30 08:49:36 +00003724 // Automatic destructor called on new_queue, freeing array_to_free.
3725}
3726
3727
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00003728LockingCommandMessageQueue::LockingCommandMessageQueue(Logger* logger, int size)
3729 : logger_(logger), queue_(size) {
kasper.lund7276f142008-07-30 08:49:36 +00003730 lock_ = OS::CreateMutex();
3731}
3732
3733
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003734LockingCommandMessageQueue::~LockingCommandMessageQueue() {
kasper.lund7276f142008-07-30 08:49:36 +00003735 delete lock_;
3736}
3737
3738
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003739bool LockingCommandMessageQueue::IsEmpty() const {
kasper.lund7276f142008-07-30 08:49:36 +00003740 ScopedLock sl(lock_);
3741 return queue_.IsEmpty();
3742}
3743
3744
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003745CommandMessage LockingCommandMessageQueue::Get() {
kasper.lund7276f142008-07-30 08:49:36 +00003746 ScopedLock sl(lock_);
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003747 CommandMessage result = queue_.Get();
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00003748 logger_->DebugEvent("Get", result.text());
kasper.lund7276f142008-07-30 08:49:36 +00003749 return result;
3750}
3751
3752
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003753void LockingCommandMessageQueue::Put(const CommandMessage& message) {
kasper.lund7276f142008-07-30 08:49:36 +00003754 ScopedLock sl(lock_);
3755 queue_.Put(message);
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00003756 logger_->DebugEvent("Put", message.text());
kasper.lund7276f142008-07-30 08:49:36 +00003757}
3758
3759
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003760void LockingCommandMessageQueue::Clear() {
kasper.lund7276f142008-07-30 08:49:36 +00003761 ScopedLock sl(lock_);
3762 queue_.Clear();
3763}
3764
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003765
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003766MessageDispatchHelperThread::MessageDispatchHelperThread(Isolate* isolate)
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00003767 : Thread("v8:MsgDispHelpr"),
mstarzinger@chromium.orgf705b502013-04-04 11:38:09 +00003768 isolate_(isolate), sem_(OS::CreateSemaphore(0)),
3769 mutex_(OS::CreateMutex()), already_signalled_(false) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003770}
3771
3772
3773MessageDispatchHelperThread::~MessageDispatchHelperThread() {
3774 delete mutex_;
3775 delete sem_;
3776}
3777
3778
3779void MessageDispatchHelperThread::Schedule() {
3780 {
3781 ScopedLock lock(mutex_);
3782 if (already_signalled_) {
3783 return;
3784 }
3785 already_signalled_ = true;
3786 }
3787 sem_->Signal();
3788}
3789
3790
3791void MessageDispatchHelperThread::Run() {
3792 while (true) {
3793 sem_->Wait();
3794 {
3795 ScopedLock lock(mutex_);
3796 already_signalled_ = false;
3797 }
3798 {
mstarzinger@chromium.orgf705b502013-04-04 11:38:09 +00003799 Locker locker(reinterpret_cast<v8::Isolate*>(isolate_));
3800 isolate_->debugger()->CallMessageDispatchHandler();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003801 }
3802 }
3803}
3804
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003805#endif // ENABLE_DEBUGGER_SUPPORT
kasper.lund7276f142008-07-30 08:49:36 +00003806
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003807} } // namespace v8::internal