Segment fault时使用gdb跟踪core文件

1.    Segment faultcore文件生成设置

1.1. 修改/etc/profile

/etc/profile ulimit那一行修改成:

ulimit -S -c unlimited

 

然后sh /etc/profile , 就可以生效,如有进程segment fault ,可以用gdb core 文件看死在哪里了。

 

1.2 确保后台进程也产生coredump的方法:

1).创建一个存放core文件的目录: mkdir /core_dir

 

2).编辑/etc/sysctl.conf,添加下面内容:

 

      fs.suid_dumpable = 1

      kernel.core_pattern=/core_dir/core-%s-%t

 

3).重新加载/etc/sysctl.conf的配置:

 

      sysctl -p

 

当后台进程不正常终止,在指定的/core_dir/目录下将会保留一个core文件。

 

2. 使用gdb跟踪core文件

2.1. 产生segment fault的源文件seg_fault.c

 

编译,执行,产生core文件

$ gcc -Wall -g foo.c

$ ./a.out
Segmentation fault

$ ls /core_dir/

core.1546

 

2.2. 使用gdb

$ gdb --core=/core_dir/core.1546

GNU gdb Asianux (6.0post-0.20040223.17.1AX)

Copyright 2004 Free Software Foundation, Inc.

GDB is free software, covered by the GNU General Public License, and you are

welcome to change it and/or distribute copies of it under certain conditions.

Type "show copying" to see the conditions.

There is absolutely no warranty for GDB. Type "show warranty" for details.

This GDB was configured as "i386-asianux-linux-gnu".

Core was generated by `./a.out'.

Program terminated with signal 11, Segmentation fault.

#0 0x08048373 in ?? ()

(gdb) bt

#0 0x08048373 in ?? ()

#1 0xbfffd8f8 in ?? ()

#2 0x0804839e in ?? ()

#3 0xb74cc6b3 in ?? ()

#4 0x00000000 in ?? ()

 

此时用bt看不到backtrace,也就是调用堆栈,原来GDB还不知道符号信息在哪里。我们告诉它一下:

(gdb) file ./a.out

Reading symbols from ./a.out...done.

Using host libthread_db library "/lib/tls/libthread_db.so.1".

(gdb) bt

#0 0x08048373 in sub () at foo.c:14

#1 0x08048359 in main () at foo.c:8

 

此时backtrace出来了。

(gdb) l

8         sub();

9         return 0;

10     }

11     static void sub(void)

12     {

13        int *p = NULL;

14        printf("%d", *p);

 (gdb)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值