blob: 726f7eb0e0aa734b8cd24f71b69118d1c55ec7f9 [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{
81 default_handler("terminate");
82}
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
97namespace 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);
Dave Zarzycki13e35c02012-03-15 08:58:06 +0000106}
107
108terminate_handler
109set_terminate(terminate_handler func) _NOEXCEPT
110{
111 if (func == 0)
112 func = default_terminate_handler;
Howard Hinnant4ac72dd2012-03-19 16:20:34 +0000113 return __sync_swap(&__cxa_terminate_handler, func);
Dave Zarzycki13e35c02012-03-15 08:58:06 +0000114}
115
116};