blob: c2d324e502589acb7391972d1c67e4753170b232 [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
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000089static v8::Handle<v8::Context> GetDebugEventContext(Isolate* isolate) {
90 Handle<Context> context = isolate->debug()->debugger_entry()->GetContext();
91 // Isolate::context() may have been NULL when "script collected" event
92 // occured.
93 if (context.is_null()) return v8::Local<v8::Context>();
yangguo@chromium.org46839fb2012-08-28 09:06:19 +000094 Handle<Context> native_context(context->native_context());
95 return v8::Utils::ToLocal(native_context);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +000096}
97
98
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000099BreakLocationIterator::BreakLocationIterator(Handle<DebugInfo> debug_info,
100 BreakLocatorType type) {
101 debug_info_ = debug_info;
102 type_ = type;
103 reloc_iterator_ = NULL;
104 reloc_iterator_original_ = NULL;
105 Reset(); // Initialize the rest of the member variables.
106}
107
108
109BreakLocationIterator::~BreakLocationIterator() {
110 ASSERT(reloc_iterator_ != NULL);
111 ASSERT(reloc_iterator_original_ != NULL);
112 delete reloc_iterator_;
113 delete reloc_iterator_original_;
114}
115
116
117void BreakLocationIterator::Next() {
rossberg@chromium.org79e79022013-06-03 15:43:46 +0000118 DisallowHeapAllocation no_gc;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000119 ASSERT(!RinfoDone());
120
121 // Iterate through reloc info for code and original code stopping at each
122 // breakable code target.
123 bool first = break_point_ == -1;
124 while (!RinfoDone()) {
125 if (!first) RinfoNext();
126 first = false;
127 if (RinfoDone()) return;
128
ager@chromium.org236ad962008-09-25 09:45:57 +0000129 // Whenever a statement position or (plain) position is passed update the
130 // current value of these.
131 if (RelocInfo::IsPosition(rmode())) {
132 if (RelocInfo::IsStatementPosition(rmode())) {
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000133 statement_position_ = static_cast<int>(
134 rinfo()->data() - debug_info_->shared()->start_position());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000135 }
ager@chromium.org236ad962008-09-25 09:45:57 +0000136 // Always update the position as we don't want that to be before the
137 // statement position.
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000138 position_ = static_cast<int>(
139 rinfo()->data() - debug_info_->shared()->start_position());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000140 ASSERT(position_ >= 0);
141 ASSERT(statement_position_ >= 0);
142 }
143
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000144 if (IsDebugBreakSlot()) {
145 // There is always a possible break point at a debug break slot.
146 break_point_++;
147 return;
148 } else if (RelocInfo::IsCodeTarget(rmode())) {
149 // Check for breakable code target. Look in the original code as setting
150 // break points can cause the code targets in the running (debugged) code
151 // to be of a different kind than in the original code.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000152 Address target = original_rinfo()->target_address();
ager@chromium.org8bb60582008-12-11 12:02:20 +0000153 Code* code = Code::GetCodeFromTargetAddress(target);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000154 if ((code->is_inline_cache_stub() &&
danno@chromium.org40cb8782011-05-25 07:58:50 +0000155 !code->is_binary_op_stub() &&
ricow@chromium.org9fa09672011-07-25 11:05:35 +0000156 !code->is_compare_ic_stub() &&
157 !code->is_to_boolean_ic_stub()) ||
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000158 RelocInfo::IsConstructCall(rmode())) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000159 break_point_++;
160 return;
161 }
162 if (code->kind() == Code::STUB) {
ager@chromium.orga1645e22009-09-09 19:27:10 +0000163 if (IsDebuggerStatement()) {
164 break_point_++;
165 return;
166 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000167 if (type_ == ALL_BREAK_LOCATIONS) {
168 if (Debug::IsBreakStub(code)) {
169 break_point_++;
170 return;
171 }
172 } else {
173 ASSERT(type_ == SOURCE_BREAK_LOCATIONS);
174 if (Debug::IsSourceBreakStub(code)) {
175 break_point_++;
176 return;
177 }
178 }
179 }
180 }
181
182 // Check for break at return.
ager@chromium.org236ad962008-09-25 09:45:57 +0000183 if (RelocInfo::IsJSReturn(rmode())) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000184 // Set the positions to the end of the function.
185 if (debug_info_->shared()->HasSourceCode()) {
186 position_ = debug_info_->shared()->end_position() -
whesse@chromium.orge90029b2010-08-02 11:52:17 +0000187 debug_info_->shared()->start_position() - 1;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000188 } else {
189 position_ = 0;
190 }
191 statement_position_ = position_;
192 break_point_++;
193 return;
194 }
195 }
196}
197
198
199void BreakLocationIterator::Next(int count) {
200 while (count > 0) {
201 Next();
202 count--;
203 }
204}
205
206
verwaest@chromium.org8a00e822013-06-10 15:11:22 +0000207// Find the break point at the supplied address, or the closest one before
208// the address.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000209void BreakLocationIterator::FindBreakLocationFromAddress(Address pc) {
210 // Run through all break points to locate the one closest to the address.
211 int closest_break_point = 0;
212 int distance = kMaxInt;
213 while (!Done()) {
214 // Check if this break point is closer that what was previously found.
verwaest@chromium.org8a00e822013-06-10 15:11:22 +0000215 if (this->pc() <= pc && pc - this->pc() < distance) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000216 closest_break_point = break_point();
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000217 distance = static_cast<int>(pc - this->pc());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000218 // Check whether we can't get any closer.
219 if (distance == 0) break;
220 }
221 Next();
222 }
223
224 // Move to the break point found.
225 Reset();
226 Next(closest_break_point);
227}
228
229
230// Find the break point closest to the supplied source position.
jkummerow@chromium.org93a47f42013-07-02 14:43:41 +0000231void BreakLocationIterator::FindBreakLocationFromPosition(int position,
232 BreakPositionAlignment alignment) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000233 // Run through all break points to locate the one closest to the source
234 // position.
235 int closest_break_point = 0;
236 int distance = kMaxInt;
jkummerow@chromium.org93a47f42013-07-02 14:43:41 +0000237
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000238 while (!Done()) {
jkummerow@chromium.org93a47f42013-07-02 14:43:41 +0000239 int next_position;
240 switch (alignment) {
241 case STATEMENT_ALIGNED:
242 next_position = this->statement_position();
243 break;
244 case BREAK_POSITION_ALIGNED:
245 next_position = this->position();
246 break;
247 default:
248 UNREACHABLE();
249 next_position = this->statement_position();
250 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000251 // Check if this break point is closer that what was previously found.
jkummerow@chromium.org93a47f42013-07-02 14:43:41 +0000252 if (position <= next_position && next_position - position < distance) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000253 closest_break_point = break_point();
jkummerow@chromium.org93a47f42013-07-02 14:43:41 +0000254 distance = next_position - position;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000255 // Check whether we can't get any closer.
256 if (distance == 0) break;
257 }
258 Next();
259 }
260
261 // Move to the break point found.
262 Reset();
263 Next(closest_break_point);
264}
265
266
267void BreakLocationIterator::Reset() {
268 // Create relocation iterators for the two code objects.
269 if (reloc_iterator_ != NULL) delete reloc_iterator_;
270 if (reloc_iterator_original_ != NULL) delete reloc_iterator_original_;
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +0000271 reloc_iterator_ = new RelocIterator(
272 debug_info_->code(),
273 ~RelocInfo::ModeMask(RelocInfo::CODE_AGE_SEQUENCE));
274 reloc_iterator_original_ = new RelocIterator(
275 debug_info_->original_code(),
276 ~RelocInfo::ModeMask(RelocInfo::CODE_AGE_SEQUENCE));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000277
278 // Position at the first break point.
279 break_point_ = -1;
280 position_ = 1;
281 statement_position_ = 1;
282 Next();
283}
284
285
286bool BreakLocationIterator::Done() const {
287 return RinfoDone();
288}
289
290
291void BreakLocationIterator::SetBreakPoint(Handle<Object> break_point_object) {
292 // If there is not already a real break point here patch code with debug
293 // break.
294 if (!HasBreakPoint()) {
295 SetDebugBreak();
296 }
ager@chromium.orga1645e22009-09-09 19:27:10 +0000297 ASSERT(IsDebugBreak() || IsDebuggerStatement());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000298 // Set the break point information.
299 DebugInfo::SetBreakPoint(debug_info_, code_position(),
300 position(), statement_position(),
301 break_point_object);
302}
303
304
305void BreakLocationIterator::ClearBreakPoint(Handle<Object> break_point_object) {
306 // Clear the break point information.
307 DebugInfo::ClearBreakPoint(debug_info_, code_position(), break_point_object);
308 // If there are no more break points here remove the debug break.
309 if (!HasBreakPoint()) {
310 ClearDebugBreak();
311 ASSERT(!IsDebugBreak());
312 }
313}
314
315
316void BreakLocationIterator::SetOneShot() {
ager@chromium.orga1645e22009-09-09 19:27:10 +0000317 // Debugger statement always calls debugger. No need to modify it.
318 if (IsDebuggerStatement()) {
319 return;
320 }
321
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000322 // If there is a real break point here no more to do.
323 if (HasBreakPoint()) {
324 ASSERT(IsDebugBreak());
325 return;
326 }
327
328 // Patch code with debug break.
329 SetDebugBreak();
330}
331
332
333void BreakLocationIterator::ClearOneShot() {
ager@chromium.orga1645e22009-09-09 19:27:10 +0000334 // Debugger statement always calls debugger. No need to modify it.
335 if (IsDebuggerStatement()) {
336 return;
337 }
338
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000339 // If there is a real break point here no more to do.
340 if (HasBreakPoint()) {
341 ASSERT(IsDebugBreak());
342 return;
343 }
344
345 // Patch code removing debug break.
346 ClearDebugBreak();
347 ASSERT(!IsDebugBreak());
348}
349
350
351void BreakLocationIterator::SetDebugBreak() {
ager@chromium.orga1645e22009-09-09 19:27:10 +0000352 // Debugger statement always calls debugger. No need to modify it.
353 if (IsDebuggerStatement()) {
354 return;
355 }
356
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000357 // If there is already a break point here just return. This might happen if
v8.team.kasperl727e9952008-09-02 14:56:44 +0000358 // the same code is flooded with break points twice. Flooding the same
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000359 // function twice might happen when stepping in a function with an exception
360 // handler as the handler and the function is the same.
361 if (IsDebugBreak()) {
362 return;
363 }
364
ager@chromium.org236ad962008-09-25 09:45:57 +0000365 if (RelocInfo::IsJSReturn(rmode())) {
iposva@chromium.org245aa852009-02-10 00:49:54 +0000366 // Patch the frame exit code with a break point.
367 SetDebugBreakAtReturn();
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000368 } else if (IsDebugBreakSlot()) {
369 // Patch the code in the break slot.
370 SetDebugBreakAtSlot();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000371 } else {
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000372 // Patch the IC call.
373 SetDebugBreakAtIC();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000374 }
375 ASSERT(IsDebugBreak());
376}
377
378
379void BreakLocationIterator::ClearDebugBreak() {
ager@chromium.orga1645e22009-09-09 19:27:10 +0000380 // Debugger statement always calls debugger. No need to modify it.
381 if (IsDebuggerStatement()) {
382 return;
383 }
384
ager@chromium.org236ad962008-09-25 09:45:57 +0000385 if (RelocInfo::IsJSReturn(rmode())) {
iposva@chromium.org245aa852009-02-10 00:49:54 +0000386 // Restore the frame exit code.
387 ClearDebugBreakAtReturn();
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000388 } else if (IsDebugBreakSlot()) {
389 // Restore the code in the break slot.
390 ClearDebugBreakAtSlot();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000391 } else {
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000392 // Patch the IC call.
393 ClearDebugBreakAtIC();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000394 }
395 ASSERT(!IsDebugBreak());
396}
397
398
mstarzinger@chromium.org1510d582013-06-28 14:00:48 +0000399bool BreakLocationIterator::IsStepInLocation(Isolate* isolate) {
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +0000400 if (RelocInfo::IsConstructCall(original_rmode())) {
mstarzinger@chromium.org1510d582013-06-28 14:00:48 +0000401 return true;
402 } else if (RelocInfo::IsCodeTarget(rmode())) {
403 HandleScope scope(debug_info_->GetIsolate());
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +0000404 Address target = original_rinfo()->target_address();
mstarzinger@chromium.org1510d582013-06-28 14:00:48 +0000405 Handle<Code> target_code(Code::GetCodeFromTargetAddress(target));
danno@chromium.org59400602013-08-13 17:09:37 +0000406 if (target_code->kind() == Code::STUB) {
407 return target_code->major_key() == CodeStub::CallFunction;
408 }
mstarzinger@chromium.org1510d582013-06-28 14:00:48 +0000409 }
machenbach@chromium.orga03ba1e2014-02-01 08:54:43 +0000410 return false;
mstarzinger@chromium.org1510d582013-06-28 14:00:48 +0000411}
412
413
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +0000414void BreakLocationIterator::PrepareStepIn(Isolate* isolate) {
machenbach@chromium.orga03ba1e2014-02-01 08:54:43 +0000415#ifdef DEBUG
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +0000416 HandleScope scope(isolate);
ager@chromium.orga1645e22009-09-09 19:27:10 +0000417 // Step in can only be prepared if currently positioned on an IC call,
418 // construct call or CallFunction stub call.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000419 Address target = rinfo()->target_address();
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000420 Handle<Code> target_code(Code::GetCodeFromTargetAddress(target));
machenbach@chromium.orga03ba1e2014-02-01 08:54:43 +0000421 // All the following stuff is needed only for assertion checks so the code
422 // is wrapped in ifdef.
423 Handle<Code> maybe_call_function_stub = target_code;
424 if (IsDebugBreak()) {
425 Address original_target = original_rinfo()->target_address();
426 maybe_call_function_stub =
427 Handle<Code>(Code::GetCodeFromTargetAddress(original_target));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000428 }
machenbach@chromium.orga03ba1e2014-02-01 08:54:43 +0000429 bool is_call_function_stub =
430 (maybe_call_function_stub->kind() == Code::STUB &&
431 maybe_call_function_stub->major_key() == CodeStub::CallFunction);
432
433 // Step in through construct call requires no changes to the running code.
434 // Step in through getters/setters should already be prepared as well
435 // because caller of this function (Debug::PrepareStep) is expected to
436 // flood the top frame's function with one shot breakpoints.
437 // Step in through CallFunction stub should also be prepared by caller of
438 // this function (Debug::PrepareStep) which should flood target function
439 // with breakpoints.
440 ASSERT(RelocInfo::IsConstructCall(rmode()) ||
441 target_code->is_inline_cache_stub() ||
442 is_call_function_stub);
443#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000444}
445
446
447// Check whether the break point is at a position which will exit the function.
448bool BreakLocationIterator::IsExit() const {
ager@chromium.org236ad962008-09-25 09:45:57 +0000449 return (RelocInfo::IsJSReturn(rmode()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000450}
451
452
453bool BreakLocationIterator::HasBreakPoint() {
454 return debug_info_->HasBreakPoint(code_position());
455}
456
457
458// Check whether there is a debug break at the current position.
459bool BreakLocationIterator::IsDebugBreak() {
ager@chromium.org236ad962008-09-25 09:45:57 +0000460 if (RelocInfo::IsJSReturn(rmode())) {
iposva@chromium.org245aa852009-02-10 00:49:54 +0000461 return IsDebugBreakAtReturn();
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000462 } else if (IsDebugBreakSlot()) {
463 return IsDebugBreakAtSlot();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000464 } else {
465 return Debug::IsDebugBreak(rinfo()->target_address());
466 }
467}
468
469
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000470void BreakLocationIterator::SetDebugBreakAtIC() {
471 // Patch the original code with the current address as the current address
472 // might have changed by the inline caching since the code was copied.
473 original_rinfo()->set_target_address(rinfo()->target_address());
474
475 RelocInfo::Mode mode = rmode();
476 if (RelocInfo::IsCodeTarget(mode)) {
477 Address target = rinfo()->target_address();
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000478 Handle<Code> target_code(Code::GetCodeFromTargetAddress(target));
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000479
480 // Patch the code to invoke the builtin debug break function matching the
481 // calling convention used by the call site.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000482 Handle<Code> dbgbrk_code(Debug::FindDebugBreak(target_code, mode));
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000483 rinfo()->set_target_address(dbgbrk_code->entry());
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000484 }
485}
486
487
488void BreakLocationIterator::ClearDebugBreakAtIC() {
489 // Patch the code to the original invoke.
490 rinfo()->set_target_address(original_rinfo()->target_address());
491}
492
493
ager@chromium.orga1645e22009-09-09 19:27:10 +0000494bool BreakLocationIterator::IsDebuggerStatement() {
ager@chromium.org5c838252010-02-19 08:53:10 +0000495 return RelocInfo::DEBUG_BREAK == rmode();
ager@chromium.orga1645e22009-09-09 19:27:10 +0000496}
497
498
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000499bool BreakLocationIterator::IsDebugBreakSlot() {
500 return RelocInfo::DEBUG_BREAK_SLOT == rmode();
501}
502
503
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000504Object* BreakLocationIterator::BreakPointObjects() {
505 return debug_info_->GetBreakPointObjects(code_position());
506}
507
508
ager@chromium.org381abbb2009-02-25 13:23:22 +0000509// Clear out all the debug break code. This is ONLY supposed to be used when
510// shutting down the debugger as it will leave the break point information in
511// DebugInfo even though the code is patched back to the non break point state.
512void BreakLocationIterator::ClearAllDebugBreak() {
513 while (!Done()) {
514 ClearDebugBreak();
515 Next();
516 }
517}
518
519
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000520bool BreakLocationIterator::RinfoDone() const {
521 ASSERT(reloc_iterator_->done() == reloc_iterator_original_->done());
522 return reloc_iterator_->done();
523}
524
525
526void BreakLocationIterator::RinfoNext() {
527 reloc_iterator_->next();
528 reloc_iterator_original_->next();
529#ifdef DEBUG
530 ASSERT(reloc_iterator_->done() == reloc_iterator_original_->done());
531 if (!reloc_iterator_->done()) {
532 ASSERT(rmode() == original_rmode());
533 }
534#endif
535}
536
537
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000538// Threading support.
539void Debug::ThreadInit() {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000540 thread_local_.break_count_ = 0;
541 thread_local_.break_id_ = 0;
542 thread_local_.break_frame_id_ = StackFrame::NO_ID;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000543 thread_local_.last_step_action_ = StepNone;
ager@chromium.org236ad962008-09-25 09:45:57 +0000544 thread_local_.last_statement_position_ = RelocInfo::kNoPosition;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000545 thread_local_.step_count_ = 0;
546 thread_local_.last_fp_ = 0;
lrn@chromium.org34e60782011-09-15 07:25:40 +0000547 thread_local_.queued_step_count_ = 0;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000548 thread_local_.step_into_fp_ = 0;
ager@chromium.orga1645e22009-09-09 19:27:10 +0000549 thread_local_.step_out_fp_ = 0;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000550 thread_local_.after_break_target_ = 0;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000551 // TODO(isolates): frames_are_dropped_?
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000552 thread_local_.debugger_entry_ = NULL;
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000553 thread_local_.pending_interrupts_ = 0;
ricow@chromium.org0b9f8502010-08-18 07:45:01 +0000554 thread_local_.restarter_frame_function_pointer_ = NULL;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000555}
556
557
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000558char* Debug::ArchiveDebug(char* storage) {
559 char* to = storage;
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000560 OS::MemCopy(to, reinterpret_cast<char*>(&thread_local_), sizeof(ThreadLocal));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000561 to += sizeof(ThreadLocal);
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000562 OS::MemCopy(to, reinterpret_cast<char*>(&registers_), sizeof(registers_));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000563 ThreadInit();
564 ASSERT(to <= storage + ArchiveSpacePerThread());
565 return storage + ArchiveSpacePerThread();
566}
567
568
569char* Debug::RestoreDebug(char* storage) {
570 char* from = storage;
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000571 OS::MemCopy(
572 reinterpret_cast<char*>(&thread_local_), from, sizeof(ThreadLocal));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000573 from += sizeof(ThreadLocal);
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000574 OS::MemCopy(reinterpret_cast<char*>(&registers_), from, sizeof(registers_));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000575 ASSERT(from <= storage + ArchiveSpacePerThread());
576 return storage + ArchiveSpacePerThread();
577}
578
579
580int Debug::ArchiveSpacePerThread() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000581 return sizeof(ThreadLocal) + sizeof(JSCallerSavedBuffer);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000582}
583
584
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000585// Frame structure (conforms InternalFrame structure):
586// -- code
587// -- SMI maker
588// -- function (slot is called "context")
589// -- frame base
590Object** Debug::SetUpFrameDropperFrame(StackFrame* bottom_js_frame,
591 Handle<Code> code) {
592 ASSERT(bottom_js_frame->is_java_script());
593
594 Address fp = bottom_js_frame->fp();
595
596 // Move function pointer into "context" slot.
597 Memory::Object_at(fp + StandardFrameConstants::kContextOffset) =
598 Memory::Object_at(fp + JavaScriptFrameConstants::kFunctionOffset);
599
600 Memory::Object_at(fp + InternalFrameConstants::kCodeOffset) = *code;
601 Memory::Object_at(fp + StandardFrameConstants::kMarkerOffset) =
602 Smi::FromInt(StackFrame::INTERNAL);
603
604 return reinterpret_cast<Object**>(&Memory::Object_at(
605 fp + StandardFrameConstants::kContextOffset));
606}
607
608const int Debug::kFrameDropperFrameSize = 4;
609
610
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000611void ScriptCache::Add(Handle<Script> script) {
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +0000612 GlobalHandles* global_handles = isolate_->global_handles();
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000613 // Create an entry in the hash map for the script.
mstarzinger@chromium.org1510d582013-06-28 14:00:48 +0000614 int id = script->id()->value();
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000615 HashMap::Entry* entry =
616 HashMap::Lookup(reinterpret_cast<void*>(id), Hash(id), true);
617 if (entry->value != NULL) {
618 ASSERT(*script == *reinterpret_cast<Script**>(entry->value));
619 return;
620 }
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000621 // Globalize the script object, make it weak and use the location of the
622 // global handle as the value in the hash map.
623 Handle<Script> script_ =
hpayer@chromium.org4f99be92013-12-18 16:23:55 +0000624 Handle<Script>::cast(global_handles->Create(*script));
625 GlobalHandles::MakeWeak(reinterpret_cast<Object**>(script_.location()),
626 this,
627 ScriptCache::HandleWeakScript);
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000628 entry->value = script_.location();
629}
630
631
632Handle<FixedArray> ScriptCache::GetScripts() {
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +0000633 Factory* factory = isolate_->factory();
verwaest@chromium.orgd4be0f02013-06-05 13:39:03 +0000634 Handle<FixedArray> instances = factory->NewFixedArray(occupancy());
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000635 int count = 0;
636 for (HashMap::Entry* entry = Start(); entry != NULL; entry = Next(entry)) {
637 ASSERT(entry->value != NULL);
638 if (entry->value != NULL) {
639 instances->set(count, *reinterpret_cast<Script**>(entry->value));
640 count++;
641 }
642 }
643 return instances;
644}
645
646
647void ScriptCache::ProcessCollectedScripts() {
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +0000648 Debugger* debugger = isolate_->debugger();
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000649 for (int i = 0; i < collected_scripts_.length(); i++) {
lrn@chromium.org7516f052011-03-30 08:52:27 +0000650 debugger->OnScriptCollected(collected_scripts_[i]);
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000651 }
652 collected_scripts_.Clear();
653}
654
655
656void ScriptCache::Clear() {
657 // Iterate the script cache to get rid of all the weak handles.
658 for (HashMap::Entry* entry = Start(); entry != NULL; entry = Next(entry)) {
659 ASSERT(entry != NULL);
660 Object** location = reinterpret_cast<Object**>(entry->value);
661 ASSERT((*location)->IsScript());
hpayer@chromium.org4f99be92013-12-18 16:23:55 +0000662 GlobalHandles::ClearWeakness(location);
663 GlobalHandles::Destroy(location);
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000664 }
665 // Clear the content of the hash map.
666 HashMap::Clear();
667}
668
669
hpayer@chromium.org4f99be92013-12-18 16:23:55 +0000670void ScriptCache::HandleWeakScript(
671 const v8::WeakCallbackData<v8::Value, void>& data) {
672 // Retrieve the script identifier.
673 Handle<Object> object = Utils::OpenHandle(*data.GetValue());
674 int id = Handle<Script>::cast(object)->id()->value();
675 void* key = reinterpret_cast<void*>(id);
676 uint32_t hash = Hash(id);
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000677
hpayer@chromium.org4f99be92013-12-18 16:23:55 +0000678 // Remove the corresponding entry from the cache.
679 ScriptCache* script_cache =
680 reinterpret_cast<ScriptCache*>(data.GetParameter());
681 HashMap::Entry* entry = script_cache->Lookup(key, hash, false);
682 Object** location = reinterpret_cast<Object**>(entry->value);
683 script_cache->Remove(key, hash);
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000684 script_cache->collected_scripts_.Add(id);
685
686 // Clear the weak handle.
hpayer@chromium.org4f99be92013-12-18 16:23:55 +0000687 GlobalHandles::Destroy(location);
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000688}
689
690
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000691void Debug::SetUp(bool create_heap_objects) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000692 ThreadInit();
693 if (create_heap_objects) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000694 // Get code to handle debug break on return.
695 debug_break_return_ =
lrn@chromium.org7516f052011-03-30 08:52:27 +0000696 isolate_->builtins()->builtin(Builtins::kReturn_DebugBreak);
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000697 ASSERT(debug_break_return_->IsCode());
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000698 // Get code to handle debug break in debug break slots.
699 debug_break_slot_ =
lrn@chromium.org7516f052011-03-30 08:52:27 +0000700 isolate_->builtins()->builtin(Builtins::kSlot_DebugBreak);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000701 ASSERT(debug_break_slot_->IsCode());
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000702 }
703}
704
705
hpayer@chromium.org4f99be92013-12-18 16:23:55 +0000706void Debug::HandleWeakDebugInfo(
707 const v8::WeakCallbackData<v8::Value, void>& data) {
708 Debug* debug = reinterpret_cast<Isolate*>(data.GetIsolate())->debug();
709 DebugInfoListNode* node =
710 reinterpret_cast<DebugInfoListNode*>(data.GetParameter());
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000711 // We need to clear all breakpoints associated with the function to restore
712 // original code and avoid patching the code twice later because
713 // the function will live in the heap until next gc, and can be found by
jkummerow@chromium.org78502a92012-09-06 13:50:42 +0000714 // Debug::FindSharedFunctionInfoInScript.
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000715 BreakLocationIterator it(node->debug_info(), ALL_BREAK_LOCATIONS);
716 it.ClearAllDebugBreak();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000717 debug->RemoveDebugInfo(node->debug_info());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000718#ifdef DEBUG
hpayer@chromium.org4f99be92013-12-18 16:23:55 +0000719 for (DebugInfoListNode* n = debug->debug_info_list_;
720 n != NULL;
721 n = n->next()) {
722 ASSERT(n != node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000723 }
724#endif
725}
726
727
728DebugInfoListNode::DebugInfoListNode(DebugInfo* debug_info): next_(NULL) {
729 // Globalize the request debug info object and make it weak.
hpayer@chromium.org4f99be92013-12-18 16:23:55 +0000730 GlobalHandles* global_handles = debug_info->GetIsolate()->global_handles();
731 debug_info_ = Handle<DebugInfo>::cast(global_handles->Create(debug_info));
732 GlobalHandles::MakeWeak(reinterpret_cast<Object**>(debug_info_.location()),
733 this,
734 Debug::HandleWeakDebugInfo);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000735}
736
737
738DebugInfoListNode::~DebugInfoListNode() {
hpayer@chromium.org4f99be92013-12-18 16:23:55 +0000739 GlobalHandles::Destroy(reinterpret_cast<Object**>(debug_info_.location()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000740}
741
742
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +0000743bool Debug::CompileDebuggerScript(Isolate* isolate, int index) {
lrn@chromium.org7516f052011-03-30 08:52:27 +0000744 Factory* factory = isolate->factory();
745 HandleScope scope(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000746
kasper.lund44510672008-07-25 07:37:58 +0000747 // Bail out if the index is invalid.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000748 if (index == -1) {
749 return false;
750 }
kasper.lund44510672008-07-25 07:37:58 +0000751
752 // Find source and name for the requested script.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000753 Handle<String> source_code =
lrn@chromium.org7516f052011-03-30 08:52:27 +0000754 isolate->bootstrapper()->NativesSourceLookup(index);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000755 Vector<const char> name = Natives::GetScriptName(index);
machenbach@chromium.org84960272014-04-18 00:05:16 +0000756 Handle<String> script_name =
757 factory->NewStringFromAscii(name).ToHandleChecked();
yangguo@chromium.org355cfd12012-08-29 15:32:24 +0000758 Handle<Context> context = isolate->native_context();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000759
760 // Compile the script.
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000761 Handle<SharedFunctionInfo> function_info;
yangguo@chromium.org49546742013-12-23 16:17:49 +0000762 function_info = Compiler::CompileScript(source_code,
763 script_name, 0, 0,
764 false,
765 context,
machenbach@chromium.org69f64b12014-03-20 01:04:55 +0000766 NULL, NULL, NO_CACHED_DATA,
yangguo@chromium.org49546742013-12-23 16:17:49 +0000767 NATIVES_CODE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000768
769 // Silently ignore stack overflows during compilation.
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000770 if (function_info.is_null()) {
lrn@chromium.org7516f052011-03-30 08:52:27 +0000771 ASSERT(isolate->has_pending_exception());
772 isolate->clear_pending_exception();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000773 return false;
774 }
775
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000776 // Execute the shared function in the debugger context.
kasper.lund44510672008-07-25 07:37:58 +0000777 Handle<JSFunction> function =
lrn@chromium.org7516f052011-03-30 08:52:27 +0000778 factory->NewFunctionFromSharedFunctionInfo(function_info, context);
rossberg@chromium.org717967f2011-07-20 13:44:42 +0000779
machenbach@chromium.org2ebef182014-04-14 00:05:03 +0000780 Handle<Object> exception;
781 MaybeHandle<Object> result =
ulan@chromium.org09d7ab52013-02-25 15:50:35 +0000782 Execution::TryCall(function,
783 Handle<Object>(context->global_object(), isolate),
784 0,
785 NULL,
machenbach@chromium.org2ebef182014-04-14 00:05:03 +0000786 &exception);
kasper.lund44510672008-07-25 07:37:58 +0000787
788 // Check for caught exceptions.
machenbach@chromium.org2ebef182014-04-14 00:05:03 +0000789 if (result.is_null()) {
fschneider@chromium.org7d10be52012-04-10 12:30:14 +0000790 ASSERT(!isolate->has_pending_exception());
791 MessageLocation computed_location;
792 isolate->ComputeLocation(&computed_location);
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000793 Handle<Object> message = MessageHandler::MakeMessageObject(
verwaest@chromium.orgd4be0f02013-06-05 13:39:03 +0000794 isolate, "error_loading_debugger", &computed_location,
titzer@chromium.orgf5a24542014-03-04 09:06:17 +0000795 Vector<Handle<Object> >::empty(), Handle<JSArray>());
fschneider@chromium.org7d10be52012-04-10 12:30:14 +0000796 ASSERT(!isolate->has_pending_exception());
mmassi@chromium.org49a44672012-12-04 13:52:03 +0000797 if (!exception.is_null()) {
798 isolate->set_pending_exception(*exception);
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +0000799 MessageHandler::ReportMessage(isolate, NULL, message);
mmassi@chromium.org49a44672012-12-04 13:52:03 +0000800 isolate->clear_pending_exception();
801 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000802 return false;
803 }
804
kasper.lund44510672008-07-25 07:37:58 +0000805 // Mark this script as native and return successfully.
806 Handle<Script> script(Script::cast(function->shared()->script()));
ager@chromium.orge2902be2009-06-08 12:21:35 +0000807 script->set_type(Smi::FromInt(Script::TYPE_NATIVE));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000808 return true;
809}
810
811
812bool Debug::Load() {
813 // Return if debugger is already loaded.
kasper.lund212ac232008-07-16 07:07:30 +0000814 if (IsLoaded()) return true;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000815
lrn@chromium.org7516f052011-03-30 08:52:27 +0000816 Debugger* debugger = isolate_->debugger();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000817
kasper.lund44510672008-07-25 07:37:58 +0000818 // Bail out if we're already in the process of compiling the native
819 // JavaScript source code for the debugger.
lrn@chromium.org7516f052011-03-30 08:52:27 +0000820 if (debugger->compiling_natives() ||
821 debugger->is_loading_debugger())
mads.s.agercbaa0602008-08-14 13:41:48 +0000822 return false;
lrn@chromium.org7516f052011-03-30 08:52:27 +0000823 debugger->set_loading_debugger(true);
kasper.lund44510672008-07-25 07:37:58 +0000824
825 // Disable breakpoints and interrupts while compiling and running the
826 // debugger scripts including the context creation code.
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +0000827 DisableBreak disable(isolate_, true);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000828 PostponeInterruptsScope postpone(isolate_);
kasper.lund44510672008-07-25 07:37:58 +0000829
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000830 // Create the debugger context.
lrn@chromium.org7516f052011-03-30 08:52:27 +0000831 HandleScope scope(isolate_);
machenbach@chromium.org26ca35c2014-01-16 08:22:55 +0000832 ExtensionConfiguration no_extensions;
kasper.lund44510672008-07-25 07:37:58 +0000833 Handle<Context> context =
lrn@chromium.org7516f052011-03-30 08:52:27 +0000834 isolate_->bootstrapper()->CreateEnvironment(
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000835 Handle<Object>::null(),
836 v8::Handle<ObjectTemplate>(),
machenbach@chromium.org26ca35c2014-01-16 08:22:55 +0000837 &no_extensions);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000838
fschneider@chromium.org7d10be52012-04-10 12:30:14 +0000839 // Fail if no context could be created.
840 if (context.is_null()) return false;
841
kasper.lund44510672008-07-25 07:37:58 +0000842 // Use the debugger context.
lrn@chromium.org7516f052011-03-30 08:52:27 +0000843 SaveContext save(isolate_);
844 isolate_->set_context(*context);
kasper.lund44510672008-07-25 07:37:58 +0000845
846 // Expose the builtins object in the debugger context.
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000847 Handle<String> key = isolate_->factory()->InternalizeOneByteString(
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +0000848 STATIC_ASCII_VECTOR("builtins"));
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000849 Handle<GlobalObject> global = Handle<GlobalObject>(context->global_object());
machenbach@chromium.org8f8fe812014-04-07 00:05:03 +0000850 RETURN_ON_EXCEPTION_VALUE(
lrn@chromium.org7516f052011-03-30 08:52:27 +0000851 isolate_,
ulan@chromium.org09d7ab52013-02-25 15:50:35 +0000852 JSReceiver::SetProperty(global,
853 key,
854 Handle<Object>(global->builtins(), isolate_),
855 NONE,
dslomov@chromium.org486536d2014-03-12 13:09:18 +0000856 SLOPPY),
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +0000857 false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000858
859 // Compile the JavaScript for the debugger in the debugger context.
lrn@chromium.org7516f052011-03-30 08:52:27 +0000860 debugger->set_compiling_natives(true);
kasper.lund44510672008-07-25 07:37:58 +0000861 bool caught_exception =
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +0000862 !CompileDebuggerScript(isolate_, Natives::GetIndex("mirror")) ||
863 !CompileDebuggerScript(isolate_, Natives::GetIndex("debug"));
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000864
865 if (FLAG_enable_liveedit) {
866 caught_exception = caught_exception ||
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +0000867 !CompileDebuggerScript(isolate_, Natives::GetIndex("liveedit"));
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000868 }
869
lrn@chromium.org7516f052011-03-30 08:52:27 +0000870 debugger->set_compiling_natives(false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000871
mads.s.agercbaa0602008-08-14 13:41:48 +0000872 // Make sure we mark the debugger as not loading before we might
873 // return.
lrn@chromium.org7516f052011-03-30 08:52:27 +0000874 debugger->set_loading_debugger(false);
mads.s.agercbaa0602008-08-14 13:41:48 +0000875
kasper.lund44510672008-07-25 07:37:58 +0000876 // Check for caught exceptions.
877 if (caught_exception) return false;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000878
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000879 // Debugger loaded, create debugger context global handle.
880 debug_context_ = Handle<Context>::cast(
881 isolate_->global_handles()->Create(*context));
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000882
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000883 return true;
884}
885
886
887void Debug::Unload() {
888 // Return debugger is not loaded.
889 if (!IsLoaded()) {
890 return;
891 }
892
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000893 // Clear the script cache.
894 DestroyScriptCache();
895
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000896 // Clear debugger context global handle.
hpayer@chromium.org4f99be92013-12-18 16:23:55 +0000897 GlobalHandles::Destroy(reinterpret_cast<Object**>(debug_context_.location()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000898 debug_context_ = Handle<Context>();
899}
900
901
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000902// Set the flag indicating that preemption happened during debugging.
903void Debug::PreemptionWhileInDebugger() {
904 ASSERT(InDebugger());
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000905 Debug::set_interrupts_pending(PREEMPT);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000906}
907
908
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000909void Debug::Iterate(ObjectVisitor* v) {
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000910 v->VisitPointer(BitCast<Object**>(&(debug_break_return_)));
911 v->VisitPointer(BitCast<Object**>(&(debug_break_slot_)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000912}
913
914
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000915Object* Debug::Break(Arguments args) {
916 Heap* heap = isolate_->heap();
917 HandleScope scope(isolate_);
mads.s.ager31e71382008-08-13 09:32:07 +0000918 ASSERT(args.length() == 0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000919
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000920 thread_local_.frame_drop_mode_ = FRAMES_UNTOUCHED;
ager@chromium.org357bf652010-04-12 11:30:10 +0000921
kasper.lundbd3ec4e2008-07-09 11:06:54 +0000922 // Get the top-most JavaScript frame.
vegorov@chromium.org74f333b2011-04-06 11:17:46 +0000923 JavaScriptFrameIterator it(isolate_);
kasper.lundbd3ec4e2008-07-09 11:06:54 +0000924 JavaScriptFrame* frame = it.frame();
925
926 // Just continue if breaks are disabled or debugger cannot be loaded.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000927 if (disable_break() || !Load()) {
928 SetAfterBreakTarget(frame);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000929 return heap->undefined_value();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000930 }
931
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000932 // Enter the debugger.
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +0000933 EnterDebugger debugger(isolate_);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000934 if (debugger.FailedToEnter()) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000935 return heap->undefined_value();
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000936 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000937
kasper.lund44510672008-07-25 07:37:58 +0000938 // Postpone interrupt during breakpoint processing.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000939 PostponeInterruptsScope postpone(isolate_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000940
941 // Get the debug info (create it if it does not exist).
942 Handle<SharedFunctionInfo> shared =
danno@chromium.org169691d2013-07-15 08:01:13 +0000943 Handle<SharedFunctionInfo>(frame->function()->shared());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000944 Handle<DebugInfo> debug_info = GetDebugInfo(shared);
945
946 // Find the break point where execution has stopped.
947 BreakLocationIterator break_location_iterator(debug_info,
948 ALL_BREAK_LOCATIONS);
verwaest@chromium.org8a00e822013-06-10 15:11:22 +0000949 // pc points to the instruction after the current one, possibly a break
950 // location as well. So the "- 1" to exclude it from the search.
951 break_location_iterator.FindBreakLocationFromAddress(frame->pc() - 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000952
953 // Check whether step next reached a new statement.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000954 if (!StepNextContinue(&break_location_iterator, frame)) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000955 // Decrease steps left if performing multiple steps.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000956 if (thread_local_.step_count_ > 0) {
957 thread_local_.step_count_--;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000958 }
959 }
960
961 // If there is one or more real break points check whether any of these are
962 // triggered.
ulan@chromium.org09d7ab52013-02-25 15:50:35 +0000963 Handle<Object> break_points_hit(heap->undefined_value(), isolate_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000964 if (break_location_iterator.HasBreakPoint()) {
965 Handle<Object> break_point_objects =
ulan@chromium.org09d7ab52013-02-25 15:50:35 +0000966 Handle<Object>(break_location_iterator.BreakPointObjects(), isolate_);
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000967 break_points_hit = CheckBreakPoints(break_point_objects);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000968 }
969
ager@chromium.orga1645e22009-09-09 19:27:10 +0000970 // If step out is active skip everything until the frame where we need to step
971 // out to is reached, unless real breakpoint is hit.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000972 if (StepOutActive() && frame->fp() != step_out_fp() &&
ager@chromium.orga1645e22009-09-09 19:27:10 +0000973 break_points_hit->IsUndefined() ) {
974 // Step count should always be 0 for StepOut.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000975 ASSERT(thread_local_.step_count_ == 0);
ager@chromium.orga1645e22009-09-09 19:27:10 +0000976 } else if (!break_points_hit->IsUndefined() ||
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000977 (thread_local_.last_step_action_ != StepNone &&
978 thread_local_.step_count_ == 0)) {
ager@chromium.orga1645e22009-09-09 19:27:10 +0000979 // Notify debugger if a real break point is triggered or if performing
980 // single stepping with no more steps to perform. Otherwise do another step.
981
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000982 // Clear all current stepping setup.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000983 ClearStepping();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000984
lrn@chromium.org34e60782011-09-15 07:25:40 +0000985 if (thread_local_.queued_step_count_ > 0) {
986 // Perform queued steps
987 int step_count = thread_local_.queued_step_count_;
988
989 // Clear queue
990 thread_local_.queued_step_count_ = 0;
991
dslomov@chromium.org639bac02013-09-09 11:58:54 +0000992 PrepareStep(StepNext, step_count, StackFrame::NO_ID);
lrn@chromium.org34e60782011-09-15 07:25:40 +0000993 } else {
994 // Notify the debug event listeners.
995 isolate_->debugger()->OnDebugBreak(break_points_hit, false);
996 }
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000997 } else if (thread_local_.last_step_action_ != StepNone) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000998 // Hold on to last step action as it is cleared by the call to
999 // ClearStepping.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001000 StepAction step_action = thread_local_.last_step_action_;
1001 int step_count = thread_local_.step_count_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001002
lrn@chromium.org34e60782011-09-15 07:25:40 +00001003 // If StepNext goes deeper in code, StepOut until original frame
1004 // and keep step count queued up in the meantime.
1005 if (step_action == StepNext && frame->fp() < thread_local_.last_fp_) {
1006 // Count frames until target frame
1007 int count = 0;
1008 JavaScriptFrameIterator it(isolate_);
jkummerow@chromium.org212d9642012-05-11 15:02:09 +00001009 while (!it.done() && it.frame()->fp() < thread_local_.last_fp_) {
lrn@chromium.org34e60782011-09-15 07:25:40 +00001010 count++;
1011 it.Advance();
1012 }
1013
danno@chromium.org81cac2b2012-07-10 11:28:27 +00001014 // Check that we indeed found the frame we are looking for.
1015 CHECK(!it.done() && (it.frame()->fp() == thread_local_.last_fp_));
1016 if (step_count > 1) {
1017 // Save old count and action to continue stepping after StepOut.
1018 thread_local_.queued_step_count_ = step_count - 1;
jkummerow@chromium.org212d9642012-05-11 15:02:09 +00001019 }
1020
danno@chromium.org81cac2b2012-07-10 11:28:27 +00001021 // Set up for StepOut to reach target frame.
1022 step_action = StepOut;
1023 step_count = count;
lrn@chromium.org34e60782011-09-15 07:25:40 +00001024 }
1025
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001026 // Clear all current stepping setup.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001027 ClearStepping();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001028
1029 // Set up for the remaining steps.
dslomov@chromium.org639bac02013-09-09 11:58:54 +00001030 PrepareStep(step_action, step_count, StackFrame::NO_ID);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001031 }
1032
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001033 if (thread_local_.frame_drop_mode_ == FRAMES_UNTOUCHED) {
1034 SetAfterBreakTarget(frame);
1035 } else if (thread_local_.frame_drop_mode_ ==
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001036 FRAME_DROPPED_IN_IC_CALL) {
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001037 // We must have been calling IC stub. Do not go there anymore.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001038 Code* plain_return = isolate_->builtins()->builtin(
1039 Builtins::kPlainReturn_LiveEdit);
1040 thread_local_.after_break_target_ = plain_return->entry();
1041 } else if (thread_local_.frame_drop_mode_ ==
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001042 FRAME_DROPPED_IN_DEBUG_SLOT_CALL) {
1043 // Debug break slot stub does not return normally, instead it manually
1044 // cleans the stack and jumps. We should patch the jump address.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001045 Code* plain_return = isolate_->builtins()->builtin(
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00001046 Builtins::kFrameDropper_LiveEdit);
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001047 thread_local_.after_break_target_ = plain_return->entry();
1048 } else if (thread_local_.frame_drop_mode_ ==
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001049 FRAME_DROPPED_IN_DIRECT_CALL) {
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001050 // Nothing to do, after_break_target is not used here.
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00001051 } else if (thread_local_.frame_drop_mode_ ==
1052 FRAME_DROPPED_IN_RETURN_CALL) {
1053 Code* plain_return = isolate_->builtins()->builtin(
1054 Builtins::kFrameDropper_LiveEdit);
1055 thread_local_.after_break_target_ = plain_return->entry();
ager@chromium.org357bf652010-04-12 11:30:10 +00001056 } else {
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001057 UNREACHABLE();
ager@chromium.org357bf652010-04-12 11:30:10 +00001058 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001059
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001060 return heap->undefined_value();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001061}
1062
1063
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001064RUNTIME_FUNCTION(Object*, Debug_Break) {
1065 return isolate->debug()->Break(args);
1066}
1067
1068
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001069// Check the break point objects for whether one or more are actually
1070// triggered. This function returns a JSArray with the break point objects
1071// which is triggered.
1072Handle<Object> Debug::CheckBreakPoints(Handle<Object> break_point_objects) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00001073 Factory* factory = isolate_->factory();
1074
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001075 // Count the number of break points hit. If there are multiple break points
1076 // they are in a FixedArray.
1077 Handle<FixedArray> break_points_hit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001078 int break_points_hit_count = 0;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001079 ASSERT(!break_point_objects->IsUndefined());
1080 if (break_point_objects->IsFixedArray()) {
1081 Handle<FixedArray> array(FixedArray::cast(*break_point_objects));
lrn@chromium.org7516f052011-03-30 08:52:27 +00001082 break_points_hit = factory->NewFixedArray(array->length());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001083 for (int i = 0; i < array->length(); i++) {
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00001084 Handle<Object> o(array->get(i), isolate_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001085 if (CheckBreakPoint(o)) {
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001086 break_points_hit->set(break_points_hit_count++, *o);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001087 }
1088 }
1089 } else {
lrn@chromium.org7516f052011-03-30 08:52:27 +00001090 break_points_hit = factory->NewFixedArray(1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001091 if (CheckBreakPoint(break_point_objects)) {
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001092 break_points_hit->set(break_points_hit_count++, *break_point_objects);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001093 }
1094 }
1095
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001096 // Return undefined if no break points were triggered.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001097 if (break_points_hit_count == 0) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00001098 return factory->undefined_value();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001099 }
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001100 // Return break points hit as a JSArray.
lrn@chromium.org7516f052011-03-30 08:52:27 +00001101 Handle<JSArray> result = factory->NewJSArrayWithElements(break_points_hit);
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001102 result->set_length(Smi::FromInt(break_points_hit_count));
1103 return result;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001104}
1105
1106
1107// Check whether a single break point object is triggered.
1108bool Debug::CheckBreakPoint(Handle<Object> break_point_object) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00001109 Factory* factory = isolate_->factory();
1110 HandleScope scope(isolate_);
ager@chromium.org381abbb2009-02-25 13:23:22 +00001111
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001112 // Ignore check if break point object is not a JSObject.
1113 if (!break_point_object->IsJSObject()) return true;
1114
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001115 // Get the function IsBreakPointTriggered (defined in debug-debugger.js).
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001116 Handle<String> is_break_point_triggered_string =
1117 factory->InternalizeOneByteString(
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00001118 STATIC_ASCII_VECTOR("IsBreakPointTriggered"));
machenbach@chromium.org5b080562014-04-10 00:05:02 +00001119 Handle<GlobalObject> debug_global(debug_context()->global_object());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001120 Handle<JSFunction> check_break_point =
machenbach@chromium.org2ebef182014-04-14 00:05:03 +00001121 Handle<JSFunction>::cast(Object::GetProperty(
1122 debug_global, is_break_point_triggered_string).ToHandleChecked());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001123
1124 // Get the break id as an object.
lrn@chromium.org7516f052011-03-30 08:52:27 +00001125 Handle<Object> break_id = factory->NewNumberFromInt(Debug::break_id());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001126
1127 // Call HandleBreakPointx.
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00001128 Handle<Object> argv[] = { break_id, break_point_object };
machenbach@chromium.org2ebef182014-04-14 00:05:03 +00001129 Handle<Object> result;
machenbach@chromium.orge9fd6582014-04-16 00:05:02 +00001130 if (!Execution::TryCall(check_break_point,
1131 isolate_->js_builtins_object(),
1132 ARRAY_SIZE(argv),
1133 argv).ToHandle(&result)) {
1134 return false;
1135 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001136
1137 // Return whether the break point is triggered.
machenbach@chromium.org2ebef182014-04-14 00:05:03 +00001138 return result->IsTrue();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001139}
1140
1141
1142// Check whether the function has debug information.
1143bool Debug::HasDebugInfo(Handle<SharedFunctionInfo> shared) {
1144 return !shared->debug_info()->IsUndefined();
1145}
1146
1147
kasper.lundbd3ec4e2008-07-09 11:06:54 +00001148// Return the debug info for this function. EnsureDebugInfo must be called
1149// prior to ensure the debug info has been generated for shared.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001150Handle<DebugInfo> Debug::GetDebugInfo(Handle<SharedFunctionInfo> shared) {
kasper.lundbd3ec4e2008-07-09 11:06:54 +00001151 ASSERT(HasDebugInfo(shared));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001152 return Handle<DebugInfo>(DebugInfo::cast(shared->debug_info()));
1153}
1154
1155
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001156void Debug::SetBreakPoint(Handle<JSFunction> function,
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00001157 Handle<Object> break_point_object,
1158 int* source_position) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00001159 HandleScope scope(isolate_);
ager@chromium.org381abbb2009-02-25 13:23:22 +00001160
lrn@chromium.org34e60782011-09-15 07:25:40 +00001161 PrepareForBreakPoints();
1162
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001163 // Make sure the function is compiled and has set up the debug info.
1164 Handle<SharedFunctionInfo> shared(function->shared());
1165 if (!EnsureDebugInfo(shared, function)) {
kasper.lundbd3ec4e2008-07-09 11:06:54 +00001166 // Return if retrieving debug info failed.
1167 return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001168 }
1169
kasper.lundbd3ec4e2008-07-09 11:06:54 +00001170 Handle<DebugInfo> debug_info = GetDebugInfo(shared);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001171 // Source positions starts with zero.
danno@chromium.orgbf0c8202011-12-27 10:09:42 +00001172 ASSERT(*source_position >= 0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001173
1174 // Find the break point and change it.
1175 BreakLocationIterator it(debug_info, SOURCE_BREAK_LOCATIONS);
jkummerow@chromium.org93a47f42013-07-02 14:43:41 +00001176 it.FindBreakLocationFromPosition(*source_position, STATEMENT_ALIGNED);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001177 it.SetBreakPoint(break_point_object);
1178
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00001179 *source_position = it.position();
1180
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001181 // At least one active break point now.
1182 ASSERT(debug_info->GetBreakPointCount() > 0);
1183}
1184
1185
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001186bool Debug::SetBreakPointForScript(Handle<Script> script,
1187 Handle<Object> break_point_object,
jkummerow@chromium.org93a47f42013-07-02 14:43:41 +00001188 int* source_position,
1189 BreakPositionAlignment alignment) {
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001190 HandleScope scope(isolate_);
1191
jkummerow@chromium.org78502a92012-09-06 13:50:42 +00001192 PrepareForBreakPoints();
1193
1194 // Obtain shared function info for the function.
1195 Object* result = FindSharedFunctionInfoInScript(script, *source_position);
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001196 if (result->IsUndefined()) return false;
1197
1198 // Make sure the function has set up the debug info.
1199 Handle<SharedFunctionInfo> shared(SharedFunctionInfo::cast(result));
1200 if (!EnsureDebugInfo(shared, Handle<JSFunction>::null())) {
1201 // Return if retrieving debug info failed.
1202 return false;
1203 }
1204
1205 // Find position within function. The script position might be before the
1206 // source position of the first function.
1207 int position;
1208 if (shared->start_position() > *source_position) {
1209 position = 0;
1210 } else {
1211 position = *source_position - shared->start_position();
1212 }
1213
1214 Handle<DebugInfo> debug_info = GetDebugInfo(shared);
1215 // Source positions starts with zero.
1216 ASSERT(position >= 0);
1217
1218 // Find the break point and change it.
1219 BreakLocationIterator it(debug_info, SOURCE_BREAK_LOCATIONS);
jkummerow@chromium.org93a47f42013-07-02 14:43:41 +00001220 it.FindBreakLocationFromPosition(position, alignment);
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001221 it.SetBreakPoint(break_point_object);
1222
1223 *source_position = it.position() + shared->start_position();
1224
1225 // At least one active break point now.
1226 ASSERT(debug_info->GetBreakPointCount() > 0);
1227 return true;
1228}
1229
1230
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001231void Debug::ClearBreakPoint(Handle<Object> break_point_object) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00001232 HandleScope scope(isolate_);
ager@chromium.org381abbb2009-02-25 13:23:22 +00001233
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001234 DebugInfoListNode* node = debug_info_list_;
1235 while (node != NULL) {
1236 Object* result = DebugInfo::FindBreakPointInfo(node->debug_info(),
1237 break_point_object);
1238 if (!result->IsUndefined()) {
1239 // Get information in the break point.
1240 BreakPointInfo* break_point_info = BreakPointInfo::cast(result);
1241 Handle<DebugInfo> debug_info = node->debug_info();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001242
1243 // Find the break point and clear it.
1244 BreakLocationIterator it(debug_info, SOURCE_BREAK_LOCATIONS);
verwaest@chromium.org8a00e822013-06-10 15:11:22 +00001245 it.FindBreakLocationFromAddress(debug_info->code()->entry() +
1246 break_point_info->code_position()->value());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001247 it.ClearBreakPoint(break_point_object);
1248
1249 // If there are no more break points left remove the debug info for this
1250 // function.
1251 if (debug_info->GetBreakPointCount() == 0) {
1252 RemoveDebugInfo(debug_info);
1253 }
1254
1255 return;
1256 }
1257 node = node->next();
1258 }
1259}
1260
1261
ager@chromium.org381abbb2009-02-25 13:23:22 +00001262void Debug::ClearAllBreakPoints() {
1263 DebugInfoListNode* node = debug_info_list_;
1264 while (node != NULL) {
1265 // Remove all debug break code.
1266 BreakLocationIterator it(node->debug_info(), ALL_BREAK_LOCATIONS);
1267 it.ClearAllDebugBreak();
1268 node = node->next();
1269 }
1270
1271 // Remove all debug info.
1272 while (debug_info_list_ != NULL) {
1273 RemoveDebugInfo(debug_info_list_->debug_info());
1274 }
1275}
1276
1277
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001278void Debug::FloodWithOneShot(Handle<JSFunction> function) {
lrn@chromium.org34e60782011-09-15 07:25:40 +00001279 PrepareForBreakPoints();
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001280
1281 // Make sure the function is compiled and has set up the debug info.
1282 Handle<SharedFunctionInfo> shared(function->shared());
1283 if (!EnsureDebugInfo(shared, function)) {
kasper.lundbd3ec4e2008-07-09 11:06:54 +00001284 // Return if we failed to retrieve the debug info.
1285 return;
1286 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001287
1288 // Flood the function with break points.
kasper.lundbd3ec4e2008-07-09 11:06:54 +00001289 BreakLocationIterator it(GetDebugInfo(shared), ALL_BREAK_LOCATIONS);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001290 while (!it.Done()) {
1291 it.SetOneShot();
1292 it.Next();
1293 }
1294}
1295
1296
rossberg@chromium.org2c067b12012-03-19 11:01:52 +00001297void Debug::FloodBoundFunctionWithOneShot(Handle<JSFunction> function) {
1298 Handle<FixedArray> new_bindings(function->function_bindings());
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00001299 Handle<Object> bindee(new_bindings->get(JSFunction::kBoundFunctionIndex),
1300 isolate_);
rossberg@chromium.org2c067b12012-03-19 11:01:52 +00001301
1302 if (!bindee.is_null() && bindee->IsJSFunction() &&
1303 !JSFunction::cast(*bindee)->IsBuiltin()) {
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001304 Handle<JSFunction> bindee_function(JSFunction::cast(*bindee));
1305 Debug::FloodWithOneShot(bindee_function);
rossberg@chromium.org2c067b12012-03-19 11:01:52 +00001306 }
1307}
1308
1309
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001310void Debug::FloodHandlerWithOneShot() {
ager@chromium.org8bb60582008-12-11 12:02:20 +00001311 // Iterate through the JavaScript stack looking for handlers.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001312 StackFrame::Id id = break_frame_id();
ager@chromium.org8bb60582008-12-11 12:02:20 +00001313 if (id == StackFrame::NO_ID) {
1314 // If there is no JavaScript stack don't do anything.
1315 return;
1316 }
vegorov@chromium.org74f333b2011-04-06 11:17:46 +00001317 for (JavaScriptFrameIterator it(isolate_, id); !it.done(); it.Advance()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001318 JavaScriptFrame* frame = it.frame();
1319 if (frame->HasHandler()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001320 // Flood the function with the catch block with break points
danno@chromium.org169691d2013-07-15 08:01:13 +00001321 FloodWithOneShot(Handle<JSFunction>(frame->function()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001322 return;
1323 }
1324 }
1325}
1326
1327
1328void Debug::ChangeBreakOnException(ExceptionBreakType type, bool enable) {
1329 if (type == BreakUncaughtException) {
1330 break_on_uncaught_exception_ = enable;
1331 } else {
1332 break_on_exception_ = enable;
1333 }
1334}
1335
1336
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00001337bool Debug::IsBreakOnException(ExceptionBreakType type) {
1338 if (type == BreakUncaughtException) {
1339 return break_on_uncaught_exception_;
1340 } else {
1341 return break_on_exception_;
1342 }
1343}
1344
1345
dslomov@chromium.org639bac02013-09-09 11:58:54 +00001346void Debug::PrepareStep(StepAction step_action,
1347 int step_count,
1348 StackFrame::Id frame_id) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00001349 HandleScope scope(isolate_);
lrn@chromium.org34e60782011-09-15 07:25:40 +00001350
1351 PrepareForBreakPoints();
1352
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001353 ASSERT(Debug::InDebugger());
1354
1355 // Remember this step action and count.
1356 thread_local_.last_step_action_ = step_action;
ager@chromium.orga1645e22009-09-09 19:27:10 +00001357 if (step_action == StepOut) {
1358 // For step out target frame will be found on the stack so there is no need
1359 // to set step counter for it. It's expected to always be 0 for StepOut.
1360 thread_local_.step_count_ = 0;
1361 } else {
1362 thread_local_.step_count_ = step_count;
1363 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001364
1365 // Get the frame where the execution has stopped and skip the debug frame if
1366 // any. The debug frame will only be present if execution was stopped due to
1367 // hitting a break point. In other situations (e.g. unhandled exception) the
1368 // debug frame is not present.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001369 StackFrame::Id id = break_frame_id();
ager@chromium.org8bb60582008-12-11 12:02:20 +00001370 if (id == StackFrame::NO_ID) {
1371 // If there is no JavaScript stack don't do anything.
1372 return;
1373 }
dslomov@chromium.org639bac02013-09-09 11:58:54 +00001374 if (frame_id != StackFrame::NO_ID) {
1375 id = frame_id;
1376 }
vegorov@chromium.org74f333b2011-04-06 11:17:46 +00001377 JavaScriptFrameIterator frames_it(isolate_, id);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001378 JavaScriptFrame* frame = frames_it.frame();
1379
1380 // First of all ensure there is one-shot break points in the top handler
1381 // if any.
1382 FloodHandlerWithOneShot();
1383
1384 // If the function on the top frame is unresolved perform step out. This will
1385 // be the case when calling unknown functions and having the debugger stopped
1386 // in an unhandled exception.
1387 if (!frame->function()->IsJSFunction()) {
1388 // Step out: Find the calling JavaScript frame and flood it with
1389 // breakpoints.
1390 frames_it.Advance();
1391 // Fill the function to return to with one-shot break points.
danno@chromium.org169691d2013-07-15 08:01:13 +00001392 JSFunction* function = frames_it.frame()->function();
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001393 FloodWithOneShot(Handle<JSFunction>(function));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001394 return;
1395 }
1396
1397 // Get the debug info (create it if it does not exist).
danno@chromium.org169691d2013-07-15 08:01:13 +00001398 Handle<JSFunction> function(frame->function());
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001399 Handle<SharedFunctionInfo> shared(function->shared());
1400 if (!EnsureDebugInfo(shared, function)) {
kasper.lundbd3ec4e2008-07-09 11:06:54 +00001401 // Return if ensuring debug info failed.
1402 return;
1403 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001404 Handle<DebugInfo> debug_info = GetDebugInfo(shared);
1405
1406 // Find the break location where execution has stopped.
1407 BreakLocationIterator it(debug_info, ALL_BREAK_LOCATIONS);
verwaest@chromium.org8a00e822013-06-10 15:11:22 +00001408 // pc points to the instruction after the current one, possibly a break
1409 // location as well. So the "- 1" to exclude it from the search.
1410 it.FindBreakLocationFromAddress(frame->pc() - 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001411
1412 // Compute whether or not the target is a call target.
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001413 bool is_load_or_store = false;
1414 bool is_inline_cache_stub = false;
whesse@chromium.orge90029b2010-08-02 11:52:17 +00001415 bool is_at_restarted_function = false;
ager@chromium.orga1645e22009-09-09 19:27:10 +00001416 Handle<Code> call_function_stub;
ager@chromium.orga1645e22009-09-09 19:27:10 +00001417
whesse@chromium.orge90029b2010-08-02 11:52:17 +00001418 if (thread_local_.restarter_frame_function_pointer_ == NULL) {
1419 if (RelocInfo::IsCodeTarget(it.rinfo()->rmode())) {
1420 bool is_call_target = false;
1421 Address target = it.rinfo()->target_address();
1422 Code* code = Code::GetCodeFromTargetAddress(target);
whesse@chromium.orge90029b2010-08-02 11:52:17 +00001423 if (code->is_inline_cache_stub()) {
1424 is_inline_cache_stub = true;
1425 is_load_or_store = !is_call_target;
1426 }
1427
1428 // Check if target code is CallFunction stub.
1429 Code* maybe_call_function_stub = code;
1430 // If there is a breakpoint at this line look at the original code to
1431 // check if it is a CallFunction stub.
1432 if (it.IsDebugBreak()) {
1433 Address original_target = it.original_rinfo()->target_address();
1434 maybe_call_function_stub =
1435 Code::GetCodeFromTargetAddress(original_target);
1436 }
1437 if (maybe_call_function_stub->kind() == Code::STUB &&
1438 maybe_call_function_stub->major_key() == CodeStub::CallFunction) {
1439 // Save reference to the code as we may need it to find out arguments
1440 // count for 'step in' later.
1441 call_function_stub = Handle<Code>(maybe_call_function_stub);
1442 }
ager@chromium.orga1645e22009-09-09 19:27:10 +00001443 }
whesse@chromium.orge90029b2010-08-02 11:52:17 +00001444 } else {
1445 is_at_restarted_function = true;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001446 }
1447
v8.team.kasperl727e9952008-09-02 14:56:44 +00001448 // If this is the last break code target step out is the only possibility.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001449 if (it.IsExit() || step_action == StepOut) {
ager@chromium.orga1645e22009-09-09 19:27:10 +00001450 if (step_action == StepOut) {
1451 // Skip step_count frames starting with the current one.
1452 while (step_count-- > 0 && !frames_it.done()) {
1453 frames_it.Advance();
1454 }
1455 } else {
1456 ASSERT(it.IsExit());
1457 frames_it.Advance();
1458 }
1459 // Skip builtin functions on the stack.
danno@chromium.org169691d2013-07-15 08:01:13 +00001460 while (!frames_it.done() && frames_it.frame()->function()->IsBuiltin()) {
ager@chromium.orga1645e22009-09-09 19:27:10 +00001461 frames_it.Advance();
1462 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001463 // Step out: If there is a JavaScript caller frame, we need to
1464 // flood it with breakpoints.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001465 if (!frames_it.done()) {
1466 // Fill the function to return to with one-shot break points.
danno@chromium.org169691d2013-07-15 08:01:13 +00001467 JSFunction* function = frames_it.frame()->function();
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001468 FloodWithOneShot(Handle<JSFunction>(function));
ager@chromium.orga1645e22009-09-09 19:27:10 +00001469 // Set target frame pointer.
1470 ActivateStepOut(frames_it.frame());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001471 }
ager@chromium.orga1645e22009-09-09 19:27:10 +00001472 } else if (!(is_inline_cache_stub || RelocInfo::IsConstructCall(it.rmode()) ||
whesse@chromium.orge90029b2010-08-02 11:52:17 +00001473 !call_function_stub.is_null() || is_at_restarted_function)
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001474 || step_action == StepNext || step_action == StepMin) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001475 // Step next or step min.
1476
1477 // Fill the current function with one-shot break points.
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001478 FloodWithOneShot(function);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001479
1480 // Remember source position and frame to handle step next.
1481 thread_local_.last_statement_position_ =
1482 debug_info->code()->SourceStatementPosition(frame->pc());
mmassi@chromium.org7028c052012-06-13 11:51:58 +00001483 thread_local_.last_fp_ = frame->UnpaddedFP();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001484 } else {
whesse@chromium.orge90029b2010-08-02 11:52:17 +00001485 // If there's restarter frame on top of the stack, just get the pointer
1486 // to function which is going to be restarted.
1487 if (is_at_restarted_function) {
1488 Handle<JSFunction> restarted_function(
1489 JSFunction::cast(*thread_local_.restarter_frame_function_pointer_));
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001490 FloodWithOneShot(restarted_function);
whesse@chromium.orge90029b2010-08-02 11:52:17 +00001491 } else if (!call_function_stub.is_null()) {
1492 // If it's CallFunction stub ensure target function is compiled and flood
1493 // it with one shot breakpoints.
1494
ager@chromium.orga1645e22009-09-09 19:27:10 +00001495 // Find out number of arguments from the stub minor key.
1496 // Reverse lookup required as the minor key cannot be retrieved
1497 // from the code object.
1498 Handle<Object> obj(
lrn@chromium.org7516f052011-03-30 08:52:27 +00001499 isolate_->heap()->code_stubs()->SlowReverseLookup(
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00001500 *call_function_stub),
1501 isolate_);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001502 ASSERT(!obj.is_null());
1503 ASSERT(!(*obj)->IsUndefined());
ager@chromium.orga1645e22009-09-09 19:27:10 +00001504 ASSERT(obj->IsSmi());
1505 // Get the STUB key and extract major and minor key.
1506 uint32_t key = Smi::cast(*obj)->value();
1507 // Argc in the stub is the number of arguments passed - not the
1508 // expected arguments of the called function.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001509 int call_function_arg_count =
1510 CallFunctionStub::ExtractArgcFromMinorKey(
1511 CodeStub::MinorKeyFromKey(key));
ager@chromium.orga1645e22009-09-09 19:27:10 +00001512 ASSERT(call_function_stub->major_key() ==
1513 CodeStub::MajorKeyFromKey(key));
1514
1515 // Find target function on the expression stack.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001516 // Expression stack looks like this (top to bottom):
ager@chromium.orga1645e22009-09-09 19:27:10 +00001517 // argN
1518 // ...
1519 // arg0
1520 // Receiver
1521 // Function to call
1522 int expressions_count = frame->ComputeExpressionsCount();
1523 ASSERT(expressions_count - 2 - call_function_arg_count >= 0);
1524 Object* fun = frame->GetExpression(
1525 expressions_count - 2 - call_function_arg_count);
machenbach@chromium.orga03ba1e2014-02-01 08:54:43 +00001526
1527 // Flood the actual target of call/apply.
1528 if (fun->IsJSFunction()) {
1529 Isolate* isolate = JSFunction::cast(fun)->GetIsolate();
1530 Code* apply = isolate->builtins()->builtin(Builtins::kFunctionApply);
1531 Code* call = isolate->builtins()->builtin(Builtins::kFunctionCall);
1532 while (fun->IsJSFunction()) {
1533 Code* code = JSFunction::cast(fun)->shared()->code();
1534 if (code != apply && code != call) break;
1535 fun = frame->GetExpression(
1536 expressions_count - 1 - call_function_arg_count);
1537 }
1538 }
1539
ager@chromium.orga1645e22009-09-09 19:27:10 +00001540 if (fun->IsJSFunction()) {
1541 Handle<JSFunction> js_function(JSFunction::cast(fun));
rossberg@chromium.org2c067b12012-03-19 11:01:52 +00001542 if (js_function->shared()->bound()) {
1543 Debug::FloodBoundFunctionWithOneShot(js_function);
1544 } else if (!js_function->IsBuiltin()) {
1545 // Don't step into builtins.
ager@chromium.orga1645e22009-09-09 19:27:10 +00001546 // It will also compile target function if it's not compiled yet.
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001547 FloodWithOneShot(js_function);
ager@chromium.orga1645e22009-09-09 19:27:10 +00001548 }
1549 }
1550 }
1551
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001552 // Fill the current function with one-shot break points even for step in on
1553 // a call target as the function called might be a native function for
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001554 // which step in will not stop. It also prepares for stepping in
1555 // getters/setters.
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001556 FloodWithOneShot(function);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001557
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001558 if (is_load_or_store) {
1559 // Remember source position and frame to handle step in getter/setter. If
1560 // there is a custom getter/setter it will be handled in
1561 // Object::Get/SetPropertyWithCallback, otherwise the step action will be
1562 // propagated on the next Debug::Break.
1563 thread_local_.last_statement_position_ =
1564 debug_info->code()->SourceStatementPosition(frame->pc());
mmassi@chromium.org7028c052012-06-13 11:51:58 +00001565 thread_local_.last_fp_ = frame->UnpaddedFP();
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001566 }
1567
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001568 // Step in or Step in min
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00001569 it.PrepareStepIn(isolate_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001570 ActivateStepIn(frame);
1571 }
1572}
1573
1574
1575// Check whether the current debug break should be reported to the debugger. It
1576// is used to have step next and step in only report break back to the debugger
1577// if on a different frame or in a different statement. In some situations
1578// there will be several break points in the same statement when the code is
v8.team.kasperl727e9952008-09-02 14:56:44 +00001579// flooded with one-shot break points. This function helps to perform several
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001580// steps before reporting break back to the debugger.
1581bool Debug::StepNextContinue(BreakLocationIterator* break_location_iterator,
1582 JavaScriptFrame* frame) {
lrn@chromium.org34e60782011-09-15 07:25:40 +00001583 // StepNext and StepOut shouldn't bring us deeper in code, so last frame
1584 // shouldn't be a parent of current frame.
1585 if (thread_local_.last_step_action_ == StepNext ||
1586 thread_local_.last_step_action_ == StepOut) {
1587 if (frame->fp() < thread_local_.last_fp_) return true;
1588 }
1589
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001590 // If the step last action was step next or step in make sure that a new
1591 // statement is hit.
1592 if (thread_local_.last_step_action_ == StepNext ||
1593 thread_local_.last_step_action_ == StepIn) {
1594 // Never continue if returning from function.
1595 if (break_location_iterator->IsExit()) return false;
1596
1597 // Continue if we are still on the same frame and in the same statement.
1598 int current_statement_position =
1599 break_location_iterator->code()->SourceStatementPosition(frame->pc());
mmassi@chromium.org7028c052012-06-13 11:51:58 +00001600 return thread_local_.last_fp_ == frame->UnpaddedFP() &&
ager@chromium.org236ad962008-09-25 09:45:57 +00001601 thread_local_.last_statement_position_ == current_statement_position;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001602 }
1603
1604 // No step next action - don't continue.
1605 return false;
1606}
1607
1608
1609// Check whether the code object at the specified address is a debug break code
1610// object.
1611bool Debug::IsDebugBreak(Address addr) {
ager@chromium.org8bb60582008-12-11 12:02:20 +00001612 Code* code = Code::GetCodeFromTargetAddress(addr);
verwaest@chromium.orgec6855e2013-08-22 12:26:58 +00001613 return code->is_debug_stub() && code->extra_ic_state() == DEBUG_BREAK;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001614}
1615
1616
1617// Check whether a code stub with the specified major key is a possible break
1618// point location when looking for source break locations.
1619bool Debug::IsSourceBreakStub(Code* code) {
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001620 CodeStub::Major major_key = CodeStub::GetMajorKey(code);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001621 return major_key == CodeStub::CallFunction;
1622}
1623
1624
1625// Check whether a code stub with the specified major key is a possible break
1626// location.
1627bool Debug::IsBreakStub(Code* code) {
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001628 CodeStub::Major major_key = CodeStub::GetMajorKey(code);
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00001629 return major_key == CodeStub::CallFunction;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001630}
1631
1632
1633// Find the builtin to use for invoking the debug break
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001634Handle<Code> Debug::FindDebugBreak(Handle<Code> code, RelocInfo::Mode mode) {
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +00001635 Isolate* isolate = code->GetIsolate();
danno@chromium.orgfa458e42012-02-01 10:48:36 +00001636
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001637 // Find the builtin debug break function matching the calling convention
1638 // used by the call site.
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001639 if (code->is_inline_cache_stub()) {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001640 switch (code->kind()) {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001641 case Code::LOAD_IC:
danno@chromium.orgfa458e42012-02-01 10:48:36 +00001642 return isolate->builtins()->LoadIC_DebugBreak();
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001643
1644 case Code::STORE_IC:
danno@chromium.orgfa458e42012-02-01 10:48:36 +00001645 return isolate->builtins()->StoreIC_DebugBreak();
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001646
1647 case Code::KEYED_LOAD_IC:
danno@chromium.orgfa458e42012-02-01 10:48:36 +00001648 return isolate->builtins()->KeyedLoadIC_DebugBreak();
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001649
1650 case Code::KEYED_STORE_IC:
danno@chromium.orgfa458e42012-02-01 10:48:36 +00001651 return isolate->builtins()->KeyedStoreIC_DebugBreak();
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001652
danno@chromium.orgf005df62013-04-30 16:36:45 +00001653 case Code::COMPARE_NIL_IC:
1654 return isolate->builtins()->CompareNilIC_DebugBreak();
1655
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001656 default:
1657 UNREACHABLE();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001658 }
1659 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001660 if (RelocInfo::IsConstructCall(mode)) {
danno@chromium.orgfa458e42012-02-01 10:48:36 +00001661 if (code->has_function_cache()) {
1662 return isolate->builtins()->CallConstructStub_Recording_DebugBreak();
1663 } else {
1664 return isolate->builtins()->CallConstructStub_DebugBreak();
1665 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001666 }
1667 if (code->kind() == Code::STUB) {
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00001668 ASSERT(code->major_key() == CodeStub::CallFunction);
danno@chromium.orgfa458e42012-02-01 10:48:36 +00001669 if (code->has_function_cache()) {
1670 return isolate->builtins()->CallFunctionStub_Recording_DebugBreak();
1671 } else {
1672 return isolate->builtins()->CallFunctionStub_DebugBreak();
1673 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001674 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001675
1676 UNREACHABLE();
1677 return Handle<Code>::null();
1678}
1679
1680
1681// Simple function for returning the source positions for active break points.
1682Handle<Object> Debug::GetSourceBreakLocations(
jkummerow@chromium.org93a47f42013-07-02 14:43:41 +00001683 Handle<SharedFunctionInfo> shared,
1684 BreakPositionAlignment position_alignment) {
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +00001685 Isolate* isolate = shared->GetIsolate();
lrn@chromium.org7516f052011-03-30 08:52:27 +00001686 Heap* heap = isolate->heap();
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00001687 if (!HasDebugInfo(shared)) {
1688 return Handle<Object>(heap->undefined_value(), isolate);
1689 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001690 Handle<DebugInfo> debug_info = GetDebugInfo(shared);
1691 if (debug_info->GetBreakPointCount() == 0) {
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00001692 return Handle<Object>(heap->undefined_value(), isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001693 }
1694 Handle<FixedArray> locations =
lrn@chromium.org7516f052011-03-30 08:52:27 +00001695 isolate->factory()->NewFixedArray(debug_info->GetBreakPointCount());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001696 int count = 0;
1697 for (int i = 0; i < debug_info->break_points()->length(); i++) {
1698 if (!debug_info->break_points()->get(i)->IsUndefined()) {
1699 BreakPointInfo* break_point_info =
1700 BreakPointInfo::cast(debug_info->break_points()->get(i));
1701 if (break_point_info->GetBreakPointCount() > 0) {
jkummerow@chromium.org93a47f42013-07-02 14:43:41 +00001702 Smi* position;
1703 switch (position_alignment) {
1704 case STATEMENT_ALIGNED:
1705 position = break_point_info->statement_position();
1706 break;
1707 case BREAK_POSITION_ALIGNED:
1708 position = break_point_info->source_position();
1709 break;
1710 default:
1711 UNREACHABLE();
1712 position = break_point_info->statement_position();
1713 }
1714
1715 locations->set(count++, position);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001716 }
1717 }
1718 }
1719 return locations;
1720}
1721
1722
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001723void Debug::NewBreak(StackFrame::Id break_frame_id) {
1724 thread_local_.break_frame_id_ = break_frame_id;
1725 thread_local_.break_id_ = ++thread_local_.break_count_;
1726}
1727
1728
1729void Debug::SetBreak(StackFrame::Id break_frame_id, int break_id) {
1730 thread_local_.break_frame_id_ = break_frame_id;
1731 thread_local_.break_id_ = break_id;
1732}
1733
1734
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001735// Handle stepping into a function.
1736void Debug::HandleStepIn(Handle<JSFunction> function,
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001737 Handle<Object> holder,
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001738 Address fp,
1739 bool is_constructor) {
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00001740 Isolate* isolate = function->GetIsolate();
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001741 // If the frame pointer is not supplied by the caller find it.
1742 if (fp == 0) {
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00001743 StackFrameIterator it(isolate);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001744 it.Advance();
1745 // For constructor functions skip another frame.
1746 if (is_constructor) {
1747 ASSERT(it.frame()->is_construct());
1748 it.Advance();
1749 }
1750 fp = it.frame()->fp();
1751 }
1752
1753 // Flood the function with one-shot break points if it is called from where
1754 // step into was requested.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001755 if (fp == step_in_fp()) {
rossberg@chromium.org2c067b12012-03-19 11:01:52 +00001756 if (function->shared()->bound()) {
1757 // Handle Function.prototype.bind
1758 Debug::FloodBoundFunctionWithOneShot(function);
1759 } else if (!function->IsBuiltin()) {
1760 // Don't allow step into functions in the native context.
kasperl@chromium.orgacae3782009-04-11 09:17:08 +00001761 if (function->shared()->code() ==
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00001762 isolate->builtins()->builtin(Builtins::kFunctionApply) ||
kasperl@chromium.orgacae3782009-04-11 09:17:08 +00001763 function->shared()->code() ==
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00001764 isolate->builtins()->builtin(Builtins::kFunctionCall)) {
kasperl@chromium.orgacae3782009-04-11 09:17:08 +00001765 // Handle function.apply and function.call separately to flood the
1766 // function to be called and not the code for Builtins::FunctionApply or
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001767 // Builtins::FunctionCall. The receiver of call/apply is the target
1768 // function.
machenbach@chromium.org528ce022013-09-23 14:09:36 +00001769 if (!holder.is_null() && holder->IsJSFunction()) {
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001770 Handle<JSFunction> js_function = Handle<JSFunction>::cast(holder);
machenbach@chromium.org528ce022013-09-23 14:09:36 +00001771 if (!js_function->IsBuiltin()) {
1772 Debug::FloodWithOneShot(js_function);
1773 } else if (js_function->shared()->bound()) {
1774 // Handle Function.prototype.bind
1775 Debug::FloodBoundFunctionWithOneShot(js_function);
1776 }
kasperl@chromium.orgacae3782009-04-11 09:17:08 +00001777 }
1778 } else {
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001779 Debug::FloodWithOneShot(function);
kasperl@chromium.orgacae3782009-04-11 09:17:08 +00001780 }
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001781 }
1782 }
1783}
1784
1785
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001786void Debug::ClearStepping() {
1787 // Clear the various stepping setup.
1788 ClearOneShot();
1789 ClearStepIn();
ager@chromium.orga1645e22009-09-09 19:27:10 +00001790 ClearStepOut();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001791 ClearStepNext();
1792
1793 // Clear multiple step counter.
1794 thread_local_.step_count_ = 0;
1795}
1796
mstarzinger@chromium.orge0e1b0d2013-07-08 08:38:06 +00001797
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001798// Clears all the one-shot break points that are currently set. Normally this
1799// function is called each time a break point is hit as one shot break points
1800// are used to support stepping.
1801void Debug::ClearOneShot() {
1802 // The current implementation just runs through all the breakpoints. When the
v8.team.kasperl727e9952008-09-02 14:56:44 +00001803 // last break point for a function is removed that function is automatically
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001804 // removed from the list.
1805
1806 DebugInfoListNode* node = debug_info_list_;
1807 while (node != NULL) {
1808 BreakLocationIterator it(node->debug_info(), ALL_BREAK_LOCATIONS);
1809 while (!it.Done()) {
1810 it.ClearOneShot();
1811 it.Next();
1812 }
1813 node = node->next();
1814 }
1815}
1816
1817
1818void Debug::ActivateStepIn(StackFrame* frame) {
ager@chromium.orga1645e22009-09-09 19:27:10 +00001819 ASSERT(!StepOutActive());
mmassi@chromium.org7028c052012-06-13 11:51:58 +00001820 thread_local_.step_into_fp_ = frame->UnpaddedFP();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001821}
1822
1823
1824void Debug::ClearStepIn() {
1825 thread_local_.step_into_fp_ = 0;
1826}
1827
1828
ager@chromium.orga1645e22009-09-09 19:27:10 +00001829void Debug::ActivateStepOut(StackFrame* frame) {
1830 ASSERT(!StepInActive());
mmassi@chromium.org7028c052012-06-13 11:51:58 +00001831 thread_local_.step_out_fp_ = frame->UnpaddedFP();
ager@chromium.orga1645e22009-09-09 19:27:10 +00001832}
1833
1834
1835void Debug::ClearStepOut() {
1836 thread_local_.step_out_fp_ = 0;
1837}
1838
1839
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001840void Debug::ClearStepNext() {
1841 thread_local_.last_step_action_ = StepNone;
ager@chromium.org236ad962008-09-25 09:45:57 +00001842 thread_local_.last_statement_position_ = RelocInfo::kNoPosition;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001843 thread_local_.last_fp_ = 0;
1844}
1845
1846
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001847static void CollectActiveFunctionsFromThread(
1848 Isolate* isolate,
1849 ThreadLocalTop* top,
1850 List<Handle<JSFunction> >* active_functions,
1851 Object* active_code_marker) {
1852 // Find all non-optimized code functions with activation frames
1853 // on the stack. This includes functions which have optimized
1854 // activations (including inlined functions) on the stack as the
1855 // non-optimized code is needed for the lazy deoptimization.
1856 for (JavaScriptFrameIterator it(isolate, top); !it.done(); it.Advance()) {
1857 JavaScriptFrame* frame = it.frame();
1858 if (frame->is_optimized()) {
jkummerow@chromium.orgba72ec82013-07-22 09:21:20 +00001859 List<JSFunction*> functions(FLAG_max_inlining_levels + 1);
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001860 frame->GetFunctions(&functions);
1861 for (int i = 0; i < functions.length(); i++) {
1862 JSFunction* function = functions[i];
1863 active_functions->Add(Handle<JSFunction>(function));
1864 function->shared()->code()->set_gc_metadata(active_code_marker);
1865 }
1866 } else if (frame->function()->IsJSFunction()) {
danno@chromium.org169691d2013-07-15 08:01:13 +00001867 JSFunction* function = frame->function();
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001868 ASSERT(frame->LookupCode()->kind() == Code::FUNCTION);
1869 active_functions->Add(Handle<JSFunction>(function));
1870 function->shared()->code()->set_gc_metadata(active_code_marker);
1871 }
1872 }
1873}
1874
1875
1876static void RedirectActivationsToRecompiledCodeOnThread(
1877 Isolate* isolate,
1878 ThreadLocalTop* top) {
1879 for (JavaScriptFrameIterator it(isolate, top); !it.done(); it.Advance()) {
1880 JavaScriptFrame* frame = it.frame();
1881
1882 if (frame->is_optimized() || !frame->function()->IsJSFunction()) continue;
1883
danno@chromium.org169691d2013-07-15 08:01:13 +00001884 JSFunction* function = frame->function();
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001885
1886 ASSERT(frame->LookupCode()->kind() == Code::FUNCTION);
1887
1888 Handle<Code> frame_code(frame->LookupCode());
1889 if (frame_code->has_debug_break_slots()) continue;
1890
1891 Handle<Code> new_code(function->shared()->code());
1892 if (new_code->kind() != Code::FUNCTION ||
1893 !new_code->has_debug_break_slots()) {
1894 continue;
1895 }
1896
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001897 // Iterate over the RelocInfo in the original code to compute the sum of the
machenbach@chromium.org97b98c92014-03-13 03:05:00 +00001898 // constant pools and veneer pools sizes. (See Assembler::CheckConstPool()
1899 // and Assembler::CheckVeneerPool())
1900 // Note that this is only useful for architectures using constant pools or
1901 // veneer pools.
1902 int pool_mask = RelocInfo::ModeMask(RelocInfo::CONST_POOL) |
1903 RelocInfo::ModeMask(RelocInfo::VENEER_POOL);
1904 int frame_pool_size = 0;
1905 for (RelocIterator it(*frame_code, pool_mask); !it.done(); it.next()) {
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001906 RelocInfo* info = it.rinfo();
1907 if (info->pc() >= frame->pc()) break;
machenbach@chromium.org97b98c92014-03-13 03:05:00 +00001908 frame_pool_size += static_cast<int>(info->data());
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001909 }
1910 intptr_t frame_offset =
machenbach@chromium.org97b98c92014-03-13 03:05:00 +00001911 frame->pc() - frame_code->instruction_start() - frame_pool_size;
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001912
1913 // Iterate over the RelocInfo for new code to find the number of bytes
1914 // generated for debug slots and constant pools.
1915 int debug_break_slot_bytes = 0;
machenbach@chromium.org97b98c92014-03-13 03:05:00 +00001916 int new_code_pool_size = 0;
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001917 int mask = RelocInfo::ModeMask(RelocInfo::DEBUG_BREAK_SLOT) |
machenbach@chromium.org97b98c92014-03-13 03:05:00 +00001918 RelocInfo::ModeMask(RelocInfo::CONST_POOL) |
1919 RelocInfo::ModeMask(RelocInfo::VENEER_POOL);
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001920 for (RelocIterator it(*new_code, mask); !it.done(); it.next()) {
1921 // Check if the pc in the new code with debug break
1922 // slots is before this slot.
1923 RelocInfo* info = it.rinfo();
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001924 intptr_t new_offset = info->pc() - new_code->instruction_start() -
machenbach@chromium.org97b98c92014-03-13 03:05:00 +00001925 new_code_pool_size - debug_break_slot_bytes;
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001926 if (new_offset >= frame_offset) {
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001927 break;
1928 }
1929
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001930 if (RelocInfo::IsDebugBreakSlot(info->rmode())) {
1931 debug_break_slot_bytes += Assembler::kDebugBreakSlotLength;
1932 } else {
1933 ASSERT(RelocInfo::IsConstPool(info->rmode()));
machenbach@chromium.org97b98c92014-03-13 03:05:00 +00001934 // The size of the pools is encoded in the data.
1935 new_code_pool_size += static_cast<int>(info->data());
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001936 }
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001937 }
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001938
1939 // Compute the equivalent pc in the new code.
1940 byte* new_pc = new_code->instruction_start() + frame_offset +
machenbach@chromium.org97b98c92014-03-13 03:05:00 +00001941 debug_break_slot_bytes + new_code_pool_size;
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001942
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001943 if (FLAG_trace_deopt) {
1944 PrintF("Replacing code %08" V8PRIxPTR " - %08" V8PRIxPTR " (%d) "
1945 "with %08" V8PRIxPTR " - %08" V8PRIxPTR " (%d) "
1946 "for debugging, "
1947 "changing pc from %08" V8PRIxPTR " to %08" V8PRIxPTR "\n",
1948 reinterpret_cast<intptr_t>(
1949 frame_code->instruction_start()),
1950 reinterpret_cast<intptr_t>(
1951 frame_code->instruction_start()) +
1952 frame_code->instruction_size(),
1953 frame_code->instruction_size(),
1954 reinterpret_cast<intptr_t>(new_code->instruction_start()),
1955 reinterpret_cast<intptr_t>(new_code->instruction_start()) +
1956 new_code->instruction_size(),
1957 new_code->instruction_size(),
1958 reinterpret_cast<intptr_t>(frame->pc()),
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001959 reinterpret_cast<intptr_t>(new_pc));
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001960 }
1961
1962 // Patch the return address to return into the code with
1963 // debug break slots.
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001964 frame->set_pc(new_pc);
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001965 }
1966}
1967
1968
1969class ActiveFunctionsCollector : public ThreadVisitor {
1970 public:
1971 explicit ActiveFunctionsCollector(List<Handle<JSFunction> >* active_functions,
1972 Object* active_code_marker)
1973 : active_functions_(active_functions),
1974 active_code_marker_(active_code_marker) { }
1975
1976 void VisitThread(Isolate* isolate, ThreadLocalTop* top) {
1977 CollectActiveFunctionsFromThread(isolate,
1978 top,
1979 active_functions_,
1980 active_code_marker_);
1981 }
1982
1983 private:
1984 List<Handle<JSFunction> >* active_functions_;
1985 Object* active_code_marker_;
1986};
1987
1988
1989class ActiveFunctionsRedirector : public ThreadVisitor {
1990 public:
1991 void VisitThread(Isolate* isolate, ThreadLocalTop* top) {
1992 RedirectActivationsToRecompiledCodeOnThread(isolate, top);
1993 }
1994};
1995
1996
lrn@chromium.org34e60782011-09-15 07:25:40 +00001997void Debug::PrepareForBreakPoints() {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001998 // If preparing for the first break point make sure to deoptimize all
1999 // functions as debugging does not work with optimized code.
2000 if (!has_break_points_) {
machenbach@chromium.org9af454f2013-11-20 09:25:57 +00002001 if (isolate_->concurrent_recompilation_enabled()) {
danno@chromium.org59400602013-08-13 17:09:37 +00002002 isolate_->optimizing_compiler_thread()->Flush();
2003 }
2004
svenpanne@chromium.org876cca82013-03-18 14:43:20 +00002005 Deoptimizer::DeoptimizeAll(isolate_);
lrn@chromium.org34e60782011-09-15 07:25:40 +00002006
yangguo@chromium.org49546742013-12-23 16:17:49 +00002007 Handle<Code> lazy_compile = isolate_->builtins()->CompileUnoptimized();
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002008
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00002009 // There will be at least one break point when we are done.
2010 has_break_points_ = true;
2011
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002012 // Keep the list of activated functions in a handlified list as it
2013 // is used both in GC and non-GC code.
2014 List<Handle<JSFunction> > active_functions(100);
lrn@chromium.org34e60782011-09-15 07:25:40 +00002015
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002016 {
2017 // We are going to iterate heap to find all functions without
2018 // debug break slots.
hpayer@chromium.org7c3372b2013-02-13 17:26:04 +00002019 Heap* heap = isolate_->heap();
2020 heap->CollectAllGarbage(Heap::kMakeHeapIterableMask,
2021 "preparing for breakpoints");
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002022
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002023 // Ensure no GC in this scope as we are going to use gc_metadata
2024 // field in the Code object to mark active functions.
rossberg@chromium.org79e79022013-06-03 15:43:46 +00002025 DisallowHeapAllocation no_allocation;
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002026
hpayer@chromium.org7c3372b2013-02-13 17:26:04 +00002027 Object* active_code_marker = heap->the_hole_value();
svenpanne@chromium.orgecb9dd62011-12-01 08:22:35 +00002028
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002029 CollectActiveFunctionsFromThread(isolate_,
2030 isolate_->thread_local_top(),
2031 &active_functions,
2032 active_code_marker);
2033 ActiveFunctionsCollector active_functions_collector(&active_functions,
2034 active_code_marker);
2035 isolate_->thread_manager()->IterateArchivedThreads(
2036 &active_functions_collector);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002037
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002038 // Scan the heap for all non-optimized functions which have no
2039 // debug break slots and are not active or inlined into an active
2040 // function and mark them for lazy compilation.
hpayer@chromium.org7c3372b2013-02-13 17:26:04 +00002041 HeapIterator iterator(heap);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002042 HeapObject* obj = NULL;
2043 while (((obj = iterator.next()) != NULL)) {
2044 if (obj->IsJSFunction()) {
2045 JSFunction* function = JSFunction::cast(obj);
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002046 SharedFunctionInfo* shared = function->shared();
mstarzinger@chromium.org1510d582013-06-28 14:00:48 +00002047
2048 if (!shared->allows_lazy_compilation()) continue;
2049 if (!shared->script()->IsScript()) continue;
bmeurer@chromium.orgc9913f02013-10-24 06:31:36 +00002050 if (function->IsBuiltin()) continue;
mstarzinger@chromium.org1510d582013-06-28 14:00:48 +00002051 if (shared->code()->gc_metadata() == active_code_marker) continue;
2052
2053 Code::Kind kind = function->code()->kind();
2054 if (kind == Code::FUNCTION &&
2055 !function->code()->has_debug_break_slots()) {
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002056 function->set_code(*lazy_compile);
2057 function->shared()->set_code(*lazy_compile);
mstarzinger@chromium.org1510d582013-06-28 14:00:48 +00002058 } else if (kind == Code::BUILTIN &&
yangguo@chromium.org49546742013-12-23 16:17:49 +00002059 (function->IsInOptimizationQueue() ||
2060 function->IsMarkedForOptimization() ||
2061 function->IsMarkedForConcurrentOptimization())) {
mstarzinger@chromium.org1510d582013-06-28 14:00:48 +00002062 // Abort in-flight compilation.
2063 Code* shared_code = function->shared()->code();
2064 if (shared_code->kind() == Code::FUNCTION &&
2065 shared_code->has_debug_break_slots()) {
2066 function->set_code(shared_code);
2067 } else {
2068 function->set_code(*lazy_compile);
2069 function->shared()->set_code(*lazy_compile);
2070 }
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002071 }
2072 }
lrn@chromium.org34e60782011-09-15 07:25:40 +00002073 }
lrn@chromium.org34e60782011-09-15 07:25:40 +00002074
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002075 // Clear gc_metadata field.
2076 for (int i = 0; i < active_functions.length(); i++) {
2077 Handle<JSFunction> function = active_functions[i];
2078 function->shared()->code()->set_gc_metadata(Smi::FromInt(0));
2079 }
2080 }
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002081
2082 // Now recompile all functions with activation frames and and
2083 // patch the return address to run in the new compiled code.
2084 for (int i = 0; i < active_functions.length(); i++) {
2085 Handle<JSFunction> function = active_functions[i];
mmassi@chromium.org7028c052012-06-13 11:51:58 +00002086 Handle<SharedFunctionInfo> shared(function->shared());
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002087
2088 if (function->code()->kind() == Code::FUNCTION &&
2089 function->code()->has_debug_break_slots()) {
2090 // Nothing to do. Function code already had debug break slots.
2091 continue;
2092 }
2093
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002094 // If recompilation is not possible just skip it.
2095 if (shared->is_toplevel() ||
2096 !shared->allows_lazy_compilation() ||
2097 shared->code()->kind() == Code::BUILTIN) {
2098 continue;
2099 }
2100
2101 // Make sure that the shared full code is compiled with debug
2102 // break slots.
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00002103 if (!shared->code()->has_debug_break_slots()) {
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002104 // Try to compile the full code with debug break slots. If it
2105 // fails just keep the current code.
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002106 bool prev_force_debugger_active =
2107 isolate_->debugger()->force_debugger_active();
2108 isolate_->debugger()->set_force_debugger_active(true);
ulan@chromium.org19aca232013-12-30 09:24:30 +00002109 Handle<Code> code = Compiler::GetCodeForDebugging(function);
2110 function->ReplaceCode(*code);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002111 isolate_->debugger()->set_force_debugger_active(
2112 prev_force_debugger_active);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002113 }
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002114
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002115 // Keep function code in sync with shared function info.
2116 function->set_code(shared->code());
lrn@chromium.org34e60782011-09-15 07:25:40 +00002117 }
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002118
2119 RedirectActivationsToRecompiledCodeOnThread(isolate_,
2120 isolate_->thread_local_top());
2121
2122 ActiveFunctionsRedirector active_functions_redirector;
2123 isolate_->thread_manager()->IterateArchivedThreads(
2124 &active_functions_redirector);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002125 }
lrn@chromium.org34e60782011-09-15 07:25:40 +00002126}
2127
2128
jkummerow@chromium.org78502a92012-09-06 13:50:42 +00002129Object* Debug::FindSharedFunctionInfoInScript(Handle<Script> script,
2130 int position) {
2131 // Iterate the heap looking for SharedFunctionInfo generated from the
2132 // script. The inner most SharedFunctionInfo containing the source position
2133 // for the requested break point is found.
2134 // NOTE: This might require several heap iterations. If the SharedFunctionInfo
2135 // which is found is not compiled it is compiled and the heap is iterated
2136 // again as the compilation might create inner functions from the newly
2137 // compiled function and the actual requested break point might be in one of
2138 // these functions.
2139 // NOTE: The below fix-point iteration depends on all functions that cannot be
2140 // compiled lazily without a context to not be compiled at all. Compilation
2141 // will be triggered at points where we do not need a context.
2142 bool done = false;
2143 // The current candidate for the source position:
2144 int target_start_position = RelocInfo::kNoPosition;
2145 Handle<JSFunction> target_function;
2146 Handle<SharedFunctionInfo> target;
hpayer@chromium.org7c3372b2013-02-13 17:26:04 +00002147 Heap* heap = isolate_->heap();
jkummerow@chromium.org78502a92012-09-06 13:50:42 +00002148 while (!done) {
2149 { // Extra scope for iterator and no-allocation.
hpayer@chromium.org7c3372b2013-02-13 17:26:04 +00002150 heap->EnsureHeapIsIterable();
rossberg@chromium.org79e79022013-06-03 15:43:46 +00002151 DisallowHeapAllocation no_alloc_during_heap_iteration;
hpayer@chromium.org7c3372b2013-02-13 17:26:04 +00002152 HeapIterator iterator(heap);
jkummerow@chromium.org78502a92012-09-06 13:50:42 +00002153 for (HeapObject* obj = iterator.next();
2154 obj != NULL; obj = iterator.next()) {
2155 bool found_next_candidate = false;
2156 Handle<JSFunction> function;
2157 Handle<SharedFunctionInfo> shared;
2158 if (obj->IsJSFunction()) {
2159 function = Handle<JSFunction>(JSFunction::cast(obj));
2160 shared = Handle<SharedFunctionInfo>(function->shared());
2161 ASSERT(shared->allows_lazy_compilation() || shared->is_compiled());
2162 found_next_candidate = true;
2163 } else if (obj->IsSharedFunctionInfo()) {
2164 shared = Handle<SharedFunctionInfo>(SharedFunctionInfo::cast(obj));
2165 // Skip functions that we cannot compile lazily without a context,
2166 // which is not available here, because there is no closure.
2167 found_next_candidate = shared->is_compiled() ||
2168 shared->allows_lazy_compilation_without_context();
2169 }
2170 if (!found_next_candidate) continue;
2171 if (shared->script() == *script) {
2172 // If the SharedFunctionInfo found has the requested script data and
2173 // contains the source position it is a candidate.
2174 int start_position = shared->function_token_position();
2175 if (start_position == RelocInfo::kNoPosition) {
2176 start_position = shared->start_position();
2177 }
2178 if (start_position <= position &&
2179 position <= shared->end_position()) {
2180 // If there is no candidate or this function is within the current
2181 // candidate this is the new candidate.
2182 if (target.is_null()) {
2183 target_start_position = start_position;
2184 target_function = function;
2185 target = shared;
2186 } else {
2187 if (target_start_position == start_position &&
2188 shared->end_position() == target->end_position()) {
2189 // If a top-level function contains only one function
2190 // declaration the source for the top-level and the function
2191 // is the same. In that case prefer the non top-level function.
2192 if (!shared->is_toplevel()) {
2193 target_start_position = start_position;
2194 target_function = function;
2195 target = shared;
2196 }
2197 } else if (target_start_position <= start_position &&
2198 shared->end_position() <= target->end_position()) {
2199 // This containment check includes equality as a function
2200 // inside a top-level function can share either start or end
2201 // position with the top-level function.
2202 target_start_position = start_position;
2203 target_function = function;
2204 target = shared;
2205 }
2206 }
2207 }
2208 }
2209 } // End for loop.
2210 } // End no-allocation scope.
2211
hpayer@chromium.org7c3372b2013-02-13 17:26:04 +00002212 if (target.is_null()) return heap->undefined_value();
jkummerow@chromium.org78502a92012-09-06 13:50:42 +00002213
2214 // There will be at least one break point when we are done.
2215 has_break_points_ = true;
2216
2217 // If the candidate found is compiled we are done.
2218 done = target->is_compiled();
2219 if (!done) {
2220 // If the candidate is not compiled, compile it to reveal any inner
2221 // functions which might contain the requested source position. This
2222 // will compile all inner functions that cannot be compiled without a
2223 // context, because Compiler::BuildFunctionInfo checks whether the
2224 // debugger is active.
yangguo@chromium.org49546742013-12-23 16:17:49 +00002225 Handle<Code> result = target_function.is_null()
2226 ? Compiler::GetUnoptimizedCode(target)
2227 : Compiler::GetUnoptimizedCode(target_function);
2228 if (result.is_null()) return isolate_->heap()->undefined_value();
jkummerow@chromium.org78502a92012-09-06 13:50:42 +00002229 }
2230 } // End while loop.
2231
2232 return *target;
2233}
2234
2235
lrn@chromium.org34e60782011-09-15 07:25:40 +00002236// Ensures the debug information is present for shared.
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00002237bool Debug::EnsureDebugInfo(Handle<SharedFunctionInfo> shared,
2238 Handle<JSFunction> function) {
verwaest@chromium.orgd4be0f02013-06-05 13:39:03 +00002239 Isolate* isolate = shared->GetIsolate();
2240
lrn@chromium.org34e60782011-09-15 07:25:40 +00002241 // Return if we already have the debug info for shared.
2242 if (HasDebugInfo(shared)) {
2243 ASSERT(shared->is_compiled());
2244 return true;
2245 }
2246
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00002247 // There will be at least one break point when we are done.
2248 has_break_points_ = true;
2249
2250 // Ensure function is compiled. Return false if this failed.
2251 if (!function.is_null() &&
yangguo@chromium.org49546742013-12-23 16:17:49 +00002252 !Compiler::EnsureCompiled(function, CLEAR_EXCEPTION)) {
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002253 return false;
2254 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002255
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002256 // Create the debug info object.
verwaest@chromium.orgd4be0f02013-06-05 13:39:03 +00002257 Handle<DebugInfo> debug_info = isolate->factory()->NewDebugInfo(shared);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002258
2259 // Add debug info to the list.
2260 DebugInfoListNode* node = new DebugInfoListNode(*debug_info);
2261 node->set_next(debug_info_list_);
2262 debug_info_list_ = node;
2263
kasper.lundbd3ec4e2008-07-09 11:06:54 +00002264 return true;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002265}
2266
2267
2268void Debug::RemoveDebugInfo(Handle<DebugInfo> debug_info) {
2269 ASSERT(debug_info_list_ != NULL);
2270 // Run through the debug info objects to find this one and remove it.
2271 DebugInfoListNode* prev = NULL;
2272 DebugInfoListNode* current = debug_info_list_;
2273 while (current != NULL) {
2274 if (*current->debug_info() == *debug_info) {
2275 // Unlink from list. If prev is NULL we are looking at the first element.
2276 if (prev == NULL) {
2277 debug_info_list_ = current->next();
2278 } else {
2279 prev->set_next(current->next());
2280 }
lrn@chromium.org7516f052011-03-30 08:52:27 +00002281 current->debug_info()->shared()->set_debug_info(
2282 isolate_->heap()->undefined_value());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002283 delete current;
2284
2285 // If there are no more debug info objects there are not more break
2286 // points.
2287 has_break_points_ = debug_info_list_ != NULL;
2288
2289 return;
2290 }
2291 // Move to next in list.
2292 prev = current;
2293 current = current->next();
2294 }
2295 UNREACHABLE();
2296}
2297
2298
2299void Debug::SetAfterBreakTarget(JavaScriptFrame* frame) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002300 HandleScope scope(isolate_);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002301
lrn@chromium.org34e60782011-09-15 07:25:40 +00002302 PrepareForBreakPoints();
2303
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002304 // Get the executing function in which the debug break occurred.
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00002305 Handle<JSFunction> function(JSFunction::cast(frame->function()));
2306 Handle<SharedFunctionInfo> shared(function->shared());
2307 if (!EnsureDebugInfo(shared, function)) {
kasper.lundbd3ec4e2008-07-09 11:06:54 +00002308 // Return if we failed to retrieve the debug info.
2309 return;
2310 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002311 Handle<DebugInfo> debug_info = GetDebugInfo(shared);
2312 Handle<Code> code(debug_info->code());
2313 Handle<Code> original_code(debug_info->original_code());
2314#ifdef DEBUG
2315 // Get the code which is actually executing.
vegorov@chromium.org74f333b2011-04-06 11:17:46 +00002316 Handle<Code> frame_code(frame->LookupCode());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002317 ASSERT(frame_code.is_identical_to(code));
2318#endif
2319
2320 // Find the call address in the running code. This address holds the call to
2321 // either a DebugBreakXXX or to the debug break return entry code if the
2322 // break point is still active after processing the break point.
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00002323 Address addr = frame->pc() - Assembler::kPatchDebugBreakSlotReturnOffset;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002324
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002325 // Check if the location is at JS exit or debug break slot.
ager@chromium.orga1645e22009-09-09 19:27:10 +00002326 bool at_js_return = false;
2327 bool break_at_js_return_active = false;
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002328 bool at_debug_break_slot = false;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002329 RelocIterator it(debug_info->code());
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002330 while (!it.done() && !at_js_return && !at_debug_break_slot) {
ager@chromium.org236ad962008-09-25 09:45:57 +00002331 if (RelocInfo::IsJSReturn(it.rinfo()->rmode())) {
ager@chromium.orga1645e22009-09-09 19:27:10 +00002332 at_js_return = (it.rinfo()->pc() ==
2333 addr - Assembler::kPatchReturnSequenceAddressOffset);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002334 break_at_js_return_active = it.rinfo()->IsPatchedReturnSequence();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002335 }
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002336 if (RelocInfo::IsDebugBreakSlot(it.rinfo()->rmode())) {
2337 at_debug_break_slot = (it.rinfo()->pc() ==
2338 addr - Assembler::kPatchDebugBreakSlotAddressOffset);
2339 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002340 it.next();
2341 }
2342
2343 // Handle the jump to continue execution after break point depending on the
2344 // break location.
ager@chromium.orga1645e22009-09-09 19:27:10 +00002345 if (at_js_return) {
2346 // If the break point as return is still active jump to the corresponding
2347 // place in the original code. If not the break point was removed during
2348 // break point processing.
2349 if (break_at_js_return_active) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002350 addr += original_code->instruction_start() - code->instruction_start();
2351 }
2352
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00002353 // Move back to where the call instruction sequence started.
2354 thread_local_.after_break_target_ =
2355 addr - Assembler::kPatchReturnSequenceAddressOffset;
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002356 } else if (at_debug_break_slot) {
2357 // Address of where the debug break slot starts.
2358 addr = addr - Assembler::kPatchDebugBreakSlotAddressOffset;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002359
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002360 // Continue just after the slot.
2361 thread_local_.after_break_target_ = addr + Assembler::kDebugBreakSlotLength;
machenbach@chromium.org97b98c92014-03-13 03:05:00 +00002362 } else if (IsDebugBreak(Assembler::target_address_at(addr, *code))) {
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002363 // We now know that there is still a debug break call at the target address,
2364 // so the break point is still there and the original code will hold the
2365 // address to jump to in order to complete the call which is replaced by a
2366 // call to DebugBreakXXX.
2367
2368 // Find the corresponding address in the original code.
2369 addr += original_code->instruction_start() - code->instruction_start();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002370
2371 // Install jump to the call address in the original code. This will be the
2372 // call which was overwritten by the call to DebugBreakXXX.
machenbach@chromium.org97b98c92014-03-13 03:05:00 +00002373 thread_local_.after_break_target_ =
2374 Assembler::target_address_at(addr, *original_code);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002375 } else {
2376 // There is no longer a break point present. Don't try to look in the
2377 // original code as the running code will have the right address. This takes
2378 // care of the case where the last break point is removed from the function
2379 // and therefore no "original code" is available.
machenbach@chromium.org97b98c92014-03-13 03:05:00 +00002380 thread_local_.after_break_target_ =
2381 Assembler::target_address_at(addr, *code);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002382 }
2383}
2384
2385
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00002386bool Debug::IsBreakAtReturn(JavaScriptFrame* frame) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002387 HandleScope scope(isolate_);
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00002388
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00002389 // If there are no break points this cannot be break at return, as
2390 // the debugger statement and stack guard bebug break cannot be at
2391 // return.
2392 if (!has_break_points_) {
2393 return false;
2394 }
2395
lrn@chromium.org34e60782011-09-15 07:25:40 +00002396 PrepareForBreakPoints();
2397
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00002398 // Get the executing function in which the debug break occurred.
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00002399 Handle<JSFunction> function(JSFunction::cast(frame->function()));
2400 Handle<SharedFunctionInfo> shared(function->shared());
2401 if (!EnsureDebugInfo(shared, function)) {
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00002402 // Return if we failed to retrieve the debug info.
2403 return false;
2404 }
2405 Handle<DebugInfo> debug_info = GetDebugInfo(shared);
2406 Handle<Code> code(debug_info->code());
2407#ifdef DEBUG
2408 // Get the code which is actually executing.
vegorov@chromium.org74f333b2011-04-06 11:17:46 +00002409 Handle<Code> frame_code(frame->LookupCode());
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00002410 ASSERT(frame_code.is_identical_to(code));
2411#endif
2412
2413 // Find the call address in the running code.
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00002414 Address addr = frame->pc() - Assembler::kPatchDebugBreakSlotReturnOffset;
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00002415
2416 // Check if the location is at JS return.
2417 RelocIterator it(debug_info->code());
2418 while (!it.done()) {
2419 if (RelocInfo::IsJSReturn(it.rinfo()->rmode())) {
2420 return (it.rinfo()->pc() ==
2421 addr - Assembler::kPatchReturnSequenceAddressOffset);
2422 }
2423 it.next();
2424 }
2425 return false;
2426}
2427
2428
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00002429void Debug::FramesHaveBeenDropped(StackFrame::Id new_break_frame_id,
whesse@chromium.orge90029b2010-08-02 11:52:17 +00002430 FrameDropMode mode,
2431 Object** restarter_frame_function_pointer) {
ulan@chromium.orgd9e468a2012-06-25 09:47:40 +00002432 if (mode != CURRENTLY_SET_MODE) {
2433 thread_local_.frame_drop_mode_ = mode;
2434 }
ager@chromium.org357bf652010-04-12 11:30:10 +00002435 thread_local_.break_frame_id_ = new_break_frame_id;
whesse@chromium.orge90029b2010-08-02 11:52:17 +00002436 thread_local_.restarter_frame_function_pointer_ =
2437 restarter_frame_function_pointer;
ager@chromium.org357bf652010-04-12 11:30:10 +00002438}
2439
2440
jkummerow@chromium.org212d9642012-05-11 15:02:09 +00002441const int Debug::FramePaddingLayout::kInitialSize = 1;
2442
2443
2444// Any even value bigger than kInitialSize as needed for stack scanning.
2445const int Debug::FramePaddingLayout::kPaddingValue = kInitialSize + 1;
2446
2447
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002448bool Debug::IsDebugGlobal(GlobalObject* global) {
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00002449 return IsLoaded() && global == debug_context()->global_object();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002450}
2451
2452
ager@chromium.org32912102009-01-16 10:38:43 +00002453void Debug::ClearMirrorCache() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002454 PostponeInterruptsScope postpone(isolate_);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002455 HandleScope scope(isolate_);
2456 ASSERT(isolate_->context() == *Debug::debug_context());
ager@chromium.org32912102009-01-16 10:38:43 +00002457
2458 // Clear the mirror cache.
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00002459 Handle<String> function_name = isolate_->factory()->InternalizeOneByteString(
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00002460 STATIC_ASCII_VECTOR("ClearMirrorCache"));
machenbach@chromium.org2ebef182014-04-14 00:05:03 +00002461 Handle<Object> fun = Object::GetProperty(
2462 isolate_->global_object(), function_name).ToHandleChecked();
ager@chromium.org32912102009-01-16 10:38:43 +00002463 ASSERT(fun->IsJSFunction());
machenbach@chromium.org2ebef182014-04-14 00:05:03 +00002464 Execution::TryCall(
2465 Handle<JSFunction>::cast(fun),
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00002466 Handle<JSObject>(Debug::debug_context()->global_object()),
machenbach@chromium.org2ebef182014-04-14 00:05:03 +00002467 0,
2468 NULL);
ager@chromium.org32912102009-01-16 10:38:43 +00002469}
2470
2471
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002472void Debug::CreateScriptCache() {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002473 Heap* heap = isolate_->heap();
2474 HandleScope scope(isolate_);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002475
2476 // Perform two GCs to get rid of all unreferenced scripts. The first GC gets
2477 // rid of all the cached script wrappers and the second gets rid of the
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002478 // scripts which are no longer referenced. The second also sweeps precisely,
2479 // which saves us doing yet another GC to make the heap iterable.
rossberg@chromium.org994edf62012-02-06 10:12:55 +00002480 heap->CollectAllGarbage(Heap::kNoGCFlags, "Debug::CreateScriptCache");
2481 heap->CollectAllGarbage(Heap::kMakeHeapIterableMask,
2482 "Debug::CreateScriptCache");
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002483
2484 ASSERT(script_cache_ == NULL);
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +00002485 script_cache_ = new ScriptCache(isolate_);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002486
2487 // Scan heap for Script objects.
2488 int count = 0;
hpayer@chromium.org7c3372b2013-02-13 17:26:04 +00002489 HeapIterator iterator(heap);
rossberg@chromium.org79e79022013-06-03 15:43:46 +00002490 DisallowHeapAllocation no_allocation;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002491
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002492 for (HeapObject* obj = iterator.next(); obj != NULL; obj = iterator.next()) {
sgjesse@chromium.org152a0b02009-10-07 13:50:16 +00002493 if (obj->IsScript() && Script::cast(obj)->HasValidSource()) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002494 script_cache_->Add(Handle<Script>(Script::cast(obj)));
2495 count++;
2496 }
2497 }
2498}
2499
2500
2501void Debug::DestroyScriptCache() {
2502 // Get rid of the script cache if it was created.
2503 if (script_cache_ != NULL) {
2504 delete script_cache_;
2505 script_cache_ = NULL;
2506 }
2507}
2508
2509
2510void Debug::AddScriptToScriptCache(Handle<Script> script) {
2511 if (script_cache_ != NULL) {
2512 script_cache_->Add(script);
2513 }
2514}
2515
2516
2517Handle<FixedArray> Debug::GetLoadedScripts() {
2518 // Create and fill the script cache when the loaded scripts is requested for
2519 // the first time.
2520 if (script_cache_ == NULL) {
2521 CreateScriptCache();
2522 }
2523
2524 // If the script cache is not active just return an empty array.
2525 ASSERT(script_cache_ != NULL);
2526 if (script_cache_ == NULL) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002527 isolate_->factory()->NewFixedArray(0);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002528 }
2529
2530 // Perform GC to get unreferenced scripts evicted from the cache before
2531 // returning the content.
rossberg@chromium.org994edf62012-02-06 10:12:55 +00002532 isolate_->heap()->CollectAllGarbage(Heap::kNoGCFlags,
2533 "Debug::GetLoadedScripts");
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002534
2535 // Get the scripts from the cache.
2536 return script_cache_->GetScripts();
2537}
2538
2539
yangguo@chromium.org49546742013-12-23 16:17:49 +00002540void Debug::RecordEvalCaller(Handle<Script> script) {
2541 script->set_compilation_type(Script::COMPILATION_TYPE_EVAL);
2542 // For eval scripts add information on the function from which eval was
2543 // called.
2544 StackTraceFrameIterator it(script->GetIsolate());
2545 if (!it.done()) {
2546 script->set_eval_from_shared(it.frame()->function()->shared());
2547 Code* code = it.frame()->LookupCode();
2548 int offset = static_cast<int>(
2549 it.frame()->pc() - code->instruction_start());
2550 script->set_eval_from_instructions_offset(Smi::FromInt(offset));
2551 }
2552}
2553
2554
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002555void Debug::AfterGarbageCollection() {
2556 // Generate events for collected scripts.
2557 if (script_cache_ != NULL) {
2558 script_cache_->ProcessCollectedScripts();
2559 }
2560}
2561
2562
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00002563Debugger::Debugger(Isolate* isolate)
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +00002564 : debugger_access_(isolate->debugger_access()),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002565 event_listener_(Handle<Object>()),
2566 event_listener_data_(Handle<Object>()),
2567 compiling_natives_(false),
2568 is_loading_debugger_(false),
mstarzinger@chromium.orgde886792012-09-11 13:22:37 +00002569 live_edit_enabled_(true),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002570 never_unload_debugger_(false),
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002571 force_debugger_active_(false),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002572 message_handler_(NULL),
2573 debugger_unload_pending_(false),
2574 host_dispatch_handler_(NULL),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002575 debug_message_dispatch_handler_(NULL),
2576 message_dispatch_helper_thread_(NULL),
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +00002577 host_dispatch_period_(TimeDelta::FromMilliseconds(100)),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002578 agent_(NULL),
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00002579 command_queue_(isolate->logger(), kQueueInitialSize),
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +00002580 command_received_(0),
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00002581 event_command_queue_(isolate->logger(), kQueueInitialSize),
2582 isolate_(isolate) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002583}
2584
2585
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +00002586Debugger::~Debugger() {}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002587
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002588
machenbach@chromium.org2ebef182014-04-14 00:05:03 +00002589MaybeHandle<Object> Debugger::MakeJSObject(
2590 Vector<const char> constructor_name,
2591 int argc,
2592 Handle<Object> argv[]) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002593 ASSERT(isolate_->context() == *isolate_->debug()->debug_context());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002594
2595 // Create the execution state object.
lrn@chromium.org7516f052011-03-30 08:52:27 +00002596 Handle<String> constructor_str =
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00002597 isolate_->factory()->InternalizeUtf8String(constructor_name);
machenbach@chromium.orgb5ed9302014-03-25 13:44:35 +00002598 ASSERT(!constructor_str.is_null());
machenbach@chromium.org2ebef182014-04-14 00:05:03 +00002599 Handle<Object> constructor = Object::GetProperty(
2600 isolate_->global_object(), constructor_str).ToHandleChecked();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002601 ASSERT(constructor->IsJSFunction());
machenbach@chromium.org2ebef182014-04-14 00:05:03 +00002602 if (!constructor->IsJSFunction()) return MaybeHandle<Object>();
2603 return Execution::TryCall(
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002604 Handle<JSFunction>::cast(constructor),
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00002605 Handle<JSObject>(isolate_->debug()->debug_context()->global_object()),
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002606 argc,
machenbach@chromium.org2ebef182014-04-14 00:05:03 +00002607 argv);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002608}
2609
2610
machenbach@chromium.org2ebef182014-04-14 00:05:03 +00002611MaybeHandle<Object> Debugger::MakeExecutionState() {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002612 // Create the execution state object.
lrn@chromium.org7516f052011-03-30 08:52:27 +00002613 Handle<Object> break_id = isolate_->factory()->NewNumberFromInt(
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002614 isolate_->debug()->break_id());
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002615 Handle<Object> argv[] = { break_id };
machenbach@chromium.org2ebef182014-04-14 00:05:03 +00002616 return MakeJSObject(CStrVector("MakeExecutionState"), ARRAY_SIZE(argv), argv);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002617}
2618
2619
machenbach@chromium.org2ebef182014-04-14 00:05:03 +00002620MaybeHandle<Object> Debugger::MakeBreakEvent(Handle<Object> break_points_hit) {
2621 Handle<Object> exec_state;
machenbach@chromium.orge9fd6582014-04-16 00:05:02 +00002622 if (!MakeExecutionState().ToHandle(&exec_state)) return MaybeHandle<Object>();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002623 // Create the new break event object.
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002624 Handle<Object> argv[] = { exec_state, break_points_hit };
machenbach@chromium.org2ebef182014-04-14 00:05:03 +00002625 return MakeJSObject(CStrVector("MakeBreakEvent"), ARRAY_SIZE(argv), argv);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002626}
2627
2628
machenbach@chromium.org2ebef182014-04-14 00:05:03 +00002629MaybeHandle<Object> Debugger::MakeExceptionEvent(Handle<Object> exception,
2630 bool uncaught) {
2631 Handle<Object> exec_state;
machenbach@chromium.orge9fd6582014-04-16 00:05:02 +00002632 if (!MakeExecutionState().ToHandle(&exec_state)) return MaybeHandle<Object>();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002633 // Create the new exception event object.
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002634 Handle<Object> argv[] = { exec_state,
2635 exception,
machenbach@chromium.org2ebef182014-04-14 00:05:03 +00002636 isolate_->factory()->ToBoolean(uncaught) };
2637 return MakeJSObject(CStrVector("MakeExceptionEvent"), ARRAY_SIZE(argv), argv);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002638}
2639
2640
machenbach@chromium.org2ebef182014-04-14 00:05:03 +00002641MaybeHandle<Object> Debugger::MakeCompileEvent(Handle<Script> script,
2642 bool before) {
machenbach@chromium.org2ebef182014-04-14 00:05:03 +00002643 Handle<Object> exec_state;
machenbach@chromium.orge9fd6582014-04-16 00:05:02 +00002644 if (!MakeExecutionState().ToHandle(&exec_state)) return MaybeHandle<Object>();
2645 // Create the compile event object.
machenbach@chromium.org9fa61952014-04-17 00:05:12 +00002646 Handle<Object> script_wrapper = Script::GetWrapper(script);
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002647 Handle<Object> argv[] = { exec_state,
2648 script_wrapper,
machenbach@chromium.orge9fd6582014-04-16 00:05:02 +00002649 isolate_->factory()->ToBoolean(before) };
machenbach@chromium.org2ebef182014-04-14 00:05:03 +00002650 return MakeJSObject(CStrVector("MakeCompileEvent"), ARRAY_SIZE(argv), argv);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002651}
2652
2653
machenbach@chromium.org2ebef182014-04-14 00:05:03 +00002654MaybeHandle<Object> Debugger::MakeScriptCollectedEvent(int id) {
machenbach@chromium.org2ebef182014-04-14 00:05:03 +00002655 Handle<Object> exec_state;
machenbach@chromium.orge9fd6582014-04-16 00:05:02 +00002656 if (!MakeExecutionState().ToHandle(&exec_state)) return MaybeHandle<Object>();
2657 // Create the script collected event object.
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00002658 Handle<Object> id_object = Handle<Smi>(Smi::FromInt(id), isolate_);
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002659 Handle<Object> argv[] = { exec_state, id_object };
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002660
machenbach@chromium.org2ebef182014-04-14 00:05:03 +00002661 return MakeJSObject(
2662 CStrVector("MakeScriptCollectedEvent"), ARRAY_SIZE(argv), argv);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002663}
2664
2665
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002666void Debugger::OnException(Handle<Object> exception, bool uncaught) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002667 HandleScope scope(isolate_);
2668 Debug* debug = isolate_->debug();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002669
2670 // Bail out based on state or if there is no listener for this event
lrn@chromium.org7516f052011-03-30 08:52:27 +00002671 if (debug->InDebugger()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002672 if (!Debugger::EventActive(v8::Exception)) return;
2673
2674 // Bail out if exception breaks are not active
2675 if (uncaught) {
2676 // Uncaught exceptions are reported by either flags.
lrn@chromium.org7516f052011-03-30 08:52:27 +00002677 if (!(debug->break_on_uncaught_exception() ||
2678 debug->break_on_exception())) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002679 } else {
2680 // Caught exceptions are reported is activated.
lrn@chromium.org7516f052011-03-30 08:52:27 +00002681 if (!debug->break_on_exception()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002682 }
2683
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002684 // Enter the debugger.
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +00002685 EnterDebugger debugger(isolate_);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002686 if (debugger.FailedToEnter()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002687
2688 // Clear all current stepping setup.
lrn@chromium.org7516f052011-03-30 08:52:27 +00002689 debug->ClearStepping();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002690 // Create the event data object.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002691 Handle<Object> event_data;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002692 // Bail out and don't call debugger if exception.
machenbach@chromium.orge9fd6582014-04-16 00:05:02 +00002693 if (!MakeExceptionEvent(exception, uncaught).ToHandle(&event_data)) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002694
ager@chromium.org5ec48922009-05-05 07:25:34 +00002695 // Process debug event.
2696 ProcessDebugEvent(v8::Exception, Handle<JSObject>::cast(event_data), false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002697 // Return to continue execution from where the exception was thrown.
2698}
2699
2700
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002701void Debugger::OnDebugBreak(Handle<Object> break_points_hit,
2702 bool auto_continue) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002703 HandleScope scope(isolate_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002704
kasper.lund212ac232008-07-16 07:07:30 +00002705 // Debugger has already been entered by caller.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002706 ASSERT(isolate_->context() == *isolate_->debug()->debug_context());
kasper.lund212ac232008-07-16 07:07:30 +00002707
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002708 // Bail out if there is no listener for this event
2709 if (!Debugger::EventActive(v8::Break)) return;
2710
2711 // Debugger must be entered in advance.
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00002712 ASSERT(isolate_->context() == *isolate_->debug()->debug_context());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002713
2714 // Create the event data object.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002715 Handle<Object> event_data;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002716 // Bail out and don't call debugger if exception.
machenbach@chromium.orge9fd6582014-04-16 00:05:02 +00002717 if (!MakeBreakEvent(break_points_hit).ToHandle(&event_data)) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002718
ager@chromium.org5ec48922009-05-05 07:25:34 +00002719 // Process debug event.
2720 ProcessDebugEvent(v8::Break,
2721 Handle<JSObject>::cast(event_data),
2722 auto_continue);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002723}
2724
2725
2726void Debugger::OnBeforeCompile(Handle<Script> script) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002727 HandleScope scope(isolate_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002728
2729 // Bail out based on state or if there is no listener for this event
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002730 if (isolate_->debug()->InDebugger()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002731 if (compiling_natives()) return;
2732 if (!EventActive(v8::BeforeCompile)) return;
2733
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002734 // Enter the debugger.
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +00002735 EnterDebugger debugger(isolate_);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002736 if (debugger.FailedToEnter()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002737
2738 // Create the event data object.
machenbach@chromium.org2ebef182014-04-14 00:05:03 +00002739 Handle<Object> event_data;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002740 // Bail out and don't call debugger if exception.
machenbach@chromium.orge9fd6582014-04-16 00:05:02 +00002741 if (!MakeCompileEvent(script, true).ToHandle(&event_data)) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002742
ager@chromium.org5ec48922009-05-05 07:25:34 +00002743 // Process debug event.
2744 ProcessDebugEvent(v8::BeforeCompile,
2745 Handle<JSObject>::cast(event_data),
2746 true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002747}
2748
2749
2750// Handle debugger actions when a new script is compiled.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00002751void Debugger::OnAfterCompile(Handle<Script> script,
2752 AfterCompileFlags after_compile_flags) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002753 HandleScope scope(isolate_);
2754 Debug* debug = isolate_->debug();
kasper.lund212ac232008-07-16 07:07:30 +00002755
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002756 // Add the newly compiled script to the script cache.
lrn@chromium.org7516f052011-03-30 08:52:27 +00002757 debug->AddScriptToScriptCache(script);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002758
2759 // No more to do if not debugging.
ager@chromium.org71daaf62009-04-01 07:22:49 +00002760 if (!IsDebuggerActive()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002761
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002762 // No compile events while compiling natives.
2763 if (compiling_natives()) return;
2764
iposva@chromium.org245aa852009-02-10 00:49:54 +00002765 // Store whether in debugger before entering debugger.
lrn@chromium.org7516f052011-03-30 08:52:27 +00002766 bool in_debugger = debug->InDebugger();
iposva@chromium.org245aa852009-02-10 00:49:54 +00002767
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002768 // Enter the debugger.
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +00002769 EnterDebugger debugger(isolate_);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002770 if (debugger.FailedToEnter()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002771
2772 // If debugging there might be script break points registered for this
2773 // script. Make sure that these break points are set.
2774
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002775 // Get the function UpdateScriptBreakPoints (defined in debug-debugger.js).
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00002776 Handle<String> update_script_break_points_string =
2777 isolate_->factory()->InternalizeOneByteString(
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00002778 STATIC_ASCII_VECTOR("UpdateScriptBreakPoints"));
machenbach@chromium.org5b080562014-04-10 00:05:02 +00002779 Handle<GlobalObject> debug_global(debug->debug_context()->global_object());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002780 Handle<Object> update_script_break_points =
machenbach@chromium.org2ebef182014-04-14 00:05:03 +00002781 Object::GetProperty(
2782 debug_global, update_script_break_points_string).ToHandleChecked();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002783 if (!update_script_break_points->IsJSFunction()) {
2784 return;
2785 }
2786 ASSERT(update_script_break_points->IsJSFunction());
2787
2788 // Wrap the script object in a proper JS object before passing it
2789 // to JavaScript.
machenbach@chromium.org9fa61952014-04-17 00:05:12 +00002790 Handle<Object> wrapper = Script::GetWrapper(script);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002791
2792 // Call UpdateScriptBreakPoints expect no exceptions.
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002793 Handle<Object> argv[] = { wrapper };
machenbach@chromium.org2ebef182014-04-14 00:05:03 +00002794 if (Execution::TryCall(Handle<JSFunction>::cast(update_script_break_points),
2795 isolate_->js_builtins_object(),
2796 ARRAY_SIZE(argv),
2797 argv).is_null()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002798 return;
2799 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002800 // Bail out based on state or if there is no listener for this event
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00002801 if (in_debugger && (after_compile_flags & SEND_WHEN_DEBUGGING) == 0) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002802 if (!Debugger::EventActive(v8::AfterCompile)) return;
2803
2804 // Create the compile state object.
machenbach@chromium.org2ebef182014-04-14 00:05:03 +00002805 Handle<Object> event_data;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002806 // Bail out and don't call debugger if exception.
machenbach@chromium.orge9fd6582014-04-16 00:05:02 +00002807 if (!MakeCompileEvent(script, false).ToHandle(&event_data)) return;
machenbach@chromium.org2ebef182014-04-14 00:05:03 +00002808
ager@chromium.org5ec48922009-05-05 07:25:34 +00002809 // Process debug event.
machenbach@chromium.org2ebef182014-04-14 00:05:03 +00002810 ProcessDebugEvent(v8::AfterCompile, Handle<JSObject>::cast(event_data), true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002811}
2812
2813
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002814void Debugger::OnScriptCollected(int id) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002815 HandleScope scope(isolate_);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002816
2817 // No more to do if not debugging.
ulan@chromium.org56c14af2012-09-20 12:51:09 +00002818 if (isolate_->debug()->InDebugger()) return;
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002819 if (!IsDebuggerActive()) return;
2820 if (!Debugger::EventActive(v8::ScriptCollected)) return;
2821
2822 // Enter the debugger.
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +00002823 EnterDebugger debugger(isolate_);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002824 if (debugger.FailedToEnter()) return;
2825
2826 // Create the script collected state object.
machenbach@chromium.org2ebef182014-04-14 00:05:03 +00002827 Handle<Object> event_data;
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002828 // Bail out and don't call debugger if exception.
machenbach@chromium.orge9fd6582014-04-16 00:05:02 +00002829 if (!MakeScriptCollectedEvent(id).ToHandle(&event_data)) return;
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002830
2831 // Process debug event.
2832 ProcessDebugEvent(v8::ScriptCollected,
2833 Handle<JSObject>::cast(event_data),
2834 true);
2835}
2836
2837
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002838void Debugger::ProcessDebugEvent(v8::DebugEvent event,
ager@chromium.org5ec48922009-05-05 07:25:34 +00002839 Handle<JSObject> event_data,
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002840 bool auto_continue) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002841 HandleScope scope(isolate_);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002842
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +00002843 // Clear any pending debug break if this is a real break.
2844 if (!auto_continue) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002845 isolate_->debug()->clear_interrupt_pending(DEBUGBREAK);
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +00002846 }
2847
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002848 // Create the execution state.
machenbach@chromium.org2ebef182014-04-14 00:05:03 +00002849 Handle<Object> exec_state;
2850 // Bail out and don't call debugger if exception.
machenbach@chromium.orge9fd6582014-04-16 00:05:02 +00002851 if (!MakeExecutionState().ToHandle(&exec_state)) return;
2852
ager@chromium.org41826e72009-03-30 13:30:57 +00002853 // First notify the message handler if any.
2854 if (message_handler_ != NULL) {
ager@chromium.org5ec48922009-05-05 07:25:34 +00002855 NotifyMessageHandler(event,
2856 Handle<JSObject>::cast(exec_state),
2857 event_data,
2858 auto_continue);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002859 }
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00002860 // Notify registered debug event listener. This can be either a C or
2861 // a JavaScript function. Don't call event listener for v8::Break
2862 // here, if it's only a debug command -- they will be processed later.
2863 if ((event != v8::Break || !auto_continue) && !event_listener_.is_null()) {
2864 CallEventCallback(event, exec_state, event_data, NULL);
2865 }
2866 // Process pending debug commands.
2867 if (event == v8::Break) {
2868 while (!event_command_queue_.IsEmpty()) {
2869 CommandMessage command = event_command_queue_.Get();
2870 if (!event_listener_.is_null()) {
2871 CallEventCallback(v8::BreakForCommand,
2872 exec_state,
2873 event_data,
2874 command.client_data());
2875 }
2876 command.Dispose();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002877 }
2878 }
2879}
2880
2881
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00002882void Debugger::CallEventCallback(v8::DebugEvent event,
2883 Handle<Object> exec_state,
2884 Handle<Object> event_data,
2885 v8::Debug::ClientData* client_data) {
ager@chromium.orgea91cc52011-05-23 06:06:11 +00002886 if (event_listener_->IsForeign()) {
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00002887 CallCEventCallback(event, exec_state, event_data, client_data);
2888 } else {
2889 CallJSEventCallback(event, exec_state, event_data);
2890 }
2891}
2892
2893
2894void Debugger::CallCEventCallback(v8::DebugEvent event,
2895 Handle<Object> exec_state,
2896 Handle<Object> event_data,
2897 v8::Debug::ClientData* client_data) {
ager@chromium.orgea91cc52011-05-23 06:06:11 +00002898 Handle<Foreign> callback_obj(Handle<Foreign>::cast(event_listener_));
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00002899 v8::Debug::EventCallback2 callback =
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002900 FUNCTION_CAST<v8::Debug::EventCallback2>(
2901 callback_obj->foreign_address());
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00002902 EventDetailsImpl event_details(
2903 event,
2904 Handle<JSObject>::cast(exec_state),
2905 Handle<JSObject>::cast(event_data),
2906 event_listener_data_,
2907 client_data);
2908 callback(event_details);
2909}
2910
2911
2912void Debugger::CallJSEventCallback(v8::DebugEvent event,
2913 Handle<Object> exec_state,
2914 Handle<Object> event_data) {
2915 ASSERT(event_listener_->IsJSFunction());
2916 Handle<JSFunction> fun(Handle<JSFunction>::cast(event_listener_));
2917
2918 // Invoke the JavaScript debug event listener.
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00002919 Handle<Object> argv[] = { Handle<Object>(Smi::FromInt(event), isolate_),
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002920 exec_state,
2921 event_data,
2922 event_listener_data_ };
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002923 Execution::TryCall(fun,
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00002924 isolate_->global_object(),
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002925 ARRAY_SIZE(argv),
machenbach@chromium.org2ebef182014-04-14 00:05:03 +00002926 argv);
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00002927 // Silently ignore exceptions from debug event listeners.
2928}
2929
2930
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00002931Handle<Context> Debugger::GetDebugContext() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002932 never_unload_debugger_ = true;
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +00002933 EnterDebugger debugger(isolate_);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002934 return isolate_->debug()->debug_context();
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00002935}
2936
2937
ager@chromium.org71daaf62009-04-01 07:22:49 +00002938void Debugger::UnloadDebugger() {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002939 Debug* debug = isolate_->debug();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002940
ager@chromium.org71daaf62009-04-01 07:22:49 +00002941 // Make sure that there are no breakpoints left.
lrn@chromium.org7516f052011-03-30 08:52:27 +00002942 debug->ClearAllBreakPoints();
ager@chromium.org71daaf62009-04-01 07:22:49 +00002943
2944 // Unload the debugger if feasible.
2945 if (!never_unload_debugger_) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002946 debug->Unload();
ager@chromium.org71daaf62009-04-01 07:22:49 +00002947 }
2948
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002949 // Clear the flag indicating that the debugger should be unloaded.
2950 debugger_unload_pending_ = false;
ager@chromium.org71daaf62009-04-01 07:22:49 +00002951}
2952
2953
ager@chromium.org41826e72009-03-30 13:30:57 +00002954void Debugger::NotifyMessageHandler(v8::DebugEvent event,
ager@chromium.org5ec48922009-05-05 07:25:34 +00002955 Handle<JSObject> exec_state,
2956 Handle<JSObject> event_data,
ager@chromium.org41826e72009-03-30 13:30:57 +00002957 bool auto_continue) {
machenbach@chromium.orgf9841892013-11-25 12:01:13 +00002958 v8::Isolate* isolate = reinterpret_cast<v8::Isolate*>(isolate_);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002959 HandleScope scope(isolate_);
ager@chromium.org41826e72009-03-30 13:30:57 +00002960
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002961 if (!isolate_->debug()->Load()) return;
ager@chromium.org41826e72009-03-30 13:30:57 +00002962
2963 // Process the individual events.
ager@chromium.org5ec48922009-05-05 07:25:34 +00002964 bool sendEventMessage = false;
ager@chromium.org41826e72009-03-30 13:30:57 +00002965 switch (event) {
2966 case v8::Break:
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00002967 case v8::BreakForCommand:
ager@chromium.org5ec48922009-05-05 07:25:34 +00002968 sendEventMessage = !auto_continue;
ager@chromium.org41826e72009-03-30 13:30:57 +00002969 break;
2970 case v8::Exception:
ager@chromium.org5ec48922009-05-05 07:25:34 +00002971 sendEventMessage = true;
ager@chromium.org41826e72009-03-30 13:30:57 +00002972 break;
2973 case v8::BeforeCompile:
2974 break;
2975 case v8::AfterCompile:
ager@chromium.org5ec48922009-05-05 07:25:34 +00002976 sendEventMessage = true;
ager@chromium.org41826e72009-03-30 13:30:57 +00002977 break;
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002978 case v8::ScriptCollected:
2979 sendEventMessage = true;
2980 break;
ager@chromium.org41826e72009-03-30 13:30:57 +00002981 case v8::NewFunction:
2982 break;
2983 default:
2984 UNREACHABLE();
2985 }
2986
ager@chromium.org5ec48922009-05-05 07:25:34 +00002987 // The debug command interrupt flag might have been set when the command was
2988 // added. It should be enough to clear the flag only once while we are in the
2989 // debugger.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002990 ASSERT(isolate_->debug()->InDebugger());
2991 isolate_->stack_guard()->Continue(DEBUGCOMMAND);
ager@chromium.org5ec48922009-05-05 07:25:34 +00002992
2993 // Notify the debugger that a debug event has occurred unless auto continue is
2994 // active in which case no event is send.
2995 if (sendEventMessage) {
2996 MessageImpl message = MessageImpl::NewEvent(
2997 event,
2998 auto_continue,
2999 Handle<JSObject>::cast(exec_state),
3000 Handle<JSObject>::cast(event_data));
3001 InvokeMessageHandler(message);
3002 }
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +00003003
3004 // If auto continue don't make the event cause a break, but process messages
3005 // in the queue if any. For script collected events don't even process
3006 // messages in the queue as the execution state might not be what is expected
3007 // by the client.
ager@chromium.org6ffc2172009-05-29 19:20:16 +00003008 if ((auto_continue && !HasCommands()) || event == v8::ScriptCollected) {
ager@chromium.org5ec48922009-05-05 07:25:34 +00003009 return;
3010 }
ager@chromium.org41826e72009-03-30 13:30:57 +00003011
ager@chromium.org41826e72009-03-30 13:30:57 +00003012 v8::TryCatch try_catch;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003013
3014 // DebugCommandProcessor goes here.
3015 v8::Local<v8::Object> cmd_processor;
3016 {
3017 v8::Local<v8::Object> api_exec_state =
3018 v8::Utils::ToLocal(Handle<JSObject>::cast(exec_state));
machenbach@chromium.orgf9841892013-11-25 12:01:13 +00003019 v8::Local<v8::String> fun_name = v8::String::NewFromUtf8(
3020 isolate, "debugCommandProcessor");
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003021 v8::Local<v8::Function> fun =
danno@chromium.orgf95d4b92013-06-13 14:40:17 +00003022 v8::Local<v8::Function>::Cast(api_exec_state->Get(fun_name));
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003023
machenbach@chromium.org37be4082013-11-26 13:50:38 +00003024 v8::Handle<v8::Boolean> running = v8::Boolean::New(isolate, auto_continue);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003025 static const int kArgc = 1;
3026 v8::Handle<Value> argv[kArgc] = { running };
danno@chromium.orgf95d4b92013-06-13 14:40:17 +00003027 cmd_processor = v8::Local<v8::Object>::Cast(
3028 fun->Call(api_exec_state, kArgc, argv));
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003029 if (try_catch.HasCaught()) {
3030 PrintLn(try_catch.Exception());
3031 return;
3032 }
ager@chromium.org41826e72009-03-30 13:30:57 +00003033 }
3034
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003035 bool running = auto_continue;
3036
ager@chromium.org41826e72009-03-30 13:30:57 +00003037 // Process requests from the debugger.
3038 while (true) {
3039 // Wait for new command in the queue.
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003040 if (Debugger::host_dispatch_handler_) {
3041 // In case there is a host dispatch - do periodic dispatches.
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +00003042 if (!command_received_.WaitFor(host_dispatch_period_)) {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003043 // Timout expired, do the dispatch.
3044 Debugger::host_dispatch_handler_();
3045 continue;
3046 }
3047 } else {
3048 // In case there is no host dispatch - just wait.
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +00003049 command_received_.Wait();
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003050 }
ager@chromium.org41826e72009-03-30 13:30:57 +00003051
ager@chromium.org41826e72009-03-30 13:30:57 +00003052 // Get the command from the queue.
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003053 CommandMessage command = command_queue_.Get();
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00003054 isolate_->logger()->DebugTag(
3055 "Got request from command queue, in interactive loop.");
ager@chromium.org71daaf62009-04-01 07:22:49 +00003056 if (!Debugger::IsDebuggerActive()) {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003057 // Delete command text and user data.
3058 command.Dispose();
ager@chromium.org41826e72009-03-30 13:30:57 +00003059 return;
3060 }
3061
ager@chromium.org41826e72009-03-30 13:30:57 +00003062 // Invoke JavaScript to process the debug request.
3063 v8::Local<v8::String> fun_name;
3064 v8::Local<v8::Function> fun;
3065 v8::Local<v8::Value> request;
3066 v8::TryCatch try_catch;
machenbach@chromium.orgf9841892013-11-25 12:01:13 +00003067 fun_name = v8::String::NewFromUtf8(isolate, "processDebugRequest");
danno@chromium.orgf95d4b92013-06-13 14:40:17 +00003068 fun = v8::Local<v8::Function>::Cast(cmd_processor->Get(fun_name));
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003069
machenbach@chromium.orgf9841892013-11-25 12:01:13 +00003070 request = v8::String::NewFromTwoByte(isolate, command.text().start(),
3071 v8::String::kNormalString,
3072 command.text().length());
ager@chromium.org41826e72009-03-30 13:30:57 +00003073 static const int kArgc = 1;
3074 v8::Handle<Value> argv[kArgc] = { request };
3075 v8::Local<v8::Value> response_val = fun->Call(cmd_processor, kArgc, argv);
3076
3077 // Get the response.
3078 v8::Local<v8::String> response;
ager@chromium.org41826e72009-03-30 13:30:57 +00003079 if (!try_catch.HasCaught()) {
3080 // Get response string.
3081 if (!response_val->IsUndefined()) {
danno@chromium.orgf95d4b92013-06-13 14:40:17 +00003082 response = v8::Local<v8::String>::Cast(response_val);
ager@chromium.org41826e72009-03-30 13:30:57 +00003083 } else {
machenbach@chromium.orgf9841892013-11-25 12:01:13 +00003084 response = v8::String::NewFromUtf8(isolate, "");
ager@chromium.org41826e72009-03-30 13:30:57 +00003085 }
3086
3087 // Log the JSON request/response.
3088 if (FLAG_trace_debug_json) {
3089 PrintLn(request);
3090 PrintLn(response);
3091 }
3092
3093 // Get the running state.
machenbach@chromium.orgf9841892013-11-25 12:01:13 +00003094 fun_name = v8::String::NewFromUtf8(isolate, "isRunning");
danno@chromium.orgf95d4b92013-06-13 14:40:17 +00003095 fun = v8::Local<v8::Function>::Cast(cmd_processor->Get(fun_name));
ager@chromium.org41826e72009-03-30 13:30:57 +00003096 static const int kArgc = 1;
3097 v8::Handle<Value> argv[kArgc] = { response };
3098 v8::Local<v8::Value> running_val = fun->Call(cmd_processor, kArgc, argv);
3099 if (!try_catch.HasCaught()) {
3100 running = running_val->ToBoolean()->Value();
3101 }
3102 } else {
3103 // In case of failure the result text is the exception text.
3104 response = try_catch.Exception()->ToString();
3105 }
3106
ager@chromium.org41826e72009-03-30 13:30:57 +00003107 // Return the result.
ager@chromium.org5ec48922009-05-05 07:25:34 +00003108 MessageImpl message = MessageImpl::NewResponse(
3109 event,
3110 running,
3111 Handle<JSObject>::cast(exec_state),
3112 Handle<JSObject>::cast(event_data),
3113 Handle<String>(Utils::OpenHandle(*response)),
3114 command.client_data());
3115 InvokeMessageHandler(message);
3116 command.Dispose();
ager@chromium.org41826e72009-03-30 13:30:57 +00003117
3118 // Return from debug event processing if either the VM is put into the
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00003119 // running state (through a continue command) or auto continue is active
ager@chromium.org41826e72009-03-30 13:30:57 +00003120 // and there are no more commands queued.
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003121 if (running && !HasCommands()) {
ager@chromium.org41826e72009-03-30 13:30:57 +00003122 return;
3123 }
3124 }
3125}
3126
3127
iposva@chromium.org245aa852009-02-10 00:49:54 +00003128void Debugger::SetEventListener(Handle<Object> callback,
3129 Handle<Object> data) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00003130 HandleScope scope(isolate_);
3131 GlobalHandles* global_handles = isolate_->global_handles();
iposva@chromium.org245aa852009-02-10 00:49:54 +00003132
3133 // Clear the global handles for the event listener and the event listener data
3134 // object.
3135 if (!event_listener_.is_null()) {
hpayer@chromium.org4f99be92013-12-18 16:23:55 +00003136 GlobalHandles::Destroy(
iposva@chromium.org245aa852009-02-10 00:49:54 +00003137 reinterpret_cast<Object**>(event_listener_.location()));
3138 event_listener_ = Handle<Object>();
3139 }
3140 if (!event_listener_data_.is_null()) {
hpayer@chromium.org4f99be92013-12-18 16:23:55 +00003141 GlobalHandles::Destroy(
iposva@chromium.org245aa852009-02-10 00:49:54 +00003142 reinterpret_cast<Object**>(event_listener_data_.location()));
3143 event_listener_data_ = Handle<Object>();
3144 }
3145
3146 // If there is a new debug event listener register it together with its data
3147 // object.
3148 if (!callback->IsUndefined() && !callback->IsNull()) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003149 event_listener_ = Handle<Object>::cast(
lrn@chromium.org7516f052011-03-30 08:52:27 +00003150 global_handles->Create(*callback));
iposva@chromium.org245aa852009-02-10 00:49:54 +00003151 if (data.is_null()) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00003152 data = isolate_->factory()->undefined_value();
iposva@chromium.org245aa852009-02-10 00:49:54 +00003153 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003154 event_listener_data_ = Handle<Object>::cast(
lrn@chromium.org7516f052011-03-30 08:52:27 +00003155 global_handles->Create(*data));
iposva@chromium.org245aa852009-02-10 00:49:54 +00003156 }
3157
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003158 ListenersChanged();
iposva@chromium.org245aa852009-02-10 00:49:54 +00003159}
3160
3161
ager@chromium.org5ec48922009-05-05 07:25:34 +00003162void Debugger::SetMessageHandler(v8::Debug::MessageHandler2 handler) {
jkummerow@chromium.orgdc94e192013-08-30 11:35:42 +00003163 LockGuard<RecursiveMutex> with(debugger_access_);
ager@chromium.org71daaf62009-04-01 07:22:49 +00003164
ager@chromium.org381abbb2009-02-25 13:23:22 +00003165 message_handler_ = handler;
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003166 ListenersChanged();
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003167 if (handler == NULL) {
ager@chromium.org71daaf62009-04-01 07:22:49 +00003168 // Send an empty command to the debugger if in a break to make JavaScript
3169 // run again if the debugger is closed.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003170 if (isolate_->debug()->InDebugger()) {
ager@chromium.org71daaf62009-04-01 07:22:49 +00003171 ProcessCommand(Vector<const uint16_t>::empty());
3172 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003173 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003174}
3175
3176
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003177void Debugger::ListenersChanged() {
3178 if (IsDebuggerActive()) {
3179 // Disable the compilation cache when the debugger is active.
lrn@chromium.org7516f052011-03-30 08:52:27 +00003180 isolate_->compilation_cache()->Disable();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003181 debugger_unload_pending_ = false;
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003182 } else {
lrn@chromium.org7516f052011-03-30 08:52:27 +00003183 isolate_->compilation_cache()->Enable();
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003184 // Unload the debugger if event listener and message handler cleared.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003185 // Schedule this for later, because we may be in non-V8 thread.
3186 debugger_unload_pending_ = true;
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003187 }
3188}
3189
3190
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003191void Debugger::SetHostDispatchHandler(v8::Debug::HostDispatchHandler handler,
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +00003192 TimeDelta period) {
ager@chromium.org381abbb2009-02-25 13:23:22 +00003193 host_dispatch_handler_ = handler;
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +00003194 host_dispatch_period_ = period;
ager@chromium.org381abbb2009-02-25 13:23:22 +00003195}
3196
3197
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003198void Debugger::SetDebugMessageDispatchHandler(
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003199 v8::Debug::DebugMessageDispatchHandler handler, bool provide_locker) {
jkummerow@chromium.orgdc94e192013-08-30 11:35:42 +00003200 LockGuard<Mutex> lock_guard(&dispatch_handler_access_);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003201 debug_message_dispatch_handler_ = handler;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003202
3203 if (provide_locker && message_dispatch_helper_thread_ == NULL) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003204 message_dispatch_helper_thread_ = new MessageDispatchHelperThread(isolate_);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003205 message_dispatch_helper_thread_->Start();
3206 }
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003207}
3208
3209
ager@chromium.org41826e72009-03-30 13:30:57 +00003210// Calls the registered debug message handler. This callback is part of the
ager@chromium.org5ec48922009-05-05 07:25:34 +00003211// public API.
3212void Debugger::InvokeMessageHandler(MessageImpl message) {
jkummerow@chromium.orgdc94e192013-08-30 11:35:42 +00003213 LockGuard<RecursiveMutex> with(debugger_access_);
ager@chromium.org71daaf62009-04-01 07:22:49 +00003214
ager@chromium.org381abbb2009-02-25 13:23:22 +00003215 if (message_handler_ != NULL) {
ager@chromium.org5ec48922009-05-05 07:25:34 +00003216 message_handler_(message);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003217 }
ager@chromium.org41826e72009-03-30 13:30:57 +00003218}
3219
3220
3221// Puts a command coming from the public API on the queue. Creates
3222// a copy of the command string managed by the debugger. Up to this
3223// point, the command data was managed by the API client. Called
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003224// by the API client thread.
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003225void Debugger::ProcessCommand(Vector<const uint16_t> command,
3226 v8::Debug::ClientData* client_data) {
3227 // Need to cast away const.
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003228 CommandMessage message = CommandMessage::New(
ager@chromium.org41826e72009-03-30 13:30:57 +00003229 Vector<uint16_t>(const_cast<uint16_t*>(command.start()),
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003230 command.length()),
3231 client_data);
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00003232 isolate_->logger()->DebugTag("Put command on command_queue.");
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003233 command_queue_.Put(message);
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +00003234 command_received_.Signal();
sgjesse@chromium.org3afc1582009-04-16 22:31:44 +00003235
3236 // Set the debug command break flag to have the command processed.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003237 if (!isolate_->debug()->InDebugger()) {
3238 isolate_->stack_guard()->DebugCommand();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003239 }
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003240
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003241 MessageDispatchHelperThread* dispatch_thread;
3242 {
jkummerow@chromium.orgdc94e192013-08-30 11:35:42 +00003243 LockGuard<Mutex> lock_guard(&dispatch_handler_access_);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003244 dispatch_thread = message_dispatch_helper_thread_;
3245 }
3246
3247 if (dispatch_thread == NULL) {
3248 CallMessageDispatchHandler();
3249 } else {
3250 dispatch_thread->Schedule();
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003251 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003252}
3253
3254
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003255bool Debugger::HasCommands() {
ager@chromium.org41826e72009-03-30 13:30:57 +00003256 return !command_queue_.IsEmpty();
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003257}
3258
3259
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00003260void Debugger::EnqueueDebugCommand(v8::Debug::ClientData* client_data) {
3261 CommandMessage message = CommandMessage::New(Vector<uint16_t>(), client_data);
3262 event_command_queue_.Put(message);
3263
3264 // Set the debug command break flag to have the command processed.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003265 if (!isolate_->debug()->InDebugger()) {
3266 isolate_->stack_guard()->DebugCommand();
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00003267 }
3268}
3269
3270
ager@chromium.org71daaf62009-04-01 07:22:49 +00003271bool Debugger::IsDebuggerActive() {
jkummerow@chromium.orgdc94e192013-08-30 11:35:42 +00003272 LockGuard<RecursiveMutex> with(debugger_access_);
ager@chromium.org71daaf62009-04-01 07:22:49 +00003273
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00003274 return message_handler_ != NULL ||
3275 !event_listener_.is_null() ||
3276 force_debugger_active_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003277}
3278
3279
machenbach@chromium.org2ebef182014-04-14 00:05:03 +00003280MaybeHandle<Object> Debugger::Call(Handle<JSFunction> fun,
3281 Handle<Object> data) {
ager@chromium.org71daaf62009-04-01 07:22:49 +00003282 // When calling functions in the debugger prevent it from beeing unloaded.
3283 Debugger::never_unload_debugger_ = true;
3284
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003285 // Enter the debugger.
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +00003286 EnterDebugger debugger(isolate_);
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +00003287 if (debugger.FailedToEnter()) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00003288 return isolate_->factory()->undefined_value();
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003289 }
3290
3291 // Create the execution state.
machenbach@chromium.org2ebef182014-04-14 00:05:03 +00003292 Handle<Object> exec_state;
machenbach@chromium.orge9fd6582014-04-16 00:05:02 +00003293 if (!MakeExecutionState().ToHandle(&exec_state)) {
3294 return isolate_->factory()->undefined_value();
3295 }
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003296
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00003297 Handle<Object> argv[] = { exec_state, data };
machenbach@chromium.org2ebef182014-04-14 00:05:03 +00003298 return Execution::Call(
jkummerow@chromium.org2c9426b2013-09-05 16:31:13 +00003299 isolate_,
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +00003300 fun,
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00003301 Handle<Object>(isolate_->debug()->debug_context_->global_proxy(),
3302 isolate_),
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00003303 ARRAY_SIZE(argv),
machenbach@chromium.org2ebef182014-04-14 00:05:03 +00003304 argv);
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003305}
3306
3307
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003308static void StubMessageHandler2(const v8::Debug::Message& message) {
3309 // Simply ignore message.
3310}
3311
3312
3313bool Debugger::StartAgent(const char* name, int port,
3314 bool wait_for_connection) {
3315 if (wait_for_connection) {
3316 // Suspend V8 if it is already running or set V8 to suspend whenever
3317 // it starts.
3318 // Provide stub message handler; V8 auto-continues each suspend
3319 // when there is no message handler; we doesn't need it.
3320 // Once become suspended, V8 will stay so indefinitely long, until remote
3321 // debugger connects and issues "continue" command.
3322 Debugger::message_handler_ = StubMessageHandler2;
machenbach@chromium.orge9fd6582014-04-16 00:05:02 +00003323 v8::Debug::DebugBreak(reinterpret_cast<v8::Isolate*>(isolate_));
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003324 }
3325
jkummerow@chromium.org3d00d0a2013-09-04 13:57:32 +00003326 if (agent_ == NULL) {
3327 agent_ = new DebuggerAgent(isolate_, name, port);
3328 agent_->Start();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003329 }
jkummerow@chromium.org3d00d0a2013-09-04 13:57:32 +00003330 return true;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003331}
3332
3333
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003334void Debugger::StopAgent() {
3335 if (agent_ != NULL) {
3336 agent_->Shutdown();
3337 agent_->Join();
3338 delete agent_;
3339 agent_ = NULL;
3340 }
3341}
3342
3343
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003344void Debugger::WaitForAgent() {
3345 if (agent_ != NULL)
3346 agent_->WaitUntilListening();
3347}
3348
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003349
3350void Debugger::CallMessageDispatchHandler() {
3351 v8::Debug::DebugMessageDispatchHandler handler;
3352 {
jkummerow@chromium.orgdc94e192013-08-30 11:35:42 +00003353 LockGuard<Mutex> lock_guard(&dispatch_handler_access_);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003354 handler = Debugger::debug_message_dispatch_handler_;
3355 }
3356 if (handler != NULL) {
3357 handler();
3358 }
3359}
3360
3361
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +00003362EnterDebugger::EnterDebugger(Isolate* isolate)
3363 : isolate_(isolate),
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003364 prev_(isolate_->debug()->debugger_entry()),
3365 it_(isolate_),
3366 has_js_frames_(!it_.done()),
3367 save_(isolate_) {
3368 Debug* debug = isolate_->debug();
3369 ASSERT(prev_ != NULL || !debug->is_interrupt_pending(PREEMPT));
3370 ASSERT(prev_ != NULL || !debug->is_interrupt_pending(DEBUGBREAK));
3371
3372 // Link recursive debugger entry.
3373 debug->set_debugger_entry(this);
3374
3375 // Store the previous break id and frame id.
3376 break_id_ = debug->break_id();
3377 break_frame_id_ = debug->break_frame_id();
3378
3379 // Create the new break info. If there is no JavaScript frames there is no
3380 // break frame id.
3381 if (has_js_frames_) {
3382 debug->NewBreak(it_.frame()->id());
3383 } else {
3384 debug->NewBreak(StackFrame::NO_ID);
3385 }
3386
3387 // Make sure that debugger is loaded and enter the debugger context.
3388 load_failed_ = !debug->Load();
3389 if (!load_failed_) {
3390 // NOTE the member variable save which saves the previous context before
3391 // this change.
3392 isolate_->set_context(*debug->debug_context());
3393 }
3394}
3395
3396
3397EnterDebugger::~EnterDebugger() {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003398 Debug* debug = isolate_->debug();
3399
3400 // Restore to the previous break state.
3401 debug->SetBreak(break_frame_id_, break_id_);
3402
3403 // Check for leaving the debugger.
fschneider@chromium.org7d10be52012-04-10 12:30:14 +00003404 if (!load_failed_ && prev_ == NULL) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003405 // Clear mirror cache when leaving the debugger. Skip this if there is a
3406 // pending exception as clearing the mirror cache calls back into
3407 // JavaScript. This can happen if the v8::Debug::Call is used in which
3408 // case the exception should end up in the calling code.
3409 if (!isolate_->has_pending_exception()) {
3410 // Try to avoid any pending debug break breaking in the clear mirror
3411 // cache JavaScript code.
3412 if (isolate_->stack_guard()->IsDebugBreak()) {
3413 debug->set_interrupts_pending(DEBUGBREAK);
3414 isolate_->stack_guard()->Continue(DEBUGBREAK);
3415 }
3416 debug->ClearMirrorCache();
3417 }
3418
3419 // Request preemption and debug break when leaving the last debugger entry
3420 // if any of these where recorded while debugging.
3421 if (debug->is_interrupt_pending(PREEMPT)) {
3422 // This re-scheduling of preemption is to avoid starvation in some
3423 // debugging scenarios.
3424 debug->clear_interrupt_pending(PREEMPT);
3425 isolate_->stack_guard()->Preempt();
3426 }
3427 if (debug->is_interrupt_pending(DEBUGBREAK)) {
3428 debug->clear_interrupt_pending(DEBUGBREAK);
3429 isolate_->stack_guard()->DebugBreak();
3430 }
3431
3432 // If there are commands in the queue when leaving the debugger request
3433 // that these commands are processed.
3434 if (isolate_->debugger()->HasCommands()) {
3435 isolate_->stack_guard()->DebugCommand();
3436 }
3437
3438 // If leaving the debugger with the debugger no longer active unload it.
3439 if (!isolate_->debugger()->IsDebuggerActive()) {
3440 isolate_->debugger()->UnloadDebugger();
3441 }
3442 }
3443
3444 // Leaving this debugger entry.
3445 debug->set_debugger_entry(prev_);
3446}
3447
3448
ager@chromium.org5ec48922009-05-05 07:25:34 +00003449MessageImpl MessageImpl::NewEvent(DebugEvent event,
3450 bool running,
3451 Handle<JSObject> exec_state,
3452 Handle<JSObject> event_data) {
3453 MessageImpl message(true, event, running,
3454 exec_state, event_data, Handle<String>(), NULL);
3455 return message;
3456}
3457
3458
3459MessageImpl MessageImpl::NewResponse(DebugEvent event,
3460 bool running,
3461 Handle<JSObject> exec_state,
3462 Handle<JSObject> event_data,
3463 Handle<String> response_json,
3464 v8::Debug::ClientData* client_data) {
3465 MessageImpl message(false, event, running,
3466 exec_state, event_data, response_json, client_data);
3467 return message;
3468}
3469
3470
3471MessageImpl::MessageImpl(bool is_event,
3472 DebugEvent event,
3473 bool running,
3474 Handle<JSObject> exec_state,
3475 Handle<JSObject> event_data,
3476 Handle<String> response_json,
3477 v8::Debug::ClientData* client_data)
3478 : is_event_(is_event),
3479 event_(event),
3480 running_(running),
3481 exec_state_(exec_state),
3482 event_data_(event_data),
3483 response_json_(response_json),
3484 client_data_(client_data) {}
3485
3486
3487bool MessageImpl::IsEvent() const {
3488 return is_event_;
3489}
3490
3491
3492bool MessageImpl::IsResponse() const {
3493 return !is_event_;
3494}
3495
3496
3497DebugEvent MessageImpl::GetEvent() const {
3498 return event_;
3499}
3500
3501
3502bool MessageImpl::WillStartRunning() const {
3503 return running_;
3504}
3505
3506
3507v8::Handle<v8::Object> MessageImpl::GetExecutionState() const {
3508 return v8::Utils::ToLocal(exec_state_);
3509}
3510
3511
hpayer@chromium.orgc5d49712013-09-11 08:25:48 +00003512v8::Isolate* MessageImpl::GetIsolate() const {
3513 return reinterpret_cast<v8::Isolate*>(exec_state_->GetIsolate());
3514}
3515
3516
ager@chromium.org5ec48922009-05-05 07:25:34 +00003517v8::Handle<v8::Object> MessageImpl::GetEventData() const {
3518 return v8::Utils::ToLocal(event_data_);
3519}
3520
3521
3522v8::Handle<v8::String> MessageImpl::GetJSON() const {
machenbach@chromium.org2ebef182014-04-14 00:05:03 +00003523 Isolate* isolate = event_data_->GetIsolate();
3524 v8::EscapableHandleScope scope(reinterpret_cast<v8::Isolate*>(isolate));
ager@chromium.org5ec48922009-05-05 07:25:34 +00003525
3526 if (IsEvent()) {
3527 // Call toJSONProtocol on the debug event object.
machenbach@chromium.org9fa61952014-04-17 00:05:12 +00003528 Handle<Object> fun = Object::GetProperty(
3529 isolate, event_data_, "toJSONProtocol").ToHandleChecked();
ager@chromium.org5ec48922009-05-05 07:25:34 +00003530 if (!fun->IsJSFunction()) {
3531 return v8::Handle<v8::String>();
3532 }
machenbach@chromium.org2ebef182014-04-14 00:05:03 +00003533
3534 MaybeHandle<Object> maybe_json =
3535 Execution::TryCall(Handle<JSFunction>::cast(fun), event_data_, 0, NULL);
3536 Handle<Object> json;
3537 if (!maybe_json.ToHandle(&json) || !json->IsString()) {
ager@chromium.org5ec48922009-05-05 07:25:34 +00003538 return v8::Handle<v8::String>();
3539 }
machenbach@chromium.orgce9c5142013-12-03 08:00:39 +00003540 return scope.Escape(v8::Utils::ToLocal(Handle<String>::cast(json)));
ager@chromium.org5ec48922009-05-05 07:25:34 +00003541 } else {
3542 return v8::Utils::ToLocal(response_json_);
3543 }
3544}
3545
3546
3547v8::Handle<v8::Context> MessageImpl::GetEventContext() const {
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +00003548 Isolate* isolate = event_data_->GetIsolate();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003549 v8::Handle<v8::Context> context = GetDebugEventContext(isolate);
3550 // Isolate::context() may be NULL when "script collected" event occures.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003551 ASSERT(!context.IsEmpty() || event_ == v8::ScriptCollected);
rossberg@chromium.org717967f2011-07-20 13:44:42 +00003552 return context;
ager@chromium.org5ec48922009-05-05 07:25:34 +00003553}
3554
3555
3556v8::Debug::ClientData* MessageImpl::GetClientData() const {
3557 return client_data_;
3558}
3559
3560
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003561EventDetailsImpl::EventDetailsImpl(DebugEvent event,
3562 Handle<JSObject> exec_state,
3563 Handle<JSObject> event_data,
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00003564 Handle<Object> callback_data,
3565 v8::Debug::ClientData* client_data)
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003566 : event_(event),
3567 exec_state_(exec_state),
3568 event_data_(event_data),
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00003569 callback_data_(callback_data),
3570 client_data_(client_data) {}
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003571
3572
3573DebugEvent EventDetailsImpl::GetEvent() const {
3574 return event_;
3575}
3576
3577
3578v8::Handle<v8::Object> EventDetailsImpl::GetExecutionState() const {
3579 return v8::Utils::ToLocal(exec_state_);
3580}
3581
3582
3583v8::Handle<v8::Object> EventDetailsImpl::GetEventData() const {
3584 return v8::Utils::ToLocal(event_data_);
3585}
3586
3587
3588v8::Handle<v8::Context> EventDetailsImpl::GetEventContext() const {
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +00003589 return GetDebugEventContext(exec_state_->GetIsolate());
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003590}
3591
3592
3593v8::Handle<v8::Value> EventDetailsImpl::GetCallbackData() const {
3594 return v8::Utils::ToLocal(callback_data_);
3595}
3596
3597
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00003598v8::Debug::ClientData* EventDetailsImpl::GetClientData() const {
3599 return client_data_;
3600}
3601
3602
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003603CommandMessage::CommandMessage() : text_(Vector<uint16_t>::empty()),
3604 client_data_(NULL) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003605}
3606
3607
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003608CommandMessage::CommandMessage(const Vector<uint16_t>& text,
3609 v8::Debug::ClientData* data)
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003610 : text_(text),
3611 client_data_(data) {
3612}
3613
3614
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003615CommandMessage::~CommandMessage() {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003616}
3617
3618
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003619void CommandMessage::Dispose() {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003620 text_.Dispose();
3621 delete client_data_;
3622 client_data_ = NULL;
3623}
3624
3625
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003626CommandMessage CommandMessage::New(const Vector<uint16_t>& command,
3627 v8::Debug::ClientData* data) {
3628 return CommandMessage(command.Clone(), data);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003629}
3630
3631
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003632CommandMessageQueue::CommandMessageQueue(int size) : start_(0), end_(0),
3633 size_(size) {
3634 messages_ = NewArray<CommandMessage>(size);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003635}
3636
3637
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003638CommandMessageQueue::~CommandMessageQueue() {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003639 while (!IsEmpty()) {
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003640 CommandMessage m = Get();
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003641 m.Dispose();
3642 }
kasper.lund7276f142008-07-30 08:49:36 +00003643 DeleteArray(messages_);
3644}
3645
3646
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003647CommandMessage CommandMessageQueue::Get() {
kasper.lund7276f142008-07-30 08:49:36 +00003648 ASSERT(!IsEmpty());
3649 int result = start_;
3650 start_ = (start_ + 1) % size_;
3651 return messages_[result];
3652}
3653
3654
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003655void CommandMessageQueue::Put(const CommandMessage& message) {
kasper.lund7276f142008-07-30 08:49:36 +00003656 if ((end_ + 1) % size_ == start_) {
3657 Expand();
3658 }
3659 messages_[end_] = message;
3660 end_ = (end_ + 1) % size_;
3661}
3662
3663
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003664void CommandMessageQueue::Expand() {
3665 CommandMessageQueue new_queue(size_ * 2);
kasper.lund7276f142008-07-30 08:49:36 +00003666 while (!IsEmpty()) {
3667 new_queue.Put(Get());
3668 }
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003669 CommandMessage* array_to_free = messages_;
kasper.lund7276f142008-07-30 08:49:36 +00003670 *this = new_queue;
3671 new_queue.messages_ = array_to_free;
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003672 // Make the new_queue empty so that it doesn't call Dispose on any messages.
3673 new_queue.start_ = new_queue.end_;
kasper.lund7276f142008-07-30 08:49:36 +00003674 // Automatic destructor called on new_queue, freeing array_to_free.
3675}
3676
3677
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00003678LockingCommandMessageQueue::LockingCommandMessageQueue(Logger* logger, int size)
jkummerow@chromium.orgdc94e192013-08-30 11:35:42 +00003679 : logger_(logger), queue_(size) {}
kasper.lund7276f142008-07-30 08:49:36 +00003680
3681
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003682bool LockingCommandMessageQueue::IsEmpty() const {
jkummerow@chromium.orgdc94e192013-08-30 11:35:42 +00003683 LockGuard<Mutex> lock_guard(&mutex_);
kasper.lund7276f142008-07-30 08:49:36 +00003684 return queue_.IsEmpty();
3685}
3686
3687
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003688CommandMessage LockingCommandMessageQueue::Get() {
jkummerow@chromium.orgdc94e192013-08-30 11:35:42 +00003689 LockGuard<Mutex> lock_guard(&mutex_);
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003690 CommandMessage result = queue_.Get();
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00003691 logger_->DebugEvent("Get", result.text());
kasper.lund7276f142008-07-30 08:49:36 +00003692 return result;
3693}
3694
3695
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003696void LockingCommandMessageQueue::Put(const CommandMessage& message) {
jkummerow@chromium.orgdc94e192013-08-30 11:35:42 +00003697 LockGuard<Mutex> lock_guard(&mutex_);
kasper.lund7276f142008-07-30 08:49:36 +00003698 queue_.Put(message);
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00003699 logger_->DebugEvent("Put", message.text());
kasper.lund7276f142008-07-30 08:49:36 +00003700}
3701
3702
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003703void LockingCommandMessageQueue::Clear() {
jkummerow@chromium.orgdc94e192013-08-30 11:35:42 +00003704 LockGuard<Mutex> lock_guard(&mutex_);
kasper.lund7276f142008-07-30 08:49:36 +00003705 queue_.Clear();
3706}
3707
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003708
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003709MessageDispatchHelperThread::MessageDispatchHelperThread(Isolate* isolate)
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00003710 : Thread("v8:MsgDispHelpr"),
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +00003711 isolate_(isolate), sem_(0),
jkummerow@chromium.orgdc94e192013-08-30 11:35:42 +00003712 already_signalled_(false) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003713}
3714
3715
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003716void MessageDispatchHelperThread::Schedule() {
3717 {
jkummerow@chromium.orgdc94e192013-08-30 11:35:42 +00003718 LockGuard<Mutex> lock_guard(&mutex_);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003719 if (already_signalled_) {
3720 return;
3721 }
3722 already_signalled_ = true;
3723 }
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +00003724 sem_.Signal();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003725}
3726
3727
3728void MessageDispatchHelperThread::Run() {
3729 while (true) {
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +00003730 sem_.Wait();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003731 {
jkummerow@chromium.orgdc94e192013-08-30 11:35:42 +00003732 LockGuard<Mutex> lock_guard(&mutex_);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003733 already_signalled_ = false;
3734 }
3735 {
mstarzinger@chromium.orgf705b502013-04-04 11:38:09 +00003736 Locker locker(reinterpret_cast<v8::Isolate*>(isolate_));
3737 isolate_->debugger()->CallMessageDispatchHandler();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003738 }
3739 }
3740}
3741
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003742#endif // ENABLE_DEBUGGER_SUPPORT
kasper.lund7276f142008-07-30 08:49:36 +00003743
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003744} } // namespace v8::internal