如何通过GDB调试器学习C语言编程技能?
摘要:## GDB调试器 我们在讲[指针][初级指针]时用 `GDB` 调试段错误。 本篇将详细介绍 gdb 的`最常用命令`、`日志记录`、`检测点`,最后介绍如何用 gdb `调试进程`以及用gdb 调试一个开源项目的`调试版本` —— gl
GDB调试器
我们在讲指针时用 GDB 调试段错误。
本篇将详细介绍 gdb 的最常用命令、日志记录、检测点,最后介绍如何用 gdb 调试进程以及用gdb 调试一个开源项目的调试版本 —— glmark2。
gdb介绍
GDB, the GNU Project debugger —— gdb官网
gdb 是一款调试器,能打断点。支持多种语言,例如 c、c++、go。
Tip:有关 GNU Project,请看本篇扩展。
官网显示最新版本是13.2(20230704)。点击官网顶部[documentation]可查看文档。
安装GDB
笔者已经用 apt 源安装了gbd:
jjj-pc:~/pj/glmark2$ sudo apt install gdb
正在读取软件包列表... 完成
正在分析软件包的依赖关系树
正在读取状态信息... 完成
gdb 已经是最新版 (9.1-0kylin1)。
下列软件包是自动安装的并且现在不需要了:
archdetect-deb dmeventd libaio1 libdebian-installer4 libdevmapper-event1.02.1 liblvm2cmd2.03 localechooser-data lvm2 user-setup
使用'sudo apt autoremove'来卸载它(它们)。
升级了 0 个软件包,新安装了 0 个软件包,要卸载 0 个软件包,有 6 个软件包未被升级。
笔者gbd版本是 9.1
jjj-pc:~/pj/glmark2$ gdb --version
GNU gdb (Ubuntu 9.1-0kylin1) 9.1
Copyright (C) 2020 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
最常用命令
man gdb 告诉我们最常用的命令有:break、run、print、c、next、list、step、quit。
// 以下是一些最常用的GDB命令:
Here are some of the most frequently needed GDB commands:
break [file:]function
Set a breakpoint at function (in file).
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
// 执行下一条程序语句(在停止后);跳过该行中的任何函数调用。
