#include#include void print_call_stack() { auto stack = boost::stacktrace::stacktrace(); for (const auto& frame : stack) { std::cout << frame.name() << std::endl; } } int main() { // Call some functions here... print_call_stack(); return 0; }
#includeIn this example, we define a function `foo()` that takes two arguments `a` and `b` and computes their sum into a local variable `c`. We then create a new `stacktrace` object for the current function call and extract the values of the arguments and local variable from the first frame of the trace. The StackFrame class is part of the Boost C++ library, specifically the Boost.Stacktrace module.#include void foo(int a, int b) { int c = a + b; auto frame = boost::stacktrace::stacktrace(1, 1).as_vector()[0]; std::cout << "a = " << frame.arg(0) << std::endl; // prints "a = 1" std::cout << "b = " << frame.arg(1) << std::endl; // prints "b = 2" std::cout << "c = " << frame.var("c") << std::endl; // prints "c = 3" } int main() { foo(1, 2); return 0; }