用gdb来调试除数为0而产生的core

时间:2021-10-05 16:41:53

        前面讲了gdb调试core dump的入门, 下面来趁热打铁, 熟练一下。 某次, 在某代码中, 一个除数为零的概率事件, 弄得两个入职不久的同事耗费数天, 为什么搞这儿久呢? 因为不太熟悉gdb调试core.

        看程序和调试过程:

[taoge@localhost test]$ cat main.c -n
     1  #include <stdio.h>
     2
     3  int main()
     4  {
     5          int a = 1;
     6          int b = 0;
     7          int c = a / b;
     8
     9          printf("bad\n");
    10          return 0;
    11  }
    12
[taoge@localhost test]$ gcc -g main.c 
[taoge@localhost test]$ ls
a.out  main.c
[taoge@localhost test]$ ./a.out 
Floating point exception (core dumped)
[taoge@localhost test]$ ls
a.out  core.2441  main.c
[taoge@localhost test]$ 
[taoge@localhost test]$ 
[taoge@localhost test]$ 
[taoge@localhost test]$ gdb a.out core.2441 
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 2441]
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 8, Arithmetic exception.
#0  0x080483d6 in main () at main.c:7
7               int c = a / b;
Missing separate debuginfos, use: debuginfo-install glibc-2.12-1.7.el6.i686
(gdb) 
(gdb) 
(gdb) 
(gdb) bt
#0  0x080483d6 in main () at main.c:7
(gdb) 
       执行程序后, 程序core dump了, 产生了对应的core文件, 用gdb调试, 然后用bt命令查看堆栈情况, 定位到第7行, good.


       ok, 复习至此。