之前问了一个问题:
linux 程序崩溃之后会产生core file,带有时间戳,但是时间戳总是不是很直观,每次都要转换下才能知道是什么时候产生的,怎么样才能让生成的core文件的时间戳变成日期格式,如20160911
这样的
在地址:http://ask.csdn.net/questions/332597
得到了一个朋友的回答:
自己写代码...
先修改 /proc/sys/kernel/core_pattern
|/sbin/genCore %p %t
%p - 获取crash 进程号,也能获取crash进程所在的路径
%t - 时间戳,用于转换成Y/m/d格式
genCore的源代码如下,会在crash进程同一目录下生成core.pid.MYD格式的coredump.
genCore.c
#define _GNU_SOURCE
#include
#include
#include
#include
#include
#include
#include
#define BUF_SIZE 1024
#define FILENAME_MAX_LENGTH 256
#define TIMESTAMP_MAX_LENGTH 20
int
main(int argc, char *argv[])
{
int tot, j;
ssize_t nread;
char buf[BUF_SIZE];
FILE *fp;
char cwd[PATH_MAX];
char fileName[FILENAME_MAX_LENGTH];
char strTimeStamp[TIMESTAMP_MAX_LENGTH];
struct tm *coreTimeStamp;
time_t rawTimeStamp;
/* Change our current working directory to that of the
crashing process */
snprintf(cwd, PATH_MAX, "/proc/%s/cwd", argv[1]);
chdir(cwd);
/* Write output to file "core.<pid>.<Ymd>" in that directory */
rawTimeStamp = (time_t) (atoi(argv[2]));
coreTimeStamp = localtime(&rawTimeStamp);
strftime(strTimeStamp, TIMESTAMP_MAX_LENGTH,"%Y%m%d",coreTimeStamp);
snprintf(fileName, FILENAME_MAX_LENGTH, "core.%s.%s", argv[1], strTimeStamp);
fp = fopen(fileName, "w+");
if (fp == NULL)
exit(EXIT_FAILURE);
/* Dump the core dump to file */
while ((nread = read(STDIN_FILENO, buf, BUF_SIZE)) > 0)
fwrite(buf, nread, 1, fp);
fclose(fp);
exit(EXIT_SUCCESS);
}
功能已经实现,但是core产生的位置我想固定产生到/app/core目录下,在没有使用genCore的时候我知道是这样设置:echo "/app/corefile/core-%e-%p-%t" > /proc/sys/kernel/core_pattern,用了genCore之后就不知道该如何弄了,
应该不能这么搞吧:echo " /app/corefile/|/sbin/genCore %e %p %t " > /proc/sys/kernel/core_pattern
第一步:echo "|/sbin/genCore %e %p %t" > /proc/sys/kernel/core_pattern
第二步:将代码修改为如下:
#define _GNU_SOURCE
#include
#include
#include
#include
#include
#include
#include
#define BUF_SIZE 1024
#define FILENAME_MAX_LENGTH 256
#define TIMESTAMP_MAX_LENGTH 20
int
main(int argc, char *argv[])
{
int tot, j;
ssize_t nread;
char buf[BUF_SIZE];
FILE *fp;
char cwd[PATH_MAX];
char fileName[FILENAME_MAX_LENGTH];
char strTimeStamp[TIMESTAMP_MAX_LENGTH];
struct tm *coreTimeStamp;
time_t rawTimeStamp;
/* Change our current working directory to that of the
crashing process */
snprintf(cwd, PATH_MAX, "/proc/%s/cwd", argv[1]);
chdir(cwd);
/* Write output to file "core.<pid>.<Ymd>" in that directory */
rawTimeStamp = (time_t) (atoi(argv[2]));
coreTimeStamp = localtime(&rawTimeStamp);
strftime(strTimeStamp, TIMESTAMP_MAX_LENGTH,"%Y%m%d",coreTimeStamp);
snprintf(fileName, FILENAME_MAX_LENGTH, "core.%s.%s.%s", argv[1],argv[2], strTimeStamp);
fp = fopen(fileName, "w+");
if (fp == NULL)
exit(EXIT_FAILURE);
/* Dump the core dump to file */
while ((nread = read(STDIN_FILENO, buf, BUF_SIZE)) > 0)
fwrite(buf, nread, 1, fp);
fclose(fp);
exit(EXIT_SUCCESS);
}
第三步:将genCore.c编译为genCore放到/sbin目录下
设置 Core Dump 的核心转储文件目录和命名规则
/proc/sys/kernel/core_uses_pid 可以控制产生的 core 文件的文件名中是否添加 pid 作为扩展 ,如果添加则文件内容为 1 ,否则为 0
proc/sys/kernel/core_pattern 可以设置格式化的 core 文件保存位置或文件名 ,比如原来文件内容是 core-%e
可以这样修改 :
echo "/corefile/core-%e-%p-%t" > core_pattern
将会控制所产生的 core 文件会存放到 /corefile 目录下,产生的文件名为 core- 命令名 -pid- 时间戳
以下是参数列表 :
%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 添加命令名
如果要放到/app/corefile/这个目录下,就将/app/corefile/先写入到fileName,然后再把文件名拼接到fileName中