core dump 生成与调试

来源:互联网 发布:企业域名备案流程 编辑:程序博客网 时间:2024/06/14 00:05

//---------------------------------------------------------------
说明:

   此在linux上尝试通过,gdb,gcc都是安装系统时已

安装好。
//---------------------------------------------------------------

 

//---------------------------------------------------------------
1. core文件的简单介绍
//---------------------------------------------------------------

在一个程序崩溃时,它一般会在指定目录下生成一个core文件。core文件仅仅是一个内存映象(同时加上调试信息),主要是用来调试的。


//---------------------------------------------------------------
2. 开启或关闭core文件的生成
//---------------------------------------------------------------

用以下命令来允许系统生成core文件:
ulimit -c <大小>  // 只是当前登陆用户有效,重新退出则无效
ulimit -c 1024 // 则限制产生的core文件的大小不能超过1024kb

下面的命令可以检查生成core文件的选项是否打开:
ulimit -a

也可以修改系统文件来调整core选项
ulimit -S -c 0 > /dev/null 2>&1
但是在开发过程中有时为了调试问题,还是需要在特定的用户环境下打开core文件产生的设置

运行ulimit -c unlimited来让特定的用户可以产生core文件
如果ulimit -c 0 则也是禁止产生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
可以这样修改:
echo "/corefile/core-%e-%p-%t" > core_pattern // 此方法在我的环境中不行,采用的命令方式是:
sysctl -a | grep core_pattern // 查看当前的 core_pattern 设置样式
sysctl -w kernel.core_pattern="/tmp/core-%s-%h-%p-%e"

将会控制所产生的core文件会存放到/tmp 目录下,产生的文件名为core-导致产生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文件
//---------------------------------------------------------------

在core文件所在目录下键入:
gdb 执行的命令 core文件名

它会启动GNU的调试器,来调试core文件,并且会显示生成此core文件的程序名,中止此程序的信号等等
如果你已经知道是由什么程序生成此core文件的,比如MyServer崩溃了生成 core-8-localhost-6536-eg,那么用此指令调试:
gdb ./eg core-8-localhost-6536-eg
以后怎么办就该去学习gdb的使用了


//---------------------------------------------------------------
5. 一个小方法来测试产生core文件
//---------------------------------------------------------------

导致溢出c程序样例 (文件名:main.c)

int main()
 
{
 
    int i=0;
 
    int j=5;
 
    int tmp;
 
    for(; i < 10; i++, j--)
 
    {
 
        tmp=i/j;
 
        printf("%d/%d=%dn", i, j, tmp);
 
    }
 
}

该程序运行到i=5时,会发生浮点运算错误(被除数等于0,j=0)

编译上面的程序

gcc -g main.c -o eg

./eg

发生core-dump后,如果核心转储文件是 core-8-localhost-6536-eg,执行下面的命令

gdb ./eg core-8-localhost-6536-eg

========== 以下是运行后的显示信息样例 ==================
GNU gdb Red Hat Linux (6.5-37.el5rh)
Copyright (C) 2006 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB.  Type "show warranty" for details.
This GDB was configured as "i386-redhat-linux-gnu"...Using host libthread_db library "/lib/libthread_db.so.1".
 
 
warning: exec file is newer than core file.
 
warning: Can't read pathname for load map: Input/output error.
Reading symbols from /lib/libc.so.6...done.
Loaded symbols for /lib/libc.so.6
Reading symbols from /lib/ld-linux.so.2...done.
Loaded symbols for /lib/ld-linux.so.2
Core was generated by `./eg'.
Program terminated with signal 8, Arithmetic exception.
#0  0x080483ad in main () at main.c:17
17              tmp=i/j;
========== end==================

原创粉丝点击