pthread_t到gdb线程id

时间:2010-02-02 22:09:30

标签: c gdb pthreads

有没有人知道从pthread_t到GDB用信息线程显示的方法?

所以我有:

(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

有谁知道我是如何弄清楚这是哪个线程的?它似乎是22768,但我的线程都没有那么低。

2 个答案:

答案 0 :(得分:23)

GDB的新版本实际上在pthread_t中输出info thread的值,使pthread_t与线程编号的关联变得微不足道。

例如,使用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

答案 1 :(得分:8)

pthread_t的值与您在GDB中看到的该线程的系统相关线程ID(在Linux gettid(2)中)不同。

AFAIK,两者之间没有任何转换功能。你需要自己跟踪它。