From 819ef2a82b2f079891ba17fb423a375835f3f648 Mon Sep 17 00:00:00 2001 From: Bartosz Taudul Date: Fri, 16 Aug 2019 21:00:42 +0200 Subject: [PATCH] External process/thread name retrieval on linux. --- client/TracySysTrace.cpp | 54 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 52 insertions(+), 2 deletions(-) diff --git a/client/TracySysTrace.cpp b/client/TracySysTrace.cpp index 44df0f46..109d50eb 100644 --- a/client/TracySysTrace.cpp +++ b/client/TracySysTrace.cpp @@ -213,6 +213,7 @@ void SysTraceSendExternalName( uint64_t thread ) # include # include # include +# include # include # include # include @@ -393,8 +394,57 @@ void SysTraceWorker( void* ptr ) void SysTraceSendExternalName( uint64_t thread ) { - // TODO - GetProfiler().SendString( thread, "???", QueueType::ExternalThreadName ); + FILE* f; + char fn[256]; + sprintf( fn, "/proc/%" PRIu64 "/comm", thread ); + f = fopen( fn, "rb" ); + if( f ) + { + char buf[256]; + const auto sz = fread( buf, 1, 256, f ); + if( sz > 0 && buf[sz-1] == '\n' ) buf[sz-1] = '\0'; + GetProfiler().SendString( thread, buf, QueueType::ExternalThreadName ); + fclose( f ); + } + else + { + GetProfiler().SendString( thread, "???", QueueType::ExternalThreadName ); + } + + sprintf( fn, "/proc/%" PRIu64 "/status", thread ); + f = fopen( fn, "rb" ); + if( f ) + { + int pid = -1; + size_t lsz = 1024; + auto line = (char*)malloc( lsz ); + for(;;) + { + auto rd = getline( &line, &lsz, f ); + if( rd <= 0 ) break; + if( memcmp( "Tgid:\t", line, 6 ) == 0 ) + { + pid = atoi( line + 6 ); + break; + } + } + free( line ); + fclose( f ); + if( pid >= 0 ) + { + sprintf( fn, "/proc/%i/comm", pid ); + f = fopen( fn, "rb" ); + if( f ) + { + char buf[256]; + const auto sz = fread( buf, 1, 256, f ); + if( sz > 0 && buf[sz-1] == '\n' ) buf[sz-1] = '\0'; + GetProfiler().SendString( thread, buf, QueueType::ExternalName ); + fclose( f ); + return; + } + } + } GetProfiler().SendString( thread, "???", QueueType::ExternalName ); }