到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文件)