gdb调试core文件
本案例主要介绍如何用gdb根据进程生成的core文件定位进程core dump的原因。
1. 打开core文件生成开关
首先生成core需要调整服务器设置,输入ulimit -c如果结果为0表示没有打开。在/etc/profile中增加ulimit -S -c unlimited > /dev/null 2>&1,然后执行source /etc/profile,再次执行ulimit -c如果结果为unlimited,则表示设置成功。
2. 调试过程
示例代码中testCore.c将malloc的指针free了两次,会引起进程coredump。使用file core.5257可以查看core文件是哪个可执行文件生成的,执行gdb [exec file] [core file]后,输入where或bt能够直接打印core dump时的堆栈信息。
[root@localhost bin]# source /etc/profile
[root@localhost bin]# ulimit -c
unlimited
[root@localhost bin]# ./testCore
*** Error in `./testCore': double free or corruption (fasttop): 0x0000000001757010 ***
======= Backtrace: =========
/lib64/libc.so.6(+0x81489)[0x7fb063db1489]
./testCore[0x40058b]
/lib64/libc.so.6(__libc_start_main+0xf5)[0x7fb063d523d5]
./testCore[0x400499]
======= Memory map: ========
00400000-00401000 r-xp 00000000 fd:03 67108941 /opt/testgdb/bin/testCore
00600000-00601000 r--p 00000000 fd:03 67108941 /opt/testgdb/bin/testCore
00601000-00602000 rw-p 00001000 fd:03 67108941 /opt/testgdb/bin/testCore
01757000-01778000 rw-p 00000000 00:00 0 [heap]
7fb05c000000-7fb05c021000 rw-p 00000000 00:00 0
7fb05c021000-7fb060000000 ---p 00000000 00:00 0
7fb063b1a000-7fb063b2f000 r-xp 00000000 fd:00 134304845 /usr/lib64/libgcc_s-4.8.5-20150702.so.1
7fb063b2f000-7fb063d2e000 ---p 00015000 fd:00 134304845 /usr/lib64/libgcc_s-4.8.5-20150702.so.1
7fb063d2e000-7fb063d2f000 r--p 00014000 fd:00 134304845 /usr/lib64/libgcc_s-4.8.5-20150702.so.1
7fb063d2f000-7fb063d30000 rw-p 00015000 fd:00 134304845 /usr/lib64/libgcc_s-4.8.5-20150702.so.1
7fb063d30000-7fb063ef2000 r-xp 00000000 fd:00 134378497 /usr/lib64/libc-2.17.so
7fb063ef2000-7fb0640f2000 ---p 001c2000 fd:00 134378497 /usr/lib64/libc-2.17.so
7fb0640f2000-7fb0640f6000 r--p 001c2000 fd:00 134378497 /usr/lib64/libc-2.17.so
7fb0640f6000-7fb0640f8000 rw-p 001c6000 fd:00 134378497 /usr/lib64/libc-2.17.so
7fb0640f8000-7fb0640fd000 rw-p 00000000 00:00 0
7fb0640fd000-7fb06411f000 r-xp 00000000 fd:00 134378490 /usr/lib64/ld-2.17.so
7fb064306000-7fb064309000 rw-p 00000000 00:00 0
7fb06431c000-7fb06431e000 rw-p 00000000 00:00 0
7fb06431e000-7fb06431f000 r--p 00021000 fd:00 134378490 /usr/lib64/ld-2.17.so
7fb06431f000-7fb064320000 rw-p 00022000 fd:00 134378490 /usr/lib64/ld-2.17.so
7fb064320000-7fb064321000 rw-p 00000000 00:00 0
7ffd08368000-7ffd08389000 rw-p 00000000 00:00 0 [stack]
7ffd083d1000-7ffd083d3000 r-xp 00000000 00:00 0 [vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0 [vsyscall]
已放弃(吐核)
[root@localhost bin]# ls
1.ok core.5135 core.5257 testCore
[root@localhost bin]# file core.5257 #查看core文件是谁生成的
core.5257: ELF 64-bit LSB core file x86-64, version 1 (SYSV), SVR4-style, from './testCore', real uid: 0, effective uid: 0, real gid: 0, effective gid: 0, execfn: './testCore', platform: 'x86_64'
[root@localhost bin]# gdb testCore core.5257
GNU gdb (GDB) Red Hat Enterprise Linux 7.6.1-114.el7
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 "x86_64-redhat-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /opt/testgdb/bin/testCore...done.
[New LWP 5257]
Core was generated by `./testCore'.
Program terminated with signal 6, Aborted.
#0 0x00007fb063d66207 in raise () from /lib64/libc.so.6
Missing separate debuginfos, use: debuginfo-install glibc-2.17-260.el7.x86_64 libgcc-4.8.5-36.el7.x86_64
(gdb) where #打印堆栈
#0 0x00007fb063d66207 in raise () from /lib64/libc.so.6
#1 0x00007fb063d678f8 in abort () from /lib64/libc.so.6
#2 0x00007fb063da8d27 in __libc_message () from /lib64/libc.so.6
#3 0x00007fb063db1489 in _int_free () from /lib64/libc.so.6 #1 2 3 是systemcall
#4 0x000000000040058b in main () at /opt/test/src/testCore.c:9
#这里可以看到core的位置是testCore.c的第9行
(gdb) quit
[root@localhost bin]#