相关链接: http://blog.csdn.net/yuyin86/article/details/8014840
Core dump在服务器上的应用
// corefile.c
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
//#include<sys/wait.h>
// 设置core文件大小,进程能打开的文件数大小的函数
int SetRLimit()
{
#ifdef WIN32
#else
struct rlimit tRLimit;
int iRtn = 0;
printf("SetRLimit ...\n");
// 设置core文件大小
iRtn = getrlimit(RLIMIT_CORE, &tRLimit);
if(0 == iRtn)
{
printf("getrlimit corefile size limit_cur = %d, max = %d\n", tRLimit.rlim_cur, tRLimit.rlim_max);
tRLimit.rlim_max =RLIM_INFINITY;//设置最大限制为无限
tRLimit.rlim_cur = RLIM_INFINITY;/设置最大限制为无限
iRtn = setrlimit(RLIMIT_CORE, &tRLimit);
printf("setrlimit corefile size limit:cur = %d, return %d\n", tRLimit.rlim_cur, iRtn);
}
// 设置进程能打开的文件数大小
iRtn = getrlimit(RLIMIT_NOFILE, &tRLimit);
if(0 == iRtn)
{
printf("getrlimit number of files limit : cur = %d, max = %d\n", tRLimit.rlim_cur, tRLimit.rlim_max);
tRLimit.rlim_cur = 10000;
iRtn = setrlimit(RLIMIT_NOFILE, &tRLimit);
printf("setrlimit number of files limit : cur = %d, return %d\n",tRLimit.rlim_cur, iRtn);
}
#endif
return 0;
}
int mian()
{
unsigned char *ptr = 0x00;
SetRLimit();
*ptr =0x00; //会产生段错误,测试生产core文件
return 0;
}
运行:
ulimit -c //查看系统默认core文件大小,如果为0,不会生产core文件
ulimit -c unlimited //设置core文件的大小为不限大小
gcc -g corefile.c -o corefile //记得加 -g ,不然没有行号,是一个地址值,看不出什么来
gdb ./corefile core //用gdb运行产生的core文件
bt //查看出问题的堆栈