简介
breakpad是一组用于实现崩溃报告系统的客户端和服务器组件。Chromium的Breakpad是目前Native崩溃捕获中最成熟的方案。它是一套完整的工具集,从Crash的捕获到Crash的dump,都提供了相对应的工具。它记录了崩溃时的.dump文件,无论我们是在本地或者发送到服务器端,都可以用相对应的工具来解析.dump文件帮助我们查找C和C++堆栈踪迹。
工作原理:
项目地址
breakpad:GitHub - google/breakpad: Mirror of Google Breakpad project
编译安装
linux平台下
linux的编译安装稍简单些。
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.youkuaiyun.com/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的博客-优快云博客_breakpad
通过vcpkg编译breakpad并在qt项目中应用,VS编译器_沐大人的博客-优快云博客
https://blog.youkuaiyun.com/qq_17766199/article/details/85716750
https://blog.youkuaiyun.com/lm111111/article/details/105623432
通过vcpkg编译breakpad并在qt项目中应用,VS编译器_沐大人的博客-优快云博客
Breakpad(跨平台crash工具)_奇小葩的博客-优快云博客_breakpad
Google Breakpad:脱离符号的调试工具-电子头条-EEWORLD电子工程世界