因为有运行环境产生coredump问题,之前也没怎么接触这一块,所以先记录学习一下基本操作
os坏境
[root@localhost ~]# cat /etc/.productinfo
KYLIN Linux Advanced Server
Release V10(SP1)/(Tercel)-sw64-Build23-20210810-GFB
[root@localhost ~]#
这里我们只了解如何检查以及自定义程序运行过程中产生coredump的情况
1. 检查coredum文件开关
使用ulimit -c命令查看core文件的开关。返回值为0表示关闭了这个功能,则不会产生coredum文件。若未开启,则使用ulimit -c unlimited开启即可
[root@localhost ~]# ulimit -c
unlimited
[root@localhost ~]#
这样的设置只是暂时,重启机器会失效,最好的办法就是写入系统配置文件中,即/etc/profile
[root@localhost ~]# echo "ulimit -c unlimited" >> /etc/profile
[root@localhost ~]# source /etc/profile
2. 设置core文件生成路径
[root@localhost ~]# cat /proc/sys/kernel/core_uses_pid
1
[root@localhost systemd]# cat /proc/sys/kernel/core_pattern
|/usr/lib/systemd/systemd-coredump %P %u %g %s %t %c %h
[root@localhost systemd]# echo "/corefile/core-%e-%p-%t" > /proc/sys/kernel/core_pattern
[root@localhost ~]# cat /proc/sys/kernel/core_pattern
/corefile/core-%e-%p-%t
[root@localhost ~]# mkdir /corefile
/proc/sys/kernel/core_uses_pid :主要用于控制文件名是否添加pid作为扩展名。
core_pattern默认被配置成以|管道字符开头的字符串,意味着程序的coredump信息将会作为标准输入交由systemd-coredump程序进行处理。systemd-coredump默认会为所有进程在/var/lib/systemd/coredump目录下以特定的压缩格式生成core文件。
/proc/sys/kernel/core_pattern:控制core文件保存位置和文件名格式。%e表示可执行程序的文件名,%p表示core dump进程id,%t表示产生coredump的时间。
3. 测试一下
[root@localhost ~]# cat test.c
#include <stdio.h>
#include <stdlib.h>
int crash()
{
char *crash_info = "crash!!!";
crash_info[1] = 'D';
return 0;
}
int main()
{
crash();
return 0;
}
[root@localhost corefile]# gcc -g test.c -o test
[root@localhost corefile]# ./test
Segmentation fault (核心已转储)
[root@localhost corefile]# ls
core-test-3194981-1632201352 test test.c
[root@localhost corefile]# gdb test
GNU gdb (GDB) 8.2.1 Penv. of 0x9916-v1.0 by xiewb 20200623
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 "sw_64-sunway-linux-gnu".
Type "show configuration" for configuration details.
..
Reading symbols from test...done.
(gdb) ru
Starting program: /corefile/test
Program received signal SIGSEGV, Segmentation fault.
0x0000000120000580 in crash () at test.c:8
8 crash_info[1] = 'D';
(gdb)