从核心文件获取程序开始时间 [英] Get program start time from core file

查看:128
本文介绍了从核心文件获取程序开始时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个来自长时间运行的进程的核心文件。这有助于我获得该进程的开始时间。



在linux下有没有一种方法,使用gdb只从核心文件获得进程启动时间?



谢谢

解决方案


在Linux下,使用gdb只从核心文件获得进程启动时间?


我不这么认为。不过,请参阅核心(5) proc(5)并运行 file(1)命令在核心 file。



将来,您可以使用 / proc / sys / kernel / core_pattern 在系统启动时)。 %t 给出了 dump 的时间(不是开始时间)。

我猜你已经拥有了core-dumping可执行文件的源代码(否则,检查核心在实践中并不是非常有用)。



我建议改善你的源代码长时间运行的程序将时间(2)转换为全局变量或静态变量,接近 main 的开头。
您可能还想在源代码中添加时间戳。






假设您正在构建 make 并使用 git 作为版本控制来获取(源代码的)时间戳:



无论是使用 __ DATE __ 还是 __ TIME __ 生成的包含日期和文件的C文件当前 git commit 日志。如果您使用 make 请随意添加一些规则,如

  _timestamp。 c:Makefile 
@date +'const char my_timestamp [] =%c;'> _timestamp.tmp
@(echo -n'const char my_lastgitcommit [] =''; \
git log --format = oneline --abbrev = 12 --abbrev-commit -q \
| head -1 | tr -d'\\\
\r\f\\\\\\'; \
echo';')>> _timestamp.tmp
@(echo -n'const char my_lastgittag [] =''; \
(git describe --abbrev = 0 --all || echo'* notag *')| \\ \\
tr -d'\\\
\r\f\\\\\'; echo';')>> _timestamp.tmp
@mv _timestamp.tmp _timestamp.c

在您的 Makefile (并在构建可执行文件的规则中,将它与 _timestamp.o 连接并添加 $(RM )_timestamp.c 在其食谱中)。






我推荐,对于长时间运行的程序如服务器(或批量数字代码运行数天),以使用 syslog (3)设施。某些初始 syslog 可能会显示 my_timestamp my_lastgitcommit my_lastgittag ,如上面 _timestamp.c 中生成的那样。


I have a core file from a long running process. It would help me to have the start time of that process.

Is there a way under linux, using gdb to get a process start time only from core file?

Thanks

解决方案

Is there a way under linux, using gdb to get a process start time only from core file?

I don't think so. However, see core(5) & proc(5) and run the file(1) command on your core file.

In the future, you might use /proc/sys/kernel/core_pattern (at system startup time). The %t gives the time of dump (not the start time).

I guess you have the source code of the core-dumping executable (otherwise, inspecting the core is in practice not very helpful).

I suggest to improve the source of your long-running program to put the result of time(2) into a global or static variable, near the start of main. You probably want also to put a timestamp inside your source code.


here is a trick to get a timestamp (of your source code), assuming you are building with make and using git as version control:

Either use just using __DATE__ and __TIME__ in some C or C++ file, or more probably an additional generated C file containing the date and e.g. the current git commit log. If you use make feel free do add some rule like

_timestamp.c: Makefile
        @date +'const char my_timestamp[]="%c";' > _timestamp.tmp
        @(echo -n 'const char my_lastgitcommit[]="' ; \
           git log --format=oneline --abbrev=12 --abbrev-commit -q  \
             | head -1 | tr -d '\n\r\f\"\\\\' ; \
           echo '";') >> _timestamp.tmp
        @(echo -n 'const char my_lastgittag[]="'; \
           (git describe --abbrev=0 --all || echo '*notag*') | \
             tr -d '\n\r\f\"\\\\'; echo '";') >> _timestamp.tmp
        @mv _timestamp.tmp _timestamp.c

in your Makefile (and in the rule building your executable, link it with also _timestamp.o and add $(RM) _timestamp.c in its recipes).


I recommend, for long-running programs such as servers (or batch numerical code running for days), to use syslog(3) facilities inside them. Some initial syslog might show my_timestamp, my_lastgitcommit, my_lastgittag as generated in _timestamp.c above.

这篇关于从核心文件获取程序开始时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆