在Linux中,gdb
是GNU调试器(GNU debugger)的缩写。它是一个强大的命令行调试工具,用于调试C、C++和其他编程语言的程序。
一、生成可调式程序
gcc 加上 -g 选项可生成C语言可调式程序。
(base) [root@localhost 01_test]# vim test1.c
(base) [root@localhost 01_test]# cat test1.c
#include <stdio.h>
int main()
{
int a = 0;
int b = 0;
return 0;
}
(base) [root@localhost 01_test]# vim makefile
(base) [root@localhost 01_test]# cat makefile
all: test1 test1_g
test1: test1.c
gcc -o $@ $^
test1_g: test1.c
gcc -o $@ $^ -g
.PHONY: clean
clean:
rm -rf test1 test1_g
(base) [root@localhost 01_test]# make
gcc -o test1 test1.c
gcc -o test1_g test1.c -g
(base) [root@localhost 01_test]# ls
makefile test1 test1.c test1_g
(base) [root@localhost 01_test]#
二、调试程序指令
- gdb [exe_filename] 开始调试
- quit 退出调试
- r 开始调试,若无断点,则直接运行结束(run)
- b + 行号 给这一行打断点(break point)
- info b 查看断点
- d + 断点编号 删掉某断点(delete)
- n 逐过程调试(next)
- s 逐语句调试(step)
- c 进入下一个断点(continue)
- bt 查看调用堆栈
- finish 将函数调试结束
- print + 变量 显示变量的值
- display + 变量 设置常显示
- undisplay + 常显示编号 取消常显示
- enable/disable + 断点编号 打开/关闭断点