目录
设置core文件无限大:"ulimit -c unlimited"
core文件相当于案发现场,就是用于记录程序是怎么死的,下面还是用"Liunx操作-Record10"中的代码,来进行举例,将第19行的注释进行清除,保留一个bug:"Msg[0] = '1';"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "head.h"
typedef struct TTfunInfo
{
int fun_type;//函数的类型
int a;//函数的第一个参数
int b;//函数的第二个参数
char funname[10];//函数名称
}TfunInfo;
int main(int argc,char *argv[])
{
int a = 2;
int i = 0;
int a1 = 10,b1=5;
TfunInfo funinfo[2];
// char *Msg="I will die!";
Msg[0] = '1';
if(argc == 3)
{
a1 = atoi(argv[1]);
b1 = atoi(argv[2]);
funinfo[0].a = a1;
funinfo[0].b = b1;
funinfo[1].a = a1;
funinfo[1].b = b1;
}
for(i = 0; i < 2; i ++)
{
printf("i===%d,LINE=%d\n",i,__LINE__);
if(i == 0)
{
funinfo[i].fun_type = 1;//call sum
printf("begin call sum\n");
strcpy(funinfo[i].funname,"sum");
sum(funinfo[i].a,funinfo[i].b);
}
if(i == 1)
{
funinfo[i].fun_type = 2;//call mul
printf("begin call mul\n");
strcpy(funinfo[i].funname,"mul");
mul(funinfo[i].a,funinfo[i].b);
}
}
printf("byebye\n");
return 0;
}
查看core文件设置的大小:"ulimit -c"
一般情况下,系统默认将core文件的大小设置为0,所以通过"ls"查看文件,并不能查看到它
那么,下面将core文件可以生成的大小设置成无限大(core文件的目的是用来记录代码运行出错的信息,如果没报错,那core肯定就是0了,如果报错了,那记录的信息多大,取决于代码错了多少了,我们唯一能做的,就是不限制可生成的core文件的大小,让其"自由"的记录错误信息)
设置core文件无限大:"ulimit -c unlimited"
如果报错了,就会生成core文件,来记录"翻车现场"
取消生成core:"ulimit-c 0"
如何使用core文件:"gdb app core"
设置core文件名字的方法
为了防止每次新生成的core文件都会覆盖上次的,就采用如下的方式,将时间戳加到core文件的命名当中,就可以保留每次的core文件了!
- 需要root权限 sudo su
- 执行: echo "core-%e-%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 添加命令名