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 <cstdlib> 16 #include "abort_message.h" 17 #include "cxxabi.h" 18 #include "cxa_handlers.hpp" 19 #include "cxa_exception.hpp" 20 #include "private_typeinfo.h" 21 #include "include/atomic_support.h" 22 23 #if !defined(LIBCXXABI_SILENT_TERMINATE) 24 static const char* cause = "uncaught"; 25 26 __attribute__((noreturn)) 27 static void demangling_terminate_handler() 28 { 29 // If there might be an uncaught exception 30 using namespace __cxxabiv1; 31 __cxa_eh_globals* globals = __cxa_get_globals_fast(); 32 if (globals) 33 { 34 __cxa_exception* exception_header = globals->caughtExceptions; 35 // If there is an uncaught exception 36 if (exception_header) 37 { 38 _Unwind_Exception* unwind_exception = 39 reinterpret_cast<_Unwind_Exception*>(exception_header + 1) - 1; 40 if (__isOurExceptionClass(unwind_exception)) 41 { 42 void* thrown_object = 43 __getExceptionClass(unwind_exception) == 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 79 __attribute__((noreturn)) 80 static void demangling_unexpected_handler() 81 { 82 cause = "unexpected"; 83 std::terminate(); 84 } 85 86 static std::terminate_handler default_terminate_handler = demangling_terminate_handler; 87 static std::terminate_handler default_unexpected_handler = demangling_unexpected_handler; 88 #else 89 static std::terminate_handler default_terminate_handler = std::abort; 90 static std::terminate_handler default_unexpected_handler = std::terminate; 91 #endif 92 93 // 94 // Global variables that hold the pointers to the current handler 95 // 96 _LIBCXXABI_DATA_VIS 97 std::terminate_handler __cxa_terminate_handler = default_terminate_handler; 98 99 _LIBCXXABI_DATA_VIS 100 std::unexpected_handler __cxa_unexpected_handler = default_unexpected_handler; 101 102 namespace std 103 { 104 105 unexpected_handler 106 set_unexpected(unexpected_handler func) _NOEXCEPT 107 { 108 if (func == 0) 109 func = default_unexpected_handler; 110 return __libcpp_atomic_exchange(&__cxa_unexpected_handler, func, 111 _AO_Acq_Rel); 112 } 113 114 terminate_handler 115 set_terminate(terminate_handler func) _NOEXCEPT 116 { 117 if (func == 0) 118 func = default_terminate_handler; 119 return __libcpp_atomic_exchange(&__cxa_terminate_handler, func, 120 _AO_Acq_Rel); 121 } 122 123 } 124