core文件生成及調試
1 代碼
#include<stdio.h>
int main()
{
int *p = NULL;
*p = 0;
return 0;
}shell
2 在當前shell執行ulimit -c unlimited
注意:該命令只在當前shell生效,其餘shell會失效ulimit -c是0,生成core文件失敗。
3 設置core文件格式和生成路徑,默認在執行程序當前目錄下,執行如下兩條命令:
echo "1" > /proc/sys/kernel/core_uses_pid
echo "/corefile/core-%e-%p-%t" > /proc/sys/kernel/core_patternapp
將core文件統一輩子成到/corefile目錄下,產生的文件名爲core-命令名-pid-時間戳
注意:/corefile目錄須要本身建立。
如下是參數列表:
%p - insert pid into filename 添加pid(進程id)
%u - insert current uid into filename 添加當前uid(用戶id)
%g - insert current gid into filename 添加當前gid(用戶組id)
%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 添加致使產生core的命令名
4 生成及調試core文件
gcc -o test test.c
./test
Segmentation fault (core dumped)
這裏出現段錯誤並生成core文件了ui
在/corefile目錄下發現core-test-31421-1476266571
開始調試
gdb ./test core-test-31421-1476266571unix
5 ulimit -c ulimited 設置永久生效調試
打開 /etc/security/limits.conf文件blog
添加紅框中兩行:進程
* soft core unlimitedit
* hard core unlimitedio