xref: /aosp_15_r20/external/google-breakpad/docs/linux_starter_guide.md (revision 9712c20fc9bbfbac4935993a2ca0b3958c5adad2)
1*9712c20fSFrederick Mayle# How To Add Breakpad To Your Linux Application
2*9712c20fSFrederick Mayle
3*9712c20fSFrederick MayleThis document is an overview of using the Breakpad client libraries on Linux.
4*9712c20fSFrederick Mayle
5*9712c20fSFrederick Mayle## Building the Breakpad libraries
6*9712c20fSFrederick Mayle
7*9712c20fSFrederick MayleBreakpad provides an Autotools build system that will build both the Linux
8*9712c20fSFrederick Mayleclient libraries and the processor libraries. Running `./configure && make` in
9*9712c20fSFrederick Maylethe Breakpad source directory will produce
10*9712c20fSFrederick Mayle**src/client/linux/libbreakpad\_client.a**, which contains all the code
11*9712c20fSFrederick Maylenecessary to produce minidumps from an application.
12*9712c20fSFrederick Mayle
13*9712c20fSFrederick Mayle## Integrating Breakpad into your Application
14*9712c20fSFrederick Mayle
15*9712c20fSFrederick MayleFirst, configure your build process to link **libbreakpad\_client.a** into your
16*9712c20fSFrederick Maylebinary, and set your include paths to include the **src** directory in the
17*9712c20fSFrederick Mayle**google-breakpad** source tree. Next, include the exception handler header:
18*9712c20fSFrederick Mayle
19*9712c20fSFrederick Mayle```cpp
20*9712c20fSFrederick Mayle#include "client/linux/handler/exception_handler.h"
21*9712c20fSFrederick Mayle```
22*9712c20fSFrederick Mayle
23*9712c20fSFrederick MayleNow you can instantiate an `ExceptionHandler` object. Exception handling is active for the lifetime of the `ExceptionHandler` object, so you should instantiate it as early as possible in your application's startup process, and keep it alive for as close to shutdown as possible. To do anything useful, the `ExceptionHandler` constructor requires a path where it can write minidumps, as well as a callback function to receive information about minidumps that were written:
24*9712c20fSFrederick Mayle
25*9712c20fSFrederick Mayle```cpp
26*9712c20fSFrederick Maylestatic bool dumpCallback(const google_breakpad::MinidumpDescriptor& descriptor,
27*9712c20fSFrederick Maylevoid* context, bool succeeded) {
28*9712c20fSFrederick Mayle  printf("Dump path: %s\n", descriptor.path());
29*9712c20fSFrederick Mayle  return succeeded;
30*9712c20fSFrederick Mayle}
31*9712c20fSFrederick Mayle
32*9712c20fSFrederick Maylevoid crash() { volatile int* a = (int*)(NULL); *a = 1; }
33*9712c20fSFrederick Mayle
34*9712c20fSFrederick Mayleint main(int argc, char* argv[]) {
35*9712c20fSFrederick Mayle  google_breakpad::MinidumpDescriptor descriptor("/tmp");
36*9712c20fSFrederick Mayle  google_breakpad::ExceptionHandler eh(descriptor, NULL, dumpCallback, NULL, true, -1);
37*9712c20fSFrederick Mayle  crash();
38*9712c20fSFrederick Mayle  return 0;
39*9712c20fSFrederick Mayle}
40*9712c20fSFrederick Mayle```
41*9712c20fSFrederick Mayle
42*9712c20fSFrederick MayleCompiling and running this example should produce a minidump file in /tmp, and
43*9712c20fSFrederick Mayleit should print the minidump filename before exiting. You can read more about
44*9712c20fSFrederick Maylethe other parameters to the `ExceptionHandler` constructor [in the exception_handler.h source file][1].
45*9712c20fSFrederick Mayle
46*9712c20fSFrederick Mayle[1]: https://chromium.googlesource.com/breakpad/breakpad/+/master/src/client/linux/handler/exception_handler.h
47*9712c20fSFrederick Mayle
48*9712c20fSFrederick Mayle**Note**: You should do as little work as possible in the callback function.
49*9712c20fSFrederick MayleYour application is in an unsafe state. It may not be safe to allocate memory or
50*9712c20fSFrederick Maylecall functions from other shared libraries. The safest thing to do is `fork` and
51*9712c20fSFrederick Mayle`exec` a new process to do any work you need to do. If you must do some work in
52*9712c20fSFrederick Maylethe callback, the Breakpad source contains [some simple reimplementations of libc functions][2], to avoid calling directly into
53*9712c20fSFrederick Maylelibc, as well as [a header file for making Linux system calls][3] (in **src/third\_party/lss**) to avoid calling into other shared libraries.
54*9712c20fSFrederick Mayle
55*9712c20fSFrederick Mayle[2]: https://chromium.googlesource.com/breakpad/breakpad/+/master/src/common/linux/linux_libc_support.h
56*9712c20fSFrederick Mayle[3]: https://chromium.googlesource.com/linux-syscall-support/+/master
57*9712c20fSFrederick Mayle
58*9712c20fSFrederick Mayle## Sending the minidump file
59*9712c20fSFrederick Mayle
60*9712c20fSFrederick MayleIn a real application, you would want to handle the minidump in some way, likely
61*9712c20fSFrederick Mayleby sending it to a server for analysis. The Breakpad source tree contains [some
62*9712c20fSFrederick MayleHTTP upload source][4] that you might find useful, as well as [a minidump upload tool][5].
63*9712c20fSFrederick Mayle
64*9712c20fSFrederick Mayle[4]: https://chromium.googlesource.com/breakpad/breakpad/+/master/src/common/linux/http_upload.h
65*9712c20fSFrederick Mayle[5]: https://chromium.googlesource.com/breakpad/breakpad/+/master/src/tools/linux/symupload/minidump_upload.cc
66*9712c20fSFrederick Mayle
67*9712c20fSFrederick Mayle## Producing symbols for your application
68*9712c20fSFrederick Mayle
69*9712c20fSFrederick MayleTo produce useful stack traces, Breakpad requires you to convert the debugging
70*9712c20fSFrederick Maylesymbols in your binaries to [text-format symbol files][6]. First, ensure that you've compiled your binaries with `-g` to
71*9712c20fSFrederick Mayleinclude debugging symbols. Next, compile the `dump_syms` tool by running
72*9712c20fSFrederick Mayle`configure && make` in the Breakpad source directory. Next, run `dump_syms` on
73*9712c20fSFrederick Mayleyour binaries to produce the text-format symbols. For example, if your main
74*9712c20fSFrederick Maylebinary was named `test`:
75*9712c20fSFrederick Mayle
76*9712c20fSFrederick Mayle[6]: https://chromium.googlesource.com/breakpad/breakpad/+/master/docs/symbol_files.md
77*9712c20fSFrederick Mayle
78*9712c20fSFrederick Mayle```
79*9712c20fSFrederick Mayle$ google-breakpad/src/tools/linux/dump_syms/dump_syms ./test > test.sym
80*9712c20fSFrederick Mayle```
81*9712c20fSFrederick Mayle
82*9712c20fSFrederick MayleIn order to use these symbols with the `minidump_stackwalk` tool, you will need
83*9712c20fSFrederick Mayleto place them in a specific directory structure. The first line of the symbol
84*9712c20fSFrederick Maylefile contains the information you need to produce this directory structure, for
85*9712c20fSFrederick Mayleexample (your output will vary):
86*9712c20fSFrederick Mayle
87*9712c20fSFrederick Mayle```
88*9712c20fSFrederick Mayle$ head -n1 test.sym MODULE Linux x86_64 6EDC6ACDB282125843FD59DA9C81BD830 test
89*9712c20fSFrederick Mayle$ mkdir -p ./symbols/test/6EDC6ACDB282125843FD59DA9C81BD830
90*9712c20fSFrederick Mayle$ mv test.sym ./symbols/test/6EDC6ACDB282125843FD59DA9C81BD830
91*9712c20fSFrederick Mayle```
92*9712c20fSFrederick Mayle
93*9712c20fSFrederick MayleYou may also find the [symbolstore.py][7] script in the Mozilla repository useful, as it encapsulates these steps.
94*9712c20fSFrederick Mayle
95*9712c20fSFrederick Mayle[7]: https://dxr.mozilla.org/mozilla-central/source/toolkit/crashreporter/tools/symbolstore.py
96*9712c20fSFrederick Mayle
97*9712c20fSFrederick Mayle## Processing the minidump to produce a stack trace
98*9712c20fSFrederick Mayle
99*9712c20fSFrederick MayleBreakpad includes a tool called `minidump_stackwalk` which can take a minidump
100*9712c20fSFrederick Mayleplus its corresponding text-format symbols and produce a symbolized stacktrace.
101*9712c20fSFrederick MayleIt should be in the **google-breakpad/src/processor** directory if you compiled
102*9712c20fSFrederick Maylethe Breakpad source using the directions above. Simply pass it the minidump and
103*9712c20fSFrederick Maylethe symbol path as commandline parameters:
104*9712c20fSFrederick Mayle
105*9712c20fSFrederick Mayle```
106*9712c20fSFrederick Mayle$ google-breakpad/src/processor/minidump_stackwalk minidump.dmp ./symbols
107*9712c20fSFrederick Mayle```
108*9712c20fSFrederick Mayle
109*9712c20fSFrederick MayleIt produces verbose output on stderr, and the stacktrace on stdout, so you may
110*9712c20fSFrederick Maylewant to redirect stderr.
111