Core檔案簡單介紹及生成設定方法
Core檔案簡單介紹及生成設定方法
Core檔案其實就是記憶體的映像,當程式崩潰時,儲存記憶體的相應資訊,主用用於對程式進行除錯。當程式崩潰時便會產生core檔案,其實準確的應該說是core dump 檔案,預設生成位置與可執行程式位於同一目錄下,檔名為core.***,其中***是某一數字。
1、檔案大小限制(開關)
(1)臨時性設定
檢視core檔案是否生成:
$ulimit -c 可以檢視是否開啟此選項,若為0則為關閉;
ulimit -c 0可手動關閉
開啟core檔案生成:
$ulimit -c unlimited設定core檔案大小為不限制大小;
檢查core檔案的選項是否開啟:
$ulimit -a
ulimit引數含義如下:
-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
以上配置只對當前會話起作用,下次重新登陸後,還是得重新配置
ulimit -c 0 是禁止產生core檔案,而ulimit -c 1024則限制產生的core檔案的大小不能超過1024kb
(2)永久設定
要想配置永久生效,得在/etc/profile或者/etc/security/limits.conf檔案中進行配置。
首先以root許可權登陸,然後開啟/etc/security/limits.conf檔案,進行配置:
#vim /etc/security/limits.conf(在檔案結尾中新增)
<domain> <type> <item> <value>
* soft core unlimited
或者在/etc/profile中作如下配置(不推薦使用):
#vim /etc/profile
ulimit -S -c unlimited >/dev/null 2>&1
或者想配置只針對某一使用者有效,則修改此使用者的~/.bashrc或者~/.bash_profile檔案:
limit -c unlimited
2.Core Dump的檔案路徑和命名規則
在預設的情況下,很多系統的core檔案是生成在你執行程式的目錄下,或者你在程式中chdir後的那個目錄,(就說是在計算機目錄)然後在core檔案的後面加了一個 pid。在實際工作中,這樣可能會造成很多目錄下產生core檔案,並且可能導致大量的時候不便於管理的情況,實際上,core檔案的生成位置和檔名的命名都是可以配置的。對core檔案的名稱進行一些規定。這種設定是對/proc/sys/kernel/core_pattern和/proc/sys/kernel/core_uses_pid這兩個檔案進行修改。改動這兩個檔案的方法如下:
echo <pattern> > /proc/sys/kernel/core_pattern
echo <"0"/"1"> /proc/sys/kernel/core_uses_pid
並且注意,只有超級使用者才可以修改這兩個表。
core_pattern接受的是core檔名稱的pattern,它包含任何字串,並且用%作為轉移符號生成一些標示符,為core檔名稱加入特殊含義。已定義的標示符有如下這些:
%%:相當於%
%p:相當於<pid>
%u:相當於<uid>
%g:相當於<gid>
%s:相當於導致dump的訊號的數字
%t:相當於dump的時間
%e:相當於執行檔案的名稱
%h:相當於hostname
除以上這些標誌位外,還規定:
1、末尾的單個%可以直接去除;
2、%加上除上述以外的任何字元,%和該字元都會被去除;
3、所有其他字元都作為一般字元加入名稱中;
4、core檔案的名稱最大值為64個位元組(包括'\0');
5、core_pattern中預設的pattern為core;
6、為了保持相容性,通過設定core_uses_pid,可以在core檔案的末尾加上%p;
7、pattern中可以包含路徑資訊。
(1)臨時性設定
/proc/sys/kernel/core_uses_pid可以控制產生的core檔案的檔名中是否新增pid作為擴充套件,如果新增則檔案內容為1,否則為0
proc/sys/kernel/core_pattern可以設定格式化的core檔案儲存位置或檔名,比如原來檔案內容是core-%e
可以這樣修改:
echo "/corefiles/core-%e-%t" > core_pattern
將會控制所產生的core檔案會存放到/corefile目錄下,產生的檔名為core-命令名-時間戳
以下是引數列表:
%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 新增命令名
當然,你可以用下列方式來完成
sysctl -w kernel.core_pattern=/corefiles/core-%e-%p
這些操作一旦計算機重啟,則會丟失
(2)永久性設定:
如果你想持久化這些操作,可以在/etc/sysctl.conf檔案中增加: kernel.core_pattern=/corefiles/core-e%-%t
加好後,如果你想不重啟看看效果的話,則用下面的命令:sysctl -p /etc/sysctl.conf
3.測試是否生效
直接在終端中輸入:kill –s SIGSEGV $$
檢視設定core檔案存放的目錄是否有生成core檔案。