Commit c2281fce authored by Giuseppe Ottaviano's avatar Giuseppe Ottaviano Committed by Facebook Github Bot

Expose active exceptions stack in ExceptionStackTraceLib

Summary: It can be useful for diagnostics to print the stack traces of thrown exceptions that are not yet handled, for example if a fatal error happens during unwinding.

Reviewed By: philippv, luciang

Differential Revision: D19191089

fbshipit-source-id: dae8cc0a095570bfe7d6c4c7ef2b5dc08aa9f1f9
parent f519c88f
......@@ -30,13 +30,16 @@ namespace {
// trace at all because in could be bogus
FOLLY_TLS bool invalid;
FOLLY_TLS StackTraceStack activeExceptions;
FOLLY_TLS StackTraceStack uncaughtExceptions;
FOLLY_TLS StackTraceStack caughtExceptions;
} // namespace
// This function is exported and may be found via dlsym(RTLD_NEXT, ...)
extern "C" StackTraceStack* getExceptionStackTraceStack() {
// These functions are exported and may be found via dlsym(RTLD_NEXT, ...)
extern "C" StackTraceStack* getUncaughtExceptionStackTraceStack() {
return invalid ? nullptr : &uncaughtExceptions;
}
extern "C" StackTraceStack* getCaughtExceptionStackTraceStack() {
return invalid ? nullptr : &caughtExceptions;
}
......@@ -45,8 +48,8 @@ namespace {
void addActiveException() {
// Capture stack trace
if (!invalid) {
if (!activeExceptions.pushCurrent()) {
activeExceptions.clear();
if (!uncaughtExceptions.pushCurrent()) {
uncaughtExceptions.clear();
caughtExceptions.clear();
invalid = true;
}
......@@ -72,11 +75,11 @@ struct Initializer {
});
registerCxaBeginCatchCallback([](void*) noexcept {
moveTopException(activeExceptions, caughtExceptions);
moveTopException(uncaughtExceptions, caughtExceptions);
});
registerCxaRethrowCallback([]() noexcept {
moveTopException(caughtExceptions, activeExceptions);
moveTopException(caughtExceptions, uncaughtExceptions);
});
registerCxaEndCatchCallback([]() noexcept {
......@@ -95,7 +98,7 @@ struct Initializer {
// For Lua interop, we see the handlerCount = 0
if ((top->handlerCount == 1) || (top->handlerCount == 0)) {
if (!caughtExceptions.pop()) {
activeExceptions.clear();
uncaughtExceptions.clear();
invalid = true;
}
}
......
......@@ -37,9 +37,10 @@ using namespace ::folly::symbolizer;
using namespace __cxxabiv1;
extern "C" {
StackTraceStack* getExceptionStackTraceStack(void) __attribute__((__weak__));
typedef StackTraceStack* (*GetExceptionStackTraceStackType)();
GetExceptionStackTraceStackType getExceptionStackTraceStackFn;
StackTraceStack* getCaughtExceptionStackTraceStack(void)
__attribute__((__weak__));
typedef StackTraceStack* (*GetCaughtExceptionStackTraceStackType)();
GetCaughtExceptionStackTraceStackType getCaughtExceptionStackTraceStackFn;
}
} // namespace
......@@ -130,13 +131,14 @@ bool isAbiCppException(const __cxa_exception* exc) {
std::vector<ExceptionInfo> getCurrentExceptions() {
struct Once {
Once() {
// See if linked in with us (getExceptionStackTraceStack is weak)
getExceptionStackTraceStackFn = getExceptionStackTraceStack;
// See if linked in with us (getCaughtExceptionStackTraceStack is weak)
getCaughtExceptionStackTraceStackFn = getCaughtExceptionStackTraceStack;
if (!getExceptionStackTraceStackFn) {
if (!getCaughtExceptionStackTraceStackFn) {
// Nope, see if it's in a shared library
getExceptionStackTraceStackFn = (GetExceptionStackTraceStackType)dlsym(
RTLD_NEXT, "getExceptionStackTraceStack");
getCaughtExceptionStackTraceStackFn =
(GetCaughtExceptionStackTraceStackType)dlsym(
RTLD_NEXT, "getCaughtExceptionStackTraceStack");
}
}
};
......@@ -149,14 +151,14 @@ std::vector<ExceptionInfo> getCurrentExceptions() {
}
StackTraceStack* traceStack = nullptr;
if (!getExceptionStackTraceStackFn) {
if (!getCaughtExceptionStackTraceStackFn) {
static bool logged = false;
if (!logged) {
LOG(WARNING)
<< "Exception tracer library not linked, stack traces not available";
logged = true;
}
} else if ((traceStack = getExceptionStackTraceStackFn()) == nullptr) {
} else if ((traceStack = getCaughtExceptionStackTraceStackFn()) == nullptr) {
static bool logged = false;
if (!logged) {
LOG(WARNING)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment