GDB调试多进程|多线程程序
1. 默認設置下,在調試多進程程序時GDB只會調試主進程。但是GDB(>V7.0)支持多進程的分別以及同時調試,換句話說,GDB可以同時調試多個程序。只需要設置follow-fork-mode(默認值:parent)和detach-on-fork(默認值:on)即可。
follow-fork-mode? detach-on-fork?? 說明 parent??????????????? ?? on?????????????? 只調試主進程(GDB默認)child? ??? ? ??? ? ? ?? ? on?????????????? 只調試子進程
parent?????????? ?? ? ?? off????????????? 同時調試兩個進程,gdb跟主進程,子進程block在fork位置
child? ???????? ? ?? ? ?? off ???????????? 同時調試兩個進程,gdb跟子進程,主進程block在fork位置
?? 設置方法:set follow-fork-mode [parent|child] ? set detach-on-fork [on|off]
?? 查詢正在調試的進程:info inferiors
?? 切換調試的進程: inferior <infer number>
?? 添加新的調試進程: add-inferior [-copies n] [-exec executable] ,可以用file executable來分配給inferior可執行文件。
?? 其他:remove-inferiors infno, detach inferior
2. GDB默認支持調試多線程,跟主線程,子線程block在create thread。
?? 查詢線程:info threads
?? 切換調試線程:thread <thread number>
例程:
#include <pthread.h>
void processA();
void processB();
void * processAworker(void *arg);
int main(int argc, const char *argv[])
? {
? int pid;
? pid = fork();
? if(pid != 0)
??? processA();
? else
??? processB();
? return 0;
? }
void processA()
? {
? pid_t pid = getpid();
? char prefix[] = "ProcessA: ";
? char tprefix[] = "thread ";
? int tstatus;
? pthread_t pt;
? printf("%s%lu %s\n", prefix, pid, "step1");
? tstatus = pthread_create(&pt, NULL, processAworker, NULL);
? if( tstatus != 0 )
??? {
??? printf("ProcessA: Can not create new thread.");
??? }
?
? processAworker(NULL);
? sleep(1);
? }
void * processAworker(void *arg)
? {
? pid_t pid = getpid();
? pthread_t tid = pthread_self();
? char prefix[] = "ProcessA: ";
? char tprefix[] = "thread ";
? printf("%s%lu %s%lu %s\n", prefix, pid, tprefix, tid, "step2");
? printf("%s%lu %s%lu %s\n", prefix, pid, tprefix, tid, "step3");
? return NULL;
? }
void processB()
? {
? pid_t pid = getpid();
? char prefix[] = "ProcessB: ";
? printf("%s%lu %s\n", prefix, pid, "step1");
? printf("%s%lu %s\n", prefix, pid, "step2");
? printf("%s%lu %s\n", prefix, pid, "step3");
? }
輸出:
[cnwuwil@centos c-lab]$ ./test ProcessA: 802 step1ProcessB: 803 step1
ProcessB: 803 step2
ProcessB: 803 step3
ProcessA: 802 thread 3077555904 step2
ProcessA: 802 thread 3077555904 step3
ProcessA: 802 thread 3077553008 step2
ProcessA: 802 thread 3077553008 step3
調試:
1. 調試主進程,block子進程。
(gdb) show detach-on-fork
Whether gdb will detach the child of a fork is off.
(gdb) catch fork
Catchpoint 1 (fork)
(gdb) r
[Thread debugging using libthread_db enabled]
Catchpoint 1 (forked process 3475), 0x00110424 in __kernel_vsyscall ()
Missing separate debuginfos, use: debuginfo-install glibc-2.12-1.47.el6.i686
(gdb) break test.c:14
Breakpoint 2 at 0x8048546: file test.c, line 14.
(gdb) cont
[New process 3475]
[Thread debugging using libthread_db enabled]
Breakpoint 2, main (argc=1, argv=0xbffff364) at test.c:14
Missing separate debuginfos, use: debuginfo-install glibc-2.12-1.47.el6.i686
(gdb) info inferiors
? Num? Description?????? Executable???????
? 2??? process 3475????? /home/cnwuwil/labs/c-lab/test
* 1??? process 3472????? /home/cnwuwil/labs/c-lab/test
2. 切換到子進程:
(gdb) inferior 2[Switching to inferior 2 [process 3475] (/home/cnwuwil/labs/c-lab/test)]
[Switching to thread 2 (Thread 0xb7fe86c0 (LWP 3475))]
#0? 0x00110424 in ?? ()
(gdb) info inferiors
? Num? Description?????? Executable???????
* 2??? process 3475????? /home/cnwuwil/labs/c-lab/test
? 1??? process 3472????? /home/cnwuwil/labs/c-lab/test
(gdb) inferior 1
[Switching to inferior 1 [process 3472] (/home/cnwuwil/labs/c-lab/test)]
[Switching to thread 1 (Thread 0xb7fe86c0 (LWP 3472))]
#0? main (argc=1, argv=0xbffff364) at test.c:14
(gdb) info inferiors
? Num? Description?????? Executable???????
? 2??? process 3475????? /home/cnwuwil/labs/c-lab/test
* 1??? process 3472????? /home/cnwuwil/labs/c-lab/test
3. 設斷點繼續調試主進程,主進程產生兩個子線程:
(gdb) break test.c:50Breakpoint 3 at 0x804867d: file test.c, line 50. (2 locations)
(gdb) cont
ProcessA: 3472 step1
[New Thread 0xb7fe7b70 (LWP 3562)]
ProcessA: 3472 thread 3086911168 step2
Breakpoint 3, processAworker (arg=0x0) at test.c:50
(gdb) info inferiors
? Num? Description?????? Executable???????
? 2??? process 3475????? /home/cnwuwil/labs/c-lab/test
* 1??? process 3472????? /home/cnwuwil/labs/c-lab/test
(gdb) info threads
? 3 Thread 0xb7fe7b70 (LWP 3562)? 0x00110424 in __kernel_vsyscall ()
? 2 Thread 0xb7fe86c0 (LWP 3475)? 0x00110424 in ?? ()
* 1 Thread 0xb7fe86c0 (LWP 3472)? processAworker (arg=0x0) at test.c:50
4. 切換到主進程中的子線程,注意:線程2為前面產生的子進程
(gdb) thread 3[Switching to thread 3 (Thread 0xb7fe7b70 (LWP 3562))]#0? 0x00110424 in __kernel_vsyscall ()
(gdb) cont
ProcessA: 3472 thread 3086911168 step3
ProcessA: 3472 thread 3086908272 step2
[Switching to Thread 0xb7fe7b70 (LWP 3562)]
Breakpoint 3, processAworker (arg=0x0) at test.c:50
(gdb) info threads
* 3 Thread 0xb7fe7b70 (LWP 3562)? processAworker (arg=0x0) at test.c:50
? 2 Thread 0xb7fe86c0 (LWP 3475)? 0x00110424 in ?? ()
? 1 Thread 0xb7fe86c0 (LWP 3472)? 0x00110424 in __kernel_vsyscall ()
(gdb) thread 1
轉載于:https://www.cnblogs.com/chezxiaoqiang/archive/2012/03/14/2674388.html
總結
以上是生活随笔為你收集整理的GDB调试多进程|多线程程序的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 代码优化从数据库里查数据
- 下一篇: CGRect vs CGPoint vs