Path: blob/master/libs/c++abi/src/cxa_default_handlers.cpp
12346 views
//===------------------------- cxa_default_handlers.cpp -------------------===//1//2// The LLVM Compiler Infrastructure3//4// This file is dual licensed under the MIT and the University of Illinois Open5// Source Licenses. See LICENSE.TXT for details.6//7//8// This file implements the default terminate_handler and unexpected_handler.9//===----------------------------------------------------------------------===//1011#include <stdexcept>12#include <new>13#include <exception>14#include <cstdlib>15#include "abort_message.h"16#include "cxxabi.h"17#include "cxa_handlers.hpp"18#include "cxa_exception.hpp"19#include "private_typeinfo.h"20#include "include/atomic_support.h"2122#if !defined(LIBCXXABI_SILENT_TERMINATE)23static const char* cause = "uncaught";2425__attribute__((noreturn))26static void demangling_terminate_handler()27{28// If there might be an uncaught exception29using namespace __cxxabiv1;30__cxa_eh_globals* globals = __cxa_get_globals_fast();31if (globals)32{33__cxa_exception* exception_header = globals->caughtExceptions;34// If there is an uncaught exception35if (exception_header)36{37_Unwind_Exception* unwind_exception =38reinterpret_cast<_Unwind_Exception*>(exception_header + 1) - 1;39if (__isOurExceptionClass(unwind_exception))40{41void* thrown_object =42__getExceptionClass(unwind_exception) == kOurDependentExceptionClass ?43((__cxa_dependent_exception*)exception_header)->primaryException :44exception_header + 1;45const __shim_type_info* thrown_type =46static_cast<const __shim_type_info*>(exception_header->exceptionType);47// Try to get demangled name of thrown_type48int status;49char buf[1024];50size_t len = sizeof(buf);51const char* name = __cxa_demangle(thrown_type->name(), buf, &len, &status);52if (status != 0)53name = thrown_type->name();54// If the uncaught exception can be caught with std::exception&55const __shim_type_info* catch_type =56static_cast<const __shim_type_info*>(&typeid(std::exception));57if (catch_type->can_catch(thrown_type, thrown_object))58{59// Include the what() message from the exception60const std::exception* e = static_cast<const std::exception*>(thrown_object);61abort_message("terminating with %s exception of type %s: %s",62cause, name, e->what());63}64else65// Else just note that we're terminating with an exception66abort_message("terminating with %s exception of type %s",67cause, name);68}69else70// Else we're terminating with a foreign exception71abort_message("terminating with %s foreign exception", cause);72}73}74// Else just note that we're terminating75abort_message("terminating");76}7778__attribute__((noreturn))79static void demangling_unexpected_handler()80{81cause = "unexpected";82std::terminate();83}8485static std::terminate_handler default_terminate_handler = demangling_terminate_handler;86static std::terminate_handler default_unexpected_handler = demangling_unexpected_handler;87#else88static std::terminate_handler default_terminate_handler = std::abort;89static std::terminate_handler default_unexpected_handler = std::terminate;90#endif9192//93// Global variables that hold the pointers to the current handler94//95_LIBCXXABI_DATA_VIS96std::terminate_handler __cxa_terminate_handler = default_terminate_handler;9798_LIBCXXABI_DATA_VIS99std::unexpected_handler __cxa_unexpected_handler = default_unexpected_handler;100101namespace std102{103104unexpected_handler105set_unexpected(unexpected_handler func) _NOEXCEPT106{107if (func == 0)108func = default_unexpected_handler;109return __libcpp_atomic_exchange(&__cxa_unexpected_handler, func,110_AO_Acq_Rel);111}112113terminate_handler114set_terminate(terminate_handler func) _NOEXCEPT115{116if (func == 0)117func = default_terminate_handler;118return __libcpp_atomic_exchange(&__cxa_terminate_handler, func,119_AO_Acq_Rel);120}121122}123124125