pthread_t to gdb thread id

The value of pthread_t is not the same as that thread's system dependent thread id (in Linux gettid(2)) which you see in GDB.

AFAIK, there isn't any function to convert between the two. You need to keep track of that yourself.


New versions of GDB actually output the value of pthread_t in the info thread, making association of pthread_t with thread number trivial.

For example, using GDB 7.0:

cat t.c
#include <pthread.h>

void *fn(void *p)
{
  sleep(180);
}

int main()
{
  pthread_t pth1, pth2;
  pthread_create(&pth1, 0, fn, 0);
  pthread_create(&pth2, 0, fn, 0);
  pthread_join(pth1, 0);
  return 0;
}

gcc -g -m32 -pthread t.c && gdb -q ../a.out

(gdb) r
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib64/libthread_db.so.1".
[New Thread 0xf7e56b90 (LWP 25343)]
[New Thread 0xf7655b90 (LWP 25344)]

Program received signal SIGINT, Interrupt.
0xffffe405 in __kernel_vsyscall ()
(gdb) info thread
  3 Thread 0xf7655b90 (LWP 25344)  0xffffe405 in __kernel_vsyscall ()
  2 Thread 0xf7e56b90 (LWP 25343)  0xffffe405 in __kernel_vsyscall ()
* 1 Thread 0xf7e576b0 (LWP 25338)  0xffffe405 in __kernel_vsyscall ()
(gdb) up 2
#2  0x080484e2 in main () at t.c:13
13    pthread_join(pth1, 0);
(gdb) p/x pth1
$1 = 0xf7e56b90  ## this is thread #2 above
(gdb) p/x pth2
$2 = 0xf7655b90  ## this is thread #3 above

Tags:

C

Gdb

Pthreads