博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Linux core 文件介绍
阅读量:2387 次
发布时间:2019-05-10

本文共 4301 字,大约阅读时间需要 14 分钟。

1. core文件的简单介绍

在一个程序崩溃时,它一般会在指定目录下生成一个core文件。core文件仅仅是一个内存映象(同时加上调试信息),主要是用来调试的。

2. 开启或关闭core文件的生成

用以下命令来阻止系统生成core文件:

ulimit -c 0

下面的命令可以检查生成core文件的选项是否打开:

ulimit -a

该命令将显示所有的用户定制,其中选项-a代表“all”。

程序崩溃时生成的Core文件大小即为程序运行时占用的内存大小

也可以修改系统文件来调整core选项

在/etc/profile通常会有这样一句话来禁止产生core文件,通常这种设置是合理的:

# No core files by default

ulimit -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 添加命令名  

在shell里使用命令:ulimit -c unlimited,这样进行修改只是对本次会话有效,是临时的,如果想让修改永久生效,则需要修改配置文件,如 .bash_profile/etc/profile/etc/security/limits.conf

4. 使用core文件

在core文件所在目录下键入:

gdb -c core

它会启动GNU的调试器,来调试core文件,并且会显示生成此core文件的程序名,中止此程序的信号等等

如果你已经知道是由什么程序生成此core文件的,比如MyServer崩溃了生成core.12345,那么用此指令调试:

gdb -c core MyServer

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

在程序不寻常退出时,内核会在当前工作目录下生成一个core文件(是一个内存映像,同时加上调试信息)。使用gdb来查看core文件,可以指示出导致程序出错的代码所在文件和行数。

1.core文件的生成开关和大小限制


 (1)使用ulimit -c命令可查看core文件的生成开关。若结果为0,则表示关闭了此功能,不会生成core文件。


 (2)使用ulimit -c filesize命令,可以限制core文件的大小(filesize的单位为kbyte)。若ulimit -c unlimited,则表示core文件的大小不受限制。如果生成的信息超过此大小,将会被裁剪,最终生成一个不完整的core文件。在调试此core文件的时候,gdb会提示错误。

2.core文件的名称和生成路径


core文件生成路径:


输入可执行文件运行命令的同一路径下。
若系统生成的core文件不带其它任何扩展名称,则全部命名为core。新的core文件生成将覆盖原来的core文件。

(1)/proc/sys/kernel/core_uses_pid可以控制core文件的文件名中是否添

加pid作为扩展。文件内容为1,表示添加pid作为扩展名,生成的core文件格式为core.xxxx;为0则表示生成的core文件同一命名为core。
可通过以下命令修改此文件:
echo "1" > /proc/sys/kernel/core_uses_pid

(2)proc/sys/kernel/core_pattern可以控制core文件保存位置和文件名格式。
可通过以下命令修改此文件:
echo "/corefile/core-%e-%p-%t" > 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 添加命令名

 3.core文件的查看
 core文件需要使用gdb来查看。
 gdb ./a.out
 core-file core.xxxx
 使用bt命令即可看到程序出错的地方。 
以下两种命令方式具有相同的效果,但是在有些环境下不生效,所以推荐使用上面的命令。 
(1)gdb -core=core.xxxx
file ./a.out
bt 
(2)gdb -c core.xxxx
file ./a.out
bt

转载地址:http://posab.baihongyu.com/

你可能感兴趣的文章
进度条例子
查看>>
C#获取CPU序列号代码、硬盘ID、网卡硬件地址等类文件
查看>>
Html常用符号
查看>>
WinForm控制Webbrowser自动登录
查看>>
access表(.mdb文件) 导入 power designer
查看>>
PowerDesigner如何设计表之间的关联
查看>>
SQLite通用数据库类
查看>>
外部修改应用程序图标的做法
查看>>
database disk image is malformed解决方法
查看>>
实现自定义对话框程序快捷键的两种方法
查看>>
如何对抗微软霸权,google给我们上了一课
查看>>
忆父亲
查看>>
png库结合zlib库使用出现的一个链接问题的解决
查看>>
STL数组和com数组相互转换的做法
查看>>
开发平台软件中关于第三方库管理的一些思考
查看>>
svn创建分支的做法
查看>>
“当前不会命中断点。源代码与原始版本不同”的问题的有效解决办法
查看>>
对面向对象和面向过程的一些新理解
查看>>
软件开发中的资源管理
查看>>
有关博客的一些断想
查看>>