OLD | NEW |
(Empty) | |
| 1 //===------------------------- cxa_default_handlers.cpp -------------------===// |
| 2 // |
| 3 // The LLVM Compiler Infrastructure |
| 4 // |
| 5 // This file is dual licensed under the MIT and the University of Illinois Open |
| 6 // Source Licenses. See LICENSE.TXT for details. |
| 7 // |
| 8 // |
| 9 // This file implements the default terminate_handler and unexpected_handler. |
| 10 //===----------------------------------------------------------------------===// |
| 11 |
| 12 #include <stdexcept> |
| 13 #include <new> |
| 14 #include <exception> |
| 15 #include "abort_message.h" |
| 16 #include "cxxabi.h" |
| 17 #include "cxa_handlers.hpp" |
| 18 #include "cxa_exception.hpp" |
| 19 #include "private_typeinfo.h" |
| 20 |
| 21 static const char* cause = "uncaught"; |
| 22 |
| 23 __attribute__((noreturn)) |
| 24 static void default_terminate_handler() |
| 25 { |
| 26 // If there might be an uncaught exception |
| 27 using namespace __cxxabiv1; |
| 28 __cxa_eh_globals* globals = __cxa_get_globals_fast(); |
| 29 if (globals) |
| 30 { |
| 31 __cxa_exception* exception_header = globals->caughtExceptions; |
| 32 // If there is an uncaught exception |
| 33 if (exception_header) |
| 34 { |
| 35 _Unwind_Exception* unwind_exception = |
| 36 reinterpret_cast<_Unwind_Exception*>(exception_header + 1) - 1; |
| 37 bool native_exception = |
| 38 (unwind_exception->exception_class & get_vendor_and_language)
== |
| 39 (kOurExceptionClass & get_vendor_and_language); |
| 40 if (native_exception) |
| 41 { |
| 42 void* thrown_object = |
| 43 unwind_exception->exception_class == kOurDependentExceptionC
lass ? |
| 44 ((__cxa_dependent_exception*)exception_header)->primaryE
xception : |
| 45 exception_header + 1; |
| 46 const __shim_type_info* thrown_type = |
| 47 static_cast<const __shim_type_info*>(exception_header->excep
tionType); |
| 48 // Try to get demangled name of thrown_type |
| 49 int status; |
| 50 char buf[1024]; |
| 51 size_t len = sizeof(buf); |
| 52 const char* name = __cxa_demangle(thrown_type->name(), buf, &len
, &status); |
| 53 if (status != 0) |
| 54 name = thrown_type->name(); |
| 55 // If the uncaught exception can be caught with std::exception& |
| 56 const __shim_type_info* catch_type = |
| 57 static_cast<const __shim_type_info*>(&typeid(st
d::exception)); |
| 58 if (catch_type->can_catch(thrown_type, thrown_object)) |
| 59 { |
| 60 // Include the what() message from the exception |
| 61 const std::exception* e = static_cast<const std::exception*>
(thrown_object); |
| 62 abort_message("terminating with %s exception of type %s: %s"
, |
| 63 cause, name, e->what()); |
| 64 } |
| 65 else |
| 66 // Else just note that we're terminating with an exception |
| 67 abort_message("terminating with %s exception of type %s", |
| 68 cause, name); |
| 69 } |
| 70 else |
| 71 // Else we're terminating with a foreign exception |
| 72 abort_message("terminating with %s foreign exception", cause); |
| 73 } |
| 74 } |
| 75 // Else just note that we're terminating |
| 76 abort_message("terminating"); |
| 77 } |
| 78 |
| 79 __attribute__((noreturn)) |
| 80 static void default_unexpected_handler() |
| 81 { |
| 82 cause = "unexpected"; |
| 83 std::terminate(); |
| 84 } |
| 85 |
| 86 |
| 87 // |
| 88 // Global variables that hold the pointers to the current handler |
| 89 // |
| 90 std::terminate_handler __cxa_terminate_handler = default_terminate_handler; |
| 91 std::unexpected_handler __cxa_unexpected_handler = default_unexpected_handler; |
| 92 |
| 93 // In the future these will become: |
| 94 // std::atomic<std::terminate_handler> __cxa_terminate_handler(default_terminat
e_handler); |
| 95 // std::atomic<std::unexpected_handler> __cxa_unexpected_handler(default_unexpec
ted_handler); |
| 96 |
| 97 namespace std |
| 98 { |
| 99 |
| 100 unexpected_handler |
| 101 set_unexpected(unexpected_handler func) _NOEXCEPT |
| 102 { |
| 103 if (func == 0) |
| 104 func = default_unexpected_handler; |
| 105 return __sync_swap(&__cxa_unexpected_handler, func); |
| 106 // Using of C++11 atomics this should be rewritten |
| 107 // return __cxa_unexpected_handler.exchange(func, memory_order_acq_rel); |
| 108 } |
| 109 |
| 110 terminate_handler |
| 111 set_terminate(terminate_handler func) _NOEXCEPT |
| 112 { |
| 113 if (func == 0) |
| 114 func = default_terminate_handler; |
| 115 return __sync_swap(&__cxa_terminate_handler, func); |
| 116 // Using of C++11 atomics this should be rewritten |
| 117 // return __cxa_terminate_handler.exchange(func, memory_order_acq_rel); |
| 118 } |
| 119 |
| 120 } |
OLD | NEW |