GDB(GNU Debugger)是GCC调试工具,功能强大:
生成调试信息 :
一般来说GDB主要用于调试 C/C++程序。在编译时,我们必须要把调试信息加到可执行文件中。
gcc -g hello.c -o hello
启动GDB
- 启动gdb: gdb program(program:执行文件,一般在当前目录下)
- 设置运行参数(set args, show args)
- 启动程序(run 开始执行到断点处, start:程序向下执行一行)
断点操作
设置断点:
b linenum
b func
b file:linenum
b file:func
查看断点:
info break
使断点有效/无效
disable m n | m-n
enable m n | m-n
条件断点:
使用if关键字,后面跟其断点条件。
b test.c:8 if intValue ==5
代码调试
- run 运行程序,可简写为r
- next 单步跟踪,且把函数调用当成一条语句来执行,简写为n
- step 单步跟踪,可以进入被调函数内部,简写为s
- finish退出进入的函数,如果进入的函数中有断点,就退不出去,此时需要把断点设置无效
- until 在一个循环体内单步跟踪时,这个命令可以运行程序知道退出循环体 简写为u
- continue 继续运行程序,有断点则运行到下一个断点处。简写为c
查看变量的值:
自动显示:display var
查看已经显示:info display
使m n不显示 disable display m-n
使m n显示 enable display m n | m-n