执行gdb a.out core的时候运行a.out了吗? a.out和core要对应吗?

        前面我们讲了不少gdb来调试core的方法, 现在我们来看这样两个问题:执行gdb a.out core的时候, 运行a.out了吗?a.out和core要对应吗?

 

        看操作:

 

[taoge@localhost test]$ cat test.c -n
     1  #include <stdio.h>  
     2
     3  int main()  
     4  {
     5          printf("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n");
     6    
     7          int *p = NULL;  
     8          *p = 0;  
     9
    10          printf("bad\n");  
    11          return 0;  
    12  }  
[taoge@localhost test]$ gcc -g test.c 
[taoge@localhost test]$ ls
a.out  test.c
[taoge@localhost test]$ ./a.out 
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Segmentation fault (core dumped)
[taoge@localhost test]$ ls
a.out  core.2407  test.c
[taoge@localhost test]$ 
[taoge@localhost test]$ 
[taoge@localhost test]$ 
[taoge@localhost test]$ gdb a.out core.2407 
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 2407]
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  0x080483d5 in main () at test.c:8
8               *p = 0;  
Missing separate debuginfos, use: debuginfo-install glibc-2.12-1.7.el6.i686
(gdb) 
(gdb) 
(gdb) 
(gdb) bt
#0  0x080483d5 in main () at test.c:8
(gdb) 
#0  0x080483d5 in main () at test.c:8
(gdb) 
#0  0x080483d5 in main () at test.c:8
(gdb) 
#0  0x080483d5 in main () at test.c:8
(gdb) 
#0  0x080483d5 in main () at test.c:8
(gdb) 
#0  0x080483d5 in main () at test.c:8
(gdb) 
#0  0x080483d5 in main () at test.c:8
(gdb) r
No core file now.
Starting program: /home/taoge/test/a.out 
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

Program received signal SIGSEGV, Segmentation fault.
0x080483d5 in main () at test.c:8
8               *p = 0;  
(gdb) q
A debugging session is active.

        Inferior 1 [process 2414] will be killed.

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

        可见, 执行gdb a.out core, 并执行bt的时候, 并没有执行a.out.   为了进一步验证, 我写了如下程序:

 

#include <stdio.h>

void swap(int *px, int *py)
{
        int tmp = *px;
        *px = *py;
        *py = tmp;
}

int main()
{
        int a = 1;
        int b = 2;
        int c = a + b;
        printf("%d, %d, %d\n", a, b, c);

        FILE *fp = fopen("data.txt", "a");
        fprintf(fp, "hello\n");
        fclose(fp);

        swap(&a,& b);
        printf("%d, %d, %d\n", a, b, c);

        int *p = NULL;
        *p = 0;

        return 0;
}

       然后操作如下:

[taoge@localhost test]$ gcc -g test.c 
[taoge@localhost test]$ ls
a.out  test.c
[taoge@localhost test]$ ./a.out 
1, 2, 3
2, 1, 3
Segmentation fault (core dumped)
[taoge@localhost test]$ ls
a.out  core.2486  data.txt  test.c
[taoge@localhost test]$ cat data.txt 
hello
[taoge@localhost test]$ gdb a.out core.2486 
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 2486]
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  0x08048564 in main () at test.c:25
25              *p = 0;
Missing separate debuginfos, use: debuginfo-install glibc-2.12-1.7.el6.i686
(gdb) bt
#0  0x08048564 in main () at test.c:25
(gdb) q
[taoge@localhost test]$ cat data.txt 
hello
[taoge@localhost test]$ 

       我们看到, 执行gdb a.out core.2486和bt后, 确实没有新的hello生成, 可见呢, gdb a.out core.2486确实没有运行a.out.
 

 

 

       另外, 我要强调一点, 当a.out与core文件不对应, 会有对应提示, 也就自然无法准确分析错误所在了, 如下, 我们只更新a.out, 但保留旧core文件:

 

[taoge@localhost test]$ gcc -g test.c 
[taoge@localhost test]$ gdb a.out core.2407 
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.

warning: exec file is newer than core file.
[New Thread 2407]
Cannot access memory at address 0x871730
(gdb) bt
#0  0x080483d5 in frame_dummy ()
#1  0x080484b4 in __libc_csu_init ()
#2  0x00894cc6 in ?? ()
#3  0x00000001 in ?? ()
#4  0xbf8d3514 in ?? ()
#5  0x08048321 in printf@plt ()
(gdb) 

         在执行gcc -g test.c后, 并么有更新core, 因此在进行gdb调试的时候, 还有warning:

 

warning: exec file is newer than core file.
[New Thread 2407]
Cannot access memory at address 0x871730

         此时, 定位不出具体问题也属正常现象。

 

        OK, 先这样。

 

 


 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值