Nick Kledzik | 778324a | 2012-03-15 01:52:12 +0000 | [diff] [blame] | 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" |
Nick Kledzik | 778324a | 2012-03-15 01:52:12 +0000 | [diff] [blame] | 20 | |
Howard Hinnant | 2b853bf | 2013-02-06 19:29:55 +0000 | [diff] [blame^] | 21 | static const char* cause = "uncaught"; |
| 22 | |
Nick Kledzik | 778324a | 2012-03-15 01:52:12 +0000 | [diff] [blame] | 23 | __attribute__((noreturn)) |
Howard Hinnant | 2b853bf | 2013-02-06 19:29:55 +0000 | [diff] [blame^] | 24 | static void default_terminate_handler() |
Nick Kledzik | 778324a | 2012-03-15 01:52:12 +0000 | [diff] [blame] | 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 == kOurDependentExceptionClass ? |
| 44 | ((__cxa_dependent_exception*)exception_header)->primaryException : |
| 45 | exception_header + 1; |
| 46 | const __shim_type_info* thrown_type = |
| 47 | static_cast<const __shim_type_info*>(exception_header->exceptionType); |
| 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(std::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 | |
Howard Hinnant | 4ac72dd | 2012-03-19 16:20:34 +0000 | [diff] [blame] | 79 | __attribute__((noreturn)) |
| 80 | static void default_unexpected_handler() |
Nick Kledzik | 778324a | 2012-03-15 01:52:12 +0000 | [diff] [blame] | 81 | { |
Howard Hinnant | 2b853bf | 2013-02-06 19:29:55 +0000 | [diff] [blame^] | 82 | cause = "unexpected"; |
| 83 | std::terminate(); |
Nick Kledzik | 778324a | 2012-03-15 01:52:12 +0000 | [diff] [blame] | 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; |
Dave Zarzycki | 13e35c0 | 2012-03-15 08:58:06 +0000 | [diff] [blame] | 92 | |
Howard Hinnant | e59dbd7 | 2012-03-19 16:56:51 +0000 | [diff] [blame] | 93 | // In the future these will become: |
| 94 | // std::atomic<std::terminate_handler> __cxa_terminate_handler(default_terminate_handler); |
| 95 | // std::atomic<std::unexpected_handler> __cxa_unexpected_handler(default_unexpected_handler); |
| 96 | |
Dave Zarzycki | 13e35c0 | 2012-03-15 08:58:06 +0000 | [diff] [blame] | 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; |
Howard Hinnant | 4ac72dd | 2012-03-19 16:20:34 +0000 | [diff] [blame] | 105 | return __sync_swap(&__cxa_unexpected_handler, func); |
Howard Hinnant | e59dbd7 | 2012-03-19 16:56:51 +0000 | [diff] [blame] | 106 | // Using of C++11 atomics this should be rewritten |
| 107 | // return __cxa_unexpected_handler.exchange(func, memory_order_acq_rel); |
Dave Zarzycki | 13e35c0 | 2012-03-15 08:58:06 +0000 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | terminate_handler |
| 111 | set_terminate(terminate_handler func) _NOEXCEPT |
| 112 | { |
| 113 | if (func == 0) |
| 114 | func = default_terminate_handler; |
Howard Hinnant | 4ac72dd | 2012-03-19 16:20:34 +0000 | [diff] [blame] | 115 | return __sync_swap(&__cxa_terminate_handler, func); |
Howard Hinnant | e59dbd7 | 2012-03-19 16:56:51 +0000 | [diff] [blame] | 116 | // Using of C++11 atomics this should be rewritten |
| 117 | // return __cxa_terminate_handler.exchange(func, memory_order_acq_rel); |
Dave Zarzycki | 13e35c0 | 2012-03-15 08:58:06 +0000 | [diff] [blame] | 118 | } |
| 119 | |
Howard Hinnant | e59dbd7 | 2012-03-19 16:56:51 +0000 | [diff] [blame] | 120 | } |