C++ Boost Stacktrace
Code for printing stack at a particular point
Reference: Stack Overflow
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
#define BOOST_STACKTRACE_USE_ADDR2LINE
#include <boost/stacktrace.hpp>
using namespace std::string_literals;
struct Fun {
Fun(const std::string& s) : _s(s) {}
void check() const {
std::cout << boost::stacktrace::stacktrace() << std::endl;
}
std::string _s;
};
void foo(const std::string& s) {
Fun fun(s);
fun.check();
}
int main() {
foo("Hello"s);
return 0;
}
// you may need to use -ldl flag with g++
Example output:
1
2
3
4
5
6
7
> g++ -o a.out -std=c++17 test.cpp -ldl && ./a.out
0# Fun::check() const in ./a.out
1# foo(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) in ./a.out
2# main in ./a.out
3# __libc_start_main in /lib64/libc.so.6
4# _start in ./a.out
This post is licensed under CC BY 4.0 by the author.