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