blob: 0fba79ced8a9f92223e0996a4f7bab9f796d42c5 [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
21__attribute__((noreturn))
22static void default_handler(const char* cause)
23{
24 // If there might be an uncaught exception
25 using namespace __cxxabiv1;
26 __cxa_eh_globals* globals = __cxa_get_globals_fast();
27 if (globals)
28 {
29 __cxa_exception* exception_header = globals->caughtExceptions;
30 // If there is an uncaught exception
31 if (exception_header)
32 {
33 _Unwind_Exception* unwind_exception =
34 reinterpret_cast<_Unwind_Exception*>(exception_header + 1) - 1;
35 bool native_exception =
36 (unwind_exception->exception_class & get_vendor_and_language) ==
37 (kOurExceptionClass & get_vendor_and_language);
38 if (native_exception)
39 {
40 void* thrown_object =
41 unwind_exception->exception_class == kOurDependentExceptionClass ?
42 ((__cxa_dependent_exception*)exception_header)->primaryException :
43 exception_header + 1;
44 const __shim_type_info* thrown_type =
45 static_cast<const __shim_type_info*>(exception_header->exceptionType);
46 // Try to get demangled name of thrown_type
47 int status;
48 char buf[1024];
49 size_t len = sizeof(buf);
50 const char* name = __cxa_demangle(thrown_type->name(), buf, &len, &status);
51 if (status != 0)
52 name = thrown_type->name();
53 // If the uncaught exception can be caught with std::exception&
54 const __shim_type_info* catch_type =
55 static_cast<const __shim_type_info*>(&typeid(std::exception));
56 if (catch_type->can_catch(thrown_type, thrown_object))
57 {
58 // Include the what() message from the exception
59 const std::exception* e = static_cast<const std::exception*>(thrown_object);
60 abort_message("terminating with %s exception of type %s: %s",
61 cause, name, e->what());
62 }
63 else
64 // Else just note that we're terminating with an exception
65 abort_message("terminating with %s exception of type %s",
66 cause, name);
67 }
68 else
69 // Else we're terminating with a foreign exception
70 abort_message("terminating with %s foreign exception", cause);
71 }
72 }
73 // Else just note that we're terminating
74 abort_message("terminating");
75}
76
77
Howard Hinnant4ac72dd2012-03-19 16:20:34 +000078__attribute__((noreturn))
79static void default_terminate_handler()
Nick Kledzik778324a2012-03-15 01:52:12 +000080{
Howard Hinnant2a9c5092012-04-24 17:41:51 +000081 default_handler("uncaught");
Nick Kledzik778324a2012-03-15 01:52:12 +000082}
83
Howard Hinnant4ac72dd2012-03-19 16:20:34 +000084__attribute__((noreturn))
85static void default_unexpected_handler()
Nick Kledzik778324a2012-03-15 01:52:12 +000086{
87 default_handler("unexpected");
88}
89
90
91//
92// Global variables that hold the pointers to the current handler
93//
94std::terminate_handler __cxa_terminate_handler = default_terminate_handler;
95std::unexpected_handler __cxa_unexpected_handler = default_unexpected_handler;
Dave Zarzycki13e35c02012-03-15 08:58:06 +000096
Howard Hinnante59dbd72012-03-19 16:56:51 +000097// In the future these will become:
98// std::atomic<std::terminate_handler> __cxa_terminate_handler(default_terminate_handler);
99// std::atomic<std::unexpected_handler> __cxa_unexpected_handler(default_unexpected_handler);
100
Dave Zarzycki13e35c02012-03-15 08:58:06 +0000101namespace std
102{
103
104unexpected_handler
105set_unexpected(unexpected_handler func) _NOEXCEPT
106{
107 if (func == 0)
108 func = default_unexpected_handler;
Howard Hinnant4ac72dd2012-03-19 16:20:34 +0000109 return __sync_swap(&__cxa_unexpected_handler, func);
Howard Hinnante59dbd72012-03-19 16:56:51 +0000110// Using of C++11 atomics this should be rewritten
111// return __cxa_unexpected_handler.exchange(func, memory_order_acq_rel);
Dave Zarzycki13e35c02012-03-15 08:58:06 +0000112}
113
114terminate_handler
115set_terminate(terminate_handler func) _NOEXCEPT
116{
117 if (func == 0)
118 func = default_terminate_handler;
Howard Hinnant4ac72dd2012-03-19 16:20:34 +0000119 return __sync_swap(&__cxa_terminate_handler, func);
Howard Hinnante59dbd72012-03-19 16:56:51 +0000120// Using of C++11 atomics this should be rewritten
121// return __cxa_terminate_handler.exchange(func, memory_order_acq_rel);
Dave Zarzycki13e35c02012-03-15 08:58:06 +0000122}
123
Howard Hinnante59dbd72012-03-19 16:56:51 +0000124}