在Linux下程序不尋常退出時,內核會在當前工作目錄下生成一個core文件(是一個內存映像,同時加上調試信息)。使用gdb來查看core文件,可以指示出導致程序出錯的代碼所在文件和行數。
注: 1. 當然首先編譯時要帶上gdb信息
下面說一下永久打開生成Core文件的步驟:
1、設置core文件的大小不受限制。
ulimit -c unlimited
2、ulimit -c校驗是否設置成功(如果是0,說明生成core文件開關為關閉)
[root@A03-R05-I115-53-5254972 jimdb]# ulimit -c
Unlimited
3、【重要!!】打開 /etc/security/limits.conf文件(使ulimit -c ulimited 設置永久生效)
vi /etc/security/limits.conf
添加紅框中兩行:
* soft core unlimited
* hard core unlimited

用命令修改的方式:
echo "* soft core unlimited" >> /etc/security/limits.conf
echo "* hard core unlimited" >> /etc/security/limits.conf
4、【重要!!】創建存放 core文件的文件夾 (確認應用有寫的權限)
mkdir /export/Logs/jimdb
5、設置生成 core文件的名稱和生成路徑(以下兩種方式任選一種,哪種生效與系統有關)
方式一: 修改/proc/sys/kernel/core_pattern和/proc/sys/kernel/core_uses_pid
echo "/export/Logs/jimdb/core-%e-%p-%t" > /proc/sys/kernel/core_pattern
echo "1" > /proc/sys/kernel/core_uses_pid
(core_pattern文件名為core-命令名-pid-時間戳)
(kernel.core_uses_pid控制core文件的文件名中是否添加pid作為擴展,文件內容為1,表示添加pid作為擴展名,生成的core文件格式為core.xxxx;為0則表示生成的core文件同一命名為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 添加命令名
方式二:修改/etc/sysctl.conf
sysctl -w "kernel.core_pattern=/export/Logs/jimdb/core-%e-%p-%t" >>/etc/sysctl.conf
sysctl -w "kernel.core_uses_pid=1" >>/etc/sysctl.conf
sysctl -p (查看生效參數,驗證設置是否生效)
5、快速驗證是否能生成core文件
kill -s SIGSEGV $$
6、驗證是否生效
1)再次登錄機器,查看/export/Logs/jimdb目錄下有core-bash的文件

2)ulimit -c校驗設置是否依然是Unlimited(如果是0,說明生成core文件開關為關閉)
[root@A03-R05-I115-53-5254972 jimdb]# ulimit -c
Unlimited
