2007年3月21日星期三

Core Dump

Core Dump?!

何谓 core?
    在使用半导体作为内存的材料前,人类是利用线圈当作内存的材料(发明 者为王安),线圈就叫作 core ,用线圈做的内存就叫作 core memory。如今 ,半导体工业澎勃发展,已经没有人用 core memory 了,不过,在许多情况下, 人们还是把记忆体叫作 core 。
何谓 core dump?
    我们在开发(或使用)一个程序时,最怕的就是程序莫明其妙地当掉。虽然系 统没事,但我们下次仍可能遇到相同的问题。于是这时操作系统就会把程序当掉 时的内存内容 dump 出来(现在通常是写在一个叫 core 的 file 里面),让 我们或是 debugger 做为参考。这个动作就叫作 core dump。
为何会发生 core dump?
    前面说过,在程序当掉时出错。在 C/C++语言中,最常发生错误的地方就是指 针有问题。您可以利用 core 文件和 debugger 把错误找出来(要怎麽在 debugger 中使用 core 文件?man 一下 gdb 吧!)。
我可以把 core 文件删掉吗?
    如果你不会、不能、不需要修改程序,那就放心地把它删除了吧!
要怎麽才不会让 core 文件出现?
    如果用的是tcsh的话, 以试著在 .tcshrc 里加一行:
    limit coredumpsize 0
    如果用的是bash的话, 在/etc/profile里加上(或者修改)一条:
    ulimit -c 0
有一招, 可以让你看出 core 最好用的地方 :)

gdb -c core, 进去後打 where, 就可以 show 出你是在程序哪一行当掉的, 还有在当掉时在哪个 function 里, 这个 function 是被哪个 function 所 call 的, 而这个 function 又是被哪个 function 所 call 的.... 一直到 main()

由这个信息, 可以找出五六成的 bug........ 屡试不爽

但, 先决条件, 当你在 compile 时必须把 debug information 的选项打开 不然, 就会出现一大堆你看不懂的东西,而不是你喜欢的源程序。

在编程的过程中,不可避免地会有一误操作,当然这些误操作编译器是不知道的,你运行gcc命令编译程序,编译通过了生成可运行程序了。这个程序虽然可以运行了,但可能是不正常的。
比如有的程序只顾着打开文件却不关闭文件,因为操作系统会为每个打开的文件分配一个句柄(file descriptor),这样的程序长久运行下去必然导致打开的文件句柄超出系统资源限制。超出系统资源限制的后果可能不仅仅影响到当前用户,整个系统都可能会受影响。
又比如有些程序,比如下面的代码:

#include
#include
#include
#include
int main()
{
while(1) if(fork() <>
这段代码虽然编译完全正确,也能运行。但它会导致系统死机,因为它不断产生进程,每产生一个进程,系统必然耗费一些资源,最终导致系统资源耗尽而死机。
作为一个初学者,编程过程中发生这些失误再所难免,但如果因为这样的错误就导致我们要不断重新启动计算机,甚至不小心丢失一些还未来得及保存的数据,那就可惜了。
Linux系统中提供了一些保护机制,我们可以避免不必要的麻烦。其中bash提供这样一个ulimit命令就可以用来帮助我们实现资源配置的目的。查看bash手册可以看到这些:
ulimit [-SHacdefilmnpqrstuvx [limit]]
Provides control over the resources available to the shell and to processes started by it, on systems that allow such con‐
trol. The -H and -S options specify that the hard or soft limit is set for the given resource. A hard limit cannot be
increased once it is set; a soft limit may be increased up to the value of the hard limit. If neither -H nor -S is speci‐
fied, both the soft and hard limits are set. The value of limit can be a number in the unit specified for the resource or
one of the special values hard, soft, or unlimited, which stand for the current hard limit, the current soft limit, and no
limit, respectively. If limit is omitted, the current value of the soft limit of the resource is printed, unless the -H
option is given. When more than one resource is specified, the limit name and unit are printed before the value. Other
options are interpreted as follows:
-a All current limits are reported
-c The maximum size of core files created
-d The maximum size of a process’s data segment
-e The maximum scheduling priority (‘nice’)
-f The maximum size of files created by the shell
-i The maximum number of pending signals
-l The maximum size that may be locked into memory
-m The maximum resident set size
-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 rt 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

If limit is given, it is the new value of the specified resource (the -a option is display only). If no option is given,
then -f is assumed. Values are in 1024-byte increments, except for -t, which is in seconds, -p, which is in units of
512-byte blocks, and -n and -u, which are unscaled values. The return status is 0 unless an invalid option or argument is
supplied, or an error occurs while setting a new limit.
这是一个bash内嵌命令(要通过man bash命令才能查看到)。用来设置当前shell及其开启的进程可使用的资源的。
你可以用下列命令试一下:
ulimit -a
这个命令显示所有当前资源限制情况。
限制有两种,一种是软限制(soft limit),另一种是硬限制(hard limit)。比如:
ulimit -S -f 1024
这个命令将限制当前shell创建的文件大小为1024K,所以你可能没办法产生一个超过1M的文件。但这是软限制。
又比如:
ulimit -H -u 100
这个命令将设置系统中当前用户能够开启的进程最多为100个,所以这一设置上面那个fork程序就不会让系统死机了。

软限制是可以突破的,而硬限制是不能突破的,软限制还可以突破到硬限制设定的值来。

用ulimit的另外一个好处就是设置系统资源以满足进行特定测试。
比如我们想要调试程序,可能有些系统默认是限制产生core dump文件的,而我们知道,core dump文件是我们调试程序的一个很有用的帮助文件,根据core dump文件提供的信息,我们可以比较快地定位到程序的bug。
如果用命令:
ulimit -c
看到的是0,则说明系统限制了产生core dump,我们可以设定一个文件大小以产生core dump文件。比如:
ulimit -c 1024
将设置允许产生的core dump文件最大为1024K
当然我们也可以设置为
ulimit -c unlimited
表示不限制产生的core dump文件的大小。


没有评论: