https://www.bilibili.com/video/BV1EK411g7Li/?spm_id_from=333.1387.homepage.video_card.click&vd_source=2eed67e9865f67de8eb4fd48aea09b6f
https://sourceware.org/gdb/documentation/

1 GDB是什么东西
2 解决什么问题
3 quickstart快速应用
4 了解它的底层是怎么实现

GDB是什么东西

GDB调试器, 可以运行你在程序运行时检查里面到底发生了什么
GDB可以做:

  • 开始并设置参数
    可以打断点,在特殊情况下停止
    当你程序停止,检查发生了什么

搭建实验环境安装GBD
安装gdb
yum install gdb
检查gdb是否成功安装
gdb --version
image-20250518202239451

QuickStart

1 写程序

#include<stdio.h>
int main(){
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 test1.c
gdb ./a.out
run 运行程序
quit 退出gdb

man gdb 查看gdb文档

img

list 查看我们的源代码
b break 打断点
函数,函数名,在第几行打断点
info b 查看断点的情况
print打印变量
arr[0]
&arr[0]
step s进去某一个具体的函数调试

image.png

watchpoint

GDB技巧

shell ll
shell ls

  1. shell去调用我们的终端命令
  2. 日志功能 (gdb)set logging on,当前页面的结果都会被打印
  3. 断点:watchpoint,catchpoint(观察变量是否变化)

img

调试 core文件

有些时候调试的程序并不是a.out文件,而是挂掉的文件,因此你可能调试的是一个正在运行的进程
段错误:程序访问了它没有权限访问的内存地址
解引用了空指针 、 数组越界、 访问已释放的内存 、写入只读区域

ulimit -a 查看当前进程有哪些限制

image.png

//修改core file size为 unlimited
ucore@ucore-virtual-machine:~/Documents$ ulimit -c unlimited
ucore@ucore-virtual-machine:~/Documents$ gcc -g test1.c
ucore@ucore-virtual-machine:~/Documents$ rm -rf a.out
ucore@ucore-virtual-machine:~/Documents$ gcc -g test1.c
ucore@ucore-virtual-machine:~/Documents$ ls
a.out test1.c
ucore@ucore-virtual-machine:~/Documents$ ./a.out
Segmentation fault (core dumped)
ucore@ucore-virtual-machine:~/Documents$ ls
a.out core test1.c
ucore@ucore-virtual-machine:~/Documents$ gdb a.out core
GNU gdb (Ubuntu 8.1.1-0ubuntu1) 8.1.1
Copyright (C) 2018 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. Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from a.out...done.
[New LWP 117072]
Core was generated by `./a.out'.
Program terminated with signal SIGSEGV, Segmentation fault.
#0 0x000055e23495c6f0 in main () at test1.c:7
7 *p = 5;

image-20250518202705236

如果这里修改了core file size为 unlimited,但编译生成后,运行./x.out文件没有core文件,尝试运行
cat /proc/sys/kernel/core_pattern
如果你的输出是类似 |/usr/share/apport/apport %p %s ...,说明:
🔥 Ubuntu 使用 apport 捕获崩溃并吞掉了 core 文件!
解决方法
sudo nano /etc/default/apport
修改为
enabled=0
sudo systemctl restart apport.service

调试正在运行的一个进程

./a.out & 后台执行

ucore@ucore-virtual-machine:~/Documents$ gcc test_for.c -g
ucore@ucore-virtual-machine:~/Documents$ ./a.out &
[4] 21528
ucore@ucore-virtual-machine:~/Documents$ ps -ef | grep a.out
ucore@ucore-virtual-machine:~/Documents$ gbd -p 21528

ucore@ucore-virtual-machine:~/Documents$ ps -ef | grep a.out