Linux下core文件的使用方法详解

吾爱主题 阅读:124 2024-04-05 13:53:13 评论:0

前言

有时候程序会异常退出而不带任何日志,此时就可以使用 code 文件进行分析,它会记录程序运行的内存,寄存器,堆栈指针等信息

什么是core文件

通常在 Linux 下遇到程序异常退出或者中止,我们都会使用 core 文件进行分析,其中包含了程序运行时的内存,寄存器,堆栈指针等信息,格式为 ELF ,可以理解是程序工作当前状态转储成一个文件,通过工具分析这个文件,我们可以定位到程序异常退出或者终止时相应的堆栈调用等信息,为解决问题提供帮助。

使用core文件调试

生成方法

查看当前 core 文件的状态

?
1 2 3 4 $ ulimit -a ... -c: core file size (blocks)   0 # 关闭状态 ...

打开生成开关

?
1 2 3 4 5 ulimit -c unlimited ulimit -a ... -c: core file size (blocks)   unlimited ...

对 core 文件的大小进行限制,单位为 blocks ,一般 1 block=512 bytes ,设置太小可能导致不会生成文件

?
1 2 3 4 5 $ ulimit -c 1024 $ ulimit -a ... -c: core file size (blocks)   1024 ...

关闭生成开关

?
1 2 3 4 5 ulimit -c 0 ulimit -a ... -c: core file size (blocks)   0 ...

上面对 core 文件的操作仅对当前生效,若需要永久生效,则要将相应操作写入 /etc/profile

生成路径

core 文件默认生成在程序的工作目录,可以对生成路径进行设置,需要保证对对应目录有足够空间并具有写权限

?
1 echo /MyCoreDumpDir/core .%e.%p > /proc/sys/kernel/core_pattern

其中命名使用的参数列表

?
1 2 3 4 5 6 7 %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 # 添加命令名

/proc/sys/kernel/core_uses_pid 这个文件的值若为1,则无论时候配置 %p ,最后生成的 core 文件都会添加 pid

调试方法

可以使用 gdb 对 core 文件进行调试,编译是需要带上 -g 选项

?
1 2 3 4 5 6 $ gdb a.out ... (gdb) core- file core ... (gdb) bt ...

如需要在 PC 上调试嵌入式设备产生的 core 文件,则需要选取相应平台的 gdb 工具,并在进入 gdb 后设置符号文件的位置

?
1 2 3 4 5 6 7 8 $ xxx-xxx-gdb a.out ... (gdb) solib-search-path xxx.so:xxx.so ... (gdb) core- file core ... (gdb) bt ...

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对服务器之家的支持。

原文链接:http://answerywj.com/2018/03/07/usage-of-core-in-linux/

可以去百度分享获取分享代码输入这里。
声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

【腾讯云】云服务器产品特惠热卖中
搜索
标签列表
    关注我们

    了解等多精彩内容