2e84227a0b6bb3b471a0623d67be5f9125652326
[folly.git] / folly / experimental / exception_tracer / ExceptionTracer.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
18 #include "folly/experimental/exception_tracer/ExceptionTracer.h"
19
20 #include <dlfcn.h>
21 #include <exception>
22 #include <iostream>
23 #include <glog/logging.h>
24
25 #include "folly/experimental/exception_tracer/ExceptionAbi.h"
26 #include "folly/experimental/exception_tracer/StackTrace.h"
27 #include "folly/experimental/symbolizer/Symbolizer.h"
28 #include "folly/String.h"
29
30 namespace {
31
32 extern "C" {
33 const StackTraceStack* getExceptionStackTraceStack(void) __attribute__((weak));
34 typedef const StackTraceStack* (*GetExceptionStackTraceStackType)(void);
35 GetExceptionStackTraceStackType getExceptionStackTraceStackFn;
36 }
37
38 }  // namespace
39
40 using namespace ::folly::symbolizer;
41 using namespace __cxxabiv1;
42
43 namespace folly {
44 namespace exception_tracer {
45
46 std::ostream& operator<<(std::ostream& out, const ExceptionInfo& info) {
47   out << "Exception type: ";
48   if (info.type) {
49     out << folly::demangle(*info.type);
50   } else {
51     out << "(unknown type)";
52   }
53   out << " (" << info.frames.size()
54       << (info.frames.size() == 1 ? " frame" : " frames")
55       << ")\n";
56   try {
57     std::vector<FrameInfo> addresses;
58     addresses.reserve(info.frames.size());
59     for (auto ip : info.frames) {
60       // Symbolize the previous address because the IP might be in the
61       // next function, per glog/src/signalhandler.cc
62       addresses.emplace_back(ip - 1);
63     }
64
65     Symbolizer symbolizer;
66     symbolizer.symbolize(addresses.data(), addresses.size());
67
68     OStreamSymbolizePrinter osp(out);
69     osp.print(addresses.data(), addresses.size(), addresses.size());
70   } catch (const std::exception& e) {
71     out << "\n !! caught " << folly::exceptionStr(e) << "\n";
72   } catch (...) {
73     out << "\n !!! caught unexpected exception\n";
74   }
75   return out;
76 }
77
78 namespace {
79
80 /**
81  * Is this a standard C++ ABI exception?
82  *
83  * Dependent exceptions (thrown via std::rethrow_exception) aren't --
84  * exc doesn't actually point to a __cxa_exception structure, but
85  * the offset of unwindHeader is correct, so exc->unwindHeader actually
86  * returns a _Unwind_Exception object.  Yeah, it's ugly like that.
87  */
88 bool isAbiCppException(const __cxa_exception* exc) {
89   // The least significant four bytes must be "C++\0"
90   static const uint64_t cppClass =
91     ((uint64_t)'C' << 24) |
92     ((uint64_t)'+' << 16) |
93     ((uint64_t)'+' << 8);
94   return (exc->unwindHeader.exception_class & 0xffffffff) == cppClass;
95 }
96
97 }  // namespace
98
99 std::vector<ExceptionInfo> getCurrentExceptions() {
100   struct Once {
101     Once() {
102       // See if linked in with us (getExceptionStackTraceStack is weak)
103       getExceptionStackTraceStackFn = getExceptionStackTraceStack;
104
105       if (!getExceptionStackTraceStackFn) {
106         // Nope, see if it's in a shared library
107         getExceptionStackTraceStackFn =
108           (GetExceptionStackTraceStackType)dlsym(
109               RTLD_NEXT, "getExceptionStackTraceStack");
110       }
111     }
112   };
113   static Once once;
114
115   std::vector<ExceptionInfo> exceptions;
116   auto currentException = __cxa_get_globals()->caughtExceptions;
117   if (!currentException) {
118     return exceptions;
119   }
120
121   bool hasTraceStack = false;
122   const StackTraceStack* traceStack = nullptr;
123   if (!getExceptionStackTraceStackFn) {
124     static bool logged = false;
125     if (!logged) {
126       LOG(WARNING)
127         << "Exception tracer library not linked, stack traces not available";
128       logged = true;
129     }
130   } else if ((traceStack = getExceptionStackTraceStackFn()) == nullptr) {
131     static bool logged = false;
132     if (!logged) {
133       LOG(WARNING)
134         << "Exception stack trace invalid, stack traces not available";
135       logged = true;
136     }
137   } else {
138     hasTraceStack = true;
139   }
140
141   while (currentException) {
142     ExceptionInfo info;
143     // Dependent exceptions (thrown via std::rethrow_exception) aren't
144     // standard ABI __cxa_exception objects, and are correctly labeled as
145     // such in the exception_class field.  We could try to extract the
146     // primary exception type in horribly hacky ways, but, for now, NULL.
147     info.type =
148       isAbiCppException(currentException) ?
149       currentException->exceptionType :
150       nullptr;
151     if (hasTraceStack) {
152       CHECK(traceStack) << "Invalid trace stack!";
153       info.frames.assign(
154           traceStack->trace.frameIPs,
155           traceStack->trace.frameIPs + traceStack->trace.frameCount);
156       traceStack = traceStack->next;
157     }
158     currentException = currentException->nextException;
159     exceptions.push_back(std::move(info));
160   }
161
162   CHECK(!traceStack) << "Invalid trace stack!";
163
164   return exceptions;
165 }
166
167 namespace {
168
169 std::terminate_handler origTerminate = abort;
170 std::unexpected_handler origUnexpected = abort;
171
172 void dumpExceptionStack(const char* prefix) {
173   auto exceptions = getCurrentExceptions();
174   if (exceptions.empty()) {
175     return;
176   }
177   LOG(ERROR) << prefix << ", exception stack follows";
178   for (auto& exc : exceptions) {
179     LOG(ERROR) << exc << "\n";
180   }
181   LOG(ERROR) << "exception stack complete";
182 }
183
184 void terminateHandler() {
185   dumpExceptionStack("terminate() called");
186   origTerminate();
187 }
188
189 void unexpectedHandler() {
190   dumpExceptionStack("Unexpected exception");
191   origUnexpected();
192 }
193
194 }  // namespace
195
196 void installHandlers() {
197   struct Once {
198     Once() {
199       origTerminate = std::set_terminate(terminateHandler);
200       origUnexpected = std::set_unexpected(unexpectedHandler);
201     }
202   };
203   static Once once;
204 }
205
206 }  // namespace exception_tracer
207 }  // namespace folly
208