We're on Windows and we want to get a crash dump (possibly using MiniDumpWriteDump
) for all scenarios where our application exit's unexpectedly.
So far we have identified, and set up, the following:
SetUnhandledExceptionFilter
for unhandled exception (Win32 as well as "normal" C++ ones.)_set_invalid_parameter_handler
for the CRT invalid argument handling_set_abort_behavior
plus aSIGABRT
handler to account for calls toabort()
Is there anything we missed? (Modulo some code non-legitimately calling ExitProcess
, TerminateProcess
or one of the exit
variants.)
I'll note that this question here is orthogonal to how a crash dump is then obtained. E.g., if you want a crash dump in case of abort
, you always must use _set_abort_behaviour
because otherwise abort just exit
s.
I'll also note that on Windows7+, not setting SetUHEF
and just setting up the "correct" WER dump settings in the registry is often a viable way.
Tall order, just in brief:
_set*
functions,SetUnhandledExceptionFilter
is enough for all.abort
will disable the global exception handler, which you setup usingSetUnhandledExceptionFilter
. CRT will simply call this same function willNULL
parameter, and your exception-handler is disabled (not called), if CRT is causing crash! What you can do? [X]CreateToolhelp32Snapshot
, and other functions. Look for this process, and suspend all other running threads (except current, ofcourse).There is more to it, which I haven't tried (not found usefulness) - Vectored Exception Handling.
One other approach is to run the application into a debugger, which you can craft yourself! In the debugger, you can catch ALL exceptions, just like VS debugger catches. See my article. But, you know, this is not proper approach.
EDIT: Just read the last content about process-termination. You shouldn't control that. In any case, you can just hook the required APIs, which would act as what you code for (like displaying message box).
[X] You need to use API hooking. I dont have link and details handy. You'd hook other related APIs, but primarily the
SetUnhandledExceptionFilter
(after you'd called it for you). You dummy (hooked) function will look like:I dont have link and details of API hooking handy.
And why not attempt to make your application more safe?
You can catch any exception with WER. As you have seen the CRT sometimes forces to call WER.
If you want to always catch excpetion in-process, you need to prevent the calling of
SetUnhandledExceptionFilter(NULL)
from the CRT. For more info see my blog entry: Improved “PreventSetUnhandledExceptionFilter”SetUnhandledExceptionFilter is emphatically not enough to capture all unexpected exits. If an application accidentally calls a pure virtual function then a dialog will pop up. The application will hang, but not crash. Since there is no exception neither SetUnhandledExceptionFilter nor WER can help. There are a few variations on this theme.
Worse yet is the weirdness if you crash in a kernel callback such as a WindowProc. If this happens in a 32-bit application on 64-bit Windows then the exception is caught by the OS and execution continues. Yes, the crash is silently handed. I find that terrifying.
http://randomascii.wordpress.com/2012/07/05/when-even-crashing-doesnt-work/ should detail all of the tricks needed to handle these unusual cases.
I use exactly the ones you've listed, plus
_set_purecall_handler
, plus this handy snippet of code:Source: http://randomascii.wordpress.com/2012/07/05/when-even-crashing-doesnt-work/
These other articles on his site also helped me understand this: http://randomascii.wordpress.com/2011/12/07/increased-reliability-through-more-crashes/ http://randomascii.wordpress.com/2012/07/22/more-adventures-in-failing-to-crash-properly/
To expand on all the answers here's what I found to work best for 100M+ installs:
std::set_terminate and std::set_unexpected perhaps should also be mentioned.
And the most important part to get it all right:
See setExceptionHandlers below for reference. Also, most likely you don't want to hook up all the handlers in debug builds or when IsDebuggerPresent.
I will add a workaround that one can use in certain scenarios when running on Windows 7:
Windows Error Reporting (WER) offers the option to write full memory dumps on app crash.
So, if you are fine with that, you "just" have to make sure that the crash scenarios you're actually interested in will trigger WER. ... Which, really, leads us back to this very question, but still ...