C++ Boost::Boost::Stacktrace

C++ Boost::Boost::Stacktrace

Stacktrace is a report of the active stack frames at a certain point in time during the execution of a program. When a program is run, memory is often dynamically allocated in two places, the stack, and the heap. 

Stacktrace can be referred to as backtracking.
Let's see an example of stack tracing:

#include <boost/stacktrace.hpp>

// ... somewhere inside the `bar(int)` function that is called recursively: std::cout << boost::stacktrace::stacktrace();

In that example:

  • boost::stacktrace:: is the namespace that has all the classes and functions to work with the stack trace.
  •   stacktrace() is the default constructor call; constructor stores the current function call sequence inside the stack trace class.


#include <boost/stacktrace.hpp>


std::cout << boost::stacktrace::stacktrace();


#include <signal.h> // ::signal, ::raise #include <boost/stacktrace.hpp> #include<iostream> int main() { void my_signal_handler(void); } void my_signal_handler(int signum) { ::signal(signum, SIG_DFL); boost::stacktrace::safe_dump_to("./backtrace.dump"); ::raise(SIGABRT); }


# bar(int) at /path/to/source/file.cpp:70 1# bar(int) at /path/to/source/file.cpp:70 2# bar(int) at /path/to/source/file.cpp:70 3# bar(int) at /path/to/source/file.cpp:70 4# main at /path/to/main.cpp:93 5# __libc_start_main in /lib/x86_64-linux-gnu/libc.so.6 6# _start