1.什麼是core文件?
有問題的程序運行後,產生「段錯誤 (核心已轉儲)」時生成的具備堆棧信息和調試信息的文件。app
編譯時須要加 -g 選項使程序生成調試信息: gcc -g core_test.c -o core_testui
2.怎樣配置生成 core 文件
(1)core文件開關
①使用 ulimit -c 查看core開關,若是爲0表示關閉,不會生成core文件;.net
②使用 ulimit -c [filesize] 設置core文件大小,當最小設置爲4以後纔會生成core文件;unix
③使用 ulimit -c unlimited 設置core文件大小爲不限制,這是經常使用的作法;調試
④若是須要開機就執行,則須要將這句命令寫到 /etc/profile 等文件。blog
(2)core文件命名和保存路徑it
1)/proc/sys/kernel/core_uses_pid能夠控制core文件的文件名中是否添加pid做爲擴展。文件內容爲1,表示添加pid做爲擴展名,生成的core文件格式爲core.xxxx;爲0則表示生成的core文件同一命名爲core。
可經過如下命令修改此文件:
echo 「1″ > /proc/sys/kernel/core_uses_pid編譯
2)proc/sys/kernel/core_pattern能夠控制core文件保存位置和文件名格式。
可經過如下命令修改此文件:
echo 「/corefile/core-%e-%p-%t」 > core_pattern,能夠將core文件統一輩子成到/corefile目錄下,產生的文件名爲core-命令名-pid-時間戳table
①core文件有默認的名稱和路徑,但爲了方便,咱們一般會本身命名和指定保存路徑;test
②能夠經過 /proc/sys/kernel/core_pattern 設置 core 文件名和保存路徑,方法以下:
echo "/corefile/core-%e-%p-%t" > /proc/sys/kernel/core_pattern
命名的參數列表:
%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 添加命令名。
1)gdb -core=core.xxxx
file ./a.out
bt
2)gdb -c core.xxxx
file ./a.out
bt
(1)方法1: gdb [exec file] [core file] 而後執行bt看堆棧信息:
(2)方法②:gdb -c [core file],而後 file [exec file],最後再使用 bt 查看錯誤位置:
————————————————版權聲明:本文爲CSDN博主「wkd_007」的原創文章,遵循 CC 4.0 BY-SA 版權協議,轉載請附上原文出處連接及本聲明。原文連接:https://blog.csdn.net/wkd_007/article/details/79757289