2 * Copyright 2013 Facebook, Inc.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include "folly/experimental/exception_tracer/ExceptionTracer.h"
23 #include <glog/logging.h>
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"
33 const StackTraceStack* getExceptionStackTraceStack(void) __attribute__((weak));
34 typedef const StackTraceStack* (*GetExceptionStackTraceStackType)(void);
35 GetExceptionStackTraceStackType getExceptionStackTraceStackFn;
40 using namespace ::folly::symbolizer;
41 using namespace __cxxabiv1;
44 namespace exception_tracer {
46 std::ostream& operator<<(std::ostream& out, const ExceptionInfo& info) {
47 out << "Exception type: ";
49 out << folly::demangle(*info.type);
51 out << "(unknown type)";
53 out << " (" << info.frames.size()
54 << (info.frames.size() == 1 ? " frame" : " frames")
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);
66 } catch (const std::exception& e) {
67 out << "\n !! caught " << folly::exceptionStr(e) << "\n";
69 out << "\n !!! caught unexpected exception\n";
77 * Is this a standard C++ ABI exception?
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.
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) |
90 return (exc->unwindHeader.exception_class & 0xffffffff) == cppClass;
95 std::vector<ExceptionInfo> getCurrentExceptions() {
98 // See if linked in with us (getExceptionStackTraceStack is weak)
99 getExceptionStackTraceStackFn = getExceptionStackTraceStack;
101 if (!getExceptionStackTraceStackFn) {
102 // Nope, see if it's in a shared library
103 getExceptionStackTraceStackFn =
104 (GetExceptionStackTraceStackType)dlsym(
105 RTLD_NEXT, "getExceptionStackTraceStack");
111 std::vector<ExceptionInfo> exceptions;
112 auto currentException = __cxa_get_globals()->caughtExceptions;
113 if (!currentException) {
117 bool hasTraceStack = false;
118 const StackTraceStack* traceStack = nullptr;
119 if (!getExceptionStackTraceStackFn) {
120 static bool logged = false;
123 << "Exception tracer library not linked, stack traces not available";
126 } else if ((traceStack = getExceptionStackTraceStackFn()) == nullptr) {
127 static bool logged = false;
130 << "Exception stack trace invalid, stack traces not available";
134 hasTraceStack = true;
137 while (currentException) {
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.
144 isAbiCppException(currentException) ?
145 currentException->exceptionType :
148 CHECK(traceStack) << "Invalid trace stack!";
150 traceStack->trace.frameIPs,
151 traceStack->trace.frameIPs + traceStack->trace.frameCount);
152 traceStack = traceStack->next;
154 currentException = currentException->nextException;
155 exceptions.push_back(std::move(info));
158 CHECK(!traceStack) << "Invalid trace stack!";
165 std::terminate_handler origTerminate = abort;
166 std::unexpected_handler origUnexpected = abort;
168 void dumpExceptionStack(const char* prefix) {
169 auto exceptions = getCurrentExceptions();
170 if (exceptions.empty()) {
173 LOG(ERROR) << prefix << ", exception stack follows";
174 for (auto& exc : exceptions) {
175 LOG(ERROR) << exc << "\n";
177 LOG(ERROR) << "exception stack complete";
180 void terminateHandler() {
181 dumpExceptionStack("terminate() called");
185 void unexpectedHandler() {
186 dumpExceptionStack("Unexpected exception");
192 void installHandlers() {
195 origTerminate = std::set_terminate(terminateHandler);
196 origUnexpected = std::set_unexpected(unexpectedHandler);
202 } // namespace exception_tracer