Skip to content

Commit 1f6531f

Browse files
GabrielSiliceumLectem
authored andcommitted
Add faster module caching fallback by using GetModuleHandleExA
1 parent bd3660e commit 1f6531f

File tree

1 file changed

+15
-17
lines changed

1 file changed

+15
-17
lines changed

‎public/client/TracyCallstack.cpp‎

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -555,30 +555,28 @@ ModuleNameAndBaseAddress GetModuleNameAndPrepareSymbols( uint64_t addr )
555555
}
556556
}
557557

558-
HMODULE mod[1024];
559-
DWORD needed;
560558
HANDLE proc = GetCurrentProcess();
559+
// Do not use FreeLibrary because we set the flag GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT
560+
// see https://learn.microsoft.com/en-us/windows/win32/api/libloaderapi/nf-libloaderapi-getmodulehandleexa to get more information
561+
constexpr DWORD flag = GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT;
562+
HMODULE mod = NULL;
561563

562564
InitRpmalloc();
563-
if( EnumProcessModules( proc, mod, sizeof( mod ), &needed ) != 0 )
565+
if( GetModuleHandleExA( flag, (char*)addr, &mod ) != 0 )
564566
{
565-
const auto sz = needed / sizeof( HMODULE );
566-
for( size_t i=0; i<sz; i++ )
567+
MODULEINFO info;
568+
if( GetModuleInformation( proc, mod, &info, sizeof( info ) ) != 0 )
567569
{
568-
MODULEINFO info;
569-
if( GetModuleInformation( proc, mod[i], &info, sizeof( info ) ) != 0 )
570+
const auto base = uint64_t( info.lpBaseOfDll );
571+
if( addr >= base && addr < ( base + info.SizeOfImage ) )
570572
{
571-
const auto base = uint64_t( info.lpBaseOfDll );
572-
if( addr >= base && addr < base + info.SizeOfImage )
573+
char name[1024];
574+
const auto nameLength = GetModuleFileNameA( mod, name, sizeof( name ) );
575+
if( nameLength > 0 )
573576
{
574-
char name[1024];
575-
const auto nameLength = GetModuleFileNameA( mod[i], name, 1021 );
576-
if( nameLength > 0 )
577-
{
578-
// since this is the first time we encounter this module, load its symbols (needed for modules loaded after SymInitialize)
579-
ModuleCache* cachedModule = LoadSymbolsForModuleAndCache( name, nameLength, (DWORD64)info.lpBaseOfDll, info.SizeOfImage );
580-
return ModuleNameAndBaseAddress{ cachedModule->name, cachedModule->start };
581-
}
577+
// since this is the first time we encounter this module, load its symbols (needed for modules loaded after SymInitialize)
578+
ModuleCache* cachedModule = LoadSymbolsForModuleAndCache( name, nameLength, (DWORD64)info.lpBaseOfDll, info.SizeOfImage );
579+
return ModuleNameAndBaseAddress{ cachedModule->name, cachedModule->start };
582580
}
583581
}
584582
}

0 commit comments

Comments
 (0)