Nick Kledzik | 439ce87 | 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> |
James Y Knight | 0ecfd76 | 2017-01-13 19:22:26 +0000 | [diff] [blame] | 15 | #include <cstdlib> |
Nick Kledzik | 439ce87 | 2012-03-15 01:52:12 +0000 | [diff] [blame] | 16 | #include "abort_message.h" |
Nico Weber | 298baa3 | 2014-06-25 23:55:37 +0000 | [diff] [blame] | 17 | #include "config.h" // For __sync_swap |
Nick Kledzik | 439ce87 | 2012-03-15 01:52:12 +0000 | [diff] [blame] | 18 | #include "cxxabi.h" |
| 19 | #include "cxa_handlers.hpp" |
| 20 | #include "cxa_exception.hpp" |
| 21 | #include "private_typeinfo.h" |
Nick Kledzik | 439ce87 | 2012-03-15 01:52:12 +0000 | [diff] [blame] | 22 | |
Howard Hinnant | cba79c6 | 2013-02-06 19:29:55 +0000 | [diff] [blame] | 23 | static const char* cause = "uncaught"; |
| 24 | |
Nick Kledzik | 439ce87 | 2012-03-15 01:52:12 +0000 | [diff] [blame] | 25 | __attribute__((noreturn)) |
James Y Knight | 0ecfd76 | 2017-01-13 19:22:26 +0000 | [diff] [blame] | 26 | static void demangling_terminate_handler() |
Nick Kledzik | 439ce87 | 2012-03-15 01:52:12 +0000 | [diff] [blame] | 27 | { |
| 28 | // If there might be an uncaught exception |
| 29 | using namespace __cxxabiv1; |
| 30 | __cxa_eh_globals* globals = __cxa_get_globals_fast(); |
| 31 | if (globals) |
| 32 | { |
| 33 | __cxa_exception* exception_header = globals->caughtExceptions; |
| 34 | // If there is an uncaught exception |
| 35 | if (exception_header) |
| 36 | { |
| 37 | _Unwind_Exception* unwind_exception = |
| 38 | reinterpret_cast<_Unwind_Exception*>(exception_header + 1) - 1; |
| 39 | bool native_exception = |
| 40 | (unwind_exception->exception_class & get_vendor_and_language) == |
| 41 | (kOurExceptionClass & get_vendor_and_language); |
| 42 | if (native_exception) |
| 43 | { |
| 44 | void* thrown_object = |
| 45 | unwind_exception->exception_class == kOurDependentExceptionClass ? |
| 46 | ((__cxa_dependent_exception*)exception_header)->primaryException : |
| 47 | exception_header + 1; |
| 48 | const __shim_type_info* thrown_type = |
| 49 | static_cast<const __shim_type_info*>(exception_header->exceptionType); |
| 50 | // Try to get demangled name of thrown_type |
| 51 | int status; |
| 52 | char buf[1024]; |
| 53 | size_t len = sizeof(buf); |
| 54 | const char* name = __cxa_demangle(thrown_type->name(), buf, &len, &status); |
| 55 | if (status != 0) |
| 56 | name = thrown_type->name(); |
| 57 | // If the uncaught exception can be caught with std::exception& |
| 58 | const __shim_type_info* catch_type = |
| 59 | static_cast<const __shim_type_info*>(&typeid(std::exception)); |
| 60 | if (catch_type->can_catch(thrown_type, thrown_object)) |
| 61 | { |
| 62 | // Include the what() message from the exception |
| 63 | const std::exception* e = static_cast<const std::exception*>(thrown_object); |
| 64 | abort_message("terminating with %s exception of type %s: %s", |
| 65 | cause, name, e->what()); |
| 66 | } |
| 67 | else |
| 68 | // Else just note that we're terminating with an exception |
| 69 | abort_message("terminating with %s exception of type %s", |
| 70 | cause, name); |
| 71 | } |
| 72 | else |
| 73 | // Else we're terminating with a foreign exception |
| 74 | abort_message("terminating with %s foreign exception", cause); |
| 75 | } |
| 76 | } |
| 77 | // Else just note that we're terminating |
| 78 | abort_message("terminating"); |
| 79 | } |
| 80 | |
Howard Hinnant | 0f80bb7 | 2012-03-19 16:20:34 +0000 | [diff] [blame] | 81 | __attribute__((noreturn)) |
James Y Knight | 0ecfd76 | 2017-01-13 19:22:26 +0000 | [diff] [blame] | 82 | static void demangling_unexpected_handler() |
Nick Kledzik | 439ce87 | 2012-03-15 01:52:12 +0000 | [diff] [blame] | 83 | { |
Howard Hinnant | cba79c6 | 2013-02-06 19:29:55 +0000 | [diff] [blame] | 84 | cause = "unexpected"; |
| 85 | std::terminate(); |
Nick Kledzik | 439ce87 | 2012-03-15 01:52:12 +0000 | [diff] [blame] | 86 | } |
| 87 | |
James Y Knight | 0ecfd76 | 2017-01-13 19:22:26 +0000 | [diff] [blame] | 88 | #if !LIBCXXABI_SILENT_TERMINATE |
| 89 | static std::terminate_handler default_terminate_handler = demangling_terminate_handler; |
| 90 | static std::terminate_handler default_unexpected_handler = demangling_unexpected_handler; |
| 91 | #else |
| 92 | static std::terminate_handler default_terminate_handler = std::abort; |
| 93 | static std::terminate_handler default_unexpected_handler = std::abort; |
| 94 | #endif |
Nick Kledzik | 439ce87 | 2012-03-15 01:52:12 +0000 | [diff] [blame] | 95 | |
| 96 | // |
| 97 | // Global variables that hold the pointers to the current handler |
| 98 | // |
Saleem Abdulrasool | 436072f | 2016-05-26 02:12:20 +0000 | [diff] [blame] | 99 | _LIBCXXABI_DATA_VIS |
| 100 | std::terminate_handler __cxa_terminate_handler = default_terminate_handler; |
| 101 | |
| 102 | _LIBCXXABI_DATA_VIS |
Nick Kledzik | 439ce87 | 2012-03-15 01:52:12 +0000 | [diff] [blame] | 103 | std::unexpected_handler __cxa_unexpected_handler = default_unexpected_handler; |
Dave Zarzycki | 2507bef | 2012-03-15 08:58:06 +0000 | [diff] [blame] | 104 | |
Howard Hinnant | 4cfb63f | 2012-03-19 16:56:51 +0000 | [diff] [blame] | 105 | // In the future these will become: |
| 106 | // std::atomic<std::terminate_handler> __cxa_terminate_handler(default_terminate_handler); |
| 107 | // std::atomic<std::unexpected_handler> __cxa_unexpected_handler(default_unexpected_handler); |
| 108 | |
Dave Zarzycki | 2507bef | 2012-03-15 08:58:06 +0000 | [diff] [blame] | 109 | namespace std |
| 110 | { |
| 111 | |
| 112 | unexpected_handler |
| 113 | set_unexpected(unexpected_handler func) _NOEXCEPT |
| 114 | { |
Reid Kleckner | 5660f75 | 2014-10-03 20:03:47 +0000 | [diff] [blame] | 115 | if (func == 0) |
| 116 | func = default_unexpected_handler; |
| 117 | return __atomic_exchange_n(&__cxa_unexpected_handler, func, |
| 118 | __ATOMIC_ACQ_REL); |
Howard Hinnant | 4cfb63f | 2012-03-19 16:56:51 +0000 | [diff] [blame] | 119 | // Using of C++11 atomics this should be rewritten |
| 120 | // return __cxa_unexpected_handler.exchange(func, memory_order_acq_rel); |
Dave Zarzycki | 2507bef | 2012-03-15 08:58:06 +0000 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | terminate_handler |
| 124 | set_terminate(terminate_handler func) _NOEXCEPT |
| 125 | { |
Reid Kleckner | 5660f75 | 2014-10-03 20:03:47 +0000 | [diff] [blame] | 126 | if (func == 0) |
| 127 | func = default_terminate_handler; |
| 128 | return __atomic_exchange_n(&__cxa_terminate_handler, func, |
| 129 | __ATOMIC_ACQ_REL); |
Howard Hinnant | 4cfb63f | 2012-03-19 16:56:51 +0000 | [diff] [blame] | 130 | // Using of C++11 atomics this should be rewritten |
| 131 | // return __cxa_terminate_handler.exchange(func, memory_order_acq_rel); |
Dave Zarzycki | 2507bef | 2012-03-15 08:58:06 +0000 | [diff] [blame] | 132 | } |
| 133 | |
Howard Hinnant | 4cfb63f | 2012-03-19 16:56:51 +0000 | [diff] [blame] | 134 | } |