linux下开启程序崩溃生成core文件开关之ulimit详解

来源:互联网 发布:yum安装mysql 编辑:程序博客网 时间:2024/06/02 23:48

运行服务器程序经常会出现崩溃,而我们不可能一天24小时都等着他出现。在实际运行中也不能总是使用gdb启动,毕竟gdb绑定运行会消耗很大一部分性能。

不过linux系统在程序崩溃时会生成一个coredump文件,里面保存程序收到退出信号时的堆栈信息,对于调试c++程序是很有帮助的,ulimit命令就是设置程序崩溃时生成coredump文件的开关和大小。

使用命令在shell终端设置,只对当前终端有效,修改当前用户下配置文件,只对当前用户有效,修改etc下系统配置,对所有用户有效,这些都是通用的规律了。

使用命令gdb打开文件就可以调试查看崩溃的堆栈了,如果使用python调用的c++程序,打开core文件命令为gdb python core,使用c语言生产的coredump,打开命令为gdb c core,使用c++生成的coredump,打开命令为gdb cpp core。

查看当前生成core文件设置

ulimit -c

为0表示不生成core文件,非0的数字表示生成core文件大小,为unlimited表示不限制生成core文件大小。

ulimit -c 1024

表示设置core文件大小为1024kb,单位是kb。

ulimit -c unlimited

表示设置core文件大小不受限制。

顺便看看其他选项有什么用:

$ ulimit -h-bash: ulimit: -h: 无效选项ulimit: 用法: ulimit [-SHabcdefilmnpqrstuvxT] [limit]
输入ulimit -h和ulimit --help都提示无效的选项,下面出现了-SH两个大写的选项,还有一个-T也是大写的,还有其他的一串小写的选项,不过也不知道什么意思。

使用man ulimit看看

ULIMIT(3)                                                                            Linux Programmer's Manual                                                                           ULIMIT(3)NAME       ulimit - get and set user limitsSYNOPSIS       #include <ulimit.h>       long ulimit(int cmd, long newlimit);DESCRIPTION       Warning: This routine is obsolete.  Use getrlimit(2), setrlimit(2), and sysconf(3) instead.  For the shell command ulimit(), see bash(1).       The ulimit() call will get or set some limit for the calling process.  The cmd argument can have one of the following values.       UL_GETFSIZE              Return the limit on the size of a file, in units of 512 bytes.       UL_SETFSIZE              Set the limit on the size of a file.       3      (Not implemented for Linux.)  Return the maximum possible address of the data segment.       4      (Implemented but no symbolic constant provided.)  Return the maximum number of files that the calling process can open.RETURN VALUE       On success, ulimit() returns a nonnegative value.  On error, -1 is returned, and errno is set appropriately.ERRORS       EPERM  A unprivileged process tried to increase a limit.ATTRIBUTES       For an explanation of the terms used in this section, see attributes(7).       ┌──────────┬───────────────┬─────────┐       │Interface │ Attribute     │ Value   │       ├──────────┼───────────────┼─────────┤       │ulimit()  │ Thread safety │ MT-Safe │       └──────────┴───────────────┴─────────┘CONFORMING TO       SVr4, POSIX.1-2001.  POSIX.1-2008 marks ulimit() as obsolete.SEE ALSO       bash(1), getrlimit(2), setrlimit(2), sysconf(3)COLOPHON       This  page  is  part  of release 4.04 of the Linux man-pages project.  A description of the project, information about reporting bugs, and the latest version of this page, can be found at       http://www.kernel.org/doc/man-pages/.Linux                                                                                       2015-03-29                                                                                   ULIMIT(3)

好像是一个系统接口函数说明,对我们没什么用,不过里面提到了getrlimit与setrlimit两个接口,这两个接口是编写c/c++程序可以用到的,他可以使用调用接口对运行程序修改值,不用修改配置也不影响其他的程序的限制。

不过可以通过ulimit -a这个命令来查看其他选项的用途,查看ulimit设置的所有系统限制:

$ ulimit -acore file size          (blocks, -c) unlimiteddata seg size           (kbytes, -d) unlimitedscheduling priority             (-e) 0file size               (blocks, -f) unlimitedpending signals                 (-i) 127350max locked memory       (kbytes, -l) 64max memory size         (kbytes, -m) unlimitedopen files                      (-n) 1024pipe size            (512 bytes, -p) 8POSIX message queues     (bytes, -q) 819200real-time priority              (-r) 0stack size              (kbytes, -s) 8192cpu time               (seconds, -t) unlimitedmax user processes              (-u) 127350virtual memory          (kbytes, -v) unlimitedfile locks                      (-x) unlimited

上面列出了前面各个选项的意思,其中大写的-H和-S分别是系统硬性限制和用户软件限制,-T和-b不知道什么意思。

一般在当前目录生成名称为core的coredump文件,为了防止多次coredump生成时文件覆盖,通过查阅其他的资料,生成coredump文件的目录和命名格式都是可以修改的。


阅读全文
0 0
原创粉丝点击