Add support for user-provided dbghelp locks.

This commit is contained in:
Bartosz Taudul 2020-10-28 20:04:37 +01:00
parent 7e18540f9c
commit 3d2ff4ffd1

View File

@ -30,6 +30,21 @@
# include <cxxabi.h> # include <cxxabi.h>
#endif #endif
#ifdef TRACY_DBGHELP_LOCK
# include "TracyProfiler.hpp"
# define DBGHELP_INIT TracyConcat( TRACY_DBGHELP_LOCK, Init() )
# define DBGHELP_LOCK TracyConcat( TRACY_DBGHELP_LOCK, Lock() );
# define DBGHELP_UNLOCK TracyConcat( TRACY_DBGHELP_LOCK, Unlock() );
extern "C"
{
void DBGHELP_INIT;
void DBGHELP_LOCK;
void DBGHELP_UNLOCK;
};
#endif
namespace tracy namespace tracy
{ {
@ -94,6 +109,11 @@ void InitCallstack()
{ {
RtlWalkFrameChain = (t_RtlWalkFrameChain)GetProcAddress( GetModuleHandleA( "ntdll.dll" ), "RtlWalkFrameChain" ); RtlWalkFrameChain = (t_RtlWalkFrameChain)GetProcAddress( GetModuleHandleA( "ntdll.dll" ), "RtlWalkFrameChain" );
#ifdef TRACY_DBGHELP_LOCK
DBGHELP_INIT;
DBGHELP_LOCK;
#endif
SymInitialize( GetCurrentProcess(), nullptr, true ); SymInitialize( GetCurrentProcess(), nullptr, true );
SymSetOptions( SYMOPT_LOAD_LINES ); SymSetOptions( SYMOPT_LOAD_LINES );
@ -135,6 +155,10 @@ void InitCallstack()
} }
} }
#endif #endif
#ifdef TRACY_DBGHELP_LOCK
DBGHELP_UNLOCK;
#endif
} }
TRACY_API uintptr_t* CallTrace( int depth ) TRACY_API uintptr_t* CallTrace( int depth )
@ -155,6 +179,9 @@ const char* DecodeCallstackPtrFast( uint64_t ptr )
si->SizeOfStruct = sizeof( SYMBOL_INFO ); si->SizeOfStruct = sizeof( SYMBOL_INFO );
si->MaxNameLen = MaxNameSize; si->MaxNameLen = MaxNameSize;
#ifdef TRACY_DBGHELP_LOCK
DBGHELP_LOCK;
#endif
if( SymFromAddr( proc, ptr, nullptr, si ) == 0 ) if( SymFromAddr( proc, ptr, nullptr, si ) == 0 )
{ {
*ret = '\0'; *ret = '\0';
@ -164,6 +191,9 @@ const char* DecodeCallstackPtrFast( uint64_t ptr )
memcpy( ret, si->Name, si->NameLen ); memcpy( ret, si->Name, si->NameLen );
ret[si->NameLen] = '\0'; ret[si->NameLen] = '\0';
} }
#ifdef TRACY_DBGHELP_LOCK
DBGHELP_UNLOCK;
#endif
return ret; return ret;
} }
@ -228,7 +258,14 @@ CallstackSymbolData DecodeSymbolAddress( uint64_t ptr )
IMAGEHLP_LINE64 line; IMAGEHLP_LINE64 line;
DWORD displacement = 0; DWORD displacement = 0;
line.SizeOfStruct = sizeof(IMAGEHLP_LINE64); line.SizeOfStruct = sizeof(IMAGEHLP_LINE64);
if( SymGetLineFromAddr64( GetCurrentProcess(), ptr, &displacement, &line ) == 0 ) #ifdef TRACY_DBGHELP_LOCK
DBGHELP_LOCK;
#endif
const auto res = SymGetLineFromAddr64( GetCurrentProcess(), ptr, &displacement, &line );
#ifdef TRACY_DBGHELP_LOCK
DBGHELP_UNLOCK;
#endif
if( res == 0 )
{ {
sym.file = "[unknown]"; sym.file = "[unknown]";
sym.line = 0; sym.line = 0;
@ -252,6 +289,9 @@ CallstackSymbolData DecodeCodeAddress( uint64_t ptr )
DWORD displacement = 0; DWORD displacement = 0;
line.SizeOfStruct = sizeof(IMAGEHLP_LINE64); line.SizeOfStruct = sizeof(IMAGEHLP_LINE64);
#ifdef TRACY_DBGHELP_LOCK
DBGHELP_LOCK;
#endif
#ifndef __CYGWIN__ #ifndef __CYGWIN__
DWORD inlineNum = SymAddrIncludeInlineTrace( proc, ptr ); DWORD inlineNum = SymAddrIncludeInlineTrace( proc, ptr );
DWORD ctx = 0; DWORD ctx = 0;
@ -281,6 +321,9 @@ CallstackSymbolData DecodeCodeAddress( uint64_t ptr )
sym.line = line.LineNumber; sym.line = line.LineNumber;
} }
} }
#ifdef TRACY_DBGHELP_LOCK
DBGHELP_UNLOCK;
#endif
sym.needFree = false; sym.needFree = false;
return sym; return sym;
} }
@ -289,6 +332,9 @@ CallstackEntryData DecodeCallstackPtr( uint64_t ptr )
{ {
int write; int write;
const auto proc = GetCurrentProcess(); const auto proc = GetCurrentProcess();
#ifdef TRACY_DBGHELP_LOCK
DBGHELP_LOCK;
#endif
#ifndef __CYGWIN__ #ifndef __CYGWIN__
DWORD inlineNum = SymAddrIncludeInlineTrace( proc, ptr ); DWORD inlineNum = SymAddrIncludeInlineTrace( proc, ptr );
if( inlineNum > MaxCbTrace - 1 ) inlineNum = MaxCbTrace - 1; if( inlineNum > MaxCbTrace - 1 ) inlineNum = MaxCbTrace - 1;
@ -383,6 +429,9 @@ CallstackEntryData DecodeCallstackPtr( uint64_t ptr )
} }
} }
#endif #endif
#ifdef TRACY_DBGHELP_LOCK
DBGHELP_UNLOCK;
#endif
return { cb_data, uint8_t( cb_num ), moduleName }; return { cb_data, uint8_t( cb_num ), moduleName };
} }