gdb调试core dump入门实践(顺便复习一下之前介绍过的addr2line命令调试)

        调试技能是软件开发的必备技能, 不会调试, 就抓不到bug, 就很痛苦。 本文我们来一起聊聊gdb调试core

 

         Part 1:

        在前面的博文中, 我们聊过重要的addr2line调试, 现在再来一起看看, 就当是复习吧。

        程序如下:

 

#include <stdio.h>

int main()
{
        int *p = NULL;
        *p = 0;

        printf("bad\n");
        return 0;
}

       几乎所有的码农都能一眼看出错在哪里, 但在大型项目中, 光靠肉眼怎行? 必须借助工具, 我们用addr2line来搞起,编译并运行:

 

 

[taoge@localhost test]$ cat main.c -n
     1  #include <stdio.h>
     2
     3  int main()
     4  {
     5          int *p = NULL;
     6          *p = 0;
     7
     8          printf("bad\n");
     9          return 0;
    10  }
    11
[taoge@localhost test]$ gcc -g main.c 
[taoge@localhost test]$ ./a.out 
Segmentation fault (core dumped)
[taoge@localhost test]$ 
[taoge@localhost test]$ 
[taoge@localhost test]$ 
[taoge@localhost test]$ dmesg | grep a.out 
virtual kernel memory layout:
a.out[2282]: segfault at 0 ip 080483c9 sp bfacd460 error 6 in a.out[8048000+1000]
a.out[2303]: segfault at 0 ip 080483c9 sp bfadc1a0 error 6 in a.out[8048000+1000]
a.out[2307]: segfault at 0 ip 080483c9 sp bfef76e0 error 6 in a.out[8048000+1000]
a.out[4153]: segfault at 0 ip 080483c9 sp bf9f2490 error 6 in a.out[8048000+1000]
a.out[4932]: segfault at 0 ip 080483c9 sp bfcfd2a0 error 6 in a.out[8048000+1000]
a.out[5408]: segfault at 0 ip 080483c9 sp bf894b40 error 6 in a.out[8048000+1000]
[taoge@localhost test]$ addr2line -e a.out 080483c9
/home/taoge/test/main.c:6
[taoge@localhost test]$ 

        可以看到, 程序core dump了, 用dmesg命令查出程序core对应的地址为:080483c9, 然后呢, 利用addr2line命令转换为对应的代码行, 可以看到问题出在第6行, 回头看代码, 果真如此。

 

        在实际开发中, dmesg命令用的不多, 那怎么知道像080483c9这样的地址呢? 通常来讲, 程序core dump之后, 在日志中会记录堆栈信息的, 可以在日志文件中搜索backtrace这个字段, 然后查找地址。

 

 

       Part 2:

       接下来先预热一下, 搞点前戏, 我们来用gdb的r命令来玩玩, 如下:

 

[taoge@localhost test]$ rm a.out
[taoge@localhost test]$ gcc -g main.c 
[taoge@localhost test]$ ./a.out 
Segmentation fault (core dumped)
[taoge@localhost test]$ gdb a.out 
GNU gdb (GDB) Red Hat Enterprise Linux (7.1-29.el6)
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 /home/taoge/test/a.out...done.
(gdb) r
Starting program: /home/taoge/test/a.out 

Program received signal SIGSEGV, Segmentation fault.
0x080483c9 in main () at main.c:6
6               *p = 0;
Missing separate debuginfos, use: debuginfo-install glibc-2.12-1.7.el6.i686
(gdb) quit
A debugging session is active.

        Inferior 1 [process 5489] will be killed.

Quit anyway? (y or n) y
[taoge@localhost test]$ 

        可以看到, 在gdb调试的时候, 用r命令让程序再跑起来, 同样可以定位到问题出在第6行。还没完, 我们来看bt命令:

 

 

[taoge@localhost test]$ rm a.out
[taoge@localhost test]$ gcc -g main.c 
[taoge@localhost test]$ ./a.out 
Segmentation fault (core dumped)
[taoge@localhost test]$ gdb a.out 
GNU gdb (GDB) Red Hat Enterprise Linux (7.1-29.el6)
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 /home/taoge/test/a.out...done.
(gdb) bt
No stack.
(gdb) r
Starting program: /home/taoge/test/a.out 

Program received signal SIGSEGV, Segmentation fault.
0x080483c9 in main () at main.c:6
6               *p = 0;
Missing separate debuginfos, use: debuginfo-install glibc-2.12-1.7.el6.i686
(gdb) bt
#0  0x080483c9 in main () at main.c:6
(gdb) 

        可以看到, 第一个bt显示no stack, 因为程序a.out还没有跑起来。 好, 用r让程序run起来,继续bt,  就可以看到函数堆栈, 也能定位到第6行。 我们仔细看一下, gdb也找到了080483c9这个地址,进而找到了第6行,  所以完全可以认为, gdb里面内置了addr2line命令。


 

Part 3:

        好的, 还是来说我们的重头戏------gdb调试core

 

        我们先来看如下操作:

 

[taoge@localhost test]$ rm a.out 
[taoge@localhost test]$ ls
main.c
[taoge@localhost test]$ gcc -g main.c 
[taoge@localhost test]$ ls
a.out  main.c
[taoge@localhost test]$ ./a.out 
Segmentation fault (core dumped)
[taoge@localhost test]$ ls
a.out  main.c
[taoge@localhost test]$ 

        可以看到, 程序core dump后, 并没有生成什么core文件, 原因是ulimit对应的开关没有打开, 我们来看看:

 

 

[taoge@localhost test]$ ulimit -c
0
[taoge@localhost test]$ ulimit -c 999
[taoge@localhost test]$ ulimit -c
999
[taoge@localhost test]$ rm a.out 
[taoge@localhost test]$ gcc -g main.c 
[taoge@localhost test]$ ls
a.out  main.c
[taoge@localhost test]$ ./a.out 
Segmentation fault (core dumped)
[taoge@localhost test]$ ls
a.out  core.5911  main.c
[taoge@localhost test]$ gdb a.out core.5911 
GNU gdb (GDB) Red Hat Enterprise Linux (7.1-29.el6)
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 /home/taoge/test/a.out...done.
[New Thread 5911]
Missing separate debuginfo for 
Try: yum --disablerepo='*' --enablerepo='*-debuginfo' install /usr/lib/debug/.build-id/74/d23352fd770753e375bd0caecf375bd77bded5
Reading symbols from /lib/libc.so.6...(no debugging symbols found)...done.
Loaded symbols for /lib/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 `./a.out'.
Program terminated with signal 11, Segmentation fault.
#0  0x080483c9 in main () at main.c:6
6               *p = 0;
Missing separate debuginfos, use: debuginfo-install glibc-2.12-1.7.el6.i686
(gdb) bt
#0  0x080483c9 in main () at main.c:6
(gdb) quit
[taoge@localhost test]$

        看上面的过程, ulimit -c是用来查询所设定的core文件大小的, 可以看到, 在默认情况下为0, 所以执行./a.out的时候, 并没有core文件生成。 我们把它设置为999, 然后在执行./a.out, 可以看到, 有core文件(core.5911)生成。 然后利用gdb来调试core, 刚执行gdb a.out core.5911的之后, 就看出了问题出在第6行, 此时如果用bt命令来看堆栈, 也可以看出问题出在第6行。
 

 


        那上述调试各有什么特点呢? 

        我们知道,在实际中, 有很多问题是概率发生的, 很难重现。 此时, 如果用gdb的r命令(实际相当于重新运行程序)则是不可能的, 所以,本文part 2中的方法不太实用。

 

        对于概率性问题, 我们通常采用的是part 1和 part 3的方法。

        先说part 1方法:当程序出现堆栈错误时, 我们可以从日志中看到出错的地址, 然后用part 1中介绍的方法来查询, 以前经常这么玩。

        再说part 3方法:当程序出现堆栈错误时, 如果产生了core文件, 我们一定要视之为宝贝, 记得保存, 否则很可能被冲掉。 拿到core文件后, 我们可以用part 3中介绍的方法来调试core, 以前也这么玩过。

 

        有一个重要的问题必须指出, 在本文中, 所有的编译都加了-g这个选项, 主要是为了调试(保存了调试所需的调试信息), 如果没有-g, 那么只能知道程序出错的堆栈地址, 却无法知道对应的代码行, 前功尽弃, 抓狂不已抓狂

        在实际项目开发中(比如嵌入式开发), 真正的可执行文件/动态库等都都不能太大, 比如说,生成的xxx.so库有10M,  嵌入式那点小设备哪能奢侈地装这么大的东西啊?

所以要对xxx.so文件进行脱衣服操作(strip xxx.so), 去掉一些调试信息, 形成对应的yyy.so,  真正发布的时候, 只发布yyy.so就行了, 如果出了问题, 需要定位的时候, 再用对应的xxx.so来定位, 因为xxx.so中有调试信息, 而yyy.so的衣服被脱了, 没有对应的调试信息了。   而且, 嵌入式设备生成了core文件, 但设备一般不支持gbd调试, 所以要把设备中的core文件拷贝到linux机器上进行调试, 千万要记得保存编译的环境哦。

 

         OK, gdb调试core dump的入门介绍就到此为止, 在后续的博文中, 我会继续介绍更多有关gdb调试的内容, 到时再一起嗨。

 

 

 

 

 

 

  • 5
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 7
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值