介绍

breakpad是Google chromium项目中的一个C/C++程序崩溃日志收集工具,支持跨平台

breakpad安装

arch linux直接通过pacman安装。

从github上克隆代码

git clone https://github.com/google/breakpad.git

进入代码目录进行编译

cd breakpad
mkdir build
cd build
../configure --prefix=/usr/local
make
sudo make install

编译时会报错

../src/client/linux/crash_generation/crash_generation_client.cc:40:10: fatal error: third_party/lss/linux_syscall_support.h: 没有那个文件或目录
 #include "third_party/lss/linux_syscall_support.h"

将头文件

https://raw.githubusercontent.com/adelshokhy112/linux-syscall-support/master/lss/linux_syscall_support.h

下载到 src/third_party/lss下,新建目录lss

返回原来目录重新执行makesudo make install

breakpad不包含cmake库,可以通过cmake调用pkg-config来找到具体安装位置和链接库

测试

CMakeLists.txt

cmake_minimum_required(VERSION 3.5)
project(test_breakpad LANGUAGES CXX)

set(CMAKE_CXX_COMPILER g++)
set(CMAKE_C_COMPILER gcc)
set(CMAKE_CXX_STANDARD 11)

if (CMAKE_BUILD_TYPE MATCHES Debug)
    message("Debug编译")
    set(CMAKE_CXX_FLAGS "-pipe -g -std=gnu++11 -Wall -D_REENTRANT -fPIC")
    set(CMAKE_C_FLAGS "-pipe -g -Wall -D_REENTRANT -fPIC")
else () #CMAKE_BUILD_TYPE MATCHES Release
    message("Release编译")
    set(CMAKE_CXX_FLAGS "-pipe -O2 -std=gnu++11 -Wall -W -D_REENTRANT -fPIC")
    set(CMAKE_C_FLAGS "-pipe -O2 -Wall -W -D_REENTRANT -fPIC")
endif ()

include(FindPkgConfig)
pkg_check_modules(BREAKPAD_CLIENT REQUIRED breakpad-client)
message(STATUS "breakpad_client目录 ${BREAKPAD_CLIENT_INCLUDE_DIRS}")
include_directories(${BREAKPAD_CLIENT_INCLUDE_DIRS})

set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/output)
add_executable(test_breakpad main.cpp func.cpp func.hpp)
target_link_libraries(test_breakpad
        ${BREAKPAD_CLIENT_LIBRARIES}
        pthread)

main.cpp

#include <breakpad/client/linux/handler/exception_handler.h>
#include "func.hpp"

static bool dumpCallback(const google_breakpad::MinidumpDescriptor &descriptor,
                         void *context, bool succeeded)
{
    printf("Dump path: %s\n", descriptor.path());
    return succeeded;
}

int main(int argc, char *argv[])
{
    google_breakpad::MinidumpDescriptor descriptor("./");
    google_breakpad::ExceptionHandler eh(descriptor, nullptr, dumpCallback, nullptr, true, -1);
    crash();
    return 0;
}

func.hpp

#ifndef TEST_BREAKPAD_FUNC_HPP
#define TEST_BREAKPAD_FUNC_HPP

void crash();

#endif //TEST_BREAKPAD_FUNC_HPP

func.cpp

#include "func.hpp"

void crash()
{
    volatile int *a = (int *) (nullptr);
    *a = 1;
}

执行cmake时加上-DCMAKE_BUILD_TYPE=Debug,编译时会加上-g参数

运行编译的test_breakpad程序,会在同目录下生产一个dmp文件

执行如下两条语句:

minidump-2-core f989e168-1c4e-410b-be97d4b2-f02ba661.dmp > core
gdb test_breakpad core

最后可以看到如下输出:

Core was generated by `/home/bekl/code/test_breakpad/output/test_breakpad'.
Program terminated with signal SIGSEGV, Segmentation fault.
#0  0x000000000040238d in crash () at /home/bekl/code/test_breakpad/func.cpp:10
10        *a = 1;

执行cmake时加上-DCMAKE_BUILD_TYPE=Release

可以看到输出:

Core was generated by `/home/bekl/code/test_breakpad/output/test_breakpad'.
Program terminated with signal SIGSEGV, Segmentation fault.
#0  0x0000000000401f50 in crash() ()

另外一种方法可以查看帮助文件:https://github.com/google/breakpad/blob/master/docs/linux_starter_guide.md#producing-symbols-for-your-application

得到的输出结果:

Crash reason:  SIGSEGV /SEGV_MAPERR
Crash address: 0x0
Process uptime: not available

Thread 0 (crashed)
 0  test_breakpad!crash() + 0x0
    rax = 0xffffffffffffffff   rdx = 0x0000000000000000
    rcx = 0x00007f618b132b20   rbx = 0x00007fff4a450cd0
    rsi = 0x0000000000000000   rdi = 0x0000000000619860
    rbp = 0x00007fff4a450e70   rsp = 0x00007fff4a450cc8
     r8 = 0x0000000000000000    r9 = 0x0000000000000000
    r10 = 0x0000000000000131   r11 = 0x00007f618b6db4c0
    r12 = 0x00007fff4a450d60   r13 = 0x00007fff4a450f50
    r14 = 0x00007fff4a450d60   r15 = 0x0000000000000000
    rip = 0x0000000000401f50
    Found by: given as instruction pointer in context
 1  test_breakpad!main + 0x11a
    rbx = 0x00007fff4a450cd0   rbp = 0x00007fff4a450e70
    rsp = 0x00007fff4a450cd0   r12 = 0x00007fff4a450d60
    r13 = 0x00007fff4a450f50   r14 = 0x00007fff4a450d60
    r15 = 0x0000000000000000   rip = 0x0000000000401d9a
    Found by: call frame info
 2  libc-2.23.so + 0x20830
    rbx = 0x0000000000000000   rbp = 0x0000000000412f20
    rsp = 0x00007fff4a450e80   r12 = 0x0000000000401df0
    r13 = 0x00007fff4a450f50   r14 = 0x0000000000000000
    r15 = 0x0000000000000000   rip = 0x00007f618ad8e830
    Found by: call frame info
 3  test_breakpad + 0x1c80
    rsp = 0x00007fff4a450ea0   rip = 0x0000000000401c80
    Found by: stack scanning
 4  test_breakpad!main + 0x170
    rsp = 0x00007fff4a450eb8   rip = 0x0000000000401df0
    Found by: stack scanning
 5  ld-2.23.so + 0x107db
    rsp = 0x00007fff4a450f10   rip = 0x00007f618b8fd7db
    Found by: stack scanning
 6  test_breakpad!main + 0x170
    rsp = 0x00007fff4a450f28   rip = 0x0000000000401df0
    Found by: stack scanning
 7  test_breakpad!_start + 0x29
    rsp = 0x00007fff4a450f40   rip = 0x0000000000401e19
    Found by: stack scanning
 8  0x7fff4a450f48
    rsp = 0x00007fff4a450f48   rip = 0x00007fff4a450f48
    Found by: call frame info

Loaded modules:
0x00400000 - 0x00418fff  test_breakpad  ???  (main)
0x7f618aa65000 - 0x7f618ab6cfff  libm-2.23.so  ???
0x7f618ad6e000 - 0x7f618af2dfff  libc-2.23.so  ???  (WARNING: No symbols, libc-2.23.so, 6E4AA51C760D811805B12E49FE6B80190)
0x7f618b138000 - 0x7f618b14dfff  libgcc_s.so.1  ???
0x7f618b34e000 - 0x7f618b4bffff  libstdc++.so.6.0.21  ???
0x7f618b6d0000 - 0x7f618b6e7fff  libpthread-2.23.so  ???
0x7f618b8ed000 - 0x7f618b912fff  ld-2.23.so  ???  (WARNING: No symbols, ld-2.23.so, D6BAADC0A3F94439F2B3567C078EC4720)
0x7fff4a4cd000 - 0x7fff4a4cefff  linux-gate.so  ???
2019-09-09 09:49:01: minidump.cc:5079: INFO: Minidump closing minidump