• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
22 #if !defined(LIBCXXABI_SILENT_TERMINATE)
23 static const char* cause = "uncaught";
24 
25 __attribute__((noreturn))
demangling_terminate_handler()26 static void demangling_terminate_handler()
27 {
28     // If there might be an uncaught exception
29     using namespace __cxxabiv1;
30     __cxa_eh_globals* globals = __cxa_get_globals_fast();
31     if (globals)
32     {
33         __cxa_exception* exception_header = globals->caughtExceptions;
34         // If there is an uncaught exception
35         if (exception_header)
36         {
37             _Unwind_Exception* unwind_exception =
38                 reinterpret_cast<_Unwind_Exception*>(exception_header + 1) - 1;
39             bool native_exception =
40                 (unwind_exception->exception_class   & get_vendor_and_language) ==
41                                  (kOurExceptionClass & get_vendor_and_language);
42             if (native_exception)
43             {
44                 void* thrown_object =
45                     unwind_exception->exception_class == kOurDependentExceptionClass ?
46                         ((__cxa_dependent_exception*)exception_header)->primaryException :
47                         exception_header + 1;
48                 const __shim_type_info* thrown_type =
49                     static_cast<const __shim_type_info*>(exception_header->exceptionType);
50                 // Try to get demangled name of thrown_type
51                 int status;
52                 char buf[1024];
53                 size_t len = sizeof(buf);
54                 const char* name = __cxa_demangle(thrown_type->name(), buf, &len, &status);
55                 if (status != 0)
56                     name = thrown_type->name();
57                 // If the uncaught exception can be caught with std::exception&
58                 const __shim_type_info* catch_type =
59 				 static_cast<const __shim_type_info*>(&typeid(std::exception));
60                 if (catch_type->can_catch(thrown_type, thrown_object))
61                 {
62                     // Include the what() message from the exception
63                     const std::exception* e = static_cast<const std::exception*>(thrown_object);
64                     abort_message("terminating with %s exception of type %s: %s",
65                                   cause, name, e->what());
66                 }
67                 else
68                     // Else just note that we're terminating with an exception
69                     abort_message("terminating with %s exception of type %s",
70                                    cause, name);
71             }
72             else
73                 // Else we're terminating with a foreign exception
74                 abort_message("terminating with %s foreign exception", cause);
75         }
76     }
77     // Else just note that we're terminating
78     abort_message("terminating");
79 }
80 
81 __attribute__((noreturn))
demangling_unexpected_handler()82 static void demangling_unexpected_handler()
83 {
84     cause = "unexpected";
85     std::terminate();
86 }
87 
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;
92 static std::terminate_handler default_unexpected_handler = std::terminate;
93 #endif
94 
95 //
96 // Global variables that hold the pointers to the current handler
97 //
98 _LIBCXXABI_DATA_VIS
99 std::terminate_handler __cxa_terminate_handler = default_terminate_handler;
100 
101 _LIBCXXABI_DATA_VIS
102 std::unexpected_handler __cxa_unexpected_handler = default_unexpected_handler;
103 
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 
108 namespace std
109 {
110 
111 unexpected_handler
set_unexpected(unexpected_handler func)112 set_unexpected(unexpected_handler func) _NOEXCEPT
113 {
114     if (func == 0)
115         func = default_unexpected_handler;
116     return __atomic_exchange_n(&__cxa_unexpected_handler, func,
117                                __ATOMIC_ACQ_REL);
118 //  Using of C++11 atomics this should be rewritten
119 //  return __cxa_unexpected_handler.exchange(func, memory_order_acq_rel);
120 }
121 
122 terminate_handler
set_terminate(terminate_handler func)123 set_terminate(terminate_handler func) _NOEXCEPT
124 {
125     if (func == 0)
126         func = default_terminate_handler;
127     return __atomic_exchange_n(&__cxa_terminate_handler, func,
128                                __ATOMIC_ACQ_REL);
129 //  Using of C++11 atomics this should be rewritten
130 //  return __cxa_terminate_handler.exchange(func, memory_order_acq_rel);
131 }
132 
133 }
134