ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 1 | // Copyright 2006-2008 the V8 project authors. All rights reserved. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2 | // 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.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 34 | #include "messages.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 35 | #include "platform.h" |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 36 | #include "simulator.h" |
| 37 | #include "string-stream.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 38 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 39 | namespace v8 { |
| 40 | namespace internal { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 41 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 42 | ThreadLocalTop Top::thread_local_; |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 43 | Mutex* Top::break_access_ = OS::CreateMutex(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 44 | |
kasper.lund | af4734f | 2008-07-28 12:50:18 +0000 | [diff] [blame] | 45 | NoAllocationStringAllocator* preallocated_message_space = NULL; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 46 | |
ager@chromium.org | 6a2b0aa | 2010-07-13 20:58:03 +0000 | [diff] [blame] | 47 | bool capture_stack_trace_for_uncaught_exceptions = false; |
| 48 | int stack_trace_for_uncaught_exceptions_frame_limit = 0; |
| 49 | StackTrace::StackTraceOptions stack_trace_for_uncaught_exceptions_options = |
| 50 | StackTrace::kOverview; |
| 51 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 52 | Address top_addresses[] = { |
| 53 | #define C(name) reinterpret_cast<Address>(Top::name()), |
| 54 | TOP_ADDRESS_LIST(C) |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 55 | TOP_ADDRESS_LIST_PROF(C) |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 56 | #undef C |
| 57 | NULL |
| 58 | }; |
| 59 | |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 60 | |
| 61 | v8::TryCatch* ThreadLocalTop::TryCatchHandler() { |
| 62 | return TRY_CATCH_FROM_ADDRESS(try_catch_handler_address()); |
| 63 | } |
| 64 | |
| 65 | |
| 66 | void ThreadLocalTop::Initialize() { |
| 67 | c_entry_fp_ = 0; |
| 68 | handler_ = 0; |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 69 | #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.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 76 | #ifdef ENABLE_LOGGING_AND_PROFILING |
| 77 | js_entry_sp_ = 0; |
| 78 | #endif |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 79 | #ifdef ENABLE_VMSTATE_TRACKING |
| 80 | current_vm_state_ = NULL; |
| 81 | #endif |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 82 | 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.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 93 | Address Top::get_address_from_id(Top::AddressId id) { |
| 94 | return top_addresses[id]; |
| 95 | } |
| 96 | |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 97 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 98 | char* 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.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 105 | void Top::IterateThread(ThreadVisitor* v) { |
| 106 | v->VisitThread(&thread_local_); |
| 107 | } |
| 108 | |
| 109 | |
| 110 | void Top::IterateThread(ThreadVisitor* v, char* t) { |
| 111 | ThreadLocalTop* thread = reinterpret_cast<ThreadLocalTop*>(t); |
| 112 | v->VisitThread(thread); |
| 113 | } |
| 114 | |
| 115 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 116 | void Top::Iterate(ObjectVisitor* v, ThreadLocalTop* thread) { |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 117 | // 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.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 125 | v->VisitPointer(&(thread->pending_message_obj_)); |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 126 | v->VisitPointer(BitCast<Object**>(&(thread->pending_message_script_))); |
| 127 | v->VisitPointer(BitCast<Object**>(&(thread->context_))); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 128 | Object* scheduled; |
| 129 | if (thread->scheduled_exception_->ToObject(&scheduled)) { |
| 130 | v->VisitPointer(&scheduled); |
| 131 | thread->scheduled_exception_ = scheduled; |
| 132 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 133 | |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 134 | for (v8::TryCatch* block = thread->TryCatchHandler(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 135 | block != NULL; |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 136 | block = TRY_CATCH_FROM_ADDRESS(block->next_)) { |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 137 | v->VisitPointer(BitCast<Object**>(&(block->exception_))); |
| 138 | v->VisitPointer(BitCast<Object**>(&(block->message_))); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 139 | } |
| 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.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 146 | |
| 147 | |
| 148 | void Top::Iterate(ObjectVisitor* v) { |
| 149 | ThreadLocalTop* current_t = &thread_local_; |
| 150 | Iterate(v, current_t); |
| 151 | } |
| 152 | |
| 153 | |
| 154 | void Top::InitializeThreadLocal() { |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 155 | thread_local_.Initialize(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 156 | clear_pending_exception(); |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 157 | clear_pending_message(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 158 | clear_scheduled_exception(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | |
kasper.lund | af4734f | 2008-07-28 12:50:18 +0000 | [diff] [blame] | 162 | // 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). |
| 165 | class 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.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 175 | EmbeddedVector<char, 15 * 1024> local_buffer; |
kasper.lund | af4734f | 2008-07-28 12:50:18 +0000 | [diff] [blame] | 176 | |
| 177 | // Initialize the buffer with a known good value. |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 178 | OS::StrNCpy(local_buffer, "Trace data was not generated.\n", |
| 179 | local_buffer.length()); |
kasper.lund | af4734f | 2008-07-28 12:50:18 +0000 | [diff] [blame] | 180 | |
| 181 | // Publish the local buffer and signal its availability. |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 182 | data_ = local_buffer.start(); |
| 183 | length_ = local_buffer.length(); |
kasper.lund | af4734f | 2008-07-28 12:50:18 +0000 | [diff] [blame] | 184 | 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.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 193 | OS::StrNCpy(local_buffer, "PreallocatedMemoryThread shutting down.\n", |
| 194 | local_buffer.length()); |
kasper.lund | af4734f | 2008-07-28 12:50:18 +0000 | [diff] [blame] | 195 | } |
| 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.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 262 | DISALLOW_COPY_AND_ASSIGN(PreallocatedMemoryThread); |
kasper.lund | af4734f | 2008-07-28 12:50:18 +0000 | [diff] [blame] | 263 | }; |
| 264 | |
| 265 | PreallocatedMemoryThread* PreallocatedMemoryThread::the_thread_ = NULL; |
| 266 | Semaphore* PreallocatedMemoryThread::wait_for_ever_semaphore_ = NULL; |
| 267 | Semaphore* PreallocatedMemoryThread::data_ready_semaphore_ = NULL; |
| 268 | char* PreallocatedMemoryThread::data_ = NULL; |
| 269 | unsigned PreallocatedMemoryThread::length_ = 0; |
| 270 | |
| 271 | static bool initialized = false; |
| 272 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 273 | void Top::Initialize() { |
kasper.lund | af4734f | 2008-07-28 12:50:18 +0000 | [diff] [blame] | 274 | CHECK(!initialized); |
| 275 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 276 | InitializeThreadLocal(); |
| 277 | |
kasper.lund | af4734f | 2008-07-28 12:50:18 +0000 | [diff] [blame] | 278 | // 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.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 282 | preallocated_message_space = |
kasper.lund | af4734f | 2008-07-28 12:50:18 +0000 | [diff] [blame] | 283 | new NoAllocationStringAllocator(PreallocatedMemoryThread::data(), |
| 284 | PreallocatedMemoryThread::length()); |
| 285 | PreallocatedStorage::Init(PreallocatedMemoryThread::length() / 4); |
| 286 | } |
| 287 | initialized = true; |
| 288 | } |
| 289 | |
| 290 | |
| 291 | void 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.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 301 | } |
| 302 | } |
| 303 | |
| 304 | |
| 305 | void Top::RegisterTryCatchHandler(v8::TryCatch* that) { |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 306 | // 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.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | |
| 317 | void Top::UnregisterTryCatchHandler(v8::TryCatch* that) { |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 318 | ASSERT(thread_local_.TryCatchHandler() == that); |
| 319 | thread_local_.set_try_catch_handler_address( |
| 320 | reinterpret_cast<Address>(that->next_)); |
ager@chromium.org | 71daaf6 | 2009-04-01 07:22:49 +0000 | [diff] [blame] | 321 | thread_local_.catcher_ = NULL; |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 322 | SimulatorStack::UnregisterCTryCatch(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 323 | } |
| 324 | |
| 325 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 326 | |
| 327 | static int stack_trace_nesting_level = 0; |
| 328 | static StringStream* incomplete_message = NULL; |
| 329 | |
| 330 | |
sgjesse@chromium.org | 720dc0b | 2010-05-10 09:25:39 +0000 | [diff] [blame] | 331 | Handle<String> Top::StackTraceString() { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 332 | 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.org | 6a2b0aa | 2010-07-13 20:58:03 +0000 | [diff] [blame] | 359 | Handle<JSArray> Top::CaptureCurrentStackTrace( |
sgjesse@chromium.org | 720dc0b | 2010-05-10 09:25:39 +0000 | [diff] [blame] | 360 | int frame_limit, StackTrace::StackTraceOptions options) { |
sgjesse@chromium.org | 720dc0b | 2010-05-10 09:25:39 +0000 | [diff] [blame] | 361 | // Ensure no negative values. |
| 362 | int limit = Max(frame_limit, 0); |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 363 | Handle<JSArray> stack_trace = Factory::NewJSArray(frame_limit); |
sgjesse@chromium.org | 720dc0b | 2010-05-10 09:25:39 +0000 | [diff] [blame] | 364 | |
| 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.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 368 | 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.org | 720dc0b | 2010-05-10 09:25:39 +0000 | [diff] [blame] | 372 | 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.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 383 | Handle<JSFunction> fun(JSFunction::cast(frame->function())); |
| 384 | Handle<Script> script(Script::cast(fun->shared()->script())); |
sgjesse@chromium.org | 720dc0b | 2010-05-10 09:25:39 +0000 | [diff] [blame] | 385 | |
| 386 | if (options & StackTrace::kLineNumber) { |
| 387 | int script_line_offset = script->line_offset()->value(); |
| 388 | int position = frame->code()->SourcePosition(frame->pc()); |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 389 | int line_number = GetScriptLineNumber(script, position); |
sgjesse@chromium.org | 720dc0b | 2010-05-10 09:25:39 +0000 | [diff] [blame] | 390 | // 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.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 413 | 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.org | 720dc0b | 2010-05-10 09:25:39 +0000 | [diff] [blame] | 429 | 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.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 450 | FixedArray::cast(stack_trace->elements())->set(frames_seen, *stackFrame); |
sgjesse@chromium.org | 720dc0b | 2010-05-10 09:25:39 +0000 | [diff] [blame] | 451 | frames_seen++; |
| 452 | it.Advance(); |
| 453 | } |
| 454 | |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 455 | stack_trace->set_length(Smi::FromInt(frames_seen)); |
ager@chromium.org | 6a2b0aa | 2010-07-13 20:58:03 +0000 | [diff] [blame] | 456 | return stack_trace; |
sgjesse@chromium.org | 720dc0b | 2010-05-10 09:25:39 +0000 | [diff] [blame] | 457 | } |
| 458 | |
| 459 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 460 | void Top::PrintStack() { |
| 461 | if (stack_trace_nesting_level == 0) { |
| 462 | stack_trace_nesting_level++; |
| 463 | |
| 464 | StringAllocator* allocator; |
kasper.lund | af4734f | 2008-07-28 12:50:18 +0000 | [diff] [blame] | 465 | if (preallocated_message_space == NULL) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 466 | allocator = new HeapStringAllocator(); |
| 467 | } else { |
| 468 | allocator = preallocated_message_space; |
| 469 | } |
| 470 | |
| 471 | NativeAllocationChecker allocation_checker( |
kasper.lund | af4734f | 2008-07-28 12:50:18 +0000 | [diff] [blame] | 472 | !FLAG_preallocate_message_memory ? |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 473 | 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.lund | af4734f | 2008-07-28 12:50:18 +0000 | [diff] [blame] | 484 | if (preallocated_message_space == NULL) { |
| 485 | // Remove the HeapStringAllocator created above. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 486 | 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 | |
| 499 | static 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 | |
| 508 | void 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 | |
| 529 | void Top::SetFailedAccessCheckCallback(v8::FailedAccessCheckCallback callback) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 530 | thread_local_.failed_access_check_callback_ = callback; |
| 531 | } |
| 532 | |
| 533 | |
| 534 | void Top::ReportFailedAccessCheck(JSObject* receiver, v8::AccessType type) { |
| 535 | if (!thread_local_.failed_access_check_callback_) return; |
| 536 | |
| 537 | ASSERT(receiver->IsAccessCheckNeeded()); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 538 | ASSERT(Top::context()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 539 | |
| 540 | // Get the data object from access check info. |
| 541 | JSFunction* constructor = JSFunction::cast(receiver->map()->constructor()); |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 542 | if (!constructor->shared()->IsApiFunction()) return; |
| 543 | Object* data_obj = |
| 544 | constructor->shared()->get_api_func_data()->access_check_info(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 545 | 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.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 556 | |
| 557 | enum MayAccessDecision { |
| 558 | YES, NO, UNKNOWN |
| 559 | }; |
| 560 | |
| 561 | |
| 562 | static 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.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 585 | bool Top::MayNamedAccess(JSObject* receiver, Object* key, v8::AccessType type) { |
| 586 | ASSERT(receiver->IsAccessCheckNeeded()); |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 587 | |
| 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.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 595 | // Check for compatibility between the security tokens in the |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 596 | // current lexical context and the accessed object. |
| 597 | ASSERT(Top::context()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 598 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 599 | MayAccessDecision decision = MayAccessPreCheck(receiver, type); |
| 600 | if (decision != UNKNOWN) return decision == YES; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 601 | |
| 602 | // Get named access check callback |
| 603 | JSFunction* constructor = JSFunction::cast(receiver->map()->constructor()); |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 604 | if (!constructor->shared()->IsApiFunction()) return false; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 605 | |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 606 | Object* data_obj = |
| 607 | constructor->shared()->get_api_func_data()->access_check_info(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 608 | 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.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 624 | VMState state(EXTERNAL); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 625 | 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 | |
| 634 | bool 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.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 639 | // current lexical context and the accessed object. |
| 640 | ASSERT(Top::context()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 641 | // The callers of this method are not expecting a GC. |
| 642 | AssertNoAllocation no_gc; |
| 643 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 644 | MayAccessDecision decision = MayAccessPreCheck(receiver, type); |
| 645 | if (decision != UNKNOWN) return decision == YES; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 646 | |
| 647 | // Get indexed access check callback |
| 648 | JSFunction* constructor = JSFunction::cast(receiver->map()->constructor()); |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 649 | if (!constructor->shared()->IsApiFunction()) return false; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 650 | |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 651 | Object* data_obj = |
| 652 | constructor->shared()->get_api_func_data()->access_check_info(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 653 | 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.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 668 | VMState state(EXTERNAL); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 669 | 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.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 678 | const char* Top::kStackOverflowMessage = |
| 679 | "Uncaught RangeError: Maximum call stack size exceeded"; |
| 680 | |
| 681 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 682 | Failure* Top::StackOverflow() { |
| 683 | HandleScope scope; |
| 684 | Handle<String> key = Factory::stack_overflow_symbol(); |
| 685 | Handle<JSObject> boilerplate = |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 686 | Handle<JSObject>::cast(GetProperty(Top::builtins(), key)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 687 | 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.org | 6a2b0aa | 2010-07-13 20:58:03 +0000 | [diff] [blame] | 691 | // precomputed message. |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 692 | DoThrow(*exception, NULL, kStackOverflowMessage); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 693 | return Failure::Exception(); |
| 694 | } |
| 695 | |
| 696 | |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 697 | Failure* Top::TerminateExecution() { |
| 698 | DoThrow(Heap::termination_exception(), NULL, NULL); |
| 699 | return Failure::Exception(); |
| 700 | } |
| 701 | |
| 702 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 703 | Failure* Top::Throw(Object* exception, MessageLocation* location) { |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 704 | DoThrow(exception, location, NULL); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 705 | return Failure::Exception(); |
| 706 | } |
| 707 | |
| 708 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 709 | Failure* Top::ReThrow(MaybeObject* exception, MessageLocation* location) { |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 710 | // Set the exception being re-thrown. |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 711 | set_pending_exception(exception); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 712 | return Failure::Exception(); |
| 713 | } |
| 714 | |
| 715 | |
ager@chromium.org | 3e87580 | 2009-06-29 08:26:34 +0000 | [diff] [blame] | 716 | Failure* Top::ThrowIllegalOperation() { |
| 717 | return Throw(Heap::illegal_access_symbol()); |
| 718 | } |
| 719 | |
| 720 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 721 | void 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.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 731 | Failure* Top::PromoteScheduledException() { |
| 732 | MaybeObject* thrown = scheduled_exception(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 733 | clear_scheduled_exception(); |
| 734 | // Re-throw the exception to avoid getting repeated error reporting. |
| 735 | return ReThrow(thrown); |
| 736 | } |
| 737 | |
| 738 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 739 | void 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.org | 061ef74 | 2009-02-27 12:16:20 +0000 | [diff] [blame] | 745 | int pos = frame->code()->SourcePosition(frame->pc()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 746 | 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.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 756 | // Generate and print stack trace line. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 757 | 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.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 767 | void Top::ComputeLocation(MessageLocation* target) { |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 768 | *target = MessageLocation(Handle<Script>(Heap::empty_script()), -1, -1); |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 769 | 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.org | 061ef74 | 2009-02-27 12:16:20 +0000 | [diff] [blame] | 776 | int pos = frame->code()->SourcePosition(frame->pc()); |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 777 | // 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.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 785 | bool Top::ShouldReturnException(bool* is_caught_externally, |
| 786 | bool catchable_by_javascript) { |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 787 | // Find the top-most try-catch handler. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 788 | StackHandler* handler = |
| 789 | StackHandler::FromAddress(Top::handler(Top::GetCurrentThread())); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 790 | while (handler != NULL && !handler->is_try_catch()) { |
| 791 | handler = handler->next(); |
| 792 | } |
| 793 | |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 794 | // 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.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 796 | Address external_handler_address = thread_local_.try_catch_handler_address(); |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 797 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 798 | // The exception has been externally caught if and only if there is |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 799 | // an external handler which is on top of the top-most try-catch |
| 800 | // handler. |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 801 | *is_caught_externally = external_handler_address != NULL && |
| 802 | (handler == NULL || handler->address() > external_handler_address || |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 803 | !catchable_by_javascript); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 804 | |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 805 | if (*is_caught_externally) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 806 | // Only report the exception if the external handler is verbose. |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 807 | return thread_local_.TryCatchHandler()->is_verbose_; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 808 | } else { |
| 809 | // Report the exception if it isn't caught by JavaScript code. |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 810 | return handler == NULL; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 811 | } |
| 812 | } |
| 813 | |
| 814 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 815 | void Top::DoThrow(MaybeObject* exception, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 816 | MessageLocation* location, |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 817 | const char* message) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 818 | ASSERT(!has_pending_exception()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 819 | |
| 820 | HandleScope scope; |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 821 | Object* exception_object = Smi::FromInt(0); |
| 822 | bool is_object = exception->ToObject(&exception_object); |
| 823 | Handle<Object> exception_handle(exception_object); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 824 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 825 | // Determine reporting and whether the exception is caught externally. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 826 | bool is_caught_externally = false; |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 827 | bool is_out_of_memory = exception == Failure::OutOfMemoryException(); |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 828 | bool is_termination_exception = exception == Heap::termination_exception(); |
| 829 | bool catchable_by_javascript = !is_termination_exception && !is_out_of_memory; |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 830 | // Only real objects can be caught by JS. |
| 831 | ASSERT(!catchable_by_javascript || is_object); |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 832 | bool should_return_exception = |
| 833 | ShouldReturnException(&is_caught_externally, catchable_by_javascript); |
| 834 | bool report_exception = catchable_by_javascript && should_return_exception; |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 835 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 836 | #ifdef ENABLE_DEBUGGER_SUPPORT |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 837 | // Notify debugger of exception. |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 838 | if (catchable_by_javascript) { |
| 839 | Debugger::OnException(exception_handle, report_exception); |
| 840 | } |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 841 | #endif |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 842 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 843 | // Generate the message. |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 844 | Handle<Object> message_obj; |
| 845 | MessageLocation potential_computed_location; |
| 846 | bool try_catch_needs_message = |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 847 | is_caught_externally && |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 848 | thread_local_.TryCatchHandler()->capture_message_; |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 849 | 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.org | 4af710e | 2009-09-15 12:20:11 +0000 | [diff] [blame] | 855 | 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.org | 720dc0b | 2010-05-10 09:25:39 +0000 | [diff] [blame] | 860 | if (FLAG_trace_exception) stack_trace = StackTraceString(); |
ager@chromium.org | 6a2b0aa | 2010-07-13 20:58:03 +0000 | [diff] [blame] | 861 | 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.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 867 | ASSERT(is_object); // Can't use the handle unless there's a real object. |
ager@chromium.org | 4af710e | 2009-09-15 12:20:11 +0000 | [diff] [blame] | 868 | message_obj = MessageHandler::MakeMessageObject("uncaught_exception", |
ager@chromium.org | 6a2b0aa | 2010-07-13 20:58:03 +0000 | [diff] [blame] | 869 | location, HandleVector<Object>(&exception_handle, 1), stack_trace, |
| 870 | stack_trace_object); |
ager@chromium.org | 4af710e | 2009-09-15 12:20:11 +0000 | [diff] [blame] | 871 | } |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 872 | } |
| 873 | |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 874 | // 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.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 886 | if (is_caught_externally) { |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 887 | thread_local_.catcher_ = thread_local_.TryCatchHandler(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 888 | } |
| 889 | |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 890 | // 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.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 893 | 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.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 900 | } |
| 901 | |
| 902 | |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 903 | void 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.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 910 | bool external_caught = thread_local_.external_caught_exception_; |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 911 | HandleScope scope; |
| 912 | if (thread_local_.pending_exception_ == Failure::OutOfMemoryException()) { |
| 913 | context()->mark_out_of_memory(); |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 914 | } else if (thread_local_.pending_exception_ == |
| 915 | Heap::termination_exception()) { |
| 916 | if (external_caught) { |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 917 | thread_local_.TryCatchHandler()->can_continue_ = false; |
| 918 | thread_local_.TryCatchHandler()->exception_ = Heap::null_value(); |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 919 | } |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 920 | } else { |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 921 | // 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.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 925 | thread_local_.external_caught_exception_ = false; |
| 926 | if (external_caught) { |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 927 | thread_local_.TryCatchHandler()->can_continue_ = true; |
| 928 | thread_local_.TryCatchHandler()->exception_ = |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 929 | 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.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 958 | void Top::TraceException(bool flag) { |
| 959 | FLAG_trace_exception = flag; |
| 960 | } |
| 961 | |
| 962 | |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 963 | bool Top::OptionalRescheduleException(bool is_bottom_call) { |
ager@chromium.org | 71daaf6 | 2009-04-01 07:22:49 +0000 | [diff] [blame] | 964 | // Allways reschedule out of memory exceptions. |
| 965 | if (!is_out_of_memory()) { |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 966 | bool is_termination_exception = |
| 967 | pending_exception() == Heap::termination_exception(); |
ager@chromium.org | 71daaf6 | 2009-04-01 07:22:49 +0000 | [diff] [blame] | 968 | |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 969 | // Do not reschedule the exception if this is the bottom call. |
| 970 | bool clear_exception = is_bottom_call; |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 971 | |
| 972 | if (is_termination_exception) { |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 973 | if (is_bottom_call) { |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 974 | thread_local_.external_caught_exception_ = false; |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 975 | 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.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 982 | ASSERT(thread_local_.try_catch_handler_address() != NULL); |
ager@chromium.org | 71daaf6 | 2009-04-01 07:22:49 +0000 | [diff] [blame] | 983 | Address external_handler_address = |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 984 | thread_local_.try_catch_handler_address(); |
ager@chromium.org | 71daaf6 | 2009-04-01 07:22:49 +0000 | [diff] [blame] | 985 | 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.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 997 | } |
ager@chromium.org | 71daaf6 | 2009-04-01 07:22:49 +0000 | [diff] [blame] | 998 | |
| 999 | // Reschedule the exception. |
| 1000 | thread_local_.scheduled_exception_ = pending_exception(); |
| 1001 | clear_pending_exception(); |
| 1002 | return true; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1003 | } |
| 1004 | |
| 1005 | |
ager@chromium.org | 6a2b0aa | 2010-07-13 20:58:03 +0000 | [diff] [blame] | 1006 | void 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.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1016 | bool Top::is_out_of_memory() { |
| 1017 | if (has_pending_exception()) { |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 1018 | MaybeObject* e = pending_exception(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1019 | if (e->IsFailure() && Failure::cast(e)->IsOutOfMemoryException()) { |
| 1020 | return true; |
| 1021 | } |
| 1022 | } |
| 1023 | if (has_scheduled_exception()) { |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 1024 | MaybeObject* e = scheduled_exception(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1025 | if (e->IsFailure() && Failure::cast(e)->IsOutOfMemoryException()) { |
| 1026 | return true; |
| 1027 | } |
| 1028 | } |
| 1029 | return false; |
| 1030 | } |
| 1031 | |
| 1032 | |
| 1033 | Handle<Context> Top::global_context() { |
| 1034 | GlobalObject* global = thread_local_.context_->global(); |
| 1035 | return Handle<Context>(global->global_context()); |
| 1036 | } |
| 1037 | |
| 1038 | |
ager@chromium.org | 1bf0cd0 | 2009-05-20 11:34:19 +0000 | [diff] [blame] | 1039 | Handle<Context> Top::GetCallingGlobalContext() { |
| 1040 | JavaScriptFrameIterator it; |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 1041 | #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.org | 1bf0cd0 | 2009-05-20 11:34:19 +0000 | [diff] [blame] | 1054 | 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.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1061 | char* Top::ArchiveThread(char* to) { |
| 1062 | memcpy(to, reinterpret_cast<char*>(&thread_local_), sizeof(thread_local_)); |
kasper.lund | af4734f | 2008-07-28 12:50:18 +0000 | [diff] [blame] | 1063 | InitializeThreadLocal(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1064 | return to + sizeof(thread_local_); |
| 1065 | } |
| 1066 | |
| 1067 | |
| 1068 | char* Top::RestoreThread(char* from) { |
| 1069 | memcpy(reinterpret_cast<char*>(&thread_local_), from, sizeof(thread_local_)); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 1070 | // 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.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1079 | return from + sizeof(thread_local_); |
| 1080 | } |
| 1081 | |
| 1082 | |
| 1083 | ExecutionAccess::ExecutionAccess() { |
| 1084 | Top::break_access_->Lock(); |
| 1085 | } |
| 1086 | |
| 1087 | |
| 1088 | ExecutionAccess::~ExecutionAccess() { |
| 1089 | Top::break_access_->Unlock(); |
| 1090 | } |
| 1091 | |
| 1092 | |
| 1093 | } } // namespace v8::internal |