- 查看生成core文件的开关是否开启;
[root@VM-0-17-centos bin]# ulimit -a
说明:第一行core文件大小为0,没有开启。
2.使用#ulimit -c [kbytes]可以设置系统允许生成的core文件大小;
ulimit -c 0 不产生core文件
ulimit -c 100 设置core文件最大为100k
ulimit -c unlimited 不限制core文件大小
[root@VM-0-17-centos bin]# ulimit -c unlimited
##查看配置
[root@VM-0-17-centos bin]# ulimit -a
修改/etc/profile文件添加参数;
1. 在profile文件中加入ulimit -c unlimited
[root@VM-0-17-centos bin]# vim /etc/profile
ulimit -c unlimited
[root@VM-0-17-centos bin]# source /etc/profile
[root@VM-0-17-centos bin]# cat /proc/sys/kernel/core_uses_pid 0 -- 修改 /proc/sys/kernel/core_uses_pid 文件内容为: 1 有下面的两种方式 随便选一种 [root@VM-0-17-centos bin]# echo "1" > /proc/sys/kernel/core_uses_pid [root@VM-0-17-centos bin]# sysctl -w kernel.core_uses_pid=1 kernel.core_uses_pid = 1
-- 查看core文件的存在位置: [root@VM-0-17-centos bin]# cat /proc/sys/kernel/core_pattern core
基本两种情况
-- core文件统一生成指定到/corefile目录下,产生的文件名为core-命令名-pid-时间戳
[root@VM-0-17-centos bin]# mkdir /corefile
[root@VM-0-17-centos bin]# vi /etc/sysctl.conf
kernel.core_uses_pid = 1
kernel.core_pattern=/corefile/core-%e-%p
[root@VM-0-17-centos bin]# sysctl -p /etc/sysctl.conf
以下是参数列表:
%p - insert pid into filename 添加pid(进程id)
%u - insert current uid into filename 添加当前uid(用户id)
%g - insert current gid into filename 添加当前gid(用户组id)
%s - insert signal that caused the coredump into the filename 添加导致产生core的信号
%t - insert UNIX time that the coredump occurred into filename 添加core文件生成时的unix时间
%h - insert hostname where the coredump happened into filename 添加主机名
%e - insert coredumping executable name into filename 添加导致产生core的命令名
## 1. 修改 /etc/systemd/coredump.conf 设置 ProcessSizeMax ExternalSizeMax 参数值
## 2. 建议值比数据库使用总内存量大,即大于top命令下的dmserver的virt值
##1.修改配置文件
[root@kylin ~]# vi /etc/systemd/coredump.conf
[Coredump]
#Storage=external
#Compress=yes
ProcessSizeMax=2G # 需大于top命令下的dmserver的virt值
ExternalSizeMax=2G # 需大于top命令下的dmserver的virt值
#JournalSizeMax=767M
#MaxUse=
#KeepFree=
##2.重启coredump进程
##重新读取配置文件
[root@kylin ~]# systemctl daemon-reload
##重启进程
[root@kylin ~]# systemctl restart systemd-coredump.socket
##注意如曾经修改过/etc/sysctl.conf文件中core文件的生成路径的话,需要将core文件的路径还原为默认路径即做如下修改后,再修改coredump参数
[root@VM-0-17-centos bin]# echo "|/usr/lib/systemd/systemd-coredump %P %u %g %s %t %c %h">proc/sys/kernel/core_pattern
[root@VM-0-17-centos bin]# echo "1">/proc/sys/kernel/core_uses_pid
[root@VM-0-17-centos bin]# sysctl -p /etc/sysctl.conf
新建test.c文件
#include<stdio.h>
void test(){
int *p = NULL;
*p = 0;
}
int main()
{
test();
return 0;
}
[root@VM-24-17-centos bin]# vim test.c [root@VM-24-17-centos bin]# gcc -o test1 test.c [root@VM-24-17-centos bin]# ./test 1 段错误 (核心已转储) [root@VM-24-17-centos bin]# ll /corefile/* -rw------- 1 root root 249856 12月 7 19:20 /corefile/core-test1-2291735-1650281826
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int *fun()
{
int *p=(int*)malloc(1024 * 1024 * 500);//动态申请空间
memset(p, 0, 1024 * 1024 * 500);
return p;//返回动态申请的空间是可以的.
}
int main(int argc, char *argv[])
{
int create_num;
if(argc == 2){
create_num=atoi(argv[1])*2;
printf("coredump Generate Size is %dG \n",create_num/2);
}else{
create_num=5;
printf("coredump Generate Size is 2.5G by default\n");
}
char *arr[create_num];
int i =0;
for(i =0;i<create_num;i++){
arr[i]=(char *)malloc(1024 * 1024 * 512);
memset(arr[i], 0, 1024 * 1024 * 512);
// int *p_t = (int*)malloc(1024 * 1024 * 500);
// memset(arr[i], 0, 1024 * 1024 * 500);
// arr[i]= (char *)fun();
// printf("create_num is %d \n",create_num);
// printf("num is %d \n",i);
if(i>=create_num-1){
printf("Generating coredump, please wait\n");
int *core_p1 =NULL;
*core_p1=0;
}
}
printf("end end \n");
return 0;
}
#!/bin/bash
rm -rf coremain
rm -rf ./core.*
gcc -g -o ./coremain core_main.c
#$1的值 =1,则表示生成1G的core文件,以此类推
if [ ! $1 ]; then
./coremain
else
./coremain $1
fi
##使用步骤:
##1. 将core_main.c与make.sh放到同级目录
##2. 给make.sh脚本赋权限
[dmdba@localhost ~]$ chmod 755 make.sh
##3. 执行脚本生成core
## 3.1 不指定生成core的大小,则默认是2.5g
[dmdba@localhost ~]$ ./make.sh
## 3.2 指定生成core的大小,后面跟参数即可, 210则表示生成的core文件为210G
[dmdba@localhost ~]$ ./make.sh 210
## 进行截断查看是否完整
生成core的时候会在操作系统的日志中有明显提示。需要注意查看操作系统有没有问题,如下图
文章
阅读量
获赞