blob: dbbfe7e76f4e6956bffad2d5d17e970ecda6582c [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();
ulan@chromium.org57ff8812013-05-10 08:16:55 +000079 ScopedVector<char> data(s->Utf8Length() + 1);
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +000080 if (data.start() == NULL) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000081 V8::FatalProcessOutOfMemory("PrintLn");
82 return;
83 }
ulan@chromium.org57ff8812013-05-10 08:16:55 +000084 s->WriteUtf8(data.start());
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +000085 PrintF("%s\n", data.start());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000086}
87
88
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +000089static Handle<Code> ComputeCallDebugPrepareStepIn(Isolate* isolate,
90 int argc,
91 Code::Kind kind) {
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +000092 return isolate->stub_cache()->ComputeCallDebugPrepareStepIn(argc, kind);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000093}
94
95
96static v8::Handle<v8::Context> GetDebugEventContext(Isolate* isolate) {
97 Handle<Context> context = isolate->debug()->debugger_entry()->GetContext();
98 // Isolate::context() may have been NULL when "script collected" event
99 // occured.
100 if (context.is_null()) return v8::Local<v8::Context>();
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000101 Handle<Context> native_context(context->native_context());
102 return v8::Utils::ToLocal(native_context);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000103}
104
105
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000106BreakLocationIterator::BreakLocationIterator(Handle<DebugInfo> debug_info,
107 BreakLocatorType type) {
108 debug_info_ = debug_info;
109 type_ = type;
110 reloc_iterator_ = NULL;
111 reloc_iterator_original_ = NULL;
112 Reset(); // Initialize the rest of the member variables.
113}
114
115
116BreakLocationIterator::~BreakLocationIterator() {
117 ASSERT(reloc_iterator_ != NULL);
118 ASSERT(reloc_iterator_original_ != NULL);
119 delete reloc_iterator_;
120 delete reloc_iterator_original_;
121}
122
123
124void BreakLocationIterator::Next() {
rossberg@chromium.org79e79022013-06-03 15:43:46 +0000125 DisallowHeapAllocation no_gc;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000126 ASSERT(!RinfoDone());
127
128 // Iterate through reloc info for code and original code stopping at each
129 // breakable code target.
130 bool first = break_point_ == -1;
131 while (!RinfoDone()) {
132 if (!first) RinfoNext();
133 first = false;
134 if (RinfoDone()) return;
135
ager@chromium.org236ad962008-09-25 09:45:57 +0000136 // Whenever a statement position or (plain) position is passed update the
137 // current value of these.
138 if (RelocInfo::IsPosition(rmode())) {
139 if (RelocInfo::IsStatementPosition(rmode())) {
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000140 statement_position_ = static_cast<int>(
141 rinfo()->data() - debug_info_->shared()->start_position());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000142 }
ager@chromium.org236ad962008-09-25 09:45:57 +0000143 // Always update the position as we don't want that to be before the
144 // statement position.
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000145 position_ = static_cast<int>(
146 rinfo()->data() - debug_info_->shared()->start_position());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000147 ASSERT(position_ >= 0);
148 ASSERT(statement_position_ >= 0);
149 }
150
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000151 if (IsDebugBreakSlot()) {
152 // There is always a possible break point at a debug break slot.
153 break_point_++;
154 return;
155 } else if (RelocInfo::IsCodeTarget(rmode())) {
156 // Check for breakable code target. Look in the original code as setting
157 // break points can cause the code targets in the running (debugged) code
158 // to be of a different kind than in the original code.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000159 Address target = original_rinfo()->target_address();
ager@chromium.org8bb60582008-12-11 12:02:20 +0000160 Code* code = Code::GetCodeFromTargetAddress(target);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000161 if ((code->is_inline_cache_stub() &&
danno@chromium.org40cb8782011-05-25 07:58:50 +0000162 !code->is_binary_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
verwaest@chromium.org8a00e822013-06-10 15:11:22 +0000214// Find the break point at the supplied address, or the closest one before
215// the address.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000216void BreakLocationIterator::FindBreakLocationFromAddress(Address pc) {
217 // Run through all break points to locate the one closest to the address.
218 int closest_break_point = 0;
219 int distance = kMaxInt;
220 while (!Done()) {
221 // Check if this break point is closer that what was previously found.
verwaest@chromium.org8a00e822013-06-10 15:11:22 +0000222 if (this->pc() <= pc && pc - this->pc() < distance) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000223 closest_break_point = break_point();
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000224 distance = static_cast<int>(pc - this->pc());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000225 // Check whether we can't get any closer.
226 if (distance == 0) break;
227 }
228 Next();
229 }
230
231 // Move to the break point found.
232 Reset();
233 Next(closest_break_point);
234}
235
236
237// Find the break point closest to the supplied source position.
jkummerow@chromium.org93a47f42013-07-02 14:43:41 +0000238void BreakLocationIterator::FindBreakLocationFromPosition(int position,
239 BreakPositionAlignment alignment) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000240 // Run through all break points to locate the one closest to the source
241 // position.
242 int closest_break_point = 0;
243 int distance = kMaxInt;
jkummerow@chromium.org93a47f42013-07-02 14:43:41 +0000244
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000245 while (!Done()) {
jkummerow@chromium.org93a47f42013-07-02 14:43:41 +0000246 int next_position;
247 switch (alignment) {
248 case STATEMENT_ALIGNED:
249 next_position = this->statement_position();
250 break;
251 case BREAK_POSITION_ALIGNED:
252 next_position = this->position();
253 break;
254 default:
255 UNREACHABLE();
256 next_position = this->statement_position();
257 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000258 // Check if this break point is closer that what was previously found.
jkummerow@chromium.org93a47f42013-07-02 14:43:41 +0000259 if (position <= next_position && next_position - position < distance) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000260 closest_break_point = break_point();
jkummerow@chromium.org93a47f42013-07-02 14:43:41 +0000261 distance = next_position - position;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000262 // Check whether we can't get any closer.
263 if (distance == 0) break;
264 }
265 Next();
266 }
267
268 // Move to the break point found.
269 Reset();
270 Next(closest_break_point);
271}
272
273
274void BreakLocationIterator::Reset() {
275 // Create relocation iterators for the two code objects.
276 if (reloc_iterator_ != NULL) delete reloc_iterator_;
277 if (reloc_iterator_original_ != NULL) delete reloc_iterator_original_;
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +0000278 reloc_iterator_ = new RelocIterator(
279 debug_info_->code(),
280 ~RelocInfo::ModeMask(RelocInfo::CODE_AGE_SEQUENCE));
281 reloc_iterator_original_ = new RelocIterator(
282 debug_info_->original_code(),
283 ~RelocInfo::ModeMask(RelocInfo::CODE_AGE_SEQUENCE));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000284
285 // Position at the first break point.
286 break_point_ = -1;
287 position_ = 1;
288 statement_position_ = 1;
289 Next();
290}
291
292
293bool BreakLocationIterator::Done() const {
294 return RinfoDone();
295}
296
297
298void BreakLocationIterator::SetBreakPoint(Handle<Object> break_point_object) {
299 // If there is not already a real break point here patch code with debug
300 // break.
301 if (!HasBreakPoint()) {
302 SetDebugBreak();
303 }
ager@chromium.orga1645e22009-09-09 19:27:10 +0000304 ASSERT(IsDebugBreak() || IsDebuggerStatement());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000305 // Set the break point information.
306 DebugInfo::SetBreakPoint(debug_info_, code_position(),
307 position(), statement_position(),
308 break_point_object);
309}
310
311
312void BreakLocationIterator::ClearBreakPoint(Handle<Object> break_point_object) {
313 // Clear the break point information.
314 DebugInfo::ClearBreakPoint(debug_info_, code_position(), break_point_object);
315 // If there are no more break points here remove the debug break.
316 if (!HasBreakPoint()) {
317 ClearDebugBreak();
318 ASSERT(!IsDebugBreak());
319 }
320}
321
322
323void BreakLocationIterator::SetOneShot() {
ager@chromium.orga1645e22009-09-09 19:27:10 +0000324 // Debugger statement always calls debugger. No need to modify it.
325 if (IsDebuggerStatement()) {
326 return;
327 }
328
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000329 // If there is a real break point here no more to do.
330 if (HasBreakPoint()) {
331 ASSERT(IsDebugBreak());
332 return;
333 }
334
335 // Patch code with debug break.
336 SetDebugBreak();
337}
338
339
340void BreakLocationIterator::ClearOneShot() {
ager@chromium.orga1645e22009-09-09 19:27:10 +0000341 // Debugger statement always calls debugger. No need to modify it.
342 if (IsDebuggerStatement()) {
343 return;
344 }
345
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000346 // If there is a real break point here no more to do.
347 if (HasBreakPoint()) {
348 ASSERT(IsDebugBreak());
349 return;
350 }
351
352 // Patch code removing debug break.
353 ClearDebugBreak();
354 ASSERT(!IsDebugBreak());
355}
356
357
358void BreakLocationIterator::SetDebugBreak() {
ager@chromium.orga1645e22009-09-09 19:27:10 +0000359 // Debugger statement always calls debugger. No need to modify it.
360 if (IsDebuggerStatement()) {
361 return;
362 }
363
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000364 // If there is already a break point here just return. This might happen if
v8.team.kasperl727e9952008-09-02 14:56:44 +0000365 // the same code is flooded with break points twice. Flooding the same
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000366 // function twice might happen when stepping in a function with an exception
367 // handler as the handler and the function is the same.
368 if (IsDebugBreak()) {
369 return;
370 }
371
ager@chromium.org236ad962008-09-25 09:45:57 +0000372 if (RelocInfo::IsJSReturn(rmode())) {
iposva@chromium.org245aa852009-02-10 00:49:54 +0000373 // Patch the frame exit code with a break point.
374 SetDebugBreakAtReturn();
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000375 } else if (IsDebugBreakSlot()) {
376 // Patch the code in the break slot.
377 SetDebugBreakAtSlot();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000378 } else {
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000379 // Patch the IC call.
380 SetDebugBreakAtIC();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000381 }
382 ASSERT(IsDebugBreak());
383}
384
385
386void BreakLocationIterator::ClearDebugBreak() {
ager@chromium.orga1645e22009-09-09 19:27:10 +0000387 // Debugger statement always calls debugger. No need to modify it.
388 if (IsDebuggerStatement()) {
389 return;
390 }
391
ager@chromium.org236ad962008-09-25 09:45:57 +0000392 if (RelocInfo::IsJSReturn(rmode())) {
iposva@chromium.org245aa852009-02-10 00:49:54 +0000393 // Restore the frame exit code.
394 ClearDebugBreakAtReturn();
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000395 } else if (IsDebugBreakSlot()) {
396 // Restore the code in the break slot.
397 ClearDebugBreakAtSlot();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000398 } else {
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000399 // Patch the IC call.
400 ClearDebugBreakAtIC();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000401 }
402 ASSERT(!IsDebugBreak());
403}
404
405
mstarzinger@chromium.org1510d582013-06-28 14:00:48 +0000406bool BreakLocationIterator::IsStepInLocation(Isolate* isolate) {
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +0000407 if (RelocInfo::IsConstructCall(original_rmode())) {
mstarzinger@chromium.org1510d582013-06-28 14:00:48 +0000408 return true;
409 } else if (RelocInfo::IsCodeTarget(rmode())) {
410 HandleScope scope(debug_info_->GetIsolate());
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +0000411 Address target = original_rinfo()->target_address();
mstarzinger@chromium.org1510d582013-06-28 14:00:48 +0000412 Handle<Code> target_code(Code::GetCodeFromTargetAddress(target));
danno@chromium.org59400602013-08-13 17:09:37 +0000413 if (target_code->kind() == Code::STUB) {
414 return target_code->major_key() == CodeStub::CallFunction;
415 }
mstarzinger@chromium.org1510d582013-06-28 14:00:48 +0000416 return target_code->is_call_stub() || target_code->is_keyed_call_stub();
417 } else {
418 return false;
419 }
420}
421
422
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +0000423void BreakLocationIterator::PrepareStepIn(Isolate* isolate) {
424 HandleScope scope(isolate);
ager@chromium.org381abbb2009-02-25 13:23:22 +0000425
ager@chromium.orga1645e22009-09-09 19:27:10 +0000426 // Step in can only be prepared if currently positioned on an IC call,
427 // construct call or CallFunction stub call.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000428 Address target = rinfo()->target_address();
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000429 Handle<Code> target_code(Code::GetCodeFromTargetAddress(target));
430 if (target_code->is_call_stub() || target_code->is_keyed_call_stub()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000431 // Step in through IC call is handled by the runtime system. Therefore make
432 // sure that the any current IC is cleared and the runtime system is
433 // called. If the executing code has a debug break at the location change
434 // the call in the original code as it is the code there that will be
435 // executed in place of the debug break call.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000436 Handle<Code> stub = ComputeCallDebugPrepareStepIn(
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +0000437 isolate, target_code->arguments_count(), target_code->kind());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000438 if (IsDebugBreak()) {
439 original_rinfo()->set_target_address(stub->entry());
440 } else {
441 rinfo()->set_target_address(stub->entry());
442 }
443 } else {
ager@chromium.orga1645e22009-09-09 19:27:10 +0000444#ifdef DEBUG
445 // All the following stuff is needed only for assertion checks so the code
446 // is wrapped in ifdef.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000447 Handle<Code> maybe_call_function_stub = target_code;
ager@chromium.orga1645e22009-09-09 19:27:10 +0000448 if (IsDebugBreak()) {
449 Address original_target = original_rinfo()->target_address();
450 maybe_call_function_stub =
451 Handle<Code>(Code::GetCodeFromTargetAddress(original_target));
452 }
453 bool is_call_function_stub =
454 (maybe_call_function_stub->kind() == Code::STUB &&
455 maybe_call_function_stub->major_key() == CodeStub::CallFunction);
456
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000457 // Step in through construct call requires no changes to the running code.
458 // Step in through getters/setters should already be prepared as well
459 // because caller of this function (Debug::PrepareStep) is expected to
460 // flood the top frame's function with one shot breakpoints.
ager@chromium.orga1645e22009-09-09 19:27:10 +0000461 // Step in through CallFunction stub should also be prepared by caller of
462 // this function (Debug::PrepareStep) which should flood target function
463 // with breakpoints.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000464 ASSERT(RelocInfo::IsConstructCall(rmode()) ||
465 target_code->is_inline_cache_stub() ||
466 is_call_function_stub);
ager@chromium.orga1645e22009-09-09 19:27:10 +0000467#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000468 }
469}
470
471
472// Check whether the break point is at a position which will exit the function.
473bool BreakLocationIterator::IsExit() const {
ager@chromium.org236ad962008-09-25 09:45:57 +0000474 return (RelocInfo::IsJSReturn(rmode()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000475}
476
477
478bool BreakLocationIterator::HasBreakPoint() {
479 return debug_info_->HasBreakPoint(code_position());
480}
481
482
483// Check whether there is a debug break at the current position.
484bool BreakLocationIterator::IsDebugBreak() {
ager@chromium.org236ad962008-09-25 09:45:57 +0000485 if (RelocInfo::IsJSReturn(rmode())) {
iposva@chromium.org245aa852009-02-10 00:49:54 +0000486 return IsDebugBreakAtReturn();
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000487 } else if (IsDebugBreakSlot()) {
488 return IsDebugBreakAtSlot();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000489 } else {
490 return Debug::IsDebugBreak(rinfo()->target_address());
491 }
492}
493
494
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000495void BreakLocationIterator::SetDebugBreakAtIC() {
496 // Patch the original code with the current address as the current address
497 // might have changed by the inline caching since the code was copied.
498 original_rinfo()->set_target_address(rinfo()->target_address());
499
500 RelocInfo::Mode mode = rmode();
501 if (RelocInfo::IsCodeTarget(mode)) {
502 Address target = rinfo()->target_address();
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000503 Handle<Code> target_code(Code::GetCodeFromTargetAddress(target));
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000504
505 // Patch the code to invoke the builtin debug break function matching the
506 // calling convention used by the call site.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000507 Handle<Code> dbgbrk_code(Debug::FindDebugBreak(target_code, mode));
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000508 rinfo()->set_target_address(dbgbrk_code->entry());
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000509 }
510}
511
512
513void BreakLocationIterator::ClearDebugBreakAtIC() {
514 // Patch the code to the original invoke.
515 rinfo()->set_target_address(original_rinfo()->target_address());
516}
517
518
ager@chromium.orga1645e22009-09-09 19:27:10 +0000519bool BreakLocationIterator::IsDebuggerStatement() {
ager@chromium.org5c838252010-02-19 08:53:10 +0000520 return RelocInfo::DEBUG_BREAK == rmode();
ager@chromium.orga1645e22009-09-09 19:27:10 +0000521}
522
523
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000524bool BreakLocationIterator::IsDebugBreakSlot() {
525 return RelocInfo::DEBUG_BREAK_SLOT == rmode();
526}
527
528
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000529Object* BreakLocationIterator::BreakPointObjects() {
530 return debug_info_->GetBreakPointObjects(code_position());
531}
532
533
ager@chromium.org381abbb2009-02-25 13:23:22 +0000534// Clear out all the debug break code. This is ONLY supposed to be used when
535// shutting down the debugger as it will leave the break point information in
536// DebugInfo even though the code is patched back to the non break point state.
537void BreakLocationIterator::ClearAllDebugBreak() {
538 while (!Done()) {
539 ClearDebugBreak();
540 Next();
541 }
542}
543
544
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000545bool BreakLocationIterator::RinfoDone() const {
546 ASSERT(reloc_iterator_->done() == reloc_iterator_original_->done());
547 return reloc_iterator_->done();
548}
549
550
551void BreakLocationIterator::RinfoNext() {
552 reloc_iterator_->next();
553 reloc_iterator_original_->next();
554#ifdef DEBUG
555 ASSERT(reloc_iterator_->done() == reloc_iterator_original_->done());
556 if (!reloc_iterator_->done()) {
557 ASSERT(rmode() == original_rmode());
558 }
559#endif
560}
561
562
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000563// Threading support.
564void Debug::ThreadInit() {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000565 thread_local_.break_count_ = 0;
566 thread_local_.break_id_ = 0;
567 thread_local_.break_frame_id_ = StackFrame::NO_ID;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000568 thread_local_.last_step_action_ = StepNone;
ager@chromium.org236ad962008-09-25 09:45:57 +0000569 thread_local_.last_statement_position_ = RelocInfo::kNoPosition;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000570 thread_local_.step_count_ = 0;
571 thread_local_.last_fp_ = 0;
lrn@chromium.org34e60782011-09-15 07:25:40 +0000572 thread_local_.queued_step_count_ = 0;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000573 thread_local_.step_into_fp_ = 0;
ager@chromium.orga1645e22009-09-09 19:27:10 +0000574 thread_local_.step_out_fp_ = 0;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000575 thread_local_.after_break_target_ = 0;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000576 // TODO(isolates): frames_are_dropped_?
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000577 thread_local_.debugger_entry_ = NULL;
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000578 thread_local_.pending_interrupts_ = 0;
ricow@chromium.org0b9f8502010-08-18 07:45:01 +0000579 thread_local_.restarter_frame_function_pointer_ = NULL;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000580}
581
582
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000583char* Debug::ArchiveDebug(char* storage) {
584 char* to = storage;
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000585 OS::MemCopy(to, reinterpret_cast<char*>(&thread_local_), sizeof(ThreadLocal));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000586 to += sizeof(ThreadLocal);
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000587 OS::MemCopy(to, reinterpret_cast<char*>(&registers_), sizeof(registers_));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000588 ThreadInit();
589 ASSERT(to <= storage + ArchiveSpacePerThread());
590 return storage + ArchiveSpacePerThread();
591}
592
593
594char* Debug::RestoreDebug(char* storage) {
595 char* from = storage;
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000596 OS::MemCopy(
597 reinterpret_cast<char*>(&thread_local_), from, sizeof(ThreadLocal));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000598 from += sizeof(ThreadLocal);
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000599 OS::MemCopy(reinterpret_cast<char*>(&registers_), from, sizeof(registers_));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000600 ASSERT(from <= storage + ArchiveSpacePerThread());
601 return storage + ArchiveSpacePerThread();
602}
603
604
605int Debug::ArchiveSpacePerThread() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000606 return sizeof(ThreadLocal) + sizeof(JSCallerSavedBuffer);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000607}
608
609
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000610// Frame structure (conforms InternalFrame structure):
611// -- code
612// -- SMI maker
613// -- function (slot is called "context")
614// -- frame base
615Object** Debug::SetUpFrameDropperFrame(StackFrame* bottom_js_frame,
616 Handle<Code> code) {
617 ASSERT(bottom_js_frame->is_java_script());
618
619 Address fp = bottom_js_frame->fp();
620
621 // Move function pointer into "context" slot.
622 Memory::Object_at(fp + StandardFrameConstants::kContextOffset) =
623 Memory::Object_at(fp + JavaScriptFrameConstants::kFunctionOffset);
624
625 Memory::Object_at(fp + InternalFrameConstants::kCodeOffset) = *code;
626 Memory::Object_at(fp + StandardFrameConstants::kMarkerOffset) =
627 Smi::FromInt(StackFrame::INTERNAL);
628
629 return reinterpret_cast<Object**>(&Memory::Object_at(
630 fp + StandardFrameConstants::kContextOffset));
631}
632
633const int Debug::kFrameDropperFrameSize = 4;
634
635
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000636void ScriptCache::Add(Handle<Script> script) {
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +0000637 GlobalHandles* global_handles = isolate_->global_handles();
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000638 // Create an entry in the hash map for the script.
mstarzinger@chromium.org1510d582013-06-28 14:00:48 +0000639 int id = script->id()->value();
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000640 HashMap::Entry* entry =
641 HashMap::Lookup(reinterpret_cast<void*>(id), Hash(id), true);
642 if (entry->value != NULL) {
643 ASSERT(*script == *reinterpret_cast<Script**>(entry->value));
644 return;
645 }
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000646 // Globalize the script object, make it weak and use the location of the
647 // global handle as the value in the hash map.
648 Handle<Script> script_ =
hpayer@chromium.org4f99be92013-12-18 16:23:55 +0000649 Handle<Script>::cast(global_handles->Create(*script));
650 GlobalHandles::MakeWeak(reinterpret_cast<Object**>(script_.location()),
651 this,
652 ScriptCache::HandleWeakScript);
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000653 entry->value = script_.location();
654}
655
656
657Handle<FixedArray> ScriptCache::GetScripts() {
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +0000658 Factory* factory = isolate_->factory();
verwaest@chromium.orgd4be0f02013-06-05 13:39:03 +0000659 Handle<FixedArray> instances = factory->NewFixedArray(occupancy());
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000660 int count = 0;
661 for (HashMap::Entry* entry = Start(); entry != NULL; entry = Next(entry)) {
662 ASSERT(entry->value != NULL);
663 if (entry->value != NULL) {
664 instances->set(count, *reinterpret_cast<Script**>(entry->value));
665 count++;
666 }
667 }
668 return instances;
669}
670
671
672void ScriptCache::ProcessCollectedScripts() {
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +0000673 Debugger* debugger = isolate_->debugger();
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000674 for (int i = 0; i < collected_scripts_.length(); i++) {
lrn@chromium.org7516f052011-03-30 08:52:27 +0000675 debugger->OnScriptCollected(collected_scripts_[i]);
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000676 }
677 collected_scripts_.Clear();
678}
679
680
681void ScriptCache::Clear() {
682 // Iterate the script cache to get rid of all the weak handles.
683 for (HashMap::Entry* entry = Start(); entry != NULL; entry = Next(entry)) {
684 ASSERT(entry != NULL);
685 Object** location = reinterpret_cast<Object**>(entry->value);
686 ASSERT((*location)->IsScript());
hpayer@chromium.org4f99be92013-12-18 16:23:55 +0000687 GlobalHandles::ClearWeakness(location);
688 GlobalHandles::Destroy(location);
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000689 }
690 // Clear the content of the hash map.
691 HashMap::Clear();
692}
693
694
hpayer@chromium.org4f99be92013-12-18 16:23:55 +0000695void ScriptCache::HandleWeakScript(
696 const v8::WeakCallbackData<v8::Value, void>& data) {
697 // Retrieve the script identifier.
698 Handle<Object> object = Utils::OpenHandle(*data.GetValue());
699 int id = Handle<Script>::cast(object)->id()->value();
700 void* key = reinterpret_cast<void*>(id);
701 uint32_t hash = Hash(id);
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000702
hpayer@chromium.org4f99be92013-12-18 16:23:55 +0000703 // Remove the corresponding entry from the cache.
704 ScriptCache* script_cache =
705 reinterpret_cast<ScriptCache*>(data.GetParameter());
706 HashMap::Entry* entry = script_cache->Lookup(key, hash, false);
707 Object** location = reinterpret_cast<Object**>(entry->value);
708 script_cache->Remove(key, hash);
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000709 script_cache->collected_scripts_.Add(id);
710
711 // Clear the weak handle.
hpayer@chromium.org4f99be92013-12-18 16:23:55 +0000712 GlobalHandles::Destroy(location);
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000713}
714
715
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000716void Debug::SetUp(bool create_heap_objects) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000717 ThreadInit();
718 if (create_heap_objects) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000719 // Get code to handle debug break on return.
720 debug_break_return_ =
lrn@chromium.org7516f052011-03-30 08:52:27 +0000721 isolate_->builtins()->builtin(Builtins::kReturn_DebugBreak);
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000722 ASSERT(debug_break_return_->IsCode());
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000723 // Get code to handle debug break in debug break slots.
724 debug_break_slot_ =
lrn@chromium.org7516f052011-03-30 08:52:27 +0000725 isolate_->builtins()->builtin(Builtins::kSlot_DebugBreak);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000726 ASSERT(debug_break_slot_->IsCode());
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000727 }
728}
729
730
hpayer@chromium.org4f99be92013-12-18 16:23:55 +0000731void Debug::HandleWeakDebugInfo(
732 const v8::WeakCallbackData<v8::Value, void>& data) {
733 Debug* debug = reinterpret_cast<Isolate*>(data.GetIsolate())->debug();
734 DebugInfoListNode* node =
735 reinterpret_cast<DebugInfoListNode*>(data.GetParameter());
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000736 // We need to clear all breakpoints associated with the function to restore
737 // original code and avoid patching the code twice later because
738 // the function will live in the heap until next gc, and can be found by
jkummerow@chromium.org78502a92012-09-06 13:50:42 +0000739 // Debug::FindSharedFunctionInfoInScript.
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000740 BreakLocationIterator it(node->debug_info(), ALL_BREAK_LOCATIONS);
741 it.ClearAllDebugBreak();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000742 debug->RemoveDebugInfo(node->debug_info());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000743#ifdef DEBUG
hpayer@chromium.org4f99be92013-12-18 16:23:55 +0000744 for (DebugInfoListNode* n = debug->debug_info_list_;
745 n != NULL;
746 n = n->next()) {
747 ASSERT(n != node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000748 }
749#endif
750}
751
752
753DebugInfoListNode::DebugInfoListNode(DebugInfo* debug_info): next_(NULL) {
754 // Globalize the request debug info object and make it weak.
hpayer@chromium.org4f99be92013-12-18 16:23:55 +0000755 GlobalHandles* global_handles = debug_info->GetIsolate()->global_handles();
756 debug_info_ = Handle<DebugInfo>::cast(global_handles->Create(debug_info));
757 GlobalHandles::MakeWeak(reinterpret_cast<Object**>(debug_info_.location()),
758 this,
759 Debug::HandleWeakDebugInfo);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000760}
761
762
763DebugInfoListNode::~DebugInfoListNode() {
hpayer@chromium.org4f99be92013-12-18 16:23:55 +0000764 GlobalHandles::Destroy(reinterpret_cast<Object**>(debug_info_.location()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000765}
766
767
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +0000768bool Debug::CompileDebuggerScript(Isolate* isolate, int index) {
lrn@chromium.org7516f052011-03-30 08:52:27 +0000769 Factory* factory = isolate->factory();
770 HandleScope scope(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000771
kasper.lund44510672008-07-25 07:37:58 +0000772 // Bail out if the index is invalid.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000773 if (index == -1) {
774 return false;
775 }
kasper.lund44510672008-07-25 07:37:58 +0000776
777 // Find source and name for the requested script.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000778 Handle<String> source_code =
lrn@chromium.org7516f052011-03-30 08:52:27 +0000779 isolate->bootstrapper()->NativesSourceLookup(index);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000780 Vector<const char> name = Natives::GetScriptName(index);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000781 Handle<String> script_name = factory->NewStringFromAscii(name);
yangguo@chromium.org355cfd12012-08-29 15:32:24 +0000782 Handle<Context> context = isolate->native_context();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000783
784 // Compile the script.
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000785 Handle<SharedFunctionInfo> function_info;
786 function_info = Compiler::Compile(source_code,
787 script_name,
yangguo@chromium.org355cfd12012-08-29 15:32:24 +0000788 0, 0,
danno@chromium.orgd3c42102013-08-01 16:58:23 +0000789 false,
yangguo@chromium.org355cfd12012-08-29 15:32:24 +0000790 context,
791 NULL, NULL,
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000792 Handle<String>::null(),
793 NATIVES_CODE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000794
795 // Silently ignore stack overflows during compilation.
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000796 if (function_info.is_null()) {
lrn@chromium.org7516f052011-03-30 08:52:27 +0000797 ASSERT(isolate->has_pending_exception());
798 isolate->clear_pending_exception();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000799 return false;
800 }
801
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000802 // Execute the shared function in the debugger context.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000803 bool caught_exception;
kasper.lund44510672008-07-25 07:37:58 +0000804 Handle<JSFunction> function =
lrn@chromium.org7516f052011-03-30 08:52:27 +0000805 factory->NewFunctionFromSharedFunctionInfo(function_info, context);
rossberg@chromium.org717967f2011-07-20 13:44:42 +0000806
fschneider@chromium.org7d10be52012-04-10 12:30:14 +0000807 Handle<Object> exception =
ulan@chromium.org09d7ab52013-02-25 15:50:35 +0000808 Execution::TryCall(function,
809 Handle<Object>(context->global_object(), isolate),
810 0,
811 NULL,
812 &caught_exception);
kasper.lund44510672008-07-25 07:37:58 +0000813
814 // Check for caught exceptions.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000815 if (caught_exception) {
fschneider@chromium.org7d10be52012-04-10 12:30:14 +0000816 ASSERT(!isolate->has_pending_exception());
817 MessageLocation computed_location;
818 isolate->ComputeLocation(&computed_location);
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000819 Handle<Object> message = MessageHandler::MakeMessageObject(
verwaest@chromium.orgd4be0f02013-06-05 13:39:03 +0000820 isolate, "error_loading_debugger", &computed_location,
fschneider@chromium.org7d10be52012-04-10 12:30:14 +0000821 Vector<Handle<Object> >::empty(), Handle<String>(), Handle<JSArray>());
822 ASSERT(!isolate->has_pending_exception());
mmassi@chromium.org49a44672012-12-04 13:52:03 +0000823 if (!exception.is_null()) {
824 isolate->set_pending_exception(*exception);
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +0000825 MessageHandler::ReportMessage(isolate, NULL, message);
mmassi@chromium.org49a44672012-12-04 13:52:03 +0000826 isolate->clear_pending_exception();
827 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000828 return false;
829 }
830
kasper.lund44510672008-07-25 07:37:58 +0000831 // Mark this script as native and return successfully.
832 Handle<Script> script(Script::cast(function->shared()->script()));
ager@chromium.orge2902be2009-06-08 12:21:35 +0000833 script->set_type(Smi::FromInt(Script::TYPE_NATIVE));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000834 return true;
835}
836
837
838bool Debug::Load() {
839 // Return if debugger is already loaded.
kasper.lund212ac232008-07-16 07:07:30 +0000840 if (IsLoaded()) return true;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000841
lrn@chromium.org7516f052011-03-30 08:52:27 +0000842 Debugger* debugger = isolate_->debugger();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000843
kasper.lund44510672008-07-25 07:37:58 +0000844 // Bail out if we're already in the process of compiling the native
845 // JavaScript source code for the debugger.
lrn@chromium.org7516f052011-03-30 08:52:27 +0000846 if (debugger->compiling_natives() ||
847 debugger->is_loading_debugger())
mads.s.agercbaa0602008-08-14 13:41:48 +0000848 return false;
lrn@chromium.org7516f052011-03-30 08:52:27 +0000849 debugger->set_loading_debugger(true);
kasper.lund44510672008-07-25 07:37:58 +0000850
851 // Disable breakpoints and interrupts while compiling and running the
852 // debugger scripts including the context creation code.
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +0000853 DisableBreak disable(isolate_, true);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000854 PostponeInterruptsScope postpone(isolate_);
kasper.lund44510672008-07-25 07:37:58 +0000855
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000856 // Create the debugger context.
lrn@chromium.org7516f052011-03-30 08:52:27 +0000857 HandleScope scope(isolate_);
kasper.lund44510672008-07-25 07:37:58 +0000858 Handle<Context> context =
lrn@chromium.org7516f052011-03-30 08:52:27 +0000859 isolate_->bootstrapper()->CreateEnvironment(
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000860 Handle<Object>::null(),
861 v8::Handle<ObjectTemplate>(),
862 NULL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000863
fschneider@chromium.org7d10be52012-04-10 12:30:14 +0000864 // Fail if no context could be created.
865 if (context.is_null()) return false;
866
kasper.lund44510672008-07-25 07:37:58 +0000867 // Use the debugger context.
lrn@chromium.org7516f052011-03-30 08:52:27 +0000868 SaveContext save(isolate_);
869 isolate_->set_context(*context);
kasper.lund44510672008-07-25 07:37:58 +0000870
871 // Expose the builtins object in the debugger context.
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000872 Handle<String> key = isolate_->factory()->InternalizeOneByteString(
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +0000873 STATIC_ASCII_VECTOR("builtins"));
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000874 Handle<GlobalObject> global = Handle<GlobalObject>(context->global_object());
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +0000875 RETURN_IF_EMPTY_HANDLE_VALUE(
lrn@chromium.org7516f052011-03-30 08:52:27 +0000876 isolate_,
ulan@chromium.org09d7ab52013-02-25 15:50:35 +0000877 JSReceiver::SetProperty(global,
878 key,
879 Handle<Object>(global->builtins(), isolate_),
880 NONE,
881 kNonStrictMode),
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +0000882 false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000883
884 // Compile the JavaScript for the debugger in the debugger context.
lrn@chromium.org7516f052011-03-30 08:52:27 +0000885 debugger->set_compiling_natives(true);
kasper.lund44510672008-07-25 07:37:58 +0000886 bool caught_exception =
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +0000887 !CompileDebuggerScript(isolate_, Natives::GetIndex("mirror")) ||
888 !CompileDebuggerScript(isolate_, Natives::GetIndex("debug"));
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000889
890 if (FLAG_enable_liveedit) {
891 caught_exception = caught_exception ||
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +0000892 !CompileDebuggerScript(isolate_, Natives::GetIndex("liveedit"));
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000893 }
894
lrn@chromium.org7516f052011-03-30 08:52:27 +0000895 debugger->set_compiling_natives(false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000896
mads.s.agercbaa0602008-08-14 13:41:48 +0000897 // Make sure we mark the debugger as not loading before we might
898 // return.
lrn@chromium.org7516f052011-03-30 08:52:27 +0000899 debugger->set_loading_debugger(false);
mads.s.agercbaa0602008-08-14 13:41:48 +0000900
kasper.lund44510672008-07-25 07:37:58 +0000901 // Check for caught exceptions.
902 if (caught_exception) return false;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000903
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000904 // Debugger loaded, create debugger context global handle.
905 debug_context_ = Handle<Context>::cast(
906 isolate_->global_handles()->Create(*context));
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000907
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000908 return true;
909}
910
911
912void Debug::Unload() {
913 // Return debugger is not loaded.
914 if (!IsLoaded()) {
915 return;
916 }
917
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000918 // Clear the script cache.
919 DestroyScriptCache();
920
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000921 // Clear debugger context global handle.
hpayer@chromium.org4f99be92013-12-18 16:23:55 +0000922 GlobalHandles::Destroy(reinterpret_cast<Object**>(debug_context_.location()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000923 debug_context_ = Handle<Context>();
924}
925
926
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000927// Set the flag indicating that preemption happened during debugging.
928void Debug::PreemptionWhileInDebugger() {
929 ASSERT(InDebugger());
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000930 Debug::set_interrupts_pending(PREEMPT);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000931}
932
933
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000934void Debug::Iterate(ObjectVisitor* v) {
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000935 v->VisitPointer(BitCast<Object**>(&(debug_break_return_)));
936 v->VisitPointer(BitCast<Object**>(&(debug_break_slot_)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000937}
938
939
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000940Object* Debug::Break(Arguments args) {
941 Heap* heap = isolate_->heap();
942 HandleScope scope(isolate_);
mads.s.ager31e71382008-08-13 09:32:07 +0000943 ASSERT(args.length() == 0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000944
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000945 thread_local_.frame_drop_mode_ = FRAMES_UNTOUCHED;
ager@chromium.org357bf652010-04-12 11:30:10 +0000946
kasper.lundbd3ec4e2008-07-09 11:06:54 +0000947 // Get the top-most JavaScript frame.
vegorov@chromium.org74f333b2011-04-06 11:17:46 +0000948 JavaScriptFrameIterator it(isolate_);
kasper.lundbd3ec4e2008-07-09 11:06:54 +0000949 JavaScriptFrame* frame = it.frame();
950
951 // Just continue if breaks are disabled or debugger cannot be loaded.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000952 if (disable_break() || !Load()) {
953 SetAfterBreakTarget(frame);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000954 return heap->undefined_value();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000955 }
956
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000957 // Enter the debugger.
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +0000958 EnterDebugger debugger(isolate_);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000959 if (debugger.FailedToEnter()) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000960 return heap->undefined_value();
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000961 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000962
kasper.lund44510672008-07-25 07:37:58 +0000963 // Postpone interrupt during breakpoint processing.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000964 PostponeInterruptsScope postpone(isolate_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000965
966 // Get the debug info (create it if it does not exist).
967 Handle<SharedFunctionInfo> shared =
danno@chromium.org169691d2013-07-15 08:01:13 +0000968 Handle<SharedFunctionInfo>(frame->function()->shared());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000969 Handle<DebugInfo> debug_info = GetDebugInfo(shared);
970
971 // Find the break point where execution has stopped.
972 BreakLocationIterator break_location_iterator(debug_info,
973 ALL_BREAK_LOCATIONS);
verwaest@chromium.org8a00e822013-06-10 15:11:22 +0000974 // pc points to the instruction after the current one, possibly a break
975 // location as well. So the "- 1" to exclude it from the search.
976 break_location_iterator.FindBreakLocationFromAddress(frame->pc() - 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000977
978 // Check whether step next reached a new statement.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000979 if (!StepNextContinue(&break_location_iterator, frame)) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000980 // Decrease steps left if performing multiple steps.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000981 if (thread_local_.step_count_ > 0) {
982 thread_local_.step_count_--;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000983 }
984 }
985
986 // If there is one or more real break points check whether any of these are
987 // triggered.
ulan@chromium.org09d7ab52013-02-25 15:50:35 +0000988 Handle<Object> break_points_hit(heap->undefined_value(), isolate_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000989 if (break_location_iterator.HasBreakPoint()) {
990 Handle<Object> break_point_objects =
ulan@chromium.org09d7ab52013-02-25 15:50:35 +0000991 Handle<Object>(break_location_iterator.BreakPointObjects(), isolate_);
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000992 break_points_hit = CheckBreakPoints(break_point_objects);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000993 }
994
ager@chromium.orga1645e22009-09-09 19:27:10 +0000995 // If step out is active skip everything until the frame where we need to step
996 // out to is reached, unless real breakpoint is hit.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000997 if (StepOutActive() && frame->fp() != step_out_fp() &&
ager@chromium.orga1645e22009-09-09 19:27:10 +0000998 break_points_hit->IsUndefined() ) {
999 // Step count should always be 0 for StepOut.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001000 ASSERT(thread_local_.step_count_ == 0);
ager@chromium.orga1645e22009-09-09 19:27:10 +00001001 } else if (!break_points_hit->IsUndefined() ||
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001002 (thread_local_.last_step_action_ != StepNone &&
1003 thread_local_.step_count_ == 0)) {
ager@chromium.orga1645e22009-09-09 19:27:10 +00001004 // Notify debugger if a real break point is triggered or if performing
1005 // single stepping with no more steps to perform. Otherwise do another step.
1006
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001007 // Clear all current stepping setup.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001008 ClearStepping();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001009
lrn@chromium.org34e60782011-09-15 07:25:40 +00001010 if (thread_local_.queued_step_count_ > 0) {
1011 // Perform queued steps
1012 int step_count = thread_local_.queued_step_count_;
1013
1014 // Clear queue
1015 thread_local_.queued_step_count_ = 0;
1016
dslomov@chromium.org639bac02013-09-09 11:58:54 +00001017 PrepareStep(StepNext, step_count, StackFrame::NO_ID);
lrn@chromium.org34e60782011-09-15 07:25:40 +00001018 } else {
1019 // Notify the debug event listeners.
1020 isolate_->debugger()->OnDebugBreak(break_points_hit, false);
1021 }
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001022 } else if (thread_local_.last_step_action_ != StepNone) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001023 // Hold on to last step action as it is cleared by the call to
1024 // ClearStepping.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001025 StepAction step_action = thread_local_.last_step_action_;
1026 int step_count = thread_local_.step_count_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001027
lrn@chromium.org34e60782011-09-15 07:25:40 +00001028 // If StepNext goes deeper in code, StepOut until original frame
1029 // and keep step count queued up in the meantime.
1030 if (step_action == StepNext && frame->fp() < thread_local_.last_fp_) {
1031 // Count frames until target frame
1032 int count = 0;
1033 JavaScriptFrameIterator it(isolate_);
jkummerow@chromium.org212d9642012-05-11 15:02:09 +00001034 while (!it.done() && it.frame()->fp() < thread_local_.last_fp_) {
lrn@chromium.org34e60782011-09-15 07:25:40 +00001035 count++;
1036 it.Advance();
1037 }
1038
danno@chromium.org81cac2b2012-07-10 11:28:27 +00001039 // Check that we indeed found the frame we are looking for.
1040 CHECK(!it.done() && (it.frame()->fp() == thread_local_.last_fp_));
1041 if (step_count > 1) {
1042 // Save old count and action to continue stepping after StepOut.
1043 thread_local_.queued_step_count_ = step_count - 1;
jkummerow@chromium.org212d9642012-05-11 15:02:09 +00001044 }
1045
danno@chromium.org81cac2b2012-07-10 11:28:27 +00001046 // Set up for StepOut to reach target frame.
1047 step_action = StepOut;
1048 step_count = count;
lrn@chromium.org34e60782011-09-15 07:25:40 +00001049 }
1050
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001051 // Clear all current stepping setup.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001052 ClearStepping();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001053
1054 // Set up for the remaining steps.
dslomov@chromium.org639bac02013-09-09 11:58:54 +00001055 PrepareStep(step_action, step_count, StackFrame::NO_ID);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001056 }
1057
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001058 if (thread_local_.frame_drop_mode_ == FRAMES_UNTOUCHED) {
1059 SetAfterBreakTarget(frame);
1060 } else if (thread_local_.frame_drop_mode_ ==
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001061 FRAME_DROPPED_IN_IC_CALL) {
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001062 // We must have been calling IC stub. Do not go there anymore.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001063 Code* plain_return = isolate_->builtins()->builtin(
1064 Builtins::kPlainReturn_LiveEdit);
1065 thread_local_.after_break_target_ = plain_return->entry();
1066 } else if (thread_local_.frame_drop_mode_ ==
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001067 FRAME_DROPPED_IN_DEBUG_SLOT_CALL) {
1068 // Debug break slot stub does not return normally, instead it manually
1069 // cleans the stack and jumps. We should patch the jump address.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001070 Code* plain_return = isolate_->builtins()->builtin(
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00001071 Builtins::kFrameDropper_LiveEdit);
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001072 thread_local_.after_break_target_ = plain_return->entry();
1073 } else if (thread_local_.frame_drop_mode_ ==
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001074 FRAME_DROPPED_IN_DIRECT_CALL) {
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001075 // Nothing to do, after_break_target is not used here.
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00001076 } else if (thread_local_.frame_drop_mode_ ==
1077 FRAME_DROPPED_IN_RETURN_CALL) {
1078 Code* plain_return = isolate_->builtins()->builtin(
1079 Builtins::kFrameDropper_LiveEdit);
1080 thread_local_.after_break_target_ = plain_return->entry();
ager@chromium.org357bf652010-04-12 11:30:10 +00001081 } else {
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001082 UNREACHABLE();
ager@chromium.org357bf652010-04-12 11:30:10 +00001083 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001084
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001085 return heap->undefined_value();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001086}
1087
1088
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001089RUNTIME_FUNCTION(Object*, Debug_Break) {
1090 return isolate->debug()->Break(args);
1091}
1092
1093
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001094// Check the break point objects for whether one or more are actually
1095// triggered. This function returns a JSArray with the break point objects
1096// which is triggered.
1097Handle<Object> Debug::CheckBreakPoints(Handle<Object> break_point_objects) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00001098 Factory* factory = isolate_->factory();
1099
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001100 // Count the number of break points hit. If there are multiple break points
1101 // they are in a FixedArray.
1102 Handle<FixedArray> break_points_hit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001103 int break_points_hit_count = 0;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001104 ASSERT(!break_point_objects->IsUndefined());
1105 if (break_point_objects->IsFixedArray()) {
1106 Handle<FixedArray> array(FixedArray::cast(*break_point_objects));
lrn@chromium.org7516f052011-03-30 08:52:27 +00001107 break_points_hit = factory->NewFixedArray(array->length());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001108 for (int i = 0; i < array->length(); i++) {
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00001109 Handle<Object> o(array->get(i), isolate_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001110 if (CheckBreakPoint(o)) {
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001111 break_points_hit->set(break_points_hit_count++, *o);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001112 }
1113 }
1114 } else {
lrn@chromium.org7516f052011-03-30 08:52:27 +00001115 break_points_hit = factory->NewFixedArray(1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001116 if (CheckBreakPoint(break_point_objects)) {
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001117 break_points_hit->set(break_points_hit_count++, *break_point_objects);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001118 }
1119 }
1120
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001121 // Return undefined if no break points were triggered.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001122 if (break_points_hit_count == 0) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00001123 return factory->undefined_value();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001124 }
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001125 // Return break points hit as a JSArray.
lrn@chromium.org7516f052011-03-30 08:52:27 +00001126 Handle<JSArray> result = factory->NewJSArrayWithElements(break_points_hit);
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001127 result->set_length(Smi::FromInt(break_points_hit_count));
1128 return result;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001129}
1130
1131
1132// Check whether a single break point object is triggered.
1133bool Debug::CheckBreakPoint(Handle<Object> break_point_object) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00001134 Factory* factory = isolate_->factory();
1135 HandleScope scope(isolate_);
ager@chromium.org381abbb2009-02-25 13:23:22 +00001136
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001137 // Ignore check if break point object is not a JSObject.
1138 if (!break_point_object->IsJSObject()) return true;
1139
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001140 // Get the function IsBreakPointTriggered (defined in debug-debugger.js).
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001141 Handle<String> is_break_point_triggered_string =
1142 factory->InternalizeOneByteString(
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00001143 STATIC_ASCII_VECTOR("IsBreakPointTriggered"));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001144 Handle<JSFunction> check_break_point =
1145 Handle<JSFunction>(JSFunction::cast(
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00001146 debug_context()->global_object()->GetPropertyNoExceptionThrown(
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001147 *is_break_point_triggered_string)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001148
1149 // Get the break id as an object.
lrn@chromium.org7516f052011-03-30 08:52:27 +00001150 Handle<Object> break_id = factory->NewNumberFromInt(Debug::break_id());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001151
1152 // Call HandleBreakPointx.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001153 bool caught_exception;
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00001154 Handle<Object> argv[] = { break_id, break_point_object };
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001155 Handle<Object> result = Execution::TryCall(check_break_point,
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00001156 isolate_->js_builtins_object(),
1157 ARRAY_SIZE(argv),
1158 argv,
1159 &caught_exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001160
1161 // If exception or non boolean result handle as not triggered
1162 if (caught_exception || !result->IsBoolean()) {
1163 return false;
1164 }
1165
1166 // Return whether the break point is triggered.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001167 ASSERT(!result.is_null());
1168 return (*result)->IsTrue();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001169}
1170
1171
1172// Check whether the function has debug information.
1173bool Debug::HasDebugInfo(Handle<SharedFunctionInfo> shared) {
1174 return !shared->debug_info()->IsUndefined();
1175}
1176
1177
kasper.lundbd3ec4e2008-07-09 11:06:54 +00001178// Return the debug info for this function. EnsureDebugInfo must be called
1179// prior to ensure the debug info has been generated for shared.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001180Handle<DebugInfo> Debug::GetDebugInfo(Handle<SharedFunctionInfo> shared) {
kasper.lundbd3ec4e2008-07-09 11:06:54 +00001181 ASSERT(HasDebugInfo(shared));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001182 return Handle<DebugInfo>(DebugInfo::cast(shared->debug_info()));
1183}
1184
1185
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001186void Debug::SetBreakPoint(Handle<JSFunction> function,
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00001187 Handle<Object> break_point_object,
1188 int* source_position) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00001189 HandleScope scope(isolate_);
ager@chromium.org381abbb2009-02-25 13:23:22 +00001190
lrn@chromium.org34e60782011-09-15 07:25:40 +00001191 PrepareForBreakPoints();
1192
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001193 // Make sure the function is compiled and has set up the debug info.
1194 Handle<SharedFunctionInfo> shared(function->shared());
1195 if (!EnsureDebugInfo(shared, function)) {
kasper.lundbd3ec4e2008-07-09 11:06:54 +00001196 // Return if retrieving debug info failed.
1197 return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001198 }
1199
kasper.lundbd3ec4e2008-07-09 11:06:54 +00001200 Handle<DebugInfo> debug_info = GetDebugInfo(shared);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001201 // Source positions starts with zero.
danno@chromium.orgbf0c8202011-12-27 10:09:42 +00001202 ASSERT(*source_position >= 0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001203
1204 // Find the break point and change it.
1205 BreakLocationIterator it(debug_info, SOURCE_BREAK_LOCATIONS);
jkummerow@chromium.org93a47f42013-07-02 14:43:41 +00001206 it.FindBreakLocationFromPosition(*source_position, STATEMENT_ALIGNED);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001207 it.SetBreakPoint(break_point_object);
1208
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00001209 *source_position = it.position();
1210
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001211 // At least one active break point now.
1212 ASSERT(debug_info->GetBreakPointCount() > 0);
1213}
1214
1215
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001216bool Debug::SetBreakPointForScript(Handle<Script> script,
1217 Handle<Object> break_point_object,
jkummerow@chromium.org93a47f42013-07-02 14:43:41 +00001218 int* source_position,
1219 BreakPositionAlignment alignment) {
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001220 HandleScope scope(isolate_);
1221
jkummerow@chromium.org78502a92012-09-06 13:50:42 +00001222 PrepareForBreakPoints();
1223
1224 // Obtain shared function info for the function.
1225 Object* result = FindSharedFunctionInfoInScript(script, *source_position);
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001226 if (result->IsUndefined()) return false;
1227
1228 // Make sure the function has set up the debug info.
1229 Handle<SharedFunctionInfo> shared(SharedFunctionInfo::cast(result));
1230 if (!EnsureDebugInfo(shared, Handle<JSFunction>::null())) {
1231 // Return if retrieving debug info failed.
1232 return false;
1233 }
1234
1235 // Find position within function. The script position might be before the
1236 // source position of the first function.
1237 int position;
1238 if (shared->start_position() > *source_position) {
1239 position = 0;
1240 } else {
1241 position = *source_position - shared->start_position();
1242 }
1243
1244 Handle<DebugInfo> debug_info = GetDebugInfo(shared);
1245 // Source positions starts with zero.
1246 ASSERT(position >= 0);
1247
1248 // Find the break point and change it.
1249 BreakLocationIterator it(debug_info, SOURCE_BREAK_LOCATIONS);
jkummerow@chromium.org93a47f42013-07-02 14:43:41 +00001250 it.FindBreakLocationFromPosition(position, alignment);
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001251 it.SetBreakPoint(break_point_object);
1252
1253 *source_position = it.position() + shared->start_position();
1254
1255 // At least one active break point now.
1256 ASSERT(debug_info->GetBreakPointCount() > 0);
1257 return true;
1258}
1259
1260
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001261void Debug::ClearBreakPoint(Handle<Object> break_point_object) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00001262 HandleScope scope(isolate_);
ager@chromium.org381abbb2009-02-25 13:23:22 +00001263
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001264 DebugInfoListNode* node = debug_info_list_;
1265 while (node != NULL) {
1266 Object* result = DebugInfo::FindBreakPointInfo(node->debug_info(),
1267 break_point_object);
1268 if (!result->IsUndefined()) {
1269 // Get information in the break point.
1270 BreakPointInfo* break_point_info = BreakPointInfo::cast(result);
1271 Handle<DebugInfo> debug_info = node->debug_info();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001272
1273 // Find the break point and clear it.
1274 BreakLocationIterator it(debug_info, SOURCE_BREAK_LOCATIONS);
verwaest@chromium.org8a00e822013-06-10 15:11:22 +00001275 it.FindBreakLocationFromAddress(debug_info->code()->entry() +
1276 break_point_info->code_position()->value());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001277 it.ClearBreakPoint(break_point_object);
1278
1279 // If there are no more break points left remove the debug info for this
1280 // function.
1281 if (debug_info->GetBreakPointCount() == 0) {
1282 RemoveDebugInfo(debug_info);
1283 }
1284
1285 return;
1286 }
1287 node = node->next();
1288 }
1289}
1290
1291
ager@chromium.org381abbb2009-02-25 13:23:22 +00001292void Debug::ClearAllBreakPoints() {
1293 DebugInfoListNode* node = debug_info_list_;
1294 while (node != NULL) {
1295 // Remove all debug break code.
1296 BreakLocationIterator it(node->debug_info(), ALL_BREAK_LOCATIONS);
1297 it.ClearAllDebugBreak();
1298 node = node->next();
1299 }
1300
1301 // Remove all debug info.
1302 while (debug_info_list_ != NULL) {
1303 RemoveDebugInfo(debug_info_list_->debug_info());
1304 }
1305}
1306
1307
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001308void Debug::FloodWithOneShot(Handle<JSFunction> function) {
lrn@chromium.org34e60782011-09-15 07:25:40 +00001309 PrepareForBreakPoints();
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001310
1311 // Make sure the function is compiled and has set up the debug info.
1312 Handle<SharedFunctionInfo> shared(function->shared());
1313 if (!EnsureDebugInfo(shared, function)) {
kasper.lundbd3ec4e2008-07-09 11:06:54 +00001314 // Return if we failed to retrieve the debug info.
1315 return;
1316 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001317
1318 // Flood the function with break points.
kasper.lundbd3ec4e2008-07-09 11:06:54 +00001319 BreakLocationIterator it(GetDebugInfo(shared), ALL_BREAK_LOCATIONS);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001320 while (!it.Done()) {
1321 it.SetOneShot();
1322 it.Next();
1323 }
1324}
1325
1326
rossberg@chromium.org2c067b12012-03-19 11:01:52 +00001327void Debug::FloodBoundFunctionWithOneShot(Handle<JSFunction> function) {
1328 Handle<FixedArray> new_bindings(function->function_bindings());
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00001329 Handle<Object> bindee(new_bindings->get(JSFunction::kBoundFunctionIndex),
1330 isolate_);
rossberg@chromium.org2c067b12012-03-19 11:01:52 +00001331
1332 if (!bindee.is_null() && bindee->IsJSFunction() &&
1333 !JSFunction::cast(*bindee)->IsBuiltin()) {
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001334 Handle<JSFunction> bindee_function(JSFunction::cast(*bindee));
1335 Debug::FloodWithOneShot(bindee_function);
rossberg@chromium.org2c067b12012-03-19 11:01:52 +00001336 }
1337}
1338
1339
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001340void Debug::FloodHandlerWithOneShot() {
ager@chromium.org8bb60582008-12-11 12:02:20 +00001341 // Iterate through the JavaScript stack looking for handlers.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001342 StackFrame::Id id = break_frame_id();
ager@chromium.org8bb60582008-12-11 12:02:20 +00001343 if (id == StackFrame::NO_ID) {
1344 // If there is no JavaScript stack don't do anything.
1345 return;
1346 }
vegorov@chromium.org74f333b2011-04-06 11:17:46 +00001347 for (JavaScriptFrameIterator it(isolate_, id); !it.done(); it.Advance()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001348 JavaScriptFrame* frame = it.frame();
1349 if (frame->HasHandler()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001350 // Flood the function with the catch block with break points
danno@chromium.org169691d2013-07-15 08:01:13 +00001351 FloodWithOneShot(Handle<JSFunction>(frame->function()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001352 return;
1353 }
1354 }
1355}
1356
1357
1358void Debug::ChangeBreakOnException(ExceptionBreakType type, bool enable) {
1359 if (type == BreakUncaughtException) {
1360 break_on_uncaught_exception_ = enable;
1361 } else {
1362 break_on_exception_ = enable;
1363 }
1364}
1365
1366
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00001367bool Debug::IsBreakOnException(ExceptionBreakType type) {
1368 if (type == BreakUncaughtException) {
1369 return break_on_uncaught_exception_;
1370 } else {
1371 return break_on_exception_;
1372 }
1373}
1374
1375
dslomov@chromium.org639bac02013-09-09 11:58:54 +00001376void Debug::PrepareStep(StepAction step_action,
1377 int step_count,
1378 StackFrame::Id frame_id) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00001379 HandleScope scope(isolate_);
lrn@chromium.org34e60782011-09-15 07:25:40 +00001380
1381 PrepareForBreakPoints();
1382
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001383 ASSERT(Debug::InDebugger());
1384
1385 // Remember this step action and count.
1386 thread_local_.last_step_action_ = step_action;
ager@chromium.orga1645e22009-09-09 19:27:10 +00001387 if (step_action == StepOut) {
1388 // For step out target frame will be found on the stack so there is no need
1389 // to set step counter for it. It's expected to always be 0 for StepOut.
1390 thread_local_.step_count_ = 0;
1391 } else {
1392 thread_local_.step_count_ = step_count;
1393 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001394
1395 // Get the frame where the execution has stopped and skip the debug frame if
1396 // any. The debug frame will only be present if execution was stopped due to
1397 // hitting a break point. In other situations (e.g. unhandled exception) the
1398 // debug frame is not present.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001399 StackFrame::Id id = break_frame_id();
ager@chromium.org8bb60582008-12-11 12:02:20 +00001400 if (id == StackFrame::NO_ID) {
1401 // If there is no JavaScript stack don't do anything.
1402 return;
1403 }
dslomov@chromium.org639bac02013-09-09 11:58:54 +00001404 if (frame_id != StackFrame::NO_ID) {
1405 id = frame_id;
1406 }
vegorov@chromium.org74f333b2011-04-06 11:17:46 +00001407 JavaScriptFrameIterator frames_it(isolate_, id);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001408 JavaScriptFrame* frame = frames_it.frame();
1409
1410 // First of all ensure there is one-shot break points in the top handler
1411 // if any.
1412 FloodHandlerWithOneShot();
1413
1414 // If the function on the top frame is unresolved perform step out. This will
1415 // be the case when calling unknown functions and having the debugger stopped
1416 // in an unhandled exception.
1417 if (!frame->function()->IsJSFunction()) {
1418 // Step out: Find the calling JavaScript frame and flood it with
1419 // breakpoints.
1420 frames_it.Advance();
1421 // Fill the function to return to with one-shot break points.
danno@chromium.org169691d2013-07-15 08:01:13 +00001422 JSFunction* function = frames_it.frame()->function();
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001423 FloodWithOneShot(Handle<JSFunction>(function));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001424 return;
1425 }
1426
1427 // Get the debug info (create it if it does not exist).
danno@chromium.org169691d2013-07-15 08:01:13 +00001428 Handle<JSFunction> function(frame->function());
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001429 Handle<SharedFunctionInfo> shared(function->shared());
1430 if (!EnsureDebugInfo(shared, function)) {
kasper.lundbd3ec4e2008-07-09 11:06:54 +00001431 // Return if ensuring debug info failed.
1432 return;
1433 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001434 Handle<DebugInfo> debug_info = GetDebugInfo(shared);
1435
1436 // Find the break location where execution has stopped.
1437 BreakLocationIterator it(debug_info, ALL_BREAK_LOCATIONS);
verwaest@chromium.org8a00e822013-06-10 15:11:22 +00001438 // pc points to the instruction after the current one, possibly a break
1439 // location as well. So the "- 1" to exclude it from the search.
1440 it.FindBreakLocationFromAddress(frame->pc() - 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001441
1442 // Compute whether or not the target is a call target.
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001443 bool is_load_or_store = false;
1444 bool is_inline_cache_stub = false;
whesse@chromium.orge90029b2010-08-02 11:52:17 +00001445 bool is_at_restarted_function = false;
ager@chromium.orga1645e22009-09-09 19:27:10 +00001446 Handle<Code> call_function_stub;
ager@chromium.orga1645e22009-09-09 19:27:10 +00001447
whesse@chromium.orge90029b2010-08-02 11:52:17 +00001448 if (thread_local_.restarter_frame_function_pointer_ == NULL) {
1449 if (RelocInfo::IsCodeTarget(it.rinfo()->rmode())) {
1450 bool is_call_target = false;
1451 Address target = it.rinfo()->target_address();
1452 Code* code = Code::GetCodeFromTargetAddress(target);
1453 if (code->is_call_stub() || code->is_keyed_call_stub()) {
1454 is_call_target = true;
1455 }
1456 if (code->is_inline_cache_stub()) {
1457 is_inline_cache_stub = true;
1458 is_load_or_store = !is_call_target;
1459 }
1460
1461 // Check if target code is CallFunction stub.
1462 Code* maybe_call_function_stub = code;
1463 // If there is a breakpoint at this line look at the original code to
1464 // check if it is a CallFunction stub.
1465 if (it.IsDebugBreak()) {
1466 Address original_target = it.original_rinfo()->target_address();
1467 maybe_call_function_stub =
1468 Code::GetCodeFromTargetAddress(original_target);
1469 }
1470 if (maybe_call_function_stub->kind() == Code::STUB &&
1471 maybe_call_function_stub->major_key() == CodeStub::CallFunction) {
1472 // Save reference to the code as we may need it to find out arguments
1473 // count for 'step in' later.
1474 call_function_stub = Handle<Code>(maybe_call_function_stub);
1475 }
ager@chromium.orga1645e22009-09-09 19:27:10 +00001476 }
whesse@chromium.orge90029b2010-08-02 11:52:17 +00001477 } else {
1478 is_at_restarted_function = true;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001479 }
1480
v8.team.kasperl727e9952008-09-02 14:56:44 +00001481 // If this is the last break code target step out is the only possibility.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001482 if (it.IsExit() || step_action == StepOut) {
ager@chromium.orga1645e22009-09-09 19:27:10 +00001483 if (step_action == StepOut) {
1484 // Skip step_count frames starting with the current one.
1485 while (step_count-- > 0 && !frames_it.done()) {
1486 frames_it.Advance();
1487 }
1488 } else {
1489 ASSERT(it.IsExit());
1490 frames_it.Advance();
1491 }
1492 // Skip builtin functions on the stack.
danno@chromium.org169691d2013-07-15 08:01:13 +00001493 while (!frames_it.done() && frames_it.frame()->function()->IsBuiltin()) {
ager@chromium.orga1645e22009-09-09 19:27:10 +00001494 frames_it.Advance();
1495 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001496 // Step out: If there is a JavaScript caller frame, we need to
1497 // flood it with breakpoints.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001498 if (!frames_it.done()) {
1499 // Fill the function to return to with one-shot break points.
danno@chromium.org169691d2013-07-15 08:01:13 +00001500 JSFunction* function = frames_it.frame()->function();
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001501 FloodWithOneShot(Handle<JSFunction>(function));
ager@chromium.orga1645e22009-09-09 19:27:10 +00001502 // Set target frame pointer.
1503 ActivateStepOut(frames_it.frame());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001504 }
ager@chromium.orga1645e22009-09-09 19:27:10 +00001505 } else if (!(is_inline_cache_stub || RelocInfo::IsConstructCall(it.rmode()) ||
whesse@chromium.orge90029b2010-08-02 11:52:17 +00001506 !call_function_stub.is_null() || is_at_restarted_function)
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001507 || step_action == StepNext || step_action == StepMin) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001508 // Step next or step min.
1509
1510 // Fill the current function with one-shot break points.
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001511 FloodWithOneShot(function);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001512
1513 // Remember source position and frame to handle step next.
1514 thread_local_.last_statement_position_ =
1515 debug_info->code()->SourceStatementPosition(frame->pc());
mmassi@chromium.org7028c052012-06-13 11:51:58 +00001516 thread_local_.last_fp_ = frame->UnpaddedFP();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001517 } else {
whesse@chromium.orge90029b2010-08-02 11:52:17 +00001518 // If there's restarter frame on top of the stack, just get the pointer
1519 // to function which is going to be restarted.
1520 if (is_at_restarted_function) {
1521 Handle<JSFunction> restarted_function(
1522 JSFunction::cast(*thread_local_.restarter_frame_function_pointer_));
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001523 FloodWithOneShot(restarted_function);
whesse@chromium.orge90029b2010-08-02 11:52:17 +00001524 } else if (!call_function_stub.is_null()) {
1525 // If it's CallFunction stub ensure target function is compiled and flood
1526 // it with one shot breakpoints.
1527
ager@chromium.orga1645e22009-09-09 19:27:10 +00001528 // Find out number of arguments from the stub minor key.
1529 // Reverse lookup required as the minor key cannot be retrieved
1530 // from the code object.
1531 Handle<Object> obj(
lrn@chromium.org7516f052011-03-30 08:52:27 +00001532 isolate_->heap()->code_stubs()->SlowReverseLookup(
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00001533 *call_function_stub),
1534 isolate_);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001535 ASSERT(!obj.is_null());
1536 ASSERT(!(*obj)->IsUndefined());
ager@chromium.orga1645e22009-09-09 19:27:10 +00001537 ASSERT(obj->IsSmi());
1538 // Get the STUB key and extract major and minor key.
1539 uint32_t key = Smi::cast(*obj)->value();
1540 // Argc in the stub is the number of arguments passed - not the
1541 // expected arguments of the called function.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001542 int call_function_arg_count =
1543 CallFunctionStub::ExtractArgcFromMinorKey(
1544 CodeStub::MinorKeyFromKey(key));
ager@chromium.orga1645e22009-09-09 19:27:10 +00001545 ASSERT(call_function_stub->major_key() ==
1546 CodeStub::MajorKeyFromKey(key));
1547
1548 // Find target function on the expression stack.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001549 // Expression stack looks like this (top to bottom):
ager@chromium.orga1645e22009-09-09 19:27:10 +00001550 // argN
1551 // ...
1552 // arg0
1553 // Receiver
1554 // Function to call
1555 int expressions_count = frame->ComputeExpressionsCount();
1556 ASSERT(expressions_count - 2 - call_function_arg_count >= 0);
1557 Object* fun = frame->GetExpression(
1558 expressions_count - 2 - call_function_arg_count);
1559 if (fun->IsJSFunction()) {
1560 Handle<JSFunction> js_function(JSFunction::cast(fun));
rossberg@chromium.org2c067b12012-03-19 11:01:52 +00001561 if (js_function->shared()->bound()) {
1562 Debug::FloodBoundFunctionWithOneShot(js_function);
1563 } else if (!js_function->IsBuiltin()) {
1564 // Don't step into builtins.
ager@chromium.orga1645e22009-09-09 19:27:10 +00001565 // It will also compile target function if it's not compiled yet.
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001566 FloodWithOneShot(js_function);
ager@chromium.orga1645e22009-09-09 19:27:10 +00001567 }
1568 }
1569 }
1570
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001571 // Fill the current function with one-shot break points even for step in on
1572 // a call target as the function called might be a native function for
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001573 // which step in will not stop. It also prepares for stepping in
1574 // getters/setters.
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001575 FloodWithOneShot(function);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001576
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001577 if (is_load_or_store) {
1578 // Remember source position and frame to handle step in getter/setter. If
1579 // there is a custom getter/setter it will be handled in
1580 // Object::Get/SetPropertyWithCallback, otherwise the step action will be
1581 // propagated on the next Debug::Break.
1582 thread_local_.last_statement_position_ =
1583 debug_info->code()->SourceStatementPosition(frame->pc());
mmassi@chromium.org7028c052012-06-13 11:51:58 +00001584 thread_local_.last_fp_ = frame->UnpaddedFP();
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001585 }
1586
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001587 // Step in or Step in min
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00001588 it.PrepareStepIn(isolate_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001589 ActivateStepIn(frame);
1590 }
1591}
1592
1593
1594// Check whether the current debug break should be reported to the debugger. It
1595// is used to have step next and step in only report break back to the debugger
1596// if on a different frame or in a different statement. In some situations
1597// there will be several break points in the same statement when the code is
v8.team.kasperl727e9952008-09-02 14:56:44 +00001598// flooded with one-shot break points. This function helps to perform several
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001599// steps before reporting break back to the debugger.
1600bool Debug::StepNextContinue(BreakLocationIterator* break_location_iterator,
1601 JavaScriptFrame* frame) {
lrn@chromium.org34e60782011-09-15 07:25:40 +00001602 // StepNext and StepOut shouldn't bring us deeper in code, so last frame
1603 // shouldn't be a parent of current frame.
1604 if (thread_local_.last_step_action_ == StepNext ||
1605 thread_local_.last_step_action_ == StepOut) {
1606 if (frame->fp() < thread_local_.last_fp_) return true;
1607 }
1608
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001609 // If the step last action was step next or step in make sure that a new
1610 // statement is hit.
1611 if (thread_local_.last_step_action_ == StepNext ||
1612 thread_local_.last_step_action_ == StepIn) {
1613 // Never continue if returning from function.
1614 if (break_location_iterator->IsExit()) return false;
1615
1616 // Continue if we are still on the same frame and in the same statement.
1617 int current_statement_position =
1618 break_location_iterator->code()->SourceStatementPosition(frame->pc());
mmassi@chromium.org7028c052012-06-13 11:51:58 +00001619 return thread_local_.last_fp_ == frame->UnpaddedFP() &&
ager@chromium.org236ad962008-09-25 09:45:57 +00001620 thread_local_.last_statement_position_ == current_statement_position;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001621 }
1622
1623 // No step next action - don't continue.
1624 return false;
1625}
1626
1627
1628// Check whether the code object at the specified address is a debug break code
1629// object.
1630bool Debug::IsDebugBreak(Address addr) {
ager@chromium.org8bb60582008-12-11 12:02:20 +00001631 Code* code = Code::GetCodeFromTargetAddress(addr);
verwaest@chromium.orgec6855e2013-08-22 12:26:58 +00001632 return code->is_debug_stub() && code->extra_ic_state() == DEBUG_BREAK;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001633}
1634
1635
1636// Check whether a code stub with the specified major key is a possible break
1637// point location when looking for source break locations.
1638bool Debug::IsSourceBreakStub(Code* code) {
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001639 CodeStub::Major major_key = CodeStub::GetMajorKey(code);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001640 return major_key == CodeStub::CallFunction;
1641}
1642
1643
1644// Check whether a code stub with the specified major key is a possible break
1645// location.
1646bool Debug::IsBreakStub(Code* code) {
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001647 CodeStub::Major major_key = CodeStub::GetMajorKey(code);
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00001648 return major_key == CodeStub::CallFunction;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001649}
1650
1651
1652// Find the builtin to use for invoking the debug break
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001653Handle<Code> Debug::FindDebugBreak(Handle<Code> code, RelocInfo::Mode mode) {
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +00001654 Isolate* isolate = code->GetIsolate();
danno@chromium.orgfa458e42012-02-01 10:48:36 +00001655
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001656 // Find the builtin debug break function matching the calling convention
1657 // used by the call site.
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001658 if (code->is_inline_cache_stub()) {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001659 switch (code->kind()) {
1660 case Code::CALL_IC:
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001661 case Code::KEYED_CALL_IC:
danno@chromium.orgfa458e42012-02-01 10:48:36 +00001662 return isolate->stub_cache()->ComputeCallDebugBreak(
1663 code->arguments_count(), code->kind());
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001664
1665 case Code::LOAD_IC:
danno@chromium.orgfa458e42012-02-01 10:48:36 +00001666 return isolate->builtins()->LoadIC_DebugBreak();
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001667
1668 case Code::STORE_IC:
danno@chromium.orgfa458e42012-02-01 10:48:36 +00001669 return isolate->builtins()->StoreIC_DebugBreak();
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001670
1671 case Code::KEYED_LOAD_IC:
danno@chromium.orgfa458e42012-02-01 10:48:36 +00001672 return isolate->builtins()->KeyedLoadIC_DebugBreak();
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001673
1674 case Code::KEYED_STORE_IC:
danno@chromium.orgfa458e42012-02-01 10:48:36 +00001675 return isolate->builtins()->KeyedStoreIC_DebugBreak();
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001676
danno@chromium.orgf005df62013-04-30 16:36:45 +00001677 case Code::COMPARE_NIL_IC:
1678 return isolate->builtins()->CompareNilIC_DebugBreak();
1679
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001680 default:
1681 UNREACHABLE();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001682 }
1683 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001684 if (RelocInfo::IsConstructCall(mode)) {
danno@chromium.orgfa458e42012-02-01 10:48:36 +00001685 if (code->has_function_cache()) {
1686 return isolate->builtins()->CallConstructStub_Recording_DebugBreak();
1687 } else {
1688 return isolate->builtins()->CallConstructStub_DebugBreak();
1689 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001690 }
1691 if (code->kind() == Code::STUB) {
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00001692 ASSERT(code->major_key() == CodeStub::CallFunction);
danno@chromium.orgfa458e42012-02-01 10:48:36 +00001693 if (code->has_function_cache()) {
1694 return isolate->builtins()->CallFunctionStub_Recording_DebugBreak();
1695 } else {
1696 return isolate->builtins()->CallFunctionStub_DebugBreak();
1697 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001698 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001699
1700 UNREACHABLE();
1701 return Handle<Code>::null();
1702}
1703
1704
1705// Simple function for returning the source positions for active break points.
1706Handle<Object> Debug::GetSourceBreakLocations(
jkummerow@chromium.org93a47f42013-07-02 14:43:41 +00001707 Handle<SharedFunctionInfo> shared,
1708 BreakPositionAlignment position_alignment) {
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +00001709 Isolate* isolate = shared->GetIsolate();
lrn@chromium.org7516f052011-03-30 08:52:27 +00001710 Heap* heap = isolate->heap();
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00001711 if (!HasDebugInfo(shared)) {
1712 return Handle<Object>(heap->undefined_value(), isolate);
1713 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001714 Handle<DebugInfo> debug_info = GetDebugInfo(shared);
1715 if (debug_info->GetBreakPointCount() == 0) {
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00001716 return Handle<Object>(heap->undefined_value(), isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001717 }
1718 Handle<FixedArray> locations =
lrn@chromium.org7516f052011-03-30 08:52:27 +00001719 isolate->factory()->NewFixedArray(debug_info->GetBreakPointCount());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001720 int count = 0;
1721 for (int i = 0; i < debug_info->break_points()->length(); i++) {
1722 if (!debug_info->break_points()->get(i)->IsUndefined()) {
1723 BreakPointInfo* break_point_info =
1724 BreakPointInfo::cast(debug_info->break_points()->get(i));
1725 if (break_point_info->GetBreakPointCount() > 0) {
jkummerow@chromium.org93a47f42013-07-02 14:43:41 +00001726 Smi* position;
1727 switch (position_alignment) {
1728 case STATEMENT_ALIGNED:
1729 position = break_point_info->statement_position();
1730 break;
1731 case BREAK_POSITION_ALIGNED:
1732 position = break_point_info->source_position();
1733 break;
1734 default:
1735 UNREACHABLE();
1736 position = break_point_info->statement_position();
1737 }
1738
1739 locations->set(count++, position);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001740 }
1741 }
1742 }
1743 return locations;
1744}
1745
1746
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001747void Debug::NewBreak(StackFrame::Id break_frame_id) {
1748 thread_local_.break_frame_id_ = break_frame_id;
1749 thread_local_.break_id_ = ++thread_local_.break_count_;
1750}
1751
1752
1753void Debug::SetBreak(StackFrame::Id break_frame_id, int break_id) {
1754 thread_local_.break_frame_id_ = break_frame_id;
1755 thread_local_.break_id_ = break_id;
1756}
1757
1758
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001759// Handle stepping into a function.
1760void Debug::HandleStepIn(Handle<JSFunction> function,
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001761 Handle<Object> holder,
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001762 Address fp,
1763 bool is_constructor) {
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00001764 Isolate* isolate = function->GetIsolate();
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001765 // If the frame pointer is not supplied by the caller find it.
1766 if (fp == 0) {
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00001767 StackFrameIterator it(isolate);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001768 it.Advance();
1769 // For constructor functions skip another frame.
1770 if (is_constructor) {
1771 ASSERT(it.frame()->is_construct());
1772 it.Advance();
1773 }
1774 fp = it.frame()->fp();
1775 }
1776
1777 // Flood the function with one-shot break points if it is called from where
1778 // step into was requested.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001779 if (fp == step_in_fp()) {
rossberg@chromium.org2c067b12012-03-19 11:01:52 +00001780 if (function->shared()->bound()) {
1781 // Handle Function.prototype.bind
1782 Debug::FloodBoundFunctionWithOneShot(function);
1783 } else if (!function->IsBuiltin()) {
1784 // Don't allow step into functions in the native context.
kasperl@chromium.orgacae3782009-04-11 09:17:08 +00001785 if (function->shared()->code() ==
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00001786 isolate->builtins()->builtin(Builtins::kFunctionApply) ||
kasperl@chromium.orgacae3782009-04-11 09:17:08 +00001787 function->shared()->code() ==
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00001788 isolate->builtins()->builtin(Builtins::kFunctionCall)) {
kasperl@chromium.orgacae3782009-04-11 09:17:08 +00001789 // Handle function.apply and function.call separately to flood the
1790 // function to be called and not the code for Builtins::FunctionApply or
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001791 // Builtins::FunctionCall. The receiver of call/apply is the target
1792 // function.
machenbach@chromium.org528ce022013-09-23 14:09:36 +00001793 if (!holder.is_null() && holder->IsJSFunction()) {
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001794 Handle<JSFunction> js_function = Handle<JSFunction>::cast(holder);
machenbach@chromium.org528ce022013-09-23 14:09:36 +00001795 if (!js_function->IsBuiltin()) {
1796 Debug::FloodWithOneShot(js_function);
1797 } else if (js_function->shared()->bound()) {
1798 // Handle Function.prototype.bind
1799 Debug::FloodBoundFunctionWithOneShot(js_function);
1800 }
kasperl@chromium.orgacae3782009-04-11 09:17:08 +00001801 }
1802 } else {
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001803 Debug::FloodWithOneShot(function);
kasperl@chromium.orgacae3782009-04-11 09:17:08 +00001804 }
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001805 }
1806 }
1807}
1808
1809
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001810void Debug::ClearStepping() {
1811 // Clear the various stepping setup.
1812 ClearOneShot();
1813 ClearStepIn();
ager@chromium.orga1645e22009-09-09 19:27:10 +00001814 ClearStepOut();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001815 ClearStepNext();
1816
1817 // Clear multiple step counter.
1818 thread_local_.step_count_ = 0;
1819}
1820
mstarzinger@chromium.orge0e1b0d2013-07-08 08:38:06 +00001821
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001822// Clears all the one-shot break points that are currently set. Normally this
1823// function is called each time a break point is hit as one shot break points
1824// are used to support stepping.
1825void Debug::ClearOneShot() {
1826 // The current implementation just runs through all the breakpoints. When the
v8.team.kasperl727e9952008-09-02 14:56:44 +00001827 // last break point for a function is removed that function is automatically
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001828 // removed from the list.
1829
1830 DebugInfoListNode* node = debug_info_list_;
1831 while (node != NULL) {
1832 BreakLocationIterator it(node->debug_info(), ALL_BREAK_LOCATIONS);
1833 while (!it.Done()) {
1834 it.ClearOneShot();
1835 it.Next();
1836 }
1837 node = node->next();
1838 }
1839}
1840
1841
1842void Debug::ActivateStepIn(StackFrame* frame) {
ager@chromium.orga1645e22009-09-09 19:27:10 +00001843 ASSERT(!StepOutActive());
mmassi@chromium.org7028c052012-06-13 11:51:58 +00001844 thread_local_.step_into_fp_ = frame->UnpaddedFP();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001845}
1846
1847
1848void Debug::ClearStepIn() {
1849 thread_local_.step_into_fp_ = 0;
1850}
1851
1852
ager@chromium.orga1645e22009-09-09 19:27:10 +00001853void Debug::ActivateStepOut(StackFrame* frame) {
1854 ASSERT(!StepInActive());
mmassi@chromium.org7028c052012-06-13 11:51:58 +00001855 thread_local_.step_out_fp_ = frame->UnpaddedFP();
ager@chromium.orga1645e22009-09-09 19:27:10 +00001856}
1857
1858
1859void Debug::ClearStepOut() {
1860 thread_local_.step_out_fp_ = 0;
1861}
1862
1863
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001864void Debug::ClearStepNext() {
1865 thread_local_.last_step_action_ = StepNone;
ager@chromium.org236ad962008-09-25 09:45:57 +00001866 thread_local_.last_statement_position_ = RelocInfo::kNoPosition;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001867 thread_local_.last_fp_ = 0;
1868}
1869
1870
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001871// Helper function to compile full code for debugging. This code will
mmassi@chromium.org7028c052012-06-13 11:51:58 +00001872// have debug break slots and deoptimization information. Deoptimization
1873// information is required in case that an optimized version of this
1874// function is still activated on the stack. It will also make sure that
1875// the full code is compiled with the same flags as the previous version,
1876// that is flags which can change the code generated. The current method
1877// of mapping from already compiled full code without debug break slots
1878// to full code with debug break slots depends on the generated code is
1879// otherwise exactly the same.
1880static bool CompileFullCodeForDebugging(Handle<JSFunction> function,
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001881 Handle<Code> current_code) {
1882 ASSERT(!current_code->has_debug_break_slots());
1883
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001884 CompilationInfoWithZone info(function);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001885 info.MarkCompilingForDebugging(current_code);
1886 ASSERT(!info.shared_info()->is_compiled());
1887 ASSERT(!info.isolate()->has_pending_exception());
1888
1889 // Use compile lazy which will end up compiling the full code in the
1890 // configuration configured above.
1891 bool result = Compiler::CompileLazy(&info);
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +00001892 ASSERT(result != info.isolate()->has_pending_exception());
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001893 info.isolate()->clear_pending_exception();
1894#if DEBUG
1895 if (result) {
mmassi@chromium.org7028c052012-06-13 11:51:58 +00001896 Handle<Code> new_code(function->shared()->code());
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001897 ASSERT(new_code->has_debug_break_slots());
1898 ASSERT(current_code->is_compiled_optimizable() ==
1899 new_code->is_compiled_optimizable());
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001900 }
1901#endif
1902 return result;
1903}
1904
1905
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001906static void CollectActiveFunctionsFromThread(
1907 Isolate* isolate,
1908 ThreadLocalTop* top,
1909 List<Handle<JSFunction> >* active_functions,
1910 Object* active_code_marker) {
1911 // Find all non-optimized code functions with activation frames
1912 // on the stack. This includes functions which have optimized
1913 // activations (including inlined functions) on the stack as the
1914 // non-optimized code is needed for the lazy deoptimization.
1915 for (JavaScriptFrameIterator it(isolate, top); !it.done(); it.Advance()) {
1916 JavaScriptFrame* frame = it.frame();
1917 if (frame->is_optimized()) {
jkummerow@chromium.orgba72ec82013-07-22 09:21:20 +00001918 List<JSFunction*> functions(FLAG_max_inlining_levels + 1);
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001919 frame->GetFunctions(&functions);
1920 for (int i = 0; i < functions.length(); i++) {
1921 JSFunction* function = functions[i];
1922 active_functions->Add(Handle<JSFunction>(function));
1923 function->shared()->code()->set_gc_metadata(active_code_marker);
1924 }
1925 } else if (frame->function()->IsJSFunction()) {
danno@chromium.org169691d2013-07-15 08:01:13 +00001926 JSFunction* function = frame->function();
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001927 ASSERT(frame->LookupCode()->kind() == Code::FUNCTION);
1928 active_functions->Add(Handle<JSFunction>(function));
1929 function->shared()->code()->set_gc_metadata(active_code_marker);
1930 }
1931 }
1932}
1933
1934
1935static void RedirectActivationsToRecompiledCodeOnThread(
1936 Isolate* isolate,
1937 ThreadLocalTop* top) {
1938 for (JavaScriptFrameIterator it(isolate, top); !it.done(); it.Advance()) {
1939 JavaScriptFrame* frame = it.frame();
1940
1941 if (frame->is_optimized() || !frame->function()->IsJSFunction()) continue;
1942
danno@chromium.org169691d2013-07-15 08:01:13 +00001943 JSFunction* function = frame->function();
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001944
1945 ASSERT(frame->LookupCode()->kind() == Code::FUNCTION);
1946
1947 Handle<Code> frame_code(frame->LookupCode());
1948 if (frame_code->has_debug_break_slots()) continue;
1949
1950 Handle<Code> new_code(function->shared()->code());
1951 if (new_code->kind() != Code::FUNCTION ||
1952 !new_code->has_debug_break_slots()) {
1953 continue;
1954 }
1955
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001956 // Iterate over the RelocInfo in the original code to compute the sum of the
1957 // constant pools sizes. (See Assembler::CheckConstPool())
1958 // Note that this is only useful for architectures using constant pools.
1959 int constpool_mask = RelocInfo::ModeMask(RelocInfo::CONST_POOL);
1960 int frame_const_pool_size = 0;
1961 for (RelocIterator it(*frame_code, constpool_mask); !it.done(); it.next()) {
1962 RelocInfo* info = it.rinfo();
1963 if (info->pc() >= frame->pc()) break;
1964 frame_const_pool_size += static_cast<int>(info->data());
1965 }
1966 intptr_t frame_offset =
1967 frame->pc() - frame_code->instruction_start() - frame_const_pool_size;
1968
1969 // Iterate over the RelocInfo for new code to find the number of bytes
1970 // generated for debug slots and constant pools.
1971 int debug_break_slot_bytes = 0;
1972 int new_code_const_pool_size = 0;
1973 int mask = RelocInfo::ModeMask(RelocInfo::DEBUG_BREAK_SLOT) |
1974 RelocInfo::ModeMask(RelocInfo::CONST_POOL);
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001975 for (RelocIterator it(*new_code, mask); !it.done(); it.next()) {
1976 // Check if the pc in the new code with debug break
1977 // slots is before this slot.
1978 RelocInfo* info = it.rinfo();
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001979 intptr_t new_offset = info->pc() - new_code->instruction_start() -
1980 new_code_const_pool_size - debug_break_slot_bytes;
1981 if (new_offset >= frame_offset) {
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001982 break;
1983 }
1984
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001985 if (RelocInfo::IsDebugBreakSlot(info->rmode())) {
1986 debug_break_slot_bytes += Assembler::kDebugBreakSlotLength;
1987 } else {
1988 ASSERT(RelocInfo::IsConstPool(info->rmode()));
1989 // The size of the constant pool is encoded in the data.
1990 new_code_const_pool_size += static_cast<int>(info->data());
1991 }
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001992 }
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001993
1994 // Compute the equivalent pc in the new code.
1995 byte* new_pc = new_code->instruction_start() + frame_offset +
1996 debug_break_slot_bytes + new_code_const_pool_size;
1997
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001998 if (FLAG_trace_deopt) {
1999 PrintF("Replacing code %08" V8PRIxPTR " - %08" V8PRIxPTR " (%d) "
2000 "with %08" V8PRIxPTR " - %08" V8PRIxPTR " (%d) "
2001 "for debugging, "
2002 "changing pc from %08" V8PRIxPTR " to %08" V8PRIxPTR "\n",
2003 reinterpret_cast<intptr_t>(
2004 frame_code->instruction_start()),
2005 reinterpret_cast<intptr_t>(
2006 frame_code->instruction_start()) +
2007 frame_code->instruction_size(),
2008 frame_code->instruction_size(),
2009 reinterpret_cast<intptr_t>(new_code->instruction_start()),
2010 reinterpret_cast<intptr_t>(new_code->instruction_start()) +
2011 new_code->instruction_size(),
2012 new_code->instruction_size(),
2013 reinterpret_cast<intptr_t>(frame->pc()),
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00002014 reinterpret_cast<intptr_t>(new_pc));
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002015 }
2016
2017 // Patch the return address to return into the code with
2018 // debug break slots.
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00002019 frame->set_pc(new_pc);
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002020 }
2021}
2022
2023
2024class ActiveFunctionsCollector : public ThreadVisitor {
2025 public:
2026 explicit ActiveFunctionsCollector(List<Handle<JSFunction> >* active_functions,
2027 Object* active_code_marker)
2028 : active_functions_(active_functions),
2029 active_code_marker_(active_code_marker) { }
2030
2031 void VisitThread(Isolate* isolate, ThreadLocalTop* top) {
2032 CollectActiveFunctionsFromThread(isolate,
2033 top,
2034 active_functions_,
2035 active_code_marker_);
2036 }
2037
2038 private:
2039 List<Handle<JSFunction> >* active_functions_;
2040 Object* active_code_marker_;
2041};
2042
2043
2044class ActiveFunctionsRedirector : public ThreadVisitor {
2045 public:
2046 void VisitThread(Isolate* isolate, ThreadLocalTop* top) {
2047 RedirectActivationsToRecompiledCodeOnThread(isolate, top);
2048 }
2049};
2050
2051
lrn@chromium.org34e60782011-09-15 07:25:40 +00002052void Debug::PrepareForBreakPoints() {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002053 // If preparing for the first break point make sure to deoptimize all
2054 // functions as debugging does not work with optimized code.
2055 if (!has_break_points_) {
machenbach@chromium.org9af454f2013-11-20 09:25:57 +00002056 if (isolate_->concurrent_recompilation_enabled()) {
danno@chromium.org59400602013-08-13 17:09:37 +00002057 isolate_->optimizing_compiler_thread()->Flush();
2058 }
2059
svenpanne@chromium.org876cca82013-03-18 14:43:20 +00002060 Deoptimizer::DeoptimizeAll(isolate_);
lrn@chromium.org34e60782011-09-15 07:25:40 +00002061
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002062 Handle<Code> lazy_compile =
2063 Handle<Code>(isolate_->builtins()->builtin(Builtins::kLazyCompile));
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002064
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00002065 // There will be at least one break point when we are done.
2066 has_break_points_ = true;
2067
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002068 // Keep the list of activated functions in a handlified list as it
2069 // is used both in GC and non-GC code.
2070 List<Handle<JSFunction> > active_functions(100);
lrn@chromium.org34e60782011-09-15 07:25:40 +00002071
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002072 {
2073 // We are going to iterate heap to find all functions without
2074 // debug break slots.
hpayer@chromium.org7c3372b2013-02-13 17:26:04 +00002075 Heap* heap = isolate_->heap();
2076 heap->CollectAllGarbage(Heap::kMakeHeapIterableMask,
2077 "preparing for breakpoints");
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002078
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002079 // Ensure no GC in this scope as we are going to use gc_metadata
2080 // field in the Code object to mark active functions.
rossberg@chromium.org79e79022013-06-03 15:43:46 +00002081 DisallowHeapAllocation no_allocation;
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002082
hpayer@chromium.org7c3372b2013-02-13 17:26:04 +00002083 Object* active_code_marker = heap->the_hole_value();
svenpanne@chromium.orgecb9dd62011-12-01 08:22:35 +00002084
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002085 CollectActiveFunctionsFromThread(isolate_,
2086 isolate_->thread_local_top(),
2087 &active_functions,
2088 active_code_marker);
2089 ActiveFunctionsCollector active_functions_collector(&active_functions,
2090 active_code_marker);
2091 isolate_->thread_manager()->IterateArchivedThreads(
2092 &active_functions_collector);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002093
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002094 // Scan the heap for all non-optimized functions which have no
2095 // debug break slots and are not active or inlined into an active
2096 // function and mark them for lazy compilation.
hpayer@chromium.org7c3372b2013-02-13 17:26:04 +00002097 HeapIterator iterator(heap);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002098 HeapObject* obj = NULL;
2099 while (((obj = iterator.next()) != NULL)) {
2100 if (obj->IsJSFunction()) {
2101 JSFunction* function = JSFunction::cast(obj);
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002102 SharedFunctionInfo* shared = function->shared();
mstarzinger@chromium.org1510d582013-06-28 14:00:48 +00002103
2104 if (!shared->allows_lazy_compilation()) continue;
2105 if (!shared->script()->IsScript()) continue;
bmeurer@chromium.orgc9913f02013-10-24 06:31:36 +00002106 if (function->IsBuiltin()) continue;
mstarzinger@chromium.org1510d582013-06-28 14:00:48 +00002107 if (shared->code()->gc_metadata() == active_code_marker) continue;
2108
2109 Code::Kind kind = function->code()->kind();
2110 if (kind == Code::FUNCTION &&
2111 !function->code()->has_debug_break_slots()) {
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002112 function->set_code(*lazy_compile);
2113 function->shared()->set_code(*lazy_compile);
mstarzinger@chromium.org1510d582013-06-28 14:00:48 +00002114 } else if (kind == Code::BUILTIN &&
dslomov@chromium.org4a35c5a2013-09-13 07:28:52 +00002115 (function->IsInRecompileQueue() ||
mstarzinger@chromium.org1510d582013-06-28 14:00:48 +00002116 function->IsMarkedForLazyRecompilation() ||
rossberg@chromium.org92597162013-08-23 13:28:00 +00002117 function->IsMarkedForConcurrentRecompilation())) {
mstarzinger@chromium.org1510d582013-06-28 14:00:48 +00002118 // Abort in-flight compilation.
2119 Code* shared_code = function->shared()->code();
2120 if (shared_code->kind() == Code::FUNCTION &&
2121 shared_code->has_debug_break_slots()) {
2122 function->set_code(shared_code);
2123 } else {
2124 function->set_code(*lazy_compile);
2125 function->shared()->set_code(*lazy_compile);
2126 }
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002127 }
2128 }
lrn@chromium.org34e60782011-09-15 07:25:40 +00002129 }
lrn@chromium.org34e60782011-09-15 07:25:40 +00002130
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002131 // Clear gc_metadata field.
2132 for (int i = 0; i < active_functions.length(); i++) {
2133 Handle<JSFunction> function = active_functions[i];
2134 function->shared()->code()->set_gc_metadata(Smi::FromInt(0));
2135 }
2136 }
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002137
2138 // Now recompile all functions with activation frames and and
2139 // patch the return address to run in the new compiled code.
2140 for (int i = 0; i < active_functions.length(); i++) {
2141 Handle<JSFunction> function = active_functions[i];
mmassi@chromium.org7028c052012-06-13 11:51:58 +00002142 Handle<SharedFunctionInfo> shared(function->shared());
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002143
2144 if (function->code()->kind() == Code::FUNCTION &&
2145 function->code()->has_debug_break_slots()) {
2146 // Nothing to do. Function code already had debug break slots.
2147 continue;
2148 }
2149
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002150 // If recompilation is not possible just skip it.
2151 if (shared->is_toplevel() ||
2152 !shared->allows_lazy_compilation() ||
2153 shared->code()->kind() == Code::BUILTIN) {
2154 continue;
2155 }
2156
2157 // Make sure that the shared full code is compiled with debug
2158 // break slots.
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00002159 if (!shared->code()->has_debug_break_slots()) {
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002160 // Try to compile the full code with debug break slots. If it
2161 // fails just keep the current code.
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00002162 Handle<Code> current_code(function->shared()->code());
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002163 shared->set_code(*lazy_compile);
2164 bool prev_force_debugger_active =
2165 isolate_->debugger()->force_debugger_active();
2166 isolate_->debugger()->set_force_debugger_active(true);
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00002167 ASSERT(current_code->kind() == Code::FUNCTION);
mmassi@chromium.org7028c052012-06-13 11:51:58 +00002168 CompileFullCodeForDebugging(function, current_code);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002169 isolate_->debugger()->set_force_debugger_active(
2170 prev_force_debugger_active);
2171 if (!shared->is_compiled()) {
2172 shared->set_code(*current_code);
2173 continue;
2174 }
2175 }
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002176
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002177 // Keep function code in sync with shared function info.
2178 function->set_code(shared->code());
lrn@chromium.org34e60782011-09-15 07:25:40 +00002179 }
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002180
2181 RedirectActivationsToRecompiledCodeOnThread(isolate_,
2182 isolate_->thread_local_top());
2183
2184 ActiveFunctionsRedirector active_functions_redirector;
2185 isolate_->thread_manager()->IterateArchivedThreads(
2186 &active_functions_redirector);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002187 }
lrn@chromium.org34e60782011-09-15 07:25:40 +00002188}
2189
2190
jkummerow@chromium.org78502a92012-09-06 13:50:42 +00002191Object* Debug::FindSharedFunctionInfoInScript(Handle<Script> script,
2192 int position) {
2193 // Iterate the heap looking for SharedFunctionInfo generated from the
2194 // script. The inner most SharedFunctionInfo containing the source position
2195 // for the requested break point is found.
2196 // NOTE: This might require several heap iterations. If the SharedFunctionInfo
2197 // which is found is not compiled it is compiled and the heap is iterated
2198 // again as the compilation might create inner functions from the newly
2199 // compiled function and the actual requested break point might be in one of
2200 // these functions.
2201 // NOTE: The below fix-point iteration depends on all functions that cannot be
2202 // compiled lazily without a context to not be compiled at all. Compilation
2203 // will be triggered at points where we do not need a context.
2204 bool done = false;
2205 // The current candidate for the source position:
2206 int target_start_position = RelocInfo::kNoPosition;
2207 Handle<JSFunction> target_function;
2208 Handle<SharedFunctionInfo> target;
hpayer@chromium.org7c3372b2013-02-13 17:26:04 +00002209 Heap* heap = isolate_->heap();
jkummerow@chromium.org78502a92012-09-06 13:50:42 +00002210 while (!done) {
2211 { // Extra scope for iterator and no-allocation.
hpayer@chromium.org7c3372b2013-02-13 17:26:04 +00002212 heap->EnsureHeapIsIterable();
rossberg@chromium.org79e79022013-06-03 15:43:46 +00002213 DisallowHeapAllocation no_alloc_during_heap_iteration;
hpayer@chromium.org7c3372b2013-02-13 17:26:04 +00002214 HeapIterator iterator(heap);
jkummerow@chromium.org78502a92012-09-06 13:50:42 +00002215 for (HeapObject* obj = iterator.next();
2216 obj != NULL; obj = iterator.next()) {
2217 bool found_next_candidate = false;
2218 Handle<JSFunction> function;
2219 Handle<SharedFunctionInfo> shared;
2220 if (obj->IsJSFunction()) {
2221 function = Handle<JSFunction>(JSFunction::cast(obj));
2222 shared = Handle<SharedFunctionInfo>(function->shared());
2223 ASSERT(shared->allows_lazy_compilation() || shared->is_compiled());
2224 found_next_candidate = true;
2225 } else if (obj->IsSharedFunctionInfo()) {
2226 shared = Handle<SharedFunctionInfo>(SharedFunctionInfo::cast(obj));
2227 // Skip functions that we cannot compile lazily without a context,
2228 // which is not available here, because there is no closure.
2229 found_next_candidate = shared->is_compiled() ||
2230 shared->allows_lazy_compilation_without_context();
2231 }
2232 if (!found_next_candidate) continue;
2233 if (shared->script() == *script) {
2234 // If the SharedFunctionInfo found has the requested script data and
2235 // contains the source position it is a candidate.
2236 int start_position = shared->function_token_position();
2237 if (start_position == RelocInfo::kNoPosition) {
2238 start_position = shared->start_position();
2239 }
2240 if (start_position <= position &&
2241 position <= shared->end_position()) {
2242 // If there is no candidate or this function is within the current
2243 // candidate this is the new candidate.
2244 if (target.is_null()) {
2245 target_start_position = start_position;
2246 target_function = function;
2247 target = shared;
2248 } else {
2249 if (target_start_position == start_position &&
2250 shared->end_position() == target->end_position()) {
2251 // If a top-level function contains only one function
2252 // declaration the source for the top-level and the function
2253 // is the same. In that case prefer the non top-level function.
2254 if (!shared->is_toplevel()) {
2255 target_start_position = start_position;
2256 target_function = function;
2257 target = shared;
2258 }
2259 } else if (target_start_position <= start_position &&
2260 shared->end_position() <= target->end_position()) {
2261 // This containment check includes equality as a function
2262 // inside a top-level function can share either start or end
2263 // position with the top-level function.
2264 target_start_position = start_position;
2265 target_function = function;
2266 target = shared;
2267 }
2268 }
2269 }
2270 }
2271 } // End for loop.
2272 } // End no-allocation scope.
2273
hpayer@chromium.org7c3372b2013-02-13 17:26:04 +00002274 if (target.is_null()) return heap->undefined_value();
jkummerow@chromium.org78502a92012-09-06 13:50:42 +00002275
2276 // There will be at least one break point when we are done.
2277 has_break_points_ = true;
2278
2279 // If the candidate found is compiled we are done.
2280 done = target->is_compiled();
2281 if (!done) {
2282 // If the candidate is not compiled, compile it to reveal any inner
2283 // functions which might contain the requested source position. This
2284 // will compile all inner functions that cannot be compiled without a
2285 // context, because Compiler::BuildFunctionInfo checks whether the
2286 // debugger is active.
2287 if (target_function.is_null()) {
2288 SharedFunctionInfo::CompileLazy(target, KEEP_EXCEPTION);
2289 } else {
2290 JSFunction::CompileLazy(target_function, KEEP_EXCEPTION);
2291 }
2292 }
2293 } // End while loop.
2294
2295 return *target;
2296}
2297
2298
lrn@chromium.org34e60782011-09-15 07:25:40 +00002299// Ensures the debug information is present for shared.
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00002300bool Debug::EnsureDebugInfo(Handle<SharedFunctionInfo> shared,
2301 Handle<JSFunction> function) {
verwaest@chromium.orgd4be0f02013-06-05 13:39:03 +00002302 Isolate* isolate = shared->GetIsolate();
2303
lrn@chromium.org34e60782011-09-15 07:25:40 +00002304 // Return if we already have the debug info for shared.
2305 if (HasDebugInfo(shared)) {
2306 ASSERT(shared->is_compiled());
2307 return true;
2308 }
2309
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00002310 // There will be at least one break point when we are done.
2311 has_break_points_ = true;
2312
2313 // Ensure function is compiled. Return false if this failed.
2314 if (!function.is_null() &&
2315 !JSFunction::EnsureCompiled(function, CLEAR_EXCEPTION)) {
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002316 return false;
2317 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002318
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002319 // Create the debug info object.
verwaest@chromium.orgd4be0f02013-06-05 13:39:03 +00002320 Handle<DebugInfo> debug_info = isolate->factory()->NewDebugInfo(shared);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002321
2322 // Add debug info to the list.
2323 DebugInfoListNode* node = new DebugInfoListNode(*debug_info);
2324 node->set_next(debug_info_list_);
2325 debug_info_list_ = node;
2326
kasper.lundbd3ec4e2008-07-09 11:06:54 +00002327 return true;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002328}
2329
2330
2331void Debug::RemoveDebugInfo(Handle<DebugInfo> debug_info) {
2332 ASSERT(debug_info_list_ != NULL);
2333 // Run through the debug info objects to find this one and remove it.
2334 DebugInfoListNode* prev = NULL;
2335 DebugInfoListNode* current = debug_info_list_;
2336 while (current != NULL) {
2337 if (*current->debug_info() == *debug_info) {
2338 // Unlink from list. If prev is NULL we are looking at the first element.
2339 if (prev == NULL) {
2340 debug_info_list_ = current->next();
2341 } else {
2342 prev->set_next(current->next());
2343 }
lrn@chromium.org7516f052011-03-30 08:52:27 +00002344 current->debug_info()->shared()->set_debug_info(
2345 isolate_->heap()->undefined_value());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002346 delete current;
2347
2348 // If there are no more debug info objects there are not more break
2349 // points.
2350 has_break_points_ = debug_info_list_ != NULL;
2351
2352 return;
2353 }
2354 // Move to next in list.
2355 prev = current;
2356 current = current->next();
2357 }
2358 UNREACHABLE();
2359}
2360
2361
2362void Debug::SetAfterBreakTarget(JavaScriptFrame* frame) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002363 HandleScope scope(isolate_);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002364
lrn@chromium.org34e60782011-09-15 07:25:40 +00002365 PrepareForBreakPoints();
2366
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002367 // Get the executing function in which the debug break occurred.
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00002368 Handle<JSFunction> function(JSFunction::cast(frame->function()));
2369 Handle<SharedFunctionInfo> shared(function->shared());
2370 if (!EnsureDebugInfo(shared, function)) {
kasper.lundbd3ec4e2008-07-09 11:06:54 +00002371 // Return if we failed to retrieve the debug info.
2372 return;
2373 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002374 Handle<DebugInfo> debug_info = GetDebugInfo(shared);
2375 Handle<Code> code(debug_info->code());
2376 Handle<Code> original_code(debug_info->original_code());
2377#ifdef DEBUG
2378 // Get the code which is actually executing.
vegorov@chromium.org74f333b2011-04-06 11:17:46 +00002379 Handle<Code> frame_code(frame->LookupCode());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002380 ASSERT(frame_code.is_identical_to(code));
2381#endif
2382
2383 // Find the call address in the running code. This address holds the call to
2384 // either a DebugBreakXXX or to the debug break return entry code if the
2385 // break point is still active after processing the break point.
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00002386 Address addr = frame->pc() - Assembler::kPatchDebugBreakSlotReturnOffset;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002387
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002388 // Check if the location is at JS exit or debug break slot.
ager@chromium.orga1645e22009-09-09 19:27:10 +00002389 bool at_js_return = false;
2390 bool break_at_js_return_active = false;
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002391 bool at_debug_break_slot = false;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002392 RelocIterator it(debug_info->code());
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002393 while (!it.done() && !at_js_return && !at_debug_break_slot) {
ager@chromium.org236ad962008-09-25 09:45:57 +00002394 if (RelocInfo::IsJSReturn(it.rinfo()->rmode())) {
ager@chromium.orga1645e22009-09-09 19:27:10 +00002395 at_js_return = (it.rinfo()->pc() ==
2396 addr - Assembler::kPatchReturnSequenceAddressOffset);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002397 break_at_js_return_active = it.rinfo()->IsPatchedReturnSequence();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002398 }
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002399 if (RelocInfo::IsDebugBreakSlot(it.rinfo()->rmode())) {
2400 at_debug_break_slot = (it.rinfo()->pc() ==
2401 addr - Assembler::kPatchDebugBreakSlotAddressOffset);
2402 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002403 it.next();
2404 }
2405
2406 // Handle the jump to continue execution after break point depending on the
2407 // break location.
ager@chromium.orga1645e22009-09-09 19:27:10 +00002408 if (at_js_return) {
2409 // If the break point as return is still active jump to the corresponding
2410 // place in the original code. If not the break point was removed during
2411 // break point processing.
2412 if (break_at_js_return_active) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002413 addr += original_code->instruction_start() - code->instruction_start();
2414 }
2415
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00002416 // Move back to where the call instruction sequence started.
2417 thread_local_.after_break_target_ =
2418 addr - Assembler::kPatchReturnSequenceAddressOffset;
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002419 } else if (at_debug_break_slot) {
2420 // Address of where the debug break slot starts.
2421 addr = addr - Assembler::kPatchDebugBreakSlotAddressOffset;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002422
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002423 // Continue just after the slot.
2424 thread_local_.after_break_target_ = addr + Assembler::kDebugBreakSlotLength;
2425 } else if (IsDebugBreak(Assembler::target_address_at(addr))) {
2426 // We now know that there is still a debug break call at the target address,
2427 // so the break point is still there and the original code will hold the
2428 // address to jump to in order to complete the call which is replaced by a
2429 // call to DebugBreakXXX.
2430
2431 // Find the corresponding address in the original code.
2432 addr += original_code->instruction_start() - code->instruction_start();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002433
2434 // Install jump to the call address in the original code. This will be the
2435 // call which was overwritten by the call to DebugBreakXXX.
2436 thread_local_.after_break_target_ = Assembler::target_address_at(addr);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002437 } else {
2438 // There is no longer a break point present. Don't try to look in the
2439 // original code as the running code will have the right address. This takes
2440 // care of the case where the last break point is removed from the function
2441 // and therefore no "original code" is available.
2442 thread_local_.after_break_target_ = Assembler::target_address_at(addr);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002443 }
2444}
2445
2446
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00002447bool Debug::IsBreakAtReturn(JavaScriptFrame* frame) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002448 HandleScope scope(isolate_);
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00002449
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00002450 // If there are no break points this cannot be break at return, as
2451 // the debugger statement and stack guard bebug break cannot be at
2452 // return.
2453 if (!has_break_points_) {
2454 return false;
2455 }
2456
lrn@chromium.org34e60782011-09-15 07:25:40 +00002457 PrepareForBreakPoints();
2458
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00002459 // Get the executing function in which the debug break occurred.
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00002460 Handle<JSFunction> function(JSFunction::cast(frame->function()));
2461 Handle<SharedFunctionInfo> shared(function->shared());
2462 if (!EnsureDebugInfo(shared, function)) {
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00002463 // Return if we failed to retrieve the debug info.
2464 return false;
2465 }
2466 Handle<DebugInfo> debug_info = GetDebugInfo(shared);
2467 Handle<Code> code(debug_info->code());
2468#ifdef DEBUG
2469 // Get the code which is actually executing.
vegorov@chromium.org74f333b2011-04-06 11:17:46 +00002470 Handle<Code> frame_code(frame->LookupCode());
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00002471 ASSERT(frame_code.is_identical_to(code));
2472#endif
2473
2474 // Find the call address in the running code.
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00002475 Address addr = frame->pc() - Assembler::kPatchDebugBreakSlotReturnOffset;
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00002476
2477 // Check if the location is at JS return.
2478 RelocIterator it(debug_info->code());
2479 while (!it.done()) {
2480 if (RelocInfo::IsJSReturn(it.rinfo()->rmode())) {
2481 return (it.rinfo()->pc() ==
2482 addr - Assembler::kPatchReturnSequenceAddressOffset);
2483 }
2484 it.next();
2485 }
2486 return false;
2487}
2488
2489
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00002490void Debug::FramesHaveBeenDropped(StackFrame::Id new_break_frame_id,
whesse@chromium.orge90029b2010-08-02 11:52:17 +00002491 FrameDropMode mode,
2492 Object** restarter_frame_function_pointer) {
ulan@chromium.orgd9e468a2012-06-25 09:47:40 +00002493 if (mode != CURRENTLY_SET_MODE) {
2494 thread_local_.frame_drop_mode_ = mode;
2495 }
ager@chromium.org357bf652010-04-12 11:30:10 +00002496 thread_local_.break_frame_id_ = new_break_frame_id;
whesse@chromium.orge90029b2010-08-02 11:52:17 +00002497 thread_local_.restarter_frame_function_pointer_ =
2498 restarter_frame_function_pointer;
ager@chromium.org357bf652010-04-12 11:30:10 +00002499}
2500
2501
jkummerow@chromium.org212d9642012-05-11 15:02:09 +00002502const int Debug::FramePaddingLayout::kInitialSize = 1;
2503
2504
2505// Any even value bigger than kInitialSize as needed for stack scanning.
2506const int Debug::FramePaddingLayout::kPaddingValue = kInitialSize + 1;
2507
2508
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002509bool Debug::IsDebugGlobal(GlobalObject* global) {
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00002510 return IsLoaded() && global == debug_context()->global_object();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002511}
2512
2513
ager@chromium.org32912102009-01-16 10:38:43 +00002514void Debug::ClearMirrorCache() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002515 PostponeInterruptsScope postpone(isolate_);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002516 HandleScope scope(isolate_);
2517 ASSERT(isolate_->context() == *Debug::debug_context());
ager@chromium.org32912102009-01-16 10:38:43 +00002518
2519 // Clear the mirror cache.
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00002520 Handle<String> function_name = isolate_->factory()->InternalizeOneByteString(
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00002521 STATIC_ASCII_VECTOR("ClearMirrorCache"));
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00002522 Handle<Object> fun(
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00002523 isolate_->global_object()->GetPropertyNoExceptionThrown(*function_name),
2524 isolate_);
ager@chromium.org32912102009-01-16 10:38:43 +00002525 ASSERT(fun->IsJSFunction());
2526 bool caught_exception;
rossberg@chromium.org717967f2011-07-20 13:44:42 +00002527 Execution::TryCall(Handle<JSFunction>::cast(fun),
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00002528 Handle<JSObject>(Debug::debug_context()->global_object()),
ager@chromium.org32912102009-01-16 10:38:43 +00002529 0, NULL, &caught_exception);
2530}
2531
2532
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002533void Debug::CreateScriptCache() {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002534 Heap* heap = isolate_->heap();
2535 HandleScope scope(isolate_);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002536
2537 // Perform two GCs to get rid of all unreferenced scripts. The first GC gets
2538 // rid of all the cached script wrappers and the second gets rid of the
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002539 // scripts which are no longer referenced. The second also sweeps precisely,
2540 // which saves us doing yet another GC to make the heap iterable.
rossberg@chromium.org994edf62012-02-06 10:12:55 +00002541 heap->CollectAllGarbage(Heap::kNoGCFlags, "Debug::CreateScriptCache");
2542 heap->CollectAllGarbage(Heap::kMakeHeapIterableMask,
2543 "Debug::CreateScriptCache");
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002544
2545 ASSERT(script_cache_ == NULL);
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +00002546 script_cache_ = new ScriptCache(isolate_);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002547
2548 // Scan heap for Script objects.
2549 int count = 0;
hpayer@chromium.org7c3372b2013-02-13 17:26:04 +00002550 HeapIterator iterator(heap);
rossberg@chromium.org79e79022013-06-03 15:43:46 +00002551 DisallowHeapAllocation no_allocation;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002552
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002553 for (HeapObject* obj = iterator.next(); obj != NULL; obj = iterator.next()) {
sgjesse@chromium.org152a0b02009-10-07 13:50:16 +00002554 if (obj->IsScript() && Script::cast(obj)->HasValidSource()) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002555 script_cache_->Add(Handle<Script>(Script::cast(obj)));
2556 count++;
2557 }
2558 }
2559}
2560
2561
2562void Debug::DestroyScriptCache() {
2563 // Get rid of the script cache if it was created.
2564 if (script_cache_ != NULL) {
2565 delete script_cache_;
2566 script_cache_ = NULL;
2567 }
2568}
2569
2570
2571void Debug::AddScriptToScriptCache(Handle<Script> script) {
2572 if (script_cache_ != NULL) {
2573 script_cache_->Add(script);
2574 }
2575}
2576
2577
2578Handle<FixedArray> Debug::GetLoadedScripts() {
2579 // Create and fill the script cache when the loaded scripts is requested for
2580 // the first time.
2581 if (script_cache_ == NULL) {
2582 CreateScriptCache();
2583 }
2584
2585 // If the script cache is not active just return an empty array.
2586 ASSERT(script_cache_ != NULL);
2587 if (script_cache_ == NULL) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002588 isolate_->factory()->NewFixedArray(0);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002589 }
2590
2591 // Perform GC to get unreferenced scripts evicted from the cache before
2592 // returning the content.
rossberg@chromium.org994edf62012-02-06 10:12:55 +00002593 isolate_->heap()->CollectAllGarbage(Heap::kNoGCFlags,
2594 "Debug::GetLoadedScripts");
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002595
2596 // Get the scripts from the cache.
2597 return script_cache_->GetScripts();
2598}
2599
2600
2601void Debug::AfterGarbageCollection() {
2602 // Generate events for collected scripts.
2603 if (script_cache_ != NULL) {
2604 script_cache_->ProcessCollectedScripts();
2605 }
2606}
2607
2608
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00002609Debugger::Debugger(Isolate* isolate)
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +00002610 : debugger_access_(isolate->debugger_access()),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002611 event_listener_(Handle<Object>()),
2612 event_listener_data_(Handle<Object>()),
2613 compiling_natives_(false),
2614 is_loading_debugger_(false),
mstarzinger@chromium.orgde886792012-09-11 13:22:37 +00002615 live_edit_enabled_(true),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002616 never_unload_debugger_(false),
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002617 force_debugger_active_(false),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002618 message_handler_(NULL),
2619 debugger_unload_pending_(false),
2620 host_dispatch_handler_(NULL),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002621 debug_message_dispatch_handler_(NULL),
2622 message_dispatch_helper_thread_(NULL),
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +00002623 host_dispatch_period_(TimeDelta::FromMilliseconds(100)),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002624 agent_(NULL),
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00002625 command_queue_(isolate->logger(), kQueueInitialSize),
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +00002626 command_received_(0),
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00002627 event_command_queue_(isolate->logger(), kQueueInitialSize),
2628 isolate_(isolate) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002629}
2630
2631
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +00002632Debugger::~Debugger() {}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002633
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002634
2635Handle<Object> Debugger::MakeJSObject(Vector<const char> constructor_name,
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002636 int argc,
2637 Handle<Object> argv[],
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002638 bool* caught_exception) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002639 ASSERT(isolate_->context() == *isolate_->debug()->debug_context());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002640
2641 // Create the execution state object.
lrn@chromium.org7516f052011-03-30 08:52:27 +00002642 Handle<String> constructor_str =
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00002643 isolate_->factory()->InternalizeUtf8String(constructor_name);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002644 Handle<Object> constructor(
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00002645 isolate_->global_object()->GetPropertyNoExceptionThrown(*constructor_str),
2646 isolate_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002647 ASSERT(constructor->IsJSFunction());
2648 if (!constructor->IsJSFunction()) {
2649 *caught_exception = true;
lrn@chromium.org7516f052011-03-30 08:52:27 +00002650 return isolate_->factory()->undefined_value();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002651 }
2652 Handle<Object> js_object = Execution::TryCall(
2653 Handle<JSFunction>::cast(constructor),
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00002654 Handle<JSObject>(isolate_->debug()->debug_context()->global_object()),
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002655 argc,
2656 argv,
2657 caught_exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002658 return js_object;
2659}
2660
2661
2662Handle<Object> Debugger::MakeExecutionState(bool* caught_exception) {
2663 // Create the execution state object.
lrn@chromium.org7516f052011-03-30 08:52:27 +00002664 Handle<Object> break_id = isolate_->factory()->NewNumberFromInt(
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002665 isolate_->debug()->break_id());
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002666 Handle<Object> argv[] = { break_id };
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002667 return MakeJSObject(CStrVector("MakeExecutionState"),
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002668 ARRAY_SIZE(argv),
2669 argv,
2670 caught_exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002671}
2672
2673
2674Handle<Object> Debugger::MakeBreakEvent(Handle<Object> exec_state,
2675 Handle<Object> break_points_hit,
2676 bool* caught_exception) {
2677 // Create the new break event object.
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002678 Handle<Object> argv[] = { exec_state, break_points_hit };
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002679 return MakeJSObject(CStrVector("MakeBreakEvent"),
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002680 ARRAY_SIZE(argv),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002681 argv,
2682 caught_exception);
2683}
2684
2685
2686Handle<Object> Debugger::MakeExceptionEvent(Handle<Object> exec_state,
2687 Handle<Object> exception,
2688 bool uncaught,
2689 bool* caught_exception) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002690 Factory* factory = isolate_->factory();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002691 // Create the new exception event object.
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002692 Handle<Object> argv[] = { exec_state,
2693 exception,
2694 factory->ToBoolean(uncaught) };
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002695 return MakeJSObject(CStrVector("MakeExceptionEvent"),
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002696 ARRAY_SIZE(argv),
2697 argv,
2698 caught_exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002699}
2700
2701
2702Handle<Object> Debugger::MakeNewFunctionEvent(Handle<Object> function,
2703 bool* caught_exception) {
2704 // Create the new function event object.
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002705 Handle<Object> argv[] = { function };
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002706 return MakeJSObject(CStrVector("MakeNewFunctionEvent"),
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002707 ARRAY_SIZE(argv),
2708 argv,
2709 caught_exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002710}
2711
2712
2713Handle<Object> Debugger::MakeCompileEvent(Handle<Script> script,
iposva@chromium.org245aa852009-02-10 00:49:54 +00002714 bool before,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002715 bool* caught_exception) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002716 Factory* factory = isolate_->factory();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002717 // Create the compile event object.
2718 Handle<Object> exec_state = MakeExecutionState(caught_exception);
iposva@chromium.org245aa852009-02-10 00:49:54 +00002719 Handle<Object> script_wrapper = GetScriptWrapper(script);
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002720 Handle<Object> argv[] = { exec_state,
2721 script_wrapper,
2722 factory->ToBoolean(before) };
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002723 return MakeJSObject(CStrVector("MakeCompileEvent"),
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002724 ARRAY_SIZE(argv),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002725 argv,
2726 caught_exception);
2727}
2728
2729
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002730Handle<Object> Debugger::MakeScriptCollectedEvent(int id,
2731 bool* caught_exception) {
2732 // Create the script collected event object.
2733 Handle<Object> exec_state = MakeExecutionState(caught_exception);
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00002734 Handle<Object> id_object = Handle<Smi>(Smi::FromInt(id), isolate_);
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002735 Handle<Object> argv[] = { exec_state, id_object };
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002736
2737 return MakeJSObject(CStrVector("MakeScriptCollectedEvent"),
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002738 ARRAY_SIZE(argv),
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002739 argv,
2740 caught_exception);
2741}
2742
2743
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002744void Debugger::OnException(Handle<Object> exception, bool uncaught) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002745 HandleScope scope(isolate_);
2746 Debug* debug = isolate_->debug();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002747
2748 // Bail out based on state or if there is no listener for this event
lrn@chromium.org7516f052011-03-30 08:52:27 +00002749 if (debug->InDebugger()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002750 if (!Debugger::EventActive(v8::Exception)) return;
2751
2752 // Bail out if exception breaks are not active
2753 if (uncaught) {
2754 // Uncaught exceptions are reported by either flags.
lrn@chromium.org7516f052011-03-30 08:52:27 +00002755 if (!(debug->break_on_uncaught_exception() ||
2756 debug->break_on_exception())) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002757 } else {
2758 // Caught exceptions are reported is activated.
lrn@chromium.org7516f052011-03-30 08:52:27 +00002759 if (!debug->break_on_exception()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002760 }
2761
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002762 // Enter the debugger.
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +00002763 EnterDebugger debugger(isolate_);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002764 if (debugger.FailedToEnter()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002765
2766 // Clear all current stepping setup.
lrn@chromium.org7516f052011-03-30 08:52:27 +00002767 debug->ClearStepping();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002768 // Create the event data object.
2769 bool caught_exception = false;
2770 Handle<Object> exec_state = MakeExecutionState(&caught_exception);
2771 Handle<Object> event_data;
2772 if (!caught_exception) {
2773 event_data = MakeExceptionEvent(exec_state, exception, uncaught,
2774 &caught_exception);
2775 }
2776 // Bail out and don't call debugger if exception.
2777 if (caught_exception) {
2778 return;
2779 }
2780
ager@chromium.org5ec48922009-05-05 07:25:34 +00002781 // Process debug event.
2782 ProcessDebugEvent(v8::Exception, Handle<JSObject>::cast(event_data), false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002783 // Return to continue execution from where the exception was thrown.
2784}
2785
2786
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002787void Debugger::OnDebugBreak(Handle<Object> break_points_hit,
2788 bool auto_continue) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002789 HandleScope scope(isolate_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002790
kasper.lund212ac232008-07-16 07:07:30 +00002791 // Debugger has already been entered by caller.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002792 ASSERT(isolate_->context() == *isolate_->debug()->debug_context());
kasper.lund212ac232008-07-16 07:07:30 +00002793
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002794 // Bail out if there is no listener for this event
2795 if (!Debugger::EventActive(v8::Break)) return;
2796
2797 // Debugger must be entered in advance.
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00002798 ASSERT(isolate_->context() == *isolate_->debug()->debug_context());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002799
2800 // Create the event data object.
2801 bool caught_exception = false;
2802 Handle<Object> exec_state = MakeExecutionState(&caught_exception);
2803 Handle<Object> event_data;
2804 if (!caught_exception) {
2805 event_data = MakeBreakEvent(exec_state, break_points_hit,
2806 &caught_exception);
2807 }
2808 // Bail out and don't call debugger if exception.
2809 if (caught_exception) {
2810 return;
2811 }
2812
ager@chromium.org5ec48922009-05-05 07:25:34 +00002813 // Process debug event.
2814 ProcessDebugEvent(v8::Break,
2815 Handle<JSObject>::cast(event_data),
2816 auto_continue);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002817}
2818
2819
2820void Debugger::OnBeforeCompile(Handle<Script> script) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002821 HandleScope scope(isolate_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002822
2823 // Bail out based on state or if there is no listener for this event
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002824 if (isolate_->debug()->InDebugger()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002825 if (compiling_natives()) return;
2826 if (!EventActive(v8::BeforeCompile)) return;
2827
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002828 // Enter the debugger.
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +00002829 EnterDebugger debugger(isolate_);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002830 if (debugger.FailedToEnter()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002831
2832 // Create the event data object.
2833 bool caught_exception = false;
iposva@chromium.org245aa852009-02-10 00:49:54 +00002834 Handle<Object> event_data = MakeCompileEvent(script, true, &caught_exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002835 // Bail out and don't call debugger if exception.
2836 if (caught_exception) {
2837 return;
2838 }
2839
ager@chromium.org5ec48922009-05-05 07:25:34 +00002840 // Process debug event.
2841 ProcessDebugEvent(v8::BeforeCompile,
2842 Handle<JSObject>::cast(event_data),
2843 true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002844}
2845
2846
2847// Handle debugger actions when a new script is compiled.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00002848void Debugger::OnAfterCompile(Handle<Script> script,
2849 AfterCompileFlags after_compile_flags) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002850 HandleScope scope(isolate_);
2851 Debug* debug = isolate_->debug();
kasper.lund212ac232008-07-16 07:07:30 +00002852
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002853 // Add the newly compiled script to the script cache.
lrn@chromium.org7516f052011-03-30 08:52:27 +00002854 debug->AddScriptToScriptCache(script);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002855
2856 // No more to do if not debugging.
ager@chromium.org71daaf62009-04-01 07:22:49 +00002857 if (!IsDebuggerActive()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002858
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002859 // No compile events while compiling natives.
2860 if (compiling_natives()) return;
2861
iposva@chromium.org245aa852009-02-10 00:49:54 +00002862 // Store whether in debugger before entering debugger.
lrn@chromium.org7516f052011-03-30 08:52:27 +00002863 bool in_debugger = debug->InDebugger();
iposva@chromium.org245aa852009-02-10 00:49:54 +00002864
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002865 // Enter the debugger.
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +00002866 EnterDebugger debugger(isolate_);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002867 if (debugger.FailedToEnter()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002868
2869 // If debugging there might be script break points registered for this
2870 // script. Make sure that these break points are set.
2871
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002872 // Get the function UpdateScriptBreakPoints (defined in debug-debugger.js).
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00002873 Handle<String> update_script_break_points_string =
2874 isolate_->factory()->InternalizeOneByteString(
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00002875 STATIC_ASCII_VECTOR("UpdateScriptBreakPoints"));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002876 Handle<Object> update_script_break_points =
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00002877 Handle<Object>(
2878 debug->debug_context()->global_object()->GetPropertyNoExceptionThrown(
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00002879 *update_script_break_points_string),
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00002880 isolate_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002881 if (!update_script_break_points->IsJSFunction()) {
2882 return;
2883 }
2884 ASSERT(update_script_break_points->IsJSFunction());
2885
2886 // Wrap the script object in a proper JS object before passing it
2887 // to JavaScript.
2888 Handle<JSValue> wrapper = GetScriptWrapper(script);
2889
2890 // Call UpdateScriptBreakPoints expect no exceptions.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002891 bool caught_exception;
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002892 Handle<Object> argv[] = { wrapper };
rossberg@chromium.org717967f2011-07-20 13:44:42 +00002893 Execution::TryCall(Handle<JSFunction>::cast(update_script_break_points),
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +00002894 isolate_->js_builtins_object(),
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002895 ARRAY_SIZE(argv),
2896 argv,
2897 &caught_exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002898 if (caught_exception) {
2899 return;
2900 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002901 // Bail out based on state or if there is no listener for this event
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00002902 if (in_debugger && (after_compile_flags & SEND_WHEN_DEBUGGING) == 0) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002903 if (!Debugger::EventActive(v8::AfterCompile)) return;
2904
2905 // Create the compile state object.
2906 Handle<Object> event_data = MakeCompileEvent(script,
iposva@chromium.org245aa852009-02-10 00:49:54 +00002907 false,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002908 &caught_exception);
2909 // Bail out and don't call debugger if exception.
2910 if (caught_exception) {
2911 return;
2912 }
ager@chromium.org5ec48922009-05-05 07:25:34 +00002913 // Process debug event.
2914 ProcessDebugEvent(v8::AfterCompile,
2915 Handle<JSObject>::cast(event_data),
2916 true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002917}
2918
2919
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002920void Debugger::OnScriptCollected(int id) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002921 HandleScope scope(isolate_);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002922
2923 // No more to do if not debugging.
ulan@chromium.org56c14af2012-09-20 12:51:09 +00002924 if (isolate_->debug()->InDebugger()) return;
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002925 if (!IsDebuggerActive()) return;
2926 if (!Debugger::EventActive(v8::ScriptCollected)) return;
2927
2928 // Enter the debugger.
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +00002929 EnterDebugger debugger(isolate_);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002930 if (debugger.FailedToEnter()) return;
2931
2932 // Create the script collected state object.
2933 bool caught_exception = false;
2934 Handle<Object> event_data = MakeScriptCollectedEvent(id,
2935 &caught_exception);
2936 // Bail out and don't call debugger if exception.
2937 if (caught_exception) {
2938 return;
2939 }
2940
2941 // Process debug event.
2942 ProcessDebugEvent(v8::ScriptCollected,
2943 Handle<JSObject>::cast(event_data),
2944 true);
2945}
2946
2947
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002948void Debugger::ProcessDebugEvent(v8::DebugEvent event,
ager@chromium.org5ec48922009-05-05 07:25:34 +00002949 Handle<JSObject> event_data,
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002950 bool auto_continue) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002951 HandleScope scope(isolate_);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002952
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +00002953 // Clear any pending debug break if this is a real break.
2954 if (!auto_continue) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002955 isolate_->debug()->clear_interrupt_pending(DEBUGBREAK);
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +00002956 }
2957
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002958 // Create the execution state.
2959 bool caught_exception = false;
2960 Handle<Object> exec_state = MakeExecutionState(&caught_exception);
2961 if (caught_exception) {
2962 return;
2963 }
ager@chromium.org41826e72009-03-30 13:30:57 +00002964 // First notify the message handler if any.
2965 if (message_handler_ != NULL) {
ager@chromium.org5ec48922009-05-05 07:25:34 +00002966 NotifyMessageHandler(event,
2967 Handle<JSObject>::cast(exec_state),
2968 event_data,
2969 auto_continue);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002970 }
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00002971 // Notify registered debug event listener. This can be either a C or
2972 // a JavaScript function. Don't call event listener for v8::Break
2973 // here, if it's only a debug command -- they will be processed later.
2974 if ((event != v8::Break || !auto_continue) && !event_listener_.is_null()) {
2975 CallEventCallback(event, exec_state, event_data, NULL);
2976 }
2977 // Process pending debug commands.
2978 if (event == v8::Break) {
2979 while (!event_command_queue_.IsEmpty()) {
2980 CommandMessage command = event_command_queue_.Get();
2981 if (!event_listener_.is_null()) {
2982 CallEventCallback(v8::BreakForCommand,
2983 exec_state,
2984 event_data,
2985 command.client_data());
2986 }
2987 command.Dispose();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002988 }
2989 }
2990}
2991
2992
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00002993void Debugger::CallEventCallback(v8::DebugEvent event,
2994 Handle<Object> exec_state,
2995 Handle<Object> event_data,
2996 v8::Debug::ClientData* client_data) {
ager@chromium.orgea91cc52011-05-23 06:06:11 +00002997 if (event_listener_->IsForeign()) {
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00002998 CallCEventCallback(event, exec_state, event_data, client_data);
2999 } else {
3000 CallJSEventCallback(event, exec_state, event_data);
3001 }
3002}
3003
3004
3005void Debugger::CallCEventCallback(v8::DebugEvent event,
3006 Handle<Object> exec_state,
3007 Handle<Object> event_data,
3008 v8::Debug::ClientData* client_data) {
ager@chromium.orgea91cc52011-05-23 06:06:11 +00003009 Handle<Foreign> callback_obj(Handle<Foreign>::cast(event_listener_));
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00003010 v8::Debug::EventCallback2 callback =
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003011 FUNCTION_CAST<v8::Debug::EventCallback2>(
3012 callback_obj->foreign_address());
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00003013 EventDetailsImpl event_details(
3014 event,
3015 Handle<JSObject>::cast(exec_state),
3016 Handle<JSObject>::cast(event_data),
3017 event_listener_data_,
3018 client_data);
3019 callback(event_details);
3020}
3021
3022
3023void Debugger::CallJSEventCallback(v8::DebugEvent event,
3024 Handle<Object> exec_state,
3025 Handle<Object> event_data) {
3026 ASSERT(event_listener_->IsJSFunction());
3027 Handle<JSFunction> fun(Handle<JSFunction>::cast(event_listener_));
3028
3029 // Invoke the JavaScript debug event listener.
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00003030 Handle<Object> argv[] = { Handle<Object>(Smi::FromInt(event), isolate_),
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00003031 exec_state,
3032 event_data,
3033 event_listener_data_ };
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003034 bool caught_exception;
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00003035 Execution::TryCall(fun,
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00003036 isolate_->global_object(),
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00003037 ARRAY_SIZE(argv),
3038 argv,
3039 &caught_exception);
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00003040 // Silently ignore exceptions from debug event listeners.
3041}
3042
3043
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00003044Handle<Context> Debugger::GetDebugContext() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003045 never_unload_debugger_ = true;
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +00003046 EnterDebugger debugger(isolate_);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003047 return isolate_->debug()->debug_context();
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00003048}
3049
3050
ager@chromium.org71daaf62009-04-01 07:22:49 +00003051void Debugger::UnloadDebugger() {
lrn@chromium.org7516f052011-03-30 08:52:27 +00003052 Debug* debug = isolate_->debug();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003053
ager@chromium.org71daaf62009-04-01 07:22:49 +00003054 // Make sure that there are no breakpoints left.
lrn@chromium.org7516f052011-03-30 08:52:27 +00003055 debug->ClearAllBreakPoints();
ager@chromium.org71daaf62009-04-01 07:22:49 +00003056
3057 // Unload the debugger if feasible.
3058 if (!never_unload_debugger_) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00003059 debug->Unload();
ager@chromium.org71daaf62009-04-01 07:22:49 +00003060 }
3061
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003062 // Clear the flag indicating that the debugger should be unloaded.
3063 debugger_unload_pending_ = false;
ager@chromium.org71daaf62009-04-01 07:22:49 +00003064}
3065
3066
ager@chromium.org41826e72009-03-30 13:30:57 +00003067void Debugger::NotifyMessageHandler(v8::DebugEvent event,
ager@chromium.org5ec48922009-05-05 07:25:34 +00003068 Handle<JSObject> exec_state,
3069 Handle<JSObject> event_data,
ager@chromium.org41826e72009-03-30 13:30:57 +00003070 bool auto_continue) {
machenbach@chromium.orgf9841892013-11-25 12:01:13 +00003071 v8::Isolate* isolate = reinterpret_cast<v8::Isolate*>(isolate_);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003072 HandleScope scope(isolate_);
ager@chromium.org41826e72009-03-30 13:30:57 +00003073
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003074 if (!isolate_->debug()->Load()) return;
ager@chromium.org41826e72009-03-30 13:30:57 +00003075
3076 // Process the individual events.
ager@chromium.org5ec48922009-05-05 07:25:34 +00003077 bool sendEventMessage = false;
ager@chromium.org41826e72009-03-30 13:30:57 +00003078 switch (event) {
3079 case v8::Break:
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00003080 case v8::BreakForCommand:
ager@chromium.org5ec48922009-05-05 07:25:34 +00003081 sendEventMessage = !auto_continue;
ager@chromium.org41826e72009-03-30 13:30:57 +00003082 break;
3083 case v8::Exception:
ager@chromium.org5ec48922009-05-05 07:25:34 +00003084 sendEventMessage = true;
ager@chromium.org41826e72009-03-30 13:30:57 +00003085 break;
3086 case v8::BeforeCompile:
3087 break;
3088 case v8::AfterCompile:
ager@chromium.org5ec48922009-05-05 07:25:34 +00003089 sendEventMessage = true;
ager@chromium.org41826e72009-03-30 13:30:57 +00003090 break;
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003091 case v8::ScriptCollected:
3092 sendEventMessage = true;
3093 break;
ager@chromium.org41826e72009-03-30 13:30:57 +00003094 case v8::NewFunction:
3095 break;
3096 default:
3097 UNREACHABLE();
3098 }
3099
ager@chromium.org5ec48922009-05-05 07:25:34 +00003100 // The debug command interrupt flag might have been set when the command was
3101 // added. It should be enough to clear the flag only once while we are in the
3102 // debugger.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003103 ASSERT(isolate_->debug()->InDebugger());
3104 isolate_->stack_guard()->Continue(DEBUGCOMMAND);
ager@chromium.org5ec48922009-05-05 07:25:34 +00003105
3106 // Notify the debugger that a debug event has occurred unless auto continue is
3107 // active in which case no event is send.
3108 if (sendEventMessage) {
3109 MessageImpl message = MessageImpl::NewEvent(
3110 event,
3111 auto_continue,
3112 Handle<JSObject>::cast(exec_state),
3113 Handle<JSObject>::cast(event_data));
3114 InvokeMessageHandler(message);
3115 }
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +00003116
3117 // If auto continue don't make the event cause a break, but process messages
3118 // in the queue if any. For script collected events don't even process
3119 // messages in the queue as the execution state might not be what is expected
3120 // by the client.
ager@chromium.org6ffc2172009-05-29 19:20:16 +00003121 if ((auto_continue && !HasCommands()) || event == v8::ScriptCollected) {
ager@chromium.org5ec48922009-05-05 07:25:34 +00003122 return;
3123 }
ager@chromium.org41826e72009-03-30 13:30:57 +00003124
ager@chromium.org41826e72009-03-30 13:30:57 +00003125 v8::TryCatch try_catch;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003126
3127 // DebugCommandProcessor goes here.
3128 v8::Local<v8::Object> cmd_processor;
3129 {
3130 v8::Local<v8::Object> api_exec_state =
3131 v8::Utils::ToLocal(Handle<JSObject>::cast(exec_state));
machenbach@chromium.orgf9841892013-11-25 12:01:13 +00003132 v8::Local<v8::String> fun_name = v8::String::NewFromUtf8(
3133 isolate, "debugCommandProcessor");
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003134 v8::Local<v8::Function> fun =
danno@chromium.orgf95d4b92013-06-13 14:40:17 +00003135 v8::Local<v8::Function>::Cast(api_exec_state->Get(fun_name));
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003136
machenbach@chromium.org37be4082013-11-26 13:50:38 +00003137 v8::Handle<v8::Boolean> running = v8::Boolean::New(isolate, auto_continue);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003138 static const int kArgc = 1;
3139 v8::Handle<Value> argv[kArgc] = { running };
danno@chromium.orgf95d4b92013-06-13 14:40:17 +00003140 cmd_processor = v8::Local<v8::Object>::Cast(
3141 fun->Call(api_exec_state, kArgc, argv));
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003142 if (try_catch.HasCaught()) {
3143 PrintLn(try_catch.Exception());
3144 return;
3145 }
ager@chromium.org41826e72009-03-30 13:30:57 +00003146 }
3147
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003148 bool running = auto_continue;
3149
ager@chromium.org41826e72009-03-30 13:30:57 +00003150 // Process requests from the debugger.
3151 while (true) {
3152 // Wait for new command in the queue.
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003153 if (Debugger::host_dispatch_handler_) {
3154 // In case there is a host dispatch - do periodic dispatches.
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +00003155 if (!command_received_.WaitFor(host_dispatch_period_)) {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003156 // Timout expired, do the dispatch.
3157 Debugger::host_dispatch_handler_();
3158 continue;
3159 }
3160 } else {
3161 // In case there is no host dispatch - just wait.
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +00003162 command_received_.Wait();
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003163 }
ager@chromium.org41826e72009-03-30 13:30:57 +00003164
ager@chromium.org41826e72009-03-30 13:30:57 +00003165 // Get the command from the queue.
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003166 CommandMessage command = command_queue_.Get();
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00003167 isolate_->logger()->DebugTag(
3168 "Got request from command queue, in interactive loop.");
ager@chromium.org71daaf62009-04-01 07:22:49 +00003169 if (!Debugger::IsDebuggerActive()) {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003170 // Delete command text and user data.
3171 command.Dispose();
ager@chromium.org41826e72009-03-30 13:30:57 +00003172 return;
3173 }
3174
ager@chromium.org41826e72009-03-30 13:30:57 +00003175 // Invoke JavaScript to process the debug request.
3176 v8::Local<v8::String> fun_name;
3177 v8::Local<v8::Function> fun;
3178 v8::Local<v8::Value> request;
3179 v8::TryCatch try_catch;
machenbach@chromium.orgf9841892013-11-25 12:01:13 +00003180 fun_name = v8::String::NewFromUtf8(isolate, "processDebugRequest");
danno@chromium.orgf95d4b92013-06-13 14:40:17 +00003181 fun = v8::Local<v8::Function>::Cast(cmd_processor->Get(fun_name));
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003182
machenbach@chromium.orgf9841892013-11-25 12:01:13 +00003183 request = v8::String::NewFromTwoByte(isolate, command.text().start(),
3184 v8::String::kNormalString,
3185 command.text().length());
ager@chromium.org41826e72009-03-30 13:30:57 +00003186 static const int kArgc = 1;
3187 v8::Handle<Value> argv[kArgc] = { request };
3188 v8::Local<v8::Value> response_val = fun->Call(cmd_processor, kArgc, argv);
3189
3190 // Get the response.
3191 v8::Local<v8::String> response;
ager@chromium.org41826e72009-03-30 13:30:57 +00003192 if (!try_catch.HasCaught()) {
3193 // Get response string.
3194 if (!response_val->IsUndefined()) {
danno@chromium.orgf95d4b92013-06-13 14:40:17 +00003195 response = v8::Local<v8::String>::Cast(response_val);
ager@chromium.org41826e72009-03-30 13:30:57 +00003196 } else {
machenbach@chromium.orgf9841892013-11-25 12:01:13 +00003197 response = v8::String::NewFromUtf8(isolate, "");
ager@chromium.org41826e72009-03-30 13:30:57 +00003198 }
3199
3200 // Log the JSON request/response.
3201 if (FLAG_trace_debug_json) {
3202 PrintLn(request);
3203 PrintLn(response);
3204 }
3205
3206 // Get the running state.
machenbach@chromium.orgf9841892013-11-25 12:01:13 +00003207 fun_name = v8::String::NewFromUtf8(isolate, "isRunning");
danno@chromium.orgf95d4b92013-06-13 14:40:17 +00003208 fun = v8::Local<v8::Function>::Cast(cmd_processor->Get(fun_name));
ager@chromium.org41826e72009-03-30 13:30:57 +00003209 static const int kArgc = 1;
3210 v8::Handle<Value> argv[kArgc] = { response };
3211 v8::Local<v8::Value> running_val = fun->Call(cmd_processor, kArgc, argv);
3212 if (!try_catch.HasCaught()) {
3213 running = running_val->ToBoolean()->Value();
3214 }
3215 } else {
3216 // In case of failure the result text is the exception text.
3217 response = try_catch.Exception()->ToString();
3218 }
3219
ager@chromium.org41826e72009-03-30 13:30:57 +00003220 // Return the result.
ager@chromium.org5ec48922009-05-05 07:25:34 +00003221 MessageImpl message = MessageImpl::NewResponse(
3222 event,
3223 running,
3224 Handle<JSObject>::cast(exec_state),
3225 Handle<JSObject>::cast(event_data),
3226 Handle<String>(Utils::OpenHandle(*response)),
3227 command.client_data());
3228 InvokeMessageHandler(message);
3229 command.Dispose();
ager@chromium.org41826e72009-03-30 13:30:57 +00003230
3231 // Return from debug event processing if either the VM is put into the
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00003232 // running state (through a continue command) or auto continue is active
ager@chromium.org41826e72009-03-30 13:30:57 +00003233 // and there are no more commands queued.
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003234 if (running && !HasCommands()) {
ager@chromium.org41826e72009-03-30 13:30:57 +00003235 return;
3236 }
3237 }
3238}
3239
3240
iposva@chromium.org245aa852009-02-10 00:49:54 +00003241void Debugger::SetEventListener(Handle<Object> callback,
3242 Handle<Object> data) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00003243 HandleScope scope(isolate_);
3244 GlobalHandles* global_handles = isolate_->global_handles();
iposva@chromium.org245aa852009-02-10 00:49:54 +00003245
3246 // Clear the global handles for the event listener and the event listener data
3247 // object.
3248 if (!event_listener_.is_null()) {
hpayer@chromium.org4f99be92013-12-18 16:23:55 +00003249 GlobalHandles::Destroy(
iposva@chromium.org245aa852009-02-10 00:49:54 +00003250 reinterpret_cast<Object**>(event_listener_.location()));
3251 event_listener_ = Handle<Object>();
3252 }
3253 if (!event_listener_data_.is_null()) {
hpayer@chromium.org4f99be92013-12-18 16:23:55 +00003254 GlobalHandles::Destroy(
iposva@chromium.org245aa852009-02-10 00:49:54 +00003255 reinterpret_cast<Object**>(event_listener_data_.location()));
3256 event_listener_data_ = Handle<Object>();
3257 }
3258
3259 // If there is a new debug event listener register it together with its data
3260 // object.
3261 if (!callback->IsUndefined() && !callback->IsNull()) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003262 event_listener_ = Handle<Object>::cast(
lrn@chromium.org7516f052011-03-30 08:52:27 +00003263 global_handles->Create(*callback));
iposva@chromium.org245aa852009-02-10 00:49:54 +00003264 if (data.is_null()) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00003265 data = isolate_->factory()->undefined_value();
iposva@chromium.org245aa852009-02-10 00:49:54 +00003266 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003267 event_listener_data_ = Handle<Object>::cast(
lrn@chromium.org7516f052011-03-30 08:52:27 +00003268 global_handles->Create(*data));
iposva@chromium.org245aa852009-02-10 00:49:54 +00003269 }
3270
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003271 ListenersChanged();
iposva@chromium.org245aa852009-02-10 00:49:54 +00003272}
3273
3274
ager@chromium.org5ec48922009-05-05 07:25:34 +00003275void Debugger::SetMessageHandler(v8::Debug::MessageHandler2 handler) {
jkummerow@chromium.orgdc94e192013-08-30 11:35:42 +00003276 LockGuard<RecursiveMutex> with(debugger_access_);
ager@chromium.org71daaf62009-04-01 07:22:49 +00003277
ager@chromium.org381abbb2009-02-25 13:23:22 +00003278 message_handler_ = handler;
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003279 ListenersChanged();
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003280 if (handler == NULL) {
ager@chromium.org71daaf62009-04-01 07:22:49 +00003281 // Send an empty command to the debugger if in a break to make JavaScript
3282 // run again if the debugger is closed.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003283 if (isolate_->debug()->InDebugger()) {
ager@chromium.org71daaf62009-04-01 07:22:49 +00003284 ProcessCommand(Vector<const uint16_t>::empty());
3285 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003286 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003287}
3288
3289
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003290void Debugger::ListenersChanged() {
3291 if (IsDebuggerActive()) {
3292 // Disable the compilation cache when the debugger is active.
lrn@chromium.org7516f052011-03-30 08:52:27 +00003293 isolate_->compilation_cache()->Disable();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003294 debugger_unload_pending_ = false;
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003295 } else {
lrn@chromium.org7516f052011-03-30 08:52:27 +00003296 isolate_->compilation_cache()->Enable();
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003297 // Unload the debugger if event listener and message handler cleared.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003298 // Schedule this for later, because we may be in non-V8 thread.
3299 debugger_unload_pending_ = true;
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003300 }
3301}
3302
3303
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003304void Debugger::SetHostDispatchHandler(v8::Debug::HostDispatchHandler handler,
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +00003305 TimeDelta period) {
ager@chromium.org381abbb2009-02-25 13:23:22 +00003306 host_dispatch_handler_ = handler;
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +00003307 host_dispatch_period_ = period;
ager@chromium.org381abbb2009-02-25 13:23:22 +00003308}
3309
3310
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003311void Debugger::SetDebugMessageDispatchHandler(
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003312 v8::Debug::DebugMessageDispatchHandler handler, bool provide_locker) {
jkummerow@chromium.orgdc94e192013-08-30 11:35:42 +00003313 LockGuard<Mutex> lock_guard(&dispatch_handler_access_);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003314 debug_message_dispatch_handler_ = handler;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003315
3316 if (provide_locker && message_dispatch_helper_thread_ == NULL) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003317 message_dispatch_helper_thread_ = new MessageDispatchHelperThread(isolate_);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003318 message_dispatch_helper_thread_->Start();
3319 }
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003320}
3321
3322
ager@chromium.org41826e72009-03-30 13:30:57 +00003323// Calls the registered debug message handler. This callback is part of the
ager@chromium.org5ec48922009-05-05 07:25:34 +00003324// public API.
3325void Debugger::InvokeMessageHandler(MessageImpl message) {
jkummerow@chromium.orgdc94e192013-08-30 11:35:42 +00003326 LockGuard<RecursiveMutex> with(debugger_access_);
ager@chromium.org71daaf62009-04-01 07:22:49 +00003327
ager@chromium.org381abbb2009-02-25 13:23:22 +00003328 if (message_handler_ != NULL) {
ager@chromium.org5ec48922009-05-05 07:25:34 +00003329 message_handler_(message);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003330 }
ager@chromium.org41826e72009-03-30 13:30:57 +00003331}
3332
3333
3334// Puts a command coming from the public API on the queue. Creates
3335// a copy of the command string managed by the debugger. Up to this
3336// point, the command data was managed by the API client. Called
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003337// by the API client thread.
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003338void Debugger::ProcessCommand(Vector<const uint16_t> command,
3339 v8::Debug::ClientData* client_data) {
3340 // Need to cast away const.
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003341 CommandMessage message = CommandMessage::New(
ager@chromium.org41826e72009-03-30 13:30:57 +00003342 Vector<uint16_t>(const_cast<uint16_t*>(command.start()),
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003343 command.length()),
3344 client_data);
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00003345 isolate_->logger()->DebugTag("Put command on command_queue.");
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003346 command_queue_.Put(message);
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +00003347 command_received_.Signal();
sgjesse@chromium.org3afc1582009-04-16 22:31:44 +00003348
3349 // Set the debug command break flag to have the command processed.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003350 if (!isolate_->debug()->InDebugger()) {
3351 isolate_->stack_guard()->DebugCommand();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003352 }
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003353
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003354 MessageDispatchHelperThread* dispatch_thread;
3355 {
jkummerow@chromium.orgdc94e192013-08-30 11:35:42 +00003356 LockGuard<Mutex> lock_guard(&dispatch_handler_access_);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003357 dispatch_thread = message_dispatch_helper_thread_;
3358 }
3359
3360 if (dispatch_thread == NULL) {
3361 CallMessageDispatchHandler();
3362 } else {
3363 dispatch_thread->Schedule();
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003364 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003365}
3366
3367
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003368bool Debugger::HasCommands() {
ager@chromium.org41826e72009-03-30 13:30:57 +00003369 return !command_queue_.IsEmpty();
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003370}
3371
3372
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00003373void Debugger::EnqueueDebugCommand(v8::Debug::ClientData* client_data) {
3374 CommandMessage message = CommandMessage::New(Vector<uint16_t>(), client_data);
3375 event_command_queue_.Put(message);
3376
3377 // Set the debug command break flag to have the command processed.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003378 if (!isolate_->debug()->InDebugger()) {
3379 isolate_->stack_guard()->DebugCommand();
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00003380 }
3381}
3382
3383
ager@chromium.org71daaf62009-04-01 07:22:49 +00003384bool Debugger::IsDebuggerActive() {
jkummerow@chromium.orgdc94e192013-08-30 11:35:42 +00003385 LockGuard<RecursiveMutex> with(debugger_access_);
ager@chromium.org71daaf62009-04-01 07:22:49 +00003386
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00003387 return message_handler_ != NULL ||
3388 !event_listener_.is_null() ||
3389 force_debugger_active_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003390}
3391
3392
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003393Handle<Object> Debugger::Call(Handle<JSFunction> fun,
3394 Handle<Object> data,
3395 bool* pending_exception) {
ager@chromium.org71daaf62009-04-01 07:22:49 +00003396 // When calling functions in the debugger prevent it from beeing unloaded.
3397 Debugger::never_unload_debugger_ = true;
3398
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003399 // Enter the debugger.
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +00003400 EnterDebugger debugger(isolate_);
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +00003401 if (debugger.FailedToEnter()) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00003402 return isolate_->factory()->undefined_value();
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003403 }
3404
3405 // Create the execution state.
3406 bool caught_exception = false;
3407 Handle<Object> exec_state = MakeExecutionState(&caught_exception);
3408 if (caught_exception) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00003409 return isolate_->factory()->undefined_value();
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003410 }
3411
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00003412 Handle<Object> argv[] = { exec_state, data };
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +00003413 Handle<Object> result = Execution::Call(
jkummerow@chromium.org2c9426b2013-09-05 16:31:13 +00003414 isolate_,
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +00003415 fun,
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00003416 Handle<Object>(isolate_->debug()->debug_context_->global_proxy(),
3417 isolate_),
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00003418 ARRAY_SIZE(argv),
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +00003419 argv,
3420 pending_exception);
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003421 return result;
3422}
3423
3424
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003425static void StubMessageHandler2(const v8::Debug::Message& message) {
3426 // Simply ignore message.
3427}
3428
3429
3430bool Debugger::StartAgent(const char* name, int port,
3431 bool wait_for_connection) {
3432 if (wait_for_connection) {
3433 // Suspend V8 if it is already running or set V8 to suspend whenever
3434 // it starts.
3435 // Provide stub message handler; V8 auto-continues each suspend
3436 // when there is no message handler; we doesn't need it.
3437 // Once become suspended, V8 will stay so indefinitely long, until remote
3438 // debugger connects and issues "continue" command.
3439 Debugger::message_handler_ = StubMessageHandler2;
3440 v8::Debug::DebugBreak();
3441 }
3442
jkummerow@chromium.org3d00d0a2013-09-04 13:57:32 +00003443 if (agent_ == NULL) {
3444 agent_ = new DebuggerAgent(isolate_, name, port);
3445 agent_->Start();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003446 }
jkummerow@chromium.org3d00d0a2013-09-04 13:57:32 +00003447 return true;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003448}
3449
3450
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003451void Debugger::StopAgent() {
3452 if (agent_ != NULL) {
3453 agent_->Shutdown();
3454 agent_->Join();
3455 delete agent_;
3456 agent_ = NULL;
3457 }
3458}
3459
3460
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003461void Debugger::WaitForAgent() {
3462 if (agent_ != NULL)
3463 agent_->WaitUntilListening();
3464}
3465
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003466
3467void Debugger::CallMessageDispatchHandler() {
3468 v8::Debug::DebugMessageDispatchHandler handler;
3469 {
jkummerow@chromium.orgdc94e192013-08-30 11:35:42 +00003470 LockGuard<Mutex> lock_guard(&dispatch_handler_access_);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003471 handler = Debugger::debug_message_dispatch_handler_;
3472 }
3473 if (handler != NULL) {
3474 handler();
3475 }
3476}
3477
3478
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +00003479EnterDebugger::EnterDebugger(Isolate* isolate)
3480 : isolate_(isolate),
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003481 prev_(isolate_->debug()->debugger_entry()),
3482 it_(isolate_),
3483 has_js_frames_(!it_.done()),
3484 save_(isolate_) {
3485 Debug* debug = isolate_->debug();
3486 ASSERT(prev_ != NULL || !debug->is_interrupt_pending(PREEMPT));
3487 ASSERT(prev_ != NULL || !debug->is_interrupt_pending(DEBUGBREAK));
3488
3489 // Link recursive debugger entry.
3490 debug->set_debugger_entry(this);
3491
3492 // Store the previous break id and frame id.
3493 break_id_ = debug->break_id();
3494 break_frame_id_ = debug->break_frame_id();
3495
3496 // Create the new break info. If there is no JavaScript frames there is no
3497 // break frame id.
3498 if (has_js_frames_) {
3499 debug->NewBreak(it_.frame()->id());
3500 } else {
3501 debug->NewBreak(StackFrame::NO_ID);
3502 }
3503
3504 // Make sure that debugger is loaded and enter the debugger context.
3505 load_failed_ = !debug->Load();
3506 if (!load_failed_) {
3507 // NOTE the member variable save which saves the previous context before
3508 // this change.
3509 isolate_->set_context(*debug->debug_context());
3510 }
3511}
3512
3513
3514EnterDebugger::~EnterDebugger() {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003515 Debug* debug = isolate_->debug();
3516
3517 // Restore to the previous break state.
3518 debug->SetBreak(break_frame_id_, break_id_);
3519
3520 // Check for leaving the debugger.
fschneider@chromium.org7d10be52012-04-10 12:30:14 +00003521 if (!load_failed_ && prev_ == NULL) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003522 // Clear mirror cache when leaving the debugger. Skip this if there is a
3523 // pending exception as clearing the mirror cache calls back into
3524 // JavaScript. This can happen if the v8::Debug::Call is used in which
3525 // case the exception should end up in the calling code.
3526 if (!isolate_->has_pending_exception()) {
3527 // Try to avoid any pending debug break breaking in the clear mirror
3528 // cache JavaScript code.
3529 if (isolate_->stack_guard()->IsDebugBreak()) {
3530 debug->set_interrupts_pending(DEBUGBREAK);
3531 isolate_->stack_guard()->Continue(DEBUGBREAK);
3532 }
3533 debug->ClearMirrorCache();
3534 }
3535
3536 // Request preemption and debug break when leaving the last debugger entry
3537 // if any of these where recorded while debugging.
3538 if (debug->is_interrupt_pending(PREEMPT)) {
3539 // This re-scheduling of preemption is to avoid starvation in some
3540 // debugging scenarios.
3541 debug->clear_interrupt_pending(PREEMPT);
3542 isolate_->stack_guard()->Preempt();
3543 }
3544 if (debug->is_interrupt_pending(DEBUGBREAK)) {
3545 debug->clear_interrupt_pending(DEBUGBREAK);
3546 isolate_->stack_guard()->DebugBreak();
3547 }
3548
3549 // If there are commands in the queue when leaving the debugger request
3550 // that these commands are processed.
3551 if (isolate_->debugger()->HasCommands()) {
3552 isolate_->stack_guard()->DebugCommand();
3553 }
3554
3555 // If leaving the debugger with the debugger no longer active unload it.
3556 if (!isolate_->debugger()->IsDebuggerActive()) {
3557 isolate_->debugger()->UnloadDebugger();
3558 }
3559 }
3560
3561 // Leaving this debugger entry.
3562 debug->set_debugger_entry(prev_);
3563}
3564
3565
ager@chromium.org5ec48922009-05-05 07:25:34 +00003566MessageImpl MessageImpl::NewEvent(DebugEvent event,
3567 bool running,
3568 Handle<JSObject> exec_state,
3569 Handle<JSObject> event_data) {
3570 MessageImpl message(true, event, running,
3571 exec_state, event_data, Handle<String>(), NULL);
3572 return message;
3573}
3574
3575
3576MessageImpl MessageImpl::NewResponse(DebugEvent event,
3577 bool running,
3578 Handle<JSObject> exec_state,
3579 Handle<JSObject> event_data,
3580 Handle<String> response_json,
3581 v8::Debug::ClientData* client_data) {
3582 MessageImpl message(false, event, running,
3583 exec_state, event_data, response_json, client_data);
3584 return message;
3585}
3586
3587
3588MessageImpl::MessageImpl(bool is_event,
3589 DebugEvent event,
3590 bool running,
3591 Handle<JSObject> exec_state,
3592 Handle<JSObject> event_data,
3593 Handle<String> response_json,
3594 v8::Debug::ClientData* client_data)
3595 : is_event_(is_event),
3596 event_(event),
3597 running_(running),
3598 exec_state_(exec_state),
3599 event_data_(event_data),
3600 response_json_(response_json),
3601 client_data_(client_data) {}
3602
3603
3604bool MessageImpl::IsEvent() const {
3605 return is_event_;
3606}
3607
3608
3609bool MessageImpl::IsResponse() const {
3610 return !is_event_;
3611}
3612
3613
3614DebugEvent MessageImpl::GetEvent() const {
3615 return event_;
3616}
3617
3618
3619bool MessageImpl::WillStartRunning() const {
3620 return running_;
3621}
3622
3623
3624v8::Handle<v8::Object> MessageImpl::GetExecutionState() const {
3625 return v8::Utils::ToLocal(exec_state_);
3626}
3627
3628
hpayer@chromium.orgc5d49712013-09-11 08:25:48 +00003629v8::Isolate* MessageImpl::GetIsolate() const {
3630 return reinterpret_cast<v8::Isolate*>(exec_state_->GetIsolate());
3631}
3632
3633
ager@chromium.org5ec48922009-05-05 07:25:34 +00003634v8::Handle<v8::Object> MessageImpl::GetEventData() const {
3635 return v8::Utils::ToLocal(event_data_);
3636}
3637
3638
3639v8::Handle<v8::String> MessageImpl::GetJSON() const {
machenbach@chromium.orgce9c5142013-12-03 08:00:39 +00003640 v8::EscapableHandleScope scope(
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +00003641 reinterpret_cast<v8::Isolate*>(event_data_->GetIsolate()));
ager@chromium.org5ec48922009-05-05 07:25:34 +00003642
3643 if (IsEvent()) {
3644 // Call toJSONProtocol on the debug event object.
3645 Handle<Object> fun = GetProperty(event_data_, "toJSONProtocol");
3646 if (!fun->IsJSFunction()) {
3647 return v8::Handle<v8::String>();
3648 }
3649 bool caught_exception;
3650 Handle<Object> json = Execution::TryCall(Handle<JSFunction>::cast(fun),
3651 event_data_,
3652 0, NULL, &caught_exception);
3653 if (caught_exception || !json->IsString()) {
3654 return v8::Handle<v8::String>();
3655 }
machenbach@chromium.orgce9c5142013-12-03 08:00:39 +00003656 return scope.Escape(v8::Utils::ToLocal(Handle<String>::cast(json)));
ager@chromium.org5ec48922009-05-05 07:25:34 +00003657 } else {
3658 return v8::Utils::ToLocal(response_json_);
3659 }
3660}
3661
3662
3663v8::Handle<v8::Context> MessageImpl::GetEventContext() const {
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +00003664 Isolate* isolate = event_data_->GetIsolate();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003665 v8::Handle<v8::Context> context = GetDebugEventContext(isolate);
3666 // Isolate::context() may be NULL when "script collected" event occures.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003667 ASSERT(!context.IsEmpty() || event_ == v8::ScriptCollected);
rossberg@chromium.org717967f2011-07-20 13:44:42 +00003668 return context;
ager@chromium.org5ec48922009-05-05 07:25:34 +00003669}
3670
3671
3672v8::Debug::ClientData* MessageImpl::GetClientData() const {
3673 return client_data_;
3674}
3675
3676
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003677EventDetailsImpl::EventDetailsImpl(DebugEvent event,
3678 Handle<JSObject> exec_state,
3679 Handle<JSObject> event_data,
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00003680 Handle<Object> callback_data,
3681 v8::Debug::ClientData* client_data)
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003682 : event_(event),
3683 exec_state_(exec_state),
3684 event_data_(event_data),
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00003685 callback_data_(callback_data),
3686 client_data_(client_data) {}
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003687
3688
3689DebugEvent EventDetailsImpl::GetEvent() const {
3690 return event_;
3691}
3692
3693
3694v8::Handle<v8::Object> EventDetailsImpl::GetExecutionState() const {
3695 return v8::Utils::ToLocal(exec_state_);
3696}
3697
3698
3699v8::Handle<v8::Object> EventDetailsImpl::GetEventData() const {
3700 return v8::Utils::ToLocal(event_data_);
3701}
3702
3703
3704v8::Handle<v8::Context> EventDetailsImpl::GetEventContext() const {
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +00003705 return GetDebugEventContext(exec_state_->GetIsolate());
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003706}
3707
3708
3709v8::Handle<v8::Value> EventDetailsImpl::GetCallbackData() const {
3710 return v8::Utils::ToLocal(callback_data_);
3711}
3712
3713
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00003714v8::Debug::ClientData* EventDetailsImpl::GetClientData() const {
3715 return client_data_;
3716}
3717
3718
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003719CommandMessage::CommandMessage() : text_(Vector<uint16_t>::empty()),
3720 client_data_(NULL) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003721}
3722
3723
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003724CommandMessage::CommandMessage(const Vector<uint16_t>& text,
3725 v8::Debug::ClientData* data)
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003726 : text_(text),
3727 client_data_(data) {
3728}
3729
3730
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003731CommandMessage::~CommandMessage() {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003732}
3733
3734
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003735void CommandMessage::Dispose() {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003736 text_.Dispose();
3737 delete client_data_;
3738 client_data_ = NULL;
3739}
3740
3741
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003742CommandMessage CommandMessage::New(const Vector<uint16_t>& command,
3743 v8::Debug::ClientData* data) {
3744 return CommandMessage(command.Clone(), data);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003745}
3746
3747
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003748CommandMessageQueue::CommandMessageQueue(int size) : start_(0), end_(0),
3749 size_(size) {
3750 messages_ = NewArray<CommandMessage>(size);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003751}
3752
3753
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003754CommandMessageQueue::~CommandMessageQueue() {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003755 while (!IsEmpty()) {
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003756 CommandMessage m = Get();
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003757 m.Dispose();
3758 }
kasper.lund7276f142008-07-30 08:49:36 +00003759 DeleteArray(messages_);
3760}
3761
3762
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003763CommandMessage CommandMessageQueue::Get() {
kasper.lund7276f142008-07-30 08:49:36 +00003764 ASSERT(!IsEmpty());
3765 int result = start_;
3766 start_ = (start_ + 1) % size_;
3767 return messages_[result];
3768}
3769
3770
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003771void CommandMessageQueue::Put(const CommandMessage& message) {
kasper.lund7276f142008-07-30 08:49:36 +00003772 if ((end_ + 1) % size_ == start_) {
3773 Expand();
3774 }
3775 messages_[end_] = message;
3776 end_ = (end_ + 1) % size_;
3777}
3778
3779
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003780void CommandMessageQueue::Expand() {
3781 CommandMessageQueue new_queue(size_ * 2);
kasper.lund7276f142008-07-30 08:49:36 +00003782 while (!IsEmpty()) {
3783 new_queue.Put(Get());
3784 }
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003785 CommandMessage* array_to_free = messages_;
kasper.lund7276f142008-07-30 08:49:36 +00003786 *this = new_queue;
3787 new_queue.messages_ = array_to_free;
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003788 // Make the new_queue empty so that it doesn't call Dispose on any messages.
3789 new_queue.start_ = new_queue.end_;
kasper.lund7276f142008-07-30 08:49:36 +00003790 // Automatic destructor called on new_queue, freeing array_to_free.
3791}
3792
3793
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00003794LockingCommandMessageQueue::LockingCommandMessageQueue(Logger* logger, int size)
jkummerow@chromium.orgdc94e192013-08-30 11:35:42 +00003795 : logger_(logger), queue_(size) {}
kasper.lund7276f142008-07-30 08:49:36 +00003796
3797
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003798bool LockingCommandMessageQueue::IsEmpty() const {
jkummerow@chromium.orgdc94e192013-08-30 11:35:42 +00003799 LockGuard<Mutex> lock_guard(&mutex_);
kasper.lund7276f142008-07-30 08:49:36 +00003800 return queue_.IsEmpty();
3801}
3802
3803
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003804CommandMessage LockingCommandMessageQueue::Get() {
jkummerow@chromium.orgdc94e192013-08-30 11:35:42 +00003805 LockGuard<Mutex> lock_guard(&mutex_);
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003806 CommandMessage result = queue_.Get();
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00003807 logger_->DebugEvent("Get", result.text());
kasper.lund7276f142008-07-30 08:49:36 +00003808 return result;
3809}
3810
3811
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003812void LockingCommandMessageQueue::Put(const CommandMessage& message) {
jkummerow@chromium.orgdc94e192013-08-30 11:35:42 +00003813 LockGuard<Mutex> lock_guard(&mutex_);
kasper.lund7276f142008-07-30 08:49:36 +00003814 queue_.Put(message);
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00003815 logger_->DebugEvent("Put", message.text());
kasper.lund7276f142008-07-30 08:49:36 +00003816}
3817
3818
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003819void LockingCommandMessageQueue::Clear() {
jkummerow@chromium.orgdc94e192013-08-30 11:35:42 +00003820 LockGuard<Mutex> lock_guard(&mutex_);
kasper.lund7276f142008-07-30 08:49:36 +00003821 queue_.Clear();
3822}
3823
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003824
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003825MessageDispatchHelperThread::MessageDispatchHelperThread(Isolate* isolate)
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00003826 : Thread("v8:MsgDispHelpr"),
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +00003827 isolate_(isolate), sem_(0),
jkummerow@chromium.orgdc94e192013-08-30 11:35:42 +00003828 already_signalled_(false) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003829}
3830
3831
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003832void MessageDispatchHelperThread::Schedule() {
3833 {
jkummerow@chromium.orgdc94e192013-08-30 11:35:42 +00003834 LockGuard<Mutex> lock_guard(&mutex_);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003835 if (already_signalled_) {
3836 return;
3837 }
3838 already_signalled_ = true;
3839 }
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +00003840 sem_.Signal();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003841}
3842
3843
3844void MessageDispatchHelperThread::Run() {
3845 while (true) {
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +00003846 sem_.Wait();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003847 {
jkummerow@chromium.orgdc94e192013-08-30 11:35:42 +00003848 LockGuard<Mutex> lock_guard(&mutex_);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003849 already_signalled_ = false;
3850 }
3851 {
mstarzinger@chromium.orgf705b502013-04-04 11:38:09 +00003852 Locker locker(reinterpret_cast<v8::Isolate*>(isolate_));
3853 isolate_->debugger()->CallMessageDispatchHandler();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003854 }
3855 }
3856}
3857
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003858#endif // ENABLE_DEBUGGER_SUPPORT
kasper.lund7276f142008-07-30 08:49:36 +00003859
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003860} } // namespace v8::internal