Nick Kledzik | 778324a | 2012-03-15 01:52:12 +0000 | [diff] [blame^] | 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 "cxxabi.h" |
| 17 | #include "cxa_handlers.hpp" |
| 18 | #include "cxa_exception.hpp" |
| 19 | #include "private_typeinfo.h" |
| 20 | #include "cxa_default_handlers.hpp" |
| 21 | |
| 22 | __attribute__((noreturn)) |
| 23 | static void default_handler(const char* cause) |
| 24 | { |
| 25 | // If there might be an uncaught exception |
| 26 | using namespace __cxxabiv1; |
| 27 | __cxa_eh_globals* globals = __cxa_get_globals_fast(); |
| 28 | if (globals) |
| 29 | { |
| 30 | __cxa_exception* exception_header = globals->caughtExceptions; |
| 31 | // If there is an uncaught exception |
| 32 | if (exception_header) |
| 33 | { |
| 34 | _Unwind_Exception* unwind_exception = |
| 35 | reinterpret_cast<_Unwind_Exception*>(exception_header + 1) - 1; |
| 36 | bool native_exception = |
| 37 | (unwind_exception->exception_class & get_vendor_and_language) == |
| 38 | (kOurExceptionClass & get_vendor_and_language); |
| 39 | if (native_exception) |
| 40 | { |
| 41 | void* thrown_object = |
| 42 | unwind_exception->exception_class == kOurDependentExceptionClass ? |
| 43 | ((__cxa_dependent_exception*)exception_header)->primaryException : |
| 44 | exception_header + 1; |
| 45 | const __shim_type_info* thrown_type = |
| 46 | static_cast<const __shim_type_info*>(exception_header->exceptionType); |
| 47 | // Try to get demangled name of thrown_type |
| 48 | int status; |
| 49 | char buf[1024]; |
| 50 | size_t len = sizeof(buf); |
| 51 | const char* name = __cxa_demangle(thrown_type->name(), buf, &len, &status); |
| 52 | if (status != 0) |
| 53 | name = thrown_type->name(); |
| 54 | // If the uncaught exception can be caught with std::exception& |
| 55 | const __shim_type_info* catch_type = |
| 56 | static_cast<const __shim_type_info*>(&typeid(std::exception)); |
| 57 | if (catch_type->can_catch(thrown_type, thrown_object)) |
| 58 | { |
| 59 | // Include the what() message from the exception |
| 60 | const std::exception* e = static_cast<const std::exception*>(thrown_object); |
| 61 | abort_message("terminating with %s exception of type %s: %s", |
| 62 | cause, name, e->what()); |
| 63 | } |
| 64 | else |
| 65 | // Else just note that we're terminating with an exception |
| 66 | abort_message("terminating with %s exception of type %s", |
| 67 | cause, name); |
| 68 | } |
| 69 | else |
| 70 | // Else we're terminating with a foreign exception |
| 71 | abort_message("terminating with %s foreign exception", cause); |
| 72 | } |
| 73 | } |
| 74 | // Else just note that we're terminating |
| 75 | abort_message("terminating"); |
| 76 | } |
| 77 | |
| 78 | |
| 79 | __attribute__((visibility("hidden"), noreturn)) |
| 80 | void default_terminate_handler() |
| 81 | { |
| 82 | default_handler("terminate"); |
| 83 | } |
| 84 | |
| 85 | __attribute__((visibility("hidden"), noreturn)) |
| 86 | void default_unexpected_handler() |
| 87 | { |
| 88 | default_handler("unexpected"); |
| 89 | } |
| 90 | |
| 91 | |
| 92 | // |
| 93 | // Global variables that hold the pointers to the current handler |
| 94 | // |
| 95 | std::terminate_handler __cxa_terminate_handler = default_terminate_handler; |
| 96 | std::unexpected_handler __cxa_unexpected_handler = default_unexpected_handler; |
| 97 | std::new_handler __cxa_new_handler = 0; |
| 98 | |