Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DUMP in unhandled C++ exception

In MSVC, how can I make any unhandled C++ exception (std::runtime_error, for instance) crash my release-compiled program so that it generates a dump with the full stack from the exception throw location?

I have installed NTSD in the AeDebug registry and can generate good dumps for things like memory access violation, so the matter here comes down to crashing the program correctly, I suppose.

Thanks in advance.

like image 843
Jorge Vasquez Avatar asked Sep 05 '25 11:09

Jorge Vasquez


2 Answers

I finally cracked it down.

  1. Use the set_terminate() function to register a handler for every thread
  2. In you main function(), make it impossible for external DLLs (event Windows') to successfully call SetUnhandledExceptionFilter(). A great article on how to do that here: http://www.debuginfo.com/articles/debugfilters.html#overwrite .
  3. As for the handle itself, it is quite straightforward:
void Terminate()
{
  OutputDebugStringA("Terminate\r\n");
  RaiseException(0xE0000010, EXCEPTION_NONCONTINUABLE, 0, 0);
}

Calling RaiseException() like the above example is enough to make the process crash and produce my mush desired dump.

Just so you know, the problem I was having was:

  1. The IPHelper Windows API loads dynamically another Windows DLL
  2. This DLL uses Windows own version of the C runtime (MSVCRT instead of MSVCRT90)
  3. The new C++ runtime calls SetUnhandledExceptionFilter() on startup to catch C++ exceptions. Since the latest filter for C++ exceptions is the one who gets to call the handle set by set_terminate(), my handle wasn't called.
like image 195
Jorge Vasquez Avatar answered Sep 08 '25 06:09

Jorge Vasquez


SetUnhandledExceptionFilter and DebugBreak should probably do the job.

Edit: oops, rereading, you want to deal with uncaught C++ exceptions. That would be a bit trickier to do well -- by the time you (normally) get wind of a C++ exception, it's already unwound the stack back to the level of the handler. You don't have any real way to know an exception has been thrown until a catch is invoked, but by then the stack has been unwound.

like image 40
Jerry Coffin Avatar answered Sep 08 '25 04:09

Jerry Coffin