C++ Logo


Advanced search

Re: Stacktrace from exception

From: Edward Catmur <ecatmur_at_[hidden]>
Date: Mon, 26 Apr 2021 17:56:48 +0100
Presumably this particular implementation won't work if libcxxabi is
statically linked?

We hook exception throwing by subclassing std::type_info, overriding
__do_catch, and using a small bit of asm to create a polymorphic type whose
vtable points to that std::type_info subclass. This does admittedly mean
that you can only access the stack trace if you have a catch specification
with this custom type, but we find that usually where you want the stack
trace you'd be doing `catch (...)` anyway so changing that to `catch
(AnyException&)` isn't too onerous.

Quick demo:
And godbolt (slightly nerfed, but you get the idea):

I've occasionally thought that it'd be nice to be able to run arbitrary
code at the throw point, but while we've had the technical ability to do so
for a couple of years there hasn't been any demand, so probably just being
able to access the current exception stack trace is sufficient.

On Mon, 26 Apr 2021 at 17:32, Андрей Некрашевич via Std-Proposals <
std-proposals_at_[hidden]> wrote:

> Hello everybody!
> The feature of getting stacktraces from exceptions was requested in
> https://lists.isocpp.org/std-proposals/2020/11/2086.php and in the "2021
> Annual C++ Developer Survey".
> I've created a prototype for getting stacktraces from exceptions
> https://github.com/axolm/libsfe.
> More details can be found in the paper D2370R0 "Stacktrace from exception"
> here -
> https://htmlpreview.github.io/?https://github.com/axolm/libsfe/blob/master/paper/stacktrace_from_exception.html
> Any comments are welcome!
> Regards, Andrei
> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals

Received on 2021-04-26 11:57:02