From 1237946a8daa0ebdcee5d2eb1dc2274cbf846c27 Mon Sep 17 00:00:00 2001 From: Wojtek Figat Date: Wed, 16 Jun 2021 23:39:05 +0200 Subject: [PATCH] Fix synchronization for dbg symbols on Windows --- .../Platform/Windows/WindowsPlatform.cpp | 39 ++++++++++++++----- Source/ThirdParty/tracy/tracy.Build.cs | 2 + 2 files changed, 32 insertions(+), 9 deletions(-) diff --git a/Source/Engine/Platform/Windows/WindowsPlatform.cpp b/Source/Engine/Platform/Windows/WindowsPlatform.cpp index 89e050267..52b0bdaa3 100644 --- a/Source/Engine/Platform/Windows/WindowsPlatform.cpp +++ b/Source/Engine/Platform/Windows/WindowsPlatform.cpp @@ -29,6 +29,28 @@ const Char* WindowsPlatform::ApplicationWindowClass = TEXT("FlaxWindow"); void* WindowsPlatform::Instance = nullptr; +#if CRASH_LOG_ENABLE || TRACY_ENABLE +// Lock for symbols list, shared with Tracy +extern "C" { +static HANDLE dbgHelpLock; + +void DbgHelpInit() +{ + dbgHelpLock = CreateMutexW(nullptr, FALSE, nullptr); +} + +void DbgHelpLock() +{ + WaitForSingleObject(dbgHelpLock, INFINITE); +} + +void DbgHelpUnlock() +{ + ReleaseMutex(dbgHelpLock); +} +} +#endif + namespace { String UserLocale, ComputerName, UserName, WindowsName; @@ -39,7 +61,6 @@ namespace int32 VersionBuild = 0; int32 SystemDpi = 96; #if CRASH_LOG_ENABLE - CriticalSection SymLocker; #if TRACY_ENABLE bool SymInitialized = true; #else @@ -501,7 +522,7 @@ void WindowsPlatform::PreInit(void* hInstance) #if CRASH_LOG_ENABLE TCHAR buffer[MAX_PATH] = { 0 }; - SymLocker.Lock(); + DbgHelpLock(); if (::GetModuleFileNameW(::GetModuleHandleW(nullptr), buffer, MAX_PATH)) SymbolsPath.Add(StringUtils::GetDirectoryName(buffer)); if (::GetEnvironmentVariableW(TEXT("_NT_SYMBOL_PATH"), buffer, MAX_PATH)) @@ -510,7 +531,7 @@ void WindowsPlatform::PreInit(void* hInstance) options |= SYMOPT_LOAD_LINES | SYMOPT_FAIL_CRITICAL_ERRORS | SYMOPT_DEFERRED_LOADS | SYMOPT_EXACT_SYMBOLS; SymSetOptions(options); OnSymbolsPathModified(); - SymLocker.Unlock(); + DbgHelpUnlock(); #endif GetWindowsVersion(WindowsName, VersionMajor, VersionMinor, VersionBuild); @@ -654,7 +675,7 @@ void WindowsPlatform::BeforeExit() void WindowsPlatform::Exit() { #if CRASH_LOG_ENABLE - SymLocker.Lock(); + DbgHelpLock(); #if !TRACY_ENABLE if (SymInitialized) { @@ -663,7 +684,7 @@ void WindowsPlatform::Exit() } #endif SymbolsPath.Resize(0); - SymLocker.Unlock(); + DbgHelpUnlock(); #endif // Unregister app class @@ -1170,13 +1191,13 @@ void* WindowsPlatform::LoadLibrary(const Char* filename) #if CRASH_LOG_ENABLE // Refresh modules info during next stack trace collecting to have valid debug symbols information - SymLocker.Lock(); + DbgHelpLock(); if (folder.HasChars() && !SymbolsPath.Contains(folder)) { SymbolsPath.Add(folder); OnSymbolsPathModified(); } - SymLocker.Unlock(); + DbgHelpUnlock(); #endif return handle; @@ -1186,7 +1207,7 @@ Array WindowsPlatform::GetStackFrames(int32 skipCount, { Array result; #if CRASH_LOG_ENABLE - SymLocker.Lock(); + DbgHelpLock(); // Initialize HANDLE process = GetCurrentProcess(); @@ -1310,7 +1331,7 @@ Array WindowsPlatform::GetStackFrames(int32 skipCount, } } - SymLocker.Unlock(); + DbgHelpUnlock(); #endif return result; } diff --git a/Source/ThirdParty/tracy/tracy.Build.cs b/Source/ThirdParty/tracy/tracy.Build.cs index aa10c514f..abcc83da4 100644 --- a/Source/ThirdParty/tracy/tracy.Build.cs +++ b/Source/ThirdParty/tracy/tracy.Build.cs @@ -33,6 +33,8 @@ public class tracy : ThirdPartyModule options.SourceFiles.Add(Path.Combine(FolderPath, "TracyClient.cpp")); options.PublicDefinitions.Add("TRACY_ENABLE"); + if (options.Platform.Target == TargetPlatform.Windows) + options.PrivateDefinitions.Add("TRACY_DBGHELP_LOCK=DbgHelp"); } ///