Centos系統開啓生成core dump調試文件功能

需求背景

linux上運行的程序,有時候莫名其妙的崩潰了,查詢日誌,沒發現打印日誌;怎麼辦呢,可以藉助系統的dump功能生成進程的內存映象(包含調試信息,前提是運行的程序是debug版本),進一步還原跟蹤;

確認是否開啓

然而,默認情況下,系統並不開啓這個功能,查看有沒有開啓功能的命令

ulimit -c,如果返回0則說明沒有開啓;即使返回的值大於0,如果程序比較大,生成的core文件超過了這個限制值,則同樣不會生成 core.xxxx文件;這裏可以設置成unlimited,不進行限制。

開啓生成core dump調試文件功能

1、臨時開啓

執行命令ulimit -c unlimited

再執行ulimit -c查看返回值是不是unlimited,如果是,則說明開啓成功;

2、驗證是否正常生成core dump文件

編寫demo程序,創建demo.c文件

include <stdio.h>

int main()
{
char* bad_point=NULL;
*bad_point = 99;
}

編譯程序

gcc -g demo.c -o demo

執行./demo,查看是否生成core.xxxx文件

如果生成成功,則可以執行命令

gdb demo core.xxxx

GNU gdb (GDB) Red Hat Enterprise Linux (7.2-60.el6_4.1)
Copyright (C) 2010 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later http://gnu.org/licenses/gpl.html
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 “i686-redhat-linux-gnu”.
For bug reporting instructions, please see:
http://www.gnu.org/software/gdb/bugs/…
Reading symbols from /usr/local/src/lab/demo…done.
[New Thread 3503]
Missing separate debuginfo for 
Try: yum –disablerepo=’‘ –enablerepo=’-debug*’ install /usr/lib/debug/.build-id/5f/7d4ef6f6ba15505d3c42a7a09e2a7ca9ae5ba6
Reading symbols from /lib/libc-2.12.so…Reading symbols from /usr/lib/debug/lib/libc-2.12.so.debug…done.
done.
Loaded symbols for /lib/libc-2.12.so
Reading symbols from /lib/ld-2.12.so…Reading symbols from /usr/lib/debug/lib/ld-2.12.so.debug…done.
done.
Loaded symbols for /lib/ld-2.12.so
Core was generated by `./demo’.
Program terminated with signal 11, Segmentation fault.0 0x080483a4 in main () at demo.c:5

5 *bad_point = 99;

可以看到第5行,*bad_point=99出錯了,因爲沒有分配內存,直接賦值,定位到崩潰問題點;

不過ulimit -c unlimited這種方式在系統重啓後,就失效了,想要永久生效,則需要換一種修改方式;

3、永久生效

vi /etc/security/limits.conf編輯文件,修改

soft core 0

soft core unlimited

保存,重啓系統後,執行ulimit -c ,即可看到還是生效的。

查看原文

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