Skip to content

Commit d408cf4

Browse files
authored
Document build options and use windbg_cached in Boost::stacktrace in CMake (#205)
1 parent 9d314b2 commit d408cf4

File tree

2 files changed

+72
-10
lines changed

2 files changed

+72
-10
lines changed

CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,10 @@ if(BOOST_STACKTRACE_ENABLE_WINDBG)
115115

116116
target_link_libraries(boost_stacktrace INTERFACE Boost::stacktrace_windbg)
117117

118+
elseif(BOOST_STACKTRACE_ENABLE_WINDBG_CACHED)
119+
120+
target_link_libraries(boost_stacktrace INTERFACE Boost::stacktrace_windbg)
121+
118122
elseif(BOOST_STACKTRACE_ENABLE_BACKTRACE)
119123

120124
target_link_libraries(boost_stacktrace INTERFACE Boost::stacktrace_backtrace)

doc/stacktrace.qbk

Lines changed: 68 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ Pretty often assertions provide not enough information to locate the problem. Fo
6868
Aborted (core dumped)
6969
```
7070

71-
That's not enough to locate the problem without debugger. There may be thousand code lines in real world examples and hundred places where that assertion could happen. Let's try to improve the assertions, and make them more informative:
71+
That's not enough to locate the problem without debugger. There may be thousand code lines in real world examples and hundred places where that assertion can happen. Let's try to improve the assertions, and make them more informative:
7272

7373
[getting_started_assert_handlers]
7474

@@ -236,7 +236,7 @@ Code from above will output:
236236

237237
[section Enabling and disabling stacktraces]
238238

239-
At some point arises a requirement to easily enable/disable stacktraces for a whole project. That could be easily achieved.
239+
At some point arises a requirement to easily enable/disable stacktraces for a whole project. That can be easily achieved.
240240

241241
Just define *BOOST_STACKTRACE_LINK* for a whole project. Now you can enable/disable stacktraces by just linking with different libraries:
242242

@@ -251,7 +251,7 @@ See [link stacktrace.configuration_and_build section "Configuration and Build"]
251251
[section Saving stacktraces by specified format]
252252

253253
[classref boost::stacktrace::stacktrace] provides access to individual [classref boost::stacktrace::frame frames] of the stacktrace,
254-
so that you could save stacktrace information in your own format. Consider the example, that saves only function addresses of each frame:
254+
so that you can save stacktrace information in your own format. Consider the example, that saves only function addresses of each frame:
255255

256256
[getting_started_trace_addresses]
257257

@@ -304,21 +304,77 @@ Terminate called:
304304

305305
[section Configuration and Build]
306306

307-
By default Boost.Stacktrace is a header-only library, but you may change that and use the following macros to improve build times or to be able to tune library without recompiling your project:
307+
[section Usage from CMake]
308+
309+
CMake library `Boost::stacktrace` provides the best available implementation:
310+
311+
```
312+
target_link_libraries(${PROJECT}
313+
PUBLIC
314+
Boost::stacktrace
315+
)
316+
```
317+
318+
There are also CMake libraries for fine grained selection of implementation:
319+
320+
* `Boost::stacktrace_windbg`
321+
* `Boost::stacktrace_windbg_cached`
322+
* `Boost::stacktrace_backtrace`
323+
* `Boost::stacktrace_addr2line`
324+
* `Boost::stacktrace_basic`
325+
* `Boost::stacktrace_noop`
326+
327+
Note that `Boost::stacktrace_from_exception` is not used by default, so add it explicitly if its functionality is required:
328+
329+
```
330+
target_link_libraries(${PROJECT} # your project
331+
PUBLIC
332+
Boost::stacktrace
333+
Boost::stacktrace_from_exception
334+
)
335+
```
336+
337+
[endsect]
338+
339+
[section CMake build notes]
340+
341+
When building the Boost.Stacktrace libraries using `CMake` the `BOOST_STACKTRACE_ENABLE_*` options control the build. For example:
342+
343+
* `cmake -DBOOST_STACKTRACE_ENABLE_NOOP=0 -DBOOST_STACKTRACE_ENABLE_BACKTRACE=1 -DBOOST_STACKTRACE_ENABLE_FROM_EXCEPTION=1` - do not build the `noop` implementation and force the builds of `backtrace` and `from_exception`.
344+
* `cmake -DBOOST_STACKTRACE_ENABLE_NOOP=1 -DBOOST_STACKTRACE_ENABLE_WINDBG=1 -DBOOST_STACKTRACE_ENABLE_WINDBG_CACHED=0` - build the `noop` and `windbg` implementations and disable the build of `windbg_cached`.
345+
346+
If options are not provided they are auto-detected and the result of detection is printed and implicitly used during build.
347+
348+
[endsect]
349+
350+
[section B2 build notes]
351+
352+
When building the Boost.Stacktrace libraries using `b2` the `boost.stacktrace.*` options can be used to control the build. For example:
353+
354+
* `b2 boost.stacktrace.noop=off boost.stacktrace.backtrace=on boost.stacktrace.from_exception=on` - do not build the `noop` implementation and force the builds of `backtrace` and `from_exception`.
355+
* `b2 boost.stacktrace.noop=on boost.stacktrace.windbg=on boost.stacktrace.windbg_cached=off` - build the `noop` and `windbg` implementations and disable the build of `windbg_cached`.
356+
357+
If options are not provided they are auto-detected and the result of detection is printed and implicitly used during build.
358+
359+
[endsect]
360+
361+
[section Header only options]
362+
363+
If CMake is not used then the Boost.Stacktrace is a header-only library by default. To change that (to improve build times or to be able to tune library without recompiling your project) use the following macros:
308364
[table:linkmacro Link macros
309365
[[Macro name] [Effect]]
310366
[[*BOOST_STACKTRACE_LINK*] [Disable header-only build and require linking with shared or static library that contains the tracing implementation. If *BOOST_ALL_DYN_LINK* is defined, then link with shared library.]]
311367
[[*BOOST_STACKTRACE_DYN_LINK*] [Disable header-only build and require linking with shared library that contains tracing implementation.]]
312368
]
313369

314370

315-
In header only mode library could be tuned by macro. If one of the link macro from above is defined, you have to manually link with one of the libraries:
371+
In header only mode library can be tuned by macro. If one of the link macro from above is defined, you have to manually link with one of the libraries:
316372
[table:libconfig Config
317373
[[Macro name or default] [Library] [Effect] [Platforms] [Uses debug information [footnote This will provide more readable backtraces with *source code locations* if the binary is built with debug information.]] [Uses dynamic exports information [footnote This will provide readable function names in backtrace for functions that are exported by the binary. Compiling with `-rdynamic` flag, without `-fvisibility=hidden` or marking functions as exported produce a better stacktraces.]] ]
318-
[[['default for MSVC, Intel on Windows, MinGW-w64] / *BOOST_STACKTRACE_USE_WINDBG*] [*boost_stacktrace_windbg*] [ Uses `dbgeng.h` to show debug info, stores the implementation internals in a static varaible protected with mutex. May require linking with *ole32* and *dbgeng*. ] [MSVC, MinGW-w64, Intel on Windows] [yes] [no]]
374+
[[['default for MSVC, Intel on Windows, MinGW-w64] / *BOOST_STACKTRACE_USE_WINDBG*] [*boost_stacktrace_windbg*] [ Uses `dbgeng.h` to show debug info, stores the implementation internals in a static variable protected with mutex. May require linking with *ole32* and *dbgeng*. ] [MSVC, MinGW-w64, Intel on Windows] [yes] [no]]
319375
[[['default for other platforms]] [*boost_stacktrace_basic*] [Uses compiler intrinsics to collect stacktrace and if possible `::dladdr` to show information about the symbol. Requires linking with *libdl* library on POSIX platforms.] [Any compiler on POSIX or MinGW] [no] [yes]]
320376
[[*BOOST_STACKTRACE_USE_WINDBG_CACHED*] [*boost_stacktrace_windbg_cached*] [ Uses `dbgeng.h` to show debug info and caches implementation internals in TLS for better performance. Useful only for cases when traces are gathered very often. May require linking with *ole32* and *dbgeng*. ] [MSVC, Intel on Windows] [yes] [no]]
321-
[[*BOOST_STACKTRACE_USE_BACKTRACE*] [*boost_stacktrace_backtrace*] [Requires linking with *libdl* on POSIX and *libbacktrace* libraries[footnote Some *libbacktrace* packages SEGFAULT if there's a concurrent work with the same `backtrace_state` instance. To avoid that issue the Boost.Stacktrace library uses `thread_local` states, unfortunately this may consume a lot of memory if you often create and destroy execution threads in your application. Define *BOOST_STACKTRACE_BACKTRACE_FORCE_STATIC* to force single instance, but make sure that [@https://github.com/boostorg/stacktrace/blob/develop/test/thread_safety_checking.cpp thread_safety_checking.cpp] works well in your setup. ]. *libbacktrace* is probably already installed in your system[footnote If you are using Clang with libstdc++ you could get into troubles of including `<backtrace.h>`, because on some platforms Clang does not search for headers in the GCC's include paths and any attempt to add GCC's include path leads to linker errors. To explicitly specify a path to the `<backtrace.h>` header you could define the *BOOST_STACKTRACE_BACKTRACE_INCLUDE_FILE* to a full path to the header. For example on Ubuntu Xenial use the command line option *-DBOOST_STACKTRACE_BACKTRACE_INCLUDE_FILE=</usr/lib/gcc/x86_64-linux-gnu/5/include/backtrace.h>* while building with Clang. ], or built into your compiler.
377+
[[*BOOST_STACKTRACE_USE_BACKTRACE*] [*boost_stacktrace_backtrace*] [Requires linking with *libdl* on POSIX and *libbacktrace* libraries[footnote Some *libbacktrace* packages SEGFAULT if there's a concurrent work with the same `backtrace_state` instance. To avoid that issue the Boost.Stacktrace library uses `thread_local` states, unfortunately this may consume a lot of memory if you often create and destroy execution threads in your application. Define *BOOST_STACKTRACE_BACKTRACE_FORCE_STATIC* to force single instance, but make sure that [@https://github.com/boostorg/stacktrace/blob/develop/test/thread_safety_checking.cpp thread_safety_checking.cpp] works well in your setup. ]. *libbacktrace* is probably already installed in your system[footnote If you are using Clang with libstdc++ you can get into troubles of including `<backtrace.h>`, because on some platforms Clang does not search for headers in the GCC's include paths and any attempt to add GCC's include path leads to linker errors. To explicitly specify a path to the `<backtrace.h>` header you can define the *BOOST_STACKTRACE_BACKTRACE_INCLUDE_FILE* to a full path to the header. For example on Ubuntu Xenial use the command line option *-DBOOST_STACKTRACE_BACKTRACE_INCLUDE_FILE=</usr/lib/gcc/x86_64-linux-gnu/5/include/backtrace.h>* while building with Clang. ], or built into your compiler.
322378

323379
Otherwise (if you are a *MinGW*/*MinGW-w64* user for example) it can be downloaded [@https://github.com/ianlancetaylor/libbacktrace from here] or [@https://github.com/gcc-mirror/gcc/tree/master/libbacktrace from here]. ] [Any compiler on POSIX, or MinGW, or MinGW-w64] [yes] [yes]]
324380
[[*BOOST_STACKTRACE_USE_ADDR2LINE*] [*boost_stacktrace_addr2line*] [Use *addr2line* program to retrieve stacktrace. Requires linking with *libdl* library and `::fork` system call. Macro *BOOST_STACKTRACE_ADDR2LINE_LOCATION* must be defined to the absolute path to the addr2line executable if it is not located in /usr/bin/addr2line. ] [Any compiler on POSIX] [yes] [yes]]
@@ -331,6 +387,8 @@ In header only mode library could be tuned by macro. If one of the link macro fr
331387
* if you wish to disable backtracing and *BOOST_STACKTRACE_LINK* is defined, you just need link with *-lboost_stacktrace_noop*
332388
* if you wish to disable backtracing and you use the library in header only mode, you just need to define *BOOST_STACKTRACE_USE_NOOP* for the whole project and recompile it
333389

390+
[endsect]
391+
334392
[section MinGW and MinGW-w64 specific notes]
335393

336394
MinGW-w64 and MinGW (without -w64) users have to install libbacktrace for getting better stacktraces. Follow the instruction:
@@ -370,11 +428,11 @@ In theory, walking the stack without decoding and demangling should be async sig
370428
In practice, it is not:
371429

372430
* Looks like a page fault while dumping the trace on a containerized/virtualized
373-
Windows system has a chance to deadlock. Page fault could happen easily
431+
Windows system has a chance to deadlock. Page fault can happen easily
374432
as we have to write the dump either to memory or to a file.
375-
* On POSIX systems a deadlock could happen if a signal is received when throwing
433+
* On POSIX systems a deadlock can happen if a signal is received when throwing
376434
an exception [@https://github.com/boostorg/stacktrace/issues/131 #131].
377-
Theoretically this could be worked around by bypassing the mutex locking
435+
Theoretically this can be worked around by bypassing the mutex locking
378436
in C++-runtime at exception throw
379437
([@https://github.com/userver-framework/userver/blob/4246909c99506d3ab34bd130a5154b4acc8e87de/core/src/engine/task/exception_hacks.cpp#L241-L244 sample implementation]
380438
in the 🐙 userver framework), or by using a very modern runtime

0 commit comments

Comments
 (0)