Revert "Change exception tracer to use per-thread caching in libunwind"
[folly.git] / folly / experimental / exception_tracer / ExceptionTracerLib.cpp
1 /*
2  * Copyright 2013 Facebook, Inc.
3  *
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
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include <dlfcn.h>
18 #include <pthread.h>
19 #include <stdlib.h>
20
21 #include <glog/logging.h>
22
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
28 namespace __cxxabiv1 {
29
30 extern "C" {
31 void __cxa_throw(void* thrownException, std::type_info* type,
32                  void (*destructor)(void)) FOLLY_NORETURN;
33 void* __cxa_begin_catch(void* excObj);
34 void __cxa_rethrow(void) FOLLY_NORETURN;
35 void __cxa_end_catch(void);
36 }
37
38 }  // namespace __cxxabiv1
39
40 namespace {
41
42 __thread bool invalid;
43 __thread StackTraceStack* activeExceptions;
44 __thread StackTraceStack* caughtExceptions;
45 pthread_once_t initialized = PTHREAD_ONCE_INIT;
46
47 extern "C" {
48 typedef void (*CxaThrowType)(void*, std::type_info*, void (*)(void))
49   FOLLY_NORETURN;
50 typedef void* (*CxaBeginCatchType)(void*);
51 typedef void (*CxaRethrowType)(void)
52   FOLLY_NORETURN;
53 typedef void (*CxaEndCatchType)(void);
54
55 CxaThrowType orig_cxa_throw;
56 CxaBeginCatchType orig_cxa_begin_catch;
57 CxaRethrowType orig_cxa_rethrow;
58 CxaEndCatchType orig_cxa_end_catch;
59 }  // extern "C"
60
61 typedef void (*RethrowExceptionType)(std::exception_ptr)
62   FOLLY_NORETURN;
63 RethrowExceptionType orig_rethrow_exception;
64
65 void initialize() {
66   orig_cxa_throw = (CxaThrowType)dlsym(RTLD_NEXT, "__cxa_throw");
67   orig_cxa_begin_catch =
68     (CxaBeginCatchType)dlsym(RTLD_NEXT, "__cxa_begin_catch");
69   orig_cxa_rethrow =
70     (CxaRethrowType)dlsym(RTLD_NEXT, "__cxa_rethrow");
71   orig_cxa_end_catch = (CxaEndCatchType)dlsym(RTLD_NEXT, "__cxa_end_catch");
72   // Mangled name for std::rethrow_exception
73   // TODO(tudorb): Dicey, as it relies on the fact that std::exception_ptr
74   // is typedef'ed to a type in namespace __exception_ptr
75   orig_rethrow_exception =
76     (RethrowExceptionType)dlsym(
77         RTLD_NEXT,
78         "_ZSt17rethrow_exceptionNSt15__exception_ptr13exception_ptrE");
79
80   if (!orig_cxa_throw || !orig_cxa_begin_catch || !orig_cxa_rethrow ||
81       !orig_cxa_end_catch || !orig_rethrow_exception) {
82     abort();  // what else can we do?
83   }
84 }
85
86 }  // namespace
87
88 // This function is exported and may be found via dlsym(RTLD_NEXT, ...)
89 extern "C" const StackTraceStack* getExceptionStackTraceStack() {
90   return caughtExceptions;
91 }
92
93 namespace {
94 // Make sure we're counting stack frames correctly for the "skip" argument to
95 // pushCurrentStackTrace, don't inline.
96 void addActiveException() __attribute__((noinline));
97
98 void addActiveException() {
99   pthread_once(&initialized, initialize);
100   // Capture stack trace
101   if (!invalid) {
102     if (pushCurrentStackTrace(3, &activeExceptions) != 0) {
103       clearStack(&activeExceptions);
104       clearStack(&caughtExceptions);
105       invalid = true;
106     }
107   }
108 }
109
110 void moveTopException(StackTraceStack** from, StackTraceStack** to) {
111   if (invalid) {
112     return;
113   }
114   if (moveTop(from, to) != 0) {
115     clearStack(from);
116     clearStack(to);
117     invalid = true;
118   }
119 }
120
121 }  // namespace
122
123 namespace __cxxabiv1 {
124
125 void __cxa_throw(void* thrownException, std::type_info* type,
126                  void (*destructor)(void)) {
127   addActiveException();
128   orig_cxa_throw(thrownException, type, destructor);
129 }
130
131 void __cxa_rethrow() {
132   // __cxa_rethrow leaves the current exception on the caught stack,
133   // and __cxa_begin_catch recognizes that case.  We could do the same, but
134   // we'll implement something simpler (and slower): we pop the exception from
135   // the caught stack, and push it back onto the active stack; this way, our
136   // implementation of __cxa_begin_catch doesn't have to do anything special.
137   moveTopException(&caughtExceptions, &activeExceptions);
138   orig_cxa_rethrow();
139 }
140
141 void* __cxa_begin_catch(void *excObj) {
142   // excObj is a pointer to the unwindHeader in __cxa_exception
143   moveTopException(&activeExceptions, &caughtExceptions);
144   return orig_cxa_begin_catch(excObj);
145 }
146
147 void __cxa_end_catch() {
148   if (!invalid) {
149     __cxa_exception* top = __cxa_get_globals_fast()->caughtExceptions;
150     // This is gcc specific and not specified in the ABI:
151     // abs(handlerCount) is the number of active handlers, it's negative
152     // for rethrown exceptions and positive (always 1) for regular exceptions.
153     // In the rethrow case, we've already popped the exception off the
154     // caught stack, so we don't do anything here.
155     if (top->handlerCount == 1) {
156       popStackTrace(&caughtExceptions);
157     }
158   }
159   orig_cxa_end_catch();
160 }
161
162 }  // namespace __cxxabiv1
163
164 namespace std {
165
166 void rethrow_exception(std::exception_ptr ep) {
167   addActiveException();
168   orig_rethrow_exception(ep);
169 }
170
171 }  // namespace std
172
173
174 namespace {
175
176 struct Initializer {
177   Initializer() {
178     try {
179       ::folly::exception_tracer::installHandlers();
180     } catch (...) {
181     }
182   }
183 };
184
185 Initializer initializer;
186
187 }  // namespace