From 16da9e07ae14dc5a1995880eb2fe2703dff4dd8b Mon Sep 17 00:00:00 2001 From: "madmaxoft@gmail.com" Date: Tue, 21 Feb 2012 21:09:42 +0000 Subject: Added code to produce dump files on Windows builds to aid with debugging server crashes git-svn-id: http://mc-server.googlecode.com/svn/trunk@306 0a769ca7-a7f5-676a-18bf-c427514a06d6 --- source/main.cpp | 85 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) diff --git a/source/main.cpp b/source/main.cpp index 416c49033..14c031e9e 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -7,6 +7,10 @@ #include //std::signal #include //exit() +#ifdef _WIN32 + #include +#endif // _WIN32 + #include "SquirrelBindings.h" #if USE_SQUIRREL #pragma warning(push) @@ -51,6 +55,72 @@ void ShowCrashReport(int) +#ifdef _WIN32 +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +// Windows stuff: when the server crashes, create a "dump file" containing the callstack of each thread and some variables; let the user send us that crash file for analysis + +typedef BOOL (WINAPI *pMiniDumpWriteDump)( + HANDLE hProcess, + DWORD ProcessId, + HANDLE hFile, + MINIDUMP_TYPE DumpType, + PMINIDUMP_EXCEPTION_INFORMATION ExceptionParam, + PMINIDUMP_USER_STREAM_INFORMATION UserStreamParam, + PMINIDUMP_CALLBACK_INFORMATION CallbackParam +); + +pMiniDumpWriteDump g_WriteMiniDump; // The function in dbghlp DLL that creates dump files + +char g_DumpFileName[MAX_PATH]; // Filename of the dump file; hes to be created before the dump handler kicks in +char g_ExceptionStack[128 * 1024]; // Substitute stack, just in case the handler kicks in because of "insufficient stack space" +MINIDUMP_TYPE DumpFlags = MiniDumpNormal; // By default dump only the stack and some helpers + + + + + +/** This function gets called just before the "program executed an illegal instruction and will be terminated" or similar. +Its purpose is to create the crashdump using the dbghlp DLLs +*/ +LONG WINAPI LastChanceExceptionFilter(__in struct _EXCEPTION_POINTERS * a_ExceptionInfo) +{ + char * newStack = &g_ExceptionStack[sizeof(g_ExceptionStack)]; + char * oldStack; + + // Use the substitute stack: + _asm + { + mov oldStack, esp + mov esp, newStack + } + + MINIDUMP_EXCEPTION_INFORMATION ExcInformation; + ExcInformation.ThreadId = GetCurrentThreadId(); + ExcInformation.ExceptionPointers = a_ExceptionInfo; + ExcInformation.ClientPointers = 0; + + // Write the dump file: + HANDLE dumpFile = CreateFile(g_DumpFileName, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); + g_WriteMiniDump(GetCurrentProcess(), GetCurrentProcessId(), dumpFile, DumpFlags, (a_ExceptionInfo) ? &ExcInformation : NULL, NULL, NULL); + CloseHandle(dumpFile); + + // Revert to old stack: + _asm + { + mov esp, oldStack + } + + return 0; +} + +#endif // _WIN32 + + + + +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +// main: + int main( int argc, char **argv ) { (void)argc; @@ -60,6 +130,18 @@ int main( int argc, char **argv ) InitLeakFinder(); #endif + // Magic code to produce dump-files on Windows if the server crashes: + #ifdef _WIN32 + HINSTANCE hDbgHelp = LoadLibrary("DBGHELP.DLL"); + g_WriteMiniDump = (pMiniDumpWriteDump)GetProcAddress(hDbgHelp, "MiniDumpWriteDump"); + if (g_WriteMiniDump != NULL) + { + _snprintf_s(g_DumpFileName, ARRAYCOUNT(g_DumpFileName), _TRUNCATE, "crash_mcs_%x.dmp", GetCurrentProcessId()); + SetUnhandledExceptionFilter(LastChanceExceptionFilter); + } + #endif // _WIN32 + // End of dump-file magic + #ifdef _DEBUG _CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF ); @@ -73,6 +155,9 @@ int main( int argc, char **argv ) std::signal(SIGSEGV, ShowCrashReport); #endif + // DEBUG: test the dumpfile creation: + // *((int *)0) = 0; + try { cRoot Root; -- cgit v1.2.3