MiniDump

一、minidump 模块集成

// .pro
QT -= gui

CONFIG += c++11 console
CONFIG -= app_bundle

# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS

# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0

SOURCES += \
        main.cpp


QMAKE_LFLAGS_RELEASE += /MAP
QMAKE_CFLAGS_RELEASE += /Zi
QMAKE_LFLAGS_RELEASE += /debug /opt:ref

QMAKE_CXXFLAGS_RELEASE += $$QMAKE_CFLAGS_RELEASE_WITH_DEBUGINFO
QMAKE_LFLAGS_RELEASE += $$QMAKE_LFLAGS_RELEASE_WITH_DEBUGINFO

LIBS += -lDbgHelp



# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

// main.cpp
#include 
#include 
#include 
#include 
#ifdef Q_OS_WIN
#   include 
#   include 
#   pragma comment( lib, "Dbghelp.lib" )
#endif

#define DumpFileDir qApp->applicationDirPath() + "/dump/"


static LONG WINAPI AppExceptionCallback(struct _EXCEPTION_POINTERS *ExceptionInfo)
{
    QString savePath = DumpFileDir;
    savePath.append(QDateTime::currentDateTime().toString("yyyyMMddhhmmsszzz"));
    savePath.append(".dmp");
    qDebug() << savePath;

    QString msg;
    HANDLE hDumpFile = CreateFileW(savePath.toStdWString().c_str(),
                                   GENERIC_READ | GENERIC_WRITE, FILE_SHARE_WRITE | FILE_SHARE_READ,
                                   0, CREATE_ALWAYS, 0, 0);
    if(INVALID_HANDLE_VALUE == hDumpFile)
    {
        return EXCEPTION_EXECUTE_HANDLER;
    }


    MINIDUMP_EXCEPTION_INFORMATION ExpParam;
    ExpParam.ThreadId = GetCurrentThreadId();
    ExpParam.ExceptionPointers = ExceptionInfo;
    ExpParam.ClientPointers = TRUE;

    // 创建Dump文件
    MiniDumpWriteDump(GetCurrentProcess(), GetCurrentProcessId(), hDumpFile, MiniDumpWithDataSegs, ExceptionInfo ? &ExpParam : nullptr, nullptr, nullptr);
    CloseHandle(hDumpFile);


    // 提示
    QString strTitle ("Application Error");
    QString strContent ("I'm Sorry,Application is Crash!");
    qDebug() << strContent << "," << QStringLiteral("dump文件路径为 ") << savePath;

    return EXCEPTION_EXECUTE_HANDLER;
}


/// crashed 模块
int testCrashedFunc(int ia,int ib)
{
    int ret = ia/ib;
    qDebug() << ret;
    return ret;
}


int main(int argc, char *argv[])
{
    QCoreApplication a(argc,argv);

#ifdef Q_OS_WIN
    QDir dir(DumpFileDir);
    if(!dir.exists())
        if (!dir.mkpath(DumpFileDir))
            qDebug() << QStringLiteral("创建dump文件目录失败");

    SetUnhandledExceptionFilter(AppExceptionCallback);
#endif

    testCrashedFunc(10,0);
    return 0;
}


二、dump文件分析

调试可参考: https://www.cnblogs.com/kekec/archive/2012/11/14/2755924.html

或者参考 https://learn.microsoft.com/zh-cn/windows-hardware/drivers/debugger/getting-started-with-windbg
MiniDump_第1张图片

你可能感兴趣的:(程序调试技术,Minidump,dump文件生成,dump分析,crash捕获)