bandaoyu 2018-02-26 07:53 采纳率: 55.6%
浏览 1611
已采纳

linux 的core file的时间戳怎么样才能变成日期格式

之前问了一个问题:

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

  • 写回答

3条回答 默认 最新

  • jijicanyu 2018-02-26 08:48
    关注

    第一步: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目录下

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥15 绘制多分类任务的roc曲线时只画出了一类的roc,其它的auc显示为nan
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?