Linux生成core文件、core文件路徑設置

一、設置core文件大小


列出所有資源的限制: ulimit -a

ulimit -a

或者查看core file size: ulimit -c

core file size:
unlimited:core文件的大小不受限制
0:程序出錯時不會產生core文件
1024:代表1024k,core文件超出該大小就不能生成了

設置core文件大小: ulimit -c fileSize

注意:

儘量將這個文件大小設置得大一些,程序崩潰時生成Core文件大小即爲程序運行時佔用的內存大小。可能發生堆棧溢出的時候,佔用更大的內存

 

二、設置core文件的名稱和文件路徑


默認生成路徑:輸入可執行文件運行命令的同一路徑下
默認生成名字:默認命名爲core。新的core文件會覆蓋舊的core文件

a.設置pid作爲文件擴展名

1:添加pid作爲擴展名,生成的core文件名稱爲core.pid
0:不添加pid作爲擴展名,生成的core文件名稱爲core
修改 /proc/sys/kernel/core_uses_pid 文件內容爲: 1
修改文件命令: echo “1” > /proc/sys/kernel/core_uses_pid
或者
sysctl -w kernel.core_uses_pid=1 kernel.core_uses_pid = 1

b. 控制core文件保存位置和文件名格式

修改文件: echo “/corefile/core-%e-%p-%t” > /proc/sys/kernel/core_pattern
或者:
sysctl -w kernel.core_pattern=/corefile/core-%e-%p-%t kernel.core_pattern = /corefile/core-%e-%p-%t
可以將core文件統一生成到/corefile目錄下,產生的文件名爲core-命令名-pid-時間戳
以下是參數列表:

%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的命令

 

三、測試是否能生成core文件


kill -s SIGSEGV $$
查看/corefile目錄下是否生成了core文件

 

四、調試core文件


test.c

#include<stdio.h>
int main()
{
      int *p = NULL;
      *p = 0;
      return 0;
}
root@ubuntu:~# gcc -o test test.c
root@ubuntu:~# ./test
Segmentation fault (core dumped)

bingo:這裏出現段錯誤並生成core文件了
在/corefile目錄下發現core-test-31421-1476266571
開始調試
gdb ./test core-test-31421-1476266571

gdb

根據堆棧信息查看bug

 

 

 

 

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