Associating SBThread with a native thread


we are currently trying work around some issues with rapid suspend/resume. We essentially setup a thread in the inferior with which we communicate via TCP to get informed of events such as thread start/stop without having to set breakpoints and stopping the inferior.

Eventually we need to associate the native threads with the SBThread we get from LLDB’s API. SBThread has an id and an index, but those don’t seem to map to anything on the inferior side (e.g. the pthread id of the thread).

Is there a way to perform this association?


We use the globally unique thread ID:

static uint64_t
GetGloballyUniqueThreadIDForMachPortID (thread_t mach_port_id)
    kern_return_t kr;
    thread_identifier_info_data_t tident;
    mach_msg_type_number_t tident_count = THREAD_IDENTIFIER_INFO_COUNT;
    kr = thread_info (mach_port_id, THREAD_IDENTIFIER_INFO, (thread_info_t) &tident, &tident_count);
    if (kr != KERN_SUCCESS)
        return mach_port_id;
    return tident.thread_id;

this is the best way because mach ports can be re-used, but the globally unique thread ID is always guaranteed to be unique.

As Greg said, on Mac OS X each thread created by the system since the last reboot is assigned an incrementing number, the globally unique thread id. That's what lldb and sample use as the thread identifiers -- pthread_t values may be reused by the system during a process lifetime as threads are created and destroyed.

You can get the globally unique thread id for the current thread via the nonportable pthread_threadid_np() call. e.g.

(lldb) thread info
thread #1: tid = 0x088f, 0x00007fff856b352e libsystem_kernel.dylib`mach_msg_trap + 10, queue = '', stop reason = signal SIGSTOP

(lldb) p/x (void*)malloc(8)
(void *) $2 = 0x0000620000007a00

(lldb) p/d (uint64_t) pthread_threadid_np(0, $2)
(uint64_t) $3 = 0

(lldb) x/gx $2
0x620000007a00: 0x000000000000088f


Awesome, thanks for the help guys!