1.查看core文件限制
执行ulimit -a命令,显示如下:
core file size (blocks, -c) 1024
data seg size (kbytes, -d) unlimited
scheduling priority (-e) 0
file size (blocks, -f) unlimited
pending signals (-i) 63411
max locked memory (kbytes, -l) 64
max memory size (kbytes, -m) 10000
open files (-n) 1024
pipe size (512 bytes, -p) 8
POSIX message queues (bytes, -q) 819200
real-time priority (-r) 0
stack size (kbytes, -s) 8192
cpu time (seconds, -t) unlimited
max user processes (-u) 63411
virtual memory (kbytes, -v) unlimited
file locks (-x) unlimited
若显示core file size (blocks, -c) 0,则执行ulimit -c 1024,1024代表core文件最大为1024k,可自行设置大小。
ulimit -c 只是临时设置大小,若想永久修改core file size,则需要修改/etc/security/limits.conf(sudo或者切换到root),修改关于core的这一行:
* soft core 1024
修改完成后并不能立即生效。需要重新登录用户,即执行su user.
此时ulimit -a,应该看到修改后的值。
2.设置core文件的格式
echo “1” > /proc/sys/kernel/core_uses_pid
echo “core.%e.%p” > /proc/sys/kernel/core_pattern
权限不够可以切换到root,%e表示相关的命令名,%p表示进程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 添加命令名
产生的core文件默认在当前目录,也可指定到core文件到某个目录(需事先创建好):
echo “/tmp/core/core.%e.%p” > /proc/sys/kernel/core_pattern
3.实例
test.cpp
#include<stdio.h>
int main()
{
int *p = NULL;
*p = 0;
return 0;
}
gcc -o test test.cpp生成test可执行文件,
执行./test
报错:段错误(核心已转储)
并产生一个core文件core.test.6061,说明配置完成。
gdb ./test core.test.6061 可调试test,core文件非常大,记得及时清理。
本文介绍了在Ubuntu Linux系统中如何设置生成core文件,包括查看core文件限制使用`ulimit -a`命令,如何永久修改`/etc/security/limits.conf`文件,以及通过调整`/proc/sys/kernel/core_pattern`来定制core文件的格式和位置。通过这些步骤,当程序出现错误时,可以方便地使用gdb等工具进行调试。
672

被折叠的 条评论
为什么被折叠?



