f59d427bd066c8a6e4b00fd967a27626d9afc057
[folly.git] / folly / experimental / exception_tracer / ExceptionTracer.cpp
1 /*
2  * Copyright 2016 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 <folly/experimental/exception_tracer/ExceptionTracer.h>
18
19 #include <dlfcn.h>
20 #include <exception>
21 #include <iostream>
22 #include <glog/logging.h>
23
24 #include <folly/experimental/exception_tracer/ExceptionAbi.h>
25 #include <folly/experimental/exception_tracer/StackTrace.h>
26 #include <folly/experimental/symbolizer/Symbolizer.h>
27 #include <folly/String.h>
28
29 namespace {
30
31 using namespace ::folly::exception_tracer;
32 using namespace ::folly::symbolizer;
33 using namespace __cxxabiv1;
34
35 extern "C" {
36 StackTraceStack* getExceptionStackTraceStack(void) __attribute__((__weak__));
37 typedef StackTraceStack* (*GetExceptionStackTraceStackType)(void);
38 GetExceptionStackTraceStackType getExceptionStackTraceStackFn;
39 }
40
41 }  // namespace
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     size_t frameCount = info.frames.size();
58
59     // Skip our own internal frames
60     static constexpr size_t kInternalFramesNumber = 3;
61     if (frameCount > kInternalFramesNumber) {
62       auto addresses = info.frames.data() + kInternalFramesNumber;
63       frameCount -= kInternalFramesNumber;
64
65       std::vector<SymbolizedFrame> frames;
66       frames.resize(frameCount);
67
68       Symbolizer symbolizer;
69       symbolizer.symbolize(addresses, frames.data(), frameCount);
70
71       OStreamSymbolizePrinter osp(out, SymbolizePrinter::COLOR_IF_TTY);
72       osp.println(addresses, frames.data(), frameCount);
73     }
74   } catch (const std::exception& e) {
75     out << "\n !! caught " << folly::exceptionStr(e) << "\n";
76   } catch (...) {
77     out << "\n !!! caught unexpected exception\n";
78   }
79   return out;
80 }
81
82 namespace {
83
84 /**
85  * Is this a standard C++ ABI exception?
86  *
87  * Dependent exceptions (thrown via std::rethrow_exception) aren't --
88  * exc doesn't actually point to a __cxa_exception structure, but
89  * the offset of unwindHeader is correct, so exc->unwindHeader actually
90  * returns a _Unwind_Exception object.  Yeah, it's ugly like that.
91  */
92 bool isAbiCppException(const __cxa_exception* exc) {
93   // The least significant four bytes must be "C++\0"
94   static const uint64_t cppClass =
95     ((uint64_t)'C' << 24) |
96     ((uint64_t)'+' << 16) |
97     ((uint64_t)'+' << 8);
98   return (exc->unwindHeader.exception_class & 0xffffffff) == cppClass;
99 }
100
101 }  // namespace
102
103 std::vector<ExceptionInfo> getCurrentExceptions() {
104   struct Once {
105     Once() {
106       // See if linked in with us (getExceptionStackTraceStack is weak)
107       getExceptionStackTraceStackFn = getExceptionStackTraceStack;
108
109       if (!getExceptionStackTraceStackFn) {
110         // Nope, see if it's in a shared library
111         getExceptionStackTraceStackFn =
112           (GetExceptionStackTraceStackType)dlsym(
113               RTLD_NEXT, "getExceptionStackTraceStack");
114       }
115     }
116   };
117   static Once once;
118
119   std::vector<ExceptionInfo> exceptions;
120   auto currentException = __cxa_get_globals()->caughtExceptions;
121   if (!currentException) {
122     return exceptions;
123   }
124
125   StackTraceStack* traceStack = nullptr;
126   if (!getExceptionStackTraceStackFn) {
127     static bool logged = false;
128     if (!logged) {
129       LOG(WARNING)
130         << "Exception tracer library not linked, stack traces not available";
131       logged = true;
132     }
133   } else if ((traceStack = getExceptionStackTraceStackFn()) == nullptr) {
134     static bool logged = false;
135     if (!logged) {
136       LOG(WARNING)
137         << "Exception stack trace invalid, stack traces not available";
138       logged = true;
139     }
140   }
141
142   StackTrace* trace = traceStack ? traceStack->top() : nullptr;
143   while (currentException) {
144     ExceptionInfo info;
145     // Dependent exceptions (thrown via std::rethrow_exception) aren't
146     // standard ABI __cxa_exception objects, and are correctly labeled as
147     // such in the exception_class field.  We could try to extract the
148     // primary exception type in horribly hacky ways, but, for now, NULL.
149     info.type =
150       isAbiCppException(currentException) ?
151       currentException->exceptionType :
152       nullptr;
153     if (traceStack) {
154       CHECK(trace) << "Invalid trace stack!";
155       info.frames.assign(trace->addresses,
156                          trace->addresses + trace->frameCount);
157       trace = traceStack->next(trace);
158     }
159     currentException = currentException->nextException;
160     exceptions.push_back(std::move(info));
161   }
162   CHECK(!trace) << "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