• crash handler


    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    
    void InitDump(void);
    void Dump_handle(int sig_no);
    
    int main(int argc, char *argv[])
    {
        printf("pid: %d\n", getpid());
        InitDump();
    //    int *p = nullptr;//SIGSEGV
    //    *p = 123132;
        while(1);
        return 0;
    }
    
    void InitDump(void)
    {
        // 试图访问未分配给自己的内存, 或试图往没有写权限的内存地址写数据.
        signal(SIGSEGV, Dump_handle);
        // 非法地址, 包括内存地址对齐(alignment)出错。比如访问一个四个字长的整数, 但其地址不是4的倍数。它与SIGSEGV的区别在于后者是由于对合
        // 法存储地址的非法访问触发的(如访问不属于自己存储空间或只读存储空间)。
        signal(SIGBUS, Dump_handle);
        // 在发生致命的算术运算错误时发出. 不仅包括浮点运算错误, 还包括溢出及除数为0等其它所有的算术的错误。
        signal(SIGFPE, Dump_handle);
        // 调用abort函数生成的信号。
        signal(SIGABRT, Dump_handle);
        sigset(SIGTERM, Dump_handle);//kill,pkill,killall  SIGTERM: 15
        sigset(SIGINT, Dump_handle);//Ctrl+C   SIGINT:2
    }
    
    void Dump_handle(int sig_no)
    {
    //    if(SIGTERM == sig_no || SIGINT == sig_no){
    //        //realease();
    //        exit(0);
    //    }
        const static int BUF_SIZE = 30;
        void *buffer[BUF_SIZE] = {0};
    
        int pointer_num = backtrace(buffer, BUF_SIZE);
        fprintf(stderr, "sig_no:%d, Obtained %d stack frames.\n", sig_no, pointer_num);
        char** string_buffer = backtrace_symbols(buffer, pointer_num);
        //崩溃日志保存,只写一次(每次覆盖),防止日志过大----------------------------------------
        int fd = open("crash.log", O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
        backtrace_symbols_fd(buffer, pointer_num, fd);
        (void)close(fd);
        //-----------------------------------------------------------------------------
        if(string_buffer == NULL){
            perror("backtrace_symbols");
            exit(EXIT_FAILURE);
        }
        printf("--------print backtrace begin--------\n");
    //    LOG_INFO("signo:%s \n", sig_no);
        for(int i = 0; i < pointer_num; i++){
            fprintf(stderr, "%s\n", string_buffer[i]);
    //        LOG_INFO("ERROR,Stack frame:%s \n",string_buffer[i]);//save to log
        }
        printf("--------print backtrace end----------\n");
        free(string_buffer);
        exit(0);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
  • 相关阅读:
    宝塔手把手教学-Linux面板安装LNMP & worldpress个人博客
    YOLOv8学习
    LibVLC: Can‘t load vlcjni library“__emutls_get_address“ “/lib/arm/libvlc.so“
    本机MySQL数据库安装
    【打卡】牛客网:BM38 在二叉树中找到两个节点的最近公共祖先
    浅谈小程序开源业务架构建设之路
    Linux系统中驱动之设备树的platform驱动实现
    Hive的常用内置函数
    关于yarn 的Plug‘n‘Play特性
    JVM相关知识点
  • 原文地址:https://blog.csdn.net/a379039233/article/details/127645594