2 * Copyright 2017 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)(void);
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")
67 size_t frameCount = info.frames.size();
69 // Skip our own internal frames
70 static constexpr size_t kInternalFramesNumber = 3;
71 if (frameCount > kInternalFramesNumber) {
72 auto addresses = info.frames.data() + kInternalFramesNumber;
73 frameCount -= kInternalFramesNumber;
75 std::vector<SymbolizedFrame> frames;
76 frames.resize(frameCount);
78 Symbolizer symbolizer(
79 (options & SymbolizePrinter::NO_FILE_AND_LINE)
80 ? Dwarf::LocationInfoMode::DISABLED
81 : Symbolizer::kDefaultLocationInfoMode);
82 symbolizer.symbolize(addresses, frames.data(), frameCount);
84 OStreamSymbolizePrinter osp(out, options);
85 osp.println(addresses, frames.data(), frameCount);
87 } catch (const std::exception& e) {
88 out << "\n !! caught " << folly::exceptionStr(e) << "\n";
90 out << "\n !!! caught unexpected exception\n";
97 * Is this a standard C++ ABI exception?
99 * Dependent exceptions (thrown via std::rethrow_exception) aren't --
100 * exc doesn't actually point to a __cxa_exception structure, but
101 * the offset of unwindHeader is correct, so exc->unwindHeader actually
102 * returns a _Unwind_Exception object. Yeah, it's ugly like that.
104 bool isAbiCppException(const __cxa_exception* exc) {
105 // The least significant four bytes must be "C++\0"
106 static const uint64_t cppClass =
107 ((uint64_t)'C' << 24) |
108 ((uint64_t)'+' << 16) |
109 ((uint64_t)'+' << 8);
110 return (exc->unwindHeader.exception_class & 0xffffffff) == cppClass;
115 std::vector<ExceptionInfo> getCurrentExceptions() {
118 // See if linked in with us (getExceptionStackTraceStack is weak)
119 getExceptionStackTraceStackFn = getExceptionStackTraceStack;
121 if (!getExceptionStackTraceStackFn) {
122 // Nope, see if it's in a shared library
123 getExceptionStackTraceStackFn =
124 (GetExceptionStackTraceStackType)dlsym(
125 RTLD_NEXT, "getExceptionStackTraceStack");
131 std::vector<ExceptionInfo> exceptions;
132 auto currentException = __cxa_get_globals()->caughtExceptions;
133 if (!currentException) {
137 StackTraceStack* traceStack = nullptr;
138 if (!getExceptionStackTraceStackFn) {
139 static bool logged = false;
142 << "Exception tracer library not linked, stack traces not available";
145 } else if ((traceStack = getExceptionStackTraceStackFn()) == nullptr) {
146 static bool logged = false;
149 << "Exception stack trace invalid, stack traces not available";
154 StackTrace* trace = traceStack ? traceStack->top() : nullptr;
155 while (currentException) {
157 // Dependent exceptions (thrown via std::rethrow_exception) aren't
158 // standard ABI __cxa_exception objects, and are correctly labeled as
159 // such in the exception_class field. We could try to extract the
160 // primary exception type in horribly hacky ways, but, for now, nullptr.
162 isAbiCppException(currentException) ?
163 currentException->exceptionType :
167 LOG_IF(DFATAL, !trace)
168 << "Invalid trace stack for exception of type: "
169 << (info.type ? folly::demangle(*info.type) : "null");
176 trace->addresses, trace->addresses + trace->frameCount);
177 trace = traceStack->next(trace);
179 currentException = currentException->nextException;
180 exceptions.push_back(std::move(info));
183 LOG_IF(DFATAL, trace) << "Invalid trace stack!";
190 std::terminate_handler origTerminate = abort;
191 std::unexpected_handler origUnexpected = abort;
193 void dumpExceptionStack(const char* prefix) {
194 auto exceptions = getCurrentExceptions();
195 if (exceptions.empty()) {
198 LOG(ERROR) << prefix << ", exception stack follows";
199 for (auto& exc : exceptions) {
200 LOG(ERROR) << exc << "\n";
202 LOG(ERROR) << "exception stack complete";
205 void terminateHandler() {
206 dumpExceptionStack("terminate() called");
210 void unexpectedHandler() {
211 dumpExceptionStack("Unexpected exception");
217 void installHandlers() {
220 origTerminate = std::set_terminate(terminateHandler);
221 origUnexpected = std::set_unexpected(unexpectedHandler);
227 } // namespace exception_tracer