blob: 1f0d159fe8bdfd0e33f253b43d2ec464c8b13b9e [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;
lrn@chromium.org303ada72010-10-27 09:33:13 +000069#ifdef USE_SIMULATOR
70#ifdef V8_TARGET_ARCH_ARM
71 simulator_ = assembler::arm::Simulator::current();
72#elif V8_TARGET_ARCH_MIPS
73 simulator_ = assembler::mips::Simulator::current();
74#endif
75#endif
ager@chromium.orgc4c92722009-11-18 14:12:51 +000076#ifdef ENABLE_LOGGING_AND_PROFILING
77 js_entry_sp_ = 0;
78#endif
whesse@chromium.org4a5224e2010-10-20 12:37:07 +000079#ifdef ENABLE_VMSTATE_TRACKING
80 current_vm_state_ = NULL;
81#endif
ager@chromium.orgc4c92722009-11-18 14:12:51 +000082 try_catch_handler_address_ = NULL;
83 context_ = NULL;
84 int id = ThreadManager::CurrentId();
85 thread_id_ = (id == 0) ? ThreadManager::kInvalidId : id;
86 external_caught_exception_ = false;
87 failed_access_check_callback_ = NULL;
88 save_context_ = NULL;
89 catcher_ = NULL;
90}
91
92
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000093Address Top::get_address_from_id(Top::AddressId id) {
94 return top_addresses[id];
95}
96
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +000097
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000098char* Top::Iterate(ObjectVisitor* v, char* thread_storage) {
99 ThreadLocalTop* thread = reinterpret_cast<ThreadLocalTop*>(thread_storage);
100 Iterate(v, thread);
101 return thread_storage + sizeof(ThreadLocalTop);
102}
103
104
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000105void Top::IterateThread(ThreadVisitor* v) {
106 v->VisitThread(&thread_local_);
107}
108
109
110void Top::IterateThread(ThreadVisitor* v, char* t) {
111 ThreadLocalTop* thread = reinterpret_cast<ThreadLocalTop*>(t);
112 v->VisitThread(thread);
113}
114
115
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000116void Top::Iterate(ObjectVisitor* v, ThreadLocalTop* thread) {
lrn@chromium.org303ada72010-10-27 09:33:13 +0000117 // Visit the roots from the top for a given thread.
118 Object *pending;
119 // The pending exception can sometimes be a failure. We can't show
120 // that to the GC, which only understands objects.
121 if (thread->pending_exception_->ToObject(&pending)) {
122 v->VisitPointer(&pending);
123 thread->pending_exception_ = pending; // In case GC updated it.
124 }
ager@chromium.org8bb60582008-12-11 12:02:20 +0000125 v->VisitPointer(&(thread->pending_message_obj_));
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000126 v->VisitPointer(BitCast<Object**>(&(thread->pending_message_script_)));
127 v->VisitPointer(BitCast<Object**>(&(thread->context_)));
lrn@chromium.org303ada72010-10-27 09:33:13 +0000128 Object* scheduled;
129 if (thread->scheduled_exception_->ToObject(&scheduled)) {
130 v->VisitPointer(&scheduled);
131 thread->scheduled_exception_ = scheduled;
132 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000133
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000134 for (v8::TryCatch* block = thread->TryCatchHandler();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000135 block != NULL;
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000136 block = TRY_CATCH_FROM_ADDRESS(block->next_)) {
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000137 v->VisitPointer(BitCast<Object**>(&(block->exception_)));
138 v->VisitPointer(BitCast<Object**>(&(block->message_)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000139 }
140
141 // Iterate over pointers on native execution stack.
142 for (StackFrameIterator it(thread); !it.done(); it.Advance()) {
143 it.frame()->Iterate(v);
144 }
145}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000146
147
148void Top::Iterate(ObjectVisitor* v) {
149 ThreadLocalTop* current_t = &thread_local_;
150 Iterate(v, current_t);
151}
152
153
154void Top::InitializeThreadLocal() {
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000155 thread_local_.Initialize();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000156 clear_pending_exception();
ager@chromium.org8bb60582008-12-11 12:02:20 +0000157 clear_pending_message();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000158 clear_scheduled_exception();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000159}
160
161
kasper.lundaf4734f2008-07-28 12:50:18 +0000162// Create a dummy thread that will wait forever on a semaphore. The only
163// purpose for this thread is to have some stack area to save essential data
164// into for use by a stacks only core dump (aka minidump).
165class PreallocatedMemoryThread: public Thread {
166 public:
167 PreallocatedMemoryThread() : keep_running_(true) {
168 wait_for_ever_semaphore_ = OS::CreateSemaphore(0);
169 data_ready_semaphore_ = OS::CreateSemaphore(0);
170 }
171
172 // When the thread starts running it will allocate a fixed number of bytes
173 // on the stack and publish the location of this memory for others to use.
174 void Run() {
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000175 EmbeddedVector<char, 15 * 1024> local_buffer;
kasper.lundaf4734f2008-07-28 12:50:18 +0000176
177 // Initialize the buffer with a known good value.
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000178 OS::StrNCpy(local_buffer, "Trace data was not generated.\n",
179 local_buffer.length());
kasper.lundaf4734f2008-07-28 12:50:18 +0000180
181 // Publish the local buffer and signal its availability.
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000182 data_ = local_buffer.start();
183 length_ = local_buffer.length();
kasper.lundaf4734f2008-07-28 12:50:18 +0000184 data_ready_semaphore_->Signal();
185
186 while (keep_running_) {
187 // This thread will wait here until the end of time.
188 wait_for_ever_semaphore_->Wait();
189 }
190
191 // Make sure we access the buffer after the wait to remove all possibility
192 // of it being optimized away.
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000193 OS::StrNCpy(local_buffer, "PreallocatedMemoryThread shutting down.\n",
194 local_buffer.length());
kasper.lundaf4734f2008-07-28 12:50:18 +0000195 }
196
197 static char* data() {
198 if (data_ready_semaphore_ != NULL) {
199 // Initial access is guarded until the data has been published.
200 data_ready_semaphore_->Wait();
201 delete data_ready_semaphore_;
202 data_ready_semaphore_ = NULL;
203 }
204 return data_;
205 }
206
207 static unsigned length() {
208 if (data_ready_semaphore_ != NULL) {
209 // Initial access is guarded until the data has been published.
210 data_ready_semaphore_->Wait();
211 delete data_ready_semaphore_;
212 data_ready_semaphore_ = NULL;
213 }
214 return length_;
215 }
216
217 static void StartThread() {
218 if (the_thread_ != NULL) return;
219
220 the_thread_ = new PreallocatedMemoryThread();
221 the_thread_->Start();
222 }
223
224 // Stop the PreallocatedMemoryThread and release its resources.
225 static void StopThread() {
226 if (the_thread_ == NULL) return;
227
228 the_thread_->keep_running_ = false;
229 wait_for_ever_semaphore_->Signal();
230
231 // Wait for the thread to terminate.
232 the_thread_->Join();
233
234 if (data_ready_semaphore_ != NULL) {
235 delete data_ready_semaphore_;
236 data_ready_semaphore_ = NULL;
237 }
238
239 delete wait_for_ever_semaphore_;
240 wait_for_ever_semaphore_ = NULL;
241
242 // Done with the thread entirely.
243 delete the_thread_;
244 the_thread_ = NULL;
245 }
246
247 private:
248 // Used to make sure that the thread keeps looping even for spurious wakeups.
249 bool keep_running_;
250
251 // The preallocated memory thread singleton.
252 static PreallocatedMemoryThread* the_thread_;
253 // This semaphore is used by the PreallocatedMemoryThread to wait for ever.
254 static Semaphore* wait_for_ever_semaphore_;
255 // Semaphore to signal that the data has been initialized.
256 static Semaphore* data_ready_semaphore_;
257
258 // Location and size of the preallocated memory block.
259 static char* data_;
260 static unsigned length_;
261
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000262 DISALLOW_COPY_AND_ASSIGN(PreallocatedMemoryThread);
kasper.lundaf4734f2008-07-28 12:50:18 +0000263};
264
265PreallocatedMemoryThread* PreallocatedMemoryThread::the_thread_ = NULL;
266Semaphore* PreallocatedMemoryThread::wait_for_ever_semaphore_ = NULL;
267Semaphore* PreallocatedMemoryThread::data_ready_semaphore_ = NULL;
268char* PreallocatedMemoryThread::data_ = NULL;
269unsigned PreallocatedMemoryThread::length_ = 0;
270
271static bool initialized = false;
272
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000273void Top::Initialize() {
kasper.lundaf4734f2008-07-28 12:50:18 +0000274 CHECK(!initialized);
275
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000276 InitializeThreadLocal();
277
kasper.lundaf4734f2008-07-28 12:50:18 +0000278 // Only preallocate on the first initialization.
279 if (FLAG_preallocate_message_memory && (preallocated_message_space == NULL)) {
280 // Start the thread which will set aside some memory.
281 PreallocatedMemoryThread::StartThread();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000282 preallocated_message_space =
kasper.lundaf4734f2008-07-28 12:50:18 +0000283 new NoAllocationStringAllocator(PreallocatedMemoryThread::data(),
284 PreallocatedMemoryThread::length());
285 PreallocatedStorage::Init(PreallocatedMemoryThread::length() / 4);
286 }
287 initialized = true;
288}
289
290
291void Top::TearDown() {
292 if (initialized) {
293 // Remove the external reference to the preallocated stack memory.
294 if (preallocated_message_space != NULL) {
295 delete preallocated_message_space;
296 preallocated_message_space = NULL;
297 }
298
299 PreallocatedMemoryThread::StopThread();
300 initialized = false;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000301 }
302}
303
304
305void Top::RegisterTryCatchHandler(v8::TryCatch* that) {
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000306 // The ARM simulator has a separate JS stack. We therefore register
307 // the C++ try catch handler with the simulator and get back an
308 // address that can be used for comparisons with addresses into the
309 // JS stack. When running without the simulator, the address
310 // returned will be the address of the C++ try catch handler itself.
311 Address address = reinterpret_cast<Address>(
312 SimulatorStack::RegisterCTryCatch(reinterpret_cast<uintptr_t>(that)));
313 thread_local_.set_try_catch_handler_address(address);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000314}
315
316
317void Top::UnregisterTryCatchHandler(v8::TryCatch* that) {
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000318 ASSERT(thread_local_.TryCatchHandler() == that);
319 thread_local_.set_try_catch_handler_address(
320 reinterpret_cast<Address>(that->next_));
ager@chromium.org71daaf62009-04-01 07:22:49 +0000321 thread_local_.catcher_ = NULL;
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000322 SimulatorStack::UnregisterCTryCatch();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000323}
324
325
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000326
327static int stack_trace_nesting_level = 0;
328static StringStream* incomplete_message = NULL;
329
330
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000331Handle<String> Top::StackTraceString() {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000332 if (stack_trace_nesting_level == 0) {
333 stack_trace_nesting_level++;
334 HeapStringAllocator allocator;
335 StringStream::ClearMentionedObjectCache();
336 StringStream accumulator(&allocator);
337 incomplete_message = &accumulator;
338 PrintStack(&accumulator);
339 Handle<String> stack_trace = accumulator.ToString();
340 incomplete_message = NULL;
341 stack_trace_nesting_level = 0;
342 return stack_trace;
343 } else if (stack_trace_nesting_level == 1) {
344 stack_trace_nesting_level++;
345 OS::PrintError(
346 "\n\nAttempt to print stack while printing stack (double fault)\n");
347 OS::PrintError(
348 "If you are lucky you may find a partial stack dump on stdout.\n\n");
349 incomplete_message->OutputToStdOut();
350 return Factory::empty_symbol();
351 } else {
352 OS::Abort();
353 // Unreachable
354 return Factory::empty_symbol();
355 }
356}
357
358
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +0000359Handle<JSArray> Top::CaptureCurrentStackTrace(
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000360 int frame_limit, StackTrace::StackTraceOptions options) {
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000361 // Ensure no negative values.
362 int limit = Max(frame_limit, 0);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000363 Handle<JSArray> stack_trace = Factory::NewJSArray(frame_limit);
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000364
365 Handle<String> column_key = Factory::LookupAsciiSymbol("column");
366 Handle<String> line_key = Factory::LookupAsciiSymbol("lineNumber");
367 Handle<String> script_key = Factory::LookupAsciiSymbol("scriptName");
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000368 Handle<String> name_or_source_url_key =
369 Factory::LookupAsciiSymbol("nameOrSourceURL");
370 Handle<String> script_name_or_source_url_key =
371 Factory::LookupAsciiSymbol("scriptNameOrSourceURL");
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000372 Handle<String> function_key = Factory::LookupAsciiSymbol("functionName");
373 Handle<String> eval_key = Factory::LookupAsciiSymbol("isEval");
374 Handle<String> constructor_key = Factory::LookupAsciiSymbol("isConstructor");
375
376 StackTraceFrameIterator it;
377 int frames_seen = 0;
378 while (!it.done() && (frames_seen < limit)) {
379 // Create a JSObject to hold the information for the StackFrame.
380 Handle<JSObject> stackFrame = Factory::NewJSObject(object_function());
381
382 JavaScriptFrame* frame = it.frame();
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000383 Handle<JSFunction> fun(JSFunction::cast(frame->function()));
384 Handle<Script> script(Script::cast(fun->shared()->script()));
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000385
386 if (options & StackTrace::kLineNumber) {
387 int script_line_offset = script->line_offset()->value();
388 int position = frame->code()->SourcePosition(frame->pc());
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000389 int line_number = GetScriptLineNumber(script, position);
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000390 // line_number is already shifted by the script_line_offset.
391 int relative_line_number = line_number - script_line_offset;
392 if (options & StackTrace::kColumnOffset && relative_line_number >= 0) {
393 Handle<FixedArray> line_ends(FixedArray::cast(script->line_ends()));
394 int start = (relative_line_number == 0) ? 0 :
395 Smi::cast(line_ends->get(relative_line_number - 1))->value() + 1;
396 int column_offset = position - start;
397 if (relative_line_number == 0) {
398 // For the case where the code is on the same line as the script tag.
399 column_offset += script->column_offset()->value();
400 }
401 SetProperty(stackFrame, column_key,
402 Handle<Smi>(Smi::FromInt(column_offset + 1)), NONE);
403 }
404 SetProperty(stackFrame, line_key,
405 Handle<Smi>(Smi::FromInt(line_number + 1)), NONE);
406 }
407
408 if (options & StackTrace::kScriptName) {
409 Handle<Object> script_name(script->name());
410 SetProperty(stackFrame, script_key, script_name, NONE);
411 }
412
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000413 if (options & StackTrace::kScriptNameOrSourceURL) {
414 Handle<Object> script_name(script->name());
415 Handle<JSValue> script_wrapper = GetScriptWrapper(script);
416 Handle<Object> property = GetProperty(script_wrapper,
417 name_or_source_url_key);
418 ASSERT(property->IsJSFunction());
419 Handle<JSFunction> method = Handle<JSFunction>::cast(property);
420 bool caught_exception;
421 Handle<Object> result = Execution::TryCall(method, script_wrapper, 0,
422 NULL, &caught_exception);
423 if (caught_exception) {
424 result = Factory::undefined_value();
425 }
426 SetProperty(stackFrame, script_name_or_source_url_key, result, NONE);
427 }
428
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000429 if (options & StackTrace::kFunctionName) {
430 Handle<Object> fun_name(fun->shared()->name());
431 if (fun_name->ToBoolean()->IsFalse()) {
432 fun_name = Handle<Object>(fun->shared()->inferred_name());
433 }
434 SetProperty(stackFrame, function_key, fun_name, NONE);
435 }
436
437 if (options & StackTrace::kIsEval) {
438 int type = Smi::cast(script->compilation_type())->value();
439 Handle<Object> is_eval = (type == Script::COMPILATION_TYPE_EVAL) ?
440 Factory::true_value() : Factory::false_value();
441 SetProperty(stackFrame, eval_key, is_eval, NONE);
442 }
443
444 if (options & StackTrace::kIsConstructor) {
445 Handle<Object> is_constructor = (frame->IsConstructor()) ?
446 Factory::true_value() : Factory::false_value();
447 SetProperty(stackFrame, constructor_key, is_constructor, NONE);
448 }
449
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000450 FixedArray::cast(stack_trace->elements())->set(frames_seen, *stackFrame);
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000451 frames_seen++;
452 it.Advance();
453 }
454
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000455 stack_trace->set_length(Smi::FromInt(frames_seen));
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +0000456 return stack_trace;
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000457}
458
459
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000460void Top::PrintStack() {
461 if (stack_trace_nesting_level == 0) {
462 stack_trace_nesting_level++;
463
464 StringAllocator* allocator;
kasper.lundaf4734f2008-07-28 12:50:18 +0000465 if (preallocated_message_space == NULL) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000466 allocator = new HeapStringAllocator();
467 } else {
468 allocator = preallocated_message_space;
469 }
470
471 NativeAllocationChecker allocation_checker(
kasper.lundaf4734f2008-07-28 12:50:18 +0000472 !FLAG_preallocate_message_memory ?
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000473 NativeAllocationChecker::ALLOW :
474 NativeAllocationChecker::DISALLOW);
475
476 StringStream::ClearMentionedObjectCache();
477 StringStream accumulator(allocator);
478 incomplete_message = &accumulator;
479 PrintStack(&accumulator);
480 accumulator.OutputToStdOut();
481 accumulator.Log();
482 incomplete_message = NULL;
483 stack_trace_nesting_level = 0;
kasper.lundaf4734f2008-07-28 12:50:18 +0000484 if (preallocated_message_space == NULL) {
485 // Remove the HeapStringAllocator created above.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000486 delete allocator;
487 }
488 } else if (stack_trace_nesting_level == 1) {
489 stack_trace_nesting_level++;
490 OS::PrintError(
491 "\n\nAttempt to print stack while printing stack (double fault)\n");
492 OS::PrintError(
493 "If you are lucky you may find a partial stack dump on stdout.\n\n");
494 incomplete_message->OutputToStdOut();
495 }
496}
497
498
499static void PrintFrames(StringStream* accumulator,
500 StackFrame::PrintMode mode) {
501 StackFrameIterator it;
502 for (int i = 0; !it.done(); it.Advance()) {
503 it.frame()->Print(accumulator, mode, i++);
504 }
505}
506
507
508void Top::PrintStack(StringStream* accumulator) {
509 // The MentionedObjectCache is not GC-proof at the moment.
510 AssertNoAllocation nogc;
511 ASSERT(StringStream::IsMentionedObjectCacheClear());
512
513 // Avoid printing anything if there are no frames.
514 if (c_entry_fp(GetCurrentThread()) == 0) return;
515
516 accumulator->Add(
517 "\n==== Stack trace ============================================\n\n");
518 PrintFrames(accumulator, StackFrame::OVERVIEW);
519
520 accumulator->Add(
521 "\n==== Details ================================================\n\n");
522 PrintFrames(accumulator, StackFrame::DETAILS);
523
524 accumulator->PrintMentionedObjectCache();
525 accumulator->Add("=====================\n\n");
526}
527
528
529void Top::SetFailedAccessCheckCallback(v8::FailedAccessCheckCallback callback) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000530 thread_local_.failed_access_check_callback_ = callback;
531}
532
533
534void Top::ReportFailedAccessCheck(JSObject* receiver, v8::AccessType type) {
535 if (!thread_local_.failed_access_check_callback_) return;
536
537 ASSERT(receiver->IsAccessCheckNeeded());
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000538 ASSERT(Top::context());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000539
540 // Get the data object from access check info.
541 JSFunction* constructor = JSFunction::cast(receiver->map()->constructor());
vegorov@chromium.orgf8372902010-03-15 10:26:20 +0000542 if (!constructor->shared()->IsApiFunction()) return;
543 Object* data_obj =
544 constructor->shared()->get_api_func_data()->access_check_info();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000545 if (data_obj == Heap::undefined_value()) return;
546
547 HandleScope scope;
548 Handle<JSObject> receiver_handle(receiver);
549 Handle<Object> data(AccessCheckInfo::cast(data_obj)->data());
550 thread_local_.failed_access_check_callback_(
551 v8::Utils::ToLocal(receiver_handle),
552 type,
553 v8::Utils::ToLocal(data));
554}
555
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000556
557enum MayAccessDecision {
558 YES, NO, UNKNOWN
559};
560
561
562static MayAccessDecision MayAccessPreCheck(JSObject* receiver,
563 v8::AccessType type) {
564 // During bootstrapping, callback functions are not enabled yet.
565 if (Bootstrapper::IsActive()) return YES;
566
567 if (receiver->IsJSGlobalProxy()) {
568 Object* receiver_context = JSGlobalProxy::cast(receiver)->context();
569 if (!receiver_context->IsContext()) return NO;
570
571 // Get the global context of current top context.
572 // avoid using Top::global_context() because it uses Handle.
573 Context* global_context = Top::context()->global()->global_context();
574 if (receiver_context == global_context) return YES;
575
576 if (Context::cast(receiver_context)->security_token() ==
577 global_context->security_token())
578 return YES;
579 }
580
581 return UNKNOWN;
582}
583
584
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000585bool Top::MayNamedAccess(JSObject* receiver, Object* key, v8::AccessType type) {
586 ASSERT(receiver->IsAccessCheckNeeded());
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000587
588 // The callers of this method are not expecting a GC.
589 AssertNoAllocation no_gc;
590
591 // Skip checks for hidden properties access. Note, we do not
592 // require existence of a context in this case.
593 if (key == Heap::hidden_symbol()) return true;
594
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000595 // Check for compatibility between the security tokens in the
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000596 // current lexical context and the accessed object.
597 ASSERT(Top::context());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000598
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000599 MayAccessDecision decision = MayAccessPreCheck(receiver, type);
600 if (decision != UNKNOWN) return decision == YES;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000601
602 // Get named access check callback
603 JSFunction* constructor = JSFunction::cast(receiver->map()->constructor());
vegorov@chromium.orgf8372902010-03-15 10:26:20 +0000604 if (!constructor->shared()->IsApiFunction()) return false;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000605
vegorov@chromium.orgf8372902010-03-15 10:26:20 +0000606 Object* data_obj =
607 constructor->shared()->get_api_func_data()->access_check_info();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000608 if (data_obj == Heap::undefined_value()) return false;
609
610 Object* fun_obj = AccessCheckInfo::cast(data_obj)->named_callback();
611 v8::NamedSecurityCallback callback =
612 v8::ToCData<v8::NamedSecurityCallback>(fun_obj);
613
614 if (!callback) return false;
615
616 HandleScope scope;
617 Handle<JSObject> receiver_handle(receiver);
618 Handle<Object> key_handle(key);
619 Handle<Object> data(AccessCheckInfo::cast(data_obj)->data());
620 LOG(ApiNamedSecurityCheck(key));
621 bool result = false;
622 {
623 // Leaving JavaScript.
ager@chromium.org41826e72009-03-30 13:30:57 +0000624 VMState state(EXTERNAL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000625 result = callback(v8::Utils::ToLocal(receiver_handle),
626 v8::Utils::ToLocal(key_handle),
627 type,
628 v8::Utils::ToLocal(data));
629 }
630 return result;
631}
632
633
634bool Top::MayIndexedAccess(JSObject* receiver,
635 uint32_t index,
636 v8::AccessType type) {
637 ASSERT(receiver->IsAccessCheckNeeded());
638 // Check for compatibility between the security tokens in the
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000639 // current lexical context and the accessed object.
640 ASSERT(Top::context());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000641 // The callers of this method are not expecting a GC.
642 AssertNoAllocation no_gc;
643
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000644 MayAccessDecision decision = MayAccessPreCheck(receiver, type);
645 if (decision != UNKNOWN) return decision == YES;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000646
647 // Get indexed access check callback
648 JSFunction* constructor = JSFunction::cast(receiver->map()->constructor());
vegorov@chromium.orgf8372902010-03-15 10:26:20 +0000649 if (!constructor->shared()->IsApiFunction()) return false;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000650
vegorov@chromium.orgf8372902010-03-15 10:26:20 +0000651 Object* data_obj =
652 constructor->shared()->get_api_func_data()->access_check_info();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000653 if (data_obj == Heap::undefined_value()) return false;
654
655 Object* fun_obj = AccessCheckInfo::cast(data_obj)->indexed_callback();
656 v8::IndexedSecurityCallback callback =
657 v8::ToCData<v8::IndexedSecurityCallback>(fun_obj);
658
659 if (!callback) return false;
660
661 HandleScope scope;
662 Handle<JSObject> receiver_handle(receiver);
663 Handle<Object> data(AccessCheckInfo::cast(data_obj)->data());
664 LOG(ApiIndexedSecurityCheck(index));
665 bool result = false;
666 {
667 // Leaving JavaScript.
ager@chromium.org41826e72009-03-30 13:30:57 +0000668 VMState state(EXTERNAL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000669 result = callback(v8::Utils::ToLocal(receiver_handle),
670 index,
671 type,
672 v8::Utils::ToLocal(data));
673 }
674 return result;
675}
676
677
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000678const char* Top::kStackOverflowMessage =
679 "Uncaught RangeError: Maximum call stack size exceeded";
680
681
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000682Failure* Top::StackOverflow() {
683 HandleScope scope;
684 Handle<String> key = Factory::stack_overflow_symbol();
685 Handle<JSObject> boilerplate =
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000686 Handle<JSObject>::cast(GetProperty(Top::builtins(), key));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000687 Handle<Object> exception = Copy(boilerplate);
688 // TODO(1240995): To avoid having to call JavaScript code to compute
689 // the message for stack overflow exceptions which is very likely to
690 // double fault with another stack overflow exception, we use a
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +0000691 // precomputed message.
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000692 DoThrow(*exception, NULL, kStackOverflowMessage);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000693 return Failure::Exception();
694}
695
696
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +0000697Failure* Top::TerminateExecution() {
698 DoThrow(Heap::termination_exception(), NULL, NULL);
699 return Failure::Exception();
700}
701
702
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000703Failure* Top::Throw(Object* exception, MessageLocation* location) {
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000704 DoThrow(exception, location, NULL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000705 return Failure::Exception();
706}
707
708
lrn@chromium.org303ada72010-10-27 09:33:13 +0000709Failure* Top::ReThrow(MaybeObject* exception, MessageLocation* location) {
ager@chromium.org32912102009-01-16 10:38:43 +0000710 // Set the exception being re-thrown.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000711 set_pending_exception(exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000712 return Failure::Exception();
713}
714
715
ager@chromium.org3e875802009-06-29 08:26:34 +0000716Failure* Top::ThrowIllegalOperation() {
717 return Throw(Heap::illegal_access_symbol());
718}
719
720
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000721void Top::ScheduleThrow(Object* exception) {
722 // When scheduling a throw we first throw the exception to get the
723 // error reporting if it is uncaught before rescheduling it.
724 Throw(exception);
725 thread_local_.scheduled_exception_ = pending_exception();
726 thread_local_.external_caught_exception_ = false;
727 clear_pending_exception();
728}
729
730
lrn@chromium.org303ada72010-10-27 09:33:13 +0000731Failure* Top::PromoteScheduledException() {
732 MaybeObject* thrown = scheduled_exception();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000733 clear_scheduled_exception();
734 // Re-throw the exception to avoid getting repeated error reporting.
735 return ReThrow(thrown);
736}
737
738
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000739void Top::PrintCurrentStackTrace(FILE* out) {
740 StackTraceFrameIterator it;
741 while (!it.done()) {
742 HandleScope scope;
743 // Find code position if recorded in relocation info.
744 JavaScriptFrame* frame = it.frame();
kasperl@chromium.org061ef742009-02-27 12:16:20 +0000745 int pos = frame->code()->SourcePosition(frame->pc());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000746 Handle<Object> pos_obj(Smi::FromInt(pos));
747 // Fetch function and receiver.
748 Handle<JSFunction> fun(JSFunction::cast(frame->function()));
749 Handle<Object> recv(frame->receiver());
750 // Advance to the next JavaScript frame and determine if the
751 // current frame is the top-level frame.
752 it.Advance();
753 Handle<Object> is_top_level = it.done()
754 ? Factory::true_value()
755 : Factory::false_value();
ager@chromium.org32912102009-01-16 10:38:43 +0000756 // Generate and print stack trace line.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000757 Handle<String> line =
758 Execution::GetStackTraceLine(recv, fun, pos_obj, is_top_level);
759 if (line->length() > 0) {
760 line->PrintOn(out);
761 fprintf(out, "\n");
762 }
763 }
764}
765
766
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000767void Top::ComputeLocation(MessageLocation* target) {
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000768 *target = MessageLocation(Handle<Script>(Heap::empty_script()), -1, -1);
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000769 StackTraceFrameIterator it;
770 if (!it.done()) {
771 JavaScriptFrame* frame = it.frame();
772 JSFunction* fun = JSFunction::cast(frame->function());
773 Object* script = fun->shared()->script();
774 if (script->IsScript() &&
775 !(Script::cast(script)->source()->IsUndefined())) {
kasperl@chromium.org061ef742009-02-27 12:16:20 +0000776 int pos = frame->code()->SourcePosition(frame->pc());
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000777 // Compute the location from the function and the reloc info.
778 Handle<Script> casted_script(Script::cast(script));
779 *target = MessageLocation(casted_script, pos, pos + 1);
780 }
781 }
782}
783
784
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +0000785bool Top::ShouldReturnException(bool* is_caught_externally,
786 bool catchable_by_javascript) {
ager@chromium.org8bb60582008-12-11 12:02:20 +0000787 // Find the top-most try-catch handler.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000788 StackHandler* handler =
789 StackHandler::FromAddress(Top::handler(Top::GetCurrentThread()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000790 while (handler != NULL && !handler->is_try_catch()) {
791 handler = handler->next();
792 }
793
ager@chromium.org8bb60582008-12-11 12:02:20 +0000794 // Get the address of the external handler so we can compare the address to
795 // determine which one is closer to the top of the stack.
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000796 Address external_handler_address = thread_local_.try_catch_handler_address();
ager@chromium.org8bb60582008-12-11 12:02:20 +0000797
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000798 // The exception has been externally caught if and only if there is
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000799 // an external handler which is on top of the top-most try-catch
800 // handler.
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000801 *is_caught_externally = external_handler_address != NULL &&
802 (handler == NULL || handler->address() > external_handler_address ||
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +0000803 !catchable_by_javascript);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000804
ager@chromium.org8bb60582008-12-11 12:02:20 +0000805 if (*is_caught_externally) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000806 // Only report the exception if the external handler is verbose.
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000807 return thread_local_.TryCatchHandler()->is_verbose_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000808 } else {
809 // Report the exception if it isn't caught by JavaScript code.
ager@chromium.org8bb60582008-12-11 12:02:20 +0000810 return handler == NULL;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000811 }
812}
813
814
lrn@chromium.org303ada72010-10-27 09:33:13 +0000815void Top::DoThrow(MaybeObject* exception,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000816 MessageLocation* location,
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000817 const char* message) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000818 ASSERT(!has_pending_exception());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000819
820 HandleScope scope;
lrn@chromium.org303ada72010-10-27 09:33:13 +0000821 Object* exception_object = Smi::FromInt(0);
822 bool is_object = exception->ToObject(&exception_object);
823 Handle<Object> exception_handle(exception_object);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000824
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000825 // Determine reporting and whether the exception is caught externally.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000826 bool is_caught_externally = false;
ager@chromium.org8bb60582008-12-11 12:02:20 +0000827 bool is_out_of_memory = exception == Failure::OutOfMemoryException();
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +0000828 bool is_termination_exception = exception == Heap::termination_exception();
829 bool catchable_by_javascript = !is_termination_exception && !is_out_of_memory;
lrn@chromium.org303ada72010-10-27 09:33:13 +0000830 // Only real objects can be caught by JS.
831 ASSERT(!catchable_by_javascript || is_object);
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +0000832 bool should_return_exception =
833 ShouldReturnException(&is_caught_externally, catchable_by_javascript);
834 bool report_exception = catchable_by_javascript && should_return_exception;
ager@chromium.org8bb60582008-12-11 12:02:20 +0000835
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000836#ifdef ENABLE_DEBUGGER_SUPPORT
ager@chromium.org8bb60582008-12-11 12:02:20 +0000837 // Notify debugger of exception.
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +0000838 if (catchable_by_javascript) {
839 Debugger::OnException(exception_handle, report_exception);
840 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000841#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000842
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000843 // Generate the message.
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000844 Handle<Object> message_obj;
845 MessageLocation potential_computed_location;
846 bool try_catch_needs_message =
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000847 is_caught_externally &&
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000848 thread_local_.TryCatchHandler()->capture_message_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000849 if (report_exception || try_catch_needs_message) {
850 if (location == NULL) {
851 // If no location was specified we use a computed one instead
852 ComputeLocation(&potential_computed_location);
853 location = &potential_computed_location;
854 }
ager@chromium.org4af710e2009-09-15 12:20:11 +0000855 if (!Bootstrapper::IsActive()) {
856 // It's not safe to try to make message objects or collect stack
857 // traces while the bootstrapper is active since the infrastructure
858 // may not have been properly initialized.
859 Handle<String> stack_trace;
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000860 if (FLAG_trace_exception) stack_trace = StackTraceString();
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +0000861 Handle<JSArray> stack_trace_object;
862 if (report_exception && capture_stack_trace_for_uncaught_exceptions) {
863 stack_trace_object = Top::CaptureCurrentStackTrace(
864 stack_trace_for_uncaught_exceptions_frame_limit,
865 stack_trace_for_uncaught_exceptions_options);
866 }
lrn@chromium.org303ada72010-10-27 09:33:13 +0000867 ASSERT(is_object); // Can't use the handle unless there's a real object.
ager@chromium.org4af710e2009-09-15 12:20:11 +0000868 message_obj = MessageHandler::MakeMessageObject("uncaught_exception",
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +0000869 location, HandleVector<Object>(&exception_handle, 1), stack_trace,
870 stack_trace_object);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000871 }
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000872 }
873
ager@chromium.org8bb60582008-12-11 12:02:20 +0000874 // Save the message for reporting if the the exception remains uncaught.
875 thread_local_.has_pending_message_ = report_exception;
876 thread_local_.pending_message_ = message;
877 if (!message_obj.is_null()) {
878 thread_local_.pending_message_obj_ = *message_obj;
879 if (location != NULL) {
880 thread_local_.pending_message_script_ = *location->script();
881 thread_local_.pending_message_start_pos_ = location->start_pos();
882 thread_local_.pending_message_end_pos_ = location->end_pos();
883 }
884 }
885
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000886 if (is_caught_externally) {
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000887 thread_local_.catcher_ = thread_local_.TryCatchHandler();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000888 }
889
ager@chromium.org8bb60582008-12-11 12:02:20 +0000890 // NOTE: Notifying the debugger or generating the message
891 // may have caused new exceptions. For now, we just ignore
892 // that and set the pending exception to the original one.
lrn@chromium.org303ada72010-10-27 09:33:13 +0000893 if (is_object) {
894 set_pending_exception(*exception_handle);
895 } else {
896 // Failures are not on the heap so they neither need nor work with handles.
897 ASSERT(exception_handle->IsFailure());
898 set_pending_exception(exception);
899 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000900}
901
902
ager@chromium.org8bb60582008-12-11 12:02:20 +0000903void Top::ReportPendingMessages() {
904 ASSERT(has_pending_exception());
905 setup_external_caught();
906 // If the pending exception is OutOfMemoryException set out_of_memory in
907 // the global context. Note: We have to mark the global context here
908 // since the GenerateThrowOutOfMemory stub cannot make a RuntimeCall to
909 // set it.
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +0000910 bool external_caught = thread_local_.external_caught_exception_;
ager@chromium.org8bb60582008-12-11 12:02:20 +0000911 HandleScope scope;
912 if (thread_local_.pending_exception_ == Failure::OutOfMemoryException()) {
913 context()->mark_out_of_memory();
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +0000914 } else if (thread_local_.pending_exception_ ==
915 Heap::termination_exception()) {
916 if (external_caught) {
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000917 thread_local_.TryCatchHandler()->can_continue_ = false;
918 thread_local_.TryCatchHandler()->exception_ = Heap::null_value();
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +0000919 }
ager@chromium.org8bb60582008-12-11 12:02:20 +0000920 } else {
lrn@chromium.org303ada72010-10-27 09:33:13 +0000921 // At this point all non-object (failure) exceptions have
922 // been dealt with so this shouldn't fail.
923 Object* pending_exception_object = pending_exception()->ToObjectUnchecked();
924 Handle<Object> exception(pending_exception_object);
ager@chromium.org8bb60582008-12-11 12:02:20 +0000925 thread_local_.external_caught_exception_ = false;
926 if (external_caught) {
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000927 thread_local_.TryCatchHandler()->can_continue_ = true;
928 thread_local_.TryCatchHandler()->exception_ =
ager@chromium.org8bb60582008-12-11 12:02:20 +0000929 thread_local_.pending_exception_;
930 if (!thread_local_.pending_message_obj_->IsTheHole()) {
931 try_catch_handler()->message_ = thread_local_.pending_message_obj_;
932 }
933 }
934 if (thread_local_.has_pending_message_) {
935 thread_local_.has_pending_message_ = false;
936 if (thread_local_.pending_message_ != NULL) {
937 MessageHandler::ReportMessage(thread_local_.pending_message_);
938 } else if (!thread_local_.pending_message_obj_->IsTheHole()) {
939 Handle<Object> message_obj(thread_local_.pending_message_obj_);
940 if (thread_local_.pending_message_script_ != NULL) {
941 Handle<Script> script(thread_local_.pending_message_script_);
942 int start_pos = thread_local_.pending_message_start_pos_;
943 int end_pos = thread_local_.pending_message_end_pos_;
944 MessageLocation location(script, start_pos, end_pos);
945 MessageHandler::ReportMessage(&location, message_obj);
946 } else {
947 MessageHandler::ReportMessage(NULL, message_obj);
948 }
949 }
950 }
951 thread_local_.external_caught_exception_ = external_caught;
952 set_pending_exception(*exception);
953 }
954 clear_pending_message();
955}
956
957
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000958void Top::TraceException(bool flag) {
959 FLAG_trace_exception = flag;
960}
961
962
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000963bool Top::OptionalRescheduleException(bool is_bottom_call) {
ager@chromium.org71daaf62009-04-01 07:22:49 +0000964 // Allways reschedule out of memory exceptions.
965 if (!is_out_of_memory()) {
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +0000966 bool is_termination_exception =
967 pending_exception() == Heap::termination_exception();
ager@chromium.org71daaf62009-04-01 07:22:49 +0000968
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000969 // Do not reschedule the exception if this is the bottom call.
970 bool clear_exception = is_bottom_call;
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +0000971
972 if (is_termination_exception) {
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +0000973 if (is_bottom_call) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000974 thread_local_.external_caught_exception_ = false;
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +0000975 clear_pending_exception();
976 return false;
977 }
978 } else if (thread_local_.external_caught_exception_) {
979 // If the exception is externally caught, clear it if there are no
980 // JavaScript frames on the way to the C++ frame that has the
981 // external handler.
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000982 ASSERT(thread_local_.try_catch_handler_address() != NULL);
ager@chromium.org71daaf62009-04-01 07:22:49 +0000983 Address external_handler_address =
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000984 thread_local_.try_catch_handler_address();
ager@chromium.org71daaf62009-04-01 07:22:49 +0000985 JavaScriptFrameIterator it;
986 if (it.done() || (it.frame()->sp() > external_handler_address)) {
987 clear_exception = true;
988 }
989 }
990
991 // Clear the exception if needed.
992 if (clear_exception) {
993 thread_local_.external_caught_exception_ = false;
994 clear_pending_exception();
995 return false;
996 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000997 }
ager@chromium.org71daaf62009-04-01 07:22:49 +0000998
999 // Reschedule the exception.
1000 thread_local_.scheduled_exception_ = pending_exception();
1001 clear_pending_exception();
1002 return true;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001003}
1004
1005
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00001006void Top::SetCaptureStackTraceForUncaughtExceptions(
1007 bool capture,
1008 int frame_limit,
1009 StackTrace::StackTraceOptions options) {
1010 capture_stack_trace_for_uncaught_exceptions = capture;
1011 stack_trace_for_uncaught_exceptions_frame_limit = frame_limit;
1012 stack_trace_for_uncaught_exceptions_options = options;
1013}
1014
1015
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001016bool Top::is_out_of_memory() {
1017 if (has_pending_exception()) {
lrn@chromium.org303ada72010-10-27 09:33:13 +00001018 MaybeObject* e = pending_exception();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001019 if (e->IsFailure() && Failure::cast(e)->IsOutOfMemoryException()) {
1020 return true;
1021 }
1022 }
1023 if (has_scheduled_exception()) {
lrn@chromium.org303ada72010-10-27 09:33:13 +00001024 MaybeObject* e = scheduled_exception();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001025 if (e->IsFailure() && Failure::cast(e)->IsOutOfMemoryException()) {
1026 return true;
1027 }
1028 }
1029 return false;
1030}
1031
1032
1033Handle<Context> Top::global_context() {
1034 GlobalObject* global = thread_local_.context_->global();
1035 return Handle<Context>(global->global_context());
1036}
1037
1038
ager@chromium.org1bf0cd02009-05-20 11:34:19 +00001039Handle<Context> Top::GetCallingGlobalContext() {
1040 JavaScriptFrameIterator it;
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001041#ifdef ENABLE_DEBUGGER_SUPPORT
1042 if (Debug::InDebugger()) {
1043 while (!it.done()) {
1044 JavaScriptFrame* frame = it.frame();
1045 Context* context = Context::cast(frame->context());
1046 if (context->global_context() == *Debug::debug_context()) {
1047 it.Advance();
1048 } else {
1049 break;
1050 }
1051 }
1052 }
1053#endif // ENABLE_DEBUGGER_SUPPORT
ager@chromium.org1bf0cd02009-05-20 11:34:19 +00001054 if (it.done()) return Handle<Context>::null();
1055 JavaScriptFrame* frame = it.frame();
1056 Context* context = Context::cast(frame->context());
1057 return Handle<Context>(context->global_context());
1058}
1059
1060
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001061char* Top::ArchiveThread(char* to) {
1062 memcpy(to, reinterpret_cast<char*>(&thread_local_), sizeof(thread_local_));
kasper.lundaf4734f2008-07-28 12:50:18 +00001063 InitializeThreadLocal();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001064 return to + sizeof(thread_local_);
1065}
1066
1067
1068char* Top::RestoreThread(char* from) {
1069 memcpy(reinterpret_cast<char*>(&thread_local_), from, sizeof(thread_local_));
lrn@chromium.org303ada72010-10-27 09:33:13 +00001070 // This might be just paranoia, but it seems to be needed in case a
1071 // thread_local_ is restored on a separate OS thread.
1072#ifdef USE_SIMULATOR
1073#ifdef V8_TARGET_ARCH_ARM
1074 thread_local_.simulator_ = assembler::arm::Simulator::current();
1075#elif V8_TARGET_ARCH_MIPS
1076 thread_local_.simulator_ = assembler::mips::Simulator::current();
1077#endif
1078#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001079 return from + sizeof(thread_local_);
1080}
1081
1082
1083ExecutionAccess::ExecutionAccess() {
1084 Top::break_access_->Lock();
1085}
1086
1087
1088ExecutionAccess::~ExecutionAccess() {
1089 Top::break_access_->Unlock();
1090}
1091
1092
1093} } // namespace v8::internal