程序的调试过程主要有:单步执行,跳入函数,跳出函数,设置断点,设置观察点,查看变量。
You can run "gdb" with no arguments or options; but the most usualway to start GDB is with one argument or two, specifying anexecutable program as the argument:gdb programYou can also start with both an executable program and a core filespecified:gdb program coreYou can, instead, specify a process ID as a second argument or useoption "-p", if you want to debug a running process:gdb program 1234gdb -p 1234would attach GDB to process 1234. With option -p you can omit theprogram filename.
本文将主要介绍linux下运行。
GDB中的命令很多,但我们只需掌握其中十个左右的命令,就大致可以完成日常的基本的程序调试工作。

新建test.c 输入以下内容:
#include
void f(){printf(" f is called \n");
}int i =1;int main(){f();i = 4;printf(" Hello world!\n");
}
要调试C/C++的程序,首先在编译时,要使用gdb调试程序,在使用gcc编译源代码时必须加上“-g”参数。
gcc -g test.c
保留调试信息,否则不能使用GDB进行调试.
有一种情况,有一个编译好的二进制文件,你不确定是不是带有-g参数,带有GDB调试,这个时候你可以使用如下的命令验证:
gdb a.out
如果没有调试信息,则会出现:
Reading symbols from a.out…
(No debugging symbols found in a.out
如果带有调试功能,下面会提示
Reading symbols from a.out……done.
使用命令readelf查看可执行文件是否带有调试功能:
readelf -S main|grep debug
如果有debug说明有调试功能,如果没有debug。说明没有带有调试功能,则不能被调试。
通过 gdb + 可执行文件 , 开始调试需要的文件。
gdb a.out
输入,
run :将运行整个程序,
list: 显示出原始的代码, 可以输入多次, 直到显示完整的代码。
quit: 退出gdb 模式。
(gdb) run
Starting program: /home/respecting-life/Desktop/os_上交/lab1/a.out f is called Hello world!
[Inferior 1 (process 5403) exited normally](gdb) list
1 #include
2
3 void f(){
4 printf(" f is called \n");
5 }
6
7
8 int i =1;
9
10 int main(){
(gdb) list
11 f();
12 i = 4;
13 printf(" Hello world!\n");
14 }
gdb 中打断点的方式, 有两种
(gdb) break main
Breakpoint 1 at 0x555555555160: file test.c, line 10.
(gdb) b 11
Breakpoint 2 at 0x555555555168: file test.c, line 11
info b 下一篇:AWVS的简介与安装