blob: f3c3764121c0f9a34b29b9743f6dfb5f5af82e0f [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_ =
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000649 Handle<Script>::cast(
lrn@chromium.org7516f052011-03-30 08:52:27 +0000650 (global_handles->Create(*script)));
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000651 global_handles->MakeWeak(reinterpret_cast<Object**>(script_.location()),
652 this,
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000653 ScriptCache::HandleWeakScript);
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000654 entry->value = script_.location();
655}
656
657
658Handle<FixedArray> ScriptCache::GetScripts() {
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +0000659 Factory* factory = isolate_->factory();
verwaest@chromium.orgd4be0f02013-06-05 13:39:03 +0000660 Handle<FixedArray> instances = factory->NewFixedArray(occupancy());
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000661 int count = 0;
662 for (HashMap::Entry* entry = Start(); entry != NULL; entry = Next(entry)) {
663 ASSERT(entry->value != NULL);
664 if (entry->value != NULL) {
665 instances->set(count, *reinterpret_cast<Script**>(entry->value));
666 count++;
667 }
668 }
669 return instances;
670}
671
672
673void ScriptCache::ProcessCollectedScripts() {
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +0000674 Debugger* debugger = isolate_->debugger();
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000675 for (int i = 0; i < collected_scripts_.length(); i++) {
lrn@chromium.org7516f052011-03-30 08:52:27 +0000676 debugger->OnScriptCollected(collected_scripts_[i]);
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000677 }
678 collected_scripts_.Clear();
679}
680
681
682void ScriptCache::Clear() {
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +0000683 GlobalHandles* global_handles = isolate_->global_handles();
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000684 // Iterate the script cache to get rid of all the weak handles.
685 for (HashMap::Entry* entry = Start(); entry != NULL; entry = Next(entry)) {
686 ASSERT(entry != NULL);
687 Object** location = reinterpret_cast<Object**>(entry->value);
688 ASSERT((*location)->IsScript());
lrn@chromium.org7516f052011-03-30 08:52:27 +0000689 global_handles->ClearWeakness(location);
690 global_handles->Destroy(location);
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000691 }
692 // Clear the content of the hash map.
693 HashMap::Clear();
694}
695
696
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000697void ScriptCache::HandleWeakScript(v8::Isolate* isolate,
rossberg@chromium.org79e79022013-06-03 15:43:46 +0000698 v8::Persistent<v8::Value>* obj,
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000699 void* data) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000700 ScriptCache* script_cache = reinterpret_cast<ScriptCache*>(data);
701 // Find the location of the global handle.
702 Script** location =
danno@chromium.orgf95d4b92013-06-13 14:40:17 +0000703 reinterpret_cast<Script**>(Utils::OpenPersistent(*obj).location());
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000704 ASSERT((*location)->IsScript());
705
706 // Remove the entry from the cache.
mstarzinger@chromium.org1510d582013-06-28 14:00:48 +0000707 int id = (*location)->id()->value();
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000708 script_cache->Remove(reinterpret_cast<void*>(id), Hash(id));
709 script_cache->collected_scripts_.Add(id);
710
711 // Clear the weak handle.
machenbach@chromium.orgf9841892013-11-25 12:01:13 +0000712 obj->Reset();
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
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000731void Debug::HandleWeakDebugInfo(v8::Isolate* isolate,
rossberg@chromium.org79e79022013-06-03 15:43:46 +0000732 v8::Persistent<v8::Value>* obj,
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000733 void* data) {
734 Debug* debug = reinterpret_cast<Isolate*>(isolate)->debug();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000735 DebugInfoListNode* node = reinterpret_cast<DebugInfoListNode*>(data);
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
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000744 node = debug->debug_info_list_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000745 while (node != NULL) {
746 ASSERT(node != reinterpret_cast<DebugInfoListNode*>(data));
747 node = node->next();
748 }
749#endif
750}
751
752
753DebugInfoListNode::DebugInfoListNode(DebugInfo* debug_info): next_(NULL) {
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +0000754 GlobalHandles* global_handles = debug_info->GetIsolate()->global_handles();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000755 // Globalize the request debug info object and make it weak.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000756 debug_info_ = Handle<DebugInfo>::cast(
lrn@chromium.org7516f052011-03-30 08:52:27 +0000757 (global_handles->Create(debug_info)));
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000758 global_handles->MakeWeak(reinterpret_cast<Object**>(debug_info_.location()),
759 this,
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000760 Debug::HandleWeakDebugInfo);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000761}
762
763
764DebugInfoListNode::~DebugInfoListNode() {
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +0000765 debug_info_->GetIsolate()->global_handles()->Destroy(
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000766 reinterpret_cast<Object**>(debug_info_.location()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000767}
768
769
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +0000770bool Debug::CompileDebuggerScript(Isolate* isolate, int index) {
lrn@chromium.org7516f052011-03-30 08:52:27 +0000771 Factory* factory = isolate->factory();
772 HandleScope scope(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000773
kasper.lund44510672008-07-25 07:37:58 +0000774 // Bail out if the index is invalid.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000775 if (index == -1) {
776 return false;
777 }
kasper.lund44510672008-07-25 07:37:58 +0000778
779 // Find source and name for the requested script.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000780 Handle<String> source_code =
lrn@chromium.org7516f052011-03-30 08:52:27 +0000781 isolate->bootstrapper()->NativesSourceLookup(index);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000782 Vector<const char> name = Natives::GetScriptName(index);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000783 Handle<String> script_name = factory->NewStringFromAscii(name);
yangguo@chromium.org355cfd12012-08-29 15:32:24 +0000784 Handle<Context> context = isolate->native_context();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000785
786 // Compile the script.
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000787 Handle<SharedFunctionInfo> function_info;
788 function_info = Compiler::Compile(source_code,
789 script_name,
yangguo@chromium.org355cfd12012-08-29 15:32:24 +0000790 0, 0,
danno@chromium.orgd3c42102013-08-01 16:58:23 +0000791 false,
yangguo@chromium.org355cfd12012-08-29 15:32:24 +0000792 context,
793 NULL, NULL,
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000794 Handle<String>::null(),
795 NATIVES_CODE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000796
797 // Silently ignore stack overflows during compilation.
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000798 if (function_info.is_null()) {
lrn@chromium.org7516f052011-03-30 08:52:27 +0000799 ASSERT(isolate->has_pending_exception());
800 isolate->clear_pending_exception();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000801 return false;
802 }
803
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000804 // Execute the shared function in the debugger context.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000805 bool caught_exception;
kasper.lund44510672008-07-25 07:37:58 +0000806 Handle<JSFunction> function =
lrn@chromium.org7516f052011-03-30 08:52:27 +0000807 factory->NewFunctionFromSharedFunctionInfo(function_info, context);
rossberg@chromium.org717967f2011-07-20 13:44:42 +0000808
fschneider@chromium.org7d10be52012-04-10 12:30:14 +0000809 Handle<Object> exception =
ulan@chromium.org09d7ab52013-02-25 15:50:35 +0000810 Execution::TryCall(function,
811 Handle<Object>(context->global_object(), isolate),
812 0,
813 NULL,
814 &caught_exception);
kasper.lund44510672008-07-25 07:37:58 +0000815
816 // Check for caught exceptions.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000817 if (caught_exception) {
fschneider@chromium.org7d10be52012-04-10 12:30:14 +0000818 ASSERT(!isolate->has_pending_exception());
819 MessageLocation computed_location;
820 isolate->ComputeLocation(&computed_location);
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000821 Handle<Object> message = MessageHandler::MakeMessageObject(
verwaest@chromium.orgd4be0f02013-06-05 13:39:03 +0000822 isolate, "error_loading_debugger", &computed_location,
fschneider@chromium.org7d10be52012-04-10 12:30:14 +0000823 Vector<Handle<Object> >::empty(), Handle<String>(), Handle<JSArray>());
824 ASSERT(!isolate->has_pending_exception());
mmassi@chromium.org49a44672012-12-04 13:52:03 +0000825 if (!exception.is_null()) {
826 isolate->set_pending_exception(*exception);
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +0000827 MessageHandler::ReportMessage(isolate, NULL, message);
mmassi@chromium.org49a44672012-12-04 13:52:03 +0000828 isolate->clear_pending_exception();
829 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000830 return false;
831 }
832
kasper.lund44510672008-07-25 07:37:58 +0000833 // Mark this script as native and return successfully.
834 Handle<Script> script(Script::cast(function->shared()->script()));
ager@chromium.orge2902be2009-06-08 12:21:35 +0000835 script->set_type(Smi::FromInt(Script::TYPE_NATIVE));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000836 return true;
837}
838
839
840bool Debug::Load() {
841 // Return if debugger is already loaded.
kasper.lund212ac232008-07-16 07:07:30 +0000842 if (IsLoaded()) return true;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000843
lrn@chromium.org7516f052011-03-30 08:52:27 +0000844 Debugger* debugger = isolate_->debugger();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000845
kasper.lund44510672008-07-25 07:37:58 +0000846 // Bail out if we're already in the process of compiling the native
847 // JavaScript source code for the debugger.
lrn@chromium.org7516f052011-03-30 08:52:27 +0000848 if (debugger->compiling_natives() ||
849 debugger->is_loading_debugger())
mads.s.agercbaa0602008-08-14 13:41:48 +0000850 return false;
lrn@chromium.org7516f052011-03-30 08:52:27 +0000851 debugger->set_loading_debugger(true);
kasper.lund44510672008-07-25 07:37:58 +0000852
853 // Disable breakpoints and interrupts while compiling and running the
854 // debugger scripts including the context creation code.
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +0000855 DisableBreak disable(isolate_, true);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000856 PostponeInterruptsScope postpone(isolate_);
kasper.lund44510672008-07-25 07:37:58 +0000857
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000858 // Create the debugger context.
lrn@chromium.org7516f052011-03-30 08:52:27 +0000859 HandleScope scope(isolate_);
kasper.lund44510672008-07-25 07:37:58 +0000860 Handle<Context> context =
lrn@chromium.org7516f052011-03-30 08:52:27 +0000861 isolate_->bootstrapper()->CreateEnvironment(
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000862 Handle<Object>::null(),
863 v8::Handle<ObjectTemplate>(),
864 NULL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000865
fschneider@chromium.org7d10be52012-04-10 12:30:14 +0000866 // Fail if no context could be created.
867 if (context.is_null()) return false;
868
kasper.lund44510672008-07-25 07:37:58 +0000869 // Use the debugger context.
lrn@chromium.org7516f052011-03-30 08:52:27 +0000870 SaveContext save(isolate_);
871 isolate_->set_context(*context);
kasper.lund44510672008-07-25 07:37:58 +0000872
873 // Expose the builtins object in the debugger context.
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000874 Handle<String> key = isolate_->factory()->InternalizeOneByteString(
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +0000875 STATIC_ASCII_VECTOR("builtins"));
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000876 Handle<GlobalObject> global = Handle<GlobalObject>(context->global_object());
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +0000877 RETURN_IF_EMPTY_HANDLE_VALUE(
lrn@chromium.org7516f052011-03-30 08:52:27 +0000878 isolate_,
ulan@chromium.org09d7ab52013-02-25 15:50:35 +0000879 JSReceiver::SetProperty(global,
880 key,
881 Handle<Object>(global->builtins(), isolate_),
882 NONE,
883 kNonStrictMode),
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +0000884 false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000885
886 // Compile the JavaScript for the debugger in the debugger context.
lrn@chromium.org7516f052011-03-30 08:52:27 +0000887 debugger->set_compiling_natives(true);
kasper.lund44510672008-07-25 07:37:58 +0000888 bool caught_exception =
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +0000889 !CompileDebuggerScript(isolate_, Natives::GetIndex("mirror")) ||
890 !CompileDebuggerScript(isolate_, Natives::GetIndex("debug"));
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000891
892 if (FLAG_enable_liveedit) {
893 caught_exception = caught_exception ||
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +0000894 !CompileDebuggerScript(isolate_, Natives::GetIndex("liveedit"));
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000895 }
896
lrn@chromium.org7516f052011-03-30 08:52:27 +0000897 debugger->set_compiling_natives(false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000898
mads.s.agercbaa0602008-08-14 13:41:48 +0000899 // Make sure we mark the debugger as not loading before we might
900 // return.
lrn@chromium.org7516f052011-03-30 08:52:27 +0000901 debugger->set_loading_debugger(false);
mads.s.agercbaa0602008-08-14 13:41:48 +0000902
kasper.lund44510672008-07-25 07:37:58 +0000903 // Check for caught exceptions.
904 if (caught_exception) return false;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000905
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000906 // Debugger loaded, create debugger context global handle.
907 debug_context_ = Handle<Context>::cast(
908 isolate_->global_handles()->Create(*context));
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000909
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000910 return true;
911}
912
913
914void Debug::Unload() {
915 // Return debugger is not loaded.
916 if (!IsLoaded()) {
917 return;
918 }
919
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000920 // Clear the script cache.
921 DestroyScriptCache();
922
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000923 // Clear debugger context global handle.
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000924 isolate_->global_handles()->Destroy(
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000925 reinterpret_cast<Object**>(debug_context_.location()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000926 debug_context_ = Handle<Context>();
927}
928
929
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000930// Set the flag indicating that preemption happened during debugging.
931void Debug::PreemptionWhileInDebugger() {
932 ASSERT(InDebugger());
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000933 Debug::set_interrupts_pending(PREEMPT);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000934}
935
936
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000937void Debug::Iterate(ObjectVisitor* v) {
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000938 v->VisitPointer(BitCast<Object**>(&(debug_break_return_)));
939 v->VisitPointer(BitCast<Object**>(&(debug_break_slot_)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000940}
941
942
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000943Object* Debug::Break(Arguments args) {
944 Heap* heap = isolate_->heap();
945 HandleScope scope(isolate_);
mads.s.ager31e71382008-08-13 09:32:07 +0000946 ASSERT(args.length() == 0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000947
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000948 thread_local_.frame_drop_mode_ = FRAMES_UNTOUCHED;
ager@chromium.org357bf652010-04-12 11:30:10 +0000949
kasper.lundbd3ec4e2008-07-09 11:06:54 +0000950 // Get the top-most JavaScript frame.
vegorov@chromium.org74f333b2011-04-06 11:17:46 +0000951 JavaScriptFrameIterator it(isolate_);
kasper.lundbd3ec4e2008-07-09 11:06:54 +0000952 JavaScriptFrame* frame = it.frame();
953
954 // Just continue if breaks are disabled or debugger cannot be loaded.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000955 if (disable_break() || !Load()) {
956 SetAfterBreakTarget(frame);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000957 return heap->undefined_value();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000958 }
959
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000960 // Enter the debugger.
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +0000961 EnterDebugger debugger(isolate_);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000962 if (debugger.FailedToEnter()) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000963 return heap->undefined_value();
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000964 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000965
kasper.lund44510672008-07-25 07:37:58 +0000966 // Postpone interrupt during breakpoint processing.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000967 PostponeInterruptsScope postpone(isolate_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000968
969 // Get the debug info (create it if it does not exist).
970 Handle<SharedFunctionInfo> shared =
danno@chromium.org169691d2013-07-15 08:01:13 +0000971 Handle<SharedFunctionInfo>(frame->function()->shared());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000972 Handle<DebugInfo> debug_info = GetDebugInfo(shared);
973
974 // Find the break point where execution has stopped.
975 BreakLocationIterator break_location_iterator(debug_info,
976 ALL_BREAK_LOCATIONS);
verwaest@chromium.org8a00e822013-06-10 15:11:22 +0000977 // pc points to the instruction after the current one, possibly a break
978 // location as well. So the "- 1" to exclude it from the search.
979 break_location_iterator.FindBreakLocationFromAddress(frame->pc() - 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000980
981 // Check whether step next reached a new statement.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000982 if (!StepNextContinue(&break_location_iterator, frame)) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000983 // Decrease steps left if performing multiple steps.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000984 if (thread_local_.step_count_ > 0) {
985 thread_local_.step_count_--;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000986 }
987 }
988
989 // If there is one or more real break points check whether any of these are
990 // triggered.
ulan@chromium.org09d7ab52013-02-25 15:50:35 +0000991 Handle<Object> break_points_hit(heap->undefined_value(), isolate_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000992 if (break_location_iterator.HasBreakPoint()) {
993 Handle<Object> break_point_objects =
ulan@chromium.org09d7ab52013-02-25 15:50:35 +0000994 Handle<Object>(break_location_iterator.BreakPointObjects(), isolate_);
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000995 break_points_hit = CheckBreakPoints(break_point_objects);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000996 }
997
ager@chromium.orga1645e22009-09-09 19:27:10 +0000998 // If step out is active skip everything until the frame where we need to step
999 // out to is reached, unless real breakpoint is hit.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001000 if (StepOutActive() && frame->fp() != step_out_fp() &&
ager@chromium.orga1645e22009-09-09 19:27:10 +00001001 break_points_hit->IsUndefined() ) {
1002 // Step count should always be 0 for StepOut.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001003 ASSERT(thread_local_.step_count_ == 0);
ager@chromium.orga1645e22009-09-09 19:27:10 +00001004 } else if (!break_points_hit->IsUndefined() ||
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001005 (thread_local_.last_step_action_ != StepNone &&
1006 thread_local_.step_count_ == 0)) {
ager@chromium.orga1645e22009-09-09 19:27:10 +00001007 // Notify debugger if a real break point is triggered or if performing
1008 // single stepping with no more steps to perform. Otherwise do another step.
1009
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001010 // Clear all current stepping setup.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001011 ClearStepping();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001012
lrn@chromium.org34e60782011-09-15 07:25:40 +00001013 if (thread_local_.queued_step_count_ > 0) {
1014 // Perform queued steps
1015 int step_count = thread_local_.queued_step_count_;
1016
1017 // Clear queue
1018 thread_local_.queued_step_count_ = 0;
1019
dslomov@chromium.org639bac02013-09-09 11:58:54 +00001020 PrepareStep(StepNext, step_count, StackFrame::NO_ID);
lrn@chromium.org34e60782011-09-15 07:25:40 +00001021 } else {
1022 // Notify the debug event listeners.
1023 isolate_->debugger()->OnDebugBreak(break_points_hit, false);
1024 }
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001025 } else if (thread_local_.last_step_action_ != StepNone) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001026 // Hold on to last step action as it is cleared by the call to
1027 // ClearStepping.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001028 StepAction step_action = thread_local_.last_step_action_;
1029 int step_count = thread_local_.step_count_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001030
lrn@chromium.org34e60782011-09-15 07:25:40 +00001031 // If StepNext goes deeper in code, StepOut until original frame
1032 // and keep step count queued up in the meantime.
1033 if (step_action == StepNext && frame->fp() < thread_local_.last_fp_) {
1034 // Count frames until target frame
1035 int count = 0;
1036 JavaScriptFrameIterator it(isolate_);
jkummerow@chromium.org212d9642012-05-11 15:02:09 +00001037 while (!it.done() && it.frame()->fp() < thread_local_.last_fp_) {
lrn@chromium.org34e60782011-09-15 07:25:40 +00001038 count++;
1039 it.Advance();
1040 }
1041
danno@chromium.org81cac2b2012-07-10 11:28:27 +00001042 // Check that we indeed found the frame we are looking for.
1043 CHECK(!it.done() && (it.frame()->fp() == thread_local_.last_fp_));
1044 if (step_count > 1) {
1045 // Save old count and action to continue stepping after StepOut.
1046 thread_local_.queued_step_count_ = step_count - 1;
jkummerow@chromium.org212d9642012-05-11 15:02:09 +00001047 }
1048
danno@chromium.org81cac2b2012-07-10 11:28:27 +00001049 // Set up for StepOut to reach target frame.
1050 step_action = StepOut;
1051 step_count = count;
lrn@chromium.org34e60782011-09-15 07:25:40 +00001052 }
1053
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001054 // Clear all current stepping setup.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001055 ClearStepping();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001056
1057 // Set up for the remaining steps.
dslomov@chromium.org639bac02013-09-09 11:58:54 +00001058 PrepareStep(step_action, step_count, StackFrame::NO_ID);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001059 }
1060
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001061 if (thread_local_.frame_drop_mode_ == FRAMES_UNTOUCHED) {
1062 SetAfterBreakTarget(frame);
1063 } else if (thread_local_.frame_drop_mode_ ==
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001064 FRAME_DROPPED_IN_IC_CALL) {
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001065 // We must have been calling IC stub. Do not go there anymore.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001066 Code* plain_return = isolate_->builtins()->builtin(
1067 Builtins::kPlainReturn_LiveEdit);
1068 thread_local_.after_break_target_ = plain_return->entry();
1069 } else if (thread_local_.frame_drop_mode_ ==
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001070 FRAME_DROPPED_IN_DEBUG_SLOT_CALL) {
1071 // Debug break slot stub does not return normally, instead it manually
1072 // cleans the stack and jumps. We should patch the jump address.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001073 Code* plain_return = isolate_->builtins()->builtin(
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00001074 Builtins::kFrameDropper_LiveEdit);
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001075 thread_local_.after_break_target_ = plain_return->entry();
1076 } else if (thread_local_.frame_drop_mode_ ==
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001077 FRAME_DROPPED_IN_DIRECT_CALL) {
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001078 // Nothing to do, after_break_target is not used here.
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00001079 } else if (thread_local_.frame_drop_mode_ ==
1080 FRAME_DROPPED_IN_RETURN_CALL) {
1081 Code* plain_return = isolate_->builtins()->builtin(
1082 Builtins::kFrameDropper_LiveEdit);
1083 thread_local_.after_break_target_ = plain_return->entry();
ager@chromium.org357bf652010-04-12 11:30:10 +00001084 } else {
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001085 UNREACHABLE();
ager@chromium.org357bf652010-04-12 11:30:10 +00001086 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001087
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001088 return heap->undefined_value();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001089}
1090
1091
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001092RUNTIME_FUNCTION(Object*, Debug_Break) {
1093 return isolate->debug()->Break(args);
1094}
1095
1096
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001097// Check the break point objects for whether one or more are actually
1098// triggered. This function returns a JSArray with the break point objects
1099// which is triggered.
1100Handle<Object> Debug::CheckBreakPoints(Handle<Object> break_point_objects) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00001101 Factory* factory = isolate_->factory();
1102
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001103 // Count the number of break points hit. If there are multiple break points
1104 // they are in a FixedArray.
1105 Handle<FixedArray> break_points_hit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001106 int break_points_hit_count = 0;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001107 ASSERT(!break_point_objects->IsUndefined());
1108 if (break_point_objects->IsFixedArray()) {
1109 Handle<FixedArray> array(FixedArray::cast(*break_point_objects));
lrn@chromium.org7516f052011-03-30 08:52:27 +00001110 break_points_hit = factory->NewFixedArray(array->length());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001111 for (int i = 0; i < array->length(); i++) {
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00001112 Handle<Object> o(array->get(i), isolate_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001113 if (CheckBreakPoint(o)) {
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001114 break_points_hit->set(break_points_hit_count++, *o);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001115 }
1116 }
1117 } else {
lrn@chromium.org7516f052011-03-30 08:52:27 +00001118 break_points_hit = factory->NewFixedArray(1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001119 if (CheckBreakPoint(break_point_objects)) {
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001120 break_points_hit->set(break_points_hit_count++, *break_point_objects);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001121 }
1122 }
1123
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001124 // Return undefined if no break points were triggered.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001125 if (break_points_hit_count == 0) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00001126 return factory->undefined_value();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001127 }
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001128 // Return break points hit as a JSArray.
lrn@chromium.org7516f052011-03-30 08:52:27 +00001129 Handle<JSArray> result = factory->NewJSArrayWithElements(break_points_hit);
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001130 result->set_length(Smi::FromInt(break_points_hit_count));
1131 return result;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001132}
1133
1134
1135// Check whether a single break point object is triggered.
1136bool Debug::CheckBreakPoint(Handle<Object> break_point_object) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00001137 Factory* factory = isolate_->factory();
1138 HandleScope scope(isolate_);
ager@chromium.org381abbb2009-02-25 13:23:22 +00001139
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001140 // Ignore check if break point object is not a JSObject.
1141 if (!break_point_object->IsJSObject()) return true;
1142
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001143 // Get the function IsBreakPointTriggered (defined in debug-debugger.js).
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001144 Handle<String> is_break_point_triggered_string =
1145 factory->InternalizeOneByteString(
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00001146 STATIC_ASCII_VECTOR("IsBreakPointTriggered"));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001147 Handle<JSFunction> check_break_point =
1148 Handle<JSFunction>(JSFunction::cast(
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00001149 debug_context()->global_object()->GetPropertyNoExceptionThrown(
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001150 *is_break_point_triggered_string)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001151
1152 // Get the break id as an object.
lrn@chromium.org7516f052011-03-30 08:52:27 +00001153 Handle<Object> break_id = factory->NewNumberFromInt(Debug::break_id());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001154
1155 // Call HandleBreakPointx.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001156 bool caught_exception;
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00001157 Handle<Object> argv[] = { break_id, break_point_object };
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001158 Handle<Object> result = Execution::TryCall(check_break_point,
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00001159 isolate_->js_builtins_object(),
1160 ARRAY_SIZE(argv),
1161 argv,
1162 &caught_exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001163
1164 // If exception or non boolean result handle as not triggered
1165 if (caught_exception || !result->IsBoolean()) {
1166 return false;
1167 }
1168
1169 // Return whether the break point is triggered.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001170 ASSERT(!result.is_null());
1171 return (*result)->IsTrue();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001172}
1173
1174
1175// Check whether the function has debug information.
1176bool Debug::HasDebugInfo(Handle<SharedFunctionInfo> shared) {
1177 return !shared->debug_info()->IsUndefined();
1178}
1179
1180
kasper.lundbd3ec4e2008-07-09 11:06:54 +00001181// Return the debug info for this function. EnsureDebugInfo must be called
1182// prior to ensure the debug info has been generated for shared.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001183Handle<DebugInfo> Debug::GetDebugInfo(Handle<SharedFunctionInfo> shared) {
kasper.lundbd3ec4e2008-07-09 11:06:54 +00001184 ASSERT(HasDebugInfo(shared));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001185 return Handle<DebugInfo>(DebugInfo::cast(shared->debug_info()));
1186}
1187
1188
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001189void Debug::SetBreakPoint(Handle<JSFunction> function,
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00001190 Handle<Object> break_point_object,
1191 int* source_position) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00001192 HandleScope scope(isolate_);
ager@chromium.org381abbb2009-02-25 13:23:22 +00001193
lrn@chromium.org34e60782011-09-15 07:25:40 +00001194 PrepareForBreakPoints();
1195
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001196 // Make sure the function is compiled and has set up the debug info.
1197 Handle<SharedFunctionInfo> shared(function->shared());
1198 if (!EnsureDebugInfo(shared, function)) {
kasper.lundbd3ec4e2008-07-09 11:06:54 +00001199 // Return if retrieving debug info failed.
1200 return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001201 }
1202
kasper.lundbd3ec4e2008-07-09 11:06:54 +00001203 Handle<DebugInfo> debug_info = GetDebugInfo(shared);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001204 // Source positions starts with zero.
danno@chromium.orgbf0c8202011-12-27 10:09:42 +00001205 ASSERT(*source_position >= 0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001206
1207 // Find the break point and change it.
1208 BreakLocationIterator it(debug_info, SOURCE_BREAK_LOCATIONS);
jkummerow@chromium.org93a47f42013-07-02 14:43:41 +00001209 it.FindBreakLocationFromPosition(*source_position, STATEMENT_ALIGNED);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001210 it.SetBreakPoint(break_point_object);
1211
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00001212 *source_position = it.position();
1213
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001214 // At least one active break point now.
1215 ASSERT(debug_info->GetBreakPointCount() > 0);
1216}
1217
1218
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001219bool Debug::SetBreakPointForScript(Handle<Script> script,
1220 Handle<Object> break_point_object,
jkummerow@chromium.org93a47f42013-07-02 14:43:41 +00001221 int* source_position,
1222 BreakPositionAlignment alignment) {
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001223 HandleScope scope(isolate_);
1224
jkummerow@chromium.org78502a92012-09-06 13:50:42 +00001225 PrepareForBreakPoints();
1226
1227 // Obtain shared function info for the function.
1228 Object* result = FindSharedFunctionInfoInScript(script, *source_position);
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001229 if (result->IsUndefined()) return false;
1230
1231 // Make sure the function has set up the debug info.
1232 Handle<SharedFunctionInfo> shared(SharedFunctionInfo::cast(result));
1233 if (!EnsureDebugInfo(shared, Handle<JSFunction>::null())) {
1234 // Return if retrieving debug info failed.
1235 return false;
1236 }
1237
1238 // Find position within function. The script position might be before the
1239 // source position of the first function.
1240 int position;
1241 if (shared->start_position() > *source_position) {
1242 position = 0;
1243 } else {
1244 position = *source_position - shared->start_position();
1245 }
1246
1247 Handle<DebugInfo> debug_info = GetDebugInfo(shared);
1248 // Source positions starts with zero.
1249 ASSERT(position >= 0);
1250
1251 // Find the break point and change it.
1252 BreakLocationIterator it(debug_info, SOURCE_BREAK_LOCATIONS);
jkummerow@chromium.org93a47f42013-07-02 14:43:41 +00001253 it.FindBreakLocationFromPosition(position, alignment);
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001254 it.SetBreakPoint(break_point_object);
1255
1256 *source_position = it.position() + shared->start_position();
1257
1258 // At least one active break point now.
1259 ASSERT(debug_info->GetBreakPointCount() > 0);
1260 return true;
1261}
1262
1263
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001264void Debug::ClearBreakPoint(Handle<Object> break_point_object) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00001265 HandleScope scope(isolate_);
ager@chromium.org381abbb2009-02-25 13:23:22 +00001266
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001267 DebugInfoListNode* node = debug_info_list_;
1268 while (node != NULL) {
1269 Object* result = DebugInfo::FindBreakPointInfo(node->debug_info(),
1270 break_point_object);
1271 if (!result->IsUndefined()) {
1272 // Get information in the break point.
1273 BreakPointInfo* break_point_info = BreakPointInfo::cast(result);
1274 Handle<DebugInfo> debug_info = node->debug_info();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001275
1276 // Find the break point and clear it.
1277 BreakLocationIterator it(debug_info, SOURCE_BREAK_LOCATIONS);
verwaest@chromium.org8a00e822013-06-10 15:11:22 +00001278 it.FindBreakLocationFromAddress(debug_info->code()->entry() +
1279 break_point_info->code_position()->value());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001280 it.ClearBreakPoint(break_point_object);
1281
1282 // If there are no more break points left remove the debug info for this
1283 // function.
1284 if (debug_info->GetBreakPointCount() == 0) {
1285 RemoveDebugInfo(debug_info);
1286 }
1287
1288 return;
1289 }
1290 node = node->next();
1291 }
1292}
1293
1294
ager@chromium.org381abbb2009-02-25 13:23:22 +00001295void Debug::ClearAllBreakPoints() {
1296 DebugInfoListNode* node = debug_info_list_;
1297 while (node != NULL) {
1298 // Remove all debug break code.
1299 BreakLocationIterator it(node->debug_info(), ALL_BREAK_LOCATIONS);
1300 it.ClearAllDebugBreak();
1301 node = node->next();
1302 }
1303
1304 // Remove all debug info.
1305 while (debug_info_list_ != NULL) {
1306 RemoveDebugInfo(debug_info_list_->debug_info());
1307 }
1308}
1309
1310
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001311void Debug::FloodWithOneShot(Handle<JSFunction> function) {
lrn@chromium.org34e60782011-09-15 07:25:40 +00001312 PrepareForBreakPoints();
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001313
1314 // Make sure the function is compiled and has set up the debug info.
1315 Handle<SharedFunctionInfo> shared(function->shared());
1316 if (!EnsureDebugInfo(shared, function)) {
kasper.lundbd3ec4e2008-07-09 11:06:54 +00001317 // Return if we failed to retrieve the debug info.
1318 return;
1319 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001320
1321 // Flood the function with break points.
kasper.lundbd3ec4e2008-07-09 11:06:54 +00001322 BreakLocationIterator it(GetDebugInfo(shared), ALL_BREAK_LOCATIONS);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001323 while (!it.Done()) {
1324 it.SetOneShot();
1325 it.Next();
1326 }
1327}
1328
1329
rossberg@chromium.org2c067b12012-03-19 11:01:52 +00001330void Debug::FloodBoundFunctionWithOneShot(Handle<JSFunction> function) {
1331 Handle<FixedArray> new_bindings(function->function_bindings());
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00001332 Handle<Object> bindee(new_bindings->get(JSFunction::kBoundFunctionIndex),
1333 isolate_);
rossberg@chromium.org2c067b12012-03-19 11:01:52 +00001334
1335 if (!bindee.is_null() && bindee->IsJSFunction() &&
1336 !JSFunction::cast(*bindee)->IsBuiltin()) {
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001337 Handle<JSFunction> bindee_function(JSFunction::cast(*bindee));
1338 Debug::FloodWithOneShot(bindee_function);
rossberg@chromium.org2c067b12012-03-19 11:01:52 +00001339 }
1340}
1341
1342
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001343void Debug::FloodHandlerWithOneShot() {
ager@chromium.org8bb60582008-12-11 12:02:20 +00001344 // Iterate through the JavaScript stack looking for handlers.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001345 StackFrame::Id id = break_frame_id();
ager@chromium.org8bb60582008-12-11 12:02:20 +00001346 if (id == StackFrame::NO_ID) {
1347 // If there is no JavaScript stack don't do anything.
1348 return;
1349 }
vegorov@chromium.org74f333b2011-04-06 11:17:46 +00001350 for (JavaScriptFrameIterator it(isolate_, id); !it.done(); it.Advance()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001351 JavaScriptFrame* frame = it.frame();
1352 if (frame->HasHandler()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001353 // Flood the function with the catch block with break points
danno@chromium.org169691d2013-07-15 08:01:13 +00001354 FloodWithOneShot(Handle<JSFunction>(frame->function()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001355 return;
1356 }
1357 }
1358}
1359
1360
1361void Debug::ChangeBreakOnException(ExceptionBreakType type, bool enable) {
1362 if (type == BreakUncaughtException) {
1363 break_on_uncaught_exception_ = enable;
1364 } else {
1365 break_on_exception_ = enable;
1366 }
1367}
1368
1369
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00001370bool Debug::IsBreakOnException(ExceptionBreakType type) {
1371 if (type == BreakUncaughtException) {
1372 return break_on_uncaught_exception_;
1373 } else {
1374 return break_on_exception_;
1375 }
1376}
1377
1378
dslomov@chromium.org639bac02013-09-09 11:58:54 +00001379void Debug::PrepareStep(StepAction step_action,
1380 int step_count,
1381 StackFrame::Id frame_id) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00001382 HandleScope scope(isolate_);
lrn@chromium.org34e60782011-09-15 07:25:40 +00001383
1384 PrepareForBreakPoints();
1385
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001386 ASSERT(Debug::InDebugger());
1387
1388 // Remember this step action and count.
1389 thread_local_.last_step_action_ = step_action;
ager@chromium.orga1645e22009-09-09 19:27:10 +00001390 if (step_action == StepOut) {
1391 // For step out target frame will be found on the stack so there is no need
1392 // to set step counter for it. It's expected to always be 0 for StepOut.
1393 thread_local_.step_count_ = 0;
1394 } else {
1395 thread_local_.step_count_ = step_count;
1396 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001397
1398 // Get the frame where the execution has stopped and skip the debug frame if
1399 // any. The debug frame will only be present if execution was stopped due to
1400 // hitting a break point. In other situations (e.g. unhandled exception) the
1401 // debug frame is not present.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001402 StackFrame::Id id = break_frame_id();
ager@chromium.org8bb60582008-12-11 12:02:20 +00001403 if (id == StackFrame::NO_ID) {
1404 // If there is no JavaScript stack don't do anything.
1405 return;
1406 }
dslomov@chromium.org639bac02013-09-09 11:58:54 +00001407 if (frame_id != StackFrame::NO_ID) {
1408 id = frame_id;
1409 }
vegorov@chromium.org74f333b2011-04-06 11:17:46 +00001410 JavaScriptFrameIterator frames_it(isolate_, id);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001411 JavaScriptFrame* frame = frames_it.frame();
1412
1413 // First of all ensure there is one-shot break points in the top handler
1414 // if any.
1415 FloodHandlerWithOneShot();
1416
1417 // If the function on the top frame is unresolved perform step out. This will
1418 // be the case when calling unknown functions and having the debugger stopped
1419 // in an unhandled exception.
1420 if (!frame->function()->IsJSFunction()) {
1421 // Step out: Find the calling JavaScript frame and flood it with
1422 // breakpoints.
1423 frames_it.Advance();
1424 // Fill the function to return to with one-shot break points.
danno@chromium.org169691d2013-07-15 08:01:13 +00001425 JSFunction* function = frames_it.frame()->function();
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001426 FloodWithOneShot(Handle<JSFunction>(function));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001427 return;
1428 }
1429
1430 // Get the debug info (create it if it does not exist).
danno@chromium.org169691d2013-07-15 08:01:13 +00001431 Handle<JSFunction> function(frame->function());
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001432 Handle<SharedFunctionInfo> shared(function->shared());
1433 if (!EnsureDebugInfo(shared, function)) {
kasper.lundbd3ec4e2008-07-09 11:06:54 +00001434 // Return if ensuring debug info failed.
1435 return;
1436 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001437 Handle<DebugInfo> debug_info = GetDebugInfo(shared);
1438
1439 // Find the break location where execution has stopped.
1440 BreakLocationIterator it(debug_info, ALL_BREAK_LOCATIONS);
verwaest@chromium.org8a00e822013-06-10 15:11:22 +00001441 // pc points to the instruction after the current one, possibly a break
1442 // location as well. So the "- 1" to exclude it from the search.
1443 it.FindBreakLocationFromAddress(frame->pc() - 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001444
1445 // Compute whether or not the target is a call target.
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001446 bool is_load_or_store = false;
1447 bool is_inline_cache_stub = false;
whesse@chromium.orge90029b2010-08-02 11:52:17 +00001448 bool is_at_restarted_function = false;
ager@chromium.orga1645e22009-09-09 19:27:10 +00001449 Handle<Code> call_function_stub;
ager@chromium.orga1645e22009-09-09 19:27:10 +00001450
whesse@chromium.orge90029b2010-08-02 11:52:17 +00001451 if (thread_local_.restarter_frame_function_pointer_ == NULL) {
1452 if (RelocInfo::IsCodeTarget(it.rinfo()->rmode())) {
1453 bool is_call_target = false;
1454 Address target = it.rinfo()->target_address();
1455 Code* code = Code::GetCodeFromTargetAddress(target);
1456 if (code->is_call_stub() || code->is_keyed_call_stub()) {
1457 is_call_target = true;
1458 }
1459 if (code->is_inline_cache_stub()) {
1460 is_inline_cache_stub = true;
1461 is_load_or_store = !is_call_target;
1462 }
1463
1464 // Check if target code is CallFunction stub.
1465 Code* maybe_call_function_stub = code;
1466 // If there is a breakpoint at this line look at the original code to
1467 // check if it is a CallFunction stub.
1468 if (it.IsDebugBreak()) {
1469 Address original_target = it.original_rinfo()->target_address();
1470 maybe_call_function_stub =
1471 Code::GetCodeFromTargetAddress(original_target);
1472 }
1473 if (maybe_call_function_stub->kind() == Code::STUB &&
1474 maybe_call_function_stub->major_key() == CodeStub::CallFunction) {
1475 // Save reference to the code as we may need it to find out arguments
1476 // count for 'step in' later.
1477 call_function_stub = Handle<Code>(maybe_call_function_stub);
1478 }
ager@chromium.orga1645e22009-09-09 19:27:10 +00001479 }
whesse@chromium.orge90029b2010-08-02 11:52:17 +00001480 } else {
1481 is_at_restarted_function = true;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001482 }
1483
v8.team.kasperl727e9952008-09-02 14:56:44 +00001484 // If this is the last break code target step out is the only possibility.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001485 if (it.IsExit() || step_action == StepOut) {
ager@chromium.orga1645e22009-09-09 19:27:10 +00001486 if (step_action == StepOut) {
1487 // Skip step_count frames starting with the current one.
1488 while (step_count-- > 0 && !frames_it.done()) {
1489 frames_it.Advance();
1490 }
1491 } else {
1492 ASSERT(it.IsExit());
1493 frames_it.Advance();
1494 }
1495 // Skip builtin functions on the stack.
danno@chromium.org169691d2013-07-15 08:01:13 +00001496 while (!frames_it.done() && frames_it.frame()->function()->IsBuiltin()) {
ager@chromium.orga1645e22009-09-09 19:27:10 +00001497 frames_it.Advance();
1498 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001499 // Step out: If there is a JavaScript caller frame, we need to
1500 // flood it with breakpoints.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001501 if (!frames_it.done()) {
1502 // Fill the function to return to with one-shot break points.
danno@chromium.org169691d2013-07-15 08:01:13 +00001503 JSFunction* function = frames_it.frame()->function();
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001504 FloodWithOneShot(Handle<JSFunction>(function));
ager@chromium.orga1645e22009-09-09 19:27:10 +00001505 // Set target frame pointer.
1506 ActivateStepOut(frames_it.frame());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001507 }
ager@chromium.orga1645e22009-09-09 19:27:10 +00001508 } else if (!(is_inline_cache_stub || RelocInfo::IsConstructCall(it.rmode()) ||
whesse@chromium.orge90029b2010-08-02 11:52:17 +00001509 !call_function_stub.is_null() || is_at_restarted_function)
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001510 || step_action == StepNext || step_action == StepMin) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001511 // Step next or step min.
1512
1513 // Fill the current function with one-shot break points.
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001514 FloodWithOneShot(function);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001515
1516 // Remember source position and frame to handle step next.
1517 thread_local_.last_statement_position_ =
1518 debug_info->code()->SourceStatementPosition(frame->pc());
mmassi@chromium.org7028c052012-06-13 11:51:58 +00001519 thread_local_.last_fp_ = frame->UnpaddedFP();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001520 } else {
whesse@chromium.orge90029b2010-08-02 11:52:17 +00001521 // If there's restarter frame on top of the stack, just get the pointer
1522 // to function which is going to be restarted.
1523 if (is_at_restarted_function) {
1524 Handle<JSFunction> restarted_function(
1525 JSFunction::cast(*thread_local_.restarter_frame_function_pointer_));
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001526 FloodWithOneShot(restarted_function);
whesse@chromium.orge90029b2010-08-02 11:52:17 +00001527 } else if (!call_function_stub.is_null()) {
1528 // If it's CallFunction stub ensure target function is compiled and flood
1529 // it with one shot breakpoints.
1530
ager@chromium.orga1645e22009-09-09 19:27:10 +00001531 // Find out number of arguments from the stub minor key.
1532 // Reverse lookup required as the minor key cannot be retrieved
1533 // from the code object.
1534 Handle<Object> obj(
lrn@chromium.org7516f052011-03-30 08:52:27 +00001535 isolate_->heap()->code_stubs()->SlowReverseLookup(
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00001536 *call_function_stub),
1537 isolate_);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001538 ASSERT(!obj.is_null());
1539 ASSERT(!(*obj)->IsUndefined());
ager@chromium.orga1645e22009-09-09 19:27:10 +00001540 ASSERT(obj->IsSmi());
1541 // Get the STUB key and extract major and minor key.
1542 uint32_t key = Smi::cast(*obj)->value();
1543 // Argc in the stub is the number of arguments passed - not the
1544 // expected arguments of the called function.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001545 int call_function_arg_count =
1546 CallFunctionStub::ExtractArgcFromMinorKey(
1547 CodeStub::MinorKeyFromKey(key));
ager@chromium.orga1645e22009-09-09 19:27:10 +00001548 ASSERT(call_function_stub->major_key() ==
1549 CodeStub::MajorKeyFromKey(key));
1550
1551 // Find target function on the expression stack.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001552 // Expression stack looks like this (top to bottom):
ager@chromium.orga1645e22009-09-09 19:27:10 +00001553 // argN
1554 // ...
1555 // arg0
1556 // Receiver
1557 // Function to call
1558 int expressions_count = frame->ComputeExpressionsCount();
1559 ASSERT(expressions_count - 2 - call_function_arg_count >= 0);
1560 Object* fun = frame->GetExpression(
1561 expressions_count - 2 - call_function_arg_count);
1562 if (fun->IsJSFunction()) {
1563 Handle<JSFunction> js_function(JSFunction::cast(fun));
rossberg@chromium.org2c067b12012-03-19 11:01:52 +00001564 if (js_function->shared()->bound()) {
1565 Debug::FloodBoundFunctionWithOneShot(js_function);
1566 } else if (!js_function->IsBuiltin()) {
1567 // Don't step into builtins.
ager@chromium.orga1645e22009-09-09 19:27:10 +00001568 // It will also compile target function if it's not compiled yet.
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001569 FloodWithOneShot(js_function);
ager@chromium.orga1645e22009-09-09 19:27:10 +00001570 }
1571 }
1572 }
1573
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001574 // Fill the current function with one-shot break points even for step in on
1575 // a call target as the function called might be a native function for
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001576 // which step in will not stop. It also prepares for stepping in
1577 // getters/setters.
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001578 FloodWithOneShot(function);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001579
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001580 if (is_load_or_store) {
1581 // Remember source position and frame to handle step in getter/setter. If
1582 // there is a custom getter/setter it will be handled in
1583 // Object::Get/SetPropertyWithCallback, otherwise the step action will be
1584 // propagated on the next Debug::Break.
1585 thread_local_.last_statement_position_ =
1586 debug_info->code()->SourceStatementPosition(frame->pc());
mmassi@chromium.org7028c052012-06-13 11:51:58 +00001587 thread_local_.last_fp_ = frame->UnpaddedFP();
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001588 }
1589
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001590 // Step in or Step in min
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00001591 it.PrepareStepIn(isolate_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001592 ActivateStepIn(frame);
1593 }
1594}
1595
1596
1597// Check whether the current debug break should be reported to the debugger. It
1598// is used to have step next and step in only report break back to the debugger
1599// if on a different frame or in a different statement. In some situations
1600// there will be several break points in the same statement when the code is
v8.team.kasperl727e9952008-09-02 14:56:44 +00001601// flooded with one-shot break points. This function helps to perform several
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001602// steps before reporting break back to the debugger.
1603bool Debug::StepNextContinue(BreakLocationIterator* break_location_iterator,
1604 JavaScriptFrame* frame) {
lrn@chromium.org34e60782011-09-15 07:25:40 +00001605 // StepNext and StepOut shouldn't bring us deeper in code, so last frame
1606 // shouldn't be a parent of current frame.
1607 if (thread_local_.last_step_action_ == StepNext ||
1608 thread_local_.last_step_action_ == StepOut) {
1609 if (frame->fp() < thread_local_.last_fp_) return true;
1610 }
1611
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001612 // If the step last action was step next or step in make sure that a new
1613 // statement is hit.
1614 if (thread_local_.last_step_action_ == StepNext ||
1615 thread_local_.last_step_action_ == StepIn) {
1616 // Never continue if returning from function.
1617 if (break_location_iterator->IsExit()) return false;
1618
1619 // Continue if we are still on the same frame and in the same statement.
1620 int current_statement_position =
1621 break_location_iterator->code()->SourceStatementPosition(frame->pc());
mmassi@chromium.org7028c052012-06-13 11:51:58 +00001622 return thread_local_.last_fp_ == frame->UnpaddedFP() &&
ager@chromium.org236ad962008-09-25 09:45:57 +00001623 thread_local_.last_statement_position_ == current_statement_position;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001624 }
1625
1626 // No step next action - don't continue.
1627 return false;
1628}
1629
1630
1631// Check whether the code object at the specified address is a debug break code
1632// object.
1633bool Debug::IsDebugBreak(Address addr) {
ager@chromium.org8bb60582008-12-11 12:02:20 +00001634 Code* code = Code::GetCodeFromTargetAddress(addr);
verwaest@chromium.orgec6855e2013-08-22 12:26:58 +00001635 return code->is_debug_stub() && code->extra_ic_state() == DEBUG_BREAK;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001636}
1637
1638
1639// Check whether a code stub with the specified major key is a possible break
1640// point location when looking for source break locations.
1641bool Debug::IsSourceBreakStub(Code* code) {
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001642 CodeStub::Major major_key = CodeStub::GetMajorKey(code);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001643 return major_key == CodeStub::CallFunction;
1644}
1645
1646
1647// Check whether a code stub with the specified major key is a possible break
1648// location.
1649bool Debug::IsBreakStub(Code* code) {
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001650 CodeStub::Major major_key = CodeStub::GetMajorKey(code);
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00001651 return major_key == CodeStub::CallFunction;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001652}
1653
1654
1655// Find the builtin to use for invoking the debug break
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001656Handle<Code> Debug::FindDebugBreak(Handle<Code> code, RelocInfo::Mode mode) {
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +00001657 Isolate* isolate = code->GetIsolate();
danno@chromium.orgfa458e42012-02-01 10:48:36 +00001658
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001659 // Find the builtin debug break function matching the calling convention
1660 // used by the call site.
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001661 if (code->is_inline_cache_stub()) {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001662 switch (code->kind()) {
1663 case Code::CALL_IC:
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001664 case Code::KEYED_CALL_IC:
danno@chromium.orgfa458e42012-02-01 10:48:36 +00001665 return isolate->stub_cache()->ComputeCallDebugBreak(
1666 code->arguments_count(), code->kind());
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001667
1668 case Code::LOAD_IC:
danno@chromium.orgfa458e42012-02-01 10:48:36 +00001669 return isolate->builtins()->LoadIC_DebugBreak();
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001670
1671 case Code::STORE_IC:
danno@chromium.orgfa458e42012-02-01 10:48:36 +00001672 return isolate->builtins()->StoreIC_DebugBreak();
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001673
1674 case Code::KEYED_LOAD_IC:
danno@chromium.orgfa458e42012-02-01 10:48:36 +00001675 return isolate->builtins()->KeyedLoadIC_DebugBreak();
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001676
1677 case Code::KEYED_STORE_IC:
danno@chromium.orgfa458e42012-02-01 10:48:36 +00001678 return isolate->builtins()->KeyedStoreIC_DebugBreak();
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001679
danno@chromium.orgf005df62013-04-30 16:36:45 +00001680 case Code::COMPARE_NIL_IC:
1681 return isolate->builtins()->CompareNilIC_DebugBreak();
1682
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001683 default:
1684 UNREACHABLE();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001685 }
1686 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001687 if (RelocInfo::IsConstructCall(mode)) {
danno@chromium.orgfa458e42012-02-01 10:48:36 +00001688 if (code->has_function_cache()) {
1689 return isolate->builtins()->CallConstructStub_Recording_DebugBreak();
1690 } else {
1691 return isolate->builtins()->CallConstructStub_DebugBreak();
1692 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001693 }
1694 if (code->kind() == Code::STUB) {
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00001695 ASSERT(code->major_key() == CodeStub::CallFunction);
danno@chromium.orgfa458e42012-02-01 10:48:36 +00001696 if (code->has_function_cache()) {
1697 return isolate->builtins()->CallFunctionStub_Recording_DebugBreak();
1698 } else {
1699 return isolate->builtins()->CallFunctionStub_DebugBreak();
1700 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001701 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001702
1703 UNREACHABLE();
1704 return Handle<Code>::null();
1705}
1706
1707
1708// Simple function for returning the source positions for active break points.
1709Handle<Object> Debug::GetSourceBreakLocations(
jkummerow@chromium.org93a47f42013-07-02 14:43:41 +00001710 Handle<SharedFunctionInfo> shared,
1711 BreakPositionAlignment position_alignment) {
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +00001712 Isolate* isolate = shared->GetIsolate();
lrn@chromium.org7516f052011-03-30 08:52:27 +00001713 Heap* heap = isolate->heap();
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00001714 if (!HasDebugInfo(shared)) {
1715 return Handle<Object>(heap->undefined_value(), isolate);
1716 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001717 Handle<DebugInfo> debug_info = GetDebugInfo(shared);
1718 if (debug_info->GetBreakPointCount() == 0) {
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00001719 return Handle<Object>(heap->undefined_value(), isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001720 }
1721 Handle<FixedArray> locations =
lrn@chromium.org7516f052011-03-30 08:52:27 +00001722 isolate->factory()->NewFixedArray(debug_info->GetBreakPointCount());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001723 int count = 0;
1724 for (int i = 0; i < debug_info->break_points()->length(); i++) {
1725 if (!debug_info->break_points()->get(i)->IsUndefined()) {
1726 BreakPointInfo* break_point_info =
1727 BreakPointInfo::cast(debug_info->break_points()->get(i));
1728 if (break_point_info->GetBreakPointCount() > 0) {
jkummerow@chromium.org93a47f42013-07-02 14:43:41 +00001729 Smi* position;
1730 switch (position_alignment) {
1731 case STATEMENT_ALIGNED:
1732 position = break_point_info->statement_position();
1733 break;
1734 case BREAK_POSITION_ALIGNED:
1735 position = break_point_info->source_position();
1736 break;
1737 default:
1738 UNREACHABLE();
1739 position = break_point_info->statement_position();
1740 }
1741
1742 locations->set(count++, position);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001743 }
1744 }
1745 }
1746 return locations;
1747}
1748
1749
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001750void Debug::NewBreak(StackFrame::Id break_frame_id) {
1751 thread_local_.break_frame_id_ = break_frame_id;
1752 thread_local_.break_id_ = ++thread_local_.break_count_;
1753}
1754
1755
1756void Debug::SetBreak(StackFrame::Id break_frame_id, int break_id) {
1757 thread_local_.break_frame_id_ = break_frame_id;
1758 thread_local_.break_id_ = break_id;
1759}
1760
1761
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001762// Handle stepping into a function.
1763void Debug::HandleStepIn(Handle<JSFunction> function,
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001764 Handle<Object> holder,
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001765 Address fp,
1766 bool is_constructor) {
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00001767 Isolate* isolate = function->GetIsolate();
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001768 // If the frame pointer is not supplied by the caller find it.
1769 if (fp == 0) {
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00001770 StackFrameIterator it(isolate);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001771 it.Advance();
1772 // For constructor functions skip another frame.
1773 if (is_constructor) {
1774 ASSERT(it.frame()->is_construct());
1775 it.Advance();
1776 }
1777 fp = it.frame()->fp();
1778 }
1779
1780 // Flood the function with one-shot break points if it is called from where
1781 // step into was requested.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001782 if (fp == step_in_fp()) {
rossberg@chromium.org2c067b12012-03-19 11:01:52 +00001783 if (function->shared()->bound()) {
1784 // Handle Function.prototype.bind
1785 Debug::FloodBoundFunctionWithOneShot(function);
1786 } else if (!function->IsBuiltin()) {
1787 // Don't allow step into functions in the native context.
kasperl@chromium.orgacae3782009-04-11 09:17:08 +00001788 if (function->shared()->code() ==
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00001789 isolate->builtins()->builtin(Builtins::kFunctionApply) ||
kasperl@chromium.orgacae3782009-04-11 09:17:08 +00001790 function->shared()->code() ==
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00001791 isolate->builtins()->builtin(Builtins::kFunctionCall)) {
kasperl@chromium.orgacae3782009-04-11 09:17:08 +00001792 // Handle function.apply and function.call separately to flood the
1793 // function to be called and not the code for Builtins::FunctionApply or
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001794 // Builtins::FunctionCall. The receiver of call/apply is the target
1795 // function.
machenbach@chromium.org528ce022013-09-23 14:09:36 +00001796 if (!holder.is_null() && holder->IsJSFunction()) {
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001797 Handle<JSFunction> js_function = Handle<JSFunction>::cast(holder);
machenbach@chromium.org528ce022013-09-23 14:09:36 +00001798 if (!js_function->IsBuiltin()) {
1799 Debug::FloodWithOneShot(js_function);
1800 } else if (js_function->shared()->bound()) {
1801 // Handle Function.prototype.bind
1802 Debug::FloodBoundFunctionWithOneShot(js_function);
1803 }
kasperl@chromium.orgacae3782009-04-11 09:17:08 +00001804 }
1805 } else {
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001806 Debug::FloodWithOneShot(function);
kasperl@chromium.orgacae3782009-04-11 09:17:08 +00001807 }
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001808 }
1809 }
1810}
1811
1812
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001813void Debug::ClearStepping() {
1814 // Clear the various stepping setup.
1815 ClearOneShot();
1816 ClearStepIn();
ager@chromium.orga1645e22009-09-09 19:27:10 +00001817 ClearStepOut();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001818 ClearStepNext();
1819
1820 // Clear multiple step counter.
1821 thread_local_.step_count_ = 0;
1822}
1823
mstarzinger@chromium.orge0e1b0d2013-07-08 08:38:06 +00001824
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001825// Clears all the one-shot break points that are currently set. Normally this
1826// function is called each time a break point is hit as one shot break points
1827// are used to support stepping.
1828void Debug::ClearOneShot() {
1829 // The current implementation just runs through all the breakpoints. When the
v8.team.kasperl727e9952008-09-02 14:56:44 +00001830 // last break point for a function is removed that function is automatically
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001831 // removed from the list.
1832
1833 DebugInfoListNode* node = debug_info_list_;
1834 while (node != NULL) {
1835 BreakLocationIterator it(node->debug_info(), ALL_BREAK_LOCATIONS);
1836 while (!it.Done()) {
1837 it.ClearOneShot();
1838 it.Next();
1839 }
1840 node = node->next();
1841 }
1842}
1843
1844
1845void Debug::ActivateStepIn(StackFrame* frame) {
ager@chromium.orga1645e22009-09-09 19:27:10 +00001846 ASSERT(!StepOutActive());
mmassi@chromium.org7028c052012-06-13 11:51:58 +00001847 thread_local_.step_into_fp_ = frame->UnpaddedFP();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001848}
1849
1850
1851void Debug::ClearStepIn() {
1852 thread_local_.step_into_fp_ = 0;
1853}
1854
1855
ager@chromium.orga1645e22009-09-09 19:27:10 +00001856void Debug::ActivateStepOut(StackFrame* frame) {
1857 ASSERT(!StepInActive());
mmassi@chromium.org7028c052012-06-13 11:51:58 +00001858 thread_local_.step_out_fp_ = frame->UnpaddedFP();
ager@chromium.orga1645e22009-09-09 19:27:10 +00001859}
1860
1861
1862void Debug::ClearStepOut() {
1863 thread_local_.step_out_fp_ = 0;
1864}
1865
1866
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001867void Debug::ClearStepNext() {
1868 thread_local_.last_step_action_ = StepNone;
ager@chromium.org236ad962008-09-25 09:45:57 +00001869 thread_local_.last_statement_position_ = RelocInfo::kNoPosition;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001870 thread_local_.last_fp_ = 0;
1871}
1872
1873
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001874// Helper function to compile full code for debugging. This code will
mmassi@chromium.org7028c052012-06-13 11:51:58 +00001875// have debug break slots and deoptimization information. Deoptimization
1876// information is required in case that an optimized version of this
1877// function is still activated on the stack. It will also make sure that
1878// the full code is compiled with the same flags as the previous version,
1879// that is flags which can change the code generated. The current method
1880// of mapping from already compiled full code without debug break slots
1881// to full code with debug break slots depends on the generated code is
1882// otherwise exactly the same.
1883static bool CompileFullCodeForDebugging(Handle<JSFunction> function,
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001884 Handle<Code> current_code) {
1885 ASSERT(!current_code->has_debug_break_slots());
1886
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001887 CompilationInfoWithZone info(function);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001888 info.MarkCompilingForDebugging(current_code);
1889 ASSERT(!info.shared_info()->is_compiled());
1890 ASSERT(!info.isolate()->has_pending_exception());
1891
1892 // Use compile lazy which will end up compiling the full code in the
1893 // configuration configured above.
1894 bool result = Compiler::CompileLazy(&info);
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +00001895 ASSERT(result != info.isolate()->has_pending_exception());
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001896 info.isolate()->clear_pending_exception();
1897#if DEBUG
1898 if (result) {
mmassi@chromium.org7028c052012-06-13 11:51:58 +00001899 Handle<Code> new_code(function->shared()->code());
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001900 ASSERT(new_code->has_debug_break_slots());
1901 ASSERT(current_code->is_compiled_optimizable() ==
1902 new_code->is_compiled_optimizable());
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001903 }
1904#endif
1905 return result;
1906}
1907
1908
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001909static void CollectActiveFunctionsFromThread(
1910 Isolate* isolate,
1911 ThreadLocalTop* top,
1912 List<Handle<JSFunction> >* active_functions,
1913 Object* active_code_marker) {
1914 // Find all non-optimized code functions with activation frames
1915 // on the stack. This includes functions which have optimized
1916 // activations (including inlined functions) on the stack as the
1917 // non-optimized code is needed for the lazy deoptimization.
1918 for (JavaScriptFrameIterator it(isolate, top); !it.done(); it.Advance()) {
1919 JavaScriptFrame* frame = it.frame();
1920 if (frame->is_optimized()) {
jkummerow@chromium.orgba72ec82013-07-22 09:21:20 +00001921 List<JSFunction*> functions(FLAG_max_inlining_levels + 1);
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001922 frame->GetFunctions(&functions);
1923 for (int i = 0; i < functions.length(); i++) {
1924 JSFunction* function = functions[i];
1925 active_functions->Add(Handle<JSFunction>(function));
1926 function->shared()->code()->set_gc_metadata(active_code_marker);
1927 }
1928 } else if (frame->function()->IsJSFunction()) {
danno@chromium.org169691d2013-07-15 08:01:13 +00001929 JSFunction* function = frame->function();
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001930 ASSERT(frame->LookupCode()->kind() == Code::FUNCTION);
1931 active_functions->Add(Handle<JSFunction>(function));
1932 function->shared()->code()->set_gc_metadata(active_code_marker);
1933 }
1934 }
1935}
1936
1937
1938static void RedirectActivationsToRecompiledCodeOnThread(
1939 Isolate* isolate,
1940 ThreadLocalTop* top) {
1941 for (JavaScriptFrameIterator it(isolate, top); !it.done(); it.Advance()) {
1942 JavaScriptFrame* frame = it.frame();
1943
1944 if (frame->is_optimized() || !frame->function()->IsJSFunction()) continue;
1945
danno@chromium.org169691d2013-07-15 08:01:13 +00001946 JSFunction* function = frame->function();
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001947
1948 ASSERT(frame->LookupCode()->kind() == Code::FUNCTION);
1949
1950 Handle<Code> frame_code(frame->LookupCode());
1951 if (frame_code->has_debug_break_slots()) continue;
1952
1953 Handle<Code> new_code(function->shared()->code());
1954 if (new_code->kind() != Code::FUNCTION ||
1955 !new_code->has_debug_break_slots()) {
1956 continue;
1957 }
1958
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001959 // Iterate over the RelocInfo in the original code to compute the sum of the
1960 // constant pools sizes. (See Assembler::CheckConstPool())
1961 // Note that this is only useful for architectures using constant pools.
1962 int constpool_mask = RelocInfo::ModeMask(RelocInfo::CONST_POOL);
1963 int frame_const_pool_size = 0;
1964 for (RelocIterator it(*frame_code, constpool_mask); !it.done(); it.next()) {
1965 RelocInfo* info = it.rinfo();
1966 if (info->pc() >= frame->pc()) break;
1967 frame_const_pool_size += static_cast<int>(info->data());
1968 }
1969 intptr_t frame_offset =
1970 frame->pc() - frame_code->instruction_start() - frame_const_pool_size;
1971
1972 // Iterate over the RelocInfo for new code to find the number of bytes
1973 // generated for debug slots and constant pools.
1974 int debug_break_slot_bytes = 0;
1975 int new_code_const_pool_size = 0;
1976 int mask = RelocInfo::ModeMask(RelocInfo::DEBUG_BREAK_SLOT) |
1977 RelocInfo::ModeMask(RelocInfo::CONST_POOL);
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001978 for (RelocIterator it(*new_code, mask); !it.done(); it.next()) {
1979 // Check if the pc in the new code with debug break
1980 // slots is before this slot.
1981 RelocInfo* info = it.rinfo();
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001982 intptr_t new_offset = info->pc() - new_code->instruction_start() -
1983 new_code_const_pool_size - debug_break_slot_bytes;
1984 if (new_offset >= frame_offset) {
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001985 break;
1986 }
1987
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001988 if (RelocInfo::IsDebugBreakSlot(info->rmode())) {
1989 debug_break_slot_bytes += Assembler::kDebugBreakSlotLength;
1990 } else {
1991 ASSERT(RelocInfo::IsConstPool(info->rmode()));
1992 // The size of the constant pool is encoded in the data.
1993 new_code_const_pool_size += static_cast<int>(info->data());
1994 }
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001995 }
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001996
1997 // Compute the equivalent pc in the new code.
1998 byte* new_pc = new_code->instruction_start() + frame_offset +
1999 debug_break_slot_bytes + new_code_const_pool_size;
2000
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002001 if (FLAG_trace_deopt) {
2002 PrintF("Replacing code %08" V8PRIxPTR " - %08" V8PRIxPTR " (%d) "
2003 "with %08" V8PRIxPTR " - %08" V8PRIxPTR " (%d) "
2004 "for debugging, "
2005 "changing pc from %08" V8PRIxPTR " to %08" V8PRIxPTR "\n",
2006 reinterpret_cast<intptr_t>(
2007 frame_code->instruction_start()),
2008 reinterpret_cast<intptr_t>(
2009 frame_code->instruction_start()) +
2010 frame_code->instruction_size(),
2011 frame_code->instruction_size(),
2012 reinterpret_cast<intptr_t>(new_code->instruction_start()),
2013 reinterpret_cast<intptr_t>(new_code->instruction_start()) +
2014 new_code->instruction_size(),
2015 new_code->instruction_size(),
2016 reinterpret_cast<intptr_t>(frame->pc()),
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00002017 reinterpret_cast<intptr_t>(new_pc));
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002018 }
2019
2020 // Patch the return address to return into the code with
2021 // debug break slots.
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00002022 frame->set_pc(new_pc);
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002023 }
2024}
2025
2026
2027class ActiveFunctionsCollector : public ThreadVisitor {
2028 public:
2029 explicit ActiveFunctionsCollector(List<Handle<JSFunction> >* active_functions,
2030 Object* active_code_marker)
2031 : active_functions_(active_functions),
2032 active_code_marker_(active_code_marker) { }
2033
2034 void VisitThread(Isolate* isolate, ThreadLocalTop* top) {
2035 CollectActiveFunctionsFromThread(isolate,
2036 top,
2037 active_functions_,
2038 active_code_marker_);
2039 }
2040
2041 private:
2042 List<Handle<JSFunction> >* active_functions_;
2043 Object* active_code_marker_;
2044};
2045
2046
2047class ActiveFunctionsRedirector : public ThreadVisitor {
2048 public:
2049 void VisitThread(Isolate* isolate, ThreadLocalTop* top) {
2050 RedirectActivationsToRecompiledCodeOnThread(isolate, top);
2051 }
2052};
2053
2054
lrn@chromium.org34e60782011-09-15 07:25:40 +00002055void Debug::PrepareForBreakPoints() {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002056 // If preparing for the first break point make sure to deoptimize all
2057 // functions as debugging does not work with optimized code.
2058 if (!has_break_points_) {
machenbach@chromium.org9af454f2013-11-20 09:25:57 +00002059 if (isolate_->concurrent_recompilation_enabled()) {
danno@chromium.org59400602013-08-13 17:09:37 +00002060 isolate_->optimizing_compiler_thread()->Flush();
2061 }
2062
svenpanne@chromium.org876cca82013-03-18 14:43:20 +00002063 Deoptimizer::DeoptimizeAll(isolate_);
lrn@chromium.org34e60782011-09-15 07:25:40 +00002064
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002065 Handle<Code> lazy_compile =
2066 Handle<Code>(isolate_->builtins()->builtin(Builtins::kLazyCompile));
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002067
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00002068 // There will be at least one break point when we are done.
2069 has_break_points_ = true;
2070
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002071 // Keep the list of activated functions in a handlified list as it
2072 // is used both in GC and non-GC code.
2073 List<Handle<JSFunction> > active_functions(100);
lrn@chromium.org34e60782011-09-15 07:25:40 +00002074
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002075 {
2076 // We are going to iterate heap to find all functions without
2077 // debug break slots.
hpayer@chromium.org7c3372b2013-02-13 17:26:04 +00002078 Heap* heap = isolate_->heap();
2079 heap->CollectAllGarbage(Heap::kMakeHeapIterableMask,
2080 "preparing for breakpoints");
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002081
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002082 // Ensure no GC in this scope as we are going to use gc_metadata
2083 // field in the Code object to mark active functions.
rossberg@chromium.org79e79022013-06-03 15:43:46 +00002084 DisallowHeapAllocation no_allocation;
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002085
hpayer@chromium.org7c3372b2013-02-13 17:26:04 +00002086 Object* active_code_marker = heap->the_hole_value();
svenpanne@chromium.orgecb9dd62011-12-01 08:22:35 +00002087
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002088 CollectActiveFunctionsFromThread(isolate_,
2089 isolate_->thread_local_top(),
2090 &active_functions,
2091 active_code_marker);
2092 ActiveFunctionsCollector active_functions_collector(&active_functions,
2093 active_code_marker);
2094 isolate_->thread_manager()->IterateArchivedThreads(
2095 &active_functions_collector);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002096
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002097 // Scan the heap for all non-optimized functions which have no
2098 // debug break slots and are not active or inlined into an active
2099 // function and mark them for lazy compilation.
hpayer@chromium.org7c3372b2013-02-13 17:26:04 +00002100 HeapIterator iterator(heap);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002101 HeapObject* obj = NULL;
2102 while (((obj = iterator.next()) != NULL)) {
2103 if (obj->IsJSFunction()) {
2104 JSFunction* function = JSFunction::cast(obj);
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002105 SharedFunctionInfo* shared = function->shared();
mstarzinger@chromium.org1510d582013-06-28 14:00:48 +00002106
2107 if (!shared->allows_lazy_compilation()) continue;
2108 if (!shared->script()->IsScript()) continue;
bmeurer@chromium.orgc9913f02013-10-24 06:31:36 +00002109 if (function->IsBuiltin()) continue;
mstarzinger@chromium.org1510d582013-06-28 14:00:48 +00002110 if (shared->code()->gc_metadata() == active_code_marker) continue;
2111
2112 Code::Kind kind = function->code()->kind();
2113 if (kind == Code::FUNCTION &&
2114 !function->code()->has_debug_break_slots()) {
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002115 function->set_code(*lazy_compile);
2116 function->shared()->set_code(*lazy_compile);
mstarzinger@chromium.org1510d582013-06-28 14:00:48 +00002117 } else if (kind == Code::BUILTIN &&
dslomov@chromium.org4a35c5a2013-09-13 07:28:52 +00002118 (function->IsInRecompileQueue() ||
mstarzinger@chromium.org1510d582013-06-28 14:00:48 +00002119 function->IsMarkedForLazyRecompilation() ||
rossberg@chromium.org92597162013-08-23 13:28:00 +00002120 function->IsMarkedForConcurrentRecompilation())) {
mstarzinger@chromium.org1510d582013-06-28 14:00:48 +00002121 // Abort in-flight compilation.
2122 Code* shared_code = function->shared()->code();
2123 if (shared_code->kind() == Code::FUNCTION &&
2124 shared_code->has_debug_break_slots()) {
2125 function->set_code(shared_code);
2126 } else {
2127 function->set_code(*lazy_compile);
2128 function->shared()->set_code(*lazy_compile);
2129 }
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002130 }
2131 }
lrn@chromium.org34e60782011-09-15 07:25:40 +00002132 }
lrn@chromium.org34e60782011-09-15 07:25:40 +00002133
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002134 // Clear gc_metadata field.
2135 for (int i = 0; i < active_functions.length(); i++) {
2136 Handle<JSFunction> function = active_functions[i];
2137 function->shared()->code()->set_gc_metadata(Smi::FromInt(0));
2138 }
2139 }
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002140
2141 // Now recompile all functions with activation frames and and
2142 // patch the return address to run in the new compiled code.
2143 for (int i = 0; i < active_functions.length(); i++) {
2144 Handle<JSFunction> function = active_functions[i];
mmassi@chromium.org7028c052012-06-13 11:51:58 +00002145 Handle<SharedFunctionInfo> shared(function->shared());
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002146
2147 if (function->code()->kind() == Code::FUNCTION &&
2148 function->code()->has_debug_break_slots()) {
2149 // Nothing to do. Function code already had debug break slots.
2150 continue;
2151 }
2152
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002153 // If recompilation is not possible just skip it.
2154 if (shared->is_toplevel() ||
2155 !shared->allows_lazy_compilation() ||
2156 shared->code()->kind() == Code::BUILTIN) {
2157 continue;
2158 }
2159
2160 // Make sure that the shared full code is compiled with debug
2161 // break slots.
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00002162 if (!shared->code()->has_debug_break_slots()) {
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002163 // Try to compile the full code with debug break slots. If it
2164 // fails just keep the current code.
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00002165 Handle<Code> current_code(function->shared()->code());
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002166 shared->set_code(*lazy_compile);
2167 bool prev_force_debugger_active =
2168 isolate_->debugger()->force_debugger_active();
2169 isolate_->debugger()->set_force_debugger_active(true);
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00002170 ASSERT(current_code->kind() == Code::FUNCTION);
mmassi@chromium.org7028c052012-06-13 11:51:58 +00002171 CompileFullCodeForDebugging(function, current_code);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002172 isolate_->debugger()->set_force_debugger_active(
2173 prev_force_debugger_active);
2174 if (!shared->is_compiled()) {
2175 shared->set_code(*current_code);
2176 continue;
2177 }
2178 }
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002179
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002180 // Keep function code in sync with shared function info.
2181 function->set_code(shared->code());
lrn@chromium.org34e60782011-09-15 07:25:40 +00002182 }
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002183
2184 RedirectActivationsToRecompiledCodeOnThread(isolate_,
2185 isolate_->thread_local_top());
2186
2187 ActiveFunctionsRedirector active_functions_redirector;
2188 isolate_->thread_manager()->IterateArchivedThreads(
2189 &active_functions_redirector);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002190 }
lrn@chromium.org34e60782011-09-15 07:25:40 +00002191}
2192
2193
jkummerow@chromium.org78502a92012-09-06 13:50:42 +00002194Object* Debug::FindSharedFunctionInfoInScript(Handle<Script> script,
2195 int position) {
2196 // Iterate the heap looking for SharedFunctionInfo generated from the
2197 // script. The inner most SharedFunctionInfo containing the source position
2198 // for the requested break point is found.
2199 // NOTE: This might require several heap iterations. If the SharedFunctionInfo
2200 // which is found is not compiled it is compiled and the heap is iterated
2201 // again as the compilation might create inner functions from the newly
2202 // compiled function and the actual requested break point might be in one of
2203 // these functions.
2204 // NOTE: The below fix-point iteration depends on all functions that cannot be
2205 // compiled lazily without a context to not be compiled at all. Compilation
2206 // will be triggered at points where we do not need a context.
2207 bool done = false;
2208 // The current candidate for the source position:
2209 int target_start_position = RelocInfo::kNoPosition;
2210 Handle<JSFunction> target_function;
2211 Handle<SharedFunctionInfo> target;
hpayer@chromium.org7c3372b2013-02-13 17:26:04 +00002212 Heap* heap = isolate_->heap();
jkummerow@chromium.org78502a92012-09-06 13:50:42 +00002213 while (!done) {
2214 { // Extra scope for iterator and no-allocation.
hpayer@chromium.org7c3372b2013-02-13 17:26:04 +00002215 heap->EnsureHeapIsIterable();
rossberg@chromium.org79e79022013-06-03 15:43:46 +00002216 DisallowHeapAllocation no_alloc_during_heap_iteration;
hpayer@chromium.org7c3372b2013-02-13 17:26:04 +00002217 HeapIterator iterator(heap);
jkummerow@chromium.org78502a92012-09-06 13:50:42 +00002218 for (HeapObject* obj = iterator.next();
2219 obj != NULL; obj = iterator.next()) {
2220 bool found_next_candidate = false;
2221 Handle<JSFunction> function;
2222 Handle<SharedFunctionInfo> shared;
2223 if (obj->IsJSFunction()) {
2224 function = Handle<JSFunction>(JSFunction::cast(obj));
2225 shared = Handle<SharedFunctionInfo>(function->shared());
2226 ASSERT(shared->allows_lazy_compilation() || shared->is_compiled());
2227 found_next_candidate = true;
2228 } else if (obj->IsSharedFunctionInfo()) {
2229 shared = Handle<SharedFunctionInfo>(SharedFunctionInfo::cast(obj));
2230 // Skip functions that we cannot compile lazily without a context,
2231 // which is not available here, because there is no closure.
2232 found_next_candidate = shared->is_compiled() ||
2233 shared->allows_lazy_compilation_without_context();
2234 }
2235 if (!found_next_candidate) continue;
2236 if (shared->script() == *script) {
2237 // If the SharedFunctionInfo found has the requested script data and
2238 // contains the source position it is a candidate.
2239 int start_position = shared->function_token_position();
2240 if (start_position == RelocInfo::kNoPosition) {
2241 start_position = shared->start_position();
2242 }
2243 if (start_position <= position &&
2244 position <= shared->end_position()) {
2245 // If there is no candidate or this function is within the current
2246 // candidate this is the new candidate.
2247 if (target.is_null()) {
2248 target_start_position = start_position;
2249 target_function = function;
2250 target = shared;
2251 } else {
2252 if (target_start_position == start_position &&
2253 shared->end_position() == target->end_position()) {
2254 // If a top-level function contains only one function
2255 // declaration the source for the top-level and the function
2256 // is the same. In that case prefer the non top-level function.
2257 if (!shared->is_toplevel()) {
2258 target_start_position = start_position;
2259 target_function = function;
2260 target = shared;
2261 }
2262 } else if (target_start_position <= start_position &&
2263 shared->end_position() <= target->end_position()) {
2264 // This containment check includes equality as a function
2265 // inside a top-level function can share either start or end
2266 // position with the top-level function.
2267 target_start_position = start_position;
2268 target_function = function;
2269 target = shared;
2270 }
2271 }
2272 }
2273 }
2274 } // End for loop.
2275 } // End no-allocation scope.
2276
hpayer@chromium.org7c3372b2013-02-13 17:26:04 +00002277 if (target.is_null()) return heap->undefined_value();
jkummerow@chromium.org78502a92012-09-06 13:50:42 +00002278
2279 // There will be at least one break point when we are done.
2280 has_break_points_ = true;
2281
2282 // If the candidate found is compiled we are done.
2283 done = target->is_compiled();
2284 if (!done) {
2285 // If the candidate is not compiled, compile it to reveal any inner
2286 // functions which might contain the requested source position. This
2287 // will compile all inner functions that cannot be compiled without a
2288 // context, because Compiler::BuildFunctionInfo checks whether the
2289 // debugger is active.
2290 if (target_function.is_null()) {
2291 SharedFunctionInfo::CompileLazy(target, KEEP_EXCEPTION);
2292 } else {
2293 JSFunction::CompileLazy(target_function, KEEP_EXCEPTION);
2294 }
2295 }
2296 } // End while loop.
2297
2298 return *target;
2299}
2300
2301
lrn@chromium.org34e60782011-09-15 07:25:40 +00002302// Ensures the debug information is present for shared.
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00002303bool Debug::EnsureDebugInfo(Handle<SharedFunctionInfo> shared,
2304 Handle<JSFunction> function) {
verwaest@chromium.orgd4be0f02013-06-05 13:39:03 +00002305 Isolate* isolate = shared->GetIsolate();
2306
lrn@chromium.org34e60782011-09-15 07:25:40 +00002307 // Return if we already have the debug info for shared.
2308 if (HasDebugInfo(shared)) {
2309 ASSERT(shared->is_compiled());
2310 return true;
2311 }
2312
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00002313 // There will be at least one break point when we are done.
2314 has_break_points_ = true;
2315
2316 // Ensure function is compiled. Return false if this failed.
2317 if (!function.is_null() &&
2318 !JSFunction::EnsureCompiled(function, CLEAR_EXCEPTION)) {
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002319 return false;
2320 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002321
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002322 // Create the debug info object.
verwaest@chromium.orgd4be0f02013-06-05 13:39:03 +00002323 Handle<DebugInfo> debug_info = isolate->factory()->NewDebugInfo(shared);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002324
2325 // Add debug info to the list.
2326 DebugInfoListNode* node = new DebugInfoListNode(*debug_info);
2327 node->set_next(debug_info_list_);
2328 debug_info_list_ = node;
2329
kasper.lundbd3ec4e2008-07-09 11:06:54 +00002330 return true;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002331}
2332
2333
2334void Debug::RemoveDebugInfo(Handle<DebugInfo> debug_info) {
2335 ASSERT(debug_info_list_ != NULL);
2336 // Run through the debug info objects to find this one and remove it.
2337 DebugInfoListNode* prev = NULL;
2338 DebugInfoListNode* current = debug_info_list_;
2339 while (current != NULL) {
2340 if (*current->debug_info() == *debug_info) {
2341 // Unlink from list. If prev is NULL we are looking at the first element.
2342 if (prev == NULL) {
2343 debug_info_list_ = current->next();
2344 } else {
2345 prev->set_next(current->next());
2346 }
lrn@chromium.org7516f052011-03-30 08:52:27 +00002347 current->debug_info()->shared()->set_debug_info(
2348 isolate_->heap()->undefined_value());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002349 delete current;
2350
2351 // If there are no more debug info objects there are not more break
2352 // points.
2353 has_break_points_ = debug_info_list_ != NULL;
2354
2355 return;
2356 }
2357 // Move to next in list.
2358 prev = current;
2359 current = current->next();
2360 }
2361 UNREACHABLE();
2362}
2363
2364
2365void Debug::SetAfterBreakTarget(JavaScriptFrame* frame) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002366 HandleScope scope(isolate_);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002367
lrn@chromium.org34e60782011-09-15 07:25:40 +00002368 PrepareForBreakPoints();
2369
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002370 // Get the executing function in which the debug break occurred.
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00002371 Handle<JSFunction> function(JSFunction::cast(frame->function()));
2372 Handle<SharedFunctionInfo> shared(function->shared());
2373 if (!EnsureDebugInfo(shared, function)) {
kasper.lundbd3ec4e2008-07-09 11:06:54 +00002374 // Return if we failed to retrieve the debug info.
2375 return;
2376 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002377 Handle<DebugInfo> debug_info = GetDebugInfo(shared);
2378 Handle<Code> code(debug_info->code());
2379 Handle<Code> original_code(debug_info->original_code());
2380#ifdef DEBUG
2381 // Get the code which is actually executing.
vegorov@chromium.org74f333b2011-04-06 11:17:46 +00002382 Handle<Code> frame_code(frame->LookupCode());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002383 ASSERT(frame_code.is_identical_to(code));
2384#endif
2385
2386 // Find the call address in the running code. This address holds the call to
2387 // either a DebugBreakXXX or to the debug break return entry code if the
2388 // break point is still active after processing the break point.
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00002389 Address addr = frame->pc() - Assembler::kPatchDebugBreakSlotReturnOffset;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002390
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002391 // Check if the location is at JS exit or debug break slot.
ager@chromium.orga1645e22009-09-09 19:27:10 +00002392 bool at_js_return = false;
2393 bool break_at_js_return_active = false;
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002394 bool at_debug_break_slot = false;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002395 RelocIterator it(debug_info->code());
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002396 while (!it.done() && !at_js_return && !at_debug_break_slot) {
ager@chromium.org236ad962008-09-25 09:45:57 +00002397 if (RelocInfo::IsJSReturn(it.rinfo()->rmode())) {
ager@chromium.orga1645e22009-09-09 19:27:10 +00002398 at_js_return = (it.rinfo()->pc() ==
2399 addr - Assembler::kPatchReturnSequenceAddressOffset);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002400 break_at_js_return_active = it.rinfo()->IsPatchedReturnSequence();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002401 }
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002402 if (RelocInfo::IsDebugBreakSlot(it.rinfo()->rmode())) {
2403 at_debug_break_slot = (it.rinfo()->pc() ==
2404 addr - Assembler::kPatchDebugBreakSlotAddressOffset);
2405 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002406 it.next();
2407 }
2408
2409 // Handle the jump to continue execution after break point depending on the
2410 // break location.
ager@chromium.orga1645e22009-09-09 19:27:10 +00002411 if (at_js_return) {
2412 // If the break point as return is still active jump to the corresponding
2413 // place in the original code. If not the break point was removed during
2414 // break point processing.
2415 if (break_at_js_return_active) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002416 addr += original_code->instruction_start() - code->instruction_start();
2417 }
2418
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00002419 // Move back to where the call instruction sequence started.
2420 thread_local_.after_break_target_ =
2421 addr - Assembler::kPatchReturnSequenceAddressOffset;
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002422 } else if (at_debug_break_slot) {
2423 // Address of where the debug break slot starts.
2424 addr = addr - Assembler::kPatchDebugBreakSlotAddressOffset;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002425
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002426 // Continue just after the slot.
2427 thread_local_.after_break_target_ = addr + Assembler::kDebugBreakSlotLength;
2428 } else if (IsDebugBreak(Assembler::target_address_at(addr))) {
2429 // We now know that there is still a debug break call at the target address,
2430 // so the break point is still there and the original code will hold the
2431 // address to jump to in order to complete the call which is replaced by a
2432 // call to DebugBreakXXX.
2433
2434 // Find the corresponding address in the original code.
2435 addr += original_code->instruction_start() - code->instruction_start();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002436
2437 // Install jump to the call address in the original code. This will be the
2438 // call which was overwritten by the call to DebugBreakXXX.
2439 thread_local_.after_break_target_ = Assembler::target_address_at(addr);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002440 } else {
2441 // There is no longer a break point present. Don't try to look in the
2442 // original code as the running code will have the right address. This takes
2443 // care of the case where the last break point is removed from the function
2444 // and therefore no "original code" is available.
2445 thread_local_.after_break_target_ = Assembler::target_address_at(addr);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002446 }
2447}
2448
2449
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00002450bool Debug::IsBreakAtReturn(JavaScriptFrame* frame) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002451 HandleScope scope(isolate_);
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00002452
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00002453 // If there are no break points this cannot be break at return, as
2454 // the debugger statement and stack guard bebug break cannot be at
2455 // return.
2456 if (!has_break_points_) {
2457 return false;
2458 }
2459
lrn@chromium.org34e60782011-09-15 07:25:40 +00002460 PrepareForBreakPoints();
2461
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00002462 // Get the executing function in which the debug break occurred.
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00002463 Handle<JSFunction> function(JSFunction::cast(frame->function()));
2464 Handle<SharedFunctionInfo> shared(function->shared());
2465 if (!EnsureDebugInfo(shared, function)) {
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00002466 // Return if we failed to retrieve the debug info.
2467 return false;
2468 }
2469 Handle<DebugInfo> debug_info = GetDebugInfo(shared);
2470 Handle<Code> code(debug_info->code());
2471#ifdef DEBUG
2472 // Get the code which is actually executing.
vegorov@chromium.org74f333b2011-04-06 11:17:46 +00002473 Handle<Code> frame_code(frame->LookupCode());
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00002474 ASSERT(frame_code.is_identical_to(code));
2475#endif
2476
2477 // Find the call address in the running code.
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00002478 Address addr = frame->pc() - Assembler::kPatchDebugBreakSlotReturnOffset;
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00002479
2480 // Check if the location is at JS return.
2481 RelocIterator it(debug_info->code());
2482 while (!it.done()) {
2483 if (RelocInfo::IsJSReturn(it.rinfo()->rmode())) {
2484 return (it.rinfo()->pc() ==
2485 addr - Assembler::kPatchReturnSequenceAddressOffset);
2486 }
2487 it.next();
2488 }
2489 return false;
2490}
2491
2492
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00002493void Debug::FramesHaveBeenDropped(StackFrame::Id new_break_frame_id,
whesse@chromium.orge90029b2010-08-02 11:52:17 +00002494 FrameDropMode mode,
2495 Object** restarter_frame_function_pointer) {
ulan@chromium.orgd9e468a2012-06-25 09:47:40 +00002496 if (mode != CURRENTLY_SET_MODE) {
2497 thread_local_.frame_drop_mode_ = mode;
2498 }
ager@chromium.org357bf652010-04-12 11:30:10 +00002499 thread_local_.break_frame_id_ = new_break_frame_id;
whesse@chromium.orge90029b2010-08-02 11:52:17 +00002500 thread_local_.restarter_frame_function_pointer_ =
2501 restarter_frame_function_pointer;
ager@chromium.org357bf652010-04-12 11:30:10 +00002502}
2503
2504
jkummerow@chromium.org212d9642012-05-11 15:02:09 +00002505const int Debug::FramePaddingLayout::kInitialSize = 1;
2506
2507
2508// Any even value bigger than kInitialSize as needed for stack scanning.
2509const int Debug::FramePaddingLayout::kPaddingValue = kInitialSize + 1;
2510
2511
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002512bool Debug::IsDebugGlobal(GlobalObject* global) {
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00002513 return IsLoaded() && global == debug_context()->global_object();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002514}
2515
2516
ager@chromium.org32912102009-01-16 10:38:43 +00002517void Debug::ClearMirrorCache() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002518 PostponeInterruptsScope postpone(isolate_);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002519 HandleScope scope(isolate_);
2520 ASSERT(isolate_->context() == *Debug::debug_context());
ager@chromium.org32912102009-01-16 10:38:43 +00002521
2522 // Clear the mirror cache.
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00002523 Handle<String> function_name = isolate_->factory()->InternalizeOneByteString(
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00002524 STATIC_ASCII_VECTOR("ClearMirrorCache"));
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00002525 Handle<Object> fun(
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00002526 isolate_->global_object()->GetPropertyNoExceptionThrown(*function_name),
2527 isolate_);
ager@chromium.org32912102009-01-16 10:38:43 +00002528 ASSERT(fun->IsJSFunction());
2529 bool caught_exception;
rossberg@chromium.org717967f2011-07-20 13:44:42 +00002530 Execution::TryCall(Handle<JSFunction>::cast(fun),
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00002531 Handle<JSObject>(Debug::debug_context()->global_object()),
ager@chromium.org32912102009-01-16 10:38:43 +00002532 0, NULL, &caught_exception);
2533}
2534
2535
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002536void Debug::CreateScriptCache() {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002537 Heap* heap = isolate_->heap();
2538 HandleScope scope(isolate_);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002539
2540 // Perform two GCs to get rid of all unreferenced scripts. The first GC gets
2541 // rid of all the cached script wrappers and the second gets rid of the
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002542 // scripts which are no longer referenced. The second also sweeps precisely,
2543 // which saves us doing yet another GC to make the heap iterable.
rossberg@chromium.org994edf62012-02-06 10:12:55 +00002544 heap->CollectAllGarbage(Heap::kNoGCFlags, "Debug::CreateScriptCache");
2545 heap->CollectAllGarbage(Heap::kMakeHeapIterableMask,
2546 "Debug::CreateScriptCache");
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002547
2548 ASSERT(script_cache_ == NULL);
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +00002549 script_cache_ = new ScriptCache(isolate_);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002550
2551 // Scan heap for Script objects.
2552 int count = 0;
hpayer@chromium.org7c3372b2013-02-13 17:26:04 +00002553 HeapIterator iterator(heap);
rossberg@chromium.org79e79022013-06-03 15:43:46 +00002554 DisallowHeapAllocation no_allocation;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002555
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002556 for (HeapObject* obj = iterator.next(); obj != NULL; obj = iterator.next()) {
sgjesse@chromium.org152a0b02009-10-07 13:50:16 +00002557 if (obj->IsScript() && Script::cast(obj)->HasValidSource()) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002558 script_cache_->Add(Handle<Script>(Script::cast(obj)));
2559 count++;
2560 }
2561 }
2562}
2563
2564
2565void Debug::DestroyScriptCache() {
2566 // Get rid of the script cache if it was created.
2567 if (script_cache_ != NULL) {
2568 delete script_cache_;
2569 script_cache_ = NULL;
2570 }
2571}
2572
2573
2574void Debug::AddScriptToScriptCache(Handle<Script> script) {
2575 if (script_cache_ != NULL) {
2576 script_cache_->Add(script);
2577 }
2578}
2579
2580
2581Handle<FixedArray> Debug::GetLoadedScripts() {
2582 // Create and fill the script cache when the loaded scripts is requested for
2583 // the first time.
2584 if (script_cache_ == NULL) {
2585 CreateScriptCache();
2586 }
2587
2588 // If the script cache is not active just return an empty array.
2589 ASSERT(script_cache_ != NULL);
2590 if (script_cache_ == NULL) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002591 isolate_->factory()->NewFixedArray(0);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002592 }
2593
2594 // Perform GC to get unreferenced scripts evicted from the cache before
2595 // returning the content.
rossberg@chromium.org994edf62012-02-06 10:12:55 +00002596 isolate_->heap()->CollectAllGarbage(Heap::kNoGCFlags,
2597 "Debug::GetLoadedScripts");
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002598
2599 // Get the scripts from the cache.
2600 return script_cache_->GetScripts();
2601}
2602
2603
2604void Debug::AfterGarbageCollection() {
2605 // Generate events for collected scripts.
2606 if (script_cache_ != NULL) {
2607 script_cache_->ProcessCollectedScripts();
2608 }
2609}
2610
2611
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00002612Debugger::Debugger(Isolate* isolate)
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +00002613 : debugger_access_(isolate->debugger_access()),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002614 event_listener_(Handle<Object>()),
2615 event_listener_data_(Handle<Object>()),
2616 compiling_natives_(false),
2617 is_loading_debugger_(false),
mstarzinger@chromium.orgde886792012-09-11 13:22:37 +00002618 live_edit_enabled_(true),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002619 never_unload_debugger_(false),
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002620 force_debugger_active_(false),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002621 message_handler_(NULL),
2622 debugger_unload_pending_(false),
2623 host_dispatch_handler_(NULL),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002624 debug_message_dispatch_handler_(NULL),
2625 message_dispatch_helper_thread_(NULL),
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +00002626 host_dispatch_period_(TimeDelta::FromMilliseconds(100)),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002627 agent_(NULL),
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00002628 command_queue_(isolate->logger(), kQueueInitialSize),
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +00002629 command_received_(0),
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00002630 event_command_queue_(isolate->logger(), kQueueInitialSize),
2631 isolate_(isolate) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002632}
2633
2634
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +00002635Debugger::~Debugger() {}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002636
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002637
2638Handle<Object> Debugger::MakeJSObject(Vector<const char> constructor_name,
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002639 int argc,
2640 Handle<Object> argv[],
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002641 bool* caught_exception) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002642 ASSERT(isolate_->context() == *isolate_->debug()->debug_context());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002643
2644 // Create the execution state object.
lrn@chromium.org7516f052011-03-30 08:52:27 +00002645 Handle<String> constructor_str =
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00002646 isolate_->factory()->InternalizeUtf8String(constructor_name);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002647 Handle<Object> constructor(
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00002648 isolate_->global_object()->GetPropertyNoExceptionThrown(*constructor_str),
2649 isolate_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002650 ASSERT(constructor->IsJSFunction());
2651 if (!constructor->IsJSFunction()) {
2652 *caught_exception = true;
lrn@chromium.org7516f052011-03-30 08:52:27 +00002653 return isolate_->factory()->undefined_value();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002654 }
2655 Handle<Object> js_object = Execution::TryCall(
2656 Handle<JSFunction>::cast(constructor),
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00002657 Handle<JSObject>(isolate_->debug()->debug_context()->global_object()),
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002658 argc,
2659 argv,
2660 caught_exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002661 return js_object;
2662}
2663
2664
2665Handle<Object> Debugger::MakeExecutionState(bool* caught_exception) {
2666 // Create the execution state object.
lrn@chromium.org7516f052011-03-30 08:52:27 +00002667 Handle<Object> break_id = isolate_->factory()->NewNumberFromInt(
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002668 isolate_->debug()->break_id());
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002669 Handle<Object> argv[] = { break_id };
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002670 return MakeJSObject(CStrVector("MakeExecutionState"),
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002671 ARRAY_SIZE(argv),
2672 argv,
2673 caught_exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002674}
2675
2676
2677Handle<Object> Debugger::MakeBreakEvent(Handle<Object> exec_state,
2678 Handle<Object> break_points_hit,
2679 bool* caught_exception) {
2680 // Create the new break event object.
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002681 Handle<Object> argv[] = { exec_state, break_points_hit };
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002682 return MakeJSObject(CStrVector("MakeBreakEvent"),
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002683 ARRAY_SIZE(argv),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002684 argv,
2685 caught_exception);
2686}
2687
2688
2689Handle<Object> Debugger::MakeExceptionEvent(Handle<Object> exec_state,
2690 Handle<Object> exception,
2691 bool uncaught,
2692 bool* caught_exception) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002693 Factory* factory = isolate_->factory();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002694 // Create the new exception event object.
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002695 Handle<Object> argv[] = { exec_state,
2696 exception,
2697 factory->ToBoolean(uncaught) };
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002698 return MakeJSObject(CStrVector("MakeExceptionEvent"),
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002699 ARRAY_SIZE(argv),
2700 argv,
2701 caught_exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002702}
2703
2704
2705Handle<Object> Debugger::MakeNewFunctionEvent(Handle<Object> function,
2706 bool* caught_exception) {
2707 // Create the new function event object.
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002708 Handle<Object> argv[] = { function };
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002709 return MakeJSObject(CStrVector("MakeNewFunctionEvent"),
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002710 ARRAY_SIZE(argv),
2711 argv,
2712 caught_exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002713}
2714
2715
2716Handle<Object> Debugger::MakeCompileEvent(Handle<Script> script,
iposva@chromium.org245aa852009-02-10 00:49:54 +00002717 bool before,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002718 bool* caught_exception) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002719 Factory* factory = isolate_->factory();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002720 // Create the compile event object.
2721 Handle<Object> exec_state = MakeExecutionState(caught_exception);
iposva@chromium.org245aa852009-02-10 00:49:54 +00002722 Handle<Object> script_wrapper = GetScriptWrapper(script);
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002723 Handle<Object> argv[] = { exec_state,
2724 script_wrapper,
2725 factory->ToBoolean(before) };
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002726 return MakeJSObject(CStrVector("MakeCompileEvent"),
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002727 ARRAY_SIZE(argv),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002728 argv,
2729 caught_exception);
2730}
2731
2732
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002733Handle<Object> Debugger::MakeScriptCollectedEvent(int id,
2734 bool* caught_exception) {
2735 // Create the script collected event object.
2736 Handle<Object> exec_state = MakeExecutionState(caught_exception);
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00002737 Handle<Object> id_object = Handle<Smi>(Smi::FromInt(id), isolate_);
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002738 Handle<Object> argv[] = { exec_state, id_object };
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002739
2740 return MakeJSObject(CStrVector("MakeScriptCollectedEvent"),
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002741 ARRAY_SIZE(argv),
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002742 argv,
2743 caught_exception);
2744}
2745
2746
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002747void Debugger::OnException(Handle<Object> exception, bool uncaught) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002748 HandleScope scope(isolate_);
2749 Debug* debug = isolate_->debug();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002750
2751 // Bail out based on state or if there is no listener for this event
lrn@chromium.org7516f052011-03-30 08:52:27 +00002752 if (debug->InDebugger()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002753 if (!Debugger::EventActive(v8::Exception)) return;
2754
2755 // Bail out if exception breaks are not active
2756 if (uncaught) {
2757 // Uncaught exceptions are reported by either flags.
lrn@chromium.org7516f052011-03-30 08:52:27 +00002758 if (!(debug->break_on_uncaught_exception() ||
2759 debug->break_on_exception())) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002760 } else {
2761 // Caught exceptions are reported is activated.
lrn@chromium.org7516f052011-03-30 08:52:27 +00002762 if (!debug->break_on_exception()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002763 }
2764
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002765 // Enter the debugger.
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +00002766 EnterDebugger debugger(isolate_);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002767 if (debugger.FailedToEnter()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002768
2769 // Clear all current stepping setup.
lrn@chromium.org7516f052011-03-30 08:52:27 +00002770 debug->ClearStepping();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002771 // Create the event data object.
2772 bool caught_exception = false;
2773 Handle<Object> exec_state = MakeExecutionState(&caught_exception);
2774 Handle<Object> event_data;
2775 if (!caught_exception) {
2776 event_data = MakeExceptionEvent(exec_state, exception, uncaught,
2777 &caught_exception);
2778 }
2779 // Bail out and don't call debugger if exception.
2780 if (caught_exception) {
2781 return;
2782 }
2783
ager@chromium.org5ec48922009-05-05 07:25:34 +00002784 // Process debug event.
2785 ProcessDebugEvent(v8::Exception, Handle<JSObject>::cast(event_data), false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002786 // Return to continue execution from where the exception was thrown.
2787}
2788
2789
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002790void Debugger::OnDebugBreak(Handle<Object> break_points_hit,
2791 bool auto_continue) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002792 HandleScope scope(isolate_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002793
kasper.lund212ac232008-07-16 07:07:30 +00002794 // Debugger has already been entered by caller.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002795 ASSERT(isolate_->context() == *isolate_->debug()->debug_context());
kasper.lund212ac232008-07-16 07:07:30 +00002796
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002797 // Bail out if there is no listener for this event
2798 if (!Debugger::EventActive(v8::Break)) return;
2799
2800 // Debugger must be entered in advance.
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00002801 ASSERT(isolate_->context() == *isolate_->debug()->debug_context());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002802
2803 // Create the event data object.
2804 bool caught_exception = false;
2805 Handle<Object> exec_state = MakeExecutionState(&caught_exception);
2806 Handle<Object> event_data;
2807 if (!caught_exception) {
2808 event_data = MakeBreakEvent(exec_state, break_points_hit,
2809 &caught_exception);
2810 }
2811 // Bail out and don't call debugger if exception.
2812 if (caught_exception) {
2813 return;
2814 }
2815
ager@chromium.org5ec48922009-05-05 07:25:34 +00002816 // Process debug event.
2817 ProcessDebugEvent(v8::Break,
2818 Handle<JSObject>::cast(event_data),
2819 auto_continue);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002820}
2821
2822
2823void Debugger::OnBeforeCompile(Handle<Script> script) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002824 HandleScope scope(isolate_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002825
2826 // Bail out based on state or if there is no listener for this event
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002827 if (isolate_->debug()->InDebugger()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002828 if (compiling_natives()) return;
2829 if (!EventActive(v8::BeforeCompile)) return;
2830
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002831 // Enter the debugger.
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +00002832 EnterDebugger debugger(isolate_);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002833 if (debugger.FailedToEnter()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002834
2835 // Create the event data object.
2836 bool caught_exception = false;
iposva@chromium.org245aa852009-02-10 00:49:54 +00002837 Handle<Object> event_data = MakeCompileEvent(script, true, &caught_exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002838 // Bail out and don't call debugger if exception.
2839 if (caught_exception) {
2840 return;
2841 }
2842
ager@chromium.org5ec48922009-05-05 07:25:34 +00002843 // Process debug event.
2844 ProcessDebugEvent(v8::BeforeCompile,
2845 Handle<JSObject>::cast(event_data),
2846 true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002847}
2848
2849
2850// Handle debugger actions when a new script is compiled.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00002851void Debugger::OnAfterCompile(Handle<Script> script,
2852 AfterCompileFlags after_compile_flags) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002853 HandleScope scope(isolate_);
2854 Debug* debug = isolate_->debug();
kasper.lund212ac232008-07-16 07:07:30 +00002855
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002856 // Add the newly compiled script to the script cache.
lrn@chromium.org7516f052011-03-30 08:52:27 +00002857 debug->AddScriptToScriptCache(script);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002858
2859 // No more to do if not debugging.
ager@chromium.org71daaf62009-04-01 07:22:49 +00002860 if (!IsDebuggerActive()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002861
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002862 // No compile events while compiling natives.
2863 if (compiling_natives()) return;
2864
iposva@chromium.org245aa852009-02-10 00:49:54 +00002865 // Store whether in debugger before entering debugger.
lrn@chromium.org7516f052011-03-30 08:52:27 +00002866 bool in_debugger = debug->InDebugger();
iposva@chromium.org245aa852009-02-10 00:49:54 +00002867
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002868 // Enter the debugger.
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +00002869 EnterDebugger debugger(isolate_);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002870 if (debugger.FailedToEnter()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002871
2872 // If debugging there might be script break points registered for this
2873 // script. Make sure that these break points are set.
2874
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002875 // Get the function UpdateScriptBreakPoints (defined in debug-debugger.js).
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00002876 Handle<String> update_script_break_points_string =
2877 isolate_->factory()->InternalizeOneByteString(
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00002878 STATIC_ASCII_VECTOR("UpdateScriptBreakPoints"));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002879 Handle<Object> update_script_break_points =
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00002880 Handle<Object>(
2881 debug->debug_context()->global_object()->GetPropertyNoExceptionThrown(
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00002882 *update_script_break_points_string),
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00002883 isolate_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002884 if (!update_script_break_points->IsJSFunction()) {
2885 return;
2886 }
2887 ASSERT(update_script_break_points->IsJSFunction());
2888
2889 // Wrap the script object in a proper JS object before passing it
2890 // to JavaScript.
2891 Handle<JSValue> wrapper = GetScriptWrapper(script);
2892
2893 // Call UpdateScriptBreakPoints expect no exceptions.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002894 bool caught_exception;
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002895 Handle<Object> argv[] = { wrapper };
rossberg@chromium.org717967f2011-07-20 13:44:42 +00002896 Execution::TryCall(Handle<JSFunction>::cast(update_script_break_points),
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +00002897 isolate_->js_builtins_object(),
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002898 ARRAY_SIZE(argv),
2899 argv,
2900 &caught_exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002901 if (caught_exception) {
2902 return;
2903 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002904 // Bail out based on state or if there is no listener for this event
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00002905 if (in_debugger && (after_compile_flags & SEND_WHEN_DEBUGGING) == 0) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002906 if (!Debugger::EventActive(v8::AfterCompile)) return;
2907
2908 // Create the compile state object.
2909 Handle<Object> event_data = MakeCompileEvent(script,
iposva@chromium.org245aa852009-02-10 00:49:54 +00002910 false,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002911 &caught_exception);
2912 // Bail out and don't call debugger if exception.
2913 if (caught_exception) {
2914 return;
2915 }
ager@chromium.org5ec48922009-05-05 07:25:34 +00002916 // Process debug event.
2917 ProcessDebugEvent(v8::AfterCompile,
2918 Handle<JSObject>::cast(event_data),
2919 true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002920}
2921
2922
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002923void Debugger::OnScriptCollected(int id) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002924 HandleScope scope(isolate_);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002925
2926 // No more to do if not debugging.
ulan@chromium.org56c14af2012-09-20 12:51:09 +00002927 if (isolate_->debug()->InDebugger()) return;
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002928 if (!IsDebuggerActive()) return;
2929 if (!Debugger::EventActive(v8::ScriptCollected)) return;
2930
2931 // Enter the debugger.
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +00002932 EnterDebugger debugger(isolate_);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002933 if (debugger.FailedToEnter()) return;
2934
2935 // Create the script collected state object.
2936 bool caught_exception = false;
2937 Handle<Object> event_data = MakeScriptCollectedEvent(id,
2938 &caught_exception);
2939 // Bail out and don't call debugger if exception.
2940 if (caught_exception) {
2941 return;
2942 }
2943
2944 // Process debug event.
2945 ProcessDebugEvent(v8::ScriptCollected,
2946 Handle<JSObject>::cast(event_data),
2947 true);
2948}
2949
2950
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002951void Debugger::ProcessDebugEvent(v8::DebugEvent event,
ager@chromium.org5ec48922009-05-05 07:25:34 +00002952 Handle<JSObject> event_data,
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002953 bool auto_continue) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002954 HandleScope scope(isolate_);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002955
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +00002956 // Clear any pending debug break if this is a real break.
2957 if (!auto_continue) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002958 isolate_->debug()->clear_interrupt_pending(DEBUGBREAK);
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +00002959 }
2960
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002961 // Create the execution state.
2962 bool caught_exception = false;
2963 Handle<Object> exec_state = MakeExecutionState(&caught_exception);
2964 if (caught_exception) {
2965 return;
2966 }
ager@chromium.org41826e72009-03-30 13:30:57 +00002967 // First notify the message handler if any.
2968 if (message_handler_ != NULL) {
ager@chromium.org5ec48922009-05-05 07:25:34 +00002969 NotifyMessageHandler(event,
2970 Handle<JSObject>::cast(exec_state),
2971 event_data,
2972 auto_continue);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002973 }
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00002974 // Notify registered debug event listener. This can be either a C or
2975 // a JavaScript function. Don't call event listener for v8::Break
2976 // here, if it's only a debug command -- they will be processed later.
2977 if ((event != v8::Break || !auto_continue) && !event_listener_.is_null()) {
2978 CallEventCallback(event, exec_state, event_data, NULL);
2979 }
2980 // Process pending debug commands.
2981 if (event == v8::Break) {
2982 while (!event_command_queue_.IsEmpty()) {
2983 CommandMessage command = event_command_queue_.Get();
2984 if (!event_listener_.is_null()) {
2985 CallEventCallback(v8::BreakForCommand,
2986 exec_state,
2987 event_data,
2988 command.client_data());
2989 }
2990 command.Dispose();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002991 }
2992 }
2993}
2994
2995
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00002996void Debugger::CallEventCallback(v8::DebugEvent event,
2997 Handle<Object> exec_state,
2998 Handle<Object> event_data,
2999 v8::Debug::ClientData* client_data) {
ager@chromium.orgea91cc52011-05-23 06:06:11 +00003000 if (event_listener_->IsForeign()) {
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00003001 CallCEventCallback(event, exec_state, event_data, client_data);
3002 } else {
3003 CallJSEventCallback(event, exec_state, event_data);
3004 }
3005}
3006
3007
3008void Debugger::CallCEventCallback(v8::DebugEvent event,
3009 Handle<Object> exec_state,
3010 Handle<Object> event_data,
3011 v8::Debug::ClientData* client_data) {
ager@chromium.orgea91cc52011-05-23 06:06:11 +00003012 Handle<Foreign> callback_obj(Handle<Foreign>::cast(event_listener_));
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00003013 v8::Debug::EventCallback2 callback =
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003014 FUNCTION_CAST<v8::Debug::EventCallback2>(
3015 callback_obj->foreign_address());
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00003016 EventDetailsImpl event_details(
3017 event,
3018 Handle<JSObject>::cast(exec_state),
3019 Handle<JSObject>::cast(event_data),
3020 event_listener_data_,
3021 client_data);
3022 callback(event_details);
3023}
3024
3025
3026void Debugger::CallJSEventCallback(v8::DebugEvent event,
3027 Handle<Object> exec_state,
3028 Handle<Object> event_data) {
3029 ASSERT(event_listener_->IsJSFunction());
3030 Handle<JSFunction> fun(Handle<JSFunction>::cast(event_listener_));
3031
3032 // Invoke the JavaScript debug event listener.
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00003033 Handle<Object> argv[] = { Handle<Object>(Smi::FromInt(event), isolate_),
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00003034 exec_state,
3035 event_data,
3036 event_listener_data_ };
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003037 bool caught_exception;
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00003038 Execution::TryCall(fun,
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00003039 isolate_->global_object(),
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00003040 ARRAY_SIZE(argv),
3041 argv,
3042 &caught_exception);
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00003043 // Silently ignore exceptions from debug event listeners.
3044}
3045
3046
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00003047Handle<Context> Debugger::GetDebugContext() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003048 never_unload_debugger_ = true;
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +00003049 EnterDebugger debugger(isolate_);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003050 return isolate_->debug()->debug_context();
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00003051}
3052
3053
ager@chromium.org71daaf62009-04-01 07:22:49 +00003054void Debugger::UnloadDebugger() {
lrn@chromium.org7516f052011-03-30 08:52:27 +00003055 Debug* debug = isolate_->debug();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003056
ager@chromium.org71daaf62009-04-01 07:22:49 +00003057 // Make sure that there are no breakpoints left.
lrn@chromium.org7516f052011-03-30 08:52:27 +00003058 debug->ClearAllBreakPoints();
ager@chromium.org71daaf62009-04-01 07:22:49 +00003059
3060 // Unload the debugger if feasible.
3061 if (!never_unload_debugger_) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00003062 debug->Unload();
ager@chromium.org71daaf62009-04-01 07:22:49 +00003063 }
3064
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003065 // Clear the flag indicating that the debugger should be unloaded.
3066 debugger_unload_pending_ = false;
ager@chromium.org71daaf62009-04-01 07:22:49 +00003067}
3068
3069
ager@chromium.org41826e72009-03-30 13:30:57 +00003070void Debugger::NotifyMessageHandler(v8::DebugEvent event,
ager@chromium.org5ec48922009-05-05 07:25:34 +00003071 Handle<JSObject> exec_state,
3072 Handle<JSObject> event_data,
ager@chromium.org41826e72009-03-30 13:30:57 +00003073 bool auto_continue) {
machenbach@chromium.orgf9841892013-11-25 12:01:13 +00003074 v8::Isolate* isolate = reinterpret_cast<v8::Isolate*>(isolate_);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003075 HandleScope scope(isolate_);
ager@chromium.org41826e72009-03-30 13:30:57 +00003076
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003077 if (!isolate_->debug()->Load()) return;
ager@chromium.org41826e72009-03-30 13:30:57 +00003078
3079 // Process the individual events.
ager@chromium.org5ec48922009-05-05 07:25:34 +00003080 bool sendEventMessage = false;
ager@chromium.org41826e72009-03-30 13:30:57 +00003081 switch (event) {
3082 case v8::Break:
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00003083 case v8::BreakForCommand:
ager@chromium.org5ec48922009-05-05 07:25:34 +00003084 sendEventMessage = !auto_continue;
ager@chromium.org41826e72009-03-30 13:30:57 +00003085 break;
3086 case v8::Exception:
ager@chromium.org5ec48922009-05-05 07:25:34 +00003087 sendEventMessage = true;
ager@chromium.org41826e72009-03-30 13:30:57 +00003088 break;
3089 case v8::BeforeCompile:
3090 break;
3091 case v8::AfterCompile:
ager@chromium.org5ec48922009-05-05 07:25:34 +00003092 sendEventMessage = true;
ager@chromium.org41826e72009-03-30 13:30:57 +00003093 break;
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003094 case v8::ScriptCollected:
3095 sendEventMessage = true;
3096 break;
ager@chromium.org41826e72009-03-30 13:30:57 +00003097 case v8::NewFunction:
3098 break;
3099 default:
3100 UNREACHABLE();
3101 }
3102
ager@chromium.org5ec48922009-05-05 07:25:34 +00003103 // The debug command interrupt flag might have been set when the command was
3104 // added. It should be enough to clear the flag only once while we are in the
3105 // debugger.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003106 ASSERT(isolate_->debug()->InDebugger());
3107 isolate_->stack_guard()->Continue(DEBUGCOMMAND);
ager@chromium.org5ec48922009-05-05 07:25:34 +00003108
3109 // Notify the debugger that a debug event has occurred unless auto continue is
3110 // active in which case no event is send.
3111 if (sendEventMessage) {
3112 MessageImpl message = MessageImpl::NewEvent(
3113 event,
3114 auto_continue,
3115 Handle<JSObject>::cast(exec_state),
3116 Handle<JSObject>::cast(event_data));
3117 InvokeMessageHandler(message);
3118 }
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +00003119
3120 // If auto continue don't make the event cause a break, but process messages
3121 // in the queue if any. For script collected events don't even process
3122 // messages in the queue as the execution state might not be what is expected
3123 // by the client.
ager@chromium.org6ffc2172009-05-29 19:20:16 +00003124 if ((auto_continue && !HasCommands()) || event == v8::ScriptCollected) {
ager@chromium.org5ec48922009-05-05 07:25:34 +00003125 return;
3126 }
ager@chromium.org41826e72009-03-30 13:30:57 +00003127
ager@chromium.org41826e72009-03-30 13:30:57 +00003128 v8::TryCatch try_catch;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003129
3130 // DebugCommandProcessor goes here.
3131 v8::Local<v8::Object> cmd_processor;
3132 {
3133 v8::Local<v8::Object> api_exec_state =
3134 v8::Utils::ToLocal(Handle<JSObject>::cast(exec_state));
machenbach@chromium.orgf9841892013-11-25 12:01:13 +00003135 v8::Local<v8::String> fun_name = v8::String::NewFromUtf8(
3136 isolate, "debugCommandProcessor");
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003137 v8::Local<v8::Function> fun =
danno@chromium.orgf95d4b92013-06-13 14:40:17 +00003138 v8::Local<v8::Function>::Cast(api_exec_state->Get(fun_name));
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003139
machenbach@chromium.orgcfdf67d2013-09-27 07:27:26 +00003140 v8::Handle<v8::Boolean> running = v8::Boolean::New(auto_continue);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003141 static const int kArgc = 1;
3142 v8::Handle<Value> argv[kArgc] = { running };
danno@chromium.orgf95d4b92013-06-13 14:40:17 +00003143 cmd_processor = v8::Local<v8::Object>::Cast(
3144 fun->Call(api_exec_state, kArgc, argv));
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003145 if (try_catch.HasCaught()) {
3146 PrintLn(try_catch.Exception());
3147 return;
3148 }
ager@chromium.org41826e72009-03-30 13:30:57 +00003149 }
3150
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003151 bool running = auto_continue;
3152
ager@chromium.org41826e72009-03-30 13:30:57 +00003153 // Process requests from the debugger.
3154 while (true) {
3155 // Wait for new command in the queue.
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003156 if (Debugger::host_dispatch_handler_) {
3157 // In case there is a host dispatch - do periodic dispatches.
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +00003158 if (!command_received_.WaitFor(host_dispatch_period_)) {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003159 // Timout expired, do the dispatch.
3160 Debugger::host_dispatch_handler_();
3161 continue;
3162 }
3163 } else {
3164 // In case there is no host dispatch - just wait.
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +00003165 command_received_.Wait();
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003166 }
ager@chromium.org41826e72009-03-30 13:30:57 +00003167
ager@chromium.org41826e72009-03-30 13:30:57 +00003168 // Get the command from the queue.
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003169 CommandMessage command = command_queue_.Get();
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00003170 isolate_->logger()->DebugTag(
3171 "Got request from command queue, in interactive loop.");
ager@chromium.org71daaf62009-04-01 07:22:49 +00003172 if (!Debugger::IsDebuggerActive()) {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003173 // Delete command text and user data.
3174 command.Dispose();
ager@chromium.org41826e72009-03-30 13:30:57 +00003175 return;
3176 }
3177
ager@chromium.org41826e72009-03-30 13:30:57 +00003178 // Invoke JavaScript to process the debug request.
3179 v8::Local<v8::String> fun_name;
3180 v8::Local<v8::Function> fun;
3181 v8::Local<v8::Value> request;
3182 v8::TryCatch try_catch;
machenbach@chromium.orgf9841892013-11-25 12:01:13 +00003183 fun_name = v8::String::NewFromUtf8(isolate, "processDebugRequest");
danno@chromium.orgf95d4b92013-06-13 14:40:17 +00003184 fun = v8::Local<v8::Function>::Cast(cmd_processor->Get(fun_name));
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003185
machenbach@chromium.orgf9841892013-11-25 12:01:13 +00003186 request = v8::String::NewFromTwoByte(isolate, command.text().start(),
3187 v8::String::kNormalString,
3188 command.text().length());
ager@chromium.org41826e72009-03-30 13:30:57 +00003189 static const int kArgc = 1;
3190 v8::Handle<Value> argv[kArgc] = { request };
3191 v8::Local<v8::Value> response_val = fun->Call(cmd_processor, kArgc, argv);
3192
3193 // Get the response.
3194 v8::Local<v8::String> response;
ager@chromium.org41826e72009-03-30 13:30:57 +00003195 if (!try_catch.HasCaught()) {
3196 // Get response string.
3197 if (!response_val->IsUndefined()) {
danno@chromium.orgf95d4b92013-06-13 14:40:17 +00003198 response = v8::Local<v8::String>::Cast(response_val);
ager@chromium.org41826e72009-03-30 13:30:57 +00003199 } else {
machenbach@chromium.orgf9841892013-11-25 12:01:13 +00003200 response = v8::String::NewFromUtf8(isolate, "");
ager@chromium.org41826e72009-03-30 13:30:57 +00003201 }
3202
3203 // Log the JSON request/response.
3204 if (FLAG_trace_debug_json) {
3205 PrintLn(request);
3206 PrintLn(response);
3207 }
3208
3209 // Get the running state.
machenbach@chromium.orgf9841892013-11-25 12:01:13 +00003210 fun_name = v8::String::NewFromUtf8(isolate, "isRunning");
danno@chromium.orgf95d4b92013-06-13 14:40:17 +00003211 fun = v8::Local<v8::Function>::Cast(cmd_processor->Get(fun_name));
ager@chromium.org41826e72009-03-30 13:30:57 +00003212 static const int kArgc = 1;
3213 v8::Handle<Value> argv[kArgc] = { response };
3214 v8::Local<v8::Value> running_val = fun->Call(cmd_processor, kArgc, argv);
3215 if (!try_catch.HasCaught()) {
3216 running = running_val->ToBoolean()->Value();
3217 }
3218 } else {
3219 // In case of failure the result text is the exception text.
3220 response = try_catch.Exception()->ToString();
3221 }
3222
ager@chromium.org41826e72009-03-30 13:30:57 +00003223 // Return the result.
ager@chromium.org5ec48922009-05-05 07:25:34 +00003224 MessageImpl message = MessageImpl::NewResponse(
3225 event,
3226 running,
3227 Handle<JSObject>::cast(exec_state),
3228 Handle<JSObject>::cast(event_data),
3229 Handle<String>(Utils::OpenHandle(*response)),
3230 command.client_data());
3231 InvokeMessageHandler(message);
3232 command.Dispose();
ager@chromium.org41826e72009-03-30 13:30:57 +00003233
3234 // Return from debug event processing if either the VM is put into the
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00003235 // running state (through a continue command) or auto continue is active
ager@chromium.org41826e72009-03-30 13:30:57 +00003236 // and there are no more commands queued.
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003237 if (running && !HasCommands()) {
ager@chromium.org41826e72009-03-30 13:30:57 +00003238 return;
3239 }
3240 }
3241}
3242
3243
iposva@chromium.org245aa852009-02-10 00:49:54 +00003244void Debugger::SetEventListener(Handle<Object> callback,
3245 Handle<Object> data) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00003246 HandleScope scope(isolate_);
3247 GlobalHandles* global_handles = isolate_->global_handles();
iposva@chromium.org245aa852009-02-10 00:49:54 +00003248
3249 // Clear the global handles for the event listener and the event listener data
3250 // object.
3251 if (!event_listener_.is_null()) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00003252 global_handles->Destroy(
iposva@chromium.org245aa852009-02-10 00:49:54 +00003253 reinterpret_cast<Object**>(event_listener_.location()));
3254 event_listener_ = Handle<Object>();
3255 }
3256 if (!event_listener_data_.is_null()) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00003257 global_handles->Destroy(
iposva@chromium.org245aa852009-02-10 00:49:54 +00003258 reinterpret_cast<Object**>(event_listener_data_.location()));
3259 event_listener_data_ = Handle<Object>();
3260 }
3261
3262 // If there is a new debug event listener register it together with its data
3263 // object.
3264 if (!callback->IsUndefined() && !callback->IsNull()) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003265 event_listener_ = Handle<Object>::cast(
lrn@chromium.org7516f052011-03-30 08:52:27 +00003266 global_handles->Create(*callback));
iposva@chromium.org245aa852009-02-10 00:49:54 +00003267 if (data.is_null()) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00003268 data = isolate_->factory()->undefined_value();
iposva@chromium.org245aa852009-02-10 00:49:54 +00003269 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003270 event_listener_data_ = Handle<Object>::cast(
lrn@chromium.org7516f052011-03-30 08:52:27 +00003271 global_handles->Create(*data));
iposva@chromium.org245aa852009-02-10 00:49:54 +00003272 }
3273
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003274 ListenersChanged();
iposva@chromium.org245aa852009-02-10 00:49:54 +00003275}
3276
3277
ager@chromium.org5ec48922009-05-05 07:25:34 +00003278void Debugger::SetMessageHandler(v8::Debug::MessageHandler2 handler) {
jkummerow@chromium.orgdc94e192013-08-30 11:35:42 +00003279 LockGuard<RecursiveMutex> with(debugger_access_);
ager@chromium.org71daaf62009-04-01 07:22:49 +00003280
ager@chromium.org381abbb2009-02-25 13:23:22 +00003281 message_handler_ = handler;
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003282 ListenersChanged();
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003283 if (handler == NULL) {
ager@chromium.org71daaf62009-04-01 07:22:49 +00003284 // Send an empty command to the debugger if in a break to make JavaScript
3285 // run again if the debugger is closed.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003286 if (isolate_->debug()->InDebugger()) {
ager@chromium.org71daaf62009-04-01 07:22:49 +00003287 ProcessCommand(Vector<const uint16_t>::empty());
3288 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003289 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003290}
3291
3292
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003293void Debugger::ListenersChanged() {
3294 if (IsDebuggerActive()) {
3295 // Disable the compilation cache when the debugger is active.
lrn@chromium.org7516f052011-03-30 08:52:27 +00003296 isolate_->compilation_cache()->Disable();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003297 debugger_unload_pending_ = false;
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003298 } else {
lrn@chromium.org7516f052011-03-30 08:52:27 +00003299 isolate_->compilation_cache()->Enable();
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003300 // Unload the debugger if event listener and message handler cleared.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003301 // Schedule this for later, because we may be in non-V8 thread.
3302 debugger_unload_pending_ = true;
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003303 }
3304}
3305
3306
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003307void Debugger::SetHostDispatchHandler(v8::Debug::HostDispatchHandler handler,
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +00003308 TimeDelta period) {
ager@chromium.org381abbb2009-02-25 13:23:22 +00003309 host_dispatch_handler_ = handler;
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +00003310 host_dispatch_period_ = period;
ager@chromium.org381abbb2009-02-25 13:23:22 +00003311}
3312
3313
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003314void Debugger::SetDebugMessageDispatchHandler(
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003315 v8::Debug::DebugMessageDispatchHandler handler, bool provide_locker) {
jkummerow@chromium.orgdc94e192013-08-30 11:35:42 +00003316 LockGuard<Mutex> lock_guard(&dispatch_handler_access_);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003317 debug_message_dispatch_handler_ = handler;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003318
3319 if (provide_locker && message_dispatch_helper_thread_ == NULL) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003320 message_dispatch_helper_thread_ = new MessageDispatchHelperThread(isolate_);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003321 message_dispatch_helper_thread_->Start();
3322 }
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003323}
3324
3325
ager@chromium.org41826e72009-03-30 13:30:57 +00003326// Calls the registered debug message handler. This callback is part of the
ager@chromium.org5ec48922009-05-05 07:25:34 +00003327// public API.
3328void Debugger::InvokeMessageHandler(MessageImpl message) {
jkummerow@chromium.orgdc94e192013-08-30 11:35:42 +00003329 LockGuard<RecursiveMutex> with(debugger_access_);
ager@chromium.org71daaf62009-04-01 07:22:49 +00003330
ager@chromium.org381abbb2009-02-25 13:23:22 +00003331 if (message_handler_ != NULL) {
ager@chromium.org5ec48922009-05-05 07:25:34 +00003332 message_handler_(message);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003333 }
ager@chromium.org41826e72009-03-30 13:30:57 +00003334}
3335
3336
3337// Puts a command coming from the public API on the queue. Creates
3338// a copy of the command string managed by the debugger. Up to this
3339// point, the command data was managed by the API client. Called
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003340// by the API client thread.
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003341void Debugger::ProcessCommand(Vector<const uint16_t> command,
3342 v8::Debug::ClientData* client_data) {
3343 // Need to cast away const.
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003344 CommandMessage message = CommandMessage::New(
ager@chromium.org41826e72009-03-30 13:30:57 +00003345 Vector<uint16_t>(const_cast<uint16_t*>(command.start()),
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003346 command.length()),
3347 client_data);
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00003348 isolate_->logger()->DebugTag("Put command on command_queue.");
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003349 command_queue_.Put(message);
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +00003350 command_received_.Signal();
sgjesse@chromium.org3afc1582009-04-16 22:31:44 +00003351
3352 // Set the debug command break flag to have the command processed.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003353 if (!isolate_->debug()->InDebugger()) {
3354 isolate_->stack_guard()->DebugCommand();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003355 }
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003356
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003357 MessageDispatchHelperThread* dispatch_thread;
3358 {
jkummerow@chromium.orgdc94e192013-08-30 11:35:42 +00003359 LockGuard<Mutex> lock_guard(&dispatch_handler_access_);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003360 dispatch_thread = message_dispatch_helper_thread_;
3361 }
3362
3363 if (dispatch_thread == NULL) {
3364 CallMessageDispatchHandler();
3365 } else {
3366 dispatch_thread->Schedule();
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003367 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003368}
3369
3370
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003371bool Debugger::HasCommands() {
ager@chromium.org41826e72009-03-30 13:30:57 +00003372 return !command_queue_.IsEmpty();
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003373}
3374
3375
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00003376void Debugger::EnqueueDebugCommand(v8::Debug::ClientData* client_data) {
3377 CommandMessage message = CommandMessage::New(Vector<uint16_t>(), client_data);
3378 event_command_queue_.Put(message);
3379
3380 // Set the debug command break flag to have the command processed.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003381 if (!isolate_->debug()->InDebugger()) {
3382 isolate_->stack_guard()->DebugCommand();
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00003383 }
3384}
3385
3386
ager@chromium.org71daaf62009-04-01 07:22:49 +00003387bool Debugger::IsDebuggerActive() {
jkummerow@chromium.orgdc94e192013-08-30 11:35:42 +00003388 LockGuard<RecursiveMutex> with(debugger_access_);
ager@chromium.org71daaf62009-04-01 07:22:49 +00003389
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00003390 return message_handler_ != NULL ||
3391 !event_listener_.is_null() ||
3392 force_debugger_active_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003393}
3394
3395
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003396Handle<Object> Debugger::Call(Handle<JSFunction> fun,
3397 Handle<Object> data,
3398 bool* pending_exception) {
ager@chromium.org71daaf62009-04-01 07:22:49 +00003399 // When calling functions in the debugger prevent it from beeing unloaded.
3400 Debugger::never_unload_debugger_ = true;
3401
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003402 // Enter the debugger.
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +00003403 EnterDebugger debugger(isolate_);
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +00003404 if (debugger.FailedToEnter()) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00003405 return isolate_->factory()->undefined_value();
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003406 }
3407
3408 // Create the execution state.
3409 bool caught_exception = false;
3410 Handle<Object> exec_state = MakeExecutionState(&caught_exception);
3411 if (caught_exception) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00003412 return isolate_->factory()->undefined_value();
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003413 }
3414
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00003415 Handle<Object> argv[] = { exec_state, data };
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +00003416 Handle<Object> result = Execution::Call(
jkummerow@chromium.org2c9426b2013-09-05 16:31:13 +00003417 isolate_,
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +00003418 fun,
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00003419 Handle<Object>(isolate_->debug()->debug_context_->global_proxy(),
3420 isolate_),
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00003421 ARRAY_SIZE(argv),
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +00003422 argv,
3423 pending_exception);
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003424 return result;
3425}
3426
3427
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003428static void StubMessageHandler2(const v8::Debug::Message& message) {
3429 // Simply ignore message.
3430}
3431
3432
3433bool Debugger::StartAgent(const char* name, int port,
3434 bool wait_for_connection) {
3435 if (wait_for_connection) {
3436 // Suspend V8 if it is already running or set V8 to suspend whenever
3437 // it starts.
3438 // Provide stub message handler; V8 auto-continues each suspend
3439 // when there is no message handler; we doesn't need it.
3440 // Once become suspended, V8 will stay so indefinitely long, until remote
3441 // debugger connects and issues "continue" command.
3442 Debugger::message_handler_ = StubMessageHandler2;
3443 v8::Debug::DebugBreak();
3444 }
3445
jkummerow@chromium.org3d00d0a2013-09-04 13:57:32 +00003446 if (agent_ == NULL) {
3447 agent_ = new DebuggerAgent(isolate_, name, port);
3448 agent_->Start();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003449 }
jkummerow@chromium.org3d00d0a2013-09-04 13:57:32 +00003450 return true;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003451}
3452
3453
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003454void Debugger::StopAgent() {
3455 if (agent_ != NULL) {
3456 agent_->Shutdown();
3457 agent_->Join();
3458 delete agent_;
3459 agent_ = NULL;
3460 }
3461}
3462
3463
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003464void Debugger::WaitForAgent() {
3465 if (agent_ != NULL)
3466 agent_->WaitUntilListening();
3467}
3468
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003469
3470void Debugger::CallMessageDispatchHandler() {
3471 v8::Debug::DebugMessageDispatchHandler handler;
3472 {
jkummerow@chromium.orgdc94e192013-08-30 11:35:42 +00003473 LockGuard<Mutex> lock_guard(&dispatch_handler_access_);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003474 handler = Debugger::debug_message_dispatch_handler_;
3475 }
3476 if (handler != NULL) {
3477 handler();
3478 }
3479}
3480
3481
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +00003482EnterDebugger::EnterDebugger(Isolate* isolate)
3483 : isolate_(isolate),
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003484 prev_(isolate_->debug()->debugger_entry()),
3485 it_(isolate_),
3486 has_js_frames_(!it_.done()),
3487 save_(isolate_) {
3488 Debug* debug = isolate_->debug();
3489 ASSERT(prev_ != NULL || !debug->is_interrupt_pending(PREEMPT));
3490 ASSERT(prev_ != NULL || !debug->is_interrupt_pending(DEBUGBREAK));
3491
3492 // Link recursive debugger entry.
3493 debug->set_debugger_entry(this);
3494
3495 // Store the previous break id and frame id.
3496 break_id_ = debug->break_id();
3497 break_frame_id_ = debug->break_frame_id();
3498
3499 // Create the new break info. If there is no JavaScript frames there is no
3500 // break frame id.
3501 if (has_js_frames_) {
3502 debug->NewBreak(it_.frame()->id());
3503 } else {
3504 debug->NewBreak(StackFrame::NO_ID);
3505 }
3506
3507 // Make sure that debugger is loaded and enter the debugger context.
3508 load_failed_ = !debug->Load();
3509 if (!load_failed_) {
3510 // NOTE the member variable save which saves the previous context before
3511 // this change.
3512 isolate_->set_context(*debug->debug_context());
3513 }
3514}
3515
3516
3517EnterDebugger::~EnterDebugger() {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003518 Debug* debug = isolate_->debug();
3519
3520 // Restore to the previous break state.
3521 debug->SetBreak(break_frame_id_, break_id_);
3522
3523 // Check for leaving the debugger.
fschneider@chromium.org7d10be52012-04-10 12:30:14 +00003524 if (!load_failed_ && prev_ == NULL) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003525 // Clear mirror cache when leaving the debugger. Skip this if there is a
3526 // pending exception as clearing the mirror cache calls back into
3527 // JavaScript. This can happen if the v8::Debug::Call is used in which
3528 // case the exception should end up in the calling code.
3529 if (!isolate_->has_pending_exception()) {
3530 // Try to avoid any pending debug break breaking in the clear mirror
3531 // cache JavaScript code.
3532 if (isolate_->stack_guard()->IsDebugBreak()) {
3533 debug->set_interrupts_pending(DEBUGBREAK);
3534 isolate_->stack_guard()->Continue(DEBUGBREAK);
3535 }
3536 debug->ClearMirrorCache();
3537 }
3538
3539 // Request preemption and debug break when leaving the last debugger entry
3540 // if any of these where recorded while debugging.
3541 if (debug->is_interrupt_pending(PREEMPT)) {
3542 // This re-scheduling of preemption is to avoid starvation in some
3543 // debugging scenarios.
3544 debug->clear_interrupt_pending(PREEMPT);
3545 isolate_->stack_guard()->Preempt();
3546 }
3547 if (debug->is_interrupt_pending(DEBUGBREAK)) {
3548 debug->clear_interrupt_pending(DEBUGBREAK);
3549 isolate_->stack_guard()->DebugBreak();
3550 }
3551
3552 // If there are commands in the queue when leaving the debugger request
3553 // that these commands are processed.
3554 if (isolate_->debugger()->HasCommands()) {
3555 isolate_->stack_guard()->DebugCommand();
3556 }
3557
3558 // If leaving the debugger with the debugger no longer active unload it.
3559 if (!isolate_->debugger()->IsDebuggerActive()) {
3560 isolate_->debugger()->UnloadDebugger();
3561 }
3562 }
3563
3564 // Leaving this debugger entry.
3565 debug->set_debugger_entry(prev_);
3566}
3567
3568
ager@chromium.org5ec48922009-05-05 07:25:34 +00003569MessageImpl MessageImpl::NewEvent(DebugEvent event,
3570 bool running,
3571 Handle<JSObject> exec_state,
3572 Handle<JSObject> event_data) {
3573 MessageImpl message(true, event, running,
3574 exec_state, event_data, Handle<String>(), NULL);
3575 return message;
3576}
3577
3578
3579MessageImpl MessageImpl::NewResponse(DebugEvent event,
3580 bool running,
3581 Handle<JSObject> exec_state,
3582 Handle<JSObject> event_data,
3583 Handle<String> response_json,
3584 v8::Debug::ClientData* client_data) {
3585 MessageImpl message(false, event, running,
3586 exec_state, event_data, response_json, client_data);
3587 return message;
3588}
3589
3590
3591MessageImpl::MessageImpl(bool is_event,
3592 DebugEvent event,
3593 bool running,
3594 Handle<JSObject> exec_state,
3595 Handle<JSObject> event_data,
3596 Handle<String> response_json,
3597 v8::Debug::ClientData* client_data)
3598 : is_event_(is_event),
3599 event_(event),
3600 running_(running),
3601 exec_state_(exec_state),
3602 event_data_(event_data),
3603 response_json_(response_json),
3604 client_data_(client_data) {}
3605
3606
3607bool MessageImpl::IsEvent() const {
3608 return is_event_;
3609}
3610
3611
3612bool MessageImpl::IsResponse() const {
3613 return !is_event_;
3614}
3615
3616
3617DebugEvent MessageImpl::GetEvent() const {
3618 return event_;
3619}
3620
3621
3622bool MessageImpl::WillStartRunning() const {
3623 return running_;
3624}
3625
3626
3627v8::Handle<v8::Object> MessageImpl::GetExecutionState() const {
3628 return v8::Utils::ToLocal(exec_state_);
3629}
3630
3631
hpayer@chromium.orgc5d49712013-09-11 08:25:48 +00003632v8::Isolate* MessageImpl::GetIsolate() const {
3633 return reinterpret_cast<v8::Isolate*>(exec_state_->GetIsolate());
3634}
3635
3636
ager@chromium.org5ec48922009-05-05 07:25:34 +00003637v8::Handle<v8::Object> MessageImpl::GetEventData() const {
3638 return v8::Utils::ToLocal(event_data_);
3639}
3640
3641
3642v8::Handle<v8::String> MessageImpl::GetJSON() const {
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +00003643 v8::HandleScope scope(
3644 reinterpret_cast<v8::Isolate*>(event_data_->GetIsolate()));
ager@chromium.org5ec48922009-05-05 07:25:34 +00003645
3646 if (IsEvent()) {
3647 // Call toJSONProtocol on the debug event object.
3648 Handle<Object> fun = GetProperty(event_data_, "toJSONProtocol");
3649 if (!fun->IsJSFunction()) {
3650 return v8::Handle<v8::String>();
3651 }
3652 bool caught_exception;
3653 Handle<Object> json = Execution::TryCall(Handle<JSFunction>::cast(fun),
3654 event_data_,
3655 0, NULL, &caught_exception);
3656 if (caught_exception || !json->IsString()) {
3657 return v8::Handle<v8::String>();
3658 }
3659 return scope.Close(v8::Utils::ToLocal(Handle<String>::cast(json)));
3660 } else {
3661 return v8::Utils::ToLocal(response_json_);
3662 }
3663}
3664
3665
3666v8::Handle<v8::Context> MessageImpl::GetEventContext() const {
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +00003667 Isolate* isolate = event_data_->GetIsolate();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003668 v8::Handle<v8::Context> context = GetDebugEventContext(isolate);
3669 // Isolate::context() may be NULL when "script collected" event occures.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003670 ASSERT(!context.IsEmpty() || event_ == v8::ScriptCollected);
rossberg@chromium.org717967f2011-07-20 13:44:42 +00003671 return context;
ager@chromium.org5ec48922009-05-05 07:25:34 +00003672}
3673
3674
3675v8::Debug::ClientData* MessageImpl::GetClientData() const {
3676 return client_data_;
3677}
3678
3679
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003680EventDetailsImpl::EventDetailsImpl(DebugEvent event,
3681 Handle<JSObject> exec_state,
3682 Handle<JSObject> event_data,
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00003683 Handle<Object> callback_data,
3684 v8::Debug::ClientData* client_data)
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003685 : event_(event),
3686 exec_state_(exec_state),
3687 event_data_(event_data),
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00003688 callback_data_(callback_data),
3689 client_data_(client_data) {}
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003690
3691
3692DebugEvent EventDetailsImpl::GetEvent() const {
3693 return event_;
3694}
3695
3696
3697v8::Handle<v8::Object> EventDetailsImpl::GetExecutionState() const {
3698 return v8::Utils::ToLocal(exec_state_);
3699}
3700
3701
3702v8::Handle<v8::Object> EventDetailsImpl::GetEventData() const {
3703 return v8::Utils::ToLocal(event_data_);
3704}
3705
3706
3707v8::Handle<v8::Context> EventDetailsImpl::GetEventContext() const {
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +00003708 return GetDebugEventContext(exec_state_->GetIsolate());
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003709}
3710
3711
3712v8::Handle<v8::Value> EventDetailsImpl::GetCallbackData() const {
3713 return v8::Utils::ToLocal(callback_data_);
3714}
3715
3716
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00003717v8::Debug::ClientData* EventDetailsImpl::GetClientData() const {
3718 return client_data_;
3719}
3720
3721
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003722CommandMessage::CommandMessage() : text_(Vector<uint16_t>::empty()),
3723 client_data_(NULL) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003724}
3725
3726
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003727CommandMessage::CommandMessage(const Vector<uint16_t>& text,
3728 v8::Debug::ClientData* data)
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003729 : text_(text),
3730 client_data_(data) {
3731}
3732
3733
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003734CommandMessage::~CommandMessage() {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003735}
3736
3737
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003738void CommandMessage::Dispose() {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003739 text_.Dispose();
3740 delete client_data_;
3741 client_data_ = NULL;
3742}
3743
3744
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003745CommandMessage CommandMessage::New(const Vector<uint16_t>& command,
3746 v8::Debug::ClientData* data) {
3747 return CommandMessage(command.Clone(), data);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003748}
3749
3750
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003751CommandMessageQueue::CommandMessageQueue(int size) : start_(0), end_(0),
3752 size_(size) {
3753 messages_ = NewArray<CommandMessage>(size);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003754}
3755
3756
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003757CommandMessageQueue::~CommandMessageQueue() {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003758 while (!IsEmpty()) {
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003759 CommandMessage m = Get();
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003760 m.Dispose();
3761 }
kasper.lund7276f142008-07-30 08:49:36 +00003762 DeleteArray(messages_);
3763}
3764
3765
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003766CommandMessage CommandMessageQueue::Get() {
kasper.lund7276f142008-07-30 08:49:36 +00003767 ASSERT(!IsEmpty());
3768 int result = start_;
3769 start_ = (start_ + 1) % size_;
3770 return messages_[result];
3771}
3772
3773
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003774void CommandMessageQueue::Put(const CommandMessage& message) {
kasper.lund7276f142008-07-30 08:49:36 +00003775 if ((end_ + 1) % size_ == start_) {
3776 Expand();
3777 }
3778 messages_[end_] = message;
3779 end_ = (end_ + 1) % size_;
3780}
3781
3782
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003783void CommandMessageQueue::Expand() {
3784 CommandMessageQueue new_queue(size_ * 2);
kasper.lund7276f142008-07-30 08:49:36 +00003785 while (!IsEmpty()) {
3786 new_queue.Put(Get());
3787 }
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003788 CommandMessage* array_to_free = messages_;
kasper.lund7276f142008-07-30 08:49:36 +00003789 *this = new_queue;
3790 new_queue.messages_ = array_to_free;
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003791 // Make the new_queue empty so that it doesn't call Dispose on any messages.
3792 new_queue.start_ = new_queue.end_;
kasper.lund7276f142008-07-30 08:49:36 +00003793 // Automatic destructor called on new_queue, freeing array_to_free.
3794}
3795
3796
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00003797LockingCommandMessageQueue::LockingCommandMessageQueue(Logger* logger, int size)
jkummerow@chromium.orgdc94e192013-08-30 11:35:42 +00003798 : logger_(logger), queue_(size) {}
kasper.lund7276f142008-07-30 08:49:36 +00003799
3800
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003801bool LockingCommandMessageQueue::IsEmpty() const {
jkummerow@chromium.orgdc94e192013-08-30 11:35:42 +00003802 LockGuard<Mutex> lock_guard(&mutex_);
kasper.lund7276f142008-07-30 08:49:36 +00003803 return queue_.IsEmpty();
3804}
3805
3806
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003807CommandMessage LockingCommandMessageQueue::Get() {
jkummerow@chromium.orgdc94e192013-08-30 11:35:42 +00003808 LockGuard<Mutex> lock_guard(&mutex_);
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003809 CommandMessage result = queue_.Get();
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00003810 logger_->DebugEvent("Get", result.text());
kasper.lund7276f142008-07-30 08:49:36 +00003811 return result;
3812}
3813
3814
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003815void LockingCommandMessageQueue::Put(const CommandMessage& message) {
jkummerow@chromium.orgdc94e192013-08-30 11:35:42 +00003816 LockGuard<Mutex> lock_guard(&mutex_);
kasper.lund7276f142008-07-30 08:49:36 +00003817 queue_.Put(message);
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00003818 logger_->DebugEvent("Put", message.text());
kasper.lund7276f142008-07-30 08:49:36 +00003819}
3820
3821
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003822void LockingCommandMessageQueue::Clear() {
jkummerow@chromium.orgdc94e192013-08-30 11:35:42 +00003823 LockGuard<Mutex> lock_guard(&mutex_);
kasper.lund7276f142008-07-30 08:49:36 +00003824 queue_.Clear();
3825}
3826
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003827
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003828MessageDispatchHelperThread::MessageDispatchHelperThread(Isolate* isolate)
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00003829 : Thread("v8:MsgDispHelpr"),
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +00003830 isolate_(isolate), sem_(0),
jkummerow@chromium.orgdc94e192013-08-30 11:35:42 +00003831 already_signalled_(false) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003832}
3833
3834
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003835void MessageDispatchHelperThread::Schedule() {
3836 {
jkummerow@chromium.orgdc94e192013-08-30 11:35:42 +00003837 LockGuard<Mutex> lock_guard(&mutex_);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003838 if (already_signalled_) {
3839 return;
3840 }
3841 already_signalled_ = true;
3842 }
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +00003843 sem_.Signal();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003844}
3845
3846
3847void MessageDispatchHelperThread::Run() {
3848 while (true) {
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +00003849 sem_.Wait();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003850 {
jkummerow@chromium.orgdc94e192013-08-30 11:35:42 +00003851 LockGuard<Mutex> lock_guard(&mutex_);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003852 already_signalled_ = false;
3853 }
3854 {
mstarzinger@chromium.orgf705b502013-04-04 11:38:09 +00003855 Locker locker(reinterpret_cast<v8::Isolate*>(isolate_));
3856 isolate_->debugger()->CallMessageDispatchHandler();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003857 }
3858 }
3859}
3860
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003861#endif // ENABLE_DEBUGGER_SUPPORT
kasper.lund7276f142008-07-30 08:49:36 +00003862
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003863} } // namespace v8::internal