Windows下用breakpad抓取C++程序崩溃报告

  breakpad是一组用于实现崩溃报告系统的客户端和服务器组件,然而我在google官方及网上仅能找到客户端的使用方法,google官方似乎并没有提供服务端的东西,仅提供了用于上传崩溃文件的方法(详情见源码中的src\tools\windows\symupload文件夹)。
  breakpad源码见 https://chromium.googlesource.com/breakpad/breakpad 。需翻墙访问,用git下载源码也需翻墙,如何翻墙就自行百度。
  本文环境为Windows 10和Visual Studio 2015,Win7和VS2013环境也适用,本人在公司使用时就是Win7和VS2013环境。(注:下文git命令中$号指命令提示符,不用输入git bash命令行上。)

1 打包breakpad产生工程文件

1.1 获取breakpad源码

breakpad源码可在 https://chromium.googlesource.com/breakpad/breakpad 下载,建议用git下载,windows下的git安装可直接在git官网下载安装即可,用以下命令获取breakpad源码:

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

用于墙的原因,可能会clone失败,就自行百度翻墙吧,实在不行可在以下链接下载breakpad源码:
http://download.csdn.net/detail/bingzhongdehuoyan/9716434
breakpad源码文件夹即为breakpad。

1.2 获取gyp工具

GYP(Generate Your Projects)是由 Chromium 团队开发的跨平台自动化项目构建工具,Chromium 便是通过 GYP 进行项目构建管理。
gyp工具在https://chromium.googlesource.com/external/gyp/可获得,建议用git获取:

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

同样需要翻墙,无法翻墙的可在下面的链接下载:
http://download.csdn.net/detail/bingzhongdehuoyan/9720517
将获取到的gyp文件夹复制到breakpad\src\tools\文件夹下。

1.3 安装python2.7

用gyp打包breakpad时需要python支持,python可在官网直接下载安装并添加到path,具体就自行百度,不过不能安装python3.x,否则会出现如下错误:

只能安装python2.7.x,本人安装的是最新的2.7.13。

1.4 获取googletest

还必须获取googletest,否则会出现如下警告:

googletest可在GitHub上获取(https://github.com/google/googletest),如下:

$ git clone https://github.com/google/googletest.git

将获取到的googletest文件夹下将会有两个文件夹,googlemock和googletest,如下图:

将其中的googlemock文件夹复制到breakpad\src\目录下,并重命名为testing;然后将获取到的googletest文件夹下的googletest文件夹复制到breakpad\src\testing\文件夹下,并重命名为gtest。

1.5 用gyp构建出工程文件

在breakpad\src文件夹下打开命令行窗口,有两种方法:一是win+R打开运行,输入cmd打开命令行,然后用cd指令进入breakpad\src目录;二是在breakpad\src文件夹下shift+鼠标右键,点击在此处打开命令窗口。然后在命令行窗口输入如下命令:

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


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

2 使用breakpad自带示例程序

breakpad中有自带的示例程序,可用来感受breakpad如何抓取的崩溃报告,下面就先简单使用一下这个示例程序。
用Visual Studio打开breakpad\src\client\windows\目录下的breakpad_client.sln工程,打开后的解决方案资源管理器如图:

右键build_all项目,点击生成,VS2015会出现如下错误:

都是什么警告被视为错误,解决方法是:右键项目名(如crash_generation_client),打开属性,展开C/C++,点击常规,把右侧的“将警告视为错误”选项选为否,如图:

依次操作除build_all项目外各属性有C/C++标签的项目,然后再右键build_all项目,点击重新生成,还是会失败,Win10和VS2015事真多,我用Win7和VS2013时没这么多事,错误信息如下:

双击该错误后定位到错误行:

粗暴的将错误行删掉吧:

又一次右键build_all项目,点击重新生成,终于成功了,不容易啊:

成功后在breakpad\src\client\windows\Debug\文件夹下会有crash_generation_app.exe程序,双击打开,点击Client->Deref Zero,程序就会崩溃,崩溃后到C盘根目录下看是否有Dumps文件夹,其中有名字类似于a9414977-693d-4013-89c1-9c7c4ef81689.dmp的崩溃转储文件,用VS打开该文件:

可以看到错误信息,点击使用 仅限本机 进行调试,可以看到错误详情及错误具体发生在哪行代码:

仔细研究该程序代码,即可大概明白要如何使用breakpad抓取C++程序崩溃报告。

3 写个小程序试试水

下面就写个简单的小程序试试用breakpad抓取崩溃报告。
用VS 2015新建一个Visual C++控制台程序,本文程序起名为wincrash。
然后在VS的wincrash解决方案中添加common、crash_generation_client、crash_generation_server、exception_handler等四个项目,右键解决方案->添加->现有项目,选中breakpad\src\client\windows\目录中和crash_generation、handler子目录中扩展名为.vcxproj的文件即可:


然后在wincrash项目中添加包含目录breakpad\src目录的全路径,本人的路径是D:\breakpad\src,添加方法是:右键wincrash->属性->VC++目录->包含目录->编辑:


之后分别右键common、crash_generation_client、crash_generation_server、exception_handler等四个项目点击生成,以编译出lib库文件:

在wincrash项目中包含生成的四个.lib库文件:右键wincrash->属性->链接器->输入->附加依赖项->编辑:

在附加依赖项编辑框中输入:

..\Debug\lib\common.lib
..\Debug\lib\crash_generation_client.lib
..\Debug\lib\crash_generation_server.lib
..\Debug\lib\exception_handler.lib


wincrash主程序代码如下:

#include <cstdio>
#include "client/windows/handler/exception_handler.h"

bool callback(const wchar_t *dump_path, const wchar_t *id,
    void *context, EXCEPTION_POINTERS *exinfo,
    MDRawAssertionInfo *assertion,
    bool succeeded)
{
    if (succeeded) {
        printf("dump guid is %ws\n", id);
    }
    else {
        printf("dump failed\n");
    }
    system("pause");
    return succeeded;
}

int mydiv(int x, int y)
{
    int z;
    z = x / y;
    return z;
}

int main()
{
    google_breakpad::ExceptionHandler eh(
        L".", NULL, callback, NULL,
        google_breakpad::ExceptionHandler::HANDLER_ALL);
    printf("9/3=%d\n", mydiv(9, 3));
    printf("9/0=%d\n", mydiv(9, 0));  //程序将在此崩溃
    printf("8/2=%d\n", mydiv(8, 2));
    system("pause");
    return 0;
}

编译程序,可能会有如下错误:

原因是common、crash_generation_client、crash_generation_server、exception_handler等四个项目的代码生成运行库为“多线程调试 (/MTd)”,跟wincrash的不一样,将wincrash的改为“多线程调试 (/MTd)”即可:右键wincrash->属性->C/C++->代码生成->运行库->多线程调试 (/MTd),如图:

再次编译程序,这次应该能在Debug目录下成功生成wincrash.exe程序,双击打开运行:

成功抓取崩溃文件!!
在wincrash.exe程序所在目录下可看到生成的.dmp扩展名的崩溃转储文件(名称类似1a4ecf76-1df3-46de-be69-88df37bb1b11.dmp),用VS打开该文件:

可以看到错误信息,点击使用 仅限本机 进行调试,可以看到错误详情及错误具体发生在哪行代码:

准确定位26行和36行!!
下面简单解析一下这个小程序,使用breakpad抓取C++程序的崩溃报告,需要包含exception_handler.h头文件,并在程序所有错误之前创建一个google_breakpad::ExceptionHandler对象,建议在main()函数开头创建该对象,当程序出错时,该对象会捕捉到错误,输出dump文件,还可通过回调函数作一些必要的处理操作。
本程序所用google_breakpad::ExceptionHandler的其中一个构造函数定义如下(见exception_handler.h文件):

  // Creates a new ExceptionHandler instance to handle writing minidumps.
  // Before writing a minidump, the optional filter callback will be called.
  // Its return value determines whether or not Breakpad should write a
  // minidump.  Minidump files will be written to dump_path, and the optional
  // callback is called after writing the dump file, as described above.
  // handler_types specifies the types of handlers that should be installed.
  ExceptionHandler(const wstring& dump_path,
                   FilterCallback filter,
                   MinidumpCallback callback,
                   void* callback_context,
                   int handler_types);
  • const wstring& dump_path:宽字符串类型的dump_path用于定义.dmp扩展名的崩溃转储文件生成路径。
  • FilterCallback filter:程序崩溃时用于过滤的回调函数,通过返回ture/false来继续/停止 异常处理,其定义如下:
  // A callback function to run before Breakpad performs any substantial
  // processing of an exception.  A FilterCallback is called before writing
  // a minidump.  context is the parameter supplied by the user as
  // callback_context when the handler was created.  exinfo points to the
  // exception record, if any; assertion points to assertion information,
  // if any.
  //
  // If a FilterCallback returns true, Breakpad will continue processing,
  // attempting to write a minidump.  If a FilterCallback returns false,
  // Breakpad will immediately report the exception as unhandled without
  // writing a minidump, allowing another handler the opportunity to handle it.
  typedef bool (*FilterCallback)(void* context, EXCEPTION_POINTERS* exinfo,
                                 MDRawAssertionInfo* assertion);
  • MinidumpCallback callback:输出dump文件后的回调函数,可做一些必要的处理操作,不过不建议太复杂,毕竟程序崩溃后堆栈已被破坏,其定义如下:
  // A callback function to run after the minidump has been written.
  // minidump_id is a unique id for the dump, so the minidump
  // file is <dump_path>\<minidump_id>.dmp.  context is the parameter supplied
  // by the user as callback_context when the handler was created.  exinfo
  // points to the exception record, or NULL if no exception occurred.
  // succeeded indicates whether a minidump file was successfully written.
  // assertion points to information about an assertion if the handler was
  // invoked by an assertion.
  //
  // If an exception occurred and the callback returns true, Breakpad will treat
  // the exception as fully-handled, suppressing any other handlers from being
  // notified of the exception.  If the callback returns false, Breakpad will
  // treat the exception as unhandled, and allow another handler to handle it.
  // If there are no other handlers, Breakpad will report the exception to the
  // system as unhandled, allowing a debugger or native crash dialog the
  // opportunity to handle the exception.  Most callback implementations
  // should normally return the value of |succeeded|, or when they wish to
  // not report an exception of handled, false.  Callbacks will rarely want to
  // return true directly (unless |succeeded| is true).
  //
  // For out-of-process dump generation, dump path and minidump ID will always
  // be NULL. In case of out-of-process dump generation, the dump path and
  // minidump id are controlled by the server process and are not communicated
  // back to the crashing process.
  typedef bool (*MinidumpCallback)(const wchar_t* dump_path,
                                   const wchar_t* minidump_id,
                                   void* context,
                                   EXCEPTION_POINTERS* exinfo,
                                   MDRawAssertionInfo* assertion,
                                   bool succeeded);
  • void* callback_context:设备上下文,回调使用的。
  • int handler_types:HandlerType异常类型,可在exception_handler.h查看。

google_breakpad::ExceptionHandler的详细使用方法就自己参考官方文档或源码中的注释。

  • 6
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
Breakpad 是一个开源的崩溃报告库,它可以捕获应用程序崩溃信息,生成崩溃报告,以便于开发人员进行分析和修复。 要捕获 C++ 标准库的崩溃,需要使用 Breakpad 提供的异常处理机制。在 C++ 中,异常是通过 `try-catch` 语句块来处理的,当程序遇到异常时,会跳转到相应的 `catch` 块中进行处理。Breakpad 利用这个机制,在 `catch` 块中捕获异常并生成崩溃报告。 具体的实现步骤如下: 1. 在应用程序中使用 Breakpad 库,将崩溃信息保存到指定的文件中。 2. 在程序中使用 `try-catch` 语句块捕获异常。 3. 在 `catch` 块中,调用 Breakpad 提供的函数,将崩溃信息写入文件。 以下是一个使用 Breakpad 捕获 C++ 标准库崩溃的示例代码: ```c++ #include <stdexcept> #include "client/linux/handler/exception_handler.h" bool DumpCallback(const google_breakpad::MinidumpDescriptor& descriptor, void* context, bool succeeded) { // 将崩溃信息写入文件 return succeeded; } int main() { // 创建 Breakpad 异常处理器 google_breakpad::MinidumpDescriptor descriptor("."); google_breakpad::ExceptionHandler eh(descriptor, NULL, DumpCallback, NULL, true, -1); try { // 在这里写下可能会抛出异常的代码 throw std::runtime_error("C++ 标准库崩溃"); } catch (...) { // 在 catch 块中调用 Breakpad 的 WriteMinidump 函数 eh.WriteMinidump(); } return 0; } ``` 在上面的代码中,`DumpCallback` 函数用于将崩溃信息写入文件,`main` 函数中创建了一个 Breakpad 异常处理器,并在 `try-catch` 语句块中捕获异常。当程序遇到异常时,会跳转到 `catch` 块中,调用 Breakpad 的 `WriteMinidump` 函数将崩溃信息写入文件。 需要注意的是,使用 Breakpad 捕获崩溃信息时,需要在编译时链接 Breakpad 库,并将 Breakpad 的头文件包含在程序中。同时,需要在程序中注册异常处理器,以便于在发生异常时能够捕获崩溃信息。
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值