From 41bb4f58b4ddbf358008d03b4714e3bb40a488cd Mon Sep 17 00:00:00 2001 From: Jonathan Hargreaves Date: Thu, 1 Oct 2026 22:53:06 +0100 Subject: [PATCH] Automatic thread naming on Apple --- public/client/apple/TracyMach.cpp | 26 +++++--------------------- public/common/TracySystem.cpp | 22 ++++++++++++++++++++++ 2 files changed, 27 insertions(+), 21 deletions(-) diff --git a/public/client/apple/TracyMach.cpp b/public/client/apple/TracyMach.cpp index e1024610..5c01d43a 100644 --- a/public/client/apple/TracyMach.cpp +++ b/public/client/apple/TracyMach.cpp @@ -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() ); } diff --git a/public/common/TracySystem.cpp b/public/common/TracySystem.cpp index 143da227..6566a1b6 100644 --- a/public/common/TracySystem.cpp +++ b/public/common/TracySystem.cpp @@ -40,6 +40,8 @@ #elif defined __QNX__ # include # include +#elif defined __APPLE__ +# include #endif #ifdef __MINGW32__ @@ -307,6 +309,26 @@ TRACY_API const char* GetThreadName( uint32_t id ) if (pthread_getname_np(static_cast(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 );