良许Linux教程网 干货合集 详解Linux core文件

详解Linux core文件

1. core文件的简单介绍

我们经常听到大家说到程序core掉了,需要定位解决,这里说的大部分是指对应程序由于各种异常或者bug导致在运行过程中异常退出或者中止,并且在满足一定条件下(这里为什么说需要满足一定的条件呢?下面会分析)会产生一个叫做core的文件。

通常情况下,core文件会包含了程序运行时的内存,寄存器状态,堆栈指针,内存管理信息还有各种函数调用堆栈信息等,我们可以理解为是程序工作当前状态存储生成第一个文件,许多的程序出错的时候都会产生一个core文件,通过工具分析这个文件,我们可以定位到程序异常退出的时候对应的堆栈调用等信息,找出问题所在并进行及时解决。

u=2346571094,1872445124&fm=26&gp=0

2. 开启或关闭core文件的生成用以下命令来阻止系统生成core文件:ulimit -c 0下面的命令可以检查生成core文件的选项是否打开:ulimit -a该命令将显示所有的用户定制,其中选项-a代表“all”。

也可以修改系统文件来调整core选项在/etc/profile通常会有这样一句话来禁止产生core文件,通常这种设置是合理的:# No core files by defaultulimit -S -c 0 > /dev/null 2>&1但是在开发过程中有时为了调试问题,还是需要在特定的用户环境下打开core文件产生的设置在用户的~/.bash_profile里加上ulimit -c unlimited来让特定的用户可以产生core文件如果ulimit -c 0 则也是禁止产生core文件,而ulimit -c 1024则限制产生的core文件的大小不能超过1024kb

3. 设置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” > /proc/sys/kernel/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 添加命令名

4. 使用core文件在core文件所在目录下键入:gdb -c core它会启动GNU的调试器,来调试core文件,并且会显示生成此core文件的程序名,中止此程序的信号等等如果你已经知道是由什么程序生成此core文件的,比如MyServer崩溃了生成core.12345,那么用此指令调试:gdb -c core MyServer以下怎么办就该去学习gdb的使用了

5. 一个小方法来测试产生core文件直接输入指令:kill -s SIGSEGV $$

6. **为何有时程序**Down了,却没生成 **Core**文件。

Linux下,有一些设置,标明了resources available to the shell and to processes。 可以使用

#ulimit -a 来看这些设置。 (ulimit是bash built-in Command)

-a All current limits are reported -c The maximum size of core files created -d The maximum size of a process**鈥檚 data segment -e The maximum scheduling priority (“nice”) -f The maximum size of files written by the shell and its children -i The maximum number of pending signals -l The maximum size that may be locked into memory -m The maximum resident set size (has no effect on Linux) -n The maximum number of open file descriptors (most systems do not allow this value to be set) -p The pipe size in 512-byte blocks (this may not be set) -q The maximum number of bytes in POSIX message queues -r The maximum real-time scheduling priority -s The maximum stack size -t The maximum amount of cpu time in seconds -u The maximum number of processes available to a single user -v The maximum amount of virtual memory available to the shell -x The maximum number of file locks

从这里可以看出,如果 -c是显示:core file size (blocks, -c)

如果这个值为0,则无法生成core文件。所以可以使用:

#ulimit -c 1024 或者 #ulimit -c unlimited 来使能 core文件。

如果程序出错时生成Core 文件,则会显示Segmentation fault (core dumped)

**7. Core Dump**的核心转储文件目录和命名规则:/proc/sys/kernel/core_uses_pid可以控制产生的core文件的文件名中是否添加pid作为扩展,如果添加则文件内容为1,否则为0

以上就是良许教程网为各位朋友分享的Linu系统相关内容。想要了解更多Linux相关知识记得关注公众号“良许Linux”,或扫描下方二维码进行关注,更多干货等着你 !

img
本文由 良许Linux教程网 发布,可自由转载、引用,但需署名作者且注明文章出处。如转载至微信公众号,请在文末添加作者公众号二维码。
良许

作者: 良许

良许,世界500强企业Linux开发工程师,公众号【良许Linux】的作者,全网拥有超30W粉丝。个人标签:创业者,CSDN学院讲师,副业达人,流量玩家,摄影爱好者。
上一篇
下一篇

发表评论

联系我们

联系我们

公众号:良许Linux

在线咨询: QQ交谈

邮箱: yychuyu@163.com

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

关注微博
返回顶部