http://fcamel-life.blogspot.hk/2012/02/core-dump.html
http://blog.chinaunix.net/uid-23145525-id-4026304.html
這篇是 "Learning core dump from the hard way", 寫下幾個注意事項, 後來才發現 man core 就可以看到全部東西:
測試
若執行的程式有讀 terminal input, 可直接輸入 ctrl + \ 送出 SIGQUIT。或用指令 kill -QUIT PID 或 kill -ABRT PID, 要求程式產生 core dump, 藉此測試目前的設定是否 ok。
參考《從 /proc/PID/status 了解執行中程式處理 signal 的方式》, 先確認 signal handler 沒有被覆蓋掉, 才可放心測試。
ulimit / setrlimit(RLIMIT_CORE, ...)
Ubuntu 預設為 ulimit -c 0, 表示不產生 core dump, 所以要先執行 ulimit -c unlimited 允許產生 core dump。可以將這行寫到 ~/.bashrc 裡, 以後就預設會產生 core dump。或在程式裡呼叫 setrlimit(RLIMIT_CORE, &limit) 也可以。
/proc/sys/kernel/core_pattern
預設 core dump 的檔名可能不合使用, 參考《setting the core dump name schema》, 可用
$ echo "core.%e.%p.%t" | sudo tee /proc/sys/kernel/core_pattern
改變 core dump 的檔名, 這樣檔名會記錄是程式名稱、PID、發生的時間。在 multi-process 或 multi-thread 時特別有用。若希望每次開機都會生效, 則要在 /etc/sysctl.conf 加入 kernel.core_pattern = core.%e.%p.%t。
core pattern 可以是 "|PROGRAM", 這樣會將 core dump 導到 PROGRAM 的標準輸入, 可以自己寫 PROGRAM 做控制。像是 core dump 太頻繁時, 取樣留下幾個就好, 以免一下就塞爆硬碟。同樣的, %e 那些參數也可以接在 PROGRAM 後當參數用, 像是 "|PROGRAM core.%e.%p.%t", 自己的 PROGRAM 就能從 argv[1] 裡取得適合的檔名。需要特別注意的是, | 和 PROGRAM 之間不可以有空白。
setuid / setgid
若確認 ulimit -c 有設好, core_pattern 沒寫到奇怪的地方 (像是 /dev/null), 硬碟也仍有空間, 卻仍無法產生 core dump, 可能是用到 setuid。用 ls -l 檢查一下, 或用 strace -esetuid 檢查。Linux 為了安全考量, 在使用 setuid/setgid 後, 會自動禁用 core dump。
若確實有觸發 setuid 的話, 在 setuid 後執行:
prctl(PR_SET_DUMPABLE, 1); 经测试发现,进行过setuid(), setgid()的进程,即使设置了prctl(PR_SET_DUMPABLE, 1),也是无法产生core文件的
重新允許產生 core dump, 應該就 ok 了。
2013-03-26 更新
裝 Ubuntu 12.04 後, 發覺 /etc/sysctl.conf 的設定沒有生效, 查了一下才發覺 apport 覆寫了 /proc/sys/kernel/core_pattern, 參考這裡關掉它, /etc/sysctl.conf 的值應該可如預期作用:
$ vi /etc/default/apport # 更新成 "enabled=0"
=======================
设置coredump标记。
- #if HAVE_PRCTL && defined(PR_SET_DUMPABLE)
- /* Set Linux DUMPABLE flag */
- if (prctl(PR_SET_DUMPABLE, 1, 0, 0, 0) != 0)
- debug(50, 2) ("prctl: %s\n", xstrerror());
- #endif
man中对PR_SET_DUMPABLE的解释,注意橙色部分,会导致core出不来,所以要显示的设置下。
- PR_SET_DUMPABLE (since Linux 2.3.20)
- Set the state of the flag determining whether core dumps are produced for this process upon delivery of a signal whose default behavior is to produce a core
- dump. (Normally this flag is set for a process by default, but it is cleared when a set-user-ID or set-group-ID program is executed and also by various sys-
- tem calls that manipulate process UIDs and GIDs). In kernels up to and including 2.6.12, arg2 must be either 0 (process is not dumpable) or 1 (process is
- dumpable). Between kernels 2.6.13 and 2.6.17, the value 2 was also permitted, which caused any binary which normally would not be dumped to be dumped read-
- able by root only; for security reasons, this feature has been removed. (See also the description of /proc/sys/fs/suid_dumpable in proc(5).)