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