Linux下core文件的使用方法詳解
前言
有時候程序會異常退出而不帶任何日志,此時就可以使用 code 文件進行分析,它會記錄程序運行的內(nèi)存,寄存器,堆棧指針等信息
什么是core文件
通常在 Linux 下遇到程序異常退出或者中止,我們都會使用 core 文件進行分析,其中包含了程序運行時的內(nèi)存,寄存器,堆棧指針等信息,格式為 ELF ,可以理解是程序工作當(dāng)前狀態(tài)轉(zhuǎn)儲成一個文件,通過工具分析這個文件,我們可以定位到程序異常退出或者終止時相應(yīng)的堆棧調(diào)用等信息,為解決問題提供幫助。
使用core文件調(diào)試
生成方法
查看當(dāng)前 core 文件的狀態(tài)
$ ulimit -a ... -c: core file size (blocks) 0 # 關(guān)閉狀態(tài) ...
打開生成開關(guān)
ulimit -c unlimited ulimit -a ... -c: core file size (blocks) unlimited ...
對 core 文件的大小進行限制,單位為 blocks ,一般 1 block=512 bytes ,設(shè)置太小可能導(dǎo)致不會生成文件
$ ulimit -c 1024 $ ulimit -a ... -c: core file size (blocks) 1024 ...
關(guān)閉生成開關(guān)
ulimit -c 0 ulimit -a ... -c: core file size (blocks) 0 ...
上面對 core 文件的操作僅對當(dāng)前生效,若需要永久生效,則要將相應(yīng)操作寫入 /etc/profile
生成路徑
core 文件默認(rèn)生成在程序的工作目錄,可以對生成路徑進行設(shè)置,需要保證對對應(yīng)目錄有足夠空間并具有寫權(quán)限
echo /MyCoreDumpDir/core.%e.%p > /proc/sys/kernel/core_pattern
其中命名使用的參數(shù)列表
%p - insert pid into filename # 添加 pid %u - insert current uid into filename # 添加當(dāng)前 uid %g - insert current gid into filename # 添加當(dāng)前 gid %s - insert signal that caused the coredump into the filename # 添加導(dǎo)致產(chǎn)生 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 # 添加命令名
/proc/sys/kernel/core_uses_pid 這個文件的值若為1,則無論時候配置 %p ,最后生成的 core 文件都會添加 pid
調(diào)試方法
可以使用 gdb 對 core 文件進行調(diào)試,編譯是需要帶上 -g 選項
$ gdb a.out ... (gdb) core-file core ... (gdb) bt ...
如需要在 PC 上調(diào)試嵌入式設(shè)備產(chǎn)生的 core 文件,則需要選取相應(yīng)平臺的 gdb 工具,并在進入 gdb 后設(shè)置符號文件的位置
$ xxx-xxx-gdb a.out ... (gdb) solib-search-path xxx.so:xxx.so ... (gdb) core-file core ... (gdb) bt ...
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。
相關(guān)文章
CentOS 6.5中利用yum搭建LNMP環(huán)境的步驟詳解
這篇文章主要給大家介紹了關(guān)于在CentOS 6.5中利用yum搭建LNMP環(huán)境的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-12-12