GDB使用之调试CoreDump

在软件出现问题的时候,有时候没有现场环境让我们来调试,或者是想保存现场环境,以防现场环境被我们给破坏了,导致无法再重现问题。碰到这种情况,我们一般都会把进程给DUMP下来。也可以设置程序崩溃时自动DUMP。
以前一直使用WinDBG来调试Dump文件,分析种种无法重现的问题。来这个由于是在Linux平台上,所有就看GDB是否也有这种功能。
一 、命令
generated core file(gcore) [filename] 产生core dump文件
gdb -c dump文件 调试core dump文件 
symbol-file fileName 加载调试符文件

(我试过MinGW中不能使用产生dump文件命令,在Linux上可以,Cygwin我没试,不知道可不可以)

二、例子

1、产生core文件

linux-26u1:/home/project/topcoder# gdb gdb_core_dump
GNU gdb (GDB) SUSE(6.8.50.20090302-1.5.18)
Copyright (C) 2009Free Software Foundation, Inc.
License GPLv3+: GNUGPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is freesoftware: you are free to change and redistribute it.
There is NO WARRANTY,to the extent permitted by law. Type"show copying"
and "showwarranty" for details.
This GDB wasconfigured as "i586-suse-linux".
For bug reportinginstructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
(gdb) l
1 #include<stdio.h>

3 intmain()
4 {
5 int val1 = 1;
6 int val2 = 2;

8 printf("%d,%d\n",val1,val2);
9 }
(gdb) br 8
Breakpoint 1 at0x80483d3: file gdb_core_dump.c, line 8.
(gdb) r
Starting program:/home/project/topcoder/gdb_core_dump
Missing separatedebuginfo for /lib/ld-linux.so.2
Missing separatedebuginfo for /lib/libc.so.6

Breakpoint 1, main ()at gdb_core_dump.c:8
8 printf("%d,%d\n",val1,val2);
(gdb) gcore
Saved corefilecore.8469
(gdb) rwatch val1
Hardware readwatchpoint 2: val1
(gdb) c
Continuing.
1,2

Watchpoint 2 deletedbecause the program has left the block in
which its expressionis valid.
0xb7ebd89c in__libc_start_main () from /lib/libc.so.6
(gdb) kb
Undefined command:"kb". Try "help".
(gdb) bt
#0 0xb7ebd89c in __libc_start_main () from/lib/libc.so.6
#1 0x08048331 in _start ()
(gdb) ls
Undefined command:"ls". Try "help".
(gdb) q
The program isrunning. Quit anyway (and kill it)? (yor n) y
linux-26u1:/home/project/topcoder # objdump -h core.8469

core.8469: file format elf32-i386

Sections:
Idx Name Size VMA LMA File off Algn
0 note0 0000046c 00000000 00000000 000001b4 2**0
CONTENTS, READONLY
1 .reg/8469 00000044 00000000 00000000 000002a0 2**2
CONTENTS
2 .reg 00000044 00000000 00000000 000002a0 2**2
CONTENTS
3 .reg2/8469 0000006c 00000000 00000000 000002fc 2**2
CONTENTS
4 .reg2 0000006c 00000000 00000000 000002fc 2**2
CONTENTS
5 .reg-xfp/8469 00000200 00000000 00000000 0000037c 2**2
CONTENTS
6 .reg-xfp 00000200 00000000 00000000 0000037c 2**2
CONTENTS
7 .auxv 00000090 00000000 00000000 00000590 2**2
CONTENTS
8 load1 00000000 08048000 00000000 00000620 2**0
CONTENTS, ALLOC, LOAD,READONLY, CODE
9 load2 00001000 08049000 00000000 00000620 2**0
CONTENTS, ALLOC, LOAD
10 load3 00001000 b7ea7000 00000000 00001620 2**0
CONTENTS, ALLOC, LOAD
11 load4 00000000 b7ea8000 00000000 00002620 2**0
CONTENTS, ALLOC, LOAD,READONLY, CODE
12 load5 00000000 b7fce000 00000000 00002620 2**0
CONTENTS, ALLOC, LOAD,READONLY
13 load6 00002000 b7fd0000 00000000 00002620 2**0
CONTENTS, ALLOC, LOAD
14 load7 00003000 b7fd2000 00000000 00004620 2**0
CONTENTS, ALLOC, LOAD
15 load8 00001000 b7fe5000 00000000 00007620 2**0
CONTENTS, ALLOC, LOAD
16 load9 00000000 b7fe6000 00000000 00008620 2**0
CONTENTS, ALLOC, LOAD,READONLY, CODE
17 load10 00002000 b8000000 00000000 00008620 2**0
CONTENTS, ALLOC, LOAD
18 load11 00015000 bffeb000 00000000 0000a620 2**0
CONTENTS, ALLOC, LOAD
linux-26u1:/home/project/topcoder#


二、调试core文件

linux-26u1:/home/project/topcoder# gdb -c core.8469
GNU gdb (GDB) SUSE(6.8.50.20090302-1.5.18)
Copyright (C) 2009Free Software Foundation, Inc.
License GPLv3+: GNUGPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is freesoftware: you are free to change and redistribute it.
There is NO WARRANTY,to the extent permitted by law. Type"show copying"
and "showwarranty" for details.
This GDB wasconfigured as "i586-suse-linux".
For bug reportinginstructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Failed to read avalid object file image from memory.
Core was generated by`/home/project/topcoder/gdb_core_dump'.
Program terminatedwith signal 5, Trace/breakpoint trap.
#0 0x080483d3 in ?? ()
(gdb) bt
#0 0x080483d3 in ?? ()
#1 0xb7ebd89c in ?? ()
#2 0x08048331 in ?? ()
(gdb) lb
Undefined command:"lb". Try "help".
(gdb) l
No symbol table isloaded. Use the "file"command.
(gdb) -symbol
Undefined command:"-symbol". Try"help".
(gdb) -symbol--file
Undefined command:"-symbol--file". Try"help".
(gdb) symbol-file /home/project/topcoder/gdb_core_dump
Reading symbols from/home/project/topcoder/gdb_core_dump...done.
(gdb) l
1 #include<stdio.h>

3 intmain()
4 {
5 int val1 = 1;
6 int val2 = 2;

8 printf("%d,%d\n",val1,val2);
9 }
(gdb) br 8
Breakpoint 1 at0x80483d3: file gdb_core_dump.c, line 8.
(gdb)

原文地址:https://www.cnblogs.com/Quincy/p/2418467.html