What is GDB?
GDB, the GNU Project debugger, allows you to see what is going on `inside’ another program while it executes – or what another program was doing at the moment it crashed.
GDB can do four main kinds of things (plus other things in support of these) to help you catch bugs in the act:
Start your program, specifying anything that might affect its behavior.
Make your program stop on specified conditions.
Examine what has happened, when your program has stopped.
Change things in your program, so you can experiment with correcting the effects of one bug and go on to learn about another.
The program being debugged can be written in Ada, C, C++, Objective-C, Pascal (and many other languages). Those programs might be executing on the same machine as GDB (native) or on another machine (remote). GDB can run on most popular UNIX and Microsoft Windows variants.
简单来说,GDB就是一个DEBUG的工具
GDB 安装
Linux系统下:
终端运行如下命令
Windows系统下:
下载MinGw或者TDM-GCC然后配置一下环境变量就可以在CMD中使用了。
安装完成后输入:
出现如下提示表示安装成功:
编译说明
要使用gdb调试程序,必须在进行编译时(g++或gcc)加入-g选项进行编译:
运行GDB
linux系统下编译链接默认生成a.out文件,所以在a.out文件所在目录下执行:
windows系统下默认生成a.exe,同理在a.exe文件所在目录下执行:
调试实例及部分简单调试命令
test.c:
|
|
step(简写为s)命令和next命令都是执行下一步,不同的是step命令会进入函数的内部,而next命令则是会直接执行完函数,然后在该函数的下一步等待
如在上面代码的17行加上func(30)并进行gdb调试;
使用next命令直接跳到18行等待
可以看到,使用s进入到了func函数的内部