Windows XP Windows 7 Windows 2003 Windows Vista Windows教程綜合 Linux 系統教程
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟件教程
 Windows教程網 >> Linux系統教程 >> Linux教程 >> Linux core 文件介紹

Linux core 文件介紹

日期:2017/2/7 14:32:41      編輯:Linux教程
 

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

Copyright © Windows教程網 All Rights Reserved