blob: 1a4a9485d17ee9f5754cf268df6d3606ddb1f214 [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 "bootstrapper.h"
32#include "debug.h"
33#include "execution.h"
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000034#include "messages.h"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000035#include "platform.h"
ager@chromium.orgc4c92722009-11-18 14:12:51 +000036#include "simulator.h"
37#include "string-stream.h"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000038
kasperl@chromium.org71affb52009-05-26 05:44:31 +000039namespace v8 {
40namespace internal {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000041
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000042ThreadLocalTop Top::thread_local_;
ager@chromium.orgddb913d2009-01-27 10:01:48 +000043Mutex* Top::break_access_ = OS::CreateMutex();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000044
kasper.lundaf4734f2008-07-28 12:50:18 +000045NoAllocationStringAllocator* preallocated_message_space = NULL;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000046
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +000047bool capture_stack_trace_for_uncaught_exceptions = false;
48int stack_trace_for_uncaught_exceptions_frame_limit = 0;
49StackTrace::StackTraceOptions stack_trace_for_uncaught_exceptions_options =
50 StackTrace::kOverview;
51
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000052Address top_addresses[] = {
53#define C(name) reinterpret_cast<Address>(Top::name()),
54 TOP_ADDRESS_LIST(C)
ager@chromium.orge2902be2009-06-08 12:21:35 +000055 TOP_ADDRESS_LIST_PROF(C)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000056#undef C
57 NULL
58};
59
ager@chromium.orgc4c92722009-11-18 14:12:51 +000060
61v8::TryCatch* ThreadLocalTop::TryCatchHandler() {
62 return TRY_CATCH_FROM_ADDRESS(try_catch_handler_address());
63}
64
65
66void ThreadLocalTop::Initialize() {
67 c_entry_fp_ = 0;
68 handler_ = 0;
69#ifdef ENABLE_LOGGING_AND_PROFILING
70 js_entry_sp_ = 0;
71#endif
72 stack_is_cooked_ = false;
73 try_catch_handler_address_ = NULL;
74 context_ = NULL;
75 int id = ThreadManager::CurrentId();
76 thread_id_ = (id == 0) ? ThreadManager::kInvalidId : id;
77 external_caught_exception_ = false;
78 failed_access_check_callback_ = NULL;
79 save_context_ = NULL;
80 catcher_ = NULL;
81}
82
83
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000084Address Top::get_address_from_id(Top::AddressId id) {
85 return top_addresses[id];
86}
87
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +000088
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000089char* Top::Iterate(ObjectVisitor* v, char* thread_storage) {
90 ThreadLocalTop* thread = reinterpret_cast<ThreadLocalTop*>(thread_storage);
91 Iterate(v, thread);
92 return thread_storage + sizeof(ThreadLocalTop);
93}
94
95
whesse@chromium.orgcec079d2010-03-22 14:44:04 +000096void Top::IterateThread(ThreadVisitor* v) {
97 v->VisitThread(&thread_local_);
98}
99
100
101void Top::IterateThread(ThreadVisitor* v, char* t) {
102 ThreadLocalTop* thread = reinterpret_cast<ThreadLocalTop*>(t);
103 v->VisitThread(thread);
104}
105
106
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000107void Top::Iterate(ObjectVisitor* v, ThreadLocalTop* thread) {
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000108 v->VisitPointer(&(thread->pending_exception_));
ager@chromium.org8bb60582008-12-11 12:02:20 +0000109 v->VisitPointer(&(thread->pending_message_obj_));
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000110 v->VisitPointer(BitCast<Object**>(&(thread->pending_message_script_)));
111 v->VisitPointer(BitCast<Object**>(&(thread->context_)));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000112 v->VisitPointer(&(thread->scheduled_exception_));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000113
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000114 for (v8::TryCatch* block = thread->TryCatchHandler();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000115 block != NULL;
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000116 block = TRY_CATCH_FROM_ADDRESS(block->next_)) {
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000117 v->VisitPointer(BitCast<Object**>(&(block->exception_)));
118 v->VisitPointer(BitCast<Object**>(&(block->message_)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000119 }
120
121 // Iterate over pointers on native execution stack.
122 for (StackFrameIterator it(thread); !it.done(); it.Advance()) {
123 it.frame()->Iterate(v);
124 }
125}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000126
127
128void Top::Iterate(ObjectVisitor* v) {
129 ThreadLocalTop* current_t = &thread_local_;
130 Iterate(v, current_t);
131}
132
133
134void Top::InitializeThreadLocal() {
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000135 thread_local_.Initialize();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000136 clear_pending_exception();
ager@chromium.org8bb60582008-12-11 12:02:20 +0000137 clear_pending_message();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000138 clear_scheduled_exception();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000139}
140
141
kasper.lundaf4734f2008-07-28 12:50:18 +0000142// Create a dummy thread that will wait forever on a semaphore. The only
143// purpose for this thread is to have some stack area to save essential data
144// into for use by a stacks only core dump (aka minidump).
145class PreallocatedMemoryThread: public Thread {
146 public:
147 PreallocatedMemoryThread() : keep_running_(true) {
148 wait_for_ever_semaphore_ = OS::CreateSemaphore(0);
149 data_ready_semaphore_ = OS::CreateSemaphore(0);
150 }
151
152 // When the thread starts running it will allocate a fixed number of bytes
153 // on the stack and publish the location of this memory for others to use.
154 void Run() {
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000155 EmbeddedVector<char, 15 * 1024> local_buffer;
kasper.lundaf4734f2008-07-28 12:50:18 +0000156
157 // Initialize the buffer with a known good value.
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000158 OS::StrNCpy(local_buffer, "Trace data was not generated.\n",
159 local_buffer.length());
kasper.lundaf4734f2008-07-28 12:50:18 +0000160
161 // Publish the local buffer and signal its availability.
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000162 data_ = local_buffer.start();
163 length_ = local_buffer.length();
kasper.lundaf4734f2008-07-28 12:50:18 +0000164 data_ready_semaphore_->Signal();
165
166 while (keep_running_) {
167 // This thread will wait here until the end of time.
168 wait_for_ever_semaphore_->Wait();
169 }
170
171 // Make sure we access the buffer after the wait to remove all possibility
172 // of it being optimized away.
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000173 OS::StrNCpy(local_buffer, "PreallocatedMemoryThread shutting down.\n",
174 local_buffer.length());
kasper.lundaf4734f2008-07-28 12:50:18 +0000175 }
176
177 static char* data() {
178 if (data_ready_semaphore_ != NULL) {
179 // Initial access is guarded until the data has been published.
180 data_ready_semaphore_->Wait();
181 delete data_ready_semaphore_;
182 data_ready_semaphore_ = NULL;
183 }
184 return data_;
185 }
186
187 static unsigned length() {
188 if (data_ready_semaphore_ != NULL) {
189 // Initial access is guarded until the data has been published.
190 data_ready_semaphore_->Wait();
191 delete data_ready_semaphore_;
192 data_ready_semaphore_ = NULL;
193 }
194 return length_;
195 }
196
197 static void StartThread() {
198 if (the_thread_ != NULL) return;
199
200 the_thread_ = new PreallocatedMemoryThread();
201 the_thread_->Start();
202 }
203
204 // Stop the PreallocatedMemoryThread and release its resources.
205 static void StopThread() {
206 if (the_thread_ == NULL) return;
207
208 the_thread_->keep_running_ = false;
209 wait_for_ever_semaphore_->Signal();
210
211 // Wait for the thread to terminate.
212 the_thread_->Join();
213
214 if (data_ready_semaphore_ != NULL) {
215 delete data_ready_semaphore_;
216 data_ready_semaphore_ = NULL;
217 }
218
219 delete wait_for_ever_semaphore_;
220 wait_for_ever_semaphore_ = NULL;
221
222 // Done with the thread entirely.
223 delete the_thread_;
224 the_thread_ = NULL;
225 }
226
227 private:
228 // Used to make sure that the thread keeps looping even for spurious wakeups.
229 bool keep_running_;
230
231 // The preallocated memory thread singleton.
232 static PreallocatedMemoryThread* the_thread_;
233 // This semaphore is used by the PreallocatedMemoryThread to wait for ever.
234 static Semaphore* wait_for_ever_semaphore_;
235 // Semaphore to signal that the data has been initialized.
236 static Semaphore* data_ready_semaphore_;
237
238 // Location and size of the preallocated memory block.
239 static char* data_;
240 static unsigned length_;
241
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000242 DISALLOW_COPY_AND_ASSIGN(PreallocatedMemoryThread);
kasper.lundaf4734f2008-07-28 12:50:18 +0000243};
244
245PreallocatedMemoryThread* PreallocatedMemoryThread::the_thread_ = NULL;
246Semaphore* PreallocatedMemoryThread::wait_for_ever_semaphore_ = NULL;
247Semaphore* PreallocatedMemoryThread::data_ready_semaphore_ = NULL;
248char* PreallocatedMemoryThread::data_ = NULL;
249unsigned PreallocatedMemoryThread::length_ = 0;
250
251static bool initialized = false;
252
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000253void Top::Initialize() {
kasper.lundaf4734f2008-07-28 12:50:18 +0000254 CHECK(!initialized);
255
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000256 InitializeThreadLocal();
257
kasper.lundaf4734f2008-07-28 12:50:18 +0000258 // Only preallocate on the first initialization.
259 if (FLAG_preallocate_message_memory && (preallocated_message_space == NULL)) {
260 // Start the thread which will set aside some memory.
261 PreallocatedMemoryThread::StartThread();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000262 preallocated_message_space =
kasper.lundaf4734f2008-07-28 12:50:18 +0000263 new NoAllocationStringAllocator(PreallocatedMemoryThread::data(),
264 PreallocatedMemoryThread::length());
265 PreallocatedStorage::Init(PreallocatedMemoryThread::length() / 4);
266 }
267 initialized = true;
268}
269
270
271void Top::TearDown() {
272 if (initialized) {
273 // Remove the external reference to the preallocated stack memory.
274 if (preallocated_message_space != NULL) {
275 delete preallocated_message_space;
276 preallocated_message_space = NULL;
277 }
278
279 PreallocatedMemoryThread::StopThread();
280 initialized = false;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000281 }
282}
283
284
285void Top::RegisterTryCatchHandler(v8::TryCatch* that) {
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000286 // The ARM simulator has a separate JS stack. We therefore register
287 // the C++ try catch handler with the simulator and get back an
288 // address that can be used for comparisons with addresses into the
289 // JS stack. When running without the simulator, the address
290 // returned will be the address of the C++ try catch handler itself.
291 Address address = reinterpret_cast<Address>(
292 SimulatorStack::RegisterCTryCatch(reinterpret_cast<uintptr_t>(that)));
293 thread_local_.set_try_catch_handler_address(address);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000294}
295
296
297void Top::UnregisterTryCatchHandler(v8::TryCatch* that) {
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000298 ASSERT(thread_local_.TryCatchHandler() == that);
299 thread_local_.set_try_catch_handler_address(
300 reinterpret_cast<Address>(that->next_));
ager@chromium.org71daaf62009-04-01 07:22:49 +0000301 thread_local_.catcher_ = NULL;
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000302 SimulatorStack::UnregisterCTryCatch();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000303}
304
305
kasperl@chromium.org061ef742009-02-27 12:16:20 +0000306void Top::MarkCompactPrologue(bool is_compacting) {
307 MarkCompactPrologue(is_compacting, &thread_local_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000308}
309
310
kasperl@chromium.org061ef742009-02-27 12:16:20 +0000311void Top::MarkCompactPrologue(bool is_compacting, char* data) {
312 MarkCompactPrologue(is_compacting, reinterpret_cast<ThreadLocalTop*>(data));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000313}
314
315
kasperl@chromium.org061ef742009-02-27 12:16:20 +0000316void Top::MarkCompactPrologue(bool is_compacting, ThreadLocalTop* thread) {
317 if (is_compacting) {
318 StackFrame::CookFramesForThread(thread);
319 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000320}
321
322
kasperl@chromium.org061ef742009-02-27 12:16:20 +0000323void Top::MarkCompactEpilogue(bool is_compacting, char* data) {
324 MarkCompactEpilogue(is_compacting, reinterpret_cast<ThreadLocalTop*>(data));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000325}
326
327
kasperl@chromium.org061ef742009-02-27 12:16:20 +0000328void Top::MarkCompactEpilogue(bool is_compacting) {
329 MarkCompactEpilogue(is_compacting, &thread_local_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000330}
331
332
kasperl@chromium.org061ef742009-02-27 12:16:20 +0000333void Top::MarkCompactEpilogue(bool is_compacting, ThreadLocalTop* thread) {
334 if (is_compacting) {
335 StackFrame::UncookFramesForThread(thread);
336 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000337}
338
339
340static int stack_trace_nesting_level = 0;
341static StringStream* incomplete_message = NULL;
342
343
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000344Handle<String> Top::StackTraceString() {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000345 if (stack_trace_nesting_level == 0) {
346 stack_trace_nesting_level++;
347 HeapStringAllocator allocator;
348 StringStream::ClearMentionedObjectCache();
349 StringStream accumulator(&allocator);
350 incomplete_message = &accumulator;
351 PrintStack(&accumulator);
352 Handle<String> stack_trace = accumulator.ToString();
353 incomplete_message = NULL;
354 stack_trace_nesting_level = 0;
355 return stack_trace;
356 } else if (stack_trace_nesting_level == 1) {
357 stack_trace_nesting_level++;
358 OS::PrintError(
359 "\n\nAttempt to print stack while printing stack (double fault)\n");
360 OS::PrintError(
361 "If you are lucky you may find a partial stack dump on stdout.\n\n");
362 incomplete_message->OutputToStdOut();
363 return Factory::empty_symbol();
364 } else {
365 OS::Abort();
366 // Unreachable
367 return Factory::empty_symbol();
368 }
369}
370
371
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +0000372Handle<JSArray> Top::CaptureCurrentStackTrace(
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000373 int frame_limit, StackTrace::StackTraceOptions options) {
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000374 // Ensure no negative values.
375 int limit = Max(frame_limit, 0);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000376 Handle<JSArray> stack_trace = Factory::NewJSArray(frame_limit);
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000377
378 Handle<String> column_key = Factory::LookupAsciiSymbol("column");
379 Handle<String> line_key = Factory::LookupAsciiSymbol("lineNumber");
380 Handle<String> script_key = Factory::LookupAsciiSymbol("scriptName");
381 Handle<String> function_key = Factory::LookupAsciiSymbol("functionName");
382 Handle<String> eval_key = Factory::LookupAsciiSymbol("isEval");
383 Handle<String> constructor_key = Factory::LookupAsciiSymbol("isConstructor");
384
385 StackTraceFrameIterator it;
386 int frames_seen = 0;
387 while (!it.done() && (frames_seen < limit)) {
388 // Create a JSObject to hold the information for the StackFrame.
389 Handle<JSObject> stackFrame = Factory::NewJSObject(object_function());
390
391 JavaScriptFrame* frame = it.frame();
392 JSFunction* fun(JSFunction::cast(frame->function()));
393 Script* script = Script::cast(fun->shared()->script());
394
395 if (options & StackTrace::kLineNumber) {
396 int script_line_offset = script->line_offset()->value();
397 int position = frame->code()->SourcePosition(frame->pc());
398 int line_number = GetScriptLineNumber(Handle<Script>(script), position);
399 // line_number is already shifted by the script_line_offset.
400 int relative_line_number = line_number - script_line_offset;
401 if (options & StackTrace::kColumnOffset && relative_line_number >= 0) {
402 Handle<FixedArray> line_ends(FixedArray::cast(script->line_ends()));
403 int start = (relative_line_number == 0) ? 0 :
404 Smi::cast(line_ends->get(relative_line_number - 1))->value() + 1;
405 int column_offset = position - start;
406 if (relative_line_number == 0) {
407 // For the case where the code is on the same line as the script tag.
408 column_offset += script->column_offset()->value();
409 }
410 SetProperty(stackFrame, column_key,
411 Handle<Smi>(Smi::FromInt(column_offset + 1)), NONE);
412 }
413 SetProperty(stackFrame, line_key,
414 Handle<Smi>(Smi::FromInt(line_number + 1)), NONE);
415 }
416
417 if (options & StackTrace::kScriptName) {
418 Handle<Object> script_name(script->name());
419 SetProperty(stackFrame, script_key, script_name, NONE);
420 }
421
422 if (options & StackTrace::kFunctionName) {
423 Handle<Object> fun_name(fun->shared()->name());
424 if (fun_name->ToBoolean()->IsFalse()) {
425 fun_name = Handle<Object>(fun->shared()->inferred_name());
426 }
427 SetProperty(stackFrame, function_key, fun_name, NONE);
428 }
429
430 if (options & StackTrace::kIsEval) {
431 int type = Smi::cast(script->compilation_type())->value();
432 Handle<Object> is_eval = (type == Script::COMPILATION_TYPE_EVAL) ?
433 Factory::true_value() : Factory::false_value();
434 SetProperty(stackFrame, eval_key, is_eval, NONE);
435 }
436
437 if (options & StackTrace::kIsConstructor) {
438 Handle<Object> is_constructor = (frame->IsConstructor()) ?
439 Factory::true_value() : Factory::false_value();
440 SetProperty(stackFrame, constructor_key, is_constructor, NONE);
441 }
442
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000443 FixedArray::cast(stack_trace->elements())->set(frames_seen, *stackFrame);
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000444 frames_seen++;
445 it.Advance();
446 }
447
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000448 stack_trace->set_length(Smi::FromInt(frames_seen));
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +0000449 return stack_trace;
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000450}
451
452
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000453void Top::PrintStack() {
454 if (stack_trace_nesting_level == 0) {
455 stack_trace_nesting_level++;
456
457 StringAllocator* allocator;
kasper.lundaf4734f2008-07-28 12:50:18 +0000458 if (preallocated_message_space == NULL) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000459 allocator = new HeapStringAllocator();
460 } else {
461 allocator = preallocated_message_space;
462 }
463
464 NativeAllocationChecker allocation_checker(
kasper.lundaf4734f2008-07-28 12:50:18 +0000465 !FLAG_preallocate_message_memory ?
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000466 NativeAllocationChecker::ALLOW :
467 NativeAllocationChecker::DISALLOW);
468
469 StringStream::ClearMentionedObjectCache();
470 StringStream accumulator(allocator);
471 incomplete_message = &accumulator;
472 PrintStack(&accumulator);
473 accumulator.OutputToStdOut();
474 accumulator.Log();
475 incomplete_message = NULL;
476 stack_trace_nesting_level = 0;
kasper.lundaf4734f2008-07-28 12:50:18 +0000477 if (preallocated_message_space == NULL) {
478 // Remove the HeapStringAllocator created above.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000479 delete allocator;
480 }
481 } else if (stack_trace_nesting_level == 1) {
482 stack_trace_nesting_level++;
483 OS::PrintError(
484 "\n\nAttempt to print stack while printing stack (double fault)\n");
485 OS::PrintError(
486 "If you are lucky you may find a partial stack dump on stdout.\n\n");
487 incomplete_message->OutputToStdOut();
488 }
489}
490
491
492static void PrintFrames(StringStream* accumulator,
493 StackFrame::PrintMode mode) {
494 StackFrameIterator it;
495 for (int i = 0; !it.done(); it.Advance()) {
496 it.frame()->Print(accumulator, mode, i++);
497 }
498}
499
500
501void Top::PrintStack(StringStream* accumulator) {
502 // The MentionedObjectCache is not GC-proof at the moment.
503 AssertNoAllocation nogc;
504 ASSERT(StringStream::IsMentionedObjectCacheClear());
505
506 // Avoid printing anything if there are no frames.
507 if (c_entry_fp(GetCurrentThread()) == 0) return;
508
509 accumulator->Add(
510 "\n==== Stack trace ============================================\n\n");
511 PrintFrames(accumulator, StackFrame::OVERVIEW);
512
513 accumulator->Add(
514 "\n==== Details ================================================\n\n");
515 PrintFrames(accumulator, StackFrame::DETAILS);
516
517 accumulator->PrintMentionedObjectCache();
518 accumulator->Add("=====================\n\n");
519}
520
521
522void Top::SetFailedAccessCheckCallback(v8::FailedAccessCheckCallback callback) {
523 ASSERT(thread_local_.failed_access_check_callback_ == NULL);
524 thread_local_.failed_access_check_callback_ = callback;
525}
526
527
528void Top::ReportFailedAccessCheck(JSObject* receiver, v8::AccessType type) {
529 if (!thread_local_.failed_access_check_callback_) return;
530
531 ASSERT(receiver->IsAccessCheckNeeded());
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000532 ASSERT(Top::context());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000533 // The callers of this method are not expecting a GC.
534 AssertNoAllocation no_gc;
535
536 // Get the data object from access check info.
537 JSFunction* constructor = JSFunction::cast(receiver->map()->constructor());
vegorov@chromium.orgf8372902010-03-15 10:26:20 +0000538 if (!constructor->shared()->IsApiFunction()) return;
539 Object* data_obj =
540 constructor->shared()->get_api_func_data()->access_check_info();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000541 if (data_obj == Heap::undefined_value()) return;
542
543 HandleScope scope;
544 Handle<JSObject> receiver_handle(receiver);
545 Handle<Object> data(AccessCheckInfo::cast(data_obj)->data());
546 thread_local_.failed_access_check_callback_(
547 v8::Utils::ToLocal(receiver_handle),
548 type,
549 v8::Utils::ToLocal(data));
550}
551
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000552
553enum MayAccessDecision {
554 YES, NO, UNKNOWN
555};
556
557
558static MayAccessDecision MayAccessPreCheck(JSObject* receiver,
559 v8::AccessType type) {
560 // During bootstrapping, callback functions are not enabled yet.
561 if (Bootstrapper::IsActive()) return YES;
562
563 if (receiver->IsJSGlobalProxy()) {
564 Object* receiver_context = JSGlobalProxy::cast(receiver)->context();
565 if (!receiver_context->IsContext()) return NO;
566
567 // Get the global context of current top context.
568 // avoid using Top::global_context() because it uses Handle.
569 Context* global_context = Top::context()->global()->global_context();
570 if (receiver_context == global_context) return YES;
571
572 if (Context::cast(receiver_context)->security_token() ==
573 global_context->security_token())
574 return YES;
575 }
576
577 return UNKNOWN;
578}
579
580
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000581bool Top::MayNamedAccess(JSObject* receiver, Object* key, v8::AccessType type) {
582 ASSERT(receiver->IsAccessCheckNeeded());
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000583
584 // The callers of this method are not expecting a GC.
585 AssertNoAllocation no_gc;
586
587 // Skip checks for hidden properties access. Note, we do not
588 // require existence of a context in this case.
589 if (key == Heap::hidden_symbol()) return true;
590
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000591 // Check for compatibility between the security tokens in the
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000592 // current lexical context and the accessed object.
593 ASSERT(Top::context());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000594
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000595 MayAccessDecision decision = MayAccessPreCheck(receiver, type);
596 if (decision != UNKNOWN) return decision == YES;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000597
598 // Get named access check callback
599 JSFunction* constructor = JSFunction::cast(receiver->map()->constructor());
vegorov@chromium.orgf8372902010-03-15 10:26:20 +0000600 if (!constructor->shared()->IsApiFunction()) return false;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000601
vegorov@chromium.orgf8372902010-03-15 10:26:20 +0000602 Object* data_obj =
603 constructor->shared()->get_api_func_data()->access_check_info();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000604 if (data_obj == Heap::undefined_value()) return false;
605
606 Object* fun_obj = AccessCheckInfo::cast(data_obj)->named_callback();
607 v8::NamedSecurityCallback callback =
608 v8::ToCData<v8::NamedSecurityCallback>(fun_obj);
609
610 if (!callback) return false;
611
612 HandleScope scope;
613 Handle<JSObject> receiver_handle(receiver);
614 Handle<Object> key_handle(key);
615 Handle<Object> data(AccessCheckInfo::cast(data_obj)->data());
616 LOG(ApiNamedSecurityCheck(key));
617 bool result = false;
618 {
619 // Leaving JavaScript.
ager@chromium.org41826e72009-03-30 13:30:57 +0000620 VMState state(EXTERNAL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000621 result = callback(v8::Utils::ToLocal(receiver_handle),
622 v8::Utils::ToLocal(key_handle),
623 type,
624 v8::Utils::ToLocal(data));
625 }
626 return result;
627}
628
629
630bool Top::MayIndexedAccess(JSObject* receiver,
631 uint32_t index,
632 v8::AccessType type) {
633 ASSERT(receiver->IsAccessCheckNeeded());
634 // Check for compatibility between the security tokens in the
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000635 // current lexical context and the accessed object.
636 ASSERT(Top::context());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000637 // The callers of this method are not expecting a GC.
638 AssertNoAllocation no_gc;
639
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000640 MayAccessDecision decision = MayAccessPreCheck(receiver, type);
641 if (decision != UNKNOWN) return decision == YES;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000642
643 // Get indexed access check callback
644 JSFunction* constructor = JSFunction::cast(receiver->map()->constructor());
vegorov@chromium.orgf8372902010-03-15 10:26:20 +0000645 if (!constructor->shared()->IsApiFunction()) return false;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000646
vegorov@chromium.orgf8372902010-03-15 10:26:20 +0000647 Object* data_obj =
648 constructor->shared()->get_api_func_data()->access_check_info();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000649 if (data_obj == Heap::undefined_value()) return false;
650
651 Object* fun_obj = AccessCheckInfo::cast(data_obj)->indexed_callback();
652 v8::IndexedSecurityCallback callback =
653 v8::ToCData<v8::IndexedSecurityCallback>(fun_obj);
654
655 if (!callback) return false;
656
657 HandleScope scope;
658 Handle<JSObject> receiver_handle(receiver);
659 Handle<Object> data(AccessCheckInfo::cast(data_obj)->data());
660 LOG(ApiIndexedSecurityCheck(index));
661 bool result = false;
662 {
663 // Leaving JavaScript.
ager@chromium.org41826e72009-03-30 13:30:57 +0000664 VMState state(EXTERNAL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000665 result = callback(v8::Utils::ToLocal(receiver_handle),
666 index,
667 type,
668 v8::Utils::ToLocal(data));
669 }
670 return result;
671}
672
673
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000674const char* Top::kStackOverflowMessage =
675 "Uncaught RangeError: Maximum call stack size exceeded";
676
677
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000678Failure* Top::StackOverflow() {
679 HandleScope scope;
680 Handle<String> key = Factory::stack_overflow_symbol();
681 Handle<JSObject> boilerplate =
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000682 Handle<JSObject>::cast(GetProperty(Top::builtins(), key));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000683 Handle<Object> exception = Copy(boilerplate);
684 // TODO(1240995): To avoid having to call JavaScript code to compute
685 // the message for stack overflow exceptions which is very likely to
686 // double fault with another stack overflow exception, we use a
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +0000687 // precomputed message.
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000688 DoThrow(*exception, NULL, kStackOverflowMessage);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000689 return Failure::Exception();
690}
691
692
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +0000693Failure* Top::TerminateExecution() {
694 DoThrow(Heap::termination_exception(), NULL, NULL);
695 return Failure::Exception();
696}
697
698
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000699Failure* Top::Throw(Object* exception, MessageLocation* location) {
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000700 DoThrow(exception, location, NULL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000701 return Failure::Exception();
702}
703
704
705Failure* Top::ReThrow(Object* exception, MessageLocation* location) {
ager@chromium.org32912102009-01-16 10:38:43 +0000706 // Set the exception being re-thrown.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000707 set_pending_exception(exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000708 return Failure::Exception();
709}
710
711
ager@chromium.org3e875802009-06-29 08:26:34 +0000712Failure* Top::ThrowIllegalOperation() {
713 return Throw(Heap::illegal_access_symbol());
714}
715
716
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000717void Top::ScheduleThrow(Object* exception) {
718 // When scheduling a throw we first throw the exception to get the
719 // error reporting if it is uncaught before rescheduling it.
720 Throw(exception);
721 thread_local_.scheduled_exception_ = pending_exception();
722 thread_local_.external_caught_exception_ = false;
723 clear_pending_exception();
724}
725
726
727Object* Top::PromoteScheduledException() {
728 Object* thrown = scheduled_exception();
729 clear_scheduled_exception();
730 // Re-throw the exception to avoid getting repeated error reporting.
731 return ReThrow(thrown);
732}
733
734
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000735void Top::PrintCurrentStackTrace(FILE* out) {
736 StackTraceFrameIterator it;
737 while (!it.done()) {
738 HandleScope scope;
739 // Find code position if recorded in relocation info.
740 JavaScriptFrame* frame = it.frame();
kasperl@chromium.org061ef742009-02-27 12:16:20 +0000741 int pos = frame->code()->SourcePosition(frame->pc());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000742 Handle<Object> pos_obj(Smi::FromInt(pos));
743 // Fetch function and receiver.
744 Handle<JSFunction> fun(JSFunction::cast(frame->function()));
745 Handle<Object> recv(frame->receiver());
746 // Advance to the next JavaScript frame and determine if the
747 // current frame is the top-level frame.
748 it.Advance();
749 Handle<Object> is_top_level = it.done()
750 ? Factory::true_value()
751 : Factory::false_value();
ager@chromium.org32912102009-01-16 10:38:43 +0000752 // Generate and print stack trace line.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000753 Handle<String> line =
754 Execution::GetStackTraceLine(recv, fun, pos_obj, is_top_level);
755 if (line->length() > 0) {
756 line->PrintOn(out);
757 fprintf(out, "\n");
758 }
759 }
760}
761
762
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000763void Top::ComputeLocation(MessageLocation* target) {
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000764 *target = MessageLocation(Handle<Script>(Heap::empty_script()), -1, -1);
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000765 StackTraceFrameIterator it;
766 if (!it.done()) {
767 JavaScriptFrame* frame = it.frame();
768 JSFunction* fun = JSFunction::cast(frame->function());
769 Object* script = fun->shared()->script();
770 if (script->IsScript() &&
771 !(Script::cast(script)->source()->IsUndefined())) {
kasperl@chromium.org061ef742009-02-27 12:16:20 +0000772 int pos = frame->code()->SourcePosition(frame->pc());
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000773 // Compute the location from the function and the reloc info.
774 Handle<Script> casted_script(Script::cast(script));
775 *target = MessageLocation(casted_script, pos, pos + 1);
776 }
777 }
778}
779
780
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +0000781bool Top::ShouldReturnException(bool* is_caught_externally,
782 bool catchable_by_javascript) {
ager@chromium.org8bb60582008-12-11 12:02:20 +0000783 // Find the top-most try-catch handler.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000784 StackHandler* handler =
785 StackHandler::FromAddress(Top::handler(Top::GetCurrentThread()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000786 while (handler != NULL && !handler->is_try_catch()) {
787 handler = handler->next();
788 }
789
ager@chromium.org8bb60582008-12-11 12:02:20 +0000790 // Get the address of the external handler so we can compare the address to
791 // determine which one is closer to the top of the stack.
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000792 Address external_handler_address = thread_local_.try_catch_handler_address();
ager@chromium.org8bb60582008-12-11 12:02:20 +0000793
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000794 // The exception has been externally caught if and only if there is
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000795 // an external handler which is on top of the top-most try-catch
796 // handler.
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000797 *is_caught_externally = external_handler_address != NULL &&
798 (handler == NULL || handler->address() > external_handler_address ||
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +0000799 !catchable_by_javascript);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000800
ager@chromium.org8bb60582008-12-11 12:02:20 +0000801 if (*is_caught_externally) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000802 // Only report the exception if the external handler is verbose.
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000803 return thread_local_.TryCatchHandler()->is_verbose_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000804 } else {
805 // Report the exception if it isn't caught by JavaScript code.
ager@chromium.org8bb60582008-12-11 12:02:20 +0000806 return handler == NULL;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000807 }
808}
809
810
811void Top::DoThrow(Object* exception,
812 MessageLocation* location,
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000813 const char* message) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000814 ASSERT(!has_pending_exception());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000815
816 HandleScope scope;
817 Handle<Object> exception_handle(exception);
818
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000819 // Determine reporting and whether the exception is caught externally.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000820 bool is_caught_externally = false;
ager@chromium.org8bb60582008-12-11 12:02:20 +0000821 bool is_out_of_memory = exception == Failure::OutOfMemoryException();
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +0000822 bool is_termination_exception = exception == Heap::termination_exception();
823 bool catchable_by_javascript = !is_termination_exception && !is_out_of_memory;
824 bool should_return_exception =
825 ShouldReturnException(&is_caught_externally, catchable_by_javascript);
826 bool report_exception = catchable_by_javascript && should_return_exception;
ager@chromium.org8bb60582008-12-11 12:02:20 +0000827
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000828#ifdef ENABLE_DEBUGGER_SUPPORT
ager@chromium.org8bb60582008-12-11 12:02:20 +0000829 // Notify debugger of exception.
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +0000830 if (catchable_by_javascript) {
831 Debugger::OnException(exception_handle, report_exception);
832 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000833#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000834
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000835 // Generate the message.
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000836 Handle<Object> message_obj;
837 MessageLocation potential_computed_location;
838 bool try_catch_needs_message =
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000839 is_caught_externally &&
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000840 thread_local_.TryCatchHandler()->capture_message_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000841 if (report_exception || try_catch_needs_message) {
842 if (location == NULL) {
843 // If no location was specified we use a computed one instead
844 ComputeLocation(&potential_computed_location);
845 location = &potential_computed_location;
846 }
ager@chromium.org4af710e2009-09-15 12:20:11 +0000847 if (!Bootstrapper::IsActive()) {
848 // It's not safe to try to make message objects or collect stack
849 // traces while the bootstrapper is active since the infrastructure
850 // may not have been properly initialized.
851 Handle<String> stack_trace;
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000852 if (FLAG_trace_exception) stack_trace = StackTraceString();
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +0000853 Handle<JSArray> stack_trace_object;
854 if (report_exception && capture_stack_trace_for_uncaught_exceptions) {
855 stack_trace_object = Top::CaptureCurrentStackTrace(
856 stack_trace_for_uncaught_exceptions_frame_limit,
857 stack_trace_for_uncaught_exceptions_options);
858 }
ager@chromium.org4af710e2009-09-15 12:20:11 +0000859 message_obj = MessageHandler::MakeMessageObject("uncaught_exception",
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +0000860 location, HandleVector<Object>(&exception_handle, 1), stack_trace,
861 stack_trace_object);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000862 }
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000863 }
864
ager@chromium.org8bb60582008-12-11 12:02:20 +0000865 // Save the message for reporting if the the exception remains uncaught.
866 thread_local_.has_pending_message_ = report_exception;
867 thread_local_.pending_message_ = message;
868 if (!message_obj.is_null()) {
869 thread_local_.pending_message_obj_ = *message_obj;
870 if (location != NULL) {
871 thread_local_.pending_message_script_ = *location->script();
872 thread_local_.pending_message_start_pos_ = location->start_pos();
873 thread_local_.pending_message_end_pos_ = location->end_pos();
874 }
875 }
876
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000877 if (is_caught_externally) {
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000878 thread_local_.catcher_ = thread_local_.TryCatchHandler();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000879 }
880
ager@chromium.org8bb60582008-12-11 12:02:20 +0000881 // NOTE: Notifying the debugger or generating the message
882 // may have caused new exceptions. For now, we just ignore
883 // that and set the pending exception to the original one.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000884 set_pending_exception(*exception_handle);
885}
886
887
ager@chromium.org8bb60582008-12-11 12:02:20 +0000888void Top::ReportPendingMessages() {
889 ASSERT(has_pending_exception());
890 setup_external_caught();
891 // If the pending exception is OutOfMemoryException set out_of_memory in
892 // the global context. Note: We have to mark the global context here
893 // since the GenerateThrowOutOfMemory stub cannot make a RuntimeCall to
894 // set it.
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +0000895 bool external_caught = thread_local_.external_caught_exception_;
ager@chromium.org8bb60582008-12-11 12:02:20 +0000896 HandleScope scope;
897 if (thread_local_.pending_exception_ == Failure::OutOfMemoryException()) {
898 context()->mark_out_of_memory();
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +0000899 } else if (thread_local_.pending_exception_ ==
900 Heap::termination_exception()) {
901 if (external_caught) {
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000902 thread_local_.TryCatchHandler()->can_continue_ = false;
903 thread_local_.TryCatchHandler()->exception_ = Heap::null_value();
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +0000904 }
ager@chromium.org8bb60582008-12-11 12:02:20 +0000905 } else {
906 Handle<Object> exception(pending_exception());
ager@chromium.org8bb60582008-12-11 12:02:20 +0000907 thread_local_.external_caught_exception_ = false;
908 if (external_caught) {
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000909 thread_local_.TryCatchHandler()->can_continue_ = true;
910 thread_local_.TryCatchHandler()->exception_ =
ager@chromium.org8bb60582008-12-11 12:02:20 +0000911 thread_local_.pending_exception_;
912 if (!thread_local_.pending_message_obj_->IsTheHole()) {
913 try_catch_handler()->message_ = thread_local_.pending_message_obj_;
914 }
915 }
916 if (thread_local_.has_pending_message_) {
917 thread_local_.has_pending_message_ = false;
918 if (thread_local_.pending_message_ != NULL) {
919 MessageHandler::ReportMessage(thread_local_.pending_message_);
920 } else if (!thread_local_.pending_message_obj_->IsTheHole()) {
921 Handle<Object> message_obj(thread_local_.pending_message_obj_);
922 if (thread_local_.pending_message_script_ != NULL) {
923 Handle<Script> script(thread_local_.pending_message_script_);
924 int start_pos = thread_local_.pending_message_start_pos_;
925 int end_pos = thread_local_.pending_message_end_pos_;
926 MessageLocation location(script, start_pos, end_pos);
927 MessageHandler::ReportMessage(&location, message_obj);
928 } else {
929 MessageHandler::ReportMessage(NULL, message_obj);
930 }
931 }
932 }
933 thread_local_.external_caught_exception_ = external_caught;
934 set_pending_exception(*exception);
935 }
936 clear_pending_message();
937}
938
939
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000940void Top::TraceException(bool flag) {
941 FLAG_trace_exception = flag;
942}
943
944
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000945bool Top::OptionalRescheduleException(bool is_bottom_call) {
ager@chromium.org71daaf62009-04-01 07:22:49 +0000946 // Allways reschedule out of memory exceptions.
947 if (!is_out_of_memory()) {
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +0000948 bool is_termination_exception =
949 pending_exception() == Heap::termination_exception();
ager@chromium.org71daaf62009-04-01 07:22:49 +0000950
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000951 // Do not reschedule the exception if this is the bottom call.
952 bool clear_exception = is_bottom_call;
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +0000953
954 if (is_termination_exception) {
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +0000955 if (is_bottom_call) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000956 thread_local_.external_caught_exception_ = false;
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +0000957 clear_pending_exception();
958 return false;
959 }
960 } else if (thread_local_.external_caught_exception_) {
961 // If the exception is externally caught, clear it if there are no
962 // JavaScript frames on the way to the C++ frame that has the
963 // external handler.
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000964 ASSERT(thread_local_.try_catch_handler_address() != NULL);
ager@chromium.org71daaf62009-04-01 07:22:49 +0000965 Address external_handler_address =
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000966 thread_local_.try_catch_handler_address();
ager@chromium.org71daaf62009-04-01 07:22:49 +0000967 JavaScriptFrameIterator it;
968 if (it.done() || (it.frame()->sp() > external_handler_address)) {
969 clear_exception = true;
970 }
971 }
972
973 // Clear the exception if needed.
974 if (clear_exception) {
975 thread_local_.external_caught_exception_ = false;
976 clear_pending_exception();
977 return false;
978 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000979 }
ager@chromium.org71daaf62009-04-01 07:22:49 +0000980
981 // Reschedule the exception.
982 thread_local_.scheduled_exception_ = pending_exception();
983 clear_pending_exception();
984 return true;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000985}
986
987
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +0000988void Top::SetCaptureStackTraceForUncaughtExceptions(
989 bool capture,
990 int frame_limit,
991 StackTrace::StackTraceOptions options) {
992 capture_stack_trace_for_uncaught_exceptions = capture;
993 stack_trace_for_uncaught_exceptions_frame_limit = frame_limit;
994 stack_trace_for_uncaught_exceptions_options = options;
995}
996
997
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000998bool Top::is_out_of_memory() {
999 if (has_pending_exception()) {
1000 Object* e = pending_exception();
1001 if (e->IsFailure() && Failure::cast(e)->IsOutOfMemoryException()) {
1002 return true;
1003 }
1004 }
1005 if (has_scheduled_exception()) {
1006 Object* e = scheduled_exception();
1007 if (e->IsFailure() && Failure::cast(e)->IsOutOfMemoryException()) {
1008 return true;
1009 }
1010 }
1011 return false;
1012}
1013
1014
1015Handle<Context> Top::global_context() {
1016 GlobalObject* global = thread_local_.context_->global();
1017 return Handle<Context>(global->global_context());
1018}
1019
1020
ager@chromium.org1bf0cd02009-05-20 11:34:19 +00001021Handle<Context> Top::GetCallingGlobalContext() {
1022 JavaScriptFrameIterator it;
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001023#ifdef ENABLE_DEBUGGER_SUPPORT
1024 if (Debug::InDebugger()) {
1025 while (!it.done()) {
1026 JavaScriptFrame* frame = it.frame();
1027 Context* context = Context::cast(frame->context());
1028 if (context->global_context() == *Debug::debug_context()) {
1029 it.Advance();
1030 } else {
1031 break;
1032 }
1033 }
1034 }
1035#endif // ENABLE_DEBUGGER_SUPPORT
ager@chromium.org1bf0cd02009-05-20 11:34:19 +00001036 if (it.done()) return Handle<Context>::null();
1037 JavaScriptFrame* frame = it.frame();
1038 Context* context = Context::cast(frame->context());
1039 return Handle<Context>(context->global_context());
1040}
1041
1042
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001043char* Top::ArchiveThread(char* to) {
1044 memcpy(to, reinterpret_cast<char*>(&thread_local_), sizeof(thread_local_));
kasper.lundaf4734f2008-07-28 12:50:18 +00001045 InitializeThreadLocal();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001046 return to + sizeof(thread_local_);
1047}
1048
1049
1050char* Top::RestoreThread(char* from) {
1051 memcpy(reinterpret_cast<char*>(&thread_local_), from, sizeof(thread_local_));
1052 return from + sizeof(thread_local_);
1053}
1054
1055
1056ExecutionAccess::ExecutionAccess() {
1057 Top::break_access_->Lock();
1058}
1059
1060
1061ExecutionAccess::~ExecutionAccess() {
1062 Top::break_access_->Unlock();
1063}
1064
1065
1066} } // namespace v8::internal