1 设置
vi /etc/sysctl.conf
kernel.core_pattern=/home/allcores/core-%e-%p-%s-%t
sysctl -p
在home目录下创建allcores文件夹
附上core文件支持的格式列表:
%p – insert pid into filename 【pid】
%u – insert current uid into filename 【uid】
%g – insert current gid into filename 【gid】
%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 【应用的名字
2 确认
more /proc/sys/kernel/core_pattern查看是否修改成功
3 测试
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <sys/resource.h>
int main()
{
struct rlimit rl;
rl.rlim_cur = RLIM_INFINITY;
rl.rlim_max = RLIM_INFINITY;
setrlimit(RLIMIT_CORE,&rl);
int i = 0;
int *p = &i;
p = NULL;
i = *p;
printf("%s", p);
printf("printf success!\n");
return 0;
}