1. 程式人生 > >Linux core 檔案介紹

Linux core 檔案介紹

1. core檔案的簡單介紹
在一個程式崩潰時,它一般會在指定目錄下生成一個core檔案。core檔案僅僅是一個記憶體映象(同時加上除錯資訊),主要是用來除錯的。 

2. 開啟或關閉core檔案的生成
用以下命令來阻止系統生成core檔案:
ulimit -c 0
下面的命令可以檢查生成core檔案的選項是否開啟:
ulimit -a
該命令將顯示所有的使用者定製,其中選項-a代表“all”。 

也可以修改系統檔案來調整core選項
在/etc/profile通常會有這樣一句話來禁止產生core檔案,通常這種設定是合理的:
# No core files by default
ulimit -S -c 0 > /dev/null 2>&1
但是在開發過程中有時為了除錯問題,還是需要在特定的使用者環境下開啟core檔案產生的設定
在使用者的~/.bash_profile里加上ulimit -c unlimited來讓特定的使用者可以產生core檔案
如果ulimit -c 0 則也是禁止產生core檔案,而ulimit -c 1024則限制產生的core檔案的大小不能超過1024kb 

3. 設定Core Dump的核心轉儲檔案目錄和命名規則
/proc/sys/kernel/core_uses_pid可以控制產生的core檔案的檔名中是否新增pid作為擴充套件,如果新增則檔案內容為1,否則為0
/proc/sys/kernel/core_pattern可以設定格式化的core檔案儲存位置或檔名,比如原來檔案內容是core-%e
可以這樣修改:
echo "/data/corefile/core-%e-%p-%t" > /proc/sys/kernel/core_pattern
將會控制所產生的core檔案會存放到/data/corefile目錄下,產生的檔名為core-命令名-pid-時間戳
以下是引數列表:
    %p - insert pid into filename 新增pid
    %u - insert current uid into filename 添加當前uid
    %g - insert current gid into filename 添加當前gid
    %s - insert signal that caused the coredump into the filename 新增導致產生core的訊號
    %t - insert UNIX time that the coredump occurred into filename 新增core檔案生成時的unix時間
    %h - insert hostname where the coredump happened into filename 新增主機名
    %e - insert coredumping executable name into filename 新增命令名 

 

4. 使用core檔案
在core檔案所在目錄下鍵入:
gdb -c core
它會啟動GNU的偵錯程式,來除錯core檔案,並且會顯示生成此core檔案的程式名,中止此程式的訊號等等
如果你已經知道是由什麼程式生成此core檔案的,比如MyServer崩潰了生成core.12345,那麼用此指令除錯:
gdb -c core MyServer
以下怎麼辦就該去學習gdb的使用了 

 

5. 一個小方法來測試產生core檔案
直接輸入指令:
kill -s SIGSEGV $$ 

 

6. 為何有時程式Down了,卻沒生成 Core

檔案。

Linux下,有一些設定,標明瞭resources available to the shell and to processes。 可以使用 

#ulimit -a 來看這些設定。 (ulimit是bash built-in Command) 

             -a     All current limits are reported
              -c     The maximum size of core files created
              -d     The maximum size of a process
鈥檚 data segment
              -e     The maximum scheduling priority ("nice")
              -f     The maximum size of files written by the shell and its children
              -i     The maximum number of pending signals
              -l     The maximum size that may be locked into memory
              -m     The maximum resident set size (has no effect on Linux)
              -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 real-time scheduling 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

從這裡可以看出,如果 -c是顯示:core file size          (blocks, -c)

如果這個值為0,則無法生成core檔案。所以可以使用: 

#ulimit -c 1024 或者 #ulimit -c unlimited 來使能 core檔案。 

如果程式出錯時生成Core 檔案,則會顯示Segmentation fault (core dumped)。 

 

我們線上一般是寫到/etc/sysctl.conf裡面追加一行

kernel.core_pattern=/data/crash/core.%e.%p.%h.%t

sysctl -p

但有時corefile卻沒生成到/data/crash目錄下面去,執行下面這條命令吧

sed -i '/|\/usr\/share\/apport\/apport/s/^/#/' /etc/init/apport.conf

再用 kill -s SIGSEGV $$ 測試一下吧