Linux下的dump文件一般称为core文件 ,可以用来记录程序崩溃前的线程callstack信息,内存信息等,可以作为排错的一种辅助手段。生存的core大小可以限制,保存的内容也可以灵活的选择,因此在MID上也可以使用。
一 core文件的生成
1 查看core信息
输入 ulimit -a,显示如下:
core file size (blocks, -c) 1024
data seg size (kbytes, -d) unlimited
scheduling priority (-e) 20
file size (blocks, -f) unlimited
pending signals (-i) 16382
max locked memory (kbytes, -l) 64
max memory size (kbytes, -m) unlimited
open files (-n) 1024
pipe size (512 bytes, -p) 8
POSIX message queues (bytes, -q) 819200
real-time priority (-r) 0
stack size (kbytes, -s) 8192
cpu time (seconds, -t) unlimited
max user processes (-u) unlimited
virtual memory (kbytes, -v) unlimited
file locks (-x) unlimited
如果core file size为0的话,则输入ulimit -c 1024, 表示设置成1024个block,如果超过这个值就不再写core文件
2 测试程序如下
#include <stdio.h>
int test()
{
int * p = NULL;
printf("%d /n",*p);
}
int main()
{
test();
}
在ubuntu上编译程序:
gcc –Wall -g test.c
加上可执行权限
chmod +x a.out
执行 ,在同级目录下生成了一个core文件
gdb --core=core
显示如下:
GNU gdb 6.8-debian
Copyright (C) 2008 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-linux-gnu".
(no debugging symbols found)
Core was generated by `./a.out'.
Program terminated with signal 11, Segmentation fault.
[New process 18079]
#0 0x0000000000400520 in ?? ()
(gdb)
接下来指定可执行文件(含调试信息)
(gdb) file ./a.out
显示如下:Reading symbols from /home/share/test/a.out...done.
执行(gdb) bt,显示如下:
#0 0x0000000000400520 in just_a_test () at hello.c:8
#1 0x0000000000400546 in main () at hello.c:15
即定位到相应的错误行