2 * Copyright 2014 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.
21 #include <glog/logging.h>
23 #include <folly/Portability.h>
24 #include <folly/experimental/exception_tracer/StackTrace.h>
25 #include <folly/experimental/exception_tracer/ExceptionAbi.h>
26 #include <folly/experimental/exception_tracer/ExceptionTracer.h>
27 #include <folly/experimental/symbolizer/Symbolizer.h>
29 namespace __cxxabiv1 {
32 FOLLY_NORETURN void __cxa_throw(void* thrownException,
33 std::type_info* type, void (*destructor)(void));
34 void* __cxa_begin_catch(void* excObj);
35 FOLLY_NORETURN void __cxa_rethrow(void);
36 void __cxa_end_catch(void);
39 } // namespace __cxxabiv1
41 using namespace folly::exception_tracer;
45 FOLLY_TLS bool invalid;
46 FOLLY_TLS StackTraceStack activeExceptions;
47 FOLLY_TLS StackTraceStack caughtExceptions;
48 pthread_once_t initialized = PTHREAD_ONCE_INIT;
51 FOLLY_NORETURN typedef void (*CxaThrowType)(void*, std::type_info*,
53 typedef void* (*CxaBeginCatchType)(void*);
54 FOLLY_NORETURN typedef void (*CxaRethrowType)(void);
55 typedef void (*CxaEndCatchType)(void);
57 CxaThrowType orig_cxa_throw;
58 CxaBeginCatchType orig_cxa_begin_catch;
59 CxaRethrowType orig_cxa_rethrow;
60 CxaEndCatchType orig_cxa_end_catch;
63 FOLLY_NORETURN typedef void (*RethrowExceptionType)(std::exception_ptr);
64 RethrowExceptionType orig_rethrow_exception;
67 orig_cxa_throw = (CxaThrowType)dlsym(RTLD_NEXT, "__cxa_throw");
68 orig_cxa_begin_catch =
69 (CxaBeginCatchType)dlsym(RTLD_NEXT, "__cxa_begin_catch");
71 (CxaRethrowType)dlsym(RTLD_NEXT, "__cxa_rethrow");
72 orig_cxa_end_catch = (CxaEndCatchType)dlsym(RTLD_NEXT, "__cxa_end_catch");
73 // Mangled name for std::rethrow_exception
74 // TODO(tudorb): Dicey, as it relies on the fact that std::exception_ptr
75 // is typedef'ed to a type in namespace __exception_ptr
76 orig_rethrow_exception =
77 (RethrowExceptionType)dlsym(
79 "_ZSt17rethrow_exceptionNSt15__exception_ptr13exception_ptrE");
81 if (!orig_cxa_throw || !orig_cxa_begin_catch || !orig_cxa_rethrow ||
82 !orig_cxa_end_catch || !orig_rethrow_exception) {
83 abort(); // what else can we do?
89 // This function is exported and may be found via dlsym(RTLD_NEXT, ...)
90 extern "C" StackTraceStack* getExceptionStackTraceStack() {
91 return invalid ? nullptr : &caughtExceptions;
96 // Make sure we're counting stack frames correctly, don't inline.
97 FOLLY_NOINLINE void addActiveException();
99 void addActiveException() {
100 pthread_once(&initialized, initialize);
101 // Capture stack trace
103 if (!activeExceptions.pushCurrent()) {
104 activeExceptions.clear();
105 caughtExceptions.clear();
111 void moveTopException(StackTraceStack& from, StackTraceStack& to) {
115 if (!to.moveTopFrom(from)) {
124 namespace __cxxabiv1 {
126 void __cxa_throw(void* thrownException, std::type_info* type,
127 void (*destructor)(void)) {
128 addActiveException();
129 orig_cxa_throw(thrownException, type, destructor);
132 void __cxa_rethrow() {
133 // __cxa_rethrow leaves the current exception on the caught stack,
134 // and __cxa_begin_catch recognizes that case. We could do the same, but
135 // we'll implement something simpler (and slower): we pop the exception from
136 // the caught stack, and push it back onto the active stack; this way, our
137 // implementation of __cxa_begin_catch doesn't have to do anything special.
138 moveTopException(caughtExceptions, activeExceptions);
142 void* __cxa_begin_catch(void *excObj) {
143 // excObj is a pointer to the unwindHeader in __cxa_exception
144 moveTopException(activeExceptions, caughtExceptions);
145 return orig_cxa_begin_catch(excObj);
148 void __cxa_end_catch() {
150 __cxa_exception* top = __cxa_get_globals_fast()->caughtExceptions;
151 // This is gcc specific and not specified in the ABI:
152 // abs(handlerCount) is the number of active handlers, it's negative
153 // for rethrown exceptions and positive (always 1) for regular exceptions.
154 // In the rethrow case, we've already popped the exception off the
155 // caught stack, so we don't do anything here.
156 if (top->handlerCount == 1) {
157 if (!caughtExceptions.pop()) {
158 activeExceptions.clear();
163 orig_cxa_end_catch();
166 } // namespace __cxxabiv1
170 void rethrow_exception(std::exception_ptr ep) {
171 addActiveException();
172 orig_rethrow_exception(ep);
183 ::folly::exception_tracer::installHandlers();
189 Initializer initializer;