2c6e109adb4704fb57c03e57378d58ae29efc56c
[folly.git] / folly / experimental / exception_tracer / ExceptionTracer.cpp
1 /*
2  * Copyright 2017 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   printExceptionInfo(out, info, SymbolizePrinter::COLOR_IF_TTY);
48   return out;
49 }
50
51 void printExceptionInfo(
52     std::ostream& out,
53     const ExceptionInfo& info,
54     int options) {
55   out << "Exception type: ";
56   if (info.type) {
57     out << folly::demangle(*info.type);
58   } else {
59     out << "(unknown type)";
60   }
61   out << " (" << info.frames.size()
62       << (info.frames.size() == 1 ? " frame" : " frames")
63       << ")\n";
64   try {
65     size_t frameCount = info.frames.size();
66
67     // Skip our own internal frames
68     static constexpr size_t kInternalFramesNumber = 3;
69     if (frameCount > kInternalFramesNumber) {
70       auto addresses = info.frames.data() + kInternalFramesNumber;
71       frameCount -= kInternalFramesNumber;
72
73       std::vector<SymbolizedFrame> frames;
74       frames.resize(frameCount);
75
76       Symbolizer symbolizer(
77           (options & SymbolizePrinter::NO_FILE_AND_LINE)
78               ? Dwarf::LocationInfoMode::DISABLED
79               : Symbolizer::kDefaultLocationInfoMode);
80       symbolizer.symbolize(addresses, frames.data(), frameCount);
81
82       OStreamSymbolizePrinter osp(out, options);
83       osp.println(addresses, frames.data(), frameCount);
84     }
85   } catch (const std::exception& e) {
86     out << "\n !! caught " << folly::exceptionStr(e) << "\n";
87   } catch (...) {
88     out << "\n !!! caught unexpected exception\n";
89   }
90 }
91
92 namespace {
93
94 /**
95  * Is this a standard C++ ABI exception?
96  *
97  * Dependent exceptions (thrown via std::rethrow_exception) aren't --
98  * exc doesn't actually point to a __cxa_exception structure, but
99  * the offset of unwindHeader is correct, so exc->unwindHeader actually
100  * returns a _Unwind_Exception object.  Yeah, it's ugly like that.
101  */
102 bool isAbiCppException(const __cxa_exception* exc) {
103   // The least significant four bytes must be "C++\0"
104   static const uint64_t cppClass =
105     ((uint64_t)'C' << 24) |
106     ((uint64_t)'+' << 16) |
107     ((uint64_t)'+' << 8);
108   return (exc->unwindHeader.exception_class & 0xffffffff) == cppClass;
109 }
110
111 }  // namespace
112
113 std::vector<ExceptionInfo> getCurrentExceptions() {
114   struct Once {
115     Once() {
116       // See if linked in with us (getExceptionStackTraceStack is weak)
117       getExceptionStackTraceStackFn = getExceptionStackTraceStack;
118
119       if (!getExceptionStackTraceStackFn) {
120         // Nope, see if it's in a shared library
121         getExceptionStackTraceStackFn =
122           (GetExceptionStackTraceStackType)dlsym(
123               RTLD_NEXT, "getExceptionStackTraceStack");
124       }
125     }
126   };
127   static Once once;
128
129   std::vector<ExceptionInfo> exceptions;
130   auto currentException = __cxa_get_globals()->caughtExceptions;
131   if (!currentException) {
132     return exceptions;
133   }
134
135   StackTraceStack* traceStack = nullptr;
136   if (!getExceptionStackTraceStackFn) {
137     static bool logged = false;
138     if (!logged) {
139       LOG(WARNING)
140         << "Exception tracer library not linked, stack traces not available";
141       logged = true;
142     }
143   } else if ((traceStack = getExceptionStackTraceStackFn()) == nullptr) {
144     static bool logged = false;
145     if (!logged) {
146       LOG(WARNING)
147         << "Exception stack trace invalid, stack traces not available";
148       logged = true;
149     }
150   }
151
152   StackTrace* trace = traceStack ? traceStack->top() : nullptr;
153   while (currentException) {
154     ExceptionInfo info;
155     // Dependent exceptions (thrown via std::rethrow_exception) aren't
156     // standard ABI __cxa_exception objects, and are correctly labeled as
157     // such in the exception_class field.  We could try to extract the
158     // primary exception type in horribly hacky ways, but, for now, nullptr.
159     info.type =
160       isAbiCppException(currentException) ?
161       currentException->exceptionType :
162       nullptr;
163     if (traceStack) {
164       CHECK(trace) << "Invalid trace stack!";
165       info.frames.assign(trace->addresses,
166                          trace->addresses + trace->frameCount);
167       trace = traceStack->next(trace);
168     }
169     currentException = currentException->nextException;
170     exceptions.push_back(std::move(info));
171   }
172   CHECK(!trace) << "Invalid trace stack!";
173
174   return exceptions;
175 }
176
177 namespace {
178
179 std::terminate_handler origTerminate = abort;
180 std::unexpected_handler origUnexpected = abort;
181
182 void dumpExceptionStack(const char* prefix) {
183   auto exceptions = getCurrentExceptions();
184   if (exceptions.empty()) {
185     return;
186   }
187   LOG(ERROR) << prefix << ", exception stack follows";
188   for (auto& exc : exceptions) {
189     LOG(ERROR) << exc << "\n";
190   }
191   LOG(ERROR) << "exception stack complete";
192 }
193
194 void terminateHandler() {
195   dumpExceptionStack("terminate() called");
196   origTerminate();
197 }
198
199 void unexpectedHandler() {
200   dumpExceptionStack("Unexpected exception");
201   origUnexpected();
202 }
203
204 }  // namespace
205
206 void installHandlers() {
207   struct Once {
208     Once() {
209       origTerminate = std::set_terminate(terminateHandler);
210       origUnexpected = std::set_unexpected(unexpectedHandler);
211     }
212   };
213   static Once once;
214 }
215
216 }  // namespace exception_tracer
217 }  // namespace folly