日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

GDB调试多进程|多线程程序

發(fā)布時(shí)間:2023/12/9 编程问答 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 GDB调试多进程|多线程程序 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

1. 默認(rèn)設(shè)置下,在調(diào)試多進(jìn)程程序時(shí)GDB只會(huì)調(diào)試主進(jìn)程。但是GDB(>V7.0)支持多進(jìn)程的分別以及同時(shí)調(diào)試,換句話說(shuō),GDB可以同時(shí)調(diào)試多個(gè)程序。只需要設(shè)置follow-fork-mode(默認(rèn)值:parent)和detach-on-fork(默認(rèn)值:on)即可。

follow-fork-mode? detach-on-fork?? 說(shuō)明 parent??????????????? ?? on?????????????? 只調(diào)試主進(jìn)程(GDB默認(rèn))
child? ??? ? ??? ? ? ?? ? on?????????????? 只調(diào)試子進(jìn)程
parent?????????? ?? ? ?? off????????????? 同時(shí)調(diào)試兩個(gè)進(jìn)程,gdb跟主進(jìn)程,子進(jìn)程block在fork位置
child? ???????? ? ?? ? ?? off ???????????? 同時(shí)調(diào)試兩個(gè)進(jìn)程,gdb跟子進(jìn)程,主進(jìn)程block在fork位置

?? 設(shè)置方法:set follow-fork-mode [parent|child] ? set detach-on-fork [on|off]

?? 查詢正在調(diào)試的進(jìn)程:info inferiors
?? 切換調(diào)試的進(jìn)程: inferior <infer number>
?? 添加新的調(diào)試進(jìn)程: add-inferior [-copies n] [-exec executable] ,可以用file executable來(lái)分配給inferior可執(zhí)行文件。
?? 其他:remove-inferiors infno, detach inferior

2. GDB默認(rèn)支持調(diào)試多線程,跟主線程,子線程block在create thread。
?? 查詢線程:info threads
?? 切換調(diào)試線程:thread <thread number>

例程:

#include <stdio.h>
#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 step1
ProcessB: 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


調(diào)試:
1. 調(diào)試主進(jìn)程,block子進(jìn)程。

(gdb) set detach-on-fork off
(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. 切換到子進(jìn)程:

(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. 設(shè)斷點(diǎn)繼續(xù)調(diào)試主進(jìn)程,主進(jìn)程產(chǎn)生兩個(gè)子線程:

(gdb) break test.c:50
Breakpoint 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. 切換到主進(jìn)程中的子線程,注意:線程2為前面產(chǎn)生的子進(jìn)程

(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





轉(zhuǎn)載于:https://www.cnblogs.com/chezxiaoqiang/archive/2012/03/14/2674388.html

總結(jié)

以上是生活随笔為你收集整理的GDB调试多进程|多线程程序的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。