前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >CentOS开启coredump转储并生成core文件的配置

CentOS开启coredump转储并生成core文件的配置

作者头像
typecodes
发布2024-03-29 13:31:22
2050
发布2024-03-29 13:31:22
举报
文章被收录于专栏:typecodestypecodes

在CentOS或者suse等Linux系统中默认是关闭coredump核心转储的,也就不会产生core文件。由于在C/C++开发中会用到gdb调试,所以需要开启coredump功能。下面是具体的配置命令,可以保存为一个简单的shell脚本执行。

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27

#!/bin/bash ### Filename: coredumpshell.sh ### Description: enable coredump and format the name of core file on centos system # enable coredump whith unlimited file-size for all users echo -e "\n# enable coredump whith unlimited file-size for all users\n* soft core unlimited" >> /etc/security/limits.conf # set the path of core file with permission 777 cd /mydata && mkdir corefile && chmod 777 corefile # format the name of core file. # %% – 符号% # %p – 进程号 # %u – 进程用户id # %g – 进程用户组id # %s – 生成core文件时收到的信号 # %t – 生成core文件的时间戳(seconds since 0:00h, 1 Jan 1970) # %h – 主机名 # %e – 程序文件名 echo -e "/mydata/corefile/core-%e-%s-%u-%g-%p-%t" > /proc/sys/kernel/core_pattern # for centos7 system(update 2017.2.3 21:44) echo -e "/mydata/corefile/core-%e-%s-%u-%g-%p-%t" > /etc/sysctl.conf # suffix of the core file name echo -e "1" > /proc/sys/kernel/core_uses_pid

在Linux终端上执行完上面的脚本后,退出并重新登录即可生效。可以通过[root@typecodes ~]# ulimit -a命令查看效果,如下图所示:

CentOS7.1开启coredump核心转储功能
CentOS7.1开启coredump核心转储功能

最后,写了一个简单C程序测试CentOS是否生成了相应的core文件。

代码语言:javascript
复制
[root@typecodes test]# vim test.c 
#include <stdio.h>
int main( int argc, char * argv[] )
{
    char a[1];
    scanf( "%s", a );
    return 0;
}

使用命令gcc test.c -o test编译后执行,然后故意输入错误的数据,让进程报错。这时,在/mydata/corefile/目录就生成了相应的core文件core-test-11-0-0-27124-1434029850

core文件生成目录
core文件生成目录
update 2017.2.3 21:44

每次重启CentOS7后,由于proc目录是动态更新的,所以系统会初始化core_pattern的值。如果想永久设置coredump文件为上面的格式,那么需要使用编辑文件 /etc/sysctl.conf ,加入kernel.core_pattern=/mydata/corefile/core-%e-%s-%u-%g-%p-%t,然后使用命令sysctl -p /etc/sysctl.conf重新加载。

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24

#!/bin/bash ### Filename: coredumpshell.sh ### Description: enable coredump and format the name of core file on centos system # enable coredump whith unlimited file-size for all users echo -e "\n# enable coredump whith unlimited file-size for all users\n* soft core unlimited" >> /etc/security/limits.conf # format the name of core file. # %% – 符号% # %p – 进程号 # %u – 进程用户id # %g – 进程用户组id # %s – 生成core文件时收到的信号 # %t – 生成core文件的时间戳(seconds since 0:00h, 1 Jan 1970) # %h – 主机名 # %e – 程序文件名 # for centos7 system(update 2017.4.2 21:44) echo -e "\nkernel.core_pattern=/mydata/corefile/core-%e-%s-%u-%g-%p-%t" >> /etc/sysctl.conf # suffix of the core file name echo -e "1" > /proc/sys/kernel/core_uses_pid sysctl -p /etc/sysctl.conf

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2015-06-11 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • update 2017.2.3 21:44
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档