`
lxy2330
  • 浏览: 459779 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

使用CORE DUMP

 
阅读更多
程序出现SIGSEGV ,Segmentation fault 等错误,没有提示所在的行位置,无法定位问题,这时就需要用到CORE DUMP文件。
在此CORE   不是核心的意思,而是只内存。
1.一般linux不会产生CORE DUMP文件,通过ulimit -c来查看core dump文件的大小,一般开始是0,可以设置core文件大小,ulimit -c 1024(kbytes单位)或者ulimit -c unlimited。
2.输出目录一般是当前目录,或者可以在/proc/sys/kernel中找到core-user-pid,通过

echo "1" > /proc/sys/kernel/core-user-pid使core文件名加上pid号,还可以用

mkdir -p /root/corefile

echo "/root/corefile/core-%e-%p-%t" > /proc/sys/kernel/core-pattern 控制core文件保存位置和文件名格式。

以下是参数列表:
    %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 添加命令名

3.用GDB查看

下面我们可以在发生运行时信号引起的错误时发生core dump了.编译时加上-g
发生core dump之后, 用gdb进行查看core文件的内容, 以定位文件中引发core dump的行.
gdb [exec file] [core file]
如:
gdb ./test test.core
在进入gdb后, 用bt命令查看backtrace以检查发生程序运行到哪里, 来定位core dump的文件行.

5. 给个例子

test.c

#include <stdio.h>

void a(){

   char *p = NULL;

   printf("%d/n", *p);

}

int main()

{

    a();

    return 0;

}

编译 gcc -g -o test test.c

运行 ./test

报segmentation fault(core dump)

gdb ./test test.core如果生成的是test.core.(我在UBUNTU上生成的的是CORE文件)

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics