blob: f36bf2d106ea460365203b4f6241610b1ed86182 [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"
34#include "compiler.h"
35#include "debug.h"
36#include "execution.h"
37#include "global-handles.h"
38#include "natives.h"
39#include "stub-cache.h"
kasper.lund7276f142008-07-30 08:49:36 +000040#include "log.h"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000041
42namespace v8 { namespace internal {
43
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000044static void PrintLn(v8::Local<v8::Value> value) {
45 v8::Local<v8::String> s = value->ToString();
46 char* data = NewArray<char>(s->Length() + 1);
47 if (data == NULL) {
48 V8::FatalProcessOutOfMemory("PrintLn");
49 return;
50 }
51 s->WriteAscii(data);
52 PrintF("%s\n", data);
53 DeleteArray(data);
54}
55
56
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000057static Handle<Code> ComputeCallDebugBreak(int argc) {
58 CALL_HEAP_FUNCTION(StubCache::ComputeCallDebugBreak(argc), Code);
59}
60
61
62static Handle<Code> ComputeCallDebugPrepareStepIn(int argc) {
63 CALL_HEAP_FUNCTION(StubCache::ComputeCallDebugPrepareStepIn(argc), Code);
64}
65
66
67BreakLocationIterator::BreakLocationIterator(Handle<DebugInfo> debug_info,
68 BreakLocatorType type) {
69 debug_info_ = debug_info;
70 type_ = type;
71 reloc_iterator_ = NULL;
72 reloc_iterator_original_ = NULL;
73 Reset(); // Initialize the rest of the member variables.
74}
75
76
77BreakLocationIterator::~BreakLocationIterator() {
78 ASSERT(reloc_iterator_ != NULL);
79 ASSERT(reloc_iterator_original_ != NULL);
80 delete reloc_iterator_;
81 delete reloc_iterator_original_;
82}
83
84
85void BreakLocationIterator::Next() {
86 AssertNoAllocation nogc;
87 ASSERT(!RinfoDone());
88
89 // Iterate through reloc info for code and original code stopping at each
90 // breakable code target.
91 bool first = break_point_ == -1;
92 while (!RinfoDone()) {
93 if (!first) RinfoNext();
94 first = false;
95 if (RinfoDone()) return;
96
ager@chromium.org236ad962008-09-25 09:45:57 +000097 // Whenever a statement position or (plain) position is passed update the
98 // current value of these.
99 if (RelocInfo::IsPosition(rmode())) {
100 if (RelocInfo::IsStatementPosition(rmode())) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000101 statement_position_ =
102 rinfo()->data() - debug_info_->shared()->start_position();
103 }
ager@chromium.org236ad962008-09-25 09:45:57 +0000104 // Always update the position as we don't want that to be before the
105 // statement position.
106 position_ = rinfo()->data() - debug_info_->shared()->start_position();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000107 ASSERT(position_ >= 0);
108 ASSERT(statement_position_ >= 0);
109 }
110
111 // Check for breakable code target. Look in the original code as setting
112 // break points can cause the code targets in the running (debugged) code to
113 // be of a different kind than in the original code.
ager@chromium.org236ad962008-09-25 09:45:57 +0000114 if (RelocInfo::IsCodeTarget(rmode())) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000115 Address target = original_rinfo()->target_address();
ager@chromium.org8bb60582008-12-11 12:02:20 +0000116 Code* code = Code::GetCodeFromTargetAddress(target);
ager@chromium.org236ad962008-09-25 09:45:57 +0000117 if (code->is_inline_cache_stub() || RelocInfo::IsConstructCall(rmode())) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000118 break_point_++;
119 return;
120 }
121 if (code->kind() == Code::STUB) {
122 if (type_ == ALL_BREAK_LOCATIONS) {
123 if (Debug::IsBreakStub(code)) {
124 break_point_++;
125 return;
126 }
127 } else {
128 ASSERT(type_ == SOURCE_BREAK_LOCATIONS);
129 if (Debug::IsSourceBreakStub(code)) {
130 break_point_++;
131 return;
132 }
133 }
134 }
135 }
136
137 // Check for break at return.
ager@chromium.org236ad962008-09-25 09:45:57 +0000138 if (RelocInfo::IsJSReturn(rmode())) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000139 // Set the positions to the end of the function.
140 if (debug_info_->shared()->HasSourceCode()) {
141 position_ = debug_info_->shared()->end_position() -
142 debug_info_->shared()->start_position();
143 } else {
144 position_ = 0;
145 }
146 statement_position_ = position_;
147 break_point_++;
148 return;
149 }
150 }
151}
152
153
154void BreakLocationIterator::Next(int count) {
155 while (count > 0) {
156 Next();
157 count--;
158 }
159}
160
161
162// Find the break point closest to the supplied address.
163void BreakLocationIterator::FindBreakLocationFromAddress(Address pc) {
164 // Run through all break points to locate the one closest to the address.
165 int closest_break_point = 0;
166 int distance = kMaxInt;
167 while (!Done()) {
168 // Check if this break point is closer that what was previously found.
169 if (this->pc() < pc && pc - this->pc() < distance) {
170 closest_break_point = break_point();
171 distance = pc - this->pc();
172 // Check whether we can't get any closer.
173 if (distance == 0) break;
174 }
175 Next();
176 }
177
178 // Move to the break point found.
179 Reset();
180 Next(closest_break_point);
181}
182
183
184// Find the break point closest to the supplied source position.
185void BreakLocationIterator::FindBreakLocationFromPosition(int position) {
186 // Run through all break points to locate the one closest to the source
187 // position.
188 int closest_break_point = 0;
189 int distance = kMaxInt;
190 while (!Done()) {
191 // Check if this break point is closer that what was previously found.
192 if (position <= statement_position() &&
193 statement_position() - position < distance) {
194 closest_break_point = break_point();
195 distance = statement_position() - position;
196 // Check whether we can't get any closer.
197 if (distance == 0) break;
198 }
199 Next();
200 }
201
202 // Move to the break point found.
203 Reset();
204 Next(closest_break_point);
205}
206
207
208void BreakLocationIterator::Reset() {
209 // Create relocation iterators for the two code objects.
210 if (reloc_iterator_ != NULL) delete reloc_iterator_;
211 if (reloc_iterator_original_ != NULL) delete reloc_iterator_original_;
212 reloc_iterator_ = new RelocIterator(debug_info_->code());
213 reloc_iterator_original_ = new RelocIterator(debug_info_->original_code());
214
215 // Position at the first break point.
216 break_point_ = -1;
217 position_ = 1;
218 statement_position_ = 1;
219 Next();
220}
221
222
223bool BreakLocationIterator::Done() const {
224 return RinfoDone();
225}
226
227
228void BreakLocationIterator::SetBreakPoint(Handle<Object> break_point_object) {
229 // If there is not already a real break point here patch code with debug
230 // break.
231 if (!HasBreakPoint()) {
232 SetDebugBreak();
233 }
234 ASSERT(IsDebugBreak());
235 // Set the break point information.
236 DebugInfo::SetBreakPoint(debug_info_, code_position(),
237 position(), statement_position(),
238 break_point_object);
239}
240
241
242void BreakLocationIterator::ClearBreakPoint(Handle<Object> break_point_object) {
243 // Clear the break point information.
244 DebugInfo::ClearBreakPoint(debug_info_, code_position(), break_point_object);
245 // If there are no more break points here remove the debug break.
246 if (!HasBreakPoint()) {
247 ClearDebugBreak();
248 ASSERT(!IsDebugBreak());
249 }
250}
251
252
253void BreakLocationIterator::SetOneShot() {
254 // If there is a real break point here no more to do.
255 if (HasBreakPoint()) {
256 ASSERT(IsDebugBreak());
257 return;
258 }
259
260 // Patch code with debug break.
261 SetDebugBreak();
262}
263
264
265void BreakLocationIterator::ClearOneShot() {
266 // If there is a real break point here no more to do.
267 if (HasBreakPoint()) {
268 ASSERT(IsDebugBreak());
269 return;
270 }
271
272 // Patch code removing debug break.
273 ClearDebugBreak();
274 ASSERT(!IsDebugBreak());
275}
276
277
278void BreakLocationIterator::SetDebugBreak() {
279 // If there is already a break point here just return. This might happen if
v8.team.kasperl727e9952008-09-02 14:56:44 +0000280 // the same code is flooded with break points twice. Flooding the same
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000281 // function twice might happen when stepping in a function with an exception
282 // handler as the handler and the function is the same.
283 if (IsDebugBreak()) {
284 return;
285 }
286
ager@chromium.org236ad962008-09-25 09:45:57 +0000287 if (RelocInfo::IsJSReturn(rmode())) {
iposva@chromium.org245aa852009-02-10 00:49:54 +0000288 // Patch the frame exit code with a break point.
289 SetDebugBreakAtReturn();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000290 } else {
291 // Patch the original code with the current address as the current address
292 // might have changed by the inline caching since the code was copied.
293 original_rinfo()->set_target_address(rinfo()->target_address());
294
295 // Patch the code to invoke the builtin debug break function matching the
296 // calling convention used by the call site.
297 Handle<Code> dbgbrk_code(Debug::FindDebugBreak(rinfo()));
298 rinfo()->set_target_address(dbgbrk_code->entry());
299 }
300 ASSERT(IsDebugBreak());
301}
302
303
304void BreakLocationIterator::ClearDebugBreak() {
ager@chromium.org236ad962008-09-25 09:45:57 +0000305 if (RelocInfo::IsJSReturn(rmode())) {
iposva@chromium.org245aa852009-02-10 00:49:54 +0000306 // Restore the frame exit code.
307 ClearDebugBreakAtReturn();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000308 } else {
309 // Patch the code to the original invoke.
310 rinfo()->set_target_address(original_rinfo()->target_address());
311 }
312 ASSERT(!IsDebugBreak());
313}
314
315
316void BreakLocationIterator::PrepareStepIn() {
ager@chromium.org381abbb2009-02-25 13:23:22 +0000317 HandleScope scope;
318
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000319 // Step in can only be prepared if currently positioned on an IC call or
320 // construct call.
321 Address target = rinfo()->target_address();
ager@chromium.org8bb60582008-12-11 12:02:20 +0000322 Code* code = Code::GetCodeFromTargetAddress(target);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000323 if (code->is_call_stub()) {
324 // Step in through IC call is handled by the runtime system. Therefore make
325 // sure that the any current IC is cleared and the runtime system is
326 // called. If the executing code has a debug break at the location change
327 // the call in the original code as it is the code there that will be
328 // executed in place of the debug break call.
329 Handle<Code> stub = ComputeCallDebugPrepareStepIn(code->arguments_count());
330 if (IsDebugBreak()) {
331 original_rinfo()->set_target_address(stub->entry());
332 } else {
333 rinfo()->set_target_address(stub->entry());
334 }
335 } else {
v8.team.kasperl727e9952008-09-02 14:56:44 +0000336 // Step in through constructs call requires no changes to the running code.
ager@chromium.org236ad962008-09-25 09:45:57 +0000337 ASSERT(RelocInfo::IsConstructCall(rmode()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000338 }
339}
340
341
342// Check whether the break point is at a position which will exit the function.
343bool BreakLocationIterator::IsExit() const {
ager@chromium.org236ad962008-09-25 09:45:57 +0000344 return (RelocInfo::IsJSReturn(rmode()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000345}
346
347
348bool BreakLocationIterator::HasBreakPoint() {
349 return debug_info_->HasBreakPoint(code_position());
350}
351
352
353// Check whether there is a debug break at the current position.
354bool BreakLocationIterator::IsDebugBreak() {
ager@chromium.org236ad962008-09-25 09:45:57 +0000355 if (RelocInfo::IsJSReturn(rmode())) {
iposva@chromium.org245aa852009-02-10 00:49:54 +0000356 return IsDebugBreakAtReturn();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000357 } else {
358 return Debug::IsDebugBreak(rinfo()->target_address());
359 }
360}
361
362
363Object* BreakLocationIterator::BreakPointObjects() {
364 return debug_info_->GetBreakPointObjects(code_position());
365}
366
367
ager@chromium.org381abbb2009-02-25 13:23:22 +0000368// Clear out all the debug break code. This is ONLY supposed to be used when
369// shutting down the debugger as it will leave the break point information in
370// DebugInfo even though the code is patched back to the non break point state.
371void BreakLocationIterator::ClearAllDebugBreak() {
372 while (!Done()) {
373 ClearDebugBreak();
374 Next();
375 }
376}
377
378
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000379bool BreakLocationIterator::RinfoDone() const {
380 ASSERT(reloc_iterator_->done() == reloc_iterator_original_->done());
381 return reloc_iterator_->done();
382}
383
384
385void BreakLocationIterator::RinfoNext() {
386 reloc_iterator_->next();
387 reloc_iterator_original_->next();
388#ifdef DEBUG
389 ASSERT(reloc_iterator_->done() == reloc_iterator_original_->done());
390 if (!reloc_iterator_->done()) {
391 ASSERT(rmode() == original_rmode());
392 }
393#endif
394}
395
396
397bool Debug::has_break_points_ = false;
398DebugInfoListNode* Debug::debug_info_list_ = NULL;
399
400
401// Threading support.
402void Debug::ThreadInit() {
403 thread_local_.last_step_action_ = StepNone;
ager@chromium.org236ad962008-09-25 09:45:57 +0000404 thread_local_.last_statement_position_ = RelocInfo::kNoPosition;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000405 thread_local_.step_count_ = 0;
406 thread_local_.last_fp_ = 0;
407 thread_local_.step_into_fp_ = 0;
408 thread_local_.after_break_target_ = 0;
409}
410
411
412JSCallerSavedBuffer Debug::registers_;
413Debug::ThreadLocal Debug::thread_local_;
414
415
416char* Debug::ArchiveDebug(char* storage) {
417 char* to = storage;
418 memcpy(to, reinterpret_cast<char*>(&thread_local_), sizeof(ThreadLocal));
419 to += sizeof(ThreadLocal);
420 memcpy(to, reinterpret_cast<char*>(&registers_), sizeof(registers_));
421 ThreadInit();
422 ASSERT(to <= storage + ArchiveSpacePerThread());
423 return storage + ArchiveSpacePerThread();
424}
425
426
427char* Debug::RestoreDebug(char* storage) {
428 char* from = storage;
429 memcpy(reinterpret_cast<char*>(&thread_local_), from, sizeof(ThreadLocal));
430 from += sizeof(ThreadLocal);
431 memcpy(reinterpret_cast<char*>(&registers_), from, sizeof(registers_));
432 ASSERT(from <= storage + ArchiveSpacePerThread());
433 return storage + ArchiveSpacePerThread();
434}
435
436
437int Debug::ArchiveSpacePerThread() {
438 return sizeof(ThreadLocal) + sizeof(registers_);
439}
440
441
kasper.lundbd3ec4e2008-07-09 11:06:54 +0000442// Default break enabled.
443bool Debug::disable_break_ = false;
444
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000445// Default call debugger on uncaught exception.
446bool Debug::break_on_exception_ = false;
447bool Debug::break_on_uncaught_exception_ = true;
448
449Handle<Context> Debug::debug_context_ = Handle<Context>();
450Code* Debug::debug_break_return_entry_ = NULL;
451Code* Debug::debug_break_return_ = NULL;
452
453
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000454void Debug::HandleWeakDebugInfo(v8::Persistent<v8::Value> obj, void* data) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000455 DebugInfoListNode* node = reinterpret_cast<DebugInfoListNode*>(data);
456 RemoveDebugInfo(node->debug_info());
457#ifdef DEBUG
458 node = Debug::debug_info_list_;
459 while (node != NULL) {
460 ASSERT(node != reinterpret_cast<DebugInfoListNode*>(data));
461 node = node->next();
462 }
463#endif
464}
465
466
467DebugInfoListNode::DebugInfoListNode(DebugInfo* debug_info): next_(NULL) {
468 // Globalize the request debug info object and make it weak.
469 debug_info_ = Handle<DebugInfo>::cast((GlobalHandles::Create(debug_info)));
470 GlobalHandles::MakeWeak(reinterpret_cast<Object**>(debug_info_.location()),
471 this, Debug::HandleWeakDebugInfo);
472}
473
474
475DebugInfoListNode::~DebugInfoListNode() {
476 GlobalHandles::Destroy(reinterpret_cast<Object**>(debug_info_.location()));
477}
478
479
480void Debug::Setup(bool create_heap_objects) {
481 ThreadInit();
482 if (create_heap_objects) {
483 // Get code to handle entry to debug break on return.
484 debug_break_return_entry_ =
485 Builtins::builtin(Builtins::Return_DebugBreakEntry);
486 ASSERT(debug_break_return_entry_->IsCode());
487
488 // Get code to handle debug break on return.
489 debug_break_return_ =
490 Builtins::builtin(Builtins::Return_DebugBreak);
491 ASSERT(debug_break_return_->IsCode());
492 }
493}
494
495
496bool Debug::CompileDebuggerScript(int index) {
497 HandleScope scope;
498
kasper.lund44510672008-07-25 07:37:58 +0000499 // Bail out if the index is invalid.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000500 if (index == -1) {
501 return false;
502 }
kasper.lund44510672008-07-25 07:37:58 +0000503
504 // Find source and name for the requested script.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000505 Handle<String> source_code = Bootstrapper::NativesSourceLookup(index);
506 Vector<const char> name = Natives::GetScriptName(index);
507 Handle<String> script_name = Factory::NewStringFromAscii(name);
508
509 // Compile the script.
510 bool allow_natives_syntax = FLAG_allow_natives_syntax;
511 FLAG_allow_natives_syntax = true;
512 Handle<JSFunction> boilerplate;
513 boilerplate = Compiler::Compile(source_code, script_name, 0, 0, NULL, NULL);
514 FLAG_allow_natives_syntax = allow_natives_syntax;
515
516 // Silently ignore stack overflows during compilation.
517 if (boilerplate.is_null()) {
518 ASSERT(Top::has_pending_exception());
519 Top::clear_pending_exception();
520 return false;
521 }
522
kasper.lund44510672008-07-25 07:37:58 +0000523 // Execute the boilerplate function in the debugger context.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000524 Handle<Context> context = Top::global_context();
kasper.lund44510672008-07-25 07:37:58 +0000525 bool caught_exception = false;
526 Handle<JSFunction> function =
527 Factory::NewFunctionFromBoilerplate(boilerplate, context);
528 Handle<Object> result =
529 Execution::TryCall(function, Handle<Object>(context->global()),
530 0, NULL, &caught_exception);
531
532 // Check for caught exceptions.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000533 if (caught_exception) {
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000534 Handle<Object> message = MessageHandler::MakeMessageObject(
535 "error_loading_debugger", NULL, HandleVector<Object>(&result, 1),
536 Handle<String>());
537 MessageHandler::ReportMessage(NULL, message);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000538 return false;
539 }
540
kasper.lund44510672008-07-25 07:37:58 +0000541 // Mark this script as native and return successfully.
542 Handle<Script> script(Script::cast(function->shared()->script()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000543 script->set_type(Smi::FromInt(SCRIPT_TYPE_NATIVE));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000544 return true;
545}
546
547
548bool Debug::Load() {
549 // Return if debugger is already loaded.
kasper.lund212ac232008-07-16 07:07:30 +0000550 if (IsLoaded()) return true;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000551
kasper.lund44510672008-07-25 07:37:58 +0000552 // Bail out if we're already in the process of compiling the native
553 // JavaScript source code for the debugger.
mads.s.agercbaa0602008-08-14 13:41:48 +0000554 if (Debugger::compiling_natives() || Debugger::is_loading_debugger())
555 return false;
556 Debugger::set_loading_debugger(true);
kasper.lund44510672008-07-25 07:37:58 +0000557
558 // Disable breakpoints and interrupts while compiling and running the
559 // debugger scripts including the context creation code.
560 DisableBreak disable(true);
561 PostponeInterruptsScope postpone;
562
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000563 // Create the debugger context.
564 HandleScope scope;
kasper.lund44510672008-07-25 07:37:58 +0000565 Handle<Context> context =
566 Bootstrapper::CreateEnvironment(Handle<Object>::null(),
567 v8::Handle<ObjectTemplate>(),
568 NULL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000569
kasper.lund44510672008-07-25 07:37:58 +0000570 // Use the debugger context.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000571 SaveContext save;
kasper.lund44510672008-07-25 07:37:58 +0000572 Top::set_context(*context);
kasper.lund44510672008-07-25 07:37:58 +0000573
574 // Expose the builtins object in the debugger context.
575 Handle<String> key = Factory::LookupAsciiSymbol("builtins");
576 Handle<GlobalObject> global = Handle<GlobalObject>(context->global());
577 SetProperty(global, key, Handle<Object>(global->builtins()), NONE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000578
579 // Compile the JavaScript for the debugger in the debugger context.
580 Debugger::set_compiling_natives(true);
kasper.lund44510672008-07-25 07:37:58 +0000581 bool caught_exception =
582 !CompileDebuggerScript(Natives::GetIndex("mirror")) ||
583 !CompileDebuggerScript(Natives::GetIndex("debug"));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000584 Debugger::set_compiling_natives(false);
585
mads.s.agercbaa0602008-08-14 13:41:48 +0000586 // Make sure we mark the debugger as not loading before we might
587 // return.
588 Debugger::set_loading_debugger(false);
589
kasper.lund44510672008-07-25 07:37:58 +0000590 // Check for caught exceptions.
591 if (caught_exception) return false;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000592
593 // Debugger loaded.
kasper.lund44510672008-07-25 07:37:58 +0000594 debug_context_ = Handle<Context>::cast(GlobalHandles::Create(*context));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000595 return true;
596}
597
598
599void Debug::Unload() {
600 // Return debugger is not loaded.
601 if (!IsLoaded()) {
602 return;
603 }
604
ager@chromium.org381abbb2009-02-25 13:23:22 +0000605 // Get rid of all break points and related information.
606 ClearAllBreakPoints();
607
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000608 // Clear debugger context global handle.
609 GlobalHandles::Destroy(reinterpret_cast<Object**>(debug_context_.location()));
610 debug_context_ = Handle<Context>();
611}
612
613
614void Debug::Iterate(ObjectVisitor* v) {
iposva@chromium.org245aa852009-02-10 00:49:54 +0000615 v->VisitPointer(bit_cast<Object**, Code**>(&(debug_break_return_entry_)));
616 v->VisitPointer(bit_cast<Object**, Code**>(&(debug_break_return_)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000617}
618
619
620Object* Debug::Break(Arguments args) {
621 HandleScope scope;
mads.s.ager31e71382008-08-13 09:32:07 +0000622 ASSERT(args.length() == 0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000623
kasper.lundbd3ec4e2008-07-09 11:06:54 +0000624 // Get the top-most JavaScript frame.
625 JavaScriptFrameIterator it;
626 JavaScriptFrame* frame = it.frame();
627
628 // Just continue if breaks are disabled or debugger cannot be loaded.
629 if (disable_break() || !Load()) {
630 SetAfterBreakTarget(frame);
mads.s.ager31e71382008-08-13 09:32:07 +0000631 return Heap::undefined_value();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000632 }
633
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000634 // Enter the debugger.
635 EnterDebugger debugger;
636 if (debugger.FailedToEnter()) {
637 return Heap::undefined_value();
638 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000639
kasper.lund44510672008-07-25 07:37:58 +0000640 // Postpone interrupt during breakpoint processing.
641 PostponeInterruptsScope postpone;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000642
643 // Get the debug info (create it if it does not exist).
644 Handle<SharedFunctionInfo> shared =
645 Handle<SharedFunctionInfo>(JSFunction::cast(frame->function())->shared());
646 Handle<DebugInfo> debug_info = GetDebugInfo(shared);
647
648 // Find the break point where execution has stopped.
649 BreakLocationIterator break_location_iterator(debug_info,
650 ALL_BREAK_LOCATIONS);
651 break_location_iterator.FindBreakLocationFromAddress(frame->pc());
652
653 // Check whether step next reached a new statement.
654 if (!StepNextContinue(&break_location_iterator, frame)) {
655 // Decrease steps left if performing multiple steps.
656 if (thread_local_.step_count_ > 0) {
657 thread_local_.step_count_--;
658 }
659 }
660
661 // If there is one or more real break points check whether any of these are
662 // triggered.
663 Handle<Object> break_points_hit(Heap::undefined_value());
664 if (break_location_iterator.HasBreakPoint()) {
665 Handle<Object> break_point_objects =
666 Handle<Object>(break_location_iterator.BreakPointObjects());
667 break_points_hit = CheckBreakPoints(break_point_objects);
668 }
669
670 // Notify debugger if a real break point is triggered or if performing single
671 // stepping with no more steps to perform. Otherwise do another step.
672 if (!break_points_hit->IsUndefined() ||
673 (thread_local_.last_step_action_ != StepNone &&
674 thread_local_.step_count_ == 0)) {
675 // Clear all current stepping setup.
676 ClearStepping();
677
678 // Notify the debug event listeners.
679 Debugger::OnDebugBreak(break_points_hit);
680 } else if (thread_local_.last_step_action_ != StepNone) {
681 // Hold on to last step action as it is cleared by the call to
682 // ClearStepping.
683 StepAction step_action = thread_local_.last_step_action_;
684 int step_count = thread_local_.step_count_;
685
686 // Clear all current stepping setup.
687 ClearStepping();
688
689 // Set up for the remaining steps.
690 PrepareStep(step_action, step_count);
691 }
692
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000693 // Install jump to the call address which was overwritten.
694 SetAfterBreakTarget(frame);
695
mads.s.ager31e71382008-08-13 09:32:07 +0000696 return Heap::undefined_value();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000697}
698
699
700// Check the break point objects for whether one or more are actually
701// triggered. This function returns a JSArray with the break point objects
702// which is triggered.
703Handle<Object> Debug::CheckBreakPoints(Handle<Object> break_point_objects) {
704 int break_points_hit_count = 0;
705 Handle<JSArray> break_points_hit = Factory::NewJSArray(1);
706
v8.team.kasperl727e9952008-09-02 14:56:44 +0000707 // If there are multiple break points they are in a FixedArray.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000708 ASSERT(!break_point_objects->IsUndefined());
709 if (break_point_objects->IsFixedArray()) {
710 Handle<FixedArray> array(FixedArray::cast(*break_point_objects));
711 for (int i = 0; i < array->length(); i++) {
712 Handle<Object> o(array->get(i));
713 if (CheckBreakPoint(o)) {
714 break_points_hit->SetElement(break_points_hit_count++, *o);
715 }
716 }
717 } else {
718 if (CheckBreakPoint(break_point_objects)) {
719 break_points_hit->SetElement(break_points_hit_count++,
720 *break_point_objects);
721 }
722 }
723
724 // Return undefined if no break points where triggered.
725 if (break_points_hit_count == 0) {
726 return Factory::undefined_value();
727 }
728 return break_points_hit;
729}
730
731
732// Check whether a single break point object is triggered.
733bool Debug::CheckBreakPoint(Handle<Object> break_point_object) {
ager@chromium.org381abbb2009-02-25 13:23:22 +0000734 HandleScope scope;
735
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000736 // Ignore check if break point object is not a JSObject.
737 if (!break_point_object->IsJSObject()) return true;
738
739 // Get the function CheckBreakPoint (defined in debug.js).
740 Handle<JSFunction> check_break_point =
741 Handle<JSFunction>(JSFunction::cast(
742 debug_context()->global()->GetProperty(
743 *Factory::LookupAsciiSymbol("IsBreakPointTriggered"))));
744
745 // Get the break id as an object.
746 Handle<Object> break_id = Factory::NewNumberFromInt(Top::break_id());
747
748 // Call HandleBreakPointx.
749 bool caught_exception = false;
750 const int argc = 2;
751 Object** argv[argc] = {
752 break_id.location(),
753 reinterpret_cast<Object**>(break_point_object.location())
754 };
755 Handle<Object> result = Execution::TryCall(check_break_point,
756 Top::builtins(), argc, argv,
757 &caught_exception);
758
759 // If exception or non boolean result handle as not triggered
760 if (caught_exception || !result->IsBoolean()) {
761 return false;
762 }
763
764 // Return whether the break point is triggered.
765 return *result == Heap::true_value();
766}
767
768
769// Check whether the function has debug information.
770bool Debug::HasDebugInfo(Handle<SharedFunctionInfo> shared) {
771 return !shared->debug_info()->IsUndefined();
772}
773
774
kasper.lundbd3ec4e2008-07-09 11:06:54 +0000775// Return the debug info for this function. EnsureDebugInfo must be called
776// prior to ensure the debug info has been generated for shared.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000777Handle<DebugInfo> Debug::GetDebugInfo(Handle<SharedFunctionInfo> shared) {
kasper.lundbd3ec4e2008-07-09 11:06:54 +0000778 ASSERT(HasDebugInfo(shared));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000779 return Handle<DebugInfo>(DebugInfo::cast(shared->debug_info()));
780}
781
782
783void Debug::SetBreakPoint(Handle<SharedFunctionInfo> shared,
784 int source_position,
785 Handle<Object> break_point_object) {
ager@chromium.org381abbb2009-02-25 13:23:22 +0000786 HandleScope scope;
787
kasper.lundbd3ec4e2008-07-09 11:06:54 +0000788 if (!EnsureDebugInfo(shared)) {
789 // Return if retrieving debug info failed.
790 return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000791 }
792
kasper.lundbd3ec4e2008-07-09 11:06:54 +0000793 Handle<DebugInfo> debug_info = GetDebugInfo(shared);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000794 // Source positions starts with zero.
795 ASSERT(source_position >= 0);
796
797 // Find the break point and change it.
798 BreakLocationIterator it(debug_info, SOURCE_BREAK_LOCATIONS);
799 it.FindBreakLocationFromPosition(source_position);
800 it.SetBreakPoint(break_point_object);
801
802 // At least one active break point now.
803 ASSERT(debug_info->GetBreakPointCount() > 0);
804}
805
806
807void Debug::ClearBreakPoint(Handle<Object> break_point_object) {
ager@chromium.org381abbb2009-02-25 13:23:22 +0000808 HandleScope scope;
809
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000810 DebugInfoListNode* node = debug_info_list_;
811 while (node != NULL) {
812 Object* result = DebugInfo::FindBreakPointInfo(node->debug_info(),
813 break_point_object);
814 if (!result->IsUndefined()) {
815 // Get information in the break point.
816 BreakPointInfo* break_point_info = BreakPointInfo::cast(result);
817 Handle<DebugInfo> debug_info = node->debug_info();
818 Handle<SharedFunctionInfo> shared(debug_info->shared());
819 int source_position = break_point_info->statement_position()->value();
820
821 // Source positions starts with zero.
822 ASSERT(source_position >= 0);
823
824 // Find the break point and clear it.
825 BreakLocationIterator it(debug_info, SOURCE_BREAK_LOCATIONS);
826 it.FindBreakLocationFromPosition(source_position);
827 it.ClearBreakPoint(break_point_object);
828
829 // If there are no more break points left remove the debug info for this
830 // function.
831 if (debug_info->GetBreakPointCount() == 0) {
832 RemoveDebugInfo(debug_info);
833 }
834
835 return;
836 }
837 node = node->next();
838 }
839}
840
841
ager@chromium.org381abbb2009-02-25 13:23:22 +0000842void Debug::ClearAllBreakPoints() {
843 DebugInfoListNode* node = debug_info_list_;
844 while (node != NULL) {
845 // Remove all debug break code.
846 BreakLocationIterator it(node->debug_info(), ALL_BREAK_LOCATIONS);
847 it.ClearAllDebugBreak();
848 node = node->next();
849 }
850
851 // Remove all debug info.
852 while (debug_info_list_ != NULL) {
853 RemoveDebugInfo(debug_info_list_->debug_info());
854 }
855}
856
857
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000858void Debug::FloodWithOneShot(Handle<SharedFunctionInfo> shared) {
kasper.lundbd3ec4e2008-07-09 11:06:54 +0000859 // Make sure the function has setup the debug info.
860 if (!EnsureDebugInfo(shared)) {
861 // Return if we failed to retrieve the debug info.
862 return;
863 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000864
865 // Flood the function with break points.
kasper.lundbd3ec4e2008-07-09 11:06:54 +0000866 BreakLocationIterator it(GetDebugInfo(shared), ALL_BREAK_LOCATIONS);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000867 while (!it.Done()) {
868 it.SetOneShot();
869 it.Next();
870 }
871}
872
873
874void Debug::FloodHandlerWithOneShot() {
ager@chromium.org8bb60582008-12-11 12:02:20 +0000875 // Iterate through the JavaScript stack looking for handlers.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000876 StackFrame::Id id = Top::break_frame_id();
ager@chromium.org8bb60582008-12-11 12:02:20 +0000877 if (id == StackFrame::NO_ID) {
878 // If there is no JavaScript stack don't do anything.
879 return;
880 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000881 for (JavaScriptFrameIterator it(id); !it.done(); it.Advance()) {
882 JavaScriptFrame* frame = it.frame();
883 if (frame->HasHandler()) {
884 Handle<SharedFunctionInfo> shared =
885 Handle<SharedFunctionInfo>(
886 JSFunction::cast(frame->function())->shared());
887 // Flood the function with the catch block with break points
888 FloodWithOneShot(shared);
889 return;
890 }
891 }
892}
893
894
895void Debug::ChangeBreakOnException(ExceptionBreakType type, bool enable) {
896 if (type == BreakUncaughtException) {
897 break_on_uncaught_exception_ = enable;
898 } else {
899 break_on_exception_ = enable;
900 }
901}
902
903
904void Debug::PrepareStep(StepAction step_action, int step_count) {
905 HandleScope scope;
906 ASSERT(Debug::InDebugger());
907
908 // Remember this step action and count.
909 thread_local_.last_step_action_ = step_action;
910 thread_local_.step_count_ = step_count;
911
912 // Get the frame where the execution has stopped and skip the debug frame if
913 // any. The debug frame will only be present if execution was stopped due to
914 // hitting a break point. In other situations (e.g. unhandled exception) the
915 // debug frame is not present.
916 StackFrame::Id id = Top::break_frame_id();
ager@chromium.org8bb60582008-12-11 12:02:20 +0000917 if (id == StackFrame::NO_ID) {
918 // If there is no JavaScript stack don't do anything.
919 return;
920 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000921 JavaScriptFrameIterator frames_it(id);
922 JavaScriptFrame* frame = frames_it.frame();
923
924 // First of all ensure there is one-shot break points in the top handler
925 // if any.
926 FloodHandlerWithOneShot();
927
928 // If the function on the top frame is unresolved perform step out. This will
929 // be the case when calling unknown functions and having the debugger stopped
930 // in an unhandled exception.
931 if (!frame->function()->IsJSFunction()) {
932 // Step out: Find the calling JavaScript frame and flood it with
933 // breakpoints.
934 frames_it.Advance();
935 // Fill the function to return to with one-shot break points.
936 JSFunction* function = JSFunction::cast(frames_it.frame()->function());
937 FloodWithOneShot(Handle<SharedFunctionInfo>(function->shared()));
938 return;
939 }
940
941 // Get the debug info (create it if it does not exist).
942 Handle<SharedFunctionInfo> shared =
943 Handle<SharedFunctionInfo>(JSFunction::cast(frame->function())->shared());
kasper.lundbd3ec4e2008-07-09 11:06:54 +0000944 if (!EnsureDebugInfo(shared)) {
945 // Return if ensuring debug info failed.
946 return;
947 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000948 Handle<DebugInfo> debug_info = GetDebugInfo(shared);
949
950 // Find the break location where execution has stopped.
951 BreakLocationIterator it(debug_info, ALL_BREAK_LOCATIONS);
952 it.FindBreakLocationFromAddress(frame->pc());
953
954 // Compute whether or not the target is a call target.
955 bool is_call_target = false;
ager@chromium.org236ad962008-09-25 09:45:57 +0000956 if (RelocInfo::IsCodeTarget(it.rinfo()->rmode())) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000957 Address target = it.rinfo()->target_address();
ager@chromium.org8bb60582008-12-11 12:02:20 +0000958 Code* code = Code::GetCodeFromTargetAddress(target);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000959 if (code->is_call_stub()) is_call_target = true;
960 }
961
v8.team.kasperl727e9952008-09-02 14:56:44 +0000962 // If this is the last break code target step out is the only possibility.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000963 if (it.IsExit() || step_action == StepOut) {
964 // Step out: If there is a JavaScript caller frame, we need to
965 // flood it with breakpoints.
966 frames_it.Advance();
967 if (!frames_it.done()) {
968 // Fill the function to return to with one-shot break points.
969 JSFunction* function = JSFunction::cast(frames_it.frame()->function());
970 FloodWithOneShot(Handle<SharedFunctionInfo>(function->shared()));
971 }
ager@chromium.org236ad962008-09-25 09:45:57 +0000972 } else if (!(is_call_target || RelocInfo::IsConstructCall(it.rmode())) ||
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000973 step_action == StepNext || step_action == StepMin) {
974 // Step next or step min.
975
976 // Fill the current function with one-shot break points.
977 FloodWithOneShot(shared);
978
979 // Remember source position and frame to handle step next.
980 thread_local_.last_statement_position_ =
981 debug_info->code()->SourceStatementPosition(frame->pc());
982 thread_local_.last_fp_ = frame->fp();
983 } else {
984 // Fill the current function with one-shot break points even for step in on
985 // a call target as the function called might be a native function for
986 // which step in will not stop.
987 FloodWithOneShot(shared);
988
989 // Step in or Step in min
990 it.PrepareStepIn();
991 ActivateStepIn(frame);
992 }
993}
994
995
996// Check whether the current debug break should be reported to the debugger. It
997// is used to have step next and step in only report break back to the debugger
998// if on a different frame or in a different statement. In some situations
999// there will be several break points in the same statement when the code is
v8.team.kasperl727e9952008-09-02 14:56:44 +00001000// flooded with one-shot break points. This function helps to perform several
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001001// steps before reporting break back to the debugger.
1002bool Debug::StepNextContinue(BreakLocationIterator* break_location_iterator,
1003 JavaScriptFrame* frame) {
1004 // If the step last action was step next or step in make sure that a new
1005 // statement is hit.
1006 if (thread_local_.last_step_action_ == StepNext ||
1007 thread_local_.last_step_action_ == StepIn) {
1008 // Never continue if returning from function.
1009 if (break_location_iterator->IsExit()) return false;
1010
1011 // Continue if we are still on the same frame and in the same statement.
1012 int current_statement_position =
1013 break_location_iterator->code()->SourceStatementPosition(frame->pc());
1014 return thread_local_.last_fp_ == frame->fp() &&
ager@chromium.org236ad962008-09-25 09:45:57 +00001015 thread_local_.last_statement_position_ == current_statement_position;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001016 }
1017
1018 // No step next action - don't continue.
1019 return false;
1020}
1021
1022
1023// Check whether the code object at the specified address is a debug break code
1024// object.
1025bool Debug::IsDebugBreak(Address addr) {
ager@chromium.org8bb60582008-12-11 12:02:20 +00001026 Code* code = Code::GetCodeFromTargetAddress(addr);
kasper.lund7276f142008-07-30 08:49:36 +00001027 return code->ic_state() == DEBUG_BREAK;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001028}
1029
1030
1031// Check whether a code stub with the specified major key is a possible break
1032// point location when looking for source break locations.
1033bool Debug::IsSourceBreakStub(Code* code) {
1034 CodeStub::Major major_key = code->major_key();
1035 return major_key == CodeStub::CallFunction;
1036}
1037
1038
1039// Check whether a code stub with the specified major key is a possible break
1040// location.
1041bool Debug::IsBreakStub(Code* code) {
1042 CodeStub::Major major_key = code->major_key();
1043 return major_key == CodeStub::CallFunction ||
1044 major_key == CodeStub::StackCheck;
1045}
1046
1047
1048// Find the builtin to use for invoking the debug break
1049Handle<Code> Debug::FindDebugBreak(RelocInfo* rinfo) {
1050 // Find the builtin debug break function matching the calling convention
1051 // used by the call site.
ager@chromium.org236ad962008-09-25 09:45:57 +00001052 RelocInfo::Mode mode = rinfo->rmode();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001053
ager@chromium.org236ad962008-09-25 09:45:57 +00001054 if (RelocInfo::IsCodeTarget(mode)) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001055 Address target = rinfo->target_address();
ager@chromium.org8bb60582008-12-11 12:02:20 +00001056 Code* code = Code::GetCodeFromTargetAddress(target);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001057 if (code->is_inline_cache_stub()) {
1058 if (code->is_call_stub()) {
1059 return ComputeCallDebugBreak(code->arguments_count());
1060 }
1061 if (code->is_load_stub()) {
1062 return Handle<Code>(Builtins::builtin(Builtins::LoadIC_DebugBreak));
1063 }
1064 if (code->is_store_stub()) {
1065 return Handle<Code>(Builtins::builtin(Builtins::StoreIC_DebugBreak));
1066 }
1067 if (code->is_keyed_load_stub()) {
1068 Handle<Code> result =
1069 Handle<Code>(Builtins::builtin(Builtins::KeyedLoadIC_DebugBreak));
1070 return result;
1071 }
1072 if (code->is_keyed_store_stub()) {
1073 Handle<Code> result =
1074 Handle<Code>(Builtins::builtin(Builtins::KeyedStoreIC_DebugBreak));
1075 return result;
1076 }
1077 }
ager@chromium.org236ad962008-09-25 09:45:57 +00001078 if (RelocInfo::IsConstructCall(mode)) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001079 Handle<Code> result =
1080 Handle<Code>(Builtins::builtin(Builtins::ConstructCall_DebugBreak));
1081 return result;
1082 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001083 if (code->kind() == Code::STUB) {
1084 ASSERT(code->major_key() == CodeStub::CallFunction ||
1085 code->major_key() == CodeStub::StackCheck);
1086 Handle<Code> result =
1087 Handle<Code>(Builtins::builtin(Builtins::StubNoRegisters_DebugBreak));
1088 return result;
1089 }
1090 }
1091
1092 UNREACHABLE();
1093 return Handle<Code>::null();
1094}
1095
1096
1097// Simple function for returning the source positions for active break points.
1098Handle<Object> Debug::GetSourceBreakLocations(
1099 Handle<SharedFunctionInfo> shared) {
1100 if (!HasDebugInfo(shared)) return Handle<Object>(Heap::undefined_value());
1101 Handle<DebugInfo> debug_info = GetDebugInfo(shared);
1102 if (debug_info->GetBreakPointCount() == 0) {
1103 return Handle<Object>(Heap::undefined_value());
1104 }
1105 Handle<FixedArray> locations =
1106 Factory::NewFixedArray(debug_info->GetBreakPointCount());
1107 int count = 0;
1108 for (int i = 0; i < debug_info->break_points()->length(); i++) {
1109 if (!debug_info->break_points()->get(i)->IsUndefined()) {
1110 BreakPointInfo* break_point_info =
1111 BreakPointInfo::cast(debug_info->break_points()->get(i));
1112 if (break_point_info->GetBreakPointCount() > 0) {
1113 locations->set(count++, break_point_info->statement_position());
1114 }
1115 }
1116 }
1117 return locations;
1118}
1119
1120
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001121// Handle stepping into a function.
1122void Debug::HandleStepIn(Handle<JSFunction> function,
1123 Address fp,
1124 bool is_constructor) {
1125 // If the frame pointer is not supplied by the caller find it.
1126 if (fp == 0) {
1127 StackFrameIterator it;
1128 it.Advance();
1129 // For constructor functions skip another frame.
1130 if (is_constructor) {
1131 ASSERT(it.frame()->is_construct());
1132 it.Advance();
1133 }
1134 fp = it.frame()->fp();
1135 }
1136
1137 // Flood the function with one-shot break points if it is called from where
1138 // step into was requested.
1139 if (fp == Debug::step_in_fp()) {
1140 // Don't allow step into functions in the native context.
1141 if (function->context()->global() != Top::context()->builtins()) {
1142 Debug::FloodWithOneShot(Handle<SharedFunctionInfo>(function->shared()));
1143 }
1144 }
1145}
1146
1147
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001148void Debug::ClearStepping() {
1149 // Clear the various stepping setup.
1150 ClearOneShot();
1151 ClearStepIn();
1152 ClearStepNext();
1153
1154 // Clear multiple step counter.
1155 thread_local_.step_count_ = 0;
1156}
1157
1158// Clears all the one-shot break points that are currently set. Normally this
1159// function is called each time a break point is hit as one shot break points
1160// are used to support stepping.
1161void Debug::ClearOneShot() {
1162 // The current implementation just runs through all the breakpoints. When the
v8.team.kasperl727e9952008-09-02 14:56:44 +00001163 // last break point for a function is removed that function is automatically
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001164 // removed from the list.
1165
1166 DebugInfoListNode* node = debug_info_list_;
1167 while (node != NULL) {
1168 BreakLocationIterator it(node->debug_info(), ALL_BREAK_LOCATIONS);
1169 while (!it.Done()) {
1170 it.ClearOneShot();
1171 it.Next();
1172 }
1173 node = node->next();
1174 }
1175}
1176
1177
1178void Debug::ActivateStepIn(StackFrame* frame) {
1179 thread_local_.step_into_fp_ = frame->fp();
1180}
1181
1182
1183void Debug::ClearStepIn() {
1184 thread_local_.step_into_fp_ = 0;
1185}
1186
1187
1188void Debug::ClearStepNext() {
1189 thread_local_.last_step_action_ = StepNone;
ager@chromium.org236ad962008-09-25 09:45:57 +00001190 thread_local_.last_statement_position_ = RelocInfo::kNoPosition;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001191 thread_local_.last_fp_ = 0;
1192}
1193
1194
kasper.lundbd3ec4e2008-07-09 11:06:54 +00001195bool Debug::EnsureCompiled(Handle<SharedFunctionInfo> shared) {
1196 if (shared->is_compiled()) return true;
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001197 return CompileLazyShared(shared, CLEAR_EXCEPTION, 0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001198}
1199
1200
kasper.lundbd3ec4e2008-07-09 11:06:54 +00001201// Ensures the debug information is present for shared.
1202bool Debug::EnsureDebugInfo(Handle<SharedFunctionInfo> shared) {
1203 // Return if we already have the debug info for shared.
1204 if (HasDebugInfo(shared)) return true;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001205
kasper.lundbd3ec4e2008-07-09 11:06:54 +00001206 // Ensure shared in compiled. Return false if this failed.
1207 if (!EnsureCompiled(shared)) return false;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001208
1209 // Create the debug info object.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001210 Handle<DebugInfo> debug_info = Factory::NewDebugInfo(shared);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001211
1212 // Add debug info to the list.
1213 DebugInfoListNode* node = new DebugInfoListNode(*debug_info);
1214 node->set_next(debug_info_list_);
1215 debug_info_list_ = node;
1216
1217 // Now there is at least one break point.
1218 has_break_points_ = true;
1219
kasper.lundbd3ec4e2008-07-09 11:06:54 +00001220 return true;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001221}
1222
1223
1224void Debug::RemoveDebugInfo(Handle<DebugInfo> debug_info) {
1225 ASSERT(debug_info_list_ != NULL);
1226 // Run through the debug info objects to find this one and remove it.
1227 DebugInfoListNode* prev = NULL;
1228 DebugInfoListNode* current = debug_info_list_;
1229 while (current != NULL) {
1230 if (*current->debug_info() == *debug_info) {
1231 // Unlink from list. If prev is NULL we are looking at the first element.
1232 if (prev == NULL) {
1233 debug_info_list_ = current->next();
1234 } else {
1235 prev->set_next(current->next());
1236 }
1237 current->debug_info()->shared()->set_debug_info(Heap::undefined_value());
1238 delete current;
1239
1240 // If there are no more debug info objects there are not more break
1241 // points.
1242 has_break_points_ = debug_info_list_ != NULL;
1243
1244 return;
1245 }
1246 // Move to next in list.
1247 prev = current;
1248 current = current->next();
1249 }
1250 UNREACHABLE();
1251}
1252
1253
1254void Debug::SetAfterBreakTarget(JavaScriptFrame* frame) {
ager@chromium.org381abbb2009-02-25 13:23:22 +00001255 HandleScope scope;
1256
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001257 // Get the executing function in which the debug break occurred.
1258 Handle<SharedFunctionInfo> shared =
1259 Handle<SharedFunctionInfo>(JSFunction::cast(frame->function())->shared());
kasper.lundbd3ec4e2008-07-09 11:06:54 +00001260 if (!EnsureDebugInfo(shared)) {
1261 // Return if we failed to retrieve the debug info.
1262 return;
1263 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001264 Handle<DebugInfo> debug_info = GetDebugInfo(shared);
1265 Handle<Code> code(debug_info->code());
1266 Handle<Code> original_code(debug_info->original_code());
1267#ifdef DEBUG
1268 // Get the code which is actually executing.
kasperl@chromium.org061ef742009-02-27 12:16:20 +00001269 Handle<Code> frame_code(frame->code());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001270 ASSERT(frame_code.is_identical_to(code));
1271#endif
1272
1273 // Find the call address in the running code. This address holds the call to
1274 // either a DebugBreakXXX or to the debug break return entry code if the
1275 // break point is still active after processing the break point.
1276 Address addr = frame->pc() - Assembler::kTargetAddrToReturnAddrDist;
1277
1278 // Check if the location is at JS exit.
1279 bool at_js_exit = false;
1280 RelocIterator it(debug_info->code());
1281 while (!it.done()) {
ager@chromium.org236ad962008-09-25 09:45:57 +00001282 if (RelocInfo::IsJSReturn(it.rinfo()->rmode())) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001283 at_js_exit = it.rinfo()->pc() == addr - 1;
1284 }
1285 it.next();
1286 }
1287
1288 // Handle the jump to continue execution after break point depending on the
1289 // break location.
1290 if (at_js_exit) {
1291 // First check if the call in the code is still the debug break return
1292 // entry code. If it is the break point is still active. If not the break
1293 // point was removed during break point processing.
1294 if (Assembler::target_address_at(addr) ==
1295 debug_break_return_entry()->entry()) {
1296 // Break point still active. Jump to the corresponding place in the
1297 // original code.
1298 addr += original_code->instruction_start() - code->instruction_start();
1299 }
1300
1301 // Move one byte back to where the call instruction was placed.
1302 thread_local_.after_break_target_ = addr - 1;
1303 } else {
1304 // Check if there still is a debug break call at the target address. If the
1305 // break point has been removed it will have disappeared. If it have
1306 // disappeared don't try to look in the original code as the running code
1307 // will have the right address. This takes care of the case where the last
1308 // break point is removed from the function and therefore no "original code"
1309 // is available. If the debug break call is still there find the address in
1310 // the original code.
1311 if (IsDebugBreak(Assembler::target_address_at(addr))) {
1312 // If the break point is still there find the call address which was
1313 // overwritten in the original code by the call to DebugBreakXXX.
1314
1315 // Find the corresponding address in the original code.
1316 addr += original_code->instruction_start() - code->instruction_start();
1317 }
1318
1319 // Install jump to the call address in the original code. This will be the
1320 // call which was overwritten by the call to DebugBreakXXX.
1321 thread_local_.after_break_target_ = Assembler::target_address_at(addr);
1322 }
1323}
1324
1325
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001326bool Debug::IsDebugGlobal(GlobalObject* global) {
1327 return IsLoaded() && global == Debug::debug_context()->global();
1328}
1329
1330
ager@chromium.org32912102009-01-16 10:38:43 +00001331void Debug::ClearMirrorCache() {
ager@chromium.org381abbb2009-02-25 13:23:22 +00001332 HandleScope scope;
ager@chromium.org32912102009-01-16 10:38:43 +00001333 ASSERT(Top::context() == *Debug::debug_context());
1334
1335 // Clear the mirror cache.
1336 Handle<String> function_name =
1337 Factory::LookupSymbol(CStrVector("ClearMirrorCache"));
1338 Handle<Object> fun(Top::global()->GetProperty(*function_name));
1339 ASSERT(fun->IsJSFunction());
1340 bool caught_exception;
1341 Handle<Object> js_object = Execution::TryCall(
1342 Handle<JSFunction>::cast(fun),
1343 Handle<JSObject>(Debug::debug_context()->global()),
1344 0, NULL, &caught_exception);
1345}
1346
1347
iposva@chromium.org245aa852009-02-10 00:49:54 +00001348Handle<Object> Debugger::event_listener_ = Handle<Object>();
1349Handle<Object> Debugger::event_listener_data_ = Handle<Object>();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001350bool Debugger::debugger_active_ = false;
1351bool Debugger::compiling_natives_ = false;
mads.s.agercbaa0602008-08-14 13:41:48 +00001352bool Debugger::is_loading_debugger_ = false;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001353DebugMessageThread* Debugger::message_thread_ = NULL;
ager@chromium.org381abbb2009-02-25 13:23:22 +00001354v8::DebugMessageHandler Debugger::message_handler_ = NULL;
1355void* Debugger::message_handler_data_ = NULL;
1356v8::DebugHostDispatchHandler Debugger::host_dispatch_handler_ = NULL;
1357void* Debugger::host_dispatch_handler_data_ = NULL;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001358
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001359
1360Handle<Object> Debugger::MakeJSObject(Vector<const char> constructor_name,
1361 int argc, Object*** argv,
1362 bool* caught_exception) {
1363 ASSERT(Top::context() == *Debug::debug_context());
1364
1365 // Create the execution state object.
1366 Handle<String> constructor_str = Factory::LookupSymbol(constructor_name);
1367 Handle<Object> constructor(Top::global()->GetProperty(*constructor_str));
1368 ASSERT(constructor->IsJSFunction());
1369 if (!constructor->IsJSFunction()) {
1370 *caught_exception = true;
1371 return Factory::undefined_value();
1372 }
1373 Handle<Object> js_object = Execution::TryCall(
1374 Handle<JSFunction>::cast(constructor),
1375 Handle<JSObject>(Debug::debug_context()->global()), argc, argv,
1376 caught_exception);
1377 return js_object;
1378}
1379
1380
1381Handle<Object> Debugger::MakeExecutionState(bool* caught_exception) {
1382 // Create the execution state object.
1383 Handle<Object> break_id = Factory::NewNumberFromInt(Top::break_id());
1384 const int argc = 1;
1385 Object** argv[argc] = { break_id.location() };
1386 return MakeJSObject(CStrVector("MakeExecutionState"),
1387 argc, argv, caught_exception);
1388}
1389
1390
1391Handle<Object> Debugger::MakeBreakEvent(Handle<Object> exec_state,
1392 Handle<Object> break_points_hit,
1393 bool* caught_exception) {
1394 // Create the new break event object.
1395 const int argc = 2;
1396 Object** argv[argc] = { exec_state.location(),
1397 break_points_hit.location() };
1398 return MakeJSObject(CStrVector("MakeBreakEvent"),
1399 argc,
1400 argv,
1401 caught_exception);
1402}
1403
1404
1405Handle<Object> Debugger::MakeExceptionEvent(Handle<Object> exec_state,
1406 Handle<Object> exception,
1407 bool uncaught,
1408 bool* caught_exception) {
1409 // Create the new exception event object.
1410 const int argc = 3;
1411 Object** argv[argc] = { exec_state.location(),
1412 exception.location(),
1413 uncaught ? Factory::true_value().location() :
1414 Factory::false_value().location()};
1415 return MakeJSObject(CStrVector("MakeExceptionEvent"),
1416 argc, argv, caught_exception);
1417}
1418
1419
1420Handle<Object> Debugger::MakeNewFunctionEvent(Handle<Object> function,
1421 bool* caught_exception) {
1422 // Create the new function event object.
1423 const int argc = 1;
1424 Object** argv[argc] = { function.location() };
1425 return MakeJSObject(CStrVector("MakeNewFunctionEvent"),
1426 argc, argv, caught_exception);
1427}
1428
1429
1430Handle<Object> Debugger::MakeCompileEvent(Handle<Script> script,
iposva@chromium.org245aa852009-02-10 00:49:54 +00001431 bool before,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001432 bool* caught_exception) {
1433 // Create the compile event object.
1434 Handle<Object> exec_state = MakeExecutionState(caught_exception);
iposva@chromium.org245aa852009-02-10 00:49:54 +00001435 Handle<Object> script_wrapper = GetScriptWrapper(script);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001436 const int argc = 3;
iposva@chromium.org245aa852009-02-10 00:49:54 +00001437 Object** argv[argc] = { exec_state.location(),
1438 script_wrapper.location(),
1439 before ? Factory::true_value().location() :
1440 Factory::false_value().location() };
1441
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001442 return MakeJSObject(CStrVector("MakeCompileEvent"),
1443 argc,
1444 argv,
1445 caught_exception);
1446}
1447
1448
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001449void Debugger::OnException(Handle<Object> exception, bool uncaught) {
1450 HandleScope scope;
1451
1452 // Bail out based on state or if there is no listener for this event
1453 if (Debug::InDebugger()) return;
1454 if (!Debugger::EventActive(v8::Exception)) return;
1455
1456 // Bail out if exception breaks are not active
1457 if (uncaught) {
1458 // Uncaught exceptions are reported by either flags.
1459 if (!(Debug::break_on_uncaught_exception() ||
1460 Debug::break_on_exception())) return;
1461 } else {
1462 // Caught exceptions are reported is activated.
1463 if (!Debug::break_on_exception()) return;
1464 }
1465
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001466 // Enter the debugger.
1467 EnterDebugger debugger;
1468 if (debugger.FailedToEnter()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001469
1470 // Clear all current stepping setup.
1471 Debug::ClearStepping();
1472 // Create the event data object.
1473 bool caught_exception = false;
1474 Handle<Object> exec_state = MakeExecutionState(&caught_exception);
1475 Handle<Object> event_data;
1476 if (!caught_exception) {
1477 event_data = MakeExceptionEvent(exec_state, exception, uncaught,
1478 &caught_exception);
1479 }
1480 // Bail out and don't call debugger if exception.
1481 if (caught_exception) {
1482 return;
1483 }
1484
1485 // Process debug event
1486 ProcessDebugEvent(v8::Exception, event_data);
1487 // Return to continue execution from where the exception was thrown.
1488}
1489
1490
1491void Debugger::OnDebugBreak(Handle<Object> break_points_hit) {
1492 HandleScope scope;
1493
kasper.lund212ac232008-07-16 07:07:30 +00001494 // Debugger has already been entered by caller.
1495 ASSERT(Top::context() == *Debug::debug_context());
1496
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001497 // Bail out if there is no listener for this event
1498 if (!Debugger::EventActive(v8::Break)) return;
1499
1500 // Debugger must be entered in advance.
1501 ASSERT(Top::context() == *Debug::debug_context());
1502
1503 // Create the event data object.
1504 bool caught_exception = false;
1505 Handle<Object> exec_state = MakeExecutionState(&caught_exception);
1506 Handle<Object> event_data;
1507 if (!caught_exception) {
1508 event_data = MakeBreakEvent(exec_state, break_points_hit,
1509 &caught_exception);
1510 }
1511 // Bail out and don't call debugger if exception.
1512 if (caught_exception) {
1513 return;
1514 }
1515
1516 // Process debug event
1517 ProcessDebugEvent(v8::Break, event_data);
1518}
1519
1520
1521void Debugger::OnBeforeCompile(Handle<Script> script) {
1522 HandleScope scope;
1523
1524 // Bail out based on state or if there is no listener for this event
1525 if (Debug::InDebugger()) return;
1526 if (compiling_natives()) return;
1527 if (!EventActive(v8::BeforeCompile)) return;
1528
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001529 // Enter the debugger.
1530 EnterDebugger debugger;
1531 if (debugger.FailedToEnter()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001532
1533 // Create the event data object.
1534 bool caught_exception = false;
iposva@chromium.org245aa852009-02-10 00:49:54 +00001535 Handle<Object> event_data = MakeCompileEvent(script, true, &caught_exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001536 // Bail out and don't call debugger if exception.
1537 if (caught_exception) {
1538 return;
1539 }
1540
1541 // Process debug event
1542 ProcessDebugEvent(v8::BeforeCompile, event_data);
1543}
1544
1545
1546// Handle debugger actions when a new script is compiled.
1547void Debugger::OnAfterCompile(Handle<Script> script, Handle<JSFunction> fun) {
kasper.lund212ac232008-07-16 07:07:30 +00001548 HandleScope scope;
1549
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001550 // No compile events while compiling natives.
1551 if (compiling_natives()) return;
1552
1553 // No more to do if not debugging.
1554 if (!debugger_active()) return;
1555
iposva@chromium.org245aa852009-02-10 00:49:54 +00001556 // Store whether in debugger before entering debugger.
1557 bool in_debugger = Debug::InDebugger();
1558
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001559 // Enter the debugger.
1560 EnterDebugger debugger;
1561 if (debugger.FailedToEnter()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001562
1563 // If debugging there might be script break points registered for this
1564 // script. Make sure that these break points are set.
1565
1566 // Get the function UpdateScriptBreakPoints (defined in debug-delay.js).
1567 Handle<Object> update_script_break_points =
1568 Handle<Object>(Debug::debug_context()->global()->GetProperty(
1569 *Factory::LookupAsciiSymbol("UpdateScriptBreakPoints")));
1570 if (!update_script_break_points->IsJSFunction()) {
1571 return;
1572 }
1573 ASSERT(update_script_break_points->IsJSFunction());
1574
1575 // Wrap the script object in a proper JS object before passing it
1576 // to JavaScript.
1577 Handle<JSValue> wrapper = GetScriptWrapper(script);
1578
1579 // Call UpdateScriptBreakPoints expect no exceptions.
kasper.lund212ac232008-07-16 07:07:30 +00001580 bool caught_exception = false;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001581 const int argc = 1;
1582 Object** argv[argc] = { reinterpret_cast<Object**>(wrapper.location()) };
1583 Handle<Object> result = Execution::TryCall(
1584 Handle<JSFunction>::cast(update_script_break_points),
1585 Top::builtins(), argc, argv,
1586 &caught_exception);
1587 if (caught_exception) {
1588 return;
1589 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001590 // Bail out based on state or if there is no listener for this event
iposva@chromium.org245aa852009-02-10 00:49:54 +00001591 if (in_debugger) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001592 if (!Debugger::EventActive(v8::AfterCompile)) return;
1593
1594 // Create the compile state object.
1595 Handle<Object> event_data = MakeCompileEvent(script,
iposva@chromium.org245aa852009-02-10 00:49:54 +00001596 false,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001597 &caught_exception);
1598 // Bail out and don't call debugger if exception.
1599 if (caught_exception) {
1600 return;
1601 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001602 // Process debug event
1603 ProcessDebugEvent(v8::AfterCompile, event_data);
1604}
1605
1606
1607void Debugger::OnNewFunction(Handle<JSFunction> function) {
1608 return;
1609 HandleScope scope;
1610
1611 // Bail out based on state or if there is no listener for this event
1612 if (Debug::InDebugger()) return;
1613 if (compiling_natives()) return;
1614 if (!Debugger::EventActive(v8::NewFunction)) return;
1615
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001616 // Enter the debugger.
1617 EnterDebugger debugger;
1618 if (debugger.FailedToEnter()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001619
1620 // Create the event object.
1621 bool caught_exception = false;
1622 Handle<Object> event_data = MakeNewFunctionEvent(function, &caught_exception);
1623 // Bail out and don't call debugger if exception.
1624 if (caught_exception) {
1625 return;
1626 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001627 // Process debug event.
1628 ProcessDebugEvent(v8::NewFunction, event_data);
1629}
1630
1631
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001632void Debugger::ProcessDebugEvent(v8::DebugEvent event,
1633 Handle<Object> event_data) {
ager@chromium.org381abbb2009-02-25 13:23:22 +00001634 HandleScope scope;
1635
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001636 // Create the execution state.
1637 bool caught_exception = false;
1638 Handle<Object> exec_state = MakeExecutionState(&caught_exception);
1639 if (caught_exception) {
1640 return;
1641 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001642 // First notify the builtin debugger.
1643 if (message_thread_ != NULL) {
1644 message_thread_->DebugEvent(event, exec_state, event_data);
1645 }
iposva@chromium.org245aa852009-02-10 00:49:54 +00001646 // Notify registered debug event listener. This can be either a C or a
1647 // JavaScript function.
1648 if (!event_listener_.is_null()) {
1649 if (event_listener_->IsProxy()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001650 // C debug event listener.
iposva@chromium.org245aa852009-02-10 00:49:54 +00001651 Handle<Proxy> callback_obj(Handle<Proxy>::cast(event_listener_));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001652 v8::DebugEventCallback callback =
1653 FUNCTION_CAST<v8::DebugEventCallback>(callback_obj->proxy());
1654 callback(event,
1655 v8::Utils::ToLocal(Handle<JSObject>::cast(exec_state)),
1656 v8::Utils::ToLocal(Handle<JSObject>::cast(event_data)),
iposva@chromium.org245aa852009-02-10 00:49:54 +00001657 v8::Utils::ToLocal(Handle<Object>::cast(event_listener_data_)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001658 } else {
1659 // JavaScript debug event listener.
iposva@chromium.org245aa852009-02-10 00:49:54 +00001660 ASSERT(event_listener_->IsJSFunction());
1661 Handle<JSFunction> fun(Handle<JSFunction>::cast(event_listener_));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001662
1663 // Invoke the JavaScript debug event listener.
1664 const int argc = 4;
1665 Object** argv[argc] = { Handle<Object>(Smi::FromInt(event)).location(),
1666 exec_state.location(),
1667 event_data.location(),
iposva@chromium.org245aa852009-02-10 00:49:54 +00001668 event_listener_data_.location() };
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001669 Handle<Object> result = Execution::TryCall(fun, Top::global(),
1670 argc, argv, &caught_exception);
1671 if (caught_exception) {
1672 // Silently ignore exceptions from debug event listeners.
1673 }
1674 }
1675 }
ager@chromium.org32912102009-01-16 10:38:43 +00001676
1677 // Clear the mirror cache.
1678 Debug::ClearMirrorCache();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001679}
1680
1681
iposva@chromium.org245aa852009-02-10 00:49:54 +00001682void Debugger::SetEventListener(Handle<Object> callback,
1683 Handle<Object> data) {
1684 HandleScope scope;
1685
1686 // Clear the global handles for the event listener and the event listener data
1687 // object.
1688 if (!event_listener_.is_null()) {
1689 GlobalHandles::Destroy(
1690 reinterpret_cast<Object**>(event_listener_.location()));
1691 event_listener_ = Handle<Object>();
1692 }
1693 if (!event_listener_data_.is_null()) {
1694 GlobalHandles::Destroy(
1695 reinterpret_cast<Object**>(event_listener_data_.location()));
1696 event_listener_data_ = Handle<Object>();
1697 }
1698
1699 // If there is a new debug event listener register it together with its data
1700 // object.
1701 if (!callback->IsUndefined() && !callback->IsNull()) {
1702 event_listener_ = Handle<Object>::cast(GlobalHandles::Create(*callback));
1703 if (data.is_null()) {
1704 data = Factory::undefined_value();
1705 }
1706 event_listener_data_ = Handle<Object>::cast(GlobalHandles::Create(*data));
1707 }
1708
1709 UpdateActiveDebugger();
1710}
1711
1712
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001713void Debugger::SetMessageHandler(v8::DebugMessageHandler handler, void* data) {
ager@chromium.org381abbb2009-02-25 13:23:22 +00001714 message_handler_ = handler;
1715 message_handler_data_ = data;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001716 if (!message_thread_) {
iposva@chromium.org245aa852009-02-10 00:49:54 +00001717 message_thread_ = new DebugMessageThread();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001718 message_thread_->Start();
1719 }
1720 UpdateActiveDebugger();
1721}
1722
1723
ager@chromium.org381abbb2009-02-25 13:23:22 +00001724void Debugger::SetHostDispatchHandler(v8::DebugHostDispatchHandler handler,
1725 void* data) {
1726 host_dispatch_handler_ = handler;
1727 host_dispatch_handler_data_ = data;
1728}
1729
1730
kasper.lund7276f142008-07-30 08:49:36 +00001731// Posts an output message from the debugger to the debug_message_handler
1732// callback. This callback is part of the public API. Messages are
1733// kept internally as Vector<uint16_t> strings, which are allocated in various
1734// places and deallocated by the calling function sometime after this call.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001735void Debugger::SendMessage(Vector< uint16_t> message) {
ager@chromium.org381abbb2009-02-25 13:23:22 +00001736 if (message_handler_ != NULL) {
1737 message_handler_(message.start(), message.length(), message_handler_data_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001738 }
1739}
1740
1741
1742void Debugger::ProcessCommand(Vector<const uint16_t> command) {
1743 if (message_thread_ != NULL) {
1744 message_thread_->ProcessCommand(
1745 Vector<uint16_t>(const_cast<uint16_t *>(command.start()),
1746 command.length()));
1747 }
1748}
1749
1750
ager@chromium.org381abbb2009-02-25 13:23:22 +00001751void Debugger::ProcessHostDispatch(void* dispatch) {
1752 if (message_thread_ != NULL) {
1753 message_thread_->ProcessHostDispatch(dispatch);
1754 }
1755}
1756
1757
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001758void Debugger::UpdateActiveDebugger() {
iposva@chromium.org245aa852009-02-10 00:49:54 +00001759 set_debugger_active((message_thread_ != NULL &&
ager@chromium.org381abbb2009-02-25 13:23:22 +00001760 message_handler_ != NULL) ||
iposva@chromium.org245aa852009-02-10 00:49:54 +00001761 !event_listener_.is_null());
1762 if (!debugger_active() && message_thread_) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001763 message_thread_->OnDebuggerInactive();
iposva@chromium.org245aa852009-02-10 00:49:54 +00001764 }
ager@chromium.org381abbb2009-02-25 13:23:22 +00001765 if (!debugger_active()) {
1766 Debug::Unload();
1767 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001768}
1769
1770
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001771Handle<Object> Debugger::Call(Handle<JSFunction> fun,
1772 Handle<Object> data,
1773 bool* pending_exception) {
1774 // Enter the debugger.
1775 EnterDebugger debugger;
1776 if (debugger.FailedToEnter() || !debugger.HasJavaScriptFrames()) {
1777 return Factory::undefined_value();
1778 }
1779
1780 // Create the execution state.
1781 bool caught_exception = false;
1782 Handle<Object> exec_state = MakeExecutionState(&caught_exception);
1783 if (caught_exception) {
1784 return Factory::undefined_value();
1785 }
1786
1787 static const int kArgc = 2;
1788 Object** argv[kArgc] = { exec_state.location(), data.location() };
1789 Handle<Object> result = Execution::Call(fun, Factory::undefined_value(),
1790 kArgc, argv, pending_exception);
1791 return result;
1792}
1793
1794
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001795DebugMessageThread::DebugMessageThread()
1796 : host_running_(true),
kasper.lund7276f142008-07-30 08:49:36 +00001797 command_queue_(kQueueInitialSize),
1798 message_queue_(kQueueInitialSize) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001799 command_received_ = OS::CreateSemaphore(0);
kasper.lund7276f142008-07-30 08:49:36 +00001800 message_received_ = OS::CreateSemaphore(0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001801}
1802
kasper.lund7276f142008-07-30 08:49:36 +00001803// Does not free resources held by DebugMessageThread
1804// because this cannot be done thread-safely.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001805DebugMessageThread::~DebugMessageThread() {
1806}
1807
1808
kasper.lund7276f142008-07-30 08:49:36 +00001809// Puts an event coming from V8 on the queue. Creates
1810// a copy of the JSON formatted event string managed by the V8.
1811// Called by the V8 thread.
1812// The new copy of the event string is destroyed in Run().
1813void DebugMessageThread::SendMessage(Vector<uint16_t> message) {
1814 Vector<uint16_t> message_copy = message.Clone();
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +00001815 Logger::DebugTag("Put message on event message_queue.");
kasper.lund7276f142008-07-30 08:49:36 +00001816 message_queue_.Put(message_copy);
1817 message_received_->Signal();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001818}
1819
1820
ager@chromium.org8bb60582008-12-11 12:02:20 +00001821bool DebugMessageThread::SetEventJSONFromEvent(Handle<Object> event_data) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001822 v8::HandleScope scope;
1823 // Call toJSONProtocol on the debug event object.
1824 v8::Local<v8::Object> api_event_data =
1825 v8::Utils::ToLocal(Handle<JSObject>::cast(event_data));
1826 v8::Local<v8::String> fun_name = v8::String::New("toJSONProtocol");
1827 v8::Local<v8::Function> fun =
1828 v8::Function::Cast(*api_event_data->Get(fun_name));
1829 v8::TryCatch try_catch;
kasper.lund7276f142008-07-30 08:49:36 +00001830 v8::Local<v8::Value> json_event = *fun->Call(api_event_data, 0, NULL);
1831 v8::Local<v8::String> json_event_string;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001832 if (!try_catch.HasCaught()) {
kasper.lund7276f142008-07-30 08:49:36 +00001833 if (!json_event->IsUndefined()) {
1834 json_event_string = json_event->ToString();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001835 if (FLAG_trace_debug_json) {
kasper.lund7276f142008-07-30 08:49:36 +00001836 PrintLn(json_event_string);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001837 }
kasper.lund7276f142008-07-30 08:49:36 +00001838 v8::String::Value val(json_event_string);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001839 Vector<uint16_t> str(reinterpret_cast<uint16_t*>(*val),
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001840 json_event_string->Length());
kasper.lund7276f142008-07-30 08:49:36 +00001841 SendMessage(str);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001842 } else {
kasper.lund7276f142008-07-30 08:49:36 +00001843 SendMessage(Vector<uint16_t>::empty());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001844 }
1845 } else {
1846 PrintLn(try_catch.Exception());
ager@chromium.org8bb60582008-12-11 12:02:20 +00001847 return false;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001848 }
ager@chromium.org8bb60582008-12-11 12:02:20 +00001849 return true;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001850}
1851
1852
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001853void DebugMessageThread::Run() {
kasper.lund7276f142008-07-30 08:49:36 +00001854 // Sends debug events to an installed debugger message callback.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001855 while (true) {
kasper.lund7276f142008-07-30 08:49:36 +00001856 // Wait and Get are paired so that semaphore count equals queue length.
1857 message_received_->Wait();
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +00001858 Logger::DebugTag("Get message from event message_queue.");
kasper.lund7276f142008-07-30 08:49:36 +00001859 Vector<uint16_t> message = message_queue_.Get();
1860 if (message.length() > 0) {
1861 Debugger::SendMessage(message);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001862 }
1863 }
1864}
1865
1866
kasper.lund44510672008-07-25 07:37:58 +00001867// This method is called by the V8 thread whenever a debug event occurs in
1868// the VM.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001869void DebugMessageThread::DebugEvent(v8::DebugEvent event,
1870 Handle<Object> exec_state,
1871 Handle<Object> event_data) {
ager@chromium.org381abbb2009-02-25 13:23:22 +00001872 HandleScope scope;
1873
kasper.lund212ac232008-07-16 07:07:30 +00001874 if (!Debug::Load()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001875
1876 // Process the individual events.
1877 bool interactive = false;
1878 switch (event) {
1879 case v8::Break:
v8.team.kasperl727e9952008-09-02 14:56:44 +00001880 interactive = true; // Break event is always interactive
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001881 break;
1882 case v8::Exception:
v8.team.kasperl727e9952008-09-02 14:56:44 +00001883 interactive = true; // Exception event is always interactive
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001884 break;
1885 case v8::BeforeCompile:
1886 break;
1887 case v8::AfterCompile:
1888 break;
1889 case v8::NewFunction:
1890 break;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001891 default:
1892 UNREACHABLE();
1893 }
1894
1895 // Done if not interactive.
1896 if (!interactive) return;
1897
1898 // Get the DebugCommandProcessor.
1899 v8::Local<v8::Object> api_exec_state =
1900 v8::Utils::ToLocal(Handle<JSObject>::cast(exec_state));
1901 v8::Local<v8::String> fun_name =
1902 v8::String::New("debugCommandProcessor");
1903 v8::Local<v8::Function> fun =
1904 v8::Function::Cast(*api_exec_state->Get(fun_name));
1905 v8::TryCatch try_catch;
1906 v8::Local<v8::Object> cmd_processor =
1907 v8::Object::Cast(*fun->Call(api_exec_state, 0, NULL));
1908 if (try_catch.HasCaught()) {
1909 PrintLn(try_catch.Exception());
1910 return;
1911 }
1912
v8.team.kasperl727e9952008-09-02 14:56:44 +00001913 // Notify the debugger that a debug event has occurred.
ager@chromium.org8bb60582008-12-11 12:02:20 +00001914 bool success = SetEventJSONFromEvent(event_data);
1915 if (!success) {
1916 // If failed to notify debugger just continue running.
1917 return;
1918 }
kasper.lund7276f142008-07-30 08:49:36 +00001919
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001920 // Wait for requests from the debugger.
ager@chromium.org8bb60582008-12-11 12:02:20 +00001921 host_running_ = false;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001922 while (true) {
kasper.lund7276f142008-07-30 08:49:36 +00001923 command_received_->Wait();
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001924 Logger::DebugTag("Got request from command queue, in interactive loop.");
kasper.lund7276f142008-07-30 08:49:36 +00001925 Vector<uint16_t> command = command_queue_.Get();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001926 ASSERT(!host_running_);
1927 if (!Debugger::debugger_active()) {
1928 host_running_ = true;
1929 return;
1930 }
1931
ager@chromium.org381abbb2009-02-25 13:23:22 +00001932 // Check if the command is a host dispatch.
1933 if (command[0] == 0) {
1934 if (Debugger::host_dispatch_handler_) {
1935 int32_t dispatch = (command[1] << 16) | command[2];
1936 Debugger::host_dispatch_handler_(reinterpret_cast<void*>(dispatch),
1937 Debugger::host_dispatch_handler_data_);
1938 }
1939 continue;
1940 }
1941
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001942 // Invoke the JavaScript to process the debug request.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001943 v8::Local<v8::String> fun_name;
1944 v8::Local<v8::Function> fun;
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001945 v8::Local<v8::Value> request;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001946 v8::TryCatch try_catch;
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001947 fun_name = v8::String::New("processDebugRequest");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001948 fun = v8::Function::Cast(*cmd_processor->Get(fun_name));
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001949 request = v8::String::New(reinterpret_cast<uint16_t*>(command.start()),
kasper.lund7276f142008-07-30 08:49:36 +00001950 command.length());
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001951 static const int kArgc = 1;
1952 v8::Handle<Value> argv[kArgc] = { request };
1953 v8::Local<v8::Value> response_val = fun->Call(cmd_processor, kArgc, argv);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001954
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001955 // Get the response.
1956 v8::Local<v8::String> response;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001957 bool running = false;
1958 if (!try_catch.HasCaught()) {
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001959 // Get response string.
1960 if (!response_val->IsUndefined()) {
1961 response = v8::String::Cast(*response_val);
1962 } else {
1963 response = v8::String::New("");
1964 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001965
1966 // Log the JSON request/response.
1967 if (FLAG_trace_debug_json) {
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001968 PrintLn(request);
1969 PrintLn(response);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001970 }
1971
1972 // Get the running state.
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001973 fun_name = v8::String::New("isRunning");
1974 fun = v8::Function::Cast(*cmd_processor->Get(fun_name));
1975 static const int kArgc = 1;
1976 v8::Handle<Value> argv[kArgc] = { response };
1977 v8::Local<v8::Value> running_val = fun->Call(cmd_processor, kArgc, argv);
1978 if (!try_catch.HasCaught()) {
1979 running = running_val->ToBoolean()->Value();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001980 }
1981 } else {
1982 // In case of failure the result text is the exception text.
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001983 response = try_catch.Exception()->ToString();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001984 }
1985
1986 // Convert text result to C string.
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001987 v8::String::Value val(response);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001988 Vector<uint16_t> str(reinterpret_cast<uint16_t*>(*val),
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001989 response->Length());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001990
kasper.lund7276f142008-07-30 08:49:36 +00001991 // Set host_running_ correctly for nested debugger evaluations.
1992 host_running_ = running;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001993
1994 // Return the result.
kasper.lund7276f142008-07-30 08:49:36 +00001995 SendMessage(str);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001996
1997 // Return from debug event processing is VM should be running.
1998 if (running) {
1999 return;
2000 }
2001 }
2002}
2003
2004
kasper.lund7276f142008-07-30 08:49:36 +00002005// Puts a command coming from the public API on the queue. Creates
2006// a copy of the command string managed by the debugger. Up to this
2007// point, the command data was managed by the API client. Called
2008// by the API client thread. This is where the API client hands off
2009// processing of the command to the DebugMessageThread thread.
2010// The new copy of the command is destroyed in HandleCommand().
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002011void DebugMessageThread::ProcessCommand(Vector<uint16_t> command) {
kasper.lund7276f142008-07-30 08:49:36 +00002012 Vector<uint16_t> command_copy = command.Clone();
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +00002013 Logger::DebugTag("Put command on command_queue.");
kasper.lund7276f142008-07-30 08:49:36 +00002014 command_queue_.Put(command_copy);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002015 command_received_->Signal();
2016}
2017
2018
ager@chromium.org381abbb2009-02-25 13:23:22 +00002019// Puts a host dispatch comming from the public API on the queue.
2020void DebugMessageThread::ProcessHostDispatch(void* dispatch) {
2021 uint16_t hack[3];
2022 hack[0] = 0;
2023 hack[1] = reinterpret_cast<uint32_t>(dispatch) >> 16;
2024 hack[2] = reinterpret_cast<uint32_t>(dispatch) & 0xFFFF;
2025 Logger::DebugTag("Put dispatch on command_queue.");
2026 command_queue_.Put(Vector<uint16_t>(hack, 3).Clone());
2027 command_received_->Signal();
2028}
2029
2030
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002031void DebugMessageThread::OnDebuggerInactive() {
kasper.lund7276f142008-07-30 08:49:36 +00002032 // Send an empty command to the debugger if in a break to make JavaScript run
2033 // again if the debugger is closed.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002034 if (!host_running_) {
kasper.lund7276f142008-07-30 08:49:36 +00002035 ProcessCommand(Vector<uint16_t>::empty());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002036 }
2037}
2038
kasper.lund7276f142008-07-30 08:49:36 +00002039
2040MessageQueue::MessageQueue(int size) : start_(0), end_(0), size_(size) {
2041 messages_ = NewArray<Vector<uint16_t> >(size);
2042}
2043
2044
2045MessageQueue::~MessageQueue() {
2046 DeleteArray(messages_);
2047}
2048
2049
2050Vector<uint16_t> MessageQueue::Get() {
2051 ASSERT(!IsEmpty());
2052 int result = start_;
2053 start_ = (start_ + 1) % size_;
2054 return messages_[result];
2055}
2056
2057
2058void MessageQueue::Put(const Vector<uint16_t>& message) {
2059 if ((end_ + 1) % size_ == start_) {
2060 Expand();
2061 }
2062 messages_[end_] = message;
2063 end_ = (end_ + 1) % size_;
2064}
2065
2066
2067void MessageQueue::Expand() {
2068 MessageQueue new_queue(size_ * 2);
2069 while (!IsEmpty()) {
2070 new_queue.Put(Get());
2071 }
2072 Vector<uint16_t>* array_to_free = messages_;
2073 *this = new_queue;
2074 new_queue.messages_ = array_to_free;
2075 // Automatic destructor called on new_queue, freeing array_to_free.
2076}
2077
2078
2079LockingMessageQueue::LockingMessageQueue(int size) : queue_(size) {
2080 lock_ = OS::CreateMutex();
2081}
2082
2083
2084LockingMessageQueue::~LockingMessageQueue() {
2085 delete lock_;
2086}
2087
2088
2089bool LockingMessageQueue::IsEmpty() const {
2090 ScopedLock sl(lock_);
2091 return queue_.IsEmpty();
2092}
2093
2094
2095Vector<uint16_t> LockingMessageQueue::Get() {
2096 ScopedLock sl(lock_);
2097 Vector<uint16_t> result = queue_.Get();
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +00002098 Logger::DebugEvent("Get", result);
kasper.lund7276f142008-07-30 08:49:36 +00002099 return result;
2100}
2101
2102
2103void LockingMessageQueue::Put(const Vector<uint16_t>& message) {
2104 ScopedLock sl(lock_);
2105 queue_.Put(message);
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +00002106 Logger::DebugEvent("Put", message);
kasper.lund7276f142008-07-30 08:49:36 +00002107}
2108
2109
2110void LockingMessageQueue::Clear() {
2111 ScopedLock sl(lock_);
2112 queue_.Clear();
2113}
2114
2115
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002116} } // namespace v8::internal