gdb调试多进程多线程

xiaoxiao2021-02-27  130

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 <stdio.h> #include <pthread.h> void child() { int i = 0; printf("child; %d\n", i); } void father() { int i = 1; printf("father: %d\n", i); } void *thread_run(void *arg) { int count = 0; while(count++ < 5) { sleep(1); printf("T am new thread,tid:%lu, pid:%d\n", pthread_self(), getpid()); } pthread_exit((void *)123); } int main() { pid_t pid = fork(); if(pid == 0) child(); else father(); pthread_t id; pthread_create(&id, NULL, thread_run, NULL); while(1) { sleep(1); printf("I am main thread,tid:%lu, pid:%d\n", pthread_self(), getpid()); } sleep(4); int ret2 = pthread_cancel(id); void *val = NULL; int ret = pthread_join(id, &val); printf("The new thread is quit.val:%d\n", (int)val); printf("%lu\n",id); return 0; }

调试:

1. 调试主进程,block子进程。

2.切换到子进程

3.调试主进程中的子线程

4.切换线程

转载请注明原文地址: https://www.6miu.com/read-16302.html

最新回复(0)