blob: 7d0225220681f27d9f60f59dcfb616f3972f5a91 [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
214// Find the break point closest to the supplied address.
215void BreakLocationIterator::FindBreakLocationFromAddress(Address pc) {
216 // Run through all break points to locate the one closest to the address.
217 int closest_break_point = 0;
218 int distance = kMaxInt;
219 while (!Done()) {
220 // Check if this break point is closer that what was previously found.
221 if (this->pc() < pc && pc - this->pc() < distance) {
222 closest_break_point = break_point();
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000223 distance = static_cast<int>(pc - this->pc());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000224 // Check whether we can't get any closer.
225 if (distance == 0) break;
226 }
227 Next();
228 }
229
230 // Move to the break point found.
231 Reset();
232 Next(closest_break_point);
233}
234
235
236// Find the break point closest to the supplied source position.
237void BreakLocationIterator::FindBreakLocationFromPosition(int position) {
238 // Run through all break points to locate the one closest to the source
239 // position.
240 int closest_break_point = 0;
241 int distance = kMaxInt;
242 while (!Done()) {
243 // Check if this break point is closer that what was previously found.
244 if (position <= statement_position() &&
245 statement_position() - position < distance) {
246 closest_break_point = break_point();
247 distance = statement_position() - position;
248 // Check whether we can't get any closer.
249 if (distance == 0) break;
250 }
251 Next();
252 }
253
254 // Move to the break point found.
255 Reset();
256 Next(closest_break_point);
257}
258
259
260void BreakLocationIterator::Reset() {
261 // Create relocation iterators for the two code objects.
262 if (reloc_iterator_ != NULL) delete reloc_iterator_;
263 if (reloc_iterator_original_ != NULL) delete reloc_iterator_original_;
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +0000264 reloc_iterator_ = new RelocIterator(
265 debug_info_->code(),
266 ~RelocInfo::ModeMask(RelocInfo::CODE_AGE_SEQUENCE));
267 reloc_iterator_original_ = new RelocIterator(
268 debug_info_->original_code(),
269 ~RelocInfo::ModeMask(RelocInfo::CODE_AGE_SEQUENCE));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000270
271 // Position at the first break point.
272 break_point_ = -1;
273 position_ = 1;
274 statement_position_ = 1;
275 Next();
276}
277
278
279bool BreakLocationIterator::Done() const {
280 return RinfoDone();
281}
282
283
284void BreakLocationIterator::SetBreakPoint(Handle<Object> break_point_object) {
285 // If there is not already a real break point here patch code with debug
286 // break.
287 if (!HasBreakPoint()) {
288 SetDebugBreak();
289 }
ager@chromium.orga1645e22009-09-09 19:27:10 +0000290 ASSERT(IsDebugBreak() || IsDebuggerStatement());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000291 // Set the break point information.
292 DebugInfo::SetBreakPoint(debug_info_, code_position(),
293 position(), statement_position(),
294 break_point_object);
295}
296
297
298void BreakLocationIterator::ClearBreakPoint(Handle<Object> break_point_object) {
299 // Clear the break point information.
300 DebugInfo::ClearBreakPoint(debug_info_, code_position(), break_point_object);
301 // If there are no more break points here remove the debug break.
302 if (!HasBreakPoint()) {
303 ClearDebugBreak();
304 ASSERT(!IsDebugBreak());
305 }
306}
307
308
309void BreakLocationIterator::SetOneShot() {
ager@chromium.orga1645e22009-09-09 19:27:10 +0000310 // Debugger statement always calls debugger. No need to modify it.
311 if (IsDebuggerStatement()) {
312 return;
313 }
314
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000315 // If there is a real break point here no more to do.
316 if (HasBreakPoint()) {
317 ASSERT(IsDebugBreak());
318 return;
319 }
320
321 // Patch code with debug break.
322 SetDebugBreak();
323}
324
325
326void BreakLocationIterator::ClearOneShot() {
ager@chromium.orga1645e22009-09-09 19:27:10 +0000327 // Debugger statement always calls debugger. No need to modify it.
328 if (IsDebuggerStatement()) {
329 return;
330 }
331
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000332 // If there is a real break point here no more to do.
333 if (HasBreakPoint()) {
334 ASSERT(IsDebugBreak());
335 return;
336 }
337
338 // Patch code removing debug break.
339 ClearDebugBreak();
340 ASSERT(!IsDebugBreak());
341}
342
343
344void BreakLocationIterator::SetDebugBreak() {
ager@chromium.orga1645e22009-09-09 19:27:10 +0000345 // Debugger statement always calls debugger. No need to modify it.
346 if (IsDebuggerStatement()) {
347 return;
348 }
349
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000350 // If there is already a break point here just return. This might happen if
v8.team.kasperl727e9952008-09-02 14:56:44 +0000351 // the same code is flooded with break points twice. Flooding the same
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000352 // function twice might happen when stepping in a function with an exception
353 // handler as the handler and the function is the same.
354 if (IsDebugBreak()) {
355 return;
356 }
357
ager@chromium.org236ad962008-09-25 09:45:57 +0000358 if (RelocInfo::IsJSReturn(rmode())) {
iposva@chromium.org245aa852009-02-10 00:49:54 +0000359 // Patch the frame exit code with a break point.
360 SetDebugBreakAtReturn();
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000361 } else if (IsDebugBreakSlot()) {
362 // Patch the code in the break slot.
363 SetDebugBreakAtSlot();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000364 } else {
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000365 // Patch the IC call.
366 SetDebugBreakAtIC();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000367 }
368 ASSERT(IsDebugBreak());
369}
370
371
372void BreakLocationIterator::ClearDebugBreak() {
ager@chromium.orga1645e22009-09-09 19:27:10 +0000373 // Debugger statement always calls debugger. No need to modify it.
374 if (IsDebuggerStatement()) {
375 return;
376 }
377
ager@chromium.org236ad962008-09-25 09:45:57 +0000378 if (RelocInfo::IsJSReturn(rmode())) {
iposva@chromium.org245aa852009-02-10 00:49:54 +0000379 // Restore the frame exit code.
380 ClearDebugBreakAtReturn();
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000381 } else if (IsDebugBreakSlot()) {
382 // Restore the code in the break slot.
383 ClearDebugBreakAtSlot();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000384 } else {
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000385 // Patch the IC call.
386 ClearDebugBreakAtIC();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000387 }
388 ASSERT(!IsDebugBreak());
389}
390
391
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +0000392void BreakLocationIterator::PrepareStepIn(Isolate* isolate) {
393 HandleScope scope(isolate);
ager@chromium.org381abbb2009-02-25 13:23:22 +0000394
ager@chromium.orga1645e22009-09-09 19:27:10 +0000395 // Step in can only be prepared if currently positioned on an IC call,
396 // construct call or CallFunction stub call.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000397 Address target = rinfo()->target_address();
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000398 Handle<Code> target_code(Code::GetCodeFromTargetAddress(target));
399 if (target_code->is_call_stub() || target_code->is_keyed_call_stub()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000400 // Step in through IC call is handled by the runtime system. Therefore make
401 // sure that the any current IC is cleared and the runtime system is
402 // called. If the executing code has a debug break at the location change
403 // the call in the original code as it is the code there that will be
404 // executed in place of the debug break call.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000405 Handle<Code> stub = ComputeCallDebugPrepareStepIn(
406 target_code->arguments_count(), target_code->kind());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000407 if (IsDebugBreak()) {
408 original_rinfo()->set_target_address(stub->entry());
409 } else {
410 rinfo()->set_target_address(stub->entry());
411 }
412 } else {
ager@chromium.orga1645e22009-09-09 19:27:10 +0000413#ifdef DEBUG
414 // All the following stuff is needed only for assertion checks so the code
415 // is wrapped in ifdef.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000416 Handle<Code> maybe_call_function_stub = target_code;
ager@chromium.orga1645e22009-09-09 19:27:10 +0000417 if (IsDebugBreak()) {
418 Address original_target = original_rinfo()->target_address();
419 maybe_call_function_stub =
420 Handle<Code>(Code::GetCodeFromTargetAddress(original_target));
421 }
422 bool is_call_function_stub =
423 (maybe_call_function_stub->kind() == Code::STUB &&
424 maybe_call_function_stub->major_key() == CodeStub::CallFunction);
425
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000426 // Step in through construct call requires no changes to the running code.
427 // Step in through getters/setters should already be prepared as well
428 // because caller of this function (Debug::PrepareStep) is expected to
429 // flood the top frame's function with one shot breakpoints.
ager@chromium.orga1645e22009-09-09 19:27:10 +0000430 // Step in through CallFunction stub should also be prepared by caller of
431 // this function (Debug::PrepareStep) which should flood target function
432 // with breakpoints.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000433 ASSERT(RelocInfo::IsConstructCall(rmode()) ||
434 target_code->is_inline_cache_stub() ||
435 is_call_function_stub);
ager@chromium.orga1645e22009-09-09 19:27:10 +0000436#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000437 }
438}
439
440
441// Check whether the break point is at a position which will exit the function.
442bool BreakLocationIterator::IsExit() const {
ager@chromium.org236ad962008-09-25 09:45:57 +0000443 return (RelocInfo::IsJSReturn(rmode()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000444}
445
446
447bool BreakLocationIterator::HasBreakPoint() {
448 return debug_info_->HasBreakPoint(code_position());
449}
450
451
452// Check whether there is a debug break at the current position.
453bool BreakLocationIterator::IsDebugBreak() {
ager@chromium.org236ad962008-09-25 09:45:57 +0000454 if (RelocInfo::IsJSReturn(rmode())) {
iposva@chromium.org245aa852009-02-10 00:49:54 +0000455 return IsDebugBreakAtReturn();
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000456 } else if (IsDebugBreakSlot()) {
457 return IsDebugBreakAtSlot();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000458 } else {
459 return Debug::IsDebugBreak(rinfo()->target_address());
460 }
461}
462
463
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000464void BreakLocationIterator::SetDebugBreakAtIC() {
465 // Patch the original code with the current address as the current address
466 // might have changed by the inline caching since the code was copied.
467 original_rinfo()->set_target_address(rinfo()->target_address());
468
469 RelocInfo::Mode mode = rmode();
470 if (RelocInfo::IsCodeTarget(mode)) {
471 Address target = rinfo()->target_address();
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000472 Handle<Code> target_code(Code::GetCodeFromTargetAddress(target));
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000473
474 // Patch the code to invoke the builtin debug break function matching the
475 // calling convention used by the call site.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000476 Handle<Code> dbgbrk_code(Debug::FindDebugBreak(target_code, mode));
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000477 rinfo()->set_target_address(dbgbrk_code->entry());
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000478 }
479}
480
481
482void BreakLocationIterator::ClearDebugBreakAtIC() {
483 // Patch the code to the original invoke.
484 rinfo()->set_target_address(original_rinfo()->target_address());
485}
486
487
ager@chromium.orga1645e22009-09-09 19:27:10 +0000488bool BreakLocationIterator::IsDebuggerStatement() {
ager@chromium.org5c838252010-02-19 08:53:10 +0000489 return RelocInfo::DEBUG_BREAK == rmode();
ager@chromium.orga1645e22009-09-09 19:27:10 +0000490}
491
492
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000493bool BreakLocationIterator::IsDebugBreakSlot() {
494 return RelocInfo::DEBUG_BREAK_SLOT == rmode();
495}
496
497
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000498Object* BreakLocationIterator::BreakPointObjects() {
499 return debug_info_->GetBreakPointObjects(code_position());
500}
501
502
ager@chromium.org381abbb2009-02-25 13:23:22 +0000503// Clear out all the debug break code. This is ONLY supposed to be used when
504// shutting down the debugger as it will leave the break point information in
505// DebugInfo even though the code is patched back to the non break point state.
506void BreakLocationIterator::ClearAllDebugBreak() {
507 while (!Done()) {
508 ClearDebugBreak();
509 Next();
510 }
511}
512
513
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000514bool BreakLocationIterator::RinfoDone() const {
515 ASSERT(reloc_iterator_->done() == reloc_iterator_original_->done());
516 return reloc_iterator_->done();
517}
518
519
520void BreakLocationIterator::RinfoNext() {
521 reloc_iterator_->next();
522 reloc_iterator_original_->next();
523#ifdef DEBUG
524 ASSERT(reloc_iterator_->done() == reloc_iterator_original_->done());
525 if (!reloc_iterator_->done()) {
526 ASSERT(rmode() == original_rmode());
527 }
528#endif
529}
530
531
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000532// Threading support.
533void Debug::ThreadInit() {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000534 thread_local_.break_count_ = 0;
535 thread_local_.break_id_ = 0;
536 thread_local_.break_frame_id_ = StackFrame::NO_ID;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000537 thread_local_.last_step_action_ = StepNone;
ager@chromium.org236ad962008-09-25 09:45:57 +0000538 thread_local_.last_statement_position_ = RelocInfo::kNoPosition;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000539 thread_local_.step_count_ = 0;
540 thread_local_.last_fp_ = 0;
lrn@chromium.org34e60782011-09-15 07:25:40 +0000541 thread_local_.queued_step_count_ = 0;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000542 thread_local_.step_into_fp_ = 0;
ager@chromium.orga1645e22009-09-09 19:27:10 +0000543 thread_local_.step_out_fp_ = 0;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000544 thread_local_.after_break_target_ = 0;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000545 // TODO(isolates): frames_are_dropped_?
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000546 thread_local_.debugger_entry_ = NULL;
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000547 thread_local_.pending_interrupts_ = 0;
ricow@chromium.org0b9f8502010-08-18 07:45:01 +0000548 thread_local_.restarter_frame_function_pointer_ = NULL;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000549}
550
551
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000552char* Debug::ArchiveDebug(char* storage) {
553 char* to = storage;
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000554 OS::MemCopy(to, reinterpret_cast<char*>(&thread_local_), sizeof(ThreadLocal));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000555 to += sizeof(ThreadLocal);
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000556 OS::MemCopy(to, reinterpret_cast<char*>(&registers_), sizeof(registers_));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000557 ThreadInit();
558 ASSERT(to <= storage + ArchiveSpacePerThread());
559 return storage + ArchiveSpacePerThread();
560}
561
562
563char* Debug::RestoreDebug(char* storage) {
564 char* from = storage;
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000565 OS::MemCopy(
566 reinterpret_cast<char*>(&thread_local_), from, sizeof(ThreadLocal));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000567 from += sizeof(ThreadLocal);
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000568 OS::MemCopy(reinterpret_cast<char*>(&registers_), from, sizeof(registers_));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000569 ASSERT(from <= storage + ArchiveSpacePerThread());
570 return storage + ArchiveSpacePerThread();
571}
572
573
574int Debug::ArchiveSpacePerThread() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000575 return sizeof(ThreadLocal) + sizeof(JSCallerSavedBuffer);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000576}
577
578
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000579// Frame structure (conforms InternalFrame structure):
580// -- code
581// -- SMI maker
582// -- function (slot is called "context")
583// -- frame base
584Object** Debug::SetUpFrameDropperFrame(StackFrame* bottom_js_frame,
585 Handle<Code> code) {
586 ASSERT(bottom_js_frame->is_java_script());
587
588 Address fp = bottom_js_frame->fp();
589
590 // Move function pointer into "context" slot.
591 Memory::Object_at(fp + StandardFrameConstants::kContextOffset) =
592 Memory::Object_at(fp + JavaScriptFrameConstants::kFunctionOffset);
593
594 Memory::Object_at(fp + InternalFrameConstants::kCodeOffset) = *code;
595 Memory::Object_at(fp + StandardFrameConstants::kMarkerOffset) =
596 Smi::FromInt(StackFrame::INTERNAL);
597
598 return reinterpret_cast<Object**>(&Memory::Object_at(
599 fp + StandardFrameConstants::kContextOffset));
600}
601
602const int Debug::kFrameDropperFrameSize = 4;
603
604
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000605void ScriptCache::Add(Handle<Script> script) {
lrn@chromium.org7516f052011-03-30 08:52:27 +0000606 GlobalHandles* global_handles = Isolate::Current()->global_handles();
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000607 // Create an entry in the hash map for the script.
608 int id = Smi::cast(script->id())->value();
609 HashMap::Entry* entry =
610 HashMap::Lookup(reinterpret_cast<void*>(id), Hash(id), true);
611 if (entry->value != NULL) {
612 ASSERT(*script == *reinterpret_cast<Script**>(entry->value));
613 return;
614 }
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000615 // Globalize the script object, make it weak and use the location of the
616 // global handle as the value in the hash map.
617 Handle<Script> script_ =
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000618 Handle<Script>::cast(
lrn@chromium.org7516f052011-03-30 08:52:27 +0000619 (global_handles->Create(*script)));
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000620 global_handles->MakeWeak(reinterpret_cast<Object**>(script_.location()),
621 this,
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000622 ScriptCache::HandleWeakScript);
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000623 entry->value = script_.location();
624}
625
626
627Handle<FixedArray> ScriptCache::GetScripts() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000628 Handle<FixedArray> instances = FACTORY->NewFixedArray(occupancy());
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000629 int count = 0;
630 for (HashMap::Entry* entry = Start(); entry != NULL; entry = Next(entry)) {
631 ASSERT(entry->value != NULL);
632 if (entry->value != NULL) {
633 instances->set(count, *reinterpret_cast<Script**>(entry->value));
634 count++;
635 }
636 }
637 return instances;
638}
639
640
641void ScriptCache::ProcessCollectedScripts() {
lrn@chromium.org7516f052011-03-30 08:52:27 +0000642 Debugger* debugger = Isolate::Current()->debugger();
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000643 for (int i = 0; i < collected_scripts_.length(); i++) {
lrn@chromium.org7516f052011-03-30 08:52:27 +0000644 debugger->OnScriptCollected(collected_scripts_[i]);
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000645 }
646 collected_scripts_.Clear();
647}
648
649
650void ScriptCache::Clear() {
lrn@chromium.org7516f052011-03-30 08:52:27 +0000651 GlobalHandles* global_handles = Isolate::Current()->global_handles();
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000652 // Iterate the script cache to get rid of all the weak handles.
653 for (HashMap::Entry* entry = Start(); entry != NULL; entry = Next(entry)) {
654 ASSERT(entry != NULL);
655 Object** location = reinterpret_cast<Object**>(entry->value);
656 ASSERT((*location)->IsScript());
lrn@chromium.org7516f052011-03-30 08:52:27 +0000657 global_handles->ClearWeakness(location);
658 global_handles->Destroy(location);
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000659 }
660 // Clear the content of the hash map.
661 HashMap::Clear();
662}
663
664
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000665void ScriptCache::HandleWeakScript(v8::Isolate* isolate,
rossberg@chromium.org79e79022013-06-03 15:43:46 +0000666 v8::Persistent<v8::Value>* obj,
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000667 void* data) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000668 ScriptCache* script_cache = reinterpret_cast<ScriptCache*>(data);
669 // Find the location of the global handle.
670 Script** location =
rossberg@chromium.org79e79022013-06-03 15:43:46 +0000671 reinterpret_cast<Script**>(Utils::OpenHandle(**obj).location());
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000672 ASSERT((*location)->IsScript());
673
674 // Remove the entry from the cache.
675 int id = Smi::cast((*location)->id())->value();
676 script_cache->Remove(reinterpret_cast<void*>(id), Hash(id));
677 script_cache->collected_scripts_.Add(id);
678
679 // Clear the weak handle.
rossberg@chromium.org79e79022013-06-03 15:43:46 +0000680 obj->Dispose(isolate);
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000681}
682
683
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000684void Debug::SetUp(bool create_heap_objects) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000685 ThreadInit();
686 if (create_heap_objects) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000687 // Get code to handle debug break on return.
688 debug_break_return_ =
lrn@chromium.org7516f052011-03-30 08:52:27 +0000689 isolate_->builtins()->builtin(Builtins::kReturn_DebugBreak);
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000690 ASSERT(debug_break_return_->IsCode());
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000691 // Get code to handle debug break in debug break slots.
692 debug_break_slot_ =
lrn@chromium.org7516f052011-03-30 08:52:27 +0000693 isolate_->builtins()->builtin(Builtins::kSlot_DebugBreak);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000694 ASSERT(debug_break_slot_->IsCode());
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000695 }
696}
697
698
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000699void Debug::HandleWeakDebugInfo(v8::Isolate* isolate,
rossberg@chromium.org79e79022013-06-03 15:43:46 +0000700 v8::Persistent<v8::Value>* obj,
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000701 void* data) {
702 Debug* debug = reinterpret_cast<Isolate*>(isolate)->debug();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000703 DebugInfoListNode* node = reinterpret_cast<DebugInfoListNode*>(data);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000704 // We need to clear all breakpoints associated with the function to restore
705 // original code and avoid patching the code twice later because
706 // the function will live in the heap until next gc, and can be found by
jkummerow@chromium.org78502a92012-09-06 13:50:42 +0000707 // Debug::FindSharedFunctionInfoInScript.
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000708 BreakLocationIterator it(node->debug_info(), ALL_BREAK_LOCATIONS);
709 it.ClearAllDebugBreak();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000710 debug->RemoveDebugInfo(node->debug_info());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000711#ifdef DEBUG
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000712 node = debug->debug_info_list_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000713 while (node != NULL) {
714 ASSERT(node != reinterpret_cast<DebugInfoListNode*>(data));
715 node = node->next();
716 }
717#endif
718}
719
720
721DebugInfoListNode::DebugInfoListNode(DebugInfo* debug_info): next_(NULL) {
lrn@chromium.org7516f052011-03-30 08:52:27 +0000722 GlobalHandles* global_handles = Isolate::Current()->global_handles();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000723 // Globalize the request debug info object and make it weak.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000724 debug_info_ = Handle<DebugInfo>::cast(
lrn@chromium.org7516f052011-03-30 08:52:27 +0000725 (global_handles->Create(debug_info)));
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000726 global_handles->MakeWeak(reinterpret_cast<Object**>(debug_info_.location()),
727 this,
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000728 Debug::HandleWeakDebugInfo);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000729}
730
731
732DebugInfoListNode::~DebugInfoListNode() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000733 Isolate::Current()->global_handles()->Destroy(
734 reinterpret_cast<Object**>(debug_info_.location()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000735}
736
737
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000738bool Debug::CompileDebuggerScript(int index) {
lrn@chromium.org7516f052011-03-30 08:52:27 +0000739 Isolate* isolate = Isolate::Current();
740 Factory* factory = isolate->factory();
741 HandleScope scope(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000742
kasper.lund44510672008-07-25 07:37:58 +0000743 // Bail out if the index is invalid.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000744 if (index == -1) {
745 return false;
746 }
kasper.lund44510672008-07-25 07:37:58 +0000747
748 // Find source and name for the requested script.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000749 Handle<String> source_code =
lrn@chromium.org7516f052011-03-30 08:52:27 +0000750 isolate->bootstrapper()->NativesSourceLookup(index);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000751 Vector<const char> name = Natives::GetScriptName(index);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000752 Handle<String> script_name = factory->NewStringFromAscii(name);
yangguo@chromium.org355cfd12012-08-29 15:32:24 +0000753 Handle<Context> context = isolate->native_context();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000754
755 // Compile the script.
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000756 Handle<SharedFunctionInfo> function_info;
757 function_info = Compiler::Compile(source_code,
758 script_name,
yangguo@chromium.org355cfd12012-08-29 15:32:24 +0000759 0, 0,
760 context,
761 NULL, NULL,
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000762 Handle<String>::null(),
763 NATIVES_CODE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000764
765 // Silently ignore stack overflows during compilation.
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000766 if (function_info.is_null()) {
lrn@chromium.org7516f052011-03-30 08:52:27 +0000767 ASSERT(isolate->has_pending_exception());
768 isolate->clear_pending_exception();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000769 return false;
770 }
771
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000772 // Execute the shared function in the debugger context.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000773 bool caught_exception;
kasper.lund44510672008-07-25 07:37:58 +0000774 Handle<JSFunction> function =
lrn@chromium.org7516f052011-03-30 08:52:27 +0000775 factory->NewFunctionFromSharedFunctionInfo(function_info, context);
rossberg@chromium.org717967f2011-07-20 13:44:42 +0000776
fschneider@chromium.org7d10be52012-04-10 12:30:14 +0000777 Handle<Object> exception =
ulan@chromium.org09d7ab52013-02-25 15:50:35 +0000778 Execution::TryCall(function,
779 Handle<Object>(context->global_object(), isolate),
780 0,
781 NULL,
782 &caught_exception);
kasper.lund44510672008-07-25 07:37:58 +0000783
784 // Check for caught exceptions.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000785 if (caught_exception) {
fschneider@chromium.org7d10be52012-04-10 12:30:14 +0000786 ASSERT(!isolate->has_pending_exception());
787 MessageLocation computed_location;
788 isolate->ComputeLocation(&computed_location);
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000789 Handle<Object> message = MessageHandler::MakeMessageObject(
fschneider@chromium.org7d10be52012-04-10 12:30:14 +0000790 "error_loading_debugger", &computed_location,
791 Vector<Handle<Object> >::empty(), Handle<String>(), Handle<JSArray>());
792 ASSERT(!isolate->has_pending_exception());
mmassi@chromium.org49a44672012-12-04 13:52:03 +0000793 if (!exception.is_null()) {
794 isolate->set_pending_exception(*exception);
795 MessageHandler::ReportMessage(Isolate::Current(), NULL, message);
796 isolate->clear_pending_exception();
797 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000798 return false;
799 }
800
kasper.lund44510672008-07-25 07:37:58 +0000801 // Mark this script as native and return successfully.
802 Handle<Script> script(Script::cast(function->shared()->script()));
ager@chromium.orge2902be2009-06-08 12:21:35 +0000803 script->set_type(Smi::FromInt(Script::TYPE_NATIVE));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000804 return true;
805}
806
807
808bool Debug::Load() {
809 // Return if debugger is already loaded.
kasper.lund212ac232008-07-16 07:07:30 +0000810 if (IsLoaded()) return true;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000811
lrn@chromium.org7516f052011-03-30 08:52:27 +0000812 Debugger* debugger = isolate_->debugger();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000813
kasper.lund44510672008-07-25 07:37:58 +0000814 // Bail out if we're already in the process of compiling the native
815 // JavaScript source code for the debugger.
lrn@chromium.org7516f052011-03-30 08:52:27 +0000816 if (debugger->compiling_natives() ||
817 debugger->is_loading_debugger())
mads.s.agercbaa0602008-08-14 13:41:48 +0000818 return false;
lrn@chromium.org7516f052011-03-30 08:52:27 +0000819 debugger->set_loading_debugger(true);
kasper.lund44510672008-07-25 07:37:58 +0000820
821 // Disable breakpoints and interrupts while compiling and running the
822 // debugger scripts including the context creation code.
823 DisableBreak disable(true);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000824 PostponeInterruptsScope postpone(isolate_);
kasper.lund44510672008-07-25 07:37:58 +0000825
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000826 // Create the debugger context.
lrn@chromium.org7516f052011-03-30 08:52:27 +0000827 HandleScope scope(isolate_);
kasper.lund44510672008-07-25 07:37:58 +0000828 Handle<Context> context =
lrn@chromium.org7516f052011-03-30 08:52:27 +0000829 isolate_->bootstrapper()->CreateEnvironment(
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000830 Handle<Object>::null(),
831 v8::Handle<ObjectTemplate>(),
832 NULL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000833
fschneider@chromium.org7d10be52012-04-10 12:30:14 +0000834 // Fail if no context could be created.
835 if (context.is_null()) return false;
836
kasper.lund44510672008-07-25 07:37:58 +0000837 // Use the debugger context.
lrn@chromium.org7516f052011-03-30 08:52:27 +0000838 SaveContext save(isolate_);
839 isolate_->set_context(*context);
kasper.lund44510672008-07-25 07:37:58 +0000840
841 // Expose the builtins object in the debugger context.
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000842 Handle<String> key = isolate_->factory()->InternalizeOneByteString(
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +0000843 STATIC_ASCII_VECTOR("builtins"));
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000844 Handle<GlobalObject> global = Handle<GlobalObject>(context->global_object());
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +0000845 RETURN_IF_EMPTY_HANDLE_VALUE(
lrn@chromium.org7516f052011-03-30 08:52:27 +0000846 isolate_,
ulan@chromium.org09d7ab52013-02-25 15:50:35 +0000847 JSReceiver::SetProperty(global,
848 key,
849 Handle<Object>(global->builtins(), isolate_),
850 NONE,
851 kNonStrictMode),
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +0000852 false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000853
854 // Compile the JavaScript for the debugger in the debugger context.
lrn@chromium.org7516f052011-03-30 08:52:27 +0000855 debugger->set_compiling_natives(true);
kasper.lund44510672008-07-25 07:37:58 +0000856 bool caught_exception =
857 !CompileDebuggerScript(Natives::GetIndex("mirror")) ||
858 !CompileDebuggerScript(Natives::GetIndex("debug"));
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000859
860 if (FLAG_enable_liveedit) {
861 caught_exception = caught_exception ||
862 !CompileDebuggerScript(Natives::GetIndex("liveedit"));
863 }
864
lrn@chromium.org7516f052011-03-30 08:52:27 +0000865 debugger->set_compiling_natives(false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000866
mads.s.agercbaa0602008-08-14 13:41:48 +0000867 // Make sure we mark the debugger as not loading before we might
868 // return.
lrn@chromium.org7516f052011-03-30 08:52:27 +0000869 debugger->set_loading_debugger(false);
mads.s.agercbaa0602008-08-14 13:41:48 +0000870
kasper.lund44510672008-07-25 07:37:58 +0000871 // Check for caught exceptions.
872 if (caught_exception) return false;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000873
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000874 // Debugger loaded, create debugger context global handle.
875 debug_context_ = Handle<Context>::cast(
876 isolate_->global_handles()->Create(*context));
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000877
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000878 return true;
879}
880
881
882void Debug::Unload() {
883 // Return debugger is not loaded.
884 if (!IsLoaded()) {
885 return;
886 }
887
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000888 // Clear the script cache.
889 DestroyScriptCache();
890
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000891 // Clear debugger context global handle.
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000892 isolate_->global_handles()->Destroy(
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000893 reinterpret_cast<Object**>(debug_context_.location()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000894 debug_context_ = Handle<Context>();
895}
896
897
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000898// Set the flag indicating that preemption happened during debugging.
899void Debug::PreemptionWhileInDebugger() {
900 ASSERT(InDebugger());
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000901 Debug::set_interrupts_pending(PREEMPT);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000902}
903
904
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000905void Debug::Iterate(ObjectVisitor* v) {
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000906 v->VisitPointer(BitCast<Object**>(&(debug_break_return_)));
907 v->VisitPointer(BitCast<Object**>(&(debug_break_slot_)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000908}
909
910
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000911Object* Debug::Break(Arguments args) {
912 Heap* heap = isolate_->heap();
913 HandleScope scope(isolate_);
mads.s.ager31e71382008-08-13 09:32:07 +0000914 ASSERT(args.length() == 0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000915
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000916 thread_local_.frame_drop_mode_ = FRAMES_UNTOUCHED;
ager@chromium.org357bf652010-04-12 11:30:10 +0000917
kasper.lundbd3ec4e2008-07-09 11:06:54 +0000918 // Get the top-most JavaScript frame.
vegorov@chromium.org74f333b2011-04-06 11:17:46 +0000919 JavaScriptFrameIterator it(isolate_);
kasper.lundbd3ec4e2008-07-09 11:06:54 +0000920 JavaScriptFrame* frame = it.frame();
921
922 // Just continue if breaks are disabled or debugger cannot be loaded.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000923 if (disable_break() || !Load()) {
924 SetAfterBreakTarget(frame);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000925 return heap->undefined_value();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000926 }
927
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000928 // Enter the debugger.
929 EnterDebugger debugger;
930 if (debugger.FailedToEnter()) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000931 return heap->undefined_value();
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000932 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000933
kasper.lund44510672008-07-25 07:37:58 +0000934 // Postpone interrupt during breakpoint processing.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000935 PostponeInterruptsScope postpone(isolate_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000936
937 // Get the debug info (create it if it does not exist).
938 Handle<SharedFunctionInfo> shared =
939 Handle<SharedFunctionInfo>(JSFunction::cast(frame->function())->shared());
940 Handle<DebugInfo> debug_info = GetDebugInfo(shared);
941
942 // Find the break point where execution has stopped.
943 BreakLocationIterator break_location_iterator(debug_info,
944 ALL_BREAK_LOCATIONS);
945 break_location_iterator.FindBreakLocationFromAddress(frame->pc());
946
947 // Check whether step next reached a new statement.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000948 if (!StepNextContinue(&break_location_iterator, frame)) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000949 // Decrease steps left if performing multiple steps.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000950 if (thread_local_.step_count_ > 0) {
951 thread_local_.step_count_--;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000952 }
953 }
954
955 // If there is one or more real break points check whether any of these are
956 // triggered.
ulan@chromium.org09d7ab52013-02-25 15:50:35 +0000957 Handle<Object> break_points_hit(heap->undefined_value(), isolate_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000958 if (break_location_iterator.HasBreakPoint()) {
959 Handle<Object> break_point_objects =
ulan@chromium.org09d7ab52013-02-25 15:50:35 +0000960 Handle<Object>(break_location_iterator.BreakPointObjects(), isolate_);
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000961 break_points_hit = CheckBreakPoints(break_point_objects);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000962 }
963
ager@chromium.orga1645e22009-09-09 19:27:10 +0000964 // If step out is active skip everything until the frame where we need to step
965 // out to is reached, unless real breakpoint is hit.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000966 if (StepOutActive() && frame->fp() != step_out_fp() &&
ager@chromium.orga1645e22009-09-09 19:27:10 +0000967 break_points_hit->IsUndefined() ) {
968 // Step count should always be 0 for StepOut.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000969 ASSERT(thread_local_.step_count_ == 0);
ager@chromium.orga1645e22009-09-09 19:27:10 +0000970 } else if (!break_points_hit->IsUndefined() ||
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000971 (thread_local_.last_step_action_ != StepNone &&
972 thread_local_.step_count_ == 0)) {
ager@chromium.orga1645e22009-09-09 19:27:10 +0000973 // Notify debugger if a real break point is triggered or if performing
974 // single stepping with no more steps to perform. Otherwise do another step.
975
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000976 // Clear all current stepping setup.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000977 ClearStepping();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000978
lrn@chromium.org34e60782011-09-15 07:25:40 +0000979 if (thread_local_.queued_step_count_ > 0) {
980 // Perform queued steps
981 int step_count = thread_local_.queued_step_count_;
982
983 // Clear queue
984 thread_local_.queued_step_count_ = 0;
985
986 PrepareStep(StepNext, step_count);
987 } else {
988 // Notify the debug event listeners.
989 isolate_->debugger()->OnDebugBreak(break_points_hit, false);
990 }
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000991 } else if (thread_local_.last_step_action_ != StepNone) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000992 // Hold on to last step action as it is cleared by the call to
993 // ClearStepping.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000994 StepAction step_action = thread_local_.last_step_action_;
995 int step_count = thread_local_.step_count_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000996
lrn@chromium.org34e60782011-09-15 07:25:40 +0000997 // If StepNext goes deeper in code, StepOut until original frame
998 // and keep step count queued up in the meantime.
999 if (step_action == StepNext && frame->fp() < thread_local_.last_fp_) {
1000 // Count frames until target frame
1001 int count = 0;
1002 JavaScriptFrameIterator it(isolate_);
jkummerow@chromium.org212d9642012-05-11 15:02:09 +00001003 while (!it.done() && it.frame()->fp() < thread_local_.last_fp_) {
lrn@chromium.org34e60782011-09-15 07:25:40 +00001004 count++;
1005 it.Advance();
1006 }
1007
danno@chromium.org81cac2b2012-07-10 11:28:27 +00001008 // Check that we indeed found the frame we are looking for.
1009 CHECK(!it.done() && (it.frame()->fp() == thread_local_.last_fp_));
1010 if (step_count > 1) {
1011 // Save old count and action to continue stepping after StepOut.
1012 thread_local_.queued_step_count_ = step_count - 1;
jkummerow@chromium.org212d9642012-05-11 15:02:09 +00001013 }
1014
danno@chromium.org81cac2b2012-07-10 11:28:27 +00001015 // Set up for StepOut to reach target frame.
1016 step_action = StepOut;
1017 step_count = count;
lrn@chromium.org34e60782011-09-15 07:25:40 +00001018 }
1019
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001020 // Clear all current stepping setup.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001021 ClearStepping();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001022
1023 // Set up for the remaining steps.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001024 PrepareStep(step_action, step_count);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001025 }
1026
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001027 if (thread_local_.frame_drop_mode_ == FRAMES_UNTOUCHED) {
1028 SetAfterBreakTarget(frame);
1029 } else if (thread_local_.frame_drop_mode_ ==
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001030 FRAME_DROPPED_IN_IC_CALL) {
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001031 // We must have been calling IC stub. Do not go there anymore.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001032 Code* plain_return = isolate_->builtins()->builtin(
1033 Builtins::kPlainReturn_LiveEdit);
1034 thread_local_.after_break_target_ = plain_return->entry();
1035 } else if (thread_local_.frame_drop_mode_ ==
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001036 FRAME_DROPPED_IN_DEBUG_SLOT_CALL) {
1037 // Debug break slot stub does not return normally, instead it manually
1038 // cleans the stack and jumps. We should patch the jump address.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001039 Code* plain_return = isolate_->builtins()->builtin(
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00001040 Builtins::kFrameDropper_LiveEdit);
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001041 thread_local_.after_break_target_ = plain_return->entry();
1042 } else if (thread_local_.frame_drop_mode_ ==
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001043 FRAME_DROPPED_IN_DIRECT_CALL) {
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001044 // Nothing to do, after_break_target is not used here.
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00001045 } else if (thread_local_.frame_drop_mode_ ==
1046 FRAME_DROPPED_IN_RETURN_CALL) {
1047 Code* plain_return = isolate_->builtins()->builtin(
1048 Builtins::kFrameDropper_LiveEdit);
1049 thread_local_.after_break_target_ = plain_return->entry();
ager@chromium.org357bf652010-04-12 11:30:10 +00001050 } else {
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001051 UNREACHABLE();
ager@chromium.org357bf652010-04-12 11:30:10 +00001052 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001053
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001054 return heap->undefined_value();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001055}
1056
1057
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001058RUNTIME_FUNCTION(Object*, Debug_Break) {
1059 return isolate->debug()->Break(args);
1060}
1061
1062
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001063// Check the break point objects for whether one or more are actually
1064// triggered. This function returns a JSArray with the break point objects
1065// which is triggered.
1066Handle<Object> Debug::CheckBreakPoints(Handle<Object> break_point_objects) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00001067 Factory* factory = isolate_->factory();
1068
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001069 // Count the number of break points hit. If there are multiple break points
1070 // they are in a FixedArray.
1071 Handle<FixedArray> break_points_hit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001072 int break_points_hit_count = 0;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001073 ASSERT(!break_point_objects->IsUndefined());
1074 if (break_point_objects->IsFixedArray()) {
1075 Handle<FixedArray> array(FixedArray::cast(*break_point_objects));
lrn@chromium.org7516f052011-03-30 08:52:27 +00001076 break_points_hit = factory->NewFixedArray(array->length());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001077 for (int i = 0; i < array->length(); i++) {
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00001078 Handle<Object> o(array->get(i), isolate_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001079 if (CheckBreakPoint(o)) {
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001080 break_points_hit->set(break_points_hit_count++, *o);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001081 }
1082 }
1083 } else {
lrn@chromium.org7516f052011-03-30 08:52:27 +00001084 break_points_hit = factory->NewFixedArray(1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001085 if (CheckBreakPoint(break_point_objects)) {
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001086 break_points_hit->set(break_points_hit_count++, *break_point_objects);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001087 }
1088 }
1089
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001090 // Return undefined if no break points were triggered.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001091 if (break_points_hit_count == 0) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00001092 return factory->undefined_value();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001093 }
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001094 // Return break points hit as a JSArray.
lrn@chromium.org7516f052011-03-30 08:52:27 +00001095 Handle<JSArray> result = factory->NewJSArrayWithElements(break_points_hit);
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001096 result->set_length(Smi::FromInt(break_points_hit_count));
1097 return result;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001098}
1099
1100
1101// Check whether a single break point object is triggered.
1102bool Debug::CheckBreakPoint(Handle<Object> break_point_object) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00001103 Factory* factory = isolate_->factory();
1104 HandleScope scope(isolate_);
ager@chromium.org381abbb2009-02-25 13:23:22 +00001105
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001106 // Ignore check if break point object is not a JSObject.
1107 if (!break_point_object->IsJSObject()) return true;
1108
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001109 // Get the function IsBreakPointTriggered (defined in debug-debugger.js).
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001110 Handle<String> is_break_point_triggered_string =
1111 factory->InternalizeOneByteString(
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00001112 STATIC_ASCII_VECTOR("IsBreakPointTriggered"));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001113 Handle<JSFunction> check_break_point =
1114 Handle<JSFunction>(JSFunction::cast(
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00001115 debug_context()->global_object()->GetPropertyNoExceptionThrown(
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001116 *is_break_point_triggered_string)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001117
1118 // Get the break id as an object.
lrn@chromium.org7516f052011-03-30 08:52:27 +00001119 Handle<Object> break_id = factory->NewNumberFromInt(Debug::break_id());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001120
1121 // Call HandleBreakPointx.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001122 bool caught_exception;
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00001123 Handle<Object> argv[] = { break_id, break_point_object };
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001124 Handle<Object> result = Execution::TryCall(check_break_point,
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00001125 isolate_->js_builtins_object(),
1126 ARRAY_SIZE(argv),
1127 argv,
1128 &caught_exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001129
1130 // If exception or non boolean result handle as not triggered
1131 if (caught_exception || !result->IsBoolean()) {
1132 return false;
1133 }
1134
1135 // Return whether the break point is triggered.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001136 ASSERT(!result.is_null());
1137 return (*result)->IsTrue();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001138}
1139
1140
1141// Check whether the function has debug information.
1142bool Debug::HasDebugInfo(Handle<SharedFunctionInfo> shared) {
1143 return !shared->debug_info()->IsUndefined();
1144}
1145
1146
kasper.lundbd3ec4e2008-07-09 11:06:54 +00001147// Return the debug info for this function. EnsureDebugInfo must be called
1148// prior to ensure the debug info has been generated for shared.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001149Handle<DebugInfo> Debug::GetDebugInfo(Handle<SharedFunctionInfo> shared) {
kasper.lundbd3ec4e2008-07-09 11:06:54 +00001150 ASSERT(HasDebugInfo(shared));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001151 return Handle<DebugInfo>(DebugInfo::cast(shared->debug_info()));
1152}
1153
1154
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001155void Debug::SetBreakPoint(Handle<JSFunction> function,
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00001156 Handle<Object> break_point_object,
1157 int* source_position) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00001158 HandleScope scope(isolate_);
ager@chromium.org381abbb2009-02-25 13:23:22 +00001159
lrn@chromium.org34e60782011-09-15 07:25:40 +00001160 PrepareForBreakPoints();
1161
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001162 // Make sure the function is compiled and has set up the debug info.
1163 Handle<SharedFunctionInfo> shared(function->shared());
1164 if (!EnsureDebugInfo(shared, function)) {
kasper.lundbd3ec4e2008-07-09 11:06:54 +00001165 // Return if retrieving debug info failed.
1166 return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001167 }
1168
kasper.lundbd3ec4e2008-07-09 11:06:54 +00001169 Handle<DebugInfo> debug_info = GetDebugInfo(shared);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001170 // Source positions starts with zero.
danno@chromium.orgbf0c8202011-12-27 10:09:42 +00001171 ASSERT(*source_position >= 0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001172
1173 // Find the break point and change it.
1174 BreakLocationIterator it(debug_info, SOURCE_BREAK_LOCATIONS);
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00001175 it.FindBreakLocationFromPosition(*source_position);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001176 it.SetBreakPoint(break_point_object);
1177
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00001178 *source_position = it.position();
1179
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001180 // At least one active break point now.
1181 ASSERT(debug_info->GetBreakPointCount() > 0);
1182}
1183
1184
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001185bool Debug::SetBreakPointForScript(Handle<Script> script,
1186 Handle<Object> break_point_object,
1187 int* source_position) {
1188 HandleScope scope(isolate_);
1189
jkummerow@chromium.org78502a92012-09-06 13:50:42 +00001190 PrepareForBreakPoints();
1191
1192 // Obtain shared function info for the function.
1193 Object* result = FindSharedFunctionInfoInScript(script, *source_position);
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001194 if (result->IsUndefined()) return false;
1195
1196 // Make sure the function has set up the debug info.
1197 Handle<SharedFunctionInfo> shared(SharedFunctionInfo::cast(result));
1198 if (!EnsureDebugInfo(shared, Handle<JSFunction>::null())) {
1199 // Return if retrieving debug info failed.
1200 return false;
1201 }
1202
1203 // Find position within function. The script position might be before the
1204 // source position of the first function.
1205 int position;
1206 if (shared->start_position() > *source_position) {
1207 position = 0;
1208 } else {
1209 position = *source_position - shared->start_position();
1210 }
1211
1212 Handle<DebugInfo> debug_info = GetDebugInfo(shared);
1213 // Source positions starts with zero.
1214 ASSERT(position >= 0);
1215
1216 // Find the break point and change it.
1217 BreakLocationIterator it(debug_info, SOURCE_BREAK_LOCATIONS);
1218 it.FindBreakLocationFromPosition(position);
1219 it.SetBreakPoint(break_point_object);
1220
1221 *source_position = it.position() + shared->start_position();
1222
1223 // At least one active break point now.
1224 ASSERT(debug_info->GetBreakPointCount() > 0);
1225 return true;
1226}
1227
1228
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001229void Debug::ClearBreakPoint(Handle<Object> break_point_object) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00001230 HandleScope scope(isolate_);
ager@chromium.org381abbb2009-02-25 13:23:22 +00001231
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001232 DebugInfoListNode* node = debug_info_list_;
1233 while (node != NULL) {
1234 Object* result = DebugInfo::FindBreakPointInfo(node->debug_info(),
1235 break_point_object);
1236 if (!result->IsUndefined()) {
1237 // Get information in the break point.
1238 BreakPointInfo* break_point_info = BreakPointInfo::cast(result);
1239 Handle<DebugInfo> debug_info = node->debug_info();
1240 Handle<SharedFunctionInfo> shared(debug_info->shared());
1241 int source_position = break_point_info->statement_position()->value();
1242
1243 // Source positions starts with zero.
1244 ASSERT(source_position >= 0);
1245
1246 // Find the break point and clear it.
1247 BreakLocationIterator it(debug_info, SOURCE_BREAK_LOCATIONS);
1248 it.FindBreakLocationFromPosition(source_position);
1249 it.ClearBreakPoint(break_point_object);
1250
1251 // If there are no more break points left remove the debug info for this
1252 // function.
1253 if (debug_info->GetBreakPointCount() == 0) {
1254 RemoveDebugInfo(debug_info);
1255 }
1256
1257 return;
1258 }
1259 node = node->next();
1260 }
1261}
1262
1263
ager@chromium.org381abbb2009-02-25 13:23:22 +00001264void Debug::ClearAllBreakPoints() {
1265 DebugInfoListNode* node = debug_info_list_;
1266 while (node != NULL) {
1267 // Remove all debug break code.
1268 BreakLocationIterator it(node->debug_info(), ALL_BREAK_LOCATIONS);
1269 it.ClearAllDebugBreak();
1270 node = node->next();
1271 }
1272
1273 // Remove all debug info.
1274 while (debug_info_list_ != NULL) {
1275 RemoveDebugInfo(debug_info_list_->debug_info());
1276 }
1277}
1278
1279
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001280void Debug::FloodWithOneShot(Handle<JSFunction> function) {
lrn@chromium.org34e60782011-09-15 07:25:40 +00001281 PrepareForBreakPoints();
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001282
1283 // Make sure the function is compiled and has set up the debug info.
1284 Handle<SharedFunctionInfo> shared(function->shared());
1285 if (!EnsureDebugInfo(shared, function)) {
kasper.lundbd3ec4e2008-07-09 11:06:54 +00001286 // Return if we failed to retrieve the debug info.
1287 return;
1288 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001289
1290 // Flood the function with break points.
kasper.lundbd3ec4e2008-07-09 11:06:54 +00001291 BreakLocationIterator it(GetDebugInfo(shared), ALL_BREAK_LOCATIONS);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001292 while (!it.Done()) {
1293 it.SetOneShot();
1294 it.Next();
1295 }
1296}
1297
1298
rossberg@chromium.org2c067b12012-03-19 11:01:52 +00001299void Debug::FloodBoundFunctionWithOneShot(Handle<JSFunction> function) {
1300 Handle<FixedArray> new_bindings(function->function_bindings());
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00001301 Handle<Object> bindee(new_bindings->get(JSFunction::kBoundFunctionIndex),
1302 isolate_);
rossberg@chromium.org2c067b12012-03-19 11:01:52 +00001303
1304 if (!bindee.is_null() && bindee->IsJSFunction() &&
1305 !JSFunction::cast(*bindee)->IsBuiltin()) {
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001306 Handle<JSFunction> bindee_function(JSFunction::cast(*bindee));
1307 Debug::FloodWithOneShot(bindee_function);
rossberg@chromium.org2c067b12012-03-19 11:01:52 +00001308 }
1309}
1310
1311
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001312void Debug::FloodHandlerWithOneShot() {
ager@chromium.org8bb60582008-12-11 12:02:20 +00001313 // Iterate through the JavaScript stack looking for handlers.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001314 StackFrame::Id id = break_frame_id();
ager@chromium.org8bb60582008-12-11 12:02:20 +00001315 if (id == StackFrame::NO_ID) {
1316 // If there is no JavaScript stack don't do anything.
1317 return;
1318 }
vegorov@chromium.org74f333b2011-04-06 11:17:46 +00001319 for (JavaScriptFrameIterator it(isolate_, id); !it.done(); it.Advance()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001320 JavaScriptFrame* frame = it.frame();
1321 if (frame->HasHandler()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001322 // Flood the function with the catch block with break points
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001323 JSFunction* function = JSFunction::cast(frame->function());
1324 FloodWithOneShot(Handle<JSFunction>(function));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001325 return;
1326 }
1327 }
1328}
1329
1330
1331void Debug::ChangeBreakOnException(ExceptionBreakType type, bool enable) {
1332 if (type == BreakUncaughtException) {
1333 break_on_uncaught_exception_ = enable;
1334 } else {
1335 break_on_exception_ = enable;
1336 }
1337}
1338
1339
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00001340bool Debug::IsBreakOnException(ExceptionBreakType type) {
1341 if (type == BreakUncaughtException) {
1342 return break_on_uncaught_exception_;
1343 } else {
1344 return break_on_exception_;
1345 }
1346}
1347
1348
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001349void Debug::PrepareStep(StepAction step_action, int step_count) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00001350 HandleScope scope(isolate_);
lrn@chromium.org34e60782011-09-15 07:25:40 +00001351
1352 PrepareForBreakPoints();
1353
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001354 ASSERT(Debug::InDebugger());
1355
1356 // Remember this step action and count.
1357 thread_local_.last_step_action_ = step_action;
ager@chromium.orga1645e22009-09-09 19:27:10 +00001358 if (step_action == StepOut) {
1359 // For step out target frame will be found on the stack so there is no need
1360 // to set step counter for it. It's expected to always be 0 for StepOut.
1361 thread_local_.step_count_ = 0;
1362 } else {
1363 thread_local_.step_count_ = step_count;
1364 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001365
1366 // Get the frame where the execution has stopped and skip the debug frame if
1367 // any. The debug frame will only be present if execution was stopped due to
1368 // hitting a break point. In other situations (e.g. unhandled exception) the
1369 // debug frame is not present.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001370 StackFrame::Id id = break_frame_id();
ager@chromium.org8bb60582008-12-11 12:02:20 +00001371 if (id == StackFrame::NO_ID) {
1372 // If there is no JavaScript stack don't do anything.
1373 return;
1374 }
vegorov@chromium.org74f333b2011-04-06 11:17:46 +00001375 JavaScriptFrameIterator frames_it(isolate_, id);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001376 JavaScriptFrame* frame = frames_it.frame();
1377
1378 // First of all ensure there is one-shot break points in the top handler
1379 // if any.
1380 FloodHandlerWithOneShot();
1381
1382 // If the function on the top frame is unresolved perform step out. This will
1383 // be the case when calling unknown functions and having the debugger stopped
1384 // in an unhandled exception.
1385 if (!frame->function()->IsJSFunction()) {
1386 // Step out: Find the calling JavaScript frame and flood it with
1387 // breakpoints.
1388 frames_it.Advance();
1389 // Fill the function to return to with one-shot break points.
1390 JSFunction* function = JSFunction::cast(frames_it.frame()->function());
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001391 FloodWithOneShot(Handle<JSFunction>(function));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001392 return;
1393 }
1394
1395 // Get the debug info (create it if it does not exist).
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001396 Handle<JSFunction> function(JSFunction::cast(frame->function()));
1397 Handle<SharedFunctionInfo> shared(function->shared());
1398 if (!EnsureDebugInfo(shared, function)) {
kasper.lundbd3ec4e2008-07-09 11:06:54 +00001399 // Return if ensuring debug info failed.
1400 return;
1401 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001402 Handle<DebugInfo> debug_info = GetDebugInfo(shared);
1403
1404 // Find the break location where execution has stopped.
1405 BreakLocationIterator it(debug_info, ALL_BREAK_LOCATIONS);
1406 it.FindBreakLocationFromAddress(frame->pc());
1407
1408 // Compute whether or not the target is a call target.
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001409 bool is_load_or_store = false;
1410 bool is_inline_cache_stub = false;
whesse@chromium.orge90029b2010-08-02 11:52:17 +00001411 bool is_at_restarted_function = false;
ager@chromium.orga1645e22009-09-09 19:27:10 +00001412 Handle<Code> call_function_stub;
ager@chromium.orga1645e22009-09-09 19:27:10 +00001413
whesse@chromium.orge90029b2010-08-02 11:52:17 +00001414 if (thread_local_.restarter_frame_function_pointer_ == NULL) {
1415 if (RelocInfo::IsCodeTarget(it.rinfo()->rmode())) {
1416 bool is_call_target = false;
1417 Address target = it.rinfo()->target_address();
1418 Code* code = Code::GetCodeFromTargetAddress(target);
1419 if (code->is_call_stub() || code->is_keyed_call_stub()) {
1420 is_call_target = true;
1421 }
1422 if (code->is_inline_cache_stub()) {
1423 is_inline_cache_stub = true;
1424 is_load_or_store = !is_call_target;
1425 }
1426
1427 // Check if target code is CallFunction stub.
1428 Code* maybe_call_function_stub = code;
1429 // If there is a breakpoint at this line look at the original code to
1430 // check if it is a CallFunction stub.
1431 if (it.IsDebugBreak()) {
1432 Address original_target = it.original_rinfo()->target_address();
1433 maybe_call_function_stub =
1434 Code::GetCodeFromTargetAddress(original_target);
1435 }
1436 if (maybe_call_function_stub->kind() == Code::STUB &&
1437 maybe_call_function_stub->major_key() == CodeStub::CallFunction) {
1438 // Save reference to the code as we may need it to find out arguments
1439 // count for 'step in' later.
1440 call_function_stub = Handle<Code>(maybe_call_function_stub);
1441 }
ager@chromium.orga1645e22009-09-09 19:27:10 +00001442 }
whesse@chromium.orge90029b2010-08-02 11:52:17 +00001443 } else {
1444 is_at_restarted_function = true;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001445 }
1446
v8.team.kasperl727e9952008-09-02 14:56:44 +00001447 // If this is the last break code target step out is the only possibility.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001448 if (it.IsExit() || step_action == StepOut) {
ager@chromium.orga1645e22009-09-09 19:27:10 +00001449 if (step_action == StepOut) {
1450 // Skip step_count frames starting with the current one.
1451 while (step_count-- > 0 && !frames_it.done()) {
1452 frames_it.Advance();
1453 }
1454 } else {
1455 ASSERT(it.IsExit());
1456 frames_it.Advance();
1457 }
1458 // Skip builtin functions on the stack.
1459 while (!frames_it.done() &&
1460 JSFunction::cast(frames_it.frame()->function())->IsBuiltin()) {
1461 frames_it.Advance();
1462 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001463 // Step out: If there is a JavaScript caller frame, we need to
1464 // flood it with breakpoints.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001465 if (!frames_it.done()) {
1466 // Fill the function to return to with one-shot break points.
1467 JSFunction* function = JSFunction::cast(frames_it.frame()->function());
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001468 FloodWithOneShot(Handle<JSFunction>(function));
ager@chromium.orga1645e22009-09-09 19:27:10 +00001469 // Set target frame pointer.
1470 ActivateStepOut(frames_it.frame());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001471 }
ager@chromium.orga1645e22009-09-09 19:27:10 +00001472 } else if (!(is_inline_cache_stub || RelocInfo::IsConstructCall(it.rmode()) ||
whesse@chromium.orge90029b2010-08-02 11:52:17 +00001473 !call_function_stub.is_null() || is_at_restarted_function)
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001474 || step_action == StepNext || step_action == StepMin) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001475 // Step next or step min.
1476
1477 // Fill the current function with one-shot break points.
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001478 FloodWithOneShot(function);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001479
1480 // Remember source position and frame to handle step next.
1481 thread_local_.last_statement_position_ =
1482 debug_info->code()->SourceStatementPosition(frame->pc());
mmassi@chromium.org7028c052012-06-13 11:51:58 +00001483 thread_local_.last_fp_ = frame->UnpaddedFP();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001484 } else {
whesse@chromium.orge90029b2010-08-02 11:52:17 +00001485 // If there's restarter frame on top of the stack, just get the pointer
1486 // to function which is going to be restarted.
1487 if (is_at_restarted_function) {
1488 Handle<JSFunction> restarted_function(
1489 JSFunction::cast(*thread_local_.restarter_frame_function_pointer_));
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001490 FloodWithOneShot(restarted_function);
whesse@chromium.orge90029b2010-08-02 11:52:17 +00001491 } else if (!call_function_stub.is_null()) {
1492 // If it's CallFunction stub ensure target function is compiled and flood
1493 // it with one shot breakpoints.
1494
ager@chromium.orga1645e22009-09-09 19:27:10 +00001495 // Find out number of arguments from the stub minor key.
1496 // Reverse lookup required as the minor key cannot be retrieved
1497 // from the code object.
1498 Handle<Object> obj(
lrn@chromium.org7516f052011-03-30 08:52:27 +00001499 isolate_->heap()->code_stubs()->SlowReverseLookup(
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00001500 *call_function_stub),
1501 isolate_);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001502 ASSERT(!obj.is_null());
1503 ASSERT(!(*obj)->IsUndefined());
ager@chromium.orga1645e22009-09-09 19:27:10 +00001504 ASSERT(obj->IsSmi());
1505 // Get the STUB key and extract major and minor key.
1506 uint32_t key = Smi::cast(*obj)->value();
1507 // Argc in the stub is the number of arguments passed - not the
1508 // expected arguments of the called function.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001509 int call_function_arg_count =
1510 CallFunctionStub::ExtractArgcFromMinorKey(
1511 CodeStub::MinorKeyFromKey(key));
ager@chromium.orga1645e22009-09-09 19:27:10 +00001512 ASSERT(call_function_stub->major_key() ==
1513 CodeStub::MajorKeyFromKey(key));
1514
1515 // Find target function on the expression stack.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001516 // Expression stack looks like this (top to bottom):
ager@chromium.orga1645e22009-09-09 19:27:10 +00001517 // argN
1518 // ...
1519 // arg0
1520 // Receiver
1521 // Function to call
1522 int expressions_count = frame->ComputeExpressionsCount();
1523 ASSERT(expressions_count - 2 - call_function_arg_count >= 0);
1524 Object* fun = frame->GetExpression(
1525 expressions_count - 2 - call_function_arg_count);
1526 if (fun->IsJSFunction()) {
1527 Handle<JSFunction> js_function(JSFunction::cast(fun));
rossberg@chromium.org2c067b12012-03-19 11:01:52 +00001528 if (js_function->shared()->bound()) {
1529 Debug::FloodBoundFunctionWithOneShot(js_function);
1530 } else if (!js_function->IsBuiltin()) {
1531 // Don't step into builtins.
ager@chromium.orga1645e22009-09-09 19:27:10 +00001532 // It will also compile target function if it's not compiled yet.
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001533 FloodWithOneShot(js_function);
ager@chromium.orga1645e22009-09-09 19:27:10 +00001534 }
1535 }
1536 }
1537
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001538 // Fill the current function with one-shot break points even for step in on
1539 // a call target as the function called might be a native function for
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001540 // which step in will not stop. It also prepares for stepping in
1541 // getters/setters.
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001542 FloodWithOneShot(function);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001543
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001544 if (is_load_or_store) {
1545 // Remember source position and frame to handle step in getter/setter. If
1546 // there is a custom getter/setter it will be handled in
1547 // Object::Get/SetPropertyWithCallback, otherwise the step action will be
1548 // propagated on the next Debug::Break.
1549 thread_local_.last_statement_position_ =
1550 debug_info->code()->SourceStatementPosition(frame->pc());
mmassi@chromium.org7028c052012-06-13 11:51:58 +00001551 thread_local_.last_fp_ = frame->UnpaddedFP();
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001552 }
1553
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001554 // Step in or Step in min
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00001555 it.PrepareStepIn(isolate_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001556 ActivateStepIn(frame);
1557 }
1558}
1559
1560
1561// Check whether the current debug break should be reported to the debugger. It
1562// is used to have step next and step in only report break back to the debugger
1563// if on a different frame or in a different statement. In some situations
1564// there will be several break points in the same statement when the code is
v8.team.kasperl727e9952008-09-02 14:56:44 +00001565// flooded with one-shot break points. This function helps to perform several
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001566// steps before reporting break back to the debugger.
1567bool Debug::StepNextContinue(BreakLocationIterator* break_location_iterator,
1568 JavaScriptFrame* frame) {
lrn@chromium.org34e60782011-09-15 07:25:40 +00001569 // StepNext and StepOut shouldn't bring us deeper in code, so last frame
1570 // shouldn't be a parent of current frame.
1571 if (thread_local_.last_step_action_ == StepNext ||
1572 thread_local_.last_step_action_ == StepOut) {
1573 if (frame->fp() < thread_local_.last_fp_) return true;
1574 }
1575
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001576 // If the step last action was step next or step in make sure that a new
1577 // statement is hit.
1578 if (thread_local_.last_step_action_ == StepNext ||
1579 thread_local_.last_step_action_ == StepIn) {
1580 // Never continue if returning from function.
1581 if (break_location_iterator->IsExit()) return false;
1582
1583 // Continue if we are still on the same frame and in the same statement.
1584 int current_statement_position =
1585 break_location_iterator->code()->SourceStatementPosition(frame->pc());
mmassi@chromium.org7028c052012-06-13 11:51:58 +00001586 return thread_local_.last_fp_ == frame->UnpaddedFP() &&
ager@chromium.org236ad962008-09-25 09:45:57 +00001587 thread_local_.last_statement_position_ == current_statement_position;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001588 }
1589
1590 // No step next action - don't continue.
1591 return false;
1592}
1593
1594
1595// Check whether the code object at the specified address is a debug break code
1596// object.
1597bool Debug::IsDebugBreak(Address addr) {
ager@chromium.org8bb60582008-12-11 12:02:20 +00001598 Code* code = Code::GetCodeFromTargetAddress(addr);
yangguo@chromium.org9768bf12013-01-11 14:51:07 +00001599 return code->is_debug_break();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001600}
1601
1602
1603// Check whether a code stub with the specified major key is a possible break
1604// point location when looking for source break locations.
1605bool Debug::IsSourceBreakStub(Code* code) {
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001606 CodeStub::Major major_key = CodeStub::GetMajorKey(code);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001607 return major_key == CodeStub::CallFunction;
1608}
1609
1610
1611// Check whether a code stub with the specified major key is a possible break
1612// location.
1613bool Debug::IsBreakStub(Code* code) {
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001614 CodeStub::Major major_key = CodeStub::GetMajorKey(code);
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00001615 return major_key == CodeStub::CallFunction;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001616}
1617
1618
1619// Find the builtin to use for invoking the debug break
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001620Handle<Code> Debug::FindDebugBreak(Handle<Code> code, RelocInfo::Mode mode) {
danno@chromium.orgfa458e42012-02-01 10:48:36 +00001621 Isolate* isolate = Isolate::Current();
1622
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001623 // Find the builtin debug break function matching the calling convention
1624 // used by the call site.
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001625 if (code->is_inline_cache_stub()) {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001626 switch (code->kind()) {
1627 case Code::CALL_IC:
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001628 case Code::KEYED_CALL_IC:
danno@chromium.orgfa458e42012-02-01 10:48:36 +00001629 return isolate->stub_cache()->ComputeCallDebugBreak(
1630 code->arguments_count(), code->kind());
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001631
1632 case Code::LOAD_IC:
danno@chromium.orgfa458e42012-02-01 10:48:36 +00001633 return isolate->builtins()->LoadIC_DebugBreak();
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001634
1635 case Code::STORE_IC:
danno@chromium.orgfa458e42012-02-01 10:48:36 +00001636 return isolate->builtins()->StoreIC_DebugBreak();
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001637
1638 case Code::KEYED_LOAD_IC:
danno@chromium.orgfa458e42012-02-01 10:48:36 +00001639 return isolate->builtins()->KeyedLoadIC_DebugBreak();
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001640
1641 case Code::KEYED_STORE_IC:
danno@chromium.orgfa458e42012-02-01 10:48:36 +00001642 return isolate->builtins()->KeyedStoreIC_DebugBreak();
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001643
danno@chromium.orgf005df62013-04-30 16:36:45 +00001644 case Code::COMPARE_NIL_IC:
1645 return isolate->builtins()->CompareNilIC_DebugBreak();
1646
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001647 default:
1648 UNREACHABLE();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001649 }
1650 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001651 if (RelocInfo::IsConstructCall(mode)) {
danno@chromium.orgfa458e42012-02-01 10:48:36 +00001652 if (code->has_function_cache()) {
1653 return isolate->builtins()->CallConstructStub_Recording_DebugBreak();
1654 } else {
1655 return isolate->builtins()->CallConstructStub_DebugBreak();
1656 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001657 }
1658 if (code->kind() == Code::STUB) {
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00001659 ASSERT(code->major_key() == CodeStub::CallFunction);
danno@chromium.orgfa458e42012-02-01 10:48:36 +00001660 if (code->has_function_cache()) {
1661 return isolate->builtins()->CallFunctionStub_Recording_DebugBreak();
1662 } else {
1663 return isolate->builtins()->CallFunctionStub_DebugBreak();
1664 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001665 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001666
1667 UNREACHABLE();
1668 return Handle<Code>::null();
1669}
1670
1671
1672// Simple function for returning the source positions for active break points.
1673Handle<Object> Debug::GetSourceBreakLocations(
1674 Handle<SharedFunctionInfo> shared) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00001675 Isolate* isolate = Isolate::Current();
1676 Heap* heap = isolate->heap();
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00001677 if (!HasDebugInfo(shared)) {
1678 return Handle<Object>(heap->undefined_value(), isolate);
1679 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001680 Handle<DebugInfo> debug_info = GetDebugInfo(shared);
1681 if (debug_info->GetBreakPointCount() == 0) {
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00001682 return Handle<Object>(heap->undefined_value(), isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001683 }
1684 Handle<FixedArray> locations =
lrn@chromium.org7516f052011-03-30 08:52:27 +00001685 isolate->factory()->NewFixedArray(debug_info->GetBreakPointCount());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001686 int count = 0;
1687 for (int i = 0; i < debug_info->break_points()->length(); i++) {
1688 if (!debug_info->break_points()->get(i)->IsUndefined()) {
1689 BreakPointInfo* break_point_info =
1690 BreakPointInfo::cast(debug_info->break_points()->get(i));
1691 if (break_point_info->GetBreakPointCount() > 0) {
1692 locations->set(count++, break_point_info->statement_position());
1693 }
1694 }
1695 }
1696 return locations;
1697}
1698
1699
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001700void Debug::NewBreak(StackFrame::Id break_frame_id) {
1701 thread_local_.break_frame_id_ = break_frame_id;
1702 thread_local_.break_id_ = ++thread_local_.break_count_;
1703}
1704
1705
1706void Debug::SetBreak(StackFrame::Id break_frame_id, int break_id) {
1707 thread_local_.break_frame_id_ = break_frame_id;
1708 thread_local_.break_id_ = break_id;
1709}
1710
1711
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001712// Handle stepping into a function.
1713void Debug::HandleStepIn(Handle<JSFunction> function,
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001714 Handle<Object> holder,
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001715 Address fp,
1716 bool is_constructor) {
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00001717 Isolate* isolate = function->GetIsolate();
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001718 // If the frame pointer is not supplied by the caller find it.
1719 if (fp == 0) {
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00001720 StackFrameIterator it(isolate);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001721 it.Advance();
1722 // For constructor functions skip another frame.
1723 if (is_constructor) {
1724 ASSERT(it.frame()->is_construct());
1725 it.Advance();
1726 }
1727 fp = it.frame()->fp();
1728 }
1729
1730 // Flood the function with one-shot break points if it is called from where
1731 // step into was requested.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001732 if (fp == step_in_fp()) {
rossberg@chromium.org2c067b12012-03-19 11:01:52 +00001733 if (function->shared()->bound()) {
1734 // Handle Function.prototype.bind
1735 Debug::FloodBoundFunctionWithOneShot(function);
1736 } else if (!function->IsBuiltin()) {
1737 // Don't allow step into functions in the native context.
kasperl@chromium.orgacae3782009-04-11 09:17:08 +00001738 if (function->shared()->code() ==
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00001739 isolate->builtins()->builtin(Builtins::kFunctionApply) ||
kasperl@chromium.orgacae3782009-04-11 09:17:08 +00001740 function->shared()->code() ==
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00001741 isolate->builtins()->builtin(Builtins::kFunctionCall)) {
kasperl@chromium.orgacae3782009-04-11 09:17:08 +00001742 // Handle function.apply and function.call separately to flood the
1743 // function to be called and not the code for Builtins::FunctionApply or
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001744 // Builtins::FunctionCall. The receiver of call/apply is the target
1745 // function.
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001746 if (!holder.is_null() && holder->IsJSFunction() &&
1747 !JSFunction::cast(*holder)->IsBuiltin()) {
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001748 Handle<JSFunction> js_function = Handle<JSFunction>::cast(holder);
1749 Debug::FloodWithOneShot(js_function);
kasperl@chromium.orgacae3782009-04-11 09:17:08 +00001750 }
1751 } else {
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001752 Debug::FloodWithOneShot(function);
kasperl@chromium.orgacae3782009-04-11 09:17:08 +00001753 }
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001754 }
1755 }
1756}
1757
1758
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001759void Debug::ClearStepping() {
1760 // Clear the various stepping setup.
1761 ClearOneShot();
1762 ClearStepIn();
ager@chromium.orga1645e22009-09-09 19:27:10 +00001763 ClearStepOut();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001764 ClearStepNext();
1765
1766 // Clear multiple step counter.
1767 thread_local_.step_count_ = 0;
1768}
1769
1770// Clears all the one-shot break points that are currently set. Normally this
1771// function is called each time a break point is hit as one shot break points
1772// are used to support stepping.
1773void Debug::ClearOneShot() {
1774 // The current implementation just runs through all the breakpoints. When the
v8.team.kasperl727e9952008-09-02 14:56:44 +00001775 // last break point for a function is removed that function is automatically
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001776 // removed from the list.
1777
1778 DebugInfoListNode* node = debug_info_list_;
1779 while (node != NULL) {
1780 BreakLocationIterator it(node->debug_info(), ALL_BREAK_LOCATIONS);
1781 while (!it.Done()) {
1782 it.ClearOneShot();
1783 it.Next();
1784 }
1785 node = node->next();
1786 }
1787}
1788
1789
1790void Debug::ActivateStepIn(StackFrame* frame) {
ager@chromium.orga1645e22009-09-09 19:27:10 +00001791 ASSERT(!StepOutActive());
mmassi@chromium.org7028c052012-06-13 11:51:58 +00001792 thread_local_.step_into_fp_ = frame->UnpaddedFP();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001793}
1794
1795
1796void Debug::ClearStepIn() {
1797 thread_local_.step_into_fp_ = 0;
1798}
1799
1800
ager@chromium.orga1645e22009-09-09 19:27:10 +00001801void Debug::ActivateStepOut(StackFrame* frame) {
1802 ASSERT(!StepInActive());
mmassi@chromium.org7028c052012-06-13 11:51:58 +00001803 thread_local_.step_out_fp_ = frame->UnpaddedFP();
ager@chromium.orga1645e22009-09-09 19:27:10 +00001804}
1805
1806
1807void Debug::ClearStepOut() {
1808 thread_local_.step_out_fp_ = 0;
1809}
1810
1811
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001812void Debug::ClearStepNext() {
1813 thread_local_.last_step_action_ = StepNone;
ager@chromium.org236ad962008-09-25 09:45:57 +00001814 thread_local_.last_statement_position_ = RelocInfo::kNoPosition;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001815 thread_local_.last_fp_ = 0;
1816}
1817
1818
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001819// Helper function to compile full code for debugging. This code will
mmassi@chromium.org7028c052012-06-13 11:51:58 +00001820// have debug break slots and deoptimization information. Deoptimization
1821// information is required in case that an optimized version of this
1822// function is still activated on the stack. It will also make sure that
1823// the full code is compiled with the same flags as the previous version,
1824// that is flags which can change the code generated. The current method
1825// of mapping from already compiled full code without debug break slots
1826// to full code with debug break slots depends on the generated code is
1827// otherwise exactly the same.
1828static bool CompileFullCodeForDebugging(Handle<JSFunction> function,
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001829 Handle<Code> current_code) {
1830 ASSERT(!current_code->has_debug_break_slots());
1831
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001832 CompilationInfoWithZone info(function);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001833 info.MarkCompilingForDebugging(current_code);
1834 ASSERT(!info.shared_info()->is_compiled());
1835 ASSERT(!info.isolate()->has_pending_exception());
1836
1837 // Use compile lazy which will end up compiling the full code in the
1838 // configuration configured above.
1839 bool result = Compiler::CompileLazy(&info);
1840 ASSERT(result != Isolate::Current()->has_pending_exception());
1841 info.isolate()->clear_pending_exception();
1842#if DEBUG
1843 if (result) {
mmassi@chromium.org7028c052012-06-13 11:51:58 +00001844 Handle<Code> new_code(function->shared()->code());
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001845 ASSERT(new_code->has_debug_break_slots());
1846 ASSERT(current_code->is_compiled_optimizable() ==
1847 new_code->is_compiled_optimizable());
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001848 }
1849#endif
1850 return result;
1851}
1852
1853
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001854static void CollectActiveFunctionsFromThread(
1855 Isolate* isolate,
1856 ThreadLocalTop* top,
1857 List<Handle<JSFunction> >* active_functions,
1858 Object* active_code_marker) {
1859 // Find all non-optimized code functions with activation frames
1860 // on the stack. This includes functions which have optimized
1861 // activations (including inlined functions) on the stack as the
1862 // non-optimized code is needed for the lazy deoptimization.
1863 for (JavaScriptFrameIterator it(isolate, top); !it.done(); it.Advance()) {
1864 JavaScriptFrame* frame = it.frame();
1865 if (frame->is_optimized()) {
1866 List<JSFunction*> functions(Compiler::kMaxInliningLevels + 1);
1867 frame->GetFunctions(&functions);
1868 for (int i = 0; i < functions.length(); i++) {
1869 JSFunction* function = functions[i];
1870 active_functions->Add(Handle<JSFunction>(function));
1871 function->shared()->code()->set_gc_metadata(active_code_marker);
1872 }
1873 } else if (frame->function()->IsJSFunction()) {
1874 JSFunction* function = JSFunction::cast(frame->function());
1875 ASSERT(frame->LookupCode()->kind() == Code::FUNCTION);
1876 active_functions->Add(Handle<JSFunction>(function));
1877 function->shared()->code()->set_gc_metadata(active_code_marker);
1878 }
1879 }
1880}
1881
1882
1883static void RedirectActivationsToRecompiledCodeOnThread(
1884 Isolate* isolate,
1885 ThreadLocalTop* top) {
1886 for (JavaScriptFrameIterator it(isolate, top); !it.done(); it.Advance()) {
1887 JavaScriptFrame* frame = it.frame();
1888
1889 if (frame->is_optimized() || !frame->function()->IsJSFunction()) continue;
1890
1891 JSFunction* function = JSFunction::cast(frame->function());
1892
1893 ASSERT(frame->LookupCode()->kind() == Code::FUNCTION);
1894
1895 Handle<Code> frame_code(frame->LookupCode());
1896 if (frame_code->has_debug_break_slots()) continue;
1897
1898 Handle<Code> new_code(function->shared()->code());
1899 if (new_code->kind() != Code::FUNCTION ||
1900 !new_code->has_debug_break_slots()) {
1901 continue;
1902 }
1903
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001904 // Iterate over the RelocInfo in the original code to compute the sum of the
1905 // constant pools sizes. (See Assembler::CheckConstPool())
1906 // Note that this is only useful for architectures using constant pools.
1907 int constpool_mask = RelocInfo::ModeMask(RelocInfo::CONST_POOL);
1908 int frame_const_pool_size = 0;
1909 for (RelocIterator it(*frame_code, constpool_mask); !it.done(); it.next()) {
1910 RelocInfo* info = it.rinfo();
1911 if (info->pc() >= frame->pc()) break;
1912 frame_const_pool_size += static_cast<int>(info->data());
1913 }
1914 intptr_t frame_offset =
1915 frame->pc() - frame_code->instruction_start() - frame_const_pool_size;
1916
1917 // Iterate over the RelocInfo for new code to find the number of bytes
1918 // generated for debug slots and constant pools.
1919 int debug_break_slot_bytes = 0;
1920 int new_code_const_pool_size = 0;
1921 int mask = RelocInfo::ModeMask(RelocInfo::DEBUG_BREAK_SLOT) |
1922 RelocInfo::ModeMask(RelocInfo::CONST_POOL);
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001923 for (RelocIterator it(*new_code, mask); !it.done(); it.next()) {
1924 // Check if the pc in the new code with debug break
1925 // slots is before this slot.
1926 RelocInfo* info = it.rinfo();
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001927 intptr_t new_offset = info->pc() - new_code->instruction_start() -
1928 new_code_const_pool_size - debug_break_slot_bytes;
1929 if (new_offset >= frame_offset) {
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001930 break;
1931 }
1932
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001933 if (RelocInfo::IsDebugBreakSlot(info->rmode())) {
1934 debug_break_slot_bytes += Assembler::kDebugBreakSlotLength;
1935 } else {
1936 ASSERT(RelocInfo::IsConstPool(info->rmode()));
1937 // The size of the constant pool is encoded in the data.
1938 new_code_const_pool_size += static_cast<int>(info->data());
1939 }
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001940 }
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001941
1942 // Compute the equivalent pc in the new code.
1943 byte* new_pc = new_code->instruction_start() + frame_offset +
1944 debug_break_slot_bytes + new_code_const_pool_size;
1945
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001946 if (FLAG_trace_deopt) {
1947 PrintF("Replacing code %08" V8PRIxPTR " - %08" V8PRIxPTR " (%d) "
1948 "with %08" V8PRIxPTR " - %08" V8PRIxPTR " (%d) "
1949 "for debugging, "
1950 "changing pc from %08" V8PRIxPTR " to %08" V8PRIxPTR "\n",
1951 reinterpret_cast<intptr_t>(
1952 frame_code->instruction_start()),
1953 reinterpret_cast<intptr_t>(
1954 frame_code->instruction_start()) +
1955 frame_code->instruction_size(),
1956 frame_code->instruction_size(),
1957 reinterpret_cast<intptr_t>(new_code->instruction_start()),
1958 reinterpret_cast<intptr_t>(new_code->instruction_start()) +
1959 new_code->instruction_size(),
1960 new_code->instruction_size(),
1961 reinterpret_cast<intptr_t>(frame->pc()),
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001962 reinterpret_cast<intptr_t>(new_pc));
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001963 }
1964
1965 // Patch the return address to return into the code with
1966 // debug break slots.
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001967 frame->set_pc(new_pc);
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001968 }
1969}
1970
1971
1972class ActiveFunctionsCollector : public ThreadVisitor {
1973 public:
1974 explicit ActiveFunctionsCollector(List<Handle<JSFunction> >* active_functions,
1975 Object* active_code_marker)
1976 : active_functions_(active_functions),
1977 active_code_marker_(active_code_marker) { }
1978
1979 void VisitThread(Isolate* isolate, ThreadLocalTop* top) {
1980 CollectActiveFunctionsFromThread(isolate,
1981 top,
1982 active_functions_,
1983 active_code_marker_);
1984 }
1985
1986 private:
1987 List<Handle<JSFunction> >* active_functions_;
1988 Object* active_code_marker_;
1989};
1990
1991
1992class ActiveFunctionsRedirector : public ThreadVisitor {
1993 public:
1994 void VisitThread(Isolate* isolate, ThreadLocalTop* top) {
1995 RedirectActivationsToRecompiledCodeOnThread(isolate, top);
1996 }
1997};
1998
1999
lrn@chromium.org34e60782011-09-15 07:25:40 +00002000void Debug::PrepareForBreakPoints() {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002001 // If preparing for the first break point make sure to deoptimize all
2002 // functions as debugging does not work with optimized code.
2003 if (!has_break_points_) {
svenpanne@chromium.org876cca82013-03-18 14:43:20 +00002004 Deoptimizer::DeoptimizeAll(isolate_);
lrn@chromium.org34e60782011-09-15 07:25:40 +00002005
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002006 Handle<Code> lazy_compile =
2007 Handle<Code>(isolate_->builtins()->builtin(Builtins::kLazyCompile));
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002008
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00002009 // There will be at least one break point when we are done.
2010 has_break_points_ = true;
2011
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002012 // Keep the list of activated functions in a handlified list as it
2013 // is used both in GC and non-GC code.
2014 List<Handle<JSFunction> > active_functions(100);
lrn@chromium.org34e60782011-09-15 07:25:40 +00002015
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002016 {
2017 // We are going to iterate heap to find all functions without
2018 // debug break slots.
hpayer@chromium.org7c3372b2013-02-13 17:26:04 +00002019 Heap* heap = isolate_->heap();
2020 heap->CollectAllGarbage(Heap::kMakeHeapIterableMask,
2021 "preparing for breakpoints");
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002022
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002023 // Ensure no GC in this scope as we are going to use gc_metadata
2024 // field in the Code object to mark active functions.
rossberg@chromium.org79e79022013-06-03 15:43:46 +00002025 DisallowHeapAllocation no_allocation;
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002026
hpayer@chromium.org7c3372b2013-02-13 17:26:04 +00002027 Object* active_code_marker = heap->the_hole_value();
svenpanne@chromium.orgecb9dd62011-12-01 08:22:35 +00002028
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002029 CollectActiveFunctionsFromThread(isolate_,
2030 isolate_->thread_local_top(),
2031 &active_functions,
2032 active_code_marker);
2033 ActiveFunctionsCollector active_functions_collector(&active_functions,
2034 active_code_marker);
2035 isolate_->thread_manager()->IterateArchivedThreads(
2036 &active_functions_collector);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002037
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002038 // Scan the heap for all non-optimized functions which have no
2039 // debug break slots and are not active or inlined into an active
2040 // function and mark them for lazy compilation.
hpayer@chromium.org7c3372b2013-02-13 17:26:04 +00002041 HeapIterator iterator(heap);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002042 HeapObject* obj = NULL;
2043 while (((obj = iterator.next()) != NULL)) {
2044 if (obj->IsJSFunction()) {
2045 JSFunction* function = JSFunction::cast(obj);
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002046 SharedFunctionInfo* shared = function->shared();
2047 if (shared->allows_lazy_compilation() &&
2048 shared->script()->IsScript() &&
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002049 function->code()->kind() == Code::FUNCTION &&
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002050 !function->code()->has_debug_break_slots() &&
2051 shared->code()->gc_metadata() != active_code_marker) {
2052 function->set_code(*lazy_compile);
2053 function->shared()->set_code(*lazy_compile);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002054 }
2055 }
lrn@chromium.org34e60782011-09-15 07:25:40 +00002056 }
lrn@chromium.org34e60782011-09-15 07:25:40 +00002057
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002058 // Clear gc_metadata field.
2059 for (int i = 0; i < active_functions.length(); i++) {
2060 Handle<JSFunction> function = active_functions[i];
2061 function->shared()->code()->set_gc_metadata(Smi::FromInt(0));
2062 }
2063 }
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002064
2065 // Now recompile all functions with activation frames and and
2066 // patch the return address to run in the new compiled code.
2067 for (int i = 0; i < active_functions.length(); i++) {
2068 Handle<JSFunction> function = active_functions[i];
mmassi@chromium.org7028c052012-06-13 11:51:58 +00002069 Handle<SharedFunctionInfo> shared(function->shared());
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002070
2071 if (function->code()->kind() == Code::FUNCTION &&
2072 function->code()->has_debug_break_slots()) {
2073 // Nothing to do. Function code already had debug break slots.
2074 continue;
2075 }
2076
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002077 // If recompilation is not possible just skip it.
2078 if (shared->is_toplevel() ||
2079 !shared->allows_lazy_compilation() ||
2080 shared->code()->kind() == Code::BUILTIN) {
2081 continue;
2082 }
2083
2084 // Make sure that the shared full code is compiled with debug
2085 // break slots.
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00002086 if (!shared->code()->has_debug_break_slots()) {
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002087 // Try to compile the full code with debug break slots. If it
2088 // fails just keep the current code.
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00002089 Handle<Code> current_code(function->shared()->code());
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002090 shared->set_code(*lazy_compile);
2091 bool prev_force_debugger_active =
2092 isolate_->debugger()->force_debugger_active();
2093 isolate_->debugger()->set_force_debugger_active(true);
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00002094 ASSERT(current_code->kind() == Code::FUNCTION);
mmassi@chromium.org7028c052012-06-13 11:51:58 +00002095 CompileFullCodeForDebugging(function, current_code);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002096 isolate_->debugger()->set_force_debugger_active(
2097 prev_force_debugger_active);
2098 if (!shared->is_compiled()) {
2099 shared->set_code(*current_code);
2100 continue;
2101 }
2102 }
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002103
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002104 // Keep function code in sync with shared function info.
2105 function->set_code(shared->code());
lrn@chromium.org34e60782011-09-15 07:25:40 +00002106 }
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002107
2108 RedirectActivationsToRecompiledCodeOnThread(isolate_,
2109 isolate_->thread_local_top());
2110
2111 ActiveFunctionsRedirector active_functions_redirector;
2112 isolate_->thread_manager()->IterateArchivedThreads(
2113 &active_functions_redirector);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002114 }
lrn@chromium.org34e60782011-09-15 07:25:40 +00002115}
2116
2117
jkummerow@chromium.org78502a92012-09-06 13:50:42 +00002118Object* Debug::FindSharedFunctionInfoInScript(Handle<Script> script,
2119 int position) {
2120 // Iterate the heap looking for SharedFunctionInfo generated from the
2121 // script. The inner most SharedFunctionInfo containing the source position
2122 // for the requested break point is found.
2123 // NOTE: This might require several heap iterations. If the SharedFunctionInfo
2124 // which is found is not compiled it is compiled and the heap is iterated
2125 // again as the compilation might create inner functions from the newly
2126 // compiled function and the actual requested break point might be in one of
2127 // these functions.
2128 // NOTE: The below fix-point iteration depends on all functions that cannot be
2129 // compiled lazily without a context to not be compiled at all. Compilation
2130 // will be triggered at points where we do not need a context.
2131 bool done = false;
2132 // The current candidate for the source position:
2133 int target_start_position = RelocInfo::kNoPosition;
2134 Handle<JSFunction> target_function;
2135 Handle<SharedFunctionInfo> target;
hpayer@chromium.org7c3372b2013-02-13 17:26:04 +00002136 Heap* heap = isolate_->heap();
jkummerow@chromium.org78502a92012-09-06 13:50:42 +00002137 while (!done) {
2138 { // Extra scope for iterator and no-allocation.
hpayer@chromium.org7c3372b2013-02-13 17:26:04 +00002139 heap->EnsureHeapIsIterable();
rossberg@chromium.org79e79022013-06-03 15:43:46 +00002140 DisallowHeapAllocation no_alloc_during_heap_iteration;
hpayer@chromium.org7c3372b2013-02-13 17:26:04 +00002141 HeapIterator iterator(heap);
jkummerow@chromium.org78502a92012-09-06 13:50:42 +00002142 for (HeapObject* obj = iterator.next();
2143 obj != NULL; obj = iterator.next()) {
2144 bool found_next_candidate = false;
2145 Handle<JSFunction> function;
2146 Handle<SharedFunctionInfo> shared;
2147 if (obj->IsJSFunction()) {
2148 function = Handle<JSFunction>(JSFunction::cast(obj));
2149 shared = Handle<SharedFunctionInfo>(function->shared());
2150 ASSERT(shared->allows_lazy_compilation() || shared->is_compiled());
2151 found_next_candidate = true;
2152 } else if (obj->IsSharedFunctionInfo()) {
2153 shared = Handle<SharedFunctionInfo>(SharedFunctionInfo::cast(obj));
2154 // Skip functions that we cannot compile lazily without a context,
2155 // which is not available here, because there is no closure.
2156 found_next_candidate = shared->is_compiled() ||
2157 shared->allows_lazy_compilation_without_context();
2158 }
2159 if (!found_next_candidate) continue;
2160 if (shared->script() == *script) {
2161 // If the SharedFunctionInfo found has the requested script data and
2162 // contains the source position it is a candidate.
2163 int start_position = shared->function_token_position();
2164 if (start_position == RelocInfo::kNoPosition) {
2165 start_position = shared->start_position();
2166 }
2167 if (start_position <= position &&
2168 position <= shared->end_position()) {
2169 // If there is no candidate or this function is within the current
2170 // candidate this is the new candidate.
2171 if (target.is_null()) {
2172 target_start_position = start_position;
2173 target_function = function;
2174 target = shared;
2175 } else {
2176 if (target_start_position == start_position &&
2177 shared->end_position() == target->end_position()) {
2178 // If a top-level function contains only one function
2179 // declaration the source for the top-level and the function
2180 // is the same. In that case prefer the non top-level function.
2181 if (!shared->is_toplevel()) {
2182 target_start_position = start_position;
2183 target_function = function;
2184 target = shared;
2185 }
2186 } else if (target_start_position <= start_position &&
2187 shared->end_position() <= target->end_position()) {
2188 // This containment check includes equality as a function
2189 // inside a top-level function can share either start or end
2190 // position with the top-level function.
2191 target_start_position = start_position;
2192 target_function = function;
2193 target = shared;
2194 }
2195 }
2196 }
2197 }
2198 } // End for loop.
2199 } // End no-allocation scope.
2200
hpayer@chromium.org7c3372b2013-02-13 17:26:04 +00002201 if (target.is_null()) return heap->undefined_value();
jkummerow@chromium.org78502a92012-09-06 13:50:42 +00002202
2203 // There will be at least one break point when we are done.
2204 has_break_points_ = true;
2205
2206 // If the candidate found is compiled we are done.
2207 done = target->is_compiled();
2208 if (!done) {
2209 // If the candidate is not compiled, compile it to reveal any inner
2210 // functions which might contain the requested source position. This
2211 // will compile all inner functions that cannot be compiled without a
2212 // context, because Compiler::BuildFunctionInfo checks whether the
2213 // debugger is active.
2214 if (target_function.is_null()) {
2215 SharedFunctionInfo::CompileLazy(target, KEEP_EXCEPTION);
2216 } else {
2217 JSFunction::CompileLazy(target_function, KEEP_EXCEPTION);
2218 }
2219 }
2220 } // End while loop.
2221
2222 return *target;
2223}
2224
2225
lrn@chromium.org34e60782011-09-15 07:25:40 +00002226// Ensures the debug information is present for shared.
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00002227bool Debug::EnsureDebugInfo(Handle<SharedFunctionInfo> shared,
2228 Handle<JSFunction> function) {
lrn@chromium.org34e60782011-09-15 07:25:40 +00002229 // Return if we already have the debug info for shared.
2230 if (HasDebugInfo(shared)) {
2231 ASSERT(shared->is_compiled());
2232 return true;
2233 }
2234
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00002235 // There will be at least one break point when we are done.
2236 has_break_points_ = true;
2237
2238 // Ensure function is compiled. Return false if this failed.
2239 if (!function.is_null() &&
2240 !JSFunction::EnsureCompiled(function, CLEAR_EXCEPTION)) {
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002241 return false;
2242 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002243
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002244 // Create the debug info object.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002245 Handle<DebugInfo> debug_info = FACTORY->NewDebugInfo(shared);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002246
2247 // Add debug info to the list.
2248 DebugInfoListNode* node = new DebugInfoListNode(*debug_info);
2249 node->set_next(debug_info_list_);
2250 debug_info_list_ = node;
2251
kasper.lundbd3ec4e2008-07-09 11:06:54 +00002252 return true;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002253}
2254
2255
2256void Debug::RemoveDebugInfo(Handle<DebugInfo> debug_info) {
2257 ASSERT(debug_info_list_ != NULL);
2258 // Run through the debug info objects to find this one and remove it.
2259 DebugInfoListNode* prev = NULL;
2260 DebugInfoListNode* current = debug_info_list_;
2261 while (current != NULL) {
2262 if (*current->debug_info() == *debug_info) {
2263 // Unlink from list. If prev is NULL we are looking at the first element.
2264 if (prev == NULL) {
2265 debug_info_list_ = current->next();
2266 } else {
2267 prev->set_next(current->next());
2268 }
lrn@chromium.org7516f052011-03-30 08:52:27 +00002269 current->debug_info()->shared()->set_debug_info(
2270 isolate_->heap()->undefined_value());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002271 delete current;
2272
2273 // If there are no more debug info objects there are not more break
2274 // points.
2275 has_break_points_ = debug_info_list_ != NULL;
2276
2277 return;
2278 }
2279 // Move to next in list.
2280 prev = current;
2281 current = current->next();
2282 }
2283 UNREACHABLE();
2284}
2285
2286
2287void Debug::SetAfterBreakTarget(JavaScriptFrame* frame) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002288 HandleScope scope(isolate_);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002289
lrn@chromium.org34e60782011-09-15 07:25:40 +00002290 PrepareForBreakPoints();
2291
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002292 // Get the executing function in which the debug break occurred.
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00002293 Handle<JSFunction> function(JSFunction::cast(frame->function()));
2294 Handle<SharedFunctionInfo> shared(function->shared());
2295 if (!EnsureDebugInfo(shared, function)) {
kasper.lundbd3ec4e2008-07-09 11:06:54 +00002296 // Return if we failed to retrieve the debug info.
2297 return;
2298 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002299 Handle<DebugInfo> debug_info = GetDebugInfo(shared);
2300 Handle<Code> code(debug_info->code());
2301 Handle<Code> original_code(debug_info->original_code());
2302#ifdef DEBUG
2303 // Get the code which is actually executing.
vegorov@chromium.org74f333b2011-04-06 11:17:46 +00002304 Handle<Code> frame_code(frame->LookupCode());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002305 ASSERT(frame_code.is_identical_to(code));
2306#endif
2307
2308 // Find the call address in the running code. This address holds the call to
2309 // either a DebugBreakXXX or to the debug break return entry code if the
2310 // break point is still active after processing the break point.
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00002311 Address addr = frame->pc() - Assembler::kPatchDebugBreakSlotReturnOffset;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002312
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002313 // Check if the location is at JS exit or debug break slot.
ager@chromium.orga1645e22009-09-09 19:27:10 +00002314 bool at_js_return = false;
2315 bool break_at_js_return_active = false;
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002316 bool at_debug_break_slot = false;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002317 RelocIterator it(debug_info->code());
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002318 while (!it.done() && !at_js_return && !at_debug_break_slot) {
ager@chromium.org236ad962008-09-25 09:45:57 +00002319 if (RelocInfo::IsJSReturn(it.rinfo()->rmode())) {
ager@chromium.orga1645e22009-09-09 19:27:10 +00002320 at_js_return = (it.rinfo()->pc() ==
2321 addr - Assembler::kPatchReturnSequenceAddressOffset);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002322 break_at_js_return_active = it.rinfo()->IsPatchedReturnSequence();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002323 }
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002324 if (RelocInfo::IsDebugBreakSlot(it.rinfo()->rmode())) {
2325 at_debug_break_slot = (it.rinfo()->pc() ==
2326 addr - Assembler::kPatchDebugBreakSlotAddressOffset);
2327 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002328 it.next();
2329 }
2330
2331 // Handle the jump to continue execution after break point depending on the
2332 // break location.
ager@chromium.orga1645e22009-09-09 19:27:10 +00002333 if (at_js_return) {
2334 // If the break point as return is still active jump to the corresponding
2335 // place in the original code. If not the break point was removed during
2336 // break point processing.
2337 if (break_at_js_return_active) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002338 addr += original_code->instruction_start() - code->instruction_start();
2339 }
2340
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00002341 // Move back to where the call instruction sequence started.
2342 thread_local_.after_break_target_ =
2343 addr - Assembler::kPatchReturnSequenceAddressOffset;
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002344 } else if (at_debug_break_slot) {
2345 // Address of where the debug break slot starts.
2346 addr = addr - Assembler::kPatchDebugBreakSlotAddressOffset;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002347
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002348 // Continue just after the slot.
2349 thread_local_.after_break_target_ = addr + Assembler::kDebugBreakSlotLength;
2350 } else if (IsDebugBreak(Assembler::target_address_at(addr))) {
2351 // We now know that there is still a debug break call at the target address,
2352 // so the break point is still there and the original code will hold the
2353 // address to jump to in order to complete the call which is replaced by a
2354 // call to DebugBreakXXX.
2355
2356 // Find the corresponding address in the original code.
2357 addr += original_code->instruction_start() - code->instruction_start();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002358
2359 // Install jump to the call address in the original code. This will be the
2360 // call which was overwritten by the call to DebugBreakXXX.
2361 thread_local_.after_break_target_ = Assembler::target_address_at(addr);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002362 } else {
2363 // There is no longer a break point present. Don't try to look in the
2364 // original code as the running code will have the right address. This takes
2365 // care of the case where the last break point is removed from the function
2366 // and therefore no "original code" is available.
2367 thread_local_.after_break_target_ = Assembler::target_address_at(addr);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002368 }
2369}
2370
2371
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00002372bool Debug::IsBreakAtReturn(JavaScriptFrame* frame) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002373 HandleScope scope(isolate_);
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00002374
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00002375 // If there are no break points this cannot be break at return, as
2376 // the debugger statement and stack guard bebug break cannot be at
2377 // return.
2378 if (!has_break_points_) {
2379 return false;
2380 }
2381
lrn@chromium.org34e60782011-09-15 07:25:40 +00002382 PrepareForBreakPoints();
2383
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00002384 // Get the executing function in which the debug break occurred.
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00002385 Handle<JSFunction> function(JSFunction::cast(frame->function()));
2386 Handle<SharedFunctionInfo> shared(function->shared());
2387 if (!EnsureDebugInfo(shared, function)) {
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00002388 // Return if we failed to retrieve the debug info.
2389 return false;
2390 }
2391 Handle<DebugInfo> debug_info = GetDebugInfo(shared);
2392 Handle<Code> code(debug_info->code());
2393#ifdef DEBUG
2394 // Get the code which is actually executing.
vegorov@chromium.org74f333b2011-04-06 11:17:46 +00002395 Handle<Code> frame_code(frame->LookupCode());
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00002396 ASSERT(frame_code.is_identical_to(code));
2397#endif
2398
2399 // Find the call address in the running code.
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00002400 Address addr = frame->pc() - Assembler::kPatchDebugBreakSlotReturnOffset;
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00002401
2402 // Check if the location is at JS return.
2403 RelocIterator it(debug_info->code());
2404 while (!it.done()) {
2405 if (RelocInfo::IsJSReturn(it.rinfo()->rmode())) {
2406 return (it.rinfo()->pc() ==
2407 addr - Assembler::kPatchReturnSequenceAddressOffset);
2408 }
2409 it.next();
2410 }
2411 return false;
2412}
2413
2414
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00002415void Debug::FramesHaveBeenDropped(StackFrame::Id new_break_frame_id,
whesse@chromium.orge90029b2010-08-02 11:52:17 +00002416 FrameDropMode mode,
2417 Object** restarter_frame_function_pointer) {
ulan@chromium.orgd9e468a2012-06-25 09:47:40 +00002418 if (mode != CURRENTLY_SET_MODE) {
2419 thread_local_.frame_drop_mode_ = mode;
2420 }
ager@chromium.org357bf652010-04-12 11:30:10 +00002421 thread_local_.break_frame_id_ = new_break_frame_id;
whesse@chromium.orge90029b2010-08-02 11:52:17 +00002422 thread_local_.restarter_frame_function_pointer_ =
2423 restarter_frame_function_pointer;
ager@chromium.org357bf652010-04-12 11:30:10 +00002424}
2425
2426
jkummerow@chromium.org212d9642012-05-11 15:02:09 +00002427const int Debug::FramePaddingLayout::kInitialSize = 1;
2428
2429
2430// Any even value bigger than kInitialSize as needed for stack scanning.
2431const int Debug::FramePaddingLayout::kPaddingValue = kInitialSize + 1;
2432
2433
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002434bool Debug::IsDebugGlobal(GlobalObject* global) {
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00002435 return IsLoaded() && global == debug_context()->global_object();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002436}
2437
2438
ager@chromium.org32912102009-01-16 10:38:43 +00002439void Debug::ClearMirrorCache() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002440 PostponeInterruptsScope postpone(isolate_);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002441 HandleScope scope(isolate_);
2442 ASSERT(isolate_->context() == *Debug::debug_context());
ager@chromium.org32912102009-01-16 10:38:43 +00002443
2444 // Clear the mirror cache.
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00002445 Handle<String> function_name = isolate_->factory()->InternalizeOneByteString(
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00002446 STATIC_ASCII_VECTOR("ClearMirrorCache"));
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00002447 Handle<Object> fun(
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00002448 isolate_->global_object()->GetPropertyNoExceptionThrown(*function_name),
2449 isolate_);
ager@chromium.org32912102009-01-16 10:38:43 +00002450 ASSERT(fun->IsJSFunction());
2451 bool caught_exception;
rossberg@chromium.org717967f2011-07-20 13:44:42 +00002452 Execution::TryCall(Handle<JSFunction>::cast(fun),
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00002453 Handle<JSObject>(Debug::debug_context()->global_object()),
ager@chromium.org32912102009-01-16 10:38:43 +00002454 0, NULL, &caught_exception);
2455}
2456
2457
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002458void Debug::CreateScriptCache() {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002459 Heap* heap = isolate_->heap();
2460 HandleScope scope(isolate_);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002461
2462 // Perform two GCs to get rid of all unreferenced scripts. The first GC gets
2463 // rid of all the cached script wrappers and the second gets rid of the
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002464 // scripts which are no longer referenced. The second also sweeps precisely,
2465 // which saves us doing yet another GC to make the heap iterable.
rossberg@chromium.org994edf62012-02-06 10:12:55 +00002466 heap->CollectAllGarbage(Heap::kNoGCFlags, "Debug::CreateScriptCache");
2467 heap->CollectAllGarbage(Heap::kMakeHeapIterableMask,
2468 "Debug::CreateScriptCache");
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002469
2470 ASSERT(script_cache_ == NULL);
2471 script_cache_ = new ScriptCache();
2472
2473 // Scan heap for Script objects.
2474 int count = 0;
hpayer@chromium.org7c3372b2013-02-13 17:26:04 +00002475 HeapIterator iterator(heap);
rossberg@chromium.org79e79022013-06-03 15:43:46 +00002476 DisallowHeapAllocation no_allocation;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002477
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002478 for (HeapObject* obj = iterator.next(); obj != NULL; obj = iterator.next()) {
sgjesse@chromium.org152a0b02009-10-07 13:50:16 +00002479 if (obj->IsScript() && Script::cast(obj)->HasValidSource()) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002480 script_cache_->Add(Handle<Script>(Script::cast(obj)));
2481 count++;
2482 }
2483 }
2484}
2485
2486
2487void Debug::DestroyScriptCache() {
2488 // Get rid of the script cache if it was created.
2489 if (script_cache_ != NULL) {
2490 delete script_cache_;
2491 script_cache_ = NULL;
2492 }
2493}
2494
2495
2496void Debug::AddScriptToScriptCache(Handle<Script> script) {
2497 if (script_cache_ != NULL) {
2498 script_cache_->Add(script);
2499 }
2500}
2501
2502
2503Handle<FixedArray> Debug::GetLoadedScripts() {
2504 // Create and fill the script cache when the loaded scripts is requested for
2505 // the first time.
2506 if (script_cache_ == NULL) {
2507 CreateScriptCache();
2508 }
2509
2510 // If the script cache is not active just return an empty array.
2511 ASSERT(script_cache_ != NULL);
2512 if (script_cache_ == NULL) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002513 isolate_->factory()->NewFixedArray(0);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002514 }
2515
2516 // Perform GC to get unreferenced scripts evicted from the cache before
2517 // returning the content.
rossberg@chromium.org994edf62012-02-06 10:12:55 +00002518 isolate_->heap()->CollectAllGarbage(Heap::kNoGCFlags,
2519 "Debug::GetLoadedScripts");
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002520
2521 // Get the scripts from the cache.
2522 return script_cache_->GetScripts();
2523}
2524
2525
2526void Debug::AfterGarbageCollection() {
2527 // Generate events for collected scripts.
2528 if (script_cache_ != NULL) {
2529 script_cache_->ProcessCollectedScripts();
2530 }
2531}
2532
2533
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00002534Debugger::Debugger(Isolate* isolate)
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +00002535 : debugger_access_(isolate->debugger_access()),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002536 event_listener_(Handle<Object>()),
2537 event_listener_data_(Handle<Object>()),
2538 compiling_natives_(false),
2539 is_loading_debugger_(false),
mstarzinger@chromium.orgde886792012-09-11 13:22:37 +00002540 live_edit_enabled_(true),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002541 never_unload_debugger_(false),
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002542 force_debugger_active_(false),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002543 message_handler_(NULL),
2544 debugger_unload_pending_(false),
2545 host_dispatch_handler_(NULL),
2546 dispatch_handler_access_(OS::CreateMutex()),
2547 debug_message_dispatch_handler_(NULL),
2548 message_dispatch_helper_thread_(NULL),
2549 host_dispatch_micros_(100 * 1000),
2550 agent_(NULL),
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00002551 command_queue_(isolate->logger(), kQueueInitialSize),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002552 command_received_(OS::CreateSemaphore(0)),
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00002553 event_command_queue_(isolate->logger(), kQueueInitialSize),
2554 isolate_(isolate) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002555}
2556
2557
2558Debugger::~Debugger() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002559 delete dispatch_handler_access_;
2560 dispatch_handler_access_ = 0;
2561 delete command_received_;
2562 command_received_ = 0;
2563}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002564
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002565
2566Handle<Object> Debugger::MakeJSObject(Vector<const char> constructor_name,
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002567 int argc,
2568 Handle<Object> argv[],
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002569 bool* caught_exception) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002570 ASSERT(isolate_->context() == *isolate_->debug()->debug_context());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002571
2572 // Create the execution state object.
lrn@chromium.org7516f052011-03-30 08:52:27 +00002573 Handle<String> constructor_str =
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00002574 isolate_->factory()->InternalizeUtf8String(constructor_name);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002575 Handle<Object> constructor(
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00002576 isolate_->global_object()->GetPropertyNoExceptionThrown(*constructor_str),
2577 isolate_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002578 ASSERT(constructor->IsJSFunction());
2579 if (!constructor->IsJSFunction()) {
2580 *caught_exception = true;
lrn@chromium.org7516f052011-03-30 08:52:27 +00002581 return isolate_->factory()->undefined_value();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002582 }
2583 Handle<Object> js_object = Execution::TryCall(
2584 Handle<JSFunction>::cast(constructor),
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00002585 Handle<JSObject>(isolate_->debug()->debug_context()->global_object()),
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002586 argc,
2587 argv,
2588 caught_exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002589 return js_object;
2590}
2591
2592
2593Handle<Object> Debugger::MakeExecutionState(bool* caught_exception) {
2594 // Create the execution state object.
lrn@chromium.org7516f052011-03-30 08:52:27 +00002595 Handle<Object> break_id = isolate_->factory()->NewNumberFromInt(
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002596 isolate_->debug()->break_id());
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002597 Handle<Object> argv[] = { break_id };
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002598 return MakeJSObject(CStrVector("MakeExecutionState"),
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002599 ARRAY_SIZE(argv),
2600 argv,
2601 caught_exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002602}
2603
2604
2605Handle<Object> Debugger::MakeBreakEvent(Handle<Object> exec_state,
2606 Handle<Object> break_points_hit,
2607 bool* caught_exception) {
2608 // Create the new break event object.
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002609 Handle<Object> argv[] = { exec_state, break_points_hit };
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002610 return MakeJSObject(CStrVector("MakeBreakEvent"),
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002611 ARRAY_SIZE(argv),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002612 argv,
2613 caught_exception);
2614}
2615
2616
2617Handle<Object> Debugger::MakeExceptionEvent(Handle<Object> exec_state,
2618 Handle<Object> exception,
2619 bool uncaught,
2620 bool* caught_exception) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002621 Factory* factory = isolate_->factory();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002622 // Create the new exception event object.
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002623 Handle<Object> argv[] = { exec_state,
2624 exception,
2625 factory->ToBoolean(uncaught) };
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002626 return MakeJSObject(CStrVector("MakeExceptionEvent"),
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002627 ARRAY_SIZE(argv),
2628 argv,
2629 caught_exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002630}
2631
2632
2633Handle<Object> Debugger::MakeNewFunctionEvent(Handle<Object> function,
2634 bool* caught_exception) {
2635 // Create the new function event object.
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002636 Handle<Object> argv[] = { function };
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002637 return MakeJSObject(CStrVector("MakeNewFunctionEvent"),
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002638 ARRAY_SIZE(argv),
2639 argv,
2640 caught_exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002641}
2642
2643
2644Handle<Object> Debugger::MakeCompileEvent(Handle<Script> script,
iposva@chromium.org245aa852009-02-10 00:49:54 +00002645 bool before,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002646 bool* caught_exception) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002647 Factory* factory = isolate_->factory();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002648 // Create the compile event object.
2649 Handle<Object> exec_state = MakeExecutionState(caught_exception);
iposva@chromium.org245aa852009-02-10 00:49:54 +00002650 Handle<Object> script_wrapper = GetScriptWrapper(script);
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002651 Handle<Object> argv[] = { exec_state,
2652 script_wrapper,
2653 factory->ToBoolean(before) };
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002654 return MakeJSObject(CStrVector("MakeCompileEvent"),
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002655 ARRAY_SIZE(argv),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002656 argv,
2657 caught_exception);
2658}
2659
2660
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002661Handle<Object> Debugger::MakeScriptCollectedEvent(int id,
2662 bool* caught_exception) {
2663 // Create the script collected event object.
2664 Handle<Object> exec_state = MakeExecutionState(caught_exception);
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00002665 Handle<Object> id_object = Handle<Smi>(Smi::FromInt(id), isolate_);
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002666 Handle<Object> argv[] = { exec_state, id_object };
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002667
2668 return MakeJSObject(CStrVector("MakeScriptCollectedEvent"),
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002669 ARRAY_SIZE(argv),
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002670 argv,
2671 caught_exception);
2672}
2673
2674
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002675void Debugger::OnException(Handle<Object> exception, bool uncaught) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002676 HandleScope scope(isolate_);
2677 Debug* debug = isolate_->debug();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002678
2679 // Bail out based on state or if there is no listener for this event
lrn@chromium.org7516f052011-03-30 08:52:27 +00002680 if (debug->InDebugger()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002681 if (!Debugger::EventActive(v8::Exception)) return;
2682
2683 // Bail out if exception breaks are not active
2684 if (uncaught) {
2685 // Uncaught exceptions are reported by either flags.
lrn@chromium.org7516f052011-03-30 08:52:27 +00002686 if (!(debug->break_on_uncaught_exception() ||
2687 debug->break_on_exception())) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002688 } else {
2689 // Caught exceptions are reported is activated.
lrn@chromium.org7516f052011-03-30 08:52:27 +00002690 if (!debug->break_on_exception()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002691 }
2692
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002693 // Enter the debugger.
2694 EnterDebugger debugger;
2695 if (debugger.FailedToEnter()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002696
2697 // Clear all current stepping setup.
lrn@chromium.org7516f052011-03-30 08:52:27 +00002698 debug->ClearStepping();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002699 // Create the event data object.
2700 bool caught_exception = false;
2701 Handle<Object> exec_state = MakeExecutionState(&caught_exception);
2702 Handle<Object> event_data;
2703 if (!caught_exception) {
2704 event_data = MakeExceptionEvent(exec_state, exception, uncaught,
2705 &caught_exception);
2706 }
2707 // Bail out and don't call debugger if exception.
2708 if (caught_exception) {
2709 return;
2710 }
2711
ager@chromium.org5ec48922009-05-05 07:25:34 +00002712 // Process debug event.
2713 ProcessDebugEvent(v8::Exception, Handle<JSObject>::cast(event_data), false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002714 // Return to continue execution from where the exception was thrown.
2715}
2716
2717
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002718void Debugger::OnDebugBreak(Handle<Object> break_points_hit,
2719 bool auto_continue) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002720 HandleScope scope(isolate_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002721
kasper.lund212ac232008-07-16 07:07:30 +00002722 // Debugger has already been entered by caller.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002723 ASSERT(isolate_->context() == *isolate_->debug()->debug_context());
kasper.lund212ac232008-07-16 07:07:30 +00002724
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002725 // Bail out if there is no listener for this event
2726 if (!Debugger::EventActive(v8::Break)) return;
2727
2728 // Debugger must be entered in advance.
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00002729 ASSERT(isolate_->context() == *isolate_->debug()->debug_context());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002730
2731 // Create the event data object.
2732 bool caught_exception = false;
2733 Handle<Object> exec_state = MakeExecutionState(&caught_exception);
2734 Handle<Object> event_data;
2735 if (!caught_exception) {
2736 event_data = MakeBreakEvent(exec_state, break_points_hit,
2737 &caught_exception);
2738 }
2739 // Bail out and don't call debugger if exception.
2740 if (caught_exception) {
2741 return;
2742 }
2743
ager@chromium.org5ec48922009-05-05 07:25:34 +00002744 // Process debug event.
2745 ProcessDebugEvent(v8::Break,
2746 Handle<JSObject>::cast(event_data),
2747 auto_continue);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002748}
2749
2750
2751void Debugger::OnBeforeCompile(Handle<Script> script) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002752 HandleScope scope(isolate_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002753
2754 // Bail out based on state or if there is no listener for this event
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002755 if (isolate_->debug()->InDebugger()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002756 if (compiling_natives()) return;
2757 if (!EventActive(v8::BeforeCompile)) return;
2758
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002759 // Enter the debugger.
2760 EnterDebugger debugger;
2761 if (debugger.FailedToEnter()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002762
2763 // Create the event data object.
2764 bool caught_exception = false;
iposva@chromium.org245aa852009-02-10 00:49:54 +00002765 Handle<Object> event_data = MakeCompileEvent(script, true, &caught_exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002766 // Bail out and don't call debugger if exception.
2767 if (caught_exception) {
2768 return;
2769 }
2770
ager@chromium.org5ec48922009-05-05 07:25:34 +00002771 // Process debug event.
2772 ProcessDebugEvent(v8::BeforeCompile,
2773 Handle<JSObject>::cast(event_data),
2774 true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002775}
2776
2777
2778// Handle debugger actions when a new script is compiled.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00002779void Debugger::OnAfterCompile(Handle<Script> script,
2780 AfterCompileFlags after_compile_flags) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002781 HandleScope scope(isolate_);
2782 Debug* debug = isolate_->debug();
kasper.lund212ac232008-07-16 07:07:30 +00002783
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002784 // Add the newly compiled script to the script cache.
lrn@chromium.org7516f052011-03-30 08:52:27 +00002785 debug->AddScriptToScriptCache(script);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002786
2787 // No more to do if not debugging.
ager@chromium.org71daaf62009-04-01 07:22:49 +00002788 if (!IsDebuggerActive()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002789
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002790 // No compile events while compiling natives.
2791 if (compiling_natives()) return;
2792
iposva@chromium.org245aa852009-02-10 00:49:54 +00002793 // Store whether in debugger before entering debugger.
lrn@chromium.org7516f052011-03-30 08:52:27 +00002794 bool in_debugger = debug->InDebugger();
iposva@chromium.org245aa852009-02-10 00:49:54 +00002795
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002796 // Enter the debugger.
2797 EnterDebugger debugger;
2798 if (debugger.FailedToEnter()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002799
2800 // If debugging there might be script break points registered for this
2801 // script. Make sure that these break points are set.
2802
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002803 // Get the function UpdateScriptBreakPoints (defined in debug-debugger.js).
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00002804 Handle<String> update_script_break_points_string =
2805 isolate_->factory()->InternalizeOneByteString(
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00002806 STATIC_ASCII_VECTOR("UpdateScriptBreakPoints"));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002807 Handle<Object> update_script_break_points =
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00002808 Handle<Object>(
2809 debug->debug_context()->global_object()->GetPropertyNoExceptionThrown(
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00002810 *update_script_break_points_string),
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00002811 isolate_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002812 if (!update_script_break_points->IsJSFunction()) {
2813 return;
2814 }
2815 ASSERT(update_script_break_points->IsJSFunction());
2816
2817 // Wrap the script object in a proper JS object before passing it
2818 // to JavaScript.
2819 Handle<JSValue> wrapper = GetScriptWrapper(script);
2820
2821 // Call UpdateScriptBreakPoints expect no exceptions.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002822 bool caught_exception;
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002823 Handle<Object> argv[] = { wrapper };
rossberg@chromium.org717967f2011-07-20 13:44:42 +00002824 Execution::TryCall(Handle<JSFunction>::cast(update_script_break_points),
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002825 Isolate::Current()->js_builtins_object(),
2826 ARRAY_SIZE(argv),
2827 argv,
2828 &caught_exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002829 if (caught_exception) {
2830 return;
2831 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002832 // Bail out based on state or if there is no listener for this event
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00002833 if (in_debugger && (after_compile_flags & SEND_WHEN_DEBUGGING) == 0) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002834 if (!Debugger::EventActive(v8::AfterCompile)) return;
2835
2836 // Create the compile state object.
2837 Handle<Object> event_data = MakeCompileEvent(script,
iposva@chromium.org245aa852009-02-10 00:49:54 +00002838 false,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002839 &caught_exception);
2840 // Bail out and don't call debugger if exception.
2841 if (caught_exception) {
2842 return;
2843 }
ager@chromium.org5ec48922009-05-05 07:25:34 +00002844 // Process debug event.
2845 ProcessDebugEvent(v8::AfterCompile,
2846 Handle<JSObject>::cast(event_data),
2847 true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002848}
2849
2850
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002851void Debugger::OnScriptCollected(int id) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002852 HandleScope scope(isolate_);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002853
2854 // No more to do if not debugging.
ulan@chromium.org56c14af2012-09-20 12:51:09 +00002855 if (isolate_->debug()->InDebugger()) return;
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002856 if (!IsDebuggerActive()) return;
2857 if (!Debugger::EventActive(v8::ScriptCollected)) return;
2858
2859 // Enter the debugger.
2860 EnterDebugger debugger;
2861 if (debugger.FailedToEnter()) return;
2862
2863 // Create the script collected state object.
2864 bool caught_exception = false;
2865 Handle<Object> event_data = MakeScriptCollectedEvent(id,
2866 &caught_exception);
2867 // Bail out and don't call debugger if exception.
2868 if (caught_exception) {
2869 return;
2870 }
2871
2872 // Process debug event.
2873 ProcessDebugEvent(v8::ScriptCollected,
2874 Handle<JSObject>::cast(event_data),
2875 true);
2876}
2877
2878
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002879void Debugger::ProcessDebugEvent(v8::DebugEvent event,
ager@chromium.org5ec48922009-05-05 07:25:34 +00002880 Handle<JSObject> event_data,
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002881 bool auto_continue) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002882 HandleScope scope(isolate_);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002883
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +00002884 // Clear any pending debug break if this is a real break.
2885 if (!auto_continue) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002886 isolate_->debug()->clear_interrupt_pending(DEBUGBREAK);
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +00002887 }
2888
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002889 // Create the execution state.
2890 bool caught_exception = false;
2891 Handle<Object> exec_state = MakeExecutionState(&caught_exception);
2892 if (caught_exception) {
2893 return;
2894 }
ager@chromium.org41826e72009-03-30 13:30:57 +00002895 // First notify the message handler if any.
2896 if (message_handler_ != NULL) {
ager@chromium.org5ec48922009-05-05 07:25:34 +00002897 NotifyMessageHandler(event,
2898 Handle<JSObject>::cast(exec_state),
2899 event_data,
2900 auto_continue);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002901 }
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00002902 // Notify registered debug event listener. This can be either a C or
2903 // a JavaScript function. Don't call event listener for v8::Break
2904 // here, if it's only a debug command -- they will be processed later.
2905 if ((event != v8::Break || !auto_continue) && !event_listener_.is_null()) {
2906 CallEventCallback(event, exec_state, event_data, NULL);
2907 }
2908 // Process pending debug commands.
2909 if (event == v8::Break) {
2910 while (!event_command_queue_.IsEmpty()) {
2911 CommandMessage command = event_command_queue_.Get();
2912 if (!event_listener_.is_null()) {
2913 CallEventCallback(v8::BreakForCommand,
2914 exec_state,
2915 event_data,
2916 command.client_data());
2917 }
2918 command.Dispose();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002919 }
2920 }
2921}
2922
2923
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00002924void Debugger::CallEventCallback(v8::DebugEvent event,
2925 Handle<Object> exec_state,
2926 Handle<Object> event_data,
2927 v8::Debug::ClientData* client_data) {
ager@chromium.orgea91cc52011-05-23 06:06:11 +00002928 if (event_listener_->IsForeign()) {
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00002929 CallCEventCallback(event, exec_state, event_data, client_data);
2930 } else {
2931 CallJSEventCallback(event, exec_state, event_data);
2932 }
2933}
2934
2935
2936void Debugger::CallCEventCallback(v8::DebugEvent event,
2937 Handle<Object> exec_state,
2938 Handle<Object> event_data,
2939 v8::Debug::ClientData* client_data) {
ager@chromium.orgea91cc52011-05-23 06:06:11 +00002940 Handle<Foreign> callback_obj(Handle<Foreign>::cast(event_listener_));
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00002941 v8::Debug::EventCallback2 callback =
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00002942 FUNCTION_CAST<v8::Debug::EventCallback2>(
2943 callback_obj->foreign_address());
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00002944 EventDetailsImpl event_details(
2945 event,
2946 Handle<JSObject>::cast(exec_state),
2947 Handle<JSObject>::cast(event_data),
2948 event_listener_data_,
2949 client_data);
2950 callback(event_details);
2951}
2952
2953
2954void Debugger::CallJSEventCallback(v8::DebugEvent event,
2955 Handle<Object> exec_state,
2956 Handle<Object> event_data) {
2957 ASSERT(event_listener_->IsJSFunction());
2958 Handle<JSFunction> fun(Handle<JSFunction>::cast(event_listener_));
2959
2960 // Invoke the JavaScript debug event listener.
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00002961 Handle<Object> argv[] = { Handle<Object>(Smi::FromInt(event), isolate_),
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002962 exec_state,
2963 event_data,
2964 event_listener_data_ };
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002965 bool caught_exception;
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002966 Execution::TryCall(fun,
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00002967 isolate_->global_object(),
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002968 ARRAY_SIZE(argv),
2969 argv,
2970 &caught_exception);
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00002971 // Silently ignore exceptions from debug event listeners.
2972}
2973
2974
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00002975Handle<Context> Debugger::GetDebugContext() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002976 never_unload_debugger_ = true;
2977 EnterDebugger debugger;
2978 return isolate_->debug()->debug_context();
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00002979}
2980
2981
ager@chromium.org71daaf62009-04-01 07:22:49 +00002982void Debugger::UnloadDebugger() {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002983 Debug* debug = isolate_->debug();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002984
ager@chromium.org71daaf62009-04-01 07:22:49 +00002985 // Make sure that there are no breakpoints left.
lrn@chromium.org7516f052011-03-30 08:52:27 +00002986 debug->ClearAllBreakPoints();
ager@chromium.org71daaf62009-04-01 07:22:49 +00002987
2988 // Unload the debugger if feasible.
2989 if (!never_unload_debugger_) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002990 debug->Unload();
ager@chromium.org71daaf62009-04-01 07:22:49 +00002991 }
2992
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002993 // Clear the flag indicating that the debugger should be unloaded.
2994 debugger_unload_pending_ = false;
ager@chromium.org71daaf62009-04-01 07:22:49 +00002995}
2996
2997
ager@chromium.org41826e72009-03-30 13:30:57 +00002998void Debugger::NotifyMessageHandler(v8::DebugEvent event,
ager@chromium.org5ec48922009-05-05 07:25:34 +00002999 Handle<JSObject> exec_state,
3000 Handle<JSObject> event_data,
ager@chromium.org41826e72009-03-30 13:30:57 +00003001 bool auto_continue) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00003002 HandleScope scope(isolate_);
ager@chromium.org41826e72009-03-30 13:30:57 +00003003
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003004 if (!isolate_->debug()->Load()) return;
ager@chromium.org41826e72009-03-30 13:30:57 +00003005
3006 // Process the individual events.
ager@chromium.org5ec48922009-05-05 07:25:34 +00003007 bool sendEventMessage = false;
ager@chromium.org41826e72009-03-30 13:30:57 +00003008 switch (event) {
3009 case v8::Break:
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00003010 case v8::BreakForCommand:
ager@chromium.org5ec48922009-05-05 07:25:34 +00003011 sendEventMessage = !auto_continue;
ager@chromium.org41826e72009-03-30 13:30:57 +00003012 break;
3013 case v8::Exception:
ager@chromium.org5ec48922009-05-05 07:25:34 +00003014 sendEventMessage = true;
ager@chromium.org41826e72009-03-30 13:30:57 +00003015 break;
3016 case v8::BeforeCompile:
3017 break;
3018 case v8::AfterCompile:
ager@chromium.org5ec48922009-05-05 07:25:34 +00003019 sendEventMessage = true;
ager@chromium.org41826e72009-03-30 13:30:57 +00003020 break;
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003021 case v8::ScriptCollected:
3022 sendEventMessage = true;
3023 break;
ager@chromium.org41826e72009-03-30 13:30:57 +00003024 case v8::NewFunction:
3025 break;
3026 default:
3027 UNREACHABLE();
3028 }
3029
ager@chromium.org5ec48922009-05-05 07:25:34 +00003030 // The debug command interrupt flag might have been set when the command was
3031 // added. It should be enough to clear the flag only once while we are in the
3032 // debugger.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003033 ASSERT(isolate_->debug()->InDebugger());
3034 isolate_->stack_guard()->Continue(DEBUGCOMMAND);
ager@chromium.org5ec48922009-05-05 07:25:34 +00003035
3036 // Notify the debugger that a debug event has occurred unless auto continue is
3037 // active in which case no event is send.
3038 if (sendEventMessage) {
3039 MessageImpl message = MessageImpl::NewEvent(
3040 event,
3041 auto_continue,
3042 Handle<JSObject>::cast(exec_state),
3043 Handle<JSObject>::cast(event_data));
3044 InvokeMessageHandler(message);
3045 }
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +00003046
3047 // If auto continue don't make the event cause a break, but process messages
3048 // in the queue if any. For script collected events don't even process
3049 // messages in the queue as the execution state might not be what is expected
3050 // by the client.
ager@chromium.org6ffc2172009-05-29 19:20:16 +00003051 if ((auto_continue && !HasCommands()) || event == v8::ScriptCollected) {
ager@chromium.org5ec48922009-05-05 07:25:34 +00003052 return;
3053 }
ager@chromium.org41826e72009-03-30 13:30:57 +00003054
ager@chromium.org41826e72009-03-30 13:30:57 +00003055 v8::TryCatch try_catch;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003056
3057 // DebugCommandProcessor goes here.
3058 v8::Local<v8::Object> cmd_processor;
3059 {
3060 v8::Local<v8::Object> api_exec_state =
3061 v8::Utils::ToLocal(Handle<JSObject>::cast(exec_state));
3062 v8::Local<v8::String> fun_name =
3063 v8::String::New("debugCommandProcessor");
3064 v8::Local<v8::Function> fun =
3065 v8::Function::Cast(*api_exec_state->Get(fun_name));
3066
3067 v8::Handle<v8::Boolean> running =
3068 auto_continue ? v8::True() : v8::False();
3069 static const int kArgc = 1;
3070 v8::Handle<Value> argv[kArgc] = { running };
3071 cmd_processor = v8::Object::Cast(*fun->Call(api_exec_state, kArgc, argv));
3072 if (try_catch.HasCaught()) {
3073 PrintLn(try_catch.Exception());
3074 return;
3075 }
ager@chromium.org41826e72009-03-30 13:30:57 +00003076 }
3077
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003078 bool running = auto_continue;
3079
ager@chromium.org41826e72009-03-30 13:30:57 +00003080 // Process requests from the debugger.
3081 while (true) {
3082 // Wait for new command in the queue.
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003083 if (Debugger::host_dispatch_handler_) {
3084 // In case there is a host dispatch - do periodic dispatches.
3085 if (!command_received_->Wait(host_dispatch_micros_)) {
3086 // Timout expired, do the dispatch.
3087 Debugger::host_dispatch_handler_();
3088 continue;
3089 }
3090 } else {
3091 // In case there is no host dispatch - just wait.
3092 command_received_->Wait();
3093 }
ager@chromium.org41826e72009-03-30 13:30:57 +00003094
ager@chromium.org41826e72009-03-30 13:30:57 +00003095 // Get the command from the queue.
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003096 CommandMessage command = command_queue_.Get();
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00003097 isolate_->logger()->DebugTag(
3098 "Got request from command queue, in interactive loop.");
ager@chromium.org71daaf62009-04-01 07:22:49 +00003099 if (!Debugger::IsDebuggerActive()) {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003100 // Delete command text and user data.
3101 command.Dispose();
ager@chromium.org41826e72009-03-30 13:30:57 +00003102 return;
3103 }
3104
ager@chromium.org41826e72009-03-30 13:30:57 +00003105 // Invoke JavaScript to process the debug request.
3106 v8::Local<v8::String> fun_name;
3107 v8::Local<v8::Function> fun;
3108 v8::Local<v8::Value> request;
3109 v8::TryCatch try_catch;
3110 fun_name = v8::String::New("processDebugRequest");
3111 fun = v8::Function::Cast(*cmd_processor->Get(fun_name));
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003112
3113 request = v8::String::New(command.text().start(),
3114 command.text().length());
ager@chromium.org41826e72009-03-30 13:30:57 +00003115 static const int kArgc = 1;
3116 v8::Handle<Value> argv[kArgc] = { request };
3117 v8::Local<v8::Value> response_val = fun->Call(cmd_processor, kArgc, argv);
3118
3119 // Get the response.
3120 v8::Local<v8::String> response;
ager@chromium.org41826e72009-03-30 13:30:57 +00003121 if (!try_catch.HasCaught()) {
3122 // Get response string.
3123 if (!response_val->IsUndefined()) {
3124 response = v8::String::Cast(*response_val);
3125 } else {
3126 response = v8::String::New("");
3127 }
3128
3129 // Log the JSON request/response.
3130 if (FLAG_trace_debug_json) {
3131 PrintLn(request);
3132 PrintLn(response);
3133 }
3134
3135 // Get the running state.
3136 fun_name = v8::String::New("isRunning");
3137 fun = v8::Function::Cast(*cmd_processor->Get(fun_name));
3138 static const int kArgc = 1;
3139 v8::Handle<Value> argv[kArgc] = { response };
3140 v8::Local<v8::Value> running_val = fun->Call(cmd_processor, kArgc, argv);
3141 if (!try_catch.HasCaught()) {
3142 running = running_val->ToBoolean()->Value();
3143 }
3144 } else {
3145 // In case of failure the result text is the exception text.
3146 response = try_catch.Exception()->ToString();
3147 }
3148
ager@chromium.org41826e72009-03-30 13:30:57 +00003149 // Return the result.
ager@chromium.org5ec48922009-05-05 07:25:34 +00003150 MessageImpl message = MessageImpl::NewResponse(
3151 event,
3152 running,
3153 Handle<JSObject>::cast(exec_state),
3154 Handle<JSObject>::cast(event_data),
3155 Handle<String>(Utils::OpenHandle(*response)),
3156 command.client_data());
3157 InvokeMessageHandler(message);
3158 command.Dispose();
ager@chromium.org41826e72009-03-30 13:30:57 +00003159
3160 // Return from debug event processing if either the VM is put into the
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00003161 // running state (through a continue command) or auto continue is active
ager@chromium.org41826e72009-03-30 13:30:57 +00003162 // and there are no more commands queued.
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003163 if (running && !HasCommands()) {
ager@chromium.org41826e72009-03-30 13:30:57 +00003164 return;
3165 }
3166 }
3167}
3168
3169
iposva@chromium.org245aa852009-02-10 00:49:54 +00003170void Debugger::SetEventListener(Handle<Object> callback,
3171 Handle<Object> data) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00003172 HandleScope scope(isolate_);
3173 GlobalHandles* global_handles = isolate_->global_handles();
iposva@chromium.org245aa852009-02-10 00:49:54 +00003174
3175 // Clear the global handles for the event listener and the event listener data
3176 // object.
3177 if (!event_listener_.is_null()) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00003178 global_handles->Destroy(
iposva@chromium.org245aa852009-02-10 00:49:54 +00003179 reinterpret_cast<Object**>(event_listener_.location()));
3180 event_listener_ = Handle<Object>();
3181 }
3182 if (!event_listener_data_.is_null()) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00003183 global_handles->Destroy(
iposva@chromium.org245aa852009-02-10 00:49:54 +00003184 reinterpret_cast<Object**>(event_listener_data_.location()));
3185 event_listener_data_ = Handle<Object>();
3186 }
3187
3188 // If there is a new debug event listener register it together with its data
3189 // object.
3190 if (!callback->IsUndefined() && !callback->IsNull()) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003191 event_listener_ = Handle<Object>::cast(
lrn@chromium.org7516f052011-03-30 08:52:27 +00003192 global_handles->Create(*callback));
iposva@chromium.org245aa852009-02-10 00:49:54 +00003193 if (data.is_null()) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00003194 data = isolate_->factory()->undefined_value();
iposva@chromium.org245aa852009-02-10 00:49:54 +00003195 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003196 event_listener_data_ = Handle<Object>::cast(
lrn@chromium.org7516f052011-03-30 08:52:27 +00003197 global_handles->Create(*data));
iposva@chromium.org245aa852009-02-10 00:49:54 +00003198 }
3199
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003200 ListenersChanged();
iposva@chromium.org245aa852009-02-10 00:49:54 +00003201}
3202
3203
ager@chromium.org5ec48922009-05-05 07:25:34 +00003204void Debugger::SetMessageHandler(v8::Debug::MessageHandler2 handler) {
ager@chromium.org71daaf62009-04-01 07:22:49 +00003205 ScopedLock with(debugger_access_);
3206
ager@chromium.org381abbb2009-02-25 13:23:22 +00003207 message_handler_ = handler;
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003208 ListenersChanged();
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003209 if (handler == NULL) {
ager@chromium.org71daaf62009-04-01 07:22:49 +00003210 // Send an empty command to the debugger if in a break to make JavaScript
3211 // run again if the debugger is closed.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003212 if (isolate_->debug()->InDebugger()) {
ager@chromium.org71daaf62009-04-01 07:22:49 +00003213 ProcessCommand(Vector<const uint16_t>::empty());
3214 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003215 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003216}
3217
3218
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003219void Debugger::ListenersChanged() {
3220 if (IsDebuggerActive()) {
3221 // Disable the compilation cache when the debugger is active.
lrn@chromium.org7516f052011-03-30 08:52:27 +00003222 isolate_->compilation_cache()->Disable();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003223 debugger_unload_pending_ = false;
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003224 } else {
lrn@chromium.org7516f052011-03-30 08:52:27 +00003225 isolate_->compilation_cache()->Enable();
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003226 // Unload the debugger if event listener and message handler cleared.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003227 // Schedule this for later, because we may be in non-V8 thread.
3228 debugger_unload_pending_ = true;
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003229 }
3230}
3231
3232
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003233void Debugger::SetHostDispatchHandler(v8::Debug::HostDispatchHandler handler,
3234 int period) {
ager@chromium.org381abbb2009-02-25 13:23:22 +00003235 host_dispatch_handler_ = handler;
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003236 host_dispatch_micros_ = period * 1000;
ager@chromium.org381abbb2009-02-25 13:23:22 +00003237}
3238
3239
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003240void Debugger::SetDebugMessageDispatchHandler(
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003241 v8::Debug::DebugMessageDispatchHandler handler, bool provide_locker) {
3242 ScopedLock with(dispatch_handler_access_);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003243 debug_message_dispatch_handler_ = handler;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003244
3245 if (provide_locker && message_dispatch_helper_thread_ == NULL) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003246 message_dispatch_helper_thread_ = new MessageDispatchHelperThread(isolate_);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003247 message_dispatch_helper_thread_->Start();
3248 }
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003249}
3250
3251
ager@chromium.org41826e72009-03-30 13:30:57 +00003252// Calls the registered debug message handler. This callback is part of the
ager@chromium.org5ec48922009-05-05 07:25:34 +00003253// public API.
3254void Debugger::InvokeMessageHandler(MessageImpl message) {
ager@chromium.org71daaf62009-04-01 07:22:49 +00003255 ScopedLock with(debugger_access_);
3256
ager@chromium.org381abbb2009-02-25 13:23:22 +00003257 if (message_handler_ != NULL) {
ager@chromium.org5ec48922009-05-05 07:25:34 +00003258 message_handler_(message);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003259 }
ager@chromium.org41826e72009-03-30 13:30:57 +00003260}
3261
3262
3263// Puts a command coming from the public API on the queue. Creates
3264// a copy of the command string managed by the debugger. Up to this
3265// point, the command data was managed by the API client. Called
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003266// by the API client thread.
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003267void Debugger::ProcessCommand(Vector<const uint16_t> command,
3268 v8::Debug::ClientData* client_data) {
3269 // Need to cast away const.
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003270 CommandMessage message = CommandMessage::New(
ager@chromium.org41826e72009-03-30 13:30:57 +00003271 Vector<uint16_t>(const_cast<uint16_t*>(command.start()),
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003272 command.length()),
3273 client_data);
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00003274 isolate_->logger()->DebugTag("Put command on command_queue.");
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003275 command_queue_.Put(message);
ager@chromium.org41826e72009-03-30 13:30:57 +00003276 command_received_->Signal();
sgjesse@chromium.org3afc1582009-04-16 22:31:44 +00003277
3278 // Set the debug command break flag to have the command processed.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003279 if (!isolate_->debug()->InDebugger()) {
3280 isolate_->stack_guard()->DebugCommand();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003281 }
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003282
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003283 MessageDispatchHelperThread* dispatch_thread;
3284 {
3285 ScopedLock with(dispatch_handler_access_);
3286 dispatch_thread = message_dispatch_helper_thread_;
3287 }
3288
3289 if (dispatch_thread == NULL) {
3290 CallMessageDispatchHandler();
3291 } else {
3292 dispatch_thread->Schedule();
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003293 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003294}
3295
3296
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003297bool Debugger::HasCommands() {
ager@chromium.org41826e72009-03-30 13:30:57 +00003298 return !command_queue_.IsEmpty();
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003299}
3300
3301
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00003302void Debugger::EnqueueDebugCommand(v8::Debug::ClientData* client_data) {
3303 CommandMessage message = CommandMessage::New(Vector<uint16_t>(), client_data);
3304 event_command_queue_.Put(message);
3305
3306 // Set the debug command break flag to have the command processed.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003307 if (!isolate_->debug()->InDebugger()) {
3308 isolate_->stack_guard()->DebugCommand();
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00003309 }
3310}
3311
3312
ager@chromium.org71daaf62009-04-01 07:22:49 +00003313bool Debugger::IsDebuggerActive() {
3314 ScopedLock with(debugger_access_);
3315
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00003316 return message_handler_ != NULL ||
3317 !event_listener_.is_null() ||
3318 force_debugger_active_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003319}
3320
3321
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003322Handle<Object> Debugger::Call(Handle<JSFunction> fun,
3323 Handle<Object> data,
3324 bool* pending_exception) {
ager@chromium.org71daaf62009-04-01 07:22:49 +00003325 // When calling functions in the debugger prevent it from beeing unloaded.
3326 Debugger::never_unload_debugger_ = true;
3327
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003328 // Enter the debugger.
3329 EnterDebugger debugger;
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +00003330 if (debugger.FailedToEnter()) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00003331 return isolate_->factory()->undefined_value();
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003332 }
3333
3334 // Create the execution state.
3335 bool caught_exception = false;
3336 Handle<Object> exec_state = MakeExecutionState(&caught_exception);
3337 if (caught_exception) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00003338 return isolate_->factory()->undefined_value();
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003339 }
3340
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00003341 Handle<Object> argv[] = { exec_state, data };
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +00003342 Handle<Object> result = Execution::Call(
3343 fun,
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00003344 Handle<Object>(isolate_->debug()->debug_context_->global_proxy(),
3345 isolate_),
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00003346 ARRAY_SIZE(argv),
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +00003347 argv,
3348 pending_exception);
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003349 return result;
3350}
3351
3352
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003353static void StubMessageHandler2(const v8::Debug::Message& message) {
3354 // Simply ignore message.
3355}
3356
3357
3358bool Debugger::StartAgent(const char* name, int port,
3359 bool wait_for_connection) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003360 ASSERT(Isolate::Current() == isolate_);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003361 if (wait_for_connection) {
3362 // Suspend V8 if it is already running or set V8 to suspend whenever
3363 // it starts.
3364 // Provide stub message handler; V8 auto-continues each suspend
3365 // when there is no message handler; we doesn't need it.
3366 // Once become suspended, V8 will stay so indefinitely long, until remote
3367 // debugger connects and issues "continue" command.
3368 Debugger::message_handler_ = StubMessageHandler2;
3369 v8::Debug::DebugBreak();
3370 }
3371
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00003372 if (Socket::SetUp()) {
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00003373 if (agent_ == NULL) {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00003374 agent_ = new DebuggerAgent(name, port);
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00003375 agent_->Start();
3376 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003377 return true;
3378 }
3379
3380 return false;
3381}
3382
3383
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003384void Debugger::StopAgent() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003385 ASSERT(Isolate::Current() == isolate_);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003386 if (agent_ != NULL) {
3387 agent_->Shutdown();
3388 agent_->Join();
3389 delete agent_;
3390 agent_ = NULL;
3391 }
3392}
3393
3394
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003395void Debugger::WaitForAgent() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003396 ASSERT(Isolate::Current() == isolate_);
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003397 if (agent_ != NULL)
3398 agent_->WaitUntilListening();
3399}
3400
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003401
3402void Debugger::CallMessageDispatchHandler() {
3403 v8::Debug::DebugMessageDispatchHandler handler;
3404 {
3405 ScopedLock with(dispatch_handler_access_);
3406 handler = Debugger::debug_message_dispatch_handler_;
3407 }
3408 if (handler != NULL) {
3409 handler();
3410 }
3411}
3412
3413
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003414EnterDebugger::EnterDebugger()
3415 : isolate_(Isolate::Current()),
3416 prev_(isolate_->debug()->debugger_entry()),
3417 it_(isolate_),
3418 has_js_frames_(!it_.done()),
3419 save_(isolate_) {
3420 Debug* debug = isolate_->debug();
3421 ASSERT(prev_ != NULL || !debug->is_interrupt_pending(PREEMPT));
3422 ASSERT(prev_ != NULL || !debug->is_interrupt_pending(DEBUGBREAK));
3423
3424 // Link recursive debugger entry.
3425 debug->set_debugger_entry(this);
3426
3427 // Store the previous break id and frame id.
3428 break_id_ = debug->break_id();
3429 break_frame_id_ = debug->break_frame_id();
3430
3431 // Create the new break info. If there is no JavaScript frames there is no
3432 // break frame id.
3433 if (has_js_frames_) {
3434 debug->NewBreak(it_.frame()->id());
3435 } else {
3436 debug->NewBreak(StackFrame::NO_ID);
3437 }
3438
3439 // Make sure that debugger is loaded and enter the debugger context.
3440 load_failed_ = !debug->Load();
3441 if (!load_failed_) {
3442 // NOTE the member variable save which saves the previous context before
3443 // this change.
3444 isolate_->set_context(*debug->debug_context());
3445 }
3446}
3447
3448
3449EnterDebugger::~EnterDebugger() {
3450 ASSERT(Isolate::Current() == isolate_);
3451 Debug* debug = isolate_->debug();
3452
3453 // Restore to the previous break state.
3454 debug->SetBreak(break_frame_id_, break_id_);
3455
3456 // Check for leaving the debugger.
fschneider@chromium.org7d10be52012-04-10 12:30:14 +00003457 if (!load_failed_ && prev_ == NULL) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003458 // Clear mirror cache when leaving the debugger. Skip this if there is a
3459 // pending exception as clearing the mirror cache calls back into
3460 // JavaScript. This can happen if the v8::Debug::Call is used in which
3461 // case the exception should end up in the calling code.
3462 if (!isolate_->has_pending_exception()) {
3463 // Try to avoid any pending debug break breaking in the clear mirror
3464 // cache JavaScript code.
3465 if (isolate_->stack_guard()->IsDebugBreak()) {
3466 debug->set_interrupts_pending(DEBUGBREAK);
3467 isolate_->stack_guard()->Continue(DEBUGBREAK);
3468 }
3469 debug->ClearMirrorCache();
3470 }
3471
3472 // Request preemption and debug break when leaving the last debugger entry
3473 // if any of these where recorded while debugging.
3474 if (debug->is_interrupt_pending(PREEMPT)) {
3475 // This re-scheduling of preemption is to avoid starvation in some
3476 // debugging scenarios.
3477 debug->clear_interrupt_pending(PREEMPT);
3478 isolate_->stack_guard()->Preempt();
3479 }
3480 if (debug->is_interrupt_pending(DEBUGBREAK)) {
3481 debug->clear_interrupt_pending(DEBUGBREAK);
3482 isolate_->stack_guard()->DebugBreak();
3483 }
3484
3485 // If there are commands in the queue when leaving the debugger request
3486 // that these commands are processed.
3487 if (isolate_->debugger()->HasCommands()) {
3488 isolate_->stack_guard()->DebugCommand();
3489 }
3490
3491 // If leaving the debugger with the debugger no longer active unload it.
3492 if (!isolate_->debugger()->IsDebuggerActive()) {
3493 isolate_->debugger()->UnloadDebugger();
3494 }
3495 }
3496
3497 // Leaving this debugger entry.
3498 debug->set_debugger_entry(prev_);
3499}
3500
3501
ager@chromium.org5ec48922009-05-05 07:25:34 +00003502MessageImpl MessageImpl::NewEvent(DebugEvent event,
3503 bool running,
3504 Handle<JSObject> exec_state,
3505 Handle<JSObject> event_data) {
3506 MessageImpl message(true, event, running,
3507 exec_state, event_data, Handle<String>(), NULL);
3508 return message;
3509}
3510
3511
3512MessageImpl MessageImpl::NewResponse(DebugEvent event,
3513 bool running,
3514 Handle<JSObject> exec_state,
3515 Handle<JSObject> event_data,
3516 Handle<String> response_json,
3517 v8::Debug::ClientData* client_data) {
3518 MessageImpl message(false, event, running,
3519 exec_state, event_data, response_json, client_data);
3520 return message;
3521}
3522
3523
3524MessageImpl::MessageImpl(bool is_event,
3525 DebugEvent event,
3526 bool running,
3527 Handle<JSObject> exec_state,
3528 Handle<JSObject> event_data,
3529 Handle<String> response_json,
3530 v8::Debug::ClientData* client_data)
3531 : is_event_(is_event),
3532 event_(event),
3533 running_(running),
3534 exec_state_(exec_state),
3535 event_data_(event_data),
3536 response_json_(response_json),
3537 client_data_(client_data) {}
3538
3539
3540bool MessageImpl::IsEvent() const {
3541 return is_event_;
3542}
3543
3544
3545bool MessageImpl::IsResponse() const {
3546 return !is_event_;
3547}
3548
3549
3550DebugEvent MessageImpl::GetEvent() const {
3551 return event_;
3552}
3553
3554
3555bool MessageImpl::WillStartRunning() const {
3556 return running_;
3557}
3558
3559
3560v8::Handle<v8::Object> MessageImpl::GetExecutionState() const {
3561 return v8::Utils::ToLocal(exec_state_);
3562}
3563
3564
3565v8::Handle<v8::Object> MessageImpl::GetEventData() const {
3566 return v8::Utils::ToLocal(event_data_);
3567}
3568
3569
3570v8::Handle<v8::String> MessageImpl::GetJSON() const {
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +00003571 v8::HandleScope scope(
3572 reinterpret_cast<v8::Isolate*>(event_data_->GetIsolate()));
ager@chromium.org5ec48922009-05-05 07:25:34 +00003573
3574 if (IsEvent()) {
3575 // Call toJSONProtocol on the debug event object.
3576 Handle<Object> fun = GetProperty(event_data_, "toJSONProtocol");
3577 if (!fun->IsJSFunction()) {
3578 return v8::Handle<v8::String>();
3579 }
3580 bool caught_exception;
3581 Handle<Object> json = Execution::TryCall(Handle<JSFunction>::cast(fun),
3582 event_data_,
3583 0, NULL, &caught_exception);
3584 if (caught_exception || !json->IsString()) {
3585 return v8::Handle<v8::String>();
3586 }
3587 return scope.Close(v8::Utils::ToLocal(Handle<String>::cast(json)));
3588 } else {
3589 return v8::Utils::ToLocal(response_json_);
3590 }
3591}
3592
3593
3594v8::Handle<v8::Context> MessageImpl::GetEventContext() const {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003595 Isolate* isolate = Isolate::Current();
3596 v8::Handle<v8::Context> context = GetDebugEventContext(isolate);
3597 // Isolate::context() may be NULL when "script collected" event occures.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003598 ASSERT(!context.IsEmpty() || event_ == v8::ScriptCollected);
rossberg@chromium.org717967f2011-07-20 13:44:42 +00003599 return context;
ager@chromium.org5ec48922009-05-05 07:25:34 +00003600}
3601
3602
3603v8::Debug::ClientData* MessageImpl::GetClientData() const {
3604 return client_data_;
3605}
3606
3607
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003608EventDetailsImpl::EventDetailsImpl(DebugEvent event,
3609 Handle<JSObject> exec_state,
3610 Handle<JSObject> event_data,
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00003611 Handle<Object> callback_data,
3612 v8::Debug::ClientData* client_data)
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003613 : event_(event),
3614 exec_state_(exec_state),
3615 event_data_(event_data),
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00003616 callback_data_(callback_data),
3617 client_data_(client_data) {}
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003618
3619
3620DebugEvent EventDetailsImpl::GetEvent() const {
3621 return event_;
3622}
3623
3624
3625v8::Handle<v8::Object> EventDetailsImpl::GetExecutionState() const {
3626 return v8::Utils::ToLocal(exec_state_);
3627}
3628
3629
3630v8::Handle<v8::Object> EventDetailsImpl::GetEventData() const {
3631 return v8::Utils::ToLocal(event_data_);
3632}
3633
3634
3635v8::Handle<v8::Context> EventDetailsImpl::GetEventContext() const {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003636 return GetDebugEventContext(Isolate::Current());
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003637}
3638
3639
3640v8::Handle<v8::Value> EventDetailsImpl::GetCallbackData() const {
3641 return v8::Utils::ToLocal(callback_data_);
3642}
3643
3644
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00003645v8::Debug::ClientData* EventDetailsImpl::GetClientData() const {
3646 return client_data_;
3647}
3648
3649
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003650CommandMessage::CommandMessage() : text_(Vector<uint16_t>::empty()),
3651 client_data_(NULL) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003652}
3653
3654
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003655CommandMessage::CommandMessage(const Vector<uint16_t>& text,
3656 v8::Debug::ClientData* data)
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003657 : text_(text),
3658 client_data_(data) {
3659}
3660
3661
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003662CommandMessage::~CommandMessage() {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003663}
3664
3665
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003666void CommandMessage::Dispose() {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003667 text_.Dispose();
3668 delete client_data_;
3669 client_data_ = NULL;
3670}
3671
3672
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003673CommandMessage CommandMessage::New(const Vector<uint16_t>& command,
3674 v8::Debug::ClientData* data) {
3675 return CommandMessage(command.Clone(), data);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003676}
3677
3678
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003679CommandMessageQueue::CommandMessageQueue(int size) : start_(0), end_(0),
3680 size_(size) {
3681 messages_ = NewArray<CommandMessage>(size);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003682}
3683
3684
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003685CommandMessageQueue::~CommandMessageQueue() {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003686 while (!IsEmpty()) {
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003687 CommandMessage m = Get();
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003688 m.Dispose();
3689 }
kasper.lund7276f142008-07-30 08:49:36 +00003690 DeleteArray(messages_);
3691}
3692
3693
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003694CommandMessage CommandMessageQueue::Get() {
kasper.lund7276f142008-07-30 08:49:36 +00003695 ASSERT(!IsEmpty());
3696 int result = start_;
3697 start_ = (start_ + 1) % size_;
3698 return messages_[result];
3699}
3700
3701
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003702void CommandMessageQueue::Put(const CommandMessage& message) {
kasper.lund7276f142008-07-30 08:49:36 +00003703 if ((end_ + 1) % size_ == start_) {
3704 Expand();
3705 }
3706 messages_[end_] = message;
3707 end_ = (end_ + 1) % size_;
3708}
3709
3710
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003711void CommandMessageQueue::Expand() {
3712 CommandMessageQueue new_queue(size_ * 2);
kasper.lund7276f142008-07-30 08:49:36 +00003713 while (!IsEmpty()) {
3714 new_queue.Put(Get());
3715 }
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003716 CommandMessage* array_to_free = messages_;
kasper.lund7276f142008-07-30 08:49:36 +00003717 *this = new_queue;
3718 new_queue.messages_ = array_to_free;
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003719 // Make the new_queue empty so that it doesn't call Dispose on any messages.
3720 new_queue.start_ = new_queue.end_;
kasper.lund7276f142008-07-30 08:49:36 +00003721 // Automatic destructor called on new_queue, freeing array_to_free.
3722}
3723
3724
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00003725LockingCommandMessageQueue::LockingCommandMessageQueue(Logger* logger, int size)
3726 : logger_(logger), queue_(size) {
kasper.lund7276f142008-07-30 08:49:36 +00003727 lock_ = OS::CreateMutex();
3728}
3729
3730
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003731LockingCommandMessageQueue::~LockingCommandMessageQueue() {
kasper.lund7276f142008-07-30 08:49:36 +00003732 delete lock_;
3733}
3734
3735
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003736bool LockingCommandMessageQueue::IsEmpty() const {
kasper.lund7276f142008-07-30 08:49:36 +00003737 ScopedLock sl(lock_);
3738 return queue_.IsEmpty();
3739}
3740
3741
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003742CommandMessage LockingCommandMessageQueue::Get() {
kasper.lund7276f142008-07-30 08:49:36 +00003743 ScopedLock sl(lock_);
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003744 CommandMessage result = queue_.Get();
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00003745 logger_->DebugEvent("Get", result.text());
kasper.lund7276f142008-07-30 08:49:36 +00003746 return result;
3747}
3748
3749
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003750void LockingCommandMessageQueue::Put(const CommandMessage& message) {
kasper.lund7276f142008-07-30 08:49:36 +00003751 ScopedLock sl(lock_);
3752 queue_.Put(message);
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00003753 logger_->DebugEvent("Put", message.text());
kasper.lund7276f142008-07-30 08:49:36 +00003754}
3755
3756
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003757void LockingCommandMessageQueue::Clear() {
kasper.lund7276f142008-07-30 08:49:36 +00003758 ScopedLock sl(lock_);
3759 queue_.Clear();
3760}
3761
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003762
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003763MessageDispatchHelperThread::MessageDispatchHelperThread(Isolate* isolate)
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00003764 : Thread("v8:MsgDispHelpr"),
mstarzinger@chromium.orgf705b502013-04-04 11:38:09 +00003765 isolate_(isolate), sem_(OS::CreateSemaphore(0)),
3766 mutex_(OS::CreateMutex()), already_signalled_(false) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003767}
3768
3769
3770MessageDispatchHelperThread::~MessageDispatchHelperThread() {
3771 delete mutex_;
3772 delete sem_;
3773}
3774
3775
3776void MessageDispatchHelperThread::Schedule() {
3777 {
3778 ScopedLock lock(mutex_);
3779 if (already_signalled_) {
3780 return;
3781 }
3782 already_signalled_ = true;
3783 }
3784 sem_->Signal();
3785}
3786
3787
3788void MessageDispatchHelperThread::Run() {
3789 while (true) {
3790 sem_->Wait();
3791 {
3792 ScopedLock lock(mutex_);
3793 already_signalled_ = false;
3794 }
3795 {
mstarzinger@chromium.orgf705b502013-04-04 11:38:09 +00003796 Locker locker(reinterpret_cast<v8::Isolate*>(isolate_));
3797 isolate_->debugger()->CallMessageDispatchHandler();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003798 }
3799 }
3800}
3801
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003802#endif // ENABLE_DEBUGGER_SUPPORT
kasper.lund7276f142008-07-30 08:49:36 +00003803
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003804} } // namespace v8::internal