windows mingw下如何使用breakpad生成minidump并查看crash信息

1. breakpad简介


Breakpad是一个开源的跨平台crash捕捉工具,由谷歌和crashpad的前身用C++编写。它支持 macOS、Windows 和 Linux,可在进程崩溃时,记录.dump文件,并提供了相对应的工具来解析.dump文件帮助我们查找C和C++堆栈踪迹。
在这里插入图片描述

2. breakpad下载


官方github: https://github.com/google/breakpad

linux下编译可能需要lss头文件,众所周知的原因不一定能下载得到,可以在以下链接获取:
https://download.csdn.net/download/SuperYang_/89777728

3. breakpad编译


这里以windows mingw的方式进行编译说明,其他方式类似;首先需要确保你已经安装了mingw,这里不详细介绍,不会安装的自行百度,编译命令很简单:

./configure
mingw32-make
mingw32-make install

正常这样编译即可,但是实际上你这样编译的时候可能在./configure命令的时候报出如下的错误:
在这里插入图片描述
这其实是因为你./configure的命令具体后面会使用哪个make编译没有指定,或者你压根make都没安装,这里没安装就自行百度吧,太简单了不赘述,已经安装了需要执行下面命令

export MAKE=mingw32-make.exe

然后再执行编译的三条命令即可。

4. 使用libbreakpad.a


上述编译正常的情况下会在src目录生成libbreakpad.a静态库文件,安装目录也会生成,一样的东西
我这里以Qt creator编写c++代码的方式说明以下怎么使用,代码如下:

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

bool dumpCallback(const 
MinGW 环境下使用 Breakpad,可以通过以下步骤实现崩溃转储(minidump)的生成与分析。Breakpad 是一个由 Google 开发的跨平台崩溃捕捉库,支持 Windows、Linux 和 macOS。它可以在程序崩溃时自动生成 `.dmp` 文件,提供工具来解析这些文件以获取堆栈信息。 ### 配置 Breakpad MinGW 下编译 1. **下载 Breakpad 源码** 从官方仓库克隆或下载最新版本的 Breakpad: ```bash git clone https://chromium.googlesource.com/breakpad/breakpad.git ``` 2. **构建 Breakpad 库** 在 Windows使用 MinGW 编译 Breakpad,需要先安装 CMake 工具配置环境变量。然后执行以下命令: ```bash cd breakpad mkdir build && cd build cmake -G "MinGW Makefiles" .. mingw32-make ``` 这将生成 `libbreakpad_client.a` 等静态库文件[^3]。 3. **集成到项目中** 将 `client/windows/handler/exception_handler.h` 头文件包含进项目,链接 `libbreakpad_client.a` 库文件。例如,在项目的 `main.cpp` 中可以添加如下代码: ```cpp #include <iostream> #include <cstdio> #include "client/windows/handler/exception_handler.h" namespace { static bool callback(const wchar_t* dump_path, const wchar_t* id, void* context, EXCEPTION_POINTERS* exinfo, MDRawAssertionInfo* assertion, bool succeeded) { if (succeeded) { std::wcout << L"Dump GUID: " << id << std::endl; } else { std::cerr << "Failed to generate dump." << std::endl; } return succeeded; } static void CrashFunction() { int* i = reinterpret_cast<int*>(0x45); *i = 5; // 引发崩溃 } } // namespace int main() { google_breakpad::ExceptionHandler eh( L".", nullptr, callback, nullptr, google_breakpad::ExceptionHandler::HANDLER_ALL); CrashFunction(); std::cout << "Program did not crash?" << std::endl; return 0; } ``` 4. **编译运行程序** 使用 MinGW 编译器进行编译时,确保链接 Breakpad 的库文件: ```bash g++ -o test_breakpad main.cpp -I../breakpad/src -L../breakpad/build/client -lbreakpad_client ``` 执行后如果发生崩溃,会在当前目录生成 `.dmp` 文件。 5. **使用 `minidump_stackwalk` 分析崩溃日志** Breakpad 提供了 `minidump_stackwalk` 工具用于解析 `.dmp` 文件。首先编译该工具: ```bash cd breakpad/src/processor make minidump_stackwalk ``` 然后使用如下命令分析崩溃文件: ```bash ./minidump_stackwalk your_dump_file.dmp ../symbols ``` ### 注意事项 - 在 Windows使用 MinGW 编译时,需确保路径不包含空格。 - 若使用 MSYS 或 MSYS2 环境,可简化部分依赖管理。 - 调试信息符号表可通过 `dump_syms` 工具提取存储到指定目录,以便更精确地解析堆栈信息。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

SuperYang_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值