标签:
如果在 Linux下编写程序,有时运行程序的时候程序崩溃,比如说只有“Segmentation fault (core dumped) ”,程序比较小的话,还可以一行一行查看,但是如果程序很庞大,一行行查询,效率非常低下。Linux下可以程序可以生成core file文件,借助gdb很快能定位到崩溃的代码行。
测试程序,除零操作,程序会崩溃
/*
test.c
*/
#include <stdio.h>
#include <stdlib.h>
int func(int a, int b)
{
int c = a/b;
return c;
}
int main()
{
int a = 20;
int b = 0;
int ret = func(a,b);
return 0;
}
第一步:设置core file的大小,设置成无限大
ulimit -c unlimited
第二步:把生成的core file 重定位到 自定义的目录下,比如说系统目录下的“/tmp”下,%e是可执行文件名称,%p是pid
echo "/tmp/core-%e-%p" > /proc/sys/kernel/core_pattern
第三步:生成可执行文件
gcc -o test -g test.c
第四步:利用gdb和core file定位到程序崩溃的那一行
gdb test /tmp/core-test-2488
接下来生成下面的信息:
warning: Can‘t read pathname for load map: 输入/输出错误.
Reading symbols from /lib/tls/i686/cmov/libc.so.6...(no debugging symbols found)...done.
Loaded symbols for /lib/tls/i686/cmov/libc.so.6
Reading symbols from /lib/ld-linux.so.2...(no debugging symbols found)...done.
Loaded symbols for /lib/ld-linux.so.2
Core was generated by `./test‘.
Program terminated with signal 8, Arithmetic exception.
#0 0x080483c2 in func (a=20, b=0) at test.c:5
5 int c = a/b;
最后一行,出错的程序已经定位出来了。
参考资料
gdb结合coredump定位崩溃进程 - Ali’s Blog - 如你遇見這花 如我遇見你
http://lazycat.is-programmer.com/posts/31925.html
版权声明:本文为博主原创文章,未经博主允许不得转载。
Linux 使用core file文件快速定位程序崩溃代码行
标签:
原文地址:http://blog.csdn.net/zwhlxl/article/details/47072355