Automatic thread naming on Apple

This commit is contained in:
Jonathan Hargreaves
2026-10-01 22:53:06 +01:00
parent 275b578c1b
commit 41bb4f58b4
2 changed files with 27 additions and 21 deletions

View File

@@ -228,28 +228,12 @@ void SysTraceStop()
void SysTraceGetExternalName( uint64_t thread, const char*& threadName, const char*& name )
{
// Resolve pthread handle from the Mach port so we can query the thread name.
const mach_port_t mach_tid = (mach_port_t)thread;
thread_identifier_info_data_t idInfo;
mach_msg_type_number_t idInfoCount = THREAD_IDENTIFIER_INFO_COUNT;
if( thread_info( mach_tid, THREAD_IDENTIFIER_INFO, (thread_info_t)&idInfo, &idInfoCount ) == KERN_SUCCESS )
{
char buf[64] = {};
const pthread_t pt = (pthread_t)(uintptr_t)idInfo.thread_handle;
if( pt && pthread_getname_np( pt, buf, sizeof( buf ) ) == 0 && buf[0] != '\0' )
threadName = CopyString( buf );
else
threadName = CopyString( "???", 3 );
threadName = CopyString( GetThreadName( (uint32_t)thread ) );
TracyLfqPrepare( QueueType::TidToPid );
MemWrite( &item->tidToPid.tid, thread );
MemWrite( &item->tidToPid.pid, (uint64_t)getpid() );
TracyLfqCommit;
}
else
{
threadName = CopyString( "???", 3 );
}
TracyLfqPrepare( QueueType::TidToPid );
MemWrite( &item->tidToPid.tid, thread );
MemWrite( &item->tidToPid.pid, (uint64_t)getpid() );
TracyLfqCommit;
name = CopyStringFast( getprogname() );
}

View File

@@ -40,6 +40,8 @@
#elif defined __QNX__
# include <process.h>
# include <sys/neutrino.h>
#elif defined __APPLE__
# include <mach/mach.h>
#endif
#ifdef __MINGW32__
@@ -307,6 +309,26 @@ TRACY_API const char* GetThreadName( uint32_t id )
if (pthread_getname_np(static_cast<int>(id), qnxNameBuf, _NTO_THREAD_NAME_MAX) == 0) {
return qnxNameBuf;
};
#elif defined __APPLE__
bool found = false;
thread_act_array_t threads;
mach_msg_type_number_t threadCount;
if( task_threads( mach_task_self(), &threads, &threadCount ) == KERN_SUCCESS )
{
for( mach_msg_type_number_t i = 0; i < threadCount; i++ )
{
pthread_t pt = pthread_from_mach_thread_np( threads[i] );
uint64_t tid;
if( pt && pthread_threadid_np( pt, &tid ) == 0 && (uint32_t)tid == id )
{
found = pthread_getname_np( pt, buf, sizeof( buf ) ) == 0 && buf[0];
break;
}
}
for( mach_msg_type_number_t i = 0; i < threadCount; i++ ) mach_port_deallocate( mach_task_self(), threads[i] );
vm_deallocate( mach_task_self(), (vm_address_t)threads, sizeof( thread_t ) * threadCount );
if( found ) return buf;
}
#endif
sprintf( buf, "%" PRIu32, id );