【Linux环境配置】core dump配置和快速gdb调试core文件

本文介绍了如何在Ubuntu系统中配置coredump以生成core文件,包括设置core文件格式为`core.%p.%u.%s.%e.%t`和设置核心文件大小限制。通过编写并调试一个示例代码,展示了coredump的使用以及如何使用gdb对core文件进行调试。
摘要由CSDN通过智能技术生成

0. 在Ubuntu系统中直接运行导致coredump的程序并没有生成core文件

  • 如下所示, 执行时出现异常, 虽然提示进行core dump实际上并没有产生core文件, 此时就需要配置一下了;
$ gcc -g test.c 
$ ./a.out 
Floating point exception (core dumped)
$ ll
total 32
drwxrwxr-x 2 neo neo  4096 Jan 28 20:51 ./
drwxrwxr-x 4 neo neo  4096 Jan 28 20:50 ../
-rwxrwxr-x 1 neo neo 17016 Jan 28 20:51 a.out*
-rw-rw-r-- 1 neo neo   171 Jan 28 20:51 test.c

1. core dump 设置

1.1 设置core文件格式

  • 在配置文件 /etc/sysctl.conf 中, 配置core文件的格式, 参数名称为: kernel.core_pattern, 参考配置如下:
kernel.core_pattern=core.%p.%u.%s.%e.%t
  • 查询配置是否成功 sysctl -p, 输出示例如下 :
$ sudo sysctl -p
[sudo] password for neo: 
kernel.core_pattern = core.%p.%u.%s.%e.%t

1.2 设置core 大小限制

  • 在配置文件 /etc/security/limits.conf 中, 配置core文件大小限制, 参考配置如下:
* soft core unlimited
* hard core unlimited

2. 写个代码测一下

2.1 示例代码放入 test.c

int actual_calc(int a, int b){
	int c;
	c=a/b;
	return 0;
}

int calc(){
	int a;
	int b;
	a=13;
	b=0;
	actual_calc(a, b);
	return 0;
}

int main(){
	calc();
	return 0;
}

2.2 编译运行

  • 编译运行
$ gcc -g test.c 
$ ./a.out 
Floating point exception (core dumped)
$ ll
total 156
drwxrwxr-x 2 neo neo   4096 Jan 28 20:56 ./
drwxrwxr-x 4 neo neo   4096 Jan 28 20:50 ../
-rwxrwxr-x 1 neo neo  17016 Jan 28 20:51 a.out*
-rw------- 1 neo neo 307200 Jan 28 20:56 core.25819.1000.8.a.out.1706446582
-rw-rw-r-- 1 neo neo    171 Jan 28 20:51 test.c
  • 本次运行并产生core文件: core.25819.1000.8.a.out.1706446582 ;

2.3 gdb调试一下core

  • 可以直接 gdb -c core文件名 启动调试, 也可以同时指定一下对应的程序名, 或者后续再通过 file 命令指定, 此处示例分步指定, 所以执行 l 时提示没有符号表, 需要手动加载一下;
$ gdb -c core.25819.1000.8.a.out.1706446582 
GNU gdb (Ubuntu 12.1-0ubuntu1~22.04) 12.1
Copyright (C) 2022 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:
<https://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".
[New LWP 25819]
Core was generated by `./a.out'.
Program terminated with signal SIGFPE, Arithmetic exception.
#0  0x000055f620b4c13b in ?? ()
(gdb) l
No symbol table is loaded.  Use the "file" command.
  • 加载core对应的编译后的程序文件,里面含有代码的调试信息, 此处为 a.out 程序
(gdb) file a.out 
Reading symbols from a.out...
(gdb) l
3		c=a/b;
4		return 0;
5	}
6	
7	int calc(){
8		int a;
9		int b;
10		a=13;
11		b=0;
12		actual_calc(a, b);
(gdb) bt
#0  0x000055f620b4c13b in actual_calc (a=13, b=0) at test.c:3
#1  0x000055f620b4c171 in calc () at test.c:12
#2  0x000055f620b4c18a in main () at test.c:17
(gdb) p b
$1 = 0
(gdb) p a/b
Division by zero
(gdb) f 1
#1  0x000055f620b4c171 in calc () at test.c:12
12		actual_calc(a, b);
(gdb) l
7	int calc(){
8		int a;
9		int b;
10		a=13;
11		b=0;
12		actual_calc(a, b);
13		return 0;
14	}
15	
16	int main(){
(gdb) f 2
#2  0x000055f620b4c18a in main () at test.c:17
17		calc();
(gdb) l
12		actual_calc(a, b);
13		return 0;
14	}
15	
16	int main(){
17		calc();
18		return 0;
19	}
20	
(gdb) q

  • 注意上面加载core后, 默认堆栈在最顶端, 类似于执行了f 0的效果, 对应的就是当前出错位置, 直接执行 p a/b 得到 Division by zero, 也就是错误原因;

3. 参考资料

  • https://linuxconfig.org/gdb-debugging-tutorial-for-beginners
  • 10
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

逸云沙鸥のIHave@Dream

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值