- 默认设置下,在调试多进程程序时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位置
查询正在调试的进程:info inferiors
切换调试的进程: inferior
添加新的调试进程: add-inferior [-copies n] [-exec executable] ,可以用file executable来分配给inferior可执行文件。
其他:remove-inferiors infno, detach inferior
2.GDB默认支持调试多线程,跟主线程,子线程block在create thread。
查询线程:info threads
切换调试线程:thread
测试代码:
调试:
1. 调试主进程,block子进程。
2.切换到子进程:
3.设断点继续调试主进程,主进程产生两个子线程:
4.切换到主进程中的子线程,注意:线程2为前面产生的子进程
gdb调试常用命令:
$ gcc -g example.c -o example.x
通过上述命令对example.c编译之后,使用下列命令进入到gdb调试:
在gdb调试中,常用的命令有以下几个:
列出程序源码,每次列出10行,按回车重复运行上一命令;
程序开始运行,在r后可以加入程序启动参数,程序运行到断点处暂停;
程序继续运行,到下一断点处暂停;
单步调试
程序继续运行到下一断点;
在程序某一位置设置断点;
查看断点信息;
设置/查看运行参数
加载运行中的进程进行调试(Attach to the running process to be debugged.):
以十六进制输出内存块数据
段错误调试,core文件样例
通过ulimit命令查看一下系统是否配置支持了dump core的功能。通过ulimit -c或ulimit -a,可以查看core file大小的配置情况,如果为0,则表示系统关闭了dump core;可以通过ulimit -c unlimited来打开。若发生了段错误,但没有core dump,是由于系统禁止core文件的生成。
查看堆栈信息: