Breakpad跨平台c++ crash捕获和生成工具使用

简介

breakpad是一组用于实现崩溃报告系统的客户端和服务器组件。Chromium的Breakpad是目前Native崩溃捕获中最成熟的方案。它是一套完整的工具集,从Crash的捕获到Crash的dump,都提供了相对应的工具。它记录了崩溃时的.dump文件,无论我们是在本地或者发送到服务器端,都可以用相对应的工具来解析.dump文件帮助我们查找C和C++堆栈踪迹。

工作原理: 

项目地址

breakpad:GitHub - google/breakpad: Mirror of Google Breakpad project

编译安装

linux平台下

linux的编译安装稍简单些。

LSS:linux-syscall-support: clickhouse submodule sentry-native submodulehttps://chromium.googlesource.com/linux-syscall-support

1.下载breakpad和LSS源码
2.将LSS中的linux_syscall_support.h移动到breakpad/src/third_party/lss/目录下(没有就自己新建一个)
3.编译,步骤如下 

cd breakpad
./configure
make
sudo make install
# sudo checkinstall

Windows下的安装

稍麻烦些,推荐使用vcpkg安装。

先cd到vcpkg的安装目录,然后执行下条指令,:x64-windows表示安装win64版本。

./vcpkg install breakpad:x64-windows

因为众所周知的原因,下载可能会很慢。不过有大佬给了国内镜像。

详情查看链接链接:https://blog.csdn.net/jackboos/article/details/105026109

使用breakpad

在QT中的测试:

在vcpkg/packages/breakpad_x64-windows中可以找到breakpad的头文件可库。

将breakpad_x64-windows拷贝到项目的同级目录中。

在qt的项目文件.pro中增加以下配置:

win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/breakpad_x64-windows/lib/ -llibbreakpad -llibbreakpad_client
win32:CONFIG(release, debug|release): LIBS += -L$$PWD/breakpad_x64-windows/lib/ -llibbreakpad -llibbreakpad_client
else:unix: LIBS += -L$$PWD/breakpad_x64-windows/lib/ -llibbreakpa

INCLUDEPATH += $$PWD/breakpad_x64-windows/include
DEPENDPATH += $$PWD/breakpad_x64-windows/include

接下来一个简单的测试:

#include <QCoreApplication>
#include<QDebug>
#include <QDir>
#include "breakpad_x64-windows/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) {
        qDebug() << "Create dump file success";
    } else {
        qDebug() << "Create dump file failed";
    }
    return succeeded;
}

// 触发crash来测试
void crash() {
    volatile int* a = (int*)(NULL);
    *a = 1;
}
int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    qDebug() << "hello test";
    //获取程序当前运行目录
    QString appDirPath = QCoreApplication::applicationDirPath() + "/crash";

    QDir dir;
    if (!dir.exists(appDirPath)) {
        bool res = dir.mkpath(appDirPath);
        qDebug() << "New mkdir " << appDirPath << " " << res;
    }

    google_breakpad::ExceptionHandler eh(
        L".", NULL, callback, NULL,
        google_breakpad::ExceptionHandler::HANDLER_ALL);


    crash();
    return a.exec();
}

运行起来看到已经生成成功啦,生成了文件3af12e91-8dca-4587-b5f3-d13d5cb3d637.dmp

cmake中使用

....
//vcpkg install breakpad:x64-windows

find_library(LibConfig libconfig++)
message(STATUS ${LibConfig})
find_package(unofficial-breakpad CONFIG REQUIRED)

set(LOGGING_LIB ${LIB_DIR}/lib/Logging${LIB_FIX}.lib)
set(THIRD_LIBS
        ${LOGGING_LIB}
        unofficial::breakpad::libbreakpad
        unofficial::breakpad::libbreakpad_client
        )
cmake_minimum_required(VERSION 3.20)

IF (WIN32)
    MESSAGE(STATUS "Now is windows")
    set(VCPKG_ROOT F:/git/vcpkg)
ELSEIF (APPLE)
    MESSAGE(STATUS "Now is Apple system.")
    set(VCPKG_ROOT /Users/hualongzhang/work/vcpkg)
ENDIF ()
set(VCPKG_CMAKE ${VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake)
set(CMAKE_TOOLCHAIN_FILE ${VCPKG_CMAKE})

project(testbreakpad)

set(CMAKE_CXX_STANDARD 17)

find_package(unofficial-breakpad CONFIG REQUIRED)
link_libraries(unofficial::breakpad::libbreakpad unofficial::breakpad::libbreakpad_client)

add_executable(untitled1 main.cpp)
bool dumpCallback(const wchar_t *dump_path, const wchar_t *id, void *context, EXCEPTION_POINTERS *exp_info,
                         MDRawAssertionInfo *assertion, bool succeeded){
    if (succeeded){
        printf("dump guid is %ws\n", id);
    }else{
        printf("dump failed\n");
    }
    return succeeded;
}
......
google_breakpad::ExceptionHandler handler(helper::help_string::StringToWString(crashPath), nullptr, dumpCallback,
                                              nullptr, google_breakpad::ExceptionHandler::HANDLER_ALL);


inline std::wstring StringToWString(const std::string& str)
{
  using convert_typeX = std::codecvt_utf8<wchar_t>;
  std::wstring_convert<convert_typeX, wchar_t> converterX;

  return converterX.from_bytes(str);
}

inline std::string WStringToString(const std::wstring& w_str)
{
  using convert_typeX = std::codecvt_utf8<wchar_t>;
  std::wstring_convert<convert_typeX, wchar_t> converterX;

  return converterX.to_bytes(w_str);
}

解析dmp文件

解析文件可以使用minidump_stackwalk,解析的结果存放到test.txt文件中。

./minidump_stackwalk.exe test.dmp >test.txt

minidump_stackwalk.exe工具下载,放在我的资源里了,可以直接下载使用。

引用:

Breakpad使用(window)_narkang的博客-CSDN博客_breakpad

通过vcpkg编译breakpad并在qt项目中应用,VS编译器_沐大人的博客-CSDN博客

https://blog.csdn.net/qq_17766199/article/details/85716750

https://blog.csdn.net/lm111111/article/details/105623432

通过vcpkg编译breakpad并在qt项目中应用,VS编译器_沐大人的博客-CSDN博客

Breakpad(跨平台crash工具)_奇小葩的博客-CSDN博客_breakpad

Google Breakpad:脱离符号的调试工具-电子头条-EEWORLD电子工程世界

Ubuntu搭建breakpad环境及查看dmp文件_Geroff的博客-CSDN博客_breakpad linux

breakpad尝试 - Leehm - 博客园

breakpad的正确编译和常规用法 - 简书

使用breakpad定位崩溃(windows&mac) - 简书

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

特立独行的猫a

您的鼓励是我的创作动力

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

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

打赏作者

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

抵扣说明:

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

余额充值