blob: 4a7fa6bb5186b9d629a18350ae5c9b8c3a087dd3 [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);
yangguo@chromium.org49546742013-12-23 16:17:49 +00002128 function->ReplaceCode(*Compiler::GetCodeForDebugging(function));
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002129 isolate_->debugger()->set_force_debugger_active(
2130 prev_force_debugger_active);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002131 }
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002132
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002133 // Keep function code in sync with shared function info.
2134 function->set_code(shared->code());
lrn@chromium.org34e60782011-09-15 07:25:40 +00002135 }
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002136
2137 RedirectActivationsToRecompiledCodeOnThread(isolate_,
2138 isolate_->thread_local_top());
2139
2140 ActiveFunctionsRedirector active_functions_redirector;
2141 isolate_->thread_manager()->IterateArchivedThreads(
2142 &active_functions_redirector);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002143 }
lrn@chromium.org34e60782011-09-15 07:25:40 +00002144}
2145
2146
jkummerow@chromium.org78502a92012-09-06 13:50:42 +00002147Object* Debug::FindSharedFunctionInfoInScript(Handle<Script> script,
2148 int position) {
2149 // Iterate the heap looking for SharedFunctionInfo generated from the
2150 // script. The inner most SharedFunctionInfo containing the source position
2151 // for the requested break point is found.
2152 // NOTE: This might require several heap iterations. If the SharedFunctionInfo
2153 // which is found is not compiled it is compiled and the heap is iterated
2154 // again as the compilation might create inner functions from the newly
2155 // compiled function and the actual requested break point might be in one of
2156 // these functions.
2157 // NOTE: The below fix-point iteration depends on all functions that cannot be
2158 // compiled lazily without a context to not be compiled at all. Compilation
2159 // will be triggered at points where we do not need a context.
2160 bool done = false;
2161 // The current candidate for the source position:
2162 int target_start_position = RelocInfo::kNoPosition;
2163 Handle<JSFunction> target_function;
2164 Handle<SharedFunctionInfo> target;
hpayer@chromium.org7c3372b2013-02-13 17:26:04 +00002165 Heap* heap = isolate_->heap();
jkummerow@chromium.org78502a92012-09-06 13:50:42 +00002166 while (!done) {
2167 { // Extra scope for iterator and no-allocation.
hpayer@chromium.org7c3372b2013-02-13 17:26:04 +00002168 heap->EnsureHeapIsIterable();
rossberg@chromium.org79e79022013-06-03 15:43:46 +00002169 DisallowHeapAllocation no_alloc_during_heap_iteration;
hpayer@chromium.org7c3372b2013-02-13 17:26:04 +00002170 HeapIterator iterator(heap);
jkummerow@chromium.org78502a92012-09-06 13:50:42 +00002171 for (HeapObject* obj = iterator.next();
2172 obj != NULL; obj = iterator.next()) {
2173 bool found_next_candidate = false;
2174 Handle<JSFunction> function;
2175 Handle<SharedFunctionInfo> shared;
2176 if (obj->IsJSFunction()) {
2177 function = Handle<JSFunction>(JSFunction::cast(obj));
2178 shared = Handle<SharedFunctionInfo>(function->shared());
2179 ASSERT(shared->allows_lazy_compilation() || shared->is_compiled());
2180 found_next_candidate = true;
2181 } else if (obj->IsSharedFunctionInfo()) {
2182 shared = Handle<SharedFunctionInfo>(SharedFunctionInfo::cast(obj));
2183 // Skip functions that we cannot compile lazily without a context,
2184 // which is not available here, because there is no closure.
2185 found_next_candidate = shared->is_compiled() ||
2186 shared->allows_lazy_compilation_without_context();
2187 }
2188 if (!found_next_candidate) continue;
2189 if (shared->script() == *script) {
2190 // If the SharedFunctionInfo found has the requested script data and
2191 // contains the source position it is a candidate.
2192 int start_position = shared->function_token_position();
2193 if (start_position == RelocInfo::kNoPosition) {
2194 start_position = shared->start_position();
2195 }
2196 if (start_position <= position &&
2197 position <= shared->end_position()) {
2198 // If there is no candidate or this function is within the current
2199 // candidate this is the new candidate.
2200 if (target.is_null()) {
2201 target_start_position = start_position;
2202 target_function = function;
2203 target = shared;
2204 } else {
2205 if (target_start_position == start_position &&
2206 shared->end_position() == target->end_position()) {
2207 // If a top-level function contains only one function
2208 // declaration the source for the top-level and the function
2209 // is the same. In that case prefer the non top-level function.
2210 if (!shared->is_toplevel()) {
2211 target_start_position = start_position;
2212 target_function = function;
2213 target = shared;
2214 }
2215 } else if (target_start_position <= start_position &&
2216 shared->end_position() <= target->end_position()) {
2217 // This containment check includes equality as a function
2218 // inside a top-level function can share either start or end
2219 // position with the top-level function.
2220 target_start_position = start_position;
2221 target_function = function;
2222 target = shared;
2223 }
2224 }
2225 }
2226 }
2227 } // End for loop.
2228 } // End no-allocation scope.
2229
hpayer@chromium.org7c3372b2013-02-13 17:26:04 +00002230 if (target.is_null()) return heap->undefined_value();
jkummerow@chromium.org78502a92012-09-06 13:50:42 +00002231
2232 // There will be at least one break point when we are done.
2233 has_break_points_ = true;
2234
2235 // If the candidate found is compiled we are done.
2236 done = target->is_compiled();
2237 if (!done) {
2238 // If the candidate is not compiled, compile it to reveal any inner
2239 // functions which might contain the requested source position. This
2240 // will compile all inner functions that cannot be compiled without a
2241 // context, because Compiler::BuildFunctionInfo checks whether the
2242 // debugger is active.
yangguo@chromium.org49546742013-12-23 16:17:49 +00002243 Handle<Code> result = target_function.is_null()
2244 ? Compiler::GetUnoptimizedCode(target)
2245 : Compiler::GetUnoptimizedCode(target_function);
2246 if (result.is_null()) return isolate_->heap()->undefined_value();
jkummerow@chromium.org78502a92012-09-06 13:50:42 +00002247 }
2248 } // End while loop.
2249
2250 return *target;
2251}
2252
2253
lrn@chromium.org34e60782011-09-15 07:25:40 +00002254// Ensures the debug information is present for shared.
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00002255bool Debug::EnsureDebugInfo(Handle<SharedFunctionInfo> shared,
2256 Handle<JSFunction> function) {
verwaest@chromium.orgd4be0f02013-06-05 13:39:03 +00002257 Isolate* isolate = shared->GetIsolate();
2258
lrn@chromium.org34e60782011-09-15 07:25:40 +00002259 // Return if we already have the debug info for shared.
2260 if (HasDebugInfo(shared)) {
2261 ASSERT(shared->is_compiled());
2262 return true;
2263 }
2264
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00002265 // There will be at least one break point when we are done.
2266 has_break_points_ = true;
2267
2268 // Ensure function is compiled. Return false if this failed.
2269 if (!function.is_null() &&
yangguo@chromium.org49546742013-12-23 16:17:49 +00002270 !Compiler::EnsureCompiled(function, CLEAR_EXCEPTION)) {
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002271 return false;
2272 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002273
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002274 // Create the debug info object.
verwaest@chromium.orgd4be0f02013-06-05 13:39:03 +00002275 Handle<DebugInfo> debug_info = isolate->factory()->NewDebugInfo(shared);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002276
2277 // Add debug info to the list.
2278 DebugInfoListNode* node = new DebugInfoListNode(*debug_info);
2279 node->set_next(debug_info_list_);
2280 debug_info_list_ = node;
2281
kasper.lundbd3ec4e2008-07-09 11:06:54 +00002282 return true;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002283}
2284
2285
2286void Debug::RemoveDebugInfo(Handle<DebugInfo> debug_info) {
2287 ASSERT(debug_info_list_ != NULL);
2288 // Run through the debug info objects to find this one and remove it.
2289 DebugInfoListNode* prev = NULL;
2290 DebugInfoListNode* current = debug_info_list_;
2291 while (current != NULL) {
2292 if (*current->debug_info() == *debug_info) {
2293 // Unlink from list. If prev is NULL we are looking at the first element.
2294 if (prev == NULL) {
2295 debug_info_list_ = current->next();
2296 } else {
2297 prev->set_next(current->next());
2298 }
lrn@chromium.org7516f052011-03-30 08:52:27 +00002299 current->debug_info()->shared()->set_debug_info(
2300 isolate_->heap()->undefined_value());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002301 delete current;
2302
2303 // If there are no more debug info objects there are not more break
2304 // points.
2305 has_break_points_ = debug_info_list_ != NULL;
2306
2307 return;
2308 }
2309 // Move to next in list.
2310 prev = current;
2311 current = current->next();
2312 }
2313 UNREACHABLE();
2314}
2315
2316
2317void Debug::SetAfterBreakTarget(JavaScriptFrame* frame) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002318 HandleScope scope(isolate_);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002319
lrn@chromium.org34e60782011-09-15 07:25:40 +00002320 PrepareForBreakPoints();
2321
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002322 // Get the executing function in which the debug break occurred.
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00002323 Handle<JSFunction> function(JSFunction::cast(frame->function()));
2324 Handle<SharedFunctionInfo> shared(function->shared());
2325 if (!EnsureDebugInfo(shared, function)) {
kasper.lundbd3ec4e2008-07-09 11:06:54 +00002326 // Return if we failed to retrieve the debug info.
2327 return;
2328 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002329 Handle<DebugInfo> debug_info = GetDebugInfo(shared);
2330 Handle<Code> code(debug_info->code());
2331 Handle<Code> original_code(debug_info->original_code());
2332#ifdef DEBUG
2333 // Get the code which is actually executing.
vegorov@chromium.org74f333b2011-04-06 11:17:46 +00002334 Handle<Code> frame_code(frame->LookupCode());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002335 ASSERT(frame_code.is_identical_to(code));
2336#endif
2337
2338 // Find the call address in the running code. This address holds the call to
2339 // either a DebugBreakXXX or to the debug break return entry code if the
2340 // break point is still active after processing the break point.
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00002341 Address addr = frame->pc() - Assembler::kPatchDebugBreakSlotReturnOffset;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002342
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002343 // Check if the location is at JS exit or debug break slot.
ager@chromium.orga1645e22009-09-09 19:27:10 +00002344 bool at_js_return = false;
2345 bool break_at_js_return_active = false;
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002346 bool at_debug_break_slot = false;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002347 RelocIterator it(debug_info->code());
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002348 while (!it.done() && !at_js_return && !at_debug_break_slot) {
ager@chromium.org236ad962008-09-25 09:45:57 +00002349 if (RelocInfo::IsJSReturn(it.rinfo()->rmode())) {
ager@chromium.orga1645e22009-09-09 19:27:10 +00002350 at_js_return = (it.rinfo()->pc() ==
2351 addr - Assembler::kPatchReturnSequenceAddressOffset);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002352 break_at_js_return_active = it.rinfo()->IsPatchedReturnSequence();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002353 }
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002354 if (RelocInfo::IsDebugBreakSlot(it.rinfo()->rmode())) {
2355 at_debug_break_slot = (it.rinfo()->pc() ==
2356 addr - Assembler::kPatchDebugBreakSlotAddressOffset);
2357 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002358 it.next();
2359 }
2360
2361 // Handle the jump to continue execution after break point depending on the
2362 // break location.
ager@chromium.orga1645e22009-09-09 19:27:10 +00002363 if (at_js_return) {
2364 // If the break point as return is still active jump to the corresponding
2365 // place in the original code. If not the break point was removed during
2366 // break point processing.
2367 if (break_at_js_return_active) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002368 addr += original_code->instruction_start() - code->instruction_start();
2369 }
2370
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00002371 // Move back to where the call instruction sequence started.
2372 thread_local_.after_break_target_ =
2373 addr - Assembler::kPatchReturnSequenceAddressOffset;
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002374 } else if (at_debug_break_slot) {
2375 // Address of where the debug break slot starts.
2376 addr = addr - Assembler::kPatchDebugBreakSlotAddressOffset;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002377
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002378 // Continue just after the slot.
2379 thread_local_.after_break_target_ = addr + Assembler::kDebugBreakSlotLength;
2380 } else if (IsDebugBreak(Assembler::target_address_at(addr))) {
2381 // We now know that there is still a debug break call at the target address,
2382 // so the break point is still there and the original code will hold the
2383 // address to jump to in order to complete the call which is replaced by a
2384 // call to DebugBreakXXX.
2385
2386 // Find the corresponding address in the original code.
2387 addr += original_code->instruction_start() - code->instruction_start();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002388
2389 // Install jump to the call address in the original code. This will be the
2390 // call which was overwritten by the call to DebugBreakXXX.
2391 thread_local_.after_break_target_ = Assembler::target_address_at(addr);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002392 } else {
2393 // There is no longer a break point present. Don't try to look in the
2394 // original code as the running code will have the right address. This takes
2395 // care of the case where the last break point is removed from the function
2396 // and therefore no "original code" is available.
2397 thread_local_.after_break_target_ = Assembler::target_address_at(addr);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002398 }
2399}
2400
2401
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00002402bool Debug::IsBreakAtReturn(JavaScriptFrame* frame) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002403 HandleScope scope(isolate_);
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00002404
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00002405 // If there are no break points this cannot be break at return, as
2406 // the debugger statement and stack guard bebug break cannot be at
2407 // return.
2408 if (!has_break_points_) {
2409 return false;
2410 }
2411
lrn@chromium.org34e60782011-09-15 07:25:40 +00002412 PrepareForBreakPoints();
2413
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00002414 // Get the executing function in which the debug break occurred.
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00002415 Handle<JSFunction> function(JSFunction::cast(frame->function()));
2416 Handle<SharedFunctionInfo> shared(function->shared());
2417 if (!EnsureDebugInfo(shared, function)) {
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00002418 // Return if we failed to retrieve the debug info.
2419 return false;
2420 }
2421 Handle<DebugInfo> debug_info = GetDebugInfo(shared);
2422 Handle<Code> code(debug_info->code());
2423#ifdef DEBUG
2424 // Get the code which is actually executing.
vegorov@chromium.org74f333b2011-04-06 11:17:46 +00002425 Handle<Code> frame_code(frame->LookupCode());
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00002426 ASSERT(frame_code.is_identical_to(code));
2427#endif
2428
2429 // Find the call address in the running code.
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00002430 Address addr = frame->pc() - Assembler::kPatchDebugBreakSlotReturnOffset;
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00002431
2432 // Check if the location is at JS return.
2433 RelocIterator it(debug_info->code());
2434 while (!it.done()) {
2435 if (RelocInfo::IsJSReturn(it.rinfo()->rmode())) {
2436 return (it.rinfo()->pc() ==
2437 addr - Assembler::kPatchReturnSequenceAddressOffset);
2438 }
2439 it.next();
2440 }
2441 return false;
2442}
2443
2444
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00002445void Debug::FramesHaveBeenDropped(StackFrame::Id new_break_frame_id,
whesse@chromium.orge90029b2010-08-02 11:52:17 +00002446 FrameDropMode mode,
2447 Object** restarter_frame_function_pointer) {
ulan@chromium.orgd9e468a2012-06-25 09:47:40 +00002448 if (mode != CURRENTLY_SET_MODE) {
2449 thread_local_.frame_drop_mode_ = mode;
2450 }
ager@chromium.org357bf652010-04-12 11:30:10 +00002451 thread_local_.break_frame_id_ = new_break_frame_id;
whesse@chromium.orge90029b2010-08-02 11:52:17 +00002452 thread_local_.restarter_frame_function_pointer_ =
2453 restarter_frame_function_pointer;
ager@chromium.org357bf652010-04-12 11:30:10 +00002454}
2455
2456
jkummerow@chromium.org212d9642012-05-11 15:02:09 +00002457const int Debug::FramePaddingLayout::kInitialSize = 1;
2458
2459
2460// Any even value bigger than kInitialSize as needed for stack scanning.
2461const int Debug::FramePaddingLayout::kPaddingValue = kInitialSize + 1;
2462
2463
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002464bool Debug::IsDebugGlobal(GlobalObject* global) {
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00002465 return IsLoaded() && global == debug_context()->global_object();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002466}
2467
2468
ager@chromium.org32912102009-01-16 10:38:43 +00002469void Debug::ClearMirrorCache() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002470 PostponeInterruptsScope postpone(isolate_);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002471 HandleScope scope(isolate_);
2472 ASSERT(isolate_->context() == *Debug::debug_context());
ager@chromium.org32912102009-01-16 10:38:43 +00002473
2474 // Clear the mirror cache.
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00002475 Handle<String> function_name = isolate_->factory()->InternalizeOneByteString(
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00002476 STATIC_ASCII_VECTOR("ClearMirrorCache"));
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00002477 Handle<Object> fun(
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00002478 isolate_->global_object()->GetPropertyNoExceptionThrown(*function_name),
2479 isolate_);
ager@chromium.org32912102009-01-16 10:38:43 +00002480 ASSERT(fun->IsJSFunction());
2481 bool caught_exception;
rossberg@chromium.org717967f2011-07-20 13:44:42 +00002482 Execution::TryCall(Handle<JSFunction>::cast(fun),
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00002483 Handle<JSObject>(Debug::debug_context()->global_object()),
ager@chromium.org32912102009-01-16 10:38:43 +00002484 0, NULL, &caught_exception);
2485}
2486
2487
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002488void Debug::CreateScriptCache() {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002489 Heap* heap = isolate_->heap();
2490 HandleScope scope(isolate_);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002491
2492 // Perform two GCs to get rid of all unreferenced scripts. The first GC gets
2493 // rid of all the cached script wrappers and the second gets rid of the
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002494 // scripts which are no longer referenced. The second also sweeps precisely,
2495 // which saves us doing yet another GC to make the heap iterable.
rossberg@chromium.org994edf62012-02-06 10:12:55 +00002496 heap->CollectAllGarbage(Heap::kNoGCFlags, "Debug::CreateScriptCache");
2497 heap->CollectAllGarbage(Heap::kMakeHeapIterableMask,
2498 "Debug::CreateScriptCache");
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002499
2500 ASSERT(script_cache_ == NULL);
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +00002501 script_cache_ = new ScriptCache(isolate_);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002502
2503 // Scan heap for Script objects.
2504 int count = 0;
hpayer@chromium.org7c3372b2013-02-13 17:26:04 +00002505 HeapIterator iterator(heap);
rossberg@chromium.org79e79022013-06-03 15:43:46 +00002506 DisallowHeapAllocation no_allocation;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002507
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002508 for (HeapObject* obj = iterator.next(); obj != NULL; obj = iterator.next()) {
sgjesse@chromium.org152a0b02009-10-07 13:50:16 +00002509 if (obj->IsScript() && Script::cast(obj)->HasValidSource()) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002510 script_cache_->Add(Handle<Script>(Script::cast(obj)));
2511 count++;
2512 }
2513 }
2514}
2515
2516
2517void Debug::DestroyScriptCache() {
2518 // Get rid of the script cache if it was created.
2519 if (script_cache_ != NULL) {
2520 delete script_cache_;
2521 script_cache_ = NULL;
2522 }
2523}
2524
2525
2526void Debug::AddScriptToScriptCache(Handle<Script> script) {
2527 if (script_cache_ != NULL) {
2528 script_cache_->Add(script);
2529 }
2530}
2531
2532
2533Handle<FixedArray> Debug::GetLoadedScripts() {
2534 // Create and fill the script cache when the loaded scripts is requested for
2535 // the first time.
2536 if (script_cache_ == NULL) {
2537 CreateScriptCache();
2538 }
2539
2540 // If the script cache is not active just return an empty array.
2541 ASSERT(script_cache_ != NULL);
2542 if (script_cache_ == NULL) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002543 isolate_->factory()->NewFixedArray(0);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002544 }
2545
2546 // Perform GC to get unreferenced scripts evicted from the cache before
2547 // returning the content.
rossberg@chromium.org994edf62012-02-06 10:12:55 +00002548 isolate_->heap()->CollectAllGarbage(Heap::kNoGCFlags,
2549 "Debug::GetLoadedScripts");
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002550
2551 // Get the scripts from the cache.
2552 return script_cache_->GetScripts();
2553}
2554
2555
yangguo@chromium.org49546742013-12-23 16:17:49 +00002556void Debug::RecordEvalCaller(Handle<Script> script) {
2557 script->set_compilation_type(Script::COMPILATION_TYPE_EVAL);
2558 // For eval scripts add information on the function from which eval was
2559 // called.
2560 StackTraceFrameIterator it(script->GetIsolate());
2561 if (!it.done()) {
2562 script->set_eval_from_shared(it.frame()->function()->shared());
2563 Code* code = it.frame()->LookupCode();
2564 int offset = static_cast<int>(
2565 it.frame()->pc() - code->instruction_start());
2566 script->set_eval_from_instructions_offset(Smi::FromInt(offset));
2567 }
2568}
2569
2570
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002571void Debug::AfterGarbageCollection() {
2572 // Generate events for collected scripts.
2573 if (script_cache_ != NULL) {
2574 script_cache_->ProcessCollectedScripts();
2575 }
2576}
2577
2578
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00002579Debugger::Debugger(Isolate* isolate)
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +00002580 : debugger_access_(isolate->debugger_access()),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002581 event_listener_(Handle<Object>()),
2582 event_listener_data_(Handle<Object>()),
2583 compiling_natives_(false),
2584 is_loading_debugger_(false),
mstarzinger@chromium.orgde886792012-09-11 13:22:37 +00002585 live_edit_enabled_(true),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002586 never_unload_debugger_(false),
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002587 force_debugger_active_(false),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002588 message_handler_(NULL),
2589 debugger_unload_pending_(false),
2590 host_dispatch_handler_(NULL),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002591 debug_message_dispatch_handler_(NULL),
2592 message_dispatch_helper_thread_(NULL),
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +00002593 host_dispatch_period_(TimeDelta::FromMilliseconds(100)),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002594 agent_(NULL),
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00002595 command_queue_(isolate->logger(), kQueueInitialSize),
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +00002596 command_received_(0),
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00002597 event_command_queue_(isolate->logger(), kQueueInitialSize),
2598 isolate_(isolate) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002599}
2600
2601
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +00002602Debugger::~Debugger() {}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002603
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002604
2605Handle<Object> Debugger::MakeJSObject(Vector<const char> constructor_name,
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002606 int argc,
2607 Handle<Object> argv[],
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002608 bool* caught_exception) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002609 ASSERT(isolate_->context() == *isolate_->debug()->debug_context());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002610
2611 // Create the execution state object.
lrn@chromium.org7516f052011-03-30 08:52:27 +00002612 Handle<String> constructor_str =
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00002613 isolate_->factory()->InternalizeUtf8String(constructor_name);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002614 Handle<Object> constructor(
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00002615 isolate_->global_object()->GetPropertyNoExceptionThrown(*constructor_str),
2616 isolate_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002617 ASSERT(constructor->IsJSFunction());
2618 if (!constructor->IsJSFunction()) {
2619 *caught_exception = true;
lrn@chromium.org7516f052011-03-30 08:52:27 +00002620 return isolate_->factory()->undefined_value();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002621 }
2622 Handle<Object> js_object = Execution::TryCall(
2623 Handle<JSFunction>::cast(constructor),
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00002624 Handle<JSObject>(isolate_->debug()->debug_context()->global_object()),
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002625 argc,
2626 argv,
2627 caught_exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002628 return js_object;
2629}
2630
2631
2632Handle<Object> Debugger::MakeExecutionState(bool* caught_exception) {
2633 // Create the execution state object.
lrn@chromium.org7516f052011-03-30 08:52:27 +00002634 Handle<Object> break_id = isolate_->factory()->NewNumberFromInt(
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002635 isolate_->debug()->break_id());
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002636 Handle<Object> argv[] = { break_id };
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002637 return MakeJSObject(CStrVector("MakeExecutionState"),
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002638 ARRAY_SIZE(argv),
2639 argv,
2640 caught_exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002641}
2642
2643
2644Handle<Object> Debugger::MakeBreakEvent(Handle<Object> exec_state,
2645 Handle<Object> break_points_hit,
2646 bool* caught_exception) {
2647 // Create the new break event object.
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002648 Handle<Object> argv[] = { exec_state, break_points_hit };
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002649 return MakeJSObject(CStrVector("MakeBreakEvent"),
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002650 ARRAY_SIZE(argv),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002651 argv,
2652 caught_exception);
2653}
2654
2655
2656Handle<Object> Debugger::MakeExceptionEvent(Handle<Object> exec_state,
2657 Handle<Object> exception,
2658 bool uncaught,
2659 bool* caught_exception) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002660 Factory* factory = isolate_->factory();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002661 // Create the new exception event object.
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002662 Handle<Object> argv[] = { exec_state,
2663 exception,
2664 factory->ToBoolean(uncaught) };
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002665 return MakeJSObject(CStrVector("MakeExceptionEvent"),
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002666 ARRAY_SIZE(argv),
2667 argv,
2668 caught_exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002669}
2670
2671
2672Handle<Object> Debugger::MakeNewFunctionEvent(Handle<Object> function,
2673 bool* caught_exception) {
2674 // Create the new function event object.
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002675 Handle<Object> argv[] = { function };
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002676 return MakeJSObject(CStrVector("MakeNewFunctionEvent"),
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002677 ARRAY_SIZE(argv),
2678 argv,
2679 caught_exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002680}
2681
2682
2683Handle<Object> Debugger::MakeCompileEvent(Handle<Script> script,
iposva@chromium.org245aa852009-02-10 00:49:54 +00002684 bool before,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002685 bool* caught_exception) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002686 Factory* factory = isolate_->factory();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002687 // Create the compile event object.
2688 Handle<Object> exec_state = MakeExecutionState(caught_exception);
iposva@chromium.org245aa852009-02-10 00:49:54 +00002689 Handle<Object> script_wrapper = GetScriptWrapper(script);
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002690 Handle<Object> argv[] = { exec_state,
2691 script_wrapper,
2692 factory->ToBoolean(before) };
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002693 return MakeJSObject(CStrVector("MakeCompileEvent"),
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002694 ARRAY_SIZE(argv),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002695 argv,
2696 caught_exception);
2697}
2698
2699
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002700Handle<Object> Debugger::MakeScriptCollectedEvent(int id,
2701 bool* caught_exception) {
2702 // Create the script collected event object.
2703 Handle<Object> exec_state = MakeExecutionState(caught_exception);
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00002704 Handle<Object> id_object = Handle<Smi>(Smi::FromInt(id), isolate_);
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002705 Handle<Object> argv[] = { exec_state, id_object };
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002706
2707 return MakeJSObject(CStrVector("MakeScriptCollectedEvent"),
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002708 ARRAY_SIZE(argv),
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002709 argv,
2710 caught_exception);
2711}
2712
2713
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002714void Debugger::OnException(Handle<Object> exception, bool uncaught) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002715 HandleScope scope(isolate_);
2716 Debug* debug = isolate_->debug();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002717
2718 // Bail out based on state or if there is no listener for this event
lrn@chromium.org7516f052011-03-30 08:52:27 +00002719 if (debug->InDebugger()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002720 if (!Debugger::EventActive(v8::Exception)) return;
2721
2722 // Bail out if exception breaks are not active
2723 if (uncaught) {
2724 // Uncaught exceptions are reported by either flags.
lrn@chromium.org7516f052011-03-30 08:52:27 +00002725 if (!(debug->break_on_uncaught_exception() ||
2726 debug->break_on_exception())) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002727 } else {
2728 // Caught exceptions are reported is activated.
lrn@chromium.org7516f052011-03-30 08:52:27 +00002729 if (!debug->break_on_exception()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002730 }
2731
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002732 // Enter the debugger.
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +00002733 EnterDebugger debugger(isolate_);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002734 if (debugger.FailedToEnter()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002735
2736 // Clear all current stepping setup.
lrn@chromium.org7516f052011-03-30 08:52:27 +00002737 debug->ClearStepping();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002738 // Create the event data object.
2739 bool caught_exception = false;
2740 Handle<Object> exec_state = MakeExecutionState(&caught_exception);
2741 Handle<Object> event_data;
2742 if (!caught_exception) {
2743 event_data = MakeExceptionEvent(exec_state, exception, uncaught,
2744 &caught_exception);
2745 }
2746 // Bail out and don't call debugger if exception.
2747 if (caught_exception) {
2748 return;
2749 }
2750
ager@chromium.org5ec48922009-05-05 07:25:34 +00002751 // Process debug event.
2752 ProcessDebugEvent(v8::Exception, Handle<JSObject>::cast(event_data), false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002753 // Return to continue execution from where the exception was thrown.
2754}
2755
2756
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002757void Debugger::OnDebugBreak(Handle<Object> break_points_hit,
2758 bool auto_continue) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002759 HandleScope scope(isolate_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002760
kasper.lund212ac232008-07-16 07:07:30 +00002761 // Debugger has already been entered by caller.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002762 ASSERT(isolate_->context() == *isolate_->debug()->debug_context());
kasper.lund212ac232008-07-16 07:07:30 +00002763
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002764 // Bail out if there is no listener for this event
2765 if (!Debugger::EventActive(v8::Break)) return;
2766
2767 // Debugger must be entered in advance.
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00002768 ASSERT(isolate_->context() == *isolate_->debug()->debug_context());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002769
2770 // Create the event data object.
2771 bool caught_exception = false;
2772 Handle<Object> exec_state = MakeExecutionState(&caught_exception);
2773 Handle<Object> event_data;
2774 if (!caught_exception) {
2775 event_data = MakeBreakEvent(exec_state, break_points_hit,
2776 &caught_exception);
2777 }
2778 // Bail out and don't call debugger if exception.
2779 if (caught_exception) {
2780 return;
2781 }
2782
ager@chromium.org5ec48922009-05-05 07:25:34 +00002783 // Process debug event.
2784 ProcessDebugEvent(v8::Break,
2785 Handle<JSObject>::cast(event_data),
2786 auto_continue);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002787}
2788
2789
2790void Debugger::OnBeforeCompile(Handle<Script> script) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002791 HandleScope scope(isolate_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002792
2793 // Bail out based on state or if there is no listener for this event
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002794 if (isolate_->debug()->InDebugger()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002795 if (compiling_natives()) return;
2796 if (!EventActive(v8::BeforeCompile)) return;
2797
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002798 // Enter the debugger.
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +00002799 EnterDebugger debugger(isolate_);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002800 if (debugger.FailedToEnter()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002801
2802 // Create the event data object.
2803 bool caught_exception = false;
iposva@chromium.org245aa852009-02-10 00:49:54 +00002804 Handle<Object> event_data = MakeCompileEvent(script, true, &caught_exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002805 // Bail out and don't call debugger if exception.
2806 if (caught_exception) {
2807 return;
2808 }
2809
ager@chromium.org5ec48922009-05-05 07:25:34 +00002810 // Process debug event.
2811 ProcessDebugEvent(v8::BeforeCompile,
2812 Handle<JSObject>::cast(event_data),
2813 true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002814}
2815
2816
2817// Handle debugger actions when a new script is compiled.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00002818void Debugger::OnAfterCompile(Handle<Script> script,
2819 AfterCompileFlags after_compile_flags) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002820 HandleScope scope(isolate_);
2821 Debug* debug = isolate_->debug();
kasper.lund212ac232008-07-16 07:07:30 +00002822
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002823 // Add the newly compiled script to the script cache.
lrn@chromium.org7516f052011-03-30 08:52:27 +00002824 debug->AddScriptToScriptCache(script);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002825
2826 // No more to do if not debugging.
ager@chromium.org71daaf62009-04-01 07:22:49 +00002827 if (!IsDebuggerActive()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002828
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002829 // No compile events while compiling natives.
2830 if (compiling_natives()) return;
2831
iposva@chromium.org245aa852009-02-10 00:49:54 +00002832 // Store whether in debugger before entering debugger.
lrn@chromium.org7516f052011-03-30 08:52:27 +00002833 bool in_debugger = debug->InDebugger();
iposva@chromium.org245aa852009-02-10 00:49:54 +00002834
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002835 // Enter the debugger.
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +00002836 EnterDebugger debugger(isolate_);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002837 if (debugger.FailedToEnter()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002838
2839 // If debugging there might be script break points registered for this
2840 // script. Make sure that these break points are set.
2841
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002842 // Get the function UpdateScriptBreakPoints (defined in debug-debugger.js).
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00002843 Handle<String> update_script_break_points_string =
2844 isolate_->factory()->InternalizeOneByteString(
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00002845 STATIC_ASCII_VECTOR("UpdateScriptBreakPoints"));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002846 Handle<Object> update_script_break_points =
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00002847 Handle<Object>(
2848 debug->debug_context()->global_object()->GetPropertyNoExceptionThrown(
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00002849 *update_script_break_points_string),
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00002850 isolate_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002851 if (!update_script_break_points->IsJSFunction()) {
2852 return;
2853 }
2854 ASSERT(update_script_break_points->IsJSFunction());
2855
2856 // Wrap the script object in a proper JS object before passing it
2857 // to JavaScript.
2858 Handle<JSValue> wrapper = GetScriptWrapper(script);
2859
2860 // Call UpdateScriptBreakPoints expect no exceptions.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002861 bool caught_exception;
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002862 Handle<Object> argv[] = { wrapper };
rossberg@chromium.org717967f2011-07-20 13:44:42 +00002863 Execution::TryCall(Handle<JSFunction>::cast(update_script_break_points),
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +00002864 isolate_->js_builtins_object(),
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002865 ARRAY_SIZE(argv),
2866 argv,
2867 &caught_exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002868 if (caught_exception) {
2869 return;
2870 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002871 // Bail out based on state or if there is no listener for this event
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00002872 if (in_debugger && (after_compile_flags & SEND_WHEN_DEBUGGING) == 0) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002873 if (!Debugger::EventActive(v8::AfterCompile)) return;
2874
2875 // Create the compile state object.
2876 Handle<Object> event_data = MakeCompileEvent(script,
iposva@chromium.org245aa852009-02-10 00:49:54 +00002877 false,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002878 &caught_exception);
2879 // Bail out and don't call debugger if exception.
2880 if (caught_exception) {
2881 return;
2882 }
ager@chromium.org5ec48922009-05-05 07:25:34 +00002883 // Process debug event.
2884 ProcessDebugEvent(v8::AfterCompile,
2885 Handle<JSObject>::cast(event_data),
2886 true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002887}
2888
2889
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002890void Debugger::OnScriptCollected(int id) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002891 HandleScope scope(isolate_);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002892
2893 // No more to do if not debugging.
ulan@chromium.org56c14af2012-09-20 12:51:09 +00002894 if (isolate_->debug()->InDebugger()) return;
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002895 if (!IsDebuggerActive()) return;
2896 if (!Debugger::EventActive(v8::ScriptCollected)) return;
2897
2898 // Enter the debugger.
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +00002899 EnterDebugger debugger(isolate_);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002900 if (debugger.FailedToEnter()) return;
2901
2902 // Create the script collected state object.
2903 bool caught_exception = false;
2904 Handle<Object> event_data = MakeScriptCollectedEvent(id,
2905 &caught_exception);
2906 // Bail out and don't call debugger if exception.
2907 if (caught_exception) {
2908 return;
2909 }
2910
2911 // Process debug event.
2912 ProcessDebugEvent(v8::ScriptCollected,
2913 Handle<JSObject>::cast(event_data),
2914 true);
2915}
2916
2917
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002918void Debugger::ProcessDebugEvent(v8::DebugEvent event,
ager@chromium.org5ec48922009-05-05 07:25:34 +00002919 Handle<JSObject> event_data,
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002920 bool auto_continue) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002921 HandleScope scope(isolate_);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002922
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +00002923 // Clear any pending debug break if this is a real break.
2924 if (!auto_continue) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002925 isolate_->debug()->clear_interrupt_pending(DEBUGBREAK);
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +00002926 }
2927
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002928 // Create the execution state.
2929 bool caught_exception = false;
2930 Handle<Object> exec_state = MakeExecutionState(&caught_exception);
2931 if (caught_exception) {
2932 return;
2933 }
ager@chromium.org41826e72009-03-30 13:30:57 +00002934 // First notify the message handler if any.
2935 if (message_handler_ != NULL) {
ager@chromium.org5ec48922009-05-05 07:25:34 +00002936 NotifyMessageHandler(event,
2937 Handle<JSObject>::cast(exec_state),
2938 event_data,
2939 auto_continue);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002940 }
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00002941 // Notify registered debug event listener. This can be either a C or
2942 // a JavaScript function. Don't call event listener for v8::Break
2943 // here, if it's only a debug command -- they will be processed later.
2944 if ((event != v8::Break || !auto_continue) && !event_listener_.is_null()) {
2945 CallEventCallback(event, exec_state, event_data, NULL);
2946 }
2947 // Process pending debug commands.
2948 if (event == v8::Break) {
2949 while (!event_command_queue_.IsEmpty()) {
2950 CommandMessage command = event_command_queue_.Get();
2951 if (!event_listener_.is_null()) {
2952 CallEventCallback(v8::BreakForCommand,
2953 exec_state,
2954 event_data,
2955 command.client_data());
2956 }
2957 command.Dispose();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002958 }
2959 }
2960}
2961
2962
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00002963void Debugger::CallEventCallback(v8::DebugEvent event,
2964 Handle<Object> exec_state,
2965 Handle<Object> event_data,
2966 v8::Debug::ClientData* client_data) {
ager@chromium.orgea91cc52011-05-23 06:06:11 +00002967 if (event_listener_->IsForeign()) {
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00002968 CallCEventCallback(event, exec_state, event_data, client_data);
2969 } else {
2970 CallJSEventCallback(event, exec_state, event_data);
2971 }
2972}
2973
2974
2975void Debugger::CallCEventCallback(v8::DebugEvent event,
2976 Handle<Object> exec_state,
2977 Handle<Object> event_data,
2978 v8::Debug::ClientData* client_data) {
ager@chromium.orgea91cc52011-05-23 06:06:11 +00002979 Handle<Foreign> callback_obj(Handle<Foreign>::cast(event_listener_));
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00002980 v8::Debug::EventCallback2 callback =
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002981 FUNCTION_CAST<v8::Debug::EventCallback2>(
2982 callback_obj->foreign_address());
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00002983 EventDetailsImpl event_details(
2984 event,
2985 Handle<JSObject>::cast(exec_state),
2986 Handle<JSObject>::cast(event_data),
2987 event_listener_data_,
2988 client_data);
2989 callback(event_details);
2990}
2991
2992
2993void Debugger::CallJSEventCallback(v8::DebugEvent event,
2994 Handle<Object> exec_state,
2995 Handle<Object> event_data) {
2996 ASSERT(event_listener_->IsJSFunction());
2997 Handle<JSFunction> fun(Handle<JSFunction>::cast(event_listener_));
2998
2999 // Invoke the JavaScript debug event listener.
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00003000 Handle<Object> argv[] = { Handle<Object>(Smi::FromInt(event), isolate_),
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00003001 exec_state,
3002 event_data,
3003 event_listener_data_ };
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003004 bool caught_exception;
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00003005 Execution::TryCall(fun,
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00003006 isolate_->global_object(),
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00003007 ARRAY_SIZE(argv),
3008 argv,
3009 &caught_exception);
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00003010 // Silently ignore exceptions from debug event listeners.
3011}
3012
3013
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00003014Handle<Context> Debugger::GetDebugContext() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003015 never_unload_debugger_ = true;
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +00003016 EnterDebugger debugger(isolate_);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003017 return isolate_->debug()->debug_context();
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00003018}
3019
3020
ager@chromium.org71daaf62009-04-01 07:22:49 +00003021void Debugger::UnloadDebugger() {
lrn@chromium.org7516f052011-03-30 08:52:27 +00003022 Debug* debug = isolate_->debug();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003023
ager@chromium.org71daaf62009-04-01 07:22:49 +00003024 // Make sure that there are no breakpoints left.
lrn@chromium.org7516f052011-03-30 08:52:27 +00003025 debug->ClearAllBreakPoints();
ager@chromium.org71daaf62009-04-01 07:22:49 +00003026
3027 // Unload the debugger if feasible.
3028 if (!never_unload_debugger_) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00003029 debug->Unload();
ager@chromium.org71daaf62009-04-01 07:22:49 +00003030 }
3031
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003032 // Clear the flag indicating that the debugger should be unloaded.
3033 debugger_unload_pending_ = false;
ager@chromium.org71daaf62009-04-01 07:22:49 +00003034}
3035
3036
ager@chromium.org41826e72009-03-30 13:30:57 +00003037void Debugger::NotifyMessageHandler(v8::DebugEvent event,
ager@chromium.org5ec48922009-05-05 07:25:34 +00003038 Handle<JSObject> exec_state,
3039 Handle<JSObject> event_data,
ager@chromium.org41826e72009-03-30 13:30:57 +00003040 bool auto_continue) {
machenbach@chromium.orgf9841892013-11-25 12:01:13 +00003041 v8::Isolate* isolate = reinterpret_cast<v8::Isolate*>(isolate_);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003042 HandleScope scope(isolate_);
ager@chromium.org41826e72009-03-30 13:30:57 +00003043
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003044 if (!isolate_->debug()->Load()) return;
ager@chromium.org41826e72009-03-30 13:30:57 +00003045
3046 // Process the individual events.
ager@chromium.org5ec48922009-05-05 07:25:34 +00003047 bool sendEventMessage = false;
ager@chromium.org41826e72009-03-30 13:30:57 +00003048 switch (event) {
3049 case v8::Break:
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00003050 case v8::BreakForCommand:
ager@chromium.org5ec48922009-05-05 07:25:34 +00003051 sendEventMessage = !auto_continue;
ager@chromium.org41826e72009-03-30 13:30:57 +00003052 break;
3053 case v8::Exception:
ager@chromium.org5ec48922009-05-05 07:25:34 +00003054 sendEventMessage = true;
ager@chromium.org41826e72009-03-30 13:30:57 +00003055 break;
3056 case v8::BeforeCompile:
3057 break;
3058 case v8::AfterCompile:
ager@chromium.org5ec48922009-05-05 07:25:34 +00003059 sendEventMessage = true;
ager@chromium.org41826e72009-03-30 13:30:57 +00003060 break;
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003061 case v8::ScriptCollected:
3062 sendEventMessage = true;
3063 break;
ager@chromium.org41826e72009-03-30 13:30:57 +00003064 case v8::NewFunction:
3065 break;
3066 default:
3067 UNREACHABLE();
3068 }
3069
ager@chromium.org5ec48922009-05-05 07:25:34 +00003070 // The debug command interrupt flag might have been set when the command was
3071 // added. It should be enough to clear the flag only once while we are in the
3072 // debugger.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003073 ASSERT(isolate_->debug()->InDebugger());
3074 isolate_->stack_guard()->Continue(DEBUGCOMMAND);
ager@chromium.org5ec48922009-05-05 07:25:34 +00003075
3076 // Notify the debugger that a debug event has occurred unless auto continue is
3077 // active in which case no event is send.
3078 if (sendEventMessage) {
3079 MessageImpl message = MessageImpl::NewEvent(
3080 event,
3081 auto_continue,
3082 Handle<JSObject>::cast(exec_state),
3083 Handle<JSObject>::cast(event_data));
3084 InvokeMessageHandler(message);
3085 }
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +00003086
3087 // If auto continue don't make the event cause a break, but process messages
3088 // in the queue if any. For script collected events don't even process
3089 // messages in the queue as the execution state might not be what is expected
3090 // by the client.
ager@chromium.org6ffc2172009-05-29 19:20:16 +00003091 if ((auto_continue && !HasCommands()) || event == v8::ScriptCollected) {
ager@chromium.org5ec48922009-05-05 07:25:34 +00003092 return;
3093 }
ager@chromium.org41826e72009-03-30 13:30:57 +00003094
ager@chromium.org41826e72009-03-30 13:30:57 +00003095 v8::TryCatch try_catch;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003096
3097 // DebugCommandProcessor goes here.
3098 v8::Local<v8::Object> cmd_processor;
3099 {
3100 v8::Local<v8::Object> api_exec_state =
3101 v8::Utils::ToLocal(Handle<JSObject>::cast(exec_state));
machenbach@chromium.orgf9841892013-11-25 12:01:13 +00003102 v8::Local<v8::String> fun_name = v8::String::NewFromUtf8(
3103 isolate, "debugCommandProcessor");
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003104 v8::Local<v8::Function> fun =
danno@chromium.orgf95d4b92013-06-13 14:40:17 +00003105 v8::Local<v8::Function>::Cast(api_exec_state->Get(fun_name));
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003106
machenbach@chromium.org37be4082013-11-26 13:50:38 +00003107 v8::Handle<v8::Boolean> running = v8::Boolean::New(isolate, auto_continue);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003108 static const int kArgc = 1;
3109 v8::Handle<Value> argv[kArgc] = { running };
danno@chromium.orgf95d4b92013-06-13 14:40:17 +00003110 cmd_processor = v8::Local<v8::Object>::Cast(
3111 fun->Call(api_exec_state, kArgc, argv));
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003112 if (try_catch.HasCaught()) {
3113 PrintLn(try_catch.Exception());
3114 return;
3115 }
ager@chromium.org41826e72009-03-30 13:30:57 +00003116 }
3117
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003118 bool running = auto_continue;
3119
ager@chromium.org41826e72009-03-30 13:30:57 +00003120 // Process requests from the debugger.
3121 while (true) {
3122 // Wait for new command in the queue.
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003123 if (Debugger::host_dispatch_handler_) {
3124 // In case there is a host dispatch - do periodic dispatches.
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +00003125 if (!command_received_.WaitFor(host_dispatch_period_)) {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003126 // Timout expired, do the dispatch.
3127 Debugger::host_dispatch_handler_();
3128 continue;
3129 }
3130 } else {
3131 // In case there is no host dispatch - just wait.
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +00003132 command_received_.Wait();
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003133 }
ager@chromium.org41826e72009-03-30 13:30:57 +00003134
ager@chromium.org41826e72009-03-30 13:30:57 +00003135 // Get the command from the queue.
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003136 CommandMessage command = command_queue_.Get();
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00003137 isolate_->logger()->DebugTag(
3138 "Got request from command queue, in interactive loop.");
ager@chromium.org71daaf62009-04-01 07:22:49 +00003139 if (!Debugger::IsDebuggerActive()) {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003140 // Delete command text and user data.
3141 command.Dispose();
ager@chromium.org41826e72009-03-30 13:30:57 +00003142 return;
3143 }
3144
ager@chromium.org41826e72009-03-30 13:30:57 +00003145 // Invoke JavaScript to process the debug request.
3146 v8::Local<v8::String> fun_name;
3147 v8::Local<v8::Function> fun;
3148 v8::Local<v8::Value> request;
3149 v8::TryCatch try_catch;
machenbach@chromium.orgf9841892013-11-25 12:01:13 +00003150 fun_name = v8::String::NewFromUtf8(isolate, "processDebugRequest");
danno@chromium.orgf95d4b92013-06-13 14:40:17 +00003151 fun = v8::Local<v8::Function>::Cast(cmd_processor->Get(fun_name));
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003152
machenbach@chromium.orgf9841892013-11-25 12:01:13 +00003153 request = v8::String::NewFromTwoByte(isolate, command.text().start(),
3154 v8::String::kNormalString,
3155 command.text().length());
ager@chromium.org41826e72009-03-30 13:30:57 +00003156 static const int kArgc = 1;
3157 v8::Handle<Value> argv[kArgc] = { request };
3158 v8::Local<v8::Value> response_val = fun->Call(cmd_processor, kArgc, argv);
3159
3160 // Get the response.
3161 v8::Local<v8::String> response;
ager@chromium.org41826e72009-03-30 13:30:57 +00003162 if (!try_catch.HasCaught()) {
3163 // Get response string.
3164 if (!response_val->IsUndefined()) {
danno@chromium.orgf95d4b92013-06-13 14:40:17 +00003165 response = v8::Local<v8::String>::Cast(response_val);
ager@chromium.org41826e72009-03-30 13:30:57 +00003166 } else {
machenbach@chromium.orgf9841892013-11-25 12:01:13 +00003167 response = v8::String::NewFromUtf8(isolate, "");
ager@chromium.org41826e72009-03-30 13:30:57 +00003168 }
3169
3170 // Log the JSON request/response.
3171 if (FLAG_trace_debug_json) {
3172 PrintLn(request);
3173 PrintLn(response);
3174 }
3175
3176 // Get the running state.
machenbach@chromium.orgf9841892013-11-25 12:01:13 +00003177 fun_name = v8::String::NewFromUtf8(isolate, "isRunning");
danno@chromium.orgf95d4b92013-06-13 14:40:17 +00003178 fun = v8::Local<v8::Function>::Cast(cmd_processor->Get(fun_name));
ager@chromium.org41826e72009-03-30 13:30:57 +00003179 static const int kArgc = 1;
3180 v8::Handle<Value> argv[kArgc] = { response };
3181 v8::Local<v8::Value> running_val = fun->Call(cmd_processor, kArgc, argv);
3182 if (!try_catch.HasCaught()) {
3183 running = running_val->ToBoolean()->Value();
3184 }
3185 } else {
3186 // In case of failure the result text is the exception text.
3187 response = try_catch.Exception()->ToString();
3188 }
3189
ager@chromium.org41826e72009-03-30 13:30:57 +00003190 // Return the result.
ager@chromium.org5ec48922009-05-05 07:25:34 +00003191 MessageImpl message = MessageImpl::NewResponse(
3192 event,
3193 running,
3194 Handle<JSObject>::cast(exec_state),
3195 Handle<JSObject>::cast(event_data),
3196 Handle<String>(Utils::OpenHandle(*response)),
3197 command.client_data());
3198 InvokeMessageHandler(message);
3199 command.Dispose();
ager@chromium.org41826e72009-03-30 13:30:57 +00003200
3201 // Return from debug event processing if either the VM is put into the
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00003202 // running state (through a continue command) or auto continue is active
ager@chromium.org41826e72009-03-30 13:30:57 +00003203 // and there are no more commands queued.
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003204 if (running && !HasCommands()) {
ager@chromium.org41826e72009-03-30 13:30:57 +00003205 return;
3206 }
3207 }
3208}
3209
3210
iposva@chromium.org245aa852009-02-10 00:49:54 +00003211void Debugger::SetEventListener(Handle<Object> callback,
3212 Handle<Object> data) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00003213 HandleScope scope(isolate_);
3214 GlobalHandles* global_handles = isolate_->global_handles();
iposva@chromium.org245aa852009-02-10 00:49:54 +00003215
3216 // Clear the global handles for the event listener and the event listener data
3217 // object.
3218 if (!event_listener_.is_null()) {
hpayer@chromium.org4f99be92013-12-18 16:23:55 +00003219 GlobalHandles::Destroy(
iposva@chromium.org245aa852009-02-10 00:49:54 +00003220 reinterpret_cast<Object**>(event_listener_.location()));
3221 event_listener_ = Handle<Object>();
3222 }
3223 if (!event_listener_data_.is_null()) {
hpayer@chromium.org4f99be92013-12-18 16:23:55 +00003224 GlobalHandles::Destroy(
iposva@chromium.org245aa852009-02-10 00:49:54 +00003225 reinterpret_cast<Object**>(event_listener_data_.location()));
3226 event_listener_data_ = Handle<Object>();
3227 }
3228
3229 // If there is a new debug event listener register it together with its data
3230 // object.
3231 if (!callback->IsUndefined() && !callback->IsNull()) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003232 event_listener_ = Handle<Object>::cast(
lrn@chromium.org7516f052011-03-30 08:52:27 +00003233 global_handles->Create(*callback));
iposva@chromium.org245aa852009-02-10 00:49:54 +00003234 if (data.is_null()) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00003235 data = isolate_->factory()->undefined_value();
iposva@chromium.org245aa852009-02-10 00:49:54 +00003236 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003237 event_listener_data_ = Handle<Object>::cast(
lrn@chromium.org7516f052011-03-30 08:52:27 +00003238 global_handles->Create(*data));
iposva@chromium.org245aa852009-02-10 00:49:54 +00003239 }
3240
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003241 ListenersChanged();
iposva@chromium.org245aa852009-02-10 00:49:54 +00003242}
3243
3244
ager@chromium.org5ec48922009-05-05 07:25:34 +00003245void Debugger::SetMessageHandler(v8::Debug::MessageHandler2 handler) {
jkummerow@chromium.orgdc94e192013-08-30 11:35:42 +00003246 LockGuard<RecursiveMutex> with(debugger_access_);
ager@chromium.org71daaf62009-04-01 07:22:49 +00003247
ager@chromium.org381abbb2009-02-25 13:23:22 +00003248 message_handler_ = handler;
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003249 ListenersChanged();
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003250 if (handler == NULL) {
ager@chromium.org71daaf62009-04-01 07:22:49 +00003251 // Send an empty command to the debugger if in a break to make JavaScript
3252 // run again if the debugger is closed.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003253 if (isolate_->debug()->InDebugger()) {
ager@chromium.org71daaf62009-04-01 07:22:49 +00003254 ProcessCommand(Vector<const uint16_t>::empty());
3255 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003256 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003257}
3258
3259
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003260void Debugger::ListenersChanged() {
3261 if (IsDebuggerActive()) {
3262 // Disable the compilation cache when the debugger is active.
lrn@chromium.org7516f052011-03-30 08:52:27 +00003263 isolate_->compilation_cache()->Disable();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003264 debugger_unload_pending_ = false;
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003265 } else {
lrn@chromium.org7516f052011-03-30 08:52:27 +00003266 isolate_->compilation_cache()->Enable();
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003267 // Unload the debugger if event listener and message handler cleared.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003268 // Schedule this for later, because we may be in non-V8 thread.
3269 debugger_unload_pending_ = true;
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003270 }
3271}
3272
3273
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003274void Debugger::SetHostDispatchHandler(v8::Debug::HostDispatchHandler handler,
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +00003275 TimeDelta period) {
ager@chromium.org381abbb2009-02-25 13:23:22 +00003276 host_dispatch_handler_ = handler;
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +00003277 host_dispatch_period_ = period;
ager@chromium.org381abbb2009-02-25 13:23:22 +00003278}
3279
3280
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003281void Debugger::SetDebugMessageDispatchHandler(
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003282 v8::Debug::DebugMessageDispatchHandler handler, bool provide_locker) {
jkummerow@chromium.orgdc94e192013-08-30 11:35:42 +00003283 LockGuard<Mutex> lock_guard(&dispatch_handler_access_);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003284 debug_message_dispatch_handler_ = handler;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003285
3286 if (provide_locker && message_dispatch_helper_thread_ == NULL) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003287 message_dispatch_helper_thread_ = new MessageDispatchHelperThread(isolate_);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003288 message_dispatch_helper_thread_->Start();
3289 }
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003290}
3291
3292
ager@chromium.org41826e72009-03-30 13:30:57 +00003293// Calls the registered debug message handler. This callback is part of the
ager@chromium.org5ec48922009-05-05 07:25:34 +00003294// public API.
3295void Debugger::InvokeMessageHandler(MessageImpl message) {
jkummerow@chromium.orgdc94e192013-08-30 11:35:42 +00003296 LockGuard<RecursiveMutex> with(debugger_access_);
ager@chromium.org71daaf62009-04-01 07:22:49 +00003297
ager@chromium.org381abbb2009-02-25 13:23:22 +00003298 if (message_handler_ != NULL) {
ager@chromium.org5ec48922009-05-05 07:25:34 +00003299 message_handler_(message);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003300 }
ager@chromium.org41826e72009-03-30 13:30:57 +00003301}
3302
3303
3304// Puts a command coming from the public API on the queue. Creates
3305// a copy of the command string managed by the debugger. Up to this
3306// point, the command data was managed by the API client. Called
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003307// by the API client thread.
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003308void Debugger::ProcessCommand(Vector<const uint16_t> command,
3309 v8::Debug::ClientData* client_data) {
3310 // Need to cast away const.
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003311 CommandMessage message = CommandMessage::New(
ager@chromium.org41826e72009-03-30 13:30:57 +00003312 Vector<uint16_t>(const_cast<uint16_t*>(command.start()),
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003313 command.length()),
3314 client_data);
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00003315 isolate_->logger()->DebugTag("Put command on command_queue.");
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003316 command_queue_.Put(message);
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +00003317 command_received_.Signal();
sgjesse@chromium.org3afc1582009-04-16 22:31:44 +00003318
3319 // Set the debug command break flag to have the command processed.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003320 if (!isolate_->debug()->InDebugger()) {
3321 isolate_->stack_guard()->DebugCommand();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003322 }
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003323
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003324 MessageDispatchHelperThread* dispatch_thread;
3325 {
jkummerow@chromium.orgdc94e192013-08-30 11:35:42 +00003326 LockGuard<Mutex> lock_guard(&dispatch_handler_access_);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003327 dispatch_thread = message_dispatch_helper_thread_;
3328 }
3329
3330 if (dispatch_thread == NULL) {
3331 CallMessageDispatchHandler();
3332 } else {
3333 dispatch_thread->Schedule();
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003334 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003335}
3336
3337
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003338bool Debugger::HasCommands() {
ager@chromium.org41826e72009-03-30 13:30:57 +00003339 return !command_queue_.IsEmpty();
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003340}
3341
3342
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00003343void Debugger::EnqueueDebugCommand(v8::Debug::ClientData* client_data) {
3344 CommandMessage message = CommandMessage::New(Vector<uint16_t>(), client_data);
3345 event_command_queue_.Put(message);
3346
3347 // Set the debug command break flag to have the command processed.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003348 if (!isolate_->debug()->InDebugger()) {
3349 isolate_->stack_guard()->DebugCommand();
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00003350 }
3351}
3352
3353
ager@chromium.org71daaf62009-04-01 07:22:49 +00003354bool Debugger::IsDebuggerActive() {
jkummerow@chromium.orgdc94e192013-08-30 11:35:42 +00003355 LockGuard<RecursiveMutex> with(debugger_access_);
ager@chromium.org71daaf62009-04-01 07:22:49 +00003356
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00003357 return message_handler_ != NULL ||
3358 !event_listener_.is_null() ||
3359 force_debugger_active_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003360}
3361
3362
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003363Handle<Object> Debugger::Call(Handle<JSFunction> fun,
3364 Handle<Object> data,
3365 bool* pending_exception) {
ager@chromium.org71daaf62009-04-01 07:22:49 +00003366 // When calling functions in the debugger prevent it from beeing unloaded.
3367 Debugger::never_unload_debugger_ = true;
3368
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003369 // Enter the debugger.
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +00003370 EnterDebugger debugger(isolate_);
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +00003371 if (debugger.FailedToEnter()) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00003372 return isolate_->factory()->undefined_value();
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003373 }
3374
3375 // Create the execution state.
3376 bool caught_exception = false;
3377 Handle<Object> exec_state = MakeExecutionState(&caught_exception);
3378 if (caught_exception) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00003379 return isolate_->factory()->undefined_value();
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003380 }
3381
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00003382 Handle<Object> argv[] = { exec_state, data };
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +00003383 Handle<Object> result = Execution::Call(
jkummerow@chromium.org2c9426b2013-09-05 16:31:13 +00003384 isolate_,
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +00003385 fun,
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00003386 Handle<Object>(isolate_->debug()->debug_context_->global_proxy(),
3387 isolate_),
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00003388 ARRAY_SIZE(argv),
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +00003389 argv,
3390 pending_exception);
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003391 return result;
3392}
3393
3394
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003395static void StubMessageHandler2(const v8::Debug::Message& message) {
3396 // Simply ignore message.
3397}
3398
3399
3400bool Debugger::StartAgent(const char* name, int port,
3401 bool wait_for_connection) {
3402 if (wait_for_connection) {
3403 // Suspend V8 if it is already running or set V8 to suspend whenever
3404 // it starts.
3405 // Provide stub message handler; V8 auto-continues each suspend
3406 // when there is no message handler; we doesn't need it.
3407 // Once become suspended, V8 will stay so indefinitely long, until remote
3408 // debugger connects and issues "continue" command.
3409 Debugger::message_handler_ = StubMessageHandler2;
3410 v8::Debug::DebugBreak();
3411 }
3412
jkummerow@chromium.org3d00d0a2013-09-04 13:57:32 +00003413 if (agent_ == NULL) {
3414 agent_ = new DebuggerAgent(isolate_, name, port);
3415 agent_->Start();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003416 }
jkummerow@chromium.org3d00d0a2013-09-04 13:57:32 +00003417 return true;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003418}
3419
3420
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003421void Debugger::StopAgent() {
3422 if (agent_ != NULL) {
3423 agent_->Shutdown();
3424 agent_->Join();
3425 delete agent_;
3426 agent_ = NULL;
3427 }
3428}
3429
3430
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003431void Debugger::WaitForAgent() {
3432 if (agent_ != NULL)
3433 agent_->WaitUntilListening();
3434}
3435
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003436
3437void Debugger::CallMessageDispatchHandler() {
3438 v8::Debug::DebugMessageDispatchHandler handler;
3439 {
jkummerow@chromium.orgdc94e192013-08-30 11:35:42 +00003440 LockGuard<Mutex> lock_guard(&dispatch_handler_access_);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003441 handler = Debugger::debug_message_dispatch_handler_;
3442 }
3443 if (handler != NULL) {
3444 handler();
3445 }
3446}
3447
3448
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +00003449EnterDebugger::EnterDebugger(Isolate* isolate)
3450 : isolate_(isolate),
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003451 prev_(isolate_->debug()->debugger_entry()),
3452 it_(isolate_),
3453 has_js_frames_(!it_.done()),
3454 save_(isolate_) {
3455 Debug* debug = isolate_->debug();
3456 ASSERT(prev_ != NULL || !debug->is_interrupt_pending(PREEMPT));
3457 ASSERT(prev_ != NULL || !debug->is_interrupt_pending(DEBUGBREAK));
3458
3459 // Link recursive debugger entry.
3460 debug->set_debugger_entry(this);
3461
3462 // Store the previous break id and frame id.
3463 break_id_ = debug->break_id();
3464 break_frame_id_ = debug->break_frame_id();
3465
3466 // Create the new break info. If there is no JavaScript frames there is no
3467 // break frame id.
3468 if (has_js_frames_) {
3469 debug->NewBreak(it_.frame()->id());
3470 } else {
3471 debug->NewBreak(StackFrame::NO_ID);
3472 }
3473
3474 // Make sure that debugger is loaded and enter the debugger context.
3475 load_failed_ = !debug->Load();
3476 if (!load_failed_) {
3477 // NOTE the member variable save which saves the previous context before
3478 // this change.
3479 isolate_->set_context(*debug->debug_context());
3480 }
3481}
3482
3483
3484EnterDebugger::~EnterDebugger() {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003485 Debug* debug = isolate_->debug();
3486
3487 // Restore to the previous break state.
3488 debug->SetBreak(break_frame_id_, break_id_);
3489
3490 // Check for leaving the debugger.
fschneider@chromium.org7d10be52012-04-10 12:30:14 +00003491 if (!load_failed_ && prev_ == NULL) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003492 // Clear mirror cache when leaving the debugger. Skip this if there is a
3493 // pending exception as clearing the mirror cache calls back into
3494 // JavaScript. This can happen if the v8::Debug::Call is used in which
3495 // case the exception should end up in the calling code.
3496 if (!isolate_->has_pending_exception()) {
3497 // Try to avoid any pending debug break breaking in the clear mirror
3498 // cache JavaScript code.
3499 if (isolate_->stack_guard()->IsDebugBreak()) {
3500 debug->set_interrupts_pending(DEBUGBREAK);
3501 isolate_->stack_guard()->Continue(DEBUGBREAK);
3502 }
3503 debug->ClearMirrorCache();
3504 }
3505
3506 // Request preemption and debug break when leaving the last debugger entry
3507 // if any of these where recorded while debugging.
3508 if (debug->is_interrupt_pending(PREEMPT)) {
3509 // This re-scheduling of preemption is to avoid starvation in some
3510 // debugging scenarios.
3511 debug->clear_interrupt_pending(PREEMPT);
3512 isolate_->stack_guard()->Preempt();
3513 }
3514 if (debug->is_interrupt_pending(DEBUGBREAK)) {
3515 debug->clear_interrupt_pending(DEBUGBREAK);
3516 isolate_->stack_guard()->DebugBreak();
3517 }
3518
3519 // If there are commands in the queue when leaving the debugger request
3520 // that these commands are processed.
3521 if (isolate_->debugger()->HasCommands()) {
3522 isolate_->stack_guard()->DebugCommand();
3523 }
3524
3525 // If leaving the debugger with the debugger no longer active unload it.
3526 if (!isolate_->debugger()->IsDebuggerActive()) {
3527 isolate_->debugger()->UnloadDebugger();
3528 }
3529 }
3530
3531 // Leaving this debugger entry.
3532 debug->set_debugger_entry(prev_);
3533}
3534
3535
ager@chromium.org5ec48922009-05-05 07:25:34 +00003536MessageImpl MessageImpl::NewEvent(DebugEvent event,
3537 bool running,
3538 Handle<JSObject> exec_state,
3539 Handle<JSObject> event_data) {
3540 MessageImpl message(true, event, running,
3541 exec_state, event_data, Handle<String>(), NULL);
3542 return message;
3543}
3544
3545
3546MessageImpl MessageImpl::NewResponse(DebugEvent event,
3547 bool running,
3548 Handle<JSObject> exec_state,
3549 Handle<JSObject> event_data,
3550 Handle<String> response_json,
3551 v8::Debug::ClientData* client_data) {
3552 MessageImpl message(false, event, running,
3553 exec_state, event_data, response_json, client_data);
3554 return message;
3555}
3556
3557
3558MessageImpl::MessageImpl(bool is_event,
3559 DebugEvent event,
3560 bool running,
3561 Handle<JSObject> exec_state,
3562 Handle<JSObject> event_data,
3563 Handle<String> response_json,
3564 v8::Debug::ClientData* client_data)
3565 : is_event_(is_event),
3566 event_(event),
3567 running_(running),
3568 exec_state_(exec_state),
3569 event_data_(event_data),
3570 response_json_(response_json),
3571 client_data_(client_data) {}
3572
3573
3574bool MessageImpl::IsEvent() const {
3575 return is_event_;
3576}
3577
3578
3579bool MessageImpl::IsResponse() const {
3580 return !is_event_;
3581}
3582
3583
3584DebugEvent MessageImpl::GetEvent() const {
3585 return event_;
3586}
3587
3588
3589bool MessageImpl::WillStartRunning() const {
3590 return running_;
3591}
3592
3593
3594v8::Handle<v8::Object> MessageImpl::GetExecutionState() const {
3595 return v8::Utils::ToLocal(exec_state_);
3596}
3597
3598
hpayer@chromium.orgc5d49712013-09-11 08:25:48 +00003599v8::Isolate* MessageImpl::GetIsolate() const {
3600 return reinterpret_cast<v8::Isolate*>(exec_state_->GetIsolate());
3601}
3602
3603
ager@chromium.org5ec48922009-05-05 07:25:34 +00003604v8::Handle<v8::Object> MessageImpl::GetEventData() const {
3605 return v8::Utils::ToLocal(event_data_);
3606}
3607
3608
3609v8::Handle<v8::String> MessageImpl::GetJSON() const {
machenbach@chromium.orgce9c5142013-12-03 08:00:39 +00003610 v8::EscapableHandleScope scope(
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +00003611 reinterpret_cast<v8::Isolate*>(event_data_->GetIsolate()));
ager@chromium.org5ec48922009-05-05 07:25:34 +00003612
3613 if (IsEvent()) {
3614 // Call toJSONProtocol on the debug event object.
3615 Handle<Object> fun = GetProperty(event_data_, "toJSONProtocol");
3616 if (!fun->IsJSFunction()) {
3617 return v8::Handle<v8::String>();
3618 }
3619 bool caught_exception;
3620 Handle<Object> json = Execution::TryCall(Handle<JSFunction>::cast(fun),
3621 event_data_,
3622 0, NULL, &caught_exception);
3623 if (caught_exception || !json->IsString()) {
3624 return v8::Handle<v8::String>();
3625 }
machenbach@chromium.orgce9c5142013-12-03 08:00:39 +00003626 return scope.Escape(v8::Utils::ToLocal(Handle<String>::cast(json)));
ager@chromium.org5ec48922009-05-05 07:25:34 +00003627 } else {
3628 return v8::Utils::ToLocal(response_json_);
3629 }
3630}
3631
3632
3633v8::Handle<v8::Context> MessageImpl::GetEventContext() const {
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +00003634 Isolate* isolate = event_data_->GetIsolate();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003635 v8::Handle<v8::Context> context = GetDebugEventContext(isolate);
3636 // Isolate::context() may be NULL when "script collected" event occures.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003637 ASSERT(!context.IsEmpty() || event_ == v8::ScriptCollected);
rossberg@chromium.org717967f2011-07-20 13:44:42 +00003638 return context;
ager@chromium.org5ec48922009-05-05 07:25:34 +00003639}
3640
3641
3642v8::Debug::ClientData* MessageImpl::GetClientData() const {
3643 return client_data_;
3644}
3645
3646
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003647EventDetailsImpl::EventDetailsImpl(DebugEvent event,
3648 Handle<JSObject> exec_state,
3649 Handle<JSObject> event_data,
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00003650 Handle<Object> callback_data,
3651 v8::Debug::ClientData* client_data)
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003652 : event_(event),
3653 exec_state_(exec_state),
3654 event_data_(event_data),
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00003655 callback_data_(callback_data),
3656 client_data_(client_data) {}
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003657
3658
3659DebugEvent EventDetailsImpl::GetEvent() const {
3660 return event_;
3661}
3662
3663
3664v8::Handle<v8::Object> EventDetailsImpl::GetExecutionState() const {
3665 return v8::Utils::ToLocal(exec_state_);
3666}
3667
3668
3669v8::Handle<v8::Object> EventDetailsImpl::GetEventData() const {
3670 return v8::Utils::ToLocal(event_data_);
3671}
3672
3673
3674v8::Handle<v8::Context> EventDetailsImpl::GetEventContext() const {
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +00003675 return GetDebugEventContext(exec_state_->GetIsolate());
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003676}
3677
3678
3679v8::Handle<v8::Value> EventDetailsImpl::GetCallbackData() const {
3680 return v8::Utils::ToLocal(callback_data_);
3681}
3682
3683
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00003684v8::Debug::ClientData* EventDetailsImpl::GetClientData() const {
3685 return client_data_;
3686}
3687
3688
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003689CommandMessage::CommandMessage() : text_(Vector<uint16_t>::empty()),
3690 client_data_(NULL) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003691}
3692
3693
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003694CommandMessage::CommandMessage(const Vector<uint16_t>& text,
3695 v8::Debug::ClientData* data)
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003696 : text_(text),
3697 client_data_(data) {
3698}
3699
3700
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003701CommandMessage::~CommandMessage() {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003702}
3703
3704
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003705void CommandMessage::Dispose() {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003706 text_.Dispose();
3707 delete client_data_;
3708 client_data_ = NULL;
3709}
3710
3711
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003712CommandMessage CommandMessage::New(const Vector<uint16_t>& command,
3713 v8::Debug::ClientData* data) {
3714 return CommandMessage(command.Clone(), data);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003715}
3716
3717
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003718CommandMessageQueue::CommandMessageQueue(int size) : start_(0), end_(0),
3719 size_(size) {
3720 messages_ = NewArray<CommandMessage>(size);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003721}
3722
3723
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003724CommandMessageQueue::~CommandMessageQueue() {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003725 while (!IsEmpty()) {
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003726 CommandMessage m = Get();
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003727 m.Dispose();
3728 }
kasper.lund7276f142008-07-30 08:49:36 +00003729 DeleteArray(messages_);
3730}
3731
3732
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003733CommandMessage CommandMessageQueue::Get() {
kasper.lund7276f142008-07-30 08:49:36 +00003734 ASSERT(!IsEmpty());
3735 int result = start_;
3736 start_ = (start_ + 1) % size_;
3737 return messages_[result];
3738}
3739
3740
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003741void CommandMessageQueue::Put(const CommandMessage& message) {
kasper.lund7276f142008-07-30 08:49:36 +00003742 if ((end_ + 1) % size_ == start_) {
3743 Expand();
3744 }
3745 messages_[end_] = message;
3746 end_ = (end_ + 1) % size_;
3747}
3748
3749
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003750void CommandMessageQueue::Expand() {
3751 CommandMessageQueue new_queue(size_ * 2);
kasper.lund7276f142008-07-30 08:49:36 +00003752 while (!IsEmpty()) {
3753 new_queue.Put(Get());
3754 }
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003755 CommandMessage* array_to_free = messages_;
kasper.lund7276f142008-07-30 08:49:36 +00003756 *this = new_queue;
3757 new_queue.messages_ = array_to_free;
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003758 // Make the new_queue empty so that it doesn't call Dispose on any messages.
3759 new_queue.start_ = new_queue.end_;
kasper.lund7276f142008-07-30 08:49:36 +00003760 // Automatic destructor called on new_queue, freeing array_to_free.
3761}
3762
3763
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00003764LockingCommandMessageQueue::LockingCommandMessageQueue(Logger* logger, int size)
jkummerow@chromium.orgdc94e192013-08-30 11:35:42 +00003765 : logger_(logger), queue_(size) {}
kasper.lund7276f142008-07-30 08:49:36 +00003766
3767
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003768bool LockingCommandMessageQueue::IsEmpty() const {
jkummerow@chromium.orgdc94e192013-08-30 11:35:42 +00003769 LockGuard<Mutex> lock_guard(&mutex_);
kasper.lund7276f142008-07-30 08:49:36 +00003770 return queue_.IsEmpty();
3771}
3772
3773
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003774CommandMessage LockingCommandMessageQueue::Get() {
jkummerow@chromium.orgdc94e192013-08-30 11:35:42 +00003775 LockGuard<Mutex> lock_guard(&mutex_);
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003776 CommandMessage result = queue_.Get();
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00003777 logger_->DebugEvent("Get", result.text());
kasper.lund7276f142008-07-30 08:49:36 +00003778 return result;
3779}
3780
3781
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003782void LockingCommandMessageQueue::Put(const CommandMessage& message) {
jkummerow@chromium.orgdc94e192013-08-30 11:35:42 +00003783 LockGuard<Mutex> lock_guard(&mutex_);
kasper.lund7276f142008-07-30 08:49:36 +00003784 queue_.Put(message);
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00003785 logger_->DebugEvent("Put", message.text());
kasper.lund7276f142008-07-30 08:49:36 +00003786}
3787
3788
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003789void LockingCommandMessageQueue::Clear() {
jkummerow@chromium.orgdc94e192013-08-30 11:35:42 +00003790 LockGuard<Mutex> lock_guard(&mutex_);
kasper.lund7276f142008-07-30 08:49:36 +00003791 queue_.Clear();
3792}
3793
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003794
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003795MessageDispatchHelperThread::MessageDispatchHelperThread(Isolate* isolate)
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00003796 : Thread("v8:MsgDispHelpr"),
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +00003797 isolate_(isolate), sem_(0),
jkummerow@chromium.orgdc94e192013-08-30 11:35:42 +00003798 already_signalled_(false) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003799}
3800
3801
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003802void MessageDispatchHelperThread::Schedule() {
3803 {
jkummerow@chromium.orgdc94e192013-08-30 11:35:42 +00003804 LockGuard<Mutex> lock_guard(&mutex_);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003805 if (already_signalled_) {
3806 return;
3807 }
3808 already_signalled_ = true;
3809 }
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +00003810 sem_.Signal();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003811}
3812
3813
3814void MessageDispatchHelperThread::Run() {
3815 while (true) {
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +00003816 sem_.Wait();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003817 {
jkummerow@chromium.orgdc94e192013-08-30 11:35:42 +00003818 LockGuard<Mutex> lock_guard(&mutex_);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003819 already_signalled_ = false;
3820 }
3821 {
mstarzinger@chromium.orgf705b502013-04-04 11:38:09 +00003822 Locker locker(reinterpret_cast<v8::Isolate*>(isolate_));
3823 isolate_->debugger()->CallMessageDispatchHandler();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003824 }
3825 }
3826}
3827
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003828#endif // ENABLE_DEBUGGER_SUPPORT
kasper.lund7276f142008-07-30 08:49:36 +00003829
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003830} } // namespace v8::internal