Linux core 檔案介紹

來源:互聯網
上載者:User

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 "/corefile/core-%e-%p-%t" > /proc/sys/kernel/core_pattern
將會控制所產生的core檔案會存放到/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)

7. Core Dump的核心轉儲檔案目錄和命名規則:
/proc/sys/kernel/core_uses_pid可以控制產生的core檔案的檔案名稱中是否添加pid作為擴充,如果添加則檔案內容為1,否則為0

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.