下载依赖:
这边的命令行主要以ubuntu为标准,对应的centos下的yum或者macos 上的brew以及windows下的choco可能有些区别。
sudo apt-get update
sudo apt install g++
sudo apt install git-all
sudo apt install build-essential
sudo apt-get install zlib1g-dev
获取源码:
git clone https://chromium.googlesource.com/breakpad/breakpad
这边需要注意了。如果直接按照googlesource上的说明,直接 make的话,会发现有第三方库lss缺失,这个库当然也在googlesource上,不过全称是linux-syscall-support,直接搜索lss可能搜索不到,这边还是通过git手动装一下第三方的库。
cd breakpad
git clone https://chromium.googlesource.com/linux-syscall-support src/third_party/lss
编译
进入到breakpad的文件夹下
./configuration && make
写个Demo
#include "client/linux/handler/exception_handler.h"
static bool dumpCallback(const google_breakpad::MinidumpDescriptor& descriptor,
void* context, bool succeeded) {
printf("Dump path: %s\n", descriptor.path());
return succeeded;
}
void crash() { volatile int* a = (int*)(NULL); *a = 1; }
int main(int argc, char* argv[]) {
google_breakpad::MinidumpDescriptor descriptor("/tmp");
google_breakpad::ExceptionHandler eh(descriptor, NULL, dumpCallback, NULL, true, -1);
crash();
return 0;
}
在src文件夹内创建一个新的c文件,我们命名为test.cpp,并且在文件内写上上诉的代码。
执行编译指令:
g++ test.c -o test.out -lbreakpad_client -lpthread -I./
这时候会发现 -lbreakpad_client 这个参数并没有生效,因为之前的configuration && make生成了libbreakpad_client.so动态库并没有放在-l会搜寻的文件夹下,此时只需要
cp client/linux/libbreakpad_client.a /usr/local/lib/
再执行上诉的g++编译程序,就可以再当前的文件夹下获取到一个 test.out的可执行文件。我们执行test.out可执行文件
./test.out
此时,触发了Cpp里面的crash异常,空指针赋值。 这时候就会调用程序中准备好的回调函数"dumpCallback",由于我们在程序中指定了输出目录为 "/tmp", 这时候就会输出对应的dmp文件所在的地址,我们可以到/tmp文件夹下寻找生成的.dmp文件了。