GDB Debugger
GDB 调试器
1. 什么是GDB?
可以在运行程序的时候检查到底发生了什么:
可以开始程序,并设置参数
打入断点,必要时停止
当你程序停止时,检查发生了什么
1.1. GDB 支持
Ada
Assembly 汇编语言
C 最常用
C++ 最常用
Fortran
Go
Pascal
Rust
1.2. 搭建环境
使用主机 Ubuntu。
gdb --version
发现已经装好了。
2. 测试功能
2.1. 测试代码
vim test2.c
#include<stdio.h>
int main(void)
{
int arr[4] = {1, 2, 3, 4};
int i = 0;
for(i = 0; i < 4; i++)
{
printf("%d\n", arr[i]);
}
return 0;
}
保存退出之后
gcc -g test2.c
其中 -g
是必要的,否则没有办法进行调试
随后进行gdb ./a.out
使用 gdb 进行调试
run
/ r
可以让程序跑起来
quit
/ q
退出 gdb 模式
2.2. 常用指令
break [file:]function
Set a breakpoint at
function (in file).
打断点, 可以缩写为 b
通过 list
查看源代码后, 可以用 b + 行号
对某一行打断点
run [arglist]
Start your program (with
arglist, if specified).
让程序跑起来
bt
Backtrace:
display the program stack.
print expr
Display the value of an
expression.
c
Continue running your
program (after stopping,
e.g. at a breakpoint).
next
Execute next program
line (after stopping);
step over any function
calls in the line.
进入下一步, 可以简写为n
edit [file:]function
look at the program line
where it is presently
stopped.
list [file:]function
type the text of the
program in the vicinity
of where it is presently
stopped.
step
Execute next program
line (after stopping);
step into any function
calls in the line.
进入某个具体的函数, 进行调试
help [name]
Show information about
GDB command name, or
general information
about using GDB.
quit
Exit from GDB.
退出 GDB
info b
查看断点的情况.
p
查看变量的值或变量的地址
2.3. GDB 的小技巧
2.3.1. Shell
可以使用 shell + shell命令
可以使用 shell 指令
通过 shell 取调用终端命令
2.3.2. 日志模式
set logging on
可以加入日志功能
2.3.3. 观察点
watch + 地址
观察某个地址的值会不会发生变化
或者也可以
watch + 变量名
可以通过 info
来查看观察点
3. 进阶命令
日后的工作可以进行需要性学习
3.1. 调试 core 文件
TO DO
3.2. 调试正在运行的程序
TO DO
4. 资料
在线 GDB 调试网站. 不用担心玩坏自己的机器, 把代码复制上去就可以进行 GDB 调试 https://www.onlinegdb.com/
GDB 官方文档 (英文) https://www.sourceware.org/gdb/documentation/
Last updated