| 1 |
Backward-cpp [](http://www.conan.io/source/backward/1.3.0/Manu343726/testing) |
| 2 |
============ |
| 3 |
|
| 4 |
Backward is a beautiful stack trace pretty printer for C++. |
| 5 |
|
| 6 |
If you are bored to see this: |
| 7 |
|
| 8 |
 |
| 9 |
|
| 10 |
Backward will spice it up for you: |
| 11 |
|
| 12 |
 |
| 13 |
|
| 14 |
There is not much to say. Of course it will be able to display the code |
| 15 |
snippets only if the source files are accessible (else see trace #4 in the |
| 16 |
example). |
| 17 |
|
| 18 |
All "Source" lines and code snippet prefixed by a pipe "|" are frames inline |
| 19 |
the next frame. |
| 20 |
You can see that for the trace #1 in the example, the function |
| 21 |
`you_shall_not_pass()` was inlined in the function `...read2::do_test()` by the |
| 22 |
compiler. |
| 23 |
|
| 24 |
## Installation |
| 25 |
|
| 26 |
#### Install backward.hpp |
| 27 |
|
| 28 |
Backward is a header only library. So installing Backward is easy, simply drop |
| 29 |
a copy of `backward.hpp` along with your other source files in your C++ project. |
| 30 |
You can also use a git submodule or really any other way that best fits your |
| 31 |
environment, as long as you can include `backward.hpp`. |
| 32 |
|
| 33 |
#### Install backward.cpp |
| 34 |
|
| 35 |
If you want Backward to automatically print a stack trace on most common fatal |
| 36 |
errors (segfault, abort, un-handled exception...), simply add a copy of |
| 37 |
`backward.cpp` to your project, and don't forget to tell your build system. |
| 38 |
|
| 39 |
The code in `backward.cpp` is trivial anyway, you can simply copy what it's |
| 40 |
doing at your convenience. |
| 41 |
|
| 42 |
Note for [folly](https://github.com/facebook/folly) library users: must define `backward::SignalHandling sh;` after `folly::init(&argc, &argv);`. |
| 43 |
|
| 44 |
## Configuration & Dependencies |
| 45 |
|
| 46 |
### Integration with CMake |
| 47 |
|
| 48 |
If you are using CMake and want to use its configuration abilities to save |
| 49 |
you the trouble, you can easily integrate Backward, depending on how you obtained |
| 50 |
the library. |
| 51 |
|
| 52 |
#### As a subdirectory: |
| 53 |
|
| 54 |
In this case you have a subdirectory containing the whole repository of Backward |
| 55 |
(eg.: using git-submodules), in this case you can do: |
| 56 |
|
| 57 |
``` |
| 58 |
add_subdirectory(/path/to/backward-cpp) |
| 59 |
|
| 60 |
# This will add backward.cpp to your target |
| 61 |
add_executable(mytarget mysource.cpp ${BACKWARD_ENABLE}) |
| 62 |
|
| 63 |
# This will add libraries, definitions and include directories needed by backward |
| 64 |
# by setting each property on the target. |
| 65 |
add_backward(mytarget) |
| 66 |
``` |
| 67 |
|
| 68 |
#### Modifying CMAKE_MODULE_PATH |
| 69 |
|
| 70 |
In this case you can have Backward installed as a subdirectory: |
| 71 |
|
| 72 |
``` |
| 73 |
list(APPEND CMAKE_MODULE_PATH /path/to/backward-cpp) |
| 74 |
find_package(Backward) |
| 75 |
|
| 76 |
# This will add libraries, definitions and include directories needed by backward |
| 77 |
# through an IMPORTED target. |
| 78 |
target_link_libraries(mytarget PUBLIC Backward::Backward) |
| 79 |
``` |
| 80 |
|
| 81 |
Notice that this is equivalent to using the the approach that uses `add_subdirectory()`, |
| 82 |
however it uses cmake's [imported target](https://cmake.org/Wiki/CMake/Tutorials/Exporting_and_Importing_Targets) mechanism. |
| 83 |
|
| 84 |
#### Installation through a regular package manager |
| 85 |
|
| 86 |
In this case you have obtained Backward through a package manager. |
| 87 |
|
| 88 |
Packages currently available: |
| 89 |
- [conda-forge](https://anaconda.org/conda-forge/backward-cpp) |
| 90 |
|
| 91 |
``` |
| 92 |
find_package(Backward) |
| 93 |
|
| 94 |
# This will add libraries, definitions and include directories needed by backward |
| 95 |
# through an IMPORTED target. |
| 96 |
target_link_libraries(mytarget PUBLIC Backward::Backward) |
| 97 |
``` |
| 98 |
### Libraries to unwind the stack |
| 99 |
|
| 100 |
On Linux and macOS, backtrace can back-trace or "walk" the stack using the |
| 101 |
following libraries: |
| 102 |
|
| 103 |
#### unwind |
| 104 |
|
| 105 |
Unwind comes from libgcc, but there is an equivalent inside clang itself. With |
| 106 |
unwind, the stacktrace is as accurate as it can possibly be, since this is |
| 107 |
used by the C++ runtine in gcc/clang for stack unwinding on exception. |
| 108 |
|
| 109 |
Normally libgcc is already linked to your program by default. |
| 110 |
|
| 111 |
#### libunwind from the [libunwind project](https://github.com/libunwind/libunwind) |
| 112 |
|
| 113 |
apt-get install binutils-dev (or equivalent) |
| 114 |
|
| 115 |
Libunwind provides, in some cases, a more accurate stacktrace as it knows |
| 116 |
to decode signal handler frames and lets us edit the context registers when |
| 117 |
unwinding, allowing stack traces over bad function references. |
| 118 |
|
| 119 |
For best results make sure you are using libunwind 1.3 or later, which added |
| 120 |
`unw_init_local2` and support for handling signal frames. |
| 121 |
|
| 122 |
CMake will warn you when configuring if your libunwind version doesn't support |
| 123 |
signal frames. |
| 124 |
|
| 125 |
On macOS clang provides a libunwind API compatible library as part of its |
| 126 |
environment, so no third party libraries are necessary. |
| 127 |
|
| 128 |
### Compile with debug info |
| 129 |
|
| 130 |
You need to compile your project with generation of debug symbols enabled, |
| 131 |
usually `-g` with clang++ and g++. |
| 132 |
|
| 133 |
Note that you can use `-g` with any level of optimization, with modern debug |
| 134 |
information encoding like DWARF, it only takes space in the binary (it's not |
| 135 |
loaded in memory until your debugger or Backward makes use of it, don't worry), |
| 136 |
and it doesn't impact the code generation (at least on GNU/Linux x86\_64 for |
| 137 |
what I know). |
| 138 |
|
| 139 |
If you are missing debug information, the stack trace will lack details about |
| 140 |
your sources. |
| 141 |
|
| 142 |
### Libraries to read the debug info |
| 143 |
|
| 144 |
Backward supports pretty printed stack traces on GNU/Linux, macOS and Windows, |
| 145 |
it will compile fine under other platforms but will not do anything. **Pull |
| 146 |
requests are welcome :)** |
| 147 |
|
| 148 |
Also, by default you will get a really basic stack trace, based on the |
| 149 |
`backtrace_symbols` API: |
| 150 |
|
| 151 |
 |
| 152 |
|
| 153 |
You will need to install some dependencies to get the ultimate stack trace. |
| 154 |
Three libraries are currently supported, the only difference is which one is the |
| 155 |
easiest for you to install, so pick your poison: |
| 156 |
|
| 157 |
#### libbfd from the [GNU/binutils](http://www.gnu.org/software/binutils/) |
| 158 |
|
| 159 |
apt-get install binutils-dev (or equivalent) |
| 160 |
|
| 161 |
And do not forget to link with the lib: `g++/clang++ -lbfd -ldl ...` |
| 162 |
|
| 163 |
This library requires dynamic loading. Which is provided by the library `dl`. |
| 164 |
Hence why we also link with `-ldl`. |
| 165 |
|
| 166 |
Then define the following before every inclusion of `backward.hpp` (don't |
| 167 |
forget to update `backward.cpp` as well): |
| 168 |
|
| 169 |
#define BACKWARD_HAS_BFD 1 |
| 170 |
|
| 171 |
#### libdw from the [elfutils](https://fedorahosted.org/elfutils/) |
| 172 |
|
| 173 |
apt-get install libdw-dev (or equivalent) |
| 174 |
|
| 175 |
And do not forget to link with the lib and inform Backward to use it: |
| 176 |
|
| 177 |
#define BACKWARD_HAS_DW 1 |
| 178 |
|
| 179 |
Of course you can simply add the define (`-DBACKWARD_HAS_...=1`) and the |
| 180 |
linkage details in your build system and even auto-detect which library is |
| 181 |
installed, it's up to you. |
| 182 |
|
| 183 |
#### [libdwarf](https://sourceforge.net/projects/libdwarf/) and [libelf](http://www.mr511.de/software/english.html) |
| 184 |
|
| 185 |
apt-get install libdwarf-dev (or equivalent) |
| 186 |
|
| 187 |
And do not forget to link with the lib and inform Backward to use it: |
| 188 |
|
| 189 |
#define BACKWARD_HAS_DWARF 1 |
| 190 |
|
| 191 |
There are several alternative implementations of libdwarf and libelf that |
| 192 |
are API compatible so it's possible, although it hasn't been tested, to |
| 193 |
replace the ones used when developing backward (in bold, below): |
| 194 |
|
| 195 |
* **_libelf_** by [Michael "Tired" Riepe](http://www.mr511.de/software/english.html) |
| 196 |
* **_libdwarf_** by [David Anderson](https://www.prevanders.net/dwarf.html) |
| 197 |
* libelf from [elfutils](https://fedorahosted.org/elfutils/) |
| 198 |
* libelf and libdwarf from FreeBSD's [ELF Tool Chain](https://sourceforge.net/p/elftoolchain/wiki/Home/) project |
| 199 |
|
| 200 |
|
| 201 |
Of course you can simply add the define (`-DBACKWARD_HAS_...=1`) and the |
| 202 |
linkage details in your build system and even auto-detect which library is |
| 203 |
installed, it's up to you. |
| 204 |
|
| 205 |
That's it, you are all set, you should be getting nice stack traces like the |
| 206 |
one at the beginning of this document. |
| 207 |
|
| 208 |
## API |
| 209 |
|
| 210 |
If you don't want to limit yourself to the defaults offered by `backward.cpp`, |
| 211 |
and you want to take some random stack traces for whatever reason and pretty |
| 212 |
print them the way you love or you decide to send them all to your buddies over |
| 213 |
the Internet, you will appreciate the simplicity of Backward's API. |
| 214 |
|
| 215 |
### Stacktrace |
| 216 |
|
| 217 |
The StackTrace class lets you take a "snapshot" of the current stack. |
| 218 |
You can use it like this: |
| 219 |
|
| 220 |
```c++ |
| 221 |
using namespace backward; |
| 222 |
StackTrace st; st.load_here(32); |
| 223 |
Printer p; p.print(st); |
| 224 |
``` |
| 225 |
|
| 226 |
The public methods are: |
| 227 |
|
| 228 |
```c++ |
| 229 |
class StackTrace { public: |
| 230 |
// Take a snapshot of the current stack, with at most "trace_cnt_max" |
| 231 |
// traces in it. The first trace is the most recent (ie the current |
| 232 |
// frame). You can also provide a trace address to load_from() assuming |
| 233 |
// the address is a valid stack frame (useful for signal handling traces). |
| 234 |
// Both function return size(). |
| 235 |
size_t load_here(size_t trace_cnt_max) |
| 236 |
size_t load_from(void* address, size_t trace_cnt_max) |
| 237 |
|
| 238 |
// The number of traces loaded. This can be less than "trace_cnt_max". |
| 239 |
size_t size() const |
| 240 |
|
| 241 |
// A unique id for the thread in which the trace was taken. The value |
| 242 |
// 0 means the stack trace comes from the main thread. |
| 243 |
size_t thread_id() const |
| 244 |
|
| 245 |
// Retrieve a trace by index. 0 is the most recent trace, size()-1 is |
| 246 |
// the oldest one. |
| 247 |
Trace operator[](size_t trace_idx) |
| 248 |
}; |
| 249 |
``` |
| 250 |
|
| 251 |
### TraceResolver |
| 252 |
|
| 253 |
The `TraceResolver` does the heavy lifting, and intends to transform a simple |
| 254 |
`Trace` from its address into a fully detailed `ResolvedTrace` with the |
| 255 |
filename of the source, line numbers, inlined functions and so on. |
| 256 |
|
| 257 |
You can use it like this: |
| 258 |
|
| 259 |
```c++ |
| 260 |
using namespace backward; |
| 261 |
StackTrace st; st.load_here(32); |
| 262 |
|
| 263 |
TraceResolver tr; tr.load_stacktrace(st); |
| 264 |
for (size_t i = 0; i < st.size(); ++i) { |
| 265 |
ResolvedTrace trace = tr.resolve(st[i]); |
| 266 |
std::cout << "#" << i |
| 267 |
<< " " << trace.object_filename |
| 268 |
<< " " << trace.object_function |
| 269 |
<< " [" << trace.addr << "]" |
| 270 |
<< std::endl; |
| 271 |
} |
| 272 |
``` |
| 273 |
|
| 274 |
The public methods are: |
| 275 |
|
| 276 |
```c++ |
| 277 |
class TraceResolver { public: |
| 278 |
// Pre-load whatever is necessary from the stack trace. |
| 279 |
template <class ST> |
| 280 |
void load_stacktrace(ST&) |
| 281 |
|
| 282 |
// Resolve a trace. It takes a ResolvedTrace, because a `Trace` is |
| 283 |
// implicitly convertible to it. |
| 284 |
ResolvedTrace resolve(ResolvedTrace t) |
| 285 |
}; |
| 286 |
``` |
| 287 |
|
| 288 |
### SnippetFactory |
| 289 |
|
| 290 |
The SnippetFactory is a simple helper class to automatically load and cache |
| 291 |
source files in order to extract code snippets. |
| 292 |
|
| 293 |
```c++ |
| 294 |
class SnippetFactory { public: |
| 295 |
// A snippet is a list of line numbers and line contents. |
| 296 |
typedef std::vector<std::pair<size_t, std::string> > lines_t; |
| 297 |
|
| 298 |
// Return a snippet starting at line_start with up to context_size lines. |
| 299 |
lines_t get_snippet(const std::string& filename, |
| 300 |
size_t line_start, size_t context_size) |
| 301 |
|
| 302 |
// Return a combined snippet from two different locations and combine them. |
| 303 |
// context_size / 2 lines will be extracted from each location. |
| 304 |
lines_t get_combined_snippet( |
| 305 |
const std::string& filename_a, size_t line_a, |
| 306 |
const std::string& filename_b, size_t line_b, |
| 307 |
size_t context_size) |
| 308 |
|
| 309 |
// Tries to return a unified snippet if the two locations from the same |
| 310 |
// file are close enough to fit inside one context_size, else returns |
| 311 |
// the equivalent of get_combined_snippet(). |
| 312 |
lines_t get_coalesced_snippet(const std::string& filename, |
| 313 |
size_t line_a, size_t line_b, size_t context_size) |
| 314 |
``` |
| 315 |
|
| 316 |
### Printer |
| 317 |
|
| 318 |
A simpler way to pretty print a stack trace to the terminal. It will |
| 319 |
automatically resolve the traces for you: |
| 320 |
|
| 321 |
```c++ |
| 322 |
using namespace backward; |
| 323 |
StackTrace st; st.load_here(32); |
| 324 |
Printer p; |
| 325 |
p.object = true; |
| 326 |
p.color_mode = ColorMode::always; |
| 327 |
p.address = true; |
| 328 |
p.print(st, stderr); |
| 329 |
``` |
| 330 |
|
| 331 |
You can set a few options: |
| 332 |
|
| 333 |
```c++ |
| 334 |
class Printer { public: |
| 335 |
// Print a little snippet of code if possible. |
| 336 |
bool snippet = true; |
| 337 |
|
| 338 |
// Colorize the trace |
| 339 |
// - ColorMode::automatic: Activate colors if possible. For example, when using a TTY on linux. |
| 340 |
// - ColorMode::always: Always use colors. |
| 341 |
// - ColorMode::never: Never use colors. |
| 342 |
bool color_mode = ColorMode::automatic; |
| 343 |
|
| 344 |
// Add the addresses of every source location to the trace. |
| 345 |
bool address = false; |
| 346 |
|
| 347 |
// Even if there is a source location, also prints the object |
| 348 |
// from where the trace came from. |
| 349 |
bool object = false; |
| 350 |
|
| 351 |
// Resolve and print a stack trace to the given C FILE* object. |
| 352 |
// On linux, if the FILE* object is attached to a TTY, |
| 353 |
// color will be used if color_mode is set to automatic. |
| 354 |
template <typename StackTrace> |
| 355 |
FILE* print(StackTrace& st, FILE* fp = stderr); |
| 356 |
|
| 357 |
// Resolve and print a stack trace to the given std::ostream object. |
| 358 |
// Color will only be used if color_mode is set to always. |
| 359 |
template <typename ST> |
| 360 |
std::ostream& print(ST& st, std::ostream& os); |
| 361 |
``` |
| 362 |
|
| 363 |
|
| 364 |
### SignalHandling |
| 365 |
|
| 366 |
A simple helper class that registers for you the most common signals and other |
| 367 |
callbacks to segfault, hardware exception, un-handled exception etc. |
| 368 |
|
| 369 |
`backward.cpp` simply uses it like that: |
| 370 |
|
| 371 |
```c++ |
| 372 |
backward::SignalHandling sh; |
| 373 |
``` |
| 374 |
|
| 375 |
Creating the object registers all the different signals and hooks. Destroying |
| 376 |
this object doesn't do anything. It exposes only one method: |
| 377 |
|
| 378 |
```c++ |
| 379 |
bool loaded() const // true if loaded with success |
| 380 |
``` |
| 381 |
|
| 382 |
### Trace object |
| 383 |
|
| 384 |
To keep the memory footprint of a loaded `StackTrace` on the low-side, there a |
| 385 |
hierarchy of trace object, from a minimal `Trace `to a `ResolvedTrace`. |
| 386 |
|
| 387 |
#### Simple trace |
| 388 |
|
| 389 |
```c++ |
| 390 |
struct Trace { |
| 391 |
void* addr; // address of the trace |
| 392 |
size_t idx; // its index (0 == most recent) |
| 393 |
}; |
| 394 |
``` |
| 395 |
|
| 396 |
#### Resolved trace |
| 397 |
|
| 398 |
A `ResolvedTrace` should contains a maximum of details about the location of |
| 399 |
the trace in the source code. Note that not all fields might be set. |
| 400 |
|
| 401 |
```c++ |
| 402 |
struct ResolvedTrace: public Trace { |
| 403 |
|
| 404 |
struct SourceLoc { |
| 405 |
std::string function; |
| 406 |
std::string filename; |
| 407 |
size_t line; |
| 408 |
size_t col; |
| 409 |
}; |
| 410 |
|
| 411 |
// In which binary object this trace is located. |
| 412 |
std::string object_filename; |
| 413 |
|
| 414 |
// The function in the object that contains the trace. This is not the same |
| 415 |
// as source.function which can be an function inlined in object_function. |
| 416 |
std::string object_function; |
| 417 |
|
| 418 |
// The source location of this trace. It is possible for filename to be |
| 419 |
// empty and for line/col to be invalid (value 0) if this information |
| 420 |
// couldn't be deduced, for example if there is no debug information in the |
| 421 |
// binary object. |
| 422 |
SourceLoc source; |
| 423 |
|
| 424 |
// An optional list of "inliners". All of these sources locations where |
| 425 |
// inlined in the source location of the trace (the attribute right above). |
| 426 |
// This is especially useful when you compile with optimizations turned on. |
| 427 |
typedef std::vector<SourceLoc> source_locs_t; |
| 428 |
source_locs_t inliners; |
| 429 |
}; |
| 430 |
``` |
| 431 |
|
| 432 |
## Contact and copyright |
| 433 |
|
| 434 |
François-Xavier Bourlet <[email protected]> |
| 435 |
|
| 436 |
Copyright 2013-2017 Google Inc. All Rights Reserved. |
| 437 |
MIT License. |
| 438 |
|
| 439 |
### Disclaimer |
| 440 |
|
| 441 |
Although this project is owned by Google Inc. this is not a Google supported or |
| 442 |
affiliated project. |