blob: 3f2739eca040d50a6a70a140db92ba1180e6e94a [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.
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +0000712 obj->Dispose();
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.
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001796 if (!holder.is_null() && holder->IsJSFunction() &&
1797 !JSFunction::cast(*holder)->IsBuiltin()) {
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001798 Handle<JSFunction> js_function = Handle<JSFunction>::cast(holder);
1799 Debug::FloodWithOneShot(js_function);
kasperl@chromium.orgacae3782009-04-11 09:17:08 +00001800 }
1801 } else {
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001802 Debug::FloodWithOneShot(function);
kasperl@chromium.orgacae3782009-04-11 09:17:08 +00001803 }
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001804 }
1805 }
1806}
1807
1808
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001809void Debug::ClearStepping() {
1810 // Clear the various stepping setup.
1811 ClearOneShot();
1812 ClearStepIn();
ager@chromium.orga1645e22009-09-09 19:27:10 +00001813 ClearStepOut();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001814 ClearStepNext();
1815
1816 // Clear multiple step counter.
1817 thread_local_.step_count_ = 0;
1818}
1819
mstarzinger@chromium.orge0e1b0d2013-07-08 08:38:06 +00001820
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001821// Clears all the one-shot break points that are currently set. Normally this
1822// function is called each time a break point is hit as one shot break points
1823// are used to support stepping.
1824void Debug::ClearOneShot() {
1825 // The current implementation just runs through all the breakpoints. When the
v8.team.kasperl727e9952008-09-02 14:56:44 +00001826 // last break point for a function is removed that function is automatically
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001827 // removed from the list.
1828
1829 DebugInfoListNode* node = debug_info_list_;
1830 while (node != NULL) {
1831 BreakLocationIterator it(node->debug_info(), ALL_BREAK_LOCATIONS);
1832 while (!it.Done()) {
1833 it.ClearOneShot();
1834 it.Next();
1835 }
1836 node = node->next();
1837 }
1838}
1839
1840
1841void Debug::ActivateStepIn(StackFrame* frame) {
ager@chromium.orga1645e22009-09-09 19:27:10 +00001842 ASSERT(!StepOutActive());
mmassi@chromium.org7028c052012-06-13 11:51:58 +00001843 thread_local_.step_into_fp_ = frame->UnpaddedFP();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001844}
1845
1846
1847void Debug::ClearStepIn() {
1848 thread_local_.step_into_fp_ = 0;
1849}
1850
1851
ager@chromium.orga1645e22009-09-09 19:27:10 +00001852void Debug::ActivateStepOut(StackFrame* frame) {
1853 ASSERT(!StepInActive());
mmassi@chromium.org7028c052012-06-13 11:51:58 +00001854 thread_local_.step_out_fp_ = frame->UnpaddedFP();
ager@chromium.orga1645e22009-09-09 19:27:10 +00001855}
1856
1857
1858void Debug::ClearStepOut() {
1859 thread_local_.step_out_fp_ = 0;
1860}
1861
1862
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001863void Debug::ClearStepNext() {
1864 thread_local_.last_step_action_ = StepNone;
ager@chromium.org236ad962008-09-25 09:45:57 +00001865 thread_local_.last_statement_position_ = RelocInfo::kNoPosition;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001866 thread_local_.last_fp_ = 0;
1867}
1868
1869
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001870// Helper function to compile full code for debugging. This code will
mmassi@chromium.org7028c052012-06-13 11:51:58 +00001871// have debug break slots and deoptimization information. Deoptimization
1872// information is required in case that an optimized version of this
1873// function is still activated on the stack. It will also make sure that
1874// the full code is compiled with the same flags as the previous version,
1875// that is flags which can change the code generated. The current method
1876// of mapping from already compiled full code without debug break slots
1877// to full code with debug break slots depends on the generated code is
1878// otherwise exactly the same.
1879static bool CompileFullCodeForDebugging(Handle<JSFunction> function,
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001880 Handle<Code> current_code) {
1881 ASSERT(!current_code->has_debug_break_slots());
1882
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001883 CompilationInfoWithZone info(function);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001884 info.MarkCompilingForDebugging(current_code);
1885 ASSERT(!info.shared_info()->is_compiled());
1886 ASSERT(!info.isolate()->has_pending_exception());
1887
1888 // Use compile lazy which will end up compiling the full code in the
1889 // configuration configured above.
1890 bool result = Compiler::CompileLazy(&info);
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +00001891 ASSERT(result != info.isolate()->has_pending_exception());
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001892 info.isolate()->clear_pending_exception();
1893#if DEBUG
1894 if (result) {
mmassi@chromium.org7028c052012-06-13 11:51:58 +00001895 Handle<Code> new_code(function->shared()->code());
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001896 ASSERT(new_code->has_debug_break_slots());
1897 ASSERT(current_code->is_compiled_optimizable() ==
1898 new_code->is_compiled_optimizable());
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001899 }
1900#endif
1901 return result;
1902}
1903
1904
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001905static void CollectActiveFunctionsFromThread(
1906 Isolate* isolate,
1907 ThreadLocalTop* top,
1908 List<Handle<JSFunction> >* active_functions,
1909 Object* active_code_marker) {
1910 // Find all non-optimized code functions with activation frames
1911 // on the stack. This includes functions which have optimized
1912 // activations (including inlined functions) on the stack as the
1913 // non-optimized code is needed for the lazy deoptimization.
1914 for (JavaScriptFrameIterator it(isolate, top); !it.done(); it.Advance()) {
1915 JavaScriptFrame* frame = it.frame();
1916 if (frame->is_optimized()) {
jkummerow@chromium.orgba72ec82013-07-22 09:21:20 +00001917 List<JSFunction*> functions(FLAG_max_inlining_levels + 1);
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001918 frame->GetFunctions(&functions);
1919 for (int i = 0; i < functions.length(); i++) {
1920 JSFunction* function = functions[i];
1921 active_functions->Add(Handle<JSFunction>(function));
1922 function->shared()->code()->set_gc_metadata(active_code_marker);
1923 }
1924 } else if (frame->function()->IsJSFunction()) {
danno@chromium.org169691d2013-07-15 08:01:13 +00001925 JSFunction* function = frame->function();
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001926 ASSERT(frame->LookupCode()->kind() == Code::FUNCTION);
1927 active_functions->Add(Handle<JSFunction>(function));
1928 function->shared()->code()->set_gc_metadata(active_code_marker);
1929 }
1930 }
1931}
1932
1933
1934static void RedirectActivationsToRecompiledCodeOnThread(
1935 Isolate* isolate,
1936 ThreadLocalTop* top) {
1937 for (JavaScriptFrameIterator it(isolate, top); !it.done(); it.Advance()) {
1938 JavaScriptFrame* frame = it.frame();
1939
1940 if (frame->is_optimized() || !frame->function()->IsJSFunction()) continue;
1941
danno@chromium.org169691d2013-07-15 08:01:13 +00001942 JSFunction* function = frame->function();
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001943
1944 ASSERT(frame->LookupCode()->kind() == Code::FUNCTION);
1945
1946 Handle<Code> frame_code(frame->LookupCode());
1947 if (frame_code->has_debug_break_slots()) continue;
1948
1949 Handle<Code> new_code(function->shared()->code());
1950 if (new_code->kind() != Code::FUNCTION ||
1951 !new_code->has_debug_break_slots()) {
1952 continue;
1953 }
1954
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001955 // Iterate over the RelocInfo in the original code to compute the sum of the
1956 // constant pools sizes. (See Assembler::CheckConstPool())
1957 // Note that this is only useful for architectures using constant pools.
1958 int constpool_mask = RelocInfo::ModeMask(RelocInfo::CONST_POOL);
1959 int frame_const_pool_size = 0;
1960 for (RelocIterator it(*frame_code, constpool_mask); !it.done(); it.next()) {
1961 RelocInfo* info = it.rinfo();
1962 if (info->pc() >= frame->pc()) break;
1963 frame_const_pool_size += static_cast<int>(info->data());
1964 }
1965 intptr_t frame_offset =
1966 frame->pc() - frame_code->instruction_start() - frame_const_pool_size;
1967
1968 // Iterate over the RelocInfo for new code to find the number of bytes
1969 // generated for debug slots and constant pools.
1970 int debug_break_slot_bytes = 0;
1971 int new_code_const_pool_size = 0;
1972 int mask = RelocInfo::ModeMask(RelocInfo::DEBUG_BREAK_SLOT) |
1973 RelocInfo::ModeMask(RelocInfo::CONST_POOL);
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001974 for (RelocIterator it(*new_code, mask); !it.done(); it.next()) {
1975 // Check if the pc in the new code with debug break
1976 // slots is before this slot.
1977 RelocInfo* info = it.rinfo();
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001978 intptr_t new_offset = info->pc() - new_code->instruction_start() -
1979 new_code_const_pool_size - debug_break_slot_bytes;
1980 if (new_offset >= frame_offset) {
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001981 break;
1982 }
1983
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001984 if (RelocInfo::IsDebugBreakSlot(info->rmode())) {
1985 debug_break_slot_bytes += Assembler::kDebugBreakSlotLength;
1986 } else {
1987 ASSERT(RelocInfo::IsConstPool(info->rmode()));
1988 // The size of the constant pool is encoded in the data.
1989 new_code_const_pool_size += static_cast<int>(info->data());
1990 }
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001991 }
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001992
1993 // Compute the equivalent pc in the new code.
1994 byte* new_pc = new_code->instruction_start() + frame_offset +
1995 debug_break_slot_bytes + new_code_const_pool_size;
1996
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001997 if (FLAG_trace_deopt) {
1998 PrintF("Replacing code %08" V8PRIxPTR " - %08" V8PRIxPTR " (%d) "
1999 "with %08" V8PRIxPTR " - %08" V8PRIxPTR " (%d) "
2000 "for debugging, "
2001 "changing pc from %08" V8PRIxPTR " to %08" V8PRIxPTR "\n",
2002 reinterpret_cast<intptr_t>(
2003 frame_code->instruction_start()),
2004 reinterpret_cast<intptr_t>(
2005 frame_code->instruction_start()) +
2006 frame_code->instruction_size(),
2007 frame_code->instruction_size(),
2008 reinterpret_cast<intptr_t>(new_code->instruction_start()),
2009 reinterpret_cast<intptr_t>(new_code->instruction_start()) +
2010 new_code->instruction_size(),
2011 new_code->instruction_size(),
2012 reinterpret_cast<intptr_t>(frame->pc()),
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00002013 reinterpret_cast<intptr_t>(new_pc));
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002014 }
2015
2016 // Patch the return address to return into the code with
2017 // debug break slots.
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00002018 frame->set_pc(new_pc);
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002019 }
2020}
2021
2022
2023class ActiveFunctionsCollector : public ThreadVisitor {
2024 public:
2025 explicit ActiveFunctionsCollector(List<Handle<JSFunction> >* active_functions,
2026 Object* active_code_marker)
2027 : active_functions_(active_functions),
2028 active_code_marker_(active_code_marker) { }
2029
2030 void VisitThread(Isolate* isolate, ThreadLocalTop* top) {
2031 CollectActiveFunctionsFromThread(isolate,
2032 top,
2033 active_functions_,
2034 active_code_marker_);
2035 }
2036
2037 private:
2038 List<Handle<JSFunction> >* active_functions_;
2039 Object* active_code_marker_;
2040};
2041
2042
2043class ActiveFunctionsRedirector : public ThreadVisitor {
2044 public:
2045 void VisitThread(Isolate* isolate, ThreadLocalTop* top) {
2046 RedirectActivationsToRecompiledCodeOnThread(isolate, top);
2047 }
2048};
2049
2050
lrn@chromium.org34e60782011-09-15 07:25:40 +00002051void Debug::PrepareForBreakPoints() {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002052 // If preparing for the first break point make sure to deoptimize all
2053 // functions as debugging does not work with optimized code.
2054 if (!has_break_points_) {
rossberg@chromium.org92597162013-08-23 13:28:00 +00002055 if (FLAG_concurrent_recompilation) {
danno@chromium.org59400602013-08-13 17:09:37 +00002056 isolate_->optimizing_compiler_thread()->Flush();
2057 }
2058
svenpanne@chromium.org876cca82013-03-18 14:43:20 +00002059 Deoptimizer::DeoptimizeAll(isolate_);
lrn@chromium.org34e60782011-09-15 07:25:40 +00002060
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002061 Handle<Code> lazy_compile =
2062 Handle<Code>(isolate_->builtins()->builtin(Builtins::kLazyCompile));
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002063
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00002064 // There will be at least one break point when we are done.
2065 has_break_points_ = true;
2066
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002067 // Keep the list of activated functions in a handlified list as it
2068 // is used both in GC and non-GC code.
2069 List<Handle<JSFunction> > active_functions(100);
lrn@chromium.org34e60782011-09-15 07:25:40 +00002070
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002071 {
2072 // We are going to iterate heap to find all functions without
2073 // debug break slots.
hpayer@chromium.org7c3372b2013-02-13 17:26:04 +00002074 Heap* heap = isolate_->heap();
2075 heap->CollectAllGarbage(Heap::kMakeHeapIterableMask,
2076 "preparing for breakpoints");
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002077
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002078 // Ensure no GC in this scope as we are going to use gc_metadata
2079 // field in the Code object to mark active functions.
rossberg@chromium.org79e79022013-06-03 15:43:46 +00002080 DisallowHeapAllocation no_allocation;
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002081
hpayer@chromium.org7c3372b2013-02-13 17:26:04 +00002082 Object* active_code_marker = heap->the_hole_value();
svenpanne@chromium.orgecb9dd62011-12-01 08:22:35 +00002083
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002084 CollectActiveFunctionsFromThread(isolate_,
2085 isolate_->thread_local_top(),
2086 &active_functions,
2087 active_code_marker);
2088 ActiveFunctionsCollector active_functions_collector(&active_functions,
2089 active_code_marker);
2090 isolate_->thread_manager()->IterateArchivedThreads(
2091 &active_functions_collector);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002092
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002093 // Scan the heap for all non-optimized functions which have no
2094 // debug break slots and are not active or inlined into an active
2095 // function and mark them for lazy compilation.
hpayer@chromium.org7c3372b2013-02-13 17:26:04 +00002096 HeapIterator iterator(heap);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002097 HeapObject* obj = NULL;
2098 while (((obj = iterator.next()) != NULL)) {
2099 if (obj->IsJSFunction()) {
2100 JSFunction* function = JSFunction::cast(obj);
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002101 SharedFunctionInfo* shared = function->shared();
mstarzinger@chromium.org1510d582013-06-28 14:00:48 +00002102
2103 if (!shared->allows_lazy_compilation()) continue;
2104 if (!shared->script()->IsScript()) continue;
2105 if (shared->code()->gc_metadata() == active_code_marker) continue;
2106
2107 Code::Kind kind = function->code()->kind();
2108 if (kind == Code::FUNCTION &&
2109 !function->code()->has_debug_break_slots()) {
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002110 function->set_code(*lazy_compile);
2111 function->shared()->set_code(*lazy_compile);
mstarzinger@chromium.org1510d582013-06-28 14:00:48 +00002112 } else if (kind == Code::BUILTIN &&
2113 (function->IsMarkedForInstallingRecompiledCode() ||
2114 function->IsInRecompileQueue() ||
2115 function->IsMarkedForLazyRecompilation() ||
rossberg@chromium.org92597162013-08-23 13:28:00 +00002116 function->IsMarkedForConcurrentRecompilation())) {
mstarzinger@chromium.org1510d582013-06-28 14:00:48 +00002117 // Abort in-flight compilation.
2118 Code* shared_code = function->shared()->code();
2119 if (shared_code->kind() == Code::FUNCTION &&
2120 shared_code->has_debug_break_slots()) {
2121 function->set_code(shared_code);
2122 } else {
2123 function->set_code(*lazy_compile);
2124 function->shared()->set_code(*lazy_compile);
2125 }
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002126 }
2127 }
lrn@chromium.org34e60782011-09-15 07:25:40 +00002128 }
lrn@chromium.org34e60782011-09-15 07:25:40 +00002129
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002130 // Clear gc_metadata field.
2131 for (int i = 0; i < active_functions.length(); i++) {
2132 Handle<JSFunction> function = active_functions[i];
2133 function->shared()->code()->set_gc_metadata(Smi::FromInt(0));
2134 }
2135 }
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002136
2137 // Now recompile all functions with activation frames and and
2138 // patch the return address to run in the new compiled code.
2139 for (int i = 0; i < active_functions.length(); i++) {
2140 Handle<JSFunction> function = active_functions[i];
mmassi@chromium.org7028c052012-06-13 11:51:58 +00002141 Handle<SharedFunctionInfo> shared(function->shared());
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002142
2143 if (function->code()->kind() == Code::FUNCTION &&
2144 function->code()->has_debug_break_slots()) {
2145 // Nothing to do. Function code already had debug break slots.
2146 continue;
2147 }
2148
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002149 // If recompilation is not possible just skip it.
2150 if (shared->is_toplevel() ||
2151 !shared->allows_lazy_compilation() ||
2152 shared->code()->kind() == Code::BUILTIN) {
2153 continue;
2154 }
2155
2156 // Make sure that the shared full code is compiled with debug
2157 // break slots.
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00002158 if (!shared->code()->has_debug_break_slots()) {
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002159 // Try to compile the full code with debug break slots. If it
2160 // fails just keep the current code.
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00002161 Handle<Code> current_code(function->shared()->code());
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002162 shared->set_code(*lazy_compile);
2163 bool prev_force_debugger_active =
2164 isolate_->debugger()->force_debugger_active();
2165 isolate_->debugger()->set_force_debugger_active(true);
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00002166 ASSERT(current_code->kind() == Code::FUNCTION);
mmassi@chromium.org7028c052012-06-13 11:51:58 +00002167 CompileFullCodeForDebugging(function, current_code);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002168 isolate_->debugger()->set_force_debugger_active(
2169 prev_force_debugger_active);
2170 if (!shared->is_compiled()) {
2171 shared->set_code(*current_code);
2172 continue;
2173 }
2174 }
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002175
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002176 // Keep function code in sync with shared function info.
2177 function->set_code(shared->code());
lrn@chromium.org34e60782011-09-15 07:25:40 +00002178 }
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002179
2180 RedirectActivationsToRecompiledCodeOnThread(isolate_,
2181 isolate_->thread_local_top());
2182
2183 ActiveFunctionsRedirector active_functions_redirector;
2184 isolate_->thread_manager()->IterateArchivedThreads(
2185 &active_functions_redirector);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002186 }
lrn@chromium.org34e60782011-09-15 07:25:40 +00002187}
2188
2189
jkummerow@chromium.org78502a92012-09-06 13:50:42 +00002190Object* Debug::FindSharedFunctionInfoInScript(Handle<Script> script,
2191 int position) {
2192 // Iterate the heap looking for SharedFunctionInfo generated from the
2193 // script. The inner most SharedFunctionInfo containing the source position
2194 // for the requested break point is found.
2195 // NOTE: This might require several heap iterations. If the SharedFunctionInfo
2196 // which is found is not compiled it is compiled and the heap is iterated
2197 // again as the compilation might create inner functions from the newly
2198 // compiled function and the actual requested break point might be in one of
2199 // these functions.
2200 // NOTE: The below fix-point iteration depends on all functions that cannot be
2201 // compiled lazily without a context to not be compiled at all. Compilation
2202 // will be triggered at points where we do not need a context.
2203 bool done = false;
2204 // The current candidate for the source position:
2205 int target_start_position = RelocInfo::kNoPosition;
2206 Handle<JSFunction> target_function;
2207 Handle<SharedFunctionInfo> target;
hpayer@chromium.org7c3372b2013-02-13 17:26:04 +00002208 Heap* heap = isolate_->heap();
jkummerow@chromium.org78502a92012-09-06 13:50:42 +00002209 while (!done) {
2210 { // Extra scope for iterator and no-allocation.
hpayer@chromium.org7c3372b2013-02-13 17:26:04 +00002211 heap->EnsureHeapIsIterable();
rossberg@chromium.org79e79022013-06-03 15:43:46 +00002212 DisallowHeapAllocation no_alloc_during_heap_iteration;
hpayer@chromium.org7c3372b2013-02-13 17:26:04 +00002213 HeapIterator iterator(heap);
jkummerow@chromium.org78502a92012-09-06 13:50:42 +00002214 for (HeapObject* obj = iterator.next();
2215 obj != NULL; obj = iterator.next()) {
2216 bool found_next_candidate = false;
2217 Handle<JSFunction> function;
2218 Handle<SharedFunctionInfo> shared;
2219 if (obj->IsJSFunction()) {
2220 function = Handle<JSFunction>(JSFunction::cast(obj));
2221 shared = Handle<SharedFunctionInfo>(function->shared());
2222 ASSERT(shared->allows_lazy_compilation() || shared->is_compiled());
2223 found_next_candidate = true;
2224 } else if (obj->IsSharedFunctionInfo()) {
2225 shared = Handle<SharedFunctionInfo>(SharedFunctionInfo::cast(obj));
2226 // Skip functions that we cannot compile lazily without a context,
2227 // which is not available here, because there is no closure.
2228 found_next_candidate = shared->is_compiled() ||
2229 shared->allows_lazy_compilation_without_context();
2230 }
2231 if (!found_next_candidate) continue;
2232 if (shared->script() == *script) {
2233 // If the SharedFunctionInfo found has the requested script data and
2234 // contains the source position it is a candidate.
2235 int start_position = shared->function_token_position();
2236 if (start_position == RelocInfo::kNoPosition) {
2237 start_position = shared->start_position();
2238 }
2239 if (start_position <= position &&
2240 position <= shared->end_position()) {
2241 // If there is no candidate or this function is within the current
2242 // candidate this is the new candidate.
2243 if (target.is_null()) {
2244 target_start_position = start_position;
2245 target_function = function;
2246 target = shared;
2247 } else {
2248 if (target_start_position == start_position &&
2249 shared->end_position() == target->end_position()) {
2250 // If a top-level function contains only one function
2251 // declaration the source for the top-level and the function
2252 // is the same. In that case prefer the non top-level function.
2253 if (!shared->is_toplevel()) {
2254 target_start_position = start_position;
2255 target_function = function;
2256 target = shared;
2257 }
2258 } else if (target_start_position <= start_position &&
2259 shared->end_position() <= target->end_position()) {
2260 // This containment check includes equality as a function
2261 // inside a top-level function can share either start or end
2262 // position with the top-level function.
2263 target_start_position = start_position;
2264 target_function = function;
2265 target = shared;
2266 }
2267 }
2268 }
2269 }
2270 } // End for loop.
2271 } // End no-allocation scope.
2272
hpayer@chromium.org7c3372b2013-02-13 17:26:04 +00002273 if (target.is_null()) return heap->undefined_value();
jkummerow@chromium.org78502a92012-09-06 13:50:42 +00002274
2275 // There will be at least one break point when we are done.
2276 has_break_points_ = true;
2277
2278 // If the candidate found is compiled we are done.
2279 done = target->is_compiled();
2280 if (!done) {
2281 // If the candidate is not compiled, compile it to reveal any inner
2282 // functions which might contain the requested source position. This
2283 // will compile all inner functions that cannot be compiled without a
2284 // context, because Compiler::BuildFunctionInfo checks whether the
2285 // debugger is active.
2286 if (target_function.is_null()) {
2287 SharedFunctionInfo::CompileLazy(target, KEEP_EXCEPTION);
2288 } else {
2289 JSFunction::CompileLazy(target_function, KEEP_EXCEPTION);
2290 }
2291 }
2292 } // End while loop.
2293
2294 return *target;
2295}
2296
2297
lrn@chromium.org34e60782011-09-15 07:25:40 +00002298// Ensures the debug information is present for shared.
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00002299bool Debug::EnsureDebugInfo(Handle<SharedFunctionInfo> shared,
2300 Handle<JSFunction> function) {
verwaest@chromium.orgd4be0f02013-06-05 13:39:03 +00002301 Isolate* isolate = shared->GetIsolate();
2302
lrn@chromium.org34e60782011-09-15 07:25:40 +00002303 // Return if we already have the debug info for shared.
2304 if (HasDebugInfo(shared)) {
2305 ASSERT(shared->is_compiled());
2306 return true;
2307 }
2308
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00002309 // There will be at least one break point when we are done.
2310 has_break_points_ = true;
2311
2312 // Ensure function is compiled. Return false if this failed.
2313 if (!function.is_null() &&
2314 !JSFunction::EnsureCompiled(function, CLEAR_EXCEPTION)) {
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002315 return false;
2316 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002317
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002318 // Create the debug info object.
verwaest@chromium.orgd4be0f02013-06-05 13:39:03 +00002319 Handle<DebugInfo> debug_info = isolate->factory()->NewDebugInfo(shared);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002320
2321 // Add debug info to the list.
2322 DebugInfoListNode* node = new DebugInfoListNode(*debug_info);
2323 node->set_next(debug_info_list_);
2324 debug_info_list_ = node;
2325
kasper.lundbd3ec4e2008-07-09 11:06:54 +00002326 return true;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002327}
2328
2329
2330void Debug::RemoveDebugInfo(Handle<DebugInfo> debug_info) {
2331 ASSERT(debug_info_list_ != NULL);
2332 // Run through the debug info objects to find this one and remove it.
2333 DebugInfoListNode* prev = NULL;
2334 DebugInfoListNode* current = debug_info_list_;
2335 while (current != NULL) {
2336 if (*current->debug_info() == *debug_info) {
2337 // Unlink from list. If prev is NULL we are looking at the first element.
2338 if (prev == NULL) {
2339 debug_info_list_ = current->next();
2340 } else {
2341 prev->set_next(current->next());
2342 }
lrn@chromium.org7516f052011-03-30 08:52:27 +00002343 current->debug_info()->shared()->set_debug_info(
2344 isolate_->heap()->undefined_value());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002345 delete current;
2346
2347 // If there are no more debug info objects there are not more break
2348 // points.
2349 has_break_points_ = debug_info_list_ != NULL;
2350
2351 return;
2352 }
2353 // Move to next in list.
2354 prev = current;
2355 current = current->next();
2356 }
2357 UNREACHABLE();
2358}
2359
2360
2361void Debug::SetAfterBreakTarget(JavaScriptFrame* frame) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002362 HandleScope scope(isolate_);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002363
lrn@chromium.org34e60782011-09-15 07:25:40 +00002364 PrepareForBreakPoints();
2365
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002366 // Get the executing function in which the debug break occurred.
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00002367 Handle<JSFunction> function(JSFunction::cast(frame->function()));
2368 Handle<SharedFunctionInfo> shared(function->shared());
2369 if (!EnsureDebugInfo(shared, function)) {
kasper.lundbd3ec4e2008-07-09 11:06:54 +00002370 // Return if we failed to retrieve the debug info.
2371 return;
2372 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002373 Handle<DebugInfo> debug_info = GetDebugInfo(shared);
2374 Handle<Code> code(debug_info->code());
2375 Handle<Code> original_code(debug_info->original_code());
2376#ifdef DEBUG
2377 // Get the code which is actually executing.
vegorov@chromium.org74f333b2011-04-06 11:17:46 +00002378 Handle<Code> frame_code(frame->LookupCode());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002379 ASSERT(frame_code.is_identical_to(code));
2380#endif
2381
2382 // Find the call address in the running code. This address holds the call to
2383 // either a DebugBreakXXX or to the debug break return entry code if the
2384 // break point is still active after processing the break point.
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00002385 Address addr = frame->pc() - Assembler::kPatchDebugBreakSlotReturnOffset;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002386
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002387 // Check if the location is at JS exit or debug break slot.
ager@chromium.orga1645e22009-09-09 19:27:10 +00002388 bool at_js_return = false;
2389 bool break_at_js_return_active = false;
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002390 bool at_debug_break_slot = false;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002391 RelocIterator it(debug_info->code());
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002392 while (!it.done() && !at_js_return && !at_debug_break_slot) {
ager@chromium.org236ad962008-09-25 09:45:57 +00002393 if (RelocInfo::IsJSReturn(it.rinfo()->rmode())) {
ager@chromium.orga1645e22009-09-09 19:27:10 +00002394 at_js_return = (it.rinfo()->pc() ==
2395 addr - Assembler::kPatchReturnSequenceAddressOffset);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002396 break_at_js_return_active = it.rinfo()->IsPatchedReturnSequence();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002397 }
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002398 if (RelocInfo::IsDebugBreakSlot(it.rinfo()->rmode())) {
2399 at_debug_break_slot = (it.rinfo()->pc() ==
2400 addr - Assembler::kPatchDebugBreakSlotAddressOffset);
2401 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002402 it.next();
2403 }
2404
2405 // Handle the jump to continue execution after break point depending on the
2406 // break location.
ager@chromium.orga1645e22009-09-09 19:27:10 +00002407 if (at_js_return) {
2408 // If the break point as return is still active jump to the corresponding
2409 // place in the original code. If not the break point was removed during
2410 // break point processing.
2411 if (break_at_js_return_active) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002412 addr += original_code->instruction_start() - code->instruction_start();
2413 }
2414
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00002415 // Move back to where the call instruction sequence started.
2416 thread_local_.after_break_target_ =
2417 addr - Assembler::kPatchReturnSequenceAddressOffset;
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002418 } else if (at_debug_break_slot) {
2419 // Address of where the debug break slot starts.
2420 addr = addr - Assembler::kPatchDebugBreakSlotAddressOffset;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002421
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002422 // Continue just after the slot.
2423 thread_local_.after_break_target_ = addr + Assembler::kDebugBreakSlotLength;
2424 } else if (IsDebugBreak(Assembler::target_address_at(addr))) {
2425 // We now know that there is still a debug break call at the target address,
2426 // so the break point is still there and the original code will hold the
2427 // address to jump to in order to complete the call which is replaced by a
2428 // call to DebugBreakXXX.
2429
2430 // Find the corresponding address in the original code.
2431 addr += original_code->instruction_start() - code->instruction_start();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002432
2433 // Install jump to the call address in the original code. This will be the
2434 // call which was overwritten by the call to DebugBreakXXX.
2435 thread_local_.after_break_target_ = Assembler::target_address_at(addr);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002436 } else {
2437 // There is no longer a break point present. Don't try to look in the
2438 // original code as the running code will have the right address. This takes
2439 // care of the case where the last break point is removed from the function
2440 // and therefore no "original code" is available.
2441 thread_local_.after_break_target_ = Assembler::target_address_at(addr);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002442 }
2443}
2444
2445
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00002446bool Debug::IsBreakAtReturn(JavaScriptFrame* frame) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002447 HandleScope scope(isolate_);
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00002448
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00002449 // If there are no break points this cannot be break at return, as
2450 // the debugger statement and stack guard bebug break cannot be at
2451 // return.
2452 if (!has_break_points_) {
2453 return false;
2454 }
2455
lrn@chromium.org34e60782011-09-15 07:25:40 +00002456 PrepareForBreakPoints();
2457
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00002458 // Get the executing function in which the debug break occurred.
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00002459 Handle<JSFunction> function(JSFunction::cast(frame->function()));
2460 Handle<SharedFunctionInfo> shared(function->shared());
2461 if (!EnsureDebugInfo(shared, function)) {
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00002462 // Return if we failed to retrieve the debug info.
2463 return false;
2464 }
2465 Handle<DebugInfo> debug_info = GetDebugInfo(shared);
2466 Handle<Code> code(debug_info->code());
2467#ifdef DEBUG
2468 // Get the code which is actually executing.
vegorov@chromium.org74f333b2011-04-06 11:17:46 +00002469 Handle<Code> frame_code(frame->LookupCode());
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00002470 ASSERT(frame_code.is_identical_to(code));
2471#endif
2472
2473 // Find the call address in the running code.
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00002474 Address addr = frame->pc() - Assembler::kPatchDebugBreakSlotReturnOffset;
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00002475
2476 // Check if the location is at JS return.
2477 RelocIterator it(debug_info->code());
2478 while (!it.done()) {
2479 if (RelocInfo::IsJSReturn(it.rinfo()->rmode())) {
2480 return (it.rinfo()->pc() ==
2481 addr - Assembler::kPatchReturnSequenceAddressOffset);
2482 }
2483 it.next();
2484 }
2485 return false;
2486}
2487
2488
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00002489void Debug::FramesHaveBeenDropped(StackFrame::Id new_break_frame_id,
whesse@chromium.orge90029b2010-08-02 11:52:17 +00002490 FrameDropMode mode,
2491 Object** restarter_frame_function_pointer) {
ulan@chromium.orgd9e468a2012-06-25 09:47:40 +00002492 if (mode != CURRENTLY_SET_MODE) {
2493 thread_local_.frame_drop_mode_ = mode;
2494 }
ager@chromium.org357bf652010-04-12 11:30:10 +00002495 thread_local_.break_frame_id_ = new_break_frame_id;
whesse@chromium.orge90029b2010-08-02 11:52:17 +00002496 thread_local_.restarter_frame_function_pointer_ =
2497 restarter_frame_function_pointer;
ager@chromium.org357bf652010-04-12 11:30:10 +00002498}
2499
2500
jkummerow@chromium.org212d9642012-05-11 15:02:09 +00002501const int Debug::FramePaddingLayout::kInitialSize = 1;
2502
2503
2504// Any even value bigger than kInitialSize as needed for stack scanning.
2505const int Debug::FramePaddingLayout::kPaddingValue = kInitialSize + 1;
2506
2507
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002508bool Debug::IsDebugGlobal(GlobalObject* global) {
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00002509 return IsLoaded() && global == debug_context()->global_object();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002510}
2511
2512
ager@chromium.org32912102009-01-16 10:38:43 +00002513void Debug::ClearMirrorCache() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002514 PostponeInterruptsScope postpone(isolate_);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002515 HandleScope scope(isolate_);
2516 ASSERT(isolate_->context() == *Debug::debug_context());
ager@chromium.org32912102009-01-16 10:38:43 +00002517
2518 // Clear the mirror cache.
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00002519 Handle<String> function_name = isolate_->factory()->InternalizeOneByteString(
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00002520 STATIC_ASCII_VECTOR("ClearMirrorCache"));
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00002521 Handle<Object> fun(
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00002522 isolate_->global_object()->GetPropertyNoExceptionThrown(*function_name),
2523 isolate_);
ager@chromium.org32912102009-01-16 10:38:43 +00002524 ASSERT(fun->IsJSFunction());
2525 bool caught_exception;
rossberg@chromium.org717967f2011-07-20 13:44:42 +00002526 Execution::TryCall(Handle<JSFunction>::cast(fun),
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00002527 Handle<JSObject>(Debug::debug_context()->global_object()),
ager@chromium.org32912102009-01-16 10:38:43 +00002528 0, NULL, &caught_exception);
2529}
2530
2531
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002532void Debug::CreateScriptCache() {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002533 Heap* heap = isolate_->heap();
2534 HandleScope scope(isolate_);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002535
2536 // Perform two GCs to get rid of all unreferenced scripts. The first GC gets
2537 // rid of all the cached script wrappers and the second gets rid of the
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002538 // scripts which are no longer referenced. The second also sweeps precisely,
2539 // which saves us doing yet another GC to make the heap iterable.
rossberg@chromium.org994edf62012-02-06 10:12:55 +00002540 heap->CollectAllGarbage(Heap::kNoGCFlags, "Debug::CreateScriptCache");
2541 heap->CollectAllGarbage(Heap::kMakeHeapIterableMask,
2542 "Debug::CreateScriptCache");
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002543
2544 ASSERT(script_cache_ == NULL);
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +00002545 script_cache_ = new ScriptCache(isolate_);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002546
2547 // Scan heap for Script objects.
2548 int count = 0;
hpayer@chromium.org7c3372b2013-02-13 17:26:04 +00002549 HeapIterator iterator(heap);
rossberg@chromium.org79e79022013-06-03 15:43:46 +00002550 DisallowHeapAllocation no_allocation;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002551
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002552 for (HeapObject* obj = iterator.next(); obj != NULL; obj = iterator.next()) {
sgjesse@chromium.org152a0b02009-10-07 13:50:16 +00002553 if (obj->IsScript() && Script::cast(obj)->HasValidSource()) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002554 script_cache_->Add(Handle<Script>(Script::cast(obj)));
2555 count++;
2556 }
2557 }
2558}
2559
2560
2561void Debug::DestroyScriptCache() {
2562 // Get rid of the script cache if it was created.
2563 if (script_cache_ != NULL) {
2564 delete script_cache_;
2565 script_cache_ = NULL;
2566 }
2567}
2568
2569
2570void Debug::AddScriptToScriptCache(Handle<Script> script) {
2571 if (script_cache_ != NULL) {
2572 script_cache_->Add(script);
2573 }
2574}
2575
2576
2577Handle<FixedArray> Debug::GetLoadedScripts() {
2578 // Create and fill the script cache when the loaded scripts is requested for
2579 // the first time.
2580 if (script_cache_ == NULL) {
2581 CreateScriptCache();
2582 }
2583
2584 // If the script cache is not active just return an empty array.
2585 ASSERT(script_cache_ != NULL);
2586 if (script_cache_ == NULL) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002587 isolate_->factory()->NewFixedArray(0);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002588 }
2589
2590 // Perform GC to get unreferenced scripts evicted from the cache before
2591 // returning the content.
rossberg@chromium.org994edf62012-02-06 10:12:55 +00002592 isolate_->heap()->CollectAllGarbage(Heap::kNoGCFlags,
2593 "Debug::GetLoadedScripts");
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002594
2595 // Get the scripts from the cache.
2596 return script_cache_->GetScripts();
2597}
2598
2599
2600void Debug::AfterGarbageCollection() {
2601 // Generate events for collected scripts.
2602 if (script_cache_ != NULL) {
2603 script_cache_->ProcessCollectedScripts();
2604 }
2605}
2606
2607
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00002608Debugger::Debugger(Isolate* isolate)
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +00002609 : debugger_access_(isolate->debugger_access()),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002610 event_listener_(Handle<Object>()),
2611 event_listener_data_(Handle<Object>()),
2612 compiling_natives_(false),
2613 is_loading_debugger_(false),
mstarzinger@chromium.orgde886792012-09-11 13:22:37 +00002614 live_edit_enabled_(true),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002615 never_unload_debugger_(false),
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002616 force_debugger_active_(false),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002617 message_handler_(NULL),
2618 debugger_unload_pending_(false),
2619 host_dispatch_handler_(NULL),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002620 debug_message_dispatch_handler_(NULL),
2621 message_dispatch_helper_thread_(NULL),
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +00002622 host_dispatch_period_(TimeDelta::FromMilliseconds(100)),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002623 agent_(NULL),
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00002624 command_queue_(isolate->logger(), kQueueInitialSize),
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +00002625 command_received_(0),
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00002626 event_command_queue_(isolate->logger(), kQueueInitialSize),
2627 isolate_(isolate) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002628}
2629
2630
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +00002631Debugger::~Debugger() {}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002632
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002633
2634Handle<Object> Debugger::MakeJSObject(Vector<const char> constructor_name,
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002635 int argc,
2636 Handle<Object> argv[],
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002637 bool* caught_exception) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002638 ASSERT(isolate_->context() == *isolate_->debug()->debug_context());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002639
2640 // Create the execution state object.
lrn@chromium.org7516f052011-03-30 08:52:27 +00002641 Handle<String> constructor_str =
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00002642 isolate_->factory()->InternalizeUtf8String(constructor_name);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002643 Handle<Object> constructor(
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00002644 isolate_->global_object()->GetPropertyNoExceptionThrown(*constructor_str),
2645 isolate_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002646 ASSERT(constructor->IsJSFunction());
2647 if (!constructor->IsJSFunction()) {
2648 *caught_exception = true;
lrn@chromium.org7516f052011-03-30 08:52:27 +00002649 return isolate_->factory()->undefined_value();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002650 }
2651 Handle<Object> js_object = Execution::TryCall(
2652 Handle<JSFunction>::cast(constructor),
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00002653 Handle<JSObject>(isolate_->debug()->debug_context()->global_object()),
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002654 argc,
2655 argv,
2656 caught_exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002657 return js_object;
2658}
2659
2660
2661Handle<Object> Debugger::MakeExecutionState(bool* caught_exception) {
2662 // Create the execution state object.
lrn@chromium.org7516f052011-03-30 08:52:27 +00002663 Handle<Object> break_id = isolate_->factory()->NewNumberFromInt(
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002664 isolate_->debug()->break_id());
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002665 Handle<Object> argv[] = { break_id };
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002666 return MakeJSObject(CStrVector("MakeExecutionState"),
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002667 ARRAY_SIZE(argv),
2668 argv,
2669 caught_exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002670}
2671
2672
2673Handle<Object> Debugger::MakeBreakEvent(Handle<Object> exec_state,
2674 Handle<Object> break_points_hit,
2675 bool* caught_exception) {
2676 // Create the new break event object.
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002677 Handle<Object> argv[] = { exec_state, break_points_hit };
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002678 return MakeJSObject(CStrVector("MakeBreakEvent"),
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002679 ARRAY_SIZE(argv),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002680 argv,
2681 caught_exception);
2682}
2683
2684
2685Handle<Object> Debugger::MakeExceptionEvent(Handle<Object> exec_state,
2686 Handle<Object> exception,
2687 bool uncaught,
2688 bool* caught_exception) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002689 Factory* factory = isolate_->factory();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002690 // Create the new exception event object.
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002691 Handle<Object> argv[] = { exec_state,
2692 exception,
2693 factory->ToBoolean(uncaught) };
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002694 return MakeJSObject(CStrVector("MakeExceptionEvent"),
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002695 ARRAY_SIZE(argv),
2696 argv,
2697 caught_exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002698}
2699
2700
2701Handle<Object> Debugger::MakeNewFunctionEvent(Handle<Object> function,
2702 bool* caught_exception) {
2703 // Create the new function event object.
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002704 Handle<Object> argv[] = { function };
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002705 return MakeJSObject(CStrVector("MakeNewFunctionEvent"),
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002706 ARRAY_SIZE(argv),
2707 argv,
2708 caught_exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002709}
2710
2711
2712Handle<Object> Debugger::MakeCompileEvent(Handle<Script> script,
iposva@chromium.org245aa852009-02-10 00:49:54 +00002713 bool before,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002714 bool* caught_exception) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002715 Factory* factory = isolate_->factory();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002716 // Create the compile event object.
2717 Handle<Object> exec_state = MakeExecutionState(caught_exception);
iposva@chromium.org245aa852009-02-10 00:49:54 +00002718 Handle<Object> script_wrapper = GetScriptWrapper(script);
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002719 Handle<Object> argv[] = { exec_state,
2720 script_wrapper,
2721 factory->ToBoolean(before) };
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002722 return MakeJSObject(CStrVector("MakeCompileEvent"),
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002723 ARRAY_SIZE(argv),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002724 argv,
2725 caught_exception);
2726}
2727
2728
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002729Handle<Object> Debugger::MakeScriptCollectedEvent(int id,
2730 bool* caught_exception) {
2731 // Create the script collected event object.
2732 Handle<Object> exec_state = MakeExecutionState(caught_exception);
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00002733 Handle<Object> id_object = Handle<Smi>(Smi::FromInt(id), isolate_);
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002734 Handle<Object> argv[] = { exec_state, id_object };
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002735
2736 return MakeJSObject(CStrVector("MakeScriptCollectedEvent"),
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002737 ARRAY_SIZE(argv),
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002738 argv,
2739 caught_exception);
2740}
2741
2742
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002743void Debugger::OnException(Handle<Object> exception, bool uncaught) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002744 HandleScope scope(isolate_);
2745 Debug* debug = isolate_->debug();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002746
2747 // Bail out based on state or if there is no listener for this event
lrn@chromium.org7516f052011-03-30 08:52:27 +00002748 if (debug->InDebugger()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002749 if (!Debugger::EventActive(v8::Exception)) return;
2750
2751 // Bail out if exception breaks are not active
2752 if (uncaught) {
2753 // Uncaught exceptions are reported by either flags.
lrn@chromium.org7516f052011-03-30 08:52:27 +00002754 if (!(debug->break_on_uncaught_exception() ||
2755 debug->break_on_exception())) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002756 } else {
2757 // Caught exceptions are reported is activated.
lrn@chromium.org7516f052011-03-30 08:52:27 +00002758 if (!debug->break_on_exception()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002759 }
2760
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002761 // Enter the debugger.
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +00002762 EnterDebugger debugger(isolate_);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002763 if (debugger.FailedToEnter()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002764
2765 // Clear all current stepping setup.
lrn@chromium.org7516f052011-03-30 08:52:27 +00002766 debug->ClearStepping();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002767 // Create the event data object.
2768 bool caught_exception = false;
2769 Handle<Object> exec_state = MakeExecutionState(&caught_exception);
2770 Handle<Object> event_data;
2771 if (!caught_exception) {
2772 event_data = MakeExceptionEvent(exec_state, exception, uncaught,
2773 &caught_exception);
2774 }
2775 // Bail out and don't call debugger if exception.
2776 if (caught_exception) {
2777 return;
2778 }
2779
ager@chromium.org5ec48922009-05-05 07:25:34 +00002780 // Process debug event.
2781 ProcessDebugEvent(v8::Exception, Handle<JSObject>::cast(event_data), false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002782 // Return to continue execution from where the exception was thrown.
2783}
2784
2785
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002786void Debugger::OnDebugBreak(Handle<Object> break_points_hit,
2787 bool auto_continue) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002788 HandleScope scope(isolate_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002789
kasper.lund212ac232008-07-16 07:07:30 +00002790 // Debugger has already been entered by caller.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002791 ASSERT(isolate_->context() == *isolate_->debug()->debug_context());
kasper.lund212ac232008-07-16 07:07:30 +00002792
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002793 // Bail out if there is no listener for this event
2794 if (!Debugger::EventActive(v8::Break)) return;
2795
2796 // Debugger must be entered in advance.
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00002797 ASSERT(isolate_->context() == *isolate_->debug()->debug_context());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002798
2799 // Create the event data object.
2800 bool caught_exception = false;
2801 Handle<Object> exec_state = MakeExecutionState(&caught_exception);
2802 Handle<Object> event_data;
2803 if (!caught_exception) {
2804 event_data = MakeBreakEvent(exec_state, break_points_hit,
2805 &caught_exception);
2806 }
2807 // Bail out and don't call debugger if exception.
2808 if (caught_exception) {
2809 return;
2810 }
2811
ager@chromium.org5ec48922009-05-05 07:25:34 +00002812 // Process debug event.
2813 ProcessDebugEvent(v8::Break,
2814 Handle<JSObject>::cast(event_data),
2815 auto_continue);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002816}
2817
2818
2819void Debugger::OnBeforeCompile(Handle<Script> script) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002820 HandleScope scope(isolate_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002821
2822 // Bail out based on state or if there is no listener for this event
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002823 if (isolate_->debug()->InDebugger()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002824 if (compiling_natives()) return;
2825 if (!EventActive(v8::BeforeCompile)) return;
2826
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002827 // Enter the debugger.
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +00002828 EnterDebugger debugger(isolate_);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002829 if (debugger.FailedToEnter()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002830
2831 // Create the event data object.
2832 bool caught_exception = false;
iposva@chromium.org245aa852009-02-10 00:49:54 +00002833 Handle<Object> event_data = MakeCompileEvent(script, true, &caught_exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002834 // Bail out and don't call debugger if exception.
2835 if (caught_exception) {
2836 return;
2837 }
2838
ager@chromium.org5ec48922009-05-05 07:25:34 +00002839 // Process debug event.
2840 ProcessDebugEvent(v8::BeforeCompile,
2841 Handle<JSObject>::cast(event_data),
2842 true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002843}
2844
2845
2846// Handle debugger actions when a new script is compiled.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00002847void Debugger::OnAfterCompile(Handle<Script> script,
2848 AfterCompileFlags after_compile_flags) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002849 HandleScope scope(isolate_);
2850 Debug* debug = isolate_->debug();
kasper.lund212ac232008-07-16 07:07:30 +00002851
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002852 // Add the newly compiled script to the script cache.
lrn@chromium.org7516f052011-03-30 08:52:27 +00002853 debug->AddScriptToScriptCache(script);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002854
2855 // No more to do if not debugging.
ager@chromium.org71daaf62009-04-01 07:22:49 +00002856 if (!IsDebuggerActive()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002857
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002858 // No compile events while compiling natives.
2859 if (compiling_natives()) return;
2860
iposva@chromium.org245aa852009-02-10 00:49:54 +00002861 // Store whether in debugger before entering debugger.
lrn@chromium.org7516f052011-03-30 08:52:27 +00002862 bool in_debugger = debug->InDebugger();
iposva@chromium.org245aa852009-02-10 00:49:54 +00002863
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002864 // Enter the debugger.
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +00002865 EnterDebugger debugger(isolate_);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002866 if (debugger.FailedToEnter()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002867
2868 // If debugging there might be script break points registered for this
2869 // script. Make sure that these break points are set.
2870
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002871 // Get the function UpdateScriptBreakPoints (defined in debug-debugger.js).
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00002872 Handle<String> update_script_break_points_string =
2873 isolate_->factory()->InternalizeOneByteString(
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00002874 STATIC_ASCII_VECTOR("UpdateScriptBreakPoints"));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002875 Handle<Object> update_script_break_points =
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00002876 Handle<Object>(
2877 debug->debug_context()->global_object()->GetPropertyNoExceptionThrown(
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00002878 *update_script_break_points_string),
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00002879 isolate_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002880 if (!update_script_break_points->IsJSFunction()) {
2881 return;
2882 }
2883 ASSERT(update_script_break_points->IsJSFunction());
2884
2885 // Wrap the script object in a proper JS object before passing it
2886 // to JavaScript.
2887 Handle<JSValue> wrapper = GetScriptWrapper(script);
2888
2889 // Call UpdateScriptBreakPoints expect no exceptions.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002890 bool caught_exception;
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002891 Handle<Object> argv[] = { wrapper };
rossberg@chromium.org717967f2011-07-20 13:44:42 +00002892 Execution::TryCall(Handle<JSFunction>::cast(update_script_break_points),
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +00002893 isolate_->js_builtins_object(),
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002894 ARRAY_SIZE(argv),
2895 argv,
2896 &caught_exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002897 if (caught_exception) {
2898 return;
2899 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002900 // Bail out based on state or if there is no listener for this event
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00002901 if (in_debugger && (after_compile_flags & SEND_WHEN_DEBUGGING) == 0) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002902 if (!Debugger::EventActive(v8::AfterCompile)) return;
2903
2904 // Create the compile state object.
2905 Handle<Object> event_data = MakeCompileEvent(script,
iposva@chromium.org245aa852009-02-10 00:49:54 +00002906 false,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002907 &caught_exception);
2908 // Bail out and don't call debugger if exception.
2909 if (caught_exception) {
2910 return;
2911 }
ager@chromium.org5ec48922009-05-05 07:25:34 +00002912 // Process debug event.
2913 ProcessDebugEvent(v8::AfterCompile,
2914 Handle<JSObject>::cast(event_data),
2915 true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002916}
2917
2918
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002919void Debugger::OnScriptCollected(int id) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002920 HandleScope scope(isolate_);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002921
2922 // No more to do if not debugging.
ulan@chromium.org56c14af2012-09-20 12:51:09 +00002923 if (isolate_->debug()->InDebugger()) return;
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002924 if (!IsDebuggerActive()) return;
2925 if (!Debugger::EventActive(v8::ScriptCollected)) return;
2926
2927 // Enter the debugger.
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +00002928 EnterDebugger debugger(isolate_);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002929 if (debugger.FailedToEnter()) return;
2930
2931 // Create the script collected state object.
2932 bool caught_exception = false;
2933 Handle<Object> event_data = MakeScriptCollectedEvent(id,
2934 &caught_exception);
2935 // Bail out and don't call debugger if exception.
2936 if (caught_exception) {
2937 return;
2938 }
2939
2940 // Process debug event.
2941 ProcessDebugEvent(v8::ScriptCollected,
2942 Handle<JSObject>::cast(event_data),
2943 true);
2944}
2945
2946
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002947void Debugger::ProcessDebugEvent(v8::DebugEvent event,
ager@chromium.org5ec48922009-05-05 07:25:34 +00002948 Handle<JSObject> event_data,
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002949 bool auto_continue) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002950 HandleScope scope(isolate_);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002951
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +00002952 // Clear any pending debug break if this is a real break.
2953 if (!auto_continue) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002954 isolate_->debug()->clear_interrupt_pending(DEBUGBREAK);
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +00002955 }
2956
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002957 // Create the execution state.
2958 bool caught_exception = false;
2959 Handle<Object> exec_state = MakeExecutionState(&caught_exception);
2960 if (caught_exception) {
2961 return;
2962 }
ager@chromium.org41826e72009-03-30 13:30:57 +00002963 // First notify the message handler if any.
2964 if (message_handler_ != NULL) {
ager@chromium.org5ec48922009-05-05 07:25:34 +00002965 NotifyMessageHandler(event,
2966 Handle<JSObject>::cast(exec_state),
2967 event_data,
2968 auto_continue);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002969 }
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00002970 // Notify registered debug event listener. This can be either a C or
2971 // a JavaScript function. Don't call event listener for v8::Break
2972 // here, if it's only a debug command -- they will be processed later.
2973 if ((event != v8::Break || !auto_continue) && !event_listener_.is_null()) {
2974 CallEventCallback(event, exec_state, event_data, NULL);
2975 }
2976 // Process pending debug commands.
2977 if (event == v8::Break) {
2978 while (!event_command_queue_.IsEmpty()) {
2979 CommandMessage command = event_command_queue_.Get();
2980 if (!event_listener_.is_null()) {
2981 CallEventCallback(v8::BreakForCommand,
2982 exec_state,
2983 event_data,
2984 command.client_data());
2985 }
2986 command.Dispose();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002987 }
2988 }
2989}
2990
2991
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00002992void Debugger::CallEventCallback(v8::DebugEvent event,
2993 Handle<Object> exec_state,
2994 Handle<Object> event_data,
2995 v8::Debug::ClientData* client_data) {
ager@chromium.orgea91cc52011-05-23 06:06:11 +00002996 if (event_listener_->IsForeign()) {
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00002997 CallCEventCallback(event, exec_state, event_data, client_data);
2998 } else {
2999 CallJSEventCallback(event, exec_state, event_data);
3000 }
3001}
3002
3003
3004void Debugger::CallCEventCallback(v8::DebugEvent event,
3005 Handle<Object> exec_state,
3006 Handle<Object> event_data,
3007 v8::Debug::ClientData* client_data) {
ager@chromium.orgea91cc52011-05-23 06:06:11 +00003008 Handle<Foreign> callback_obj(Handle<Foreign>::cast(event_listener_));
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00003009 v8::Debug::EventCallback2 callback =
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003010 FUNCTION_CAST<v8::Debug::EventCallback2>(
3011 callback_obj->foreign_address());
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00003012 EventDetailsImpl event_details(
3013 event,
3014 Handle<JSObject>::cast(exec_state),
3015 Handle<JSObject>::cast(event_data),
3016 event_listener_data_,
3017 client_data);
3018 callback(event_details);
3019}
3020
3021
3022void Debugger::CallJSEventCallback(v8::DebugEvent event,
3023 Handle<Object> exec_state,
3024 Handle<Object> event_data) {
3025 ASSERT(event_listener_->IsJSFunction());
3026 Handle<JSFunction> fun(Handle<JSFunction>::cast(event_listener_));
3027
3028 // Invoke the JavaScript debug event listener.
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00003029 Handle<Object> argv[] = { Handle<Object>(Smi::FromInt(event), isolate_),
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00003030 exec_state,
3031 event_data,
3032 event_listener_data_ };
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003033 bool caught_exception;
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00003034 Execution::TryCall(fun,
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00003035 isolate_->global_object(),
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00003036 ARRAY_SIZE(argv),
3037 argv,
3038 &caught_exception);
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00003039 // Silently ignore exceptions from debug event listeners.
3040}
3041
3042
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00003043Handle<Context> Debugger::GetDebugContext() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003044 never_unload_debugger_ = true;
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +00003045 EnterDebugger debugger(isolate_);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003046 return isolate_->debug()->debug_context();
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00003047}
3048
3049
ager@chromium.org71daaf62009-04-01 07:22:49 +00003050void Debugger::UnloadDebugger() {
lrn@chromium.org7516f052011-03-30 08:52:27 +00003051 Debug* debug = isolate_->debug();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003052
ager@chromium.org71daaf62009-04-01 07:22:49 +00003053 // Make sure that there are no breakpoints left.
lrn@chromium.org7516f052011-03-30 08:52:27 +00003054 debug->ClearAllBreakPoints();
ager@chromium.org71daaf62009-04-01 07:22:49 +00003055
3056 // Unload the debugger if feasible.
3057 if (!never_unload_debugger_) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00003058 debug->Unload();
ager@chromium.org71daaf62009-04-01 07:22:49 +00003059 }
3060
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003061 // Clear the flag indicating that the debugger should be unloaded.
3062 debugger_unload_pending_ = false;
ager@chromium.org71daaf62009-04-01 07:22:49 +00003063}
3064
3065
ager@chromium.org41826e72009-03-30 13:30:57 +00003066void Debugger::NotifyMessageHandler(v8::DebugEvent event,
ager@chromium.org5ec48922009-05-05 07:25:34 +00003067 Handle<JSObject> exec_state,
3068 Handle<JSObject> event_data,
ager@chromium.org41826e72009-03-30 13:30:57 +00003069 bool auto_continue) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00003070 HandleScope scope(isolate_);
ager@chromium.org41826e72009-03-30 13:30:57 +00003071
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003072 if (!isolate_->debug()->Load()) return;
ager@chromium.org41826e72009-03-30 13:30:57 +00003073
3074 // Process the individual events.
ager@chromium.org5ec48922009-05-05 07:25:34 +00003075 bool sendEventMessage = false;
ager@chromium.org41826e72009-03-30 13:30:57 +00003076 switch (event) {
3077 case v8::Break:
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00003078 case v8::BreakForCommand:
ager@chromium.org5ec48922009-05-05 07:25:34 +00003079 sendEventMessage = !auto_continue;
ager@chromium.org41826e72009-03-30 13:30:57 +00003080 break;
3081 case v8::Exception:
ager@chromium.org5ec48922009-05-05 07:25:34 +00003082 sendEventMessage = true;
ager@chromium.org41826e72009-03-30 13:30:57 +00003083 break;
3084 case v8::BeforeCompile:
3085 break;
3086 case v8::AfterCompile:
ager@chromium.org5ec48922009-05-05 07:25:34 +00003087 sendEventMessage = true;
ager@chromium.org41826e72009-03-30 13:30:57 +00003088 break;
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003089 case v8::ScriptCollected:
3090 sendEventMessage = true;
3091 break;
ager@chromium.org41826e72009-03-30 13:30:57 +00003092 case v8::NewFunction:
3093 break;
3094 default:
3095 UNREACHABLE();
3096 }
3097
ager@chromium.org5ec48922009-05-05 07:25:34 +00003098 // The debug command interrupt flag might have been set when the command was
3099 // added. It should be enough to clear the flag only once while we are in the
3100 // debugger.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003101 ASSERT(isolate_->debug()->InDebugger());
3102 isolate_->stack_guard()->Continue(DEBUGCOMMAND);
ager@chromium.org5ec48922009-05-05 07:25:34 +00003103
3104 // Notify the debugger that a debug event has occurred unless auto continue is
3105 // active in which case no event is send.
3106 if (sendEventMessage) {
3107 MessageImpl message = MessageImpl::NewEvent(
3108 event,
3109 auto_continue,
3110 Handle<JSObject>::cast(exec_state),
3111 Handle<JSObject>::cast(event_data));
3112 InvokeMessageHandler(message);
3113 }
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +00003114
3115 // If auto continue don't make the event cause a break, but process messages
3116 // in the queue if any. For script collected events don't even process
3117 // messages in the queue as the execution state might not be what is expected
3118 // by the client.
ager@chromium.org6ffc2172009-05-29 19:20:16 +00003119 if ((auto_continue && !HasCommands()) || event == v8::ScriptCollected) {
ager@chromium.org5ec48922009-05-05 07:25:34 +00003120 return;
3121 }
ager@chromium.org41826e72009-03-30 13:30:57 +00003122
ager@chromium.org41826e72009-03-30 13:30:57 +00003123 v8::TryCatch try_catch;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003124
3125 // DebugCommandProcessor goes here.
3126 v8::Local<v8::Object> cmd_processor;
3127 {
3128 v8::Local<v8::Object> api_exec_state =
3129 v8::Utils::ToLocal(Handle<JSObject>::cast(exec_state));
3130 v8::Local<v8::String> fun_name =
3131 v8::String::New("debugCommandProcessor");
3132 v8::Local<v8::Function> fun =
danno@chromium.orgf95d4b92013-06-13 14:40:17 +00003133 v8::Local<v8::Function>::Cast(api_exec_state->Get(fun_name));
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003134
3135 v8::Handle<v8::Boolean> running =
3136 auto_continue ? v8::True() : v8::False();
3137 static const int kArgc = 1;
3138 v8::Handle<Value> argv[kArgc] = { running };
danno@chromium.orgf95d4b92013-06-13 14:40:17 +00003139 cmd_processor = v8::Local<v8::Object>::Cast(
3140 fun->Call(api_exec_state, kArgc, argv));
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003141 if (try_catch.HasCaught()) {
3142 PrintLn(try_catch.Exception());
3143 return;
3144 }
ager@chromium.org41826e72009-03-30 13:30:57 +00003145 }
3146
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003147 bool running = auto_continue;
3148
ager@chromium.org41826e72009-03-30 13:30:57 +00003149 // Process requests from the debugger.
3150 while (true) {
3151 // Wait for new command in the queue.
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003152 if (Debugger::host_dispatch_handler_) {
3153 // In case there is a host dispatch - do periodic dispatches.
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +00003154 if (!command_received_.WaitFor(host_dispatch_period_)) {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003155 // Timout expired, do the dispatch.
3156 Debugger::host_dispatch_handler_();
3157 continue;
3158 }
3159 } else {
3160 // In case there is no host dispatch - just wait.
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +00003161 command_received_.Wait();
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003162 }
ager@chromium.org41826e72009-03-30 13:30:57 +00003163
ager@chromium.org41826e72009-03-30 13:30:57 +00003164 // Get the command from the queue.
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003165 CommandMessage command = command_queue_.Get();
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00003166 isolate_->logger()->DebugTag(
3167 "Got request from command queue, in interactive loop.");
ager@chromium.org71daaf62009-04-01 07:22:49 +00003168 if (!Debugger::IsDebuggerActive()) {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003169 // Delete command text and user data.
3170 command.Dispose();
ager@chromium.org41826e72009-03-30 13:30:57 +00003171 return;
3172 }
3173
ager@chromium.org41826e72009-03-30 13:30:57 +00003174 // Invoke JavaScript to process the debug request.
3175 v8::Local<v8::String> fun_name;
3176 v8::Local<v8::Function> fun;
3177 v8::Local<v8::Value> request;
3178 v8::TryCatch try_catch;
3179 fun_name = v8::String::New("processDebugRequest");
danno@chromium.orgf95d4b92013-06-13 14:40:17 +00003180 fun = v8::Local<v8::Function>::Cast(cmd_processor->Get(fun_name));
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003181
3182 request = v8::String::New(command.text().start(),
3183 command.text().length());
ager@chromium.org41826e72009-03-30 13:30:57 +00003184 static const int kArgc = 1;
3185 v8::Handle<Value> argv[kArgc] = { request };
3186 v8::Local<v8::Value> response_val = fun->Call(cmd_processor, kArgc, argv);
3187
3188 // Get the response.
3189 v8::Local<v8::String> response;
ager@chromium.org41826e72009-03-30 13:30:57 +00003190 if (!try_catch.HasCaught()) {
3191 // Get response string.
3192 if (!response_val->IsUndefined()) {
danno@chromium.orgf95d4b92013-06-13 14:40:17 +00003193 response = v8::Local<v8::String>::Cast(response_val);
ager@chromium.org41826e72009-03-30 13:30:57 +00003194 } else {
3195 response = v8::String::New("");
3196 }
3197
3198 // Log the JSON request/response.
3199 if (FLAG_trace_debug_json) {
3200 PrintLn(request);
3201 PrintLn(response);
3202 }
3203
3204 // Get the running state.
3205 fun_name = v8::String::New("isRunning");
danno@chromium.orgf95d4b92013-06-13 14:40:17 +00003206 fun = v8::Local<v8::Function>::Cast(cmd_processor->Get(fun_name));
ager@chromium.org41826e72009-03-30 13:30:57 +00003207 static const int kArgc = 1;
3208 v8::Handle<Value> argv[kArgc] = { response };
3209 v8::Local<v8::Value> running_val = fun->Call(cmd_processor, kArgc, argv);
3210 if (!try_catch.HasCaught()) {
3211 running = running_val->ToBoolean()->Value();
3212 }
3213 } else {
3214 // In case of failure the result text is the exception text.
3215 response = try_catch.Exception()->ToString();
3216 }
3217
ager@chromium.org41826e72009-03-30 13:30:57 +00003218 // Return the result.
ager@chromium.org5ec48922009-05-05 07:25:34 +00003219 MessageImpl message = MessageImpl::NewResponse(
3220 event,
3221 running,
3222 Handle<JSObject>::cast(exec_state),
3223 Handle<JSObject>::cast(event_data),
3224 Handle<String>(Utils::OpenHandle(*response)),
3225 command.client_data());
3226 InvokeMessageHandler(message);
3227 command.Dispose();
ager@chromium.org41826e72009-03-30 13:30:57 +00003228
3229 // Return from debug event processing if either the VM is put into the
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00003230 // running state (through a continue command) or auto continue is active
ager@chromium.org41826e72009-03-30 13:30:57 +00003231 // and there are no more commands queued.
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003232 if (running && !HasCommands()) {
ager@chromium.org41826e72009-03-30 13:30:57 +00003233 return;
3234 }
3235 }
3236}
3237
3238
iposva@chromium.org245aa852009-02-10 00:49:54 +00003239void Debugger::SetEventListener(Handle<Object> callback,
3240 Handle<Object> data) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00003241 HandleScope scope(isolate_);
3242 GlobalHandles* global_handles = isolate_->global_handles();
iposva@chromium.org245aa852009-02-10 00:49:54 +00003243
3244 // Clear the global handles for the event listener and the event listener data
3245 // object.
3246 if (!event_listener_.is_null()) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00003247 global_handles->Destroy(
iposva@chromium.org245aa852009-02-10 00:49:54 +00003248 reinterpret_cast<Object**>(event_listener_.location()));
3249 event_listener_ = Handle<Object>();
3250 }
3251 if (!event_listener_data_.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_data_.location()));
3254 event_listener_data_ = Handle<Object>();
3255 }
3256
3257 // If there is a new debug event listener register it together with its data
3258 // object.
3259 if (!callback->IsUndefined() && !callback->IsNull()) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003260 event_listener_ = Handle<Object>::cast(
lrn@chromium.org7516f052011-03-30 08:52:27 +00003261 global_handles->Create(*callback));
iposva@chromium.org245aa852009-02-10 00:49:54 +00003262 if (data.is_null()) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00003263 data = isolate_->factory()->undefined_value();
iposva@chromium.org245aa852009-02-10 00:49:54 +00003264 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003265 event_listener_data_ = Handle<Object>::cast(
lrn@chromium.org7516f052011-03-30 08:52:27 +00003266 global_handles->Create(*data));
iposva@chromium.org245aa852009-02-10 00:49:54 +00003267 }
3268
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003269 ListenersChanged();
iposva@chromium.org245aa852009-02-10 00:49:54 +00003270}
3271
3272
ager@chromium.org5ec48922009-05-05 07:25:34 +00003273void Debugger::SetMessageHandler(v8::Debug::MessageHandler2 handler) {
jkummerow@chromium.orgdc94e192013-08-30 11:35:42 +00003274 LockGuard<RecursiveMutex> with(debugger_access_);
ager@chromium.org71daaf62009-04-01 07:22:49 +00003275
ager@chromium.org381abbb2009-02-25 13:23:22 +00003276 message_handler_ = handler;
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003277 ListenersChanged();
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003278 if (handler == NULL) {
ager@chromium.org71daaf62009-04-01 07:22:49 +00003279 // Send an empty command to the debugger if in a break to make JavaScript
3280 // run again if the debugger is closed.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003281 if (isolate_->debug()->InDebugger()) {
ager@chromium.org71daaf62009-04-01 07:22:49 +00003282 ProcessCommand(Vector<const uint16_t>::empty());
3283 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003284 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003285}
3286
3287
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003288void Debugger::ListenersChanged() {
3289 if (IsDebuggerActive()) {
3290 // Disable the compilation cache when the debugger is active.
lrn@chromium.org7516f052011-03-30 08:52:27 +00003291 isolate_->compilation_cache()->Disable();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003292 debugger_unload_pending_ = false;
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003293 } else {
lrn@chromium.org7516f052011-03-30 08:52:27 +00003294 isolate_->compilation_cache()->Enable();
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003295 // Unload the debugger if event listener and message handler cleared.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003296 // Schedule this for later, because we may be in non-V8 thread.
3297 debugger_unload_pending_ = true;
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003298 }
3299}
3300
3301
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003302void Debugger::SetHostDispatchHandler(v8::Debug::HostDispatchHandler handler,
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +00003303 TimeDelta period) {
ager@chromium.org381abbb2009-02-25 13:23:22 +00003304 host_dispatch_handler_ = handler;
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +00003305 host_dispatch_period_ = period;
ager@chromium.org381abbb2009-02-25 13:23:22 +00003306}
3307
3308
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003309void Debugger::SetDebugMessageDispatchHandler(
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003310 v8::Debug::DebugMessageDispatchHandler handler, bool provide_locker) {
jkummerow@chromium.orgdc94e192013-08-30 11:35:42 +00003311 LockGuard<Mutex> lock_guard(&dispatch_handler_access_);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003312 debug_message_dispatch_handler_ = handler;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003313
3314 if (provide_locker && message_dispatch_helper_thread_ == NULL) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003315 message_dispatch_helper_thread_ = new MessageDispatchHelperThread(isolate_);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003316 message_dispatch_helper_thread_->Start();
3317 }
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003318}
3319
3320
ager@chromium.org41826e72009-03-30 13:30:57 +00003321// Calls the registered debug message handler. This callback is part of the
ager@chromium.org5ec48922009-05-05 07:25:34 +00003322// public API.
3323void Debugger::InvokeMessageHandler(MessageImpl message) {
jkummerow@chromium.orgdc94e192013-08-30 11:35:42 +00003324 LockGuard<RecursiveMutex> with(debugger_access_);
ager@chromium.org71daaf62009-04-01 07:22:49 +00003325
ager@chromium.org381abbb2009-02-25 13:23:22 +00003326 if (message_handler_ != NULL) {
ager@chromium.org5ec48922009-05-05 07:25:34 +00003327 message_handler_(message);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003328 }
ager@chromium.org41826e72009-03-30 13:30:57 +00003329}
3330
3331
3332// Puts a command coming from the public API on the queue. Creates
3333// a copy of the command string managed by the debugger. Up to this
3334// point, the command data was managed by the API client. Called
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003335// by the API client thread.
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003336void Debugger::ProcessCommand(Vector<const uint16_t> command,
3337 v8::Debug::ClientData* client_data) {
3338 // Need to cast away const.
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003339 CommandMessage message = CommandMessage::New(
ager@chromium.org41826e72009-03-30 13:30:57 +00003340 Vector<uint16_t>(const_cast<uint16_t*>(command.start()),
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003341 command.length()),
3342 client_data);
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00003343 isolate_->logger()->DebugTag("Put command on command_queue.");
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003344 command_queue_.Put(message);
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +00003345 command_received_.Signal();
sgjesse@chromium.org3afc1582009-04-16 22:31:44 +00003346
3347 // Set the debug command break flag to have the command processed.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003348 if (!isolate_->debug()->InDebugger()) {
3349 isolate_->stack_guard()->DebugCommand();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003350 }
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003351
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003352 MessageDispatchHelperThread* dispatch_thread;
3353 {
jkummerow@chromium.orgdc94e192013-08-30 11:35:42 +00003354 LockGuard<Mutex> lock_guard(&dispatch_handler_access_);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003355 dispatch_thread = message_dispatch_helper_thread_;
3356 }
3357
3358 if (dispatch_thread == NULL) {
3359 CallMessageDispatchHandler();
3360 } else {
3361 dispatch_thread->Schedule();
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003362 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003363}
3364
3365
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003366bool Debugger::HasCommands() {
ager@chromium.org41826e72009-03-30 13:30:57 +00003367 return !command_queue_.IsEmpty();
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003368}
3369
3370
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00003371void Debugger::EnqueueDebugCommand(v8::Debug::ClientData* client_data) {
3372 CommandMessage message = CommandMessage::New(Vector<uint16_t>(), client_data);
3373 event_command_queue_.Put(message);
3374
3375 // Set the debug command break flag to have the command processed.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003376 if (!isolate_->debug()->InDebugger()) {
3377 isolate_->stack_guard()->DebugCommand();
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00003378 }
3379}
3380
3381
ager@chromium.org71daaf62009-04-01 07:22:49 +00003382bool Debugger::IsDebuggerActive() {
jkummerow@chromium.orgdc94e192013-08-30 11:35:42 +00003383 LockGuard<RecursiveMutex> with(debugger_access_);
ager@chromium.org71daaf62009-04-01 07:22:49 +00003384
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00003385 return message_handler_ != NULL ||
3386 !event_listener_.is_null() ||
3387 force_debugger_active_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003388}
3389
3390
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003391Handle<Object> Debugger::Call(Handle<JSFunction> fun,
3392 Handle<Object> data,
3393 bool* pending_exception) {
ager@chromium.org71daaf62009-04-01 07:22:49 +00003394 // When calling functions in the debugger prevent it from beeing unloaded.
3395 Debugger::never_unload_debugger_ = true;
3396
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003397 // Enter the debugger.
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +00003398 EnterDebugger debugger(isolate_);
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +00003399 if (debugger.FailedToEnter()) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00003400 return isolate_->factory()->undefined_value();
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003401 }
3402
3403 // Create the execution state.
3404 bool caught_exception = false;
3405 Handle<Object> exec_state = MakeExecutionState(&caught_exception);
3406 if (caught_exception) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00003407 return isolate_->factory()->undefined_value();
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003408 }
3409
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00003410 Handle<Object> argv[] = { exec_state, data };
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +00003411 Handle<Object> result = Execution::Call(
jkummerow@chromium.org2c9426b2013-09-05 16:31:13 +00003412 isolate_,
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +00003413 fun,
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00003414 Handle<Object>(isolate_->debug()->debug_context_->global_proxy(),
3415 isolate_),
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00003416 ARRAY_SIZE(argv),
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +00003417 argv,
3418 pending_exception);
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003419 return result;
3420}
3421
3422
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003423static void StubMessageHandler2(const v8::Debug::Message& message) {
3424 // Simply ignore message.
3425}
3426
3427
3428bool Debugger::StartAgent(const char* name, int port,
3429 bool wait_for_connection) {
3430 if (wait_for_connection) {
3431 // Suspend V8 if it is already running or set V8 to suspend whenever
3432 // it starts.
3433 // Provide stub message handler; V8 auto-continues each suspend
3434 // when there is no message handler; we doesn't need it.
3435 // Once become suspended, V8 will stay so indefinitely long, until remote
3436 // debugger connects and issues "continue" command.
3437 Debugger::message_handler_ = StubMessageHandler2;
3438 v8::Debug::DebugBreak();
3439 }
3440
jkummerow@chromium.org3d00d0a2013-09-04 13:57:32 +00003441 if (agent_ == NULL) {
3442 agent_ = new DebuggerAgent(isolate_, name, port);
3443 agent_->Start();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003444 }
jkummerow@chromium.org3d00d0a2013-09-04 13:57:32 +00003445 return true;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003446}
3447
3448
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003449void Debugger::StopAgent() {
3450 if (agent_ != NULL) {
3451 agent_->Shutdown();
3452 agent_->Join();
3453 delete agent_;
3454 agent_ = NULL;
3455 }
3456}
3457
3458
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003459void Debugger::WaitForAgent() {
3460 if (agent_ != NULL)
3461 agent_->WaitUntilListening();
3462}
3463
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003464
3465void Debugger::CallMessageDispatchHandler() {
3466 v8::Debug::DebugMessageDispatchHandler handler;
3467 {
jkummerow@chromium.orgdc94e192013-08-30 11:35:42 +00003468 LockGuard<Mutex> lock_guard(&dispatch_handler_access_);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003469 handler = Debugger::debug_message_dispatch_handler_;
3470 }
3471 if (handler != NULL) {
3472 handler();
3473 }
3474}
3475
3476
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +00003477EnterDebugger::EnterDebugger(Isolate* isolate)
3478 : isolate_(isolate),
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003479 prev_(isolate_->debug()->debugger_entry()),
3480 it_(isolate_),
3481 has_js_frames_(!it_.done()),
3482 save_(isolate_) {
3483 Debug* debug = isolate_->debug();
3484 ASSERT(prev_ != NULL || !debug->is_interrupt_pending(PREEMPT));
3485 ASSERT(prev_ != NULL || !debug->is_interrupt_pending(DEBUGBREAK));
3486
3487 // Link recursive debugger entry.
3488 debug->set_debugger_entry(this);
3489
3490 // Store the previous break id and frame id.
3491 break_id_ = debug->break_id();
3492 break_frame_id_ = debug->break_frame_id();
3493
3494 // Create the new break info. If there is no JavaScript frames there is no
3495 // break frame id.
3496 if (has_js_frames_) {
3497 debug->NewBreak(it_.frame()->id());
3498 } else {
3499 debug->NewBreak(StackFrame::NO_ID);
3500 }
3501
3502 // Make sure that debugger is loaded and enter the debugger context.
3503 load_failed_ = !debug->Load();
3504 if (!load_failed_) {
3505 // NOTE the member variable save which saves the previous context before
3506 // this change.
3507 isolate_->set_context(*debug->debug_context());
3508 }
3509}
3510
3511
3512EnterDebugger::~EnterDebugger() {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003513 Debug* debug = isolate_->debug();
3514
3515 // Restore to the previous break state.
3516 debug->SetBreak(break_frame_id_, break_id_);
3517
3518 // Check for leaving the debugger.
fschneider@chromium.org7d10be52012-04-10 12:30:14 +00003519 if (!load_failed_ && prev_ == NULL) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003520 // Clear mirror cache when leaving the debugger. Skip this if there is a
3521 // pending exception as clearing the mirror cache calls back into
3522 // JavaScript. This can happen if the v8::Debug::Call is used in which
3523 // case the exception should end up in the calling code.
3524 if (!isolate_->has_pending_exception()) {
3525 // Try to avoid any pending debug break breaking in the clear mirror
3526 // cache JavaScript code.
3527 if (isolate_->stack_guard()->IsDebugBreak()) {
3528 debug->set_interrupts_pending(DEBUGBREAK);
3529 isolate_->stack_guard()->Continue(DEBUGBREAK);
3530 }
3531 debug->ClearMirrorCache();
3532 }
3533
3534 // Request preemption and debug break when leaving the last debugger entry
3535 // if any of these where recorded while debugging.
3536 if (debug->is_interrupt_pending(PREEMPT)) {
3537 // This re-scheduling of preemption is to avoid starvation in some
3538 // debugging scenarios.
3539 debug->clear_interrupt_pending(PREEMPT);
3540 isolate_->stack_guard()->Preempt();
3541 }
3542 if (debug->is_interrupt_pending(DEBUGBREAK)) {
3543 debug->clear_interrupt_pending(DEBUGBREAK);
3544 isolate_->stack_guard()->DebugBreak();
3545 }
3546
3547 // If there are commands in the queue when leaving the debugger request
3548 // that these commands are processed.
3549 if (isolate_->debugger()->HasCommands()) {
3550 isolate_->stack_guard()->DebugCommand();
3551 }
3552
3553 // If leaving the debugger with the debugger no longer active unload it.
3554 if (!isolate_->debugger()->IsDebuggerActive()) {
3555 isolate_->debugger()->UnloadDebugger();
3556 }
3557 }
3558
3559 // Leaving this debugger entry.
3560 debug->set_debugger_entry(prev_);
3561}
3562
3563
ager@chromium.org5ec48922009-05-05 07:25:34 +00003564MessageImpl MessageImpl::NewEvent(DebugEvent event,
3565 bool running,
3566 Handle<JSObject> exec_state,
3567 Handle<JSObject> event_data) {
3568 MessageImpl message(true, event, running,
3569 exec_state, event_data, Handle<String>(), NULL);
3570 return message;
3571}
3572
3573
3574MessageImpl MessageImpl::NewResponse(DebugEvent event,
3575 bool running,
3576 Handle<JSObject> exec_state,
3577 Handle<JSObject> event_data,
3578 Handle<String> response_json,
3579 v8::Debug::ClientData* client_data) {
3580 MessageImpl message(false, event, running,
3581 exec_state, event_data, response_json, client_data);
3582 return message;
3583}
3584
3585
3586MessageImpl::MessageImpl(bool is_event,
3587 DebugEvent event,
3588 bool running,
3589 Handle<JSObject> exec_state,
3590 Handle<JSObject> event_data,
3591 Handle<String> response_json,
3592 v8::Debug::ClientData* client_data)
3593 : is_event_(is_event),
3594 event_(event),
3595 running_(running),
3596 exec_state_(exec_state),
3597 event_data_(event_data),
3598 response_json_(response_json),
3599 client_data_(client_data) {}
3600
3601
3602bool MessageImpl::IsEvent() const {
3603 return is_event_;
3604}
3605
3606
3607bool MessageImpl::IsResponse() const {
3608 return !is_event_;
3609}
3610
3611
3612DebugEvent MessageImpl::GetEvent() const {
3613 return event_;
3614}
3615
3616
3617bool MessageImpl::WillStartRunning() const {
3618 return running_;
3619}
3620
3621
3622v8::Handle<v8::Object> MessageImpl::GetExecutionState() const {
3623 return v8::Utils::ToLocal(exec_state_);
3624}
3625
3626
hpayer@chromium.orgc5d49712013-09-11 08:25:48 +00003627v8::Isolate* MessageImpl::GetIsolate() const {
3628 return reinterpret_cast<v8::Isolate*>(exec_state_->GetIsolate());
3629}
3630
3631
ager@chromium.org5ec48922009-05-05 07:25:34 +00003632v8::Handle<v8::Object> MessageImpl::GetEventData() const {
3633 return v8::Utils::ToLocal(event_data_);
3634}
3635
3636
3637v8::Handle<v8::String> MessageImpl::GetJSON() const {
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +00003638 v8::HandleScope scope(
3639 reinterpret_cast<v8::Isolate*>(event_data_->GetIsolate()));
ager@chromium.org5ec48922009-05-05 07:25:34 +00003640
3641 if (IsEvent()) {
3642 // Call toJSONProtocol on the debug event object.
3643 Handle<Object> fun = GetProperty(event_data_, "toJSONProtocol");
3644 if (!fun->IsJSFunction()) {
3645 return v8::Handle<v8::String>();
3646 }
3647 bool caught_exception;
3648 Handle<Object> json = Execution::TryCall(Handle<JSFunction>::cast(fun),
3649 event_data_,
3650 0, NULL, &caught_exception);
3651 if (caught_exception || !json->IsString()) {
3652 return v8::Handle<v8::String>();
3653 }
3654 return scope.Close(v8::Utils::ToLocal(Handle<String>::cast(json)));
3655 } else {
3656 return v8::Utils::ToLocal(response_json_);
3657 }
3658}
3659
3660
3661v8::Handle<v8::Context> MessageImpl::GetEventContext() const {
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +00003662 Isolate* isolate = event_data_->GetIsolate();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003663 v8::Handle<v8::Context> context = GetDebugEventContext(isolate);
3664 // Isolate::context() may be NULL when "script collected" event occures.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003665 ASSERT(!context.IsEmpty() || event_ == v8::ScriptCollected);
rossberg@chromium.org717967f2011-07-20 13:44:42 +00003666 return context;
ager@chromium.org5ec48922009-05-05 07:25:34 +00003667}
3668
3669
3670v8::Debug::ClientData* MessageImpl::GetClientData() const {
3671 return client_data_;
3672}
3673
3674
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003675EventDetailsImpl::EventDetailsImpl(DebugEvent event,
3676 Handle<JSObject> exec_state,
3677 Handle<JSObject> event_data,
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00003678 Handle<Object> callback_data,
3679 v8::Debug::ClientData* client_data)
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003680 : event_(event),
3681 exec_state_(exec_state),
3682 event_data_(event_data),
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00003683 callback_data_(callback_data),
3684 client_data_(client_data) {}
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003685
3686
3687DebugEvent EventDetailsImpl::GetEvent() const {
3688 return event_;
3689}
3690
3691
3692v8::Handle<v8::Object> EventDetailsImpl::GetExecutionState() const {
3693 return v8::Utils::ToLocal(exec_state_);
3694}
3695
3696
3697v8::Handle<v8::Object> EventDetailsImpl::GetEventData() const {
3698 return v8::Utils::ToLocal(event_data_);
3699}
3700
3701
3702v8::Handle<v8::Context> EventDetailsImpl::GetEventContext() const {
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +00003703 return GetDebugEventContext(exec_state_->GetIsolate());
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003704}
3705
3706
3707v8::Handle<v8::Value> EventDetailsImpl::GetCallbackData() const {
3708 return v8::Utils::ToLocal(callback_data_);
3709}
3710
3711
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00003712v8::Debug::ClientData* EventDetailsImpl::GetClientData() const {
3713 return client_data_;
3714}
3715
3716
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003717CommandMessage::CommandMessage() : text_(Vector<uint16_t>::empty()),
3718 client_data_(NULL) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003719}
3720
3721
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003722CommandMessage::CommandMessage(const Vector<uint16_t>& text,
3723 v8::Debug::ClientData* data)
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003724 : text_(text),
3725 client_data_(data) {
3726}
3727
3728
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003729CommandMessage::~CommandMessage() {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003730}
3731
3732
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003733void CommandMessage::Dispose() {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003734 text_.Dispose();
3735 delete client_data_;
3736 client_data_ = NULL;
3737}
3738
3739
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003740CommandMessage CommandMessage::New(const Vector<uint16_t>& command,
3741 v8::Debug::ClientData* data) {
3742 return CommandMessage(command.Clone(), data);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003743}
3744
3745
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003746CommandMessageQueue::CommandMessageQueue(int size) : start_(0), end_(0),
3747 size_(size) {
3748 messages_ = NewArray<CommandMessage>(size);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003749}
3750
3751
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003752CommandMessageQueue::~CommandMessageQueue() {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003753 while (!IsEmpty()) {
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003754 CommandMessage m = Get();
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003755 m.Dispose();
3756 }
kasper.lund7276f142008-07-30 08:49:36 +00003757 DeleteArray(messages_);
3758}
3759
3760
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003761CommandMessage CommandMessageQueue::Get() {
kasper.lund7276f142008-07-30 08:49:36 +00003762 ASSERT(!IsEmpty());
3763 int result = start_;
3764 start_ = (start_ + 1) % size_;
3765 return messages_[result];
3766}
3767
3768
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003769void CommandMessageQueue::Put(const CommandMessage& message) {
kasper.lund7276f142008-07-30 08:49:36 +00003770 if ((end_ + 1) % size_ == start_) {
3771 Expand();
3772 }
3773 messages_[end_] = message;
3774 end_ = (end_ + 1) % size_;
3775}
3776
3777
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003778void CommandMessageQueue::Expand() {
3779 CommandMessageQueue new_queue(size_ * 2);
kasper.lund7276f142008-07-30 08:49:36 +00003780 while (!IsEmpty()) {
3781 new_queue.Put(Get());
3782 }
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003783 CommandMessage* array_to_free = messages_;
kasper.lund7276f142008-07-30 08:49:36 +00003784 *this = new_queue;
3785 new_queue.messages_ = array_to_free;
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003786 // Make the new_queue empty so that it doesn't call Dispose on any messages.
3787 new_queue.start_ = new_queue.end_;
kasper.lund7276f142008-07-30 08:49:36 +00003788 // Automatic destructor called on new_queue, freeing array_to_free.
3789}
3790
3791
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00003792LockingCommandMessageQueue::LockingCommandMessageQueue(Logger* logger, int size)
jkummerow@chromium.orgdc94e192013-08-30 11:35:42 +00003793 : logger_(logger), queue_(size) {}
kasper.lund7276f142008-07-30 08:49:36 +00003794
3795
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003796bool LockingCommandMessageQueue::IsEmpty() const {
jkummerow@chromium.orgdc94e192013-08-30 11:35:42 +00003797 LockGuard<Mutex> lock_guard(&mutex_);
kasper.lund7276f142008-07-30 08:49:36 +00003798 return queue_.IsEmpty();
3799}
3800
3801
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003802CommandMessage LockingCommandMessageQueue::Get() {
jkummerow@chromium.orgdc94e192013-08-30 11:35:42 +00003803 LockGuard<Mutex> lock_guard(&mutex_);
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003804 CommandMessage result = queue_.Get();
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00003805 logger_->DebugEvent("Get", result.text());
kasper.lund7276f142008-07-30 08:49:36 +00003806 return result;
3807}
3808
3809
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003810void LockingCommandMessageQueue::Put(const CommandMessage& message) {
jkummerow@chromium.orgdc94e192013-08-30 11:35:42 +00003811 LockGuard<Mutex> lock_guard(&mutex_);
kasper.lund7276f142008-07-30 08:49:36 +00003812 queue_.Put(message);
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00003813 logger_->DebugEvent("Put", message.text());
kasper.lund7276f142008-07-30 08:49:36 +00003814}
3815
3816
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003817void LockingCommandMessageQueue::Clear() {
jkummerow@chromium.orgdc94e192013-08-30 11:35:42 +00003818 LockGuard<Mutex> lock_guard(&mutex_);
kasper.lund7276f142008-07-30 08:49:36 +00003819 queue_.Clear();
3820}
3821
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003822
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003823MessageDispatchHelperThread::MessageDispatchHelperThread(Isolate* isolate)
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00003824 : Thread("v8:MsgDispHelpr"),
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +00003825 isolate_(isolate), sem_(0),
jkummerow@chromium.orgdc94e192013-08-30 11:35:42 +00003826 already_signalled_(false) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003827}
3828
3829
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003830void MessageDispatchHelperThread::Schedule() {
3831 {
jkummerow@chromium.orgdc94e192013-08-30 11:35:42 +00003832 LockGuard<Mutex> lock_guard(&mutex_);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003833 if (already_signalled_) {
3834 return;
3835 }
3836 already_signalled_ = true;
3837 }
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +00003838 sem_.Signal();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003839}
3840
3841
3842void MessageDispatchHelperThread::Run() {
3843 while (true) {
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +00003844 sem_.Wait();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003845 {
jkummerow@chromium.orgdc94e192013-08-30 11:35:42 +00003846 LockGuard<Mutex> lock_guard(&mutex_);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003847 already_signalled_ = false;
3848 }
3849 {
mstarzinger@chromium.orgf705b502013-04-04 11:38:09 +00003850 Locker locker(reinterpret_cast<v8::Isolate*>(isolate_));
3851 isolate_->debugger()->CallMessageDispatchHandler();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003852 }
3853 }
3854}
3855
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003856#endif // ENABLE_DEBUGGER_SUPPORT
kasper.lund7276f142008-07-30 08:49:36 +00003857
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003858} } // namespace v8::internal