Debugging Made Easier: C++23’s Stacktrace Library Unveiled

Kirubakaran
2 min readJun 6, 2024

For ages, developers use their own custom functions or third party libraries to print the callstack/calltrace of the C++ code. This provides the much needed assistance in debugging the issues with documented flow. However, there are still some issues like those libraries cant be ported or not supported in some cases. Worse, it falters in corner cases.

Finally, C++ Org might have heard the developers prayer :)

Among the many useful features introduced in C++ 23, Stacktrace was one of the important things which we can look on today.

We discussed another interesting C++ feature here https://medium.com/p/cee9d79e2597 ( Increases C++ speed)

Coming back to Stacktrace,

#include <stacktrace>
#include <iostream>

void foo() {
std::stacktrace trace = std::stacktrace::current();
std::cout << trace << std::endl;
}

void bar() {
foo();
}

int main() {
bar();
return 0;
}

In this example, the std::stacktrace::current() function is used to capture the current stack trace, which is then printed to the console. The output will show the sequence of function calls (main -> bar -> foo) that led to the creation of the stack trace.

--

--

Kirubakaran

Software Engineer expertise on C, C++, Golang, Python, Docker, Kubernetes.