需求背景
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 ,即可看到还是生效的。