tags:

views:

690

answers:

2

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.

+2  A: 

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.

jbcreix
This is correct. See answer at *http://stackoverflow.com/questions/558469/how-do-i-get-a-thread-id-from-an-arbitrary-pthread-t/558815#558815* for additional info.
jschmier
Ah, thank you. I'll grab it where I care about which thread things occur on.
Steven Behnke
Actually it seems this is deprecated and I have to use syscall(SYS_gettid); instead.
Steven Behnke
+3  A: 

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