diff --git a/lib/trace/include/hex/trace/exceptions.hpp b/lib/trace/include/hex/trace/exceptions.hpp index 9fa88bd58..96fd06d1f 100644 --- a/lib/trace/include/hex/trace/exceptions.hpp +++ b/lib/trace/include/hex/trace/exceptions.hpp @@ -7,5 +7,6 @@ namespace hex::trace { std::optional getLastExceptionStackTrace(); + void enableExceptionCaptureForCurrentThread(); } \ No newline at end of file diff --git a/lib/trace/source/exceptions.cpp b/lib/trace/source/exceptions.cpp index dc78e6e20..d963abc92 100644 --- a/lib/trace/source/exceptions.cpp +++ b/lib/trace/source/exceptions.cpp @@ -3,6 +3,8 @@ namespace hex::trace { static std::optional s_lastExceptionStackTrace; + static thread_local bool s_threadExceptionCaptureEnabled = false; + std::optional 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); } diff --git a/main/gui/source/main.cpp b/main/gui/source/main.cpp index b8031a8ce..a7137323a 100644 --- a/main/gui/source/main.cpp +++ b/main/gui/source/main.cpp @@ -12,6 +12,7 @@ #include #include #include +#include namespace hex::init { @@ -41,17 +42,21 @@ int main(int argc, char **argv) { // Setup crash handlers right away to catch crashes as early as possible crash::setupCrashHandlers(); + // Enable exception tracing on the main thread + trace::enableExceptionCaptureForCurrentThread(); + // Run platform-specific initialization code Window::initNative(); // Setup messaging system to allow sending commands to the main ImHex instance - hex::messaging::setupMessaging(); + messaging::setupMessaging(); // Handle command line arguments if any have been passed if (argc > 1) { init::runCommandLine(argc, argv); } + // Log some system information to aid debugging when users share their logs log::info("Welcome to ImHex {}!", ImHexApi::System::getImHexVersion().get()); log::info("Compiled using commit {}@{}", ImHexApi::System::getCommitBranch(), ImHexApi::System::getCommitHash());