blob: 27ffb719b05d79cfef07804b54ce9395478162e2 [file] [log] [blame]
Nick Kledzik778324a2012-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>
15#include "abort_message.h"
16#include "cxxabi.h"
17#include "cxa_handlers.hpp"
18#include "cxa_exception.hpp"
19#include "private_typeinfo.h"
Nick Kledzik778324a2012-03-15 01:52:12 +000020
Howard Hinnant2b853bf2013-02-06 19:29:55 +000021static const char* cause = "uncaught";
22
Nick Kledzik778324a2012-03-15 01:52:12 +000023__attribute__((noreturn))
Howard Hinnant2b853bf2013-02-06 19:29:55 +000024static void default_terminate_handler()
Nick Kledzik778324a2012-03-15 01:52:12 +000025{
26 // If there might be an uncaught exception
27 using namespace __cxxabiv1;
28 __cxa_eh_globals* globals = __cxa_get_globals_fast();
29 if (globals)
30 {
31 __cxa_exception* exception_header = globals->caughtExceptions;
32 // If there is an uncaught exception
33 if (exception_header)
34 {
35 _Unwind_Exception* unwind_exception =
36 reinterpret_cast<_Unwind_Exception*>(exception_header + 1) - 1;
37 bool native_exception =
38 (unwind_exception->exception_class & get_vendor_and_language) ==
39 (kOurExceptionClass & get_vendor_and_language);
40 if (native_exception)
41 {
42 void* thrown_object =
43 unwind_exception->exception_class == 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 // Try to get demangled name of thrown_type
49 int status;
50 char buf[1024];
51 size_t len = sizeof(buf);
52 const char* name = __cxa_demangle(thrown_type->name(), buf, &len, &status);
53 if (status != 0)
54 name = thrown_type->name();
55 // If the uncaught exception can be caught with std::exception&
56 const __shim_type_info* catch_type =
57 static_cast<const __shim_type_info*>(&typeid(std::exception));
58 if (catch_type->can_catch(thrown_type, thrown_object))
59 {
60 // Include the what() message from the exception
61 const std::exception* e = static_cast<const std::exception*>(thrown_object);
62 abort_message("terminating with %s exception of type %s: %s",
63 cause, name, e->what());
64 }
65 else
66 // Else just note that we're terminating with an exception
67 abort_message("terminating with %s exception of type %s",
68 cause, name);
69 }
70 else
71 // Else we're terminating with a foreign exception
72 abort_message("terminating with %s foreign exception", cause);
73 }
74 }
75 // Else just note that we're terminating
76 abort_message("terminating");
77}
78
Howard Hinnant4ac72dd2012-03-19 16:20:34 +000079__attribute__((noreturn))
80static void default_unexpected_handler()
Nick Kledzik778324a2012-03-15 01:52:12 +000081{
Howard Hinnant2b853bf2013-02-06 19:29:55 +000082 cause = "unexpected";
83 std::terminate();
Nick Kledzik778324a2012-03-15 01:52:12 +000084}
85
86
87//
88// Global variables that hold the pointers to the current handler
89//
90std::terminate_handler __cxa_terminate_handler = default_terminate_handler;
91std::unexpected_handler __cxa_unexpected_handler = default_unexpected_handler;
Dave Zarzycki13e35c02012-03-15 08:58:06 +000092
Howard Hinnante59dbd72012-03-19 16:56:51 +000093// In the future these will become:
94// std::atomic<std::terminate_handler> __cxa_terminate_handler(default_terminate_handler);
95// std::atomic<std::unexpected_handler> __cxa_unexpected_handler(default_unexpected_handler);
96
Dave Zarzycki13e35c02012-03-15 08:58:06 +000097namespace std
98{
99
100unexpected_handler
101set_unexpected(unexpected_handler func) _NOEXCEPT
102{
103 if (func == 0)
104 func = default_unexpected_handler;
Howard Hinnant4ac72dd2012-03-19 16:20:34 +0000105 return __sync_swap(&__cxa_unexpected_handler, func);
Howard Hinnante59dbd72012-03-19 16:56:51 +0000106// Using of C++11 atomics this should be rewritten
107// return __cxa_unexpected_handler.exchange(func, memory_order_acq_rel);
Dave Zarzycki13e35c02012-03-15 08:58:06 +0000108}
109
110terminate_handler
111set_terminate(terminate_handler func) _NOEXCEPT
112{
113 if (func == 0)
114 func = default_terminate_handler;
Howard Hinnant4ac72dd2012-03-19 16:20:34 +0000115 return __sync_swap(&__cxa_terminate_handler, func);
Howard Hinnante59dbd72012-03-19 16:56:51 +0000116// Using of C++11 atomics this should be rewritten
117// return __cxa_terminate_handler.exchange(func, memory_order_acq_rel);
Dave Zarzycki13e35c02012-03-15 08:58:06 +0000118}
119
Howard Hinnante59dbd72012-03-19 16:56:51 +0000120}