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