C++ Logo

std-proposals

Advanced search

Stacktrace from exception

From: Tarmo Pikaro <tapika_at_[hidden]>
Date: Fri, 9 Jul 2021 06:29:52 +0000 (UTC)
Hi !
Originally posted message over here:
https://lists.isocpp.org/std-proposals/2021/04/2539.php


> 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!vRegards, Andrei
C# in it's design does not allow only to catch exception, but also to survive from it.
The mechanics goes deeper in handling such events as DispatcherUnhandledException
https://github.com/tapika/stacktrace/blob/develop/example/csharp_crashy_app.cs#L16

and deeper into C#, like HandleProcessCorruptedStateExceptions special kind of attributes.
But I was wondering whether same kind of exception handling system could be implemented in c++.
No matter what kind of exception occurs - for example assignment to nullptr, (https://github.com/tapika/stacktrace/blob/develop/example/cpp_crashy_managed_dll/cpp_crashy_managed_dll.cpp#L16)Jump by null pointer - https://github.com/tapika/stacktrace/blob/develop/example/cpp_crashy_managed_dll/cpp_crashy_managed_dll.cpp#L23
C++ application would be able to survive it.
Initial idea of how low level exception could be implemented in illustrated in this header file:https://github.com/tapika/stacktrace/blob/develop/include/boost/stacktrace/exception_handler.hpp
and sample code:https://github.com/tapika/stacktrace/blob/develop/example/cpp_crashy_managed_dll/cpp_crashy_managed_dll.cpp#L108

Where you get exception code and exception textual representation if you want to log it.
Exception codes and their description could be also generalized - I have just listed all for windows and linux:https://github.com/tapika/stacktrace/blob/develop/src/exception_handler.cpp#L36

but I think null pointer exception code for windows is EXCEPTION_ACCESS_VIOLATION and for linux SIGSEGV - but that could be abstracted behind common exception codeand common description message.
There are more problems on the way - especially if you mix C++ and C# code, then windows C++ implementation might need to ignore special .net framework codes(https://github.com/tapika/stacktrace/blob/develop/src/exception_handler.cpp#L87)
But this is something Microsoft team will need to work on.
More information for Microsoft team, Windows port:

- There exists a way to probe at run-time whether application is pure C++ or mixed mode C++.(https://github.com/tapika/stacktrace/blob/develop/src/exception_handler.cpp#L194, https://github.com/tapika/stacktrace/blob/develop/src/exception_handler.cpp#L215)
- Breakpoints not always work with my own approach (https://github.com/tapika/stacktrace/blob/develop/src/exception_handler.cpp#L133)
- C# re-throw will be catched by C++ exception handler (https://github.com/dotnet/diagnostics/issues/151#issuecomment-756894271)
But I think we can clearly define interface, and let Microsoft worry about implementation details later on.
So would it be possible to add following use case:
Application can detect abnormal application behavior and survive from it without terminating the process.Call stack determination might be used when such low level exception happens.
-- Have a nice day! Tarmo.



Received on 2021-07-09 01:29:56