gdb对于多线程程序的调试有如下的支持:
- 线程产生通知:在产生新的线程时, gdb会给出提示信息
(gdb) r
Starting program: /root/thread
[New Thread 1073951360 (LWP 12900)]
[New Thread 1082342592 (LWP 12907)]---以下三个为新产生的线程
[New Thread 1090731072 (LWP 12908)]
[New Thread 1099119552 (LWP 12909)]
- 查看线程:使用info threads可以查看运行的线程。
(gdb) info threads
4 Thread 1099119552 (LWP 12940) 0xffffe002 in ?? ()
3 Thread 1090731072 (LWP 12939) 0xffffe002 in ?? ()
2 Thread 1082342592 (LWP 12938) 0xffffe002 in ?? ()
* 1 Thread 1073951360 (LWP 12931) main (argc=1, argv=0xbfffda04) at thread.c:21
(gdb)
注意,行首的蓝色文字为gdb分配的线程号,对线程进行切换时,使用该该号码,而不是上文标出的绿色数字。
另外,行首的红色星号标识了当前活动的线程
- 切换线程:使用 thread THREADNUMBER 进行切换,THREADNUMBER 为上文提到的线程号。下例显示将活动线程从 1 切换至 4。
(gdb) info threads
4 Thread 1099119552 (LWP 12940) 0xffffe002 in ?? ()
3 Thread 1090731072 (LWP 12939) 0xffffe002 in ?? ()
2 Thread 1082342592 (LWP 12938) 0xffffe002 in ?? ()
* 1 Thread 1073951360 (LWP 12931) main (argc=1, argv=0xbfffda04) at thread.c:21
(gdb) thread 4
[Switching to thread 4 (Thread 1099119552 (LWP 12940))]#0 0xffffe002 in ?? ()
(gdb) info threads
* 4 Thread 1099119552 (LWP 12940) 0xffffe002 in ?? ()
3 Thread 1090731072 (LWP 12939) 0xffffe002 in ?? ()
2 Thread 1082342592 (LWP 12938) 0xffffe002 in ?? ()
1 Thread 1073951360 (LWP 12931) main (argc=1, argv=0xbfffda04) at thread.c:21
(gdb)
后面就是直接在你的线程函数里面设置断点,然后continue到那个断点,一般情况下多线程的时候,由于是同时运行的,最好设置 set scheduler-locking on
这样的话,只调试当前线程
转载:http://www.cnblogs.com/xuxm2007/archive/2011/04/01/2002162.html
多进程多线程GDB调试
一、线程调试指南:
1. gdb attach pid 挂载到调试进程
2. gdb$ set scheduler-locking on 只执行当前选定线程的开关
3. gdb$ info threads 显示当前进程中的线程
==============out put example=========================
12 Thread 0x41f16940 (LWP 6922) 0x00000036e74cced2 in select () from /lib64/libc.so.6
11 Thread 0x42917940 (LWP 6923) 0x00000036e74cced2 in select () from /lib64/libc.so.6
10 Thread 0x43318940 (LWP 6924) 0x00000036e74cced2 in select () from /lib64/libc.so.6
9 Thread 0x43d19940 (LWP 6925) 0x00000036e74cced2 in select () from /lib64/libc.so.6
8 Thread 0x4471a940 (LWP 6926) 0x00000036e74cced2 in select () from /lib64/libc.so.6
7 Thread 0x4511b940 (LWP 6927) 0x00000036e74cced2 in select () from /lib64/libc.so.6
6 Thread 0x40a03940 (LWP 6928) 0x00000036e7c0ab99 in pthread_cond_wait@@GLIBC_2.3.2 () from /lib64/libpthread.so.0
5 Thread 0x40b04940 (LWP 6929) 0x00000036e7c0ab99 in pthread_cond_wait@@GLIBC_2.3.2 () from /lib64/libpthread.so.0
4 Thread 0x40c05940 (LWP 6930) 0x00000036e7c0ab99 in pthread_cond_wait@@GLIBC_2.3.2 () from /lib64/libpthread.so.0
3 Thread 0x4521c940 (LWP 6931) 0x00000036e7c0ab99 in pthread_cond_wait@@GLIBC_2.3.2 () from /lib64/libpthread.so.0
2 Thread 0x45c1d940 (LWP 6932) 0x00000036e74cced2 in select () from /lib64/libc.so.6
1 Thread 0x2b277c3bba40 (LWP 6913) 0x00000036e74cced2 in select () from /lib64/libc.so.6
==============out put example=========================
说明 进程中的线程安装 1,2,3 ..编号
4. gdb$ thread 1 调试1线程
gdb$ info threads
==============out put example=========================
12 Thread 0x41f16940 (LWP 6922) 0x00000036e74cced2 in select () from /lib64/libc.so.6
11 Thread 0x42917940 (LWP 6923) 0x00000036e74cced2 in select () from /lib64/libc.so.6
10 Thread 0x43318940 (LWP 6924) 0x00000036e74cced2 in select () from /lib64/libc.so.6
9 Thread 0x43d19940 (LWP 6925) 0x00000036e74cced2 in select () from /lib64/libc.so.6
8 Thread 0x4471a940 (LWP 6926) 0x00000036e74cced2 in select () from /lib64/libc.so.6
7 Thread 0x4511b940 (LWP 6927) 0x00000036e74cced2 in select () from /lib64/libc.so.6
6 Thread 0x40a03940 (LWP 6928) 0x00000036e7c0ab99 in pthread_cond_wait@@GLIBC_2.3.2 () from /lib64/libpthread.so.0
5 Thread 0x40b04940 (LWP 6929) 0x00000036e7c0ab99 in pthread_cond_wait@@GLIBC_2.3.2 () from /lib64/libpthread.so.0
4 Thread 0x40c05940 (LWP 6930) 0x00000036e7c0ab99 in pthread_cond_wait@@GLIBC_2.3.2 () from /lib64/libpthread.so.0
3 Thread 0x4521c940 (LWP 6931) 0x00000036e7c0ab99 in pthread_cond_wait@@GLIBC_2.3.2 () from /lib64/libpthread.so.0
2 Thread 0x45c1d940 (LWP 6932) 0x00000036e74cced2 in select () from /lib64/libc.so.6
*1 Thread 0x2b277c3bba40 (LWP 6913) 0x00000036e74cced2 in select () from /lib64/libc.so.6
==============out put example=========================
*代表选定线程
5. gdb$ b function_name 设置断点端到的函数
6. 通过执行
gdb$ thread id 调试id线程
执行
gdb$ continue
如果能够到达断点说明,函数在该函数中执行,运气很好,直接开始调试
否则,CTRL+C 跳出该线程
调试下一个线程,直到找到调试线程,开始调试。
二、进程调试指南:
程序又多个进程组成,在一个父进程中创建子进程,调试方法如下:
gdb$ set follow-fork-mode [child|parent] 设置fork后,跟踪进程
后续可以再进行多线程调试。
以上功能是在gdb6.4的功能
gdb6.6中增加了
gdb$ set detach-on-fork [on|off]
on: 断开 follow-fork-mode 指定进程
off:gdb控制调试父子进程,follow-fork-mode指定进程被调试,另外一个被挂起。
多进程多线程GDB调试
一、线程调试指南:
1. gdb attach pid 挂载到调试进程
2. gdb$ set scheduler-locking on 只执行当前选定线程的开关
3. gdb$ info threads 显示当前进程中的线程
==============out put example=========================
12 Thread 0x41f16940 (LWP 6922) 0x00000036e74cced2 in select () from /lib64/libc.so.6
11 Thread 0x42917940 (LWP 6923) 0x00000036e74cced2 in select () from /lib64/libc.so.6
10 Thread 0x43318940 (LWP 6924) 0x00000036e74cced2 in select () from /lib64/libc.so.6
9 Thread 0x43d19940 (LWP 6925) 0x00000036e74cced2 in select () from /lib64/libc.so.6
8 Thread 0x4471a940 (LWP 6926) 0x00000036e74cced2 in select () from /lib64/libc.so.6
7 Thread 0x4511b940 (LWP 6927) 0x00000036e74cced2 in select () from /lib64/libc.so.6
6 Thread 0x40a03940 (LWP 6928) 0x00000036e7c0ab99 in pthread_cond_wait@@GLIBC_2.3.2 () from /lib64/libpthread.so.0
5 Thread 0x40b04940 (LWP 6929) 0x00000036e7c0ab99 in pthread_cond_wait@@GLIBC_2.3.2 () from /lib64/libpthread.so.0
4 Thread 0x40c05940 (LWP 6930) 0x00000036e7c0ab99 in pthread_cond_wait@@GLIBC_2.3.2 () from /lib64/libpthread.so.0
3 Thread 0x4521c940 (LWP 6931) 0x00000036e7c0ab99 in pthread_cond_wait@@GLIBC_2.3.2 () from /lib64/libpthread.so.0
2 Thread 0x45c1d940 (LWP 6932) 0x00000036e74cced2 in select () from /lib64/libc.so.6
1 Thread 0x2b277c3bba40 (LWP 6913) 0x00000036e74cced2 in select () from /lib64/libc.so.6
==============out put example=========================
说明 进程中的线程安装 1,2,3 ..编号
4. gdb$ thread 1 调试1线程
gdb$ info threads
==============out put example=========================
12 Thread 0x41f16940 (LWP 6922) 0x00000036e74cced2 in select () from /lib64/libc.so.6
11 Thread 0x42917940 (LWP 6923) 0x00000036e74cced2 in select () from /lib64/libc.so.6
10 Thread 0x43318940 (LWP 6924) 0x00000036e74cced2 in select () from /lib64/libc.so.6
9 Thread 0x43d19940 (LWP 6925) 0x00000036e74cced2 in select () from /lib64/libc.so.6
8 Thread 0x4471a940 (LWP 6926) 0x00000036e74cced2 in select () from /lib64/libc.so.6
7 Thread 0x4511b940 (LWP 6927) 0x00000036e74cced2 in select () from /lib64/libc.so.6
6 Thread 0x40a03940 (LWP 6928) 0x00000036e7c0ab99 in pthread_cond_wait@@GLIBC_2.3.2 () from /lib64/libpthread.so.0
5 Thread 0x40b04940 (LWP 6929) 0x00000036e7c0ab99 in pthread_cond_wait@@GLIBC_2.3.2 () from /lib64/libpthread.so.0
4 Thread 0x40c05940 (LWP 6930) 0x00000036e7c0ab99 in pthread_cond_wait@@GLIBC_2.3.2 () from /lib64/libpthread.so.0
3 Thread 0x4521c940 (LWP 6931) 0x00000036e7c0ab99 in pthread_cond_wait@@GLIBC_2.3.2 () from /lib64/libpthread.so.0
2 Thread 0x45c1d940 (LWP 6932) 0x00000036e74cced2 in select () from /lib64/libc.so.6
*1 Thread 0x2b277c3bba40 (LWP 6913) 0x00000036e74cced2 in select () from /lib64/libc.so.6
==============out put example=========================
*代表选定线程
5. gdb$ b function_name 设置断点端到的函数
6. 通过执行
gdb$ thread id 调试id线程
执行
gdb$ continue
如果能够到达断点说明,函数在该函数中执行,运气很好,直接开始调试
否则,CTRL+C 跳出该线程
调试下一个线程,直到找到调试线程,开始调试。
二、进程调试指南:
程序又多个进程组成,在一个父进程中创建子进程,调试方法如下:
gdb$ set follow-fork-mode [child|parent] 设置fork后,跟踪进程
后续可以再进行多线程调试。
以上功能是在gdb6.4的功能
gdb6.6中增加了
gdb$ set detach-on-fork [on|off]
on: 断开 follow-fork-mode 指定进程
off:gdb控制调试父子进程,follow-fork-mode指定进程被调试,另外一个被挂起。
http://blog.chinaunix.net/uid-442138-id-2122473.html