Linux下core檔案的生成和使用

2021-07-05 11:04:20 字數 3872 閱讀 8446

當程式異常終止時,核心有可能把該程式當前的記憶體資訊對映到core檔案裡,以方便程式設計師定位問題。也就是說core檔案是程序異常終止時的乙個記憶體映象,可以用gdb來開啟core檔案,協助分析程式問題。

《unix環境高階程式設計》第10章種講到:當程式接收到以下訊號時會產生core檔案。

名字說明

freebsd

linux

mac os x

預設動作

sigabrt

異常終止(abort)..

.終止+core

sigbus

硬體故障..

.終止+core

sigemt

硬體故障..

.終止+core

sigfpe

算術異常..

.終止+core

sigill

非法硬體指令..

.終止+core

sigiot

硬體故障..

.終止+core

sigquit

終端退出符..

.終止+core

sigsegv

無效儲存訪問..

.終止+core

sigsys

無效系統呼叫..

.終止+core

sigtrap

硬體故障..

.終止+core

sigxcpu

超過cpu限制(setrlimit)..

.終止+core

sigxfsz

超過檔案長度限制(setrlimit)..

.終止+core

查閱core 的man page可以知道以下情況不產生core檔案

使用者沒有寫當前工作目錄的許可權;

系統磁碟空間不足

生成core檔案的目錄不存在

core file size的大小設定為0

程序是設定-使用者-id,而且當前使用者並非程式檔案的所有者

程序是設定-組-id,而且當前使用者並非該程式檔案的組所有者

該檔案已經存在但使用者對該檔案沒有寫許可權

用ulimit -a 可以檢視當前的core檔案大小限制

用ulimit -c unlimited 設定不限制core檔案大小

使用c/c++設定core檔案大小限制

int set_core_file_size()

return 0;

}return -1;

}

在core檔案後面加上程序id

echo "1" > /proc/sys

/kernel/core_uses_pid

格式化core檔名和儲存位置linux2.6之後可以通過修改/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時間

%e - insert coredumping executable name into filename 新增命令名

#include#includeint set_core_file_size()

return 0;

}return -1;

}

int main()

編譯並執行程式會出現段錯誤,此時在程式執行目錄下生產了core.pid的檔案。我們可以利用該檔案來輔助定位程式的問題。

編譯

g++

-gcore_test

.cpp

執行

zhanyi@zhanyi-pc

:~/work/config_file

$ ./a.out

段錯誤 (核心已轉儲)

除錯core檔案

zhanyi@zhanyi-pc:~/work/config_file$ gdb ./a.out core.23976 

gnu gdb (ubuntu 7.7.1-0ubuntu5~14.04.2) 7.7.1

license gplv3+: gnu gpl version 3 or later this is free software: you are free to change and redistribute it.

there is no warranty, to the extent permitted by law. type "show copying"

and "show warranty" for details.

this gdb was configured as "x86_64-linux-gnu".

type "show configuration" for configuration details.

for bug reporting instructions, please see:

.find the gdb manual and other documentation resources online at:

.for help, type "help".

type "apropos word" to search for commands related to "word"...

reading symbols from ./a.out...done.

[new lwp 23976]

core was generated by `./a.out'.

program terminated with signal sigsegv, segmentation fault.

#0 0x000000000040067d in main () at core_test.cpp:43

43 sztest[2] = '1';

(gdb) where

#0 0x000000000040067d in main () at core_test.cpp:43

(gdb) bt

#0 0x000000000040067d in main () at core_test.cpp:43

(gdb)

通過bt 或者where命令都能看到程式在第43行產生問題。原因是由於沒有對指標分配空間就直接使用。

Linux生成core檔案 core檔案路徑設定

在linux下產生並除錯core檔案 先看看我用的是個什麼機器 uname a linux dev 2.4.21 9.30axsmp 1 smp wed may 26 23 37 09 edt 2004 i686 i686 i386 gnu linux 再看看預設的一些引數,注意core file ...

Linux生成core檔案 core檔案路徑設定

在linux下產生並除錯core檔案 先看看我用的是個什麼機器 uname a linux dev 2.4.21 9.30axsmp 1 smp wed may 26 23 37 09 edt 2004 i686 i686 i386 gnu linux 再看看預設的一些引數,注意core file ...

Linux生成core檔案 core檔案路徑設定

在linux下產生並除錯core檔案 先看看我用的是個什麼機器 uname a linux dev 2.4.21 9.30axsmp 1 smp wed may 26 23 37 09 edt 2004 i686 i686 i386 gnu linux 再看看預設的一些引數,注意core file ...