Redhat Linux下如何生成core dump文件

 

使用C/C++语言开发程序时,当程序crash的时候产生core dump文件对于调试程序是很有帮助的。在Redhat Linux系统中默认是不生成core dump文件的,这是因为在/etc/profile文件中有这样一行

 

ulimit -S -c 0 > /dev/null 2>&1

 

 

如何打开core dump呢?最简单的方法是用户在自己的~/.bash_profile中加入ulimit -S -c unlimited > /dev/null 2>&1,这样设置后允许当前用户生成没有大小限制的core dump文件。此外还有两种系统级修改生成core dump的方法。

 

第一种方法是修改/etc/profile,把ulimit那一行改为

 

ulimit -S -c unlimited > /dev/null 2>&1

 

这样设置后系统允许所有用户生成没有大小限制的core dump文件。这样做的优点是不需要重起系统,缺点是无法控制只让某些用户生成core dump文件。

 

第二种方法是修改/etc/security/limits.conf文件。很多系统上限都可以通过修改这个文件改变,如最大子进程个数,最大打开文件数等等。这个文件有详细的注释,对如何修改这个文件做了说明。如果想对所有用户打开core dump,可以加入一行

 

* soft core 0

 

如果只想对某些用户或用户组打开core dump,可以加入

 

user soft core 0@group soft core 0

 

注意如果通过修改/etc/security/limits.conf文件打开core dump,还需要注释掉/etc/profile中的ulmit那一行

 

#ulimit -S -c 0 > /dev/null 2>&1

 

这样修改的优点是可以针对特定用户或特定组打开core dump文件,缺点是需要重起系统。

 

 

最后说一下生成core dump文件的位置,默认位置与可执行程序在同一目录下,文件名是core.***,其中***是一个数字。core dump文件名的模式保存在/proc/sys/kernel/core_pattern中,缺省值是core。通过以下命令可以更改core dump文件的位置(如希望生成到/tmp/cores目录下)

 

echo "/tmp/cores/core" > /proc/sys/kernel/core_pattern 

 

core dump的概念:

A core dump is the recorded state of the working memory of a computer program at a specific time, generally when the program has terminated abnormally (crashed). In practice, other key pieces of program state are usually dumped at the same time, including the processor registers, which may include the program counter and stack pointer, memory management information, and other processor and operating system flags and information. The name comes from the once-standard memory technology core memory. Core dumps are often used to diagnose or debug errors in computer programs.

On many operating systems, a fatal error in a program automatically triggers a core dump, and by extension the phrase "to dump core" has come to mean, in many cases, any fatal error, regardless of whether a record of the program memory is created.


在linux平台下,设置core dump文件生成的方法:

1) 在终端中输入ulimit -c 如果结果为0,说明当程序崩溃时,系统并不能生成core dump。

2) 使用ulimit -c unlimited命令,开启core dump功能,并且不限制生成core dump文件的大小。如果需要限制,加数字限制即可。ulimit - c 1024

3) 默认情况下,core dump生成的文件名为core,而且就在程序当前目录下。新的core会覆盖已存在的core。通过修改/proc/sys/kernel/core_uses_pid文件,可以将进程的pid作为作为扩展名,生成的core文件格式为core.xxx,其中xxx即为pid

4) 通过修改/proc/sys/kernel/core_pattern可以控制core文件保存位置和文件格式。例如:将所有的core文件生成到/corefile目录下,文件名的格式为core-命令名-pid-时间戳. echo "/corefile/core-%e-%p-%t" > /proc/sys/kernel/core_pattern

转自:

posted @ 2011-12-06 18:59  夏大王  阅读(1225)  评论(0编辑  收藏  举报