diff --git a/src/bnetd/main.cpp b/src/bnetd/main.cpp index d6c00b2..6442fbb 100644 --- a/src/bnetd/main.cpp +++ b/src/bnetd/main.cpp @@ -88,7 +88,9 @@ #include "handle_apireg.h" #include "i18n.h" #include "userlog.h" +#ifdef WIN32 #include "win32/windump.h" +#endif #include "common/setup_after.h" #ifdef WITH_LUA diff --git a/src/d2cs/main.cpp b/src/d2cs/main.cpp index d8ffad2..1503a25 100644 --- a/src/d2cs/main.cpp +++ b/src/d2cs/main.cpp @@ -37,6 +37,9 @@ #ifdef WIN32_GUI # include "win32/winmain.h" #endif +#ifdef WIN32 +# include "win32/windump.h" +#endif #include "compat/stdfileno.h" #include "compat/pgetpid.h" @@ -253,6 +256,11 @@ extern int main(int argc, char ** argv) { int pid; +#ifdef WIN32 + // create a dump file whenever the gateway crashes + SetUnhandledExceptionFilter(unhandled_handler); +#endif + eventlog_set(stderr); if (!((pid = config_init(argc, argv)) == 0)) { // if (pid==1) pid=0; diff --git a/src/d2dbs/main.cpp b/src/d2dbs/main.cpp index 7608c36..7cea46c 100644 --- a/src/d2dbs/main.cpp +++ b/src/d2dbs/main.cpp @@ -38,6 +38,9 @@ #ifdef WIN32_GUI # include "win32/winmain.h" #endif +#ifdef WIN32 +# include "win32/windump.h" +#endif #include "compat/stdfileno.h" #include "compat/pgetpid.h" @@ -233,6 +236,11 @@ extern int main(int argc, char ** argv) int pid; char * pidfile; +#ifdef WIN32 + // create a dump file whenever the gateway crashes + SetUnhandledExceptionFilter(unhandled_handler); +#endif + eventlog_set(stderr); pid = config_init(argc, argv); if (!(pid == 0)) { diff --git a/src/win32/windump.cpp b/src/win32/windump.cpp index c401517..172cf40 100644 --- a/src/win32/windump.cpp +++ b/src/win32/windump.cpp @@ -19,59 +19,53 @@ #include <windows.h> #include <Dbghelp.h> -#include "windump.h" -namespace pvpgn +void make_minidump(struct _EXCEPTION_POINTERS* e) { - namespace bnetd + auto hDbgHelp = LoadLibraryA("dbghelp"); + if (hDbgHelp == nullptr) + return; + auto pMiniDumpWriteDump = (decltype(&MiniDumpWriteDump))GetProcAddress(hDbgHelp, "MiniDumpWriteDump"); + if (pMiniDumpWriteDump == nullptr) + return; + + char name[MAX_PATH]; { - void make_minidump(struct _EXCEPTION_POINTERS* e) - { - auto hDbgHelp = LoadLibraryA("dbghelp"); - if (hDbgHelp == nullptr) - return; - auto pMiniDumpWriteDump = (decltype(&MiniDumpWriteDump))GetProcAddress(hDbgHelp, "MiniDumpWriteDump"); - if (pMiniDumpWriteDump == nullptr) - return; - - char name[MAX_PATH]; - { - auto nameEnd = name + GetModuleFileNameA(GetModuleHandleA(0), name, MAX_PATH); - SYSTEMTIME t; - GetSystemTime(&t); - wsprintfA(nameEnd - strlen(".exe"), - "_%4d%02d%02d_%02d%02d%02d.dmp", - t.wYear, t.wMonth, t.wDay, t.wHour, t.wMinute, t.wSecond); - } - - auto hFile = CreateFileA(name, GENERIC_WRITE, FILE_SHARE_READ, 0, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0); - if (hFile == INVALID_HANDLE_VALUE) - return; - - MINIDUMP_EXCEPTION_INFORMATION exceptionInfo; - exceptionInfo.ThreadId = GetCurrentThreadId(); - exceptionInfo.ExceptionPointers = e; - exceptionInfo.ClientPointers = FALSE; - - auto dumped = pMiniDumpWriteDump( - GetCurrentProcess(), - GetCurrentProcessId(), - hFile, - MINIDUMP_TYPE(MiniDumpWithIndirectlyReferencedMemory | MiniDumpScanMemory), - e ? &exceptionInfo : nullptr, - nullptr, - nullptr); - - CloseHandle(hFile); - - return; - } - - LONG WINAPI unhandled_handler(struct _EXCEPTION_POINTERS* e) - { - make_minidump(e); - return EXCEPTION_CONTINUE_SEARCH; - } + auto nameEnd = name + GetModuleFileNameA(GetModuleHandleA(0), name, MAX_PATH); + SYSTEMTIME t; + GetSystemTime(&t); + wsprintfA(nameEnd - strlen(".exe"), + "_%4d%02d%02d_%02d%02d%02d.dmp", + t.wYear, t.wMonth, t.wDay, t.wHour, t.wMinute, t.wSecond); } + + auto hFile = CreateFileA(name, GENERIC_WRITE, FILE_SHARE_READ, 0, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0); + if (hFile == INVALID_HANDLE_VALUE) + return; + + MINIDUMP_EXCEPTION_INFORMATION exceptionInfo; + exceptionInfo.ThreadId = GetCurrentThreadId(); + exceptionInfo.ExceptionPointers = e; + exceptionInfo.ClientPointers = FALSE; + + auto dumped = pMiniDumpWriteDump( + GetCurrentProcess(), + GetCurrentProcessId(), + hFile, + MINIDUMP_TYPE(MiniDumpWithIndirectlyReferencedMemory | MiniDumpScanMemory), + e ? &exceptionInfo : nullptr, + nullptr, + nullptr); + + CloseHandle(hFile); + + return; } + +LONG WINAPI unhandled_handler(struct _EXCEPTION_POINTERS* e) +{ + make_minidump(e); + return EXCEPTION_CONTINUE_SEARCH; +} + #endif diff --git a/src/win32/windump.h b/src/win32/windump.h index 6e9127d..6a683f2 100644 --- a/src/win32/windump.h +++ b/src/win32/windump.h @@ -16,13 +16,8 @@ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ #ifdef WIN32 +#include "windows.h"" -namespace pvpgn -{ - namespace bnetd - { - LONG WINAPI unhandled_handler(struct _EXCEPTION_POINTERS* e); - } -} +LONG WINAPI unhandled_handler(struct _EXCEPTION_POINTERS* e); #endif