C++ Logo


Advanced search

Re: [std-proposals] Standard Library assistance in logging of all exceptions

From: Jonathan Wakely <cxx_at_[hidden]>
Date: Wed, 28 Feb 2024 16:55:53 +0000
On Wed, 28 Feb 2024 at 16:10, Frederick Virchanza Gotham via
Std-Proposals <std-proposals_at_[hidden]> wrote:
> Recently in my job I took over the coding of a very big Software
> Development Kit (SDK) written in C++ for x86_64 MS-Windows using
> MS-Visual Studio, as the previous maintainer left the company
> suddenly, and so I'm sort of reverse-engineering the SDK trying to
> figure out the very complicated class hierarchy and how it's all
> supposed to work.
> The SDK is a DLL file loaded into a .NET program written in C#. The
> SDK is not supposed to throw an exception that propagates up to the C#
> program, but currently this is happening and so I have to investigate
> it.
> In the C# program, when they catch the exception, the only info they
> can see is "External component has thrown an exception". I've tried
> all sorts of techniques and improvisations in the C# program to catch
> an "std::exception" or "char*" thrown from the SDK, but the
> information is already lost once the exception has reached C#. The C#
> program can't see a stack trace either.
> I searched the SDK source code for all instances of "throw" and I
> found about 200 matches. I could have replaced all of these 'throw'
> statements with a preprocessor macro that does logging, but this
> wouldn't aid in logging exceptions thrown from the standard library
> (nor thrown from another DLL). It would also mean editing dozens of
> files that I don't want to edit.
> So to try figure out how Microsoft does exceptions, I wrote a simple function:
> void Func(void) { throw "blue bananas"; }
> and I got Visual Studio to compile this function to x86_64 assembler,
> and I saw this instruction:
> call _CxxThrowException
> So I looked up '_CxxThrowException' and saw that it resides in
> "vcruntime410.dll". So in order to log all exceptions, all I had to do
> was provide my own implementation of this function as follows:
> void _CxxThrowException(void *pObj, ThrowInfo const *pType)
> {
> // Log the exception along with a stack trace here
> // Next call the original _CxxThrowException function
> Original_CxxThrowException(pObj, pType);
> }
> I got this working, and so now when the C# program catches the
> exception from C++, it's able to ask the DLL to see the exception log
> to see what was thrown (along with a stack trace). When it comes to
> the GNU g++ compiler, a quick web search shows me that people have
> been able to do the same thing by providing an implementation of
> '__cxa_throw'.
> I wonder would it be useful if the Standard Library were to allow us
> to portably do something like this? For example let's say we had:

No. If you want to use dirty tricks like this, go ahead. It doesn't
belong in the standard.

Received on 2024-02-28 16:57:11