24

Does anyone know a way to go from a pthread_t to what GDB displays with info threads?

So I have:

(gdb) info threads
  37 Thread 22887  0xb7704422 in __kernel_vsyscall ()
  36 Thread 22926  0xb7704422 in __kernel_vsyscall ()
  35 Thread 22925  0xb7704422 in __kernel_vsyscall ()
  34 Thread 22924  0xb7704422 in __kernel_vsyscall ()
  33 Thread 22922  0xb7704422 in __kernel_vsyscall ()
  32 Thread 22921  0xb7704422 in __kernel_vsyscall ()

(gdb) p m_messageQueue->m_creationThread
$3 = 2694822768
(gdb) p/x m_messageQueue->m_creationThread
$4 = 0xa09fbb70

Does anyone know how I figure out which thread this is? It would appear to be 22768, but none of my threads go that low.

Steven Behnke
  • 3,336
  • 3
  • 26
  • 34
  • I was about to ask the same thing.. but my problem is worse - I have a need to recover pthread_id first from context (it's an embedded library running in other process thread.. ew) – kagali-san Jul 04 '11 at 13:58

2 Answers2

29

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
Employed Russian
  • 199,314
  • 34
  • 295
  • 362
  • gdb 6.8-27 prints: `* 1 process 22749 0x000000385c8cd372 in select () from /lib64/libc.so.6` gdb 7.0.1 prints: `* 1 Thread 22749 0x000000385c8cd372 in select () from /lib64/libc.so.6` gdb 7.2-55.1 prints: `* 1 Thread 0x40c68940 (LWP 22749) 0x000000385c8cd372 in select () from /lib64/libc.so.6` So the `GDB 7.0` statement is a bit fuzzy. – Vlad Dec 01 '16 at 04:40
  • If you have a lot of threads, you can then use `thread find 0xf7655b90` to give you something like: Thread 3 has target id 'Thread 0xf7655b90 (LWP 25344)' – thejinx0r Jan 11 '22 at 17:45
8

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.