• 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
  • 相关阅读:
    【C++】bitset介绍与用法讲解
    MSOS604A是德科技keysight MSOS604A示波器
    大数据常用的Linux命令
    内部类_Java
    MySQL 枚举类型如何定义比较好 tinyint?enum?varchar?
    CentOS(2)——rpm和yum区别与联系
    LeetCode 最大矩形,最大正方形系列 84. 85. 221. 1277. 1725. (单调栈,动态规划)
    文件操作【详解】
    ctfshow-web2(SQL注入)
    中小企业选择ERP系统时应关注的10个关键功能
  • 原文地址:https://blog.csdn.net/a379039233/article/details/127645594