r/cpp • • Apr 19 '26

Adding Stack Traces to All C++ Exceptions

https://werwolv.net/posts/cpp_exception_stacktraces/
120 Upvotes

27 comments sorted by

View all comments

Show parent comment

2

u/SkoomaDentist Antimodern C++, Embedded, Audio Apr 19 '26

What on earth is it doing if even the ”fast” case is millions of cycles?

5

u/WerWolv Apr 19 '26

Stack trace generation involves unwinding the stack to find all the return addresses followed by some sort of debug info parsing (be it embedded DWARF data or reading from a pdb file on disk) and then using that to generate the file, function and line information that will be displayed in the stack trace.

That's how std::stacktrace and co work in general though, doesn't change by having it happen for exceptions

4

u/kniy Apr 19 '26

The issue is that code might be compiled without frame pointers. On Linux, unwinding the stack involves looking up and interpreting DWARF bytecode in order to reconstruct the frame pointer from the (instruction pointer, stack pointer) pair. For code where debug symbols were stripped, it might be impossible to walk the stack. DWARF was intended for debuggers and is optimized for small size of debug symbols, not for fast extraction of the necessary information.

2

u/meltbox Apr 19 '26

This. Theres a freaking VM of sorts in there haha. And that should explain a lot.