Howard Hinnant | 44d4d3b | 2011-12-06 17:51:25 +0000 | [diff] [blame] | 1 | //===------------------------- cxa_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 functionality associated with the terminate_handler, |
| 10 | // unexpected_handler, and new_handler. |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
| 13 | #include <stdexcept> |
| 14 | #include <new> |
| 15 | #include <exception> |
| 16 | #include "abort_message.h" |
Howard Hinnant | fe90746 | 2012-01-24 18:26:29 +0000 | [diff] [blame^] | 17 | #include "cxxabi.h" |
Howard Hinnant | b80931e | 2011-12-07 21:16:40 +0000 | [diff] [blame] | 18 | #include "cxa_handlers.hpp" |
Howard Hinnant | fe90746 | 2012-01-24 18:26:29 +0000 | [diff] [blame^] | 19 | #include "cxa_exception.hpp" |
| 20 | #include "private_typeinfo.h" |
Howard Hinnant | 44d4d3b | 2011-12-06 17:51:25 +0000 | [diff] [blame] | 21 | |
| 22 | namespace std |
| 23 | { |
| 24 | |
| 25 | static const char* cause = "uncaught"; |
| 26 | |
| 27 | static void default_terminate_handler() |
| 28 | { |
Howard Hinnant | fe90746 | 2012-01-24 18:26:29 +0000 | [diff] [blame^] | 29 | // If there might be an uncaught exception |
| 30 | using namespace __cxxabiv1; |
| 31 | __cxa_eh_globals* globals = __cxa_get_globals_fast(); |
| 32 | if (globals) |
Howard Hinnant | 44d4d3b | 2011-12-06 17:51:25 +0000 | [diff] [blame] | 33 | { |
Howard Hinnant | fe90746 | 2012-01-24 18:26:29 +0000 | [diff] [blame^] | 34 | __cxa_exception* exception_header = globals->caughtExceptions; |
| 35 | // If there is an uncaught exception |
| 36 | if (exception_header) |
Howard Hinnant | 44d4d3b | 2011-12-06 17:51:25 +0000 | [diff] [blame] | 37 | { |
Howard Hinnant | fe90746 | 2012-01-24 18:26:29 +0000 | [diff] [blame^] | 38 | _Unwind_Exception* unwind_exception = |
| 39 | reinterpret_cast<_Unwind_Exception*>(exception_header + 1) - 1; |
| 40 | void* thrown_object = |
| 41 | unwind_exception->exception_class == kOurDependentExceptionClass ? |
| 42 | ((__cxa_dependent_exception*)exception_header)->primaryException : |
| 43 | exception_header + 1; |
| 44 | const __shim_type_info* thrown_type = |
| 45 | static_cast<const __shim_type_info*>(exception_header->exceptionType); |
| 46 | const __shim_type_info* catch_type = |
| 47 | static_cast<const __shim_type_info*>(&typeid(exception)); |
| 48 | // If the uncaught exception can be caught with std::exception& |
| 49 | if (catch_type->can_catch(thrown_type, thrown_object)) |
| 50 | { |
| 51 | // Include the what() message from the exception |
| 52 | const exception* e = static_cast<const exception*>(thrown_object); |
| 53 | abort_message("terminating with %s exception: %s", cause, e->what()); |
| 54 | } |
| 55 | else |
| 56 | // Else just note that we're terminating with an exception |
| 57 | abort_message("terminating with %s exception", cause); |
Howard Hinnant | 44d4d3b | 2011-12-06 17:51:25 +0000 | [diff] [blame] | 58 | } |
| 59 | } |
Howard Hinnant | fe90746 | 2012-01-24 18:26:29 +0000 | [diff] [blame^] | 60 | // Else just note that we're terminating |
| 61 | abort_message("terminating"); |
Howard Hinnant | 44d4d3b | 2011-12-06 17:51:25 +0000 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | static void default_unexpected_handler() |
| 65 | { |
| 66 | cause = "unexpected"; |
| 67 | terminate(); |
| 68 | } |
| 69 | |
| 70 | static terminate_handler __terminate_handler = default_terminate_handler; |
| 71 | static unexpected_handler __unexpected_handler = default_unexpected_handler; |
| 72 | static new_handler __new_handler = 0; |
| 73 | |
| 74 | unexpected_handler |
| 75 | set_unexpected(unexpected_handler func) _NOEXCEPT |
| 76 | { |
| 77 | if (func == 0) |
| 78 | func = default_unexpected_handler; |
| 79 | return __sync_lock_test_and_set(&__unexpected_handler, func); |
| 80 | } |
| 81 | |
| 82 | unexpected_handler |
| 83 | get_unexpected() _NOEXCEPT |
| 84 | { |
| 85 | return __sync_fetch_and_add(&__unexpected_handler, (unexpected_handler)0); |
| 86 | } |
| 87 | |
Howard Hinnant | fe90746 | 2012-01-24 18:26:29 +0000 | [diff] [blame^] | 88 | __attribute__((visibility("hidden"), noreturn)) |
Howard Hinnant | 44d4d3b | 2011-12-06 17:51:25 +0000 | [diff] [blame] | 89 | void |
| 90 | __unexpected(unexpected_handler func) |
| 91 | { |
| 92 | func(); |
| 93 | // unexpected handler should not return |
| 94 | abort_message("unexpected_handler unexpectedly returned"); |
| 95 | } |
| 96 | |
Howard Hinnant | fe90746 | 2012-01-24 18:26:29 +0000 | [diff] [blame^] | 97 | __attribute__((noreturn)) |
Howard Hinnant | 44d4d3b | 2011-12-06 17:51:25 +0000 | [diff] [blame] | 98 | void |
| 99 | unexpected() |
| 100 | { |
| 101 | __unexpected(get_unexpected()); |
| 102 | } |
| 103 | |
| 104 | terminate_handler |
| 105 | set_terminate(terminate_handler func) _NOEXCEPT |
| 106 | { |
| 107 | if (func == 0) |
| 108 | func = default_terminate_handler; |
| 109 | return __sync_lock_test_and_set(&__terminate_handler, func); |
| 110 | } |
| 111 | |
| 112 | terminate_handler |
| 113 | get_terminate() _NOEXCEPT |
| 114 | { |
| 115 | return __sync_fetch_and_add(&__terminate_handler, (terminate_handler)0); |
| 116 | } |
| 117 | |
Howard Hinnant | fe90746 | 2012-01-24 18:26:29 +0000 | [diff] [blame^] | 118 | __attribute__((visibility("hidden"), noreturn)) |
Howard Hinnant | 44d4d3b | 2011-12-06 17:51:25 +0000 | [diff] [blame] | 119 | void |
| 120 | __terminate(terminate_handler func) _NOEXCEPT |
| 121 | { |
| 122 | #if __has_feature(cxx_exceptions) |
| 123 | try |
| 124 | { |
| 125 | #endif // __has_feature(cxx_exceptions) |
| 126 | func(); |
| 127 | // handler should not return |
| 128 | abort_message("terminate_handler unexpectedly returned"); |
| 129 | #if __has_feature(cxx_exceptions) |
| 130 | } |
| 131 | catch (...) |
| 132 | { |
| 133 | // handler should not throw exception |
| 134 | abort_message("terminate_handler unexpectedly threw an exception"); |
| 135 | } |
| 136 | #endif // #if __has_feature(cxx_exceptions) |
| 137 | } |
| 138 | |
Howard Hinnant | fe90746 | 2012-01-24 18:26:29 +0000 | [diff] [blame^] | 139 | __attribute__((noreturn)) |
Howard Hinnant | 44d4d3b | 2011-12-06 17:51:25 +0000 | [diff] [blame] | 140 | void |
| 141 | terminate() _NOEXCEPT |
| 142 | { |
| 143 | __terminate(get_terminate()); |
| 144 | } |
| 145 | |
| 146 | new_handler |
| 147 | set_new_handler(new_handler handler) _NOEXCEPT |
| 148 | { |
| 149 | return __sync_lock_test_and_set(&__new_handler, handler); |
| 150 | } |
| 151 | |
| 152 | new_handler |
| 153 | get_new_handler() _NOEXCEPT |
| 154 | { |
| 155 | return __sync_fetch_and_add(&__new_handler, (new_handler)0); |
| 156 | } |
| 157 | |
| 158 | } // std |