annotate libcxxabi/src/cxa_default_handlers.cpp @ 207:2e18cbf3894f

LLVM12
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Tue, 08 Jun 2021 06:07:14 +0900
parents 1d019706d866
children c4bab56944e8
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
150
anatofuz
parents:
diff changeset
1 //===------------------------- cxa_default_handlers.cpp -------------------===//
anatofuz
parents:
diff changeset
2 //
anatofuz
parents:
diff changeset
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
anatofuz
parents:
diff changeset
4 // See https://llvm.org/LICENSE.txt for license information.
anatofuz
parents:
diff changeset
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
anatofuz
parents:
diff changeset
6 //
anatofuz
parents:
diff changeset
7 //
anatofuz
parents:
diff changeset
8 // This file implements the default terminate_handler and unexpected_handler.
anatofuz
parents:
diff changeset
9 //===----------------------------------------------------------------------===//
anatofuz
parents:
diff changeset
10
anatofuz
parents:
diff changeset
11 #include <exception>
anatofuz
parents:
diff changeset
12 #include <stdlib.h>
anatofuz
parents:
diff changeset
13 #include "abort_message.h"
anatofuz
parents:
diff changeset
14 #include "cxxabi.h"
anatofuz
parents:
diff changeset
15 #include "cxa_handlers.h"
anatofuz
parents:
diff changeset
16 #include "cxa_exception.h"
anatofuz
parents:
diff changeset
17 #include "private_typeinfo.h"
anatofuz
parents:
diff changeset
18 #include "include/atomic_support.h"
anatofuz
parents:
diff changeset
19
anatofuz
parents:
diff changeset
20 #if !defined(LIBCXXABI_SILENT_TERMINATE)
anatofuz
parents:
diff changeset
21
anatofuz
parents:
diff changeset
22 _LIBCPP_SAFE_STATIC
anatofuz
parents:
diff changeset
23 static const char* cause = "uncaught";
anatofuz
parents:
diff changeset
24
anatofuz
parents:
diff changeset
25 __attribute__((noreturn))
anatofuz
parents:
diff changeset
26 static void demangling_terminate_handler()
anatofuz
parents:
diff changeset
27 {
anatofuz
parents:
diff changeset
28 #ifndef _LIBCXXABI_NO_EXCEPTIONS
anatofuz
parents:
diff changeset
29 // If there might be an uncaught exception
anatofuz
parents:
diff changeset
30 using namespace __cxxabiv1;
anatofuz
parents:
diff changeset
31 __cxa_eh_globals* globals = __cxa_get_globals_fast();
anatofuz
parents:
diff changeset
32 if (globals)
anatofuz
parents:
diff changeset
33 {
anatofuz
parents:
diff changeset
34 __cxa_exception* exception_header = globals->caughtExceptions;
anatofuz
parents:
diff changeset
35 // If there is an uncaught exception
anatofuz
parents:
diff changeset
36 if (exception_header)
anatofuz
parents:
diff changeset
37 {
anatofuz
parents:
diff changeset
38 _Unwind_Exception* unwind_exception =
anatofuz
parents:
diff changeset
39 reinterpret_cast<_Unwind_Exception*>(exception_header + 1) - 1;
anatofuz
parents:
diff changeset
40 if (__isOurExceptionClass(unwind_exception))
anatofuz
parents:
diff changeset
41 {
anatofuz
parents:
diff changeset
42 void* thrown_object =
anatofuz
parents:
diff changeset
43 __getExceptionClass(unwind_exception) == kOurDependentExceptionClass ?
anatofuz
parents:
diff changeset
44 ((__cxa_dependent_exception*)exception_header)->primaryException :
anatofuz
parents:
diff changeset
45 exception_header + 1;
anatofuz
parents:
diff changeset
46 const __shim_type_info* thrown_type =
anatofuz
parents:
diff changeset
47 static_cast<const __shim_type_info*>(exception_header->exceptionType);
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
48 #if !defined(LIBCXXABI_NON_DEMANGLING_TERMINATE)
150
anatofuz
parents:
diff changeset
49 // Try to get demangled name of thrown_type
anatofuz
parents:
diff changeset
50 int status;
anatofuz
parents:
diff changeset
51 char buf[1024];
anatofuz
parents:
diff changeset
52 size_t len = sizeof(buf);
anatofuz
parents:
diff changeset
53 const char* name = __cxa_demangle(thrown_type->name(), buf, &len, &status);
anatofuz
parents:
diff changeset
54 if (status != 0)
anatofuz
parents:
diff changeset
55 name = thrown_type->name();
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
56 #else
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
57 const char* name = thrown_type->name();
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
58 #endif
150
anatofuz
parents:
diff changeset
59 // If the uncaught exception can be caught with std::exception&
anatofuz
parents:
diff changeset
60 const __shim_type_info* catch_type =
anatofuz
parents:
diff changeset
61 static_cast<const __shim_type_info*>(&typeid(std::exception));
anatofuz
parents:
diff changeset
62 if (catch_type->can_catch(thrown_type, thrown_object))
anatofuz
parents:
diff changeset
63 {
anatofuz
parents:
diff changeset
64 // Include the what() message from the exception
anatofuz
parents:
diff changeset
65 const std::exception* e = static_cast<const std::exception*>(thrown_object);
anatofuz
parents:
diff changeset
66 abort_message("terminating with %s exception of type %s: %s",
anatofuz
parents:
diff changeset
67 cause, name, e->what());
anatofuz
parents:
diff changeset
68 }
anatofuz
parents:
diff changeset
69 else
anatofuz
parents:
diff changeset
70 // Else just note that we're terminating with an exception
anatofuz
parents:
diff changeset
71 abort_message("terminating with %s exception of type %s",
anatofuz
parents:
diff changeset
72 cause, name);
anatofuz
parents:
diff changeset
73 }
anatofuz
parents:
diff changeset
74 else
anatofuz
parents:
diff changeset
75 // Else we're terminating with a foreign exception
anatofuz
parents:
diff changeset
76 abort_message("terminating with %s foreign exception", cause);
anatofuz
parents:
diff changeset
77 }
anatofuz
parents:
diff changeset
78 }
anatofuz
parents:
diff changeset
79 #endif
anatofuz
parents:
diff changeset
80 // Else just note that we're terminating
anatofuz
parents:
diff changeset
81 abort_message("terminating");
anatofuz
parents:
diff changeset
82 }
anatofuz
parents:
diff changeset
83
anatofuz
parents:
diff changeset
84 __attribute__((noreturn))
anatofuz
parents:
diff changeset
85 static void demangling_unexpected_handler()
anatofuz
parents:
diff changeset
86 {
anatofuz
parents:
diff changeset
87 cause = "unexpected";
anatofuz
parents:
diff changeset
88 std::terminate();
anatofuz
parents:
diff changeset
89 }
anatofuz
parents:
diff changeset
90
anatofuz
parents:
diff changeset
91 static constexpr std::terminate_handler default_terminate_handler = demangling_terminate_handler;
anatofuz
parents:
diff changeset
92 static constexpr std::terminate_handler default_unexpected_handler = demangling_unexpected_handler;
anatofuz
parents:
diff changeset
93 #else
anatofuz
parents:
diff changeset
94 static constexpr std::terminate_handler default_terminate_handler = ::abort;
anatofuz
parents:
diff changeset
95 static constexpr std::terminate_handler default_unexpected_handler = std::terminate;
anatofuz
parents:
diff changeset
96 #endif
anatofuz
parents:
diff changeset
97
anatofuz
parents:
diff changeset
98 //
anatofuz
parents:
diff changeset
99 // Global variables that hold the pointers to the current handler
anatofuz
parents:
diff changeset
100 //
anatofuz
parents:
diff changeset
101 _LIBCXXABI_DATA_VIS
anatofuz
parents:
diff changeset
102 _LIBCPP_SAFE_STATIC std::terminate_handler __cxa_terminate_handler = default_terminate_handler;
anatofuz
parents:
diff changeset
103
anatofuz
parents:
diff changeset
104 _LIBCXXABI_DATA_VIS
anatofuz
parents:
diff changeset
105 _LIBCPP_SAFE_STATIC std::unexpected_handler __cxa_unexpected_handler = default_unexpected_handler;
anatofuz
parents:
diff changeset
106
anatofuz
parents:
diff changeset
107 namespace std
anatofuz
parents:
diff changeset
108 {
anatofuz
parents:
diff changeset
109
anatofuz
parents:
diff changeset
110 unexpected_handler
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
111 set_unexpected(unexpected_handler func) noexcept
150
anatofuz
parents:
diff changeset
112 {
anatofuz
parents:
diff changeset
113 if (func == 0)
anatofuz
parents:
diff changeset
114 func = default_unexpected_handler;
anatofuz
parents:
diff changeset
115 return __libcpp_atomic_exchange(&__cxa_unexpected_handler, func,
anatofuz
parents:
diff changeset
116 _AO_Acq_Rel);
anatofuz
parents:
diff changeset
117 }
anatofuz
parents:
diff changeset
118
anatofuz
parents:
diff changeset
119 terminate_handler
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
120 set_terminate(terminate_handler func) noexcept
150
anatofuz
parents:
diff changeset
121 {
anatofuz
parents:
diff changeset
122 if (func == 0)
anatofuz
parents:
diff changeset
123 func = default_terminate_handler;
anatofuz
parents:
diff changeset
124 return __libcpp_atomic_exchange(&__cxa_terminate_handler, func,
anatofuz
parents:
diff changeset
125 _AO_Acq_Rel);
anatofuz
parents:
diff changeset
126 }
anatofuz
parents:
diff changeset
127
anatofuz
parents:
diff changeset
128 }