1 //===------------------------- cxa_default_handlers.cpp -------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
9 // This file implements the default terminate_handler and unexpected_handler.
10 //===----------------------------------------------------------------------===//
15 #include "abort_message.h"
16 #include "config.h" // For __sync_swap
18 #include "cxa_handlers.hpp"
19 #include "cxa_exception.hpp"
20 #include "private_typeinfo.h"
22 static const char* cause = "uncaught";
24 __attribute__((noreturn))
25 static void default_terminate_handler()
27 // If there might be an uncaught exception
28 using namespace __cxxabiv1;
29 __cxa_eh_globals* globals = __cxa_get_globals_fast();
32 __cxa_exception* exception_header = globals->caughtExceptions;
33 // If there is an uncaught exception
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);
44 unwind_exception->exception_class == kOurDependentExceptionClass ?
45 ((__cxa_dependent_exception*)exception_header)->primaryException :
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
52 size_t len = sizeof(buf);
53 const char* name = __cxa_demangle(thrown_type->name(), buf, &len, &status);
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))
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());
67 // Else just note that we're terminating with an exception
68 abort_message("terminating with %s exception of type %s",
72 // Else we're terminating with a foreign exception
73 abort_message("terminating with %s foreign exception", cause);
76 // Else just note that we're terminating
77 abort_message("terminating");
80 __attribute__((noreturn))
81 static void default_unexpected_handler()
89 // Global variables that hold the pointers to the current handler
91 std::terminate_handler __cxa_terminate_handler = default_terminate_handler;
92 std::unexpected_handler __cxa_unexpected_handler = default_unexpected_handler;
94 // In the future these will become:
95 // std::atomic<std::terminate_handler> __cxa_terminate_handler(default_terminate_handler);
96 // std::atomic<std::unexpected_handler> __cxa_unexpected_handler(default_unexpected_handler);
102 set_unexpected(unexpected_handler func) _NOEXCEPT
105 func = default_unexpected_handler;
106 return __sync_swap(&__cxa_unexpected_handler, func);
107 // Using of C++11 atomics this should be rewritten
108 // return __cxa_unexpected_handler.exchange(func, memory_order_acq_rel);
112 set_terminate(terminate_handler func) _NOEXCEPT
115 func = default_terminate_handler;
116 return __sync_swap(&__cxa_terminate_handler, func);
117 // Using of C++11 atomics this should be rewritten
118 // return __cxa_terminate_handler.exchange(func, memory_order_acq_rel);