#include <exception>
#include <folly/debugging/exception_tracer/ExceptionAbi.h>
#include <folly/debugging/exception_tracer/ExceptionTracer.h>
#include <folly/debugging/exception_tracer/ExceptionTracerLib.h>
#include <folly/debugging/exception_tracer/StackTrace.h>
#include <folly/experimental/symbolizer/Symbolizer.h>
#if FOLLY_HAVE_ELF && FOLLY_HAVE_DWARF
#if defined(__GLIBCXX__)
using namespace folly::exception_tracer;
namespace {
thread_local bool invalid;
thread_local StackTraceStack uncaughtExceptions;
thread_local StackTraceStack caughtExceptions;
}
extern "C" const StackTraceStack* getUncaughtExceptionStackTraceStack() {
return invalid ? nullptr : &uncaughtExceptions;
}
extern "C" const StackTraceStack* getCaughtExceptionStackTraceStack() {
return invalid ? nullptr : &caughtExceptions;
}
namespace {
void addActiveException() {
if (!invalid) {
if (!uncaughtExceptions.pushCurrent()) {
uncaughtExceptions.clear();
caughtExceptions.clear();
invalid = true;
}
}
}
void moveTopException(StackTraceStack& from, StackTraceStack& to) {
if (invalid) {
return;
}
if (!to.moveTopFrom(from)) {
from.clear();
to.clear();
invalid = true;
}
}
struct Initializer {
Initializer() {
registerCxaThrowCallback(
[](void*, std::type_info*, void (**)(void*)) noexcept {
addActiveException();
});
registerCxaBeginCatchCallback([](void*) noexcept {
moveTopException(uncaughtExceptions, caughtExceptions);
});
registerCxaRethrowCallback([]() noexcept {
moveTopException(caughtExceptions, uncaughtExceptions);
});
registerCxaEndCatchCallback([]() noexcept {
if (invalid) {
return;
}
__cxxabiv1::__cxa_exception* top =
__cxxabiv1::__cxa_get_globals_fast()->caughtExceptions;
if ((top->handlerCount == 1) || (top->handlerCount == 0)) {
if (!caughtExceptions.pop()) {
uncaughtExceptions.clear();
invalid = true;
}
}
});
registerRethrowExceptionCallback(
[](std::exception_ptr) noexcept { addActiveException(); });
try {
::folly::exception_tracer::installHandlers();
} catch (...) {
}
}
};
Initializer initializer;
}
#endif
#endif