blob: a1ea2961655ea1f5cbeb8b01bd50beb65e010fb4 [file] [log] [blame]
Nick Kledzik439ce872012-03-15 01:52:12 +00001//===------------------------- 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>
James Y Knight0ecfd762017-01-13 19:22:26 +000015#include <cstdlib>
Nick Kledzik439ce872012-03-15 01:52:12 +000016#include "abort_message.h"
Nico Weber298baa32014-06-25 23:55:37 +000017#include "config.h" // For __sync_swap
Nick Kledzik439ce872012-03-15 01:52:12 +000018#include "cxxabi.h"
19#include "cxa_handlers.hpp"
20#include "cxa_exception.hpp"
21#include "private_typeinfo.h"
Nick Kledzik439ce872012-03-15 01:52:12 +000022
Howard Hinnantcba79c62013-02-06 19:29:55 +000023static const char* cause = "uncaught";
24
Nick Kledzik439ce872012-03-15 01:52:12 +000025__attribute__((noreturn))
James Y Knight0ecfd762017-01-13 19:22:26 +000026static void demangling_terminate_handler()
Nick Kledzik439ce872012-03-15 01:52:12 +000027{
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
Howard Hinnant0f80bb72012-03-19 16:20:34 +000081__attribute__((noreturn))
James Y Knight0ecfd762017-01-13 19:22:26 +000082static void demangling_unexpected_handler()
Nick Kledzik439ce872012-03-15 01:52:12 +000083{
Howard Hinnantcba79c62013-02-06 19:29:55 +000084 cause = "unexpected";
85 std::terminate();
Nick Kledzik439ce872012-03-15 01:52:12 +000086}
87
James Y Knight0ecfd762017-01-13 19:22:26 +000088#if !LIBCXXABI_SILENT_TERMINATE
89static std::terminate_handler default_terminate_handler = demangling_terminate_handler;
90static std::terminate_handler default_unexpected_handler = demangling_unexpected_handler;
91#else
92static std::terminate_handler default_terminate_handler = std::abort;
93static std::terminate_handler default_unexpected_handler = std::abort;
94#endif
Nick Kledzik439ce872012-03-15 01:52:12 +000095
96//
97// Global variables that hold the pointers to the current handler
98//
Saleem Abdulrasool436072f2016-05-26 02:12:20 +000099_LIBCXXABI_DATA_VIS
100std::terminate_handler __cxa_terminate_handler = default_terminate_handler;
101
102_LIBCXXABI_DATA_VIS
Nick Kledzik439ce872012-03-15 01:52:12 +0000103std::unexpected_handler __cxa_unexpected_handler = default_unexpected_handler;
Dave Zarzycki2507bef2012-03-15 08:58:06 +0000104
Howard Hinnant4cfb63f2012-03-19 16:56:51 +0000105// In the future these will become:
106// std::atomic<std::terminate_handler> __cxa_terminate_handler(default_terminate_handler);
107// std::atomic<std::unexpected_handler> __cxa_unexpected_handler(default_unexpected_handler);
108
Dave Zarzycki2507bef2012-03-15 08:58:06 +0000109namespace std
110{
111
112unexpected_handler
113set_unexpected(unexpected_handler func) _NOEXCEPT
114{
Reid Kleckner5660f752014-10-03 20:03:47 +0000115 if (func == 0)
116 func = default_unexpected_handler;
117 return __atomic_exchange_n(&__cxa_unexpected_handler, func,
118 __ATOMIC_ACQ_REL);
Howard Hinnant4cfb63f2012-03-19 16:56:51 +0000119// Using of C++11 atomics this should be rewritten
120// return __cxa_unexpected_handler.exchange(func, memory_order_acq_rel);
Dave Zarzycki2507bef2012-03-15 08:58:06 +0000121}
122
123terminate_handler
124set_terminate(terminate_handler func) _NOEXCEPT
125{
Reid Kleckner5660f752014-10-03 20:03:47 +0000126 if (func == 0)
127 func = default_terminate_handler;
128 return __atomic_exchange_n(&__cxa_terminate_handler, func,
129 __ATOMIC_ACQ_REL);
Howard Hinnant4cfb63f2012-03-19 16:56:51 +0000130// Using of C++11 atomics this should be rewritten
131// return __cxa_terminate_handler.exchange(func, memory_order_acq_rel);
Dave Zarzycki2507bef2012-03-15 08:58:06 +0000132}
133
Howard Hinnant4cfb63f2012-03-19 16:56:51 +0000134}