1.core文件的简单介绍
在一个程序崩溃时,它一般会在程序的当前目录下生成一个core文件。core文件仅仅是一个内存映象(同时加上调试信息),主要是用来调试的。
2. 开启或关闭core文件的生成
以下命令可以检查生成core文件的选项是否打开:
ulimit –a
该命令将显示所有的用户定制,其中选项-a代表“all”。查看结果中形如:core file size (blocks, -c) 0的内容,如果红色文字为0,则表示未打开core文件;否则,已打开并代表core文件的最大kb字节数。
用以下命令来系统为当前bash的当前用户生成core文件,生成的文件一般在程序的当前目录下
#ulimit –c unlimited
unlimited表示coredump文件大小无限制。
另外,可以用以下命令来阻止系统生成core文件:
ulimit -c 0
0表示文件大小为0。
也可以修改系统文件来调整core选项
在/etc/profile通常会有这样一句话来禁止产生core文件,通常这种设置是合理的:
ulimit -S -c 0 > /dev/null 2>&1
但是在开发过程中有时为了调试问题,还是需要在特定的用户环境下打开core文件产生的设置
在用户的~/.bash_profile里加上ulimit -c unlimited来让特定用户可以产生core文件。
也可以用ulimit -c 1024,表示限制产生的core文件的大小不能超过1024kb
3. 设置Core Dump的核心转储文件目录和命名规则
/proc/sys/kernel/core_uses_pid可以控制产生的core文件的文件名中是否添加pid作为扩展,如果添加则文件内容为1,否则为0
/proc/sys/kernel/core_pattern可以设置格式化的core文件保存位置或文件名,比如原来文件内容是core-%e
可以这样修改:
echo "/corefile/core-%e-%p-%t"> /proc/sys/kernel/core_pattern
将会控制所产生的core文件会存放到/corefile目录下,产生的文件名为core-命令名-pid-时间戳
以下是参数列表:
%p - insert pid into filename 添加pid
%u - insert current uid into filename 添加当前uid
%g- insert current gid into filename 添加当前gid
%s - insert signal that caused the coredump into the filename 添加导致产生core的信号
%t - insert UNIX time that the coredump occurred into filename 添加core文件生成时的unix时间
%h - insert hostname where the coredump happened into filename 添加主机名
%e - insert coredumping executable name into filename 添加命令名
4. 使用core文件来调试程序
例如,a.c程序内容如下:
#include<stdio.h>
int main()
{
int a = 10;
int b;
a = a- 10;
b = 10 / a; //会出现除以0的错误,程序会崩溃
printf("%d", b);
}
1、用-g参数编译a.c程序文件,并执行
#gcc –g a.c –o a.out
#./a.out
Floating point exception (core dumped)
#ls
a.c a.out core
当前目录下生成了core文件。
2、使用gdb调试core文件
#gdb –core=core
GNU gdb (Ubuntu 7.7-0ubuntu3.1) 7.7
Copyright (C) 2014 Free SoftwareFoundation, Inc.
License GPLv3+: GNU GPL version 3 or later<http://gnu.org/licenses/gpl.html>
This is free software: you are free tochange and redistribute it.
There is NO WARRANTY, to the extentpermitted by law. Type "showcopying"
and "show warranty" for details.
This GDB was configured as"x86_64-linux-gnu".
Type "show configuration" forconfiguration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentationresources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search forcommands related to "word".
[New LWP 29993]
Core was generated by `./a.out'.
Program terminated with signal SIGFPE,Arithmetic exception.
#0 0x0000000000400546 in ?? ()
(gdb)
此时会显示生成此core文件的程序名(a.out),中止此程序的信号(SIGFPE)等等。
(gdb) bt
#0 0x0000000000400546 in ?? ()
#1 0x00007fff27fbf7d0 in ?? ()
#2 0x0000000000000000 in ?? ()
此时bt还不能看到调用堆栈,需要将程序的symbol读进来
(gdb) file ./a.out
Reading symbols from ./a.out...done.
这时就可以使用bt查看调用堆栈,用list查看程序在哪行代码中止了。
(gdb) bt
#0 0x0000000000400546 in main () at a.c:8
(gdb) list
1 #include <stdio.h>
2
3 int main()
4 {
5 int a = 10;
6 int b;
7 a = a - 10;
8 b = 10 / a;
9 printf("%d", b);
10