1*c05d8e5dSAndroid Build Coastguard Worker //===------------------------- cxa_default_handlers.cpp -------------------===//
2*c05d8e5dSAndroid Build Coastguard Worker //
3*c05d8e5dSAndroid Build Coastguard Worker // The LLVM Compiler Infrastructure
4*c05d8e5dSAndroid Build Coastguard Worker //
5*c05d8e5dSAndroid Build Coastguard Worker // This file is dual licensed under the MIT and the University of Illinois Open
6*c05d8e5dSAndroid Build Coastguard Worker // Source Licenses. See LICENSE.TXT for details.
7*c05d8e5dSAndroid Build Coastguard Worker //
8*c05d8e5dSAndroid Build Coastguard Worker //
9*c05d8e5dSAndroid Build Coastguard Worker // This file implements the default terminate_handler and unexpected_handler.
10*c05d8e5dSAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
11*c05d8e5dSAndroid Build Coastguard Worker
12*c05d8e5dSAndroid Build Coastguard Worker #include <stdexcept>
13*c05d8e5dSAndroid Build Coastguard Worker #include <new>
14*c05d8e5dSAndroid Build Coastguard Worker #include <exception>
15*c05d8e5dSAndroid Build Coastguard Worker #include <cstdlib>
16*c05d8e5dSAndroid Build Coastguard Worker #include "abort_message.h"
17*c05d8e5dSAndroid Build Coastguard Worker #include "cxxabi.h"
18*c05d8e5dSAndroid Build Coastguard Worker #include "cxa_handlers.hpp"
19*c05d8e5dSAndroid Build Coastguard Worker #include "cxa_exception.hpp"
20*c05d8e5dSAndroid Build Coastguard Worker #include "private_typeinfo.h"
21*c05d8e5dSAndroid Build Coastguard Worker #include "include/atomic_support.h"
22*c05d8e5dSAndroid Build Coastguard Worker
23*c05d8e5dSAndroid Build Coastguard Worker #if !defined(LIBCXXABI_SILENT_TERMINATE)
24*c05d8e5dSAndroid Build Coastguard Worker static const char* cause = "uncaught";
25*c05d8e5dSAndroid Build Coastguard Worker
26*c05d8e5dSAndroid Build Coastguard Worker __attribute__((noreturn))
demangling_terminate_handler()27*c05d8e5dSAndroid Build Coastguard Worker static void demangling_terminate_handler()
28*c05d8e5dSAndroid Build Coastguard Worker {
29*c05d8e5dSAndroid Build Coastguard Worker // If there might be an uncaught exception
30*c05d8e5dSAndroid Build Coastguard Worker using namespace __cxxabiv1;
31*c05d8e5dSAndroid Build Coastguard Worker __cxa_eh_globals* globals = __cxa_get_globals_fast();
32*c05d8e5dSAndroid Build Coastguard Worker if (globals)
33*c05d8e5dSAndroid Build Coastguard Worker {
34*c05d8e5dSAndroid Build Coastguard Worker __cxa_exception* exception_header = globals->caughtExceptions;
35*c05d8e5dSAndroid Build Coastguard Worker // If there is an uncaught exception
36*c05d8e5dSAndroid Build Coastguard Worker if (exception_header)
37*c05d8e5dSAndroid Build Coastguard Worker {
38*c05d8e5dSAndroid Build Coastguard Worker _Unwind_Exception* unwind_exception =
39*c05d8e5dSAndroid Build Coastguard Worker reinterpret_cast<_Unwind_Exception*>(exception_header + 1) - 1;
40*c05d8e5dSAndroid Build Coastguard Worker if (__isOurExceptionClass(unwind_exception))
41*c05d8e5dSAndroid Build Coastguard Worker {
42*c05d8e5dSAndroid Build Coastguard Worker void* thrown_object =
43*c05d8e5dSAndroid Build Coastguard Worker __getExceptionClass(unwind_exception) == kOurDependentExceptionClass ?
44*c05d8e5dSAndroid Build Coastguard Worker ((__cxa_dependent_exception*)exception_header)->primaryException :
45*c05d8e5dSAndroid Build Coastguard Worker exception_header + 1;
46*c05d8e5dSAndroid Build Coastguard Worker const __shim_type_info* thrown_type =
47*c05d8e5dSAndroid Build Coastguard Worker static_cast<const __shim_type_info*>(exception_header->exceptionType);
48*c05d8e5dSAndroid Build Coastguard Worker #if !defined(__ANDROID__)
49*c05d8e5dSAndroid Build Coastguard Worker // Try to get demangled name of thrown_type
50*c05d8e5dSAndroid Build Coastguard Worker int status;
51*c05d8e5dSAndroid Build Coastguard Worker char buf[1024];
52*c05d8e5dSAndroid Build Coastguard Worker size_t len = sizeof(buf);
53*c05d8e5dSAndroid Build Coastguard Worker const char* name = __cxa_demangle(thrown_type->name(), buf, &len, &status);
54*c05d8e5dSAndroid Build Coastguard Worker if (status != 0)
55*c05d8e5dSAndroid Build Coastguard Worker name = thrown_type->name();
56*c05d8e5dSAndroid Build Coastguard Worker #else
57*c05d8e5dSAndroid Build Coastguard Worker const char* name = thrown_type->name();
58*c05d8e5dSAndroid Build Coastguard Worker #endif
59*c05d8e5dSAndroid Build Coastguard Worker // If the uncaught exception can be caught with std::exception&
60*c05d8e5dSAndroid Build Coastguard Worker const __shim_type_info* catch_type =
61*c05d8e5dSAndroid Build Coastguard Worker static_cast<const __shim_type_info*>(&typeid(std::exception));
62*c05d8e5dSAndroid Build Coastguard Worker if (catch_type->can_catch(thrown_type, thrown_object))
63*c05d8e5dSAndroid Build Coastguard Worker {
64*c05d8e5dSAndroid Build Coastguard Worker // Include the what() message from the exception
65*c05d8e5dSAndroid Build Coastguard Worker const std::exception* e = static_cast<const std::exception*>(thrown_object);
66*c05d8e5dSAndroid Build Coastguard Worker abort_message("terminating with %s exception of type %s: %s",
67*c05d8e5dSAndroid Build Coastguard Worker cause, name, e->what());
68*c05d8e5dSAndroid Build Coastguard Worker }
69*c05d8e5dSAndroid Build Coastguard Worker else
70*c05d8e5dSAndroid Build Coastguard Worker // Else just note that we're terminating with an exception
71*c05d8e5dSAndroid Build Coastguard Worker abort_message("terminating with %s exception of type %s",
72*c05d8e5dSAndroid Build Coastguard Worker cause, name);
73*c05d8e5dSAndroid Build Coastguard Worker }
74*c05d8e5dSAndroid Build Coastguard Worker else
75*c05d8e5dSAndroid Build Coastguard Worker // Else we're terminating with a foreign exception
76*c05d8e5dSAndroid Build Coastguard Worker abort_message("terminating with %s foreign exception", cause);
77*c05d8e5dSAndroid Build Coastguard Worker }
78*c05d8e5dSAndroid Build Coastguard Worker }
79*c05d8e5dSAndroid Build Coastguard Worker // Else just note that we're terminating
80*c05d8e5dSAndroid Build Coastguard Worker abort_message("terminating");
81*c05d8e5dSAndroid Build Coastguard Worker }
82*c05d8e5dSAndroid Build Coastguard Worker
83*c05d8e5dSAndroid Build Coastguard Worker __attribute__((noreturn))
demangling_unexpected_handler()84*c05d8e5dSAndroid Build Coastguard Worker static void demangling_unexpected_handler()
85*c05d8e5dSAndroid Build Coastguard Worker {
86*c05d8e5dSAndroid Build Coastguard Worker cause = "unexpected";
87*c05d8e5dSAndroid Build Coastguard Worker std::terminate();
88*c05d8e5dSAndroid Build Coastguard Worker }
89*c05d8e5dSAndroid Build Coastguard Worker
90*c05d8e5dSAndroid Build Coastguard Worker static std::terminate_handler default_terminate_handler = demangling_terminate_handler;
91*c05d8e5dSAndroid Build Coastguard Worker static std::terminate_handler default_unexpected_handler = demangling_unexpected_handler;
92*c05d8e5dSAndroid Build Coastguard Worker #else
93*c05d8e5dSAndroid Build Coastguard Worker static std::terminate_handler default_terminate_handler = std::abort;
94*c05d8e5dSAndroid Build Coastguard Worker static std::terminate_handler default_unexpected_handler = std::terminate;
95*c05d8e5dSAndroid Build Coastguard Worker #endif
96*c05d8e5dSAndroid Build Coastguard Worker
97*c05d8e5dSAndroid Build Coastguard Worker //
98*c05d8e5dSAndroid Build Coastguard Worker // Global variables that hold the pointers to the current handler
99*c05d8e5dSAndroid Build Coastguard Worker //
100*c05d8e5dSAndroid Build Coastguard Worker _LIBCXXABI_DATA_VIS
101*c05d8e5dSAndroid Build Coastguard Worker std::terminate_handler __cxa_terminate_handler = default_terminate_handler;
102*c05d8e5dSAndroid Build Coastguard Worker
103*c05d8e5dSAndroid Build Coastguard Worker _LIBCXXABI_DATA_VIS
104*c05d8e5dSAndroid Build Coastguard Worker std::unexpected_handler __cxa_unexpected_handler = default_unexpected_handler;
105*c05d8e5dSAndroid Build Coastguard Worker
106*c05d8e5dSAndroid Build Coastguard Worker namespace std
107*c05d8e5dSAndroid Build Coastguard Worker {
108*c05d8e5dSAndroid Build Coastguard Worker
109*c05d8e5dSAndroid Build Coastguard Worker unexpected_handler
set_unexpected(unexpected_handler func)110*c05d8e5dSAndroid Build Coastguard Worker set_unexpected(unexpected_handler func) _NOEXCEPT
111*c05d8e5dSAndroid Build Coastguard Worker {
112*c05d8e5dSAndroid Build Coastguard Worker if (func == 0)
113*c05d8e5dSAndroid Build Coastguard Worker func = default_unexpected_handler;
114*c05d8e5dSAndroid Build Coastguard Worker return __libcpp_atomic_exchange(&__cxa_unexpected_handler, func,
115*c05d8e5dSAndroid Build Coastguard Worker _AO_Acq_Rel);
116*c05d8e5dSAndroid Build Coastguard Worker }
117*c05d8e5dSAndroid Build Coastguard Worker
118*c05d8e5dSAndroid Build Coastguard Worker terminate_handler
set_terminate(terminate_handler func)119*c05d8e5dSAndroid Build Coastguard Worker set_terminate(terminate_handler func) _NOEXCEPT
120*c05d8e5dSAndroid Build Coastguard Worker {
121*c05d8e5dSAndroid Build Coastguard Worker if (func == 0)
122*c05d8e5dSAndroid Build Coastguard Worker func = default_terminate_handler;
123*c05d8e5dSAndroid Build Coastguard Worker return __libcpp_atomic_exchange(&__cxa_terminate_handler, func,
124*c05d8e5dSAndroid Build Coastguard Worker _AO_Acq_Rel);
125*c05d8e5dSAndroid Build Coastguard Worker }
126*c05d8e5dSAndroid Build Coastguard Worker
127*c05d8e5dSAndroid Build Coastguard Worker }
128