2 * Copyright 2017 Facebook, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include <folly/experimental/exception_tracer/ExceptionTracerLib.h>
23 #include <folly/Indestructible.h>
24 #include <folly/Portability.h>
25 #include <folly/SharedMutex.h>
26 #include <folly/Synchronized.h>
28 namespace __cxxabiv1 {
32 void* thrownException,
34 void (*destructor)(void*)) __attribute__((__noreturn__));
35 void* __cxa_begin_catch(void* excObj) throw();
36 void __cxa_rethrow(void) __attribute__((__noreturn__));
37 void __cxa_end_catch(void);
40 } // namespace __cxxabiv1
42 using namespace folly::exception_tracer;
46 template <typename Function>
47 class CallbackHolder {
49 void registerCallback(Function f) {
50 SYNCHRONIZED(callbacks_) {
51 callbacks_.push_back(std::move(f));
55 // always inline to enforce kInternalFramesNumber
56 template <typename... Args>
57 FOLLY_ALWAYS_INLINE void invoke(Args... args) {
58 SYNCHRONIZED_CONST(callbacks_) {
59 for (auto& cb : callbacks_) {
66 folly::Synchronized<std::vector<Function>> callbacks_;
72 namespace exception_tracer {
74 #define DECLARE_CALLBACK(NAME) \
75 CallbackHolder<NAME##Type>& get##NAME##Callbacks() { \
76 static Indestructible<CallbackHolder<NAME##Type>> Callbacks; \
79 void register##NAME##Callback(NAME##Type callback) { \
80 get##NAME##Callbacks().registerCallback(callback); \
83 DECLARE_CALLBACK(CxaThrow);
84 DECLARE_CALLBACK(CxaBeginCatch);
85 DECLARE_CALLBACK(CxaRethrow);
86 DECLARE_CALLBACK(CxaEndCatch);
87 DECLARE_CALLBACK(RethrowException);
89 } // namespace exception_tracer
92 // Clang is smart enough to understand that the symbols we're loading
93 // are [[noreturn]], but GCC is not. In order to be able to build with
94 // -Wunreachable-code enable for Clang, these __builtin_unreachable()
95 // calls need to go away. Everything else is messy though, so just
96 // #define it to an empty macro under Clang and be done with it.
98 #define __builtin_unreachable()
101 namespace __cxxabiv1 {
104 void* thrownException,
105 std::type_info* type,
106 void (*destructor)(void*)) {
107 static auto orig_cxa_throw =
108 reinterpret_cast<decltype(&__cxa_throw)>(dlsym(RTLD_NEXT, "__cxa_throw"));
109 getCxaThrowCallbacks().invoke(thrownException, type, destructor);
110 orig_cxa_throw(thrownException, type, destructor);
111 __builtin_unreachable(); // orig_cxa_throw never returns
114 void __cxa_rethrow() {
115 // __cxa_rethrow leaves the current exception on the caught stack,
116 // and __cxa_begin_catch recognizes that case. We could do the same, but
117 // we'll implement something simpler (and slower): we pop the exception from
118 // the caught stack, and push it back onto the active stack; this way, our
119 // implementation of __cxa_begin_catch doesn't have to do anything special.
120 static auto orig_cxa_rethrow = reinterpret_cast<decltype(&__cxa_rethrow)>(
121 dlsym(RTLD_NEXT, "__cxa_rethrow"));
122 getCxaRethrowCallbacks().invoke();
124 __builtin_unreachable(); // orig_cxa_rethrow never returns
127 void* __cxa_begin_catch(void* excObj) throw() {
128 // excObj is a pointer to the unwindHeader in __cxa_exception
129 static auto orig_cxa_begin_catch =
130 reinterpret_cast<decltype(&__cxa_begin_catch)>(
131 dlsym(RTLD_NEXT, "__cxa_begin_catch"));
132 getCxaBeginCatchCallbacks().invoke(excObj);
133 return orig_cxa_begin_catch(excObj);
136 void __cxa_end_catch() {
137 static auto orig_cxa_end_catch = reinterpret_cast<decltype(&__cxa_end_catch)>(
138 dlsym(RTLD_NEXT, "__cxa_end_catch"));
139 getCxaEndCatchCallbacks().invoke();
140 orig_cxa_end_catch();
143 } // namespace __cxxabiv1
147 void rethrow_exception(std::exception_ptr ep) {
148 // Mangled name for std::rethrow_exception
149 // TODO(tudorb): Dicey, as it relies on the fact that std::exception_ptr
150 // is typedef'ed to a type in namespace __exception_ptr
151 static auto orig_rethrow_exception =
152 reinterpret_cast<decltype(&rethrow_exception)>(dlsym(
154 "_ZSt17rethrow_exceptionNSt15__exception_ptr13exception_ptrE"));
155 getRethrowExceptionCallbacks().invoke(ep);
156 orig_rethrow_exception(ep);
157 __builtin_unreachable(); // orig_rethrow_exception never returns