From 5213c53bb065d40f6cfc60de2048a43d24646604 Mon Sep 17 00:00:00 2001 From: Joshua Kriegshauser Date: Tue, 24 Sep 2024 09:20:04 -0700 Subject: [PATCH] Use SetUnhandledExceptionFilter instead of vectored exceptions --- public/client/TracyProfiler.cpp | 20 ++++++++++++++------ public/client/TracyProfiler.hpp | 4 +--- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/public/client/TracyProfiler.cpp b/public/client/TracyProfiler.cpp index f7ad5c6b..fc42268b 100644 --- a/public/client/TracyProfiler.cpp +++ b/public/client/TracyProfiler.cpp @@ -1501,14 +1501,17 @@ void Profiler::InstallCrashHandler() sigaction( SIGPIPE, &crashHandler, &m_prevSignal.pipe ); sigaction( SIGBUS, &crashHandler, &m_prevSignal.bus ); sigaction( SIGABRT, &crashHandler, &m_prevSignal.abrt ); + + m_crashHandlerInstalled = true; #endif #if defined _WIN32 && !defined TRACY_UWP && !defined TRACY_NO_CRASH_HANDLER - m_exceptionHandler = AddVectoredExceptionHandler( 0, CrashFilter ); -#endif - -#ifndef TRACY_NO_CRASH_HANDLER - m_crashHandlerInstalled = true; + // We cannot use Vectored Exception handling because it catches application-wide frame-based SEH blocks. We only + // want to catch unhandled exceptions in the event that there is not already an unhandled exception filter. + if( auto prev = SetUnhandledExceptionFilter( CrashFilter ) ) + SetUnhandledExceptionFilter( prev ); // Already had a handler => put it back + else + m_crashHandlerInstalled = true; #endif } @@ -1516,7 +1519,12 @@ void Profiler::InstallCrashHandler() void Profiler::RemoveCrashHandler() { #if defined _WIN32 && !defined TRACY_UWP - if( m_crashHandlerInstalled ) RemoveVectoredExceptionHandler( m_exceptionHandler ); + if( m_crashHandlerInstalled ) + { + auto prev = SetUnhandledExceptionFilter( NULL ); + if( prev != CrashFilter ) + SetUnhandledExceptionFilter( prev ); // A different exception filter was installed over ours => put it back + } #endif #if defined __linux__ && !defined TRACY_NO_CRASH_HANDLER diff --git a/public/client/TracyProfiler.hpp b/public/client/TracyProfiler.hpp index b8b132ea..b559a71b 100644 --- a/public/client/TracyProfiler.hpp +++ b/public/client/TracyProfiler.hpp @@ -1063,9 +1063,7 @@ private: char* m_safeSendBuffer; size_t m_safeSendBufferSize; -#if defined _WIN32 - void* m_exceptionHandler; -#else +#ifndef _WIN32 int m_pipe[2]; int m_pipeBufSize; #endif