Merge pull request #2190 from mc-server/CrashDumpArgs
Added cmdline params for crashdump detailness.
This commit is contained in:
commit
9c0bb6132e
@ -1 +1 @@
|
|||||||
MCServer /cdf
|
MCServer --crash-dump-full
|
||||||
|
@ -1 +1 @@
|
|||||||
MCServer /cdg
|
MCServer --crash-dump-globals
|
||||||
|
171
src/main.cpp
171
src/main.cpp
@ -39,7 +39,7 @@ bool cRoot::m_RunAsService = false;
|
|||||||
|
|
||||||
|
|
||||||
#if defined(_WIN32)
|
#if defined(_WIN32)
|
||||||
SERVICE_STATUS_HANDLE g_StatusHandle = NULL;
|
SERVICE_STATUS_HANDLE g_StatusHandle = nullptr;
|
||||||
HANDLE g_ServiceThread = INVALID_HANDLE_VALUE;
|
HANDLE g_ServiceThread = INVALID_HANDLE_VALUE;
|
||||||
#define SERVICE_NAME "MCServerService"
|
#define SERVICE_NAME "MCServerService"
|
||||||
#endif
|
#endif
|
||||||
@ -145,7 +145,7 @@ Its purpose is to create the crashdump using the dbghlp DLLs
|
|||||||
*/
|
*/
|
||||||
LONG WINAPI LastChanceExceptionFilter(__in struct _EXCEPTION_POINTERS * a_ExceptionInfo)
|
LONG WINAPI LastChanceExceptionFilter(__in struct _EXCEPTION_POINTERS * a_ExceptionInfo)
|
||||||
{
|
{
|
||||||
char * newStack = &g_ExceptionStack[sizeof(g_ExceptionStack)];
|
char * newStack = &g_ExceptionStack[sizeof(g_ExceptionStack) - 1];
|
||||||
char * oldStack;
|
char * oldStack;
|
||||||
|
|
||||||
// Use the substitute stack:
|
// Use the substitute stack:
|
||||||
@ -249,6 +249,7 @@ void universalMain(std::unique_ptr<cSettingsRepositoryInterface> overridesRepo)
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#if defined(_WIN32)
|
#if defined(_WIN32)
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
// serviceWorkerThread: Keep the service alive
|
// serviceWorkerThread: Keep the service alive
|
||||||
@ -266,6 +267,7 @@ DWORD WINAPI serviceWorkerThread(LPVOID lpParam)
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
// serviceSetState: Set the internal status of the service
|
// serviceSetState: Set the internal status of the service
|
||||||
|
|
||||||
@ -319,14 +321,10 @@ void WINAPI serviceCtrlHandler(DWORD CtrlCode)
|
|||||||
|
|
||||||
void WINAPI serviceMain(DWORD argc, TCHAR *argv[])
|
void WINAPI serviceMain(DWORD argc, TCHAR *argv[])
|
||||||
{
|
{
|
||||||
#if defined(_DEBUG) && defined(DEBUG_SERVICE_STARTUP)
|
|
||||||
Sleep(10000);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
char applicationFilename[MAX_PATH];
|
char applicationFilename[MAX_PATH];
|
||||||
char applicationDirectory[MAX_PATH];
|
char applicationDirectory[MAX_PATH];
|
||||||
|
|
||||||
GetModuleFileName(NULL, applicationFilename, sizeof(applicationFilename)); // This binary's file path.
|
GetModuleFileName(nullptr, applicationFilename, sizeof(applicationFilename)); // This binary's file path.
|
||||||
|
|
||||||
// Strip off the filename, keep only the path:
|
// Strip off the filename, keep only the path:
|
||||||
strncpy_s(applicationDirectory, sizeof(applicationDirectory), applicationFilename, (strrchr(applicationFilename, '\\') - applicationFilename));
|
strncpy_s(applicationDirectory, sizeof(applicationDirectory), applicationFilename, (strrchr(applicationFilename, '\\') - applicationFilename));
|
||||||
@ -337,8 +335,7 @@ void WINAPI serviceMain(DWORD argc, TCHAR *argv[])
|
|||||||
SetCurrentDirectory(applicationDirectory);
|
SetCurrentDirectory(applicationDirectory);
|
||||||
|
|
||||||
g_StatusHandle = RegisterServiceCtrlHandler(SERVICE_NAME, serviceCtrlHandler);
|
g_StatusHandle = RegisterServiceCtrlHandler(SERVICE_NAME, serviceCtrlHandler);
|
||||||
|
if (g_StatusHandle == nullptr)
|
||||||
if (g_StatusHandle == NULL)
|
|
||||||
{
|
{
|
||||||
OutputDebugStringA("RegisterServiceCtrlHandler() failed\n");
|
OutputDebugStringA("RegisterServiceCtrlHandler() failed\n");
|
||||||
serviceSetState(0, SERVICE_STOPPED, GetLastError());
|
serviceSetState(0, SERVICE_STOPPED, GetLastError());
|
||||||
@ -347,8 +344,9 @@ void WINAPI serviceMain(DWORD argc, TCHAR *argv[])
|
|||||||
|
|
||||||
serviceSetState(SERVICE_ACCEPT_STOP, SERVICE_RUNNING, 0);
|
serviceSetState(SERVICE_ACCEPT_STOP, SERVICE_RUNNING, 0);
|
||||||
|
|
||||||
g_ServiceThread = CreateThread(NULL, 0, serviceWorkerThread, NULL, 0, NULL);
|
DWORD ThreadID;
|
||||||
if (g_ServiceThread == NULL)
|
g_ServiceThread = CreateThread(nullptr, 0, serviceWorkerThread, nullptr, 0, &ThreadID);
|
||||||
|
if (g_ServiceThread == nullptr)
|
||||||
{
|
{
|
||||||
OutputDebugStringA("CreateThread() failed\n");
|
OutputDebugStringA("CreateThread() failed\n");
|
||||||
serviceSetState(0, SERVICE_STOPPED, GetLastError());
|
serviceSetState(0, SERVICE_STOPPED, GetLastError());
|
||||||
@ -364,50 +362,39 @@ void WINAPI serviceMain(DWORD argc, TCHAR *argv[])
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
std::unique_ptr<cMemorySettingsRepository> parseArguments(int argc, char **argv)
|
std::unique_ptr<cMemorySettingsRepository> parseArguments(int argc, char **argv)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
// Parse the comand line args:
|
||||||
TCLAP::CmdLine cmd("MCServer");
|
TCLAP::CmdLine cmd("MCServer");
|
||||||
|
TCLAP::ValueArg<int> slotsArg ("s", "max-players", "Maximum number of slots for the server to use, overrides setting in setting.ini", false, -1, "number", cmd);
|
||||||
TCLAP::ValueArg<int> slotsArg("s", "max-players", "Maximum number of slots for the server to use, overrides setting in setting.ini", false, -1, "number", cmd);
|
TCLAP::MultiArg<int> portsArg ("p", "port", "The port number the server should listen to", false, "port", cmd);
|
||||||
|
TCLAP::SwitchArg commLogArg ("", "log-comm", "Log server client communications to file", cmd);
|
||||||
TCLAP::MultiArg<int> portsArg("p", "port", "The port number the server should listen to", false, "port", cmd);
|
TCLAP::SwitchArg commLogInArg ("", "log-comm-in", "Log inbound server client communications to file", cmd);
|
||||||
|
TCLAP::SwitchArg commLogOutArg ("", "log-comm-out", "Log outbound server client communications to file", cmd);
|
||||||
TCLAP::SwitchArg commLogArg("", "log-comm", "Log server client communications to file", cmd);
|
TCLAP::SwitchArg crashDumpFull ("", "crash-dump-full", "Crashdumps created by the server will contain full server memory", cmd);
|
||||||
|
TCLAP::SwitchArg crashDumpGlobals("", "crash-dump-globals", "Crashdumps created by the server will contain the global variables' values", cmd);
|
||||||
TCLAP::SwitchArg commLogInArg("", "log-comm-in", "Log inbound server client communications to file", cmd);
|
TCLAP::SwitchArg noBufArg ("", "no-output-buffering", "Disable output buffering", cmd);
|
||||||
|
TCLAP::SwitchArg runAsServiceArg ("d", "run-as-service", "Run as a service on Windows", cmd);
|
||||||
TCLAP::SwitchArg commLogOutArg("", "log-comm-out", "Log outbound server client communications to file", cmd);
|
|
||||||
|
|
||||||
TCLAP::SwitchArg noBufArg("", "no-output-buffering", "Disable output buffering", cmd);
|
|
||||||
|
|
||||||
TCLAP::SwitchArg runAsServiceArg("d", "run-as-service", "Run as a service on Windows", cmd);
|
|
||||||
|
|
||||||
cmd.ignoreUnmatched(true);
|
|
||||||
|
|
||||||
cmd.parse(argc, argv);
|
cmd.parse(argc, argv);
|
||||||
|
|
||||||
|
// Copy the parsed args' values into a settings repository:
|
||||||
auto repo = cpp14::make_unique<cMemorySettingsRepository>();
|
auto repo = cpp14::make_unique<cMemorySettingsRepository>();
|
||||||
|
|
||||||
if (slotsArg.isSet())
|
if (slotsArg.isSet())
|
||||||
{
|
{
|
||||||
|
|
||||||
int slots = slotsArg.getValue();
|
int slots = slotsArg.getValue();
|
||||||
|
|
||||||
repo->AddValue("Server", "MaxPlayers", static_cast<Int64>(slots));
|
repo->AddValue("Server", "MaxPlayers", static_cast<Int64>(slots));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (portsArg.isSet())
|
if (portsArg.isSet())
|
||||||
{
|
{
|
||||||
std::vector<int> ports = portsArg.getValue();
|
for (auto port: portsArg.getValue())
|
||||||
for (auto port : ports)
|
|
||||||
{
|
{
|
||||||
repo->AddValue("Server", "Port", static_cast<Int64>(port));
|
repo->AddValue("Server", "Port", static_cast<Int64>(port));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (commLogArg.getValue())
|
if (commLogArg.getValue())
|
||||||
{
|
{
|
||||||
g_ShouldLogCommIn = true;
|
g_ShouldLogCommIn = true;
|
||||||
@ -418,115 +405,111 @@ std::unique_ptr<cMemorySettingsRepository> parseArguments(int argc, char **argv)
|
|||||||
g_ShouldLogCommIn = commLogInArg.getValue();
|
g_ShouldLogCommIn = commLogInArg.getValue();
|
||||||
g_ShouldLogCommOut = commLogOutArg.getValue();
|
g_ShouldLogCommOut = commLogOutArg.getValue();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (noBufArg.getValue())
|
if (noBufArg.getValue())
|
||||||
{
|
{
|
||||||
setvbuf(stdout, nullptr, _IONBF, 0);
|
setvbuf(stdout, nullptr, _IONBF, 0);
|
||||||
}
|
}
|
||||||
|
repo->SetReadOnly();
|
||||||
|
|
||||||
|
// Set the service flag directly to cRoot:
|
||||||
if (runAsServiceArg.getValue())
|
if (runAsServiceArg.getValue())
|
||||||
{
|
{
|
||||||
cRoot::m_RunAsService = true;
|
cRoot::m_RunAsService = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
repo->SetReadOnly();
|
// Apply the CrashDump flags for platforms that support them:
|
||||||
|
#if defined(_WIN32) && !defined(_WIN64) && defined(_MSC_VER) // 32-bit Windows app compiled in MSVC
|
||||||
|
if (crashDumpGlobals.getValue())
|
||||||
|
{
|
||||||
|
g_DumpFlags = static_cast<MINIDUMP_TYPE>(g_DumpFlags | MiniDumpWithDataSegs);
|
||||||
|
}
|
||||||
|
if (crashDumpFull.getValue())
|
||||||
|
{
|
||||||
|
g_DumpFlags = static_cast<MINIDUMP_TYPE>(g_DumpFlags | MiniDumpWithFullMemory);
|
||||||
|
}
|
||||||
|
#endif // 32-bit Windows app compiled in MSVC
|
||||||
|
|
||||||
return repo;
|
return repo;
|
||||||
}
|
}
|
||||||
catch (TCLAP::ArgException &e)
|
catch (const TCLAP::ArgException & exc)
|
||||||
{
|
{
|
||||||
printf("error reading command line %s for arg %s", e.error().c_str(), e.argId().c_str());
|
printf("Error reading command line %s for arg %s", exc.error().c_str(), exc.argId().c_str());
|
||||||
return cpp14::make_unique<cMemorySettingsRepository>();
|
return cpp14::make_unique<cMemorySettingsRepository>();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
// main:
|
// main:
|
||||||
|
|
||||||
int main(int argc, char **argv)
|
int main(int argc, char **argv)
|
||||||
{
|
{
|
||||||
|
|
||||||
#if defined(_MSC_VER) && defined(_DEBUG) && defined(ENABLE_LEAK_FINDER)
|
#if defined(_MSC_VER) && defined(_DEBUG) && defined(ENABLE_LEAK_FINDER)
|
||||||
InitLeakFinder();
|
InitLeakFinder();
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// Magic code to produce dump-files on Windows if the server crashes:
|
// Magic code to produce dump-files on Windows if the server crashes:
|
||||||
#if defined(_WIN32) && !defined(_WIN64) && defined(_MSC_VER)
|
#if defined(_WIN32) && !defined(_WIN64) && defined(_MSC_VER) // 32-bit Windows app compiled in MSVC
|
||||||
HINSTANCE hDbgHelp = LoadLibrary("DBGHELP.DLL");
|
HINSTANCE hDbgHelp = LoadLibrary("DBGHELP.DLL");
|
||||||
g_WriteMiniDump = (pMiniDumpWriteDump)GetProcAddress(hDbgHelp, "MiniDumpWriteDump");
|
g_WriteMiniDump = (pMiniDumpWriteDump)GetProcAddress(hDbgHelp, "MiniDumpWriteDump");
|
||||||
if (g_WriteMiniDump != nullptr)
|
if (g_WriteMiniDump != nullptr)
|
||||||
{
|
|
||||||
_snprintf_s(g_DumpFileName, ARRAYCOUNT(g_DumpFileName), _TRUNCATE, "crash_mcs_%x.dmp", GetCurrentProcessId());
|
|
||||||
SetUnhandledExceptionFilter(LastChanceExceptionFilter);
|
|
||||||
|
|
||||||
// Parse arguments for minidump flags:
|
|
||||||
for (int i = 0; i < argc; i++)
|
|
||||||
{
|
{
|
||||||
if (_stricmp(argv[i], "/cdg") == 0)
|
_snprintf_s(g_DumpFileName, ARRAYCOUNT(g_DumpFileName), _TRUNCATE, "crash_mcs_%x.dmp", GetCurrentProcessId());
|
||||||
{
|
SetUnhandledExceptionFilter(LastChanceExceptionFilter);
|
||||||
// Add globals to the dump
|
}
|
||||||
g_DumpFlags = (MINIDUMP_TYPE)(g_DumpFlags | MiniDumpWithDataSegs);
|
#endif // 32-bit Windows app compiled in MSVC
|
||||||
}
|
|
||||||
else if (_stricmp(argv[i], "/cdf") == 0)
|
|
||||||
{
|
|
||||||
// Add full memory to the dump (HUUUGE file)
|
|
||||||
g_DumpFlags = (MINIDUMP_TYPE)(g_DumpFlags | MiniDumpWithFullMemory);
|
|
||||||
}
|
|
||||||
} // for i - argv[]
|
|
||||||
}
|
|
||||||
#endif // _WIN32 && !_WIN64
|
|
||||||
// End of dump-file magic
|
// End of dump-file magic
|
||||||
|
|
||||||
|
|
||||||
#if defined(_DEBUG) && defined(_MSC_VER)
|
#if defined(_DEBUG) && defined(_MSC_VER)
|
||||||
_CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
|
_CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
|
||||||
|
|
||||||
// _X: The simple built-in CRT leak finder - simply break when allocating the Nth block ({N} is listed in the leak output)
|
// _X: The simple built-in CRT leak finder - simply break when allocating the Nth block ({N} is listed in the leak output)
|
||||||
// Only useful when the leak is in the same sequence all the time
|
// Only useful when the leak is in the same sequence all the time
|
||||||
// _CrtSetBreakAlloc(85950);
|
// _CrtSetBreakAlloc(85950);
|
||||||
|
|
||||||
#endif // _DEBUG && _MSC_VER
|
#endif // _DEBUG && _MSC_VER
|
||||||
|
|
||||||
#ifndef _DEBUG
|
#ifndef _DEBUG
|
||||||
std::signal(SIGSEGV, NonCtrlHandler);
|
std::signal(SIGSEGV, NonCtrlHandler);
|
||||||
std::signal(SIGTERM, NonCtrlHandler);
|
std::signal(SIGTERM, NonCtrlHandler);
|
||||||
std::signal(SIGINT, NonCtrlHandler);
|
std::signal(SIGINT, NonCtrlHandler);
|
||||||
std::signal(SIGABRT, NonCtrlHandler);
|
std::signal(SIGABRT, NonCtrlHandler);
|
||||||
#ifdef SIGABRT_COMPAT
|
#ifdef SIGABRT_COMPAT
|
||||||
std::signal(SIGABRT_COMPAT, NonCtrlHandler);
|
std::signal(SIGABRT_COMPAT, NonCtrlHandler);
|
||||||
#endif // SIGABRT_COMPAT
|
#endif // SIGABRT_COMPAT
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// DEBUG: test the dumpfile creation:
|
|
||||||
// *((int *)0) = 0;
|
|
||||||
|
|
||||||
auto argsRepo = parseArguments(argc, argv);
|
auto argsRepo = parseArguments(argc, argv);
|
||||||
|
|
||||||
#if defined(_WIN32)
|
#if defined(_WIN32)
|
||||||
// Attempt to run as a service
|
// Attempt to run as a service
|
||||||
if (cRoot::m_RunAsService)
|
if (cRoot::m_RunAsService)
|
||||||
{
|
|
||||||
SERVICE_TABLE_ENTRY ServiceTable[] =
|
|
||||||
{
|
{
|
||||||
{ SERVICE_NAME, (LPSERVICE_MAIN_FUNCTION)serviceMain },
|
SERVICE_TABLE_ENTRY ServiceTable[] =
|
||||||
{ NULL, NULL }
|
{
|
||||||
};
|
{ SERVICE_NAME, (LPSERVICE_MAIN_FUNCTION)serviceMain },
|
||||||
|
{ nullptr, nullptr }
|
||||||
|
};
|
||||||
|
|
||||||
if (StartServiceCtrlDispatcher(ServiceTable) == FALSE)
|
if (StartServiceCtrlDispatcher(ServiceTable) == FALSE)
|
||||||
{
|
{
|
||||||
LOGERROR("Attempted, but failed, service startup.");
|
LOGERROR("Attempted, but failed, service startup.");
|
||||||
return GetLastError();
|
return GetLastError();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
else
|
||||||
else
|
|
||||||
#endif
|
#endif
|
||||||
{
|
{
|
||||||
// Not running as a service, do normal startup
|
// Not running as a Windows service, do normal startup
|
||||||
universalMain(std::move(argsRepo));
|
universalMain(std::move(argsRepo));
|
||||||
}
|
}
|
||||||
|
|
||||||
#if defined(_MSC_VER) && defined(_DEBUG) && defined(ENABLE_LEAK_FINDER)
|
#if defined(_MSC_VER) && defined(_DEBUG) && defined(ENABLE_LEAK_FINDER)
|
||||||
DeinitLeakFinder();
|
DeinitLeakFinder();
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
return EXIT_SUCCESS;
|
return EXIT_SUCCESS;
|
||||||
|
Loading…
Reference in New Issue
Block a user