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 <cstdlib>
16 #include "abort_message.h"
17 #include "cxxabi.h"
18 #include "cxa_handlers.hpp"
19 #include "cxa_exception.hpp"
20 #include "private_typeinfo.h"
21 #include "include/atomic_support.h"
22
23 #if !defined(LIBCXXABI_SILENT_TERMINATE)
24 static const char* cause = "uncaught";
25
26 __attribute__((noreturn))
demangling_terminate_handler()27 static void demangling_terminate_handler()
28 {
29 // If there might be an uncaught exception
30 using namespace __cxxabiv1;
31 __cxa_eh_globals* globals = __cxa_get_globals_fast();
32 if (globals)
33 {
34 __cxa_exception* exception_header = globals->caughtExceptions;
35 // If there is an uncaught exception
36 if (exception_header)
37 {
38 _Unwind_Exception* unwind_exception =
39 reinterpret_cast<_Unwind_Exception*>(exception_header + 1) - 1;
40 if (__isOurExceptionClass(unwind_exception))
41 {
42 void* thrown_object =
43 __getExceptionClass(unwind_exception) == 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 #if !defined(__ANDROID__)
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 #else
57 const char* name = thrown_type->name();
58 #endif
59 // If the uncaught exception can be caught with std::exception&
60 const __shim_type_info* catch_type =
61 static_cast<const __shim_type_info*>(&typeid(std::exception));
62 if (catch_type->can_catch(thrown_type, thrown_object))
63 {
64 // Include the what() message from the exception
65 const std::exception* e = static_cast<const std::exception*>(thrown_object);
66 abort_message("terminating with %s exception of type %s: %s",
67 cause, name, e->what());
68 }
69 else
70 // Else just note that we're terminating with an exception
71 abort_message("terminating with %s exception of type %s",
72 cause, name);
73 }
74 else
75 // Else we're terminating with a foreign exception
76 abort_message("terminating with %s foreign exception", cause);
77 }
78 }
79 // Else just note that we're terminating
80 abort_message("terminating");
81 }
82
83 __attribute__((noreturn))
demangling_unexpected_handler()84 static void demangling_unexpected_handler()
85 {
86 cause = "unexpected";
87 std::terminate();
88 }
89
90 static std::terminate_handler default_terminate_handler = demangling_terminate_handler;
91 static std::terminate_handler default_unexpected_handler = demangling_unexpected_handler;
92 #else
93 static std::terminate_handler default_terminate_handler = std::abort;
94 static std::terminate_handler default_unexpected_handler = std::terminate;
95 #endif
96
97 //
98 // Global variables that hold the pointers to the current handler
99 //
100 _LIBCXXABI_DATA_VIS
101 std::terminate_handler __cxa_terminate_handler = default_terminate_handler;
102
103 _LIBCXXABI_DATA_VIS
104 std::unexpected_handler __cxa_unexpected_handler = default_unexpected_handler;
105
106 namespace std
107 {
108
109 unexpected_handler
set_unexpected(unexpected_handler func)110 set_unexpected(unexpected_handler func) _NOEXCEPT
111 {
112 if (func == 0)
113 func = default_unexpected_handler;
114 return __libcpp_atomic_exchange(&__cxa_unexpected_handler, func,
115 _AO_Acq_Rel);
116 }
117
118 terminate_handler
set_terminate(terminate_handler func)119 set_terminate(terminate_handler func) _NOEXCEPT
120 {
121 if (func == 0)
122 func = default_terminate_handler;
123 return __libcpp_atomic_exchange(&__cxa_terminate_handler, func,
124 _AO_Acq_Rel);
125 }
126
127 }
128