2 * Copyright 2012-present 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>
24 #include <glog/logging.h>
26 #include <folly/String.h>
27 #include <folly/experimental/exception_tracer/ExceptionAbi.h>
28 #include <folly/experimental/exception_tracer/StackTrace.h>
29 #include <folly/experimental/symbolizer/Symbolizer.h>
33 using namespace ::folly::exception_tracer;
34 using namespace ::folly::symbolizer;
35 using namespace __cxxabiv1;
38 StackTraceStack* getExceptionStackTraceStack(void) __attribute__((__weak__));
39 typedef StackTraceStack* (*GetExceptionStackTraceStackType)();
40 GetExceptionStackTraceStackType getExceptionStackTraceStackFn;
46 namespace exception_tracer {
48 std::ostream& operator<<(std::ostream& out, const ExceptionInfo& info) {
49 printExceptionInfo(out, info, SymbolizePrinter::COLOR_IF_TTY);
53 void printExceptionInfo(
55 const ExceptionInfo& info,
57 out << "Exception type: ";
59 out << folly::demangle(*info.type);
61 out << "(unknown type)";
63 out << " (" << info.frames.size()
64 << (info.frames.size() == 1 ? " frame" : " frames") << ")\n";
66 size_t frameCount = info.frames.size();
68 // Skip our own internal frames
69 static constexpr size_t kInternalFramesNumber = 3;
70 if (frameCount > kInternalFramesNumber) {
71 auto addresses = info.frames.data() + kInternalFramesNumber;
72 frameCount -= kInternalFramesNumber;
74 std::vector<SymbolizedFrame> frames;
75 frames.resize(frameCount);
77 Symbolizer symbolizer(
78 (options & SymbolizePrinter::NO_FILE_AND_LINE)
79 ? Dwarf::LocationInfoMode::DISABLED
80 : Symbolizer::kDefaultLocationInfoMode);
81 symbolizer.symbolize(addresses, frames.data(), frameCount);
83 OStreamSymbolizePrinter osp(out, options);
84 osp.println(addresses, frames.data(), frameCount);
86 } catch (const std::exception& e) {
87 out << "\n !! caught " << folly::exceptionStr(e) << "\n";
89 out << "\n !!! caught unexpected exception\n";
96 * Is this a standard C++ ABI exception?
98 * Dependent exceptions (thrown via std::rethrow_exception) aren't --
99 * exc doesn't actually point to a __cxa_exception structure, but
100 * the offset of unwindHeader is correct, so exc->unwindHeader actually
101 * returns a _Unwind_Exception object. Yeah, it's ugly like that.
103 bool isAbiCppException(const __cxa_exception* exc) {
104 // The least significant four bytes must be "C++\0"
105 static const uint64_t cppClass =
106 ((uint64_t)'C' << 24) | ((uint64_t)'+' << 16) | ((uint64_t)'+' << 8);
107 return (exc->unwindHeader.exception_class & 0xffffffff) == cppClass;
112 std::vector<ExceptionInfo> getCurrentExceptions() {
115 // See if linked in with us (getExceptionStackTraceStack is weak)
116 getExceptionStackTraceStackFn = getExceptionStackTraceStack;
118 if (!getExceptionStackTraceStackFn) {
119 // Nope, see if it's in a shared library
120 getExceptionStackTraceStackFn = (GetExceptionStackTraceStackType)dlsym(
121 RTLD_NEXT, "getExceptionStackTraceStack");
127 std::vector<ExceptionInfo> exceptions;
128 auto currentException = __cxa_get_globals()->caughtExceptions;
129 if (!currentException) {
133 StackTraceStack* traceStack = nullptr;
134 if (!getExceptionStackTraceStackFn) {
135 static bool logged = false;
138 << "Exception tracer library not linked, stack traces not available";
141 } else if ((traceStack = getExceptionStackTraceStackFn()) == nullptr) {
142 static bool logged = false;
145 << "Exception stack trace invalid, stack traces not available";
150 StackTrace* trace = traceStack ? traceStack->top() : nullptr;
151 while (currentException) {
153 // Dependent exceptions (thrown via std::rethrow_exception) aren't
154 // standard ABI __cxa_exception objects, and are correctly labeled as
155 // such in the exception_class field. We could try to extract the
156 // primary exception type in horribly hacky ways, but, for now, nullptr.
157 info.type = isAbiCppException(currentException)
158 ? currentException->exceptionType
162 LOG_IF(DFATAL, !trace)
163 << "Invalid trace stack for exception of type: "
164 << (info.type ? folly::demangle(*info.type) : "null");
171 trace->addresses, trace->addresses + trace->frameCount);
172 trace = traceStack->next(trace);
174 currentException = currentException->nextException;
175 exceptions.push_back(std::move(info));
178 LOG_IF(DFATAL, trace) << "Invalid trace stack!";
185 std::terminate_handler origTerminate = abort;
186 std::unexpected_handler origUnexpected = abort;
188 void dumpExceptionStack(const char* prefix) {
189 auto exceptions = getCurrentExceptions();
190 if (exceptions.empty()) {
193 LOG(ERROR) << prefix << ", exception stack follows";
194 for (auto& exc : exceptions) {
195 LOG(ERROR) << exc << "\n";
197 LOG(ERROR) << "exception stack complete";
200 void terminateHandler() {
201 dumpExceptionStack("terminate() called");
205 void unexpectedHandler() {
206 dumpExceptionStack("Unexpected exception");
212 void installHandlers() {
215 origTerminate = std::set_terminate(terminateHandler);
216 origUnexpected = std::set_unexpected(unexpectedHandler);
222 } // namespace exception_tracer