linux下开启程序崩溃生成core文件开关之ulimit详解
運行服務(wù)器程序經(jīng)常會出現(xiàn)崩潰,而我們不可能一天24小時都等著他出現(xiàn)。在實際運行中也不能總是使用gdb啟動,畢竟gdb綁定運行會消耗很大一部分性能。
不過linux系統(tǒng)在程序崩潰時會生成一個coredump文件,里面保存程序收到退出信號時的堆棧信息,對于調(diào)試c++程序是很有幫助的,ulimit命令就是設(shè)置程序崩潰時生成coredump文件的開關(guān)和大小。
使用命令在shell終端設(shè)置,只對當(dāng)前終端有效,修改當(dāng)前用戶下配置文件,只對當(dāng)前用戶有效,修改etc下系統(tǒng)配置,對所有用戶有效,這些都是通用的規(guī)律了。
使用命令gdb打開文件就可以調(diào)試查看崩潰的堆棧了,如果使用python調(diào)用的c++程序,打開core文件命令為gdb python core,使用c語言生產(chǎn)的coredump,打開命令為gdb c core,使用c++生成的coredump,打開命令為gdb cpp core。
查看當(dāng)前生成core文件設(shè)置
ulimit -c為0表示不生成core文件,非0的數(shù)字表示生成core文件大小,為unlimited表示不限制生成core文件大小。 ulimit -c 1024
表示設(shè)置core文件大小為1024kb,單位是kb。 ulimit -c unlimited
表示設(shè)置core文件大小不受限制。
順便看看其他選項有什么用:
使用man ulimit看看
ULIMIT(3) Linux Programmer's Manual ULIMIT(3)NAMEulimit - get and set user limitsSYNOPSIS#include <ulimit.h>long ulimit(int cmd, long newlimit);DESCRIPTIONWarning: 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_GETFSIZEReturn the limit on the size of a file, in units of 512 bytes.UL_SETFSIZESet 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 VALUEOn success, ulimit() returns a nonnegative value. On error, -1 is returned, and errno is set appropriately.ERRORSEPERM A unprivileged process tried to increase a limit.ATTRIBUTESFor an explanation of the terms used in this section, see attributes(7).┌──────────┬───────────────┬─────────┐│Interface │ Attribute │ Value │├──────────┼───────────────┼─────────┤│ulimit() │ Thread safety │ MT-Safe │└──────────┴───────────────┴─────────┘CONFORMING TOSVr4, POSIX.1-2001. POSIX.1-2008 marks ulimit() as obsolete.SEE ALSObash(1), getrlimit(2), setrlimit(2), sysconf(3)COLOPHONThis 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 athttp://www.kernel.org/doc/man-pages/.Linux 2015-03-29 ULIMIT(3)好像是一個系統(tǒng)接口函數(shù)說明,對我們沒什么用,不過里面提到了getrlimit與setrlimit兩個接口,這兩個接口是編寫c/c++程序可以用到的,他可以使用調(diào)用接口對運行程序修改值,不用修改配置也不影響其他的程序的限制。
不過可以通過ulimit -a這個命令來查看其他選項的用途,查看ulimit設(shè)置的所有系統(tǒng)限制:
$ ulimit -a core file size (blocks, -c) unlimited data seg size (kbytes, -d) unlimited scheduling priority (-e) 0 file size (blocks, -f) unlimited pending signals (-i) 127350 max locked memory (kbytes, -l) 64 max memory size (kbytes, -m) unlimited open files (-n) 1024 pipe size (512 bytes, -p) 8 POSIX message queues (bytes, -q) 819200 real-time priority (-r) 0 stack size (kbytes, -s) 8192 cpu time (seconds, -t) unlimited max user processes (-u) 127350 virtual memory (kbytes, -v) unlimited file locks (-x) unlimited上面列出了前面各個選項的意思,其中大寫的-H和-S分別是系統(tǒng)硬性限制和用戶軟件限制,-T和-b不知道什么意思。
一般在當(dāng)前目錄生成名稱為core的coredump文件,為了防止多次coredump生成時文件覆蓋,通過查閱其他的資料,生成coredump文件的目錄和命名格式都是可以修改的。
總結(jié)
以上是生活随笔為你收集整理的linux下开启程序崩溃生成core文件开关之ulimit详解的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: go语言实现第一个程序-hello,wo
- 下一篇: linux命令之远程文件或文件夹拷贝-s