minidump接口类,用于win平台下抓取dump文件

#pragma once
#include "pub_utility_api/UtilityCommon.h"
#include <string>

const std::string CN_MiniDumpPath = "../../minidump";   //< dump文件存储路径

namespace kbd_public
{
    /*
     @brief 初始化minidump生成类,只能捕获此方法调用之后的异常
     @return 成功返回true,失败返回false
    */
    bool PUB_UTILITY_API installBreakPad();
    /*
     @brief 销毁相关资源,在此方法调用后,就不再捕获异常了
     @return 成功返回true,失败返回false
    */
    bool PUB_UTILITY_API uninstallBreakPad();

}




#include "pub_utility_api/BreakPadInterface.h"
#include "boost/locale.hpp"
#include "boost/filesystem.hpp"
#include "common/Common.h"
#include "pub_utility_api/FileUtil.h"

#ifdef _WIN32
#pragma warning(push)
#pragma warning(disable: 4091)
#include "client/windows/handler/exception_handler.h"
#pragma warning(pop)

// Minidump with stacks, PEB, TEB, and unloaded module list.
const MINIDUMP_TYPE kSmallDumpType = static_cast<MINIDUMP_TYPE>(
    MiniDumpWithProcessThreadData |  // Get PEB and TEB.
    MiniDumpWithUnloadedModules);  // Get unloaded modules when available.

// Minidump with all of the above, plus memory referenced from stack.
const MINIDUMP_TYPE kLargerDumpType = static_cast<MINIDUMP_TYPE>(
    MiniDumpWithProcessThreadData |  // Get PEB and TEB.
    MiniDumpWithUnloadedModules |  // Get unloaded modules when available.
    MiniDumpWithIndirectlyReferencedMemory);  // Get memory referenced by stack.

// Large dump with all process memory.
const MINIDUMP_TYPE kFullDumpType = static_cast<MINIDUMP_TYPE>(
    MiniDumpWithFullMemory |  // Full memory from process.
    MiniDumpWithProcessThreadData |  // Get PEB and TEB.
    MiniDumpWithHandleData |  // Get all handle information.
    MiniDumpWithUnloadedModules);  // Get unloaded modules when available.

static google_breakpad::ExceptionHandler *sg_pCrash = NULL;
static std::string sg_strDumpPath = "";

bool FilterCallback(void* /*context*/,
                    EXCEPTION_POINTERS* /*exinfo*/,
                    MDRawAssertionInfo* /*assertion*/)
{
    if (!kbd_public::CFileUtil::createMultiDir(sg_strDumpPath))
    {
        return false;
    }
    
    return true;
}


bool kbd_public::installBreakPad()
{
    enum { BUF_LEN = 1024 };
    char szBuf[BUF_LEN];

    memset(szBuf, 0, sizeof(szBuf));
    GetModuleFileNameA(NULL, szBuf, BUF_LEN);

    boost::filesystem::path objPath(szBuf);
    std::string strFileName = objPath.stem().string();
    objPath.remove_filename();

    objPath /= CN_MiniDumpPath;
    objPath /= strFileName;

    sg_strDumpPath = objPath.string();
    std::string strCodePage = boost::locale::util::get_system_locale();
    std::locale loc = boost::locale::generator().generate(strCodePage);
    std::wstring wstrPath = boost::locale::conv::to_utf<wchar_t>(sg_strDumpPath, loc);

    sg_pCrash = new google_breakpad::ExceptionHandler(wstrPath,
                                                      FilterCallback,
                                                      NULL,
                                                      NULL,
                                                      google_breakpad::ExceptionHandler::HANDLER_ALL,
                                                      kFullDumpType,
                                                      (HANDLE)NULL,
                                                      NULL);
    bool bRet = (sg_pCrash != NULL);
    return bRet;
}


bool kbd_public::uninstallBreakPad()
{
    if (sg_pCrash != NULL)
    {
        delete sg_pCrash;
        sg_pCrash = NULL;
    }
    
    return true;
}

#else
bool kbd_public::installBreakPad()
{
    return true;
}

bool kbd_public::uninstallBreakPad()
{
    return true;
}
#endif





不为其他,只为快乐!
原文地址:https://www.cnblogs.com/1521299249study/p/11075482.html