Project introduction

Breakpad is a cross-platform C/C++ dump capture library developed by Google. The dump file is stored in Microsoft minidump format. Breakpad also supports sending the dump file to the server. You can also write dump files without triggering dump. Breakpad supports Windows, Linux, MacOS, Android, ios, and more. At present, Google Chrome, Firefox, Google Picasa, Camino, Google Earth and other projects use.

  • Homepage: chromium.googlesource.com/breakpad/br…
  • Documents: chromium.googlesource.com/breakpad/br…
  • GitHub address: github.com/google/brea…

The major components

Breakpad has three main components:

  • Breakpad Client: breakpad client static library (i.e. Libbreakpad_client.a). Its main function is to take over the exception handling of the program after the program crashes. Specifically, it mainly does two things.
    • Signal received when the responder crashes, including SIGSEGV, SIGABRT, SIGFPE, SIGILL, and SIGBUS. (The other two sigstops cannot be processed by SIGKILL)
    • Get runtime information at the moment the program crashes and save it as a minidump file.
  • Symbol Dumper: debug information file generator (dump_SYms). It is mainly used to extract symbolic information from executable programs and save it as a file in a specific format.
  • Processor Module: Minidump processor module: Minidump_stackWalk, which builds a readable call stack based on the Coredump and symbol file.

Download the compiled

  • Git Clone github.com/google/brea…
  • Git Clone github.com/adelshokhy1… The linux_SYscall_support. h file in LSS is saved to breakpad/ SRC /third_party/ LSS /.
  • Compile Breakpad: execute./configure && make in source.
Libbreakpad_client. a Is in the SRC /client/ Linux/directory. Dump_syms In the SRC /tools/ Linux /dump_syms/ directory. Minidump_stackwalk is in the SRC /processor/ directory. There is also a program to convert minidump to core file minidump-2-core in SRC /tools/ Linux /md2core/.Copy the code

Integrated breakpad

  • First, reference the exception handler’s header file in your program.
#include "client/linux/handler/exception_handler.h"
Copy the code
  • To generate the Minidump file, we need to instantiate an ExceptionHandler object and provide a path to store the Minidump and a callback function to receive information about the minidump that has been written.
static bool dumpCallback(const google_breakpad::MinidumpDescriptor& descriptor,
void* context, bool succeeded) {
  printf("Dump path: %s\n", descriptor.path());
  return succeeded;
}

void crash(a) { 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(a);return 0;
}
Copy the code
  • Breakpad’s static library libbreakpad_client.a needs to be linked to at compile time, and the header search path needs to include breakpad’s SRC directory. Compiling and running this program will generate a minidump file in the/TMP/directory and print the path to the file before exiting.
  • The compile command is as follows:
$ g++ -g -I ./ -o breakpad_test test.cpp ./client/linux/libbreakpad_client.a -lpthread -std=c++11
Copy the code
  • PS: Callback functions should do as little work as possible. Because when this function is called back, the program is in an unsafe state. Allocating memory or calling functions from other libraries may not be safe. If you have to implement something in a callback, the safest thing to do isforkandexecA new process to do whatever you need to do. The Breakpad source code includes some simple reimplementations of liBC functions, as well as some functions for making Linux system calls in SRC /third_party/ LSS. Direct calls to LIBC and other dynamic libraries should be avoided.

Send the minidump file

  • In a real application, the Minidump file could be sent to the server for subsequent analysis and statistics.
  • Breakpad source documents in SRC/common/Linux/http_upload h (HTTP upload) and SRC/tools/Linux/symupload minidump_upload. Cc (minidump upload).

  

Generate symbol file

  • To produce useful stack information, Breakpad requires that debug symbols in binaries be converted into symbol files in text format.
  • First, you need to make sure that you have compiled the program with -g to include debug symbols.
  • Then, use the Dump_syms tool to generate a symbol file in text format. For example, if the program name is test, run the following command:
$ ./dump_syms ./test > test.sym
Copy the code
  • To use these symbols with the MinidumP_StackWalk tool, you also need to place them in a specific directory structure. The first line of the symbol file contains the information needed to generate this directory structure, such as test.sym above, with the following command:
$ head -n1 test.sym MODULE Linux x86_64 6EDC6ACDB282125843FD59DA9C81BD830 test
$ mkdir -p ./symbols/test/6EDC6ACDB282125843FD59DA9C81BD830
$ mv test.sym ./symbols/test/6EDC6ACDB282125843FD59DA9C81BD830
Copy the code

  

Generates a stack trace based on minnidump and Symbol

The minidump_StackWalk tool extracts a Minidump file and its corresponding text format symbol and generates readable stack information. In the example above, you can use the following command to generate the final readable stack information file:

$ ./minidump_stackwalk xxxx.dmp ./symbols > dmp_info.txt
Copy the code

  

Used to summarize

  • Use the -g option to compile the program.
  • Use the dump_syms tool to extract the symbols from the executable generated in the previous step and place the symbol files in the specified directory.
  • Recompile the program without the -g option.
  • DMP files are generated when the program crashes.
  • Use the Minidump_StackWalk tool to convert the DMP file into a readable stack information file.

Above, it’s easier to use automated scripts.