文章目录
gdb 调试 与 coredump
gdb 是 GNU 发布的一个强大的程序调解工具, 也是Linux 程序员不可或缺的一大利器. 本章我们将给出 gdb 常用的命令的操作说明.
启动 gdb
使用gdb的前提
gcc -g hello.c -o hello
启动 gdb 调试:
gdb hello
然后就可以进入命令行操作,和Shell一样,gdb 支持命令补全。输入几个字母,按Tab键,
gdb会补全命令。按两次Tab,会提示所有可能的命令。
另外,其还支持命令缩写,如,h 代表help,
常用命令如下:
下面是实操
➜ makefile_demo git:(main) ✗ gdb hello_demo
GNU gdb (GDB) Red Hat Enterprise Linux 8.2-11.el8
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-redhat-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 hello_demo...(no debugging symbols found)...done.
(gdb) b main
Breakpoint 1 at 0x40059a
(gdb) r
Starting program: /home/working/Volume_6_Matin_Linux-C_Learning/study_test01/makefile_demo/hello_demo
Missing separate debuginfos, use: yum debuginfo-install glibc-2.28-127.el8.x86_64
Breakpoint 1, 0x000000000040059a in main ()
(gdb) n
Single stepping until exit from function main,
which has no line number information.
Hello weifc.
count: 1
Hello world.
count: 2
0x00007ffff7a347b3 in __libc_start_main () from /lib64/libc.so.6
(gdb) bt
#0 0x00007ffff7a347b3 in __libc_start_main () from /lib64/libc.so.6
#1 0x00000000004004de in _start ()
(gdb)
bt 查看堆栈情况
bt 每次程序调用一个函数, 函数的地址, 参数, 函数内部变量都会被压入 “栈(Stack)” 中, 运行时堆栈信息对程序员非常重要, 使用 “bt” 命令可以看到运行时 栈的情况.
利用gdb单步执行理解刚接手的程序的时候,提示下图中的错误:
Breakpoint 1, 0x000000000040059a in main ()
(gdb) n
Single stepping until exit from function main,
which has no line number information.
Hello weifc.
count: 1
Hello world.
count: 2
0x00007ffff7a347b3 in __libc_start_main () from /lib64/libc.so.6
(gdb)
需要升级了, 不鸟它, 直接cmake
退出 gdb
调试完毕, 使用 quit 命令(缩写为q) 退出 gdb 程序
gdb - coredump 详解
Coredump 调试
coredump 是什么?
程序异常退出是, 会产生一个 core 文件, 该文件记录了程序运行时的内存, 寄存器状态, 堆栈指针, 内存管理信息还有各种函数调用堆栈信息等, 我们可以理解为是程序工作当前状态存储生成的一文件, 通过工具分析这个文件, 我们可以定位到程序异常退出的时候对应的堆栈调用等信息, 找出问题所在并进行及时解决.
说人话, 也就是问题的现场
前期设置
- 设置 core 文件生成的目录, 其中 %e 表示文件名, %p 表示进程ID,
否则会在程序的当前目录生成 dore 文件;
echo /data/coredump/core.%e.%p>/proc/sys/kernel/core_pattern
%e具体的执行程序, %p 进程ID
/proc/sys/kernel/core_pattern ## 这是固定的
➜ / mkdir data
➜ / ls
apps bin boot data dev etc home lib lib64 media mnt opt patch proc root run sbin srv sys tmp usr var www
➜ /
➜ / cd data
➜ /data ls
➜ /data
➜ /data mkdir coredump
....
➜ build git:(main) ✗ echo /data/coredump/core.%e.%p>/proc/sys/kernel/core_pattern
➜ build git:(main) ✗ cat /proc/sys/kernel/core_pattern
/data/coredump/core.%e.%p
➜ build git:(main) ✗
新建/data/coredump, 然后 echo
这是第一步
- 当前执行程序的用户对 core 目录有写权限且有足够的空间存储 core 文件;
➜ /data ls -la
total 4
drwxr-xr-x 3 root root 22 Jan 11 13:21 .
dr-xr-xr-x. 22 root root 4096 Jan 11 13:21 ..
drwxr-xr-x 2 root root 6 Jan 11 13:21 coredump
➜ /data
第二步
- 生成不限制的 core 文件;
➜ /data
➜ /data ls -la
total 4
drwxr-xr-x 3 root root 22 Jan 11 13:21 .
dr-xr-xr-x. 22 root root 4096 Jan 11 13:21 ..
drwxr-xr-x 2 root root 6 Jan 11 13:21 coredump
➜ /data
➜ /data
➜ /data
➜ /data ulimit -a
-t: cpu time (seconds