GDB调试程序主要分两步
Pre: 调试前的准备
Action: 调试
Pre: 调试前的准备
Pre1:在代码中获得gid, 创造等待时间 【来attach上 gid】
方法:获得进程id号,并让程序等待几秒
printf("PID=%d\n",getpid());
int delay_nums = 30;
while(delay_nums)
{
sleep(1);
printf("%d ",delay_nums);
delay_nums--;
}
Pre2:打开新的terminal, 准备attach gid
对于一些特殊的机器,需要先切换到指定的用户下再执行
打开新的terminal
方法:先输入gdb --pid , 等待pid号出现再输入
gdb --pid
Action:调试
Action1:编译
make clean
make 或 make -f Makefile.se
Action2:在vnc上启动运行程序
在vnc上运行,运行中出现Pre1中需要的pid号后,会出现Pre1的sleep的等待时间
Action3:attach pid
立刻在Pre2的terminal中输入pid号
gdb --pid xxxPIDxxx
Action4:设置断点 【在Pre2的terminal中】
如在指定文件的指定行设置断点
【如是在/home/guo/TP/Oper.c文件的的第3120行设置断点】
b /home/guo/TP/Oper.c:3120
Action5:用 set solib-search-path 设置动态库的搜索路径 【在Pre2的terminal中】
set solib-search-path /home/guo/TP/
Action6:用 set follow-fork-mode child/parent 告诉gdb在目标应用调用fork之后接着调试子进程或父进程 【在Pre2的terminal中】
set follow-fork-mode child
如果涉及进程切换,用set follow-fork-mode parent切换到父进程
Action7: gdb 调试 【在Pre2的terminal中】
设置断点 b xx
查看断点 info b
删除断点 d 序号 【d 1 (删除第一个断点)】 ,【d 2 (删除第二个断点)】
继续执行 c 【继续执行到断点】
单步执行 s/n 【s会进入到方法里面】
输出变量值 p 变量名