Add a way to override profiled program name and PID.

This commit is contained in:
Bartosz Taudul
2026-03-31 00:41:20 +02:00
parent 53439ea5e2
commit 739123d005
2 changed files with 28 additions and 2 deletions

View File

@@ -425,8 +425,13 @@ static int64_t SetupHwTimer()
}
#endif
uint32_t ___tracy_magic_pid_override = 0;
char ___tracy_magic_process_name[64] = {};
static const char* GetProcessName()
{
if( *___tracy_magic_process_name != 0 ) return ___tracy_magic_process_name;
const char* processName = "unknown";
#ifdef _WIN32
static char buf[_MAX_PATH];
@@ -750,6 +755,8 @@ static const char* GetHostInfo()
static uint64_t GetPid()
{
if( ___tracy_magic_pid_override != 0 ) return uint64_t( ___tracy_magic_pid_override );
#if defined _WIN32
return uint64_t( GetCurrentProcessId() );
#else
@@ -4187,6 +4194,16 @@ void Profiler::HandleParameter( uint64_t payload )
void Profiler::HandleSymbolCodeQuery( uint64_t symbol, uint32_t size )
{
#ifdef __linux__
// When profiling an external process, symbol addresses are ELF virtual
// addresses, not pointers in the monitor's address space. We cannot
// read code bytes directly.
if( ___tracy_magic_pid_override != 0 )
{
AckSymbolCodeNotAvailable();
return;
}
#endif
if( symbol >> 63 != 0 )
{
QueueKernelCode( symbol, size );

View File

@@ -412,6 +412,8 @@ static int s_ctxBufferIdx = 0;
static RingBuffer* s_ring = nullptr;
extern uint32_t ___tracy_magic_pid_override;
static const int ThreadHashSize = 4 * 1024;
static uint32_t s_threadHash[ThreadHashSize] = {};
@@ -423,7 +425,14 @@ static bool CurrentProcOwnsThread( uint32_t tid )
if( hv == -tid ) return false;
char path[256];
sprintf( path, "/proc/self/task/%d", tid );
if( ___tracy_magic_pid_override != 0 )
{
sprintf( path, "/proc/%d/task/%d", (int)___tracy_magic_pid_override, tid );
}
else
{
sprintf( path, "/proc/self/task/%d", tid );
}
struct stat st;
if( stat( path, &st ) == 0 )
{
@@ -648,7 +657,7 @@ bool SysTraceStart( int64_t& samplingPeriod )
#endif
samplingPeriod = GetSamplingPeriod();
uint32_t currentPid = (uint32_t)getpid();
uint32_t currentPid = ___tracy_magic_pid_override != 0 ? ___tracy_magic_pid_override : (uint32_t)getpid();
s_numCpus = (int)std::thread::hardware_concurrency();