linux C++下捕获崩溃日志

#include <stdio.h>
#include <signal.h>
#include <time.h>
#include <execinfo.h>
#include <string.h>
#include <string>
#include <sys/stat.h>
const int MAX_STACK_FRAMES = 128;
void sig_crash(int sig)
{
    FILE* fd;
    struct stat buf;
    stat("./crash.log", &buf);
    if(buf.st_size > 10*1000*1000){ // 超过10兆则清空内容
        fd = fopen("./crash.log", "w");
    } else {
        fd = fopen("./crash.log", "at");
    }
    
    if (NULL == fd)
    {
        exit(0);
    }
    try
    {
        char szLine[512] = {0, };
        time_t t = time(NULL);
        tm* now = localtime(&t);
        int nLen1 = sprintf(szLine,
                            "#########################################################\n[%04d-%02d-%02d %02d:%02d:%02d][crash signal number:%d]\n",
                            now->tm_year + 1900, 
                            now->tm_mon + 1, 
                            now->tm_mday, 
                            now->tm_hour, 
                            now->tm_min, 
                            now->tm_sec, 
                            sig);
        fwrite(szLine, 1, strlen(szLine), fd);
#ifdef __linux
        void* array[MAX_STACK_FRAMES];
        size_t size = 0;
        char** strings = NULL;
        size_t i, j;
        signal(sig, SIG_DFL);
        size = backtrace(array, MAX_STACK_FRAMES);
        strings = (char**)backtrace_symbols(array, size);
        //fprintf(stderr, "oncrash;\n");
        for (i = 0; i < size; ++i)
        {
            char szLine[512] = {0, };
            sprintf(szLine, "%d %s\n", i, strings[i]);
            fwrite(szLine, 1, strlen(szLine), fd);
            
            std::string symbol(strings[i]);
            size_t pos1 = symbol.find_first_of("[");
            size_t pos2 = symbol.find_last_of("]");
            std::string address = symbol.substr(pos1 + 1, pos2 - pos1 -1);
            char cmd[128] = {0, };
            sprintf(cmd, "addr2line -e test %s", address.c_str()); // test为应用程序名称,需要改为用户自己的应用程序名
            FILE *fPipe = popen(cmd, "r");
            if(fPipe != NULL){
                char buff[1024];
                memset(buff, 0, sizeof(buff));
                char* ret = fgets(buff, sizeof(buff), fPipe);
                pclose(fPipe);
                fwrite(ret, 1, strlen(ret), fd);
            }
        }
       free(strings);
#endif // __linux
   }
   catch (...)
   {
       //
   }
   fflush(fd);
   fclose(fd);
   fd = NULL;
   exit(0);
}


int main()
{
signal(SIGSEGV, sig_crash);
signal(SIGABRT, sig_crash);
int* a = NULL;
a[10] = 0; // crash2
return 0;

}

最后使用g++ -g -o test crashdemo.cpp编译出可执行文件

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值