You mentioned that internally libc++ isn't throwing/catching errors as much, how so? Do they use something like expected or is the code structured differently compared to libstdc++ that it doesn't have to throw?
They still throw when required. They just seem to do it more in header files than inside their shared library. So more of its exceptions throwing locations get compiled into your own code
You mention this at the end but if you ever catch exceptions then always-on stack traces are problematic. Collecting a stack trace is very expensive and even if throwing is unusual it's a big latency spike. At least with a case-by-case macro you can opt out for throws you know are intended to be caught. Worth mentioning you can't fix this by making what() lazy as you'd get the wrong callstack.
So yeah this is nice but to be used with care, and if it ever bites you there isn't another solution than to disable it entirely, at least in non-debug builds.
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
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.
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.
No, it's hooking into the Itanium exception throwing code to capture the stacktrace instead. Boost's way looks like it would probably only work for boost exceptions
You seem to be right, they inject themselves by overwriting `__cxa_allocate_exception` instead of `__cxa_throw` and going the dynamic linker path I mentioned. Don't think that will work properly though when statically linking but in the general case, their implementation ends up doing pretty much the same thing
At work we have something very similar, but with some more features. Generally it has been working really well, but we have definitely run into a lot of caveats and edge cases.
It interacts badly with code running from a python process (e.g C++ code called from python) and we had an annoying bug that we access what() but some libraries with custom exception set that to a nullptr, which got us a weird segfault.
Generally a very powerful tool, but you put a lot of faith that any dependencies do the right thing (for ever).
The complete refusal to treat nullptr as an empty string in various contexts (std::string, std::string_view, and std::format foremost amongst them) is yet another example of C++ only talking about safety, and not actually doing anything to make it happen.
And sure, you can absolutely argue that nullptr is an invalid value and is therefore always wrong. But it's also an extremely common value, especially when dealing with C-based libraries, and accepting it as it is commonly used in C would remove a landmine or two.
"But what if you want to know the difference between an empty string and a nullptr?" --> Then you can still handle it yourself, before passing the char* to the relevant C++ function.
I think this is a generally better approach, and simpler to implement as well. The first thing I do on any C++ project is set the default terminate and signal handlers to print a stack trace. Honestly it should just be part of the language, can't see a downside.
The advantage of this proposal is that you only pay the cost of capturing the stack trace if a catch handler actually wants it. Otherwise, there's no extra overhead.
20
u/thisismyfavoritename Apr 19 '26
very cool trick.
You mentioned that internally libc++ isn't throwing/catching errors as much, how so? Do they use something like expected or is the code structured differently compared to libstdc++ that it doesn't have to throw?
10
u/mark_99 Apr 19 '26
You mention this at the end but if you ever catch exceptions then always-on stack traces are problematic. Collecting a stack trace is very expensive and even if throwing is unusual it's a big latency spike. At least with a case-by-case macro you can opt out for throws you know are intended to be caught. Worth mentioning you can't fix this by making
what()lazy as you'd get the wrong callstack.So yeah this is nice but to be used with care, and if it ever bites you there isn't another solution than to disable it entirely, at least in non-debug builds.
2
u/masscry Apr 19 '26
Sorry, for some reason I can't open the link.
Is the approach similar to https://www.boost.org/doc/libs/1_85_0/doc/html/boost/stacktrace/this_thread/set_cap_1_3_36_8_8_1_1_1_1.html?
6
u/JVApen Clever is an insult, not a compliment. - T. Winters Apr 19 '26
Nice! This looks useful for debug builds
6
u/Novermars Robotics Apr 19 '26
At work we have something very similar, but with some more features. Generally it has been working really well, but we have definitely run into a lot of caveats and edge cases.
It interacts badly with code running from a python process (e.g C++ code called from python) and we had an annoying bug that we access
what()but some libraries with custom exception set that to a nullptr, which got us a weird segfault.Generally a very powerful tool, but you put a lot of faith that any dependencies do the right thing (for ever).
5
u/Valuable_Leopard_799 Apr 19 '26
I'd also suggest just using
std::set_terminateand printstd::stacktrace::current()inside it.Apparently it's implementation defined whether the terminate is called before or after unraveling, but on GCC at least it works a treat.
6
u/jwakely libstdc++ tamer, LWG chair Apr 19 '26
See https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2022/p2490r3.html which some of us on the committee hope to pursue for a future version of the standard.
The advantage of this proposal is that you only pay the cost of capturing the stack trace if a catch handler actually wants it. Otherwise, there's no extra overhead.
3
u/RobinDesBuissieres Apr 19 '26
I use https://github.com/jeremy-rifkin/cpptrace/
1
u/rdtsc Apr 19 '26
You speak of crash log. If there's a crash there should be a crash dump, which already includes a stack trace (and much more).