| 更多精彩内容 |
|---|
| 👉个人内容分类汇总 👈 |
| 👉C++软件调试、异常定位 👈 |
Crashpad 是一个由 Google 开发的开源库,是 Google Breakpad 库的继任者。
用于在应用程序崩溃时捕获和处理崩溃信息。它是 Chromium 项目的一部分,因此广泛应用于各种浏览器和应用程序中,如 Google Chrome 和 Electron。以下是 Crashpad 的几个关键功能和组成部分的详细说明:
- 崩溃报告: Crashpad 的主要功能是捕获应用程序崩溃时的状态,包括崩溃的上下文信息、调用堆栈、寄存器状态等。这些信息被封装成崩溃报告发送到预设的服务器上。
- 后端服务: Crashpad 配置了一个服务器端组件,通常称为 crash server,它负责接收、存储和分析崩溃报告。这个服务可以帮助开发者收集和分析崩溃数据,从而识别和解决软件中的问题。
- 客户端库: Crashpad 包括一个客户端库,该库被集成到应用程序中,用于监控程序运行状态。一旦检测到崩溃,它会触发崩溃报告的生成和上传。
- 最小化性能影响: Crashpad 的设计旨在尽量减少对应用程序性能的影响。它的捕获和报告机制非常高效,确保即使在应用程序发生崩溃时,也能快速有效地处理崩溃数据。
- 跨平台支持: Crashpad 支持多个平台,包括 Windows、macOS 和 Linux,这使得它可以在不同操作系统的应用程序中使用。
- 易于集成和使用: Crashpad 设计了易于使用的 API 和工具,开发者可以通过简单的配置和少量代码将 Crashpad 集成到自己的应用程序中。
演示环境:
注意:源码编译依赖很多(国内可能不好访问),所以可以直接下载编译好的。
注意:使用Debug编译就链接crashpad-debug-x86-64-latest,使用Release编译就链接crashpad-release-x86-64-latest;区分32位和64位。
头文件添加

添加库目录,默认使用/md的动态链接就可以

添加库

设置编译参数
动态链接
/MD/MDd静态链接
/MT/MTd
在配置完环境就可以直接编译了,但是可能会出现报错;
E1097 未知特性 "no_init_all"
C2589 “(”:“::”右边的非法标记

在使用到crashpad头文件之前添加#define NOMINMAX就可以了;

#include
#include
#include
#include
#include
#define NOMINMAX
#include
#include
#include
#include
std::unique_ptr<crashpad::CrashReportDatabase> database;
static bool startCrashHandler(std::string const& url, std::wstring const& handler_path, std::wstring const& db_path)
{
using namespace crashpad;
std::map<std::string, std::string> annotations;
std::vector<std::string> arguments;
annotations["format"] = "minidump"; // 设置生成minidump
arguments.push_back("--no-rate-limit"); // 禁用了崩溃速率限制
base::FilePath db(db_path);
base::FilePath handler(handler_path);
database = crashpad::CrashReportDatabase::Initialize(db); // 打开一个崩溃报告数据库
if (database == nullptr || database->GetSettings() == NULL)
{
return false;
}
database->GetSettings()->SetUploadsEnabled(true); // 启用自动上传。
// 启动一个crash处理程序进程
return CrashpadClient().StartHandler(handler, db, db, url, annotations, arguments, false, false, {});
}
int main(int argc, char** argv)
{
std::string url("http://127.0.0.1:8000"); // 存储服务器的url,这里设置位本地地址
std::wstring handler_path(L"./crashpad_handler.exe"); // 指向crashpad_handler.exe的路径
std::wstring db_path(L"./crash"); // 存储dump的路径
startCrashHandler(url, handler_path, db_path);
int* p = nullptr;
*p = 123;
return 0;
}

pro文件配置
INCLUDEPATH += E:/test/untitled4/crashpad/include/
INCLUDEPATH += E:/test/untitled4/crashpad/include/mini_chromium/
INCLUDEPATH += E:/test/untitled4/crashpad/include/util/
LIBS += -L$$PWD/crashpad/lib_md/ -lbase -lclient -lutil -lcommon -lAdvapi32
Advapi32.lib,动态库为 Advapi32.dll;