您的位置:首页 > 其它

如何产生core dump文件

2015-11-26 09:03 232 查看


如何产生core dump文件

在一般Linux系统中,默认是不会产生core dump文件的,通过ulimit -c来查看core dump文件的大小,一般开始是0,可以设置core文件大小,ulimit -c 1024(kbytes单位)或者ulimit -c unlimited。


core dump文件输出设置

一般默认是当前目录,可以在/proc/sys/kernel中找
到core-user-pid,通过


1.使core文件名加上pid号

echo "1" > /proc/sys/kernel/core-user-pid


2.控制core文件保存位置和文件名格式

mkdir -p /root/corefile
echo "/root/corefile/core-%e-%p-%t" > /proc/sys/kernel/core-pattern


以下是参数列表:

%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 添加命令名


用gdb查看core文件

下面我们可以在发生运行时信号引起的错误时发生core dump了.编译时加上-g

发生core dump之后, 用gdb进行查看core文件的内容, 以定位文件中引发core dump的行.


gdb [exec file] [core file]


如:


gdb ./test test.core


在进入gdb后, 用bt命令查看backtrace以检查发生程序运行到哪里, 来定位core dump的文件行.


实例讲解

//test.c

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)

如果生成的是test.core.

gdb ./test test.core
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: