blob: a0b9884410f289302d7d74be6e65bfb62ef1a413 [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
danno@chromium.org40cb8782011-05-25 07:58:50 +000089static Handle<Code> ComputeCallDebugPrepareStepIn(int argc, Code::Kind kind) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000090 Isolate* isolate = Isolate::Current();
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +000091 return isolate->stub_cache()->ComputeCallDebugPrepareStepIn(argc, kind);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000092}
93
94
95static v8::Handle<v8::Context> GetDebugEventContext(Isolate* isolate) {
96 Handle<Context> context = isolate->debug()->debugger_entry()->GetContext();
97 // Isolate::context() may have been NULL when "script collected" event
98 // occured.
99 if (context.is_null()) return v8::Local<v8::Context>();
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000100 Handle<Context> native_context(context->native_context());
101 return v8::Utils::ToLocal(native_context);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000102}
103
104
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000105BreakLocationIterator::BreakLocationIterator(Handle<DebugInfo> debug_info,
106 BreakLocatorType type) {
107 debug_info_ = debug_info;
108 type_ = type;
109 reloc_iterator_ = NULL;
110 reloc_iterator_original_ = NULL;
111 Reset(); // Initialize the rest of the member variables.
112}
113
114
115BreakLocationIterator::~BreakLocationIterator() {
116 ASSERT(reloc_iterator_ != NULL);
117 ASSERT(reloc_iterator_original_ != NULL);
118 delete reloc_iterator_;
119 delete reloc_iterator_original_;
120}
121
122
123void BreakLocationIterator::Next() {
rossberg@chromium.org79e79022013-06-03 15:43:46 +0000124 DisallowHeapAllocation no_gc;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000125 ASSERT(!RinfoDone());
126
127 // Iterate through reloc info for code and original code stopping at each
128 // breakable code target.
129 bool first = break_point_ == -1;
130 while (!RinfoDone()) {
131 if (!first) RinfoNext();
132 first = false;
133 if (RinfoDone()) return;
134
ager@chromium.org236ad962008-09-25 09:45:57 +0000135 // Whenever a statement position or (plain) position is passed update the
136 // current value of these.
137 if (RelocInfo::IsPosition(rmode())) {
138 if (RelocInfo::IsStatementPosition(rmode())) {
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000139 statement_position_ = static_cast<int>(
140 rinfo()->data() - debug_info_->shared()->start_position());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000141 }
ager@chromium.org236ad962008-09-25 09:45:57 +0000142 // Always update the position as we don't want that to be before the
143 // statement position.
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000144 position_ = static_cast<int>(
145 rinfo()->data() - debug_info_->shared()->start_position());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000146 ASSERT(position_ >= 0);
147 ASSERT(statement_position_ >= 0);
148 }
149
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000150 if (IsDebugBreakSlot()) {
151 // There is always a possible break point at a debug break slot.
152 break_point_++;
153 return;
154 } else if (RelocInfo::IsCodeTarget(rmode())) {
155 // Check for breakable code target. Look in the original code as setting
156 // break points can cause the code targets in the running (debugged) code
157 // to be of a different kind than in the original code.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000158 Address target = original_rinfo()->target_address();
ager@chromium.org8bb60582008-12-11 12:02:20 +0000159 Code* code = Code::GetCodeFromTargetAddress(target);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000160 if ((code->is_inline_cache_stub() &&
danno@chromium.org40cb8782011-05-25 07:58:50 +0000161 !code->is_binary_op_stub() &&
danno@chromium.orgabafe3d2013-08-09 20:02:11 +0000162 !code->is_unary_op_stub() &&
ricow@chromium.org9fa09672011-07-25 11:05:35 +0000163 !code->is_compare_ic_stub() &&
164 !code->is_to_boolean_ic_stub()) ||
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000165 RelocInfo::IsConstructCall(rmode())) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000166 break_point_++;
167 return;
168 }
169 if (code->kind() == Code::STUB) {
ager@chromium.orga1645e22009-09-09 19:27:10 +0000170 if (IsDebuggerStatement()) {
171 break_point_++;
172 return;
173 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000174 if (type_ == ALL_BREAK_LOCATIONS) {
175 if (Debug::IsBreakStub(code)) {
176 break_point_++;
177 return;
178 }
179 } else {
180 ASSERT(type_ == SOURCE_BREAK_LOCATIONS);
181 if (Debug::IsSourceBreakStub(code)) {
182 break_point_++;
183 return;
184 }
185 }
186 }
187 }
188
189 // Check for break at return.
ager@chromium.org236ad962008-09-25 09:45:57 +0000190 if (RelocInfo::IsJSReturn(rmode())) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000191 // Set the positions to the end of the function.
192 if (debug_info_->shared()->HasSourceCode()) {
193 position_ = debug_info_->shared()->end_position() -
whesse@chromium.orge90029b2010-08-02 11:52:17 +0000194 debug_info_->shared()->start_position() - 1;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000195 } else {
196 position_ = 0;
197 }
198 statement_position_ = position_;
199 break_point_++;
200 return;
201 }
202 }
203}
204
205
206void BreakLocationIterator::Next(int count) {
207 while (count > 0) {
208 Next();
209 count--;
210 }
211}
212
213
verwaest@chromium.org8a00e822013-06-10 15:11:22 +0000214// Find the break point at the supplied address, or the closest one before
215// the address.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000216void BreakLocationIterator::FindBreakLocationFromAddress(Address pc) {
217 // Run through all break points to locate the one closest to the address.
218 int closest_break_point = 0;
219 int distance = kMaxInt;
220 while (!Done()) {
221 // Check if this break point is closer that what was previously found.
verwaest@chromium.org8a00e822013-06-10 15:11:22 +0000222 if (this->pc() <= pc && pc - this->pc() < distance) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000223 closest_break_point = break_point();
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000224 distance = static_cast<int>(pc - this->pc());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000225 // Check whether we can't get any closer.
226 if (distance == 0) break;
227 }
228 Next();
229 }
230
231 // Move to the break point found.
232 Reset();
233 Next(closest_break_point);
234}
235
236
237// Find the break point closest to the supplied source position.
jkummerow@chromium.org93a47f42013-07-02 14:43:41 +0000238void BreakLocationIterator::FindBreakLocationFromPosition(int position,
239 BreakPositionAlignment alignment) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000240 // Run through all break points to locate the one closest to the source
241 // position.
242 int closest_break_point = 0;
243 int distance = kMaxInt;
jkummerow@chromium.org93a47f42013-07-02 14:43:41 +0000244
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000245 while (!Done()) {
jkummerow@chromium.org93a47f42013-07-02 14:43:41 +0000246 int next_position;
247 switch (alignment) {
248 case STATEMENT_ALIGNED:
249 next_position = this->statement_position();
250 break;
251 case BREAK_POSITION_ALIGNED:
252 next_position = this->position();
253 break;
254 default:
255 UNREACHABLE();
256 next_position = this->statement_position();
257 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000258 // Check if this break point is closer that what was previously found.
jkummerow@chromium.org93a47f42013-07-02 14:43:41 +0000259 if (position <= next_position && next_position - position < distance) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000260 closest_break_point = break_point();
jkummerow@chromium.org93a47f42013-07-02 14:43:41 +0000261 distance = next_position - position;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000262 // Check whether we can't get any closer.
263 if (distance == 0) break;
264 }
265 Next();
266 }
267
268 // Move to the break point found.
269 Reset();
270 Next(closest_break_point);
271}
272
273
274void BreakLocationIterator::Reset() {
275 // Create relocation iterators for the two code objects.
276 if (reloc_iterator_ != NULL) delete reloc_iterator_;
277 if (reloc_iterator_original_ != NULL) delete reloc_iterator_original_;
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +0000278 reloc_iterator_ = new RelocIterator(
279 debug_info_->code(),
280 ~RelocInfo::ModeMask(RelocInfo::CODE_AGE_SEQUENCE));
281 reloc_iterator_original_ = new RelocIterator(
282 debug_info_->original_code(),
283 ~RelocInfo::ModeMask(RelocInfo::CODE_AGE_SEQUENCE));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000284
285 // Position at the first break point.
286 break_point_ = -1;
287 position_ = 1;
288 statement_position_ = 1;
289 Next();
290}
291
292
293bool BreakLocationIterator::Done() const {
294 return RinfoDone();
295}
296
297
298void BreakLocationIterator::SetBreakPoint(Handle<Object> break_point_object) {
299 // If there is not already a real break point here patch code with debug
300 // break.
301 if (!HasBreakPoint()) {
302 SetDebugBreak();
303 }
ager@chromium.orga1645e22009-09-09 19:27:10 +0000304 ASSERT(IsDebugBreak() || IsDebuggerStatement());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000305 // Set the break point information.
306 DebugInfo::SetBreakPoint(debug_info_, code_position(),
307 position(), statement_position(),
308 break_point_object);
309}
310
311
312void BreakLocationIterator::ClearBreakPoint(Handle<Object> break_point_object) {
313 // Clear the break point information.
314 DebugInfo::ClearBreakPoint(debug_info_, code_position(), break_point_object);
315 // If there are no more break points here remove the debug break.
316 if (!HasBreakPoint()) {
317 ClearDebugBreak();
318 ASSERT(!IsDebugBreak());
319 }
320}
321
322
323void BreakLocationIterator::SetOneShot() {
ager@chromium.orga1645e22009-09-09 19:27:10 +0000324 // Debugger statement always calls debugger. No need to modify it.
325 if (IsDebuggerStatement()) {
326 return;
327 }
328
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000329 // If there is a real break point here no more to do.
330 if (HasBreakPoint()) {
331 ASSERT(IsDebugBreak());
332 return;
333 }
334
335 // Patch code with debug break.
336 SetDebugBreak();
337}
338
339
340void BreakLocationIterator::ClearOneShot() {
ager@chromium.orga1645e22009-09-09 19:27:10 +0000341 // Debugger statement always calls debugger. No need to modify it.
342 if (IsDebuggerStatement()) {
343 return;
344 }
345
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000346 // If there is a real break point here no more to do.
347 if (HasBreakPoint()) {
348 ASSERT(IsDebugBreak());
349 return;
350 }
351
352 // Patch code removing debug break.
353 ClearDebugBreak();
354 ASSERT(!IsDebugBreak());
355}
356
357
358void BreakLocationIterator::SetDebugBreak() {
ager@chromium.orga1645e22009-09-09 19:27:10 +0000359 // Debugger statement always calls debugger. No need to modify it.
360 if (IsDebuggerStatement()) {
361 return;
362 }
363
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000364 // If there is already a break point here just return. This might happen if
v8.team.kasperl727e9952008-09-02 14:56:44 +0000365 // the same code is flooded with break points twice. Flooding the same
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000366 // function twice might happen when stepping in a function with an exception
367 // handler as the handler and the function is the same.
368 if (IsDebugBreak()) {
369 return;
370 }
371
ager@chromium.org236ad962008-09-25 09:45:57 +0000372 if (RelocInfo::IsJSReturn(rmode())) {
iposva@chromium.org245aa852009-02-10 00:49:54 +0000373 // Patch the frame exit code with a break point.
374 SetDebugBreakAtReturn();
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000375 } else if (IsDebugBreakSlot()) {
376 // Patch the code in the break slot.
377 SetDebugBreakAtSlot();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000378 } else {
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000379 // Patch the IC call.
380 SetDebugBreakAtIC();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000381 }
382 ASSERT(IsDebugBreak());
383}
384
385
386void BreakLocationIterator::ClearDebugBreak() {
ager@chromium.orga1645e22009-09-09 19:27:10 +0000387 // Debugger statement always calls debugger. No need to modify it.
388 if (IsDebuggerStatement()) {
389 return;
390 }
391
ager@chromium.org236ad962008-09-25 09:45:57 +0000392 if (RelocInfo::IsJSReturn(rmode())) {
iposva@chromium.org245aa852009-02-10 00:49:54 +0000393 // Restore the frame exit code.
394 ClearDebugBreakAtReturn();
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000395 } else if (IsDebugBreakSlot()) {
396 // Restore the code in the break slot.
397 ClearDebugBreakAtSlot();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000398 } else {
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000399 // Patch the IC call.
400 ClearDebugBreakAtIC();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000401 }
402 ASSERT(!IsDebugBreak());
403}
404
405
mstarzinger@chromium.org1510d582013-06-28 14:00:48 +0000406bool BreakLocationIterator::IsStepInLocation(Isolate* isolate) {
407 if (RelocInfo::IsConstructCall(rmode())) {
408 return true;
409 } else if (RelocInfo::IsCodeTarget(rmode())) {
410 HandleScope scope(debug_info_->GetIsolate());
411 Address target = rinfo()->target_address();
412 Handle<Code> target_code(Code::GetCodeFromTargetAddress(target));
413 return target_code->is_call_stub() || target_code->is_keyed_call_stub();
414 } else {
415 return false;
416 }
417}
418
419
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +0000420void BreakLocationIterator::PrepareStepIn(Isolate* isolate) {
421 HandleScope scope(isolate);
ager@chromium.org381abbb2009-02-25 13:23:22 +0000422
ager@chromium.orga1645e22009-09-09 19:27:10 +0000423 // Step in can only be prepared if currently positioned on an IC call,
424 // construct call or CallFunction stub call.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000425 Address target = rinfo()->target_address();
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000426 Handle<Code> target_code(Code::GetCodeFromTargetAddress(target));
427 if (target_code->is_call_stub() || target_code->is_keyed_call_stub()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000428 // Step in through IC call is handled by the runtime system. Therefore make
429 // sure that the any current IC is cleared and the runtime system is
430 // called. If the executing code has a debug break at the location change
431 // the call in the original code as it is the code there that will be
432 // executed in place of the debug break call.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000433 Handle<Code> stub = ComputeCallDebugPrepareStepIn(
434 target_code->arguments_count(), target_code->kind());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000435 if (IsDebugBreak()) {
436 original_rinfo()->set_target_address(stub->entry());
437 } else {
438 rinfo()->set_target_address(stub->entry());
439 }
440 } else {
ager@chromium.orga1645e22009-09-09 19:27:10 +0000441#ifdef DEBUG
442 // All the following stuff is needed only for assertion checks so the code
443 // is wrapped in ifdef.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000444 Handle<Code> maybe_call_function_stub = target_code;
ager@chromium.orga1645e22009-09-09 19:27:10 +0000445 if (IsDebugBreak()) {
446 Address original_target = original_rinfo()->target_address();
447 maybe_call_function_stub =
448 Handle<Code>(Code::GetCodeFromTargetAddress(original_target));
449 }
450 bool is_call_function_stub =
451 (maybe_call_function_stub->kind() == Code::STUB &&
452 maybe_call_function_stub->major_key() == CodeStub::CallFunction);
453
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000454 // Step in through construct call requires no changes to the running code.
455 // Step in through getters/setters should already be prepared as well
456 // because caller of this function (Debug::PrepareStep) is expected to
457 // flood the top frame's function with one shot breakpoints.
ager@chromium.orga1645e22009-09-09 19:27:10 +0000458 // Step in through CallFunction stub should also be prepared by caller of
459 // this function (Debug::PrepareStep) which should flood target function
460 // with breakpoints.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000461 ASSERT(RelocInfo::IsConstructCall(rmode()) ||
462 target_code->is_inline_cache_stub() ||
463 is_call_function_stub);
ager@chromium.orga1645e22009-09-09 19:27:10 +0000464#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000465 }
466}
467
468
469// Check whether the break point is at a position which will exit the function.
470bool BreakLocationIterator::IsExit() const {
ager@chromium.org236ad962008-09-25 09:45:57 +0000471 return (RelocInfo::IsJSReturn(rmode()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000472}
473
474
475bool BreakLocationIterator::HasBreakPoint() {
476 return debug_info_->HasBreakPoint(code_position());
477}
478
479
480// Check whether there is a debug break at the current position.
481bool BreakLocationIterator::IsDebugBreak() {
ager@chromium.org236ad962008-09-25 09:45:57 +0000482 if (RelocInfo::IsJSReturn(rmode())) {
iposva@chromium.org245aa852009-02-10 00:49:54 +0000483 return IsDebugBreakAtReturn();
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000484 } else if (IsDebugBreakSlot()) {
485 return IsDebugBreakAtSlot();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000486 } else {
487 return Debug::IsDebugBreak(rinfo()->target_address());
488 }
489}
490
491
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000492void BreakLocationIterator::SetDebugBreakAtIC() {
493 // Patch the original code with the current address as the current address
494 // might have changed by the inline caching since the code was copied.
495 original_rinfo()->set_target_address(rinfo()->target_address());
496
497 RelocInfo::Mode mode = rmode();
498 if (RelocInfo::IsCodeTarget(mode)) {
499 Address target = rinfo()->target_address();
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000500 Handle<Code> target_code(Code::GetCodeFromTargetAddress(target));
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000501
502 // Patch the code to invoke the builtin debug break function matching the
503 // calling convention used by the call site.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000504 Handle<Code> dbgbrk_code(Debug::FindDebugBreak(target_code, mode));
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000505 rinfo()->set_target_address(dbgbrk_code->entry());
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000506 }
507}
508
509
510void BreakLocationIterator::ClearDebugBreakAtIC() {
511 // Patch the code to the original invoke.
512 rinfo()->set_target_address(original_rinfo()->target_address());
513}
514
515
ager@chromium.orga1645e22009-09-09 19:27:10 +0000516bool BreakLocationIterator::IsDebuggerStatement() {
ager@chromium.org5c838252010-02-19 08:53:10 +0000517 return RelocInfo::DEBUG_BREAK == rmode();
ager@chromium.orga1645e22009-09-09 19:27:10 +0000518}
519
520
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000521bool BreakLocationIterator::IsDebugBreakSlot() {
522 return RelocInfo::DEBUG_BREAK_SLOT == rmode();
523}
524
525
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000526Object* BreakLocationIterator::BreakPointObjects() {
527 return debug_info_->GetBreakPointObjects(code_position());
528}
529
530
ager@chromium.org381abbb2009-02-25 13:23:22 +0000531// Clear out all the debug break code. This is ONLY supposed to be used when
532// shutting down the debugger as it will leave the break point information in
533// DebugInfo even though the code is patched back to the non break point state.
534void BreakLocationIterator::ClearAllDebugBreak() {
535 while (!Done()) {
536 ClearDebugBreak();
537 Next();
538 }
539}
540
541
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000542bool BreakLocationIterator::RinfoDone() const {
543 ASSERT(reloc_iterator_->done() == reloc_iterator_original_->done());
544 return reloc_iterator_->done();
545}
546
547
548void BreakLocationIterator::RinfoNext() {
549 reloc_iterator_->next();
550 reloc_iterator_original_->next();
551#ifdef DEBUG
552 ASSERT(reloc_iterator_->done() == reloc_iterator_original_->done());
553 if (!reloc_iterator_->done()) {
554 ASSERT(rmode() == original_rmode());
555 }
556#endif
557}
558
559
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000560// Threading support.
561void Debug::ThreadInit() {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000562 thread_local_.break_count_ = 0;
563 thread_local_.break_id_ = 0;
564 thread_local_.break_frame_id_ = StackFrame::NO_ID;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000565 thread_local_.last_step_action_ = StepNone;
ager@chromium.org236ad962008-09-25 09:45:57 +0000566 thread_local_.last_statement_position_ = RelocInfo::kNoPosition;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000567 thread_local_.step_count_ = 0;
568 thread_local_.last_fp_ = 0;
lrn@chromium.org34e60782011-09-15 07:25:40 +0000569 thread_local_.queued_step_count_ = 0;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000570 thread_local_.step_into_fp_ = 0;
ager@chromium.orga1645e22009-09-09 19:27:10 +0000571 thread_local_.step_out_fp_ = 0;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000572 thread_local_.after_break_target_ = 0;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000573 // TODO(isolates): frames_are_dropped_?
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000574 thread_local_.debugger_entry_ = NULL;
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000575 thread_local_.pending_interrupts_ = 0;
ricow@chromium.org0b9f8502010-08-18 07:45:01 +0000576 thread_local_.restarter_frame_function_pointer_ = NULL;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000577}
578
579
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000580char* Debug::ArchiveDebug(char* storage) {
581 char* to = storage;
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000582 OS::MemCopy(to, reinterpret_cast<char*>(&thread_local_), sizeof(ThreadLocal));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000583 to += sizeof(ThreadLocal);
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000584 OS::MemCopy(to, reinterpret_cast<char*>(&registers_), sizeof(registers_));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000585 ThreadInit();
586 ASSERT(to <= storage + ArchiveSpacePerThread());
587 return storage + ArchiveSpacePerThread();
588}
589
590
591char* Debug::RestoreDebug(char* storage) {
592 char* from = storage;
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000593 OS::MemCopy(
594 reinterpret_cast<char*>(&thread_local_), from, sizeof(ThreadLocal));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000595 from += sizeof(ThreadLocal);
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000596 OS::MemCopy(reinterpret_cast<char*>(&registers_), from, sizeof(registers_));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000597 ASSERT(from <= storage + ArchiveSpacePerThread());
598 return storage + ArchiveSpacePerThread();
599}
600
601
602int Debug::ArchiveSpacePerThread() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000603 return sizeof(ThreadLocal) + sizeof(JSCallerSavedBuffer);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000604}
605
606
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000607// Frame structure (conforms InternalFrame structure):
608// -- code
609// -- SMI maker
610// -- function (slot is called "context")
611// -- frame base
612Object** Debug::SetUpFrameDropperFrame(StackFrame* bottom_js_frame,
613 Handle<Code> code) {
614 ASSERT(bottom_js_frame->is_java_script());
615
616 Address fp = bottom_js_frame->fp();
617
618 // Move function pointer into "context" slot.
619 Memory::Object_at(fp + StandardFrameConstants::kContextOffset) =
620 Memory::Object_at(fp + JavaScriptFrameConstants::kFunctionOffset);
621
622 Memory::Object_at(fp + InternalFrameConstants::kCodeOffset) = *code;
623 Memory::Object_at(fp + StandardFrameConstants::kMarkerOffset) =
624 Smi::FromInt(StackFrame::INTERNAL);
625
626 return reinterpret_cast<Object**>(&Memory::Object_at(
627 fp + StandardFrameConstants::kContextOffset));
628}
629
630const int Debug::kFrameDropperFrameSize = 4;
631
632
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000633void ScriptCache::Add(Handle<Script> script) {
lrn@chromium.org7516f052011-03-30 08:52:27 +0000634 GlobalHandles* global_handles = Isolate::Current()->global_handles();
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000635 // Create an entry in the hash map for the script.
mstarzinger@chromium.org1510d582013-06-28 14:00:48 +0000636 int id = script->id()->value();
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000637 HashMap::Entry* entry =
638 HashMap::Lookup(reinterpret_cast<void*>(id), Hash(id), true);
639 if (entry->value != NULL) {
640 ASSERT(*script == *reinterpret_cast<Script**>(entry->value));
641 return;
642 }
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000643 // Globalize the script object, make it weak and use the location of the
644 // global handle as the value in the hash map.
645 Handle<Script> script_ =
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000646 Handle<Script>::cast(
lrn@chromium.org7516f052011-03-30 08:52:27 +0000647 (global_handles->Create(*script)));
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000648 global_handles->MakeWeak(reinterpret_cast<Object**>(script_.location()),
649 this,
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000650 ScriptCache::HandleWeakScript);
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000651 entry->value = script_.location();
652}
653
654
655Handle<FixedArray> ScriptCache::GetScripts() {
verwaest@chromium.orgd4be0f02013-06-05 13:39:03 +0000656 Factory* factory = Isolate::Current()->factory();
657 Handle<FixedArray> instances = factory->NewFixedArray(occupancy());
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000658 int count = 0;
659 for (HashMap::Entry* entry = Start(); entry != NULL; entry = Next(entry)) {
660 ASSERT(entry->value != NULL);
661 if (entry->value != NULL) {
662 instances->set(count, *reinterpret_cast<Script**>(entry->value));
663 count++;
664 }
665 }
666 return instances;
667}
668
669
670void ScriptCache::ProcessCollectedScripts() {
lrn@chromium.org7516f052011-03-30 08:52:27 +0000671 Debugger* debugger = Isolate::Current()->debugger();
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000672 for (int i = 0; i < collected_scripts_.length(); i++) {
lrn@chromium.org7516f052011-03-30 08:52:27 +0000673 debugger->OnScriptCollected(collected_scripts_[i]);
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000674 }
675 collected_scripts_.Clear();
676}
677
678
679void ScriptCache::Clear() {
lrn@chromium.org7516f052011-03-30 08:52:27 +0000680 GlobalHandles* global_handles = Isolate::Current()->global_handles();
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000681 // Iterate the script cache to get rid of all the weak handles.
682 for (HashMap::Entry* entry = Start(); entry != NULL; entry = Next(entry)) {
683 ASSERT(entry != NULL);
684 Object** location = reinterpret_cast<Object**>(entry->value);
685 ASSERT((*location)->IsScript());
lrn@chromium.org7516f052011-03-30 08:52:27 +0000686 global_handles->ClearWeakness(location);
687 global_handles->Destroy(location);
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000688 }
689 // Clear the content of the hash map.
690 HashMap::Clear();
691}
692
693
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000694void ScriptCache::HandleWeakScript(v8::Isolate* isolate,
rossberg@chromium.org79e79022013-06-03 15:43:46 +0000695 v8::Persistent<v8::Value>* obj,
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000696 void* data) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000697 ScriptCache* script_cache = reinterpret_cast<ScriptCache*>(data);
698 // Find the location of the global handle.
699 Script** location =
danno@chromium.orgf95d4b92013-06-13 14:40:17 +0000700 reinterpret_cast<Script**>(Utils::OpenPersistent(*obj).location());
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000701 ASSERT((*location)->IsScript());
702
703 // Remove the entry from the cache.
mstarzinger@chromium.org1510d582013-06-28 14:00:48 +0000704 int id = (*location)->id()->value();
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000705 script_cache->Remove(reinterpret_cast<void*>(id), Hash(id));
706 script_cache->collected_scripts_.Add(id);
707
708 // Clear the weak handle.
rossberg@chromium.org79e79022013-06-03 15:43:46 +0000709 obj->Dispose(isolate);
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000710}
711
712
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000713void Debug::SetUp(bool create_heap_objects) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000714 ThreadInit();
715 if (create_heap_objects) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000716 // Get code to handle debug break on return.
717 debug_break_return_ =
lrn@chromium.org7516f052011-03-30 08:52:27 +0000718 isolate_->builtins()->builtin(Builtins::kReturn_DebugBreak);
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000719 ASSERT(debug_break_return_->IsCode());
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000720 // Get code to handle debug break in debug break slots.
721 debug_break_slot_ =
lrn@chromium.org7516f052011-03-30 08:52:27 +0000722 isolate_->builtins()->builtin(Builtins::kSlot_DebugBreak);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000723 ASSERT(debug_break_slot_->IsCode());
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000724 }
725}
726
727
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000728void Debug::HandleWeakDebugInfo(v8::Isolate* isolate,
rossberg@chromium.org79e79022013-06-03 15:43:46 +0000729 v8::Persistent<v8::Value>* obj,
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000730 void* data) {
731 Debug* debug = reinterpret_cast<Isolate*>(isolate)->debug();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000732 DebugInfoListNode* node = reinterpret_cast<DebugInfoListNode*>(data);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000733 // We need to clear all breakpoints associated with the function to restore
734 // original code and avoid patching the code twice later because
735 // the function will live in the heap until next gc, and can be found by
jkummerow@chromium.org78502a92012-09-06 13:50:42 +0000736 // Debug::FindSharedFunctionInfoInScript.
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000737 BreakLocationIterator it(node->debug_info(), ALL_BREAK_LOCATIONS);
738 it.ClearAllDebugBreak();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000739 debug->RemoveDebugInfo(node->debug_info());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000740#ifdef DEBUG
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000741 node = debug->debug_info_list_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000742 while (node != NULL) {
743 ASSERT(node != reinterpret_cast<DebugInfoListNode*>(data));
744 node = node->next();
745 }
746#endif
747}
748
749
750DebugInfoListNode::DebugInfoListNode(DebugInfo* debug_info): next_(NULL) {
lrn@chromium.org7516f052011-03-30 08:52:27 +0000751 GlobalHandles* global_handles = Isolate::Current()->global_handles();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000752 // Globalize the request debug info object and make it weak.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000753 debug_info_ = Handle<DebugInfo>::cast(
lrn@chromium.org7516f052011-03-30 08:52:27 +0000754 (global_handles->Create(debug_info)));
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000755 global_handles->MakeWeak(reinterpret_cast<Object**>(debug_info_.location()),
756 this,
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000757 Debug::HandleWeakDebugInfo);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000758}
759
760
761DebugInfoListNode::~DebugInfoListNode() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000762 Isolate::Current()->global_handles()->Destroy(
763 reinterpret_cast<Object**>(debug_info_.location()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000764}
765
766
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000767bool Debug::CompileDebuggerScript(int index) {
lrn@chromium.org7516f052011-03-30 08:52:27 +0000768 Isolate* isolate = Isolate::Current();
769 Factory* factory = isolate->factory();
770 HandleScope scope(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000771
kasper.lund44510672008-07-25 07:37:58 +0000772 // Bail out if the index is invalid.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000773 if (index == -1) {
774 return false;
775 }
kasper.lund44510672008-07-25 07:37:58 +0000776
777 // Find source and name for the requested script.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000778 Handle<String> source_code =
lrn@chromium.org7516f052011-03-30 08:52:27 +0000779 isolate->bootstrapper()->NativesSourceLookup(index);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000780 Vector<const char> name = Natives::GetScriptName(index);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000781 Handle<String> script_name = factory->NewStringFromAscii(name);
yangguo@chromium.org355cfd12012-08-29 15:32:24 +0000782 Handle<Context> context = isolate->native_context();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000783
784 // Compile the script.
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000785 Handle<SharedFunctionInfo> function_info;
786 function_info = Compiler::Compile(source_code,
787 script_name,
yangguo@chromium.org355cfd12012-08-29 15:32:24 +0000788 0, 0,
danno@chromium.orgd3c42102013-08-01 16:58:23 +0000789 false,
yangguo@chromium.org355cfd12012-08-29 15:32:24 +0000790 context,
791 NULL, NULL,
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000792 Handle<String>::null(),
793 NATIVES_CODE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000794
795 // Silently ignore stack overflows during compilation.
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000796 if (function_info.is_null()) {
lrn@chromium.org7516f052011-03-30 08:52:27 +0000797 ASSERT(isolate->has_pending_exception());
798 isolate->clear_pending_exception();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000799 return false;
800 }
801
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000802 // Execute the shared function in the debugger context.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000803 bool caught_exception;
kasper.lund44510672008-07-25 07:37:58 +0000804 Handle<JSFunction> function =
lrn@chromium.org7516f052011-03-30 08:52:27 +0000805 factory->NewFunctionFromSharedFunctionInfo(function_info, context);
rossberg@chromium.org717967f2011-07-20 13:44:42 +0000806
fschneider@chromium.org7d10be52012-04-10 12:30:14 +0000807 Handle<Object> exception =
ulan@chromium.org09d7ab52013-02-25 15:50:35 +0000808 Execution::TryCall(function,
809 Handle<Object>(context->global_object(), isolate),
810 0,
811 NULL,
812 &caught_exception);
kasper.lund44510672008-07-25 07:37:58 +0000813
814 // Check for caught exceptions.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000815 if (caught_exception) {
fschneider@chromium.org7d10be52012-04-10 12:30:14 +0000816 ASSERT(!isolate->has_pending_exception());
817 MessageLocation computed_location;
818 isolate->ComputeLocation(&computed_location);
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000819 Handle<Object> message = MessageHandler::MakeMessageObject(
verwaest@chromium.orgd4be0f02013-06-05 13:39:03 +0000820 isolate, "error_loading_debugger", &computed_location,
fschneider@chromium.org7d10be52012-04-10 12:30:14 +0000821 Vector<Handle<Object> >::empty(), Handle<String>(), Handle<JSArray>());
822 ASSERT(!isolate->has_pending_exception());
mmassi@chromium.org49a44672012-12-04 13:52:03 +0000823 if (!exception.is_null()) {
824 isolate->set_pending_exception(*exception);
825 MessageHandler::ReportMessage(Isolate::Current(), NULL, message);
826 isolate->clear_pending_exception();
827 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000828 return false;
829 }
830
kasper.lund44510672008-07-25 07:37:58 +0000831 // Mark this script as native and return successfully.
832 Handle<Script> script(Script::cast(function->shared()->script()));
ager@chromium.orge2902be2009-06-08 12:21:35 +0000833 script->set_type(Smi::FromInt(Script::TYPE_NATIVE));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000834 return true;
835}
836
837
838bool Debug::Load() {
839 // Return if debugger is already loaded.
kasper.lund212ac232008-07-16 07:07:30 +0000840 if (IsLoaded()) return true;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000841
lrn@chromium.org7516f052011-03-30 08:52:27 +0000842 Debugger* debugger = isolate_->debugger();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000843
kasper.lund44510672008-07-25 07:37:58 +0000844 // Bail out if we're already in the process of compiling the native
845 // JavaScript source code for the debugger.
lrn@chromium.org7516f052011-03-30 08:52:27 +0000846 if (debugger->compiling_natives() ||
847 debugger->is_loading_debugger())
mads.s.agercbaa0602008-08-14 13:41:48 +0000848 return false;
lrn@chromium.org7516f052011-03-30 08:52:27 +0000849 debugger->set_loading_debugger(true);
kasper.lund44510672008-07-25 07:37:58 +0000850
851 // Disable breakpoints and interrupts while compiling and running the
852 // debugger scripts including the context creation code.
853 DisableBreak disable(true);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000854 PostponeInterruptsScope postpone(isolate_);
kasper.lund44510672008-07-25 07:37:58 +0000855
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000856 // Create the debugger context.
lrn@chromium.org7516f052011-03-30 08:52:27 +0000857 HandleScope scope(isolate_);
kasper.lund44510672008-07-25 07:37:58 +0000858 Handle<Context> context =
lrn@chromium.org7516f052011-03-30 08:52:27 +0000859 isolate_->bootstrapper()->CreateEnvironment(
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000860 Handle<Object>::null(),
861 v8::Handle<ObjectTemplate>(),
862 NULL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000863
fschneider@chromium.org7d10be52012-04-10 12:30:14 +0000864 // Fail if no context could be created.
865 if (context.is_null()) return false;
866
kasper.lund44510672008-07-25 07:37:58 +0000867 // Use the debugger context.
lrn@chromium.org7516f052011-03-30 08:52:27 +0000868 SaveContext save(isolate_);
869 isolate_->set_context(*context);
kasper.lund44510672008-07-25 07:37:58 +0000870
871 // Expose the builtins object in the debugger context.
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000872 Handle<String> key = isolate_->factory()->InternalizeOneByteString(
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +0000873 STATIC_ASCII_VECTOR("builtins"));
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000874 Handle<GlobalObject> global = Handle<GlobalObject>(context->global_object());
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +0000875 RETURN_IF_EMPTY_HANDLE_VALUE(
lrn@chromium.org7516f052011-03-30 08:52:27 +0000876 isolate_,
ulan@chromium.org09d7ab52013-02-25 15:50:35 +0000877 JSReceiver::SetProperty(global,
878 key,
879 Handle<Object>(global->builtins(), isolate_),
880 NONE,
881 kNonStrictMode),
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +0000882 false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000883
884 // Compile the JavaScript for the debugger in the debugger context.
lrn@chromium.org7516f052011-03-30 08:52:27 +0000885 debugger->set_compiling_natives(true);
kasper.lund44510672008-07-25 07:37:58 +0000886 bool caught_exception =
887 !CompileDebuggerScript(Natives::GetIndex("mirror")) ||
888 !CompileDebuggerScript(Natives::GetIndex("debug"));
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000889
890 if (FLAG_enable_liveedit) {
891 caught_exception = caught_exception ||
892 !CompileDebuggerScript(Natives::GetIndex("liveedit"));
893 }
894
lrn@chromium.org7516f052011-03-30 08:52:27 +0000895 debugger->set_compiling_natives(false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000896
mads.s.agercbaa0602008-08-14 13:41:48 +0000897 // Make sure we mark the debugger as not loading before we might
898 // return.
lrn@chromium.org7516f052011-03-30 08:52:27 +0000899 debugger->set_loading_debugger(false);
mads.s.agercbaa0602008-08-14 13:41:48 +0000900
kasper.lund44510672008-07-25 07:37:58 +0000901 // Check for caught exceptions.
902 if (caught_exception) return false;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000903
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000904 // Debugger loaded, create debugger context global handle.
905 debug_context_ = Handle<Context>::cast(
906 isolate_->global_handles()->Create(*context));
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000907
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000908 return true;
909}
910
911
912void Debug::Unload() {
913 // Return debugger is not loaded.
914 if (!IsLoaded()) {
915 return;
916 }
917
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000918 // Clear the script cache.
919 DestroyScriptCache();
920
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000921 // Clear debugger context global handle.
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000922 isolate_->global_handles()->Destroy(
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000923 reinterpret_cast<Object**>(debug_context_.location()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000924 debug_context_ = Handle<Context>();
925}
926
927
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000928// Set the flag indicating that preemption happened during debugging.
929void Debug::PreemptionWhileInDebugger() {
930 ASSERT(InDebugger());
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000931 Debug::set_interrupts_pending(PREEMPT);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000932}
933
934
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000935void Debug::Iterate(ObjectVisitor* v) {
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000936 v->VisitPointer(BitCast<Object**>(&(debug_break_return_)));
937 v->VisitPointer(BitCast<Object**>(&(debug_break_slot_)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000938}
939
940
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000941Object* Debug::Break(Arguments args) {
942 Heap* heap = isolate_->heap();
943 HandleScope scope(isolate_);
mads.s.ager31e71382008-08-13 09:32:07 +0000944 ASSERT(args.length() == 0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000945
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000946 thread_local_.frame_drop_mode_ = FRAMES_UNTOUCHED;
ager@chromium.org357bf652010-04-12 11:30:10 +0000947
kasper.lundbd3ec4e2008-07-09 11:06:54 +0000948 // Get the top-most JavaScript frame.
vegorov@chromium.org74f333b2011-04-06 11:17:46 +0000949 JavaScriptFrameIterator it(isolate_);
kasper.lundbd3ec4e2008-07-09 11:06:54 +0000950 JavaScriptFrame* frame = it.frame();
951
952 // Just continue if breaks are disabled or debugger cannot be loaded.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000953 if (disable_break() || !Load()) {
954 SetAfterBreakTarget(frame);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000955 return heap->undefined_value();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000956 }
957
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000958 // Enter the debugger.
959 EnterDebugger debugger;
960 if (debugger.FailedToEnter()) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000961 return heap->undefined_value();
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000962 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000963
kasper.lund44510672008-07-25 07:37:58 +0000964 // Postpone interrupt during breakpoint processing.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000965 PostponeInterruptsScope postpone(isolate_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000966
967 // Get the debug info (create it if it does not exist).
968 Handle<SharedFunctionInfo> shared =
danno@chromium.org169691d2013-07-15 08:01:13 +0000969 Handle<SharedFunctionInfo>(frame->function()->shared());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000970 Handle<DebugInfo> debug_info = GetDebugInfo(shared);
971
972 // Find the break point where execution has stopped.
973 BreakLocationIterator break_location_iterator(debug_info,
974 ALL_BREAK_LOCATIONS);
verwaest@chromium.org8a00e822013-06-10 15:11:22 +0000975 // pc points to the instruction after the current one, possibly a break
976 // location as well. So the "- 1" to exclude it from the search.
977 break_location_iterator.FindBreakLocationFromAddress(frame->pc() - 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000978
979 // Check whether step next reached a new statement.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000980 if (!StepNextContinue(&break_location_iterator, frame)) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000981 // Decrease steps left if performing multiple steps.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000982 if (thread_local_.step_count_ > 0) {
983 thread_local_.step_count_--;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000984 }
985 }
986
987 // If there is one or more real break points check whether any of these are
988 // triggered.
ulan@chromium.org09d7ab52013-02-25 15:50:35 +0000989 Handle<Object> break_points_hit(heap->undefined_value(), isolate_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000990 if (break_location_iterator.HasBreakPoint()) {
991 Handle<Object> break_point_objects =
ulan@chromium.org09d7ab52013-02-25 15:50:35 +0000992 Handle<Object>(break_location_iterator.BreakPointObjects(), isolate_);
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000993 break_points_hit = CheckBreakPoints(break_point_objects);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000994 }
995
ager@chromium.orga1645e22009-09-09 19:27:10 +0000996 // If step out is active skip everything until the frame where we need to step
997 // out to is reached, unless real breakpoint is hit.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000998 if (StepOutActive() && frame->fp() != step_out_fp() &&
ager@chromium.orga1645e22009-09-09 19:27:10 +0000999 break_points_hit->IsUndefined() ) {
1000 // Step count should always be 0 for StepOut.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001001 ASSERT(thread_local_.step_count_ == 0);
ager@chromium.orga1645e22009-09-09 19:27:10 +00001002 } else if (!break_points_hit->IsUndefined() ||
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001003 (thread_local_.last_step_action_ != StepNone &&
1004 thread_local_.step_count_ == 0)) {
ager@chromium.orga1645e22009-09-09 19:27:10 +00001005 // Notify debugger if a real break point is triggered or if performing
1006 // single stepping with no more steps to perform. Otherwise do another step.
1007
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001008 // Clear all current stepping setup.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001009 ClearStepping();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001010
lrn@chromium.org34e60782011-09-15 07:25:40 +00001011 if (thread_local_.queued_step_count_ > 0) {
1012 // Perform queued steps
1013 int step_count = thread_local_.queued_step_count_;
1014
1015 // Clear queue
1016 thread_local_.queued_step_count_ = 0;
1017
1018 PrepareStep(StepNext, step_count);
1019 } else {
1020 // Notify the debug event listeners.
1021 isolate_->debugger()->OnDebugBreak(break_points_hit, false);
1022 }
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001023 } else if (thread_local_.last_step_action_ != StepNone) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001024 // Hold on to last step action as it is cleared by the call to
1025 // ClearStepping.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001026 StepAction step_action = thread_local_.last_step_action_;
1027 int step_count = thread_local_.step_count_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001028
lrn@chromium.org34e60782011-09-15 07:25:40 +00001029 // If StepNext goes deeper in code, StepOut until original frame
1030 // and keep step count queued up in the meantime.
1031 if (step_action == StepNext && frame->fp() < thread_local_.last_fp_) {
1032 // Count frames until target frame
1033 int count = 0;
1034 JavaScriptFrameIterator it(isolate_);
jkummerow@chromium.org212d9642012-05-11 15:02:09 +00001035 while (!it.done() && it.frame()->fp() < thread_local_.last_fp_) {
lrn@chromium.org34e60782011-09-15 07:25:40 +00001036 count++;
1037 it.Advance();
1038 }
1039
danno@chromium.org81cac2b2012-07-10 11:28:27 +00001040 // Check that we indeed found the frame we are looking for.
1041 CHECK(!it.done() && (it.frame()->fp() == thread_local_.last_fp_));
1042 if (step_count > 1) {
1043 // Save old count and action to continue stepping after StepOut.
1044 thread_local_.queued_step_count_ = step_count - 1;
jkummerow@chromium.org212d9642012-05-11 15:02:09 +00001045 }
1046
danno@chromium.org81cac2b2012-07-10 11:28:27 +00001047 // Set up for StepOut to reach target frame.
1048 step_action = StepOut;
1049 step_count = count;
lrn@chromium.org34e60782011-09-15 07:25:40 +00001050 }
1051
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001052 // Clear all current stepping setup.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001053 ClearStepping();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001054
1055 // Set up for the remaining steps.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001056 PrepareStep(step_action, step_count);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001057 }
1058
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001059 if (thread_local_.frame_drop_mode_ == FRAMES_UNTOUCHED) {
1060 SetAfterBreakTarget(frame);
1061 } else if (thread_local_.frame_drop_mode_ ==
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001062 FRAME_DROPPED_IN_IC_CALL) {
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001063 // We must have been calling IC stub. Do not go there anymore.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001064 Code* plain_return = isolate_->builtins()->builtin(
1065 Builtins::kPlainReturn_LiveEdit);
1066 thread_local_.after_break_target_ = plain_return->entry();
1067 } else if (thread_local_.frame_drop_mode_ ==
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001068 FRAME_DROPPED_IN_DEBUG_SLOT_CALL) {
1069 // Debug break slot stub does not return normally, instead it manually
1070 // cleans the stack and jumps. We should patch the jump address.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001071 Code* plain_return = isolate_->builtins()->builtin(
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00001072 Builtins::kFrameDropper_LiveEdit);
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001073 thread_local_.after_break_target_ = plain_return->entry();
1074 } else if (thread_local_.frame_drop_mode_ ==
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001075 FRAME_DROPPED_IN_DIRECT_CALL) {
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001076 // Nothing to do, after_break_target is not used here.
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00001077 } else if (thread_local_.frame_drop_mode_ ==
1078 FRAME_DROPPED_IN_RETURN_CALL) {
1079 Code* plain_return = isolate_->builtins()->builtin(
1080 Builtins::kFrameDropper_LiveEdit);
1081 thread_local_.after_break_target_ = plain_return->entry();
ager@chromium.org357bf652010-04-12 11:30:10 +00001082 } else {
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001083 UNREACHABLE();
ager@chromium.org357bf652010-04-12 11:30:10 +00001084 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001085
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001086 return heap->undefined_value();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001087}
1088
1089
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001090RUNTIME_FUNCTION(Object*, Debug_Break) {
1091 return isolate->debug()->Break(args);
1092}
1093
1094
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001095// Check the break point objects for whether one or more are actually
1096// triggered. This function returns a JSArray with the break point objects
1097// which is triggered.
1098Handle<Object> Debug::CheckBreakPoints(Handle<Object> break_point_objects) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00001099 Factory* factory = isolate_->factory();
1100
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001101 // Count the number of break points hit. If there are multiple break points
1102 // they are in a FixedArray.
1103 Handle<FixedArray> break_points_hit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001104 int break_points_hit_count = 0;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001105 ASSERT(!break_point_objects->IsUndefined());
1106 if (break_point_objects->IsFixedArray()) {
1107 Handle<FixedArray> array(FixedArray::cast(*break_point_objects));
lrn@chromium.org7516f052011-03-30 08:52:27 +00001108 break_points_hit = factory->NewFixedArray(array->length());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001109 for (int i = 0; i < array->length(); i++) {
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00001110 Handle<Object> o(array->get(i), isolate_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001111 if (CheckBreakPoint(o)) {
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001112 break_points_hit->set(break_points_hit_count++, *o);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001113 }
1114 }
1115 } else {
lrn@chromium.org7516f052011-03-30 08:52:27 +00001116 break_points_hit = factory->NewFixedArray(1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001117 if (CheckBreakPoint(break_point_objects)) {
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001118 break_points_hit->set(break_points_hit_count++, *break_point_objects);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001119 }
1120 }
1121
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001122 // Return undefined if no break points were triggered.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001123 if (break_points_hit_count == 0) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00001124 return factory->undefined_value();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001125 }
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001126 // Return break points hit as a JSArray.
lrn@chromium.org7516f052011-03-30 08:52:27 +00001127 Handle<JSArray> result = factory->NewJSArrayWithElements(break_points_hit);
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001128 result->set_length(Smi::FromInt(break_points_hit_count));
1129 return result;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001130}
1131
1132
1133// Check whether a single break point object is triggered.
1134bool Debug::CheckBreakPoint(Handle<Object> break_point_object) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00001135 Factory* factory = isolate_->factory();
1136 HandleScope scope(isolate_);
ager@chromium.org381abbb2009-02-25 13:23:22 +00001137
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001138 // Ignore check if break point object is not a JSObject.
1139 if (!break_point_object->IsJSObject()) return true;
1140
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001141 // Get the function IsBreakPointTriggered (defined in debug-debugger.js).
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001142 Handle<String> is_break_point_triggered_string =
1143 factory->InternalizeOneByteString(
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00001144 STATIC_ASCII_VECTOR("IsBreakPointTriggered"));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001145 Handle<JSFunction> check_break_point =
1146 Handle<JSFunction>(JSFunction::cast(
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00001147 debug_context()->global_object()->GetPropertyNoExceptionThrown(
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001148 *is_break_point_triggered_string)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001149
1150 // Get the break id as an object.
lrn@chromium.org7516f052011-03-30 08:52:27 +00001151 Handle<Object> break_id = factory->NewNumberFromInt(Debug::break_id());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001152
1153 // Call HandleBreakPointx.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001154 bool caught_exception;
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00001155 Handle<Object> argv[] = { break_id, break_point_object };
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001156 Handle<Object> result = Execution::TryCall(check_break_point,
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00001157 isolate_->js_builtins_object(),
1158 ARRAY_SIZE(argv),
1159 argv,
1160 &caught_exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001161
1162 // If exception or non boolean result handle as not triggered
1163 if (caught_exception || !result->IsBoolean()) {
1164 return false;
1165 }
1166
1167 // Return whether the break point is triggered.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001168 ASSERT(!result.is_null());
1169 return (*result)->IsTrue();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001170}
1171
1172
1173// Check whether the function has debug information.
1174bool Debug::HasDebugInfo(Handle<SharedFunctionInfo> shared) {
1175 return !shared->debug_info()->IsUndefined();
1176}
1177
1178
kasper.lundbd3ec4e2008-07-09 11:06:54 +00001179// Return the debug info for this function. EnsureDebugInfo must be called
1180// prior to ensure the debug info has been generated for shared.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001181Handle<DebugInfo> Debug::GetDebugInfo(Handle<SharedFunctionInfo> shared) {
kasper.lundbd3ec4e2008-07-09 11:06:54 +00001182 ASSERT(HasDebugInfo(shared));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001183 return Handle<DebugInfo>(DebugInfo::cast(shared->debug_info()));
1184}
1185
1186
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001187void Debug::SetBreakPoint(Handle<JSFunction> function,
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00001188 Handle<Object> break_point_object,
1189 int* source_position) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00001190 HandleScope scope(isolate_);
ager@chromium.org381abbb2009-02-25 13:23:22 +00001191
lrn@chromium.org34e60782011-09-15 07:25:40 +00001192 PrepareForBreakPoints();
1193
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001194 // Make sure the function is compiled and has set up the debug info.
1195 Handle<SharedFunctionInfo> shared(function->shared());
1196 if (!EnsureDebugInfo(shared, function)) {
kasper.lundbd3ec4e2008-07-09 11:06:54 +00001197 // Return if retrieving debug info failed.
1198 return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001199 }
1200
kasper.lundbd3ec4e2008-07-09 11:06:54 +00001201 Handle<DebugInfo> debug_info = GetDebugInfo(shared);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001202 // Source positions starts with zero.
danno@chromium.orgbf0c8202011-12-27 10:09:42 +00001203 ASSERT(*source_position >= 0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001204
1205 // Find the break point and change it.
1206 BreakLocationIterator it(debug_info, SOURCE_BREAK_LOCATIONS);
jkummerow@chromium.org93a47f42013-07-02 14:43:41 +00001207 it.FindBreakLocationFromPosition(*source_position, STATEMENT_ALIGNED);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001208 it.SetBreakPoint(break_point_object);
1209
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00001210 *source_position = it.position();
1211
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001212 // At least one active break point now.
1213 ASSERT(debug_info->GetBreakPointCount() > 0);
1214}
1215
1216
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001217bool Debug::SetBreakPointForScript(Handle<Script> script,
1218 Handle<Object> break_point_object,
jkummerow@chromium.org93a47f42013-07-02 14:43:41 +00001219 int* source_position,
1220 BreakPositionAlignment alignment) {
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001221 HandleScope scope(isolate_);
1222
jkummerow@chromium.org78502a92012-09-06 13:50:42 +00001223 PrepareForBreakPoints();
1224
1225 // Obtain shared function info for the function.
1226 Object* result = FindSharedFunctionInfoInScript(script, *source_position);
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001227 if (result->IsUndefined()) return false;
1228
1229 // Make sure the function has set up the debug info.
1230 Handle<SharedFunctionInfo> shared(SharedFunctionInfo::cast(result));
1231 if (!EnsureDebugInfo(shared, Handle<JSFunction>::null())) {
1232 // Return if retrieving debug info failed.
1233 return false;
1234 }
1235
1236 // Find position within function. The script position might be before the
1237 // source position of the first function.
1238 int position;
1239 if (shared->start_position() > *source_position) {
1240 position = 0;
1241 } else {
1242 position = *source_position - shared->start_position();
1243 }
1244
1245 Handle<DebugInfo> debug_info = GetDebugInfo(shared);
1246 // Source positions starts with zero.
1247 ASSERT(position >= 0);
1248
1249 // Find the break point and change it.
1250 BreakLocationIterator it(debug_info, SOURCE_BREAK_LOCATIONS);
jkummerow@chromium.org93a47f42013-07-02 14:43:41 +00001251 it.FindBreakLocationFromPosition(position, alignment);
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001252 it.SetBreakPoint(break_point_object);
1253
1254 *source_position = it.position() + shared->start_position();
1255
1256 // At least one active break point now.
1257 ASSERT(debug_info->GetBreakPointCount() > 0);
1258 return true;
1259}
1260
1261
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001262void Debug::ClearBreakPoint(Handle<Object> break_point_object) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00001263 HandleScope scope(isolate_);
ager@chromium.org381abbb2009-02-25 13:23:22 +00001264
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001265 DebugInfoListNode* node = debug_info_list_;
1266 while (node != NULL) {
1267 Object* result = DebugInfo::FindBreakPointInfo(node->debug_info(),
1268 break_point_object);
1269 if (!result->IsUndefined()) {
1270 // Get information in the break point.
1271 BreakPointInfo* break_point_info = BreakPointInfo::cast(result);
1272 Handle<DebugInfo> debug_info = node->debug_info();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001273
1274 // Find the break point and clear it.
1275 BreakLocationIterator it(debug_info, SOURCE_BREAK_LOCATIONS);
verwaest@chromium.org8a00e822013-06-10 15:11:22 +00001276 it.FindBreakLocationFromAddress(debug_info->code()->entry() +
1277 break_point_info->code_position()->value());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001278 it.ClearBreakPoint(break_point_object);
1279
1280 // If there are no more break points left remove the debug info for this
1281 // function.
1282 if (debug_info->GetBreakPointCount() == 0) {
1283 RemoveDebugInfo(debug_info);
1284 }
1285
1286 return;
1287 }
1288 node = node->next();
1289 }
1290}
1291
1292
ager@chromium.org381abbb2009-02-25 13:23:22 +00001293void Debug::ClearAllBreakPoints() {
1294 DebugInfoListNode* node = debug_info_list_;
1295 while (node != NULL) {
1296 // Remove all debug break code.
1297 BreakLocationIterator it(node->debug_info(), ALL_BREAK_LOCATIONS);
1298 it.ClearAllDebugBreak();
1299 node = node->next();
1300 }
1301
1302 // Remove all debug info.
1303 while (debug_info_list_ != NULL) {
1304 RemoveDebugInfo(debug_info_list_->debug_info());
1305 }
1306}
1307
1308
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001309void Debug::FloodWithOneShot(Handle<JSFunction> function) {
lrn@chromium.org34e60782011-09-15 07:25:40 +00001310 PrepareForBreakPoints();
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001311
1312 // Make sure the function is compiled and has set up the debug info.
1313 Handle<SharedFunctionInfo> shared(function->shared());
1314 if (!EnsureDebugInfo(shared, function)) {
kasper.lundbd3ec4e2008-07-09 11:06:54 +00001315 // Return if we failed to retrieve the debug info.
1316 return;
1317 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001318
1319 // Flood the function with break points.
kasper.lundbd3ec4e2008-07-09 11:06:54 +00001320 BreakLocationIterator it(GetDebugInfo(shared), ALL_BREAK_LOCATIONS);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001321 while (!it.Done()) {
1322 it.SetOneShot();
1323 it.Next();
1324 }
1325}
1326
1327
rossberg@chromium.org2c067b12012-03-19 11:01:52 +00001328void Debug::FloodBoundFunctionWithOneShot(Handle<JSFunction> function) {
1329 Handle<FixedArray> new_bindings(function->function_bindings());
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00001330 Handle<Object> bindee(new_bindings->get(JSFunction::kBoundFunctionIndex),
1331 isolate_);
rossberg@chromium.org2c067b12012-03-19 11:01:52 +00001332
1333 if (!bindee.is_null() && bindee->IsJSFunction() &&
1334 !JSFunction::cast(*bindee)->IsBuiltin()) {
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001335 Handle<JSFunction> bindee_function(JSFunction::cast(*bindee));
1336 Debug::FloodWithOneShot(bindee_function);
rossberg@chromium.org2c067b12012-03-19 11:01:52 +00001337 }
1338}
1339
1340
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001341void Debug::FloodHandlerWithOneShot() {
ager@chromium.org8bb60582008-12-11 12:02:20 +00001342 // Iterate through the JavaScript stack looking for handlers.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001343 StackFrame::Id id = break_frame_id();
ager@chromium.org8bb60582008-12-11 12:02:20 +00001344 if (id == StackFrame::NO_ID) {
1345 // If there is no JavaScript stack don't do anything.
1346 return;
1347 }
vegorov@chromium.org74f333b2011-04-06 11:17:46 +00001348 for (JavaScriptFrameIterator it(isolate_, id); !it.done(); it.Advance()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001349 JavaScriptFrame* frame = it.frame();
1350 if (frame->HasHandler()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001351 // Flood the function with the catch block with break points
danno@chromium.org169691d2013-07-15 08:01:13 +00001352 FloodWithOneShot(Handle<JSFunction>(frame->function()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001353 return;
1354 }
1355 }
1356}
1357
1358
1359void Debug::ChangeBreakOnException(ExceptionBreakType type, bool enable) {
1360 if (type == BreakUncaughtException) {
1361 break_on_uncaught_exception_ = enable;
1362 } else {
1363 break_on_exception_ = enable;
1364 }
1365}
1366
1367
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00001368bool Debug::IsBreakOnException(ExceptionBreakType type) {
1369 if (type == BreakUncaughtException) {
1370 return break_on_uncaught_exception_;
1371 } else {
1372 return break_on_exception_;
1373 }
1374}
1375
1376
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001377void Debug::PrepareStep(StepAction step_action, int step_count) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00001378 HandleScope scope(isolate_);
lrn@chromium.org34e60782011-09-15 07:25:40 +00001379
1380 PrepareForBreakPoints();
1381
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001382 ASSERT(Debug::InDebugger());
1383
1384 // Remember this step action and count.
1385 thread_local_.last_step_action_ = step_action;
ager@chromium.orga1645e22009-09-09 19:27:10 +00001386 if (step_action == StepOut) {
1387 // For step out target frame will be found on the stack so there is no need
1388 // to set step counter for it. It's expected to always be 0 for StepOut.
1389 thread_local_.step_count_ = 0;
1390 } else {
1391 thread_local_.step_count_ = step_count;
1392 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001393
1394 // Get the frame where the execution has stopped and skip the debug frame if
1395 // any. The debug frame will only be present if execution was stopped due to
1396 // hitting a break point. In other situations (e.g. unhandled exception) the
1397 // debug frame is not present.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001398 StackFrame::Id id = break_frame_id();
ager@chromium.org8bb60582008-12-11 12:02:20 +00001399 if (id == StackFrame::NO_ID) {
1400 // If there is no JavaScript stack don't do anything.
1401 return;
1402 }
vegorov@chromium.org74f333b2011-04-06 11:17:46 +00001403 JavaScriptFrameIterator frames_it(isolate_, id);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001404 JavaScriptFrame* frame = frames_it.frame();
1405
1406 // First of all ensure there is one-shot break points in the top handler
1407 // if any.
1408 FloodHandlerWithOneShot();
1409
1410 // If the function on the top frame is unresolved perform step out. This will
1411 // be the case when calling unknown functions and having the debugger stopped
1412 // in an unhandled exception.
1413 if (!frame->function()->IsJSFunction()) {
1414 // Step out: Find the calling JavaScript frame and flood it with
1415 // breakpoints.
1416 frames_it.Advance();
1417 // Fill the function to return to with one-shot break points.
danno@chromium.org169691d2013-07-15 08:01:13 +00001418 JSFunction* function = frames_it.frame()->function();
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001419 FloodWithOneShot(Handle<JSFunction>(function));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001420 return;
1421 }
1422
1423 // Get the debug info (create it if it does not exist).
danno@chromium.org169691d2013-07-15 08:01:13 +00001424 Handle<JSFunction> function(frame->function());
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001425 Handle<SharedFunctionInfo> shared(function->shared());
1426 if (!EnsureDebugInfo(shared, function)) {
kasper.lundbd3ec4e2008-07-09 11:06:54 +00001427 // Return if ensuring debug info failed.
1428 return;
1429 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001430 Handle<DebugInfo> debug_info = GetDebugInfo(shared);
1431
1432 // Find the break location where execution has stopped.
1433 BreakLocationIterator it(debug_info, ALL_BREAK_LOCATIONS);
verwaest@chromium.org8a00e822013-06-10 15:11:22 +00001434 // pc points to the instruction after the current one, possibly a break
1435 // location as well. So the "- 1" to exclude it from the search.
1436 it.FindBreakLocationFromAddress(frame->pc() - 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001437
1438 // Compute whether or not the target is a call target.
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001439 bool is_load_or_store = false;
1440 bool is_inline_cache_stub = false;
whesse@chromium.orge90029b2010-08-02 11:52:17 +00001441 bool is_at_restarted_function = false;
ager@chromium.orga1645e22009-09-09 19:27:10 +00001442 Handle<Code> call_function_stub;
ager@chromium.orga1645e22009-09-09 19:27:10 +00001443
whesse@chromium.orge90029b2010-08-02 11:52:17 +00001444 if (thread_local_.restarter_frame_function_pointer_ == NULL) {
1445 if (RelocInfo::IsCodeTarget(it.rinfo()->rmode())) {
1446 bool is_call_target = false;
1447 Address target = it.rinfo()->target_address();
1448 Code* code = Code::GetCodeFromTargetAddress(target);
1449 if (code->is_call_stub() || code->is_keyed_call_stub()) {
1450 is_call_target = true;
1451 }
1452 if (code->is_inline_cache_stub()) {
1453 is_inline_cache_stub = true;
1454 is_load_or_store = !is_call_target;
1455 }
1456
1457 // Check if target code is CallFunction stub.
1458 Code* maybe_call_function_stub = code;
1459 // If there is a breakpoint at this line look at the original code to
1460 // check if it is a CallFunction stub.
1461 if (it.IsDebugBreak()) {
1462 Address original_target = it.original_rinfo()->target_address();
1463 maybe_call_function_stub =
1464 Code::GetCodeFromTargetAddress(original_target);
1465 }
1466 if (maybe_call_function_stub->kind() == Code::STUB &&
1467 maybe_call_function_stub->major_key() == CodeStub::CallFunction) {
1468 // Save reference to the code as we may need it to find out arguments
1469 // count for 'step in' later.
1470 call_function_stub = Handle<Code>(maybe_call_function_stub);
1471 }
ager@chromium.orga1645e22009-09-09 19:27:10 +00001472 }
whesse@chromium.orge90029b2010-08-02 11:52:17 +00001473 } else {
1474 is_at_restarted_function = true;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001475 }
1476
v8.team.kasperl727e9952008-09-02 14:56:44 +00001477 // If this is the last break code target step out is the only possibility.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001478 if (it.IsExit() || step_action == StepOut) {
ager@chromium.orga1645e22009-09-09 19:27:10 +00001479 if (step_action == StepOut) {
1480 // Skip step_count frames starting with the current one.
1481 while (step_count-- > 0 && !frames_it.done()) {
1482 frames_it.Advance();
1483 }
1484 } else {
1485 ASSERT(it.IsExit());
1486 frames_it.Advance();
1487 }
1488 // Skip builtin functions on the stack.
danno@chromium.org169691d2013-07-15 08:01:13 +00001489 while (!frames_it.done() && frames_it.frame()->function()->IsBuiltin()) {
ager@chromium.orga1645e22009-09-09 19:27:10 +00001490 frames_it.Advance();
1491 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001492 // Step out: If there is a JavaScript caller frame, we need to
1493 // flood it with breakpoints.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001494 if (!frames_it.done()) {
1495 // Fill the function to return to with one-shot break points.
danno@chromium.org169691d2013-07-15 08:01:13 +00001496 JSFunction* function = frames_it.frame()->function();
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001497 FloodWithOneShot(Handle<JSFunction>(function));
ager@chromium.orga1645e22009-09-09 19:27:10 +00001498 // Set target frame pointer.
1499 ActivateStepOut(frames_it.frame());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001500 }
ager@chromium.orga1645e22009-09-09 19:27:10 +00001501 } else if (!(is_inline_cache_stub || RelocInfo::IsConstructCall(it.rmode()) ||
whesse@chromium.orge90029b2010-08-02 11:52:17 +00001502 !call_function_stub.is_null() || is_at_restarted_function)
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001503 || step_action == StepNext || step_action == StepMin) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001504 // Step next or step min.
1505
1506 // Fill the current function with one-shot break points.
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001507 FloodWithOneShot(function);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001508
1509 // Remember source position and frame to handle step next.
1510 thread_local_.last_statement_position_ =
1511 debug_info->code()->SourceStatementPosition(frame->pc());
mmassi@chromium.org7028c052012-06-13 11:51:58 +00001512 thread_local_.last_fp_ = frame->UnpaddedFP();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001513 } else {
whesse@chromium.orge90029b2010-08-02 11:52:17 +00001514 // If there's restarter frame on top of the stack, just get the pointer
1515 // to function which is going to be restarted.
1516 if (is_at_restarted_function) {
1517 Handle<JSFunction> restarted_function(
1518 JSFunction::cast(*thread_local_.restarter_frame_function_pointer_));
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001519 FloodWithOneShot(restarted_function);
whesse@chromium.orge90029b2010-08-02 11:52:17 +00001520 } else if (!call_function_stub.is_null()) {
1521 // If it's CallFunction stub ensure target function is compiled and flood
1522 // it with one shot breakpoints.
1523
ager@chromium.orga1645e22009-09-09 19:27:10 +00001524 // Find out number of arguments from the stub minor key.
1525 // Reverse lookup required as the minor key cannot be retrieved
1526 // from the code object.
1527 Handle<Object> obj(
lrn@chromium.org7516f052011-03-30 08:52:27 +00001528 isolate_->heap()->code_stubs()->SlowReverseLookup(
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00001529 *call_function_stub),
1530 isolate_);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001531 ASSERT(!obj.is_null());
1532 ASSERT(!(*obj)->IsUndefined());
ager@chromium.orga1645e22009-09-09 19:27:10 +00001533 ASSERT(obj->IsSmi());
1534 // Get the STUB key and extract major and minor key.
1535 uint32_t key = Smi::cast(*obj)->value();
1536 // Argc in the stub is the number of arguments passed - not the
1537 // expected arguments of the called function.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001538 int call_function_arg_count =
1539 CallFunctionStub::ExtractArgcFromMinorKey(
1540 CodeStub::MinorKeyFromKey(key));
ager@chromium.orga1645e22009-09-09 19:27:10 +00001541 ASSERT(call_function_stub->major_key() ==
1542 CodeStub::MajorKeyFromKey(key));
1543
1544 // Find target function on the expression stack.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001545 // Expression stack looks like this (top to bottom):
ager@chromium.orga1645e22009-09-09 19:27:10 +00001546 // argN
1547 // ...
1548 // arg0
1549 // Receiver
1550 // Function to call
1551 int expressions_count = frame->ComputeExpressionsCount();
1552 ASSERT(expressions_count - 2 - call_function_arg_count >= 0);
1553 Object* fun = frame->GetExpression(
1554 expressions_count - 2 - call_function_arg_count);
1555 if (fun->IsJSFunction()) {
1556 Handle<JSFunction> js_function(JSFunction::cast(fun));
rossberg@chromium.org2c067b12012-03-19 11:01:52 +00001557 if (js_function->shared()->bound()) {
1558 Debug::FloodBoundFunctionWithOneShot(js_function);
1559 } else if (!js_function->IsBuiltin()) {
1560 // Don't step into builtins.
ager@chromium.orga1645e22009-09-09 19:27:10 +00001561 // It will also compile target function if it's not compiled yet.
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001562 FloodWithOneShot(js_function);
ager@chromium.orga1645e22009-09-09 19:27:10 +00001563 }
1564 }
1565 }
1566
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001567 // Fill the current function with one-shot break points even for step in on
1568 // a call target as the function called might be a native function for
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001569 // which step in will not stop. It also prepares for stepping in
1570 // getters/setters.
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001571 FloodWithOneShot(function);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001572
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001573 if (is_load_or_store) {
1574 // Remember source position and frame to handle step in getter/setter. If
1575 // there is a custom getter/setter it will be handled in
1576 // Object::Get/SetPropertyWithCallback, otherwise the step action will be
1577 // propagated on the next Debug::Break.
1578 thread_local_.last_statement_position_ =
1579 debug_info->code()->SourceStatementPosition(frame->pc());
mmassi@chromium.org7028c052012-06-13 11:51:58 +00001580 thread_local_.last_fp_ = frame->UnpaddedFP();
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001581 }
1582
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001583 // Step in or Step in min
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00001584 it.PrepareStepIn(isolate_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001585 ActivateStepIn(frame);
1586 }
1587}
1588
1589
1590// Check whether the current debug break should be reported to the debugger. It
1591// is used to have step next and step in only report break back to the debugger
1592// if on a different frame or in a different statement. In some situations
1593// there will be several break points in the same statement when the code is
v8.team.kasperl727e9952008-09-02 14:56:44 +00001594// flooded with one-shot break points. This function helps to perform several
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001595// steps before reporting break back to the debugger.
1596bool Debug::StepNextContinue(BreakLocationIterator* break_location_iterator,
1597 JavaScriptFrame* frame) {
lrn@chromium.org34e60782011-09-15 07:25:40 +00001598 // StepNext and StepOut shouldn't bring us deeper in code, so last frame
1599 // shouldn't be a parent of current frame.
1600 if (thread_local_.last_step_action_ == StepNext ||
1601 thread_local_.last_step_action_ == StepOut) {
1602 if (frame->fp() < thread_local_.last_fp_) return true;
1603 }
1604
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001605 // If the step last action was step next or step in make sure that a new
1606 // statement is hit.
1607 if (thread_local_.last_step_action_ == StepNext ||
1608 thread_local_.last_step_action_ == StepIn) {
1609 // Never continue if returning from function.
1610 if (break_location_iterator->IsExit()) return false;
1611
1612 // Continue if we are still on the same frame and in the same statement.
1613 int current_statement_position =
1614 break_location_iterator->code()->SourceStatementPosition(frame->pc());
mmassi@chromium.org7028c052012-06-13 11:51:58 +00001615 return thread_local_.last_fp_ == frame->UnpaddedFP() &&
ager@chromium.org236ad962008-09-25 09:45:57 +00001616 thread_local_.last_statement_position_ == current_statement_position;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001617 }
1618
1619 // No step next action - don't continue.
1620 return false;
1621}
1622
1623
1624// Check whether the code object at the specified address is a debug break code
1625// object.
1626bool Debug::IsDebugBreak(Address addr) {
ager@chromium.org8bb60582008-12-11 12:02:20 +00001627 Code* code = Code::GetCodeFromTargetAddress(addr);
yangguo@chromium.org9768bf12013-01-11 14:51:07 +00001628 return code->is_debug_break();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001629}
1630
1631
1632// Check whether a code stub with the specified major key is a possible break
1633// point location when looking for source break locations.
1634bool Debug::IsSourceBreakStub(Code* code) {
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001635 CodeStub::Major major_key = CodeStub::GetMajorKey(code);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001636 return major_key == CodeStub::CallFunction;
1637}
1638
1639
1640// Check whether a code stub with the specified major key is a possible break
1641// location.
1642bool Debug::IsBreakStub(Code* code) {
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001643 CodeStub::Major major_key = CodeStub::GetMajorKey(code);
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00001644 return major_key == CodeStub::CallFunction;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001645}
1646
1647
1648// Find the builtin to use for invoking the debug break
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001649Handle<Code> Debug::FindDebugBreak(Handle<Code> code, RelocInfo::Mode mode) {
danno@chromium.orgfa458e42012-02-01 10:48:36 +00001650 Isolate* isolate = Isolate::Current();
1651
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001652 // Find the builtin debug break function matching the calling convention
1653 // used by the call site.
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001654 if (code->is_inline_cache_stub()) {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001655 switch (code->kind()) {
1656 case Code::CALL_IC:
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001657 case Code::KEYED_CALL_IC:
danno@chromium.orgfa458e42012-02-01 10:48:36 +00001658 return isolate->stub_cache()->ComputeCallDebugBreak(
1659 code->arguments_count(), code->kind());
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001660
1661 case Code::LOAD_IC:
danno@chromium.orgfa458e42012-02-01 10:48:36 +00001662 return isolate->builtins()->LoadIC_DebugBreak();
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001663
1664 case Code::STORE_IC:
danno@chromium.orgfa458e42012-02-01 10:48:36 +00001665 return isolate->builtins()->StoreIC_DebugBreak();
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001666
1667 case Code::KEYED_LOAD_IC:
danno@chromium.orgfa458e42012-02-01 10:48:36 +00001668 return isolate->builtins()->KeyedLoadIC_DebugBreak();
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001669
1670 case Code::KEYED_STORE_IC:
danno@chromium.orgfa458e42012-02-01 10:48:36 +00001671 return isolate->builtins()->KeyedStoreIC_DebugBreak();
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001672
danno@chromium.orgf005df62013-04-30 16:36:45 +00001673 case Code::COMPARE_NIL_IC:
1674 return isolate->builtins()->CompareNilIC_DebugBreak();
1675
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001676 default:
1677 UNREACHABLE();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001678 }
1679 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001680 if (RelocInfo::IsConstructCall(mode)) {
danno@chromium.orgfa458e42012-02-01 10:48:36 +00001681 if (code->has_function_cache()) {
1682 return isolate->builtins()->CallConstructStub_Recording_DebugBreak();
1683 } else {
1684 return isolate->builtins()->CallConstructStub_DebugBreak();
1685 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001686 }
1687 if (code->kind() == Code::STUB) {
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00001688 ASSERT(code->major_key() == CodeStub::CallFunction);
danno@chromium.orgfa458e42012-02-01 10:48:36 +00001689 if (code->has_function_cache()) {
1690 return isolate->builtins()->CallFunctionStub_Recording_DebugBreak();
1691 } else {
1692 return isolate->builtins()->CallFunctionStub_DebugBreak();
1693 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001694 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001695
1696 UNREACHABLE();
1697 return Handle<Code>::null();
1698}
1699
1700
1701// Simple function for returning the source positions for active break points.
1702Handle<Object> Debug::GetSourceBreakLocations(
jkummerow@chromium.org93a47f42013-07-02 14:43:41 +00001703 Handle<SharedFunctionInfo> shared,
1704 BreakPositionAlignment position_alignment) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00001705 Isolate* isolate = Isolate::Current();
1706 Heap* heap = isolate->heap();
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00001707 if (!HasDebugInfo(shared)) {
1708 return Handle<Object>(heap->undefined_value(), isolate);
1709 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001710 Handle<DebugInfo> debug_info = GetDebugInfo(shared);
1711 if (debug_info->GetBreakPointCount() == 0) {
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00001712 return Handle<Object>(heap->undefined_value(), isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001713 }
1714 Handle<FixedArray> locations =
lrn@chromium.org7516f052011-03-30 08:52:27 +00001715 isolate->factory()->NewFixedArray(debug_info->GetBreakPointCount());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001716 int count = 0;
1717 for (int i = 0; i < debug_info->break_points()->length(); i++) {
1718 if (!debug_info->break_points()->get(i)->IsUndefined()) {
1719 BreakPointInfo* break_point_info =
1720 BreakPointInfo::cast(debug_info->break_points()->get(i));
1721 if (break_point_info->GetBreakPointCount() > 0) {
jkummerow@chromium.org93a47f42013-07-02 14:43:41 +00001722 Smi* position;
1723 switch (position_alignment) {
1724 case STATEMENT_ALIGNED:
1725 position = break_point_info->statement_position();
1726 break;
1727 case BREAK_POSITION_ALIGNED:
1728 position = break_point_info->source_position();
1729 break;
1730 default:
1731 UNREACHABLE();
1732 position = break_point_info->statement_position();
1733 }
1734
1735 locations->set(count++, position);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001736 }
1737 }
1738 }
1739 return locations;
1740}
1741
1742
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001743void Debug::NewBreak(StackFrame::Id break_frame_id) {
1744 thread_local_.break_frame_id_ = break_frame_id;
1745 thread_local_.break_id_ = ++thread_local_.break_count_;
1746}
1747
1748
1749void Debug::SetBreak(StackFrame::Id break_frame_id, int break_id) {
1750 thread_local_.break_frame_id_ = break_frame_id;
1751 thread_local_.break_id_ = break_id;
1752}
1753
1754
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001755// Handle stepping into a function.
1756void Debug::HandleStepIn(Handle<JSFunction> function,
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001757 Handle<Object> holder,
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001758 Address fp,
1759 bool is_constructor) {
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00001760 Isolate* isolate = function->GetIsolate();
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001761 // If the frame pointer is not supplied by the caller find it.
1762 if (fp == 0) {
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00001763 StackFrameIterator it(isolate);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001764 it.Advance();
1765 // For constructor functions skip another frame.
1766 if (is_constructor) {
1767 ASSERT(it.frame()->is_construct());
1768 it.Advance();
1769 }
1770 fp = it.frame()->fp();
1771 }
1772
1773 // Flood the function with one-shot break points if it is called from where
1774 // step into was requested.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001775 if (fp == step_in_fp()) {
rossberg@chromium.org2c067b12012-03-19 11:01:52 +00001776 if (function->shared()->bound()) {
1777 // Handle Function.prototype.bind
1778 Debug::FloodBoundFunctionWithOneShot(function);
1779 } else if (!function->IsBuiltin()) {
1780 // Don't allow step into functions in the native context.
kasperl@chromium.orgacae3782009-04-11 09:17:08 +00001781 if (function->shared()->code() ==
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00001782 isolate->builtins()->builtin(Builtins::kFunctionApply) ||
kasperl@chromium.orgacae3782009-04-11 09:17:08 +00001783 function->shared()->code() ==
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00001784 isolate->builtins()->builtin(Builtins::kFunctionCall)) {
kasperl@chromium.orgacae3782009-04-11 09:17:08 +00001785 // Handle function.apply and function.call separately to flood the
1786 // function to be called and not the code for Builtins::FunctionApply or
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001787 // Builtins::FunctionCall. The receiver of call/apply is the target
1788 // function.
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001789 if (!holder.is_null() && holder->IsJSFunction() &&
1790 !JSFunction::cast(*holder)->IsBuiltin()) {
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001791 Handle<JSFunction> js_function = Handle<JSFunction>::cast(holder);
1792 Debug::FloodWithOneShot(js_function);
kasperl@chromium.orgacae3782009-04-11 09:17:08 +00001793 }
1794 } else {
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001795 Debug::FloodWithOneShot(function);
kasperl@chromium.orgacae3782009-04-11 09:17:08 +00001796 }
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001797 }
1798 }
1799}
1800
1801
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001802void Debug::ClearStepping() {
1803 // Clear the various stepping setup.
1804 ClearOneShot();
1805 ClearStepIn();
ager@chromium.orga1645e22009-09-09 19:27:10 +00001806 ClearStepOut();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001807 ClearStepNext();
1808
1809 // Clear multiple step counter.
1810 thread_local_.step_count_ = 0;
1811}
1812
mstarzinger@chromium.orge0e1b0d2013-07-08 08:38:06 +00001813
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001814// Clears all the one-shot break points that are currently set. Normally this
1815// function is called each time a break point is hit as one shot break points
1816// are used to support stepping.
1817void Debug::ClearOneShot() {
1818 // The current implementation just runs through all the breakpoints. When the
v8.team.kasperl727e9952008-09-02 14:56:44 +00001819 // last break point for a function is removed that function is automatically
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001820 // removed from the list.
1821
1822 DebugInfoListNode* node = debug_info_list_;
1823 while (node != NULL) {
1824 BreakLocationIterator it(node->debug_info(), ALL_BREAK_LOCATIONS);
1825 while (!it.Done()) {
1826 it.ClearOneShot();
1827 it.Next();
1828 }
1829 node = node->next();
1830 }
1831}
1832
1833
1834void Debug::ActivateStepIn(StackFrame* frame) {
ager@chromium.orga1645e22009-09-09 19:27:10 +00001835 ASSERT(!StepOutActive());
mmassi@chromium.org7028c052012-06-13 11:51:58 +00001836 thread_local_.step_into_fp_ = frame->UnpaddedFP();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001837}
1838
1839
1840void Debug::ClearStepIn() {
1841 thread_local_.step_into_fp_ = 0;
1842}
1843
1844
ager@chromium.orga1645e22009-09-09 19:27:10 +00001845void Debug::ActivateStepOut(StackFrame* frame) {
1846 ASSERT(!StepInActive());
mmassi@chromium.org7028c052012-06-13 11:51:58 +00001847 thread_local_.step_out_fp_ = frame->UnpaddedFP();
ager@chromium.orga1645e22009-09-09 19:27:10 +00001848}
1849
1850
1851void Debug::ClearStepOut() {
1852 thread_local_.step_out_fp_ = 0;
1853}
1854
1855
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001856void Debug::ClearStepNext() {
1857 thread_local_.last_step_action_ = StepNone;
ager@chromium.org236ad962008-09-25 09:45:57 +00001858 thread_local_.last_statement_position_ = RelocInfo::kNoPosition;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001859 thread_local_.last_fp_ = 0;
1860}
1861
1862
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001863// Helper function to compile full code for debugging. This code will
mmassi@chromium.org7028c052012-06-13 11:51:58 +00001864// have debug break slots and deoptimization information. Deoptimization
1865// information is required in case that an optimized version of this
1866// function is still activated on the stack. It will also make sure that
1867// the full code is compiled with the same flags as the previous version,
1868// that is flags which can change the code generated. The current method
1869// of mapping from already compiled full code without debug break slots
1870// to full code with debug break slots depends on the generated code is
1871// otherwise exactly the same.
1872static bool CompileFullCodeForDebugging(Handle<JSFunction> function,
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001873 Handle<Code> current_code) {
1874 ASSERT(!current_code->has_debug_break_slots());
1875
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001876 CompilationInfoWithZone info(function);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001877 info.MarkCompilingForDebugging(current_code);
1878 ASSERT(!info.shared_info()->is_compiled());
1879 ASSERT(!info.isolate()->has_pending_exception());
1880
1881 // Use compile lazy which will end up compiling the full code in the
1882 // configuration configured above.
1883 bool result = Compiler::CompileLazy(&info);
1884 ASSERT(result != Isolate::Current()->has_pending_exception());
1885 info.isolate()->clear_pending_exception();
1886#if DEBUG
1887 if (result) {
mmassi@chromium.org7028c052012-06-13 11:51:58 +00001888 Handle<Code> new_code(function->shared()->code());
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001889 ASSERT(new_code->has_debug_break_slots());
1890 ASSERT(current_code->is_compiled_optimizable() ==
1891 new_code->is_compiled_optimizable());
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001892 }
1893#endif
1894 return result;
1895}
1896
1897
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001898static void CollectActiveFunctionsFromThread(
1899 Isolate* isolate,
1900 ThreadLocalTop* top,
1901 List<Handle<JSFunction> >* active_functions,
1902 Object* active_code_marker) {
1903 // Find all non-optimized code functions with activation frames
1904 // on the stack. This includes functions which have optimized
1905 // activations (including inlined functions) on the stack as the
1906 // non-optimized code is needed for the lazy deoptimization.
1907 for (JavaScriptFrameIterator it(isolate, top); !it.done(); it.Advance()) {
1908 JavaScriptFrame* frame = it.frame();
1909 if (frame->is_optimized()) {
jkummerow@chromium.orgba72ec82013-07-22 09:21:20 +00001910 List<JSFunction*> functions(FLAG_max_inlining_levels + 1);
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001911 frame->GetFunctions(&functions);
1912 for (int i = 0; i < functions.length(); i++) {
1913 JSFunction* function = functions[i];
1914 active_functions->Add(Handle<JSFunction>(function));
1915 function->shared()->code()->set_gc_metadata(active_code_marker);
1916 }
1917 } else if (frame->function()->IsJSFunction()) {
danno@chromium.org169691d2013-07-15 08:01:13 +00001918 JSFunction* function = frame->function();
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001919 ASSERT(frame->LookupCode()->kind() == Code::FUNCTION);
1920 active_functions->Add(Handle<JSFunction>(function));
1921 function->shared()->code()->set_gc_metadata(active_code_marker);
1922 }
1923 }
1924}
1925
1926
1927static void RedirectActivationsToRecompiledCodeOnThread(
1928 Isolate* isolate,
1929 ThreadLocalTop* top) {
1930 for (JavaScriptFrameIterator it(isolate, top); !it.done(); it.Advance()) {
1931 JavaScriptFrame* frame = it.frame();
1932
1933 if (frame->is_optimized() || !frame->function()->IsJSFunction()) continue;
1934
danno@chromium.org169691d2013-07-15 08:01:13 +00001935 JSFunction* function = frame->function();
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001936
1937 ASSERT(frame->LookupCode()->kind() == Code::FUNCTION);
1938
1939 Handle<Code> frame_code(frame->LookupCode());
1940 if (frame_code->has_debug_break_slots()) continue;
1941
1942 Handle<Code> new_code(function->shared()->code());
1943 if (new_code->kind() != Code::FUNCTION ||
1944 !new_code->has_debug_break_slots()) {
1945 continue;
1946 }
1947
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001948 // Iterate over the RelocInfo in the original code to compute the sum of the
1949 // constant pools sizes. (See Assembler::CheckConstPool())
1950 // Note that this is only useful for architectures using constant pools.
1951 int constpool_mask = RelocInfo::ModeMask(RelocInfo::CONST_POOL);
1952 int frame_const_pool_size = 0;
1953 for (RelocIterator it(*frame_code, constpool_mask); !it.done(); it.next()) {
1954 RelocInfo* info = it.rinfo();
1955 if (info->pc() >= frame->pc()) break;
1956 frame_const_pool_size += static_cast<int>(info->data());
1957 }
1958 intptr_t frame_offset =
1959 frame->pc() - frame_code->instruction_start() - frame_const_pool_size;
1960
1961 // Iterate over the RelocInfo for new code to find the number of bytes
1962 // generated for debug slots and constant pools.
1963 int debug_break_slot_bytes = 0;
1964 int new_code_const_pool_size = 0;
1965 int mask = RelocInfo::ModeMask(RelocInfo::DEBUG_BREAK_SLOT) |
1966 RelocInfo::ModeMask(RelocInfo::CONST_POOL);
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001967 for (RelocIterator it(*new_code, mask); !it.done(); it.next()) {
1968 // Check if the pc in the new code with debug break
1969 // slots is before this slot.
1970 RelocInfo* info = it.rinfo();
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001971 intptr_t new_offset = info->pc() - new_code->instruction_start() -
1972 new_code_const_pool_size - debug_break_slot_bytes;
1973 if (new_offset >= frame_offset) {
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001974 break;
1975 }
1976
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001977 if (RelocInfo::IsDebugBreakSlot(info->rmode())) {
1978 debug_break_slot_bytes += Assembler::kDebugBreakSlotLength;
1979 } else {
1980 ASSERT(RelocInfo::IsConstPool(info->rmode()));
1981 // The size of the constant pool is encoded in the data.
1982 new_code_const_pool_size += static_cast<int>(info->data());
1983 }
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001984 }
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001985
1986 // Compute the equivalent pc in the new code.
1987 byte* new_pc = new_code->instruction_start() + frame_offset +
1988 debug_break_slot_bytes + new_code_const_pool_size;
1989
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001990 if (FLAG_trace_deopt) {
1991 PrintF("Replacing code %08" V8PRIxPTR " - %08" V8PRIxPTR " (%d) "
1992 "with %08" V8PRIxPTR " - %08" V8PRIxPTR " (%d) "
1993 "for debugging, "
1994 "changing pc from %08" V8PRIxPTR " to %08" V8PRIxPTR "\n",
1995 reinterpret_cast<intptr_t>(
1996 frame_code->instruction_start()),
1997 reinterpret_cast<intptr_t>(
1998 frame_code->instruction_start()) +
1999 frame_code->instruction_size(),
2000 frame_code->instruction_size(),
2001 reinterpret_cast<intptr_t>(new_code->instruction_start()),
2002 reinterpret_cast<intptr_t>(new_code->instruction_start()) +
2003 new_code->instruction_size(),
2004 new_code->instruction_size(),
2005 reinterpret_cast<intptr_t>(frame->pc()),
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00002006 reinterpret_cast<intptr_t>(new_pc));
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002007 }
2008
2009 // Patch the return address to return into the code with
2010 // debug break slots.
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00002011 frame->set_pc(new_pc);
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002012 }
2013}
2014
2015
2016class ActiveFunctionsCollector : public ThreadVisitor {
2017 public:
2018 explicit ActiveFunctionsCollector(List<Handle<JSFunction> >* active_functions,
2019 Object* active_code_marker)
2020 : active_functions_(active_functions),
2021 active_code_marker_(active_code_marker) { }
2022
2023 void VisitThread(Isolate* isolate, ThreadLocalTop* top) {
2024 CollectActiveFunctionsFromThread(isolate,
2025 top,
2026 active_functions_,
2027 active_code_marker_);
2028 }
2029
2030 private:
2031 List<Handle<JSFunction> >* active_functions_;
2032 Object* active_code_marker_;
2033};
2034
2035
2036class ActiveFunctionsRedirector : public ThreadVisitor {
2037 public:
2038 void VisitThread(Isolate* isolate, ThreadLocalTop* top) {
2039 RedirectActivationsToRecompiledCodeOnThread(isolate, top);
2040 }
2041};
2042
2043
lrn@chromium.org34e60782011-09-15 07:25:40 +00002044void Debug::PrepareForBreakPoints() {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002045 // If preparing for the first break point make sure to deoptimize all
2046 // functions as debugging does not work with optimized code.
2047 if (!has_break_points_) {
svenpanne@chromium.org876cca82013-03-18 14:43:20 +00002048 Deoptimizer::DeoptimizeAll(isolate_);
lrn@chromium.org34e60782011-09-15 07:25:40 +00002049
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002050 Handle<Code> lazy_compile =
2051 Handle<Code>(isolate_->builtins()->builtin(Builtins::kLazyCompile));
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002052
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00002053 // There will be at least one break point when we are done.
2054 has_break_points_ = true;
2055
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002056 // Keep the list of activated functions in a handlified list as it
2057 // is used both in GC and non-GC code.
2058 List<Handle<JSFunction> > active_functions(100);
lrn@chromium.org34e60782011-09-15 07:25:40 +00002059
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002060 {
2061 // We are going to iterate heap to find all functions without
2062 // debug break slots.
hpayer@chromium.org7c3372b2013-02-13 17:26:04 +00002063 Heap* heap = isolate_->heap();
2064 heap->CollectAllGarbage(Heap::kMakeHeapIterableMask,
2065 "preparing for breakpoints");
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002066
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002067 // Ensure no GC in this scope as we are going to use gc_metadata
2068 // field in the Code object to mark active functions.
rossberg@chromium.org79e79022013-06-03 15:43:46 +00002069 DisallowHeapAllocation no_allocation;
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002070
hpayer@chromium.org7c3372b2013-02-13 17:26:04 +00002071 Object* active_code_marker = heap->the_hole_value();
svenpanne@chromium.orgecb9dd62011-12-01 08:22:35 +00002072
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002073 CollectActiveFunctionsFromThread(isolate_,
2074 isolate_->thread_local_top(),
2075 &active_functions,
2076 active_code_marker);
2077 ActiveFunctionsCollector active_functions_collector(&active_functions,
2078 active_code_marker);
2079 isolate_->thread_manager()->IterateArchivedThreads(
2080 &active_functions_collector);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002081
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002082 // Scan the heap for all non-optimized functions which have no
2083 // debug break slots and are not active or inlined into an active
2084 // function and mark them for lazy compilation.
hpayer@chromium.org7c3372b2013-02-13 17:26:04 +00002085 HeapIterator iterator(heap);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002086 HeapObject* obj = NULL;
2087 while (((obj = iterator.next()) != NULL)) {
2088 if (obj->IsJSFunction()) {
2089 JSFunction* function = JSFunction::cast(obj);
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002090 SharedFunctionInfo* shared = function->shared();
mstarzinger@chromium.org1510d582013-06-28 14:00:48 +00002091
2092 if (!shared->allows_lazy_compilation()) continue;
2093 if (!shared->script()->IsScript()) continue;
2094 if (shared->code()->gc_metadata() == active_code_marker) continue;
2095
2096 Code::Kind kind = function->code()->kind();
2097 if (kind == Code::FUNCTION &&
2098 !function->code()->has_debug_break_slots()) {
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002099 function->set_code(*lazy_compile);
2100 function->shared()->set_code(*lazy_compile);
mstarzinger@chromium.org1510d582013-06-28 14:00:48 +00002101 } else if (kind == Code::BUILTIN &&
2102 (function->IsMarkedForInstallingRecompiledCode() ||
2103 function->IsInRecompileQueue() ||
2104 function->IsMarkedForLazyRecompilation() ||
2105 function->IsMarkedForParallelRecompilation())) {
2106 // Abort in-flight compilation.
2107 Code* shared_code = function->shared()->code();
2108 if (shared_code->kind() == Code::FUNCTION &&
2109 shared_code->has_debug_break_slots()) {
2110 function->set_code(shared_code);
2111 } else {
2112 function->set_code(*lazy_compile);
2113 function->shared()->set_code(*lazy_compile);
2114 }
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002115 }
2116 }
lrn@chromium.org34e60782011-09-15 07:25:40 +00002117 }
lrn@chromium.org34e60782011-09-15 07:25:40 +00002118
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002119 // Clear gc_metadata field.
2120 for (int i = 0; i < active_functions.length(); i++) {
2121 Handle<JSFunction> function = active_functions[i];
2122 function->shared()->code()->set_gc_metadata(Smi::FromInt(0));
2123 }
2124 }
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002125
2126 // Now recompile all functions with activation frames and and
2127 // patch the return address to run in the new compiled code.
2128 for (int i = 0; i < active_functions.length(); i++) {
2129 Handle<JSFunction> function = active_functions[i];
mmassi@chromium.org7028c052012-06-13 11:51:58 +00002130 Handle<SharedFunctionInfo> shared(function->shared());
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002131
2132 if (function->code()->kind() == Code::FUNCTION &&
2133 function->code()->has_debug_break_slots()) {
2134 // Nothing to do. Function code already had debug break slots.
2135 continue;
2136 }
2137
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002138 // If recompilation is not possible just skip it.
2139 if (shared->is_toplevel() ||
2140 !shared->allows_lazy_compilation() ||
2141 shared->code()->kind() == Code::BUILTIN) {
2142 continue;
2143 }
2144
2145 // Make sure that the shared full code is compiled with debug
2146 // break slots.
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00002147 if (!shared->code()->has_debug_break_slots()) {
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002148 // Try to compile the full code with debug break slots. If it
2149 // fails just keep the current code.
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00002150 Handle<Code> current_code(function->shared()->code());
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002151 shared->set_code(*lazy_compile);
2152 bool prev_force_debugger_active =
2153 isolate_->debugger()->force_debugger_active();
2154 isolate_->debugger()->set_force_debugger_active(true);
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00002155 ASSERT(current_code->kind() == Code::FUNCTION);
mmassi@chromium.org7028c052012-06-13 11:51:58 +00002156 CompileFullCodeForDebugging(function, current_code);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002157 isolate_->debugger()->set_force_debugger_active(
2158 prev_force_debugger_active);
2159 if (!shared->is_compiled()) {
2160 shared->set_code(*current_code);
2161 continue;
2162 }
2163 }
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002164
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002165 // Keep function code in sync with shared function info.
2166 function->set_code(shared->code());
lrn@chromium.org34e60782011-09-15 07:25:40 +00002167 }
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002168
2169 RedirectActivationsToRecompiledCodeOnThread(isolate_,
2170 isolate_->thread_local_top());
2171
2172 ActiveFunctionsRedirector active_functions_redirector;
2173 isolate_->thread_manager()->IterateArchivedThreads(
2174 &active_functions_redirector);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002175 }
lrn@chromium.org34e60782011-09-15 07:25:40 +00002176}
2177
2178
jkummerow@chromium.org78502a92012-09-06 13:50:42 +00002179Object* Debug::FindSharedFunctionInfoInScript(Handle<Script> script,
2180 int position) {
2181 // Iterate the heap looking for SharedFunctionInfo generated from the
2182 // script. The inner most SharedFunctionInfo containing the source position
2183 // for the requested break point is found.
2184 // NOTE: This might require several heap iterations. If the SharedFunctionInfo
2185 // which is found is not compiled it is compiled and the heap is iterated
2186 // again as the compilation might create inner functions from the newly
2187 // compiled function and the actual requested break point might be in one of
2188 // these functions.
2189 // NOTE: The below fix-point iteration depends on all functions that cannot be
2190 // compiled lazily without a context to not be compiled at all. Compilation
2191 // will be triggered at points where we do not need a context.
2192 bool done = false;
2193 // The current candidate for the source position:
2194 int target_start_position = RelocInfo::kNoPosition;
2195 Handle<JSFunction> target_function;
2196 Handle<SharedFunctionInfo> target;
hpayer@chromium.org7c3372b2013-02-13 17:26:04 +00002197 Heap* heap = isolate_->heap();
jkummerow@chromium.org78502a92012-09-06 13:50:42 +00002198 while (!done) {
2199 { // Extra scope for iterator and no-allocation.
hpayer@chromium.org7c3372b2013-02-13 17:26:04 +00002200 heap->EnsureHeapIsIterable();
rossberg@chromium.org79e79022013-06-03 15:43:46 +00002201 DisallowHeapAllocation no_alloc_during_heap_iteration;
hpayer@chromium.org7c3372b2013-02-13 17:26:04 +00002202 HeapIterator iterator(heap);
jkummerow@chromium.org78502a92012-09-06 13:50:42 +00002203 for (HeapObject* obj = iterator.next();
2204 obj != NULL; obj = iterator.next()) {
2205 bool found_next_candidate = false;
2206 Handle<JSFunction> function;
2207 Handle<SharedFunctionInfo> shared;
2208 if (obj->IsJSFunction()) {
2209 function = Handle<JSFunction>(JSFunction::cast(obj));
2210 shared = Handle<SharedFunctionInfo>(function->shared());
2211 ASSERT(shared->allows_lazy_compilation() || shared->is_compiled());
2212 found_next_candidate = true;
2213 } else if (obj->IsSharedFunctionInfo()) {
2214 shared = Handle<SharedFunctionInfo>(SharedFunctionInfo::cast(obj));
2215 // Skip functions that we cannot compile lazily without a context,
2216 // which is not available here, because there is no closure.
2217 found_next_candidate = shared->is_compiled() ||
2218 shared->allows_lazy_compilation_without_context();
2219 }
2220 if (!found_next_candidate) continue;
2221 if (shared->script() == *script) {
2222 // If the SharedFunctionInfo found has the requested script data and
2223 // contains the source position it is a candidate.
2224 int start_position = shared->function_token_position();
2225 if (start_position == RelocInfo::kNoPosition) {
2226 start_position = shared->start_position();
2227 }
2228 if (start_position <= position &&
2229 position <= shared->end_position()) {
2230 // If there is no candidate or this function is within the current
2231 // candidate this is the new candidate.
2232 if (target.is_null()) {
2233 target_start_position = start_position;
2234 target_function = function;
2235 target = shared;
2236 } else {
2237 if (target_start_position == start_position &&
2238 shared->end_position() == target->end_position()) {
2239 // If a top-level function contains only one function
2240 // declaration the source for the top-level and the function
2241 // is the same. In that case prefer the non top-level function.
2242 if (!shared->is_toplevel()) {
2243 target_start_position = start_position;
2244 target_function = function;
2245 target = shared;
2246 }
2247 } else if (target_start_position <= start_position &&
2248 shared->end_position() <= target->end_position()) {
2249 // This containment check includes equality as a function
2250 // inside a top-level function can share either start or end
2251 // position with the top-level function.
2252 target_start_position = start_position;
2253 target_function = function;
2254 target = shared;
2255 }
2256 }
2257 }
2258 }
2259 } // End for loop.
2260 } // End no-allocation scope.
2261
hpayer@chromium.org7c3372b2013-02-13 17:26:04 +00002262 if (target.is_null()) return heap->undefined_value();
jkummerow@chromium.org78502a92012-09-06 13:50:42 +00002263
2264 // There will be at least one break point when we are done.
2265 has_break_points_ = true;
2266
2267 // If the candidate found is compiled we are done.
2268 done = target->is_compiled();
2269 if (!done) {
2270 // If the candidate is not compiled, compile it to reveal any inner
2271 // functions which might contain the requested source position. This
2272 // will compile all inner functions that cannot be compiled without a
2273 // context, because Compiler::BuildFunctionInfo checks whether the
2274 // debugger is active.
2275 if (target_function.is_null()) {
2276 SharedFunctionInfo::CompileLazy(target, KEEP_EXCEPTION);
2277 } else {
2278 JSFunction::CompileLazy(target_function, KEEP_EXCEPTION);
2279 }
2280 }
2281 } // End while loop.
2282
2283 return *target;
2284}
2285
2286
lrn@chromium.org34e60782011-09-15 07:25:40 +00002287// Ensures the debug information is present for shared.
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00002288bool Debug::EnsureDebugInfo(Handle<SharedFunctionInfo> shared,
2289 Handle<JSFunction> function) {
verwaest@chromium.orgd4be0f02013-06-05 13:39:03 +00002290 Isolate* isolate = shared->GetIsolate();
2291
lrn@chromium.org34e60782011-09-15 07:25:40 +00002292 // Return if we already have the debug info for shared.
2293 if (HasDebugInfo(shared)) {
2294 ASSERT(shared->is_compiled());
2295 return true;
2296 }
2297
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00002298 // There will be at least one break point when we are done.
2299 has_break_points_ = true;
2300
2301 // Ensure function is compiled. Return false if this failed.
2302 if (!function.is_null() &&
2303 !JSFunction::EnsureCompiled(function, CLEAR_EXCEPTION)) {
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002304 return false;
2305 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002306
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002307 // Create the debug info object.
verwaest@chromium.orgd4be0f02013-06-05 13:39:03 +00002308 Handle<DebugInfo> debug_info = isolate->factory()->NewDebugInfo(shared);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002309
2310 // Add debug info to the list.
2311 DebugInfoListNode* node = new DebugInfoListNode(*debug_info);
2312 node->set_next(debug_info_list_);
2313 debug_info_list_ = node;
2314
kasper.lundbd3ec4e2008-07-09 11:06:54 +00002315 return true;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002316}
2317
2318
2319void Debug::RemoveDebugInfo(Handle<DebugInfo> debug_info) {
2320 ASSERT(debug_info_list_ != NULL);
2321 // Run through the debug info objects to find this one and remove it.
2322 DebugInfoListNode* prev = NULL;
2323 DebugInfoListNode* current = debug_info_list_;
2324 while (current != NULL) {
2325 if (*current->debug_info() == *debug_info) {
2326 // Unlink from list. If prev is NULL we are looking at the first element.
2327 if (prev == NULL) {
2328 debug_info_list_ = current->next();
2329 } else {
2330 prev->set_next(current->next());
2331 }
lrn@chromium.org7516f052011-03-30 08:52:27 +00002332 current->debug_info()->shared()->set_debug_info(
2333 isolate_->heap()->undefined_value());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002334 delete current;
2335
2336 // If there are no more debug info objects there are not more break
2337 // points.
2338 has_break_points_ = debug_info_list_ != NULL;
2339
2340 return;
2341 }
2342 // Move to next in list.
2343 prev = current;
2344 current = current->next();
2345 }
2346 UNREACHABLE();
2347}
2348
2349
2350void Debug::SetAfterBreakTarget(JavaScriptFrame* frame) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002351 HandleScope scope(isolate_);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002352
lrn@chromium.org34e60782011-09-15 07:25:40 +00002353 PrepareForBreakPoints();
2354
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002355 // Get the executing function in which the debug break occurred.
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00002356 Handle<JSFunction> function(JSFunction::cast(frame->function()));
2357 Handle<SharedFunctionInfo> shared(function->shared());
2358 if (!EnsureDebugInfo(shared, function)) {
kasper.lundbd3ec4e2008-07-09 11:06:54 +00002359 // Return if we failed to retrieve the debug info.
2360 return;
2361 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002362 Handle<DebugInfo> debug_info = GetDebugInfo(shared);
2363 Handle<Code> code(debug_info->code());
2364 Handle<Code> original_code(debug_info->original_code());
2365#ifdef DEBUG
2366 // Get the code which is actually executing.
vegorov@chromium.org74f333b2011-04-06 11:17:46 +00002367 Handle<Code> frame_code(frame->LookupCode());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002368 ASSERT(frame_code.is_identical_to(code));
2369#endif
2370
2371 // Find the call address in the running code. This address holds the call to
2372 // either a DebugBreakXXX or to the debug break return entry code if the
2373 // break point is still active after processing the break point.
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00002374 Address addr = frame->pc() - Assembler::kPatchDebugBreakSlotReturnOffset;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002375
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002376 // Check if the location is at JS exit or debug break slot.
ager@chromium.orga1645e22009-09-09 19:27:10 +00002377 bool at_js_return = false;
2378 bool break_at_js_return_active = false;
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002379 bool at_debug_break_slot = false;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002380 RelocIterator it(debug_info->code());
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002381 while (!it.done() && !at_js_return && !at_debug_break_slot) {
ager@chromium.org236ad962008-09-25 09:45:57 +00002382 if (RelocInfo::IsJSReturn(it.rinfo()->rmode())) {
ager@chromium.orga1645e22009-09-09 19:27:10 +00002383 at_js_return = (it.rinfo()->pc() ==
2384 addr - Assembler::kPatchReturnSequenceAddressOffset);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002385 break_at_js_return_active = it.rinfo()->IsPatchedReturnSequence();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002386 }
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002387 if (RelocInfo::IsDebugBreakSlot(it.rinfo()->rmode())) {
2388 at_debug_break_slot = (it.rinfo()->pc() ==
2389 addr - Assembler::kPatchDebugBreakSlotAddressOffset);
2390 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002391 it.next();
2392 }
2393
2394 // Handle the jump to continue execution after break point depending on the
2395 // break location.
ager@chromium.orga1645e22009-09-09 19:27:10 +00002396 if (at_js_return) {
2397 // If the break point as return is still active jump to the corresponding
2398 // place in the original code. If not the break point was removed during
2399 // break point processing.
2400 if (break_at_js_return_active) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002401 addr += original_code->instruction_start() - code->instruction_start();
2402 }
2403
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00002404 // Move back to where the call instruction sequence started.
2405 thread_local_.after_break_target_ =
2406 addr - Assembler::kPatchReturnSequenceAddressOffset;
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002407 } else if (at_debug_break_slot) {
2408 // Address of where the debug break slot starts.
2409 addr = addr - Assembler::kPatchDebugBreakSlotAddressOffset;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002410
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002411 // Continue just after the slot.
2412 thread_local_.after_break_target_ = addr + Assembler::kDebugBreakSlotLength;
2413 } else if (IsDebugBreak(Assembler::target_address_at(addr))) {
2414 // We now know that there is still a debug break call at the target address,
2415 // so the break point is still there and the original code will hold the
2416 // address to jump to in order to complete the call which is replaced by a
2417 // call to DebugBreakXXX.
2418
2419 // Find the corresponding address in the original code.
2420 addr += original_code->instruction_start() - code->instruction_start();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002421
2422 // Install jump to the call address in the original code. This will be the
2423 // call which was overwritten by the call to DebugBreakXXX.
2424 thread_local_.after_break_target_ = Assembler::target_address_at(addr);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002425 } else {
2426 // There is no longer a break point present. Don't try to look in the
2427 // original code as the running code will have the right address. This takes
2428 // care of the case where the last break point is removed from the function
2429 // and therefore no "original code" is available.
2430 thread_local_.after_break_target_ = Assembler::target_address_at(addr);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002431 }
2432}
2433
2434
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00002435bool Debug::IsBreakAtReturn(JavaScriptFrame* frame) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002436 HandleScope scope(isolate_);
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00002437
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00002438 // If there are no break points this cannot be break at return, as
2439 // the debugger statement and stack guard bebug break cannot be at
2440 // return.
2441 if (!has_break_points_) {
2442 return false;
2443 }
2444
lrn@chromium.org34e60782011-09-15 07:25:40 +00002445 PrepareForBreakPoints();
2446
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00002447 // Get the executing function in which the debug break occurred.
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00002448 Handle<JSFunction> function(JSFunction::cast(frame->function()));
2449 Handle<SharedFunctionInfo> shared(function->shared());
2450 if (!EnsureDebugInfo(shared, function)) {
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00002451 // Return if we failed to retrieve the debug info.
2452 return false;
2453 }
2454 Handle<DebugInfo> debug_info = GetDebugInfo(shared);
2455 Handle<Code> code(debug_info->code());
2456#ifdef DEBUG
2457 // Get the code which is actually executing.
vegorov@chromium.org74f333b2011-04-06 11:17:46 +00002458 Handle<Code> frame_code(frame->LookupCode());
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00002459 ASSERT(frame_code.is_identical_to(code));
2460#endif
2461
2462 // Find the call address in the running code.
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00002463 Address addr = frame->pc() - Assembler::kPatchDebugBreakSlotReturnOffset;
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00002464
2465 // Check if the location is at JS return.
2466 RelocIterator it(debug_info->code());
2467 while (!it.done()) {
2468 if (RelocInfo::IsJSReturn(it.rinfo()->rmode())) {
2469 return (it.rinfo()->pc() ==
2470 addr - Assembler::kPatchReturnSequenceAddressOffset);
2471 }
2472 it.next();
2473 }
2474 return false;
2475}
2476
2477
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00002478void Debug::FramesHaveBeenDropped(StackFrame::Id new_break_frame_id,
whesse@chromium.orge90029b2010-08-02 11:52:17 +00002479 FrameDropMode mode,
2480 Object** restarter_frame_function_pointer) {
ulan@chromium.orgd9e468a2012-06-25 09:47:40 +00002481 if (mode != CURRENTLY_SET_MODE) {
2482 thread_local_.frame_drop_mode_ = mode;
2483 }
ager@chromium.org357bf652010-04-12 11:30:10 +00002484 thread_local_.break_frame_id_ = new_break_frame_id;
whesse@chromium.orge90029b2010-08-02 11:52:17 +00002485 thread_local_.restarter_frame_function_pointer_ =
2486 restarter_frame_function_pointer;
ager@chromium.org357bf652010-04-12 11:30:10 +00002487}
2488
2489
jkummerow@chromium.org212d9642012-05-11 15:02:09 +00002490const int Debug::FramePaddingLayout::kInitialSize = 1;
2491
2492
2493// Any even value bigger than kInitialSize as needed for stack scanning.
2494const int Debug::FramePaddingLayout::kPaddingValue = kInitialSize + 1;
2495
2496
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002497bool Debug::IsDebugGlobal(GlobalObject* global) {
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00002498 return IsLoaded() && global == debug_context()->global_object();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002499}
2500
2501
ager@chromium.org32912102009-01-16 10:38:43 +00002502void Debug::ClearMirrorCache() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002503 PostponeInterruptsScope postpone(isolate_);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002504 HandleScope scope(isolate_);
2505 ASSERT(isolate_->context() == *Debug::debug_context());
ager@chromium.org32912102009-01-16 10:38:43 +00002506
2507 // Clear the mirror cache.
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00002508 Handle<String> function_name = isolate_->factory()->InternalizeOneByteString(
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00002509 STATIC_ASCII_VECTOR("ClearMirrorCache"));
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00002510 Handle<Object> fun(
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00002511 isolate_->global_object()->GetPropertyNoExceptionThrown(*function_name),
2512 isolate_);
ager@chromium.org32912102009-01-16 10:38:43 +00002513 ASSERT(fun->IsJSFunction());
2514 bool caught_exception;
rossberg@chromium.org717967f2011-07-20 13:44:42 +00002515 Execution::TryCall(Handle<JSFunction>::cast(fun),
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00002516 Handle<JSObject>(Debug::debug_context()->global_object()),
ager@chromium.org32912102009-01-16 10:38:43 +00002517 0, NULL, &caught_exception);
2518}
2519
2520
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002521void Debug::CreateScriptCache() {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002522 Heap* heap = isolate_->heap();
2523 HandleScope scope(isolate_);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002524
2525 // Perform two GCs to get rid of all unreferenced scripts. The first GC gets
2526 // rid of all the cached script wrappers and the second gets rid of the
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002527 // scripts which are no longer referenced. The second also sweeps precisely,
2528 // which saves us doing yet another GC to make the heap iterable.
rossberg@chromium.org994edf62012-02-06 10:12:55 +00002529 heap->CollectAllGarbage(Heap::kNoGCFlags, "Debug::CreateScriptCache");
2530 heap->CollectAllGarbage(Heap::kMakeHeapIterableMask,
2531 "Debug::CreateScriptCache");
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002532
2533 ASSERT(script_cache_ == NULL);
2534 script_cache_ = new ScriptCache();
2535
2536 // Scan heap for Script objects.
2537 int count = 0;
hpayer@chromium.org7c3372b2013-02-13 17:26:04 +00002538 HeapIterator iterator(heap);
rossberg@chromium.org79e79022013-06-03 15:43:46 +00002539 DisallowHeapAllocation no_allocation;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002540
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002541 for (HeapObject* obj = iterator.next(); obj != NULL; obj = iterator.next()) {
sgjesse@chromium.org152a0b02009-10-07 13:50:16 +00002542 if (obj->IsScript() && Script::cast(obj)->HasValidSource()) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002543 script_cache_->Add(Handle<Script>(Script::cast(obj)));
2544 count++;
2545 }
2546 }
2547}
2548
2549
2550void Debug::DestroyScriptCache() {
2551 // Get rid of the script cache if it was created.
2552 if (script_cache_ != NULL) {
2553 delete script_cache_;
2554 script_cache_ = NULL;
2555 }
2556}
2557
2558
2559void Debug::AddScriptToScriptCache(Handle<Script> script) {
2560 if (script_cache_ != NULL) {
2561 script_cache_->Add(script);
2562 }
2563}
2564
2565
2566Handle<FixedArray> Debug::GetLoadedScripts() {
2567 // Create and fill the script cache when the loaded scripts is requested for
2568 // the first time.
2569 if (script_cache_ == NULL) {
2570 CreateScriptCache();
2571 }
2572
2573 // If the script cache is not active just return an empty array.
2574 ASSERT(script_cache_ != NULL);
2575 if (script_cache_ == NULL) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002576 isolate_->factory()->NewFixedArray(0);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002577 }
2578
2579 // Perform GC to get unreferenced scripts evicted from the cache before
2580 // returning the content.
rossberg@chromium.org994edf62012-02-06 10:12:55 +00002581 isolate_->heap()->CollectAllGarbage(Heap::kNoGCFlags,
2582 "Debug::GetLoadedScripts");
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002583
2584 // Get the scripts from the cache.
2585 return script_cache_->GetScripts();
2586}
2587
2588
2589void Debug::AfterGarbageCollection() {
2590 // Generate events for collected scripts.
2591 if (script_cache_ != NULL) {
2592 script_cache_->ProcessCollectedScripts();
2593 }
2594}
2595
2596
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00002597Debugger::Debugger(Isolate* isolate)
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +00002598 : debugger_access_(isolate->debugger_access()),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002599 event_listener_(Handle<Object>()),
2600 event_listener_data_(Handle<Object>()),
2601 compiling_natives_(false),
2602 is_loading_debugger_(false),
mstarzinger@chromium.orgde886792012-09-11 13:22:37 +00002603 live_edit_enabled_(true),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002604 never_unload_debugger_(false),
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002605 force_debugger_active_(false),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002606 message_handler_(NULL),
2607 debugger_unload_pending_(false),
2608 host_dispatch_handler_(NULL),
2609 dispatch_handler_access_(OS::CreateMutex()),
2610 debug_message_dispatch_handler_(NULL),
2611 message_dispatch_helper_thread_(NULL),
2612 host_dispatch_micros_(100 * 1000),
2613 agent_(NULL),
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00002614 command_queue_(isolate->logger(), kQueueInitialSize),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002615 command_received_(OS::CreateSemaphore(0)),
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00002616 event_command_queue_(isolate->logger(), kQueueInitialSize),
2617 isolate_(isolate) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002618}
2619
2620
2621Debugger::~Debugger() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002622 delete dispatch_handler_access_;
2623 dispatch_handler_access_ = 0;
2624 delete command_received_;
2625 command_received_ = 0;
2626}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002627
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002628
2629Handle<Object> Debugger::MakeJSObject(Vector<const char> constructor_name,
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002630 int argc,
2631 Handle<Object> argv[],
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002632 bool* caught_exception) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002633 ASSERT(isolate_->context() == *isolate_->debug()->debug_context());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002634
2635 // Create the execution state object.
lrn@chromium.org7516f052011-03-30 08:52:27 +00002636 Handle<String> constructor_str =
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00002637 isolate_->factory()->InternalizeUtf8String(constructor_name);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002638 Handle<Object> constructor(
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00002639 isolate_->global_object()->GetPropertyNoExceptionThrown(*constructor_str),
2640 isolate_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002641 ASSERT(constructor->IsJSFunction());
2642 if (!constructor->IsJSFunction()) {
2643 *caught_exception = true;
lrn@chromium.org7516f052011-03-30 08:52:27 +00002644 return isolate_->factory()->undefined_value();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002645 }
2646 Handle<Object> js_object = Execution::TryCall(
2647 Handle<JSFunction>::cast(constructor),
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00002648 Handle<JSObject>(isolate_->debug()->debug_context()->global_object()),
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002649 argc,
2650 argv,
2651 caught_exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002652 return js_object;
2653}
2654
2655
2656Handle<Object> Debugger::MakeExecutionState(bool* caught_exception) {
2657 // Create the execution state object.
lrn@chromium.org7516f052011-03-30 08:52:27 +00002658 Handle<Object> break_id = isolate_->factory()->NewNumberFromInt(
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002659 isolate_->debug()->break_id());
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002660 Handle<Object> argv[] = { break_id };
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002661 return MakeJSObject(CStrVector("MakeExecutionState"),
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002662 ARRAY_SIZE(argv),
2663 argv,
2664 caught_exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002665}
2666
2667
2668Handle<Object> Debugger::MakeBreakEvent(Handle<Object> exec_state,
2669 Handle<Object> break_points_hit,
2670 bool* caught_exception) {
2671 // Create the new break event object.
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002672 Handle<Object> argv[] = { exec_state, break_points_hit };
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002673 return MakeJSObject(CStrVector("MakeBreakEvent"),
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002674 ARRAY_SIZE(argv),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002675 argv,
2676 caught_exception);
2677}
2678
2679
2680Handle<Object> Debugger::MakeExceptionEvent(Handle<Object> exec_state,
2681 Handle<Object> exception,
2682 bool uncaught,
2683 bool* caught_exception) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002684 Factory* factory = isolate_->factory();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002685 // Create the new exception event object.
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002686 Handle<Object> argv[] = { exec_state,
2687 exception,
2688 factory->ToBoolean(uncaught) };
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002689 return MakeJSObject(CStrVector("MakeExceptionEvent"),
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002690 ARRAY_SIZE(argv),
2691 argv,
2692 caught_exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002693}
2694
2695
2696Handle<Object> Debugger::MakeNewFunctionEvent(Handle<Object> function,
2697 bool* caught_exception) {
2698 // Create the new function event object.
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002699 Handle<Object> argv[] = { function };
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002700 return MakeJSObject(CStrVector("MakeNewFunctionEvent"),
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002701 ARRAY_SIZE(argv),
2702 argv,
2703 caught_exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002704}
2705
2706
2707Handle<Object> Debugger::MakeCompileEvent(Handle<Script> script,
iposva@chromium.org245aa852009-02-10 00:49:54 +00002708 bool before,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002709 bool* caught_exception) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002710 Factory* factory = isolate_->factory();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002711 // Create the compile event object.
2712 Handle<Object> exec_state = MakeExecutionState(caught_exception);
iposva@chromium.org245aa852009-02-10 00:49:54 +00002713 Handle<Object> script_wrapper = GetScriptWrapper(script);
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002714 Handle<Object> argv[] = { exec_state,
2715 script_wrapper,
2716 factory->ToBoolean(before) };
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002717 return MakeJSObject(CStrVector("MakeCompileEvent"),
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002718 ARRAY_SIZE(argv),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002719 argv,
2720 caught_exception);
2721}
2722
2723
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002724Handle<Object> Debugger::MakeScriptCollectedEvent(int id,
2725 bool* caught_exception) {
2726 // Create the script collected event object.
2727 Handle<Object> exec_state = MakeExecutionState(caught_exception);
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00002728 Handle<Object> id_object = Handle<Smi>(Smi::FromInt(id), isolate_);
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002729 Handle<Object> argv[] = { exec_state, id_object };
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002730
2731 return MakeJSObject(CStrVector("MakeScriptCollectedEvent"),
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002732 ARRAY_SIZE(argv),
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002733 argv,
2734 caught_exception);
2735}
2736
2737
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002738void Debugger::OnException(Handle<Object> exception, bool uncaught) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002739 HandleScope scope(isolate_);
2740 Debug* debug = isolate_->debug();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002741
2742 // Bail out based on state or if there is no listener for this event
lrn@chromium.org7516f052011-03-30 08:52:27 +00002743 if (debug->InDebugger()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002744 if (!Debugger::EventActive(v8::Exception)) return;
2745
2746 // Bail out if exception breaks are not active
2747 if (uncaught) {
2748 // Uncaught exceptions are reported by either flags.
lrn@chromium.org7516f052011-03-30 08:52:27 +00002749 if (!(debug->break_on_uncaught_exception() ||
2750 debug->break_on_exception())) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002751 } else {
2752 // Caught exceptions are reported is activated.
lrn@chromium.org7516f052011-03-30 08:52:27 +00002753 if (!debug->break_on_exception()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002754 }
2755
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002756 // Enter the debugger.
2757 EnterDebugger debugger;
2758 if (debugger.FailedToEnter()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002759
2760 // Clear all current stepping setup.
lrn@chromium.org7516f052011-03-30 08:52:27 +00002761 debug->ClearStepping();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002762 // Create the event data object.
2763 bool caught_exception = false;
2764 Handle<Object> exec_state = MakeExecutionState(&caught_exception);
2765 Handle<Object> event_data;
2766 if (!caught_exception) {
2767 event_data = MakeExceptionEvent(exec_state, exception, uncaught,
2768 &caught_exception);
2769 }
2770 // Bail out and don't call debugger if exception.
2771 if (caught_exception) {
2772 return;
2773 }
2774
ager@chromium.org5ec48922009-05-05 07:25:34 +00002775 // Process debug event.
2776 ProcessDebugEvent(v8::Exception, Handle<JSObject>::cast(event_data), false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002777 // Return to continue execution from where the exception was thrown.
2778}
2779
2780
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002781void Debugger::OnDebugBreak(Handle<Object> break_points_hit,
2782 bool auto_continue) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002783 HandleScope scope(isolate_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002784
kasper.lund212ac232008-07-16 07:07:30 +00002785 // Debugger has already been entered by caller.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002786 ASSERT(isolate_->context() == *isolate_->debug()->debug_context());
kasper.lund212ac232008-07-16 07:07:30 +00002787
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002788 // Bail out if there is no listener for this event
2789 if (!Debugger::EventActive(v8::Break)) return;
2790
2791 // Debugger must be entered in advance.
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00002792 ASSERT(isolate_->context() == *isolate_->debug()->debug_context());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002793
2794 // Create the event data object.
2795 bool caught_exception = false;
2796 Handle<Object> exec_state = MakeExecutionState(&caught_exception);
2797 Handle<Object> event_data;
2798 if (!caught_exception) {
2799 event_data = MakeBreakEvent(exec_state, break_points_hit,
2800 &caught_exception);
2801 }
2802 // Bail out and don't call debugger if exception.
2803 if (caught_exception) {
2804 return;
2805 }
2806
ager@chromium.org5ec48922009-05-05 07:25:34 +00002807 // Process debug event.
2808 ProcessDebugEvent(v8::Break,
2809 Handle<JSObject>::cast(event_data),
2810 auto_continue);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002811}
2812
2813
2814void Debugger::OnBeforeCompile(Handle<Script> script) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002815 HandleScope scope(isolate_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002816
2817 // Bail out based on state or if there is no listener for this event
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002818 if (isolate_->debug()->InDebugger()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002819 if (compiling_natives()) return;
2820 if (!EventActive(v8::BeforeCompile)) return;
2821
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002822 // Enter the debugger.
2823 EnterDebugger debugger;
2824 if (debugger.FailedToEnter()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002825
2826 // Create the event data object.
2827 bool caught_exception = false;
iposva@chromium.org245aa852009-02-10 00:49:54 +00002828 Handle<Object> event_data = MakeCompileEvent(script, true, &caught_exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002829 // Bail out and don't call debugger if exception.
2830 if (caught_exception) {
2831 return;
2832 }
2833
ager@chromium.org5ec48922009-05-05 07:25:34 +00002834 // Process debug event.
2835 ProcessDebugEvent(v8::BeforeCompile,
2836 Handle<JSObject>::cast(event_data),
2837 true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002838}
2839
2840
2841// Handle debugger actions when a new script is compiled.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00002842void Debugger::OnAfterCompile(Handle<Script> script,
2843 AfterCompileFlags after_compile_flags) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002844 HandleScope scope(isolate_);
2845 Debug* debug = isolate_->debug();
kasper.lund212ac232008-07-16 07:07:30 +00002846
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002847 // Add the newly compiled script to the script cache.
lrn@chromium.org7516f052011-03-30 08:52:27 +00002848 debug->AddScriptToScriptCache(script);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002849
2850 // No more to do if not debugging.
ager@chromium.org71daaf62009-04-01 07:22:49 +00002851 if (!IsDebuggerActive()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002852
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002853 // No compile events while compiling natives.
2854 if (compiling_natives()) return;
2855
iposva@chromium.org245aa852009-02-10 00:49:54 +00002856 // Store whether in debugger before entering debugger.
lrn@chromium.org7516f052011-03-30 08:52:27 +00002857 bool in_debugger = debug->InDebugger();
iposva@chromium.org245aa852009-02-10 00:49:54 +00002858
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002859 // Enter the debugger.
2860 EnterDebugger debugger;
2861 if (debugger.FailedToEnter()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002862
2863 // If debugging there might be script break points registered for this
2864 // script. Make sure that these break points are set.
2865
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002866 // Get the function UpdateScriptBreakPoints (defined in debug-debugger.js).
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00002867 Handle<String> update_script_break_points_string =
2868 isolate_->factory()->InternalizeOneByteString(
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00002869 STATIC_ASCII_VECTOR("UpdateScriptBreakPoints"));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002870 Handle<Object> update_script_break_points =
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00002871 Handle<Object>(
2872 debug->debug_context()->global_object()->GetPropertyNoExceptionThrown(
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00002873 *update_script_break_points_string),
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00002874 isolate_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002875 if (!update_script_break_points->IsJSFunction()) {
2876 return;
2877 }
2878 ASSERT(update_script_break_points->IsJSFunction());
2879
2880 // Wrap the script object in a proper JS object before passing it
2881 // to JavaScript.
2882 Handle<JSValue> wrapper = GetScriptWrapper(script);
2883
2884 // Call UpdateScriptBreakPoints expect no exceptions.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002885 bool caught_exception;
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002886 Handle<Object> argv[] = { wrapper };
rossberg@chromium.org717967f2011-07-20 13:44:42 +00002887 Execution::TryCall(Handle<JSFunction>::cast(update_script_break_points),
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002888 Isolate::Current()->js_builtins_object(),
2889 ARRAY_SIZE(argv),
2890 argv,
2891 &caught_exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002892 if (caught_exception) {
2893 return;
2894 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002895 // Bail out based on state or if there is no listener for this event
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00002896 if (in_debugger && (after_compile_flags & SEND_WHEN_DEBUGGING) == 0) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002897 if (!Debugger::EventActive(v8::AfterCompile)) return;
2898
2899 // Create the compile state object.
2900 Handle<Object> event_data = MakeCompileEvent(script,
iposva@chromium.org245aa852009-02-10 00:49:54 +00002901 false,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002902 &caught_exception);
2903 // Bail out and don't call debugger if exception.
2904 if (caught_exception) {
2905 return;
2906 }
ager@chromium.org5ec48922009-05-05 07:25:34 +00002907 // Process debug event.
2908 ProcessDebugEvent(v8::AfterCompile,
2909 Handle<JSObject>::cast(event_data),
2910 true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002911}
2912
2913
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002914void Debugger::OnScriptCollected(int id) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002915 HandleScope scope(isolate_);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002916
2917 // No more to do if not debugging.
ulan@chromium.org56c14af2012-09-20 12:51:09 +00002918 if (isolate_->debug()->InDebugger()) return;
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002919 if (!IsDebuggerActive()) return;
2920 if (!Debugger::EventActive(v8::ScriptCollected)) return;
2921
2922 // Enter the debugger.
2923 EnterDebugger debugger;
2924 if (debugger.FailedToEnter()) return;
2925
2926 // Create the script collected state object.
2927 bool caught_exception = false;
2928 Handle<Object> event_data = MakeScriptCollectedEvent(id,
2929 &caught_exception);
2930 // Bail out and don't call debugger if exception.
2931 if (caught_exception) {
2932 return;
2933 }
2934
2935 // Process debug event.
2936 ProcessDebugEvent(v8::ScriptCollected,
2937 Handle<JSObject>::cast(event_data),
2938 true);
2939}
2940
2941
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002942void Debugger::ProcessDebugEvent(v8::DebugEvent event,
ager@chromium.org5ec48922009-05-05 07:25:34 +00002943 Handle<JSObject> event_data,
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002944 bool auto_continue) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002945 HandleScope scope(isolate_);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002946
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +00002947 // Clear any pending debug break if this is a real break.
2948 if (!auto_continue) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002949 isolate_->debug()->clear_interrupt_pending(DEBUGBREAK);
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +00002950 }
2951
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002952 // Create the execution state.
2953 bool caught_exception = false;
2954 Handle<Object> exec_state = MakeExecutionState(&caught_exception);
2955 if (caught_exception) {
2956 return;
2957 }
ager@chromium.org41826e72009-03-30 13:30:57 +00002958 // First notify the message handler if any.
2959 if (message_handler_ != NULL) {
ager@chromium.org5ec48922009-05-05 07:25:34 +00002960 NotifyMessageHandler(event,
2961 Handle<JSObject>::cast(exec_state),
2962 event_data,
2963 auto_continue);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002964 }
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00002965 // Notify registered debug event listener. This can be either a C or
2966 // a JavaScript function. Don't call event listener for v8::Break
2967 // here, if it's only a debug command -- they will be processed later.
2968 if ((event != v8::Break || !auto_continue) && !event_listener_.is_null()) {
2969 CallEventCallback(event, exec_state, event_data, NULL);
2970 }
2971 // Process pending debug commands.
2972 if (event == v8::Break) {
2973 while (!event_command_queue_.IsEmpty()) {
2974 CommandMessage command = event_command_queue_.Get();
2975 if (!event_listener_.is_null()) {
2976 CallEventCallback(v8::BreakForCommand,
2977 exec_state,
2978 event_data,
2979 command.client_data());
2980 }
2981 command.Dispose();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002982 }
2983 }
2984}
2985
2986
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00002987void Debugger::CallEventCallback(v8::DebugEvent event,
2988 Handle<Object> exec_state,
2989 Handle<Object> event_data,
2990 v8::Debug::ClientData* client_data) {
ager@chromium.orgea91cc52011-05-23 06:06:11 +00002991 if (event_listener_->IsForeign()) {
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00002992 CallCEventCallback(event, exec_state, event_data, client_data);
2993 } else {
2994 CallJSEventCallback(event, exec_state, event_data);
2995 }
2996}
2997
2998
2999void Debugger::CallCEventCallback(v8::DebugEvent event,
3000 Handle<Object> exec_state,
3001 Handle<Object> event_data,
3002 v8::Debug::ClientData* client_data) {
ager@chromium.orgea91cc52011-05-23 06:06:11 +00003003 Handle<Foreign> callback_obj(Handle<Foreign>::cast(event_listener_));
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00003004 v8::Debug::EventCallback2 callback =
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003005 FUNCTION_CAST<v8::Debug::EventCallback2>(
3006 callback_obj->foreign_address());
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00003007 EventDetailsImpl event_details(
3008 event,
3009 Handle<JSObject>::cast(exec_state),
3010 Handle<JSObject>::cast(event_data),
3011 event_listener_data_,
3012 client_data);
3013 callback(event_details);
3014}
3015
3016
3017void Debugger::CallJSEventCallback(v8::DebugEvent event,
3018 Handle<Object> exec_state,
3019 Handle<Object> event_data) {
3020 ASSERT(event_listener_->IsJSFunction());
3021 Handle<JSFunction> fun(Handle<JSFunction>::cast(event_listener_));
3022
3023 // Invoke the JavaScript debug event listener.
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00003024 Handle<Object> argv[] = { Handle<Object>(Smi::FromInt(event), isolate_),
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00003025 exec_state,
3026 event_data,
3027 event_listener_data_ };
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003028 bool caught_exception;
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00003029 Execution::TryCall(fun,
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00003030 isolate_->global_object(),
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00003031 ARRAY_SIZE(argv),
3032 argv,
3033 &caught_exception);
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00003034 // Silently ignore exceptions from debug event listeners.
3035}
3036
3037
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00003038Handle<Context> Debugger::GetDebugContext() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003039 never_unload_debugger_ = true;
3040 EnterDebugger debugger;
3041 return isolate_->debug()->debug_context();
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00003042}
3043
3044
ager@chromium.org71daaf62009-04-01 07:22:49 +00003045void Debugger::UnloadDebugger() {
lrn@chromium.org7516f052011-03-30 08:52:27 +00003046 Debug* debug = isolate_->debug();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003047
ager@chromium.org71daaf62009-04-01 07:22:49 +00003048 // Make sure that there are no breakpoints left.
lrn@chromium.org7516f052011-03-30 08:52:27 +00003049 debug->ClearAllBreakPoints();
ager@chromium.org71daaf62009-04-01 07:22:49 +00003050
3051 // Unload the debugger if feasible.
3052 if (!never_unload_debugger_) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00003053 debug->Unload();
ager@chromium.org71daaf62009-04-01 07:22:49 +00003054 }
3055
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003056 // Clear the flag indicating that the debugger should be unloaded.
3057 debugger_unload_pending_ = false;
ager@chromium.org71daaf62009-04-01 07:22:49 +00003058}
3059
3060
ager@chromium.org41826e72009-03-30 13:30:57 +00003061void Debugger::NotifyMessageHandler(v8::DebugEvent event,
ager@chromium.org5ec48922009-05-05 07:25:34 +00003062 Handle<JSObject> exec_state,
3063 Handle<JSObject> event_data,
ager@chromium.org41826e72009-03-30 13:30:57 +00003064 bool auto_continue) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00003065 HandleScope scope(isolate_);
ager@chromium.org41826e72009-03-30 13:30:57 +00003066
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003067 if (!isolate_->debug()->Load()) return;
ager@chromium.org41826e72009-03-30 13:30:57 +00003068
3069 // Process the individual events.
ager@chromium.org5ec48922009-05-05 07:25:34 +00003070 bool sendEventMessage = false;
ager@chromium.org41826e72009-03-30 13:30:57 +00003071 switch (event) {
3072 case v8::Break:
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00003073 case v8::BreakForCommand:
ager@chromium.org5ec48922009-05-05 07:25:34 +00003074 sendEventMessage = !auto_continue;
ager@chromium.org41826e72009-03-30 13:30:57 +00003075 break;
3076 case v8::Exception:
ager@chromium.org5ec48922009-05-05 07:25:34 +00003077 sendEventMessage = true;
ager@chromium.org41826e72009-03-30 13:30:57 +00003078 break;
3079 case v8::BeforeCompile:
3080 break;
3081 case v8::AfterCompile:
ager@chromium.org5ec48922009-05-05 07:25:34 +00003082 sendEventMessage = true;
ager@chromium.org41826e72009-03-30 13:30:57 +00003083 break;
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003084 case v8::ScriptCollected:
3085 sendEventMessage = true;
3086 break;
ager@chromium.org41826e72009-03-30 13:30:57 +00003087 case v8::NewFunction:
3088 break;
3089 default:
3090 UNREACHABLE();
3091 }
3092
ager@chromium.org5ec48922009-05-05 07:25:34 +00003093 // The debug command interrupt flag might have been set when the command was
3094 // added. It should be enough to clear the flag only once while we are in the
3095 // debugger.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003096 ASSERT(isolate_->debug()->InDebugger());
3097 isolate_->stack_guard()->Continue(DEBUGCOMMAND);
ager@chromium.org5ec48922009-05-05 07:25:34 +00003098
3099 // Notify the debugger that a debug event has occurred unless auto continue is
3100 // active in which case no event is send.
3101 if (sendEventMessage) {
3102 MessageImpl message = MessageImpl::NewEvent(
3103 event,
3104 auto_continue,
3105 Handle<JSObject>::cast(exec_state),
3106 Handle<JSObject>::cast(event_data));
3107 InvokeMessageHandler(message);
3108 }
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +00003109
3110 // If auto continue don't make the event cause a break, but process messages
3111 // in the queue if any. For script collected events don't even process
3112 // messages in the queue as the execution state might not be what is expected
3113 // by the client.
ager@chromium.org6ffc2172009-05-29 19:20:16 +00003114 if ((auto_continue && !HasCommands()) || event == v8::ScriptCollected) {
ager@chromium.org5ec48922009-05-05 07:25:34 +00003115 return;
3116 }
ager@chromium.org41826e72009-03-30 13:30:57 +00003117
ager@chromium.org41826e72009-03-30 13:30:57 +00003118 v8::TryCatch try_catch;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003119
3120 // DebugCommandProcessor goes here.
3121 v8::Local<v8::Object> cmd_processor;
3122 {
3123 v8::Local<v8::Object> api_exec_state =
3124 v8::Utils::ToLocal(Handle<JSObject>::cast(exec_state));
3125 v8::Local<v8::String> fun_name =
3126 v8::String::New("debugCommandProcessor");
3127 v8::Local<v8::Function> fun =
danno@chromium.orgf95d4b92013-06-13 14:40:17 +00003128 v8::Local<v8::Function>::Cast(api_exec_state->Get(fun_name));
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003129
3130 v8::Handle<v8::Boolean> running =
3131 auto_continue ? v8::True() : v8::False();
3132 static const int kArgc = 1;
3133 v8::Handle<Value> argv[kArgc] = { running };
danno@chromium.orgf95d4b92013-06-13 14:40:17 +00003134 cmd_processor = v8::Local<v8::Object>::Cast(
3135 fun->Call(api_exec_state, kArgc, argv));
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003136 if (try_catch.HasCaught()) {
3137 PrintLn(try_catch.Exception());
3138 return;
3139 }
ager@chromium.org41826e72009-03-30 13:30:57 +00003140 }
3141
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003142 bool running = auto_continue;
3143
ager@chromium.org41826e72009-03-30 13:30:57 +00003144 // Process requests from the debugger.
3145 while (true) {
3146 // Wait for new command in the queue.
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003147 if (Debugger::host_dispatch_handler_) {
3148 // In case there is a host dispatch - do periodic dispatches.
3149 if (!command_received_->Wait(host_dispatch_micros_)) {
3150 // Timout expired, do the dispatch.
3151 Debugger::host_dispatch_handler_();
3152 continue;
3153 }
3154 } else {
3155 // In case there is no host dispatch - just wait.
3156 command_received_->Wait();
3157 }
ager@chromium.org41826e72009-03-30 13:30:57 +00003158
ager@chromium.org41826e72009-03-30 13:30:57 +00003159 // Get the command from the queue.
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003160 CommandMessage command = command_queue_.Get();
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00003161 isolate_->logger()->DebugTag(
3162 "Got request from command queue, in interactive loop.");
ager@chromium.org71daaf62009-04-01 07:22:49 +00003163 if (!Debugger::IsDebuggerActive()) {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003164 // Delete command text and user data.
3165 command.Dispose();
ager@chromium.org41826e72009-03-30 13:30:57 +00003166 return;
3167 }
3168
ager@chromium.org41826e72009-03-30 13:30:57 +00003169 // Invoke JavaScript to process the debug request.
3170 v8::Local<v8::String> fun_name;
3171 v8::Local<v8::Function> fun;
3172 v8::Local<v8::Value> request;
3173 v8::TryCatch try_catch;
3174 fun_name = v8::String::New("processDebugRequest");
danno@chromium.orgf95d4b92013-06-13 14:40:17 +00003175 fun = v8::Local<v8::Function>::Cast(cmd_processor->Get(fun_name));
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003176
3177 request = v8::String::New(command.text().start(),
3178 command.text().length());
ager@chromium.org41826e72009-03-30 13:30:57 +00003179 static const int kArgc = 1;
3180 v8::Handle<Value> argv[kArgc] = { request };
3181 v8::Local<v8::Value> response_val = fun->Call(cmd_processor, kArgc, argv);
3182
3183 // Get the response.
3184 v8::Local<v8::String> response;
ager@chromium.org41826e72009-03-30 13:30:57 +00003185 if (!try_catch.HasCaught()) {
3186 // Get response string.
3187 if (!response_val->IsUndefined()) {
danno@chromium.orgf95d4b92013-06-13 14:40:17 +00003188 response = v8::Local<v8::String>::Cast(response_val);
ager@chromium.org41826e72009-03-30 13:30:57 +00003189 } else {
3190 response = v8::String::New("");
3191 }
3192
3193 // Log the JSON request/response.
3194 if (FLAG_trace_debug_json) {
3195 PrintLn(request);
3196 PrintLn(response);
3197 }
3198
3199 // Get the running state.
3200 fun_name = v8::String::New("isRunning");
danno@chromium.orgf95d4b92013-06-13 14:40:17 +00003201 fun = v8::Local<v8::Function>::Cast(cmd_processor->Get(fun_name));
ager@chromium.org41826e72009-03-30 13:30:57 +00003202 static const int kArgc = 1;
3203 v8::Handle<Value> argv[kArgc] = { response };
3204 v8::Local<v8::Value> running_val = fun->Call(cmd_processor, kArgc, argv);
3205 if (!try_catch.HasCaught()) {
3206 running = running_val->ToBoolean()->Value();
3207 }
3208 } else {
3209 // In case of failure the result text is the exception text.
3210 response = try_catch.Exception()->ToString();
3211 }
3212
ager@chromium.org41826e72009-03-30 13:30:57 +00003213 // Return the result.
ager@chromium.org5ec48922009-05-05 07:25:34 +00003214 MessageImpl message = MessageImpl::NewResponse(
3215 event,
3216 running,
3217 Handle<JSObject>::cast(exec_state),
3218 Handle<JSObject>::cast(event_data),
3219 Handle<String>(Utils::OpenHandle(*response)),
3220 command.client_data());
3221 InvokeMessageHandler(message);
3222 command.Dispose();
ager@chromium.org41826e72009-03-30 13:30:57 +00003223
3224 // Return from debug event processing if either the VM is put into the
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00003225 // running state (through a continue command) or auto continue is active
ager@chromium.org41826e72009-03-30 13:30:57 +00003226 // and there are no more commands queued.
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003227 if (running && !HasCommands()) {
ager@chromium.org41826e72009-03-30 13:30:57 +00003228 return;
3229 }
3230 }
3231}
3232
3233
iposva@chromium.org245aa852009-02-10 00:49:54 +00003234void Debugger::SetEventListener(Handle<Object> callback,
3235 Handle<Object> data) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00003236 HandleScope scope(isolate_);
3237 GlobalHandles* global_handles = isolate_->global_handles();
iposva@chromium.org245aa852009-02-10 00:49:54 +00003238
3239 // Clear the global handles for the event listener and the event listener data
3240 // object.
3241 if (!event_listener_.is_null()) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00003242 global_handles->Destroy(
iposva@chromium.org245aa852009-02-10 00:49:54 +00003243 reinterpret_cast<Object**>(event_listener_.location()));
3244 event_listener_ = Handle<Object>();
3245 }
3246 if (!event_listener_data_.is_null()) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00003247 global_handles->Destroy(
iposva@chromium.org245aa852009-02-10 00:49:54 +00003248 reinterpret_cast<Object**>(event_listener_data_.location()));
3249 event_listener_data_ = Handle<Object>();
3250 }
3251
3252 // If there is a new debug event listener register it together with its data
3253 // object.
3254 if (!callback->IsUndefined() && !callback->IsNull()) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003255 event_listener_ = Handle<Object>::cast(
lrn@chromium.org7516f052011-03-30 08:52:27 +00003256 global_handles->Create(*callback));
iposva@chromium.org245aa852009-02-10 00:49:54 +00003257 if (data.is_null()) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00003258 data = isolate_->factory()->undefined_value();
iposva@chromium.org245aa852009-02-10 00:49:54 +00003259 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003260 event_listener_data_ = Handle<Object>::cast(
lrn@chromium.org7516f052011-03-30 08:52:27 +00003261 global_handles->Create(*data));
iposva@chromium.org245aa852009-02-10 00:49:54 +00003262 }
3263
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003264 ListenersChanged();
iposva@chromium.org245aa852009-02-10 00:49:54 +00003265}
3266
3267
ager@chromium.org5ec48922009-05-05 07:25:34 +00003268void Debugger::SetMessageHandler(v8::Debug::MessageHandler2 handler) {
ager@chromium.org71daaf62009-04-01 07:22:49 +00003269 ScopedLock with(debugger_access_);
3270
ager@chromium.org381abbb2009-02-25 13:23:22 +00003271 message_handler_ = handler;
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003272 ListenersChanged();
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003273 if (handler == NULL) {
ager@chromium.org71daaf62009-04-01 07:22:49 +00003274 // Send an empty command to the debugger if in a break to make JavaScript
3275 // run again if the debugger is closed.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003276 if (isolate_->debug()->InDebugger()) {
ager@chromium.org71daaf62009-04-01 07:22:49 +00003277 ProcessCommand(Vector<const uint16_t>::empty());
3278 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003279 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003280}
3281
3282
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003283void Debugger::ListenersChanged() {
3284 if (IsDebuggerActive()) {
3285 // Disable the compilation cache when the debugger is active.
lrn@chromium.org7516f052011-03-30 08:52:27 +00003286 isolate_->compilation_cache()->Disable();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003287 debugger_unload_pending_ = false;
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003288 } else {
lrn@chromium.org7516f052011-03-30 08:52:27 +00003289 isolate_->compilation_cache()->Enable();
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003290 // Unload the debugger if event listener and message handler cleared.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003291 // Schedule this for later, because we may be in non-V8 thread.
3292 debugger_unload_pending_ = true;
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003293 }
3294}
3295
3296
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003297void Debugger::SetHostDispatchHandler(v8::Debug::HostDispatchHandler handler,
3298 int period) {
ager@chromium.org381abbb2009-02-25 13:23:22 +00003299 host_dispatch_handler_ = handler;
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003300 host_dispatch_micros_ = period * 1000;
ager@chromium.org381abbb2009-02-25 13:23:22 +00003301}
3302
3303
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003304void Debugger::SetDebugMessageDispatchHandler(
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003305 v8::Debug::DebugMessageDispatchHandler handler, bool provide_locker) {
3306 ScopedLock with(dispatch_handler_access_);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003307 debug_message_dispatch_handler_ = handler;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003308
3309 if (provide_locker && message_dispatch_helper_thread_ == NULL) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003310 message_dispatch_helper_thread_ = new MessageDispatchHelperThread(isolate_);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003311 message_dispatch_helper_thread_->Start();
3312 }
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003313}
3314
3315
ager@chromium.org41826e72009-03-30 13:30:57 +00003316// Calls the registered debug message handler. This callback is part of the
ager@chromium.org5ec48922009-05-05 07:25:34 +00003317// public API.
3318void Debugger::InvokeMessageHandler(MessageImpl message) {
ager@chromium.org71daaf62009-04-01 07:22:49 +00003319 ScopedLock with(debugger_access_);
3320
ager@chromium.org381abbb2009-02-25 13:23:22 +00003321 if (message_handler_ != NULL) {
ager@chromium.org5ec48922009-05-05 07:25:34 +00003322 message_handler_(message);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003323 }
ager@chromium.org41826e72009-03-30 13:30:57 +00003324}
3325
3326
3327// Puts a command coming from the public API on the queue. Creates
3328// a copy of the command string managed by the debugger. Up to this
3329// point, the command data was managed by the API client. Called
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003330// by the API client thread.
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003331void Debugger::ProcessCommand(Vector<const uint16_t> command,
3332 v8::Debug::ClientData* client_data) {
3333 // Need to cast away const.
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003334 CommandMessage message = CommandMessage::New(
ager@chromium.org41826e72009-03-30 13:30:57 +00003335 Vector<uint16_t>(const_cast<uint16_t*>(command.start()),
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003336 command.length()),
3337 client_data);
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00003338 isolate_->logger()->DebugTag("Put command on command_queue.");
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003339 command_queue_.Put(message);
ager@chromium.org41826e72009-03-30 13:30:57 +00003340 command_received_->Signal();
sgjesse@chromium.org3afc1582009-04-16 22:31:44 +00003341
3342 // Set the debug command break flag to have the command processed.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003343 if (!isolate_->debug()->InDebugger()) {
3344 isolate_->stack_guard()->DebugCommand();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003345 }
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003346
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003347 MessageDispatchHelperThread* dispatch_thread;
3348 {
3349 ScopedLock with(dispatch_handler_access_);
3350 dispatch_thread = message_dispatch_helper_thread_;
3351 }
3352
3353 if (dispatch_thread == NULL) {
3354 CallMessageDispatchHandler();
3355 } else {
3356 dispatch_thread->Schedule();
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003357 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003358}
3359
3360
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003361bool Debugger::HasCommands() {
ager@chromium.org41826e72009-03-30 13:30:57 +00003362 return !command_queue_.IsEmpty();
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003363}
3364
3365
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00003366void Debugger::EnqueueDebugCommand(v8::Debug::ClientData* client_data) {
3367 CommandMessage message = CommandMessage::New(Vector<uint16_t>(), client_data);
3368 event_command_queue_.Put(message);
3369
3370 // Set the debug command break flag to have the command processed.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003371 if (!isolate_->debug()->InDebugger()) {
3372 isolate_->stack_guard()->DebugCommand();
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00003373 }
3374}
3375
3376
ager@chromium.org71daaf62009-04-01 07:22:49 +00003377bool Debugger::IsDebuggerActive() {
3378 ScopedLock with(debugger_access_);
3379
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00003380 return message_handler_ != NULL ||
3381 !event_listener_.is_null() ||
3382 force_debugger_active_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003383}
3384
3385
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003386Handle<Object> Debugger::Call(Handle<JSFunction> fun,
3387 Handle<Object> data,
3388 bool* pending_exception) {
ager@chromium.org71daaf62009-04-01 07:22:49 +00003389 // When calling functions in the debugger prevent it from beeing unloaded.
3390 Debugger::never_unload_debugger_ = true;
3391
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003392 // Enter the debugger.
3393 EnterDebugger debugger;
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +00003394 if (debugger.FailedToEnter()) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00003395 return isolate_->factory()->undefined_value();
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003396 }
3397
3398 // Create the execution state.
3399 bool caught_exception = false;
3400 Handle<Object> exec_state = MakeExecutionState(&caught_exception);
3401 if (caught_exception) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00003402 return isolate_->factory()->undefined_value();
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003403 }
3404
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00003405 Handle<Object> argv[] = { exec_state, data };
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +00003406 Handle<Object> result = Execution::Call(
3407 fun,
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00003408 Handle<Object>(isolate_->debug()->debug_context_->global_proxy(),
3409 isolate_),
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00003410 ARRAY_SIZE(argv),
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +00003411 argv,
3412 pending_exception);
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003413 return result;
3414}
3415
3416
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003417static void StubMessageHandler2(const v8::Debug::Message& message) {
3418 // Simply ignore message.
3419}
3420
3421
3422bool Debugger::StartAgent(const char* name, int port,
3423 bool wait_for_connection) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003424 ASSERT(Isolate::Current() == isolate_);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003425 if (wait_for_connection) {
3426 // Suspend V8 if it is already running or set V8 to suspend whenever
3427 // it starts.
3428 // Provide stub message handler; V8 auto-continues each suspend
3429 // when there is no message handler; we doesn't need it.
3430 // Once become suspended, V8 will stay so indefinitely long, until remote
3431 // debugger connects and issues "continue" command.
3432 Debugger::message_handler_ = StubMessageHandler2;
3433 v8::Debug::DebugBreak();
3434 }
3435
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00003436 if (Socket::SetUp()) {
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00003437 if (agent_ == NULL) {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00003438 agent_ = new DebuggerAgent(name, port);
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00003439 agent_->Start();
3440 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003441 return true;
3442 }
3443
3444 return false;
3445}
3446
3447
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003448void Debugger::StopAgent() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003449 ASSERT(Isolate::Current() == isolate_);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003450 if (agent_ != NULL) {
3451 agent_->Shutdown();
3452 agent_->Join();
3453 delete agent_;
3454 agent_ = NULL;
3455 }
3456}
3457
3458
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003459void Debugger::WaitForAgent() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003460 ASSERT(Isolate::Current() == isolate_);
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003461 if (agent_ != NULL)
3462 agent_->WaitUntilListening();
3463}
3464
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003465
3466void Debugger::CallMessageDispatchHandler() {
3467 v8::Debug::DebugMessageDispatchHandler handler;
3468 {
3469 ScopedLock with(dispatch_handler_access_);
3470 handler = Debugger::debug_message_dispatch_handler_;
3471 }
3472 if (handler != NULL) {
3473 handler();
3474 }
3475}
3476
3477
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003478EnterDebugger::EnterDebugger()
3479 : isolate_(Isolate::Current()),
3480 prev_(isolate_->debug()->debugger_entry()),
3481 it_(isolate_),
3482 has_js_frames_(!it_.done()),
3483 save_(isolate_) {
3484 Debug* debug = isolate_->debug();
3485 ASSERT(prev_ != NULL || !debug->is_interrupt_pending(PREEMPT));
3486 ASSERT(prev_ != NULL || !debug->is_interrupt_pending(DEBUGBREAK));
3487
3488 // Link recursive debugger entry.
3489 debug->set_debugger_entry(this);
3490
3491 // Store the previous break id and frame id.
3492 break_id_ = debug->break_id();
3493 break_frame_id_ = debug->break_frame_id();
3494
3495 // Create the new break info. If there is no JavaScript frames there is no
3496 // break frame id.
3497 if (has_js_frames_) {
3498 debug->NewBreak(it_.frame()->id());
3499 } else {
3500 debug->NewBreak(StackFrame::NO_ID);
3501 }
3502
3503 // Make sure that debugger is loaded and enter the debugger context.
3504 load_failed_ = !debug->Load();
3505 if (!load_failed_) {
3506 // NOTE the member variable save which saves the previous context before
3507 // this change.
3508 isolate_->set_context(*debug->debug_context());
3509 }
3510}
3511
3512
3513EnterDebugger::~EnterDebugger() {
3514 ASSERT(Isolate::Current() == isolate_);
3515 Debug* debug = isolate_->debug();
3516
3517 // Restore to the previous break state.
3518 debug->SetBreak(break_frame_id_, break_id_);
3519
3520 // Check for leaving the debugger.
fschneider@chromium.org7d10be52012-04-10 12:30:14 +00003521 if (!load_failed_ && prev_ == NULL) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003522 // Clear mirror cache when leaving the debugger. Skip this if there is a
3523 // pending exception as clearing the mirror cache calls back into
3524 // JavaScript. This can happen if the v8::Debug::Call is used in which
3525 // case the exception should end up in the calling code.
3526 if (!isolate_->has_pending_exception()) {
3527 // Try to avoid any pending debug break breaking in the clear mirror
3528 // cache JavaScript code.
3529 if (isolate_->stack_guard()->IsDebugBreak()) {
3530 debug->set_interrupts_pending(DEBUGBREAK);
3531 isolate_->stack_guard()->Continue(DEBUGBREAK);
3532 }
3533 debug->ClearMirrorCache();
3534 }
3535
3536 // Request preemption and debug break when leaving the last debugger entry
3537 // if any of these where recorded while debugging.
3538 if (debug->is_interrupt_pending(PREEMPT)) {
3539 // This re-scheduling of preemption is to avoid starvation in some
3540 // debugging scenarios.
3541 debug->clear_interrupt_pending(PREEMPT);
3542 isolate_->stack_guard()->Preempt();
3543 }
3544 if (debug->is_interrupt_pending(DEBUGBREAK)) {
3545 debug->clear_interrupt_pending(DEBUGBREAK);
3546 isolate_->stack_guard()->DebugBreak();
3547 }
3548
3549 // If there are commands in the queue when leaving the debugger request
3550 // that these commands are processed.
3551 if (isolate_->debugger()->HasCommands()) {
3552 isolate_->stack_guard()->DebugCommand();
3553 }
3554
3555 // If leaving the debugger with the debugger no longer active unload it.
3556 if (!isolate_->debugger()->IsDebuggerActive()) {
3557 isolate_->debugger()->UnloadDebugger();
3558 }
3559 }
3560
3561 // Leaving this debugger entry.
3562 debug->set_debugger_entry(prev_);
3563}
3564
3565
ager@chromium.org5ec48922009-05-05 07:25:34 +00003566MessageImpl MessageImpl::NewEvent(DebugEvent event,
3567 bool running,
3568 Handle<JSObject> exec_state,
3569 Handle<JSObject> event_data) {
3570 MessageImpl message(true, event, running,
3571 exec_state, event_data, Handle<String>(), NULL);
3572 return message;
3573}
3574
3575
3576MessageImpl MessageImpl::NewResponse(DebugEvent event,
3577 bool running,
3578 Handle<JSObject> exec_state,
3579 Handle<JSObject> event_data,
3580 Handle<String> response_json,
3581 v8::Debug::ClientData* client_data) {
3582 MessageImpl message(false, event, running,
3583 exec_state, event_data, response_json, client_data);
3584 return message;
3585}
3586
3587
3588MessageImpl::MessageImpl(bool is_event,
3589 DebugEvent event,
3590 bool running,
3591 Handle<JSObject> exec_state,
3592 Handle<JSObject> event_data,
3593 Handle<String> response_json,
3594 v8::Debug::ClientData* client_data)
3595 : is_event_(is_event),
3596 event_(event),
3597 running_(running),
3598 exec_state_(exec_state),
3599 event_data_(event_data),
3600 response_json_(response_json),
3601 client_data_(client_data) {}
3602
3603
3604bool MessageImpl::IsEvent() const {
3605 return is_event_;
3606}
3607
3608
3609bool MessageImpl::IsResponse() const {
3610 return !is_event_;
3611}
3612
3613
3614DebugEvent MessageImpl::GetEvent() const {
3615 return event_;
3616}
3617
3618
3619bool MessageImpl::WillStartRunning() const {
3620 return running_;
3621}
3622
3623
3624v8::Handle<v8::Object> MessageImpl::GetExecutionState() const {
3625 return v8::Utils::ToLocal(exec_state_);
3626}
3627
3628
3629v8::Handle<v8::Object> MessageImpl::GetEventData() const {
3630 return v8::Utils::ToLocal(event_data_);
3631}
3632
3633
3634v8::Handle<v8::String> MessageImpl::GetJSON() const {
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +00003635 v8::HandleScope scope(
3636 reinterpret_cast<v8::Isolate*>(event_data_->GetIsolate()));
ager@chromium.org5ec48922009-05-05 07:25:34 +00003637
3638 if (IsEvent()) {
3639 // Call toJSONProtocol on the debug event object.
3640 Handle<Object> fun = GetProperty(event_data_, "toJSONProtocol");
3641 if (!fun->IsJSFunction()) {
3642 return v8::Handle<v8::String>();
3643 }
3644 bool caught_exception;
3645 Handle<Object> json = Execution::TryCall(Handle<JSFunction>::cast(fun),
3646 event_data_,
3647 0, NULL, &caught_exception);
3648 if (caught_exception || !json->IsString()) {
3649 return v8::Handle<v8::String>();
3650 }
3651 return scope.Close(v8::Utils::ToLocal(Handle<String>::cast(json)));
3652 } else {
3653 return v8::Utils::ToLocal(response_json_);
3654 }
3655}
3656
3657
3658v8::Handle<v8::Context> MessageImpl::GetEventContext() const {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003659 Isolate* isolate = Isolate::Current();
3660 v8::Handle<v8::Context> context = GetDebugEventContext(isolate);
3661 // Isolate::context() may be NULL when "script collected" event occures.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003662 ASSERT(!context.IsEmpty() || event_ == v8::ScriptCollected);
rossberg@chromium.org717967f2011-07-20 13:44:42 +00003663 return context;
ager@chromium.org5ec48922009-05-05 07:25:34 +00003664}
3665
3666
3667v8::Debug::ClientData* MessageImpl::GetClientData() const {
3668 return client_data_;
3669}
3670
3671
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003672EventDetailsImpl::EventDetailsImpl(DebugEvent event,
3673 Handle<JSObject> exec_state,
3674 Handle<JSObject> event_data,
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00003675 Handle<Object> callback_data,
3676 v8::Debug::ClientData* client_data)
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003677 : event_(event),
3678 exec_state_(exec_state),
3679 event_data_(event_data),
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00003680 callback_data_(callback_data),
3681 client_data_(client_data) {}
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003682
3683
3684DebugEvent EventDetailsImpl::GetEvent() const {
3685 return event_;
3686}
3687
3688
3689v8::Handle<v8::Object> EventDetailsImpl::GetExecutionState() const {
3690 return v8::Utils::ToLocal(exec_state_);
3691}
3692
3693
3694v8::Handle<v8::Object> EventDetailsImpl::GetEventData() const {
3695 return v8::Utils::ToLocal(event_data_);
3696}
3697
3698
3699v8::Handle<v8::Context> EventDetailsImpl::GetEventContext() const {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003700 return GetDebugEventContext(Isolate::Current());
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003701}
3702
3703
3704v8::Handle<v8::Value> EventDetailsImpl::GetCallbackData() const {
3705 return v8::Utils::ToLocal(callback_data_);
3706}
3707
3708
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00003709v8::Debug::ClientData* EventDetailsImpl::GetClientData() const {
3710 return client_data_;
3711}
3712
3713
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003714CommandMessage::CommandMessage() : text_(Vector<uint16_t>::empty()),
3715 client_data_(NULL) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003716}
3717
3718
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003719CommandMessage::CommandMessage(const Vector<uint16_t>& text,
3720 v8::Debug::ClientData* data)
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003721 : text_(text),
3722 client_data_(data) {
3723}
3724
3725
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003726CommandMessage::~CommandMessage() {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003727}
3728
3729
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003730void CommandMessage::Dispose() {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003731 text_.Dispose();
3732 delete client_data_;
3733 client_data_ = NULL;
3734}
3735
3736
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003737CommandMessage CommandMessage::New(const Vector<uint16_t>& command,
3738 v8::Debug::ClientData* data) {
3739 return CommandMessage(command.Clone(), data);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003740}
3741
3742
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003743CommandMessageQueue::CommandMessageQueue(int size) : start_(0), end_(0),
3744 size_(size) {
3745 messages_ = NewArray<CommandMessage>(size);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003746}
3747
3748
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003749CommandMessageQueue::~CommandMessageQueue() {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003750 while (!IsEmpty()) {
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003751 CommandMessage m = Get();
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003752 m.Dispose();
3753 }
kasper.lund7276f142008-07-30 08:49:36 +00003754 DeleteArray(messages_);
3755}
3756
3757
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003758CommandMessage CommandMessageQueue::Get() {
kasper.lund7276f142008-07-30 08:49:36 +00003759 ASSERT(!IsEmpty());
3760 int result = start_;
3761 start_ = (start_ + 1) % size_;
3762 return messages_[result];
3763}
3764
3765
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003766void CommandMessageQueue::Put(const CommandMessage& message) {
kasper.lund7276f142008-07-30 08:49:36 +00003767 if ((end_ + 1) % size_ == start_) {
3768 Expand();
3769 }
3770 messages_[end_] = message;
3771 end_ = (end_ + 1) % size_;
3772}
3773
3774
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003775void CommandMessageQueue::Expand() {
3776 CommandMessageQueue new_queue(size_ * 2);
kasper.lund7276f142008-07-30 08:49:36 +00003777 while (!IsEmpty()) {
3778 new_queue.Put(Get());
3779 }
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003780 CommandMessage* array_to_free = messages_;
kasper.lund7276f142008-07-30 08:49:36 +00003781 *this = new_queue;
3782 new_queue.messages_ = array_to_free;
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003783 // Make the new_queue empty so that it doesn't call Dispose on any messages.
3784 new_queue.start_ = new_queue.end_;
kasper.lund7276f142008-07-30 08:49:36 +00003785 // Automatic destructor called on new_queue, freeing array_to_free.
3786}
3787
3788
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00003789LockingCommandMessageQueue::LockingCommandMessageQueue(Logger* logger, int size)
3790 : logger_(logger), queue_(size) {
kasper.lund7276f142008-07-30 08:49:36 +00003791 lock_ = OS::CreateMutex();
3792}
3793
3794
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003795LockingCommandMessageQueue::~LockingCommandMessageQueue() {
kasper.lund7276f142008-07-30 08:49:36 +00003796 delete lock_;
3797}
3798
3799
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003800bool LockingCommandMessageQueue::IsEmpty() const {
kasper.lund7276f142008-07-30 08:49:36 +00003801 ScopedLock sl(lock_);
3802 return queue_.IsEmpty();
3803}
3804
3805
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003806CommandMessage LockingCommandMessageQueue::Get() {
kasper.lund7276f142008-07-30 08:49:36 +00003807 ScopedLock sl(lock_);
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003808 CommandMessage result = queue_.Get();
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00003809 logger_->DebugEvent("Get", result.text());
kasper.lund7276f142008-07-30 08:49:36 +00003810 return result;
3811}
3812
3813
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003814void LockingCommandMessageQueue::Put(const CommandMessage& message) {
kasper.lund7276f142008-07-30 08:49:36 +00003815 ScopedLock sl(lock_);
3816 queue_.Put(message);
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00003817 logger_->DebugEvent("Put", message.text());
kasper.lund7276f142008-07-30 08:49:36 +00003818}
3819
3820
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003821void LockingCommandMessageQueue::Clear() {
kasper.lund7276f142008-07-30 08:49:36 +00003822 ScopedLock sl(lock_);
3823 queue_.Clear();
3824}
3825
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003826
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003827MessageDispatchHelperThread::MessageDispatchHelperThread(Isolate* isolate)
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00003828 : Thread("v8:MsgDispHelpr"),
mstarzinger@chromium.orgf705b502013-04-04 11:38:09 +00003829 isolate_(isolate), sem_(OS::CreateSemaphore(0)),
3830 mutex_(OS::CreateMutex()), already_signalled_(false) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003831}
3832
3833
3834MessageDispatchHelperThread::~MessageDispatchHelperThread() {
3835 delete mutex_;
3836 delete sem_;
3837}
3838
3839
3840void MessageDispatchHelperThread::Schedule() {
3841 {
3842 ScopedLock lock(mutex_);
3843 if (already_signalled_) {
3844 return;
3845 }
3846 already_signalled_ = true;
3847 }
3848 sem_->Signal();
3849}
3850
3851
3852void MessageDispatchHelperThread::Run() {
3853 while (true) {
3854 sem_->Wait();
3855 {
3856 ScopedLock lock(mutex_);
3857 already_signalled_ = false;
3858 }
3859 {
mstarzinger@chromium.orgf705b502013-04-04 11:38:09 +00003860 Locker locker(reinterpret_cast<v8::Isolate*>(isolate_));
3861 isolate_->debugger()->CallMessageDispatchHandler();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003862 }
3863 }
3864}
3865
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003866#endif // ENABLE_DEBUGGER_SUPPORT
kasper.lund7276f142008-07-30 08:49:36 +00003867
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003868} } // namespace v8::internal