blob: 1dc627591557596e7b8f0f8feca9ab1deb028d33 [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"
38#include "execution.h"
39#include "global-handles.h"
ager@chromium.org65dad4b2009-04-23 08:48:43 +000040#include "ic.h"
41#include "ic-inl.h"
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000042#include "messages.h"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000043#include "natives.h"
44#include "stub-cache.h"
kasper.lund7276f142008-07-30 08:49:36 +000045#include "log.h"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000046
ager@chromium.org5ec48922009-05-05 07:25:34 +000047#include "../include/v8-debug.h"
48
kasperl@chromium.org71affb52009-05-26 05:44:31 +000049namespace v8 {
50namespace internal {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000051
ager@chromium.org65dad4b2009-04-23 08:48:43 +000052#ifdef ENABLE_DEBUGGER_SUPPORT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000053static void PrintLn(v8::Local<v8::Value> value) {
54 v8::Local<v8::String> s = value->ToString();
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +000055 ScopedVector<char> data(s->Length() + 1);
56 if (data.start() == NULL) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000057 V8::FatalProcessOutOfMemory("PrintLn");
58 return;
59 }
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +000060 s->WriteAscii(data.start());
61 PrintF("%s\n", data.start());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000062}
63
64
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +000065static Handle<Code> ComputeCallDebugBreak(int argc, Code::Kind kind) {
66 CALL_HEAP_FUNCTION(StubCache::ComputeCallDebugBreak(argc, kind), Code);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000067}
68
69
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +000070static Handle<Code> ComputeCallDebugPrepareStepIn(int argc, Code::Kind kind) {
71 CALL_HEAP_FUNCTION(
72 StubCache::ComputeCallDebugPrepareStepIn(argc, kind), Code);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000073}
74
75
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +000076static v8::Handle<v8::Context> GetDebugEventContext() {
77 Handle<Context> context = Debug::debugger_entry()->GetContext();
78 // Top::context() may have been NULL when "script collected" event occured.
79 if (*context == NULL) {
80 return v8::Local<v8::Context>();
81 }
82 Handle<Context> global_context(context->global_context());
83 return v8::Utils::ToLocal(global_context);
84}
85
86
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000087BreakLocationIterator::BreakLocationIterator(Handle<DebugInfo> debug_info,
88 BreakLocatorType type) {
89 debug_info_ = debug_info;
90 type_ = type;
91 reloc_iterator_ = NULL;
92 reloc_iterator_original_ = NULL;
93 Reset(); // Initialize the rest of the member variables.
94}
95
96
97BreakLocationIterator::~BreakLocationIterator() {
98 ASSERT(reloc_iterator_ != NULL);
99 ASSERT(reloc_iterator_original_ != NULL);
100 delete reloc_iterator_;
101 delete reloc_iterator_original_;
102}
103
104
105void BreakLocationIterator::Next() {
106 AssertNoAllocation nogc;
107 ASSERT(!RinfoDone());
108
109 // Iterate through reloc info for code and original code stopping at each
110 // breakable code target.
111 bool first = break_point_ == -1;
112 while (!RinfoDone()) {
113 if (!first) RinfoNext();
114 first = false;
115 if (RinfoDone()) return;
116
ager@chromium.org236ad962008-09-25 09:45:57 +0000117 // Whenever a statement position or (plain) position is passed update the
118 // current value of these.
119 if (RelocInfo::IsPosition(rmode())) {
120 if (RelocInfo::IsStatementPosition(rmode())) {
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000121 statement_position_ = static_cast<int>(
122 rinfo()->data() - debug_info_->shared()->start_position());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000123 }
ager@chromium.org236ad962008-09-25 09:45:57 +0000124 // Always update the position as we don't want that to be before the
125 // statement position.
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000126 position_ = static_cast<int>(
127 rinfo()->data() - debug_info_->shared()->start_position());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000128 ASSERT(position_ >= 0);
129 ASSERT(statement_position_ >= 0);
130 }
131
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000132 if (IsDebugBreakSlot()) {
133 // There is always a possible break point at a debug break slot.
134 break_point_++;
135 return;
136 } else if (RelocInfo::IsCodeTarget(rmode())) {
137 // Check for breakable code target. Look in the original code as setting
138 // break points can cause the code targets in the running (debugged) code
139 // to be of a different kind than in the original code.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000140 Address target = original_rinfo()->target_address();
ager@chromium.org8bb60582008-12-11 12:02:20 +0000141 Code* code = Code::GetCodeFromTargetAddress(target);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000142 if ((code->is_inline_cache_stub() &&
143 code->kind() != Code::BINARY_OP_IC) ||
144 RelocInfo::IsConstructCall(rmode())) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000145 break_point_++;
146 return;
147 }
148 if (code->kind() == Code::STUB) {
ager@chromium.orga1645e22009-09-09 19:27:10 +0000149 if (IsDebuggerStatement()) {
150 break_point_++;
151 return;
152 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000153 if (type_ == ALL_BREAK_LOCATIONS) {
154 if (Debug::IsBreakStub(code)) {
155 break_point_++;
156 return;
157 }
158 } else {
159 ASSERT(type_ == SOURCE_BREAK_LOCATIONS);
160 if (Debug::IsSourceBreakStub(code)) {
161 break_point_++;
162 return;
163 }
164 }
165 }
166 }
167
168 // Check for break at return.
ager@chromium.org236ad962008-09-25 09:45:57 +0000169 if (RelocInfo::IsJSReturn(rmode())) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000170 // Set the positions to the end of the function.
171 if (debug_info_->shared()->HasSourceCode()) {
172 position_ = debug_info_->shared()->end_position() -
173 debug_info_->shared()->start_position();
174 } else {
175 position_ = 0;
176 }
177 statement_position_ = position_;
178 break_point_++;
179 return;
180 }
181 }
182}
183
184
185void BreakLocationIterator::Next(int count) {
186 while (count > 0) {
187 Next();
188 count--;
189 }
190}
191
192
193// Find the break point closest to the supplied address.
194void BreakLocationIterator::FindBreakLocationFromAddress(Address pc) {
195 // Run through all break points to locate the one closest to the address.
196 int closest_break_point = 0;
197 int distance = kMaxInt;
198 while (!Done()) {
199 // Check if this break point is closer that what was previously found.
200 if (this->pc() < pc && pc - this->pc() < distance) {
201 closest_break_point = break_point();
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000202 distance = static_cast<int>(pc - this->pc());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000203 // Check whether we can't get any closer.
204 if (distance == 0) break;
205 }
206 Next();
207 }
208
209 // Move to the break point found.
210 Reset();
211 Next(closest_break_point);
212}
213
214
215// Find the break point closest to the supplied source position.
216void BreakLocationIterator::FindBreakLocationFromPosition(int position) {
217 // Run through all break points to locate the one closest to the source
218 // position.
219 int closest_break_point = 0;
220 int distance = kMaxInt;
221 while (!Done()) {
222 // Check if this break point is closer that what was previously found.
223 if (position <= statement_position() &&
224 statement_position() - position < distance) {
225 closest_break_point = break_point();
226 distance = statement_position() - position;
227 // Check whether we can't get any closer.
228 if (distance == 0) break;
229 }
230 Next();
231 }
232
233 // Move to the break point found.
234 Reset();
235 Next(closest_break_point);
236}
237
238
239void BreakLocationIterator::Reset() {
240 // Create relocation iterators for the two code objects.
241 if (reloc_iterator_ != NULL) delete reloc_iterator_;
242 if (reloc_iterator_original_ != NULL) delete reloc_iterator_original_;
243 reloc_iterator_ = new RelocIterator(debug_info_->code());
244 reloc_iterator_original_ = new RelocIterator(debug_info_->original_code());
245
246 // Position at the first break point.
247 break_point_ = -1;
248 position_ = 1;
249 statement_position_ = 1;
250 Next();
251}
252
253
254bool BreakLocationIterator::Done() const {
255 return RinfoDone();
256}
257
258
259void BreakLocationIterator::SetBreakPoint(Handle<Object> break_point_object) {
260 // If there is not already a real break point here patch code with debug
261 // break.
262 if (!HasBreakPoint()) {
263 SetDebugBreak();
264 }
ager@chromium.orga1645e22009-09-09 19:27:10 +0000265 ASSERT(IsDebugBreak() || IsDebuggerStatement());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000266 // Set the break point information.
267 DebugInfo::SetBreakPoint(debug_info_, code_position(),
268 position(), statement_position(),
269 break_point_object);
270}
271
272
273void BreakLocationIterator::ClearBreakPoint(Handle<Object> break_point_object) {
274 // Clear the break point information.
275 DebugInfo::ClearBreakPoint(debug_info_, code_position(), break_point_object);
276 // If there are no more break points here remove the debug break.
277 if (!HasBreakPoint()) {
278 ClearDebugBreak();
279 ASSERT(!IsDebugBreak());
280 }
281}
282
283
284void BreakLocationIterator::SetOneShot() {
ager@chromium.orga1645e22009-09-09 19:27:10 +0000285 // Debugger statement always calls debugger. No need to modify it.
286 if (IsDebuggerStatement()) {
287 return;
288 }
289
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000290 // If there is a real break point here no more to do.
291 if (HasBreakPoint()) {
292 ASSERT(IsDebugBreak());
293 return;
294 }
295
296 // Patch code with debug break.
297 SetDebugBreak();
298}
299
300
301void BreakLocationIterator::ClearOneShot() {
ager@chromium.orga1645e22009-09-09 19:27:10 +0000302 // Debugger statement always calls debugger. No need to modify it.
303 if (IsDebuggerStatement()) {
304 return;
305 }
306
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000307 // If there is a real break point here no more to do.
308 if (HasBreakPoint()) {
309 ASSERT(IsDebugBreak());
310 return;
311 }
312
313 // Patch code removing debug break.
314 ClearDebugBreak();
315 ASSERT(!IsDebugBreak());
316}
317
318
319void BreakLocationIterator::SetDebugBreak() {
ager@chromium.orga1645e22009-09-09 19:27:10 +0000320 // Debugger statement always calls debugger. No need to modify it.
321 if (IsDebuggerStatement()) {
322 return;
323 }
324
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000325 // If there is already a break point here just return. This might happen if
v8.team.kasperl727e9952008-09-02 14:56:44 +0000326 // the same code is flooded with break points twice. Flooding the same
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000327 // function twice might happen when stepping in a function with an exception
328 // handler as the handler and the function is the same.
329 if (IsDebugBreak()) {
330 return;
331 }
332
ager@chromium.org236ad962008-09-25 09:45:57 +0000333 if (RelocInfo::IsJSReturn(rmode())) {
iposva@chromium.org245aa852009-02-10 00:49:54 +0000334 // Patch the frame exit code with a break point.
335 SetDebugBreakAtReturn();
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000336 } else if (IsDebugBreakSlot()) {
337 // Patch the code in the break slot.
338 SetDebugBreakAtSlot();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000339 } else {
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000340 // Patch the IC call.
341 SetDebugBreakAtIC();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000342 }
343 ASSERT(IsDebugBreak());
344}
345
346
347void BreakLocationIterator::ClearDebugBreak() {
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
ager@chromium.org236ad962008-09-25 09:45:57 +0000353 if (RelocInfo::IsJSReturn(rmode())) {
iposva@chromium.org245aa852009-02-10 00:49:54 +0000354 // Restore the frame exit code.
355 ClearDebugBreakAtReturn();
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000356 } else if (IsDebugBreakSlot()) {
357 // Restore the code in the break slot.
358 ClearDebugBreakAtSlot();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000359 } else {
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000360 // Patch the IC call.
361 ClearDebugBreakAtIC();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000362 }
363 ASSERT(!IsDebugBreak());
364}
365
366
367void BreakLocationIterator::PrepareStepIn() {
ager@chromium.org381abbb2009-02-25 13:23:22 +0000368 HandleScope scope;
369
ager@chromium.orga1645e22009-09-09 19:27:10 +0000370 // Step in can only be prepared if currently positioned on an IC call,
371 // construct call or CallFunction stub call.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000372 Address target = rinfo()->target_address();
ager@chromium.orga1645e22009-09-09 19:27:10 +0000373 Handle<Code> code(Code::GetCodeFromTargetAddress(target));
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +0000374 if (code->is_call_stub() || code->is_keyed_call_stub()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000375 // Step in through IC call is handled by the runtime system. Therefore make
376 // sure that the any current IC is cleared and the runtime system is
377 // called. If the executing code has a debug break at the location change
378 // the call in the original code as it is the code there that will be
379 // executed in place of the debug break call.
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +0000380 Handle<Code> stub = ComputeCallDebugPrepareStepIn(code->arguments_count(),
381 code->kind());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000382 if (IsDebugBreak()) {
383 original_rinfo()->set_target_address(stub->entry());
384 } else {
385 rinfo()->set_target_address(stub->entry());
386 }
387 } else {
ager@chromium.orga1645e22009-09-09 19:27:10 +0000388#ifdef DEBUG
389 // All the following stuff is needed only for assertion checks so the code
390 // is wrapped in ifdef.
391 Handle<Code> maybe_call_function_stub = code;
392 if (IsDebugBreak()) {
393 Address original_target = original_rinfo()->target_address();
394 maybe_call_function_stub =
395 Handle<Code>(Code::GetCodeFromTargetAddress(original_target));
396 }
397 bool is_call_function_stub =
398 (maybe_call_function_stub->kind() == Code::STUB &&
399 maybe_call_function_stub->major_key() == CodeStub::CallFunction);
400
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000401 // Step in through construct call requires no changes to the running code.
402 // Step in through getters/setters should already be prepared as well
403 // because caller of this function (Debug::PrepareStep) is expected to
404 // flood the top frame's function with one shot breakpoints.
ager@chromium.orga1645e22009-09-09 19:27:10 +0000405 // Step in through CallFunction stub should also be prepared by caller of
406 // this function (Debug::PrepareStep) which should flood target function
407 // with breakpoints.
408 ASSERT(RelocInfo::IsConstructCall(rmode()) || code->is_inline_cache_stub()
409 || is_call_function_stub);
410#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000411 }
412}
413
414
415// Check whether the break point is at a position which will exit the function.
416bool BreakLocationIterator::IsExit() const {
ager@chromium.org236ad962008-09-25 09:45:57 +0000417 return (RelocInfo::IsJSReturn(rmode()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000418}
419
420
421bool BreakLocationIterator::HasBreakPoint() {
422 return debug_info_->HasBreakPoint(code_position());
423}
424
425
426// Check whether there is a debug break at the current position.
427bool BreakLocationIterator::IsDebugBreak() {
ager@chromium.org236ad962008-09-25 09:45:57 +0000428 if (RelocInfo::IsJSReturn(rmode())) {
iposva@chromium.org245aa852009-02-10 00:49:54 +0000429 return IsDebugBreakAtReturn();
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000430 } else if (IsDebugBreakSlot()) {
431 return IsDebugBreakAtSlot();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000432 } else {
433 return Debug::IsDebugBreak(rinfo()->target_address());
434 }
435}
436
437
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000438void BreakLocationIterator::SetDebugBreakAtIC() {
439 // Patch the original code with the current address as the current address
440 // might have changed by the inline caching since the code was copied.
441 original_rinfo()->set_target_address(rinfo()->target_address());
442
443 RelocInfo::Mode mode = rmode();
444 if (RelocInfo::IsCodeTarget(mode)) {
445 Address target = rinfo()->target_address();
446 Handle<Code> code(Code::GetCodeFromTargetAddress(target));
447
448 // Patch the code to invoke the builtin debug break function matching the
449 // calling convention used by the call site.
450 Handle<Code> dbgbrk_code(Debug::FindDebugBreak(code, mode));
451 rinfo()->set_target_address(dbgbrk_code->entry());
452
453 // For stubs that refer back to an inlined version clear the cached map for
454 // the inlined case to always go through the IC. As long as the break point
455 // is set the patching performed by the runtime system will take place in
456 // the code copy and will therefore have no effect on the running code
457 // keeping it from using the inlined code.
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000458 if (code->is_keyed_load_stub()) {
459 KeyedLoadIC::ClearInlinedVersion(pc());
460 } else if (code->is_keyed_store_stub()) {
461 KeyedStoreIC::ClearInlinedVersion(pc());
462 } else if (code->is_load_stub()) {
463 LoadIC::ClearInlinedVersion(pc());
464 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000465 }
466}
467
468
469void BreakLocationIterator::ClearDebugBreakAtIC() {
470 // Patch the code to the original invoke.
471 rinfo()->set_target_address(original_rinfo()->target_address());
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000472
473 RelocInfo::Mode mode = rmode();
474 if (RelocInfo::IsCodeTarget(mode)) {
475 Address target = original_rinfo()->target_address();
476 Handle<Code> code(Code::GetCodeFromTargetAddress(target));
477
478 // Restore the inlined version of keyed stores to get back to the
479 // fast case. We need to patch back the keyed store because no
480 // patching happens when running normally. For keyed loads, the
481 // map check will get patched back when running normally after ICs
482 // have been cleared at GC.
483 if (code->is_keyed_store_stub()) KeyedStoreIC::RestoreInlinedVersion(pc());
484 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000485}
486
487
ager@chromium.orga1645e22009-09-09 19:27:10 +0000488bool BreakLocationIterator::IsDebuggerStatement() {
ager@chromium.org5c838252010-02-19 08:53:10 +0000489 return RelocInfo::DEBUG_BREAK == rmode();
ager@chromium.orga1645e22009-09-09 19:27:10 +0000490}
491
492
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000493bool BreakLocationIterator::IsDebugBreakSlot() {
494 return RelocInfo::DEBUG_BREAK_SLOT == rmode();
495}
496
497
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000498Object* BreakLocationIterator::BreakPointObjects() {
499 return debug_info_->GetBreakPointObjects(code_position());
500}
501
502
ager@chromium.org381abbb2009-02-25 13:23:22 +0000503// Clear out all the debug break code. This is ONLY supposed to be used when
504// shutting down the debugger as it will leave the break point information in
505// DebugInfo even though the code is patched back to the non break point state.
506void BreakLocationIterator::ClearAllDebugBreak() {
507 while (!Done()) {
508 ClearDebugBreak();
509 Next();
510 }
511}
512
513
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000514bool BreakLocationIterator::RinfoDone() const {
515 ASSERT(reloc_iterator_->done() == reloc_iterator_original_->done());
516 return reloc_iterator_->done();
517}
518
519
520void BreakLocationIterator::RinfoNext() {
521 reloc_iterator_->next();
522 reloc_iterator_original_->next();
523#ifdef DEBUG
524 ASSERT(reloc_iterator_->done() == reloc_iterator_original_->done());
525 if (!reloc_iterator_->done()) {
526 ASSERT(rmode() == original_rmode());
527 }
528#endif
529}
530
531
532bool Debug::has_break_points_ = false;
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000533ScriptCache* Debug::script_cache_ = NULL;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000534DebugInfoListNode* Debug::debug_info_list_ = NULL;
535
536
537// Threading support.
538void Debug::ThreadInit() {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000539 thread_local_.break_count_ = 0;
540 thread_local_.break_id_ = 0;
541 thread_local_.break_frame_id_ = StackFrame::NO_ID;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000542 thread_local_.last_step_action_ = StepNone;
ager@chromium.org236ad962008-09-25 09:45:57 +0000543 thread_local_.last_statement_position_ = RelocInfo::kNoPosition;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000544 thread_local_.step_count_ = 0;
545 thread_local_.last_fp_ = 0;
546 thread_local_.step_into_fp_ = 0;
ager@chromium.orga1645e22009-09-09 19:27:10 +0000547 thread_local_.step_out_fp_ = 0;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000548 thread_local_.after_break_target_ = 0;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000549 thread_local_.debugger_entry_ = NULL;
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000550 thread_local_.pending_interrupts_ = 0;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000551}
552
553
554JSCallerSavedBuffer Debug::registers_;
555Debug::ThreadLocal Debug::thread_local_;
556
557
558char* Debug::ArchiveDebug(char* storage) {
559 char* to = storage;
560 memcpy(to, reinterpret_cast<char*>(&thread_local_), sizeof(ThreadLocal));
561 to += sizeof(ThreadLocal);
562 memcpy(to, reinterpret_cast<char*>(&registers_), sizeof(registers_));
563 ThreadInit();
564 ASSERT(to <= storage + ArchiveSpacePerThread());
565 return storage + ArchiveSpacePerThread();
566}
567
568
569char* Debug::RestoreDebug(char* storage) {
570 char* from = storage;
571 memcpy(reinterpret_cast<char*>(&thread_local_), from, sizeof(ThreadLocal));
572 from += sizeof(ThreadLocal);
573 memcpy(reinterpret_cast<char*>(&registers_), from, sizeof(registers_));
574 ASSERT(from <= storage + ArchiveSpacePerThread());
575 return storage + ArchiveSpacePerThread();
576}
577
578
579int Debug::ArchiveSpacePerThread() {
580 return sizeof(ThreadLocal) + sizeof(registers_);
581}
582
583
kasper.lundbd3ec4e2008-07-09 11:06:54 +0000584// Default break enabled.
585bool Debug::disable_break_ = false;
586
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000587// Default call debugger on uncaught exception.
588bool Debug::break_on_exception_ = false;
589bool Debug::break_on_uncaught_exception_ = true;
590
591Handle<Context> Debug::debug_context_ = Handle<Context>();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000592Code* Debug::debug_break_return_ = NULL;
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000593Code* Debug::debug_break_slot_ = NULL;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000594
595
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000596void ScriptCache::Add(Handle<Script> script) {
597 // Create an entry in the hash map for the script.
598 int id = Smi::cast(script->id())->value();
599 HashMap::Entry* entry =
600 HashMap::Lookup(reinterpret_cast<void*>(id), Hash(id), true);
601 if (entry->value != NULL) {
602 ASSERT(*script == *reinterpret_cast<Script**>(entry->value));
603 return;
604 }
605
606 // Globalize the script object, make it weak and use the location of the
607 // global handle as the value in the hash map.
608 Handle<Script> script_ =
609 Handle<Script>::cast((GlobalHandles::Create(*script)));
610 GlobalHandles::MakeWeak(reinterpret_cast<Object**>(script_.location()),
611 this, ScriptCache::HandleWeakScript);
612 entry->value = script_.location();
613}
614
615
616Handle<FixedArray> ScriptCache::GetScripts() {
617 Handle<FixedArray> instances = Factory::NewFixedArray(occupancy());
618 int count = 0;
619 for (HashMap::Entry* entry = Start(); entry != NULL; entry = Next(entry)) {
620 ASSERT(entry->value != NULL);
621 if (entry->value != NULL) {
622 instances->set(count, *reinterpret_cast<Script**>(entry->value));
623 count++;
624 }
625 }
626 return instances;
627}
628
629
630void ScriptCache::ProcessCollectedScripts() {
631 for (int i = 0; i < collected_scripts_.length(); i++) {
632 Debugger::OnScriptCollected(collected_scripts_[i]);
633 }
634 collected_scripts_.Clear();
635}
636
637
638void ScriptCache::Clear() {
639 // Iterate the script cache to get rid of all the weak handles.
640 for (HashMap::Entry* entry = Start(); entry != NULL; entry = Next(entry)) {
641 ASSERT(entry != NULL);
642 Object** location = reinterpret_cast<Object**>(entry->value);
643 ASSERT((*location)->IsScript());
644 GlobalHandles::ClearWeakness(location);
645 GlobalHandles::Destroy(location);
646 }
647 // Clear the content of the hash map.
648 HashMap::Clear();
649}
650
651
652void ScriptCache::HandleWeakScript(v8::Persistent<v8::Value> obj, void* data) {
653 ScriptCache* script_cache = reinterpret_cast<ScriptCache*>(data);
654 // Find the location of the global handle.
655 Script** location =
656 reinterpret_cast<Script**>(Utils::OpenHandle(*obj).location());
657 ASSERT((*location)->IsScript());
658
659 // Remove the entry from the cache.
660 int id = Smi::cast((*location)->id())->value();
661 script_cache->Remove(reinterpret_cast<void*>(id), Hash(id));
662 script_cache->collected_scripts_.Add(id);
663
664 // Clear the weak handle.
665 obj.Dispose();
666 obj.Clear();
667}
668
669
670void Debug::Setup(bool create_heap_objects) {
671 ThreadInit();
672 if (create_heap_objects) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000673 // Get code to handle debug break on return.
674 debug_break_return_ =
675 Builtins::builtin(Builtins::Return_DebugBreak);
676 ASSERT(debug_break_return_->IsCode());
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000677 // Get code to handle debug break in debug break slots.
678 debug_break_slot_ =
679 Builtins::builtin(Builtins::Slot_DebugBreak);
680 ASSERT(debug_break_slot_->IsCode());
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000681 }
682}
683
684
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000685void Debug::HandleWeakDebugInfo(v8::Persistent<v8::Value> obj, void* data) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000686 DebugInfoListNode* node = reinterpret_cast<DebugInfoListNode*>(data);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000687 // We need to clear all breakpoints associated with the function to restore
688 // original code and avoid patching the code twice later because
689 // the function will live in the heap until next gc, and can be found by
690 // Runtime::FindSharedFunctionInfoInScript.
691 BreakLocationIterator it(node->debug_info(), ALL_BREAK_LOCATIONS);
692 it.ClearAllDebugBreak();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000693 RemoveDebugInfo(node->debug_info());
694#ifdef DEBUG
695 node = Debug::debug_info_list_;
696 while (node != NULL) {
697 ASSERT(node != reinterpret_cast<DebugInfoListNode*>(data));
698 node = node->next();
699 }
700#endif
701}
702
703
704DebugInfoListNode::DebugInfoListNode(DebugInfo* debug_info): next_(NULL) {
705 // Globalize the request debug info object and make it weak.
706 debug_info_ = Handle<DebugInfo>::cast((GlobalHandles::Create(debug_info)));
707 GlobalHandles::MakeWeak(reinterpret_cast<Object**>(debug_info_.location()),
708 this, Debug::HandleWeakDebugInfo);
709}
710
711
712DebugInfoListNode::~DebugInfoListNode() {
713 GlobalHandles::Destroy(reinterpret_cast<Object**>(debug_info_.location()));
714}
715
716
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000717bool Debug::CompileDebuggerScript(int index) {
718 HandleScope scope;
719
kasper.lund44510672008-07-25 07:37:58 +0000720 // Bail out if the index is invalid.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000721 if (index == -1) {
722 return false;
723 }
kasper.lund44510672008-07-25 07:37:58 +0000724
725 // Find source and name for the requested script.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000726 Handle<String> source_code = Bootstrapper::NativesSourceLookup(index);
727 Vector<const char> name = Natives::GetScriptName(index);
728 Handle<String> script_name = Factory::NewStringFromAscii(name);
729
730 // Compile the script.
731 bool allow_natives_syntax = FLAG_allow_natives_syntax;
732 FLAG_allow_natives_syntax = true;
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000733 Handle<SharedFunctionInfo> function_info;
734 function_info = Compiler::Compile(source_code,
735 script_name,
736 0, 0, NULL, NULL,
737 Handle<String>::null(),
738 NATIVES_CODE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000739 FLAG_allow_natives_syntax = allow_natives_syntax;
740
741 // Silently ignore stack overflows during compilation.
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000742 if (function_info.is_null()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000743 ASSERT(Top::has_pending_exception());
744 Top::clear_pending_exception();
745 return false;
746 }
747
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000748 // Execute the shared function in the debugger context.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000749 Handle<Context> context = Top::global_context();
kasper.lund44510672008-07-25 07:37:58 +0000750 bool caught_exception = false;
751 Handle<JSFunction> function =
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000752 Factory::NewFunctionFromSharedFunctionInfo(function_info, context);
kasper.lund44510672008-07-25 07:37:58 +0000753 Handle<Object> result =
754 Execution::TryCall(function, Handle<Object>(context->global()),
755 0, NULL, &caught_exception);
756
757 // Check for caught exceptions.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000758 if (caught_exception) {
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000759 Handle<Object> message = MessageHandler::MakeMessageObject(
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000760 "error_loading_debugger", NULL, Vector<Handle<Object> >::empty(),
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000761 Handle<String>());
762 MessageHandler::ReportMessage(NULL, message);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000763 return false;
764 }
765
kasper.lund44510672008-07-25 07:37:58 +0000766 // Mark this script as native and return successfully.
767 Handle<Script> script(Script::cast(function->shared()->script()));
ager@chromium.orge2902be2009-06-08 12:21:35 +0000768 script->set_type(Smi::FromInt(Script::TYPE_NATIVE));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000769 return true;
770}
771
772
773bool Debug::Load() {
774 // Return if debugger is already loaded.
kasper.lund212ac232008-07-16 07:07:30 +0000775 if (IsLoaded()) return true;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000776
kasper.lund44510672008-07-25 07:37:58 +0000777 // Bail out if we're already in the process of compiling the native
778 // JavaScript source code for the debugger.
mads.s.agercbaa0602008-08-14 13:41:48 +0000779 if (Debugger::compiling_natives() || Debugger::is_loading_debugger())
780 return false;
781 Debugger::set_loading_debugger(true);
kasper.lund44510672008-07-25 07:37:58 +0000782
783 // Disable breakpoints and interrupts while compiling and running the
784 // debugger scripts including the context creation code.
785 DisableBreak disable(true);
786 PostponeInterruptsScope postpone;
787
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000788 // Create the debugger context.
789 HandleScope scope;
kasper.lund44510672008-07-25 07:37:58 +0000790 Handle<Context> context =
791 Bootstrapper::CreateEnvironment(Handle<Object>::null(),
792 v8::Handle<ObjectTemplate>(),
793 NULL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000794
kasper.lund44510672008-07-25 07:37:58 +0000795 // Use the debugger context.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000796 SaveContext save;
kasper.lund44510672008-07-25 07:37:58 +0000797 Top::set_context(*context);
kasper.lund44510672008-07-25 07:37:58 +0000798
799 // Expose the builtins object in the debugger context.
800 Handle<String> key = Factory::LookupAsciiSymbol("builtins");
801 Handle<GlobalObject> global = Handle<GlobalObject>(context->global());
802 SetProperty(global, key, Handle<Object>(global->builtins()), NONE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000803
804 // Compile the JavaScript for the debugger in the debugger context.
805 Debugger::set_compiling_natives(true);
kasper.lund44510672008-07-25 07:37:58 +0000806 bool caught_exception =
807 !CompileDebuggerScript(Natives::GetIndex("mirror")) ||
808 !CompileDebuggerScript(Natives::GetIndex("debug"));
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000809
810 if (FLAG_enable_liveedit) {
811 caught_exception = caught_exception ||
812 !CompileDebuggerScript(Natives::GetIndex("liveedit"));
813 }
814
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000815 Debugger::set_compiling_natives(false);
816
mads.s.agercbaa0602008-08-14 13:41:48 +0000817 // Make sure we mark the debugger as not loading before we might
818 // return.
819 Debugger::set_loading_debugger(false);
820
kasper.lund44510672008-07-25 07:37:58 +0000821 // Check for caught exceptions.
822 if (caught_exception) return false;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000823
824 // Debugger loaded.
kasper.lund44510672008-07-25 07:37:58 +0000825 debug_context_ = Handle<Context>::cast(GlobalHandles::Create(*context));
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000826
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000827 return true;
828}
829
830
831void Debug::Unload() {
832 // Return debugger is not loaded.
833 if (!IsLoaded()) {
834 return;
835 }
836
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000837 // Clear the script cache.
838 DestroyScriptCache();
839
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000840 // Clear debugger context global handle.
841 GlobalHandles::Destroy(reinterpret_cast<Object**>(debug_context_.location()));
842 debug_context_ = Handle<Context>();
843}
844
845
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000846// Set the flag indicating that preemption happened during debugging.
847void Debug::PreemptionWhileInDebugger() {
848 ASSERT(InDebugger());
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000849 Debug::set_interrupts_pending(PREEMPT);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000850}
851
852
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000853void Debug::Iterate(ObjectVisitor* v) {
vegorov@chromium.orgf8372902010-03-15 10:26:20 +0000854 v->VisitPointer(BitCast<Object**, Code**>(&(debug_break_return_)));
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000855 v->VisitPointer(BitCast<Object**, Code**>(&(debug_break_slot_)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000856}
857
858
859Object* Debug::Break(Arguments args) {
860 HandleScope scope;
mads.s.ager31e71382008-08-13 09:32:07 +0000861 ASSERT(args.length() == 0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000862
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000863 thread_local_.frame_drop_mode_ = FRAMES_UNTOUCHED;
ager@chromium.org357bf652010-04-12 11:30:10 +0000864
kasper.lundbd3ec4e2008-07-09 11:06:54 +0000865 // Get the top-most JavaScript frame.
866 JavaScriptFrameIterator it;
867 JavaScriptFrame* frame = it.frame();
868
869 // Just continue if breaks are disabled or debugger cannot be loaded.
870 if (disable_break() || !Load()) {
871 SetAfterBreakTarget(frame);
mads.s.ager31e71382008-08-13 09:32:07 +0000872 return Heap::undefined_value();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000873 }
874
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000875 // Enter the debugger.
876 EnterDebugger debugger;
877 if (debugger.FailedToEnter()) {
878 return Heap::undefined_value();
879 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000880
kasper.lund44510672008-07-25 07:37:58 +0000881 // Postpone interrupt during breakpoint processing.
882 PostponeInterruptsScope postpone;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000883
884 // Get the debug info (create it if it does not exist).
885 Handle<SharedFunctionInfo> shared =
886 Handle<SharedFunctionInfo>(JSFunction::cast(frame->function())->shared());
887 Handle<DebugInfo> debug_info = GetDebugInfo(shared);
888
889 // Find the break point where execution has stopped.
890 BreakLocationIterator break_location_iterator(debug_info,
891 ALL_BREAK_LOCATIONS);
892 break_location_iterator.FindBreakLocationFromAddress(frame->pc());
893
894 // Check whether step next reached a new statement.
895 if (!StepNextContinue(&break_location_iterator, frame)) {
896 // Decrease steps left if performing multiple steps.
897 if (thread_local_.step_count_ > 0) {
898 thread_local_.step_count_--;
899 }
900 }
901
902 // If there is one or more real break points check whether any of these are
903 // triggered.
904 Handle<Object> break_points_hit(Heap::undefined_value());
905 if (break_location_iterator.HasBreakPoint()) {
906 Handle<Object> break_point_objects =
907 Handle<Object>(break_location_iterator.BreakPointObjects());
908 break_points_hit = CheckBreakPoints(break_point_objects);
909 }
910
ager@chromium.orga1645e22009-09-09 19:27:10 +0000911 // If step out is active skip everything until the frame where we need to step
912 // out to is reached, unless real breakpoint is hit.
913 if (Debug::StepOutActive() && frame->fp() != Debug::step_out_fp() &&
914 break_points_hit->IsUndefined() ) {
915 // Step count should always be 0 for StepOut.
916 ASSERT(thread_local_.step_count_ == 0);
917 } else if (!break_points_hit->IsUndefined() ||
918 (thread_local_.last_step_action_ != StepNone &&
919 thread_local_.step_count_ == 0)) {
920 // Notify debugger if a real break point is triggered or if performing
921 // single stepping with no more steps to perform. Otherwise do another step.
922
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000923 // Clear all current stepping setup.
924 ClearStepping();
925
926 // Notify the debug event listeners.
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000927 Debugger::OnDebugBreak(break_points_hit, false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000928 } else if (thread_local_.last_step_action_ != StepNone) {
929 // Hold on to last step action as it is cleared by the call to
930 // ClearStepping.
931 StepAction step_action = thread_local_.last_step_action_;
932 int step_count = thread_local_.step_count_;
933
934 // Clear all current stepping setup.
935 ClearStepping();
936
937 // Set up for the remaining steps.
938 PrepareStep(step_action, step_count);
939 }
940
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000941 if (thread_local_.frame_drop_mode_ == FRAMES_UNTOUCHED) {
942 SetAfterBreakTarget(frame);
943 } else if (thread_local_.frame_drop_mode_ == FRAME_DROPPED_IN_IC_CALL) {
944 // We must have been calling IC stub. Do not go there anymore.
ager@chromium.org357bf652010-04-12 11:30:10 +0000945 Code* plain_return = Builtins::builtin(Builtins::PlainReturn_LiveEdit);
946 thread_local_.after_break_target_ = plain_return->entry();
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000947 } else if (thread_local_.frame_drop_mode_ ==
948 FRAME_DROPPED_IN_DEBUG_SLOT_CALL) {
949 // Debug break slot stub does not return normally, instead it manually
950 // cleans the stack and jumps. We should patch the jump address.
951 Code* plain_return = Builtins::builtin(Builtins::FrameDropper_LiveEdit);
952 thread_local_.after_break_target_ = plain_return->entry();
953 } else if (thread_local_.frame_drop_mode_ == FRAME_DROPPED_IN_DIRECT_CALL) {
954 // Nothing to do, after_break_target is not used here.
ager@chromium.org357bf652010-04-12 11:30:10 +0000955 } else {
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000956 UNREACHABLE();
ager@chromium.org357bf652010-04-12 11:30:10 +0000957 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000958
mads.s.ager31e71382008-08-13 09:32:07 +0000959 return Heap::undefined_value();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000960}
961
962
963// Check the break point objects for whether one or more are actually
964// triggered. This function returns a JSArray with the break point objects
965// which is triggered.
966Handle<Object> Debug::CheckBreakPoints(Handle<Object> break_point_objects) {
967 int break_points_hit_count = 0;
968 Handle<JSArray> break_points_hit = Factory::NewJSArray(1);
969
v8.team.kasperl727e9952008-09-02 14:56:44 +0000970 // If there are multiple break points they are in a FixedArray.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000971 ASSERT(!break_point_objects->IsUndefined());
972 if (break_point_objects->IsFixedArray()) {
973 Handle<FixedArray> array(FixedArray::cast(*break_point_objects));
974 for (int i = 0; i < array->length(); i++) {
975 Handle<Object> o(array->get(i));
976 if (CheckBreakPoint(o)) {
977 break_points_hit->SetElement(break_points_hit_count++, *o);
978 }
979 }
980 } else {
981 if (CheckBreakPoint(break_point_objects)) {
982 break_points_hit->SetElement(break_points_hit_count++,
983 *break_point_objects);
984 }
985 }
986
987 // Return undefined if no break points where triggered.
988 if (break_points_hit_count == 0) {
989 return Factory::undefined_value();
990 }
991 return break_points_hit;
992}
993
994
995// Check whether a single break point object is triggered.
996bool Debug::CheckBreakPoint(Handle<Object> break_point_object) {
ager@chromium.org381abbb2009-02-25 13:23:22 +0000997 HandleScope scope;
998
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000999 // Ignore check if break point object is not a JSObject.
1000 if (!break_point_object->IsJSObject()) return true;
1001
1002 // Get the function CheckBreakPoint (defined in debug.js).
1003 Handle<JSFunction> check_break_point =
1004 Handle<JSFunction>(JSFunction::cast(
1005 debug_context()->global()->GetProperty(
1006 *Factory::LookupAsciiSymbol("IsBreakPointTriggered"))));
1007
1008 // Get the break id as an object.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001009 Handle<Object> break_id = Factory::NewNumberFromInt(Debug::break_id());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001010
1011 // Call HandleBreakPointx.
1012 bool caught_exception = false;
1013 const int argc = 2;
1014 Object** argv[argc] = {
1015 break_id.location(),
1016 reinterpret_cast<Object**>(break_point_object.location())
1017 };
1018 Handle<Object> result = Execution::TryCall(check_break_point,
1019 Top::builtins(), argc, argv,
1020 &caught_exception);
1021
1022 // If exception or non boolean result handle as not triggered
1023 if (caught_exception || !result->IsBoolean()) {
1024 return false;
1025 }
1026
1027 // Return whether the break point is triggered.
1028 return *result == Heap::true_value();
1029}
1030
1031
1032// Check whether the function has debug information.
1033bool Debug::HasDebugInfo(Handle<SharedFunctionInfo> shared) {
1034 return !shared->debug_info()->IsUndefined();
1035}
1036
1037
kasper.lundbd3ec4e2008-07-09 11:06:54 +00001038// Return the debug info for this function. EnsureDebugInfo must be called
1039// prior to ensure the debug info has been generated for shared.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001040Handle<DebugInfo> Debug::GetDebugInfo(Handle<SharedFunctionInfo> shared) {
kasper.lundbd3ec4e2008-07-09 11:06:54 +00001041 ASSERT(HasDebugInfo(shared));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001042 return Handle<DebugInfo>(DebugInfo::cast(shared->debug_info()));
1043}
1044
1045
1046void Debug::SetBreakPoint(Handle<SharedFunctionInfo> shared,
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00001047 Handle<Object> break_point_object,
1048 int* source_position) {
ager@chromium.org381abbb2009-02-25 13:23:22 +00001049 HandleScope scope;
1050
kasper.lundbd3ec4e2008-07-09 11:06:54 +00001051 if (!EnsureDebugInfo(shared)) {
1052 // Return if retrieving debug info failed.
1053 return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001054 }
1055
kasper.lundbd3ec4e2008-07-09 11:06:54 +00001056 Handle<DebugInfo> debug_info = GetDebugInfo(shared);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001057 // Source positions starts with zero.
1058 ASSERT(source_position >= 0);
1059
1060 // Find the break point and change it.
1061 BreakLocationIterator it(debug_info, SOURCE_BREAK_LOCATIONS);
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00001062 it.FindBreakLocationFromPosition(*source_position);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001063 it.SetBreakPoint(break_point_object);
1064
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00001065 *source_position = it.position();
1066
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001067 // At least one active break point now.
1068 ASSERT(debug_info->GetBreakPointCount() > 0);
1069}
1070
1071
1072void Debug::ClearBreakPoint(Handle<Object> break_point_object) {
ager@chromium.org381abbb2009-02-25 13:23:22 +00001073 HandleScope scope;
1074
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001075 DebugInfoListNode* node = debug_info_list_;
1076 while (node != NULL) {
1077 Object* result = DebugInfo::FindBreakPointInfo(node->debug_info(),
1078 break_point_object);
1079 if (!result->IsUndefined()) {
1080 // Get information in the break point.
1081 BreakPointInfo* break_point_info = BreakPointInfo::cast(result);
1082 Handle<DebugInfo> debug_info = node->debug_info();
1083 Handle<SharedFunctionInfo> shared(debug_info->shared());
1084 int source_position = break_point_info->statement_position()->value();
1085
1086 // Source positions starts with zero.
1087 ASSERT(source_position >= 0);
1088
1089 // Find the break point and clear it.
1090 BreakLocationIterator it(debug_info, SOURCE_BREAK_LOCATIONS);
1091 it.FindBreakLocationFromPosition(source_position);
1092 it.ClearBreakPoint(break_point_object);
1093
1094 // If there are no more break points left remove the debug info for this
1095 // function.
1096 if (debug_info->GetBreakPointCount() == 0) {
1097 RemoveDebugInfo(debug_info);
1098 }
1099
1100 return;
1101 }
1102 node = node->next();
1103 }
1104}
1105
1106
ager@chromium.org381abbb2009-02-25 13:23:22 +00001107void Debug::ClearAllBreakPoints() {
1108 DebugInfoListNode* node = debug_info_list_;
1109 while (node != NULL) {
1110 // Remove all debug break code.
1111 BreakLocationIterator it(node->debug_info(), ALL_BREAK_LOCATIONS);
1112 it.ClearAllDebugBreak();
1113 node = node->next();
1114 }
1115
1116 // Remove all debug info.
1117 while (debug_info_list_ != NULL) {
1118 RemoveDebugInfo(debug_info_list_->debug_info());
1119 }
1120}
1121
1122
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001123void Debug::FloodWithOneShot(Handle<SharedFunctionInfo> shared) {
kasper.lundbd3ec4e2008-07-09 11:06:54 +00001124 // Make sure the function has setup the debug info.
1125 if (!EnsureDebugInfo(shared)) {
1126 // Return if we failed to retrieve the debug info.
1127 return;
1128 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001129
1130 // Flood the function with break points.
kasper.lundbd3ec4e2008-07-09 11:06:54 +00001131 BreakLocationIterator it(GetDebugInfo(shared), ALL_BREAK_LOCATIONS);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001132 while (!it.Done()) {
1133 it.SetOneShot();
1134 it.Next();
1135 }
1136}
1137
1138
1139void Debug::FloodHandlerWithOneShot() {
ager@chromium.org8bb60582008-12-11 12:02:20 +00001140 // Iterate through the JavaScript stack looking for handlers.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001141 StackFrame::Id id = break_frame_id();
ager@chromium.org8bb60582008-12-11 12:02:20 +00001142 if (id == StackFrame::NO_ID) {
1143 // If there is no JavaScript stack don't do anything.
1144 return;
1145 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001146 for (JavaScriptFrameIterator it(id); !it.done(); it.Advance()) {
1147 JavaScriptFrame* frame = it.frame();
1148 if (frame->HasHandler()) {
1149 Handle<SharedFunctionInfo> shared =
1150 Handle<SharedFunctionInfo>(
1151 JSFunction::cast(frame->function())->shared());
1152 // Flood the function with the catch block with break points
1153 FloodWithOneShot(shared);
1154 return;
1155 }
1156 }
1157}
1158
1159
1160void Debug::ChangeBreakOnException(ExceptionBreakType type, bool enable) {
1161 if (type == BreakUncaughtException) {
1162 break_on_uncaught_exception_ = enable;
1163 } else {
1164 break_on_exception_ = enable;
1165 }
1166}
1167
1168
1169void Debug::PrepareStep(StepAction step_action, int step_count) {
1170 HandleScope scope;
1171 ASSERT(Debug::InDebugger());
1172
1173 // Remember this step action and count.
1174 thread_local_.last_step_action_ = step_action;
ager@chromium.orga1645e22009-09-09 19:27:10 +00001175 if (step_action == StepOut) {
1176 // For step out target frame will be found on the stack so there is no need
1177 // to set step counter for it. It's expected to always be 0 for StepOut.
1178 thread_local_.step_count_ = 0;
1179 } else {
1180 thread_local_.step_count_ = step_count;
1181 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001182
1183 // Get the frame where the execution has stopped and skip the debug frame if
1184 // any. The debug frame will only be present if execution was stopped due to
1185 // hitting a break point. In other situations (e.g. unhandled exception) the
1186 // debug frame is not present.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001187 StackFrame::Id id = break_frame_id();
ager@chromium.org8bb60582008-12-11 12:02:20 +00001188 if (id == StackFrame::NO_ID) {
1189 // If there is no JavaScript stack don't do anything.
1190 return;
1191 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001192 JavaScriptFrameIterator frames_it(id);
1193 JavaScriptFrame* frame = frames_it.frame();
1194
1195 // First of all ensure there is one-shot break points in the top handler
1196 // if any.
1197 FloodHandlerWithOneShot();
1198
1199 // If the function on the top frame is unresolved perform step out. This will
1200 // be the case when calling unknown functions and having the debugger stopped
1201 // in an unhandled exception.
1202 if (!frame->function()->IsJSFunction()) {
1203 // Step out: Find the calling JavaScript frame and flood it with
1204 // breakpoints.
1205 frames_it.Advance();
1206 // Fill the function to return to with one-shot break points.
1207 JSFunction* function = JSFunction::cast(frames_it.frame()->function());
1208 FloodWithOneShot(Handle<SharedFunctionInfo>(function->shared()));
1209 return;
1210 }
1211
1212 // Get the debug info (create it if it does not exist).
1213 Handle<SharedFunctionInfo> shared =
1214 Handle<SharedFunctionInfo>(JSFunction::cast(frame->function())->shared());
kasper.lundbd3ec4e2008-07-09 11:06:54 +00001215 if (!EnsureDebugInfo(shared)) {
1216 // Return if ensuring debug info failed.
1217 return;
1218 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001219 Handle<DebugInfo> debug_info = GetDebugInfo(shared);
1220
1221 // Find the break location where execution has stopped.
1222 BreakLocationIterator it(debug_info, ALL_BREAK_LOCATIONS);
1223 it.FindBreakLocationFromAddress(frame->pc());
1224
1225 // Compute whether or not the target is a call target.
1226 bool is_call_target = false;
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001227 bool is_load_or_store = false;
1228 bool is_inline_cache_stub = false;
ager@chromium.orga1645e22009-09-09 19:27:10 +00001229 Handle<Code> call_function_stub;
ager@chromium.org236ad962008-09-25 09:45:57 +00001230 if (RelocInfo::IsCodeTarget(it.rinfo()->rmode())) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001231 Address target = it.rinfo()->target_address();
ager@chromium.org8bb60582008-12-11 12:02:20 +00001232 Code* code = Code::GetCodeFromTargetAddress(target);
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001233 if (code->is_call_stub() || code->is_keyed_call_stub()) {
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001234 is_call_target = true;
1235 }
1236 if (code->is_inline_cache_stub()) {
1237 is_inline_cache_stub = true;
1238 is_load_or_store = !is_call_target;
1239 }
ager@chromium.orga1645e22009-09-09 19:27:10 +00001240
1241 // Check if target code is CallFunction stub.
1242 Code* maybe_call_function_stub = code;
1243 // If there is a breakpoint at this line look at the original code to
1244 // check if it is a CallFunction stub.
1245 if (it.IsDebugBreak()) {
1246 Address original_target = it.original_rinfo()->target_address();
1247 maybe_call_function_stub =
1248 Code::GetCodeFromTargetAddress(original_target);
1249 }
1250 if (maybe_call_function_stub->kind() == Code::STUB &&
1251 maybe_call_function_stub->major_key() == CodeStub::CallFunction) {
1252 // Save reference to the code as we may need it to find out arguments
1253 // count for 'step in' later.
1254 call_function_stub = Handle<Code>(maybe_call_function_stub);
1255 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001256 }
1257
v8.team.kasperl727e9952008-09-02 14:56:44 +00001258 // If this is the last break code target step out is the only possibility.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001259 if (it.IsExit() || step_action == StepOut) {
ager@chromium.orga1645e22009-09-09 19:27:10 +00001260 if (step_action == StepOut) {
1261 // Skip step_count frames starting with the current one.
1262 while (step_count-- > 0 && !frames_it.done()) {
1263 frames_it.Advance();
1264 }
1265 } else {
1266 ASSERT(it.IsExit());
1267 frames_it.Advance();
1268 }
1269 // Skip builtin functions on the stack.
1270 while (!frames_it.done() &&
1271 JSFunction::cast(frames_it.frame()->function())->IsBuiltin()) {
1272 frames_it.Advance();
1273 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001274 // Step out: If there is a JavaScript caller frame, we need to
1275 // flood it with breakpoints.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001276 if (!frames_it.done()) {
1277 // Fill the function to return to with one-shot break points.
1278 JSFunction* function = JSFunction::cast(frames_it.frame()->function());
1279 FloodWithOneShot(Handle<SharedFunctionInfo>(function->shared()));
ager@chromium.orga1645e22009-09-09 19:27:10 +00001280 // Set target frame pointer.
1281 ActivateStepOut(frames_it.frame());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001282 }
ager@chromium.orga1645e22009-09-09 19:27:10 +00001283 } else if (!(is_inline_cache_stub || RelocInfo::IsConstructCall(it.rmode()) ||
1284 !call_function_stub.is_null())
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001285 || step_action == StepNext || step_action == StepMin) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001286 // Step next or step min.
1287
1288 // Fill the current function with one-shot break points.
1289 FloodWithOneShot(shared);
1290
1291 // Remember source position and frame to handle step next.
1292 thread_local_.last_statement_position_ =
1293 debug_info->code()->SourceStatementPosition(frame->pc());
1294 thread_local_.last_fp_ = frame->fp();
1295 } else {
ager@chromium.orga1645e22009-09-09 19:27:10 +00001296 // If it's CallFunction stub ensure target function is compiled and flood
1297 // it with one shot breakpoints.
1298 if (!call_function_stub.is_null()) {
1299 // Find out number of arguments from the stub minor key.
1300 // Reverse lookup required as the minor key cannot be retrieved
1301 // from the code object.
1302 Handle<Object> obj(
1303 Heap::code_stubs()->SlowReverseLookup(*call_function_stub));
1304 ASSERT(*obj != Heap::undefined_value());
1305 ASSERT(obj->IsSmi());
1306 // Get the STUB key and extract major and minor key.
1307 uint32_t key = Smi::cast(*obj)->value();
1308 // Argc in the stub is the number of arguments passed - not the
1309 // expected arguments of the called function.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001310 int call_function_arg_count =
1311 CallFunctionStub::ExtractArgcFromMinorKey(
1312 CodeStub::MinorKeyFromKey(key));
ager@chromium.orga1645e22009-09-09 19:27:10 +00001313 ASSERT(call_function_stub->major_key() ==
1314 CodeStub::MajorKeyFromKey(key));
1315
1316 // Find target function on the expression stack.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001317 // Expression stack looks like this (top to bottom):
ager@chromium.orga1645e22009-09-09 19:27:10 +00001318 // argN
1319 // ...
1320 // arg0
1321 // Receiver
1322 // Function to call
1323 int expressions_count = frame->ComputeExpressionsCount();
1324 ASSERT(expressions_count - 2 - call_function_arg_count >= 0);
1325 Object* fun = frame->GetExpression(
1326 expressions_count - 2 - call_function_arg_count);
1327 if (fun->IsJSFunction()) {
1328 Handle<JSFunction> js_function(JSFunction::cast(fun));
1329 // Don't step into builtins.
1330 if (!js_function->IsBuiltin()) {
1331 // It will also compile target function if it's not compiled yet.
1332 FloodWithOneShot(Handle<SharedFunctionInfo>(js_function->shared()));
1333 }
1334 }
1335 }
1336
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001337 // Fill the current function with one-shot break points even for step in on
1338 // a call target as the function called might be a native function for
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001339 // which step in will not stop. It also prepares for stepping in
1340 // getters/setters.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001341 FloodWithOneShot(shared);
1342
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001343 if (is_load_or_store) {
1344 // Remember source position and frame to handle step in getter/setter. If
1345 // there is a custom getter/setter it will be handled in
1346 // Object::Get/SetPropertyWithCallback, otherwise the step action will be
1347 // propagated on the next Debug::Break.
1348 thread_local_.last_statement_position_ =
1349 debug_info->code()->SourceStatementPosition(frame->pc());
1350 thread_local_.last_fp_ = frame->fp();
1351 }
1352
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001353 // Step in or Step in min
1354 it.PrepareStepIn();
1355 ActivateStepIn(frame);
1356 }
1357}
1358
1359
1360// Check whether the current debug break should be reported to the debugger. It
1361// is used to have step next and step in only report break back to the debugger
1362// if on a different frame or in a different statement. In some situations
1363// there will be several break points in the same statement when the code is
v8.team.kasperl727e9952008-09-02 14:56:44 +00001364// flooded with one-shot break points. This function helps to perform several
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001365// steps before reporting break back to the debugger.
1366bool Debug::StepNextContinue(BreakLocationIterator* break_location_iterator,
1367 JavaScriptFrame* frame) {
1368 // If the step last action was step next or step in make sure that a new
1369 // statement is hit.
1370 if (thread_local_.last_step_action_ == StepNext ||
1371 thread_local_.last_step_action_ == StepIn) {
1372 // Never continue if returning from function.
1373 if (break_location_iterator->IsExit()) return false;
1374
1375 // Continue if we are still on the same frame and in the same statement.
1376 int current_statement_position =
1377 break_location_iterator->code()->SourceStatementPosition(frame->pc());
1378 return thread_local_.last_fp_ == frame->fp() &&
ager@chromium.org236ad962008-09-25 09:45:57 +00001379 thread_local_.last_statement_position_ == current_statement_position;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001380 }
1381
1382 // No step next action - don't continue.
1383 return false;
1384}
1385
1386
1387// Check whether the code object at the specified address is a debug break code
1388// object.
1389bool Debug::IsDebugBreak(Address addr) {
ager@chromium.org8bb60582008-12-11 12:02:20 +00001390 Code* code = Code::GetCodeFromTargetAddress(addr);
kasper.lund7276f142008-07-30 08:49:36 +00001391 return code->ic_state() == DEBUG_BREAK;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001392}
1393
1394
1395// Check whether a code stub with the specified major key is a possible break
1396// point location when looking for source break locations.
1397bool Debug::IsSourceBreakStub(Code* code) {
1398 CodeStub::Major major_key = code->major_key();
1399 return major_key == CodeStub::CallFunction;
1400}
1401
1402
1403// Check whether a code stub with the specified major key is a possible break
1404// location.
1405bool Debug::IsBreakStub(Code* code) {
1406 CodeStub::Major major_key = code->major_key();
1407 return major_key == CodeStub::CallFunction ||
1408 major_key == CodeStub::StackCheck;
1409}
1410
1411
1412// Find the builtin to use for invoking the debug break
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001413Handle<Code> Debug::FindDebugBreak(Handle<Code> code, RelocInfo::Mode mode) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001414 // Find the builtin debug break function matching the calling convention
1415 // used by the call site.
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001416 if (code->is_inline_cache_stub()) {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001417 switch (code->kind()) {
1418 case Code::CALL_IC:
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001419 case Code::KEYED_CALL_IC:
1420 return ComputeCallDebugBreak(code->arguments_count(), code->kind());
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001421
1422 case Code::LOAD_IC:
1423 return Handle<Code>(Builtins::builtin(Builtins::LoadIC_DebugBreak));
1424
1425 case Code::STORE_IC:
1426 return Handle<Code>(Builtins::builtin(Builtins::StoreIC_DebugBreak));
1427
1428 case Code::KEYED_LOAD_IC:
1429 return Handle<Code>(
1430 Builtins::builtin(Builtins::KeyedLoadIC_DebugBreak));
1431
1432 case Code::KEYED_STORE_IC:
1433 return Handle<Code>(
1434 Builtins::builtin(Builtins::KeyedStoreIC_DebugBreak));
1435
1436 default:
1437 UNREACHABLE();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001438 }
1439 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001440 if (RelocInfo::IsConstructCall(mode)) {
1441 Handle<Code> result =
1442 Handle<Code>(Builtins::builtin(Builtins::ConstructCall_DebugBreak));
1443 return result;
1444 }
1445 if (code->kind() == Code::STUB) {
1446 ASSERT(code->major_key() == CodeStub::CallFunction ||
1447 code->major_key() == CodeStub::StackCheck);
1448 Handle<Code> result =
1449 Handle<Code>(Builtins::builtin(Builtins::StubNoRegisters_DebugBreak));
1450 return result;
1451 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001452
1453 UNREACHABLE();
1454 return Handle<Code>::null();
1455}
1456
1457
1458// Simple function for returning the source positions for active break points.
1459Handle<Object> Debug::GetSourceBreakLocations(
1460 Handle<SharedFunctionInfo> shared) {
1461 if (!HasDebugInfo(shared)) return Handle<Object>(Heap::undefined_value());
1462 Handle<DebugInfo> debug_info = GetDebugInfo(shared);
1463 if (debug_info->GetBreakPointCount() == 0) {
1464 return Handle<Object>(Heap::undefined_value());
1465 }
1466 Handle<FixedArray> locations =
1467 Factory::NewFixedArray(debug_info->GetBreakPointCount());
1468 int count = 0;
1469 for (int i = 0; i < debug_info->break_points()->length(); i++) {
1470 if (!debug_info->break_points()->get(i)->IsUndefined()) {
1471 BreakPointInfo* break_point_info =
1472 BreakPointInfo::cast(debug_info->break_points()->get(i));
1473 if (break_point_info->GetBreakPointCount() > 0) {
1474 locations->set(count++, break_point_info->statement_position());
1475 }
1476 }
1477 }
1478 return locations;
1479}
1480
1481
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001482void Debug::NewBreak(StackFrame::Id break_frame_id) {
1483 thread_local_.break_frame_id_ = break_frame_id;
1484 thread_local_.break_id_ = ++thread_local_.break_count_;
1485}
1486
1487
1488void Debug::SetBreak(StackFrame::Id break_frame_id, int break_id) {
1489 thread_local_.break_frame_id_ = break_frame_id;
1490 thread_local_.break_id_ = break_id;
1491}
1492
1493
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001494// Handle stepping into a function.
1495void Debug::HandleStepIn(Handle<JSFunction> function,
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001496 Handle<Object> holder,
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001497 Address fp,
1498 bool is_constructor) {
1499 // If the frame pointer is not supplied by the caller find it.
1500 if (fp == 0) {
1501 StackFrameIterator it;
1502 it.Advance();
1503 // For constructor functions skip another frame.
1504 if (is_constructor) {
1505 ASSERT(it.frame()->is_construct());
1506 it.Advance();
1507 }
1508 fp = it.frame()->fp();
1509 }
1510
1511 // Flood the function with one-shot break points if it is called from where
1512 // step into was requested.
1513 if (fp == Debug::step_in_fp()) {
1514 // Don't allow step into functions in the native context.
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001515 if (!function->IsBuiltin()) {
kasperl@chromium.orgacae3782009-04-11 09:17:08 +00001516 if (function->shared()->code() ==
1517 Builtins::builtin(Builtins::FunctionApply) ||
1518 function->shared()->code() ==
1519 Builtins::builtin(Builtins::FunctionCall)) {
1520 // Handle function.apply and function.call separately to flood the
1521 // function to be called and not the code for Builtins::FunctionApply or
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001522 // Builtins::FunctionCall. The receiver of call/apply is the target
1523 // function.
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001524 if (!holder.is_null() && holder->IsJSFunction() &&
1525 !JSFunction::cast(*holder)->IsBuiltin()) {
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001526 Handle<SharedFunctionInfo> shared_info(
1527 JSFunction::cast(*holder)->shared());
1528 Debug::FloodWithOneShot(shared_info);
kasperl@chromium.orgacae3782009-04-11 09:17:08 +00001529 }
1530 } else {
1531 Debug::FloodWithOneShot(Handle<SharedFunctionInfo>(function->shared()));
1532 }
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001533 }
1534 }
1535}
1536
1537
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001538void Debug::ClearStepping() {
1539 // Clear the various stepping setup.
1540 ClearOneShot();
1541 ClearStepIn();
ager@chromium.orga1645e22009-09-09 19:27:10 +00001542 ClearStepOut();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001543 ClearStepNext();
1544
1545 // Clear multiple step counter.
1546 thread_local_.step_count_ = 0;
1547}
1548
1549// Clears all the one-shot break points that are currently set. Normally this
1550// function is called each time a break point is hit as one shot break points
1551// are used to support stepping.
1552void Debug::ClearOneShot() {
1553 // The current implementation just runs through all the breakpoints. When the
v8.team.kasperl727e9952008-09-02 14:56:44 +00001554 // last break point for a function is removed that function is automatically
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001555 // removed from the list.
1556
1557 DebugInfoListNode* node = debug_info_list_;
1558 while (node != NULL) {
1559 BreakLocationIterator it(node->debug_info(), ALL_BREAK_LOCATIONS);
1560 while (!it.Done()) {
1561 it.ClearOneShot();
1562 it.Next();
1563 }
1564 node = node->next();
1565 }
1566}
1567
1568
1569void Debug::ActivateStepIn(StackFrame* frame) {
ager@chromium.orga1645e22009-09-09 19:27:10 +00001570 ASSERT(!StepOutActive());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001571 thread_local_.step_into_fp_ = frame->fp();
1572}
1573
1574
1575void Debug::ClearStepIn() {
1576 thread_local_.step_into_fp_ = 0;
1577}
1578
1579
ager@chromium.orga1645e22009-09-09 19:27:10 +00001580void Debug::ActivateStepOut(StackFrame* frame) {
1581 ASSERT(!StepInActive());
1582 thread_local_.step_out_fp_ = frame->fp();
1583}
1584
1585
1586void Debug::ClearStepOut() {
1587 thread_local_.step_out_fp_ = 0;
1588}
1589
1590
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001591void Debug::ClearStepNext() {
1592 thread_local_.last_step_action_ = StepNone;
ager@chromium.org236ad962008-09-25 09:45:57 +00001593 thread_local_.last_statement_position_ = RelocInfo::kNoPosition;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001594 thread_local_.last_fp_ = 0;
1595}
1596
1597
kasper.lundbd3ec4e2008-07-09 11:06:54 +00001598// Ensures the debug information is present for shared.
1599bool Debug::EnsureDebugInfo(Handle<SharedFunctionInfo> shared) {
1600 // Return if we already have the debug info for shared.
1601 if (HasDebugInfo(shared)) return true;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001602
kasper.lundbd3ec4e2008-07-09 11:06:54 +00001603 // Ensure shared in compiled. Return false if this failed.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001604 if (!EnsureCompiled(shared, CLEAR_EXCEPTION)) return false;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001605
1606 // Create the debug info object.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001607 Handle<DebugInfo> debug_info = Factory::NewDebugInfo(shared);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001608
1609 // Add debug info to the list.
1610 DebugInfoListNode* node = new DebugInfoListNode(*debug_info);
1611 node->set_next(debug_info_list_);
1612 debug_info_list_ = node;
1613
1614 // Now there is at least one break point.
1615 has_break_points_ = true;
1616
kasper.lundbd3ec4e2008-07-09 11:06:54 +00001617 return true;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001618}
1619
1620
1621void Debug::RemoveDebugInfo(Handle<DebugInfo> debug_info) {
1622 ASSERT(debug_info_list_ != NULL);
1623 // Run through the debug info objects to find this one and remove it.
1624 DebugInfoListNode* prev = NULL;
1625 DebugInfoListNode* current = debug_info_list_;
1626 while (current != NULL) {
1627 if (*current->debug_info() == *debug_info) {
1628 // Unlink from list. If prev is NULL we are looking at the first element.
1629 if (prev == NULL) {
1630 debug_info_list_ = current->next();
1631 } else {
1632 prev->set_next(current->next());
1633 }
1634 current->debug_info()->shared()->set_debug_info(Heap::undefined_value());
1635 delete current;
1636
1637 // If there are no more debug info objects there are not more break
1638 // points.
1639 has_break_points_ = debug_info_list_ != NULL;
1640
1641 return;
1642 }
1643 // Move to next in list.
1644 prev = current;
1645 current = current->next();
1646 }
1647 UNREACHABLE();
1648}
1649
1650
1651void Debug::SetAfterBreakTarget(JavaScriptFrame* frame) {
ager@chromium.org381abbb2009-02-25 13:23:22 +00001652 HandleScope scope;
1653
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001654 // Get the executing function in which the debug break occurred.
1655 Handle<SharedFunctionInfo> shared =
1656 Handle<SharedFunctionInfo>(JSFunction::cast(frame->function())->shared());
kasper.lundbd3ec4e2008-07-09 11:06:54 +00001657 if (!EnsureDebugInfo(shared)) {
1658 // Return if we failed to retrieve the debug info.
1659 return;
1660 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001661 Handle<DebugInfo> debug_info = GetDebugInfo(shared);
1662 Handle<Code> code(debug_info->code());
1663 Handle<Code> original_code(debug_info->original_code());
1664#ifdef DEBUG
1665 // Get the code which is actually executing.
kasperl@chromium.org061ef742009-02-27 12:16:20 +00001666 Handle<Code> frame_code(frame->code());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001667 ASSERT(frame_code.is_identical_to(code));
1668#endif
1669
1670 // Find the call address in the running code. This address holds the call to
1671 // either a DebugBreakXXX or to the debug break return entry code if the
1672 // break point is still active after processing the break point.
ager@chromium.org4af710e2009-09-15 12:20:11 +00001673 Address addr = frame->pc() - Assembler::kCallTargetAddressOffset;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001674
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001675 // Check if the location is at JS exit or debug break slot.
ager@chromium.orga1645e22009-09-09 19:27:10 +00001676 bool at_js_return = false;
1677 bool break_at_js_return_active = false;
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001678 bool at_debug_break_slot = false;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001679 RelocIterator it(debug_info->code());
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001680 while (!it.done() && !at_js_return && !at_debug_break_slot) {
ager@chromium.org236ad962008-09-25 09:45:57 +00001681 if (RelocInfo::IsJSReturn(it.rinfo()->rmode())) {
ager@chromium.orga1645e22009-09-09 19:27:10 +00001682 at_js_return = (it.rinfo()->pc() ==
1683 addr - Assembler::kPatchReturnSequenceAddressOffset);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001684 break_at_js_return_active = it.rinfo()->IsPatchedReturnSequence();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001685 }
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001686 if (RelocInfo::IsDebugBreakSlot(it.rinfo()->rmode())) {
1687 at_debug_break_slot = (it.rinfo()->pc() ==
1688 addr - Assembler::kPatchDebugBreakSlotAddressOffset);
1689 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001690 it.next();
1691 }
1692
1693 // Handle the jump to continue execution after break point depending on the
1694 // break location.
ager@chromium.orga1645e22009-09-09 19:27:10 +00001695 if (at_js_return) {
1696 // If the break point as return is still active jump to the corresponding
1697 // place in the original code. If not the break point was removed during
1698 // break point processing.
1699 if (break_at_js_return_active) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001700 addr += original_code->instruction_start() - code->instruction_start();
1701 }
1702
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00001703 // Move back to where the call instruction sequence started.
1704 thread_local_.after_break_target_ =
1705 addr - Assembler::kPatchReturnSequenceAddressOffset;
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001706 } else if (at_debug_break_slot) {
1707 // Address of where the debug break slot starts.
1708 addr = addr - Assembler::kPatchDebugBreakSlotAddressOffset;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001709
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001710 // Continue just after the slot.
1711 thread_local_.after_break_target_ = addr + Assembler::kDebugBreakSlotLength;
1712 } else if (IsDebugBreak(Assembler::target_address_at(addr))) {
1713 // We now know that there is still a debug break call at the target address,
1714 // so the break point is still there and the original code will hold the
1715 // address to jump to in order to complete the call which is replaced by a
1716 // call to DebugBreakXXX.
1717
1718 // Find the corresponding address in the original code.
1719 addr += original_code->instruction_start() - code->instruction_start();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001720
1721 // Install jump to the call address in the original code. This will be the
1722 // call which was overwritten by the call to DebugBreakXXX.
1723 thread_local_.after_break_target_ = Assembler::target_address_at(addr);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001724 } else {
1725 // There is no longer a break point present. Don't try to look in the
1726 // original code as the running code will have the right address. This takes
1727 // care of the case where the last break point is removed from the function
1728 // and therefore no "original code" is available.
1729 thread_local_.after_break_target_ = Assembler::target_address_at(addr);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001730 }
1731}
1732
1733
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00001734bool Debug::IsBreakAtReturn(JavaScriptFrame* frame) {
1735 HandleScope scope;
1736
1737 // Get the executing function in which the debug break occurred.
1738 Handle<SharedFunctionInfo> shared =
1739 Handle<SharedFunctionInfo>(JSFunction::cast(frame->function())->shared());
1740 if (!EnsureDebugInfo(shared)) {
1741 // Return if we failed to retrieve the debug info.
1742 return false;
1743 }
1744 Handle<DebugInfo> debug_info = GetDebugInfo(shared);
1745 Handle<Code> code(debug_info->code());
1746#ifdef DEBUG
1747 // Get the code which is actually executing.
1748 Handle<Code> frame_code(frame->code());
1749 ASSERT(frame_code.is_identical_to(code));
1750#endif
1751
1752 // Find the call address in the running code.
1753 Address addr = frame->pc() - Assembler::kCallTargetAddressOffset;
1754
1755 // Check if the location is at JS return.
1756 RelocIterator it(debug_info->code());
1757 while (!it.done()) {
1758 if (RelocInfo::IsJSReturn(it.rinfo()->rmode())) {
1759 return (it.rinfo()->pc() ==
1760 addr - Assembler::kPatchReturnSequenceAddressOffset);
1761 }
1762 it.next();
1763 }
1764 return false;
1765}
1766
1767
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001768void Debug::FramesHaveBeenDropped(StackFrame::Id new_break_frame_id,
1769 FrameDropMode mode) {
1770 thread_local_.frame_drop_mode_ = mode;
ager@chromium.org357bf652010-04-12 11:30:10 +00001771 thread_local_.break_frame_id_ = new_break_frame_id;
1772}
1773
1774
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001775bool Debug::IsDebugGlobal(GlobalObject* global) {
1776 return IsLoaded() && global == Debug::debug_context()->global();
1777}
1778
1779
ager@chromium.org32912102009-01-16 10:38:43 +00001780void Debug::ClearMirrorCache() {
ager@chromium.org381abbb2009-02-25 13:23:22 +00001781 HandleScope scope;
ager@chromium.org32912102009-01-16 10:38:43 +00001782 ASSERT(Top::context() == *Debug::debug_context());
1783
1784 // Clear the mirror cache.
1785 Handle<String> function_name =
1786 Factory::LookupSymbol(CStrVector("ClearMirrorCache"));
1787 Handle<Object> fun(Top::global()->GetProperty(*function_name));
1788 ASSERT(fun->IsJSFunction());
1789 bool caught_exception;
1790 Handle<Object> js_object = Execution::TryCall(
1791 Handle<JSFunction>::cast(fun),
1792 Handle<JSObject>(Debug::debug_context()->global()),
1793 0, NULL, &caught_exception);
1794}
1795
1796
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001797void Debug::CreateScriptCache() {
1798 HandleScope scope;
1799
1800 // Perform two GCs to get rid of all unreferenced scripts. The first GC gets
1801 // rid of all the cached script wrappers and the second gets rid of the
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001802 // scripts which are no longer referenced.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00001803 Heap::CollectAllGarbage(false);
1804 Heap::CollectAllGarbage(false);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001805
1806 ASSERT(script_cache_ == NULL);
1807 script_cache_ = new ScriptCache();
1808
1809 // Scan heap for Script objects.
1810 int count = 0;
1811 HeapIterator iterator;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001812 for (HeapObject* obj = iterator.next(); obj != NULL; obj = iterator.next()) {
sgjesse@chromium.org152a0b02009-10-07 13:50:16 +00001813 if (obj->IsScript() && Script::cast(obj)->HasValidSource()) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001814 script_cache_->Add(Handle<Script>(Script::cast(obj)));
1815 count++;
1816 }
1817 }
1818}
1819
1820
1821void Debug::DestroyScriptCache() {
1822 // Get rid of the script cache if it was created.
1823 if (script_cache_ != NULL) {
1824 delete script_cache_;
1825 script_cache_ = NULL;
1826 }
1827}
1828
1829
1830void Debug::AddScriptToScriptCache(Handle<Script> script) {
1831 if (script_cache_ != NULL) {
1832 script_cache_->Add(script);
1833 }
1834}
1835
1836
1837Handle<FixedArray> Debug::GetLoadedScripts() {
1838 // Create and fill the script cache when the loaded scripts is requested for
1839 // the first time.
1840 if (script_cache_ == NULL) {
1841 CreateScriptCache();
1842 }
1843
1844 // If the script cache is not active just return an empty array.
1845 ASSERT(script_cache_ != NULL);
1846 if (script_cache_ == NULL) {
1847 Factory::NewFixedArray(0);
1848 }
1849
1850 // Perform GC to get unreferenced scripts evicted from the cache before
1851 // returning the content.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00001852 Heap::CollectAllGarbage(false);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001853
1854 // Get the scripts from the cache.
1855 return script_cache_->GetScripts();
1856}
1857
1858
1859void Debug::AfterGarbageCollection() {
1860 // Generate events for collected scripts.
1861 if (script_cache_ != NULL) {
1862 script_cache_->ProcessCollectedScripts();
1863 }
1864}
1865
1866
ager@chromium.org71daaf62009-04-01 07:22:49 +00001867Mutex* Debugger::debugger_access_ = OS::CreateMutex();
iposva@chromium.org245aa852009-02-10 00:49:54 +00001868Handle<Object> Debugger::event_listener_ = Handle<Object>();
1869Handle<Object> Debugger::event_listener_data_ = Handle<Object>();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001870bool Debugger::compiling_natives_ = false;
mads.s.agercbaa0602008-08-14 13:41:48 +00001871bool Debugger::is_loading_debugger_ = false;
ager@chromium.org71daaf62009-04-01 07:22:49 +00001872bool Debugger::never_unload_debugger_ = false;
ager@chromium.org5ec48922009-05-05 07:25:34 +00001873v8::Debug::MessageHandler2 Debugger::message_handler_ = NULL;
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001874bool Debugger::debugger_unload_pending_ = false;
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001875v8::Debug::HostDispatchHandler Debugger::host_dispatch_handler_ = NULL;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001876Mutex* Debugger::dispatch_handler_access_ = OS::CreateMutex();
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001877v8::Debug::DebugMessageDispatchHandler
1878 Debugger::debug_message_dispatch_handler_ = NULL;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001879MessageDispatchHelperThread* Debugger::message_dispatch_helper_thread_ = NULL;
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001880int Debugger::host_dispatch_micros_ = 100 * 1000;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001881DebuggerAgent* Debugger::agent_ = NULL;
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00001882LockingCommandMessageQueue Debugger::command_queue_(kQueueInitialSize);
ager@chromium.org41826e72009-03-30 13:30:57 +00001883Semaphore* Debugger::command_received_ = OS::CreateSemaphore(0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001884
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001885
1886Handle<Object> Debugger::MakeJSObject(Vector<const char> constructor_name,
1887 int argc, Object*** argv,
1888 bool* caught_exception) {
1889 ASSERT(Top::context() == *Debug::debug_context());
1890
1891 // Create the execution state object.
1892 Handle<String> constructor_str = Factory::LookupSymbol(constructor_name);
1893 Handle<Object> constructor(Top::global()->GetProperty(*constructor_str));
1894 ASSERT(constructor->IsJSFunction());
1895 if (!constructor->IsJSFunction()) {
1896 *caught_exception = true;
1897 return Factory::undefined_value();
1898 }
1899 Handle<Object> js_object = Execution::TryCall(
1900 Handle<JSFunction>::cast(constructor),
1901 Handle<JSObject>(Debug::debug_context()->global()), argc, argv,
1902 caught_exception);
1903 return js_object;
1904}
1905
1906
1907Handle<Object> Debugger::MakeExecutionState(bool* caught_exception) {
1908 // Create the execution state object.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001909 Handle<Object> break_id = Factory::NewNumberFromInt(Debug::break_id());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001910 const int argc = 1;
1911 Object** argv[argc] = { break_id.location() };
1912 return MakeJSObject(CStrVector("MakeExecutionState"),
1913 argc, argv, caught_exception);
1914}
1915
1916
1917Handle<Object> Debugger::MakeBreakEvent(Handle<Object> exec_state,
1918 Handle<Object> break_points_hit,
1919 bool* caught_exception) {
1920 // Create the new break event object.
1921 const int argc = 2;
1922 Object** argv[argc] = { exec_state.location(),
1923 break_points_hit.location() };
1924 return MakeJSObject(CStrVector("MakeBreakEvent"),
1925 argc,
1926 argv,
1927 caught_exception);
1928}
1929
1930
1931Handle<Object> Debugger::MakeExceptionEvent(Handle<Object> exec_state,
1932 Handle<Object> exception,
1933 bool uncaught,
1934 bool* caught_exception) {
1935 // Create the new exception event object.
1936 const int argc = 3;
1937 Object** argv[argc] = { exec_state.location(),
1938 exception.location(),
1939 uncaught ? Factory::true_value().location() :
1940 Factory::false_value().location()};
1941 return MakeJSObject(CStrVector("MakeExceptionEvent"),
1942 argc, argv, caught_exception);
1943}
1944
1945
1946Handle<Object> Debugger::MakeNewFunctionEvent(Handle<Object> function,
1947 bool* caught_exception) {
1948 // Create the new function event object.
1949 const int argc = 1;
1950 Object** argv[argc] = { function.location() };
1951 return MakeJSObject(CStrVector("MakeNewFunctionEvent"),
1952 argc, argv, caught_exception);
1953}
1954
1955
1956Handle<Object> Debugger::MakeCompileEvent(Handle<Script> script,
iposva@chromium.org245aa852009-02-10 00:49:54 +00001957 bool before,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001958 bool* caught_exception) {
1959 // Create the compile event object.
1960 Handle<Object> exec_state = MakeExecutionState(caught_exception);
iposva@chromium.org245aa852009-02-10 00:49:54 +00001961 Handle<Object> script_wrapper = GetScriptWrapper(script);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001962 const int argc = 3;
iposva@chromium.org245aa852009-02-10 00:49:54 +00001963 Object** argv[argc] = { exec_state.location(),
1964 script_wrapper.location(),
1965 before ? Factory::true_value().location() :
1966 Factory::false_value().location() };
1967
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001968 return MakeJSObject(CStrVector("MakeCompileEvent"),
1969 argc,
1970 argv,
1971 caught_exception);
1972}
1973
1974
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001975Handle<Object> Debugger::MakeScriptCollectedEvent(int id,
1976 bool* caught_exception) {
1977 // Create the script collected event object.
1978 Handle<Object> exec_state = MakeExecutionState(caught_exception);
1979 Handle<Object> id_object = Handle<Smi>(Smi::FromInt(id));
1980 const int argc = 2;
1981 Object** argv[argc] = { exec_state.location(), id_object.location() };
1982
1983 return MakeJSObject(CStrVector("MakeScriptCollectedEvent"),
1984 argc,
1985 argv,
1986 caught_exception);
1987}
1988
1989
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001990void Debugger::OnException(Handle<Object> exception, bool uncaught) {
1991 HandleScope scope;
1992
1993 // Bail out based on state or if there is no listener for this event
1994 if (Debug::InDebugger()) return;
1995 if (!Debugger::EventActive(v8::Exception)) return;
1996
1997 // Bail out if exception breaks are not active
1998 if (uncaught) {
1999 // Uncaught exceptions are reported by either flags.
2000 if (!(Debug::break_on_uncaught_exception() ||
2001 Debug::break_on_exception())) return;
2002 } else {
2003 // Caught exceptions are reported is activated.
2004 if (!Debug::break_on_exception()) return;
2005 }
2006
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002007 // Enter the debugger.
2008 EnterDebugger debugger;
2009 if (debugger.FailedToEnter()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002010
2011 // Clear all current stepping setup.
2012 Debug::ClearStepping();
2013 // Create the event data object.
2014 bool caught_exception = false;
2015 Handle<Object> exec_state = MakeExecutionState(&caught_exception);
2016 Handle<Object> event_data;
2017 if (!caught_exception) {
2018 event_data = MakeExceptionEvent(exec_state, exception, uncaught,
2019 &caught_exception);
2020 }
2021 // Bail out and don't call debugger if exception.
2022 if (caught_exception) {
2023 return;
2024 }
2025
ager@chromium.org5ec48922009-05-05 07:25:34 +00002026 // Process debug event.
2027 ProcessDebugEvent(v8::Exception, Handle<JSObject>::cast(event_data), false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002028 // Return to continue execution from where the exception was thrown.
2029}
2030
2031
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002032void Debugger::OnDebugBreak(Handle<Object> break_points_hit,
2033 bool auto_continue) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002034 HandleScope scope;
2035
kasper.lund212ac232008-07-16 07:07:30 +00002036 // Debugger has already been entered by caller.
2037 ASSERT(Top::context() == *Debug::debug_context());
2038
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002039 // Bail out if there is no listener for this event
2040 if (!Debugger::EventActive(v8::Break)) return;
2041
2042 // Debugger must be entered in advance.
2043 ASSERT(Top::context() == *Debug::debug_context());
2044
2045 // Create the event data object.
2046 bool caught_exception = false;
2047 Handle<Object> exec_state = MakeExecutionState(&caught_exception);
2048 Handle<Object> event_data;
2049 if (!caught_exception) {
2050 event_data = MakeBreakEvent(exec_state, break_points_hit,
2051 &caught_exception);
2052 }
2053 // Bail out and don't call debugger if exception.
2054 if (caught_exception) {
2055 return;
2056 }
2057
ager@chromium.org5ec48922009-05-05 07:25:34 +00002058 // Process debug event.
2059 ProcessDebugEvent(v8::Break,
2060 Handle<JSObject>::cast(event_data),
2061 auto_continue);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002062}
2063
2064
2065void Debugger::OnBeforeCompile(Handle<Script> script) {
2066 HandleScope scope;
2067
2068 // Bail out based on state or if there is no listener for this event
2069 if (Debug::InDebugger()) return;
2070 if (compiling_natives()) return;
2071 if (!EventActive(v8::BeforeCompile)) return;
2072
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002073 // Enter the debugger.
2074 EnterDebugger debugger;
2075 if (debugger.FailedToEnter()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002076
2077 // Create the event data object.
2078 bool caught_exception = false;
iposva@chromium.org245aa852009-02-10 00:49:54 +00002079 Handle<Object> event_data = MakeCompileEvent(script, true, &caught_exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002080 // Bail out and don't call debugger if exception.
2081 if (caught_exception) {
2082 return;
2083 }
2084
ager@chromium.org5ec48922009-05-05 07:25:34 +00002085 // Process debug event.
2086 ProcessDebugEvent(v8::BeforeCompile,
2087 Handle<JSObject>::cast(event_data),
2088 true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002089}
2090
2091
2092// Handle debugger actions when a new script is compiled.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00002093void Debugger::OnAfterCompile(Handle<Script> script,
2094 AfterCompileFlags after_compile_flags) {
kasper.lund212ac232008-07-16 07:07:30 +00002095 HandleScope scope;
2096
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002097 // Add the newly compiled script to the script cache.
2098 Debug::AddScriptToScriptCache(script);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002099
2100 // No more to do if not debugging.
ager@chromium.org71daaf62009-04-01 07:22:49 +00002101 if (!IsDebuggerActive()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002102
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002103 // No compile events while compiling natives.
2104 if (compiling_natives()) return;
2105
iposva@chromium.org245aa852009-02-10 00:49:54 +00002106 // Store whether in debugger before entering debugger.
2107 bool in_debugger = Debug::InDebugger();
2108
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002109 // Enter the debugger.
2110 EnterDebugger debugger;
2111 if (debugger.FailedToEnter()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002112
2113 // If debugging there might be script break points registered for this
2114 // script. Make sure that these break points are set.
2115
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002116 // Get the function UpdateScriptBreakPoints (defined in debug-debugger.js).
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002117 Handle<Object> update_script_break_points =
2118 Handle<Object>(Debug::debug_context()->global()->GetProperty(
2119 *Factory::LookupAsciiSymbol("UpdateScriptBreakPoints")));
2120 if (!update_script_break_points->IsJSFunction()) {
2121 return;
2122 }
2123 ASSERT(update_script_break_points->IsJSFunction());
2124
2125 // Wrap the script object in a proper JS object before passing it
2126 // to JavaScript.
2127 Handle<JSValue> wrapper = GetScriptWrapper(script);
2128
2129 // Call UpdateScriptBreakPoints expect no exceptions.
kasper.lund212ac232008-07-16 07:07:30 +00002130 bool caught_exception = false;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002131 const int argc = 1;
2132 Object** argv[argc] = { reinterpret_cast<Object**>(wrapper.location()) };
2133 Handle<Object> result = Execution::TryCall(
2134 Handle<JSFunction>::cast(update_script_break_points),
2135 Top::builtins(), argc, argv,
2136 &caught_exception);
2137 if (caught_exception) {
2138 return;
2139 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002140 // Bail out based on state or if there is no listener for this event
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00002141 if (in_debugger && (after_compile_flags & SEND_WHEN_DEBUGGING) == 0) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002142 if (!Debugger::EventActive(v8::AfterCompile)) return;
2143
2144 // Create the compile state object.
2145 Handle<Object> event_data = MakeCompileEvent(script,
iposva@chromium.org245aa852009-02-10 00:49:54 +00002146 false,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002147 &caught_exception);
2148 // Bail out and don't call debugger if exception.
2149 if (caught_exception) {
2150 return;
2151 }
ager@chromium.org5ec48922009-05-05 07:25:34 +00002152 // Process debug event.
2153 ProcessDebugEvent(v8::AfterCompile,
2154 Handle<JSObject>::cast(event_data),
2155 true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002156}
2157
2158
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002159void Debugger::OnScriptCollected(int id) {
2160 HandleScope scope;
2161
2162 // No more to do if not debugging.
2163 if (!IsDebuggerActive()) return;
2164 if (!Debugger::EventActive(v8::ScriptCollected)) return;
2165
2166 // Enter the debugger.
2167 EnterDebugger debugger;
2168 if (debugger.FailedToEnter()) return;
2169
2170 // Create the script collected state object.
2171 bool caught_exception = false;
2172 Handle<Object> event_data = MakeScriptCollectedEvent(id,
2173 &caught_exception);
2174 // Bail out and don't call debugger if exception.
2175 if (caught_exception) {
2176 return;
2177 }
2178
2179 // Process debug event.
2180 ProcessDebugEvent(v8::ScriptCollected,
2181 Handle<JSObject>::cast(event_data),
2182 true);
2183}
2184
2185
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002186void Debugger::ProcessDebugEvent(v8::DebugEvent event,
ager@chromium.org5ec48922009-05-05 07:25:34 +00002187 Handle<JSObject> event_data,
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002188 bool auto_continue) {
ager@chromium.org381abbb2009-02-25 13:23:22 +00002189 HandleScope scope;
2190
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +00002191 // Clear any pending debug break if this is a real break.
2192 if (!auto_continue) {
2193 Debug::clear_interrupt_pending(DEBUGBREAK);
2194 }
2195
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002196 // Create the execution state.
2197 bool caught_exception = false;
2198 Handle<Object> exec_state = MakeExecutionState(&caught_exception);
2199 if (caught_exception) {
2200 return;
2201 }
ager@chromium.org41826e72009-03-30 13:30:57 +00002202 // First notify the message handler if any.
2203 if (message_handler_ != NULL) {
ager@chromium.org5ec48922009-05-05 07:25:34 +00002204 NotifyMessageHandler(event,
2205 Handle<JSObject>::cast(exec_state),
2206 event_data,
2207 auto_continue);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002208 }
iposva@chromium.org245aa852009-02-10 00:49:54 +00002209 // Notify registered debug event listener. This can be either a C or a
2210 // JavaScript function.
2211 if (!event_listener_.is_null()) {
2212 if (event_listener_->IsProxy()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002213 // C debug event listener.
iposva@chromium.org245aa852009-02-10 00:49:54 +00002214 Handle<Proxy> callback_obj(Handle<Proxy>::cast(event_listener_));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002215 v8::Debug::EventCallback2 callback =
2216 FUNCTION_CAST<v8::Debug::EventCallback2>(callback_obj->proxy());
2217 EventDetailsImpl event_details(
2218 event,
2219 Handle<JSObject>::cast(exec_state),
2220 event_data,
2221 event_listener_data_);
2222 callback(event_details);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002223 } else {
2224 // JavaScript debug event listener.
iposva@chromium.org245aa852009-02-10 00:49:54 +00002225 ASSERT(event_listener_->IsJSFunction());
2226 Handle<JSFunction> fun(Handle<JSFunction>::cast(event_listener_));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002227
2228 // Invoke the JavaScript debug event listener.
2229 const int argc = 4;
2230 Object** argv[argc] = { Handle<Object>(Smi::FromInt(event)).location(),
2231 exec_state.location(),
ager@chromium.org5ec48922009-05-05 07:25:34 +00002232 Handle<Object>::cast(event_data).location(),
iposva@chromium.org245aa852009-02-10 00:49:54 +00002233 event_listener_data_.location() };
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002234 Handle<Object> result = Execution::TryCall(fun, Top::global(),
2235 argc, argv, &caught_exception);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002236 // Silently ignore exceptions from debug event listeners.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002237 }
2238 }
2239}
2240
2241
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00002242Handle<Context> Debugger::GetDebugContext() {
2243 never_unload_debugger_ = true;
2244 EnterDebugger debugger;
2245 return Debug::debug_context();
2246}
2247
2248
ager@chromium.org71daaf62009-04-01 07:22:49 +00002249void Debugger::UnloadDebugger() {
2250 // Make sure that there are no breakpoints left.
2251 Debug::ClearAllBreakPoints();
2252
2253 // Unload the debugger if feasible.
2254 if (!never_unload_debugger_) {
2255 Debug::Unload();
2256 }
2257
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002258 // Clear the flag indicating that the debugger should be unloaded.
2259 debugger_unload_pending_ = false;
ager@chromium.org71daaf62009-04-01 07:22:49 +00002260}
2261
2262
ager@chromium.org41826e72009-03-30 13:30:57 +00002263void Debugger::NotifyMessageHandler(v8::DebugEvent event,
ager@chromium.org5ec48922009-05-05 07:25:34 +00002264 Handle<JSObject> exec_state,
2265 Handle<JSObject> event_data,
ager@chromium.org41826e72009-03-30 13:30:57 +00002266 bool auto_continue) {
2267 HandleScope scope;
2268
2269 if (!Debug::Load()) return;
2270
2271 // Process the individual events.
ager@chromium.org5ec48922009-05-05 07:25:34 +00002272 bool sendEventMessage = false;
ager@chromium.org41826e72009-03-30 13:30:57 +00002273 switch (event) {
2274 case v8::Break:
ager@chromium.org5ec48922009-05-05 07:25:34 +00002275 sendEventMessage = !auto_continue;
ager@chromium.org41826e72009-03-30 13:30:57 +00002276 break;
2277 case v8::Exception:
ager@chromium.org5ec48922009-05-05 07:25:34 +00002278 sendEventMessage = true;
ager@chromium.org41826e72009-03-30 13:30:57 +00002279 break;
2280 case v8::BeforeCompile:
2281 break;
2282 case v8::AfterCompile:
ager@chromium.org5ec48922009-05-05 07:25:34 +00002283 sendEventMessage = true;
ager@chromium.org41826e72009-03-30 13:30:57 +00002284 break;
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002285 case v8::ScriptCollected:
2286 sendEventMessage = true;
2287 break;
ager@chromium.org41826e72009-03-30 13:30:57 +00002288 case v8::NewFunction:
2289 break;
2290 default:
2291 UNREACHABLE();
2292 }
2293
ager@chromium.org5ec48922009-05-05 07:25:34 +00002294 // The debug command interrupt flag might have been set when the command was
2295 // added. It should be enough to clear the flag only once while we are in the
2296 // debugger.
2297 ASSERT(Debug::InDebugger());
2298 StackGuard::Continue(DEBUGCOMMAND);
2299
2300 // Notify the debugger that a debug event has occurred unless auto continue is
2301 // active in which case no event is send.
2302 if (sendEventMessage) {
2303 MessageImpl message = MessageImpl::NewEvent(
2304 event,
2305 auto_continue,
2306 Handle<JSObject>::cast(exec_state),
2307 Handle<JSObject>::cast(event_data));
2308 InvokeMessageHandler(message);
2309 }
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +00002310
2311 // If auto continue don't make the event cause a break, but process messages
2312 // in the queue if any. For script collected events don't even process
2313 // messages in the queue as the execution state might not be what is expected
2314 // by the client.
ager@chromium.org6ffc2172009-05-29 19:20:16 +00002315 if ((auto_continue && !HasCommands()) || event == v8::ScriptCollected) {
ager@chromium.org5ec48922009-05-05 07:25:34 +00002316 return;
2317 }
ager@chromium.org41826e72009-03-30 13:30:57 +00002318
ager@chromium.org41826e72009-03-30 13:30:57 +00002319 v8::TryCatch try_catch;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002320
2321 // DebugCommandProcessor goes here.
2322 v8::Local<v8::Object> cmd_processor;
2323 {
2324 v8::Local<v8::Object> api_exec_state =
2325 v8::Utils::ToLocal(Handle<JSObject>::cast(exec_state));
2326 v8::Local<v8::String> fun_name =
2327 v8::String::New("debugCommandProcessor");
2328 v8::Local<v8::Function> fun =
2329 v8::Function::Cast(*api_exec_state->Get(fun_name));
2330
2331 v8::Handle<v8::Boolean> running =
2332 auto_continue ? v8::True() : v8::False();
2333 static const int kArgc = 1;
2334 v8::Handle<Value> argv[kArgc] = { running };
2335 cmd_processor = v8::Object::Cast(*fun->Call(api_exec_state, kArgc, argv));
2336 if (try_catch.HasCaught()) {
2337 PrintLn(try_catch.Exception());
2338 return;
2339 }
ager@chromium.org41826e72009-03-30 13:30:57 +00002340 }
2341
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002342 bool running = auto_continue;
2343
ager@chromium.org41826e72009-03-30 13:30:57 +00002344 // Process requests from the debugger.
2345 while (true) {
2346 // Wait for new command in the queue.
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002347 if (Debugger::host_dispatch_handler_) {
2348 // In case there is a host dispatch - do periodic dispatches.
2349 if (!command_received_->Wait(host_dispatch_micros_)) {
2350 // Timout expired, do the dispatch.
2351 Debugger::host_dispatch_handler_();
2352 continue;
2353 }
2354 } else {
2355 // In case there is no host dispatch - just wait.
2356 command_received_->Wait();
2357 }
ager@chromium.org41826e72009-03-30 13:30:57 +00002358
ager@chromium.org41826e72009-03-30 13:30:57 +00002359 // Get the command from the queue.
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00002360 CommandMessage command = command_queue_.Get();
ager@chromium.org41826e72009-03-30 13:30:57 +00002361 Logger::DebugTag("Got request from command queue, in interactive loop.");
ager@chromium.org71daaf62009-04-01 07:22:49 +00002362 if (!Debugger::IsDebuggerActive()) {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002363 // Delete command text and user data.
2364 command.Dispose();
ager@chromium.org41826e72009-03-30 13:30:57 +00002365 return;
2366 }
2367
ager@chromium.org41826e72009-03-30 13:30:57 +00002368 // Invoke JavaScript to process the debug request.
2369 v8::Local<v8::String> fun_name;
2370 v8::Local<v8::Function> fun;
2371 v8::Local<v8::Value> request;
2372 v8::TryCatch try_catch;
2373 fun_name = v8::String::New("processDebugRequest");
2374 fun = v8::Function::Cast(*cmd_processor->Get(fun_name));
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002375
2376 request = v8::String::New(command.text().start(),
2377 command.text().length());
ager@chromium.org41826e72009-03-30 13:30:57 +00002378 static const int kArgc = 1;
2379 v8::Handle<Value> argv[kArgc] = { request };
2380 v8::Local<v8::Value> response_val = fun->Call(cmd_processor, kArgc, argv);
2381
2382 // Get the response.
2383 v8::Local<v8::String> response;
ager@chromium.org41826e72009-03-30 13:30:57 +00002384 if (!try_catch.HasCaught()) {
2385 // Get response string.
2386 if (!response_val->IsUndefined()) {
2387 response = v8::String::Cast(*response_val);
2388 } else {
2389 response = v8::String::New("");
2390 }
2391
2392 // Log the JSON request/response.
2393 if (FLAG_trace_debug_json) {
2394 PrintLn(request);
2395 PrintLn(response);
2396 }
2397
2398 // Get the running state.
2399 fun_name = v8::String::New("isRunning");
2400 fun = v8::Function::Cast(*cmd_processor->Get(fun_name));
2401 static const int kArgc = 1;
2402 v8::Handle<Value> argv[kArgc] = { response };
2403 v8::Local<v8::Value> running_val = fun->Call(cmd_processor, kArgc, argv);
2404 if (!try_catch.HasCaught()) {
2405 running = running_val->ToBoolean()->Value();
2406 }
2407 } else {
2408 // In case of failure the result text is the exception text.
2409 response = try_catch.Exception()->ToString();
2410 }
2411
ager@chromium.org41826e72009-03-30 13:30:57 +00002412 // Return the result.
ager@chromium.org5ec48922009-05-05 07:25:34 +00002413 MessageImpl message = MessageImpl::NewResponse(
2414 event,
2415 running,
2416 Handle<JSObject>::cast(exec_state),
2417 Handle<JSObject>::cast(event_data),
2418 Handle<String>(Utils::OpenHandle(*response)),
2419 command.client_data());
2420 InvokeMessageHandler(message);
2421 command.Dispose();
ager@chromium.org41826e72009-03-30 13:30:57 +00002422
2423 // Return from debug event processing if either the VM is put into the
2424 // runnning state (through a continue command) or auto continue is active
2425 // and there are no more commands queued.
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002426 if (running && !HasCommands()) {
ager@chromium.org41826e72009-03-30 13:30:57 +00002427 return;
2428 }
2429 }
2430}
2431
2432
iposva@chromium.org245aa852009-02-10 00:49:54 +00002433void Debugger::SetEventListener(Handle<Object> callback,
2434 Handle<Object> data) {
2435 HandleScope scope;
2436
2437 // Clear the global handles for the event listener and the event listener data
2438 // object.
2439 if (!event_listener_.is_null()) {
2440 GlobalHandles::Destroy(
2441 reinterpret_cast<Object**>(event_listener_.location()));
2442 event_listener_ = Handle<Object>();
2443 }
2444 if (!event_listener_data_.is_null()) {
2445 GlobalHandles::Destroy(
2446 reinterpret_cast<Object**>(event_listener_data_.location()));
2447 event_listener_data_ = Handle<Object>();
2448 }
2449
2450 // If there is a new debug event listener register it together with its data
2451 // object.
2452 if (!callback->IsUndefined() && !callback->IsNull()) {
2453 event_listener_ = Handle<Object>::cast(GlobalHandles::Create(*callback));
2454 if (data.is_null()) {
2455 data = Factory::undefined_value();
2456 }
2457 event_listener_data_ = Handle<Object>::cast(GlobalHandles::Create(*data));
2458 }
2459
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002460 ListenersChanged();
iposva@chromium.org245aa852009-02-10 00:49:54 +00002461}
2462
2463
ager@chromium.org5ec48922009-05-05 07:25:34 +00002464void Debugger::SetMessageHandler(v8::Debug::MessageHandler2 handler) {
ager@chromium.org71daaf62009-04-01 07:22:49 +00002465 ScopedLock with(debugger_access_);
2466
ager@chromium.org381abbb2009-02-25 13:23:22 +00002467 message_handler_ = handler;
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002468 ListenersChanged();
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00002469 if (handler == NULL) {
ager@chromium.org71daaf62009-04-01 07:22:49 +00002470 // Send an empty command to the debugger if in a break to make JavaScript
2471 // run again if the debugger is closed.
2472 if (Debug::InDebugger()) {
2473 ProcessCommand(Vector<const uint16_t>::empty());
2474 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002475 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002476}
2477
2478
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002479void Debugger::ListenersChanged() {
2480 if (IsDebuggerActive()) {
2481 // Disable the compilation cache when the debugger is active.
2482 CompilationCache::Disable();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002483 debugger_unload_pending_ = false;
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002484 } else {
2485 CompilationCache::Enable();
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002486 // Unload the debugger if event listener and message handler cleared.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002487 // Schedule this for later, because we may be in non-V8 thread.
2488 debugger_unload_pending_ = true;
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002489 }
2490}
2491
2492
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002493void Debugger::SetHostDispatchHandler(v8::Debug::HostDispatchHandler handler,
2494 int period) {
ager@chromium.org381abbb2009-02-25 13:23:22 +00002495 host_dispatch_handler_ = handler;
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002496 host_dispatch_micros_ = period * 1000;
ager@chromium.org381abbb2009-02-25 13:23:22 +00002497}
2498
2499
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002500void Debugger::SetDebugMessageDispatchHandler(
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002501 v8::Debug::DebugMessageDispatchHandler handler, bool provide_locker) {
2502 ScopedLock with(dispatch_handler_access_);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002503 debug_message_dispatch_handler_ = handler;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002504
2505 if (provide_locker && message_dispatch_helper_thread_ == NULL) {
2506 message_dispatch_helper_thread_ = new MessageDispatchHelperThread;
2507 message_dispatch_helper_thread_->Start();
2508 }
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002509}
2510
2511
ager@chromium.org41826e72009-03-30 13:30:57 +00002512// Calls the registered debug message handler. This callback is part of the
ager@chromium.org5ec48922009-05-05 07:25:34 +00002513// public API.
2514void Debugger::InvokeMessageHandler(MessageImpl message) {
ager@chromium.org71daaf62009-04-01 07:22:49 +00002515 ScopedLock with(debugger_access_);
2516
ager@chromium.org381abbb2009-02-25 13:23:22 +00002517 if (message_handler_ != NULL) {
ager@chromium.org5ec48922009-05-05 07:25:34 +00002518 message_handler_(message);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002519 }
ager@chromium.org41826e72009-03-30 13:30:57 +00002520}
2521
2522
2523// Puts a command coming from the public API on the queue. Creates
2524// a copy of the command string managed by the debugger. Up to this
2525// point, the command data was managed by the API client. Called
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00002526// by the API client thread.
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002527void Debugger::ProcessCommand(Vector<const uint16_t> command,
2528 v8::Debug::ClientData* client_data) {
2529 // Need to cast away const.
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00002530 CommandMessage message = CommandMessage::New(
ager@chromium.org41826e72009-03-30 13:30:57 +00002531 Vector<uint16_t>(const_cast<uint16_t*>(command.start()),
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002532 command.length()),
2533 client_data);
ager@chromium.org41826e72009-03-30 13:30:57 +00002534 Logger::DebugTag("Put command on command_queue.");
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002535 command_queue_.Put(message);
ager@chromium.org41826e72009-03-30 13:30:57 +00002536 command_received_->Signal();
sgjesse@chromium.org3afc1582009-04-16 22:31:44 +00002537
2538 // Set the debug command break flag to have the command processed.
ager@chromium.org41826e72009-03-30 13:30:57 +00002539 if (!Debug::InDebugger()) {
2540 StackGuard::DebugCommand();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002541 }
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002542
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002543 MessageDispatchHelperThread* dispatch_thread;
2544 {
2545 ScopedLock with(dispatch_handler_access_);
2546 dispatch_thread = message_dispatch_helper_thread_;
2547 }
2548
2549 if (dispatch_thread == NULL) {
2550 CallMessageDispatchHandler();
2551 } else {
2552 dispatch_thread->Schedule();
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002553 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002554}
2555
2556
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002557bool Debugger::HasCommands() {
ager@chromium.org41826e72009-03-30 13:30:57 +00002558 return !command_queue_.IsEmpty();
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002559}
2560
2561
ager@chromium.org71daaf62009-04-01 07:22:49 +00002562bool Debugger::IsDebuggerActive() {
2563 ScopedLock with(debugger_access_);
2564
2565 return message_handler_ != NULL || !event_listener_.is_null();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002566}
2567
2568
ager@chromium.orga74f0da2008-12-03 16:05:52 +00002569Handle<Object> Debugger::Call(Handle<JSFunction> fun,
2570 Handle<Object> data,
2571 bool* pending_exception) {
ager@chromium.org71daaf62009-04-01 07:22:49 +00002572 // When calling functions in the debugger prevent it from beeing unloaded.
2573 Debugger::never_unload_debugger_ = true;
2574
ager@chromium.orga74f0da2008-12-03 16:05:52 +00002575 // Enter the debugger.
2576 EnterDebugger debugger;
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +00002577 if (debugger.FailedToEnter()) {
ager@chromium.orga74f0da2008-12-03 16:05:52 +00002578 return Factory::undefined_value();
2579 }
2580
2581 // Create the execution state.
2582 bool caught_exception = false;
2583 Handle<Object> exec_state = MakeExecutionState(&caught_exception);
2584 if (caught_exception) {
2585 return Factory::undefined_value();
2586 }
2587
2588 static const int kArgc = 2;
2589 Object** argv[kArgc] = { exec_state.location(), data.location() };
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +00002590 Handle<Object> result = Execution::Call(
2591 fun,
2592 Handle<Object>(Debug::debug_context_->global_proxy()),
2593 kArgc,
2594 argv,
2595 pending_exception);
ager@chromium.orga74f0da2008-12-03 16:05:52 +00002596 return result;
2597}
2598
2599
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002600static void StubMessageHandler2(const v8::Debug::Message& message) {
2601 // Simply ignore message.
2602}
2603
2604
2605bool Debugger::StartAgent(const char* name, int port,
2606 bool wait_for_connection) {
2607 if (wait_for_connection) {
2608 // Suspend V8 if it is already running or set V8 to suspend whenever
2609 // it starts.
2610 // Provide stub message handler; V8 auto-continues each suspend
2611 // when there is no message handler; we doesn't need it.
2612 // Once become suspended, V8 will stay so indefinitely long, until remote
2613 // debugger connects and issues "continue" command.
2614 Debugger::message_handler_ = StubMessageHandler2;
2615 v8::Debug::DebugBreak();
2616 }
2617
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002618 if (Socket::Setup()) {
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002619 agent_ = new DebuggerAgent(name, port);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002620 agent_->Start();
2621 return true;
2622 }
2623
2624 return false;
2625}
2626
2627
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002628void Debugger::StopAgent() {
2629 if (agent_ != NULL) {
2630 agent_->Shutdown();
2631 agent_->Join();
2632 delete agent_;
2633 agent_ = NULL;
2634 }
2635}
2636
2637
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002638void Debugger::WaitForAgent() {
2639 if (agent_ != NULL)
2640 agent_->WaitUntilListening();
2641}
2642
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002643
2644void Debugger::CallMessageDispatchHandler() {
2645 v8::Debug::DebugMessageDispatchHandler handler;
2646 {
2647 ScopedLock with(dispatch_handler_access_);
2648 handler = Debugger::debug_message_dispatch_handler_;
2649 }
2650 if (handler != NULL) {
2651 handler();
2652 }
2653}
2654
2655
ager@chromium.org5ec48922009-05-05 07:25:34 +00002656MessageImpl MessageImpl::NewEvent(DebugEvent event,
2657 bool running,
2658 Handle<JSObject> exec_state,
2659 Handle<JSObject> event_data) {
2660 MessageImpl message(true, event, running,
2661 exec_state, event_data, Handle<String>(), NULL);
2662 return message;
2663}
2664
2665
2666MessageImpl MessageImpl::NewResponse(DebugEvent event,
2667 bool running,
2668 Handle<JSObject> exec_state,
2669 Handle<JSObject> event_data,
2670 Handle<String> response_json,
2671 v8::Debug::ClientData* client_data) {
2672 MessageImpl message(false, event, running,
2673 exec_state, event_data, response_json, client_data);
2674 return message;
2675}
2676
2677
2678MessageImpl::MessageImpl(bool is_event,
2679 DebugEvent event,
2680 bool running,
2681 Handle<JSObject> exec_state,
2682 Handle<JSObject> event_data,
2683 Handle<String> response_json,
2684 v8::Debug::ClientData* client_data)
2685 : is_event_(is_event),
2686 event_(event),
2687 running_(running),
2688 exec_state_(exec_state),
2689 event_data_(event_data),
2690 response_json_(response_json),
2691 client_data_(client_data) {}
2692
2693
2694bool MessageImpl::IsEvent() const {
2695 return is_event_;
2696}
2697
2698
2699bool MessageImpl::IsResponse() const {
2700 return !is_event_;
2701}
2702
2703
2704DebugEvent MessageImpl::GetEvent() const {
2705 return event_;
2706}
2707
2708
2709bool MessageImpl::WillStartRunning() const {
2710 return running_;
2711}
2712
2713
2714v8::Handle<v8::Object> MessageImpl::GetExecutionState() const {
2715 return v8::Utils::ToLocal(exec_state_);
2716}
2717
2718
2719v8::Handle<v8::Object> MessageImpl::GetEventData() const {
2720 return v8::Utils::ToLocal(event_data_);
2721}
2722
2723
2724v8::Handle<v8::String> MessageImpl::GetJSON() const {
2725 v8::HandleScope scope;
2726
2727 if (IsEvent()) {
2728 // Call toJSONProtocol on the debug event object.
2729 Handle<Object> fun = GetProperty(event_data_, "toJSONProtocol");
2730 if (!fun->IsJSFunction()) {
2731 return v8::Handle<v8::String>();
2732 }
2733 bool caught_exception;
2734 Handle<Object> json = Execution::TryCall(Handle<JSFunction>::cast(fun),
2735 event_data_,
2736 0, NULL, &caught_exception);
2737 if (caught_exception || !json->IsString()) {
2738 return v8::Handle<v8::String>();
2739 }
2740 return scope.Close(v8::Utils::ToLocal(Handle<String>::cast(json)));
2741 } else {
2742 return v8::Utils::ToLocal(response_json_);
2743 }
2744}
2745
2746
2747v8::Handle<v8::Context> MessageImpl::GetEventContext() const {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002748 v8::Handle<v8::Context> context = GetDebugEventContext();
2749 // Top::context() may be NULL when "script collected" event occures.
2750 ASSERT(!context.IsEmpty() || event_ == v8::ScriptCollected);
2751 return GetDebugEventContext();
ager@chromium.org5ec48922009-05-05 07:25:34 +00002752}
2753
2754
2755v8::Debug::ClientData* MessageImpl::GetClientData() const {
2756 return client_data_;
2757}
2758
2759
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002760EventDetailsImpl::EventDetailsImpl(DebugEvent event,
2761 Handle<JSObject> exec_state,
2762 Handle<JSObject> event_data,
2763 Handle<Object> callback_data)
2764 : event_(event),
2765 exec_state_(exec_state),
2766 event_data_(event_data),
2767 callback_data_(callback_data) {}
2768
2769
2770DebugEvent EventDetailsImpl::GetEvent() const {
2771 return event_;
2772}
2773
2774
2775v8::Handle<v8::Object> EventDetailsImpl::GetExecutionState() const {
2776 return v8::Utils::ToLocal(exec_state_);
2777}
2778
2779
2780v8::Handle<v8::Object> EventDetailsImpl::GetEventData() const {
2781 return v8::Utils::ToLocal(event_data_);
2782}
2783
2784
2785v8::Handle<v8::Context> EventDetailsImpl::GetEventContext() const {
2786 return GetDebugEventContext();
2787}
2788
2789
2790v8::Handle<v8::Value> EventDetailsImpl::GetCallbackData() const {
2791 return v8::Utils::ToLocal(callback_data_);
2792}
2793
2794
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00002795CommandMessage::CommandMessage() : text_(Vector<uint16_t>::empty()),
2796 client_data_(NULL) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002797}
2798
2799
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00002800CommandMessage::CommandMessage(const Vector<uint16_t>& text,
2801 v8::Debug::ClientData* data)
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002802 : text_(text),
2803 client_data_(data) {
2804}
2805
2806
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00002807CommandMessage::~CommandMessage() {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002808}
2809
2810
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00002811void CommandMessage::Dispose() {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002812 text_.Dispose();
2813 delete client_data_;
2814 client_data_ = NULL;
2815}
2816
2817
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00002818CommandMessage CommandMessage::New(const Vector<uint16_t>& command,
2819 v8::Debug::ClientData* data) {
2820 return CommandMessage(command.Clone(), data);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002821}
2822
2823
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00002824CommandMessageQueue::CommandMessageQueue(int size) : start_(0), end_(0),
2825 size_(size) {
2826 messages_ = NewArray<CommandMessage>(size);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002827}
2828
2829
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00002830CommandMessageQueue::~CommandMessageQueue() {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002831 while (!IsEmpty()) {
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00002832 CommandMessage m = Get();
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002833 m.Dispose();
2834 }
kasper.lund7276f142008-07-30 08:49:36 +00002835 DeleteArray(messages_);
2836}
2837
2838
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00002839CommandMessage CommandMessageQueue::Get() {
kasper.lund7276f142008-07-30 08:49:36 +00002840 ASSERT(!IsEmpty());
2841 int result = start_;
2842 start_ = (start_ + 1) % size_;
2843 return messages_[result];
2844}
2845
2846
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00002847void CommandMessageQueue::Put(const CommandMessage& message) {
kasper.lund7276f142008-07-30 08:49:36 +00002848 if ((end_ + 1) % size_ == start_) {
2849 Expand();
2850 }
2851 messages_[end_] = message;
2852 end_ = (end_ + 1) % size_;
2853}
2854
2855
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00002856void CommandMessageQueue::Expand() {
2857 CommandMessageQueue new_queue(size_ * 2);
kasper.lund7276f142008-07-30 08:49:36 +00002858 while (!IsEmpty()) {
2859 new_queue.Put(Get());
2860 }
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00002861 CommandMessage* array_to_free = messages_;
kasper.lund7276f142008-07-30 08:49:36 +00002862 *this = new_queue;
2863 new_queue.messages_ = array_to_free;
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002864 // Make the new_queue empty so that it doesn't call Dispose on any messages.
2865 new_queue.start_ = new_queue.end_;
kasper.lund7276f142008-07-30 08:49:36 +00002866 // Automatic destructor called on new_queue, freeing array_to_free.
2867}
2868
2869
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00002870LockingCommandMessageQueue::LockingCommandMessageQueue(int size)
2871 : queue_(size) {
kasper.lund7276f142008-07-30 08:49:36 +00002872 lock_ = OS::CreateMutex();
2873}
2874
2875
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00002876LockingCommandMessageQueue::~LockingCommandMessageQueue() {
kasper.lund7276f142008-07-30 08:49:36 +00002877 delete lock_;
2878}
2879
2880
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00002881bool LockingCommandMessageQueue::IsEmpty() const {
kasper.lund7276f142008-07-30 08:49:36 +00002882 ScopedLock sl(lock_);
2883 return queue_.IsEmpty();
2884}
2885
2886
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00002887CommandMessage LockingCommandMessageQueue::Get() {
kasper.lund7276f142008-07-30 08:49:36 +00002888 ScopedLock sl(lock_);
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00002889 CommandMessage result = queue_.Get();
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002890 Logger::DebugEvent("Get", result.text());
kasper.lund7276f142008-07-30 08:49:36 +00002891 return result;
2892}
2893
2894
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00002895void LockingCommandMessageQueue::Put(const CommandMessage& message) {
kasper.lund7276f142008-07-30 08:49:36 +00002896 ScopedLock sl(lock_);
2897 queue_.Put(message);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002898 Logger::DebugEvent("Put", message.text());
kasper.lund7276f142008-07-30 08:49:36 +00002899}
2900
2901
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00002902void LockingCommandMessageQueue::Clear() {
kasper.lund7276f142008-07-30 08:49:36 +00002903 ScopedLock sl(lock_);
2904 queue_.Clear();
2905}
2906
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002907
2908MessageDispatchHelperThread::MessageDispatchHelperThread()
2909 : sem_(OS::CreateSemaphore(0)), mutex_(OS::CreateMutex()),
2910 already_signalled_(false) {
2911}
2912
2913
2914MessageDispatchHelperThread::~MessageDispatchHelperThread() {
2915 delete mutex_;
2916 delete sem_;
2917}
2918
2919
2920void MessageDispatchHelperThread::Schedule() {
2921 {
2922 ScopedLock lock(mutex_);
2923 if (already_signalled_) {
2924 return;
2925 }
2926 already_signalled_ = true;
2927 }
2928 sem_->Signal();
2929}
2930
2931
2932void MessageDispatchHelperThread::Run() {
2933 while (true) {
2934 sem_->Wait();
2935 {
2936 ScopedLock lock(mutex_);
2937 already_signalled_ = false;
2938 }
2939 {
2940 Locker locker;
2941 Debugger::CallMessageDispatchHandler();
2942 }
2943 }
2944}
2945
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002946#endif // ENABLE_DEBUGGER_SUPPORT
kasper.lund7276f142008-07-30 08:49:36 +00002947
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002948} } // namespace v8::internal