Core文件作用、設置及用法

1.Core文件簡介

Core文件其實就是內存的映像,當程序崩潰時,存儲內存的相應信息,主用用於對程序進行調試。當程序崩潰時便會產生core文件,其實準確的應該說是core dump 文件,默認生成位置與可執行程序位於同一目錄下,文件名爲core.***,其中***是某一數字。

2.開啓或關閉Core文件的生成

關閉或阻止core文件生成:

$ulimit -c 0

打開core文件生成:

$ulimit -c unlimited

檢查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

以上配置只對當前會話起作用,下次重新登陸後,還是得重新配置。要想配置永久生效,得在/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

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   (-c指定core文件)
它會啓動GNU的調試器,來調試core文件,並且會顯示生成此core文件的程序名,中止此程序的信號等等
如果你已經知道是由什麼程序生成此core文件的,比如MyServer崩潰了生成core.12345,那麼用此指令調試:
gdb -c core MyServer


5. 一個小方法來測試產生core文件

直接輸入指令:
kill -s SIGSEGV $$

                  http://en.wikipedia.org/wiki/Core_dump
                           http://www.ibm.com/developerworks/cn/linux/l-cn-ulimit/

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章