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