当前位置:   article > 正文

win c++异常处理并生成 .dmp 文件

win c++异常处理并生成 .dmp 文件

win c++异常处理并生成 .dmp 文件

依赖win10库文件 DbgHelp.dll 或者 DbgHelp.lib

CrashHander.h

#ifndef CRASH_H
#define CRASH_H

//异常处理
#include <Windows.h>
#include <DbgHelp.h>
#include <iostream>
#include <sstream>
#include <iomanip>



/*!
 * \brief 整数转16进制字符串
 * \param s_src
 * \param len
 * \return
 */
static std::string convertToBase16Str(const unsigned char* s_src, int len)
{
    std::string str("0x");
    std::stringstream ioss;

    int a = 0x1234;
    char b = static_cast<char>(a);
    if (b == 0x12)
    {
        //cout << "大端模式" << endl;
        for (int i = 0; i < len; i++) {
            ioss << std::hex << std::setw(2) << std::setfill('0') << (int)s_src[i];
            str.append(ioss.str());
            ioss.str("");
        }
    }
    else if (b == 0x34)
    {
        //cout << "小端模式" << endl;
        int i = len-1;
        while(i>=0) {
            ioss << std::hex /*<< std::setw(2) << std::setfill('0')*/ << (int)s_src[i--];
            str.append(ioss.str());
            ioss.str("");
        }
    }
    ioss.clear();
    return str;
}



//异常捕获
static LONG ApplicationCrashHandler(EXCEPTION_POINTERS *pException)
{
    wchar_t szFileName[MAX_PATH] = { 0 };
    wchar_t szVersion[] = L"DumpFile";
    SYSTEMTIME stLocalTime;
    GetLocalTime(&stLocalTime);

    //wsprintf in user32.lib
    wsprintf(szFileName, L"%s-%04d%02d%02d-%02d%02d%02d.dmp",
    szVersion, stLocalTime.wYear, stLocalTime.wMonth, stLocalTime.wDay,
    stLocalTime.wHour, stLocalTime.wMinute, stLocalTime.wSecond);
    
    //创建 Dump 文件
    HANDLE hDumpFile = CreateFile(szFileName, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
    if (hDumpFile != INVALID_HANDLE_VALUE)
    {
        //Dump信息
        MINIDUMP_EXCEPTION_INFORMATION dumpInfo;
        dumpInfo.ExceptionPointers = pException;
        dumpInfo.ThreadId = GetCurrentThreadId();
        dumpInfo.ClientPointers = TRUE;

        //写入Dump文件内容
        MiniDumpWriteDump(GetCurrentProcess(), GetCurrentProcessId(), hDumpFile, MiniDumpNormal, &dumpInfo, NULL, NULL);
    }

    //错误输出
    EXCEPTION_RECORD* record = pException->ExceptionRecord;
    std::string errCode ="ErrCode:"+ convertToBase16Str((const unsigned char*)&record->ExceptionCode,sizeof(record->ExceptionCode));
    std::string errAddr ="ErrAddr:"+ convertToBase16Str((const unsigned char*)&record->ExceptionAddress,sizeof(record->ExceptionAddress));

    std::cout<<("!!!---------------App Crash ----------------!!! \n");
    std::cout<<"errCode : "<<errCode<<std::endl;
    std::cout<<"errAddr : "<<errAddr<<std::endl;

    fflush(stdout);

    return EXCEPTION_EXECUTE_HANDLER;
}

//注册异常处理函数
static int crashInit = []()->int{
    std::cout<<"----------- Register crash hander -----------"<<std::endl;
    SetUnhandledExceptionFilter((LPTOP_LEVEL_EXCEPTION_FILTER)ApplicationCrashHandler);
    return 0;
}();

#endif // CRASH_H

  • 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
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100

pro

QMAKE_CXXFLAGS_RELEASE = $$QMAKE_CFLAGS_RELEASE_WITH_DEBUGINFO
QMAKE_LFLAGS_RELEASE = $$QMAKE_LFLAGS_RELEASE_WITH_DEBUGINFO
LIBS +=  -lDbgHelp -luser32
  • 1
  • 2
  • 3

main.cpp

#include <QApplication>
#include <QDebug>
#include<iostream>
#include "CrashHander.h"


int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
           std::vector<int> v(5);
           std::cout << v.at(10); // vector::at() 抛出 std::out_of_range

    return a.exec();
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/245729
推荐阅读
相关标签
  

闽ICP备14008679号