Indeed. The time it takes to generate the stack trace can vary quite a bit. In places where I used it it varied from a few milliseconds on Linux with libbacktrace to multiple second freezes when using the StackWalk API on Windows
StackWalk is intended for debuggers, it's slow because will load and interpret debug symbols (.pdb files). On 64-bit Windows, you can get much faster stack walks by using RtlCaptureStackBackTrace, which works without debug symbols.
Also, you do not need to capture the stack when throwing an exception -- it's much better to capture it just at the place where you catch the exception, using the first-phase of two-phase-unwinding. With MSVC, this works by capturing the stack trace within the filter-expression of a __try/__except block. That way only the "unhandled exception" code paths that want to log a stack trace will spend time constructing it; other catch-blocks that don't need a stack trace can avoid generating it.
In what sense is a stack trace where you catch the exception "better"? That tells you nothing about what caused it. It's obviously cheaper but not terribly useful. You can always just log the exception in the handler, you don't need any stacktraces in that case at all.
You are misunderstanding. Two-phase-unwinding first searches the stack for a matching catch-block; and then in the second phase unwinds the stack (calling destructors). By capturing the stack trace within the filter-expression, it's captured in the first phase before the stack is unwound. You still get the full stack where the exception was thrown, but you only pay for it when necessary.
There doesn't seem to be any nice way like MSVC's exception filters, but I think at some point I saw something working on linux/libstdc++. I can't find it right now, but if I remember correctly, it involved hacking some RTTI implementation details to hook the "does the exception type match the catch-handler's type" check.
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
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.
View all comments
Show parent comment
3
u/WerWolv Apr 19 '26
Indeed. The time it takes to generate the stack trace can vary quite a bit. In places where I used it it varied from a few milliseconds on Linux with libbacktrace to multiple second freezes when using the StackWalk API on Windows