Liunx操作-Record11—gdb跟踪core(core文件相关的操作)

目录

查看core文件设置的大小:"ulimit -c"

设置core文件无限大:"ulimit -c unlimited"

取消生成core:"ulimit-c 0"

如何使用core文件:"gdb app core"

设置core文件名字的方法


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

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值