linux 程序崩溃之后会产生core file,带有时间戳,但是时间戳总是不是很直观,每次都要转换下才能知道是什么时候产生的,怎么样才能让生成的core文件的时间戳变成日期格式,如20160911
这样的
![](https://profile-avatar.csdnimg.cn/2ec9eac8d74c4bc6bcf35f38e1fab5ea_bandaoyu.jpg!4)
linux 的core file的时间戳怎么样才能变成日期格式
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
2条回答 默认 最新
- 呆的久 2016-09-13 04:33关注
自己写代码...
先修改 /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 <sys/stat.h> #include <fcntl.h> #include <limits.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <time.h> #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); }
解决 2无用
悬赏问题
- ¥15 metadata提取的PDF元数据,如何转换为一个Excel
- ¥15 关于arduino编程toCharArray()函数的使用
- ¥100 vc++混合CEF采用CLR方式编译报错
- ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
- ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
- ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
- ¥50 如何openEuler 22.03上安装配置drbd
- ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
- ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
- ¥15 Windows11, backspace, enter, space键失灵