blob: 04f8a7a0274eeaec9d401bc436aad178a182739f [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() &&
162 !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,
789 context,
790 NULL, NULL,
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000791 Handle<String>::null(),
792 NATIVES_CODE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000793
794 // Silently ignore stack overflows during compilation.
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000795 if (function_info.is_null()) {
lrn@chromium.org7516f052011-03-30 08:52:27 +0000796 ASSERT(isolate->has_pending_exception());
797 isolate->clear_pending_exception();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000798 return false;
799 }
800
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000801 // Execute the shared function in the debugger context.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000802 bool caught_exception;
kasper.lund44510672008-07-25 07:37:58 +0000803 Handle<JSFunction> function =
lrn@chromium.org7516f052011-03-30 08:52:27 +0000804 factory->NewFunctionFromSharedFunctionInfo(function_info, context);
rossberg@chromium.org717967f2011-07-20 13:44:42 +0000805
fschneider@chromium.org7d10be52012-04-10 12:30:14 +0000806 Handle<Object> exception =
ulan@chromium.org09d7ab52013-02-25 15:50:35 +0000807 Execution::TryCall(function,
808 Handle<Object>(context->global_object(), isolate),
809 0,
810 NULL,
811 &caught_exception);
kasper.lund44510672008-07-25 07:37:58 +0000812
813 // Check for caught exceptions.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000814 if (caught_exception) {
fschneider@chromium.org7d10be52012-04-10 12:30:14 +0000815 ASSERT(!isolate->has_pending_exception());
816 MessageLocation computed_location;
817 isolate->ComputeLocation(&computed_location);
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000818 Handle<Object> message = MessageHandler::MakeMessageObject(
verwaest@chromium.orgd4be0f02013-06-05 13:39:03 +0000819 isolate, "error_loading_debugger", &computed_location,
fschneider@chromium.org7d10be52012-04-10 12:30:14 +0000820 Vector<Handle<Object> >::empty(), Handle<String>(), Handle<JSArray>());
821 ASSERT(!isolate->has_pending_exception());
mmassi@chromium.org49a44672012-12-04 13:52:03 +0000822 if (!exception.is_null()) {
823 isolate->set_pending_exception(*exception);
824 MessageHandler::ReportMessage(Isolate::Current(), NULL, message);
825 isolate->clear_pending_exception();
826 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000827 return false;
828 }
829
kasper.lund44510672008-07-25 07:37:58 +0000830 // Mark this script as native and return successfully.
831 Handle<Script> script(Script::cast(function->shared()->script()));
ager@chromium.orge2902be2009-06-08 12:21:35 +0000832 script->set_type(Smi::FromInt(Script::TYPE_NATIVE));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000833 return true;
834}
835
836
837bool Debug::Load() {
838 // Return if debugger is already loaded.
kasper.lund212ac232008-07-16 07:07:30 +0000839 if (IsLoaded()) return true;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000840
lrn@chromium.org7516f052011-03-30 08:52:27 +0000841 Debugger* debugger = isolate_->debugger();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000842
kasper.lund44510672008-07-25 07:37:58 +0000843 // Bail out if we're already in the process of compiling the native
844 // JavaScript source code for the debugger.
lrn@chromium.org7516f052011-03-30 08:52:27 +0000845 if (debugger->compiling_natives() ||
846 debugger->is_loading_debugger())
mads.s.agercbaa0602008-08-14 13:41:48 +0000847 return false;
lrn@chromium.org7516f052011-03-30 08:52:27 +0000848 debugger->set_loading_debugger(true);
kasper.lund44510672008-07-25 07:37:58 +0000849
850 // Disable breakpoints and interrupts while compiling and running the
851 // debugger scripts including the context creation code.
852 DisableBreak disable(true);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000853 PostponeInterruptsScope postpone(isolate_);
kasper.lund44510672008-07-25 07:37:58 +0000854
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000855 // Create the debugger context.
lrn@chromium.org7516f052011-03-30 08:52:27 +0000856 HandleScope scope(isolate_);
kasper.lund44510672008-07-25 07:37:58 +0000857 Handle<Context> context =
lrn@chromium.org7516f052011-03-30 08:52:27 +0000858 isolate_->bootstrapper()->CreateEnvironment(
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000859 Handle<Object>::null(),
860 v8::Handle<ObjectTemplate>(),
861 NULL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000862
fschneider@chromium.org7d10be52012-04-10 12:30:14 +0000863 // Fail if no context could be created.
864 if (context.is_null()) return false;
865
kasper.lund44510672008-07-25 07:37:58 +0000866 // Use the debugger context.
lrn@chromium.org7516f052011-03-30 08:52:27 +0000867 SaveContext save(isolate_);
868 isolate_->set_context(*context);
kasper.lund44510672008-07-25 07:37:58 +0000869
870 // Expose the builtins object in the debugger context.
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000871 Handle<String> key = isolate_->factory()->InternalizeOneByteString(
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +0000872 STATIC_ASCII_VECTOR("builtins"));
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000873 Handle<GlobalObject> global = Handle<GlobalObject>(context->global_object());
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +0000874 RETURN_IF_EMPTY_HANDLE_VALUE(
lrn@chromium.org7516f052011-03-30 08:52:27 +0000875 isolate_,
ulan@chromium.org09d7ab52013-02-25 15:50:35 +0000876 JSReceiver::SetProperty(global,
877 key,
878 Handle<Object>(global->builtins(), isolate_),
879 NONE,
880 kNonStrictMode),
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +0000881 false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000882
883 // Compile the JavaScript for the debugger in the debugger context.
lrn@chromium.org7516f052011-03-30 08:52:27 +0000884 debugger->set_compiling_natives(true);
kasper.lund44510672008-07-25 07:37:58 +0000885 bool caught_exception =
886 !CompileDebuggerScript(Natives::GetIndex("mirror")) ||
887 !CompileDebuggerScript(Natives::GetIndex("debug"));
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000888
889 if (FLAG_enable_liveedit) {
890 caught_exception = caught_exception ||
891 !CompileDebuggerScript(Natives::GetIndex("liveedit"));
892 }
893
lrn@chromium.org7516f052011-03-30 08:52:27 +0000894 debugger->set_compiling_natives(false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000895
mads.s.agercbaa0602008-08-14 13:41:48 +0000896 // Make sure we mark the debugger as not loading before we might
897 // return.
lrn@chromium.org7516f052011-03-30 08:52:27 +0000898 debugger->set_loading_debugger(false);
mads.s.agercbaa0602008-08-14 13:41:48 +0000899
kasper.lund44510672008-07-25 07:37:58 +0000900 // Check for caught exceptions.
901 if (caught_exception) return false;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000902
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000903 // Debugger loaded, create debugger context global handle.
904 debug_context_ = Handle<Context>::cast(
905 isolate_->global_handles()->Create(*context));
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000906
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000907 return true;
908}
909
910
911void Debug::Unload() {
912 // Return debugger is not loaded.
913 if (!IsLoaded()) {
914 return;
915 }
916
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000917 // Clear the script cache.
918 DestroyScriptCache();
919
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000920 // Clear debugger context global handle.
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000921 isolate_->global_handles()->Destroy(
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000922 reinterpret_cast<Object**>(debug_context_.location()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000923 debug_context_ = Handle<Context>();
924}
925
926
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000927// Set the flag indicating that preemption happened during debugging.
928void Debug::PreemptionWhileInDebugger() {
929 ASSERT(InDebugger());
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000930 Debug::set_interrupts_pending(PREEMPT);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000931}
932
933
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000934void Debug::Iterate(ObjectVisitor* v) {
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000935 v->VisitPointer(BitCast<Object**>(&(debug_break_return_)));
936 v->VisitPointer(BitCast<Object**>(&(debug_break_slot_)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000937}
938
939
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000940Object* Debug::Break(Arguments args) {
941 Heap* heap = isolate_->heap();
942 HandleScope scope(isolate_);
mads.s.ager31e71382008-08-13 09:32:07 +0000943 ASSERT(args.length() == 0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000944
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000945 thread_local_.frame_drop_mode_ = FRAMES_UNTOUCHED;
ager@chromium.org357bf652010-04-12 11:30:10 +0000946
kasper.lundbd3ec4e2008-07-09 11:06:54 +0000947 // Get the top-most JavaScript frame.
vegorov@chromium.org74f333b2011-04-06 11:17:46 +0000948 JavaScriptFrameIterator it(isolate_);
kasper.lundbd3ec4e2008-07-09 11:06:54 +0000949 JavaScriptFrame* frame = it.frame();
950
951 // Just continue if breaks are disabled or debugger cannot be loaded.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000952 if (disable_break() || !Load()) {
953 SetAfterBreakTarget(frame);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000954 return heap->undefined_value();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000955 }
956
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000957 // Enter the debugger.
958 EnterDebugger debugger;
959 if (debugger.FailedToEnter()) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000960 return heap->undefined_value();
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000961 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000962
kasper.lund44510672008-07-25 07:37:58 +0000963 // Postpone interrupt during breakpoint processing.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000964 PostponeInterruptsScope postpone(isolate_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000965
966 // Get the debug info (create it if it does not exist).
967 Handle<SharedFunctionInfo> shared =
danno@chromium.org169691d2013-07-15 08:01:13 +0000968 Handle<SharedFunctionInfo>(frame->function()->shared());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000969 Handle<DebugInfo> debug_info = GetDebugInfo(shared);
970
971 // Find the break point where execution has stopped.
972 BreakLocationIterator break_location_iterator(debug_info,
973 ALL_BREAK_LOCATIONS);
verwaest@chromium.org8a00e822013-06-10 15:11:22 +0000974 // pc points to the instruction after the current one, possibly a break
975 // location as well. So the "- 1" to exclude it from the search.
976 break_location_iterator.FindBreakLocationFromAddress(frame->pc() - 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000977
978 // Check whether step next reached a new statement.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000979 if (!StepNextContinue(&break_location_iterator, frame)) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000980 // Decrease steps left if performing multiple steps.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000981 if (thread_local_.step_count_ > 0) {
982 thread_local_.step_count_--;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000983 }
984 }
985
986 // If there is one or more real break points check whether any of these are
987 // triggered.
ulan@chromium.org09d7ab52013-02-25 15:50:35 +0000988 Handle<Object> break_points_hit(heap->undefined_value(), isolate_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000989 if (break_location_iterator.HasBreakPoint()) {
990 Handle<Object> break_point_objects =
ulan@chromium.org09d7ab52013-02-25 15:50:35 +0000991 Handle<Object>(break_location_iterator.BreakPointObjects(), isolate_);
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000992 break_points_hit = CheckBreakPoints(break_point_objects);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000993 }
994
ager@chromium.orga1645e22009-09-09 19:27:10 +0000995 // If step out is active skip everything until the frame where we need to step
996 // out to is reached, unless real breakpoint is hit.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000997 if (StepOutActive() && frame->fp() != step_out_fp() &&
ager@chromium.orga1645e22009-09-09 19:27:10 +0000998 break_points_hit->IsUndefined() ) {
999 // Step count should always be 0 for StepOut.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001000 ASSERT(thread_local_.step_count_ == 0);
ager@chromium.orga1645e22009-09-09 19:27:10 +00001001 } else if (!break_points_hit->IsUndefined() ||
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001002 (thread_local_.last_step_action_ != StepNone &&
1003 thread_local_.step_count_ == 0)) {
ager@chromium.orga1645e22009-09-09 19:27:10 +00001004 // Notify debugger if a real break point is triggered or if performing
1005 // single stepping with no more steps to perform. Otherwise do another step.
1006
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001007 // Clear all current stepping setup.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001008 ClearStepping();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001009
lrn@chromium.org34e60782011-09-15 07:25:40 +00001010 if (thread_local_.queued_step_count_ > 0) {
1011 // Perform queued steps
1012 int step_count = thread_local_.queued_step_count_;
1013
1014 // Clear queue
1015 thread_local_.queued_step_count_ = 0;
1016
1017 PrepareStep(StepNext, step_count);
1018 } else {
1019 // Notify the debug event listeners.
1020 isolate_->debugger()->OnDebugBreak(break_points_hit, false);
1021 }
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001022 } else if (thread_local_.last_step_action_ != StepNone) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001023 // Hold on to last step action as it is cleared by the call to
1024 // ClearStepping.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001025 StepAction step_action = thread_local_.last_step_action_;
1026 int step_count = thread_local_.step_count_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001027
lrn@chromium.org34e60782011-09-15 07:25:40 +00001028 // If StepNext goes deeper in code, StepOut until original frame
1029 // and keep step count queued up in the meantime.
1030 if (step_action == StepNext && frame->fp() < thread_local_.last_fp_) {
1031 // Count frames until target frame
1032 int count = 0;
1033 JavaScriptFrameIterator it(isolate_);
jkummerow@chromium.org212d9642012-05-11 15:02:09 +00001034 while (!it.done() && it.frame()->fp() < thread_local_.last_fp_) {
lrn@chromium.org34e60782011-09-15 07:25:40 +00001035 count++;
1036 it.Advance();
1037 }
1038
danno@chromium.org81cac2b2012-07-10 11:28:27 +00001039 // Check that we indeed found the frame we are looking for.
1040 CHECK(!it.done() && (it.frame()->fp() == thread_local_.last_fp_));
1041 if (step_count > 1) {
1042 // Save old count and action to continue stepping after StepOut.
1043 thread_local_.queued_step_count_ = step_count - 1;
jkummerow@chromium.org212d9642012-05-11 15:02:09 +00001044 }
1045
danno@chromium.org81cac2b2012-07-10 11:28:27 +00001046 // Set up for StepOut to reach target frame.
1047 step_action = StepOut;
1048 step_count = count;
lrn@chromium.org34e60782011-09-15 07:25:40 +00001049 }
1050
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001051 // Clear all current stepping setup.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001052 ClearStepping();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001053
1054 // Set up for the remaining steps.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001055 PrepareStep(step_action, step_count);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001056 }
1057
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001058 if (thread_local_.frame_drop_mode_ == FRAMES_UNTOUCHED) {
1059 SetAfterBreakTarget(frame);
1060 } else if (thread_local_.frame_drop_mode_ ==
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001061 FRAME_DROPPED_IN_IC_CALL) {
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001062 // We must have been calling IC stub. Do not go there anymore.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001063 Code* plain_return = isolate_->builtins()->builtin(
1064 Builtins::kPlainReturn_LiveEdit);
1065 thread_local_.after_break_target_ = plain_return->entry();
1066 } else if (thread_local_.frame_drop_mode_ ==
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001067 FRAME_DROPPED_IN_DEBUG_SLOT_CALL) {
1068 // Debug break slot stub does not return normally, instead it manually
1069 // cleans the stack and jumps. We should patch the jump address.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001070 Code* plain_return = isolate_->builtins()->builtin(
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00001071 Builtins::kFrameDropper_LiveEdit);
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001072 thread_local_.after_break_target_ = plain_return->entry();
1073 } else if (thread_local_.frame_drop_mode_ ==
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001074 FRAME_DROPPED_IN_DIRECT_CALL) {
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001075 // Nothing to do, after_break_target is not used here.
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00001076 } else if (thread_local_.frame_drop_mode_ ==
1077 FRAME_DROPPED_IN_RETURN_CALL) {
1078 Code* plain_return = isolate_->builtins()->builtin(
1079 Builtins::kFrameDropper_LiveEdit);
1080 thread_local_.after_break_target_ = plain_return->entry();
ager@chromium.org357bf652010-04-12 11:30:10 +00001081 } else {
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001082 UNREACHABLE();
ager@chromium.org357bf652010-04-12 11:30:10 +00001083 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001084
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001085 return heap->undefined_value();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001086}
1087
1088
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001089RUNTIME_FUNCTION(Object*, Debug_Break) {
1090 return isolate->debug()->Break(args);
1091}
1092
1093
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001094// Check the break point objects for whether one or more are actually
1095// triggered. This function returns a JSArray with the break point objects
1096// which is triggered.
1097Handle<Object> Debug::CheckBreakPoints(Handle<Object> break_point_objects) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00001098 Factory* factory = isolate_->factory();
1099
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001100 // Count the number of break points hit. If there are multiple break points
1101 // they are in a FixedArray.
1102 Handle<FixedArray> break_points_hit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001103 int break_points_hit_count = 0;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001104 ASSERT(!break_point_objects->IsUndefined());
1105 if (break_point_objects->IsFixedArray()) {
1106 Handle<FixedArray> array(FixedArray::cast(*break_point_objects));
lrn@chromium.org7516f052011-03-30 08:52:27 +00001107 break_points_hit = factory->NewFixedArray(array->length());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001108 for (int i = 0; i < array->length(); i++) {
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00001109 Handle<Object> o(array->get(i), isolate_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001110 if (CheckBreakPoint(o)) {
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001111 break_points_hit->set(break_points_hit_count++, *o);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001112 }
1113 }
1114 } else {
lrn@chromium.org7516f052011-03-30 08:52:27 +00001115 break_points_hit = factory->NewFixedArray(1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001116 if (CheckBreakPoint(break_point_objects)) {
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001117 break_points_hit->set(break_points_hit_count++, *break_point_objects);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001118 }
1119 }
1120
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001121 // Return undefined if no break points were triggered.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001122 if (break_points_hit_count == 0) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00001123 return factory->undefined_value();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001124 }
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001125 // Return break points hit as a JSArray.
lrn@chromium.org7516f052011-03-30 08:52:27 +00001126 Handle<JSArray> result = factory->NewJSArrayWithElements(break_points_hit);
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001127 result->set_length(Smi::FromInt(break_points_hit_count));
1128 return result;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001129}
1130
1131
1132// Check whether a single break point object is triggered.
1133bool Debug::CheckBreakPoint(Handle<Object> break_point_object) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00001134 Factory* factory = isolate_->factory();
1135 HandleScope scope(isolate_);
ager@chromium.org381abbb2009-02-25 13:23:22 +00001136
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001137 // Ignore check if break point object is not a JSObject.
1138 if (!break_point_object->IsJSObject()) return true;
1139
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001140 // Get the function IsBreakPointTriggered (defined in debug-debugger.js).
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001141 Handle<String> is_break_point_triggered_string =
1142 factory->InternalizeOneByteString(
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00001143 STATIC_ASCII_VECTOR("IsBreakPointTriggered"));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001144 Handle<JSFunction> check_break_point =
1145 Handle<JSFunction>(JSFunction::cast(
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00001146 debug_context()->global_object()->GetPropertyNoExceptionThrown(
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001147 *is_break_point_triggered_string)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001148
1149 // Get the break id as an object.
lrn@chromium.org7516f052011-03-30 08:52:27 +00001150 Handle<Object> break_id = factory->NewNumberFromInt(Debug::break_id());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001151
1152 // Call HandleBreakPointx.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001153 bool caught_exception;
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00001154 Handle<Object> argv[] = { break_id, break_point_object };
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001155 Handle<Object> result = Execution::TryCall(check_break_point,
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00001156 isolate_->js_builtins_object(),
1157 ARRAY_SIZE(argv),
1158 argv,
1159 &caught_exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001160
1161 // If exception or non boolean result handle as not triggered
1162 if (caught_exception || !result->IsBoolean()) {
1163 return false;
1164 }
1165
1166 // Return whether the break point is triggered.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001167 ASSERT(!result.is_null());
1168 return (*result)->IsTrue();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001169}
1170
1171
1172// Check whether the function has debug information.
1173bool Debug::HasDebugInfo(Handle<SharedFunctionInfo> shared) {
1174 return !shared->debug_info()->IsUndefined();
1175}
1176
1177
kasper.lundbd3ec4e2008-07-09 11:06:54 +00001178// Return the debug info for this function. EnsureDebugInfo must be called
1179// prior to ensure the debug info has been generated for shared.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001180Handle<DebugInfo> Debug::GetDebugInfo(Handle<SharedFunctionInfo> shared) {
kasper.lundbd3ec4e2008-07-09 11:06:54 +00001181 ASSERT(HasDebugInfo(shared));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001182 return Handle<DebugInfo>(DebugInfo::cast(shared->debug_info()));
1183}
1184
1185
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001186void Debug::SetBreakPoint(Handle<JSFunction> function,
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00001187 Handle<Object> break_point_object,
1188 int* source_position) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00001189 HandleScope scope(isolate_);
ager@chromium.org381abbb2009-02-25 13:23:22 +00001190
lrn@chromium.org34e60782011-09-15 07:25:40 +00001191 PrepareForBreakPoints();
1192
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001193 // Make sure the function is compiled and has set up the debug info.
1194 Handle<SharedFunctionInfo> shared(function->shared());
1195 if (!EnsureDebugInfo(shared, function)) {
kasper.lundbd3ec4e2008-07-09 11:06:54 +00001196 // Return if retrieving debug info failed.
1197 return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001198 }
1199
kasper.lundbd3ec4e2008-07-09 11:06:54 +00001200 Handle<DebugInfo> debug_info = GetDebugInfo(shared);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001201 // Source positions starts with zero.
danno@chromium.orgbf0c8202011-12-27 10:09:42 +00001202 ASSERT(*source_position >= 0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001203
1204 // Find the break point and change it.
1205 BreakLocationIterator it(debug_info, SOURCE_BREAK_LOCATIONS);
jkummerow@chromium.org93a47f42013-07-02 14:43:41 +00001206 it.FindBreakLocationFromPosition(*source_position, STATEMENT_ALIGNED);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001207 it.SetBreakPoint(break_point_object);
1208
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00001209 *source_position = it.position();
1210
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001211 // At least one active break point now.
1212 ASSERT(debug_info->GetBreakPointCount() > 0);
1213}
1214
1215
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001216bool Debug::SetBreakPointForScript(Handle<Script> script,
1217 Handle<Object> break_point_object,
jkummerow@chromium.org93a47f42013-07-02 14:43:41 +00001218 int* source_position,
1219 BreakPositionAlignment alignment) {
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001220 HandleScope scope(isolate_);
1221
jkummerow@chromium.org78502a92012-09-06 13:50:42 +00001222 PrepareForBreakPoints();
1223
1224 // Obtain shared function info for the function.
1225 Object* result = FindSharedFunctionInfoInScript(script, *source_position);
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001226 if (result->IsUndefined()) return false;
1227
1228 // Make sure the function has set up the debug info.
1229 Handle<SharedFunctionInfo> shared(SharedFunctionInfo::cast(result));
1230 if (!EnsureDebugInfo(shared, Handle<JSFunction>::null())) {
1231 // Return if retrieving debug info failed.
1232 return false;
1233 }
1234
1235 // Find position within function. The script position might be before the
1236 // source position of the first function.
1237 int position;
1238 if (shared->start_position() > *source_position) {
1239 position = 0;
1240 } else {
1241 position = *source_position - shared->start_position();
1242 }
1243
1244 Handle<DebugInfo> debug_info = GetDebugInfo(shared);
1245 // Source positions starts with zero.
1246 ASSERT(position >= 0);
1247
1248 // Find the break point and change it.
1249 BreakLocationIterator it(debug_info, SOURCE_BREAK_LOCATIONS);
jkummerow@chromium.org93a47f42013-07-02 14:43:41 +00001250 it.FindBreakLocationFromPosition(position, alignment);
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001251 it.SetBreakPoint(break_point_object);
1252
1253 *source_position = it.position() + shared->start_position();
1254
1255 // At least one active break point now.
1256 ASSERT(debug_info->GetBreakPointCount() > 0);
1257 return true;
1258}
1259
1260
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001261void Debug::ClearBreakPoint(Handle<Object> break_point_object) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00001262 HandleScope scope(isolate_);
ager@chromium.org381abbb2009-02-25 13:23:22 +00001263
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001264 DebugInfoListNode* node = debug_info_list_;
1265 while (node != NULL) {
1266 Object* result = DebugInfo::FindBreakPointInfo(node->debug_info(),
1267 break_point_object);
1268 if (!result->IsUndefined()) {
1269 // Get information in the break point.
1270 BreakPointInfo* break_point_info = BreakPointInfo::cast(result);
1271 Handle<DebugInfo> debug_info = node->debug_info();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001272
1273 // Find the break point and clear it.
1274 BreakLocationIterator it(debug_info, SOURCE_BREAK_LOCATIONS);
verwaest@chromium.org8a00e822013-06-10 15:11:22 +00001275 it.FindBreakLocationFromAddress(debug_info->code()->entry() +
1276 break_point_info->code_position()->value());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001277 it.ClearBreakPoint(break_point_object);
1278
1279 // If there are no more break points left remove the debug info for this
1280 // function.
1281 if (debug_info->GetBreakPointCount() == 0) {
1282 RemoveDebugInfo(debug_info);
1283 }
1284
1285 return;
1286 }
1287 node = node->next();
1288 }
1289}
1290
1291
ager@chromium.org381abbb2009-02-25 13:23:22 +00001292void Debug::ClearAllBreakPoints() {
1293 DebugInfoListNode* node = debug_info_list_;
1294 while (node != NULL) {
1295 // Remove all debug break code.
1296 BreakLocationIterator it(node->debug_info(), ALL_BREAK_LOCATIONS);
1297 it.ClearAllDebugBreak();
1298 node = node->next();
1299 }
1300
1301 // Remove all debug info.
1302 while (debug_info_list_ != NULL) {
1303 RemoveDebugInfo(debug_info_list_->debug_info());
1304 }
1305}
1306
1307
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001308void Debug::FloodWithOneShot(Handle<JSFunction> function) {
lrn@chromium.org34e60782011-09-15 07:25:40 +00001309 PrepareForBreakPoints();
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001310
1311 // Make sure the function is compiled and has set up the debug info.
1312 Handle<SharedFunctionInfo> shared(function->shared());
1313 if (!EnsureDebugInfo(shared, function)) {
kasper.lundbd3ec4e2008-07-09 11:06:54 +00001314 // Return if we failed to retrieve the debug info.
1315 return;
1316 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001317
1318 // Flood the function with break points.
kasper.lundbd3ec4e2008-07-09 11:06:54 +00001319 BreakLocationIterator it(GetDebugInfo(shared), ALL_BREAK_LOCATIONS);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001320 while (!it.Done()) {
1321 it.SetOneShot();
1322 it.Next();
1323 }
1324}
1325
1326
rossberg@chromium.org2c067b12012-03-19 11:01:52 +00001327void Debug::FloodBoundFunctionWithOneShot(Handle<JSFunction> function) {
1328 Handle<FixedArray> new_bindings(function->function_bindings());
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00001329 Handle<Object> bindee(new_bindings->get(JSFunction::kBoundFunctionIndex),
1330 isolate_);
rossberg@chromium.org2c067b12012-03-19 11:01:52 +00001331
1332 if (!bindee.is_null() && bindee->IsJSFunction() &&
1333 !JSFunction::cast(*bindee)->IsBuiltin()) {
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001334 Handle<JSFunction> bindee_function(JSFunction::cast(*bindee));
1335 Debug::FloodWithOneShot(bindee_function);
rossberg@chromium.org2c067b12012-03-19 11:01:52 +00001336 }
1337}
1338
1339
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001340void Debug::FloodHandlerWithOneShot() {
ager@chromium.org8bb60582008-12-11 12:02:20 +00001341 // Iterate through the JavaScript stack looking for handlers.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001342 StackFrame::Id id = break_frame_id();
ager@chromium.org8bb60582008-12-11 12:02:20 +00001343 if (id == StackFrame::NO_ID) {
1344 // If there is no JavaScript stack don't do anything.
1345 return;
1346 }
vegorov@chromium.org74f333b2011-04-06 11:17:46 +00001347 for (JavaScriptFrameIterator it(isolate_, id); !it.done(); it.Advance()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001348 JavaScriptFrame* frame = it.frame();
1349 if (frame->HasHandler()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001350 // Flood the function with the catch block with break points
danno@chromium.org169691d2013-07-15 08:01:13 +00001351 FloodWithOneShot(Handle<JSFunction>(frame->function()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001352 return;
1353 }
1354 }
1355}
1356
1357
1358void Debug::ChangeBreakOnException(ExceptionBreakType type, bool enable) {
1359 if (type == BreakUncaughtException) {
1360 break_on_uncaught_exception_ = enable;
1361 } else {
1362 break_on_exception_ = enable;
1363 }
1364}
1365
1366
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00001367bool Debug::IsBreakOnException(ExceptionBreakType type) {
1368 if (type == BreakUncaughtException) {
1369 return break_on_uncaught_exception_;
1370 } else {
1371 return break_on_exception_;
1372 }
1373}
1374
1375
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001376void Debug::PrepareStep(StepAction step_action, int step_count) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00001377 HandleScope scope(isolate_);
lrn@chromium.org34e60782011-09-15 07:25:40 +00001378
1379 PrepareForBreakPoints();
1380
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001381 ASSERT(Debug::InDebugger());
1382
1383 // Remember this step action and count.
1384 thread_local_.last_step_action_ = step_action;
ager@chromium.orga1645e22009-09-09 19:27:10 +00001385 if (step_action == StepOut) {
1386 // For step out target frame will be found on the stack so there is no need
1387 // to set step counter for it. It's expected to always be 0 for StepOut.
1388 thread_local_.step_count_ = 0;
1389 } else {
1390 thread_local_.step_count_ = step_count;
1391 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001392
1393 // Get the frame where the execution has stopped and skip the debug frame if
1394 // any. The debug frame will only be present if execution was stopped due to
1395 // hitting a break point. In other situations (e.g. unhandled exception) the
1396 // debug frame is not present.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001397 StackFrame::Id id = break_frame_id();
ager@chromium.org8bb60582008-12-11 12:02:20 +00001398 if (id == StackFrame::NO_ID) {
1399 // If there is no JavaScript stack don't do anything.
1400 return;
1401 }
vegorov@chromium.org74f333b2011-04-06 11:17:46 +00001402 JavaScriptFrameIterator frames_it(isolate_, id);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001403 JavaScriptFrame* frame = frames_it.frame();
1404
1405 // First of all ensure there is one-shot break points in the top handler
1406 // if any.
1407 FloodHandlerWithOneShot();
1408
1409 // If the function on the top frame is unresolved perform step out. This will
1410 // be the case when calling unknown functions and having the debugger stopped
1411 // in an unhandled exception.
1412 if (!frame->function()->IsJSFunction()) {
1413 // Step out: Find the calling JavaScript frame and flood it with
1414 // breakpoints.
1415 frames_it.Advance();
1416 // Fill the function to return to with one-shot break points.
danno@chromium.org169691d2013-07-15 08:01:13 +00001417 JSFunction* function = frames_it.frame()->function();
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001418 FloodWithOneShot(Handle<JSFunction>(function));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001419 return;
1420 }
1421
1422 // Get the debug info (create it if it does not exist).
danno@chromium.org169691d2013-07-15 08:01:13 +00001423 Handle<JSFunction> function(frame->function());
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001424 Handle<SharedFunctionInfo> shared(function->shared());
1425 if (!EnsureDebugInfo(shared, function)) {
kasper.lundbd3ec4e2008-07-09 11:06:54 +00001426 // Return if ensuring debug info failed.
1427 return;
1428 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001429 Handle<DebugInfo> debug_info = GetDebugInfo(shared);
1430
1431 // Find the break location where execution has stopped.
1432 BreakLocationIterator it(debug_info, ALL_BREAK_LOCATIONS);
verwaest@chromium.org8a00e822013-06-10 15:11:22 +00001433 // pc points to the instruction after the current one, possibly a break
1434 // location as well. So the "- 1" to exclude it from the search.
1435 it.FindBreakLocationFromAddress(frame->pc() - 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001436
1437 // Compute whether or not the target is a call target.
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001438 bool is_load_or_store = false;
1439 bool is_inline_cache_stub = false;
whesse@chromium.orge90029b2010-08-02 11:52:17 +00001440 bool is_at_restarted_function = false;
ager@chromium.orga1645e22009-09-09 19:27:10 +00001441 Handle<Code> call_function_stub;
ager@chromium.orga1645e22009-09-09 19:27:10 +00001442
whesse@chromium.orge90029b2010-08-02 11:52:17 +00001443 if (thread_local_.restarter_frame_function_pointer_ == NULL) {
1444 if (RelocInfo::IsCodeTarget(it.rinfo()->rmode())) {
1445 bool is_call_target = false;
1446 Address target = it.rinfo()->target_address();
1447 Code* code = Code::GetCodeFromTargetAddress(target);
1448 if (code->is_call_stub() || code->is_keyed_call_stub()) {
1449 is_call_target = true;
1450 }
1451 if (code->is_inline_cache_stub()) {
1452 is_inline_cache_stub = true;
1453 is_load_or_store = !is_call_target;
1454 }
1455
1456 // Check if target code is CallFunction stub.
1457 Code* maybe_call_function_stub = code;
1458 // If there is a breakpoint at this line look at the original code to
1459 // check if it is a CallFunction stub.
1460 if (it.IsDebugBreak()) {
1461 Address original_target = it.original_rinfo()->target_address();
1462 maybe_call_function_stub =
1463 Code::GetCodeFromTargetAddress(original_target);
1464 }
1465 if (maybe_call_function_stub->kind() == Code::STUB &&
1466 maybe_call_function_stub->major_key() == CodeStub::CallFunction) {
1467 // Save reference to the code as we may need it to find out arguments
1468 // count for 'step in' later.
1469 call_function_stub = Handle<Code>(maybe_call_function_stub);
1470 }
ager@chromium.orga1645e22009-09-09 19:27:10 +00001471 }
whesse@chromium.orge90029b2010-08-02 11:52:17 +00001472 } else {
1473 is_at_restarted_function = true;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001474 }
1475
v8.team.kasperl727e9952008-09-02 14:56:44 +00001476 // If this is the last break code target step out is the only possibility.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001477 if (it.IsExit() || step_action == StepOut) {
ager@chromium.orga1645e22009-09-09 19:27:10 +00001478 if (step_action == StepOut) {
1479 // Skip step_count frames starting with the current one.
1480 while (step_count-- > 0 && !frames_it.done()) {
1481 frames_it.Advance();
1482 }
1483 } else {
1484 ASSERT(it.IsExit());
1485 frames_it.Advance();
1486 }
1487 // Skip builtin functions on the stack.
danno@chromium.org169691d2013-07-15 08:01:13 +00001488 while (!frames_it.done() && frames_it.frame()->function()->IsBuiltin()) {
ager@chromium.orga1645e22009-09-09 19:27:10 +00001489 frames_it.Advance();
1490 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001491 // Step out: If there is a JavaScript caller frame, we need to
1492 // flood it with breakpoints.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001493 if (!frames_it.done()) {
1494 // Fill the function to return to with one-shot break points.
danno@chromium.org169691d2013-07-15 08:01:13 +00001495 JSFunction* function = frames_it.frame()->function();
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001496 FloodWithOneShot(Handle<JSFunction>(function));
ager@chromium.orga1645e22009-09-09 19:27:10 +00001497 // Set target frame pointer.
1498 ActivateStepOut(frames_it.frame());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001499 }
ager@chromium.orga1645e22009-09-09 19:27:10 +00001500 } else if (!(is_inline_cache_stub || RelocInfo::IsConstructCall(it.rmode()) ||
whesse@chromium.orge90029b2010-08-02 11:52:17 +00001501 !call_function_stub.is_null() || is_at_restarted_function)
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001502 || step_action == StepNext || step_action == StepMin) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001503 // Step next or step min.
1504
1505 // Fill the current function with one-shot break points.
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001506 FloodWithOneShot(function);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001507
1508 // Remember source position and frame to handle step next.
1509 thread_local_.last_statement_position_ =
1510 debug_info->code()->SourceStatementPosition(frame->pc());
mmassi@chromium.org7028c052012-06-13 11:51:58 +00001511 thread_local_.last_fp_ = frame->UnpaddedFP();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001512 } else {
whesse@chromium.orge90029b2010-08-02 11:52:17 +00001513 // If there's restarter frame on top of the stack, just get the pointer
1514 // to function which is going to be restarted.
1515 if (is_at_restarted_function) {
1516 Handle<JSFunction> restarted_function(
1517 JSFunction::cast(*thread_local_.restarter_frame_function_pointer_));
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001518 FloodWithOneShot(restarted_function);
whesse@chromium.orge90029b2010-08-02 11:52:17 +00001519 } else if (!call_function_stub.is_null()) {
1520 // If it's CallFunction stub ensure target function is compiled and flood
1521 // it with one shot breakpoints.
1522
ager@chromium.orga1645e22009-09-09 19:27:10 +00001523 // Find out number of arguments from the stub minor key.
1524 // Reverse lookup required as the minor key cannot be retrieved
1525 // from the code object.
1526 Handle<Object> obj(
lrn@chromium.org7516f052011-03-30 08:52:27 +00001527 isolate_->heap()->code_stubs()->SlowReverseLookup(
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00001528 *call_function_stub),
1529 isolate_);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001530 ASSERT(!obj.is_null());
1531 ASSERT(!(*obj)->IsUndefined());
ager@chromium.orga1645e22009-09-09 19:27:10 +00001532 ASSERT(obj->IsSmi());
1533 // Get the STUB key and extract major and minor key.
1534 uint32_t key = Smi::cast(*obj)->value();
1535 // Argc in the stub is the number of arguments passed - not the
1536 // expected arguments of the called function.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001537 int call_function_arg_count =
1538 CallFunctionStub::ExtractArgcFromMinorKey(
1539 CodeStub::MinorKeyFromKey(key));
ager@chromium.orga1645e22009-09-09 19:27:10 +00001540 ASSERT(call_function_stub->major_key() ==
1541 CodeStub::MajorKeyFromKey(key));
1542
1543 // Find target function on the expression stack.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001544 // Expression stack looks like this (top to bottom):
ager@chromium.orga1645e22009-09-09 19:27:10 +00001545 // argN
1546 // ...
1547 // arg0
1548 // Receiver
1549 // Function to call
1550 int expressions_count = frame->ComputeExpressionsCount();
1551 ASSERT(expressions_count - 2 - call_function_arg_count >= 0);
1552 Object* fun = frame->GetExpression(
1553 expressions_count - 2 - call_function_arg_count);
1554 if (fun->IsJSFunction()) {
1555 Handle<JSFunction> js_function(JSFunction::cast(fun));
rossberg@chromium.org2c067b12012-03-19 11:01:52 +00001556 if (js_function->shared()->bound()) {
1557 Debug::FloodBoundFunctionWithOneShot(js_function);
1558 } else if (!js_function->IsBuiltin()) {
1559 // Don't step into builtins.
ager@chromium.orga1645e22009-09-09 19:27:10 +00001560 // It will also compile target function if it's not compiled yet.
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001561 FloodWithOneShot(js_function);
ager@chromium.orga1645e22009-09-09 19:27:10 +00001562 }
1563 }
1564 }
1565
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001566 // Fill the current function with one-shot break points even for step in on
1567 // a call target as the function called might be a native function for
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001568 // which step in will not stop. It also prepares for stepping in
1569 // getters/setters.
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001570 FloodWithOneShot(function);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001571
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001572 if (is_load_or_store) {
1573 // Remember source position and frame to handle step in getter/setter. If
1574 // there is a custom getter/setter it will be handled in
1575 // Object::Get/SetPropertyWithCallback, otherwise the step action will be
1576 // propagated on the next Debug::Break.
1577 thread_local_.last_statement_position_ =
1578 debug_info->code()->SourceStatementPosition(frame->pc());
mmassi@chromium.org7028c052012-06-13 11:51:58 +00001579 thread_local_.last_fp_ = frame->UnpaddedFP();
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001580 }
1581
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001582 // Step in or Step in min
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00001583 it.PrepareStepIn(isolate_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001584 ActivateStepIn(frame);
1585 }
1586}
1587
1588
1589// Check whether the current debug break should be reported to the debugger. It
1590// is used to have step next and step in only report break back to the debugger
1591// if on a different frame or in a different statement. In some situations
1592// there will be several break points in the same statement when the code is
v8.team.kasperl727e9952008-09-02 14:56:44 +00001593// flooded with one-shot break points. This function helps to perform several
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001594// steps before reporting break back to the debugger.
1595bool Debug::StepNextContinue(BreakLocationIterator* break_location_iterator,
1596 JavaScriptFrame* frame) {
lrn@chromium.org34e60782011-09-15 07:25:40 +00001597 // StepNext and StepOut shouldn't bring us deeper in code, so last frame
1598 // shouldn't be a parent of current frame.
1599 if (thread_local_.last_step_action_ == StepNext ||
1600 thread_local_.last_step_action_ == StepOut) {
1601 if (frame->fp() < thread_local_.last_fp_) return true;
1602 }
1603
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001604 // If the step last action was step next or step in make sure that a new
1605 // statement is hit.
1606 if (thread_local_.last_step_action_ == StepNext ||
1607 thread_local_.last_step_action_ == StepIn) {
1608 // Never continue if returning from function.
1609 if (break_location_iterator->IsExit()) return false;
1610
1611 // Continue if we are still on the same frame and in the same statement.
1612 int current_statement_position =
1613 break_location_iterator->code()->SourceStatementPosition(frame->pc());
mmassi@chromium.org7028c052012-06-13 11:51:58 +00001614 return thread_local_.last_fp_ == frame->UnpaddedFP() &&
ager@chromium.org236ad962008-09-25 09:45:57 +00001615 thread_local_.last_statement_position_ == current_statement_position;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001616 }
1617
1618 // No step next action - don't continue.
1619 return false;
1620}
1621
1622
1623// Check whether the code object at the specified address is a debug break code
1624// object.
1625bool Debug::IsDebugBreak(Address addr) {
ager@chromium.org8bb60582008-12-11 12:02:20 +00001626 Code* code = Code::GetCodeFromTargetAddress(addr);
yangguo@chromium.org9768bf12013-01-11 14:51:07 +00001627 return code->is_debug_break();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001628}
1629
1630
1631// Check whether a code stub with the specified major key is a possible break
1632// point location when looking for source break locations.
1633bool Debug::IsSourceBreakStub(Code* code) {
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001634 CodeStub::Major major_key = CodeStub::GetMajorKey(code);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001635 return major_key == CodeStub::CallFunction;
1636}
1637
1638
1639// Check whether a code stub with the specified major key is a possible break
1640// location.
1641bool Debug::IsBreakStub(Code* code) {
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001642 CodeStub::Major major_key = CodeStub::GetMajorKey(code);
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00001643 return major_key == CodeStub::CallFunction;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001644}
1645
1646
1647// Find the builtin to use for invoking the debug break
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001648Handle<Code> Debug::FindDebugBreak(Handle<Code> code, RelocInfo::Mode mode) {
danno@chromium.orgfa458e42012-02-01 10:48:36 +00001649 Isolate* isolate = Isolate::Current();
1650
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001651 // Find the builtin debug break function matching the calling convention
1652 // used by the call site.
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001653 if (code->is_inline_cache_stub()) {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001654 switch (code->kind()) {
1655 case Code::CALL_IC:
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001656 case Code::KEYED_CALL_IC:
danno@chromium.orgfa458e42012-02-01 10:48:36 +00001657 return isolate->stub_cache()->ComputeCallDebugBreak(
1658 code->arguments_count(), code->kind());
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001659
1660 case Code::LOAD_IC:
danno@chromium.orgfa458e42012-02-01 10:48:36 +00001661 return isolate->builtins()->LoadIC_DebugBreak();
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001662
1663 case Code::STORE_IC:
danno@chromium.orgfa458e42012-02-01 10:48:36 +00001664 return isolate->builtins()->StoreIC_DebugBreak();
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001665
1666 case Code::KEYED_LOAD_IC:
danno@chromium.orgfa458e42012-02-01 10:48:36 +00001667 return isolate->builtins()->KeyedLoadIC_DebugBreak();
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001668
1669 case Code::KEYED_STORE_IC:
danno@chromium.orgfa458e42012-02-01 10:48:36 +00001670 return isolate->builtins()->KeyedStoreIC_DebugBreak();
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001671
danno@chromium.orgf005df62013-04-30 16:36:45 +00001672 case Code::COMPARE_NIL_IC:
1673 return isolate->builtins()->CompareNilIC_DebugBreak();
1674
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001675 default:
1676 UNREACHABLE();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001677 }
1678 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001679 if (RelocInfo::IsConstructCall(mode)) {
danno@chromium.orgfa458e42012-02-01 10:48:36 +00001680 if (code->has_function_cache()) {
1681 return isolate->builtins()->CallConstructStub_Recording_DebugBreak();
1682 } else {
1683 return isolate->builtins()->CallConstructStub_DebugBreak();
1684 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001685 }
1686 if (code->kind() == Code::STUB) {
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00001687 ASSERT(code->major_key() == CodeStub::CallFunction);
danno@chromium.orgfa458e42012-02-01 10:48:36 +00001688 if (code->has_function_cache()) {
1689 return isolate->builtins()->CallFunctionStub_Recording_DebugBreak();
1690 } else {
1691 return isolate->builtins()->CallFunctionStub_DebugBreak();
1692 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001693 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001694
1695 UNREACHABLE();
1696 return Handle<Code>::null();
1697}
1698
1699
1700// Simple function for returning the source positions for active break points.
1701Handle<Object> Debug::GetSourceBreakLocations(
jkummerow@chromium.org93a47f42013-07-02 14:43:41 +00001702 Handle<SharedFunctionInfo> shared,
1703 BreakPositionAlignment position_alignment) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00001704 Isolate* isolate = Isolate::Current();
1705 Heap* heap = isolate->heap();
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00001706 if (!HasDebugInfo(shared)) {
1707 return Handle<Object>(heap->undefined_value(), isolate);
1708 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001709 Handle<DebugInfo> debug_info = GetDebugInfo(shared);
1710 if (debug_info->GetBreakPointCount() == 0) {
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00001711 return Handle<Object>(heap->undefined_value(), isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001712 }
1713 Handle<FixedArray> locations =
lrn@chromium.org7516f052011-03-30 08:52:27 +00001714 isolate->factory()->NewFixedArray(debug_info->GetBreakPointCount());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001715 int count = 0;
1716 for (int i = 0; i < debug_info->break_points()->length(); i++) {
1717 if (!debug_info->break_points()->get(i)->IsUndefined()) {
1718 BreakPointInfo* break_point_info =
1719 BreakPointInfo::cast(debug_info->break_points()->get(i));
1720 if (break_point_info->GetBreakPointCount() > 0) {
jkummerow@chromium.org93a47f42013-07-02 14:43:41 +00001721 Smi* position;
1722 switch (position_alignment) {
1723 case STATEMENT_ALIGNED:
1724 position = break_point_info->statement_position();
1725 break;
1726 case BREAK_POSITION_ALIGNED:
1727 position = break_point_info->source_position();
1728 break;
1729 default:
1730 UNREACHABLE();
1731 position = break_point_info->statement_position();
1732 }
1733
1734 locations->set(count++, position);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001735 }
1736 }
1737 }
1738 return locations;
1739}
1740
1741
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001742void Debug::NewBreak(StackFrame::Id break_frame_id) {
1743 thread_local_.break_frame_id_ = break_frame_id;
1744 thread_local_.break_id_ = ++thread_local_.break_count_;
1745}
1746
1747
1748void Debug::SetBreak(StackFrame::Id break_frame_id, int break_id) {
1749 thread_local_.break_frame_id_ = break_frame_id;
1750 thread_local_.break_id_ = break_id;
1751}
1752
1753
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001754// Handle stepping into a function.
1755void Debug::HandleStepIn(Handle<JSFunction> function,
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001756 Handle<Object> holder,
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001757 Address fp,
1758 bool is_constructor) {
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00001759 Isolate* isolate = function->GetIsolate();
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001760 // If the frame pointer is not supplied by the caller find it.
1761 if (fp == 0) {
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00001762 StackFrameIterator it(isolate);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001763 it.Advance();
1764 // For constructor functions skip another frame.
1765 if (is_constructor) {
1766 ASSERT(it.frame()->is_construct());
1767 it.Advance();
1768 }
1769 fp = it.frame()->fp();
1770 }
1771
1772 // Flood the function with one-shot break points if it is called from where
1773 // step into was requested.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001774 if (fp == step_in_fp()) {
rossberg@chromium.org2c067b12012-03-19 11:01:52 +00001775 if (function->shared()->bound()) {
1776 // Handle Function.prototype.bind
1777 Debug::FloodBoundFunctionWithOneShot(function);
1778 } else if (!function->IsBuiltin()) {
1779 // Don't allow step into functions in the native context.
kasperl@chromium.orgacae3782009-04-11 09:17:08 +00001780 if (function->shared()->code() ==
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00001781 isolate->builtins()->builtin(Builtins::kFunctionApply) ||
kasperl@chromium.orgacae3782009-04-11 09:17:08 +00001782 function->shared()->code() ==
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00001783 isolate->builtins()->builtin(Builtins::kFunctionCall)) {
kasperl@chromium.orgacae3782009-04-11 09:17:08 +00001784 // Handle function.apply and function.call separately to flood the
1785 // function to be called and not the code for Builtins::FunctionApply or
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001786 // Builtins::FunctionCall. The receiver of call/apply is the target
1787 // function.
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001788 if (!holder.is_null() && holder->IsJSFunction() &&
1789 !JSFunction::cast(*holder)->IsBuiltin()) {
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001790 Handle<JSFunction> js_function = Handle<JSFunction>::cast(holder);
1791 Debug::FloodWithOneShot(js_function);
kasperl@chromium.orgacae3782009-04-11 09:17:08 +00001792 }
1793 } else {
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001794 Debug::FloodWithOneShot(function);
kasperl@chromium.orgacae3782009-04-11 09:17:08 +00001795 }
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001796 }
1797 }
1798}
1799
1800
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001801void Debug::ClearStepping() {
1802 // Clear the various stepping setup.
1803 ClearOneShot();
1804 ClearStepIn();
ager@chromium.orga1645e22009-09-09 19:27:10 +00001805 ClearStepOut();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001806 ClearStepNext();
1807
1808 // Clear multiple step counter.
1809 thread_local_.step_count_ = 0;
1810}
1811
mstarzinger@chromium.orge0e1b0d2013-07-08 08:38:06 +00001812
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001813// Clears all the one-shot break points that are currently set. Normally this
1814// function is called each time a break point is hit as one shot break points
1815// are used to support stepping.
1816void Debug::ClearOneShot() {
1817 // The current implementation just runs through all the breakpoints. When the
v8.team.kasperl727e9952008-09-02 14:56:44 +00001818 // last break point for a function is removed that function is automatically
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001819 // removed from the list.
1820
1821 DebugInfoListNode* node = debug_info_list_;
1822 while (node != NULL) {
1823 BreakLocationIterator it(node->debug_info(), ALL_BREAK_LOCATIONS);
1824 while (!it.Done()) {
1825 it.ClearOneShot();
1826 it.Next();
1827 }
1828 node = node->next();
1829 }
1830}
1831
1832
1833void Debug::ActivateStepIn(StackFrame* frame) {
ager@chromium.orga1645e22009-09-09 19:27:10 +00001834 ASSERT(!StepOutActive());
mmassi@chromium.org7028c052012-06-13 11:51:58 +00001835 thread_local_.step_into_fp_ = frame->UnpaddedFP();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001836}
1837
1838
1839void Debug::ClearStepIn() {
1840 thread_local_.step_into_fp_ = 0;
1841}
1842
1843
ager@chromium.orga1645e22009-09-09 19:27:10 +00001844void Debug::ActivateStepOut(StackFrame* frame) {
1845 ASSERT(!StepInActive());
mmassi@chromium.org7028c052012-06-13 11:51:58 +00001846 thread_local_.step_out_fp_ = frame->UnpaddedFP();
ager@chromium.orga1645e22009-09-09 19:27:10 +00001847}
1848
1849
1850void Debug::ClearStepOut() {
1851 thread_local_.step_out_fp_ = 0;
1852}
1853
1854
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001855void Debug::ClearStepNext() {
1856 thread_local_.last_step_action_ = StepNone;
ager@chromium.org236ad962008-09-25 09:45:57 +00001857 thread_local_.last_statement_position_ = RelocInfo::kNoPosition;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001858 thread_local_.last_fp_ = 0;
1859}
1860
1861
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001862// Helper function to compile full code for debugging. This code will
mmassi@chromium.org7028c052012-06-13 11:51:58 +00001863// have debug break slots and deoptimization information. Deoptimization
1864// information is required in case that an optimized version of this
1865// function is still activated on the stack. It will also make sure that
1866// the full code is compiled with the same flags as the previous version,
1867// that is flags which can change the code generated. The current method
1868// of mapping from already compiled full code without debug break slots
1869// to full code with debug break slots depends on the generated code is
1870// otherwise exactly the same.
1871static bool CompileFullCodeForDebugging(Handle<JSFunction> function,
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001872 Handle<Code> current_code) {
1873 ASSERT(!current_code->has_debug_break_slots());
1874
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001875 CompilationInfoWithZone info(function);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001876 info.MarkCompilingForDebugging(current_code);
1877 ASSERT(!info.shared_info()->is_compiled());
1878 ASSERT(!info.isolate()->has_pending_exception());
1879
1880 // Use compile lazy which will end up compiling the full code in the
1881 // configuration configured above.
1882 bool result = Compiler::CompileLazy(&info);
1883 ASSERT(result != Isolate::Current()->has_pending_exception());
1884 info.isolate()->clear_pending_exception();
1885#if DEBUG
1886 if (result) {
mmassi@chromium.org7028c052012-06-13 11:51:58 +00001887 Handle<Code> new_code(function->shared()->code());
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001888 ASSERT(new_code->has_debug_break_slots());
1889 ASSERT(current_code->is_compiled_optimizable() ==
1890 new_code->is_compiled_optimizable());
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001891 }
1892#endif
1893 return result;
1894}
1895
1896
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001897static void CollectActiveFunctionsFromThread(
1898 Isolate* isolate,
1899 ThreadLocalTop* top,
1900 List<Handle<JSFunction> >* active_functions,
1901 Object* active_code_marker) {
1902 // Find all non-optimized code functions with activation frames
1903 // on the stack. This includes functions which have optimized
1904 // activations (including inlined functions) on the stack as the
1905 // non-optimized code is needed for the lazy deoptimization.
1906 for (JavaScriptFrameIterator it(isolate, top); !it.done(); it.Advance()) {
1907 JavaScriptFrame* frame = it.frame();
1908 if (frame->is_optimized()) {
jkummerow@chromium.orgba72ec82013-07-22 09:21:20 +00001909 List<JSFunction*> functions(FLAG_max_inlining_levels + 1);
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001910 frame->GetFunctions(&functions);
1911 for (int i = 0; i < functions.length(); i++) {
1912 JSFunction* function = functions[i];
1913 active_functions->Add(Handle<JSFunction>(function));
1914 function->shared()->code()->set_gc_metadata(active_code_marker);
1915 }
1916 } else if (frame->function()->IsJSFunction()) {
danno@chromium.org169691d2013-07-15 08:01:13 +00001917 JSFunction* function = frame->function();
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001918 ASSERT(frame->LookupCode()->kind() == Code::FUNCTION);
1919 active_functions->Add(Handle<JSFunction>(function));
1920 function->shared()->code()->set_gc_metadata(active_code_marker);
1921 }
1922 }
1923}
1924
1925
1926static void RedirectActivationsToRecompiledCodeOnThread(
1927 Isolate* isolate,
1928 ThreadLocalTop* top) {
1929 for (JavaScriptFrameIterator it(isolate, top); !it.done(); it.Advance()) {
1930 JavaScriptFrame* frame = it.frame();
1931
1932 if (frame->is_optimized() || !frame->function()->IsJSFunction()) continue;
1933
danno@chromium.org169691d2013-07-15 08:01:13 +00001934 JSFunction* function = frame->function();
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001935
1936 ASSERT(frame->LookupCode()->kind() == Code::FUNCTION);
1937
1938 Handle<Code> frame_code(frame->LookupCode());
1939 if (frame_code->has_debug_break_slots()) continue;
1940
1941 Handle<Code> new_code(function->shared()->code());
1942 if (new_code->kind() != Code::FUNCTION ||
1943 !new_code->has_debug_break_slots()) {
1944 continue;
1945 }
1946
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001947 // Iterate over the RelocInfo in the original code to compute the sum of the
1948 // constant pools sizes. (See Assembler::CheckConstPool())
1949 // Note that this is only useful for architectures using constant pools.
1950 int constpool_mask = RelocInfo::ModeMask(RelocInfo::CONST_POOL);
1951 int frame_const_pool_size = 0;
1952 for (RelocIterator it(*frame_code, constpool_mask); !it.done(); it.next()) {
1953 RelocInfo* info = it.rinfo();
1954 if (info->pc() >= frame->pc()) break;
1955 frame_const_pool_size += static_cast<int>(info->data());
1956 }
1957 intptr_t frame_offset =
1958 frame->pc() - frame_code->instruction_start() - frame_const_pool_size;
1959
1960 // Iterate over the RelocInfo for new code to find the number of bytes
1961 // generated for debug slots and constant pools.
1962 int debug_break_slot_bytes = 0;
1963 int new_code_const_pool_size = 0;
1964 int mask = RelocInfo::ModeMask(RelocInfo::DEBUG_BREAK_SLOT) |
1965 RelocInfo::ModeMask(RelocInfo::CONST_POOL);
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001966 for (RelocIterator it(*new_code, mask); !it.done(); it.next()) {
1967 // Check if the pc in the new code with debug break
1968 // slots is before this slot.
1969 RelocInfo* info = it.rinfo();
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001970 intptr_t new_offset = info->pc() - new_code->instruction_start() -
1971 new_code_const_pool_size - debug_break_slot_bytes;
1972 if (new_offset >= frame_offset) {
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001973 break;
1974 }
1975
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001976 if (RelocInfo::IsDebugBreakSlot(info->rmode())) {
1977 debug_break_slot_bytes += Assembler::kDebugBreakSlotLength;
1978 } else {
1979 ASSERT(RelocInfo::IsConstPool(info->rmode()));
1980 // The size of the constant pool is encoded in the data.
1981 new_code_const_pool_size += static_cast<int>(info->data());
1982 }
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001983 }
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001984
1985 // Compute the equivalent pc in the new code.
1986 byte* new_pc = new_code->instruction_start() + frame_offset +
1987 debug_break_slot_bytes + new_code_const_pool_size;
1988
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001989 if (FLAG_trace_deopt) {
1990 PrintF("Replacing code %08" V8PRIxPTR " - %08" V8PRIxPTR " (%d) "
1991 "with %08" V8PRIxPTR " - %08" V8PRIxPTR " (%d) "
1992 "for debugging, "
1993 "changing pc from %08" V8PRIxPTR " to %08" V8PRIxPTR "\n",
1994 reinterpret_cast<intptr_t>(
1995 frame_code->instruction_start()),
1996 reinterpret_cast<intptr_t>(
1997 frame_code->instruction_start()) +
1998 frame_code->instruction_size(),
1999 frame_code->instruction_size(),
2000 reinterpret_cast<intptr_t>(new_code->instruction_start()),
2001 reinterpret_cast<intptr_t>(new_code->instruction_start()) +
2002 new_code->instruction_size(),
2003 new_code->instruction_size(),
2004 reinterpret_cast<intptr_t>(frame->pc()),
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00002005 reinterpret_cast<intptr_t>(new_pc));
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002006 }
2007
2008 // Patch the return address to return into the code with
2009 // debug break slots.
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00002010 frame->set_pc(new_pc);
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002011 }
2012}
2013
2014
2015class ActiveFunctionsCollector : public ThreadVisitor {
2016 public:
2017 explicit ActiveFunctionsCollector(List<Handle<JSFunction> >* active_functions,
2018 Object* active_code_marker)
2019 : active_functions_(active_functions),
2020 active_code_marker_(active_code_marker) { }
2021
2022 void VisitThread(Isolate* isolate, ThreadLocalTop* top) {
2023 CollectActiveFunctionsFromThread(isolate,
2024 top,
2025 active_functions_,
2026 active_code_marker_);
2027 }
2028
2029 private:
2030 List<Handle<JSFunction> >* active_functions_;
2031 Object* active_code_marker_;
2032};
2033
2034
2035class ActiveFunctionsRedirector : public ThreadVisitor {
2036 public:
2037 void VisitThread(Isolate* isolate, ThreadLocalTop* top) {
2038 RedirectActivationsToRecompiledCodeOnThread(isolate, top);
2039 }
2040};
2041
2042
lrn@chromium.org34e60782011-09-15 07:25:40 +00002043void Debug::PrepareForBreakPoints() {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002044 // If preparing for the first break point make sure to deoptimize all
2045 // functions as debugging does not work with optimized code.
2046 if (!has_break_points_) {
svenpanne@chromium.org876cca82013-03-18 14:43:20 +00002047 Deoptimizer::DeoptimizeAll(isolate_);
lrn@chromium.org34e60782011-09-15 07:25:40 +00002048
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002049 Handle<Code> lazy_compile =
2050 Handle<Code>(isolate_->builtins()->builtin(Builtins::kLazyCompile));
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002051
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00002052 // There will be at least one break point when we are done.
2053 has_break_points_ = true;
2054
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002055 // Keep the list of activated functions in a handlified list as it
2056 // is used both in GC and non-GC code.
2057 List<Handle<JSFunction> > active_functions(100);
lrn@chromium.org34e60782011-09-15 07:25:40 +00002058
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002059 {
2060 // We are going to iterate heap to find all functions without
2061 // debug break slots.
hpayer@chromium.org7c3372b2013-02-13 17:26:04 +00002062 Heap* heap = isolate_->heap();
2063 heap->CollectAllGarbage(Heap::kMakeHeapIterableMask,
2064 "preparing for breakpoints");
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002065
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002066 // Ensure no GC in this scope as we are going to use gc_metadata
2067 // field in the Code object to mark active functions.
rossberg@chromium.org79e79022013-06-03 15:43:46 +00002068 DisallowHeapAllocation no_allocation;
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002069
hpayer@chromium.org7c3372b2013-02-13 17:26:04 +00002070 Object* active_code_marker = heap->the_hole_value();
svenpanne@chromium.orgecb9dd62011-12-01 08:22:35 +00002071
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002072 CollectActiveFunctionsFromThread(isolate_,
2073 isolate_->thread_local_top(),
2074 &active_functions,
2075 active_code_marker);
2076 ActiveFunctionsCollector active_functions_collector(&active_functions,
2077 active_code_marker);
2078 isolate_->thread_manager()->IterateArchivedThreads(
2079 &active_functions_collector);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002080
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002081 // Scan the heap for all non-optimized functions which have no
2082 // debug break slots and are not active or inlined into an active
2083 // function and mark them for lazy compilation.
hpayer@chromium.org7c3372b2013-02-13 17:26:04 +00002084 HeapIterator iterator(heap);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002085 HeapObject* obj = NULL;
2086 while (((obj = iterator.next()) != NULL)) {
2087 if (obj->IsJSFunction()) {
2088 JSFunction* function = JSFunction::cast(obj);
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002089 SharedFunctionInfo* shared = function->shared();
mstarzinger@chromium.org1510d582013-06-28 14:00:48 +00002090
2091 if (!shared->allows_lazy_compilation()) continue;
2092 if (!shared->script()->IsScript()) continue;
2093 if (shared->code()->gc_metadata() == active_code_marker) continue;
2094
2095 Code::Kind kind = function->code()->kind();
2096 if (kind == Code::FUNCTION &&
2097 !function->code()->has_debug_break_slots()) {
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002098 function->set_code(*lazy_compile);
2099 function->shared()->set_code(*lazy_compile);
mstarzinger@chromium.org1510d582013-06-28 14:00:48 +00002100 } else if (kind == Code::BUILTIN &&
2101 (function->IsMarkedForInstallingRecompiledCode() ||
2102 function->IsInRecompileQueue() ||
2103 function->IsMarkedForLazyRecompilation() ||
2104 function->IsMarkedForParallelRecompilation())) {
2105 // Abort in-flight compilation.
2106 Code* shared_code = function->shared()->code();
2107 if (shared_code->kind() == Code::FUNCTION &&
2108 shared_code->has_debug_break_slots()) {
2109 function->set_code(shared_code);
2110 } else {
2111 function->set_code(*lazy_compile);
2112 function->shared()->set_code(*lazy_compile);
2113 }
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002114 }
2115 }
lrn@chromium.org34e60782011-09-15 07:25:40 +00002116 }
lrn@chromium.org34e60782011-09-15 07:25:40 +00002117
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002118 // Clear gc_metadata field.
2119 for (int i = 0; i < active_functions.length(); i++) {
2120 Handle<JSFunction> function = active_functions[i];
2121 function->shared()->code()->set_gc_metadata(Smi::FromInt(0));
2122 }
2123 }
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002124
2125 // Now recompile all functions with activation frames and and
2126 // patch the return address to run in the new compiled code.
2127 for (int i = 0; i < active_functions.length(); i++) {
2128 Handle<JSFunction> function = active_functions[i];
mmassi@chromium.org7028c052012-06-13 11:51:58 +00002129 Handle<SharedFunctionInfo> shared(function->shared());
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002130
2131 if (function->code()->kind() == Code::FUNCTION &&
2132 function->code()->has_debug_break_slots()) {
2133 // Nothing to do. Function code already had debug break slots.
2134 continue;
2135 }
2136
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002137 // If recompilation is not possible just skip it.
2138 if (shared->is_toplevel() ||
2139 !shared->allows_lazy_compilation() ||
2140 shared->code()->kind() == Code::BUILTIN) {
2141 continue;
2142 }
2143
2144 // Make sure that the shared full code is compiled with debug
2145 // break slots.
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00002146 if (!shared->code()->has_debug_break_slots()) {
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002147 // Try to compile the full code with debug break slots. If it
2148 // fails just keep the current code.
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00002149 Handle<Code> current_code(function->shared()->code());
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002150 shared->set_code(*lazy_compile);
2151 bool prev_force_debugger_active =
2152 isolate_->debugger()->force_debugger_active();
2153 isolate_->debugger()->set_force_debugger_active(true);
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00002154 ASSERT(current_code->kind() == Code::FUNCTION);
mmassi@chromium.org7028c052012-06-13 11:51:58 +00002155 CompileFullCodeForDebugging(function, current_code);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002156 isolate_->debugger()->set_force_debugger_active(
2157 prev_force_debugger_active);
2158 if (!shared->is_compiled()) {
2159 shared->set_code(*current_code);
2160 continue;
2161 }
2162 }
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002163
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002164 // Keep function code in sync with shared function info.
2165 function->set_code(shared->code());
lrn@chromium.org34e60782011-09-15 07:25:40 +00002166 }
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002167
2168 RedirectActivationsToRecompiledCodeOnThread(isolate_,
2169 isolate_->thread_local_top());
2170
2171 ActiveFunctionsRedirector active_functions_redirector;
2172 isolate_->thread_manager()->IterateArchivedThreads(
2173 &active_functions_redirector);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002174 }
lrn@chromium.org34e60782011-09-15 07:25:40 +00002175}
2176
2177
jkummerow@chromium.org78502a92012-09-06 13:50:42 +00002178Object* Debug::FindSharedFunctionInfoInScript(Handle<Script> script,
2179 int position) {
2180 // Iterate the heap looking for SharedFunctionInfo generated from the
2181 // script. The inner most SharedFunctionInfo containing the source position
2182 // for the requested break point is found.
2183 // NOTE: This might require several heap iterations. If the SharedFunctionInfo
2184 // which is found is not compiled it is compiled and the heap is iterated
2185 // again as the compilation might create inner functions from the newly
2186 // compiled function and the actual requested break point might be in one of
2187 // these functions.
2188 // NOTE: The below fix-point iteration depends on all functions that cannot be
2189 // compiled lazily without a context to not be compiled at all. Compilation
2190 // will be triggered at points where we do not need a context.
2191 bool done = false;
2192 // The current candidate for the source position:
2193 int target_start_position = RelocInfo::kNoPosition;
2194 Handle<JSFunction> target_function;
2195 Handle<SharedFunctionInfo> target;
hpayer@chromium.org7c3372b2013-02-13 17:26:04 +00002196 Heap* heap = isolate_->heap();
jkummerow@chromium.org78502a92012-09-06 13:50:42 +00002197 while (!done) {
2198 { // Extra scope for iterator and no-allocation.
hpayer@chromium.org7c3372b2013-02-13 17:26:04 +00002199 heap->EnsureHeapIsIterable();
rossberg@chromium.org79e79022013-06-03 15:43:46 +00002200 DisallowHeapAllocation no_alloc_during_heap_iteration;
hpayer@chromium.org7c3372b2013-02-13 17:26:04 +00002201 HeapIterator iterator(heap);
jkummerow@chromium.org78502a92012-09-06 13:50:42 +00002202 for (HeapObject* obj = iterator.next();
2203 obj != NULL; obj = iterator.next()) {
2204 bool found_next_candidate = false;
2205 Handle<JSFunction> function;
2206 Handle<SharedFunctionInfo> shared;
2207 if (obj->IsJSFunction()) {
2208 function = Handle<JSFunction>(JSFunction::cast(obj));
2209 shared = Handle<SharedFunctionInfo>(function->shared());
2210 ASSERT(shared->allows_lazy_compilation() || shared->is_compiled());
2211 found_next_candidate = true;
2212 } else if (obj->IsSharedFunctionInfo()) {
2213 shared = Handle<SharedFunctionInfo>(SharedFunctionInfo::cast(obj));
2214 // Skip functions that we cannot compile lazily without a context,
2215 // which is not available here, because there is no closure.
2216 found_next_candidate = shared->is_compiled() ||
2217 shared->allows_lazy_compilation_without_context();
2218 }
2219 if (!found_next_candidate) continue;
2220 if (shared->script() == *script) {
2221 // If the SharedFunctionInfo found has the requested script data and
2222 // contains the source position it is a candidate.
2223 int start_position = shared->function_token_position();
2224 if (start_position == RelocInfo::kNoPosition) {
2225 start_position = shared->start_position();
2226 }
2227 if (start_position <= position &&
2228 position <= shared->end_position()) {
2229 // If there is no candidate or this function is within the current
2230 // candidate this is the new candidate.
2231 if (target.is_null()) {
2232 target_start_position = start_position;
2233 target_function = function;
2234 target = shared;
2235 } else {
2236 if (target_start_position == start_position &&
2237 shared->end_position() == target->end_position()) {
2238 // If a top-level function contains only one function
2239 // declaration the source for the top-level and the function
2240 // is the same. In that case prefer the non top-level function.
2241 if (!shared->is_toplevel()) {
2242 target_start_position = start_position;
2243 target_function = function;
2244 target = shared;
2245 }
2246 } else if (target_start_position <= start_position &&
2247 shared->end_position() <= target->end_position()) {
2248 // This containment check includes equality as a function
2249 // inside a top-level function can share either start or end
2250 // position with the top-level function.
2251 target_start_position = start_position;
2252 target_function = function;
2253 target = shared;
2254 }
2255 }
2256 }
2257 }
2258 } // End for loop.
2259 } // End no-allocation scope.
2260
hpayer@chromium.org7c3372b2013-02-13 17:26:04 +00002261 if (target.is_null()) return heap->undefined_value();
jkummerow@chromium.org78502a92012-09-06 13:50:42 +00002262
2263 // There will be at least one break point when we are done.
2264 has_break_points_ = true;
2265
2266 // If the candidate found is compiled we are done.
2267 done = target->is_compiled();
2268 if (!done) {
2269 // If the candidate is not compiled, compile it to reveal any inner
2270 // functions which might contain the requested source position. This
2271 // will compile all inner functions that cannot be compiled without a
2272 // context, because Compiler::BuildFunctionInfo checks whether the
2273 // debugger is active.
2274 if (target_function.is_null()) {
2275 SharedFunctionInfo::CompileLazy(target, KEEP_EXCEPTION);
2276 } else {
2277 JSFunction::CompileLazy(target_function, KEEP_EXCEPTION);
2278 }
2279 }
2280 } // End while loop.
2281
2282 return *target;
2283}
2284
2285
lrn@chromium.org34e60782011-09-15 07:25:40 +00002286// Ensures the debug information is present for shared.
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00002287bool Debug::EnsureDebugInfo(Handle<SharedFunctionInfo> shared,
2288 Handle<JSFunction> function) {
verwaest@chromium.orgd4be0f02013-06-05 13:39:03 +00002289 Isolate* isolate = shared->GetIsolate();
2290
lrn@chromium.org34e60782011-09-15 07:25:40 +00002291 // Return if we already have the debug info for shared.
2292 if (HasDebugInfo(shared)) {
2293 ASSERT(shared->is_compiled());
2294 return true;
2295 }
2296
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00002297 // There will be at least one break point when we are done.
2298 has_break_points_ = true;
2299
2300 // Ensure function is compiled. Return false if this failed.
2301 if (!function.is_null() &&
2302 !JSFunction::EnsureCompiled(function, CLEAR_EXCEPTION)) {
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002303 return false;
2304 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002305
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002306 // Create the debug info object.
verwaest@chromium.orgd4be0f02013-06-05 13:39:03 +00002307 Handle<DebugInfo> debug_info = isolate->factory()->NewDebugInfo(shared);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002308
2309 // Add debug info to the list.
2310 DebugInfoListNode* node = new DebugInfoListNode(*debug_info);
2311 node->set_next(debug_info_list_);
2312 debug_info_list_ = node;
2313
kasper.lundbd3ec4e2008-07-09 11:06:54 +00002314 return true;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002315}
2316
2317
2318void Debug::RemoveDebugInfo(Handle<DebugInfo> debug_info) {
2319 ASSERT(debug_info_list_ != NULL);
2320 // Run through the debug info objects to find this one and remove it.
2321 DebugInfoListNode* prev = NULL;
2322 DebugInfoListNode* current = debug_info_list_;
2323 while (current != NULL) {
2324 if (*current->debug_info() == *debug_info) {
2325 // Unlink from list. If prev is NULL we are looking at the first element.
2326 if (prev == NULL) {
2327 debug_info_list_ = current->next();
2328 } else {
2329 prev->set_next(current->next());
2330 }
lrn@chromium.org7516f052011-03-30 08:52:27 +00002331 current->debug_info()->shared()->set_debug_info(
2332 isolate_->heap()->undefined_value());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002333 delete current;
2334
2335 // If there are no more debug info objects there are not more break
2336 // points.
2337 has_break_points_ = debug_info_list_ != NULL;
2338
2339 return;
2340 }
2341 // Move to next in list.
2342 prev = current;
2343 current = current->next();
2344 }
2345 UNREACHABLE();
2346}
2347
2348
2349void Debug::SetAfterBreakTarget(JavaScriptFrame* frame) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002350 HandleScope scope(isolate_);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002351
lrn@chromium.org34e60782011-09-15 07:25:40 +00002352 PrepareForBreakPoints();
2353
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002354 // Get the executing function in which the debug break occurred.
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00002355 Handle<JSFunction> function(JSFunction::cast(frame->function()));
2356 Handle<SharedFunctionInfo> shared(function->shared());
2357 if (!EnsureDebugInfo(shared, function)) {
kasper.lundbd3ec4e2008-07-09 11:06:54 +00002358 // Return if we failed to retrieve the debug info.
2359 return;
2360 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002361 Handle<DebugInfo> debug_info = GetDebugInfo(shared);
2362 Handle<Code> code(debug_info->code());
2363 Handle<Code> original_code(debug_info->original_code());
2364#ifdef DEBUG
2365 // Get the code which is actually executing.
vegorov@chromium.org74f333b2011-04-06 11:17:46 +00002366 Handle<Code> frame_code(frame->LookupCode());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002367 ASSERT(frame_code.is_identical_to(code));
2368#endif
2369
2370 // Find the call address in the running code. This address holds the call to
2371 // either a DebugBreakXXX or to the debug break return entry code if the
2372 // break point is still active after processing the break point.
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00002373 Address addr = frame->pc() - Assembler::kPatchDebugBreakSlotReturnOffset;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002374
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002375 // Check if the location is at JS exit or debug break slot.
ager@chromium.orga1645e22009-09-09 19:27:10 +00002376 bool at_js_return = false;
2377 bool break_at_js_return_active = false;
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002378 bool at_debug_break_slot = false;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002379 RelocIterator it(debug_info->code());
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002380 while (!it.done() && !at_js_return && !at_debug_break_slot) {
ager@chromium.org236ad962008-09-25 09:45:57 +00002381 if (RelocInfo::IsJSReturn(it.rinfo()->rmode())) {
ager@chromium.orga1645e22009-09-09 19:27:10 +00002382 at_js_return = (it.rinfo()->pc() ==
2383 addr - Assembler::kPatchReturnSequenceAddressOffset);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002384 break_at_js_return_active = it.rinfo()->IsPatchedReturnSequence();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002385 }
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002386 if (RelocInfo::IsDebugBreakSlot(it.rinfo()->rmode())) {
2387 at_debug_break_slot = (it.rinfo()->pc() ==
2388 addr - Assembler::kPatchDebugBreakSlotAddressOffset);
2389 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002390 it.next();
2391 }
2392
2393 // Handle the jump to continue execution after break point depending on the
2394 // break location.
ager@chromium.orga1645e22009-09-09 19:27:10 +00002395 if (at_js_return) {
2396 // If the break point as return is still active jump to the corresponding
2397 // place in the original code. If not the break point was removed during
2398 // break point processing.
2399 if (break_at_js_return_active) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002400 addr += original_code->instruction_start() - code->instruction_start();
2401 }
2402
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00002403 // Move back to where the call instruction sequence started.
2404 thread_local_.after_break_target_ =
2405 addr - Assembler::kPatchReturnSequenceAddressOffset;
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002406 } else if (at_debug_break_slot) {
2407 // Address of where the debug break slot starts.
2408 addr = addr - Assembler::kPatchDebugBreakSlotAddressOffset;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002409
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002410 // Continue just after the slot.
2411 thread_local_.after_break_target_ = addr + Assembler::kDebugBreakSlotLength;
2412 } else if (IsDebugBreak(Assembler::target_address_at(addr))) {
2413 // We now know that there is still a debug break call at the target address,
2414 // so the break point is still there and the original code will hold the
2415 // address to jump to in order to complete the call which is replaced by a
2416 // call to DebugBreakXXX.
2417
2418 // Find the corresponding address in the original code.
2419 addr += original_code->instruction_start() - code->instruction_start();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002420
2421 // Install jump to the call address in the original code. This will be the
2422 // call which was overwritten by the call to DebugBreakXXX.
2423 thread_local_.after_break_target_ = Assembler::target_address_at(addr);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002424 } else {
2425 // There is no longer a break point present. Don't try to look in the
2426 // original code as the running code will have the right address. This takes
2427 // care of the case where the last break point is removed from the function
2428 // and therefore no "original code" is available.
2429 thread_local_.after_break_target_ = Assembler::target_address_at(addr);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002430 }
2431}
2432
2433
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00002434bool Debug::IsBreakAtReturn(JavaScriptFrame* frame) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002435 HandleScope scope(isolate_);
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00002436
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00002437 // If there are no break points this cannot be break at return, as
2438 // the debugger statement and stack guard bebug break cannot be at
2439 // return.
2440 if (!has_break_points_) {
2441 return false;
2442 }
2443
lrn@chromium.org34e60782011-09-15 07:25:40 +00002444 PrepareForBreakPoints();
2445
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00002446 // Get the executing function in which the debug break occurred.
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00002447 Handle<JSFunction> function(JSFunction::cast(frame->function()));
2448 Handle<SharedFunctionInfo> shared(function->shared());
2449 if (!EnsureDebugInfo(shared, function)) {
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00002450 // Return if we failed to retrieve the debug info.
2451 return false;
2452 }
2453 Handle<DebugInfo> debug_info = GetDebugInfo(shared);
2454 Handle<Code> code(debug_info->code());
2455#ifdef DEBUG
2456 // Get the code which is actually executing.
vegorov@chromium.org74f333b2011-04-06 11:17:46 +00002457 Handle<Code> frame_code(frame->LookupCode());
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00002458 ASSERT(frame_code.is_identical_to(code));
2459#endif
2460
2461 // Find the call address in the running code.
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00002462 Address addr = frame->pc() - Assembler::kPatchDebugBreakSlotReturnOffset;
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00002463
2464 // Check if the location is at JS return.
2465 RelocIterator it(debug_info->code());
2466 while (!it.done()) {
2467 if (RelocInfo::IsJSReturn(it.rinfo()->rmode())) {
2468 return (it.rinfo()->pc() ==
2469 addr - Assembler::kPatchReturnSequenceAddressOffset);
2470 }
2471 it.next();
2472 }
2473 return false;
2474}
2475
2476
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00002477void Debug::FramesHaveBeenDropped(StackFrame::Id new_break_frame_id,
whesse@chromium.orge90029b2010-08-02 11:52:17 +00002478 FrameDropMode mode,
2479 Object** restarter_frame_function_pointer) {
ulan@chromium.orgd9e468a2012-06-25 09:47:40 +00002480 if (mode != CURRENTLY_SET_MODE) {
2481 thread_local_.frame_drop_mode_ = mode;
2482 }
ager@chromium.org357bf652010-04-12 11:30:10 +00002483 thread_local_.break_frame_id_ = new_break_frame_id;
whesse@chromium.orge90029b2010-08-02 11:52:17 +00002484 thread_local_.restarter_frame_function_pointer_ =
2485 restarter_frame_function_pointer;
ager@chromium.org357bf652010-04-12 11:30:10 +00002486}
2487
2488
jkummerow@chromium.org212d9642012-05-11 15:02:09 +00002489const int Debug::FramePaddingLayout::kInitialSize = 1;
2490
2491
2492// Any even value bigger than kInitialSize as needed for stack scanning.
2493const int Debug::FramePaddingLayout::kPaddingValue = kInitialSize + 1;
2494
2495
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002496bool Debug::IsDebugGlobal(GlobalObject* global) {
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00002497 return IsLoaded() && global == debug_context()->global_object();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002498}
2499
2500
ager@chromium.org32912102009-01-16 10:38:43 +00002501void Debug::ClearMirrorCache() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002502 PostponeInterruptsScope postpone(isolate_);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002503 HandleScope scope(isolate_);
2504 ASSERT(isolate_->context() == *Debug::debug_context());
ager@chromium.org32912102009-01-16 10:38:43 +00002505
2506 // Clear the mirror cache.
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00002507 Handle<String> function_name = isolate_->factory()->InternalizeOneByteString(
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00002508 STATIC_ASCII_VECTOR("ClearMirrorCache"));
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00002509 Handle<Object> fun(
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00002510 isolate_->global_object()->GetPropertyNoExceptionThrown(*function_name),
2511 isolate_);
ager@chromium.org32912102009-01-16 10:38:43 +00002512 ASSERT(fun->IsJSFunction());
2513 bool caught_exception;
rossberg@chromium.org717967f2011-07-20 13:44:42 +00002514 Execution::TryCall(Handle<JSFunction>::cast(fun),
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00002515 Handle<JSObject>(Debug::debug_context()->global_object()),
ager@chromium.org32912102009-01-16 10:38:43 +00002516 0, NULL, &caught_exception);
2517}
2518
2519
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002520void Debug::CreateScriptCache() {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002521 Heap* heap = isolate_->heap();
2522 HandleScope scope(isolate_);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002523
2524 // Perform two GCs to get rid of all unreferenced scripts. The first GC gets
2525 // rid of all the cached script wrappers and the second gets rid of the
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002526 // scripts which are no longer referenced. The second also sweeps precisely,
2527 // which saves us doing yet another GC to make the heap iterable.
rossberg@chromium.org994edf62012-02-06 10:12:55 +00002528 heap->CollectAllGarbage(Heap::kNoGCFlags, "Debug::CreateScriptCache");
2529 heap->CollectAllGarbage(Heap::kMakeHeapIterableMask,
2530 "Debug::CreateScriptCache");
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002531
2532 ASSERT(script_cache_ == NULL);
2533 script_cache_ = new ScriptCache();
2534
2535 // Scan heap for Script objects.
2536 int count = 0;
hpayer@chromium.org7c3372b2013-02-13 17:26:04 +00002537 HeapIterator iterator(heap);
rossberg@chromium.org79e79022013-06-03 15:43:46 +00002538 DisallowHeapAllocation no_allocation;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002539
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002540 for (HeapObject* obj = iterator.next(); obj != NULL; obj = iterator.next()) {
sgjesse@chromium.org152a0b02009-10-07 13:50:16 +00002541 if (obj->IsScript() && Script::cast(obj)->HasValidSource()) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002542 script_cache_->Add(Handle<Script>(Script::cast(obj)));
2543 count++;
2544 }
2545 }
2546}
2547
2548
2549void Debug::DestroyScriptCache() {
2550 // Get rid of the script cache if it was created.
2551 if (script_cache_ != NULL) {
2552 delete script_cache_;
2553 script_cache_ = NULL;
2554 }
2555}
2556
2557
2558void Debug::AddScriptToScriptCache(Handle<Script> script) {
2559 if (script_cache_ != NULL) {
2560 script_cache_->Add(script);
2561 }
2562}
2563
2564
2565Handle<FixedArray> Debug::GetLoadedScripts() {
2566 // Create and fill the script cache when the loaded scripts is requested for
2567 // the first time.
2568 if (script_cache_ == NULL) {
2569 CreateScriptCache();
2570 }
2571
2572 // If the script cache is not active just return an empty array.
2573 ASSERT(script_cache_ != NULL);
2574 if (script_cache_ == NULL) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002575 isolate_->factory()->NewFixedArray(0);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002576 }
2577
2578 // Perform GC to get unreferenced scripts evicted from the cache before
2579 // returning the content.
rossberg@chromium.org994edf62012-02-06 10:12:55 +00002580 isolate_->heap()->CollectAllGarbage(Heap::kNoGCFlags,
2581 "Debug::GetLoadedScripts");
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002582
2583 // Get the scripts from the cache.
2584 return script_cache_->GetScripts();
2585}
2586
2587
2588void Debug::AfterGarbageCollection() {
2589 // Generate events for collected scripts.
2590 if (script_cache_ != NULL) {
2591 script_cache_->ProcessCollectedScripts();
2592 }
2593}
2594
2595
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00002596Debugger::Debugger(Isolate* isolate)
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +00002597 : debugger_access_(isolate->debugger_access()),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002598 event_listener_(Handle<Object>()),
2599 event_listener_data_(Handle<Object>()),
2600 compiling_natives_(false),
2601 is_loading_debugger_(false),
mstarzinger@chromium.orgde886792012-09-11 13:22:37 +00002602 live_edit_enabled_(true),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002603 never_unload_debugger_(false),
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002604 force_debugger_active_(false),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002605 message_handler_(NULL),
2606 debugger_unload_pending_(false),
2607 host_dispatch_handler_(NULL),
2608 dispatch_handler_access_(OS::CreateMutex()),
2609 debug_message_dispatch_handler_(NULL),
2610 message_dispatch_helper_thread_(NULL),
2611 host_dispatch_micros_(100 * 1000),
2612 agent_(NULL),
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00002613 command_queue_(isolate->logger(), kQueueInitialSize),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002614 command_received_(OS::CreateSemaphore(0)),
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00002615 event_command_queue_(isolate->logger(), kQueueInitialSize),
2616 isolate_(isolate) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002617}
2618
2619
2620Debugger::~Debugger() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002621 delete dispatch_handler_access_;
2622 dispatch_handler_access_ = 0;
2623 delete command_received_;
2624 command_received_ = 0;
2625}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002626
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002627
2628Handle<Object> Debugger::MakeJSObject(Vector<const char> constructor_name,
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002629 int argc,
2630 Handle<Object> argv[],
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002631 bool* caught_exception) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002632 ASSERT(isolate_->context() == *isolate_->debug()->debug_context());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002633
2634 // Create the execution state object.
lrn@chromium.org7516f052011-03-30 08:52:27 +00002635 Handle<String> constructor_str =
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00002636 isolate_->factory()->InternalizeUtf8String(constructor_name);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002637 Handle<Object> constructor(
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00002638 isolate_->global_object()->GetPropertyNoExceptionThrown(*constructor_str),
2639 isolate_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002640 ASSERT(constructor->IsJSFunction());
2641 if (!constructor->IsJSFunction()) {
2642 *caught_exception = true;
lrn@chromium.org7516f052011-03-30 08:52:27 +00002643 return isolate_->factory()->undefined_value();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002644 }
2645 Handle<Object> js_object = Execution::TryCall(
2646 Handle<JSFunction>::cast(constructor),
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00002647 Handle<JSObject>(isolate_->debug()->debug_context()->global_object()),
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002648 argc,
2649 argv,
2650 caught_exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002651 return js_object;
2652}
2653
2654
2655Handle<Object> Debugger::MakeExecutionState(bool* caught_exception) {
2656 // Create the execution state object.
lrn@chromium.org7516f052011-03-30 08:52:27 +00002657 Handle<Object> break_id = isolate_->factory()->NewNumberFromInt(
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002658 isolate_->debug()->break_id());
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002659 Handle<Object> argv[] = { break_id };
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002660 return MakeJSObject(CStrVector("MakeExecutionState"),
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002661 ARRAY_SIZE(argv),
2662 argv,
2663 caught_exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002664}
2665
2666
2667Handle<Object> Debugger::MakeBreakEvent(Handle<Object> exec_state,
2668 Handle<Object> break_points_hit,
2669 bool* caught_exception) {
2670 // Create the new break event object.
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002671 Handle<Object> argv[] = { exec_state, break_points_hit };
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002672 return MakeJSObject(CStrVector("MakeBreakEvent"),
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002673 ARRAY_SIZE(argv),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002674 argv,
2675 caught_exception);
2676}
2677
2678
2679Handle<Object> Debugger::MakeExceptionEvent(Handle<Object> exec_state,
2680 Handle<Object> exception,
2681 bool uncaught,
2682 bool* caught_exception) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002683 Factory* factory = isolate_->factory();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002684 // Create the new exception event object.
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002685 Handle<Object> argv[] = { exec_state,
2686 exception,
2687 factory->ToBoolean(uncaught) };
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002688 return MakeJSObject(CStrVector("MakeExceptionEvent"),
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002689 ARRAY_SIZE(argv),
2690 argv,
2691 caught_exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002692}
2693
2694
2695Handle<Object> Debugger::MakeNewFunctionEvent(Handle<Object> function,
2696 bool* caught_exception) {
2697 // Create the new function event object.
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002698 Handle<Object> argv[] = { function };
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002699 return MakeJSObject(CStrVector("MakeNewFunctionEvent"),
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002700 ARRAY_SIZE(argv),
2701 argv,
2702 caught_exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002703}
2704
2705
2706Handle<Object> Debugger::MakeCompileEvent(Handle<Script> script,
iposva@chromium.org245aa852009-02-10 00:49:54 +00002707 bool before,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002708 bool* caught_exception) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002709 Factory* factory = isolate_->factory();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002710 // Create the compile event object.
2711 Handle<Object> exec_state = MakeExecutionState(caught_exception);
iposva@chromium.org245aa852009-02-10 00:49:54 +00002712 Handle<Object> script_wrapper = GetScriptWrapper(script);
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002713 Handle<Object> argv[] = { exec_state,
2714 script_wrapper,
2715 factory->ToBoolean(before) };
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002716 return MakeJSObject(CStrVector("MakeCompileEvent"),
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002717 ARRAY_SIZE(argv),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002718 argv,
2719 caught_exception);
2720}
2721
2722
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002723Handle<Object> Debugger::MakeScriptCollectedEvent(int id,
2724 bool* caught_exception) {
2725 // Create the script collected event object.
2726 Handle<Object> exec_state = MakeExecutionState(caught_exception);
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00002727 Handle<Object> id_object = Handle<Smi>(Smi::FromInt(id), isolate_);
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002728 Handle<Object> argv[] = { exec_state, id_object };
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002729
2730 return MakeJSObject(CStrVector("MakeScriptCollectedEvent"),
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002731 ARRAY_SIZE(argv),
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002732 argv,
2733 caught_exception);
2734}
2735
2736
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002737void Debugger::OnException(Handle<Object> exception, bool uncaught) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002738 HandleScope scope(isolate_);
2739 Debug* debug = isolate_->debug();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002740
2741 // Bail out based on state or if there is no listener for this event
lrn@chromium.org7516f052011-03-30 08:52:27 +00002742 if (debug->InDebugger()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002743 if (!Debugger::EventActive(v8::Exception)) return;
2744
2745 // Bail out if exception breaks are not active
2746 if (uncaught) {
2747 // Uncaught exceptions are reported by either flags.
lrn@chromium.org7516f052011-03-30 08:52:27 +00002748 if (!(debug->break_on_uncaught_exception() ||
2749 debug->break_on_exception())) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002750 } else {
2751 // Caught exceptions are reported is activated.
lrn@chromium.org7516f052011-03-30 08:52:27 +00002752 if (!debug->break_on_exception()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002753 }
2754
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002755 // Enter the debugger.
2756 EnterDebugger debugger;
2757 if (debugger.FailedToEnter()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002758
2759 // Clear all current stepping setup.
lrn@chromium.org7516f052011-03-30 08:52:27 +00002760 debug->ClearStepping();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002761 // Create the event data object.
2762 bool caught_exception = false;
2763 Handle<Object> exec_state = MakeExecutionState(&caught_exception);
2764 Handle<Object> event_data;
2765 if (!caught_exception) {
2766 event_data = MakeExceptionEvent(exec_state, exception, uncaught,
2767 &caught_exception);
2768 }
2769 // Bail out and don't call debugger if exception.
2770 if (caught_exception) {
2771 return;
2772 }
2773
ager@chromium.org5ec48922009-05-05 07:25:34 +00002774 // Process debug event.
2775 ProcessDebugEvent(v8::Exception, Handle<JSObject>::cast(event_data), false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002776 // Return to continue execution from where the exception was thrown.
2777}
2778
2779
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002780void Debugger::OnDebugBreak(Handle<Object> break_points_hit,
2781 bool auto_continue) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002782 HandleScope scope(isolate_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002783
kasper.lund212ac232008-07-16 07:07:30 +00002784 // Debugger has already been entered by caller.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002785 ASSERT(isolate_->context() == *isolate_->debug()->debug_context());
kasper.lund212ac232008-07-16 07:07:30 +00002786
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002787 // Bail out if there is no listener for this event
2788 if (!Debugger::EventActive(v8::Break)) return;
2789
2790 // Debugger must be entered in advance.
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00002791 ASSERT(isolate_->context() == *isolate_->debug()->debug_context());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002792
2793 // Create the event data object.
2794 bool caught_exception = false;
2795 Handle<Object> exec_state = MakeExecutionState(&caught_exception);
2796 Handle<Object> event_data;
2797 if (!caught_exception) {
2798 event_data = MakeBreakEvent(exec_state, break_points_hit,
2799 &caught_exception);
2800 }
2801 // Bail out and don't call debugger if exception.
2802 if (caught_exception) {
2803 return;
2804 }
2805
ager@chromium.org5ec48922009-05-05 07:25:34 +00002806 // Process debug event.
2807 ProcessDebugEvent(v8::Break,
2808 Handle<JSObject>::cast(event_data),
2809 auto_continue);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002810}
2811
2812
2813void Debugger::OnBeforeCompile(Handle<Script> script) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002814 HandleScope scope(isolate_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002815
2816 // Bail out based on state or if there is no listener for this event
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002817 if (isolate_->debug()->InDebugger()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002818 if (compiling_natives()) return;
2819 if (!EventActive(v8::BeforeCompile)) return;
2820
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002821 // Enter the debugger.
2822 EnterDebugger debugger;
2823 if (debugger.FailedToEnter()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002824
2825 // Create the event data object.
2826 bool caught_exception = false;
iposva@chromium.org245aa852009-02-10 00:49:54 +00002827 Handle<Object> event_data = MakeCompileEvent(script, true, &caught_exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002828 // Bail out and don't call debugger if exception.
2829 if (caught_exception) {
2830 return;
2831 }
2832
ager@chromium.org5ec48922009-05-05 07:25:34 +00002833 // Process debug event.
2834 ProcessDebugEvent(v8::BeforeCompile,
2835 Handle<JSObject>::cast(event_data),
2836 true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002837}
2838
2839
2840// Handle debugger actions when a new script is compiled.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00002841void Debugger::OnAfterCompile(Handle<Script> script,
2842 AfterCompileFlags after_compile_flags) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002843 HandleScope scope(isolate_);
2844 Debug* debug = isolate_->debug();
kasper.lund212ac232008-07-16 07:07:30 +00002845
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002846 // Add the newly compiled script to the script cache.
lrn@chromium.org7516f052011-03-30 08:52:27 +00002847 debug->AddScriptToScriptCache(script);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002848
2849 // No more to do if not debugging.
ager@chromium.org71daaf62009-04-01 07:22:49 +00002850 if (!IsDebuggerActive()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002851
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002852 // No compile events while compiling natives.
2853 if (compiling_natives()) return;
2854
iposva@chromium.org245aa852009-02-10 00:49:54 +00002855 // Store whether in debugger before entering debugger.
lrn@chromium.org7516f052011-03-30 08:52:27 +00002856 bool in_debugger = debug->InDebugger();
iposva@chromium.org245aa852009-02-10 00:49:54 +00002857
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002858 // Enter the debugger.
2859 EnterDebugger debugger;
2860 if (debugger.FailedToEnter()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002861
2862 // If debugging there might be script break points registered for this
2863 // script. Make sure that these break points are set.
2864
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002865 // Get the function UpdateScriptBreakPoints (defined in debug-debugger.js).
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00002866 Handle<String> update_script_break_points_string =
2867 isolate_->factory()->InternalizeOneByteString(
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00002868 STATIC_ASCII_VECTOR("UpdateScriptBreakPoints"));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002869 Handle<Object> update_script_break_points =
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00002870 Handle<Object>(
2871 debug->debug_context()->global_object()->GetPropertyNoExceptionThrown(
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00002872 *update_script_break_points_string),
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00002873 isolate_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002874 if (!update_script_break_points->IsJSFunction()) {
2875 return;
2876 }
2877 ASSERT(update_script_break_points->IsJSFunction());
2878
2879 // Wrap the script object in a proper JS object before passing it
2880 // to JavaScript.
2881 Handle<JSValue> wrapper = GetScriptWrapper(script);
2882
2883 // Call UpdateScriptBreakPoints expect no exceptions.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002884 bool caught_exception;
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002885 Handle<Object> argv[] = { wrapper };
rossberg@chromium.org717967f2011-07-20 13:44:42 +00002886 Execution::TryCall(Handle<JSFunction>::cast(update_script_break_points),
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002887 Isolate::Current()->js_builtins_object(),
2888 ARRAY_SIZE(argv),
2889 argv,
2890 &caught_exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002891 if (caught_exception) {
2892 return;
2893 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002894 // Bail out based on state or if there is no listener for this event
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00002895 if (in_debugger && (after_compile_flags & SEND_WHEN_DEBUGGING) == 0) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002896 if (!Debugger::EventActive(v8::AfterCompile)) return;
2897
2898 // Create the compile state object.
2899 Handle<Object> event_data = MakeCompileEvent(script,
iposva@chromium.org245aa852009-02-10 00:49:54 +00002900 false,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002901 &caught_exception);
2902 // Bail out and don't call debugger if exception.
2903 if (caught_exception) {
2904 return;
2905 }
ager@chromium.org5ec48922009-05-05 07:25:34 +00002906 // Process debug event.
2907 ProcessDebugEvent(v8::AfterCompile,
2908 Handle<JSObject>::cast(event_data),
2909 true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002910}
2911
2912
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002913void Debugger::OnScriptCollected(int id) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002914 HandleScope scope(isolate_);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002915
2916 // No more to do if not debugging.
ulan@chromium.org56c14af2012-09-20 12:51:09 +00002917 if (isolate_->debug()->InDebugger()) return;
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002918 if (!IsDebuggerActive()) return;
2919 if (!Debugger::EventActive(v8::ScriptCollected)) return;
2920
2921 // Enter the debugger.
2922 EnterDebugger debugger;
2923 if (debugger.FailedToEnter()) return;
2924
2925 // Create the script collected state object.
2926 bool caught_exception = false;
2927 Handle<Object> event_data = MakeScriptCollectedEvent(id,
2928 &caught_exception);
2929 // Bail out and don't call debugger if exception.
2930 if (caught_exception) {
2931 return;
2932 }
2933
2934 // Process debug event.
2935 ProcessDebugEvent(v8::ScriptCollected,
2936 Handle<JSObject>::cast(event_data),
2937 true);
2938}
2939
2940
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002941void Debugger::ProcessDebugEvent(v8::DebugEvent event,
ager@chromium.org5ec48922009-05-05 07:25:34 +00002942 Handle<JSObject> event_data,
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002943 bool auto_continue) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002944 HandleScope scope(isolate_);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002945
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +00002946 // Clear any pending debug break if this is a real break.
2947 if (!auto_continue) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002948 isolate_->debug()->clear_interrupt_pending(DEBUGBREAK);
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +00002949 }
2950
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002951 // Create the execution state.
2952 bool caught_exception = false;
2953 Handle<Object> exec_state = MakeExecutionState(&caught_exception);
2954 if (caught_exception) {
2955 return;
2956 }
ager@chromium.org41826e72009-03-30 13:30:57 +00002957 // First notify the message handler if any.
2958 if (message_handler_ != NULL) {
ager@chromium.org5ec48922009-05-05 07:25:34 +00002959 NotifyMessageHandler(event,
2960 Handle<JSObject>::cast(exec_state),
2961 event_data,
2962 auto_continue);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002963 }
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00002964 // Notify registered debug event listener. This can be either a C or
2965 // a JavaScript function. Don't call event listener for v8::Break
2966 // here, if it's only a debug command -- they will be processed later.
2967 if ((event != v8::Break || !auto_continue) && !event_listener_.is_null()) {
2968 CallEventCallback(event, exec_state, event_data, NULL);
2969 }
2970 // Process pending debug commands.
2971 if (event == v8::Break) {
2972 while (!event_command_queue_.IsEmpty()) {
2973 CommandMessage command = event_command_queue_.Get();
2974 if (!event_listener_.is_null()) {
2975 CallEventCallback(v8::BreakForCommand,
2976 exec_state,
2977 event_data,
2978 command.client_data());
2979 }
2980 command.Dispose();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002981 }
2982 }
2983}
2984
2985
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00002986void Debugger::CallEventCallback(v8::DebugEvent event,
2987 Handle<Object> exec_state,
2988 Handle<Object> event_data,
2989 v8::Debug::ClientData* client_data) {
ager@chromium.orgea91cc52011-05-23 06:06:11 +00002990 if (event_listener_->IsForeign()) {
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00002991 CallCEventCallback(event, exec_state, event_data, client_data);
2992 } else {
2993 CallJSEventCallback(event, exec_state, event_data);
2994 }
2995}
2996
2997
2998void Debugger::CallCEventCallback(v8::DebugEvent event,
2999 Handle<Object> exec_state,
3000 Handle<Object> event_data,
3001 v8::Debug::ClientData* client_data) {
ager@chromium.orgea91cc52011-05-23 06:06:11 +00003002 Handle<Foreign> callback_obj(Handle<Foreign>::cast(event_listener_));
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00003003 v8::Debug::EventCallback2 callback =
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003004 FUNCTION_CAST<v8::Debug::EventCallback2>(
3005 callback_obj->foreign_address());
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00003006 EventDetailsImpl event_details(
3007 event,
3008 Handle<JSObject>::cast(exec_state),
3009 Handle<JSObject>::cast(event_data),
3010 event_listener_data_,
3011 client_data);
3012 callback(event_details);
3013}
3014
3015
3016void Debugger::CallJSEventCallback(v8::DebugEvent event,
3017 Handle<Object> exec_state,
3018 Handle<Object> event_data) {
3019 ASSERT(event_listener_->IsJSFunction());
3020 Handle<JSFunction> fun(Handle<JSFunction>::cast(event_listener_));
3021
3022 // Invoke the JavaScript debug event listener.
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00003023 Handle<Object> argv[] = { Handle<Object>(Smi::FromInt(event), isolate_),
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00003024 exec_state,
3025 event_data,
3026 event_listener_data_ };
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003027 bool caught_exception;
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00003028 Execution::TryCall(fun,
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00003029 isolate_->global_object(),
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00003030 ARRAY_SIZE(argv),
3031 argv,
3032 &caught_exception);
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00003033 // Silently ignore exceptions from debug event listeners.
3034}
3035
3036
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00003037Handle<Context> Debugger::GetDebugContext() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003038 never_unload_debugger_ = true;
3039 EnterDebugger debugger;
3040 return isolate_->debug()->debug_context();
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00003041}
3042
3043
ager@chromium.org71daaf62009-04-01 07:22:49 +00003044void Debugger::UnloadDebugger() {
lrn@chromium.org7516f052011-03-30 08:52:27 +00003045 Debug* debug = isolate_->debug();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003046
ager@chromium.org71daaf62009-04-01 07:22:49 +00003047 // Make sure that there are no breakpoints left.
lrn@chromium.org7516f052011-03-30 08:52:27 +00003048 debug->ClearAllBreakPoints();
ager@chromium.org71daaf62009-04-01 07:22:49 +00003049
3050 // Unload the debugger if feasible.
3051 if (!never_unload_debugger_) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00003052 debug->Unload();
ager@chromium.org71daaf62009-04-01 07:22:49 +00003053 }
3054
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003055 // Clear the flag indicating that the debugger should be unloaded.
3056 debugger_unload_pending_ = false;
ager@chromium.org71daaf62009-04-01 07:22:49 +00003057}
3058
3059
ager@chromium.org41826e72009-03-30 13:30:57 +00003060void Debugger::NotifyMessageHandler(v8::DebugEvent event,
ager@chromium.org5ec48922009-05-05 07:25:34 +00003061 Handle<JSObject> exec_state,
3062 Handle<JSObject> event_data,
ager@chromium.org41826e72009-03-30 13:30:57 +00003063 bool auto_continue) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00003064 HandleScope scope(isolate_);
ager@chromium.org41826e72009-03-30 13:30:57 +00003065
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003066 if (!isolate_->debug()->Load()) return;
ager@chromium.org41826e72009-03-30 13:30:57 +00003067
3068 // Process the individual events.
ager@chromium.org5ec48922009-05-05 07:25:34 +00003069 bool sendEventMessage = false;
ager@chromium.org41826e72009-03-30 13:30:57 +00003070 switch (event) {
3071 case v8::Break:
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00003072 case v8::BreakForCommand:
ager@chromium.org5ec48922009-05-05 07:25:34 +00003073 sendEventMessage = !auto_continue;
ager@chromium.org41826e72009-03-30 13:30:57 +00003074 break;
3075 case v8::Exception:
ager@chromium.org5ec48922009-05-05 07:25:34 +00003076 sendEventMessage = true;
ager@chromium.org41826e72009-03-30 13:30:57 +00003077 break;
3078 case v8::BeforeCompile:
3079 break;
3080 case v8::AfterCompile:
ager@chromium.org5ec48922009-05-05 07:25:34 +00003081 sendEventMessage = true;
ager@chromium.org41826e72009-03-30 13:30:57 +00003082 break;
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003083 case v8::ScriptCollected:
3084 sendEventMessage = true;
3085 break;
ager@chromium.org41826e72009-03-30 13:30:57 +00003086 case v8::NewFunction:
3087 break;
3088 default:
3089 UNREACHABLE();
3090 }
3091
ager@chromium.org5ec48922009-05-05 07:25:34 +00003092 // The debug command interrupt flag might have been set when the command was
3093 // added. It should be enough to clear the flag only once while we are in the
3094 // debugger.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003095 ASSERT(isolate_->debug()->InDebugger());
3096 isolate_->stack_guard()->Continue(DEBUGCOMMAND);
ager@chromium.org5ec48922009-05-05 07:25:34 +00003097
3098 // Notify the debugger that a debug event has occurred unless auto continue is
3099 // active in which case no event is send.
3100 if (sendEventMessage) {
3101 MessageImpl message = MessageImpl::NewEvent(
3102 event,
3103 auto_continue,
3104 Handle<JSObject>::cast(exec_state),
3105 Handle<JSObject>::cast(event_data));
3106 InvokeMessageHandler(message);
3107 }
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +00003108
3109 // If auto continue don't make the event cause a break, but process messages
3110 // in the queue if any. For script collected events don't even process
3111 // messages in the queue as the execution state might not be what is expected
3112 // by the client.
ager@chromium.org6ffc2172009-05-29 19:20:16 +00003113 if ((auto_continue && !HasCommands()) || event == v8::ScriptCollected) {
ager@chromium.org5ec48922009-05-05 07:25:34 +00003114 return;
3115 }
ager@chromium.org41826e72009-03-30 13:30:57 +00003116
ager@chromium.org41826e72009-03-30 13:30:57 +00003117 v8::TryCatch try_catch;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003118
3119 // DebugCommandProcessor goes here.
3120 v8::Local<v8::Object> cmd_processor;
3121 {
3122 v8::Local<v8::Object> api_exec_state =
3123 v8::Utils::ToLocal(Handle<JSObject>::cast(exec_state));
3124 v8::Local<v8::String> fun_name =
3125 v8::String::New("debugCommandProcessor");
3126 v8::Local<v8::Function> fun =
danno@chromium.orgf95d4b92013-06-13 14:40:17 +00003127 v8::Local<v8::Function>::Cast(api_exec_state->Get(fun_name));
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003128
3129 v8::Handle<v8::Boolean> running =
3130 auto_continue ? v8::True() : v8::False();
3131 static const int kArgc = 1;
3132 v8::Handle<Value> argv[kArgc] = { running };
danno@chromium.orgf95d4b92013-06-13 14:40:17 +00003133 cmd_processor = v8::Local<v8::Object>::Cast(
3134 fun->Call(api_exec_state, kArgc, argv));
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003135 if (try_catch.HasCaught()) {
3136 PrintLn(try_catch.Exception());
3137 return;
3138 }
ager@chromium.org41826e72009-03-30 13:30:57 +00003139 }
3140
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003141 bool running = auto_continue;
3142
ager@chromium.org41826e72009-03-30 13:30:57 +00003143 // Process requests from the debugger.
3144 while (true) {
3145 // Wait for new command in the queue.
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003146 if (Debugger::host_dispatch_handler_) {
3147 // In case there is a host dispatch - do periodic dispatches.
3148 if (!command_received_->Wait(host_dispatch_micros_)) {
3149 // Timout expired, do the dispatch.
3150 Debugger::host_dispatch_handler_();
3151 continue;
3152 }
3153 } else {
3154 // In case there is no host dispatch - just wait.
3155 command_received_->Wait();
3156 }
ager@chromium.org41826e72009-03-30 13:30:57 +00003157
ager@chromium.org41826e72009-03-30 13:30:57 +00003158 // Get the command from the queue.
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003159 CommandMessage command = command_queue_.Get();
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00003160 isolate_->logger()->DebugTag(
3161 "Got request from command queue, in interactive loop.");
ager@chromium.org71daaf62009-04-01 07:22:49 +00003162 if (!Debugger::IsDebuggerActive()) {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003163 // Delete command text and user data.
3164 command.Dispose();
ager@chromium.org41826e72009-03-30 13:30:57 +00003165 return;
3166 }
3167
ager@chromium.org41826e72009-03-30 13:30:57 +00003168 // Invoke JavaScript to process the debug request.
3169 v8::Local<v8::String> fun_name;
3170 v8::Local<v8::Function> fun;
3171 v8::Local<v8::Value> request;
3172 v8::TryCatch try_catch;
3173 fun_name = v8::String::New("processDebugRequest");
danno@chromium.orgf95d4b92013-06-13 14:40:17 +00003174 fun = v8::Local<v8::Function>::Cast(cmd_processor->Get(fun_name));
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003175
3176 request = v8::String::New(command.text().start(),
3177 command.text().length());
ager@chromium.org41826e72009-03-30 13:30:57 +00003178 static const int kArgc = 1;
3179 v8::Handle<Value> argv[kArgc] = { request };
3180 v8::Local<v8::Value> response_val = fun->Call(cmd_processor, kArgc, argv);
3181
3182 // Get the response.
3183 v8::Local<v8::String> response;
ager@chromium.org41826e72009-03-30 13:30:57 +00003184 if (!try_catch.HasCaught()) {
3185 // Get response string.
3186 if (!response_val->IsUndefined()) {
danno@chromium.orgf95d4b92013-06-13 14:40:17 +00003187 response = v8::Local<v8::String>::Cast(response_val);
ager@chromium.org41826e72009-03-30 13:30:57 +00003188 } else {
3189 response = v8::String::New("");
3190 }
3191
3192 // Log the JSON request/response.
3193 if (FLAG_trace_debug_json) {
3194 PrintLn(request);
3195 PrintLn(response);
3196 }
3197
3198 // Get the running state.
3199 fun_name = v8::String::New("isRunning");
danno@chromium.orgf95d4b92013-06-13 14:40:17 +00003200 fun = v8::Local<v8::Function>::Cast(cmd_processor->Get(fun_name));
ager@chromium.org41826e72009-03-30 13:30:57 +00003201 static const int kArgc = 1;
3202 v8::Handle<Value> argv[kArgc] = { response };
3203 v8::Local<v8::Value> running_val = fun->Call(cmd_processor, kArgc, argv);
3204 if (!try_catch.HasCaught()) {
3205 running = running_val->ToBoolean()->Value();
3206 }
3207 } else {
3208 // In case of failure the result text is the exception text.
3209 response = try_catch.Exception()->ToString();
3210 }
3211
ager@chromium.org41826e72009-03-30 13:30:57 +00003212 // Return the result.
ager@chromium.org5ec48922009-05-05 07:25:34 +00003213 MessageImpl message = MessageImpl::NewResponse(
3214 event,
3215 running,
3216 Handle<JSObject>::cast(exec_state),
3217 Handle<JSObject>::cast(event_data),
3218 Handle<String>(Utils::OpenHandle(*response)),
3219 command.client_data());
3220 InvokeMessageHandler(message);
3221 command.Dispose();
ager@chromium.org41826e72009-03-30 13:30:57 +00003222
3223 // Return from debug event processing if either the VM is put into the
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00003224 // running state (through a continue command) or auto continue is active
ager@chromium.org41826e72009-03-30 13:30:57 +00003225 // and there are no more commands queued.
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003226 if (running && !HasCommands()) {
ager@chromium.org41826e72009-03-30 13:30:57 +00003227 return;
3228 }
3229 }
3230}
3231
3232
iposva@chromium.org245aa852009-02-10 00:49:54 +00003233void Debugger::SetEventListener(Handle<Object> callback,
3234 Handle<Object> data) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00003235 HandleScope scope(isolate_);
3236 GlobalHandles* global_handles = isolate_->global_handles();
iposva@chromium.org245aa852009-02-10 00:49:54 +00003237
3238 // Clear the global handles for the event listener and the event listener data
3239 // object.
3240 if (!event_listener_.is_null()) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00003241 global_handles->Destroy(
iposva@chromium.org245aa852009-02-10 00:49:54 +00003242 reinterpret_cast<Object**>(event_listener_.location()));
3243 event_listener_ = Handle<Object>();
3244 }
3245 if (!event_listener_data_.is_null()) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00003246 global_handles->Destroy(
iposva@chromium.org245aa852009-02-10 00:49:54 +00003247 reinterpret_cast<Object**>(event_listener_data_.location()));
3248 event_listener_data_ = Handle<Object>();
3249 }
3250
3251 // If there is a new debug event listener register it together with its data
3252 // object.
3253 if (!callback->IsUndefined() && !callback->IsNull()) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003254 event_listener_ = Handle<Object>::cast(
lrn@chromium.org7516f052011-03-30 08:52:27 +00003255 global_handles->Create(*callback));
iposva@chromium.org245aa852009-02-10 00:49:54 +00003256 if (data.is_null()) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00003257 data = isolate_->factory()->undefined_value();
iposva@chromium.org245aa852009-02-10 00:49:54 +00003258 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003259 event_listener_data_ = Handle<Object>::cast(
lrn@chromium.org7516f052011-03-30 08:52:27 +00003260 global_handles->Create(*data));
iposva@chromium.org245aa852009-02-10 00:49:54 +00003261 }
3262
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003263 ListenersChanged();
iposva@chromium.org245aa852009-02-10 00:49:54 +00003264}
3265
3266
ager@chromium.org5ec48922009-05-05 07:25:34 +00003267void Debugger::SetMessageHandler(v8::Debug::MessageHandler2 handler) {
ager@chromium.org71daaf62009-04-01 07:22:49 +00003268 ScopedLock with(debugger_access_);
3269
ager@chromium.org381abbb2009-02-25 13:23:22 +00003270 message_handler_ = handler;
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003271 ListenersChanged();
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003272 if (handler == NULL) {
ager@chromium.org71daaf62009-04-01 07:22:49 +00003273 // Send an empty command to the debugger if in a break to make JavaScript
3274 // run again if the debugger is closed.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003275 if (isolate_->debug()->InDebugger()) {
ager@chromium.org71daaf62009-04-01 07:22:49 +00003276 ProcessCommand(Vector<const uint16_t>::empty());
3277 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003278 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003279}
3280
3281
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003282void Debugger::ListenersChanged() {
3283 if (IsDebuggerActive()) {
3284 // Disable the compilation cache when the debugger is active.
lrn@chromium.org7516f052011-03-30 08:52:27 +00003285 isolate_->compilation_cache()->Disable();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003286 debugger_unload_pending_ = false;
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003287 } else {
lrn@chromium.org7516f052011-03-30 08:52:27 +00003288 isolate_->compilation_cache()->Enable();
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003289 // Unload the debugger if event listener and message handler cleared.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003290 // Schedule this for later, because we may be in non-V8 thread.
3291 debugger_unload_pending_ = true;
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003292 }
3293}
3294
3295
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003296void Debugger::SetHostDispatchHandler(v8::Debug::HostDispatchHandler handler,
3297 int period) {
ager@chromium.org381abbb2009-02-25 13:23:22 +00003298 host_dispatch_handler_ = handler;
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003299 host_dispatch_micros_ = period * 1000;
ager@chromium.org381abbb2009-02-25 13:23:22 +00003300}
3301
3302
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003303void Debugger::SetDebugMessageDispatchHandler(
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003304 v8::Debug::DebugMessageDispatchHandler handler, bool provide_locker) {
3305 ScopedLock with(dispatch_handler_access_);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003306 debug_message_dispatch_handler_ = handler;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003307
3308 if (provide_locker && message_dispatch_helper_thread_ == NULL) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003309 message_dispatch_helper_thread_ = new MessageDispatchHelperThread(isolate_);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003310 message_dispatch_helper_thread_->Start();
3311 }
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003312}
3313
3314
ager@chromium.org41826e72009-03-30 13:30:57 +00003315// Calls the registered debug message handler. This callback is part of the
ager@chromium.org5ec48922009-05-05 07:25:34 +00003316// public API.
3317void Debugger::InvokeMessageHandler(MessageImpl message) {
ager@chromium.org71daaf62009-04-01 07:22:49 +00003318 ScopedLock with(debugger_access_);
3319
ager@chromium.org381abbb2009-02-25 13:23:22 +00003320 if (message_handler_ != NULL) {
ager@chromium.org5ec48922009-05-05 07:25:34 +00003321 message_handler_(message);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003322 }
ager@chromium.org41826e72009-03-30 13:30:57 +00003323}
3324
3325
3326// Puts a command coming from the public API on the queue. Creates
3327// a copy of the command string managed by the debugger. Up to this
3328// point, the command data was managed by the API client. Called
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003329// by the API client thread.
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003330void Debugger::ProcessCommand(Vector<const uint16_t> command,
3331 v8::Debug::ClientData* client_data) {
3332 // Need to cast away const.
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003333 CommandMessage message = CommandMessage::New(
ager@chromium.org41826e72009-03-30 13:30:57 +00003334 Vector<uint16_t>(const_cast<uint16_t*>(command.start()),
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003335 command.length()),
3336 client_data);
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00003337 isolate_->logger()->DebugTag("Put command on command_queue.");
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003338 command_queue_.Put(message);
ager@chromium.org41826e72009-03-30 13:30:57 +00003339 command_received_->Signal();
sgjesse@chromium.org3afc1582009-04-16 22:31:44 +00003340
3341 // Set the debug command break flag to have the command processed.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003342 if (!isolate_->debug()->InDebugger()) {
3343 isolate_->stack_guard()->DebugCommand();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003344 }
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003345
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003346 MessageDispatchHelperThread* dispatch_thread;
3347 {
3348 ScopedLock with(dispatch_handler_access_);
3349 dispatch_thread = message_dispatch_helper_thread_;
3350 }
3351
3352 if (dispatch_thread == NULL) {
3353 CallMessageDispatchHandler();
3354 } else {
3355 dispatch_thread->Schedule();
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003356 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003357}
3358
3359
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003360bool Debugger::HasCommands() {
ager@chromium.org41826e72009-03-30 13:30:57 +00003361 return !command_queue_.IsEmpty();
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003362}
3363
3364
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00003365void Debugger::EnqueueDebugCommand(v8::Debug::ClientData* client_data) {
3366 CommandMessage message = CommandMessage::New(Vector<uint16_t>(), client_data);
3367 event_command_queue_.Put(message);
3368
3369 // Set the debug command break flag to have the command processed.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003370 if (!isolate_->debug()->InDebugger()) {
3371 isolate_->stack_guard()->DebugCommand();
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00003372 }
3373}
3374
3375
ager@chromium.org71daaf62009-04-01 07:22:49 +00003376bool Debugger::IsDebuggerActive() {
3377 ScopedLock with(debugger_access_);
3378
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00003379 return message_handler_ != NULL ||
3380 !event_listener_.is_null() ||
3381 force_debugger_active_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003382}
3383
3384
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003385Handle<Object> Debugger::Call(Handle<JSFunction> fun,
3386 Handle<Object> data,
3387 bool* pending_exception) {
ager@chromium.org71daaf62009-04-01 07:22:49 +00003388 // When calling functions in the debugger prevent it from beeing unloaded.
3389 Debugger::never_unload_debugger_ = true;
3390
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003391 // Enter the debugger.
3392 EnterDebugger debugger;
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +00003393 if (debugger.FailedToEnter()) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00003394 return isolate_->factory()->undefined_value();
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003395 }
3396
3397 // Create the execution state.
3398 bool caught_exception = false;
3399 Handle<Object> exec_state = MakeExecutionState(&caught_exception);
3400 if (caught_exception) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00003401 return isolate_->factory()->undefined_value();
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003402 }
3403
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00003404 Handle<Object> argv[] = { exec_state, data };
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +00003405 Handle<Object> result = Execution::Call(
3406 fun,
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00003407 Handle<Object>(isolate_->debug()->debug_context_->global_proxy(),
3408 isolate_),
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00003409 ARRAY_SIZE(argv),
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +00003410 argv,
3411 pending_exception);
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003412 return result;
3413}
3414
3415
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003416static void StubMessageHandler2(const v8::Debug::Message& message) {
3417 // Simply ignore message.
3418}
3419
3420
3421bool Debugger::StartAgent(const char* name, int port,
3422 bool wait_for_connection) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003423 ASSERT(Isolate::Current() == isolate_);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003424 if (wait_for_connection) {
3425 // Suspend V8 if it is already running or set V8 to suspend whenever
3426 // it starts.
3427 // Provide stub message handler; V8 auto-continues each suspend
3428 // when there is no message handler; we doesn't need it.
3429 // Once become suspended, V8 will stay so indefinitely long, until remote
3430 // debugger connects and issues "continue" command.
3431 Debugger::message_handler_ = StubMessageHandler2;
3432 v8::Debug::DebugBreak();
3433 }
3434
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00003435 if (Socket::SetUp()) {
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00003436 if (agent_ == NULL) {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00003437 agent_ = new DebuggerAgent(name, port);
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00003438 agent_->Start();
3439 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003440 return true;
3441 }
3442
3443 return false;
3444}
3445
3446
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003447void Debugger::StopAgent() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003448 ASSERT(Isolate::Current() == isolate_);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003449 if (agent_ != NULL) {
3450 agent_->Shutdown();
3451 agent_->Join();
3452 delete agent_;
3453 agent_ = NULL;
3454 }
3455}
3456
3457
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003458void Debugger::WaitForAgent() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003459 ASSERT(Isolate::Current() == isolate_);
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003460 if (agent_ != NULL)
3461 agent_->WaitUntilListening();
3462}
3463
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003464
3465void Debugger::CallMessageDispatchHandler() {
3466 v8::Debug::DebugMessageDispatchHandler handler;
3467 {
3468 ScopedLock with(dispatch_handler_access_);
3469 handler = Debugger::debug_message_dispatch_handler_;
3470 }
3471 if (handler != NULL) {
3472 handler();
3473 }
3474}
3475
3476
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003477EnterDebugger::EnterDebugger()
3478 : isolate_(Isolate::Current()),
3479 prev_(isolate_->debug()->debugger_entry()),
3480 it_(isolate_),
3481 has_js_frames_(!it_.done()),
3482 save_(isolate_) {
3483 Debug* debug = isolate_->debug();
3484 ASSERT(prev_ != NULL || !debug->is_interrupt_pending(PREEMPT));
3485 ASSERT(prev_ != NULL || !debug->is_interrupt_pending(DEBUGBREAK));
3486
3487 // Link recursive debugger entry.
3488 debug->set_debugger_entry(this);
3489
3490 // Store the previous break id and frame id.
3491 break_id_ = debug->break_id();
3492 break_frame_id_ = debug->break_frame_id();
3493
3494 // Create the new break info. If there is no JavaScript frames there is no
3495 // break frame id.
3496 if (has_js_frames_) {
3497 debug->NewBreak(it_.frame()->id());
3498 } else {
3499 debug->NewBreak(StackFrame::NO_ID);
3500 }
3501
3502 // Make sure that debugger is loaded and enter the debugger context.
3503 load_failed_ = !debug->Load();
3504 if (!load_failed_) {
3505 // NOTE the member variable save which saves the previous context before
3506 // this change.
3507 isolate_->set_context(*debug->debug_context());
3508 }
3509}
3510
3511
3512EnterDebugger::~EnterDebugger() {
3513 ASSERT(Isolate::Current() == isolate_);
3514 Debug* debug = isolate_->debug();
3515
3516 // Restore to the previous break state.
3517 debug->SetBreak(break_frame_id_, break_id_);
3518
3519 // Check for leaving the debugger.
fschneider@chromium.org7d10be52012-04-10 12:30:14 +00003520 if (!load_failed_ && prev_ == NULL) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003521 // Clear mirror cache when leaving the debugger. Skip this if there is a
3522 // pending exception as clearing the mirror cache calls back into
3523 // JavaScript. This can happen if the v8::Debug::Call is used in which
3524 // case the exception should end up in the calling code.
3525 if (!isolate_->has_pending_exception()) {
3526 // Try to avoid any pending debug break breaking in the clear mirror
3527 // cache JavaScript code.
3528 if (isolate_->stack_guard()->IsDebugBreak()) {
3529 debug->set_interrupts_pending(DEBUGBREAK);
3530 isolate_->stack_guard()->Continue(DEBUGBREAK);
3531 }
3532 debug->ClearMirrorCache();
3533 }
3534
3535 // Request preemption and debug break when leaving the last debugger entry
3536 // if any of these where recorded while debugging.
3537 if (debug->is_interrupt_pending(PREEMPT)) {
3538 // This re-scheduling of preemption is to avoid starvation in some
3539 // debugging scenarios.
3540 debug->clear_interrupt_pending(PREEMPT);
3541 isolate_->stack_guard()->Preempt();
3542 }
3543 if (debug->is_interrupt_pending(DEBUGBREAK)) {
3544 debug->clear_interrupt_pending(DEBUGBREAK);
3545 isolate_->stack_guard()->DebugBreak();
3546 }
3547
3548 // If there are commands in the queue when leaving the debugger request
3549 // that these commands are processed.
3550 if (isolate_->debugger()->HasCommands()) {
3551 isolate_->stack_guard()->DebugCommand();
3552 }
3553
3554 // If leaving the debugger with the debugger no longer active unload it.
3555 if (!isolate_->debugger()->IsDebuggerActive()) {
3556 isolate_->debugger()->UnloadDebugger();
3557 }
3558 }
3559
3560 // Leaving this debugger entry.
3561 debug->set_debugger_entry(prev_);
3562}
3563
3564
ager@chromium.org5ec48922009-05-05 07:25:34 +00003565MessageImpl MessageImpl::NewEvent(DebugEvent event,
3566 bool running,
3567 Handle<JSObject> exec_state,
3568 Handle<JSObject> event_data) {
3569 MessageImpl message(true, event, running,
3570 exec_state, event_data, Handle<String>(), NULL);
3571 return message;
3572}
3573
3574
3575MessageImpl MessageImpl::NewResponse(DebugEvent event,
3576 bool running,
3577 Handle<JSObject> exec_state,
3578 Handle<JSObject> event_data,
3579 Handle<String> response_json,
3580 v8::Debug::ClientData* client_data) {
3581 MessageImpl message(false, event, running,
3582 exec_state, event_data, response_json, client_data);
3583 return message;
3584}
3585
3586
3587MessageImpl::MessageImpl(bool is_event,
3588 DebugEvent event,
3589 bool running,
3590 Handle<JSObject> exec_state,
3591 Handle<JSObject> event_data,
3592 Handle<String> response_json,
3593 v8::Debug::ClientData* client_data)
3594 : is_event_(is_event),
3595 event_(event),
3596 running_(running),
3597 exec_state_(exec_state),
3598 event_data_(event_data),
3599 response_json_(response_json),
3600 client_data_(client_data) {}
3601
3602
3603bool MessageImpl::IsEvent() const {
3604 return is_event_;
3605}
3606
3607
3608bool MessageImpl::IsResponse() const {
3609 return !is_event_;
3610}
3611
3612
3613DebugEvent MessageImpl::GetEvent() const {
3614 return event_;
3615}
3616
3617
3618bool MessageImpl::WillStartRunning() const {
3619 return running_;
3620}
3621
3622
3623v8::Handle<v8::Object> MessageImpl::GetExecutionState() const {
3624 return v8::Utils::ToLocal(exec_state_);
3625}
3626
3627
3628v8::Handle<v8::Object> MessageImpl::GetEventData() const {
3629 return v8::Utils::ToLocal(event_data_);
3630}
3631
3632
3633v8::Handle<v8::String> MessageImpl::GetJSON() const {
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +00003634 v8::HandleScope scope(
3635 reinterpret_cast<v8::Isolate*>(event_data_->GetIsolate()));
ager@chromium.org5ec48922009-05-05 07:25:34 +00003636
3637 if (IsEvent()) {
3638 // Call toJSONProtocol on the debug event object.
3639 Handle<Object> fun = GetProperty(event_data_, "toJSONProtocol");
3640 if (!fun->IsJSFunction()) {
3641 return v8::Handle<v8::String>();
3642 }
3643 bool caught_exception;
3644 Handle<Object> json = Execution::TryCall(Handle<JSFunction>::cast(fun),
3645 event_data_,
3646 0, NULL, &caught_exception);
3647 if (caught_exception || !json->IsString()) {
3648 return v8::Handle<v8::String>();
3649 }
3650 return scope.Close(v8::Utils::ToLocal(Handle<String>::cast(json)));
3651 } else {
3652 return v8::Utils::ToLocal(response_json_);
3653 }
3654}
3655
3656
3657v8::Handle<v8::Context> MessageImpl::GetEventContext() const {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003658 Isolate* isolate = Isolate::Current();
3659 v8::Handle<v8::Context> context = GetDebugEventContext(isolate);
3660 // Isolate::context() may be NULL when "script collected" event occures.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003661 ASSERT(!context.IsEmpty() || event_ == v8::ScriptCollected);
rossberg@chromium.org717967f2011-07-20 13:44:42 +00003662 return context;
ager@chromium.org5ec48922009-05-05 07:25:34 +00003663}
3664
3665
3666v8::Debug::ClientData* MessageImpl::GetClientData() const {
3667 return client_data_;
3668}
3669
3670
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003671EventDetailsImpl::EventDetailsImpl(DebugEvent event,
3672 Handle<JSObject> exec_state,
3673 Handle<JSObject> event_data,
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00003674 Handle<Object> callback_data,
3675 v8::Debug::ClientData* client_data)
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003676 : event_(event),
3677 exec_state_(exec_state),
3678 event_data_(event_data),
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00003679 callback_data_(callback_data),
3680 client_data_(client_data) {}
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003681
3682
3683DebugEvent EventDetailsImpl::GetEvent() const {
3684 return event_;
3685}
3686
3687
3688v8::Handle<v8::Object> EventDetailsImpl::GetExecutionState() const {
3689 return v8::Utils::ToLocal(exec_state_);
3690}
3691
3692
3693v8::Handle<v8::Object> EventDetailsImpl::GetEventData() const {
3694 return v8::Utils::ToLocal(event_data_);
3695}
3696
3697
3698v8::Handle<v8::Context> EventDetailsImpl::GetEventContext() const {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003699 return GetDebugEventContext(Isolate::Current());
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003700}
3701
3702
3703v8::Handle<v8::Value> EventDetailsImpl::GetCallbackData() const {
3704 return v8::Utils::ToLocal(callback_data_);
3705}
3706
3707
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00003708v8::Debug::ClientData* EventDetailsImpl::GetClientData() const {
3709 return client_data_;
3710}
3711
3712
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003713CommandMessage::CommandMessage() : text_(Vector<uint16_t>::empty()),
3714 client_data_(NULL) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003715}
3716
3717
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003718CommandMessage::CommandMessage(const Vector<uint16_t>& text,
3719 v8::Debug::ClientData* data)
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003720 : text_(text),
3721 client_data_(data) {
3722}
3723
3724
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003725CommandMessage::~CommandMessage() {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003726}
3727
3728
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003729void CommandMessage::Dispose() {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003730 text_.Dispose();
3731 delete client_data_;
3732 client_data_ = NULL;
3733}
3734
3735
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003736CommandMessage CommandMessage::New(const Vector<uint16_t>& command,
3737 v8::Debug::ClientData* data) {
3738 return CommandMessage(command.Clone(), data);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003739}
3740
3741
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003742CommandMessageQueue::CommandMessageQueue(int size) : start_(0), end_(0),
3743 size_(size) {
3744 messages_ = NewArray<CommandMessage>(size);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003745}
3746
3747
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003748CommandMessageQueue::~CommandMessageQueue() {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003749 while (!IsEmpty()) {
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003750 CommandMessage m = Get();
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003751 m.Dispose();
3752 }
kasper.lund7276f142008-07-30 08:49:36 +00003753 DeleteArray(messages_);
3754}
3755
3756
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003757CommandMessage CommandMessageQueue::Get() {
kasper.lund7276f142008-07-30 08:49:36 +00003758 ASSERT(!IsEmpty());
3759 int result = start_;
3760 start_ = (start_ + 1) % size_;
3761 return messages_[result];
3762}
3763
3764
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003765void CommandMessageQueue::Put(const CommandMessage& message) {
kasper.lund7276f142008-07-30 08:49:36 +00003766 if ((end_ + 1) % size_ == start_) {
3767 Expand();
3768 }
3769 messages_[end_] = message;
3770 end_ = (end_ + 1) % size_;
3771}
3772
3773
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003774void CommandMessageQueue::Expand() {
3775 CommandMessageQueue new_queue(size_ * 2);
kasper.lund7276f142008-07-30 08:49:36 +00003776 while (!IsEmpty()) {
3777 new_queue.Put(Get());
3778 }
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003779 CommandMessage* array_to_free = messages_;
kasper.lund7276f142008-07-30 08:49:36 +00003780 *this = new_queue;
3781 new_queue.messages_ = array_to_free;
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003782 // Make the new_queue empty so that it doesn't call Dispose on any messages.
3783 new_queue.start_ = new_queue.end_;
kasper.lund7276f142008-07-30 08:49:36 +00003784 // Automatic destructor called on new_queue, freeing array_to_free.
3785}
3786
3787
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00003788LockingCommandMessageQueue::LockingCommandMessageQueue(Logger* logger, int size)
3789 : logger_(logger), queue_(size) {
kasper.lund7276f142008-07-30 08:49:36 +00003790 lock_ = OS::CreateMutex();
3791}
3792
3793
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003794LockingCommandMessageQueue::~LockingCommandMessageQueue() {
kasper.lund7276f142008-07-30 08:49:36 +00003795 delete lock_;
3796}
3797
3798
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003799bool LockingCommandMessageQueue::IsEmpty() const {
kasper.lund7276f142008-07-30 08:49:36 +00003800 ScopedLock sl(lock_);
3801 return queue_.IsEmpty();
3802}
3803
3804
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003805CommandMessage LockingCommandMessageQueue::Get() {
kasper.lund7276f142008-07-30 08:49:36 +00003806 ScopedLock sl(lock_);
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003807 CommandMessage result = queue_.Get();
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00003808 logger_->DebugEvent("Get", result.text());
kasper.lund7276f142008-07-30 08:49:36 +00003809 return result;
3810}
3811
3812
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003813void LockingCommandMessageQueue::Put(const CommandMessage& message) {
kasper.lund7276f142008-07-30 08:49:36 +00003814 ScopedLock sl(lock_);
3815 queue_.Put(message);
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00003816 logger_->DebugEvent("Put", message.text());
kasper.lund7276f142008-07-30 08:49:36 +00003817}
3818
3819
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003820void LockingCommandMessageQueue::Clear() {
kasper.lund7276f142008-07-30 08:49:36 +00003821 ScopedLock sl(lock_);
3822 queue_.Clear();
3823}
3824
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003825
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003826MessageDispatchHelperThread::MessageDispatchHelperThread(Isolate* isolate)
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00003827 : Thread("v8:MsgDispHelpr"),
mstarzinger@chromium.orgf705b502013-04-04 11:38:09 +00003828 isolate_(isolate), sem_(OS::CreateSemaphore(0)),
3829 mutex_(OS::CreateMutex()), already_signalled_(false) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003830}
3831
3832
3833MessageDispatchHelperThread::~MessageDispatchHelperThread() {
3834 delete mutex_;
3835 delete sem_;
3836}
3837
3838
3839void MessageDispatchHelperThread::Schedule() {
3840 {
3841 ScopedLock lock(mutex_);
3842 if (already_signalled_) {
3843 return;
3844 }
3845 already_signalled_ = true;
3846 }
3847 sem_->Signal();
3848}
3849
3850
3851void MessageDispatchHelperThread::Run() {
3852 while (true) {
3853 sem_->Wait();
3854 {
3855 ScopedLock lock(mutex_);
3856 already_signalled_ = false;
3857 }
3858 {
mstarzinger@chromium.orgf705b502013-04-04 11:38:09 +00003859 Locker locker(reinterpret_cast<v8::Isolate*>(isolate_));
3860 isolate_->debugger()->CallMessageDispatchHandler();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003861 }
3862 }
3863}
3864
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003865#endif // ENABLE_DEBUGGER_SUPPORT
kasper.lund7276f142008-07-30 08:49:36 +00003866
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003867} } // namespace v8::internal