blob: 5c04efa19d41c2f4f08a12f48aade6802b2b0b49 [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;
yangguo@chromium.org49546742013-12-23 16:17:49 +0000786 function_info = Compiler::CompileScript(source_code,
787 script_name, 0, 0,
788 false,
789 context,
790 NULL, NULL,
791 Handle<String>::null(),
792 NATIVES_CODE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000793
794 // Silently ignore stack overflows during compilation.
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000795 if (function_info.is_null()) {
lrn@chromium.org7516f052011-03-30 08:52:27 +0000796 ASSERT(isolate->has_pending_exception());
797 isolate->clear_pending_exception();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000798 return false;
799 }
800
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000801 // Execute the shared function in the debugger context.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000802 bool caught_exception;
kasper.lund44510672008-07-25 07:37:58 +0000803 Handle<JSFunction> function =
lrn@chromium.org7516f052011-03-30 08:52:27 +0000804 factory->NewFunctionFromSharedFunctionInfo(function_info, context);
rossberg@chromium.org717967f2011-07-20 13:44:42 +0000805
fschneider@chromium.org7d10be52012-04-10 12:30:14 +0000806 Handle<Object> exception =
ulan@chromium.org09d7ab52013-02-25 15:50:35 +0000807 Execution::TryCall(function,
808 Handle<Object>(context->global_object(), isolate),
809 0,
810 NULL,
811 &caught_exception);
kasper.lund44510672008-07-25 07:37:58 +0000812
813 // Check for caught exceptions.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000814 if (caught_exception) {
fschneider@chromium.org7d10be52012-04-10 12:30:14 +0000815 ASSERT(!isolate->has_pending_exception());
816 MessageLocation computed_location;
817 isolate->ComputeLocation(&computed_location);
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000818 Handle<Object> message = MessageHandler::MakeMessageObject(
verwaest@chromium.orgd4be0f02013-06-05 13:39:03 +0000819 isolate, "error_loading_debugger", &computed_location,
fschneider@chromium.org7d10be52012-04-10 12:30:14 +0000820 Vector<Handle<Object> >::empty(), Handle<String>(), Handle<JSArray>());
821 ASSERT(!isolate->has_pending_exception());
mmassi@chromium.org49a44672012-12-04 13:52:03 +0000822 if (!exception.is_null()) {
823 isolate->set_pending_exception(*exception);
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +0000824 MessageHandler::ReportMessage(isolate, NULL, message);
mmassi@chromium.org49a44672012-12-04 13:52:03 +0000825 isolate->clear_pending_exception();
826 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000827 return false;
828 }
829
kasper.lund44510672008-07-25 07:37:58 +0000830 // Mark this script as native and return successfully.
831 Handle<Script> script(Script::cast(function->shared()->script()));
ager@chromium.orge2902be2009-06-08 12:21:35 +0000832 script->set_type(Smi::FromInt(Script::TYPE_NATIVE));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000833 return true;
834}
835
836
837bool Debug::Load() {
838 // Return if debugger is already loaded.
kasper.lund212ac232008-07-16 07:07:30 +0000839 if (IsLoaded()) return true;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000840
lrn@chromium.org7516f052011-03-30 08:52:27 +0000841 Debugger* debugger = isolate_->debugger();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000842
kasper.lund44510672008-07-25 07:37:58 +0000843 // Bail out if we're already in the process of compiling the native
844 // JavaScript source code for the debugger.
lrn@chromium.org7516f052011-03-30 08:52:27 +0000845 if (debugger->compiling_natives() ||
846 debugger->is_loading_debugger())
mads.s.agercbaa0602008-08-14 13:41:48 +0000847 return false;
lrn@chromium.org7516f052011-03-30 08:52:27 +0000848 debugger->set_loading_debugger(true);
kasper.lund44510672008-07-25 07:37:58 +0000849
850 // Disable breakpoints and interrupts while compiling and running the
851 // debugger scripts including the context creation code.
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +0000852 DisableBreak disable(isolate_, true);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000853 PostponeInterruptsScope postpone(isolate_);
kasper.lund44510672008-07-25 07:37:58 +0000854
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000855 // Create the debugger context.
lrn@chromium.org7516f052011-03-30 08:52:27 +0000856 HandleScope scope(isolate_);
kasper.lund44510672008-07-25 07:37:58 +0000857 Handle<Context> context =
lrn@chromium.org7516f052011-03-30 08:52:27 +0000858 isolate_->bootstrapper()->CreateEnvironment(
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000859 Handle<Object>::null(),
860 v8::Handle<ObjectTemplate>(),
861 NULL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000862
fschneider@chromium.org7d10be52012-04-10 12:30:14 +0000863 // Fail if no context could be created.
864 if (context.is_null()) return false;
865
kasper.lund44510672008-07-25 07:37:58 +0000866 // Use the debugger context.
lrn@chromium.org7516f052011-03-30 08:52:27 +0000867 SaveContext save(isolate_);
868 isolate_->set_context(*context);
kasper.lund44510672008-07-25 07:37:58 +0000869
870 // Expose the builtins object in the debugger context.
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000871 Handle<String> key = isolate_->factory()->InternalizeOneByteString(
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +0000872 STATIC_ASCII_VECTOR("builtins"));
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000873 Handle<GlobalObject> global = Handle<GlobalObject>(context->global_object());
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +0000874 RETURN_IF_EMPTY_HANDLE_VALUE(
lrn@chromium.org7516f052011-03-30 08:52:27 +0000875 isolate_,
ulan@chromium.org09d7ab52013-02-25 15:50:35 +0000876 JSReceiver::SetProperty(global,
877 key,
878 Handle<Object>(global->builtins(), isolate_),
879 NONE,
880 kNonStrictMode),
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +0000881 false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000882
883 // Compile the JavaScript for the debugger in the debugger context.
lrn@chromium.org7516f052011-03-30 08:52:27 +0000884 debugger->set_compiling_natives(true);
kasper.lund44510672008-07-25 07:37:58 +0000885 bool caught_exception =
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +0000886 !CompileDebuggerScript(isolate_, Natives::GetIndex("mirror")) ||
887 !CompileDebuggerScript(isolate_, Natives::GetIndex("debug"));
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000888
889 if (FLAG_enable_liveedit) {
890 caught_exception = caught_exception ||
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +0000891 !CompileDebuggerScript(isolate_, Natives::GetIndex("liveedit"));
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000892 }
893
lrn@chromium.org7516f052011-03-30 08:52:27 +0000894 debugger->set_compiling_natives(false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000895
mads.s.agercbaa0602008-08-14 13:41:48 +0000896 // Make sure we mark the debugger as not loading before we might
897 // return.
lrn@chromium.org7516f052011-03-30 08:52:27 +0000898 debugger->set_loading_debugger(false);
mads.s.agercbaa0602008-08-14 13:41:48 +0000899
kasper.lund44510672008-07-25 07:37:58 +0000900 // Check for caught exceptions.
901 if (caught_exception) return false;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000902
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000903 // Debugger loaded, create debugger context global handle.
904 debug_context_ = Handle<Context>::cast(
905 isolate_->global_handles()->Create(*context));
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000906
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000907 return true;
908}
909
910
911void Debug::Unload() {
912 // Return debugger is not loaded.
913 if (!IsLoaded()) {
914 return;
915 }
916
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000917 // Clear the script cache.
918 DestroyScriptCache();
919
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000920 // Clear debugger context global handle.
hpayer@chromium.org4f99be92013-12-18 16:23:55 +0000921 GlobalHandles::Destroy(reinterpret_cast<Object**>(debug_context_.location()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000922 debug_context_ = Handle<Context>();
923}
924
925
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000926// Set the flag indicating that preemption happened during debugging.
927void Debug::PreemptionWhileInDebugger() {
928 ASSERT(InDebugger());
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000929 Debug::set_interrupts_pending(PREEMPT);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000930}
931
932
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000933void Debug::Iterate(ObjectVisitor* v) {
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000934 v->VisitPointer(BitCast<Object**>(&(debug_break_return_)));
935 v->VisitPointer(BitCast<Object**>(&(debug_break_slot_)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000936}
937
938
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000939Object* Debug::Break(Arguments args) {
940 Heap* heap = isolate_->heap();
941 HandleScope scope(isolate_);
mads.s.ager31e71382008-08-13 09:32:07 +0000942 ASSERT(args.length() == 0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000943
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000944 thread_local_.frame_drop_mode_ = FRAMES_UNTOUCHED;
ager@chromium.org357bf652010-04-12 11:30:10 +0000945
kasper.lundbd3ec4e2008-07-09 11:06:54 +0000946 // Get the top-most JavaScript frame.
vegorov@chromium.org74f333b2011-04-06 11:17:46 +0000947 JavaScriptFrameIterator it(isolate_);
kasper.lundbd3ec4e2008-07-09 11:06:54 +0000948 JavaScriptFrame* frame = it.frame();
949
950 // Just continue if breaks are disabled or debugger cannot be loaded.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000951 if (disable_break() || !Load()) {
952 SetAfterBreakTarget(frame);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000953 return heap->undefined_value();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000954 }
955
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000956 // Enter the debugger.
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +0000957 EnterDebugger debugger(isolate_);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000958 if (debugger.FailedToEnter()) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000959 return heap->undefined_value();
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000960 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000961
kasper.lund44510672008-07-25 07:37:58 +0000962 // Postpone interrupt during breakpoint processing.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000963 PostponeInterruptsScope postpone(isolate_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000964
965 // Get the debug info (create it if it does not exist).
966 Handle<SharedFunctionInfo> shared =
danno@chromium.org169691d2013-07-15 08:01:13 +0000967 Handle<SharedFunctionInfo>(frame->function()->shared());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000968 Handle<DebugInfo> debug_info = GetDebugInfo(shared);
969
970 // Find the break point where execution has stopped.
971 BreakLocationIterator break_location_iterator(debug_info,
972 ALL_BREAK_LOCATIONS);
verwaest@chromium.org8a00e822013-06-10 15:11:22 +0000973 // pc points to the instruction after the current one, possibly a break
974 // location as well. So the "- 1" to exclude it from the search.
975 break_location_iterator.FindBreakLocationFromAddress(frame->pc() - 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000976
977 // Check whether step next reached a new statement.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000978 if (!StepNextContinue(&break_location_iterator, frame)) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000979 // Decrease steps left if performing multiple steps.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000980 if (thread_local_.step_count_ > 0) {
981 thread_local_.step_count_--;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000982 }
983 }
984
985 // If there is one or more real break points check whether any of these are
986 // triggered.
ulan@chromium.org09d7ab52013-02-25 15:50:35 +0000987 Handle<Object> break_points_hit(heap->undefined_value(), isolate_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000988 if (break_location_iterator.HasBreakPoint()) {
989 Handle<Object> break_point_objects =
ulan@chromium.org09d7ab52013-02-25 15:50:35 +0000990 Handle<Object>(break_location_iterator.BreakPointObjects(), isolate_);
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000991 break_points_hit = CheckBreakPoints(break_point_objects);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000992 }
993
ager@chromium.orga1645e22009-09-09 19:27:10 +0000994 // If step out is active skip everything until the frame where we need to step
995 // out to is reached, unless real breakpoint is hit.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000996 if (StepOutActive() && frame->fp() != step_out_fp() &&
ager@chromium.orga1645e22009-09-09 19:27:10 +0000997 break_points_hit->IsUndefined() ) {
998 // Step count should always be 0 for StepOut.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000999 ASSERT(thread_local_.step_count_ == 0);
ager@chromium.orga1645e22009-09-09 19:27:10 +00001000 } else if (!break_points_hit->IsUndefined() ||
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001001 (thread_local_.last_step_action_ != StepNone &&
1002 thread_local_.step_count_ == 0)) {
ager@chromium.orga1645e22009-09-09 19:27:10 +00001003 // Notify debugger if a real break point is triggered or if performing
1004 // single stepping with no more steps to perform. Otherwise do another step.
1005
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001006 // Clear all current stepping setup.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001007 ClearStepping();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001008
lrn@chromium.org34e60782011-09-15 07:25:40 +00001009 if (thread_local_.queued_step_count_ > 0) {
1010 // Perform queued steps
1011 int step_count = thread_local_.queued_step_count_;
1012
1013 // Clear queue
1014 thread_local_.queued_step_count_ = 0;
1015
dslomov@chromium.org639bac02013-09-09 11:58:54 +00001016 PrepareStep(StepNext, step_count, StackFrame::NO_ID);
lrn@chromium.org34e60782011-09-15 07:25:40 +00001017 } else {
1018 // Notify the debug event listeners.
1019 isolate_->debugger()->OnDebugBreak(break_points_hit, false);
1020 }
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001021 } else if (thread_local_.last_step_action_ != StepNone) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001022 // Hold on to last step action as it is cleared by the call to
1023 // ClearStepping.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001024 StepAction step_action = thread_local_.last_step_action_;
1025 int step_count = thread_local_.step_count_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001026
lrn@chromium.org34e60782011-09-15 07:25:40 +00001027 // If StepNext goes deeper in code, StepOut until original frame
1028 // and keep step count queued up in the meantime.
1029 if (step_action == StepNext && frame->fp() < thread_local_.last_fp_) {
1030 // Count frames until target frame
1031 int count = 0;
1032 JavaScriptFrameIterator it(isolate_);
jkummerow@chromium.org212d9642012-05-11 15:02:09 +00001033 while (!it.done() && it.frame()->fp() < thread_local_.last_fp_) {
lrn@chromium.org34e60782011-09-15 07:25:40 +00001034 count++;
1035 it.Advance();
1036 }
1037
danno@chromium.org81cac2b2012-07-10 11:28:27 +00001038 // Check that we indeed found the frame we are looking for.
1039 CHECK(!it.done() && (it.frame()->fp() == thread_local_.last_fp_));
1040 if (step_count > 1) {
1041 // Save old count and action to continue stepping after StepOut.
1042 thread_local_.queued_step_count_ = step_count - 1;
jkummerow@chromium.org212d9642012-05-11 15:02:09 +00001043 }
1044
danno@chromium.org81cac2b2012-07-10 11:28:27 +00001045 // Set up for StepOut to reach target frame.
1046 step_action = StepOut;
1047 step_count = count;
lrn@chromium.org34e60782011-09-15 07:25:40 +00001048 }
1049
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001050 // Clear all current stepping setup.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001051 ClearStepping();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001052
1053 // Set up for the remaining steps.
dslomov@chromium.org639bac02013-09-09 11:58:54 +00001054 PrepareStep(step_action, step_count, StackFrame::NO_ID);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001055 }
1056
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001057 if (thread_local_.frame_drop_mode_ == FRAMES_UNTOUCHED) {
1058 SetAfterBreakTarget(frame);
1059 } else if (thread_local_.frame_drop_mode_ ==
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001060 FRAME_DROPPED_IN_IC_CALL) {
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001061 // We must have been calling IC stub. Do not go there anymore.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001062 Code* plain_return = isolate_->builtins()->builtin(
1063 Builtins::kPlainReturn_LiveEdit);
1064 thread_local_.after_break_target_ = plain_return->entry();
1065 } else if (thread_local_.frame_drop_mode_ ==
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001066 FRAME_DROPPED_IN_DEBUG_SLOT_CALL) {
1067 // Debug break slot stub does not return normally, instead it manually
1068 // cleans the stack and jumps. We should patch the jump address.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001069 Code* plain_return = isolate_->builtins()->builtin(
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00001070 Builtins::kFrameDropper_LiveEdit);
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001071 thread_local_.after_break_target_ = plain_return->entry();
1072 } else if (thread_local_.frame_drop_mode_ ==
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001073 FRAME_DROPPED_IN_DIRECT_CALL) {
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001074 // Nothing to do, after_break_target is not used here.
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00001075 } else if (thread_local_.frame_drop_mode_ ==
1076 FRAME_DROPPED_IN_RETURN_CALL) {
1077 Code* plain_return = isolate_->builtins()->builtin(
1078 Builtins::kFrameDropper_LiveEdit);
1079 thread_local_.after_break_target_ = plain_return->entry();
ager@chromium.org357bf652010-04-12 11:30:10 +00001080 } else {
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001081 UNREACHABLE();
ager@chromium.org357bf652010-04-12 11:30:10 +00001082 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001083
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001084 return heap->undefined_value();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001085}
1086
1087
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001088RUNTIME_FUNCTION(Object*, Debug_Break) {
1089 return isolate->debug()->Break(args);
1090}
1091
1092
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001093// Check the break point objects for whether one or more are actually
1094// triggered. This function returns a JSArray with the break point objects
1095// which is triggered.
1096Handle<Object> Debug::CheckBreakPoints(Handle<Object> break_point_objects) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00001097 Factory* factory = isolate_->factory();
1098
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001099 // Count the number of break points hit. If there are multiple break points
1100 // they are in a FixedArray.
1101 Handle<FixedArray> break_points_hit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001102 int break_points_hit_count = 0;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001103 ASSERT(!break_point_objects->IsUndefined());
1104 if (break_point_objects->IsFixedArray()) {
1105 Handle<FixedArray> array(FixedArray::cast(*break_point_objects));
lrn@chromium.org7516f052011-03-30 08:52:27 +00001106 break_points_hit = factory->NewFixedArray(array->length());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001107 for (int i = 0; i < array->length(); i++) {
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00001108 Handle<Object> o(array->get(i), isolate_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001109 if (CheckBreakPoint(o)) {
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001110 break_points_hit->set(break_points_hit_count++, *o);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001111 }
1112 }
1113 } else {
lrn@chromium.org7516f052011-03-30 08:52:27 +00001114 break_points_hit = factory->NewFixedArray(1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001115 if (CheckBreakPoint(break_point_objects)) {
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001116 break_points_hit->set(break_points_hit_count++, *break_point_objects);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001117 }
1118 }
1119
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001120 // Return undefined if no break points were triggered.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001121 if (break_points_hit_count == 0) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00001122 return factory->undefined_value();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001123 }
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001124 // Return break points hit as a JSArray.
lrn@chromium.org7516f052011-03-30 08:52:27 +00001125 Handle<JSArray> result = factory->NewJSArrayWithElements(break_points_hit);
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001126 result->set_length(Smi::FromInt(break_points_hit_count));
1127 return result;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001128}
1129
1130
1131// Check whether a single break point object is triggered.
1132bool Debug::CheckBreakPoint(Handle<Object> break_point_object) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00001133 Factory* factory = isolate_->factory();
1134 HandleScope scope(isolate_);
ager@chromium.org381abbb2009-02-25 13:23:22 +00001135
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001136 // Ignore check if break point object is not a JSObject.
1137 if (!break_point_object->IsJSObject()) return true;
1138
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001139 // Get the function IsBreakPointTriggered (defined in debug-debugger.js).
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001140 Handle<String> is_break_point_triggered_string =
1141 factory->InternalizeOneByteString(
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00001142 STATIC_ASCII_VECTOR("IsBreakPointTriggered"));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001143 Handle<JSFunction> check_break_point =
1144 Handle<JSFunction>(JSFunction::cast(
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00001145 debug_context()->global_object()->GetPropertyNoExceptionThrown(
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001146 *is_break_point_triggered_string)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001147
1148 // Get the break id as an object.
lrn@chromium.org7516f052011-03-30 08:52:27 +00001149 Handle<Object> break_id = factory->NewNumberFromInt(Debug::break_id());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001150
1151 // Call HandleBreakPointx.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001152 bool caught_exception;
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00001153 Handle<Object> argv[] = { break_id, break_point_object };
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001154 Handle<Object> result = Execution::TryCall(check_break_point,
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00001155 isolate_->js_builtins_object(),
1156 ARRAY_SIZE(argv),
1157 argv,
1158 &caught_exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001159
1160 // If exception or non boolean result handle as not triggered
1161 if (caught_exception || !result->IsBoolean()) {
1162 return false;
1163 }
1164
1165 // Return whether the break point is triggered.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001166 ASSERT(!result.is_null());
1167 return (*result)->IsTrue();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001168}
1169
1170
1171// Check whether the function has debug information.
1172bool Debug::HasDebugInfo(Handle<SharedFunctionInfo> shared) {
1173 return !shared->debug_info()->IsUndefined();
1174}
1175
1176
kasper.lundbd3ec4e2008-07-09 11:06:54 +00001177// Return the debug info for this function. EnsureDebugInfo must be called
1178// prior to ensure the debug info has been generated for shared.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001179Handle<DebugInfo> Debug::GetDebugInfo(Handle<SharedFunctionInfo> shared) {
kasper.lundbd3ec4e2008-07-09 11:06:54 +00001180 ASSERT(HasDebugInfo(shared));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001181 return Handle<DebugInfo>(DebugInfo::cast(shared->debug_info()));
1182}
1183
1184
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001185void Debug::SetBreakPoint(Handle<JSFunction> function,
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00001186 Handle<Object> break_point_object,
1187 int* source_position) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00001188 HandleScope scope(isolate_);
ager@chromium.org381abbb2009-02-25 13:23:22 +00001189
lrn@chromium.org34e60782011-09-15 07:25:40 +00001190 PrepareForBreakPoints();
1191
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001192 // Make sure the function is compiled and has set up the debug info.
1193 Handle<SharedFunctionInfo> shared(function->shared());
1194 if (!EnsureDebugInfo(shared, function)) {
kasper.lundbd3ec4e2008-07-09 11:06:54 +00001195 // Return if retrieving debug info failed.
1196 return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001197 }
1198
kasper.lundbd3ec4e2008-07-09 11:06:54 +00001199 Handle<DebugInfo> debug_info = GetDebugInfo(shared);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001200 // Source positions starts with zero.
danno@chromium.orgbf0c8202011-12-27 10:09:42 +00001201 ASSERT(*source_position >= 0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001202
1203 // Find the break point and change it.
1204 BreakLocationIterator it(debug_info, SOURCE_BREAK_LOCATIONS);
jkummerow@chromium.org93a47f42013-07-02 14:43:41 +00001205 it.FindBreakLocationFromPosition(*source_position, STATEMENT_ALIGNED);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001206 it.SetBreakPoint(break_point_object);
1207
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00001208 *source_position = it.position();
1209
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001210 // At least one active break point now.
1211 ASSERT(debug_info->GetBreakPointCount() > 0);
1212}
1213
1214
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001215bool Debug::SetBreakPointForScript(Handle<Script> script,
1216 Handle<Object> break_point_object,
jkummerow@chromium.org93a47f42013-07-02 14:43:41 +00001217 int* source_position,
1218 BreakPositionAlignment alignment) {
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001219 HandleScope scope(isolate_);
1220
jkummerow@chromium.org78502a92012-09-06 13:50:42 +00001221 PrepareForBreakPoints();
1222
1223 // Obtain shared function info for the function.
1224 Object* result = FindSharedFunctionInfoInScript(script, *source_position);
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001225 if (result->IsUndefined()) return false;
1226
1227 // Make sure the function has set up the debug info.
1228 Handle<SharedFunctionInfo> shared(SharedFunctionInfo::cast(result));
1229 if (!EnsureDebugInfo(shared, Handle<JSFunction>::null())) {
1230 // Return if retrieving debug info failed.
1231 return false;
1232 }
1233
1234 // Find position within function. The script position might be before the
1235 // source position of the first function.
1236 int position;
1237 if (shared->start_position() > *source_position) {
1238 position = 0;
1239 } else {
1240 position = *source_position - shared->start_position();
1241 }
1242
1243 Handle<DebugInfo> debug_info = GetDebugInfo(shared);
1244 // Source positions starts with zero.
1245 ASSERT(position >= 0);
1246
1247 // Find the break point and change it.
1248 BreakLocationIterator it(debug_info, SOURCE_BREAK_LOCATIONS);
jkummerow@chromium.org93a47f42013-07-02 14:43:41 +00001249 it.FindBreakLocationFromPosition(position, alignment);
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001250 it.SetBreakPoint(break_point_object);
1251
1252 *source_position = it.position() + shared->start_position();
1253
1254 // At least one active break point now.
1255 ASSERT(debug_info->GetBreakPointCount() > 0);
1256 return true;
1257}
1258
1259
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001260void Debug::ClearBreakPoint(Handle<Object> break_point_object) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00001261 HandleScope scope(isolate_);
ager@chromium.org381abbb2009-02-25 13:23:22 +00001262
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001263 DebugInfoListNode* node = debug_info_list_;
1264 while (node != NULL) {
1265 Object* result = DebugInfo::FindBreakPointInfo(node->debug_info(),
1266 break_point_object);
1267 if (!result->IsUndefined()) {
1268 // Get information in the break point.
1269 BreakPointInfo* break_point_info = BreakPointInfo::cast(result);
1270 Handle<DebugInfo> debug_info = node->debug_info();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001271
1272 // Find the break point and clear it.
1273 BreakLocationIterator it(debug_info, SOURCE_BREAK_LOCATIONS);
verwaest@chromium.org8a00e822013-06-10 15:11:22 +00001274 it.FindBreakLocationFromAddress(debug_info->code()->entry() +
1275 break_point_info->code_position()->value());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001276 it.ClearBreakPoint(break_point_object);
1277
1278 // If there are no more break points left remove the debug info for this
1279 // function.
1280 if (debug_info->GetBreakPointCount() == 0) {
1281 RemoveDebugInfo(debug_info);
1282 }
1283
1284 return;
1285 }
1286 node = node->next();
1287 }
1288}
1289
1290
ager@chromium.org381abbb2009-02-25 13:23:22 +00001291void Debug::ClearAllBreakPoints() {
1292 DebugInfoListNode* node = debug_info_list_;
1293 while (node != NULL) {
1294 // Remove all debug break code.
1295 BreakLocationIterator it(node->debug_info(), ALL_BREAK_LOCATIONS);
1296 it.ClearAllDebugBreak();
1297 node = node->next();
1298 }
1299
1300 // Remove all debug info.
1301 while (debug_info_list_ != NULL) {
1302 RemoveDebugInfo(debug_info_list_->debug_info());
1303 }
1304}
1305
1306
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001307void Debug::FloodWithOneShot(Handle<JSFunction> function) {
lrn@chromium.org34e60782011-09-15 07:25:40 +00001308 PrepareForBreakPoints();
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001309
1310 // Make sure the function is compiled and has set up the debug info.
1311 Handle<SharedFunctionInfo> shared(function->shared());
1312 if (!EnsureDebugInfo(shared, function)) {
kasper.lundbd3ec4e2008-07-09 11:06:54 +00001313 // Return if we failed to retrieve the debug info.
1314 return;
1315 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001316
1317 // Flood the function with break points.
kasper.lundbd3ec4e2008-07-09 11:06:54 +00001318 BreakLocationIterator it(GetDebugInfo(shared), ALL_BREAK_LOCATIONS);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001319 while (!it.Done()) {
1320 it.SetOneShot();
1321 it.Next();
1322 }
1323}
1324
1325
rossberg@chromium.org2c067b12012-03-19 11:01:52 +00001326void Debug::FloodBoundFunctionWithOneShot(Handle<JSFunction> function) {
1327 Handle<FixedArray> new_bindings(function->function_bindings());
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00001328 Handle<Object> bindee(new_bindings->get(JSFunction::kBoundFunctionIndex),
1329 isolate_);
rossberg@chromium.org2c067b12012-03-19 11:01:52 +00001330
1331 if (!bindee.is_null() && bindee->IsJSFunction() &&
1332 !JSFunction::cast(*bindee)->IsBuiltin()) {
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001333 Handle<JSFunction> bindee_function(JSFunction::cast(*bindee));
1334 Debug::FloodWithOneShot(bindee_function);
rossberg@chromium.org2c067b12012-03-19 11:01:52 +00001335 }
1336}
1337
1338
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001339void Debug::FloodHandlerWithOneShot() {
ager@chromium.org8bb60582008-12-11 12:02:20 +00001340 // Iterate through the JavaScript stack looking for handlers.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001341 StackFrame::Id id = break_frame_id();
ager@chromium.org8bb60582008-12-11 12:02:20 +00001342 if (id == StackFrame::NO_ID) {
1343 // If there is no JavaScript stack don't do anything.
1344 return;
1345 }
vegorov@chromium.org74f333b2011-04-06 11:17:46 +00001346 for (JavaScriptFrameIterator it(isolate_, id); !it.done(); it.Advance()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001347 JavaScriptFrame* frame = it.frame();
1348 if (frame->HasHandler()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001349 // Flood the function with the catch block with break points
danno@chromium.org169691d2013-07-15 08:01:13 +00001350 FloodWithOneShot(Handle<JSFunction>(frame->function()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001351 return;
1352 }
1353 }
1354}
1355
1356
1357void Debug::ChangeBreakOnException(ExceptionBreakType type, bool enable) {
1358 if (type == BreakUncaughtException) {
1359 break_on_uncaught_exception_ = enable;
1360 } else {
1361 break_on_exception_ = enable;
1362 }
1363}
1364
1365
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00001366bool Debug::IsBreakOnException(ExceptionBreakType type) {
1367 if (type == BreakUncaughtException) {
1368 return break_on_uncaught_exception_;
1369 } else {
1370 return break_on_exception_;
1371 }
1372}
1373
1374
dslomov@chromium.org639bac02013-09-09 11:58:54 +00001375void Debug::PrepareStep(StepAction step_action,
1376 int step_count,
1377 StackFrame::Id frame_id) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00001378 HandleScope scope(isolate_);
lrn@chromium.org34e60782011-09-15 07:25:40 +00001379
1380 PrepareForBreakPoints();
1381
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001382 ASSERT(Debug::InDebugger());
1383
1384 // Remember this step action and count.
1385 thread_local_.last_step_action_ = step_action;
ager@chromium.orga1645e22009-09-09 19:27:10 +00001386 if (step_action == StepOut) {
1387 // For step out target frame will be found on the stack so there is no need
1388 // to set step counter for it. It's expected to always be 0 for StepOut.
1389 thread_local_.step_count_ = 0;
1390 } else {
1391 thread_local_.step_count_ = step_count;
1392 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001393
1394 // Get the frame where the execution has stopped and skip the debug frame if
1395 // any. The debug frame will only be present if execution was stopped due to
1396 // hitting a break point. In other situations (e.g. unhandled exception) the
1397 // debug frame is not present.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001398 StackFrame::Id id = break_frame_id();
ager@chromium.org8bb60582008-12-11 12:02:20 +00001399 if (id == StackFrame::NO_ID) {
1400 // If there is no JavaScript stack don't do anything.
1401 return;
1402 }
dslomov@chromium.org639bac02013-09-09 11:58:54 +00001403 if (frame_id != StackFrame::NO_ID) {
1404 id = frame_id;
1405 }
vegorov@chromium.org74f333b2011-04-06 11:17:46 +00001406 JavaScriptFrameIterator frames_it(isolate_, id);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001407 JavaScriptFrame* frame = frames_it.frame();
1408
1409 // First of all ensure there is one-shot break points in the top handler
1410 // if any.
1411 FloodHandlerWithOneShot();
1412
1413 // If the function on the top frame is unresolved perform step out. This will
1414 // be the case when calling unknown functions and having the debugger stopped
1415 // in an unhandled exception.
1416 if (!frame->function()->IsJSFunction()) {
1417 // Step out: Find the calling JavaScript frame and flood it with
1418 // breakpoints.
1419 frames_it.Advance();
1420 // Fill the function to return to with one-shot break points.
danno@chromium.org169691d2013-07-15 08:01:13 +00001421 JSFunction* function = frames_it.frame()->function();
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001422 FloodWithOneShot(Handle<JSFunction>(function));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001423 return;
1424 }
1425
1426 // Get the debug info (create it if it does not exist).
danno@chromium.org169691d2013-07-15 08:01:13 +00001427 Handle<JSFunction> function(frame->function());
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001428 Handle<SharedFunctionInfo> shared(function->shared());
1429 if (!EnsureDebugInfo(shared, function)) {
kasper.lundbd3ec4e2008-07-09 11:06:54 +00001430 // Return if ensuring debug info failed.
1431 return;
1432 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001433 Handle<DebugInfo> debug_info = GetDebugInfo(shared);
1434
1435 // Find the break location where execution has stopped.
1436 BreakLocationIterator it(debug_info, ALL_BREAK_LOCATIONS);
verwaest@chromium.org8a00e822013-06-10 15:11:22 +00001437 // pc points to the instruction after the current one, possibly a break
1438 // location as well. So the "- 1" to exclude it from the search.
1439 it.FindBreakLocationFromAddress(frame->pc() - 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001440
1441 // Compute whether or not the target is a call target.
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001442 bool is_load_or_store = false;
1443 bool is_inline_cache_stub = false;
whesse@chromium.orge90029b2010-08-02 11:52:17 +00001444 bool is_at_restarted_function = false;
ager@chromium.orga1645e22009-09-09 19:27:10 +00001445 Handle<Code> call_function_stub;
ager@chromium.orga1645e22009-09-09 19:27:10 +00001446
whesse@chromium.orge90029b2010-08-02 11:52:17 +00001447 if (thread_local_.restarter_frame_function_pointer_ == NULL) {
1448 if (RelocInfo::IsCodeTarget(it.rinfo()->rmode())) {
1449 bool is_call_target = false;
1450 Address target = it.rinfo()->target_address();
1451 Code* code = Code::GetCodeFromTargetAddress(target);
1452 if (code->is_call_stub() || code->is_keyed_call_stub()) {
1453 is_call_target = true;
1454 }
1455 if (code->is_inline_cache_stub()) {
1456 is_inline_cache_stub = true;
1457 is_load_or_store = !is_call_target;
1458 }
1459
1460 // Check if target code is CallFunction stub.
1461 Code* maybe_call_function_stub = code;
1462 // If there is a breakpoint at this line look at the original code to
1463 // check if it is a CallFunction stub.
1464 if (it.IsDebugBreak()) {
1465 Address original_target = it.original_rinfo()->target_address();
1466 maybe_call_function_stub =
1467 Code::GetCodeFromTargetAddress(original_target);
1468 }
1469 if (maybe_call_function_stub->kind() == Code::STUB &&
1470 maybe_call_function_stub->major_key() == CodeStub::CallFunction) {
1471 // Save reference to the code as we may need it to find out arguments
1472 // count for 'step in' later.
1473 call_function_stub = Handle<Code>(maybe_call_function_stub);
1474 }
ager@chromium.orga1645e22009-09-09 19:27:10 +00001475 }
whesse@chromium.orge90029b2010-08-02 11:52:17 +00001476 } else {
1477 is_at_restarted_function = true;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001478 }
1479
v8.team.kasperl727e9952008-09-02 14:56:44 +00001480 // If this is the last break code target step out is the only possibility.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001481 if (it.IsExit() || step_action == StepOut) {
ager@chromium.orga1645e22009-09-09 19:27:10 +00001482 if (step_action == StepOut) {
1483 // Skip step_count frames starting with the current one.
1484 while (step_count-- > 0 && !frames_it.done()) {
1485 frames_it.Advance();
1486 }
1487 } else {
1488 ASSERT(it.IsExit());
1489 frames_it.Advance();
1490 }
1491 // Skip builtin functions on the stack.
danno@chromium.org169691d2013-07-15 08:01:13 +00001492 while (!frames_it.done() && frames_it.frame()->function()->IsBuiltin()) {
ager@chromium.orga1645e22009-09-09 19:27:10 +00001493 frames_it.Advance();
1494 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001495 // Step out: If there is a JavaScript caller frame, we need to
1496 // flood it with breakpoints.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001497 if (!frames_it.done()) {
1498 // Fill the function to return to with one-shot break points.
danno@chromium.org169691d2013-07-15 08:01:13 +00001499 JSFunction* function = frames_it.frame()->function();
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001500 FloodWithOneShot(Handle<JSFunction>(function));
ager@chromium.orga1645e22009-09-09 19:27:10 +00001501 // Set target frame pointer.
1502 ActivateStepOut(frames_it.frame());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001503 }
ager@chromium.orga1645e22009-09-09 19:27:10 +00001504 } else if (!(is_inline_cache_stub || RelocInfo::IsConstructCall(it.rmode()) ||
whesse@chromium.orge90029b2010-08-02 11:52:17 +00001505 !call_function_stub.is_null() || is_at_restarted_function)
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001506 || step_action == StepNext || step_action == StepMin) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001507 // Step next or step min.
1508
1509 // Fill the current function with one-shot break points.
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001510 FloodWithOneShot(function);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001511
1512 // Remember source position and frame to handle step next.
1513 thread_local_.last_statement_position_ =
1514 debug_info->code()->SourceStatementPosition(frame->pc());
mmassi@chromium.org7028c052012-06-13 11:51:58 +00001515 thread_local_.last_fp_ = frame->UnpaddedFP();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001516 } else {
whesse@chromium.orge90029b2010-08-02 11:52:17 +00001517 // If there's restarter frame on top of the stack, just get the pointer
1518 // to function which is going to be restarted.
1519 if (is_at_restarted_function) {
1520 Handle<JSFunction> restarted_function(
1521 JSFunction::cast(*thread_local_.restarter_frame_function_pointer_));
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001522 FloodWithOneShot(restarted_function);
whesse@chromium.orge90029b2010-08-02 11:52:17 +00001523 } else if (!call_function_stub.is_null()) {
1524 // If it's CallFunction stub ensure target function is compiled and flood
1525 // it with one shot breakpoints.
1526
ager@chromium.orga1645e22009-09-09 19:27:10 +00001527 // Find out number of arguments from the stub minor key.
1528 // Reverse lookup required as the minor key cannot be retrieved
1529 // from the code object.
1530 Handle<Object> obj(
lrn@chromium.org7516f052011-03-30 08:52:27 +00001531 isolate_->heap()->code_stubs()->SlowReverseLookup(
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00001532 *call_function_stub),
1533 isolate_);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001534 ASSERT(!obj.is_null());
1535 ASSERT(!(*obj)->IsUndefined());
ager@chromium.orga1645e22009-09-09 19:27:10 +00001536 ASSERT(obj->IsSmi());
1537 // Get the STUB key and extract major and minor key.
1538 uint32_t key = Smi::cast(*obj)->value();
1539 // Argc in the stub is the number of arguments passed - not the
1540 // expected arguments of the called function.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001541 int call_function_arg_count =
1542 CallFunctionStub::ExtractArgcFromMinorKey(
1543 CodeStub::MinorKeyFromKey(key));
ager@chromium.orga1645e22009-09-09 19:27:10 +00001544 ASSERT(call_function_stub->major_key() ==
1545 CodeStub::MajorKeyFromKey(key));
1546
1547 // Find target function on the expression stack.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001548 // Expression stack looks like this (top to bottom):
ager@chromium.orga1645e22009-09-09 19:27:10 +00001549 // argN
1550 // ...
1551 // arg0
1552 // Receiver
1553 // Function to call
1554 int expressions_count = frame->ComputeExpressionsCount();
1555 ASSERT(expressions_count - 2 - call_function_arg_count >= 0);
1556 Object* fun = frame->GetExpression(
1557 expressions_count - 2 - call_function_arg_count);
1558 if (fun->IsJSFunction()) {
1559 Handle<JSFunction> js_function(JSFunction::cast(fun));
rossberg@chromium.org2c067b12012-03-19 11:01:52 +00001560 if (js_function->shared()->bound()) {
1561 Debug::FloodBoundFunctionWithOneShot(js_function);
1562 } else if (!js_function->IsBuiltin()) {
1563 // Don't step into builtins.
ager@chromium.orga1645e22009-09-09 19:27:10 +00001564 // It will also compile target function if it's not compiled yet.
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001565 FloodWithOneShot(js_function);
ager@chromium.orga1645e22009-09-09 19:27:10 +00001566 }
1567 }
1568 }
1569
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001570 // Fill the current function with one-shot break points even for step in on
1571 // a call target as the function called might be a native function for
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001572 // which step in will not stop. It also prepares for stepping in
1573 // getters/setters.
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001574 FloodWithOneShot(function);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001575
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001576 if (is_load_or_store) {
1577 // Remember source position and frame to handle step in getter/setter. If
1578 // there is a custom getter/setter it will be handled in
1579 // Object::Get/SetPropertyWithCallback, otherwise the step action will be
1580 // propagated on the next Debug::Break.
1581 thread_local_.last_statement_position_ =
1582 debug_info->code()->SourceStatementPosition(frame->pc());
mmassi@chromium.org7028c052012-06-13 11:51:58 +00001583 thread_local_.last_fp_ = frame->UnpaddedFP();
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001584 }
1585
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001586 // Step in or Step in min
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00001587 it.PrepareStepIn(isolate_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001588 ActivateStepIn(frame);
1589 }
1590}
1591
1592
1593// Check whether the current debug break should be reported to the debugger. It
1594// is used to have step next and step in only report break back to the debugger
1595// if on a different frame or in a different statement. In some situations
1596// there will be several break points in the same statement when the code is
v8.team.kasperl727e9952008-09-02 14:56:44 +00001597// flooded with one-shot break points. This function helps to perform several
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001598// steps before reporting break back to the debugger.
1599bool Debug::StepNextContinue(BreakLocationIterator* break_location_iterator,
1600 JavaScriptFrame* frame) {
lrn@chromium.org34e60782011-09-15 07:25:40 +00001601 // StepNext and StepOut shouldn't bring us deeper in code, so last frame
1602 // shouldn't be a parent of current frame.
1603 if (thread_local_.last_step_action_ == StepNext ||
1604 thread_local_.last_step_action_ == StepOut) {
1605 if (frame->fp() < thread_local_.last_fp_) return true;
1606 }
1607
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001608 // If the step last action was step next or step in make sure that a new
1609 // statement is hit.
1610 if (thread_local_.last_step_action_ == StepNext ||
1611 thread_local_.last_step_action_ == StepIn) {
1612 // Never continue if returning from function.
1613 if (break_location_iterator->IsExit()) return false;
1614
1615 // Continue if we are still on the same frame and in the same statement.
1616 int current_statement_position =
1617 break_location_iterator->code()->SourceStatementPosition(frame->pc());
mmassi@chromium.org7028c052012-06-13 11:51:58 +00001618 return thread_local_.last_fp_ == frame->UnpaddedFP() &&
ager@chromium.org236ad962008-09-25 09:45:57 +00001619 thread_local_.last_statement_position_ == current_statement_position;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001620 }
1621
1622 // No step next action - don't continue.
1623 return false;
1624}
1625
1626
1627// Check whether the code object at the specified address is a debug break code
1628// object.
1629bool Debug::IsDebugBreak(Address addr) {
ager@chromium.org8bb60582008-12-11 12:02:20 +00001630 Code* code = Code::GetCodeFromTargetAddress(addr);
verwaest@chromium.orgec6855e2013-08-22 12:26:58 +00001631 return code->is_debug_stub() && code->extra_ic_state() == DEBUG_BREAK;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001632}
1633
1634
1635// Check whether a code stub with the specified major key is a possible break
1636// point location when looking for source break locations.
1637bool Debug::IsSourceBreakStub(Code* code) {
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001638 CodeStub::Major major_key = CodeStub::GetMajorKey(code);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001639 return major_key == CodeStub::CallFunction;
1640}
1641
1642
1643// Check whether a code stub with the specified major key is a possible break
1644// location.
1645bool Debug::IsBreakStub(Code* code) {
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001646 CodeStub::Major major_key = CodeStub::GetMajorKey(code);
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00001647 return major_key == CodeStub::CallFunction;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001648}
1649
1650
1651// Find the builtin to use for invoking the debug break
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001652Handle<Code> Debug::FindDebugBreak(Handle<Code> code, RelocInfo::Mode mode) {
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +00001653 Isolate* isolate = code->GetIsolate();
danno@chromium.orgfa458e42012-02-01 10:48:36 +00001654
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001655 // Find the builtin debug break function matching the calling convention
1656 // used by the call site.
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001657 if (code->is_inline_cache_stub()) {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001658 switch (code->kind()) {
1659 case Code::CALL_IC:
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001660 case Code::KEYED_CALL_IC:
danno@chromium.orgfa458e42012-02-01 10:48:36 +00001661 return isolate->stub_cache()->ComputeCallDebugBreak(
1662 code->arguments_count(), code->kind());
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001663
1664 case Code::LOAD_IC:
danno@chromium.orgfa458e42012-02-01 10:48:36 +00001665 return isolate->builtins()->LoadIC_DebugBreak();
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001666
1667 case Code::STORE_IC:
danno@chromium.orgfa458e42012-02-01 10:48:36 +00001668 return isolate->builtins()->StoreIC_DebugBreak();
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001669
1670 case Code::KEYED_LOAD_IC:
danno@chromium.orgfa458e42012-02-01 10:48:36 +00001671 return isolate->builtins()->KeyedLoadIC_DebugBreak();
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001672
1673 case Code::KEYED_STORE_IC:
danno@chromium.orgfa458e42012-02-01 10:48:36 +00001674 return isolate->builtins()->KeyedStoreIC_DebugBreak();
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001675
danno@chromium.orgf005df62013-04-30 16:36:45 +00001676 case Code::COMPARE_NIL_IC:
1677 return isolate->builtins()->CompareNilIC_DebugBreak();
1678
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001679 default:
1680 UNREACHABLE();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001681 }
1682 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001683 if (RelocInfo::IsConstructCall(mode)) {
danno@chromium.orgfa458e42012-02-01 10:48:36 +00001684 if (code->has_function_cache()) {
1685 return isolate->builtins()->CallConstructStub_Recording_DebugBreak();
1686 } else {
1687 return isolate->builtins()->CallConstructStub_DebugBreak();
1688 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001689 }
1690 if (code->kind() == Code::STUB) {
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00001691 ASSERT(code->major_key() == CodeStub::CallFunction);
danno@chromium.orgfa458e42012-02-01 10:48:36 +00001692 if (code->has_function_cache()) {
1693 return isolate->builtins()->CallFunctionStub_Recording_DebugBreak();
1694 } else {
1695 return isolate->builtins()->CallFunctionStub_DebugBreak();
1696 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001697 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001698
1699 UNREACHABLE();
1700 return Handle<Code>::null();
1701}
1702
1703
1704// Simple function for returning the source positions for active break points.
1705Handle<Object> Debug::GetSourceBreakLocations(
jkummerow@chromium.org93a47f42013-07-02 14:43:41 +00001706 Handle<SharedFunctionInfo> shared,
1707 BreakPositionAlignment position_alignment) {
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +00001708 Isolate* isolate = shared->GetIsolate();
lrn@chromium.org7516f052011-03-30 08:52:27 +00001709 Heap* heap = isolate->heap();
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00001710 if (!HasDebugInfo(shared)) {
1711 return Handle<Object>(heap->undefined_value(), isolate);
1712 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001713 Handle<DebugInfo> debug_info = GetDebugInfo(shared);
1714 if (debug_info->GetBreakPointCount() == 0) {
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00001715 return Handle<Object>(heap->undefined_value(), isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001716 }
1717 Handle<FixedArray> locations =
lrn@chromium.org7516f052011-03-30 08:52:27 +00001718 isolate->factory()->NewFixedArray(debug_info->GetBreakPointCount());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001719 int count = 0;
1720 for (int i = 0; i < debug_info->break_points()->length(); i++) {
1721 if (!debug_info->break_points()->get(i)->IsUndefined()) {
1722 BreakPointInfo* break_point_info =
1723 BreakPointInfo::cast(debug_info->break_points()->get(i));
1724 if (break_point_info->GetBreakPointCount() > 0) {
jkummerow@chromium.org93a47f42013-07-02 14:43:41 +00001725 Smi* position;
1726 switch (position_alignment) {
1727 case STATEMENT_ALIGNED:
1728 position = break_point_info->statement_position();
1729 break;
1730 case BREAK_POSITION_ALIGNED:
1731 position = break_point_info->source_position();
1732 break;
1733 default:
1734 UNREACHABLE();
1735 position = break_point_info->statement_position();
1736 }
1737
1738 locations->set(count++, position);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001739 }
1740 }
1741 }
1742 return locations;
1743}
1744
1745
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001746void Debug::NewBreak(StackFrame::Id break_frame_id) {
1747 thread_local_.break_frame_id_ = break_frame_id;
1748 thread_local_.break_id_ = ++thread_local_.break_count_;
1749}
1750
1751
1752void Debug::SetBreak(StackFrame::Id break_frame_id, int break_id) {
1753 thread_local_.break_frame_id_ = break_frame_id;
1754 thread_local_.break_id_ = break_id;
1755}
1756
1757
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001758// Handle stepping into a function.
1759void Debug::HandleStepIn(Handle<JSFunction> function,
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001760 Handle<Object> holder,
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001761 Address fp,
1762 bool is_constructor) {
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00001763 Isolate* isolate = function->GetIsolate();
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001764 // If the frame pointer is not supplied by the caller find it.
1765 if (fp == 0) {
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00001766 StackFrameIterator it(isolate);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001767 it.Advance();
1768 // For constructor functions skip another frame.
1769 if (is_constructor) {
1770 ASSERT(it.frame()->is_construct());
1771 it.Advance();
1772 }
1773 fp = it.frame()->fp();
1774 }
1775
1776 // Flood the function with one-shot break points if it is called from where
1777 // step into was requested.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001778 if (fp == step_in_fp()) {
rossberg@chromium.org2c067b12012-03-19 11:01:52 +00001779 if (function->shared()->bound()) {
1780 // Handle Function.prototype.bind
1781 Debug::FloodBoundFunctionWithOneShot(function);
1782 } else if (!function->IsBuiltin()) {
1783 // Don't allow step into functions in the native context.
kasperl@chromium.orgacae3782009-04-11 09:17:08 +00001784 if (function->shared()->code() ==
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00001785 isolate->builtins()->builtin(Builtins::kFunctionApply) ||
kasperl@chromium.orgacae3782009-04-11 09:17:08 +00001786 function->shared()->code() ==
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00001787 isolate->builtins()->builtin(Builtins::kFunctionCall)) {
kasperl@chromium.orgacae3782009-04-11 09:17:08 +00001788 // Handle function.apply and function.call separately to flood the
1789 // function to be called and not the code for Builtins::FunctionApply or
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001790 // Builtins::FunctionCall. The receiver of call/apply is the target
1791 // function.
machenbach@chromium.org528ce022013-09-23 14:09:36 +00001792 if (!holder.is_null() && holder->IsJSFunction()) {
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001793 Handle<JSFunction> js_function = Handle<JSFunction>::cast(holder);
machenbach@chromium.org528ce022013-09-23 14:09:36 +00001794 if (!js_function->IsBuiltin()) {
1795 Debug::FloodWithOneShot(js_function);
1796 } else if (js_function->shared()->bound()) {
1797 // Handle Function.prototype.bind
1798 Debug::FloodBoundFunctionWithOneShot(js_function);
1799 }
kasperl@chromium.orgacae3782009-04-11 09:17:08 +00001800 }
1801 } else {
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001802 Debug::FloodWithOneShot(function);
kasperl@chromium.orgacae3782009-04-11 09:17:08 +00001803 }
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001804 }
1805 }
1806}
1807
1808
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001809void Debug::ClearStepping() {
1810 // Clear the various stepping setup.
1811 ClearOneShot();
1812 ClearStepIn();
ager@chromium.orga1645e22009-09-09 19:27:10 +00001813 ClearStepOut();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001814 ClearStepNext();
1815
1816 // Clear multiple step counter.
1817 thread_local_.step_count_ = 0;
1818}
1819
mstarzinger@chromium.orge0e1b0d2013-07-08 08:38:06 +00001820
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001821// Clears all the one-shot break points that are currently set. Normally this
1822// function is called each time a break point is hit as one shot break points
1823// are used to support stepping.
1824void Debug::ClearOneShot() {
1825 // The current implementation just runs through all the breakpoints. When the
v8.team.kasperl727e9952008-09-02 14:56:44 +00001826 // last break point for a function is removed that function is automatically
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001827 // removed from the list.
1828
1829 DebugInfoListNode* node = debug_info_list_;
1830 while (node != NULL) {
1831 BreakLocationIterator it(node->debug_info(), ALL_BREAK_LOCATIONS);
1832 while (!it.Done()) {
1833 it.ClearOneShot();
1834 it.Next();
1835 }
1836 node = node->next();
1837 }
1838}
1839
1840
1841void Debug::ActivateStepIn(StackFrame* frame) {
ager@chromium.orga1645e22009-09-09 19:27:10 +00001842 ASSERT(!StepOutActive());
mmassi@chromium.org7028c052012-06-13 11:51:58 +00001843 thread_local_.step_into_fp_ = frame->UnpaddedFP();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001844}
1845
1846
1847void Debug::ClearStepIn() {
1848 thread_local_.step_into_fp_ = 0;
1849}
1850
1851
ager@chromium.orga1645e22009-09-09 19:27:10 +00001852void Debug::ActivateStepOut(StackFrame* frame) {
1853 ASSERT(!StepInActive());
mmassi@chromium.org7028c052012-06-13 11:51:58 +00001854 thread_local_.step_out_fp_ = frame->UnpaddedFP();
ager@chromium.orga1645e22009-09-09 19:27:10 +00001855}
1856
1857
1858void Debug::ClearStepOut() {
1859 thread_local_.step_out_fp_ = 0;
1860}
1861
1862
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001863void Debug::ClearStepNext() {
1864 thread_local_.last_step_action_ = StepNone;
ager@chromium.org236ad962008-09-25 09:45:57 +00001865 thread_local_.last_statement_position_ = RelocInfo::kNoPosition;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001866 thread_local_.last_fp_ = 0;
1867}
1868
1869
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001870static void CollectActiveFunctionsFromThread(
1871 Isolate* isolate,
1872 ThreadLocalTop* top,
1873 List<Handle<JSFunction> >* active_functions,
1874 Object* active_code_marker) {
1875 // Find all non-optimized code functions with activation frames
1876 // on the stack. This includes functions which have optimized
1877 // activations (including inlined functions) on the stack as the
1878 // non-optimized code is needed for the lazy deoptimization.
1879 for (JavaScriptFrameIterator it(isolate, top); !it.done(); it.Advance()) {
1880 JavaScriptFrame* frame = it.frame();
1881 if (frame->is_optimized()) {
jkummerow@chromium.orgba72ec82013-07-22 09:21:20 +00001882 List<JSFunction*> functions(FLAG_max_inlining_levels + 1);
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001883 frame->GetFunctions(&functions);
1884 for (int i = 0; i < functions.length(); i++) {
1885 JSFunction* function = functions[i];
1886 active_functions->Add(Handle<JSFunction>(function));
1887 function->shared()->code()->set_gc_metadata(active_code_marker);
1888 }
1889 } else if (frame->function()->IsJSFunction()) {
danno@chromium.org169691d2013-07-15 08:01:13 +00001890 JSFunction* function = frame->function();
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001891 ASSERT(frame->LookupCode()->kind() == Code::FUNCTION);
1892 active_functions->Add(Handle<JSFunction>(function));
1893 function->shared()->code()->set_gc_metadata(active_code_marker);
1894 }
1895 }
1896}
1897
1898
1899static void RedirectActivationsToRecompiledCodeOnThread(
1900 Isolate* isolate,
1901 ThreadLocalTop* top) {
1902 for (JavaScriptFrameIterator it(isolate, top); !it.done(); it.Advance()) {
1903 JavaScriptFrame* frame = it.frame();
1904
1905 if (frame->is_optimized() || !frame->function()->IsJSFunction()) continue;
1906
danno@chromium.org169691d2013-07-15 08:01:13 +00001907 JSFunction* function = frame->function();
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001908
1909 ASSERT(frame->LookupCode()->kind() == Code::FUNCTION);
1910
1911 Handle<Code> frame_code(frame->LookupCode());
1912 if (frame_code->has_debug_break_slots()) continue;
1913
1914 Handle<Code> new_code(function->shared()->code());
1915 if (new_code->kind() != Code::FUNCTION ||
1916 !new_code->has_debug_break_slots()) {
1917 continue;
1918 }
1919
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001920 // Iterate over the RelocInfo in the original code to compute the sum of the
1921 // constant pools sizes. (See Assembler::CheckConstPool())
1922 // Note that this is only useful for architectures using constant pools.
1923 int constpool_mask = RelocInfo::ModeMask(RelocInfo::CONST_POOL);
1924 int frame_const_pool_size = 0;
1925 for (RelocIterator it(*frame_code, constpool_mask); !it.done(); it.next()) {
1926 RelocInfo* info = it.rinfo();
1927 if (info->pc() >= frame->pc()) break;
1928 frame_const_pool_size += static_cast<int>(info->data());
1929 }
1930 intptr_t frame_offset =
1931 frame->pc() - frame_code->instruction_start() - frame_const_pool_size;
1932
1933 // Iterate over the RelocInfo for new code to find the number of bytes
1934 // generated for debug slots and constant pools.
1935 int debug_break_slot_bytes = 0;
1936 int new_code_const_pool_size = 0;
1937 int mask = RelocInfo::ModeMask(RelocInfo::DEBUG_BREAK_SLOT) |
1938 RelocInfo::ModeMask(RelocInfo::CONST_POOL);
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001939 for (RelocIterator it(*new_code, mask); !it.done(); it.next()) {
1940 // Check if the pc in the new code with debug break
1941 // slots is before this slot.
1942 RelocInfo* info = it.rinfo();
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001943 intptr_t new_offset = info->pc() - new_code->instruction_start() -
1944 new_code_const_pool_size - debug_break_slot_bytes;
1945 if (new_offset >= frame_offset) {
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001946 break;
1947 }
1948
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001949 if (RelocInfo::IsDebugBreakSlot(info->rmode())) {
1950 debug_break_slot_bytes += Assembler::kDebugBreakSlotLength;
1951 } else {
1952 ASSERT(RelocInfo::IsConstPool(info->rmode()));
1953 // The size of the constant pool is encoded in the data.
1954 new_code_const_pool_size += static_cast<int>(info->data());
1955 }
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001956 }
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001957
1958 // Compute the equivalent pc in the new code.
1959 byte* new_pc = new_code->instruction_start() + frame_offset +
1960 debug_break_slot_bytes + new_code_const_pool_size;
1961
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001962 if (FLAG_trace_deopt) {
1963 PrintF("Replacing code %08" V8PRIxPTR " - %08" V8PRIxPTR " (%d) "
1964 "with %08" V8PRIxPTR " - %08" V8PRIxPTR " (%d) "
1965 "for debugging, "
1966 "changing pc from %08" V8PRIxPTR " to %08" V8PRIxPTR "\n",
1967 reinterpret_cast<intptr_t>(
1968 frame_code->instruction_start()),
1969 reinterpret_cast<intptr_t>(
1970 frame_code->instruction_start()) +
1971 frame_code->instruction_size(),
1972 frame_code->instruction_size(),
1973 reinterpret_cast<intptr_t>(new_code->instruction_start()),
1974 reinterpret_cast<intptr_t>(new_code->instruction_start()) +
1975 new_code->instruction_size(),
1976 new_code->instruction_size(),
1977 reinterpret_cast<intptr_t>(frame->pc()),
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001978 reinterpret_cast<intptr_t>(new_pc));
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001979 }
1980
1981 // Patch the return address to return into the code with
1982 // debug break slots.
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001983 frame->set_pc(new_pc);
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001984 }
1985}
1986
1987
1988class ActiveFunctionsCollector : public ThreadVisitor {
1989 public:
1990 explicit ActiveFunctionsCollector(List<Handle<JSFunction> >* active_functions,
1991 Object* active_code_marker)
1992 : active_functions_(active_functions),
1993 active_code_marker_(active_code_marker) { }
1994
1995 void VisitThread(Isolate* isolate, ThreadLocalTop* top) {
1996 CollectActiveFunctionsFromThread(isolate,
1997 top,
1998 active_functions_,
1999 active_code_marker_);
2000 }
2001
2002 private:
2003 List<Handle<JSFunction> >* active_functions_;
2004 Object* active_code_marker_;
2005};
2006
2007
2008class ActiveFunctionsRedirector : public ThreadVisitor {
2009 public:
2010 void VisitThread(Isolate* isolate, ThreadLocalTop* top) {
2011 RedirectActivationsToRecompiledCodeOnThread(isolate, top);
2012 }
2013};
2014
2015
lrn@chromium.org34e60782011-09-15 07:25:40 +00002016void Debug::PrepareForBreakPoints() {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002017 // If preparing for the first break point make sure to deoptimize all
2018 // functions as debugging does not work with optimized code.
2019 if (!has_break_points_) {
machenbach@chromium.org9af454f2013-11-20 09:25:57 +00002020 if (isolate_->concurrent_recompilation_enabled()) {
danno@chromium.org59400602013-08-13 17:09:37 +00002021 isolate_->optimizing_compiler_thread()->Flush();
2022 }
2023
svenpanne@chromium.org876cca82013-03-18 14:43:20 +00002024 Deoptimizer::DeoptimizeAll(isolate_);
lrn@chromium.org34e60782011-09-15 07:25:40 +00002025
yangguo@chromium.org49546742013-12-23 16:17:49 +00002026 Handle<Code> lazy_compile = isolate_->builtins()->CompileUnoptimized();
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002027
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00002028 // There will be at least one break point when we are done.
2029 has_break_points_ = true;
2030
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002031 // Keep the list of activated functions in a handlified list as it
2032 // is used both in GC and non-GC code.
2033 List<Handle<JSFunction> > active_functions(100);
lrn@chromium.org34e60782011-09-15 07:25:40 +00002034
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002035 {
2036 // We are going to iterate heap to find all functions without
2037 // debug break slots.
hpayer@chromium.org7c3372b2013-02-13 17:26:04 +00002038 Heap* heap = isolate_->heap();
2039 heap->CollectAllGarbage(Heap::kMakeHeapIterableMask,
2040 "preparing for breakpoints");
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002041
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002042 // Ensure no GC in this scope as we are going to use gc_metadata
2043 // field in the Code object to mark active functions.
rossberg@chromium.org79e79022013-06-03 15:43:46 +00002044 DisallowHeapAllocation no_allocation;
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002045
hpayer@chromium.org7c3372b2013-02-13 17:26:04 +00002046 Object* active_code_marker = heap->the_hole_value();
svenpanne@chromium.orgecb9dd62011-12-01 08:22:35 +00002047
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002048 CollectActiveFunctionsFromThread(isolate_,
2049 isolate_->thread_local_top(),
2050 &active_functions,
2051 active_code_marker);
2052 ActiveFunctionsCollector active_functions_collector(&active_functions,
2053 active_code_marker);
2054 isolate_->thread_manager()->IterateArchivedThreads(
2055 &active_functions_collector);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002056
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002057 // Scan the heap for all non-optimized functions which have no
2058 // debug break slots and are not active or inlined into an active
2059 // function and mark them for lazy compilation.
hpayer@chromium.org7c3372b2013-02-13 17:26:04 +00002060 HeapIterator iterator(heap);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002061 HeapObject* obj = NULL;
2062 while (((obj = iterator.next()) != NULL)) {
2063 if (obj->IsJSFunction()) {
2064 JSFunction* function = JSFunction::cast(obj);
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002065 SharedFunctionInfo* shared = function->shared();
mstarzinger@chromium.org1510d582013-06-28 14:00:48 +00002066
2067 if (!shared->allows_lazy_compilation()) continue;
2068 if (!shared->script()->IsScript()) continue;
bmeurer@chromium.orgc9913f02013-10-24 06:31:36 +00002069 if (function->IsBuiltin()) continue;
mstarzinger@chromium.org1510d582013-06-28 14:00:48 +00002070 if (shared->code()->gc_metadata() == active_code_marker) continue;
2071
2072 Code::Kind kind = function->code()->kind();
2073 if (kind == Code::FUNCTION &&
2074 !function->code()->has_debug_break_slots()) {
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002075 function->set_code(*lazy_compile);
2076 function->shared()->set_code(*lazy_compile);
mstarzinger@chromium.org1510d582013-06-28 14:00:48 +00002077 } else if (kind == Code::BUILTIN &&
yangguo@chromium.org49546742013-12-23 16:17:49 +00002078 (function->IsInOptimizationQueue() ||
2079 function->IsMarkedForOptimization() ||
2080 function->IsMarkedForConcurrentOptimization())) {
mstarzinger@chromium.org1510d582013-06-28 14:00:48 +00002081 // Abort in-flight compilation.
2082 Code* shared_code = function->shared()->code();
2083 if (shared_code->kind() == Code::FUNCTION &&
2084 shared_code->has_debug_break_slots()) {
2085 function->set_code(shared_code);
2086 } else {
2087 function->set_code(*lazy_compile);
2088 function->shared()->set_code(*lazy_compile);
2089 }
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002090 }
2091 }
lrn@chromium.org34e60782011-09-15 07:25:40 +00002092 }
lrn@chromium.org34e60782011-09-15 07:25:40 +00002093
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002094 // Clear gc_metadata field.
2095 for (int i = 0; i < active_functions.length(); i++) {
2096 Handle<JSFunction> function = active_functions[i];
2097 function->shared()->code()->set_gc_metadata(Smi::FromInt(0));
2098 }
2099 }
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002100
2101 // Now recompile all functions with activation frames and and
2102 // patch the return address to run in the new compiled code.
2103 for (int i = 0; i < active_functions.length(); i++) {
2104 Handle<JSFunction> function = active_functions[i];
mmassi@chromium.org7028c052012-06-13 11:51:58 +00002105 Handle<SharedFunctionInfo> shared(function->shared());
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002106
2107 if (function->code()->kind() == Code::FUNCTION &&
2108 function->code()->has_debug_break_slots()) {
2109 // Nothing to do. Function code already had debug break slots.
2110 continue;
2111 }
2112
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002113 // If recompilation is not possible just skip it.
2114 if (shared->is_toplevel() ||
2115 !shared->allows_lazy_compilation() ||
2116 shared->code()->kind() == Code::BUILTIN) {
2117 continue;
2118 }
2119
2120 // Make sure that the shared full code is compiled with debug
2121 // break slots.
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00002122 if (!shared->code()->has_debug_break_slots()) {
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002123 // Try to compile the full code with debug break slots. If it
2124 // fails just keep the current code.
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002125 bool prev_force_debugger_active =
2126 isolate_->debugger()->force_debugger_active();
2127 isolate_->debugger()->set_force_debugger_active(true);
ulan@chromium.org19aca232013-12-30 09:24:30 +00002128 Handle<Code> code = Compiler::GetCodeForDebugging(function);
2129 function->ReplaceCode(*code);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002130 isolate_->debugger()->set_force_debugger_active(
2131 prev_force_debugger_active);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002132 }
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002133
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002134 // Keep function code in sync with shared function info.
2135 function->set_code(shared->code());
lrn@chromium.org34e60782011-09-15 07:25:40 +00002136 }
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002137
2138 RedirectActivationsToRecompiledCodeOnThread(isolate_,
2139 isolate_->thread_local_top());
2140
2141 ActiveFunctionsRedirector active_functions_redirector;
2142 isolate_->thread_manager()->IterateArchivedThreads(
2143 &active_functions_redirector);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002144 }
lrn@chromium.org34e60782011-09-15 07:25:40 +00002145}
2146
2147
jkummerow@chromium.org78502a92012-09-06 13:50:42 +00002148Object* Debug::FindSharedFunctionInfoInScript(Handle<Script> script,
2149 int position) {
2150 // Iterate the heap looking for SharedFunctionInfo generated from the
2151 // script. The inner most SharedFunctionInfo containing the source position
2152 // for the requested break point is found.
2153 // NOTE: This might require several heap iterations. If the SharedFunctionInfo
2154 // which is found is not compiled it is compiled and the heap is iterated
2155 // again as the compilation might create inner functions from the newly
2156 // compiled function and the actual requested break point might be in one of
2157 // these functions.
2158 // NOTE: The below fix-point iteration depends on all functions that cannot be
2159 // compiled lazily without a context to not be compiled at all. Compilation
2160 // will be triggered at points where we do not need a context.
2161 bool done = false;
2162 // The current candidate for the source position:
2163 int target_start_position = RelocInfo::kNoPosition;
2164 Handle<JSFunction> target_function;
2165 Handle<SharedFunctionInfo> target;
hpayer@chromium.org7c3372b2013-02-13 17:26:04 +00002166 Heap* heap = isolate_->heap();
jkummerow@chromium.org78502a92012-09-06 13:50:42 +00002167 while (!done) {
2168 { // Extra scope for iterator and no-allocation.
hpayer@chromium.org7c3372b2013-02-13 17:26:04 +00002169 heap->EnsureHeapIsIterable();
rossberg@chromium.org79e79022013-06-03 15:43:46 +00002170 DisallowHeapAllocation no_alloc_during_heap_iteration;
hpayer@chromium.org7c3372b2013-02-13 17:26:04 +00002171 HeapIterator iterator(heap);
jkummerow@chromium.org78502a92012-09-06 13:50:42 +00002172 for (HeapObject* obj = iterator.next();
2173 obj != NULL; obj = iterator.next()) {
2174 bool found_next_candidate = false;
2175 Handle<JSFunction> function;
2176 Handle<SharedFunctionInfo> shared;
2177 if (obj->IsJSFunction()) {
2178 function = Handle<JSFunction>(JSFunction::cast(obj));
2179 shared = Handle<SharedFunctionInfo>(function->shared());
2180 ASSERT(shared->allows_lazy_compilation() || shared->is_compiled());
2181 found_next_candidate = true;
2182 } else if (obj->IsSharedFunctionInfo()) {
2183 shared = Handle<SharedFunctionInfo>(SharedFunctionInfo::cast(obj));
2184 // Skip functions that we cannot compile lazily without a context,
2185 // which is not available here, because there is no closure.
2186 found_next_candidate = shared->is_compiled() ||
2187 shared->allows_lazy_compilation_without_context();
2188 }
2189 if (!found_next_candidate) continue;
2190 if (shared->script() == *script) {
2191 // If the SharedFunctionInfo found has the requested script data and
2192 // contains the source position it is a candidate.
2193 int start_position = shared->function_token_position();
2194 if (start_position == RelocInfo::kNoPosition) {
2195 start_position = shared->start_position();
2196 }
2197 if (start_position <= position &&
2198 position <= shared->end_position()) {
2199 // If there is no candidate or this function is within the current
2200 // candidate this is the new candidate.
2201 if (target.is_null()) {
2202 target_start_position = start_position;
2203 target_function = function;
2204 target = shared;
2205 } else {
2206 if (target_start_position == start_position &&
2207 shared->end_position() == target->end_position()) {
2208 // If a top-level function contains only one function
2209 // declaration the source for the top-level and the function
2210 // is the same. In that case prefer the non top-level function.
2211 if (!shared->is_toplevel()) {
2212 target_start_position = start_position;
2213 target_function = function;
2214 target = shared;
2215 }
2216 } else if (target_start_position <= start_position &&
2217 shared->end_position() <= target->end_position()) {
2218 // This containment check includes equality as a function
2219 // inside a top-level function can share either start or end
2220 // position with the top-level function.
2221 target_start_position = start_position;
2222 target_function = function;
2223 target = shared;
2224 }
2225 }
2226 }
2227 }
2228 } // End for loop.
2229 } // End no-allocation scope.
2230
hpayer@chromium.org7c3372b2013-02-13 17:26:04 +00002231 if (target.is_null()) return heap->undefined_value();
jkummerow@chromium.org78502a92012-09-06 13:50:42 +00002232
2233 // There will be at least one break point when we are done.
2234 has_break_points_ = true;
2235
2236 // If the candidate found is compiled we are done.
2237 done = target->is_compiled();
2238 if (!done) {
2239 // If the candidate is not compiled, compile it to reveal any inner
2240 // functions which might contain the requested source position. This
2241 // will compile all inner functions that cannot be compiled without a
2242 // context, because Compiler::BuildFunctionInfo checks whether the
2243 // debugger is active.
yangguo@chromium.org49546742013-12-23 16:17:49 +00002244 Handle<Code> result = target_function.is_null()
2245 ? Compiler::GetUnoptimizedCode(target)
2246 : Compiler::GetUnoptimizedCode(target_function);
2247 if (result.is_null()) return isolate_->heap()->undefined_value();
jkummerow@chromium.org78502a92012-09-06 13:50:42 +00002248 }
2249 } // End while loop.
2250
2251 return *target;
2252}
2253
2254
lrn@chromium.org34e60782011-09-15 07:25:40 +00002255// Ensures the debug information is present for shared.
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00002256bool Debug::EnsureDebugInfo(Handle<SharedFunctionInfo> shared,
2257 Handle<JSFunction> function) {
verwaest@chromium.orgd4be0f02013-06-05 13:39:03 +00002258 Isolate* isolate = shared->GetIsolate();
2259
lrn@chromium.org34e60782011-09-15 07:25:40 +00002260 // Return if we already have the debug info for shared.
2261 if (HasDebugInfo(shared)) {
2262 ASSERT(shared->is_compiled());
2263 return true;
2264 }
2265
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00002266 // There will be at least one break point when we are done.
2267 has_break_points_ = true;
2268
2269 // Ensure function is compiled. Return false if this failed.
2270 if (!function.is_null() &&
yangguo@chromium.org49546742013-12-23 16:17:49 +00002271 !Compiler::EnsureCompiled(function, CLEAR_EXCEPTION)) {
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002272 return false;
2273 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002274
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002275 // Create the debug info object.
verwaest@chromium.orgd4be0f02013-06-05 13:39:03 +00002276 Handle<DebugInfo> debug_info = isolate->factory()->NewDebugInfo(shared);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002277
2278 // Add debug info to the list.
2279 DebugInfoListNode* node = new DebugInfoListNode(*debug_info);
2280 node->set_next(debug_info_list_);
2281 debug_info_list_ = node;
2282
kasper.lundbd3ec4e2008-07-09 11:06:54 +00002283 return true;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002284}
2285
2286
2287void Debug::RemoveDebugInfo(Handle<DebugInfo> debug_info) {
2288 ASSERT(debug_info_list_ != NULL);
2289 // Run through the debug info objects to find this one and remove it.
2290 DebugInfoListNode* prev = NULL;
2291 DebugInfoListNode* current = debug_info_list_;
2292 while (current != NULL) {
2293 if (*current->debug_info() == *debug_info) {
2294 // Unlink from list. If prev is NULL we are looking at the first element.
2295 if (prev == NULL) {
2296 debug_info_list_ = current->next();
2297 } else {
2298 prev->set_next(current->next());
2299 }
lrn@chromium.org7516f052011-03-30 08:52:27 +00002300 current->debug_info()->shared()->set_debug_info(
2301 isolate_->heap()->undefined_value());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002302 delete current;
2303
2304 // If there are no more debug info objects there are not more break
2305 // points.
2306 has_break_points_ = debug_info_list_ != NULL;
2307
2308 return;
2309 }
2310 // Move to next in list.
2311 prev = current;
2312 current = current->next();
2313 }
2314 UNREACHABLE();
2315}
2316
2317
2318void Debug::SetAfterBreakTarget(JavaScriptFrame* frame) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002319 HandleScope scope(isolate_);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002320
lrn@chromium.org34e60782011-09-15 07:25:40 +00002321 PrepareForBreakPoints();
2322
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002323 // Get the executing function in which the debug break occurred.
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00002324 Handle<JSFunction> function(JSFunction::cast(frame->function()));
2325 Handle<SharedFunctionInfo> shared(function->shared());
2326 if (!EnsureDebugInfo(shared, function)) {
kasper.lundbd3ec4e2008-07-09 11:06:54 +00002327 // Return if we failed to retrieve the debug info.
2328 return;
2329 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002330 Handle<DebugInfo> debug_info = GetDebugInfo(shared);
2331 Handle<Code> code(debug_info->code());
2332 Handle<Code> original_code(debug_info->original_code());
2333#ifdef DEBUG
2334 // Get the code which is actually executing.
vegorov@chromium.org74f333b2011-04-06 11:17:46 +00002335 Handle<Code> frame_code(frame->LookupCode());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002336 ASSERT(frame_code.is_identical_to(code));
2337#endif
2338
2339 // Find the call address in the running code. This address holds the call to
2340 // either a DebugBreakXXX or to the debug break return entry code if the
2341 // break point is still active after processing the break point.
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00002342 Address addr = frame->pc() - Assembler::kPatchDebugBreakSlotReturnOffset;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002343
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002344 // Check if the location is at JS exit or debug break slot.
ager@chromium.orga1645e22009-09-09 19:27:10 +00002345 bool at_js_return = false;
2346 bool break_at_js_return_active = false;
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002347 bool at_debug_break_slot = false;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002348 RelocIterator it(debug_info->code());
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002349 while (!it.done() && !at_js_return && !at_debug_break_slot) {
ager@chromium.org236ad962008-09-25 09:45:57 +00002350 if (RelocInfo::IsJSReturn(it.rinfo()->rmode())) {
ager@chromium.orga1645e22009-09-09 19:27:10 +00002351 at_js_return = (it.rinfo()->pc() ==
2352 addr - Assembler::kPatchReturnSequenceAddressOffset);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002353 break_at_js_return_active = it.rinfo()->IsPatchedReturnSequence();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002354 }
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002355 if (RelocInfo::IsDebugBreakSlot(it.rinfo()->rmode())) {
2356 at_debug_break_slot = (it.rinfo()->pc() ==
2357 addr - Assembler::kPatchDebugBreakSlotAddressOffset);
2358 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002359 it.next();
2360 }
2361
2362 // Handle the jump to continue execution after break point depending on the
2363 // break location.
ager@chromium.orga1645e22009-09-09 19:27:10 +00002364 if (at_js_return) {
2365 // If the break point as return is still active jump to the corresponding
2366 // place in the original code. If not the break point was removed during
2367 // break point processing.
2368 if (break_at_js_return_active) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002369 addr += original_code->instruction_start() - code->instruction_start();
2370 }
2371
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00002372 // Move back to where the call instruction sequence started.
2373 thread_local_.after_break_target_ =
2374 addr - Assembler::kPatchReturnSequenceAddressOffset;
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002375 } else if (at_debug_break_slot) {
2376 // Address of where the debug break slot starts.
2377 addr = addr - Assembler::kPatchDebugBreakSlotAddressOffset;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002378
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002379 // Continue just after the slot.
2380 thread_local_.after_break_target_ = addr + Assembler::kDebugBreakSlotLength;
2381 } else if (IsDebugBreak(Assembler::target_address_at(addr))) {
2382 // We now know that there is still a debug break call at the target address,
2383 // so the break point is still there and the original code will hold the
2384 // address to jump to in order to complete the call which is replaced by a
2385 // call to DebugBreakXXX.
2386
2387 // Find the corresponding address in the original code.
2388 addr += original_code->instruction_start() - code->instruction_start();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002389
2390 // Install jump to the call address in the original code. This will be the
2391 // call which was overwritten by the call to DebugBreakXXX.
2392 thread_local_.after_break_target_ = Assembler::target_address_at(addr);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002393 } else {
2394 // There is no longer a break point present. Don't try to look in the
2395 // original code as the running code will have the right address. This takes
2396 // care of the case where the last break point is removed from the function
2397 // and therefore no "original code" is available.
2398 thread_local_.after_break_target_ = Assembler::target_address_at(addr);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002399 }
2400}
2401
2402
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00002403bool Debug::IsBreakAtReturn(JavaScriptFrame* frame) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002404 HandleScope scope(isolate_);
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00002405
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00002406 // If there are no break points this cannot be break at return, as
2407 // the debugger statement and stack guard bebug break cannot be at
2408 // return.
2409 if (!has_break_points_) {
2410 return false;
2411 }
2412
lrn@chromium.org34e60782011-09-15 07:25:40 +00002413 PrepareForBreakPoints();
2414
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00002415 // Get the executing function in which the debug break occurred.
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00002416 Handle<JSFunction> function(JSFunction::cast(frame->function()));
2417 Handle<SharedFunctionInfo> shared(function->shared());
2418 if (!EnsureDebugInfo(shared, function)) {
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00002419 // Return if we failed to retrieve the debug info.
2420 return false;
2421 }
2422 Handle<DebugInfo> debug_info = GetDebugInfo(shared);
2423 Handle<Code> code(debug_info->code());
2424#ifdef DEBUG
2425 // Get the code which is actually executing.
vegorov@chromium.org74f333b2011-04-06 11:17:46 +00002426 Handle<Code> frame_code(frame->LookupCode());
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00002427 ASSERT(frame_code.is_identical_to(code));
2428#endif
2429
2430 // Find the call address in the running code.
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00002431 Address addr = frame->pc() - Assembler::kPatchDebugBreakSlotReturnOffset;
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00002432
2433 // Check if the location is at JS return.
2434 RelocIterator it(debug_info->code());
2435 while (!it.done()) {
2436 if (RelocInfo::IsJSReturn(it.rinfo()->rmode())) {
2437 return (it.rinfo()->pc() ==
2438 addr - Assembler::kPatchReturnSequenceAddressOffset);
2439 }
2440 it.next();
2441 }
2442 return false;
2443}
2444
2445
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00002446void Debug::FramesHaveBeenDropped(StackFrame::Id new_break_frame_id,
whesse@chromium.orge90029b2010-08-02 11:52:17 +00002447 FrameDropMode mode,
2448 Object** restarter_frame_function_pointer) {
ulan@chromium.orgd9e468a2012-06-25 09:47:40 +00002449 if (mode != CURRENTLY_SET_MODE) {
2450 thread_local_.frame_drop_mode_ = mode;
2451 }
ager@chromium.org357bf652010-04-12 11:30:10 +00002452 thread_local_.break_frame_id_ = new_break_frame_id;
whesse@chromium.orge90029b2010-08-02 11:52:17 +00002453 thread_local_.restarter_frame_function_pointer_ =
2454 restarter_frame_function_pointer;
ager@chromium.org357bf652010-04-12 11:30:10 +00002455}
2456
2457
jkummerow@chromium.org212d9642012-05-11 15:02:09 +00002458const int Debug::FramePaddingLayout::kInitialSize = 1;
2459
2460
2461// Any even value bigger than kInitialSize as needed for stack scanning.
2462const int Debug::FramePaddingLayout::kPaddingValue = kInitialSize + 1;
2463
2464
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002465bool Debug::IsDebugGlobal(GlobalObject* global) {
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00002466 return IsLoaded() && global == debug_context()->global_object();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002467}
2468
2469
ager@chromium.org32912102009-01-16 10:38:43 +00002470void Debug::ClearMirrorCache() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002471 PostponeInterruptsScope postpone(isolate_);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002472 HandleScope scope(isolate_);
2473 ASSERT(isolate_->context() == *Debug::debug_context());
ager@chromium.org32912102009-01-16 10:38:43 +00002474
2475 // Clear the mirror cache.
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00002476 Handle<String> function_name = isolate_->factory()->InternalizeOneByteString(
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00002477 STATIC_ASCII_VECTOR("ClearMirrorCache"));
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00002478 Handle<Object> fun(
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00002479 isolate_->global_object()->GetPropertyNoExceptionThrown(*function_name),
2480 isolate_);
ager@chromium.org32912102009-01-16 10:38:43 +00002481 ASSERT(fun->IsJSFunction());
2482 bool caught_exception;
rossberg@chromium.org717967f2011-07-20 13:44:42 +00002483 Execution::TryCall(Handle<JSFunction>::cast(fun),
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00002484 Handle<JSObject>(Debug::debug_context()->global_object()),
ager@chromium.org32912102009-01-16 10:38:43 +00002485 0, NULL, &caught_exception);
2486}
2487
2488
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002489void Debug::CreateScriptCache() {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002490 Heap* heap = isolate_->heap();
2491 HandleScope scope(isolate_);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002492
2493 // Perform two GCs to get rid of all unreferenced scripts. The first GC gets
2494 // rid of all the cached script wrappers and the second gets rid of the
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002495 // scripts which are no longer referenced. The second also sweeps precisely,
2496 // which saves us doing yet another GC to make the heap iterable.
rossberg@chromium.org994edf62012-02-06 10:12:55 +00002497 heap->CollectAllGarbage(Heap::kNoGCFlags, "Debug::CreateScriptCache");
2498 heap->CollectAllGarbage(Heap::kMakeHeapIterableMask,
2499 "Debug::CreateScriptCache");
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002500
2501 ASSERT(script_cache_ == NULL);
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +00002502 script_cache_ = new ScriptCache(isolate_);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002503
2504 // Scan heap for Script objects.
2505 int count = 0;
hpayer@chromium.org7c3372b2013-02-13 17:26:04 +00002506 HeapIterator iterator(heap);
rossberg@chromium.org79e79022013-06-03 15:43:46 +00002507 DisallowHeapAllocation no_allocation;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002508
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002509 for (HeapObject* obj = iterator.next(); obj != NULL; obj = iterator.next()) {
sgjesse@chromium.org152a0b02009-10-07 13:50:16 +00002510 if (obj->IsScript() && Script::cast(obj)->HasValidSource()) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002511 script_cache_->Add(Handle<Script>(Script::cast(obj)));
2512 count++;
2513 }
2514 }
2515}
2516
2517
2518void Debug::DestroyScriptCache() {
2519 // Get rid of the script cache if it was created.
2520 if (script_cache_ != NULL) {
2521 delete script_cache_;
2522 script_cache_ = NULL;
2523 }
2524}
2525
2526
2527void Debug::AddScriptToScriptCache(Handle<Script> script) {
2528 if (script_cache_ != NULL) {
2529 script_cache_->Add(script);
2530 }
2531}
2532
2533
2534Handle<FixedArray> Debug::GetLoadedScripts() {
2535 // Create and fill the script cache when the loaded scripts is requested for
2536 // the first time.
2537 if (script_cache_ == NULL) {
2538 CreateScriptCache();
2539 }
2540
2541 // If the script cache is not active just return an empty array.
2542 ASSERT(script_cache_ != NULL);
2543 if (script_cache_ == NULL) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002544 isolate_->factory()->NewFixedArray(0);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002545 }
2546
2547 // Perform GC to get unreferenced scripts evicted from the cache before
2548 // returning the content.
rossberg@chromium.org994edf62012-02-06 10:12:55 +00002549 isolate_->heap()->CollectAllGarbage(Heap::kNoGCFlags,
2550 "Debug::GetLoadedScripts");
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002551
2552 // Get the scripts from the cache.
2553 return script_cache_->GetScripts();
2554}
2555
2556
yangguo@chromium.org49546742013-12-23 16:17:49 +00002557void Debug::RecordEvalCaller(Handle<Script> script) {
2558 script->set_compilation_type(Script::COMPILATION_TYPE_EVAL);
2559 // For eval scripts add information on the function from which eval was
2560 // called.
2561 StackTraceFrameIterator it(script->GetIsolate());
2562 if (!it.done()) {
2563 script->set_eval_from_shared(it.frame()->function()->shared());
2564 Code* code = it.frame()->LookupCode();
2565 int offset = static_cast<int>(
2566 it.frame()->pc() - code->instruction_start());
2567 script->set_eval_from_instructions_offset(Smi::FromInt(offset));
2568 }
2569}
2570
2571
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002572void Debug::AfterGarbageCollection() {
2573 // Generate events for collected scripts.
2574 if (script_cache_ != NULL) {
2575 script_cache_->ProcessCollectedScripts();
2576 }
2577}
2578
2579
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00002580Debugger::Debugger(Isolate* isolate)
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +00002581 : debugger_access_(isolate->debugger_access()),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002582 event_listener_(Handle<Object>()),
2583 event_listener_data_(Handle<Object>()),
2584 compiling_natives_(false),
2585 is_loading_debugger_(false),
mstarzinger@chromium.orgde886792012-09-11 13:22:37 +00002586 live_edit_enabled_(true),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002587 never_unload_debugger_(false),
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002588 force_debugger_active_(false),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002589 message_handler_(NULL),
2590 debugger_unload_pending_(false),
2591 host_dispatch_handler_(NULL),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002592 debug_message_dispatch_handler_(NULL),
2593 message_dispatch_helper_thread_(NULL),
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +00002594 host_dispatch_period_(TimeDelta::FromMilliseconds(100)),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002595 agent_(NULL),
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00002596 command_queue_(isolate->logger(), kQueueInitialSize),
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +00002597 command_received_(0),
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00002598 event_command_queue_(isolate->logger(), kQueueInitialSize),
2599 isolate_(isolate) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002600}
2601
2602
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +00002603Debugger::~Debugger() {}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002604
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002605
2606Handle<Object> Debugger::MakeJSObject(Vector<const char> constructor_name,
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002607 int argc,
2608 Handle<Object> argv[],
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002609 bool* caught_exception) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002610 ASSERT(isolate_->context() == *isolate_->debug()->debug_context());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002611
2612 // Create the execution state object.
lrn@chromium.org7516f052011-03-30 08:52:27 +00002613 Handle<String> constructor_str =
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00002614 isolate_->factory()->InternalizeUtf8String(constructor_name);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002615 Handle<Object> constructor(
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00002616 isolate_->global_object()->GetPropertyNoExceptionThrown(*constructor_str),
2617 isolate_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002618 ASSERT(constructor->IsJSFunction());
2619 if (!constructor->IsJSFunction()) {
2620 *caught_exception = true;
lrn@chromium.org7516f052011-03-30 08:52:27 +00002621 return isolate_->factory()->undefined_value();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002622 }
2623 Handle<Object> js_object = Execution::TryCall(
2624 Handle<JSFunction>::cast(constructor),
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00002625 Handle<JSObject>(isolate_->debug()->debug_context()->global_object()),
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002626 argc,
2627 argv,
2628 caught_exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002629 return js_object;
2630}
2631
2632
2633Handle<Object> Debugger::MakeExecutionState(bool* caught_exception) {
2634 // Create the execution state object.
lrn@chromium.org7516f052011-03-30 08:52:27 +00002635 Handle<Object> break_id = isolate_->factory()->NewNumberFromInt(
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002636 isolate_->debug()->break_id());
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002637 Handle<Object> argv[] = { break_id };
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002638 return MakeJSObject(CStrVector("MakeExecutionState"),
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002639 ARRAY_SIZE(argv),
2640 argv,
2641 caught_exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002642}
2643
2644
2645Handle<Object> Debugger::MakeBreakEvent(Handle<Object> exec_state,
2646 Handle<Object> break_points_hit,
2647 bool* caught_exception) {
2648 // Create the new break event object.
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002649 Handle<Object> argv[] = { exec_state, break_points_hit };
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002650 return MakeJSObject(CStrVector("MakeBreakEvent"),
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002651 ARRAY_SIZE(argv),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002652 argv,
2653 caught_exception);
2654}
2655
2656
2657Handle<Object> Debugger::MakeExceptionEvent(Handle<Object> exec_state,
2658 Handle<Object> exception,
2659 bool uncaught,
2660 bool* caught_exception) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002661 Factory* factory = isolate_->factory();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002662 // Create the new exception event object.
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002663 Handle<Object> argv[] = { exec_state,
2664 exception,
2665 factory->ToBoolean(uncaught) };
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002666 return MakeJSObject(CStrVector("MakeExceptionEvent"),
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002667 ARRAY_SIZE(argv),
2668 argv,
2669 caught_exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002670}
2671
2672
2673Handle<Object> Debugger::MakeNewFunctionEvent(Handle<Object> function,
2674 bool* caught_exception) {
2675 // Create the new function event object.
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002676 Handle<Object> argv[] = { function };
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002677 return MakeJSObject(CStrVector("MakeNewFunctionEvent"),
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002678 ARRAY_SIZE(argv),
2679 argv,
2680 caught_exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002681}
2682
2683
2684Handle<Object> Debugger::MakeCompileEvent(Handle<Script> script,
iposva@chromium.org245aa852009-02-10 00:49:54 +00002685 bool before,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002686 bool* caught_exception) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002687 Factory* factory = isolate_->factory();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002688 // Create the compile event object.
2689 Handle<Object> exec_state = MakeExecutionState(caught_exception);
iposva@chromium.org245aa852009-02-10 00:49:54 +00002690 Handle<Object> script_wrapper = GetScriptWrapper(script);
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002691 Handle<Object> argv[] = { exec_state,
2692 script_wrapper,
2693 factory->ToBoolean(before) };
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002694 return MakeJSObject(CStrVector("MakeCompileEvent"),
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002695 ARRAY_SIZE(argv),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002696 argv,
2697 caught_exception);
2698}
2699
2700
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002701Handle<Object> Debugger::MakeScriptCollectedEvent(int id,
2702 bool* caught_exception) {
2703 // Create the script collected event object.
2704 Handle<Object> exec_state = MakeExecutionState(caught_exception);
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00002705 Handle<Object> id_object = Handle<Smi>(Smi::FromInt(id), isolate_);
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002706 Handle<Object> argv[] = { exec_state, id_object };
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002707
2708 return MakeJSObject(CStrVector("MakeScriptCollectedEvent"),
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002709 ARRAY_SIZE(argv),
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002710 argv,
2711 caught_exception);
2712}
2713
2714
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002715void Debugger::OnException(Handle<Object> exception, bool uncaught) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002716 HandleScope scope(isolate_);
2717 Debug* debug = isolate_->debug();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002718
2719 // Bail out based on state or if there is no listener for this event
lrn@chromium.org7516f052011-03-30 08:52:27 +00002720 if (debug->InDebugger()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002721 if (!Debugger::EventActive(v8::Exception)) return;
2722
2723 // Bail out if exception breaks are not active
2724 if (uncaught) {
2725 // Uncaught exceptions are reported by either flags.
lrn@chromium.org7516f052011-03-30 08:52:27 +00002726 if (!(debug->break_on_uncaught_exception() ||
2727 debug->break_on_exception())) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002728 } else {
2729 // Caught exceptions are reported is activated.
lrn@chromium.org7516f052011-03-30 08:52:27 +00002730 if (!debug->break_on_exception()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002731 }
2732
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002733 // Enter the debugger.
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +00002734 EnterDebugger debugger(isolate_);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002735 if (debugger.FailedToEnter()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002736
2737 // Clear all current stepping setup.
lrn@chromium.org7516f052011-03-30 08:52:27 +00002738 debug->ClearStepping();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002739 // Create the event data object.
2740 bool caught_exception = false;
2741 Handle<Object> exec_state = MakeExecutionState(&caught_exception);
2742 Handle<Object> event_data;
2743 if (!caught_exception) {
2744 event_data = MakeExceptionEvent(exec_state, exception, uncaught,
2745 &caught_exception);
2746 }
2747 // Bail out and don't call debugger if exception.
2748 if (caught_exception) {
2749 return;
2750 }
2751
ager@chromium.org5ec48922009-05-05 07:25:34 +00002752 // Process debug event.
2753 ProcessDebugEvent(v8::Exception, Handle<JSObject>::cast(event_data), false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002754 // Return to continue execution from where the exception was thrown.
2755}
2756
2757
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002758void Debugger::OnDebugBreak(Handle<Object> break_points_hit,
2759 bool auto_continue) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002760 HandleScope scope(isolate_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002761
kasper.lund212ac232008-07-16 07:07:30 +00002762 // Debugger has already been entered by caller.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002763 ASSERT(isolate_->context() == *isolate_->debug()->debug_context());
kasper.lund212ac232008-07-16 07:07:30 +00002764
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002765 // Bail out if there is no listener for this event
2766 if (!Debugger::EventActive(v8::Break)) return;
2767
2768 // Debugger must be entered in advance.
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00002769 ASSERT(isolate_->context() == *isolate_->debug()->debug_context());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002770
2771 // Create the event data object.
2772 bool caught_exception = false;
2773 Handle<Object> exec_state = MakeExecutionState(&caught_exception);
2774 Handle<Object> event_data;
2775 if (!caught_exception) {
2776 event_data = MakeBreakEvent(exec_state, break_points_hit,
2777 &caught_exception);
2778 }
2779 // Bail out and don't call debugger if exception.
2780 if (caught_exception) {
2781 return;
2782 }
2783
ager@chromium.org5ec48922009-05-05 07:25:34 +00002784 // Process debug event.
2785 ProcessDebugEvent(v8::Break,
2786 Handle<JSObject>::cast(event_data),
2787 auto_continue);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002788}
2789
2790
2791void Debugger::OnBeforeCompile(Handle<Script> script) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002792 HandleScope scope(isolate_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002793
2794 // Bail out based on state or if there is no listener for this event
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002795 if (isolate_->debug()->InDebugger()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002796 if (compiling_natives()) return;
2797 if (!EventActive(v8::BeforeCompile)) return;
2798
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002799 // Enter the debugger.
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +00002800 EnterDebugger debugger(isolate_);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002801 if (debugger.FailedToEnter()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002802
2803 // Create the event data object.
2804 bool caught_exception = false;
iposva@chromium.org245aa852009-02-10 00:49:54 +00002805 Handle<Object> event_data = MakeCompileEvent(script, true, &caught_exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002806 // Bail out and don't call debugger if exception.
2807 if (caught_exception) {
2808 return;
2809 }
2810
ager@chromium.org5ec48922009-05-05 07:25:34 +00002811 // Process debug event.
2812 ProcessDebugEvent(v8::BeforeCompile,
2813 Handle<JSObject>::cast(event_data),
2814 true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002815}
2816
2817
2818// Handle debugger actions when a new script is compiled.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00002819void Debugger::OnAfterCompile(Handle<Script> script,
2820 AfterCompileFlags after_compile_flags) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002821 HandleScope scope(isolate_);
2822 Debug* debug = isolate_->debug();
kasper.lund212ac232008-07-16 07:07:30 +00002823
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002824 // Add the newly compiled script to the script cache.
lrn@chromium.org7516f052011-03-30 08:52:27 +00002825 debug->AddScriptToScriptCache(script);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002826
2827 // No more to do if not debugging.
ager@chromium.org71daaf62009-04-01 07:22:49 +00002828 if (!IsDebuggerActive()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002829
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002830 // No compile events while compiling natives.
2831 if (compiling_natives()) return;
2832
iposva@chromium.org245aa852009-02-10 00:49:54 +00002833 // Store whether in debugger before entering debugger.
lrn@chromium.org7516f052011-03-30 08:52:27 +00002834 bool in_debugger = debug->InDebugger();
iposva@chromium.org245aa852009-02-10 00:49:54 +00002835
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002836 // Enter the debugger.
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +00002837 EnterDebugger debugger(isolate_);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002838 if (debugger.FailedToEnter()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002839
2840 // If debugging there might be script break points registered for this
2841 // script. Make sure that these break points are set.
2842
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002843 // Get the function UpdateScriptBreakPoints (defined in debug-debugger.js).
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00002844 Handle<String> update_script_break_points_string =
2845 isolate_->factory()->InternalizeOneByteString(
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00002846 STATIC_ASCII_VECTOR("UpdateScriptBreakPoints"));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002847 Handle<Object> update_script_break_points =
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00002848 Handle<Object>(
2849 debug->debug_context()->global_object()->GetPropertyNoExceptionThrown(
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00002850 *update_script_break_points_string),
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00002851 isolate_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002852 if (!update_script_break_points->IsJSFunction()) {
2853 return;
2854 }
2855 ASSERT(update_script_break_points->IsJSFunction());
2856
2857 // Wrap the script object in a proper JS object before passing it
2858 // to JavaScript.
2859 Handle<JSValue> wrapper = GetScriptWrapper(script);
2860
2861 // Call UpdateScriptBreakPoints expect no exceptions.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002862 bool caught_exception;
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002863 Handle<Object> argv[] = { wrapper };
rossberg@chromium.org717967f2011-07-20 13:44:42 +00002864 Execution::TryCall(Handle<JSFunction>::cast(update_script_break_points),
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +00002865 isolate_->js_builtins_object(),
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002866 ARRAY_SIZE(argv),
2867 argv,
2868 &caught_exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002869 if (caught_exception) {
2870 return;
2871 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002872 // Bail out based on state or if there is no listener for this event
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00002873 if (in_debugger && (after_compile_flags & SEND_WHEN_DEBUGGING) == 0) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002874 if (!Debugger::EventActive(v8::AfterCompile)) return;
2875
2876 // Create the compile state object.
2877 Handle<Object> event_data = MakeCompileEvent(script,
iposva@chromium.org245aa852009-02-10 00:49:54 +00002878 false,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002879 &caught_exception);
2880 // Bail out and don't call debugger if exception.
2881 if (caught_exception) {
2882 return;
2883 }
ager@chromium.org5ec48922009-05-05 07:25:34 +00002884 // Process debug event.
2885 ProcessDebugEvent(v8::AfterCompile,
2886 Handle<JSObject>::cast(event_data),
2887 true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002888}
2889
2890
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002891void Debugger::OnScriptCollected(int id) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002892 HandleScope scope(isolate_);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002893
2894 // No more to do if not debugging.
ulan@chromium.org56c14af2012-09-20 12:51:09 +00002895 if (isolate_->debug()->InDebugger()) return;
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002896 if (!IsDebuggerActive()) return;
2897 if (!Debugger::EventActive(v8::ScriptCollected)) return;
2898
2899 // Enter the debugger.
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +00002900 EnterDebugger debugger(isolate_);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002901 if (debugger.FailedToEnter()) return;
2902
2903 // Create the script collected state object.
2904 bool caught_exception = false;
2905 Handle<Object> event_data = MakeScriptCollectedEvent(id,
2906 &caught_exception);
2907 // Bail out and don't call debugger if exception.
2908 if (caught_exception) {
2909 return;
2910 }
2911
2912 // Process debug event.
2913 ProcessDebugEvent(v8::ScriptCollected,
2914 Handle<JSObject>::cast(event_data),
2915 true);
2916}
2917
2918
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002919void Debugger::ProcessDebugEvent(v8::DebugEvent event,
ager@chromium.org5ec48922009-05-05 07:25:34 +00002920 Handle<JSObject> event_data,
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002921 bool auto_continue) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002922 HandleScope scope(isolate_);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002923
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +00002924 // Clear any pending debug break if this is a real break.
2925 if (!auto_continue) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002926 isolate_->debug()->clear_interrupt_pending(DEBUGBREAK);
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +00002927 }
2928
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002929 // Create the execution state.
2930 bool caught_exception = false;
2931 Handle<Object> exec_state = MakeExecutionState(&caught_exception);
2932 if (caught_exception) {
2933 return;
2934 }
ager@chromium.org41826e72009-03-30 13:30:57 +00002935 // First notify the message handler if any.
2936 if (message_handler_ != NULL) {
ager@chromium.org5ec48922009-05-05 07:25:34 +00002937 NotifyMessageHandler(event,
2938 Handle<JSObject>::cast(exec_state),
2939 event_data,
2940 auto_continue);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002941 }
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00002942 // Notify registered debug event listener. This can be either a C or
2943 // a JavaScript function. Don't call event listener for v8::Break
2944 // here, if it's only a debug command -- they will be processed later.
2945 if ((event != v8::Break || !auto_continue) && !event_listener_.is_null()) {
2946 CallEventCallback(event, exec_state, event_data, NULL);
2947 }
2948 // Process pending debug commands.
2949 if (event == v8::Break) {
2950 while (!event_command_queue_.IsEmpty()) {
2951 CommandMessage command = event_command_queue_.Get();
2952 if (!event_listener_.is_null()) {
2953 CallEventCallback(v8::BreakForCommand,
2954 exec_state,
2955 event_data,
2956 command.client_data());
2957 }
2958 command.Dispose();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002959 }
2960 }
2961}
2962
2963
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00002964void Debugger::CallEventCallback(v8::DebugEvent event,
2965 Handle<Object> exec_state,
2966 Handle<Object> event_data,
2967 v8::Debug::ClientData* client_data) {
ager@chromium.orgea91cc52011-05-23 06:06:11 +00002968 if (event_listener_->IsForeign()) {
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00002969 CallCEventCallback(event, exec_state, event_data, client_data);
2970 } else {
2971 CallJSEventCallback(event, exec_state, event_data);
2972 }
2973}
2974
2975
2976void Debugger::CallCEventCallback(v8::DebugEvent event,
2977 Handle<Object> exec_state,
2978 Handle<Object> event_data,
2979 v8::Debug::ClientData* client_data) {
ager@chromium.orgea91cc52011-05-23 06:06:11 +00002980 Handle<Foreign> callback_obj(Handle<Foreign>::cast(event_listener_));
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00002981 v8::Debug::EventCallback2 callback =
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002982 FUNCTION_CAST<v8::Debug::EventCallback2>(
2983 callback_obj->foreign_address());
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00002984 EventDetailsImpl event_details(
2985 event,
2986 Handle<JSObject>::cast(exec_state),
2987 Handle<JSObject>::cast(event_data),
2988 event_listener_data_,
2989 client_data);
2990 callback(event_details);
2991}
2992
2993
2994void Debugger::CallJSEventCallback(v8::DebugEvent event,
2995 Handle<Object> exec_state,
2996 Handle<Object> event_data) {
2997 ASSERT(event_listener_->IsJSFunction());
2998 Handle<JSFunction> fun(Handle<JSFunction>::cast(event_listener_));
2999
3000 // Invoke the JavaScript debug event listener.
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00003001 Handle<Object> argv[] = { Handle<Object>(Smi::FromInt(event), isolate_),
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00003002 exec_state,
3003 event_data,
3004 event_listener_data_ };
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003005 bool caught_exception;
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00003006 Execution::TryCall(fun,
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00003007 isolate_->global_object(),
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00003008 ARRAY_SIZE(argv),
3009 argv,
3010 &caught_exception);
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00003011 // Silently ignore exceptions from debug event listeners.
3012}
3013
3014
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00003015Handle<Context> Debugger::GetDebugContext() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003016 never_unload_debugger_ = true;
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +00003017 EnterDebugger debugger(isolate_);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003018 return isolate_->debug()->debug_context();
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00003019}
3020
3021
ager@chromium.org71daaf62009-04-01 07:22:49 +00003022void Debugger::UnloadDebugger() {
lrn@chromium.org7516f052011-03-30 08:52:27 +00003023 Debug* debug = isolate_->debug();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003024
ager@chromium.org71daaf62009-04-01 07:22:49 +00003025 // Make sure that there are no breakpoints left.
lrn@chromium.org7516f052011-03-30 08:52:27 +00003026 debug->ClearAllBreakPoints();
ager@chromium.org71daaf62009-04-01 07:22:49 +00003027
3028 // Unload the debugger if feasible.
3029 if (!never_unload_debugger_) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00003030 debug->Unload();
ager@chromium.org71daaf62009-04-01 07:22:49 +00003031 }
3032
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003033 // Clear the flag indicating that the debugger should be unloaded.
3034 debugger_unload_pending_ = false;
ager@chromium.org71daaf62009-04-01 07:22:49 +00003035}
3036
3037
ager@chromium.org41826e72009-03-30 13:30:57 +00003038void Debugger::NotifyMessageHandler(v8::DebugEvent event,
ager@chromium.org5ec48922009-05-05 07:25:34 +00003039 Handle<JSObject> exec_state,
3040 Handle<JSObject> event_data,
ager@chromium.org41826e72009-03-30 13:30:57 +00003041 bool auto_continue) {
machenbach@chromium.orgf9841892013-11-25 12:01:13 +00003042 v8::Isolate* isolate = reinterpret_cast<v8::Isolate*>(isolate_);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003043 HandleScope scope(isolate_);
ager@chromium.org41826e72009-03-30 13:30:57 +00003044
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003045 if (!isolate_->debug()->Load()) return;
ager@chromium.org41826e72009-03-30 13:30:57 +00003046
3047 // Process the individual events.
ager@chromium.org5ec48922009-05-05 07:25:34 +00003048 bool sendEventMessage = false;
ager@chromium.org41826e72009-03-30 13:30:57 +00003049 switch (event) {
3050 case v8::Break:
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00003051 case v8::BreakForCommand:
ager@chromium.org5ec48922009-05-05 07:25:34 +00003052 sendEventMessage = !auto_continue;
ager@chromium.org41826e72009-03-30 13:30:57 +00003053 break;
3054 case v8::Exception:
ager@chromium.org5ec48922009-05-05 07:25:34 +00003055 sendEventMessage = true;
ager@chromium.org41826e72009-03-30 13:30:57 +00003056 break;
3057 case v8::BeforeCompile:
3058 break;
3059 case v8::AfterCompile:
ager@chromium.org5ec48922009-05-05 07:25:34 +00003060 sendEventMessage = true;
ager@chromium.org41826e72009-03-30 13:30:57 +00003061 break;
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003062 case v8::ScriptCollected:
3063 sendEventMessage = true;
3064 break;
ager@chromium.org41826e72009-03-30 13:30:57 +00003065 case v8::NewFunction:
3066 break;
3067 default:
3068 UNREACHABLE();
3069 }
3070
ager@chromium.org5ec48922009-05-05 07:25:34 +00003071 // The debug command interrupt flag might have been set when the command was
3072 // added. It should be enough to clear the flag only once while we are in the
3073 // debugger.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003074 ASSERT(isolate_->debug()->InDebugger());
3075 isolate_->stack_guard()->Continue(DEBUGCOMMAND);
ager@chromium.org5ec48922009-05-05 07:25:34 +00003076
3077 // Notify the debugger that a debug event has occurred unless auto continue is
3078 // active in which case no event is send.
3079 if (sendEventMessage) {
3080 MessageImpl message = MessageImpl::NewEvent(
3081 event,
3082 auto_continue,
3083 Handle<JSObject>::cast(exec_state),
3084 Handle<JSObject>::cast(event_data));
3085 InvokeMessageHandler(message);
3086 }
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +00003087
3088 // If auto continue don't make the event cause a break, but process messages
3089 // in the queue if any. For script collected events don't even process
3090 // messages in the queue as the execution state might not be what is expected
3091 // by the client.
ager@chromium.org6ffc2172009-05-29 19:20:16 +00003092 if ((auto_continue && !HasCommands()) || event == v8::ScriptCollected) {
ager@chromium.org5ec48922009-05-05 07:25:34 +00003093 return;
3094 }
ager@chromium.org41826e72009-03-30 13:30:57 +00003095
ager@chromium.org41826e72009-03-30 13:30:57 +00003096 v8::TryCatch try_catch;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003097
3098 // DebugCommandProcessor goes here.
3099 v8::Local<v8::Object> cmd_processor;
3100 {
3101 v8::Local<v8::Object> api_exec_state =
3102 v8::Utils::ToLocal(Handle<JSObject>::cast(exec_state));
machenbach@chromium.orgf9841892013-11-25 12:01:13 +00003103 v8::Local<v8::String> fun_name = v8::String::NewFromUtf8(
3104 isolate, "debugCommandProcessor");
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003105 v8::Local<v8::Function> fun =
danno@chromium.orgf95d4b92013-06-13 14:40:17 +00003106 v8::Local<v8::Function>::Cast(api_exec_state->Get(fun_name));
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003107
machenbach@chromium.org37be4082013-11-26 13:50:38 +00003108 v8::Handle<v8::Boolean> running = v8::Boolean::New(isolate, auto_continue);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003109 static const int kArgc = 1;
3110 v8::Handle<Value> argv[kArgc] = { running };
danno@chromium.orgf95d4b92013-06-13 14:40:17 +00003111 cmd_processor = v8::Local<v8::Object>::Cast(
3112 fun->Call(api_exec_state, kArgc, argv));
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003113 if (try_catch.HasCaught()) {
3114 PrintLn(try_catch.Exception());
3115 return;
3116 }
ager@chromium.org41826e72009-03-30 13:30:57 +00003117 }
3118
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003119 bool running = auto_continue;
3120
ager@chromium.org41826e72009-03-30 13:30:57 +00003121 // Process requests from the debugger.
3122 while (true) {
3123 // Wait for new command in the queue.
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003124 if (Debugger::host_dispatch_handler_) {
3125 // In case there is a host dispatch - do periodic dispatches.
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +00003126 if (!command_received_.WaitFor(host_dispatch_period_)) {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003127 // Timout expired, do the dispatch.
3128 Debugger::host_dispatch_handler_();
3129 continue;
3130 }
3131 } else {
3132 // In case there is no host dispatch - just wait.
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +00003133 command_received_.Wait();
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003134 }
ager@chromium.org41826e72009-03-30 13:30:57 +00003135
ager@chromium.org41826e72009-03-30 13:30:57 +00003136 // Get the command from the queue.
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003137 CommandMessage command = command_queue_.Get();
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00003138 isolate_->logger()->DebugTag(
3139 "Got request from command queue, in interactive loop.");
ager@chromium.org71daaf62009-04-01 07:22:49 +00003140 if (!Debugger::IsDebuggerActive()) {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003141 // Delete command text and user data.
3142 command.Dispose();
ager@chromium.org41826e72009-03-30 13:30:57 +00003143 return;
3144 }
3145
ager@chromium.org41826e72009-03-30 13:30:57 +00003146 // Invoke JavaScript to process the debug request.
3147 v8::Local<v8::String> fun_name;
3148 v8::Local<v8::Function> fun;
3149 v8::Local<v8::Value> request;
3150 v8::TryCatch try_catch;
machenbach@chromium.orgf9841892013-11-25 12:01:13 +00003151 fun_name = v8::String::NewFromUtf8(isolate, "processDebugRequest");
danno@chromium.orgf95d4b92013-06-13 14:40:17 +00003152 fun = v8::Local<v8::Function>::Cast(cmd_processor->Get(fun_name));
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003153
machenbach@chromium.orgf9841892013-11-25 12:01:13 +00003154 request = v8::String::NewFromTwoByte(isolate, command.text().start(),
3155 v8::String::kNormalString,
3156 command.text().length());
ager@chromium.org41826e72009-03-30 13:30:57 +00003157 static const int kArgc = 1;
3158 v8::Handle<Value> argv[kArgc] = { request };
3159 v8::Local<v8::Value> response_val = fun->Call(cmd_processor, kArgc, argv);
3160
3161 // Get the response.
3162 v8::Local<v8::String> response;
ager@chromium.org41826e72009-03-30 13:30:57 +00003163 if (!try_catch.HasCaught()) {
3164 // Get response string.
3165 if (!response_val->IsUndefined()) {
danno@chromium.orgf95d4b92013-06-13 14:40:17 +00003166 response = v8::Local<v8::String>::Cast(response_val);
ager@chromium.org41826e72009-03-30 13:30:57 +00003167 } else {
machenbach@chromium.orgf9841892013-11-25 12:01:13 +00003168 response = v8::String::NewFromUtf8(isolate, "");
ager@chromium.org41826e72009-03-30 13:30:57 +00003169 }
3170
3171 // Log the JSON request/response.
3172 if (FLAG_trace_debug_json) {
3173 PrintLn(request);
3174 PrintLn(response);
3175 }
3176
3177 // Get the running state.
machenbach@chromium.orgf9841892013-11-25 12:01:13 +00003178 fun_name = v8::String::NewFromUtf8(isolate, "isRunning");
danno@chromium.orgf95d4b92013-06-13 14:40:17 +00003179 fun = v8::Local<v8::Function>::Cast(cmd_processor->Get(fun_name));
ager@chromium.org41826e72009-03-30 13:30:57 +00003180 static const int kArgc = 1;
3181 v8::Handle<Value> argv[kArgc] = { response };
3182 v8::Local<v8::Value> running_val = fun->Call(cmd_processor, kArgc, argv);
3183 if (!try_catch.HasCaught()) {
3184 running = running_val->ToBoolean()->Value();
3185 }
3186 } else {
3187 // In case of failure the result text is the exception text.
3188 response = try_catch.Exception()->ToString();
3189 }
3190
ager@chromium.org41826e72009-03-30 13:30:57 +00003191 // Return the result.
ager@chromium.org5ec48922009-05-05 07:25:34 +00003192 MessageImpl message = MessageImpl::NewResponse(
3193 event,
3194 running,
3195 Handle<JSObject>::cast(exec_state),
3196 Handle<JSObject>::cast(event_data),
3197 Handle<String>(Utils::OpenHandle(*response)),
3198 command.client_data());
3199 InvokeMessageHandler(message);
3200 command.Dispose();
ager@chromium.org41826e72009-03-30 13:30:57 +00003201
3202 // Return from debug event processing if either the VM is put into the
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00003203 // running state (through a continue command) or auto continue is active
ager@chromium.org41826e72009-03-30 13:30:57 +00003204 // and there are no more commands queued.
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003205 if (running && !HasCommands()) {
ager@chromium.org41826e72009-03-30 13:30:57 +00003206 return;
3207 }
3208 }
3209}
3210
3211
iposva@chromium.org245aa852009-02-10 00:49:54 +00003212void Debugger::SetEventListener(Handle<Object> callback,
3213 Handle<Object> data) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00003214 HandleScope scope(isolate_);
3215 GlobalHandles* global_handles = isolate_->global_handles();
iposva@chromium.org245aa852009-02-10 00:49:54 +00003216
3217 // Clear the global handles for the event listener and the event listener data
3218 // object.
3219 if (!event_listener_.is_null()) {
hpayer@chromium.org4f99be92013-12-18 16:23:55 +00003220 GlobalHandles::Destroy(
iposva@chromium.org245aa852009-02-10 00:49:54 +00003221 reinterpret_cast<Object**>(event_listener_.location()));
3222 event_listener_ = Handle<Object>();
3223 }
3224 if (!event_listener_data_.is_null()) {
hpayer@chromium.org4f99be92013-12-18 16:23:55 +00003225 GlobalHandles::Destroy(
iposva@chromium.org245aa852009-02-10 00:49:54 +00003226 reinterpret_cast<Object**>(event_listener_data_.location()));
3227 event_listener_data_ = Handle<Object>();
3228 }
3229
3230 // If there is a new debug event listener register it together with its data
3231 // object.
3232 if (!callback->IsUndefined() && !callback->IsNull()) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003233 event_listener_ = Handle<Object>::cast(
lrn@chromium.org7516f052011-03-30 08:52:27 +00003234 global_handles->Create(*callback));
iposva@chromium.org245aa852009-02-10 00:49:54 +00003235 if (data.is_null()) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00003236 data = isolate_->factory()->undefined_value();
iposva@chromium.org245aa852009-02-10 00:49:54 +00003237 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003238 event_listener_data_ = Handle<Object>::cast(
lrn@chromium.org7516f052011-03-30 08:52:27 +00003239 global_handles->Create(*data));
iposva@chromium.org245aa852009-02-10 00:49:54 +00003240 }
3241
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003242 ListenersChanged();
iposva@chromium.org245aa852009-02-10 00:49:54 +00003243}
3244
3245
ager@chromium.org5ec48922009-05-05 07:25:34 +00003246void Debugger::SetMessageHandler(v8::Debug::MessageHandler2 handler) {
jkummerow@chromium.orgdc94e192013-08-30 11:35:42 +00003247 LockGuard<RecursiveMutex> with(debugger_access_);
ager@chromium.org71daaf62009-04-01 07:22:49 +00003248
ager@chromium.org381abbb2009-02-25 13:23:22 +00003249 message_handler_ = handler;
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003250 ListenersChanged();
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003251 if (handler == NULL) {
ager@chromium.org71daaf62009-04-01 07:22:49 +00003252 // Send an empty command to the debugger if in a break to make JavaScript
3253 // run again if the debugger is closed.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003254 if (isolate_->debug()->InDebugger()) {
ager@chromium.org71daaf62009-04-01 07:22:49 +00003255 ProcessCommand(Vector<const uint16_t>::empty());
3256 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003257 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003258}
3259
3260
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003261void Debugger::ListenersChanged() {
3262 if (IsDebuggerActive()) {
3263 // Disable the compilation cache when the debugger is active.
lrn@chromium.org7516f052011-03-30 08:52:27 +00003264 isolate_->compilation_cache()->Disable();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003265 debugger_unload_pending_ = false;
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003266 } else {
lrn@chromium.org7516f052011-03-30 08:52:27 +00003267 isolate_->compilation_cache()->Enable();
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003268 // Unload the debugger if event listener and message handler cleared.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003269 // Schedule this for later, because we may be in non-V8 thread.
3270 debugger_unload_pending_ = true;
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003271 }
3272}
3273
3274
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003275void Debugger::SetHostDispatchHandler(v8::Debug::HostDispatchHandler handler,
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +00003276 TimeDelta period) {
ager@chromium.org381abbb2009-02-25 13:23:22 +00003277 host_dispatch_handler_ = handler;
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +00003278 host_dispatch_period_ = period;
ager@chromium.org381abbb2009-02-25 13:23:22 +00003279}
3280
3281
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003282void Debugger::SetDebugMessageDispatchHandler(
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003283 v8::Debug::DebugMessageDispatchHandler handler, bool provide_locker) {
jkummerow@chromium.orgdc94e192013-08-30 11:35:42 +00003284 LockGuard<Mutex> lock_guard(&dispatch_handler_access_);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003285 debug_message_dispatch_handler_ = handler;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003286
3287 if (provide_locker && message_dispatch_helper_thread_ == NULL) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003288 message_dispatch_helper_thread_ = new MessageDispatchHelperThread(isolate_);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003289 message_dispatch_helper_thread_->Start();
3290 }
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003291}
3292
3293
ager@chromium.org41826e72009-03-30 13:30:57 +00003294// Calls the registered debug message handler. This callback is part of the
ager@chromium.org5ec48922009-05-05 07:25:34 +00003295// public API.
3296void Debugger::InvokeMessageHandler(MessageImpl message) {
jkummerow@chromium.orgdc94e192013-08-30 11:35:42 +00003297 LockGuard<RecursiveMutex> with(debugger_access_);
ager@chromium.org71daaf62009-04-01 07:22:49 +00003298
ager@chromium.org381abbb2009-02-25 13:23:22 +00003299 if (message_handler_ != NULL) {
ager@chromium.org5ec48922009-05-05 07:25:34 +00003300 message_handler_(message);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003301 }
ager@chromium.org41826e72009-03-30 13:30:57 +00003302}
3303
3304
3305// Puts a command coming from the public API on the queue. Creates
3306// a copy of the command string managed by the debugger. Up to this
3307// point, the command data was managed by the API client. Called
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003308// by the API client thread.
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003309void Debugger::ProcessCommand(Vector<const uint16_t> command,
3310 v8::Debug::ClientData* client_data) {
3311 // Need to cast away const.
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003312 CommandMessage message = CommandMessage::New(
ager@chromium.org41826e72009-03-30 13:30:57 +00003313 Vector<uint16_t>(const_cast<uint16_t*>(command.start()),
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003314 command.length()),
3315 client_data);
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00003316 isolate_->logger()->DebugTag("Put command on command_queue.");
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003317 command_queue_.Put(message);
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +00003318 command_received_.Signal();
sgjesse@chromium.org3afc1582009-04-16 22:31:44 +00003319
3320 // Set the debug command break flag to have the command processed.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003321 if (!isolate_->debug()->InDebugger()) {
3322 isolate_->stack_guard()->DebugCommand();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003323 }
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003324
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003325 MessageDispatchHelperThread* dispatch_thread;
3326 {
jkummerow@chromium.orgdc94e192013-08-30 11:35:42 +00003327 LockGuard<Mutex> lock_guard(&dispatch_handler_access_);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003328 dispatch_thread = message_dispatch_helper_thread_;
3329 }
3330
3331 if (dispatch_thread == NULL) {
3332 CallMessageDispatchHandler();
3333 } else {
3334 dispatch_thread->Schedule();
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003335 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003336}
3337
3338
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003339bool Debugger::HasCommands() {
ager@chromium.org41826e72009-03-30 13:30:57 +00003340 return !command_queue_.IsEmpty();
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003341}
3342
3343
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00003344void Debugger::EnqueueDebugCommand(v8::Debug::ClientData* client_data) {
3345 CommandMessage message = CommandMessage::New(Vector<uint16_t>(), client_data);
3346 event_command_queue_.Put(message);
3347
3348 // Set the debug command break flag to have the command processed.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003349 if (!isolate_->debug()->InDebugger()) {
3350 isolate_->stack_guard()->DebugCommand();
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00003351 }
3352}
3353
3354
ager@chromium.org71daaf62009-04-01 07:22:49 +00003355bool Debugger::IsDebuggerActive() {
jkummerow@chromium.orgdc94e192013-08-30 11:35:42 +00003356 LockGuard<RecursiveMutex> with(debugger_access_);
ager@chromium.org71daaf62009-04-01 07:22:49 +00003357
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00003358 return message_handler_ != NULL ||
3359 !event_listener_.is_null() ||
3360 force_debugger_active_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003361}
3362
3363
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003364Handle<Object> Debugger::Call(Handle<JSFunction> fun,
3365 Handle<Object> data,
3366 bool* pending_exception) {
ager@chromium.org71daaf62009-04-01 07:22:49 +00003367 // When calling functions in the debugger prevent it from beeing unloaded.
3368 Debugger::never_unload_debugger_ = true;
3369
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003370 // Enter the debugger.
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +00003371 EnterDebugger debugger(isolate_);
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +00003372 if (debugger.FailedToEnter()) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00003373 return isolate_->factory()->undefined_value();
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003374 }
3375
3376 // Create the execution state.
3377 bool caught_exception = false;
3378 Handle<Object> exec_state = MakeExecutionState(&caught_exception);
3379 if (caught_exception) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00003380 return isolate_->factory()->undefined_value();
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003381 }
3382
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00003383 Handle<Object> argv[] = { exec_state, data };
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +00003384 Handle<Object> result = Execution::Call(
jkummerow@chromium.org2c9426b2013-09-05 16:31:13 +00003385 isolate_,
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +00003386 fun,
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00003387 Handle<Object>(isolate_->debug()->debug_context_->global_proxy(),
3388 isolate_),
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00003389 ARRAY_SIZE(argv),
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +00003390 argv,
3391 pending_exception);
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003392 return result;
3393}
3394
3395
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003396static void StubMessageHandler2(const v8::Debug::Message& message) {
3397 // Simply ignore message.
3398}
3399
3400
3401bool Debugger::StartAgent(const char* name, int port,
3402 bool wait_for_connection) {
3403 if (wait_for_connection) {
3404 // Suspend V8 if it is already running or set V8 to suspend whenever
3405 // it starts.
3406 // Provide stub message handler; V8 auto-continues each suspend
3407 // when there is no message handler; we doesn't need it.
3408 // Once become suspended, V8 will stay so indefinitely long, until remote
3409 // debugger connects and issues "continue" command.
3410 Debugger::message_handler_ = StubMessageHandler2;
3411 v8::Debug::DebugBreak();
3412 }
3413
jkummerow@chromium.org3d00d0a2013-09-04 13:57:32 +00003414 if (agent_ == NULL) {
3415 agent_ = new DebuggerAgent(isolate_, name, port);
3416 agent_->Start();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003417 }
jkummerow@chromium.org3d00d0a2013-09-04 13:57:32 +00003418 return true;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003419}
3420
3421
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003422void Debugger::StopAgent() {
3423 if (agent_ != NULL) {
3424 agent_->Shutdown();
3425 agent_->Join();
3426 delete agent_;
3427 agent_ = NULL;
3428 }
3429}
3430
3431
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003432void Debugger::WaitForAgent() {
3433 if (agent_ != NULL)
3434 agent_->WaitUntilListening();
3435}
3436
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003437
3438void Debugger::CallMessageDispatchHandler() {
3439 v8::Debug::DebugMessageDispatchHandler handler;
3440 {
jkummerow@chromium.orgdc94e192013-08-30 11:35:42 +00003441 LockGuard<Mutex> lock_guard(&dispatch_handler_access_);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003442 handler = Debugger::debug_message_dispatch_handler_;
3443 }
3444 if (handler != NULL) {
3445 handler();
3446 }
3447}
3448
3449
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +00003450EnterDebugger::EnterDebugger(Isolate* isolate)
3451 : isolate_(isolate),
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003452 prev_(isolate_->debug()->debugger_entry()),
3453 it_(isolate_),
3454 has_js_frames_(!it_.done()),
3455 save_(isolate_) {
3456 Debug* debug = isolate_->debug();
3457 ASSERT(prev_ != NULL || !debug->is_interrupt_pending(PREEMPT));
3458 ASSERT(prev_ != NULL || !debug->is_interrupt_pending(DEBUGBREAK));
3459
3460 // Link recursive debugger entry.
3461 debug->set_debugger_entry(this);
3462
3463 // Store the previous break id and frame id.
3464 break_id_ = debug->break_id();
3465 break_frame_id_ = debug->break_frame_id();
3466
3467 // Create the new break info. If there is no JavaScript frames there is no
3468 // break frame id.
3469 if (has_js_frames_) {
3470 debug->NewBreak(it_.frame()->id());
3471 } else {
3472 debug->NewBreak(StackFrame::NO_ID);
3473 }
3474
3475 // Make sure that debugger is loaded and enter the debugger context.
3476 load_failed_ = !debug->Load();
3477 if (!load_failed_) {
3478 // NOTE the member variable save which saves the previous context before
3479 // this change.
3480 isolate_->set_context(*debug->debug_context());
3481 }
3482}
3483
3484
3485EnterDebugger::~EnterDebugger() {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003486 Debug* debug = isolate_->debug();
3487
3488 // Restore to the previous break state.
3489 debug->SetBreak(break_frame_id_, break_id_);
3490
3491 // Check for leaving the debugger.
fschneider@chromium.org7d10be52012-04-10 12:30:14 +00003492 if (!load_failed_ && prev_ == NULL) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003493 // Clear mirror cache when leaving the debugger. Skip this if there is a
3494 // pending exception as clearing the mirror cache calls back into
3495 // JavaScript. This can happen if the v8::Debug::Call is used in which
3496 // case the exception should end up in the calling code.
3497 if (!isolate_->has_pending_exception()) {
3498 // Try to avoid any pending debug break breaking in the clear mirror
3499 // cache JavaScript code.
3500 if (isolate_->stack_guard()->IsDebugBreak()) {
3501 debug->set_interrupts_pending(DEBUGBREAK);
3502 isolate_->stack_guard()->Continue(DEBUGBREAK);
3503 }
3504 debug->ClearMirrorCache();
3505 }
3506
3507 // Request preemption and debug break when leaving the last debugger entry
3508 // if any of these where recorded while debugging.
3509 if (debug->is_interrupt_pending(PREEMPT)) {
3510 // This re-scheduling of preemption is to avoid starvation in some
3511 // debugging scenarios.
3512 debug->clear_interrupt_pending(PREEMPT);
3513 isolate_->stack_guard()->Preempt();
3514 }
3515 if (debug->is_interrupt_pending(DEBUGBREAK)) {
3516 debug->clear_interrupt_pending(DEBUGBREAK);
3517 isolate_->stack_guard()->DebugBreak();
3518 }
3519
3520 // If there are commands in the queue when leaving the debugger request
3521 // that these commands are processed.
3522 if (isolate_->debugger()->HasCommands()) {
3523 isolate_->stack_guard()->DebugCommand();
3524 }
3525
3526 // If leaving the debugger with the debugger no longer active unload it.
3527 if (!isolate_->debugger()->IsDebuggerActive()) {
3528 isolate_->debugger()->UnloadDebugger();
3529 }
3530 }
3531
3532 // Leaving this debugger entry.
3533 debug->set_debugger_entry(prev_);
3534}
3535
3536
ager@chromium.org5ec48922009-05-05 07:25:34 +00003537MessageImpl MessageImpl::NewEvent(DebugEvent event,
3538 bool running,
3539 Handle<JSObject> exec_state,
3540 Handle<JSObject> event_data) {
3541 MessageImpl message(true, event, running,
3542 exec_state, event_data, Handle<String>(), NULL);
3543 return message;
3544}
3545
3546
3547MessageImpl MessageImpl::NewResponse(DebugEvent event,
3548 bool running,
3549 Handle<JSObject> exec_state,
3550 Handle<JSObject> event_data,
3551 Handle<String> response_json,
3552 v8::Debug::ClientData* client_data) {
3553 MessageImpl message(false, event, running,
3554 exec_state, event_data, response_json, client_data);
3555 return message;
3556}
3557
3558
3559MessageImpl::MessageImpl(bool is_event,
3560 DebugEvent event,
3561 bool running,
3562 Handle<JSObject> exec_state,
3563 Handle<JSObject> event_data,
3564 Handle<String> response_json,
3565 v8::Debug::ClientData* client_data)
3566 : is_event_(is_event),
3567 event_(event),
3568 running_(running),
3569 exec_state_(exec_state),
3570 event_data_(event_data),
3571 response_json_(response_json),
3572 client_data_(client_data) {}
3573
3574
3575bool MessageImpl::IsEvent() const {
3576 return is_event_;
3577}
3578
3579
3580bool MessageImpl::IsResponse() const {
3581 return !is_event_;
3582}
3583
3584
3585DebugEvent MessageImpl::GetEvent() const {
3586 return event_;
3587}
3588
3589
3590bool MessageImpl::WillStartRunning() const {
3591 return running_;
3592}
3593
3594
3595v8::Handle<v8::Object> MessageImpl::GetExecutionState() const {
3596 return v8::Utils::ToLocal(exec_state_);
3597}
3598
3599
hpayer@chromium.orgc5d49712013-09-11 08:25:48 +00003600v8::Isolate* MessageImpl::GetIsolate() const {
3601 return reinterpret_cast<v8::Isolate*>(exec_state_->GetIsolate());
3602}
3603
3604
ager@chromium.org5ec48922009-05-05 07:25:34 +00003605v8::Handle<v8::Object> MessageImpl::GetEventData() const {
3606 return v8::Utils::ToLocal(event_data_);
3607}
3608
3609
3610v8::Handle<v8::String> MessageImpl::GetJSON() const {
machenbach@chromium.orgce9c5142013-12-03 08:00:39 +00003611 v8::EscapableHandleScope scope(
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +00003612 reinterpret_cast<v8::Isolate*>(event_data_->GetIsolate()));
ager@chromium.org5ec48922009-05-05 07:25:34 +00003613
3614 if (IsEvent()) {
3615 // Call toJSONProtocol on the debug event object.
3616 Handle<Object> fun = GetProperty(event_data_, "toJSONProtocol");
3617 if (!fun->IsJSFunction()) {
3618 return v8::Handle<v8::String>();
3619 }
3620 bool caught_exception;
3621 Handle<Object> json = Execution::TryCall(Handle<JSFunction>::cast(fun),
3622 event_data_,
3623 0, NULL, &caught_exception);
3624 if (caught_exception || !json->IsString()) {
3625 return v8::Handle<v8::String>();
3626 }
machenbach@chromium.orgce9c5142013-12-03 08:00:39 +00003627 return scope.Escape(v8::Utils::ToLocal(Handle<String>::cast(json)));
ager@chromium.org5ec48922009-05-05 07:25:34 +00003628 } else {
3629 return v8::Utils::ToLocal(response_json_);
3630 }
3631}
3632
3633
3634v8::Handle<v8::Context> MessageImpl::GetEventContext() const {
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +00003635 Isolate* isolate = event_data_->GetIsolate();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003636 v8::Handle<v8::Context> context = GetDebugEventContext(isolate);
3637 // Isolate::context() may be NULL when "script collected" event occures.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003638 ASSERT(!context.IsEmpty() || event_ == v8::ScriptCollected);
rossberg@chromium.org717967f2011-07-20 13:44:42 +00003639 return context;
ager@chromium.org5ec48922009-05-05 07:25:34 +00003640}
3641
3642
3643v8::Debug::ClientData* MessageImpl::GetClientData() const {
3644 return client_data_;
3645}
3646
3647
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003648EventDetailsImpl::EventDetailsImpl(DebugEvent event,
3649 Handle<JSObject> exec_state,
3650 Handle<JSObject> event_data,
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00003651 Handle<Object> callback_data,
3652 v8::Debug::ClientData* client_data)
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003653 : event_(event),
3654 exec_state_(exec_state),
3655 event_data_(event_data),
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00003656 callback_data_(callback_data),
3657 client_data_(client_data) {}
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003658
3659
3660DebugEvent EventDetailsImpl::GetEvent() const {
3661 return event_;
3662}
3663
3664
3665v8::Handle<v8::Object> EventDetailsImpl::GetExecutionState() const {
3666 return v8::Utils::ToLocal(exec_state_);
3667}
3668
3669
3670v8::Handle<v8::Object> EventDetailsImpl::GetEventData() const {
3671 return v8::Utils::ToLocal(event_data_);
3672}
3673
3674
3675v8::Handle<v8::Context> EventDetailsImpl::GetEventContext() const {
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +00003676 return GetDebugEventContext(exec_state_->GetIsolate());
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003677}
3678
3679
3680v8::Handle<v8::Value> EventDetailsImpl::GetCallbackData() const {
3681 return v8::Utils::ToLocal(callback_data_);
3682}
3683
3684
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00003685v8::Debug::ClientData* EventDetailsImpl::GetClientData() const {
3686 return client_data_;
3687}
3688
3689
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003690CommandMessage::CommandMessage() : text_(Vector<uint16_t>::empty()),
3691 client_data_(NULL) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003692}
3693
3694
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003695CommandMessage::CommandMessage(const Vector<uint16_t>& text,
3696 v8::Debug::ClientData* data)
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003697 : text_(text),
3698 client_data_(data) {
3699}
3700
3701
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003702CommandMessage::~CommandMessage() {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003703}
3704
3705
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003706void CommandMessage::Dispose() {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003707 text_.Dispose();
3708 delete client_data_;
3709 client_data_ = NULL;
3710}
3711
3712
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003713CommandMessage CommandMessage::New(const Vector<uint16_t>& command,
3714 v8::Debug::ClientData* data) {
3715 return CommandMessage(command.Clone(), data);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003716}
3717
3718
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003719CommandMessageQueue::CommandMessageQueue(int size) : start_(0), end_(0),
3720 size_(size) {
3721 messages_ = NewArray<CommandMessage>(size);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003722}
3723
3724
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003725CommandMessageQueue::~CommandMessageQueue() {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003726 while (!IsEmpty()) {
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003727 CommandMessage m = Get();
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003728 m.Dispose();
3729 }
kasper.lund7276f142008-07-30 08:49:36 +00003730 DeleteArray(messages_);
3731}
3732
3733
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003734CommandMessage CommandMessageQueue::Get() {
kasper.lund7276f142008-07-30 08:49:36 +00003735 ASSERT(!IsEmpty());
3736 int result = start_;
3737 start_ = (start_ + 1) % size_;
3738 return messages_[result];
3739}
3740
3741
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003742void CommandMessageQueue::Put(const CommandMessage& message) {
kasper.lund7276f142008-07-30 08:49:36 +00003743 if ((end_ + 1) % size_ == start_) {
3744 Expand();
3745 }
3746 messages_[end_] = message;
3747 end_ = (end_ + 1) % size_;
3748}
3749
3750
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003751void CommandMessageQueue::Expand() {
3752 CommandMessageQueue new_queue(size_ * 2);
kasper.lund7276f142008-07-30 08:49:36 +00003753 while (!IsEmpty()) {
3754 new_queue.Put(Get());
3755 }
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003756 CommandMessage* array_to_free = messages_;
kasper.lund7276f142008-07-30 08:49:36 +00003757 *this = new_queue;
3758 new_queue.messages_ = array_to_free;
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003759 // Make the new_queue empty so that it doesn't call Dispose on any messages.
3760 new_queue.start_ = new_queue.end_;
kasper.lund7276f142008-07-30 08:49:36 +00003761 // Automatic destructor called on new_queue, freeing array_to_free.
3762}
3763
3764
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00003765LockingCommandMessageQueue::LockingCommandMessageQueue(Logger* logger, int size)
jkummerow@chromium.orgdc94e192013-08-30 11:35:42 +00003766 : logger_(logger), queue_(size) {}
kasper.lund7276f142008-07-30 08:49:36 +00003767
3768
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003769bool LockingCommandMessageQueue::IsEmpty() const {
jkummerow@chromium.orgdc94e192013-08-30 11:35:42 +00003770 LockGuard<Mutex> lock_guard(&mutex_);
kasper.lund7276f142008-07-30 08:49:36 +00003771 return queue_.IsEmpty();
3772}
3773
3774
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003775CommandMessage LockingCommandMessageQueue::Get() {
jkummerow@chromium.orgdc94e192013-08-30 11:35:42 +00003776 LockGuard<Mutex> lock_guard(&mutex_);
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003777 CommandMessage result = queue_.Get();
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00003778 logger_->DebugEvent("Get", result.text());
kasper.lund7276f142008-07-30 08:49:36 +00003779 return result;
3780}
3781
3782
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003783void LockingCommandMessageQueue::Put(const CommandMessage& message) {
jkummerow@chromium.orgdc94e192013-08-30 11:35:42 +00003784 LockGuard<Mutex> lock_guard(&mutex_);
kasper.lund7276f142008-07-30 08:49:36 +00003785 queue_.Put(message);
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00003786 logger_->DebugEvent("Put", message.text());
kasper.lund7276f142008-07-30 08:49:36 +00003787}
3788
3789
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003790void LockingCommandMessageQueue::Clear() {
jkummerow@chromium.orgdc94e192013-08-30 11:35:42 +00003791 LockGuard<Mutex> lock_guard(&mutex_);
kasper.lund7276f142008-07-30 08:49:36 +00003792 queue_.Clear();
3793}
3794
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003795
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003796MessageDispatchHelperThread::MessageDispatchHelperThread(Isolate* isolate)
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00003797 : Thread("v8:MsgDispHelpr"),
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +00003798 isolate_(isolate), sem_(0),
jkummerow@chromium.orgdc94e192013-08-30 11:35:42 +00003799 already_signalled_(false) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003800}
3801
3802
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003803void MessageDispatchHelperThread::Schedule() {
3804 {
jkummerow@chromium.orgdc94e192013-08-30 11:35:42 +00003805 LockGuard<Mutex> lock_guard(&mutex_);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003806 if (already_signalled_) {
3807 return;
3808 }
3809 already_signalled_ = true;
3810 }
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +00003811 sem_.Signal();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003812}
3813
3814
3815void MessageDispatchHelperThread::Run() {
3816 while (true) {
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +00003817 sem_.Wait();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003818 {
jkummerow@chromium.orgdc94e192013-08-30 11:35:42 +00003819 LockGuard<Mutex> lock_guard(&mutex_);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003820 already_signalled_ = false;
3821 }
3822 {
mstarzinger@chromium.orgf705b502013-04-04 11:38:09 +00003823 Locker locker(reinterpret_cast<v8::Isolate*>(isolate_));
3824 isolate_->debugger()->CallMessageDispatchHandler();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003825 }
3826 }
3827}
3828
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003829#endif // ENABLE_DEBUGGER_SUPPORT
kasper.lund7276f142008-07-30 08:49:36 +00003830
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003831} } // namespace v8::internal