google breakpad编译及使用示例(windows)

google breakpad编译及使用示例

2018-12-07 10:15:14 qiuxin315 阅读数 633更多

分类专栏: C++

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

本文链接:https://blog.csdn.net/qiuxin315/article/details/84870340

 

文章目录

 

概述

breakpad是google开源的一套用于进程crash的处理方案,跨平台。应该是早期版本crash report的升级版本,以前的crashrpt仅支持win平台。

不过好像由于现在有沙盒技术,会导致breakpad无法生成dump,所以google又开发了一个crashpad项目,用来替代breakpad。

翻墙访问

从http://www.goubanjia.com/找了个代理访问的。使用git下载,也需要设置代理

git config --global http.proxy 203.189.147.33:46631

源码下载

breakpad

使用depot_tools下载

google好多源码都是使用depot_tools下载,其可以将第三方依赖文件也下载下来。不过访问googlesource需要翻墙,如果不能翻墙,本工具就不用下载了。

下载地址:
git:
https://chromium.googlesource.com/chromium/tools/depot_tools.git

git clone https://chromium.googlesource.com/chromium/tools/depot_tools

压缩包:
https://storage.googleapis.com/chrome-infra/depot_tools.zip

下载解压后,获取breakpad源码:

fetch breakpad

git下载

同样的需要翻墙,如果不能访问可以使用github上的分支版本。

git下载地址:
https://chromium.googlesource.com/breakpad/breakpad

$ git clone https://chromium.googlesource.com/breakpad/breakpad

github下载

github下载地址:
https://github.com/google/breakpad

gyp(Generate Your Projects)

gyp是chromium开发的跨平台自动化项目构建工具,gyp工具下载地址:
https://chromium.googlesource.com/external/gyp/

$ git clone https://chromium.googlesource.com/external/gyp

不能翻墙就随便找个下载吧,附一个github上的地址:

https://github.com/bnoordhuis/gyp

python 2.7.x

使用gyp时需要python的支持,好像只能支持python2.7.x,不支持python 3.x。

自行官网下载python。

google test

下载地址:
https://github.com/abseil/googletest

编译

源码目录准备

breakpad\src下新建testing目录,将gtest解压后的googlemock和googletest拷贝到新建的testing下。

生成sln文件

使用gyp生成sln文件。

tools\gyp\gyp.bat --no-circular-check client\windows\breakpad_client.gyp

成功后在breakpad\src\client\windows\目录下有生成的breakpad_client.sln工程文件。

编译

vs打开sln文件,breakpad使用了Unicode编码,如果更改字符集会编译失败。

示例

win下如果使用进程内dump,需要链接下面几个库

common.lib
crash_generation_client.lib
exception_handler.lib

code1:

#include "client/windows/handler/exception_handler.h"

int main()
{
    google_breakpad::ExceptionHandler *pCrashHandel = 
        new google_breakpad::ExceptionHandler(
        L".", 
        NULL,
        NULL,
        NULL,
        google_breakpad::ExceptionHandler::HANDLER_ALL,
        NULL);
    
    char *pBuf = NULL;
    *pBuf = 'a';
    return 0;
}

code2:

#include "client/windows/handler/exception_handler.h"

// 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:Exception *sg_pCrash = NULL;
static std::string sg_strDumpPath = "";

//此方法可以不实现,本示例只是为了实现:出现异常时才产生dump文件夹,没有异常时不创建文件夹
bool FilterCallback(void *context,
                    EXCEPTION_POINTERS *exinfo,
                    MDRawAssertionInfo *assertion)
{
    if(CFileUtil::createMultiDir(sg_strDumpPath))
    {
        return false;
    }
    
    return true;
}


bool 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 /= "../../minidump";
    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);
        
    return (sg_pCrash != NULL);
}

bool uninstallBreakpad()
{
    if(sg_pCrash != NULL)
    {
        delete sg_pCrash;
        sg_pCrash = NULL;
    }
    
    return true;
}
    
int main()
{
    installBreakpad();
    
    char *pBuf = NULL;
    *pBuf = 'a';
    
    uninstallBreakpad();
    return 0;
}

 

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值