天天看點

Core檔案介紹、作用、用法一、Core 檔案介紹:二、Core檔案作用和用法:

一、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 "/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

二、Core檔案作用和用法:

 1、

轉載于:https://blog.51cto.com/yehubilee/1078777

繼續閱讀