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文件。