利用Linux coredump 进行debug

一, 设置coredump file size, 默认为0,此时系统并不产生coredump.

[ansen@localhost ~]$ ulimit -a

core file size          (blocks, -c) 0
data seg size           (kbytes, -d) unlimited
scheduling priority             (-e) 0
file size               (blocks, -f) unlimited
pending signals                 (-i) 16057
max locked memory       (kbytes, -l) 64
max memory size         (kbytes, -m) unlimited
open files                      (-n) 1024
pipe size            (512 bytes, -p) 8
POSIX message queues     (bytes, -q) 819200
real-time priority              (-r) 0
stack size              (kbytes, -s) 8192
cpu time               (seconds, -t) unlimited
max user processes              (-u) 1024
virtual memory          (kbytes, -v) unlimited
file locks                      (-x) unlimited
[ansen@localhost ~]$ ulimit -c unlimited

[ansen@localhost ~]$ 


设置 Core Dump 的核心转储文件目录和命名规则
/proc/sys/kernel/core_uses_pid 可以控制产生的 core 文件的文件名中是否添加 pid 作为扩展 ,如果添加则文件内容为 1 ,否则为 0
proc/sys/kernel/core_pattern 可以设置格式化的 core 文件保存位置或文件名 ,比如原来文件内容是 core-%e
可以这样修改 :
echo "/corefile/core-%e-%p-%t" > core_pattern
将会控制所产生的 core 文件会存放到 /corefile 目录下,产生的文件名为 core- 命令名 -pid- 时间戳
以下是参数列表 :
    %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 添加命令名

二 , 示例程序,产生coredump file, 写只读区域,导致segment fault.

[ansen@localhost ~]$ cat coregen.c 
#include <stdio.h>
#include <unistd.h>


int main(int argc, char**argv)
{
   char * ptr = "const string";
   *ptr = 0;


}
[ansen@localhost ~]$ gcc coregen.c 
[ansen@localhost ~]$ ./a.out

Segmentation fault (core dumped)


三,程序运行出错产生coredump file.

[ansen@localhost ~]$ ls -lrt
total 492
drwxr-xr-x.  2 ansen ansen   4096 Jun  8  2014 Templates
drwxr-xr-x.  2 ansen ansen   4096 Jun  8  2014 Public
drwxr-xr-x.  2 ansen ansen   4096 Jun  8  2014 Documents
drwxr-xr-x.  2 ansen ansen   4096 Jun  8  2014 Desktop
drwxr-xr-x.  2 ansen ansen   4096 Jun  8  2014 Videos
drwxr-xr-x.  2 ansen ansen   4096 Jun  8  2014 Pictures
drwxr-xr-x.  2 ansen ansen   4096 Jun  8  2014 Music
drwxrwxr-x.  3 ansen ansen   4096 Jun 10  2014 project
-rwxrwxrwx.  1 ansen ansen    220 Jun 10  2014 powerpcsetup.sh
-rw-r--r--.  1 root  root     246 Jun 11  2014 commd
-rw-rw-r--.  1 ansen ansen 201190 Jun 11  2014 glibc-ports-2.3.6.tar.gz
drwxrwxr-x.  4 ansen ansen   4096 Jun 16  2014 linux-ppc
drwxrwxr-x.  3 ansen ansen   4096 Oct 10  2014 build
-rw-rw-r--.  1 ansen ansen     39 Oct 16  2014 hellotest
-rwxrwxrwx.  1 root  root     100 Dec 22  2014 catsh.sh
-rw-rw-r--.  1 ansen ansen    251 Feb 16  2015 wraptest.c
-rwxrwxr-x.  1 ansen ansen   7371 Feb 16  2015 wraptest.o
-rwxrwxr-x.  1 ansen ansen  13345 Feb 16  2015 test.o
drwxrwxr-x.  5 ansen ansen   4096 Mar 30  2015 testgit
drwxrwxr-x.  3 ansen ansen   4096 May  7  2015 procps
drwxrwxr-x.  3 ansen ansen   4096 May 11  2015 gitsource
drwxrwxr-x. 30 ansen ansen   4096 May 26  2015 training
-rw-rw-r--.  1 ansen ansen   9478 May 29 04:36 test.cpp
drwxr-xr-x.  3 ansen ansen   4096 Jun  4 03:29 Downloads
drwxrwxr-x.  3 ansen ansen   4096 Aug  1 00:54 mypub
drwxrwxr-x.  2 ansen ansen   4096 Oct 28 03:50 algrithom
drwxrwxr-x.  2 ansen ansen   4096 Nov 23 21:27 PERL_v1
-rw-rw-r--.  1 ansen ansen    121 Nov 24 07:02 coregen.c
drwxr-xr-x.  2 ansen ansen   4096 Nov 25 00:21 Desktopbak
drwxrwxrwx.  8 root  root    4096 Nov 25 00:22 work
-rw-------.  1 ansen ansen 208896 Nov 26 04:02 core.3302
-
[ansen@localhost ~]$ 

[ansen@localhost ~]$ 


四,重新编译程序,生成带debug 信息的可执行程序。

[ansen@localhost ~]$ gcc -g coregen.c 

[ansen@localhost ~]$ 

五,利用GDB 调试coredump file,查找错误点和错误原因。 这里利用重新编译的带debug 信息的可执行程序的symbol来提供地址信息。

[ansen@localhost ~]$ gdb --core=core.3302 --se=./a.out
GNU gdb (GDB) Fedora 7.6.50.20130731-16.fc20
Copyright (C) 2013 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".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word".
..
Reading symbols from /home/ansen/a.out...done.


warning: exec file is newer than core file.
[New LWP 3302]
Core was generated by `./a.out'.
Program terminated with signal SIGSEGV, Segmentation fault.
#0  0x08048410 in main (argc=1, argv=0xbfeb2c74) at coregen.c:7     这里我们能够知道程序崩溃的地点,是在main 函数的第7行。哈哈。
7   *ptr = 0;

Missing separate debuginfos, use: debuginfo-install glibc-2.18-11.fc20.i686
(gdb) l
2 #include <unistd.h>

3

4 int main(int argc, char**argv)
5 {
6   char * ptr = "const string";
7   *ptr = 0;
8
9 }


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值