impr: Only add stacktrace to exceptions thrown in main thread

This commit is contained in:
WerWolv
2025-12-02 20:00:19 +01:00
parent 5c890e710e
commit ed32439645
3 changed files with 16 additions and 2 deletions

View File

@@ -7,5 +7,6 @@
namespace hex::trace {
std::optional<StackTraceResult> getLastExceptionStackTrace();
void enableExceptionCaptureForCurrentThread();
}

View File

@@ -3,6 +3,8 @@
namespace hex::trace {
static std::optional<StackTraceResult> s_lastExceptionStackTrace;
static thread_local bool s_threadExceptionCaptureEnabled = false;
std::optional<StackTraceResult> getLastExceptionStackTrace() {
if (!s_lastExceptionStackTrace.has_value())
return std::nullopt;
@@ -13,6 +15,10 @@ namespace hex::trace {
return result;
}
void enableExceptionCaptureForCurrentThread() {
s_threadExceptionCaptureEnabled = true;
}
}
#if defined(HEX_WRAP_CXA_THROW)
@@ -21,7 +27,9 @@ namespace hex::trace {
[[noreturn]] void __real___cxa_throw(void* thrownException, void* type, void (*destructor)(void*));
[[noreturn]] void __wrap___cxa_throw(void* thrownException, void* type, void (*destructor)(void*)) {
hex::trace::s_lastExceptionStackTrace = hex::trace::getStackTrace();
if (hex::trace::s_threadExceptionCaptureEnabled)
hex::trace::s_lastExceptionStackTrace = hex::trace::getStackTrace();
__real___cxa_throw(thrownException, type, destructor);
}