1 //===------------------------- cxa_default_handlers.cpp -------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 // 8 // This file implements the default terminate_handler and unexpected_handler. 9 //===----------------------------------------------------------------------===// 10 11 #include <exception> 12 #include <stdlib.h> 13 #include "abort_message.h" 14 #include "cxxabi.h" 15 #include "cxa_handlers.h" 16 #include "cxa_exception.h" 17 #include "private_typeinfo.h" 18 #include "include/atomic_support.h" 19 20 #if !defined(LIBCXXABI_SILENT_TERMINATE) 21 22 _LIBCPP_SAFE_STATIC 23 static const char* cause = "uncaught"; 24 25 __attribute__((noreturn)) 26 static void demangling_terminate_handler() 27 { 28 #ifndef _LIBCXXABI_NO_EXCEPTIONS 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 #if !defined(LIBCXXABI_NON_DEMANGLING_TERMINATE) 49 // Try to get demangled name of thrown_type 50 int status; 51 char buf[1024]; 52 size_t len = sizeof(buf); 53 const char* name = __cxa_demangle(thrown_type->name(), buf, &len, &status); 54 if (status != 0) 55 name = thrown_type->name(); 56 #else 57 const char* name = thrown_type->name(); 58 #endif 59 // If the uncaught exception can be caught with std::exception& 60 const __shim_type_info* catch_type = 61 static_cast<const __shim_type_info*>(&typeid(std::exception)); 62 if (catch_type->can_catch(thrown_type, thrown_object)) 63 { 64 // Include the what() message from the exception 65 const std::exception* e = static_cast<const std::exception*>(thrown_object); 66 abort_message("terminating with %s exception of type %s: %s", 67 cause, name, e->what()); 68 } 69 else 70 // Else just note that we're terminating with an exception 71 abort_message("terminating with %s exception of type %s", 72 cause, name); 73 } 74 else 75 // Else we're terminating with a foreign exception 76 abort_message("terminating with %s foreign exception", cause); 77 } 78 } 79 #endif 80 // Else just note that we're terminating 81 abort_message("terminating"); 82 } 83 84 __attribute__((noreturn)) 85 static void demangling_unexpected_handler() 86 { 87 cause = "unexpected"; 88 std::terminate(); 89 } 90 91 static constexpr std::terminate_handler default_terminate_handler = demangling_terminate_handler; 92 static constexpr std::terminate_handler default_unexpected_handler = demangling_unexpected_handler; 93 #else 94 static constexpr std::terminate_handler default_terminate_handler = ::abort; 95 static constexpr std::terminate_handler default_unexpected_handler = std::terminate; 96 #endif 97 98 // 99 // Global variables that hold the pointers to the current handler 100 // 101 _LIBCXXABI_DATA_VIS 102 _LIBCPP_SAFE_STATIC std::terminate_handler __cxa_terminate_handler = default_terminate_handler; 103 104 _LIBCXXABI_DATA_VIS 105 _LIBCPP_SAFE_STATIC std::unexpected_handler __cxa_unexpected_handler = default_unexpected_handler; 106 107 namespace std 108 { 109 110 unexpected_handler 111 set_unexpected(unexpected_handler func) noexcept 112 { 113 if (func == 0) 114 func = default_unexpected_handler; 115 return __libcpp_atomic_exchange(&__cxa_unexpected_handler, func, 116 _AO_Acq_Rel); 117 } 118 119 terminate_handler 120 set_terminate(terminate_handler func) noexcept 121 { 122 if (func == 0) 123 func = default_terminate_handler; 124 return __libcpp_atomic_exchange(&__cxa_terminate_handler, func, 125 _AO_Acq_Rel); 126 } 127 128 } 129