blob: 093f38ee50ad804b880fb457acbf866ad5e52d26 [file] [log] [blame]
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001// Copyright 2006-2008 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"
40#include "global-handles.h"
ager@chromium.org65dad4b2009-04-23 08:48:43 +000041#include "ic.h"
42#include "ic-inl.h"
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000043#include "messages.h"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000044#include "natives.h"
45#include "stub-cache.h"
kasper.lund7276f142008-07-30 08:49:36 +000046#include "log.h"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000047
ager@chromium.org5ec48922009-05-05 07:25:34 +000048#include "../include/v8-debug.h"
49
kasperl@chromium.org71affb52009-05-26 05:44:31 +000050namespace v8 {
51namespace internal {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000052
ager@chromium.org65dad4b2009-04-23 08:48:43 +000053#ifdef ENABLE_DEBUGGER_SUPPORT
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000054
55
56Debug::Debug(Isolate* isolate)
57 : has_break_points_(false),
58 script_cache_(NULL),
59 debug_info_list_(NULL),
60 disable_break_(false),
61 break_on_exception_(false),
62 break_on_uncaught_exception_(false),
63 debug_break_return_(NULL),
64 debug_break_slot_(NULL),
65 isolate_(isolate) {
66 memset(registers_, 0, sizeof(JSCallerSavedBuffer));
67}
68
69
70Debug::~Debug() {
71}
72
73
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000074static void PrintLn(v8::Local<v8::Value> value) {
75 v8::Local<v8::String> s = value->ToString();
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +000076 ScopedVector<char> data(s->Length() + 1);
77 if (data.start() == NULL) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000078 V8::FatalProcessOutOfMemory("PrintLn");
79 return;
80 }
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +000081 s->WriteAscii(data.start());
82 PrintF("%s\n", data.start());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000083}
84
85
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +000086static Handle<Code> ComputeCallDebugBreak(int argc, Code::Kind kind) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000087 Isolate* isolate = Isolate::Current();
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +000088 CALL_HEAP_FUNCTION(
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000089 isolate,
90 isolate->stub_cache()->ComputeCallDebugBreak(argc, kind),
91 Code);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000092}
93
94
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000095static Handle<Code> ComputeCallDebugPrepareStepIn(int argc, Code::Kind kind) {
96 Isolate* isolate = Isolate::Current();
97 CALL_HEAP_FUNCTION(
98 isolate,
99 isolate->stub_cache()->ComputeCallDebugPrepareStepIn(argc, kind),
100 Code);
101}
102
103
104static v8::Handle<v8::Context> GetDebugEventContext(Isolate* isolate) {
105 Handle<Context> context = isolate->debug()->debugger_entry()->GetContext();
106 // Isolate::context() may have been NULL when "script collected" event
107 // occured.
108 if (context.is_null()) return v8::Local<v8::Context>();
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000109 Handle<Context> global_context(context->global_context());
110 return v8::Utils::ToLocal(global_context);
111}
112
113
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000114BreakLocationIterator::BreakLocationIterator(Handle<DebugInfo> debug_info,
115 BreakLocatorType type) {
116 debug_info_ = debug_info;
117 type_ = type;
118 reloc_iterator_ = NULL;
119 reloc_iterator_original_ = NULL;
120 Reset(); // Initialize the rest of the member variables.
121}
122
123
124BreakLocationIterator::~BreakLocationIterator() {
125 ASSERT(reloc_iterator_ != NULL);
126 ASSERT(reloc_iterator_original_ != NULL);
127 delete reloc_iterator_;
128 delete reloc_iterator_original_;
129}
130
131
132void BreakLocationIterator::Next() {
133 AssertNoAllocation nogc;
134 ASSERT(!RinfoDone());
135
136 // Iterate through reloc info for code and original code stopping at each
137 // breakable code target.
138 bool first = break_point_ == -1;
139 while (!RinfoDone()) {
140 if (!first) RinfoNext();
141 first = false;
142 if (RinfoDone()) return;
143
ager@chromium.org236ad962008-09-25 09:45:57 +0000144 // Whenever a statement position or (plain) position is passed update the
145 // current value of these.
146 if (RelocInfo::IsPosition(rmode())) {
147 if (RelocInfo::IsStatementPosition(rmode())) {
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000148 statement_position_ = static_cast<int>(
149 rinfo()->data() - debug_info_->shared()->start_position());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000150 }
ager@chromium.org236ad962008-09-25 09:45:57 +0000151 // Always update the position as we don't want that to be before the
152 // statement position.
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000153 position_ = static_cast<int>(
154 rinfo()->data() - debug_info_->shared()->start_position());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000155 ASSERT(position_ >= 0);
156 ASSERT(statement_position_ >= 0);
157 }
158
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000159 if (IsDebugBreakSlot()) {
160 // There is always a possible break point at a debug break slot.
161 break_point_++;
162 return;
163 } else if (RelocInfo::IsCodeTarget(rmode())) {
164 // Check for breakable code target. Look in the original code as setting
165 // break points can cause the code targets in the running (debugged) code
166 // to be of a different kind than in the original code.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000167 Address target = original_rinfo()->target_address();
ager@chromium.org8bb60582008-12-11 12:02:20 +0000168 Code* code = Code::GetCodeFromTargetAddress(target);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000169 if ((code->is_inline_cache_stub() &&
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000170 !code->is_type_recording_binary_op_stub() &&
171 !code->is_compare_ic_stub()) ||
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000172 RelocInfo::IsConstructCall(rmode())) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000173 break_point_++;
174 return;
175 }
176 if (code->kind() == Code::STUB) {
ager@chromium.orga1645e22009-09-09 19:27:10 +0000177 if (IsDebuggerStatement()) {
178 break_point_++;
179 return;
180 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000181 if (type_ == ALL_BREAK_LOCATIONS) {
182 if (Debug::IsBreakStub(code)) {
183 break_point_++;
184 return;
185 }
186 } else {
187 ASSERT(type_ == SOURCE_BREAK_LOCATIONS);
188 if (Debug::IsSourceBreakStub(code)) {
189 break_point_++;
190 return;
191 }
192 }
193 }
194 }
195
196 // Check for break at return.
ager@chromium.org236ad962008-09-25 09:45:57 +0000197 if (RelocInfo::IsJSReturn(rmode())) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000198 // Set the positions to the end of the function.
199 if (debug_info_->shared()->HasSourceCode()) {
200 position_ = debug_info_->shared()->end_position() -
whesse@chromium.orge90029b2010-08-02 11:52:17 +0000201 debug_info_->shared()->start_position() - 1;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000202 } else {
203 position_ = 0;
204 }
205 statement_position_ = position_;
206 break_point_++;
207 return;
208 }
209 }
210}
211
212
213void BreakLocationIterator::Next(int count) {
214 while (count > 0) {
215 Next();
216 count--;
217 }
218}
219
220
221// Find the break point closest to the supplied address.
222void BreakLocationIterator::FindBreakLocationFromAddress(Address pc) {
223 // Run through all break points to locate the one closest to the address.
224 int closest_break_point = 0;
225 int distance = kMaxInt;
226 while (!Done()) {
227 // Check if this break point is closer that what was previously found.
228 if (this->pc() < pc && pc - this->pc() < distance) {
229 closest_break_point = break_point();
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000230 distance = static_cast<int>(pc - this->pc());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000231 // Check whether we can't get any closer.
232 if (distance == 0) break;
233 }
234 Next();
235 }
236
237 // Move to the break point found.
238 Reset();
239 Next(closest_break_point);
240}
241
242
243// Find the break point closest to the supplied source position.
244void BreakLocationIterator::FindBreakLocationFromPosition(int position) {
245 // Run through all break points to locate the one closest to the source
246 // position.
247 int closest_break_point = 0;
248 int distance = kMaxInt;
249 while (!Done()) {
250 // Check if this break point is closer that what was previously found.
251 if (position <= statement_position() &&
252 statement_position() - position < distance) {
253 closest_break_point = break_point();
254 distance = statement_position() - position;
255 // Check whether we can't get any closer.
256 if (distance == 0) break;
257 }
258 Next();
259 }
260
261 // Move to the break point found.
262 Reset();
263 Next(closest_break_point);
264}
265
266
267void BreakLocationIterator::Reset() {
268 // Create relocation iterators for the two code objects.
269 if (reloc_iterator_ != NULL) delete reloc_iterator_;
270 if (reloc_iterator_original_ != NULL) delete reloc_iterator_original_;
271 reloc_iterator_ = new RelocIterator(debug_info_->code());
272 reloc_iterator_original_ = new RelocIterator(debug_info_->original_code());
273
274 // Position at the first break point.
275 break_point_ = -1;
276 position_ = 1;
277 statement_position_ = 1;
278 Next();
279}
280
281
282bool BreakLocationIterator::Done() const {
283 return RinfoDone();
284}
285
286
287void BreakLocationIterator::SetBreakPoint(Handle<Object> break_point_object) {
288 // If there is not already a real break point here patch code with debug
289 // break.
290 if (!HasBreakPoint()) {
291 SetDebugBreak();
292 }
ager@chromium.orga1645e22009-09-09 19:27:10 +0000293 ASSERT(IsDebugBreak() || IsDebuggerStatement());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000294 // Set the break point information.
295 DebugInfo::SetBreakPoint(debug_info_, code_position(),
296 position(), statement_position(),
297 break_point_object);
298}
299
300
301void BreakLocationIterator::ClearBreakPoint(Handle<Object> break_point_object) {
302 // Clear the break point information.
303 DebugInfo::ClearBreakPoint(debug_info_, code_position(), break_point_object);
304 // If there are no more break points here remove the debug break.
305 if (!HasBreakPoint()) {
306 ClearDebugBreak();
307 ASSERT(!IsDebugBreak());
308 }
309}
310
311
312void BreakLocationIterator::SetOneShot() {
ager@chromium.orga1645e22009-09-09 19:27:10 +0000313 // Debugger statement always calls debugger. No need to modify it.
314 if (IsDebuggerStatement()) {
315 return;
316 }
317
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000318 // If there is a real break point here no more to do.
319 if (HasBreakPoint()) {
320 ASSERT(IsDebugBreak());
321 return;
322 }
323
324 // Patch code with debug break.
325 SetDebugBreak();
326}
327
328
329void BreakLocationIterator::ClearOneShot() {
ager@chromium.orga1645e22009-09-09 19:27:10 +0000330 // Debugger statement always calls debugger. No need to modify it.
331 if (IsDebuggerStatement()) {
332 return;
333 }
334
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000335 // If there is a real break point here no more to do.
336 if (HasBreakPoint()) {
337 ASSERT(IsDebugBreak());
338 return;
339 }
340
341 // Patch code removing debug break.
342 ClearDebugBreak();
343 ASSERT(!IsDebugBreak());
344}
345
346
347void BreakLocationIterator::SetDebugBreak() {
ager@chromium.orga1645e22009-09-09 19:27:10 +0000348 // Debugger statement always calls debugger. No need to modify it.
349 if (IsDebuggerStatement()) {
350 return;
351 }
352
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000353 // If there is already a break point here just return. This might happen if
v8.team.kasperl727e9952008-09-02 14:56:44 +0000354 // the same code is flooded with break points twice. Flooding the same
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000355 // function twice might happen when stepping in a function with an exception
356 // handler as the handler and the function is the same.
357 if (IsDebugBreak()) {
358 return;
359 }
360
ager@chromium.org236ad962008-09-25 09:45:57 +0000361 if (RelocInfo::IsJSReturn(rmode())) {
iposva@chromium.org245aa852009-02-10 00:49:54 +0000362 // Patch the frame exit code with a break point.
363 SetDebugBreakAtReturn();
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000364 } else if (IsDebugBreakSlot()) {
365 // Patch the code in the break slot.
366 SetDebugBreakAtSlot();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000367 } else {
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000368 // Patch the IC call.
369 SetDebugBreakAtIC();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000370 }
371 ASSERT(IsDebugBreak());
372}
373
374
375void BreakLocationIterator::ClearDebugBreak() {
ager@chromium.orga1645e22009-09-09 19:27:10 +0000376 // Debugger statement always calls debugger. No need to modify it.
377 if (IsDebuggerStatement()) {
378 return;
379 }
380
ager@chromium.org236ad962008-09-25 09:45:57 +0000381 if (RelocInfo::IsJSReturn(rmode())) {
iposva@chromium.org245aa852009-02-10 00:49:54 +0000382 // Restore the frame exit code.
383 ClearDebugBreakAtReturn();
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000384 } else if (IsDebugBreakSlot()) {
385 // Restore the code in the break slot.
386 ClearDebugBreakAtSlot();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000387 } else {
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000388 // Patch the IC call.
389 ClearDebugBreakAtIC();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000390 }
391 ASSERT(!IsDebugBreak());
392}
393
394
395void BreakLocationIterator::PrepareStepIn() {
ager@chromium.org381abbb2009-02-25 13:23:22 +0000396 HandleScope scope;
397
ager@chromium.orga1645e22009-09-09 19:27:10 +0000398 // Step in can only be prepared if currently positioned on an IC call,
399 // construct call or CallFunction stub call.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000400 Address target = rinfo()->target_address();
ager@chromium.orga1645e22009-09-09 19:27:10 +0000401 Handle<Code> code(Code::GetCodeFromTargetAddress(target));
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +0000402 if (code->is_call_stub() || code->is_keyed_call_stub()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000403 // Step in through IC call is handled by the runtime system. Therefore make
404 // sure that the any current IC is cleared and the runtime system is
405 // called. If the executing code has a debug break at the location change
406 // the call in the original code as it is the code there that will be
407 // executed in place of the debug break call.
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +0000408 Handle<Code> stub = ComputeCallDebugPrepareStepIn(code->arguments_count(),
409 code->kind());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000410 if (IsDebugBreak()) {
411 original_rinfo()->set_target_address(stub->entry());
412 } else {
413 rinfo()->set_target_address(stub->entry());
414 }
415 } else {
ager@chromium.orga1645e22009-09-09 19:27:10 +0000416#ifdef DEBUG
417 // All the following stuff is needed only for assertion checks so the code
418 // is wrapped in ifdef.
419 Handle<Code> maybe_call_function_stub = code;
420 if (IsDebugBreak()) {
421 Address original_target = original_rinfo()->target_address();
422 maybe_call_function_stub =
423 Handle<Code>(Code::GetCodeFromTargetAddress(original_target));
424 }
425 bool is_call_function_stub =
426 (maybe_call_function_stub->kind() == Code::STUB &&
427 maybe_call_function_stub->major_key() == CodeStub::CallFunction);
428
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000429 // Step in through construct call requires no changes to the running code.
430 // Step in through getters/setters should already be prepared as well
431 // because caller of this function (Debug::PrepareStep) is expected to
432 // flood the top frame's function with one shot breakpoints.
ager@chromium.orga1645e22009-09-09 19:27:10 +0000433 // Step in through CallFunction stub should also be prepared by caller of
434 // this function (Debug::PrepareStep) which should flood target function
435 // with breakpoints.
436 ASSERT(RelocInfo::IsConstructCall(rmode()) || code->is_inline_cache_stub()
437 || is_call_function_stub);
438#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000439 }
440}
441
442
443// Check whether the break point is at a position which will exit the function.
444bool BreakLocationIterator::IsExit() const {
ager@chromium.org236ad962008-09-25 09:45:57 +0000445 return (RelocInfo::IsJSReturn(rmode()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000446}
447
448
449bool BreakLocationIterator::HasBreakPoint() {
450 return debug_info_->HasBreakPoint(code_position());
451}
452
453
454// Check whether there is a debug break at the current position.
455bool BreakLocationIterator::IsDebugBreak() {
ager@chromium.org236ad962008-09-25 09:45:57 +0000456 if (RelocInfo::IsJSReturn(rmode())) {
iposva@chromium.org245aa852009-02-10 00:49:54 +0000457 return IsDebugBreakAtReturn();
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000458 } else if (IsDebugBreakSlot()) {
459 return IsDebugBreakAtSlot();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000460 } else {
461 return Debug::IsDebugBreak(rinfo()->target_address());
462 }
463}
464
465
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000466void BreakLocationIterator::SetDebugBreakAtIC() {
467 // Patch the original code with the current address as the current address
468 // might have changed by the inline caching since the code was copied.
469 original_rinfo()->set_target_address(rinfo()->target_address());
470
471 RelocInfo::Mode mode = rmode();
472 if (RelocInfo::IsCodeTarget(mode)) {
473 Address target = rinfo()->target_address();
474 Handle<Code> code(Code::GetCodeFromTargetAddress(target));
475
476 // Patch the code to invoke the builtin debug break function matching the
477 // calling convention used by the call site.
478 Handle<Code> dbgbrk_code(Debug::FindDebugBreak(code, mode));
479 rinfo()->set_target_address(dbgbrk_code->entry());
480
481 // For stubs that refer back to an inlined version clear the cached map for
482 // the inlined case to always go through the IC. As long as the break point
483 // is set the patching performed by the runtime system will take place in
484 // the code copy and will therefore have no effect on the running code
485 // keeping it from using the inlined code.
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000486 if (code->is_keyed_load_stub()) {
487 KeyedLoadIC::ClearInlinedVersion(pc());
488 } else if (code->is_keyed_store_stub()) {
489 KeyedStoreIC::ClearInlinedVersion(pc());
490 } else if (code->is_load_stub()) {
491 LoadIC::ClearInlinedVersion(pc());
ricow@chromium.org0b9f8502010-08-18 07:45:01 +0000492 } else if (code->is_store_stub()) {
493 StoreIC::ClearInlinedVersion(pc());
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000494 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000495 }
496}
497
498
499void BreakLocationIterator::ClearDebugBreakAtIC() {
500 // Patch the code to the original invoke.
501 rinfo()->set_target_address(original_rinfo()->target_address());
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000502
503 RelocInfo::Mode mode = rmode();
504 if (RelocInfo::IsCodeTarget(mode)) {
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +0000505 AssertNoAllocation nogc;
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000506 Address target = original_rinfo()->target_address();
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +0000507 Code* code = Code::GetCodeFromTargetAddress(target);
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000508
509 // Restore the inlined version of keyed stores to get back to the
510 // fast case. We need to patch back the keyed store because no
511 // patching happens when running normally. For keyed loads, the
512 // map check will get patched back when running normally after ICs
513 // have been cleared at GC.
514 if (code->is_keyed_store_stub()) KeyedStoreIC::RestoreInlinedVersion(pc());
515 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000516}
517
518
ager@chromium.orga1645e22009-09-09 19:27:10 +0000519bool BreakLocationIterator::IsDebuggerStatement() {
ager@chromium.org5c838252010-02-19 08:53:10 +0000520 return RelocInfo::DEBUG_BREAK == rmode();
ager@chromium.orga1645e22009-09-09 19:27:10 +0000521}
522
523
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000524bool BreakLocationIterator::IsDebugBreakSlot() {
525 return RelocInfo::DEBUG_BREAK_SLOT == rmode();
526}
527
528
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000529Object* BreakLocationIterator::BreakPointObjects() {
530 return debug_info_->GetBreakPointObjects(code_position());
531}
532
533
ager@chromium.org381abbb2009-02-25 13:23:22 +0000534// Clear out all the debug break code. This is ONLY supposed to be used when
535// shutting down the debugger as it will leave the break point information in
536// DebugInfo even though the code is patched back to the non break point state.
537void BreakLocationIterator::ClearAllDebugBreak() {
538 while (!Done()) {
539 ClearDebugBreak();
540 Next();
541 }
542}
543
544
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000545bool BreakLocationIterator::RinfoDone() const {
546 ASSERT(reloc_iterator_->done() == reloc_iterator_original_->done());
547 return reloc_iterator_->done();
548}
549
550
551void BreakLocationIterator::RinfoNext() {
552 reloc_iterator_->next();
553 reloc_iterator_original_->next();
554#ifdef DEBUG
555 ASSERT(reloc_iterator_->done() == reloc_iterator_original_->done());
556 if (!reloc_iterator_->done()) {
557 ASSERT(rmode() == original_rmode());
558 }
559#endif
560}
561
562
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000563// Threading support.
564void Debug::ThreadInit() {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000565 thread_local_.break_count_ = 0;
566 thread_local_.break_id_ = 0;
567 thread_local_.break_frame_id_ = StackFrame::NO_ID;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000568 thread_local_.last_step_action_ = StepNone;
ager@chromium.org236ad962008-09-25 09:45:57 +0000569 thread_local_.last_statement_position_ = RelocInfo::kNoPosition;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000570 thread_local_.step_count_ = 0;
571 thread_local_.last_fp_ = 0;
572 thread_local_.step_into_fp_ = 0;
ager@chromium.orga1645e22009-09-09 19:27:10 +0000573 thread_local_.step_out_fp_ = 0;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000574 thread_local_.after_break_target_ = 0;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000575 // TODO(isolates): frames_are_dropped_?
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000576 thread_local_.debugger_entry_ = NULL;
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000577 thread_local_.pending_interrupts_ = 0;
ricow@chromium.org0b9f8502010-08-18 07:45:01 +0000578 thread_local_.restarter_frame_function_pointer_ = NULL;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000579}
580
581
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000582char* Debug::ArchiveDebug(char* storage) {
583 char* to = storage;
584 memcpy(to, reinterpret_cast<char*>(&thread_local_), sizeof(ThreadLocal));
585 to += sizeof(ThreadLocal);
586 memcpy(to, reinterpret_cast<char*>(&registers_), sizeof(registers_));
587 ThreadInit();
588 ASSERT(to <= storage + ArchiveSpacePerThread());
589 return storage + ArchiveSpacePerThread();
590}
591
592
593char* Debug::RestoreDebug(char* storage) {
594 char* from = storage;
595 memcpy(reinterpret_cast<char*>(&thread_local_), from, sizeof(ThreadLocal));
596 from += sizeof(ThreadLocal);
597 memcpy(reinterpret_cast<char*>(&registers_), from, sizeof(registers_));
598 ASSERT(from <= storage + ArchiveSpacePerThread());
599 return storage + ArchiveSpacePerThread();
600}
601
602
603int Debug::ArchiveSpacePerThread() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000604 return sizeof(ThreadLocal) + sizeof(JSCallerSavedBuffer);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000605}
606
607
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000608// Frame structure (conforms InternalFrame structure):
609// -- code
610// -- SMI maker
611// -- function (slot is called "context")
612// -- frame base
613Object** Debug::SetUpFrameDropperFrame(StackFrame* bottom_js_frame,
614 Handle<Code> code) {
615 ASSERT(bottom_js_frame->is_java_script());
616
617 Address fp = bottom_js_frame->fp();
618
619 // Move function pointer into "context" slot.
620 Memory::Object_at(fp + StandardFrameConstants::kContextOffset) =
621 Memory::Object_at(fp + JavaScriptFrameConstants::kFunctionOffset);
622
623 Memory::Object_at(fp + InternalFrameConstants::kCodeOffset) = *code;
624 Memory::Object_at(fp + StandardFrameConstants::kMarkerOffset) =
625 Smi::FromInt(StackFrame::INTERNAL);
626
627 return reinterpret_cast<Object**>(&Memory::Object_at(
628 fp + StandardFrameConstants::kContextOffset));
629}
630
631const int Debug::kFrameDropperFrameSize = 4;
632
633
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000634void ScriptCache::Add(Handle<Script> script) {
lrn@chromium.org7516f052011-03-30 08:52:27 +0000635 GlobalHandles* global_handles = Isolate::Current()->global_handles();
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000636 // Create an entry in the hash map for the script.
637 int id = Smi::cast(script->id())->value();
638 HashMap::Entry* entry =
639 HashMap::Lookup(reinterpret_cast<void*>(id), Hash(id), true);
640 if (entry->value != NULL) {
641 ASSERT(*script == *reinterpret_cast<Script**>(entry->value));
642 return;
643 }
644
645 // Globalize the script object, make it weak and use the location of the
646 // global handle as the value in the hash map.
647 Handle<Script> script_ =
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000648 Handle<Script>::cast(
lrn@chromium.org7516f052011-03-30 08:52:27 +0000649 (global_handles->Create(*script)));
650 global_handles->MakeWeak(
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000651 reinterpret_cast<Object**>(script_.location()),
652 this,
653 ScriptCache::HandleWeakScript);
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000654 entry->value = script_.location();
655}
656
657
658Handle<FixedArray> ScriptCache::GetScripts() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000659 Handle<FixedArray> instances = FACTORY->NewFixedArray(occupancy());
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000660 int count = 0;
661 for (HashMap::Entry* entry = Start(); entry != NULL; entry = Next(entry)) {
662 ASSERT(entry->value != NULL);
663 if (entry->value != NULL) {
664 instances->set(count, *reinterpret_cast<Script**>(entry->value));
665 count++;
666 }
667 }
668 return instances;
669}
670
671
672void ScriptCache::ProcessCollectedScripts() {
lrn@chromium.org7516f052011-03-30 08:52:27 +0000673 Debugger* debugger = Isolate::Current()->debugger();
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000674 for (int i = 0; i < collected_scripts_.length(); i++) {
lrn@chromium.org7516f052011-03-30 08:52:27 +0000675 debugger->OnScriptCollected(collected_scripts_[i]);
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000676 }
677 collected_scripts_.Clear();
678}
679
680
681void ScriptCache::Clear() {
lrn@chromium.org7516f052011-03-30 08:52:27 +0000682 GlobalHandles* global_handles = Isolate::Current()->global_handles();
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000683 // Iterate the script cache to get rid of all the weak handles.
684 for (HashMap::Entry* entry = Start(); entry != NULL; entry = Next(entry)) {
685 ASSERT(entry != NULL);
686 Object** location = reinterpret_cast<Object**>(entry->value);
687 ASSERT((*location)->IsScript());
lrn@chromium.org7516f052011-03-30 08:52:27 +0000688 global_handles->ClearWeakness(location);
689 global_handles->Destroy(location);
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000690 }
691 // Clear the content of the hash map.
692 HashMap::Clear();
693}
694
695
696void ScriptCache::HandleWeakScript(v8::Persistent<v8::Value> obj, void* data) {
697 ScriptCache* script_cache = reinterpret_cast<ScriptCache*>(data);
698 // Find the location of the global handle.
699 Script** location =
700 reinterpret_cast<Script**>(Utils::OpenHandle(*obj).location());
701 ASSERT((*location)->IsScript());
702
703 // Remove the entry from the cache.
704 int id = Smi::cast((*location)->id())->value();
705 script_cache->Remove(reinterpret_cast<void*>(id), Hash(id));
706 script_cache->collected_scripts_.Add(id);
707
708 // Clear the weak handle.
709 obj.Dispose();
710 obj.Clear();
711}
712
713
714void Debug::Setup(bool create_heap_objects) {
715 ThreadInit();
716 if (create_heap_objects) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000717 // Get code to handle debug break on return.
718 debug_break_return_ =
lrn@chromium.org7516f052011-03-30 08:52:27 +0000719 isolate_->builtins()->builtin(Builtins::kReturn_DebugBreak);
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000720 ASSERT(debug_break_return_->IsCode());
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000721 // Get code to handle debug break in debug break slots.
722 debug_break_slot_ =
lrn@chromium.org7516f052011-03-30 08:52:27 +0000723 isolate_->builtins()->builtin(Builtins::kSlot_DebugBreak);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000724 ASSERT(debug_break_slot_->IsCode());
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000725 }
726}
727
728
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000729void Debug::HandleWeakDebugInfo(v8::Persistent<v8::Value> obj, void* data) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000730 Debug* debug = Isolate::Current()->debug();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000731 DebugInfoListNode* node = reinterpret_cast<DebugInfoListNode*>(data);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000732 // We need to clear all breakpoints associated with the function to restore
733 // original code and avoid patching the code twice later because
734 // the function will live in the heap until next gc, and can be found by
735 // Runtime::FindSharedFunctionInfoInScript.
736 BreakLocationIterator it(node->debug_info(), ALL_BREAK_LOCATIONS);
737 it.ClearAllDebugBreak();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000738 debug->RemoveDebugInfo(node->debug_info());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000739#ifdef DEBUG
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000740 node = debug->debug_info_list_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000741 while (node != NULL) {
742 ASSERT(node != reinterpret_cast<DebugInfoListNode*>(data));
743 node = node->next();
744 }
745#endif
746}
747
748
749DebugInfoListNode::DebugInfoListNode(DebugInfo* debug_info): next_(NULL) {
lrn@chromium.org7516f052011-03-30 08:52:27 +0000750 GlobalHandles* global_handles = Isolate::Current()->global_handles();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000751 // Globalize the request debug info object and make it weak.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000752 debug_info_ = Handle<DebugInfo>::cast(
lrn@chromium.org7516f052011-03-30 08:52:27 +0000753 (global_handles->Create(debug_info)));
754 global_handles->MakeWeak(
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000755 reinterpret_cast<Object**>(debug_info_.location()),
756 this,
757 Debug::HandleWeakDebugInfo);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000758}
759
760
761DebugInfoListNode::~DebugInfoListNode() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000762 Isolate::Current()->global_handles()->Destroy(
763 reinterpret_cast<Object**>(debug_info_.location()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000764}
765
766
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000767bool Debug::CompileDebuggerScript(int index) {
lrn@chromium.org7516f052011-03-30 08:52:27 +0000768 Isolate* isolate = Isolate::Current();
769 Factory* factory = isolate->factory();
770 HandleScope scope(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000771
kasper.lund44510672008-07-25 07:37:58 +0000772 // Bail out if the index is invalid.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000773 if (index == -1) {
774 return false;
775 }
kasper.lund44510672008-07-25 07:37:58 +0000776
777 // Find source and name for the requested script.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000778 Handle<String> source_code =
lrn@chromium.org7516f052011-03-30 08:52:27 +0000779 isolate->bootstrapper()->NativesSourceLookup(index);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000780 Vector<const char> name = Natives::GetScriptName(index);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000781 Handle<String> script_name = factory->NewStringFromAscii(name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000782
783 // Compile the script.
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000784 Handle<SharedFunctionInfo> function_info;
785 function_info = Compiler::Compile(source_code,
786 script_name,
787 0, 0, NULL, NULL,
788 Handle<String>::null(),
789 NATIVES_CODE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000790
791 // Silently ignore stack overflows during compilation.
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000792 if (function_info.is_null()) {
lrn@chromium.org7516f052011-03-30 08:52:27 +0000793 ASSERT(isolate->has_pending_exception());
794 isolate->clear_pending_exception();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000795 return false;
796 }
797
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000798 // Execute the shared function in the debugger context.
lrn@chromium.org7516f052011-03-30 08:52:27 +0000799 Handle<Context> context = isolate->global_context();
kasper.lund44510672008-07-25 07:37:58 +0000800 bool caught_exception = false;
801 Handle<JSFunction> function =
lrn@chromium.org7516f052011-03-30 08:52:27 +0000802 factory->NewFunctionFromSharedFunctionInfo(function_info, context);
kasper.lund44510672008-07-25 07:37:58 +0000803 Handle<Object> result =
804 Execution::TryCall(function, Handle<Object>(context->global()),
805 0, NULL, &caught_exception);
806
807 // Check for caught exceptions.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000808 if (caught_exception) {
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000809 Handle<Object> message = MessageHandler::MakeMessageObject(
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000810 "error_loading_debugger", NULL, Vector<Handle<Object> >::empty(),
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +0000811 Handle<String>(), Handle<JSArray>());
karlklose@chromium.org44bc7082011-04-11 12:33:05 +0000812 MessageHandler::ReportMessage(Isolate::Current(), NULL, message);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000813 return false;
814 }
815
kasper.lund44510672008-07-25 07:37:58 +0000816 // Mark this script as native and return successfully.
817 Handle<Script> script(Script::cast(function->shared()->script()));
ager@chromium.orge2902be2009-06-08 12:21:35 +0000818 script->set_type(Smi::FromInt(Script::TYPE_NATIVE));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000819 return true;
820}
821
822
823bool Debug::Load() {
824 // Return if debugger is already loaded.
kasper.lund212ac232008-07-16 07:07:30 +0000825 if (IsLoaded()) return true;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000826
lrn@chromium.org7516f052011-03-30 08:52:27 +0000827 ASSERT(Isolate::Current() == isolate_);
828 Debugger* debugger = isolate_->debugger();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000829
kasper.lund44510672008-07-25 07:37:58 +0000830 // Bail out if we're already in the process of compiling the native
831 // JavaScript source code for the debugger.
lrn@chromium.org7516f052011-03-30 08:52:27 +0000832 if (debugger->compiling_natives() ||
833 debugger->is_loading_debugger())
mads.s.agercbaa0602008-08-14 13:41:48 +0000834 return false;
lrn@chromium.org7516f052011-03-30 08:52:27 +0000835 debugger->set_loading_debugger(true);
kasper.lund44510672008-07-25 07:37:58 +0000836
837 // Disable breakpoints and interrupts while compiling and running the
838 // debugger scripts including the context creation code.
839 DisableBreak disable(true);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000840 PostponeInterruptsScope postpone(isolate_);
kasper.lund44510672008-07-25 07:37:58 +0000841
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000842 // Create the debugger context.
lrn@chromium.org7516f052011-03-30 08:52:27 +0000843 HandleScope scope(isolate_);
kasper.lund44510672008-07-25 07:37:58 +0000844 Handle<Context> context =
lrn@chromium.org7516f052011-03-30 08:52:27 +0000845 isolate_->bootstrapper()->CreateEnvironment(
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000846 Handle<Object>::null(),
847 v8::Handle<ObjectTemplate>(),
848 NULL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000849
kasper.lund44510672008-07-25 07:37:58 +0000850 // Use the debugger context.
lrn@chromium.org7516f052011-03-30 08:52:27 +0000851 SaveContext save(isolate_);
852 isolate_->set_context(*context);
kasper.lund44510672008-07-25 07:37:58 +0000853
854 // Expose the builtins object in the debugger context.
lrn@chromium.org7516f052011-03-30 08:52:27 +0000855 Handle<String> key = isolate_->factory()->LookupAsciiSymbol("builtins");
kasper.lund44510672008-07-25 07:37:58 +0000856 Handle<GlobalObject> global = Handle<GlobalObject>(context->global());
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +0000857 RETURN_IF_EMPTY_HANDLE_VALUE(
lrn@chromium.org7516f052011-03-30 08:52:27 +0000858 isolate_,
ager@chromium.org9ee27ae2011-03-02 13:43:26 +0000859 SetProperty(global, key, Handle<Object>(global->builtins()),
860 NONE, kNonStrictMode),
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +0000861 false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000862
863 // Compile the JavaScript for the debugger in the debugger context.
lrn@chromium.org7516f052011-03-30 08:52:27 +0000864 debugger->set_compiling_natives(true);
kasper.lund44510672008-07-25 07:37:58 +0000865 bool caught_exception =
866 !CompileDebuggerScript(Natives::GetIndex("mirror")) ||
867 !CompileDebuggerScript(Natives::GetIndex("debug"));
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000868
869 if (FLAG_enable_liveedit) {
870 caught_exception = caught_exception ||
871 !CompileDebuggerScript(Natives::GetIndex("liveedit"));
872 }
873
lrn@chromium.org7516f052011-03-30 08:52:27 +0000874 debugger->set_compiling_natives(false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000875
mads.s.agercbaa0602008-08-14 13:41:48 +0000876 // Make sure we mark the debugger as not loading before we might
877 // return.
lrn@chromium.org7516f052011-03-30 08:52:27 +0000878 debugger->set_loading_debugger(false);
mads.s.agercbaa0602008-08-14 13:41:48 +0000879
kasper.lund44510672008-07-25 07:37:58 +0000880 // Check for caught exceptions.
881 if (caught_exception) return false;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000882
883 // Debugger loaded.
whesse@chromium.org023421e2010-12-21 12:19:12 +0000884 debug_context_ = context;
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000885
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000886 return true;
887}
888
889
890void Debug::Unload() {
891 // Return debugger is not loaded.
892 if (!IsLoaded()) {
893 return;
894 }
895
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000896 // Clear the script cache.
897 DestroyScriptCache();
898
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000899 // Clear debugger context global handle.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000900 Isolate::Current()->global_handles()->Destroy(
901 reinterpret_cast<Object**>(debug_context_.location()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000902 debug_context_ = Handle<Context>();
903}
904
905
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000906// Set the flag indicating that preemption happened during debugging.
907void Debug::PreemptionWhileInDebugger() {
908 ASSERT(InDebugger());
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000909 Debug::set_interrupts_pending(PREEMPT);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000910}
911
912
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000913void Debug::Iterate(ObjectVisitor* v) {
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000914 v->VisitPointer(BitCast<Object**>(&(debug_break_return_)));
915 v->VisitPointer(BitCast<Object**>(&(debug_break_slot_)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000916}
917
918
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000919Object* Debug::Break(Arguments args) {
920 Heap* heap = isolate_->heap();
921 HandleScope scope(isolate_);
mads.s.ager31e71382008-08-13 09:32:07 +0000922 ASSERT(args.length() == 0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000923
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000924 thread_local_.frame_drop_mode_ = FRAMES_UNTOUCHED;
ager@chromium.org357bf652010-04-12 11:30:10 +0000925
kasper.lundbd3ec4e2008-07-09 11:06:54 +0000926 // Get the top-most JavaScript frame.
vegorov@chromium.org74f333b2011-04-06 11:17:46 +0000927 JavaScriptFrameIterator it(isolate_);
kasper.lundbd3ec4e2008-07-09 11:06:54 +0000928 JavaScriptFrame* frame = it.frame();
929
930 // Just continue if breaks are disabled or debugger cannot be loaded.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000931 if (disable_break() || !Load()) {
932 SetAfterBreakTarget(frame);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000933 return heap->undefined_value();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000934 }
935
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000936 // Enter the debugger.
937 EnterDebugger debugger;
938 if (debugger.FailedToEnter()) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000939 return heap->undefined_value();
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000940 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000941
kasper.lund44510672008-07-25 07:37:58 +0000942 // Postpone interrupt during breakpoint processing.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000943 PostponeInterruptsScope postpone(isolate_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000944
945 // Get the debug info (create it if it does not exist).
946 Handle<SharedFunctionInfo> shared =
947 Handle<SharedFunctionInfo>(JSFunction::cast(frame->function())->shared());
948 Handle<DebugInfo> debug_info = GetDebugInfo(shared);
949
950 // Find the break point where execution has stopped.
951 BreakLocationIterator break_location_iterator(debug_info,
952 ALL_BREAK_LOCATIONS);
953 break_location_iterator.FindBreakLocationFromAddress(frame->pc());
954
955 // Check whether step next reached a new statement.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000956 if (!StepNextContinue(&break_location_iterator, frame)) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000957 // Decrease steps left if performing multiple steps.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000958 if (thread_local_.step_count_ > 0) {
959 thread_local_.step_count_--;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000960 }
961 }
962
963 // If there is one or more real break points check whether any of these are
964 // triggered.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000965 Handle<Object> break_points_hit(heap->undefined_value());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000966 if (break_location_iterator.HasBreakPoint()) {
967 Handle<Object> break_point_objects =
968 Handle<Object>(break_location_iterator.BreakPointObjects());
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000969 break_points_hit = CheckBreakPoints(break_point_objects);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000970 }
971
ager@chromium.orga1645e22009-09-09 19:27:10 +0000972 // If step out is active skip everything until the frame where we need to step
973 // out to is reached, unless real breakpoint is hit.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000974 if (StepOutActive() && frame->fp() != step_out_fp() &&
ager@chromium.orga1645e22009-09-09 19:27:10 +0000975 break_points_hit->IsUndefined() ) {
976 // Step count should always be 0 for StepOut.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000977 ASSERT(thread_local_.step_count_ == 0);
ager@chromium.orga1645e22009-09-09 19:27:10 +0000978 } else if (!break_points_hit->IsUndefined() ||
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000979 (thread_local_.last_step_action_ != StepNone &&
980 thread_local_.step_count_ == 0)) {
ager@chromium.orga1645e22009-09-09 19:27:10 +0000981 // Notify debugger if a real break point is triggered or if performing
982 // single stepping with no more steps to perform. Otherwise do another step.
983
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000984 // Clear all current stepping setup.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000985 ClearStepping();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000986
987 // Notify the debug event listeners.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000988 isolate_->debugger()->OnDebugBreak(break_points_hit, false);
989 } else if (thread_local_.last_step_action_ != StepNone) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000990 // Hold on to last step action as it is cleared by the call to
991 // ClearStepping.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000992 StepAction step_action = thread_local_.last_step_action_;
993 int step_count = thread_local_.step_count_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000994
995 // Clear all current stepping setup.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000996 ClearStepping();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000997
998 // Set up for the remaining steps.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000999 PrepareStep(step_action, step_count);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001000 }
1001
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001002 if (thread_local_.frame_drop_mode_ == FRAMES_UNTOUCHED) {
1003 SetAfterBreakTarget(frame);
1004 } else if (thread_local_.frame_drop_mode_ ==
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001005 FRAME_DROPPED_IN_IC_CALL) {
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001006 // We must have been calling IC stub. Do not go there anymore.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001007 Code* plain_return = isolate_->builtins()->builtin(
1008 Builtins::kPlainReturn_LiveEdit);
1009 thread_local_.after_break_target_ = plain_return->entry();
1010 } else if (thread_local_.frame_drop_mode_ ==
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001011 FRAME_DROPPED_IN_DEBUG_SLOT_CALL) {
1012 // Debug break slot stub does not return normally, instead it manually
1013 // cleans the stack and jumps. We should patch the jump address.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001014 Code* plain_return = isolate_->builtins()->builtin(
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00001015 Builtins::kFrameDropper_LiveEdit);
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001016 thread_local_.after_break_target_ = plain_return->entry();
1017 } else if (thread_local_.frame_drop_mode_ ==
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001018 FRAME_DROPPED_IN_DIRECT_CALL) {
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001019 // Nothing to do, after_break_target is not used here.
ager@chromium.org357bf652010-04-12 11:30:10 +00001020 } else {
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001021 UNREACHABLE();
ager@chromium.org357bf652010-04-12 11:30:10 +00001022 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001023
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001024 return heap->undefined_value();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001025}
1026
1027
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001028RUNTIME_FUNCTION(Object*, Debug_Break) {
1029 return isolate->debug()->Break(args);
1030}
1031
1032
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001033// Check the break point objects for whether one or more are actually
1034// triggered. This function returns a JSArray with the break point objects
1035// which is triggered.
1036Handle<Object> Debug::CheckBreakPoints(Handle<Object> break_point_objects) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00001037 Factory* factory = isolate_->factory();
1038
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001039 // Count the number of break points hit. If there are multiple break points
1040 // they are in a FixedArray.
1041 Handle<FixedArray> break_points_hit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001042 int break_points_hit_count = 0;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001043 ASSERT(!break_point_objects->IsUndefined());
1044 if (break_point_objects->IsFixedArray()) {
1045 Handle<FixedArray> array(FixedArray::cast(*break_point_objects));
lrn@chromium.org7516f052011-03-30 08:52:27 +00001046 break_points_hit = factory->NewFixedArray(array->length());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001047 for (int i = 0; i < array->length(); i++) {
1048 Handle<Object> o(array->get(i));
1049 if (CheckBreakPoint(o)) {
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001050 break_points_hit->set(break_points_hit_count++, *o);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001051 }
1052 }
1053 } else {
lrn@chromium.org7516f052011-03-30 08:52:27 +00001054 break_points_hit = factory->NewFixedArray(1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001055 if (CheckBreakPoint(break_point_objects)) {
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001056 break_points_hit->set(break_points_hit_count++, *break_point_objects);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001057 }
1058 }
1059
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001060 // Return undefined if no break points were triggered.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001061 if (break_points_hit_count == 0) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00001062 return factory->undefined_value();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001063 }
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001064 // Return break points hit as a JSArray.
lrn@chromium.org7516f052011-03-30 08:52:27 +00001065 Handle<JSArray> result = factory->NewJSArrayWithElements(break_points_hit);
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001066 result->set_length(Smi::FromInt(break_points_hit_count));
1067 return result;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001068}
1069
1070
1071// Check whether a single break point object is triggered.
1072bool Debug::CheckBreakPoint(Handle<Object> break_point_object) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00001073 ASSERT(Isolate::Current() == isolate_);
1074 Factory* factory = isolate_->factory();
1075 HandleScope scope(isolate_);
ager@chromium.org381abbb2009-02-25 13:23:22 +00001076
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001077 // Ignore check if break point object is not a JSObject.
1078 if (!break_point_object->IsJSObject()) return true;
1079
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001080 // Get the function IsBreakPointTriggered (defined in debug-debugger.js).
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001081 Handle<String> is_break_point_triggered_symbol =
lrn@chromium.org7516f052011-03-30 08:52:27 +00001082 factory->LookupAsciiSymbol("IsBreakPointTriggered");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001083 Handle<JSFunction> check_break_point =
1084 Handle<JSFunction>(JSFunction::cast(
lrn@chromium.org303ada72010-10-27 09:33:13 +00001085 debug_context()->global()->GetPropertyNoExceptionThrown(
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001086 *is_break_point_triggered_symbol)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001087
1088 // Get the break id as an object.
lrn@chromium.org7516f052011-03-30 08:52:27 +00001089 Handle<Object> break_id = factory->NewNumberFromInt(Debug::break_id());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001090
1091 // Call HandleBreakPointx.
1092 bool caught_exception = false;
1093 const int argc = 2;
1094 Object** argv[argc] = {
1095 break_id.location(),
1096 reinterpret_cast<Object**>(break_point_object.location())
1097 };
1098 Handle<Object> result = Execution::TryCall(check_break_point,
lrn@chromium.org7516f052011-03-30 08:52:27 +00001099 isolate_->js_builtins_object(), argc, argv, &caught_exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001100
1101 // If exception or non boolean result handle as not triggered
1102 if (caught_exception || !result->IsBoolean()) {
1103 return false;
1104 }
1105
1106 // Return whether the break point is triggered.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001107 ASSERT(!result.is_null());
1108 return (*result)->IsTrue();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001109}
1110
1111
1112// Check whether the function has debug information.
1113bool Debug::HasDebugInfo(Handle<SharedFunctionInfo> shared) {
1114 return !shared->debug_info()->IsUndefined();
1115}
1116
1117
kasper.lundbd3ec4e2008-07-09 11:06:54 +00001118// Return the debug info for this function. EnsureDebugInfo must be called
1119// prior to ensure the debug info has been generated for shared.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001120Handle<DebugInfo> Debug::GetDebugInfo(Handle<SharedFunctionInfo> shared) {
kasper.lundbd3ec4e2008-07-09 11:06:54 +00001121 ASSERT(HasDebugInfo(shared));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001122 return Handle<DebugInfo>(DebugInfo::cast(shared->debug_info()));
1123}
1124
1125
1126void Debug::SetBreakPoint(Handle<SharedFunctionInfo> shared,
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00001127 Handle<Object> break_point_object,
1128 int* source_position) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00001129 HandleScope scope(isolate_);
ager@chromium.org381abbb2009-02-25 13:23:22 +00001130
kasper.lundbd3ec4e2008-07-09 11:06:54 +00001131 if (!EnsureDebugInfo(shared)) {
1132 // Return if retrieving debug info failed.
1133 return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001134 }
1135
kasper.lundbd3ec4e2008-07-09 11:06:54 +00001136 Handle<DebugInfo> debug_info = GetDebugInfo(shared);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001137 // Source positions starts with zero.
1138 ASSERT(source_position >= 0);
1139
1140 // Find the break point and change it.
1141 BreakLocationIterator it(debug_info, SOURCE_BREAK_LOCATIONS);
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00001142 it.FindBreakLocationFromPosition(*source_position);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001143 it.SetBreakPoint(break_point_object);
1144
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00001145 *source_position = it.position();
1146
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001147 // At least one active break point now.
1148 ASSERT(debug_info->GetBreakPointCount() > 0);
1149}
1150
1151
1152void Debug::ClearBreakPoint(Handle<Object> break_point_object) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00001153 HandleScope scope(isolate_);
ager@chromium.org381abbb2009-02-25 13:23:22 +00001154
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001155 DebugInfoListNode* node = debug_info_list_;
1156 while (node != NULL) {
1157 Object* result = DebugInfo::FindBreakPointInfo(node->debug_info(),
1158 break_point_object);
1159 if (!result->IsUndefined()) {
1160 // Get information in the break point.
1161 BreakPointInfo* break_point_info = BreakPointInfo::cast(result);
1162 Handle<DebugInfo> debug_info = node->debug_info();
1163 Handle<SharedFunctionInfo> shared(debug_info->shared());
1164 int source_position = break_point_info->statement_position()->value();
1165
1166 // Source positions starts with zero.
1167 ASSERT(source_position >= 0);
1168
1169 // Find the break point and clear it.
1170 BreakLocationIterator it(debug_info, SOURCE_BREAK_LOCATIONS);
1171 it.FindBreakLocationFromPosition(source_position);
1172 it.ClearBreakPoint(break_point_object);
1173
1174 // If there are no more break points left remove the debug info for this
1175 // function.
1176 if (debug_info->GetBreakPointCount() == 0) {
1177 RemoveDebugInfo(debug_info);
1178 }
1179
1180 return;
1181 }
1182 node = node->next();
1183 }
1184}
1185
1186
ager@chromium.org381abbb2009-02-25 13:23:22 +00001187void Debug::ClearAllBreakPoints() {
1188 DebugInfoListNode* node = debug_info_list_;
1189 while (node != NULL) {
1190 // Remove all debug break code.
1191 BreakLocationIterator it(node->debug_info(), ALL_BREAK_LOCATIONS);
1192 it.ClearAllDebugBreak();
1193 node = node->next();
1194 }
1195
1196 // Remove all debug info.
1197 while (debug_info_list_ != NULL) {
1198 RemoveDebugInfo(debug_info_list_->debug_info());
1199 }
1200}
1201
1202
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001203void Debug::FloodWithOneShot(Handle<SharedFunctionInfo> shared) {
kasper.lundbd3ec4e2008-07-09 11:06:54 +00001204 // Make sure the function has setup the debug info.
1205 if (!EnsureDebugInfo(shared)) {
1206 // Return if we failed to retrieve the debug info.
1207 return;
1208 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001209
1210 // Flood the function with break points.
kasper.lundbd3ec4e2008-07-09 11:06:54 +00001211 BreakLocationIterator it(GetDebugInfo(shared), ALL_BREAK_LOCATIONS);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001212 while (!it.Done()) {
1213 it.SetOneShot();
1214 it.Next();
1215 }
1216}
1217
1218
1219void Debug::FloodHandlerWithOneShot() {
ager@chromium.org8bb60582008-12-11 12:02:20 +00001220 // Iterate through the JavaScript stack looking for handlers.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001221 StackFrame::Id id = break_frame_id();
ager@chromium.org8bb60582008-12-11 12:02:20 +00001222 if (id == StackFrame::NO_ID) {
1223 // If there is no JavaScript stack don't do anything.
1224 return;
1225 }
vegorov@chromium.org74f333b2011-04-06 11:17:46 +00001226 for (JavaScriptFrameIterator it(isolate_, id); !it.done(); it.Advance()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001227 JavaScriptFrame* frame = it.frame();
1228 if (frame->HasHandler()) {
1229 Handle<SharedFunctionInfo> shared =
1230 Handle<SharedFunctionInfo>(
1231 JSFunction::cast(frame->function())->shared());
1232 // Flood the function with the catch block with break points
1233 FloodWithOneShot(shared);
1234 return;
1235 }
1236 }
1237}
1238
1239
1240void Debug::ChangeBreakOnException(ExceptionBreakType type, bool enable) {
1241 if (type == BreakUncaughtException) {
1242 break_on_uncaught_exception_ = enable;
1243 } else {
1244 break_on_exception_ = enable;
1245 }
1246}
1247
1248
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00001249bool Debug::IsBreakOnException(ExceptionBreakType type) {
1250 if (type == BreakUncaughtException) {
1251 return break_on_uncaught_exception_;
1252 } else {
1253 return break_on_exception_;
1254 }
1255}
1256
1257
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001258void Debug::PrepareStep(StepAction step_action, int step_count) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00001259 ASSERT(Isolate::Current() == isolate_);
1260 HandleScope scope(isolate_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001261 ASSERT(Debug::InDebugger());
1262
1263 // Remember this step action and count.
1264 thread_local_.last_step_action_ = step_action;
ager@chromium.orga1645e22009-09-09 19:27:10 +00001265 if (step_action == StepOut) {
1266 // For step out target frame will be found on the stack so there is no need
1267 // to set step counter for it. It's expected to always be 0 for StepOut.
1268 thread_local_.step_count_ = 0;
1269 } else {
1270 thread_local_.step_count_ = step_count;
1271 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001272
1273 // Get the frame where the execution has stopped and skip the debug frame if
1274 // any. The debug frame will only be present if execution was stopped due to
1275 // hitting a break point. In other situations (e.g. unhandled exception) the
1276 // debug frame is not present.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001277 StackFrame::Id id = break_frame_id();
ager@chromium.org8bb60582008-12-11 12:02:20 +00001278 if (id == StackFrame::NO_ID) {
1279 // If there is no JavaScript stack don't do anything.
1280 return;
1281 }
vegorov@chromium.org74f333b2011-04-06 11:17:46 +00001282 JavaScriptFrameIterator frames_it(isolate_, id);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001283 JavaScriptFrame* frame = frames_it.frame();
1284
1285 // First of all ensure there is one-shot break points in the top handler
1286 // if any.
1287 FloodHandlerWithOneShot();
1288
1289 // If the function on the top frame is unresolved perform step out. This will
1290 // be the case when calling unknown functions and having the debugger stopped
1291 // in an unhandled exception.
1292 if (!frame->function()->IsJSFunction()) {
1293 // Step out: Find the calling JavaScript frame and flood it with
1294 // breakpoints.
1295 frames_it.Advance();
1296 // Fill the function to return to with one-shot break points.
1297 JSFunction* function = JSFunction::cast(frames_it.frame()->function());
1298 FloodWithOneShot(Handle<SharedFunctionInfo>(function->shared()));
1299 return;
1300 }
1301
1302 // Get the debug info (create it if it does not exist).
1303 Handle<SharedFunctionInfo> shared =
1304 Handle<SharedFunctionInfo>(JSFunction::cast(frame->function())->shared());
kasper.lundbd3ec4e2008-07-09 11:06:54 +00001305 if (!EnsureDebugInfo(shared)) {
1306 // Return if ensuring debug info failed.
1307 return;
1308 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001309 Handle<DebugInfo> debug_info = GetDebugInfo(shared);
1310
1311 // Find the break location where execution has stopped.
1312 BreakLocationIterator it(debug_info, ALL_BREAK_LOCATIONS);
1313 it.FindBreakLocationFromAddress(frame->pc());
1314
1315 // Compute whether or not the target is a call target.
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001316 bool is_load_or_store = false;
1317 bool is_inline_cache_stub = false;
whesse@chromium.orge90029b2010-08-02 11:52:17 +00001318 bool is_at_restarted_function = false;
ager@chromium.orga1645e22009-09-09 19:27:10 +00001319 Handle<Code> call_function_stub;
ager@chromium.orga1645e22009-09-09 19:27:10 +00001320
whesse@chromium.orge90029b2010-08-02 11:52:17 +00001321 if (thread_local_.restarter_frame_function_pointer_ == NULL) {
1322 if (RelocInfo::IsCodeTarget(it.rinfo()->rmode())) {
1323 bool is_call_target = false;
1324 Address target = it.rinfo()->target_address();
1325 Code* code = Code::GetCodeFromTargetAddress(target);
1326 if (code->is_call_stub() || code->is_keyed_call_stub()) {
1327 is_call_target = true;
1328 }
1329 if (code->is_inline_cache_stub()) {
1330 is_inline_cache_stub = true;
1331 is_load_or_store = !is_call_target;
1332 }
1333
1334 // Check if target code is CallFunction stub.
1335 Code* maybe_call_function_stub = code;
1336 // If there is a breakpoint at this line look at the original code to
1337 // check if it is a CallFunction stub.
1338 if (it.IsDebugBreak()) {
1339 Address original_target = it.original_rinfo()->target_address();
1340 maybe_call_function_stub =
1341 Code::GetCodeFromTargetAddress(original_target);
1342 }
1343 if (maybe_call_function_stub->kind() == Code::STUB &&
1344 maybe_call_function_stub->major_key() == CodeStub::CallFunction) {
1345 // Save reference to the code as we may need it to find out arguments
1346 // count for 'step in' later.
1347 call_function_stub = Handle<Code>(maybe_call_function_stub);
1348 }
ager@chromium.orga1645e22009-09-09 19:27:10 +00001349 }
whesse@chromium.orge90029b2010-08-02 11:52:17 +00001350 } else {
1351 is_at_restarted_function = true;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001352 }
1353
v8.team.kasperl727e9952008-09-02 14:56:44 +00001354 // If this is the last break code target step out is the only possibility.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001355 if (it.IsExit() || step_action == StepOut) {
ager@chromium.orga1645e22009-09-09 19:27:10 +00001356 if (step_action == StepOut) {
1357 // Skip step_count frames starting with the current one.
1358 while (step_count-- > 0 && !frames_it.done()) {
1359 frames_it.Advance();
1360 }
1361 } else {
1362 ASSERT(it.IsExit());
1363 frames_it.Advance();
1364 }
1365 // Skip builtin functions on the stack.
1366 while (!frames_it.done() &&
1367 JSFunction::cast(frames_it.frame()->function())->IsBuiltin()) {
1368 frames_it.Advance();
1369 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001370 // Step out: If there is a JavaScript caller frame, we need to
1371 // flood it with breakpoints.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001372 if (!frames_it.done()) {
1373 // Fill the function to return to with one-shot break points.
1374 JSFunction* function = JSFunction::cast(frames_it.frame()->function());
1375 FloodWithOneShot(Handle<SharedFunctionInfo>(function->shared()));
ager@chromium.orga1645e22009-09-09 19:27:10 +00001376 // Set target frame pointer.
1377 ActivateStepOut(frames_it.frame());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001378 }
ager@chromium.orga1645e22009-09-09 19:27:10 +00001379 } else if (!(is_inline_cache_stub || RelocInfo::IsConstructCall(it.rmode()) ||
whesse@chromium.orge90029b2010-08-02 11:52:17 +00001380 !call_function_stub.is_null() || is_at_restarted_function)
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001381 || step_action == StepNext || step_action == StepMin) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001382 // Step next or step min.
1383
1384 // Fill the current function with one-shot break points.
1385 FloodWithOneShot(shared);
1386
1387 // Remember source position and frame to handle step next.
1388 thread_local_.last_statement_position_ =
1389 debug_info->code()->SourceStatementPosition(frame->pc());
1390 thread_local_.last_fp_ = frame->fp();
1391 } else {
whesse@chromium.orge90029b2010-08-02 11:52:17 +00001392 // If there's restarter frame on top of the stack, just get the pointer
1393 // to function which is going to be restarted.
1394 if (is_at_restarted_function) {
1395 Handle<JSFunction> restarted_function(
1396 JSFunction::cast(*thread_local_.restarter_frame_function_pointer_));
1397 Handle<SharedFunctionInfo> restarted_shared(
1398 restarted_function->shared());
1399 FloodWithOneShot(restarted_shared);
1400 } else if (!call_function_stub.is_null()) {
1401 // If it's CallFunction stub ensure target function is compiled and flood
1402 // it with one shot breakpoints.
1403
ager@chromium.orga1645e22009-09-09 19:27:10 +00001404 // Find out number of arguments from the stub minor key.
1405 // Reverse lookup required as the minor key cannot be retrieved
1406 // from the code object.
1407 Handle<Object> obj(
lrn@chromium.org7516f052011-03-30 08:52:27 +00001408 isolate_->heap()->code_stubs()->SlowReverseLookup(
1409 *call_function_stub));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001410 ASSERT(!obj.is_null());
1411 ASSERT(!(*obj)->IsUndefined());
ager@chromium.orga1645e22009-09-09 19:27:10 +00001412 ASSERT(obj->IsSmi());
1413 // Get the STUB key and extract major and minor key.
1414 uint32_t key = Smi::cast(*obj)->value();
1415 // Argc in the stub is the number of arguments passed - not the
1416 // expected arguments of the called function.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001417 int call_function_arg_count =
1418 CallFunctionStub::ExtractArgcFromMinorKey(
1419 CodeStub::MinorKeyFromKey(key));
ager@chromium.orga1645e22009-09-09 19:27:10 +00001420 ASSERT(call_function_stub->major_key() ==
1421 CodeStub::MajorKeyFromKey(key));
1422
1423 // Find target function on the expression stack.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001424 // Expression stack looks like this (top to bottom):
ager@chromium.orga1645e22009-09-09 19:27:10 +00001425 // argN
1426 // ...
1427 // arg0
1428 // Receiver
1429 // Function to call
1430 int expressions_count = frame->ComputeExpressionsCount();
1431 ASSERT(expressions_count - 2 - call_function_arg_count >= 0);
1432 Object* fun = frame->GetExpression(
1433 expressions_count - 2 - call_function_arg_count);
1434 if (fun->IsJSFunction()) {
1435 Handle<JSFunction> js_function(JSFunction::cast(fun));
1436 // Don't step into builtins.
1437 if (!js_function->IsBuiltin()) {
1438 // It will also compile target function if it's not compiled yet.
1439 FloodWithOneShot(Handle<SharedFunctionInfo>(js_function->shared()));
1440 }
1441 }
1442 }
1443
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001444 // Fill the current function with one-shot break points even for step in on
1445 // a call target as the function called might be a native function for
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001446 // which step in will not stop. It also prepares for stepping in
1447 // getters/setters.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001448 FloodWithOneShot(shared);
1449
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001450 if (is_load_or_store) {
1451 // Remember source position and frame to handle step in getter/setter. If
1452 // there is a custom getter/setter it will be handled in
1453 // Object::Get/SetPropertyWithCallback, otherwise the step action will be
1454 // propagated on the next Debug::Break.
1455 thread_local_.last_statement_position_ =
1456 debug_info->code()->SourceStatementPosition(frame->pc());
1457 thread_local_.last_fp_ = frame->fp();
1458 }
1459
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001460 // Step in or Step in min
1461 it.PrepareStepIn();
1462 ActivateStepIn(frame);
1463 }
1464}
1465
1466
1467// Check whether the current debug break should be reported to the debugger. It
1468// is used to have step next and step in only report break back to the debugger
1469// if on a different frame or in a different statement. In some situations
1470// there will be several break points in the same statement when the code is
v8.team.kasperl727e9952008-09-02 14:56:44 +00001471// flooded with one-shot break points. This function helps to perform several
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001472// steps before reporting break back to the debugger.
1473bool Debug::StepNextContinue(BreakLocationIterator* break_location_iterator,
1474 JavaScriptFrame* frame) {
1475 // If the step last action was step next or step in make sure that a new
1476 // statement is hit.
1477 if (thread_local_.last_step_action_ == StepNext ||
1478 thread_local_.last_step_action_ == StepIn) {
1479 // Never continue if returning from function.
1480 if (break_location_iterator->IsExit()) return false;
1481
1482 // Continue if we are still on the same frame and in the same statement.
1483 int current_statement_position =
1484 break_location_iterator->code()->SourceStatementPosition(frame->pc());
1485 return thread_local_.last_fp_ == frame->fp() &&
ager@chromium.org236ad962008-09-25 09:45:57 +00001486 thread_local_.last_statement_position_ == current_statement_position;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001487 }
1488
1489 // No step next action - don't continue.
1490 return false;
1491}
1492
1493
1494// Check whether the code object at the specified address is a debug break code
1495// object.
1496bool Debug::IsDebugBreak(Address addr) {
ager@chromium.org8bb60582008-12-11 12:02:20 +00001497 Code* code = Code::GetCodeFromTargetAddress(addr);
kasper.lund7276f142008-07-30 08:49:36 +00001498 return code->ic_state() == DEBUG_BREAK;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001499}
1500
1501
1502// Check whether a code stub with the specified major key is a possible break
1503// point location when looking for source break locations.
1504bool Debug::IsSourceBreakStub(Code* code) {
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001505 CodeStub::Major major_key = CodeStub::GetMajorKey(code);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001506 return major_key == CodeStub::CallFunction;
1507}
1508
1509
1510// Check whether a code stub with the specified major key is a possible break
1511// location.
1512bool Debug::IsBreakStub(Code* code) {
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001513 CodeStub::Major major_key = CodeStub::GetMajorKey(code);
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00001514 return major_key == CodeStub::CallFunction;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001515}
1516
1517
1518// Find the builtin to use for invoking the debug break
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001519Handle<Code> Debug::FindDebugBreak(Handle<Code> code, RelocInfo::Mode mode) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001520 // Find the builtin debug break function matching the calling convention
1521 // used by the call site.
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001522 if (code->is_inline_cache_stub()) {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001523 switch (code->kind()) {
1524 case Code::CALL_IC:
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001525 case Code::KEYED_CALL_IC:
1526 return ComputeCallDebugBreak(code->arguments_count(), code->kind());
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001527
1528 case Code::LOAD_IC:
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00001529 return Isolate::Current()->builtins()->LoadIC_DebugBreak();
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001530
1531 case Code::STORE_IC:
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00001532 return Isolate::Current()->builtins()->StoreIC_DebugBreak();
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001533
1534 case Code::KEYED_LOAD_IC:
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00001535 return Isolate::Current()->builtins()->KeyedLoadIC_DebugBreak();
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001536
1537 case Code::KEYED_STORE_IC:
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00001538 return Isolate::Current()->builtins()->KeyedStoreIC_DebugBreak();
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001539
1540 default:
1541 UNREACHABLE();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001542 }
1543 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001544 if (RelocInfo::IsConstructCall(mode)) {
1545 Handle<Code> result =
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00001546 Isolate::Current()->builtins()->ConstructCall_DebugBreak();
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001547 return result;
1548 }
1549 if (code->kind() == Code::STUB) {
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00001550 ASSERT(code->major_key() == CodeStub::CallFunction);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001551 Handle<Code> result =
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00001552 Isolate::Current()->builtins()->StubNoRegisters_DebugBreak();
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001553 return result;
1554 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001555
1556 UNREACHABLE();
1557 return Handle<Code>::null();
1558}
1559
1560
1561// Simple function for returning the source positions for active break points.
1562Handle<Object> Debug::GetSourceBreakLocations(
1563 Handle<SharedFunctionInfo> shared) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00001564 Isolate* isolate = Isolate::Current();
1565 Heap* heap = isolate->heap();
1566 if (!HasDebugInfo(shared)) return Handle<Object>(heap->undefined_value());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001567 Handle<DebugInfo> debug_info = GetDebugInfo(shared);
1568 if (debug_info->GetBreakPointCount() == 0) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00001569 return Handle<Object>(heap->undefined_value());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001570 }
1571 Handle<FixedArray> locations =
lrn@chromium.org7516f052011-03-30 08:52:27 +00001572 isolate->factory()->NewFixedArray(debug_info->GetBreakPointCount());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001573 int count = 0;
1574 for (int i = 0; i < debug_info->break_points()->length(); i++) {
1575 if (!debug_info->break_points()->get(i)->IsUndefined()) {
1576 BreakPointInfo* break_point_info =
1577 BreakPointInfo::cast(debug_info->break_points()->get(i));
1578 if (break_point_info->GetBreakPointCount() > 0) {
1579 locations->set(count++, break_point_info->statement_position());
1580 }
1581 }
1582 }
1583 return locations;
1584}
1585
1586
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001587void Debug::NewBreak(StackFrame::Id break_frame_id) {
1588 thread_local_.break_frame_id_ = break_frame_id;
1589 thread_local_.break_id_ = ++thread_local_.break_count_;
1590}
1591
1592
1593void Debug::SetBreak(StackFrame::Id break_frame_id, int break_id) {
1594 thread_local_.break_frame_id_ = break_frame_id;
1595 thread_local_.break_id_ = break_id;
1596}
1597
1598
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001599// Handle stepping into a function.
1600void Debug::HandleStepIn(Handle<JSFunction> function,
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001601 Handle<Object> holder,
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001602 Address fp,
1603 bool is_constructor) {
1604 // If the frame pointer is not supplied by the caller find it.
1605 if (fp == 0) {
1606 StackFrameIterator it;
1607 it.Advance();
1608 // For constructor functions skip another frame.
1609 if (is_constructor) {
1610 ASSERT(it.frame()->is_construct());
1611 it.Advance();
1612 }
1613 fp = it.frame()->fp();
1614 }
1615
1616 // Flood the function with one-shot break points if it is called from where
1617 // step into was requested.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001618 if (fp == step_in_fp()) {
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001619 // Don't allow step into functions in the native context.
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001620 if (!function->IsBuiltin()) {
kasperl@chromium.orgacae3782009-04-11 09:17:08 +00001621 if (function->shared()->code() ==
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00001622 Isolate::Current()->builtins()->builtin(Builtins::kFunctionApply) ||
kasperl@chromium.orgacae3782009-04-11 09:17:08 +00001623 function->shared()->code() ==
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00001624 Isolate::Current()->builtins()->builtin(Builtins::kFunctionCall)) {
kasperl@chromium.orgacae3782009-04-11 09:17:08 +00001625 // Handle function.apply and function.call separately to flood the
1626 // function to be called and not the code for Builtins::FunctionApply or
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001627 // Builtins::FunctionCall. The receiver of call/apply is the target
1628 // function.
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001629 if (!holder.is_null() && holder->IsJSFunction() &&
1630 !JSFunction::cast(*holder)->IsBuiltin()) {
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001631 Handle<SharedFunctionInfo> shared_info(
1632 JSFunction::cast(*holder)->shared());
1633 Debug::FloodWithOneShot(shared_info);
kasperl@chromium.orgacae3782009-04-11 09:17:08 +00001634 }
1635 } else {
1636 Debug::FloodWithOneShot(Handle<SharedFunctionInfo>(function->shared()));
1637 }
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001638 }
1639 }
1640}
1641
1642
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001643void Debug::ClearStepping() {
1644 // Clear the various stepping setup.
1645 ClearOneShot();
1646 ClearStepIn();
ager@chromium.orga1645e22009-09-09 19:27:10 +00001647 ClearStepOut();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001648 ClearStepNext();
1649
1650 // Clear multiple step counter.
1651 thread_local_.step_count_ = 0;
1652}
1653
1654// Clears all the one-shot break points that are currently set. Normally this
1655// function is called each time a break point is hit as one shot break points
1656// are used to support stepping.
1657void Debug::ClearOneShot() {
1658 // The current implementation just runs through all the breakpoints. When the
v8.team.kasperl727e9952008-09-02 14:56:44 +00001659 // last break point for a function is removed that function is automatically
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001660 // removed from the list.
1661
1662 DebugInfoListNode* node = debug_info_list_;
1663 while (node != NULL) {
1664 BreakLocationIterator it(node->debug_info(), ALL_BREAK_LOCATIONS);
1665 while (!it.Done()) {
1666 it.ClearOneShot();
1667 it.Next();
1668 }
1669 node = node->next();
1670 }
1671}
1672
1673
1674void Debug::ActivateStepIn(StackFrame* frame) {
ager@chromium.orga1645e22009-09-09 19:27:10 +00001675 ASSERT(!StepOutActive());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001676 thread_local_.step_into_fp_ = frame->fp();
1677}
1678
1679
1680void Debug::ClearStepIn() {
1681 thread_local_.step_into_fp_ = 0;
1682}
1683
1684
ager@chromium.orga1645e22009-09-09 19:27:10 +00001685void Debug::ActivateStepOut(StackFrame* frame) {
1686 ASSERT(!StepInActive());
1687 thread_local_.step_out_fp_ = frame->fp();
1688}
1689
1690
1691void Debug::ClearStepOut() {
1692 thread_local_.step_out_fp_ = 0;
1693}
1694
1695
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001696void Debug::ClearStepNext() {
1697 thread_local_.last_step_action_ = StepNone;
ager@chromium.org236ad962008-09-25 09:45:57 +00001698 thread_local_.last_statement_position_ = RelocInfo::kNoPosition;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001699 thread_local_.last_fp_ = 0;
1700}
1701
1702
kasper.lundbd3ec4e2008-07-09 11:06:54 +00001703// Ensures the debug information is present for shared.
1704bool Debug::EnsureDebugInfo(Handle<SharedFunctionInfo> shared) {
1705 // Return if we already have the debug info for shared.
1706 if (HasDebugInfo(shared)) return true;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001707
kasper.lundbd3ec4e2008-07-09 11:06:54 +00001708 // Ensure shared in compiled. Return false if this failed.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001709 if (!EnsureCompiled(shared, CLEAR_EXCEPTION)) return false;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001710
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001711 // If preparing for the first break point make sure to deoptimize all
1712 // functions as debugging does not work with optimized code.
1713 if (!has_break_points_) {
1714 Deoptimizer::DeoptimizeAll();
1715 }
1716
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001717 // Create the debug info object.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001718 Handle<DebugInfo> debug_info = FACTORY->NewDebugInfo(shared);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001719
1720 // Add debug info to the list.
1721 DebugInfoListNode* node = new DebugInfoListNode(*debug_info);
1722 node->set_next(debug_info_list_);
1723 debug_info_list_ = node;
1724
1725 // Now there is at least one break point.
1726 has_break_points_ = true;
1727
kasper.lundbd3ec4e2008-07-09 11:06:54 +00001728 return true;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001729}
1730
1731
1732void Debug::RemoveDebugInfo(Handle<DebugInfo> debug_info) {
1733 ASSERT(debug_info_list_ != NULL);
1734 // Run through the debug info objects to find this one and remove it.
1735 DebugInfoListNode* prev = NULL;
1736 DebugInfoListNode* current = debug_info_list_;
1737 while (current != NULL) {
1738 if (*current->debug_info() == *debug_info) {
1739 // Unlink from list. If prev is NULL we are looking at the first element.
1740 if (prev == NULL) {
1741 debug_info_list_ = current->next();
1742 } else {
1743 prev->set_next(current->next());
1744 }
lrn@chromium.org7516f052011-03-30 08:52:27 +00001745 current->debug_info()->shared()->set_debug_info(
1746 isolate_->heap()->undefined_value());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001747 delete current;
1748
1749 // If there are no more debug info objects there are not more break
1750 // points.
1751 has_break_points_ = debug_info_list_ != NULL;
1752
1753 return;
1754 }
1755 // Move to next in list.
1756 prev = current;
1757 current = current->next();
1758 }
1759 UNREACHABLE();
1760}
1761
1762
1763void Debug::SetAfterBreakTarget(JavaScriptFrame* frame) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00001764 ASSERT(Isolate::Current() == isolate_);
1765 HandleScope scope(isolate_);
ager@chromium.org381abbb2009-02-25 13:23:22 +00001766
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001767 // Get the executing function in which the debug break occurred.
1768 Handle<SharedFunctionInfo> shared =
1769 Handle<SharedFunctionInfo>(JSFunction::cast(frame->function())->shared());
kasper.lundbd3ec4e2008-07-09 11:06:54 +00001770 if (!EnsureDebugInfo(shared)) {
1771 // Return if we failed to retrieve the debug info.
1772 return;
1773 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001774 Handle<DebugInfo> debug_info = GetDebugInfo(shared);
1775 Handle<Code> code(debug_info->code());
1776 Handle<Code> original_code(debug_info->original_code());
1777#ifdef DEBUG
1778 // Get the code which is actually executing.
vegorov@chromium.org74f333b2011-04-06 11:17:46 +00001779 Handle<Code> frame_code(frame->LookupCode());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001780 ASSERT(frame_code.is_identical_to(code));
1781#endif
1782
1783 // Find the call address in the running code. This address holds the call to
1784 // either a DebugBreakXXX or to the debug break return entry code if the
1785 // break point is still active after processing the break point.
ager@chromium.org4af710e2009-09-15 12:20:11 +00001786 Address addr = frame->pc() - Assembler::kCallTargetAddressOffset;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001787
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001788 // Check if the location is at JS exit or debug break slot.
ager@chromium.orga1645e22009-09-09 19:27:10 +00001789 bool at_js_return = false;
1790 bool break_at_js_return_active = false;
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001791 bool at_debug_break_slot = false;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001792 RelocIterator it(debug_info->code());
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001793 while (!it.done() && !at_js_return && !at_debug_break_slot) {
ager@chromium.org236ad962008-09-25 09:45:57 +00001794 if (RelocInfo::IsJSReturn(it.rinfo()->rmode())) {
ager@chromium.orga1645e22009-09-09 19:27:10 +00001795 at_js_return = (it.rinfo()->pc() ==
1796 addr - Assembler::kPatchReturnSequenceAddressOffset);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001797 break_at_js_return_active = it.rinfo()->IsPatchedReturnSequence();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001798 }
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001799 if (RelocInfo::IsDebugBreakSlot(it.rinfo()->rmode())) {
1800 at_debug_break_slot = (it.rinfo()->pc() ==
1801 addr - Assembler::kPatchDebugBreakSlotAddressOffset);
1802 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001803 it.next();
1804 }
1805
1806 // Handle the jump to continue execution after break point depending on the
1807 // break location.
ager@chromium.orga1645e22009-09-09 19:27:10 +00001808 if (at_js_return) {
1809 // If the break point as return is still active jump to the corresponding
1810 // place in the original code. If not the break point was removed during
1811 // break point processing.
1812 if (break_at_js_return_active) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001813 addr += original_code->instruction_start() - code->instruction_start();
1814 }
1815
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00001816 // Move back to where the call instruction sequence started.
1817 thread_local_.after_break_target_ =
1818 addr - Assembler::kPatchReturnSequenceAddressOffset;
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001819 } else if (at_debug_break_slot) {
1820 // Address of where the debug break slot starts.
1821 addr = addr - Assembler::kPatchDebugBreakSlotAddressOffset;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001822
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001823 // Continue just after the slot.
1824 thread_local_.after_break_target_ = addr + Assembler::kDebugBreakSlotLength;
1825 } else if (IsDebugBreak(Assembler::target_address_at(addr))) {
1826 // We now know that there is still a debug break call at the target address,
1827 // so the break point is still there and the original code will hold the
1828 // address to jump to in order to complete the call which is replaced by a
1829 // call to DebugBreakXXX.
1830
1831 // Find the corresponding address in the original code.
1832 addr += original_code->instruction_start() - code->instruction_start();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001833
1834 // Install jump to the call address in the original code. This will be the
1835 // call which was overwritten by the call to DebugBreakXXX.
1836 thread_local_.after_break_target_ = Assembler::target_address_at(addr);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001837 } else {
1838 // There is no longer a break point present. Don't try to look in the
1839 // original code as the running code will have the right address. This takes
1840 // care of the case where the last break point is removed from the function
1841 // and therefore no "original code" is available.
1842 thread_local_.after_break_target_ = Assembler::target_address_at(addr);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001843 }
1844}
1845
1846
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00001847bool Debug::IsBreakAtReturn(JavaScriptFrame* frame) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00001848 HandleScope scope(isolate_);
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00001849
1850 // Get the executing function in which the debug break occurred.
1851 Handle<SharedFunctionInfo> shared =
1852 Handle<SharedFunctionInfo>(JSFunction::cast(frame->function())->shared());
1853 if (!EnsureDebugInfo(shared)) {
1854 // Return if we failed to retrieve the debug info.
1855 return false;
1856 }
1857 Handle<DebugInfo> debug_info = GetDebugInfo(shared);
1858 Handle<Code> code(debug_info->code());
1859#ifdef DEBUG
1860 // Get the code which is actually executing.
vegorov@chromium.org74f333b2011-04-06 11:17:46 +00001861 Handle<Code> frame_code(frame->LookupCode());
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00001862 ASSERT(frame_code.is_identical_to(code));
1863#endif
1864
1865 // Find the call address in the running code.
1866 Address addr = frame->pc() - Assembler::kCallTargetAddressOffset;
1867
1868 // Check if the location is at JS return.
1869 RelocIterator it(debug_info->code());
1870 while (!it.done()) {
1871 if (RelocInfo::IsJSReturn(it.rinfo()->rmode())) {
1872 return (it.rinfo()->pc() ==
1873 addr - Assembler::kPatchReturnSequenceAddressOffset);
1874 }
1875 it.next();
1876 }
1877 return false;
1878}
1879
1880
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001881void Debug::FramesHaveBeenDropped(StackFrame::Id new_break_frame_id,
whesse@chromium.orge90029b2010-08-02 11:52:17 +00001882 FrameDropMode mode,
1883 Object** restarter_frame_function_pointer) {
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001884 thread_local_.frame_drop_mode_ = mode;
ager@chromium.org357bf652010-04-12 11:30:10 +00001885 thread_local_.break_frame_id_ = new_break_frame_id;
whesse@chromium.orge90029b2010-08-02 11:52:17 +00001886 thread_local_.restarter_frame_function_pointer_ =
1887 restarter_frame_function_pointer;
ager@chromium.org357bf652010-04-12 11:30:10 +00001888}
1889
1890
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001891bool Debug::IsDebugGlobal(GlobalObject* global) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001892 return IsLoaded() && global == debug_context()->global();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001893}
1894
1895
ager@chromium.org32912102009-01-16 10:38:43 +00001896void Debug::ClearMirrorCache() {
lrn@chromium.org7516f052011-03-30 08:52:27 +00001897 ASSERT(Isolate::Current() == isolate_);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001898 PostponeInterruptsScope postpone(isolate_);
lrn@chromium.org7516f052011-03-30 08:52:27 +00001899 HandleScope scope(isolate_);
1900 ASSERT(isolate_->context() == *Debug::debug_context());
ager@chromium.org32912102009-01-16 10:38:43 +00001901
1902 // Clear the mirror cache.
1903 Handle<String> function_name =
lrn@chromium.org7516f052011-03-30 08:52:27 +00001904 isolate_->factory()->LookupSymbol(CStrVector("ClearMirrorCache"));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001905 Handle<Object> fun(Isolate::Current()->global()->GetPropertyNoExceptionThrown(
lrn@chromium.org303ada72010-10-27 09:33:13 +00001906 *function_name));
ager@chromium.org32912102009-01-16 10:38:43 +00001907 ASSERT(fun->IsJSFunction());
1908 bool caught_exception;
1909 Handle<Object> js_object = Execution::TryCall(
1910 Handle<JSFunction>::cast(fun),
1911 Handle<JSObject>(Debug::debug_context()->global()),
1912 0, NULL, &caught_exception);
1913}
1914
1915
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001916void Debug::CreateScriptCache() {
lrn@chromium.org7516f052011-03-30 08:52:27 +00001917 ASSERT(Isolate::Current() == isolate_);
1918 Heap* heap = isolate_->heap();
1919 HandleScope scope(isolate_);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001920
1921 // Perform two GCs to get rid of all unreferenced scripts. The first GC gets
1922 // rid of all the cached script wrappers and the second gets rid of the
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001923 // scripts which are no longer referenced.
lrn@chromium.org7516f052011-03-30 08:52:27 +00001924 heap->CollectAllGarbage(false);
1925 heap->CollectAllGarbage(false);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001926
1927 ASSERT(script_cache_ == NULL);
1928 script_cache_ = new ScriptCache();
1929
1930 // Scan heap for Script objects.
1931 int count = 0;
1932 HeapIterator iterator;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001933 for (HeapObject* obj = iterator.next(); obj != NULL; obj = iterator.next()) {
sgjesse@chromium.org152a0b02009-10-07 13:50:16 +00001934 if (obj->IsScript() && Script::cast(obj)->HasValidSource()) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001935 script_cache_->Add(Handle<Script>(Script::cast(obj)));
1936 count++;
1937 }
1938 }
1939}
1940
1941
1942void Debug::DestroyScriptCache() {
1943 // Get rid of the script cache if it was created.
1944 if (script_cache_ != NULL) {
1945 delete script_cache_;
1946 script_cache_ = NULL;
1947 }
1948}
1949
1950
1951void Debug::AddScriptToScriptCache(Handle<Script> script) {
1952 if (script_cache_ != NULL) {
1953 script_cache_->Add(script);
1954 }
1955}
1956
1957
1958Handle<FixedArray> Debug::GetLoadedScripts() {
lrn@chromium.org7516f052011-03-30 08:52:27 +00001959 ASSERT(Isolate::Current() == isolate_);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001960 // Create and fill the script cache when the loaded scripts is requested for
1961 // the first time.
1962 if (script_cache_ == NULL) {
1963 CreateScriptCache();
1964 }
1965
1966 // If the script cache is not active just return an empty array.
1967 ASSERT(script_cache_ != NULL);
1968 if (script_cache_ == NULL) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00001969 isolate_->factory()->NewFixedArray(0);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001970 }
1971
1972 // Perform GC to get unreferenced scripts evicted from the cache before
1973 // returning the content.
lrn@chromium.org7516f052011-03-30 08:52:27 +00001974 isolate_->heap()->CollectAllGarbage(false);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001975
1976 // Get the scripts from the cache.
1977 return script_cache_->GetScripts();
1978}
1979
1980
1981void Debug::AfterGarbageCollection() {
1982 // Generate events for collected scripts.
1983 if (script_cache_ != NULL) {
1984 script_cache_->ProcessCollectedScripts();
1985 }
1986}
1987
1988
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001989Debugger::Debugger()
1990 : debugger_access_(OS::CreateMutex()),
1991 event_listener_(Handle<Object>()),
1992 event_listener_data_(Handle<Object>()),
1993 compiling_natives_(false),
1994 is_loading_debugger_(false),
1995 never_unload_debugger_(false),
1996 message_handler_(NULL),
1997 debugger_unload_pending_(false),
1998 host_dispatch_handler_(NULL),
1999 dispatch_handler_access_(OS::CreateMutex()),
2000 debug_message_dispatch_handler_(NULL),
2001 message_dispatch_helper_thread_(NULL),
2002 host_dispatch_micros_(100 * 1000),
2003 agent_(NULL),
2004 command_queue_(kQueueInitialSize),
2005 command_received_(OS::CreateSemaphore(0)),
2006 event_command_queue_(kQueueInitialSize) {
2007}
2008
2009
2010Debugger::~Debugger() {
2011 delete debugger_access_;
2012 debugger_access_ = 0;
2013 delete dispatch_handler_access_;
2014 dispatch_handler_access_ = 0;
2015 delete command_received_;
2016 command_received_ = 0;
2017}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002018
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002019
2020Handle<Object> Debugger::MakeJSObject(Vector<const char> constructor_name,
2021 int argc, Object*** argv,
2022 bool* caught_exception) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002023 ASSERT(Isolate::Current() == isolate_);
2024 ASSERT(isolate_->context() == *isolate_->debug()->debug_context());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002025
2026 // Create the execution state object.
lrn@chromium.org7516f052011-03-30 08:52:27 +00002027 Handle<String> constructor_str =
2028 isolate_->factory()->LookupSymbol(constructor_name);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002029 Handle<Object> constructor(
2030 isolate_->global()->GetPropertyNoExceptionThrown(*constructor_str));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002031 ASSERT(constructor->IsJSFunction());
2032 if (!constructor->IsJSFunction()) {
2033 *caught_exception = true;
lrn@chromium.org7516f052011-03-30 08:52:27 +00002034 return isolate_->factory()->undefined_value();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002035 }
2036 Handle<Object> js_object = Execution::TryCall(
2037 Handle<JSFunction>::cast(constructor),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002038 Handle<JSObject>(isolate_->debug()->debug_context()->global()),
2039 argc, argv, caught_exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002040 return js_object;
2041}
2042
2043
2044Handle<Object> Debugger::MakeExecutionState(bool* caught_exception) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002045 ASSERT(Isolate::Current() == isolate_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002046 // Create the execution state object.
lrn@chromium.org7516f052011-03-30 08:52:27 +00002047 Handle<Object> break_id = isolate_->factory()->NewNumberFromInt(
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002048 isolate_->debug()->break_id());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002049 const int argc = 1;
2050 Object** argv[argc] = { break_id.location() };
2051 return MakeJSObject(CStrVector("MakeExecutionState"),
2052 argc, argv, caught_exception);
2053}
2054
2055
2056Handle<Object> Debugger::MakeBreakEvent(Handle<Object> exec_state,
2057 Handle<Object> break_points_hit,
2058 bool* caught_exception) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002059 ASSERT(Isolate::Current() == isolate_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002060 // Create the new break event object.
2061 const int argc = 2;
2062 Object** argv[argc] = { exec_state.location(),
2063 break_points_hit.location() };
2064 return MakeJSObject(CStrVector("MakeBreakEvent"),
2065 argc,
2066 argv,
2067 caught_exception);
2068}
2069
2070
2071Handle<Object> Debugger::MakeExceptionEvent(Handle<Object> exec_state,
2072 Handle<Object> exception,
2073 bool uncaught,
2074 bool* caught_exception) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002075 ASSERT(Isolate::Current() == isolate_);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002076 Factory* factory = isolate_->factory();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002077 // Create the new exception event object.
2078 const int argc = 3;
2079 Object** argv[argc] = { exec_state.location(),
2080 exception.location(),
lrn@chromium.org7516f052011-03-30 08:52:27 +00002081 uncaught ? factory->true_value().location() :
2082 factory->false_value().location()};
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002083 return MakeJSObject(CStrVector("MakeExceptionEvent"),
2084 argc, argv, caught_exception);
2085}
2086
2087
2088Handle<Object> Debugger::MakeNewFunctionEvent(Handle<Object> function,
2089 bool* caught_exception) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002090 ASSERT(Isolate::Current() == isolate_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002091 // Create the new function event object.
2092 const int argc = 1;
2093 Object** argv[argc] = { function.location() };
2094 return MakeJSObject(CStrVector("MakeNewFunctionEvent"),
2095 argc, argv, caught_exception);
2096}
2097
2098
2099Handle<Object> Debugger::MakeCompileEvent(Handle<Script> script,
iposva@chromium.org245aa852009-02-10 00:49:54 +00002100 bool before,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002101 bool* caught_exception) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002102 ASSERT(Isolate::Current() == isolate_);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002103 Factory* factory = isolate_->factory();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002104 // Create the compile event object.
2105 Handle<Object> exec_state = MakeExecutionState(caught_exception);
iposva@chromium.org245aa852009-02-10 00:49:54 +00002106 Handle<Object> script_wrapper = GetScriptWrapper(script);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002107 const int argc = 3;
iposva@chromium.org245aa852009-02-10 00:49:54 +00002108 Object** argv[argc] = { exec_state.location(),
2109 script_wrapper.location(),
lrn@chromium.org7516f052011-03-30 08:52:27 +00002110 before ? factory->true_value().location() :
2111 factory->false_value().location() };
iposva@chromium.org245aa852009-02-10 00:49:54 +00002112
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002113 return MakeJSObject(CStrVector("MakeCompileEvent"),
2114 argc,
2115 argv,
2116 caught_exception);
2117}
2118
2119
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002120Handle<Object> Debugger::MakeScriptCollectedEvent(int id,
2121 bool* caught_exception) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002122 ASSERT(Isolate::Current() == isolate_);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002123 // Create the script collected event object.
2124 Handle<Object> exec_state = MakeExecutionState(caught_exception);
2125 Handle<Object> id_object = Handle<Smi>(Smi::FromInt(id));
2126 const int argc = 2;
2127 Object** argv[argc] = { exec_state.location(), id_object.location() };
2128
2129 return MakeJSObject(CStrVector("MakeScriptCollectedEvent"),
2130 argc,
2131 argv,
2132 caught_exception);
2133}
2134
2135
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002136void Debugger::OnException(Handle<Object> exception, bool uncaught) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002137 ASSERT(Isolate::Current() == isolate_);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002138 HandleScope scope(isolate_);
2139 Debug* debug = isolate_->debug();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002140
2141 // Bail out based on state or if there is no listener for this event
lrn@chromium.org7516f052011-03-30 08:52:27 +00002142 if (debug->InDebugger()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002143 if (!Debugger::EventActive(v8::Exception)) return;
2144
2145 // Bail out if exception breaks are not active
2146 if (uncaught) {
2147 // Uncaught exceptions are reported by either flags.
lrn@chromium.org7516f052011-03-30 08:52:27 +00002148 if (!(debug->break_on_uncaught_exception() ||
2149 debug->break_on_exception())) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002150 } else {
2151 // Caught exceptions are reported is activated.
lrn@chromium.org7516f052011-03-30 08:52:27 +00002152 if (!debug->break_on_exception()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002153 }
2154
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002155 // Enter the debugger.
2156 EnterDebugger debugger;
2157 if (debugger.FailedToEnter()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002158
2159 // Clear all current stepping setup.
lrn@chromium.org7516f052011-03-30 08:52:27 +00002160 debug->ClearStepping();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002161 // Create the event data object.
2162 bool caught_exception = false;
2163 Handle<Object> exec_state = MakeExecutionState(&caught_exception);
2164 Handle<Object> event_data;
2165 if (!caught_exception) {
2166 event_data = MakeExceptionEvent(exec_state, exception, uncaught,
2167 &caught_exception);
2168 }
2169 // Bail out and don't call debugger if exception.
2170 if (caught_exception) {
2171 return;
2172 }
2173
ager@chromium.org5ec48922009-05-05 07:25:34 +00002174 // Process debug event.
2175 ProcessDebugEvent(v8::Exception, Handle<JSObject>::cast(event_data), false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002176 // Return to continue execution from where the exception was thrown.
2177}
2178
2179
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002180void Debugger::OnDebugBreak(Handle<Object> break_points_hit,
2181 bool auto_continue) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002182 ASSERT(Isolate::Current() == isolate_);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002183 HandleScope scope(isolate_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002184
kasper.lund212ac232008-07-16 07:07:30 +00002185 // Debugger has already been entered by caller.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002186 ASSERT(isolate_->context() == *isolate_->debug()->debug_context());
kasper.lund212ac232008-07-16 07:07:30 +00002187
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002188 // Bail out if there is no listener for this event
2189 if (!Debugger::EventActive(v8::Break)) return;
2190
2191 // Debugger must be entered in advance.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002192 ASSERT(Isolate::Current()->context() == *isolate_->debug()->debug_context());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002193
2194 // Create the event data object.
2195 bool caught_exception = false;
2196 Handle<Object> exec_state = MakeExecutionState(&caught_exception);
2197 Handle<Object> event_data;
2198 if (!caught_exception) {
2199 event_data = MakeBreakEvent(exec_state, break_points_hit,
2200 &caught_exception);
2201 }
2202 // Bail out and don't call debugger if exception.
2203 if (caught_exception) {
2204 return;
2205 }
2206
ager@chromium.org5ec48922009-05-05 07:25:34 +00002207 // Process debug event.
2208 ProcessDebugEvent(v8::Break,
2209 Handle<JSObject>::cast(event_data),
2210 auto_continue);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002211}
2212
2213
2214void Debugger::OnBeforeCompile(Handle<Script> script) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002215 ASSERT(Isolate::Current() == isolate_);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002216 HandleScope scope(isolate_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002217
2218 // Bail out based on state or if there is no listener for this event
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002219 if (isolate_->debug()->InDebugger()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002220 if (compiling_natives()) return;
2221 if (!EventActive(v8::BeforeCompile)) return;
2222
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002223 // Enter the debugger.
2224 EnterDebugger debugger;
2225 if (debugger.FailedToEnter()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002226
2227 // Create the event data object.
2228 bool caught_exception = false;
iposva@chromium.org245aa852009-02-10 00:49:54 +00002229 Handle<Object> event_data = MakeCompileEvent(script, true, &caught_exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002230 // Bail out and don't call debugger if exception.
2231 if (caught_exception) {
2232 return;
2233 }
2234
ager@chromium.org5ec48922009-05-05 07:25:34 +00002235 // Process debug event.
2236 ProcessDebugEvent(v8::BeforeCompile,
2237 Handle<JSObject>::cast(event_data),
2238 true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002239}
2240
2241
2242// Handle debugger actions when a new script is compiled.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00002243void Debugger::OnAfterCompile(Handle<Script> script,
2244 AfterCompileFlags after_compile_flags) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002245 ASSERT(Isolate::Current() == isolate_);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002246 HandleScope scope(isolate_);
2247 Debug* debug = isolate_->debug();
kasper.lund212ac232008-07-16 07:07:30 +00002248
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002249 // Add the newly compiled script to the script cache.
lrn@chromium.org7516f052011-03-30 08:52:27 +00002250 debug->AddScriptToScriptCache(script);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002251
2252 // No more to do if not debugging.
ager@chromium.org71daaf62009-04-01 07:22:49 +00002253 if (!IsDebuggerActive()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002254
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002255 // No compile events while compiling natives.
2256 if (compiling_natives()) return;
2257
iposva@chromium.org245aa852009-02-10 00:49:54 +00002258 // Store whether in debugger before entering debugger.
lrn@chromium.org7516f052011-03-30 08:52:27 +00002259 bool in_debugger = debug->InDebugger();
iposva@chromium.org245aa852009-02-10 00:49:54 +00002260
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002261 // Enter the debugger.
2262 EnterDebugger debugger;
2263 if (debugger.FailedToEnter()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002264
2265 // If debugging there might be script break points registered for this
2266 // script. Make sure that these break points are set.
2267
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002268 // Get the function UpdateScriptBreakPoints (defined in debug-debugger.js).
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002269 Handle<String> update_script_break_points_symbol =
lrn@chromium.org7516f052011-03-30 08:52:27 +00002270 isolate_->factory()->LookupAsciiSymbol("UpdateScriptBreakPoints");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002271 Handle<Object> update_script_break_points =
lrn@chromium.org7516f052011-03-30 08:52:27 +00002272 Handle<Object>(debug->debug_context()->global()->
lrn@chromium.org303ada72010-10-27 09:33:13 +00002273 GetPropertyNoExceptionThrown(*update_script_break_points_symbol));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002274 if (!update_script_break_points->IsJSFunction()) {
2275 return;
2276 }
2277 ASSERT(update_script_break_points->IsJSFunction());
2278
2279 // Wrap the script object in a proper JS object before passing it
2280 // to JavaScript.
2281 Handle<JSValue> wrapper = GetScriptWrapper(script);
2282
2283 // Call UpdateScriptBreakPoints expect no exceptions.
kasper.lund212ac232008-07-16 07:07:30 +00002284 bool caught_exception = false;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002285 const int argc = 1;
2286 Object** argv[argc] = { reinterpret_cast<Object**>(wrapper.location()) };
2287 Handle<Object> result = Execution::TryCall(
2288 Handle<JSFunction>::cast(update_script_break_points),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002289 Isolate::Current()->js_builtins_object(), argc, argv,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002290 &caught_exception);
2291 if (caught_exception) {
2292 return;
2293 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002294 // Bail out based on state or if there is no listener for this event
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00002295 if (in_debugger && (after_compile_flags & SEND_WHEN_DEBUGGING) == 0) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002296 if (!Debugger::EventActive(v8::AfterCompile)) return;
2297
2298 // Create the compile state object.
2299 Handle<Object> event_data = MakeCompileEvent(script,
iposva@chromium.org245aa852009-02-10 00:49:54 +00002300 false,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002301 &caught_exception);
2302 // Bail out and don't call debugger if exception.
2303 if (caught_exception) {
2304 return;
2305 }
ager@chromium.org5ec48922009-05-05 07:25:34 +00002306 // Process debug event.
2307 ProcessDebugEvent(v8::AfterCompile,
2308 Handle<JSObject>::cast(event_data),
2309 true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002310}
2311
2312
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002313void Debugger::OnScriptCollected(int id) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002314 ASSERT(Isolate::Current() == isolate_);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002315 HandleScope scope(isolate_);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002316
2317 // No more to do if not debugging.
2318 if (!IsDebuggerActive()) return;
2319 if (!Debugger::EventActive(v8::ScriptCollected)) return;
2320
2321 // Enter the debugger.
2322 EnterDebugger debugger;
2323 if (debugger.FailedToEnter()) return;
2324
2325 // Create the script collected state object.
2326 bool caught_exception = false;
2327 Handle<Object> event_data = MakeScriptCollectedEvent(id,
2328 &caught_exception);
2329 // Bail out and don't call debugger if exception.
2330 if (caught_exception) {
2331 return;
2332 }
2333
2334 // Process debug event.
2335 ProcessDebugEvent(v8::ScriptCollected,
2336 Handle<JSObject>::cast(event_data),
2337 true);
2338}
2339
2340
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002341void Debugger::ProcessDebugEvent(v8::DebugEvent event,
ager@chromium.org5ec48922009-05-05 07:25:34 +00002342 Handle<JSObject> event_data,
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002343 bool auto_continue) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002344 ASSERT(Isolate::Current() == isolate_);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002345 HandleScope scope(isolate_);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002346
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +00002347 // Clear any pending debug break if this is a real break.
2348 if (!auto_continue) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002349 isolate_->debug()->clear_interrupt_pending(DEBUGBREAK);
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +00002350 }
2351
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002352 // Create the execution state.
2353 bool caught_exception = false;
2354 Handle<Object> exec_state = MakeExecutionState(&caught_exception);
2355 if (caught_exception) {
2356 return;
2357 }
ager@chromium.org41826e72009-03-30 13:30:57 +00002358 // First notify the message handler if any.
2359 if (message_handler_ != NULL) {
ager@chromium.org5ec48922009-05-05 07:25:34 +00002360 NotifyMessageHandler(event,
2361 Handle<JSObject>::cast(exec_state),
2362 event_data,
2363 auto_continue);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002364 }
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00002365 // Notify registered debug event listener. This can be either a C or
2366 // a JavaScript function. Don't call event listener for v8::Break
2367 // here, if it's only a debug command -- they will be processed later.
2368 if ((event != v8::Break || !auto_continue) && !event_listener_.is_null()) {
2369 CallEventCallback(event, exec_state, event_data, NULL);
2370 }
2371 // Process pending debug commands.
2372 if (event == v8::Break) {
2373 while (!event_command_queue_.IsEmpty()) {
2374 CommandMessage command = event_command_queue_.Get();
2375 if (!event_listener_.is_null()) {
2376 CallEventCallback(v8::BreakForCommand,
2377 exec_state,
2378 event_data,
2379 command.client_data());
2380 }
2381 command.Dispose();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002382 }
2383 }
2384}
2385
2386
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00002387void Debugger::CallEventCallback(v8::DebugEvent event,
2388 Handle<Object> exec_state,
2389 Handle<Object> event_data,
2390 v8::Debug::ClientData* client_data) {
2391 if (event_listener_->IsProxy()) {
2392 CallCEventCallback(event, exec_state, event_data, client_data);
2393 } else {
2394 CallJSEventCallback(event, exec_state, event_data);
2395 }
2396}
2397
2398
2399void Debugger::CallCEventCallback(v8::DebugEvent event,
2400 Handle<Object> exec_state,
2401 Handle<Object> event_data,
2402 v8::Debug::ClientData* client_data) {
2403 Handle<Proxy> callback_obj(Handle<Proxy>::cast(event_listener_));
2404 v8::Debug::EventCallback2 callback =
2405 FUNCTION_CAST<v8::Debug::EventCallback2>(callback_obj->proxy());
2406 EventDetailsImpl event_details(
2407 event,
2408 Handle<JSObject>::cast(exec_state),
2409 Handle<JSObject>::cast(event_data),
2410 event_listener_data_,
2411 client_data);
2412 callback(event_details);
2413}
2414
2415
2416void Debugger::CallJSEventCallback(v8::DebugEvent event,
2417 Handle<Object> exec_state,
2418 Handle<Object> event_data) {
2419 ASSERT(event_listener_->IsJSFunction());
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002420 ASSERT(Isolate::Current() == isolate_);
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00002421 Handle<JSFunction> fun(Handle<JSFunction>::cast(event_listener_));
2422
2423 // Invoke the JavaScript debug event listener.
2424 const int argc = 4;
2425 Object** argv[argc] = { Handle<Object>(Smi::FromInt(event)).location(),
2426 exec_state.location(),
2427 Handle<Object>::cast(event_data).location(),
2428 event_listener_data_.location() };
2429 bool caught_exception = false;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002430 Execution::TryCall(fun, isolate_->global(), argc, argv, &caught_exception);
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00002431 // Silently ignore exceptions from debug event listeners.
2432}
2433
2434
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00002435Handle<Context> Debugger::GetDebugContext() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002436 ASSERT(Isolate::Current() == isolate_);
2437 never_unload_debugger_ = true;
2438 EnterDebugger debugger;
2439 return isolate_->debug()->debug_context();
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00002440}
2441
2442
ager@chromium.org71daaf62009-04-01 07:22:49 +00002443void Debugger::UnloadDebugger() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002444 ASSERT(Isolate::Current() == isolate_);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002445 Debug* debug = isolate_->debug();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002446
ager@chromium.org71daaf62009-04-01 07:22:49 +00002447 // Make sure that there are no breakpoints left.
lrn@chromium.org7516f052011-03-30 08:52:27 +00002448 debug->ClearAllBreakPoints();
ager@chromium.org71daaf62009-04-01 07:22:49 +00002449
2450 // Unload the debugger if feasible.
2451 if (!never_unload_debugger_) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002452 debug->Unload();
ager@chromium.org71daaf62009-04-01 07:22:49 +00002453 }
2454
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002455 // Clear the flag indicating that the debugger should be unloaded.
2456 debugger_unload_pending_ = false;
ager@chromium.org71daaf62009-04-01 07:22:49 +00002457}
2458
2459
ager@chromium.org41826e72009-03-30 13:30:57 +00002460void Debugger::NotifyMessageHandler(v8::DebugEvent event,
ager@chromium.org5ec48922009-05-05 07:25:34 +00002461 Handle<JSObject> exec_state,
2462 Handle<JSObject> event_data,
ager@chromium.org41826e72009-03-30 13:30:57 +00002463 bool auto_continue) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002464 ASSERT(Isolate::Current() == isolate_);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002465 HandleScope scope(isolate_);
ager@chromium.org41826e72009-03-30 13:30:57 +00002466
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002467 if (!isolate_->debug()->Load()) return;
ager@chromium.org41826e72009-03-30 13:30:57 +00002468
2469 // Process the individual events.
ager@chromium.org5ec48922009-05-05 07:25:34 +00002470 bool sendEventMessage = false;
ager@chromium.org41826e72009-03-30 13:30:57 +00002471 switch (event) {
2472 case v8::Break:
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00002473 case v8::BreakForCommand:
ager@chromium.org5ec48922009-05-05 07:25:34 +00002474 sendEventMessage = !auto_continue;
ager@chromium.org41826e72009-03-30 13:30:57 +00002475 break;
2476 case v8::Exception:
ager@chromium.org5ec48922009-05-05 07:25:34 +00002477 sendEventMessage = true;
ager@chromium.org41826e72009-03-30 13:30:57 +00002478 break;
2479 case v8::BeforeCompile:
2480 break;
2481 case v8::AfterCompile:
ager@chromium.org5ec48922009-05-05 07:25:34 +00002482 sendEventMessage = true;
ager@chromium.org41826e72009-03-30 13:30:57 +00002483 break;
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002484 case v8::ScriptCollected:
2485 sendEventMessage = true;
2486 break;
ager@chromium.org41826e72009-03-30 13:30:57 +00002487 case v8::NewFunction:
2488 break;
2489 default:
2490 UNREACHABLE();
2491 }
2492
ager@chromium.org5ec48922009-05-05 07:25:34 +00002493 // The debug command interrupt flag might have been set when the command was
2494 // added. It should be enough to clear the flag only once while we are in the
2495 // debugger.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002496 ASSERT(isolate_->debug()->InDebugger());
2497 isolate_->stack_guard()->Continue(DEBUGCOMMAND);
ager@chromium.org5ec48922009-05-05 07:25:34 +00002498
2499 // Notify the debugger that a debug event has occurred unless auto continue is
2500 // active in which case no event is send.
2501 if (sendEventMessage) {
2502 MessageImpl message = MessageImpl::NewEvent(
2503 event,
2504 auto_continue,
2505 Handle<JSObject>::cast(exec_state),
2506 Handle<JSObject>::cast(event_data));
2507 InvokeMessageHandler(message);
2508 }
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +00002509
2510 // If auto continue don't make the event cause a break, but process messages
2511 // in the queue if any. For script collected events don't even process
2512 // messages in the queue as the execution state might not be what is expected
2513 // by the client.
ager@chromium.org6ffc2172009-05-29 19:20:16 +00002514 if ((auto_continue && !HasCommands()) || event == v8::ScriptCollected) {
ager@chromium.org5ec48922009-05-05 07:25:34 +00002515 return;
2516 }
ager@chromium.org41826e72009-03-30 13:30:57 +00002517
ager@chromium.org41826e72009-03-30 13:30:57 +00002518 v8::TryCatch try_catch;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002519
2520 // DebugCommandProcessor goes here.
2521 v8::Local<v8::Object> cmd_processor;
2522 {
2523 v8::Local<v8::Object> api_exec_state =
2524 v8::Utils::ToLocal(Handle<JSObject>::cast(exec_state));
2525 v8::Local<v8::String> fun_name =
2526 v8::String::New("debugCommandProcessor");
2527 v8::Local<v8::Function> fun =
2528 v8::Function::Cast(*api_exec_state->Get(fun_name));
2529
2530 v8::Handle<v8::Boolean> running =
2531 auto_continue ? v8::True() : v8::False();
2532 static const int kArgc = 1;
2533 v8::Handle<Value> argv[kArgc] = { running };
2534 cmd_processor = v8::Object::Cast(*fun->Call(api_exec_state, kArgc, argv));
2535 if (try_catch.HasCaught()) {
2536 PrintLn(try_catch.Exception());
2537 return;
2538 }
ager@chromium.org41826e72009-03-30 13:30:57 +00002539 }
2540
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002541 bool running = auto_continue;
2542
ager@chromium.org41826e72009-03-30 13:30:57 +00002543 // Process requests from the debugger.
2544 while (true) {
2545 // Wait for new command in the queue.
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002546 if (Debugger::host_dispatch_handler_) {
2547 // In case there is a host dispatch - do periodic dispatches.
2548 if (!command_received_->Wait(host_dispatch_micros_)) {
2549 // Timout expired, do the dispatch.
2550 Debugger::host_dispatch_handler_();
2551 continue;
2552 }
2553 } else {
2554 // In case there is no host dispatch - just wait.
2555 command_received_->Wait();
2556 }
ager@chromium.org41826e72009-03-30 13:30:57 +00002557
ager@chromium.org41826e72009-03-30 13:30:57 +00002558 // Get the command from the queue.
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00002559 CommandMessage command = command_queue_.Get();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002560 LOGGER->DebugTag("Got request from command queue, in interactive loop.");
ager@chromium.org71daaf62009-04-01 07:22:49 +00002561 if (!Debugger::IsDebuggerActive()) {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002562 // Delete command text and user data.
2563 command.Dispose();
ager@chromium.org41826e72009-03-30 13:30:57 +00002564 return;
2565 }
2566
ager@chromium.org41826e72009-03-30 13:30:57 +00002567 // Invoke JavaScript to process the debug request.
2568 v8::Local<v8::String> fun_name;
2569 v8::Local<v8::Function> fun;
2570 v8::Local<v8::Value> request;
2571 v8::TryCatch try_catch;
2572 fun_name = v8::String::New("processDebugRequest");
2573 fun = v8::Function::Cast(*cmd_processor->Get(fun_name));
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002574
2575 request = v8::String::New(command.text().start(),
2576 command.text().length());
ager@chromium.org41826e72009-03-30 13:30:57 +00002577 static const int kArgc = 1;
2578 v8::Handle<Value> argv[kArgc] = { request };
2579 v8::Local<v8::Value> response_val = fun->Call(cmd_processor, kArgc, argv);
2580
2581 // Get the response.
2582 v8::Local<v8::String> response;
ager@chromium.org41826e72009-03-30 13:30:57 +00002583 if (!try_catch.HasCaught()) {
2584 // Get response string.
2585 if (!response_val->IsUndefined()) {
2586 response = v8::String::Cast(*response_val);
2587 } else {
2588 response = v8::String::New("");
2589 }
2590
2591 // Log the JSON request/response.
2592 if (FLAG_trace_debug_json) {
2593 PrintLn(request);
2594 PrintLn(response);
2595 }
2596
2597 // Get the running state.
2598 fun_name = v8::String::New("isRunning");
2599 fun = v8::Function::Cast(*cmd_processor->Get(fun_name));
2600 static const int kArgc = 1;
2601 v8::Handle<Value> argv[kArgc] = { response };
2602 v8::Local<v8::Value> running_val = fun->Call(cmd_processor, kArgc, argv);
2603 if (!try_catch.HasCaught()) {
2604 running = running_val->ToBoolean()->Value();
2605 }
2606 } else {
2607 // In case of failure the result text is the exception text.
2608 response = try_catch.Exception()->ToString();
2609 }
2610
ager@chromium.org41826e72009-03-30 13:30:57 +00002611 // Return the result.
ager@chromium.org5ec48922009-05-05 07:25:34 +00002612 MessageImpl message = MessageImpl::NewResponse(
2613 event,
2614 running,
2615 Handle<JSObject>::cast(exec_state),
2616 Handle<JSObject>::cast(event_data),
2617 Handle<String>(Utils::OpenHandle(*response)),
2618 command.client_data());
2619 InvokeMessageHandler(message);
2620 command.Dispose();
ager@chromium.org41826e72009-03-30 13:30:57 +00002621
2622 // Return from debug event processing if either the VM is put into the
2623 // runnning state (through a continue command) or auto continue is active
2624 // and there are no more commands queued.
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002625 if (running && !HasCommands()) {
ager@chromium.org41826e72009-03-30 13:30:57 +00002626 return;
2627 }
2628 }
2629}
2630
2631
iposva@chromium.org245aa852009-02-10 00:49:54 +00002632void Debugger::SetEventListener(Handle<Object> callback,
2633 Handle<Object> data) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002634 ASSERT(Isolate::Current() == isolate_);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002635 HandleScope scope(isolate_);
2636 GlobalHandles* global_handles = isolate_->global_handles();
iposva@chromium.org245aa852009-02-10 00:49:54 +00002637
2638 // Clear the global handles for the event listener and the event listener data
2639 // object.
2640 if (!event_listener_.is_null()) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002641 global_handles->Destroy(
iposva@chromium.org245aa852009-02-10 00:49:54 +00002642 reinterpret_cast<Object**>(event_listener_.location()));
2643 event_listener_ = Handle<Object>();
2644 }
2645 if (!event_listener_data_.is_null()) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002646 global_handles->Destroy(
iposva@chromium.org245aa852009-02-10 00:49:54 +00002647 reinterpret_cast<Object**>(event_listener_data_.location()));
2648 event_listener_data_ = Handle<Object>();
2649 }
2650
2651 // If there is a new debug event listener register it together with its data
2652 // object.
2653 if (!callback->IsUndefined() && !callback->IsNull()) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002654 event_listener_ = Handle<Object>::cast(
lrn@chromium.org7516f052011-03-30 08:52:27 +00002655 global_handles->Create(*callback));
iposva@chromium.org245aa852009-02-10 00:49:54 +00002656 if (data.is_null()) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002657 data = isolate_->factory()->undefined_value();
iposva@chromium.org245aa852009-02-10 00:49:54 +00002658 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002659 event_listener_data_ = Handle<Object>::cast(
lrn@chromium.org7516f052011-03-30 08:52:27 +00002660 global_handles->Create(*data));
iposva@chromium.org245aa852009-02-10 00:49:54 +00002661 }
2662
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002663 ListenersChanged();
iposva@chromium.org245aa852009-02-10 00:49:54 +00002664}
2665
2666
ager@chromium.org5ec48922009-05-05 07:25:34 +00002667void Debugger::SetMessageHandler(v8::Debug::MessageHandler2 handler) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002668 ASSERT(Isolate::Current() == isolate_);
ager@chromium.org71daaf62009-04-01 07:22:49 +00002669 ScopedLock with(debugger_access_);
2670
ager@chromium.org381abbb2009-02-25 13:23:22 +00002671 message_handler_ = handler;
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002672 ListenersChanged();
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00002673 if (handler == NULL) {
ager@chromium.org71daaf62009-04-01 07:22:49 +00002674 // Send an empty command to the debugger if in a break to make JavaScript
2675 // run again if the debugger is closed.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002676 if (isolate_->debug()->InDebugger()) {
ager@chromium.org71daaf62009-04-01 07:22:49 +00002677 ProcessCommand(Vector<const uint16_t>::empty());
2678 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002679 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002680}
2681
2682
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002683void Debugger::ListenersChanged() {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002684 ASSERT(Isolate::Current() == isolate_);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002685 if (IsDebuggerActive()) {
2686 // Disable the compilation cache when the debugger is active.
lrn@chromium.org7516f052011-03-30 08:52:27 +00002687 isolate_->compilation_cache()->Disable();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002688 debugger_unload_pending_ = false;
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002689 } else {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002690 isolate_->compilation_cache()->Enable();
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002691 // Unload the debugger if event listener and message handler cleared.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002692 // Schedule this for later, because we may be in non-V8 thread.
2693 debugger_unload_pending_ = true;
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002694 }
2695}
2696
2697
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002698void Debugger::SetHostDispatchHandler(v8::Debug::HostDispatchHandler handler,
2699 int period) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002700 ASSERT(Isolate::Current() == isolate_);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002701 host_dispatch_handler_ = handler;
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002702 host_dispatch_micros_ = period * 1000;
ager@chromium.org381abbb2009-02-25 13:23:22 +00002703}
2704
2705
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002706void Debugger::SetDebugMessageDispatchHandler(
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002707 v8::Debug::DebugMessageDispatchHandler handler, bool provide_locker) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002708 ASSERT(Isolate::Current() == isolate_);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002709 ScopedLock with(dispatch_handler_access_);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002710 debug_message_dispatch_handler_ = handler;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002711
2712 if (provide_locker && message_dispatch_helper_thread_ == NULL) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002713 message_dispatch_helper_thread_ = new MessageDispatchHelperThread(isolate_);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002714 message_dispatch_helper_thread_->Start();
2715 }
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002716}
2717
2718
ager@chromium.org41826e72009-03-30 13:30:57 +00002719// Calls the registered debug message handler. This callback is part of the
ager@chromium.org5ec48922009-05-05 07:25:34 +00002720// public API.
2721void Debugger::InvokeMessageHandler(MessageImpl message) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002722 ASSERT(Isolate::Current() == isolate_);
ager@chromium.org71daaf62009-04-01 07:22:49 +00002723 ScopedLock with(debugger_access_);
2724
ager@chromium.org381abbb2009-02-25 13:23:22 +00002725 if (message_handler_ != NULL) {
ager@chromium.org5ec48922009-05-05 07:25:34 +00002726 message_handler_(message);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002727 }
ager@chromium.org41826e72009-03-30 13:30:57 +00002728}
2729
2730
2731// Puts a command coming from the public API on the queue. Creates
2732// a copy of the command string managed by the debugger. Up to this
2733// point, the command data was managed by the API client. Called
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00002734// by the API client thread.
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002735void Debugger::ProcessCommand(Vector<const uint16_t> command,
2736 v8::Debug::ClientData* client_data) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002737 ASSERT(Isolate::Current() == isolate_);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002738 // Need to cast away const.
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00002739 CommandMessage message = CommandMessage::New(
ager@chromium.org41826e72009-03-30 13:30:57 +00002740 Vector<uint16_t>(const_cast<uint16_t*>(command.start()),
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002741 command.length()),
2742 client_data);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002743 LOGGER->DebugTag("Put command on command_queue.");
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002744 command_queue_.Put(message);
ager@chromium.org41826e72009-03-30 13:30:57 +00002745 command_received_->Signal();
sgjesse@chromium.org3afc1582009-04-16 22:31:44 +00002746
2747 // Set the debug command break flag to have the command processed.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002748 if (!isolate_->debug()->InDebugger()) {
2749 isolate_->stack_guard()->DebugCommand();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002750 }
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002751
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002752 MessageDispatchHelperThread* dispatch_thread;
2753 {
2754 ScopedLock with(dispatch_handler_access_);
2755 dispatch_thread = message_dispatch_helper_thread_;
2756 }
2757
2758 if (dispatch_thread == NULL) {
2759 CallMessageDispatchHandler();
2760 } else {
2761 dispatch_thread->Schedule();
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002762 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002763}
2764
2765
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002766bool Debugger::HasCommands() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002767 ASSERT(Isolate::Current() == isolate_);
ager@chromium.org41826e72009-03-30 13:30:57 +00002768 return !command_queue_.IsEmpty();
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002769}
2770
2771
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00002772void Debugger::EnqueueDebugCommand(v8::Debug::ClientData* client_data) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002773 ASSERT(Isolate::Current() == isolate_);
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00002774 CommandMessage message = CommandMessage::New(Vector<uint16_t>(), client_data);
2775 event_command_queue_.Put(message);
2776
2777 // Set the debug command break flag to have the command processed.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002778 if (!isolate_->debug()->InDebugger()) {
2779 isolate_->stack_guard()->DebugCommand();
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00002780 }
2781}
2782
2783
ager@chromium.org71daaf62009-04-01 07:22:49 +00002784bool Debugger::IsDebuggerActive() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002785 ASSERT(Isolate::Current() == isolate_);
ager@chromium.org71daaf62009-04-01 07:22:49 +00002786 ScopedLock with(debugger_access_);
2787
2788 return message_handler_ != NULL || !event_listener_.is_null();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002789}
2790
2791
ager@chromium.orga74f0da2008-12-03 16:05:52 +00002792Handle<Object> Debugger::Call(Handle<JSFunction> fun,
2793 Handle<Object> data,
2794 bool* pending_exception) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002795 ASSERT(Isolate::Current() == isolate_);
ager@chromium.org71daaf62009-04-01 07:22:49 +00002796 // When calling functions in the debugger prevent it from beeing unloaded.
2797 Debugger::never_unload_debugger_ = true;
2798
ager@chromium.orga74f0da2008-12-03 16:05:52 +00002799 // Enter the debugger.
2800 EnterDebugger debugger;
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +00002801 if (debugger.FailedToEnter()) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002802 return isolate_->factory()->undefined_value();
ager@chromium.orga74f0da2008-12-03 16:05:52 +00002803 }
2804
2805 // Create the execution state.
2806 bool caught_exception = false;
2807 Handle<Object> exec_state = MakeExecutionState(&caught_exception);
2808 if (caught_exception) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00002809 return isolate_->factory()->undefined_value();
ager@chromium.orga74f0da2008-12-03 16:05:52 +00002810 }
2811
2812 static const int kArgc = 2;
2813 Object** argv[kArgc] = { exec_state.location(), data.location() };
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +00002814 Handle<Object> result = Execution::Call(
2815 fun,
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002816 Handle<Object>(isolate_->debug()->debug_context_->global_proxy()),
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +00002817 kArgc,
2818 argv,
2819 pending_exception);
ager@chromium.orga74f0da2008-12-03 16:05:52 +00002820 return result;
2821}
2822
2823
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002824static void StubMessageHandler2(const v8::Debug::Message& message) {
2825 // Simply ignore message.
2826}
2827
2828
2829bool Debugger::StartAgent(const char* name, int port,
2830 bool wait_for_connection) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002831 ASSERT(Isolate::Current() == isolate_);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002832 if (wait_for_connection) {
2833 // Suspend V8 if it is already running or set V8 to suspend whenever
2834 // it starts.
2835 // Provide stub message handler; V8 auto-continues each suspend
2836 // when there is no message handler; we doesn't need it.
2837 // Once become suspended, V8 will stay so indefinitely long, until remote
2838 // debugger connects and issues "continue" command.
2839 Debugger::message_handler_ = StubMessageHandler2;
2840 v8::Debug::DebugBreak();
2841 }
2842
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002843 if (Socket::Setup()) {
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00002844 if (agent_ == NULL) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002845 agent_ = new DebuggerAgent(isolate_, name, port);
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00002846 agent_->Start();
2847 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002848 return true;
2849 }
2850
2851 return false;
2852}
2853
2854
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002855void Debugger::StopAgent() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002856 ASSERT(Isolate::Current() == isolate_);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002857 if (agent_ != NULL) {
2858 agent_->Shutdown();
2859 agent_->Join();
2860 delete agent_;
2861 agent_ = NULL;
2862 }
2863}
2864
2865
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002866void Debugger::WaitForAgent() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002867 ASSERT(Isolate::Current() == isolate_);
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002868 if (agent_ != NULL)
2869 agent_->WaitUntilListening();
2870}
2871
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002872
2873void Debugger::CallMessageDispatchHandler() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002874 ASSERT(Isolate::Current() == isolate_);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002875 v8::Debug::DebugMessageDispatchHandler handler;
2876 {
2877 ScopedLock with(dispatch_handler_access_);
2878 handler = Debugger::debug_message_dispatch_handler_;
2879 }
2880 if (handler != NULL) {
2881 handler();
2882 }
2883}
2884
2885
ager@chromium.org5ec48922009-05-05 07:25:34 +00002886MessageImpl MessageImpl::NewEvent(DebugEvent event,
2887 bool running,
2888 Handle<JSObject> exec_state,
2889 Handle<JSObject> event_data) {
2890 MessageImpl message(true, event, running,
2891 exec_state, event_data, Handle<String>(), NULL);
2892 return message;
2893}
2894
2895
2896MessageImpl MessageImpl::NewResponse(DebugEvent event,
2897 bool running,
2898 Handle<JSObject> exec_state,
2899 Handle<JSObject> event_data,
2900 Handle<String> response_json,
2901 v8::Debug::ClientData* client_data) {
2902 MessageImpl message(false, event, running,
2903 exec_state, event_data, response_json, client_data);
2904 return message;
2905}
2906
2907
2908MessageImpl::MessageImpl(bool is_event,
2909 DebugEvent event,
2910 bool running,
2911 Handle<JSObject> exec_state,
2912 Handle<JSObject> event_data,
2913 Handle<String> response_json,
2914 v8::Debug::ClientData* client_data)
2915 : is_event_(is_event),
2916 event_(event),
2917 running_(running),
2918 exec_state_(exec_state),
2919 event_data_(event_data),
2920 response_json_(response_json),
2921 client_data_(client_data) {}
2922
2923
2924bool MessageImpl::IsEvent() const {
2925 return is_event_;
2926}
2927
2928
2929bool MessageImpl::IsResponse() const {
2930 return !is_event_;
2931}
2932
2933
2934DebugEvent MessageImpl::GetEvent() const {
2935 return event_;
2936}
2937
2938
2939bool MessageImpl::WillStartRunning() const {
2940 return running_;
2941}
2942
2943
2944v8::Handle<v8::Object> MessageImpl::GetExecutionState() const {
2945 return v8::Utils::ToLocal(exec_state_);
2946}
2947
2948
2949v8::Handle<v8::Object> MessageImpl::GetEventData() const {
2950 return v8::Utils::ToLocal(event_data_);
2951}
2952
2953
2954v8::Handle<v8::String> MessageImpl::GetJSON() const {
2955 v8::HandleScope scope;
2956
2957 if (IsEvent()) {
2958 // Call toJSONProtocol on the debug event object.
2959 Handle<Object> fun = GetProperty(event_data_, "toJSONProtocol");
2960 if (!fun->IsJSFunction()) {
2961 return v8::Handle<v8::String>();
2962 }
2963 bool caught_exception;
2964 Handle<Object> json = Execution::TryCall(Handle<JSFunction>::cast(fun),
2965 event_data_,
2966 0, NULL, &caught_exception);
2967 if (caught_exception || !json->IsString()) {
2968 return v8::Handle<v8::String>();
2969 }
2970 return scope.Close(v8::Utils::ToLocal(Handle<String>::cast(json)));
2971 } else {
2972 return v8::Utils::ToLocal(response_json_);
2973 }
2974}
2975
2976
2977v8::Handle<v8::Context> MessageImpl::GetEventContext() const {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002978 Isolate* isolate = Isolate::Current();
2979 v8::Handle<v8::Context> context = GetDebugEventContext(isolate);
2980 // Isolate::context() may be NULL when "script collected" event occures.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002981 ASSERT(!context.IsEmpty() || event_ == v8::ScriptCollected);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002982 return GetDebugEventContext(isolate);
ager@chromium.org5ec48922009-05-05 07:25:34 +00002983}
2984
2985
2986v8::Debug::ClientData* MessageImpl::GetClientData() const {
2987 return client_data_;
2988}
2989
2990
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002991EventDetailsImpl::EventDetailsImpl(DebugEvent event,
2992 Handle<JSObject> exec_state,
2993 Handle<JSObject> event_data,
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00002994 Handle<Object> callback_data,
2995 v8::Debug::ClientData* client_data)
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002996 : event_(event),
2997 exec_state_(exec_state),
2998 event_data_(event_data),
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00002999 callback_data_(callback_data),
3000 client_data_(client_data) {}
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003001
3002
3003DebugEvent EventDetailsImpl::GetEvent() const {
3004 return event_;
3005}
3006
3007
3008v8::Handle<v8::Object> EventDetailsImpl::GetExecutionState() const {
3009 return v8::Utils::ToLocal(exec_state_);
3010}
3011
3012
3013v8::Handle<v8::Object> EventDetailsImpl::GetEventData() const {
3014 return v8::Utils::ToLocal(event_data_);
3015}
3016
3017
3018v8::Handle<v8::Context> EventDetailsImpl::GetEventContext() const {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003019 return GetDebugEventContext(Isolate::Current());
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00003020}
3021
3022
3023v8::Handle<v8::Value> EventDetailsImpl::GetCallbackData() const {
3024 return v8::Utils::ToLocal(callback_data_);
3025}
3026
3027
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00003028v8::Debug::ClientData* EventDetailsImpl::GetClientData() const {
3029 return client_data_;
3030}
3031
3032
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003033CommandMessage::CommandMessage() : text_(Vector<uint16_t>::empty()),
3034 client_data_(NULL) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003035}
3036
3037
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003038CommandMessage::CommandMessage(const Vector<uint16_t>& text,
3039 v8::Debug::ClientData* data)
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003040 : text_(text),
3041 client_data_(data) {
3042}
3043
3044
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003045CommandMessage::~CommandMessage() {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003046}
3047
3048
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003049void CommandMessage::Dispose() {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003050 text_.Dispose();
3051 delete client_data_;
3052 client_data_ = NULL;
3053}
3054
3055
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003056CommandMessage CommandMessage::New(const Vector<uint16_t>& command,
3057 v8::Debug::ClientData* data) {
3058 return CommandMessage(command.Clone(), data);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003059}
3060
3061
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003062CommandMessageQueue::CommandMessageQueue(int size) : start_(0), end_(0),
3063 size_(size) {
3064 messages_ = NewArray<CommandMessage>(size);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003065}
3066
3067
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003068CommandMessageQueue::~CommandMessageQueue() {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003069 while (!IsEmpty()) {
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003070 CommandMessage m = Get();
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003071 m.Dispose();
3072 }
kasper.lund7276f142008-07-30 08:49:36 +00003073 DeleteArray(messages_);
3074}
3075
3076
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003077CommandMessage CommandMessageQueue::Get() {
kasper.lund7276f142008-07-30 08:49:36 +00003078 ASSERT(!IsEmpty());
3079 int result = start_;
3080 start_ = (start_ + 1) % size_;
3081 return messages_[result];
3082}
3083
3084
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003085void CommandMessageQueue::Put(const CommandMessage& message) {
kasper.lund7276f142008-07-30 08:49:36 +00003086 if ((end_ + 1) % size_ == start_) {
3087 Expand();
3088 }
3089 messages_[end_] = message;
3090 end_ = (end_ + 1) % size_;
3091}
3092
3093
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003094void CommandMessageQueue::Expand() {
3095 CommandMessageQueue new_queue(size_ * 2);
kasper.lund7276f142008-07-30 08:49:36 +00003096 while (!IsEmpty()) {
3097 new_queue.Put(Get());
3098 }
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003099 CommandMessage* array_to_free = messages_;
kasper.lund7276f142008-07-30 08:49:36 +00003100 *this = new_queue;
3101 new_queue.messages_ = array_to_free;
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003102 // Make the new_queue empty so that it doesn't call Dispose on any messages.
3103 new_queue.start_ = new_queue.end_;
kasper.lund7276f142008-07-30 08:49:36 +00003104 // Automatic destructor called on new_queue, freeing array_to_free.
3105}
3106
3107
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003108LockingCommandMessageQueue::LockingCommandMessageQueue(int size)
3109 : queue_(size) {
kasper.lund7276f142008-07-30 08:49:36 +00003110 lock_ = OS::CreateMutex();
3111}
3112
3113
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003114LockingCommandMessageQueue::~LockingCommandMessageQueue() {
kasper.lund7276f142008-07-30 08:49:36 +00003115 delete lock_;
3116}
3117
3118
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003119bool LockingCommandMessageQueue::IsEmpty() const {
kasper.lund7276f142008-07-30 08:49:36 +00003120 ScopedLock sl(lock_);
3121 return queue_.IsEmpty();
3122}
3123
3124
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003125CommandMessage LockingCommandMessageQueue::Get() {
kasper.lund7276f142008-07-30 08:49:36 +00003126 ScopedLock sl(lock_);
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003127 CommandMessage result = queue_.Get();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003128 LOGGER->DebugEvent("Get", result.text());
kasper.lund7276f142008-07-30 08:49:36 +00003129 return result;
3130}
3131
3132
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003133void LockingCommandMessageQueue::Put(const CommandMessage& message) {
kasper.lund7276f142008-07-30 08:49:36 +00003134 ScopedLock sl(lock_);
3135 queue_.Put(message);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003136 LOGGER->DebugEvent("Put", message.text());
kasper.lund7276f142008-07-30 08:49:36 +00003137}
3138
3139
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003140void LockingCommandMessageQueue::Clear() {
kasper.lund7276f142008-07-30 08:49:36 +00003141 ScopedLock sl(lock_);
3142 queue_.Clear();
3143}
3144
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003145
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003146MessageDispatchHelperThread::MessageDispatchHelperThread(Isolate* isolate)
3147 : Thread(isolate, "v8:MsgDispHelpr"),
lrn@chromium.org5d00b602011-01-05 09:51:43 +00003148 sem_(OS::CreateSemaphore(0)), mutex_(OS::CreateMutex()),
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003149 already_signalled_(false) {
3150}
3151
3152
3153MessageDispatchHelperThread::~MessageDispatchHelperThread() {
3154 delete mutex_;
3155 delete sem_;
3156}
3157
3158
3159void MessageDispatchHelperThread::Schedule() {
3160 {
3161 ScopedLock lock(mutex_);
3162 if (already_signalled_) {
3163 return;
3164 }
3165 already_signalled_ = true;
3166 }
3167 sem_->Signal();
3168}
3169
3170
3171void MessageDispatchHelperThread::Run() {
3172 while (true) {
3173 sem_->Wait();
3174 {
3175 ScopedLock lock(mutex_);
3176 already_signalled_ = false;
3177 }
3178 {
3179 Locker locker;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003180 Isolate::Current()->debugger()->CallMessageDispatchHandler();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003181 }
3182 }
3183}
3184
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003185#endif // ENABLE_DEBUGGER_SUPPORT
kasper.lund7276f142008-07-30 08:49:36 +00003186
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003187} } // namespace v8::internal