安装
sudo apt-get install gdb -y
编写需要调试的程序
hello.c
int main()
{
printf("hello, world! This is a C program.\n");
for(int i=0;i<10;i++ ){
printf("output i=%d\n",i);
}
return 0;
}
调试
开始调试
gdb -q hello
查看源码
(gdb) list
运行程序
run
设置断点
(gdb) b 7
Breakpoint 1 at 0x400578: file hello.c, line 7.
查看断点信息
(gdb) info b
Num Type Disp Enb Address What
1 breakpoint keep y 0x0000000000400578 in main at hello.c:7
- NUM:断点编号
- Disp:断点执行一次之后是否还有效(keep:有效 dis:无效)
- Enb:当前断点是否有效(y:有效 n:无效)
- Address:内存地址
单步调试
- continue(简写c):继续执行,到下一个断点处(或运行结束)。
- next(简写n):单步执行,跳过子函数。
- strp(简写s):单步执行,进入子函数。
查看变量
- print+变量名。
清除断点
- clear
运行至函数结束
- finish
退出
- quit
安装cGDB
sudo apt-get install cgdb -y
开始调试
cgdb hello