2 * Copyright 2015 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.
17 #include <folly/experimental/exception_tracer/ExceptionTracer.h>
22 #include <glog/logging.h>
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>
31 using namespace ::folly::exception_tracer;
32 using namespace ::folly::symbolizer;
33 using namespace __cxxabiv1;
36 StackTraceStack* getExceptionStackTraceStack(void) __attribute__((__weak__));
37 typedef StackTraceStack* (*GetExceptionStackTraceStackType)(void);
38 GetExceptionStackTraceStackType getExceptionStackTraceStackFn;
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 size_t frameCount = info.frames.size();
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;
65 std::vector<SymbolizedFrame> frames;
66 frames.resize(frameCount);
68 Symbolizer symbolizer;
69 symbolizer.symbolize(addresses, frames.data(), frameCount);
71 OStreamSymbolizePrinter osp(out, SymbolizePrinter::COLOR_IF_TTY);
72 osp.println(addresses, frames.data(), frameCount);
74 } catch (const std::exception& e) {
75 out << "\n !! caught " << folly::exceptionStr(e) << "\n";
77 out << "\n !!! caught unexpected exception\n";
85 * Is this a standard C++ ABI exception?
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.
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) |
98 return (exc->unwindHeader.exception_class & 0xffffffff) == cppClass;
103 std::vector<ExceptionInfo> getCurrentExceptions() {
106 // See if linked in with us (getExceptionStackTraceStack is weak)
107 getExceptionStackTraceStackFn = getExceptionStackTraceStack;
109 if (!getExceptionStackTraceStackFn) {
110 // Nope, see if it's in a shared library
111 getExceptionStackTraceStackFn =
112 (GetExceptionStackTraceStackType)dlsym(
113 RTLD_NEXT, "getExceptionStackTraceStack");
119 std::vector<ExceptionInfo> exceptions;
120 auto currentException = __cxa_get_globals()->caughtExceptions;
121 if (!currentException) {
125 StackTraceStack* traceStack = nullptr;
126 if (!getExceptionStackTraceStackFn) {
127 static bool logged = false;
130 << "Exception tracer library not linked, stack traces not available";
133 } else if ((traceStack = getExceptionStackTraceStackFn()) == nullptr) {
134 static bool logged = false;
137 << "Exception stack trace invalid, stack traces not available";
142 StackTrace* trace = traceStack ? traceStack->top() : nullptr;
143 while (currentException) {
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.
150 isAbiCppException(currentException) ?
151 currentException->exceptionType :
154 CHECK(trace) << "Invalid trace stack!";
155 info.frames.assign(trace->addresses,
156 trace->addresses + trace->frameCount);
157 trace = traceStack->next(trace);
159 currentException = currentException->nextException;
160 exceptions.push_back(std::move(info));
162 CHECK(!trace) << "Invalid trace stack!";
169 std::terminate_handler origTerminate = abort;
170 std::unexpected_handler origUnexpected = abort;
172 void dumpExceptionStack(const char* prefix) {
173 auto exceptions = getCurrentExceptions();
174 if (exceptions.empty()) {
177 LOG(ERROR) << prefix << ", exception stack follows";
178 for (auto& exc : exceptions) {
179 LOG(ERROR) << exc << "\n";
181 LOG(ERROR) << "exception stack complete";
184 void terminateHandler() {
185 dumpExceptionStack("terminate() called");
189 void unexpectedHandler() {
190 dumpExceptionStack("Unexpected exception");
196 void installHandlers() {
199 origTerminate = std::set_terminate(terminateHandler);
200 origUnexpected = std::set_unexpected(unexpectedHandler);
206 } // namespace exception_tracer