For a process exiting normally in Windows, the exit code of the process is generally either the return value from main
, or the exit code passed to std::exit
. %ERRORLEVEL%
can then be used to query the exit code, and that can be used to determine whether the program executed either correctly, or there were some exceptional inputs/failures that indicate a particular problem (application specific).
However, I'm interested in the exit code if the process crashes. Take a very simple example program:
int main()
{
int * a = nullptr;
*a = 0xBAD;
return 0;
}
When I compile this and run in Windows, on the command line I get:
MyCrashProgram.exe -> crashes
echo %ERRORLEVEL% -> -1073741819
The exit code is consistently this number. Which leads me to several questions:
- Was the exit code
-1073741819
somehow predicable, based on the invalid write crash? - If so, is there some way to determine the type of crash, based on the exit code?
- Does this change with the compiler used (I used MSVC 2012)?
- Does this change with the version of Windows being used (I used Win10 TP)?
- Does this change with the architecture (eg. x64 - I used Win32)?
Note, I am not interested in how to modify the program to catch the exception. I am interested in classifying crashes that can happen in existing programs, that I may not be able to modify.
Here is a related short blog post by Raymond Chen (emphasis mine):
The comment about
STATUS_ACCESS_VIOLATION
, led me to the documentation onGetExceptionCode
:EXCEPTION_ACCESS_VIOLATION
maps toSTATUS_ACCESS_VIOLATION
in the list that follows. All exceptions in the list prefixed withSTATUS
are directly defined to exception codes prefixed withEXCEPTION
. Following the documentation toRaiseException
, it explains the process of attempting to debug the exception when it occurs, the final step being:So to answer my questions:
EXCEPTION_STATUS_VIOLATION
.This is by no mean a comprehensive answer but rather a few hints so that you can move forward.
I think there is no way to distinguish all the possible cause of crash automatically. To do that you would have to catch the error yourself and provide your own exit code
In order to catch all the possible (catchable) errors, you have to setup both exception and signal handlers. This is because access violations are exceptions under windows and signal (SIGSEV) under linux.
See this question for the fine details about the different kinds of errors on windows: Catching access violation exceptions
Here is another thread for signal handling on linux