Linux调试core dump文件

转载http://blog.csdn.net/xabc3000/article/details/6823639

 查看默认的一些参数,注意core file size是个0,程序出错时不会产生core文件了。

# ulimit -a
core file size (blocks, -c) 0
data seg size (kbytes, -d) unlimited
file size (blocks, -f) unlimited
max locked memory (kbytes, -l) 4
max memory size (kbytes, -m) unlimited
open files (-n) 2048
pipe size (512 bytes, -p) 8
stack size (kbytes, -s) 10240
cpu time (seconds, -t) unlimited
max user processes (-u) 7168
virtual memory (kbytes, -v) unlimited

修改ulimit的设置,让它产生。1024是随便取的,要是core文件大于1024个

块,就产生不出来了。

# ulimit -c 1024

# ulimit -a
core file size (blocks, -c) 1024
data seg size (kbytes, -d) unlimited
file size (blocks, -f) unlimited
max locked memory (kbytes, -l) 4
max memory size (kbytes, -m) unlimited
open files (-n) 2048
pipe size (512 bytes, -p) 8
stack size (kbytes, -s) 10240
cpu time (seconds, -t) unlimited
max user processes (-u) 7168
virtual memory (kbytes, -v) unlimited

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <string.h>
 4 #include <sys/types.h>
 5 #include <fcntl.h>
 6 #include <dirent.h>
 7 #include <unistd.h>
 8 
 9 int main(int argc, char** argv)
10 {
11     int* p;
12     int pid;
13 
14     pid = getpid();
15     printf("%d\n", pid);
16 
17     scanf("%d", p);
18 
19     return 0;
20 }

# gcc main.c -g -o app
# ./app
7981
123
Segmentation fault(core dumped)
#ls core.7981
core.7981

#
#gdb -c core.7981  或使用gdb --core=core.7981
GNU gdb (GDB) Fedora (7.1-18.fc13)
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/>.
Missing separate debuginfo for the main executable file
Try: yum --disablerepo='*' --enablerepo='*-debuginfo' install /usr/lib/debug/.build-id/37/b728299f194ea79ab89b3299835ea1a9d21af6
[New Thread 7981]
Core was generated by `./app'.
Program terminated with signal 11, Segmentation fault.
#0  0x005c2395 in ?? ()
(gdb)bt
#0  0x005c2395 in ?? ()
#1  0xbffc99b0 in ?? ()
#2  0x005cf319 in ?? ()
#3  0x006fa440 in ?? ()
#4  0x08048480 in ?? ()
#5  0x00588cc6 in ?? ()
#6  0x00000001 in ?? ()
#7  0xbffc9df4 in ?? ()
#8  0x080483b1 in ?? ()

此时用bt看不到backtrace,也就是调用堆栈,原来GDB还不知道符号信息在哪里。我们告诉它一下:
(gdb) file ./app
Reading symbols from /root/app...done.
(gdb) bt
#0  0x005c2395 in ?? ()
#1  0xbffc99b0 in ?? ()
#2  0x005cf319 in ?? ()
#3  0x006fa440 in ?? ()
#4  0x08048480 in main (argc=1, argv=0xbffc9df4) at main.c:17
(gdb) l
5       #include <fcntl.h>
6       #include <dirent.h>
7       #include <unistd.h>
8
9       int main(int argc, char** argv)
10      {
11              int* p;
12              int pid;
13
14              pid = getpid();
(gdb) l
15              printf("%d\n", pid);
16
17              scanf("%d", p);
18
19              return 0;
20      }
21
(gdb)quit

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

在实验这个程序的时候还发现:

# gcc main.c -o app   重点注意这行命令
# ./app
8015
123
Segmentation fault(core dumped)
# gdb -c core.8015
GNU gdb (GDB) Fedora (7.1-18.fc13)
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/>.
Missing separate debuginfo for the main executable file
Try: yum --disablerepo='*' --enablerepo='*-debuginfo' install /usr/lib/debug/.build-id/09/9b1f4b4531a91f34062425db5573a2f367d388
[New Thread 8015]
Core was generated by `./app'.
Program terminated with signal 11, Segmentation fault.
#0  0x005c2395 in ?? ()
(gdb) bt
#0  0x005c2395 in ?? ()
#1  0xbfaf0880 in ?? ()
#2  0x005cf319 in ?? ()
#3  0x006fa440 in ?? ()
#4  0x08048480 in ?? ()
#5  0x00588cc6 in ?? ()
#6  0x00000001 in ?? ()
#7  0xbfaf0cc4 in ?? ()
#8  0x080483b1 in ?? ()
(gdb) file ./app
Reading symbols from /root/app...(no debugging symbols found)...done.   注意这个结果。
(gdb)quit

由此可以知道如果想要调试core.xxx文件可以使用gdb,而使用gdb调试的时候必须加上-g参数,否则很多的符号表将找不到。

故在生成可执行程序的时候需要加上-g参数,以方便程序crash生成core.xxx文件时,使用gdb进行调试。

原文地址:https://www.cnblogs.com/Robotke1/p/3039108.html