From 4e1a8df50add9e1644ddfb16e610bd5be4d45c4a Mon Sep 17 00:00:00 2001 From: Bryan Roe Date: Tue, 15 Oct 2019 10:44:21 -0700 Subject: [PATCH] Updated Windows KVM, so spawned child process can write a core dump on crash, if parent process is configured to. --- meshcore/KVM/Windows/kvm.c | 67 +++++++++++++++++++++++++++++++++++--- meshservice/ServiceMain.c | 6 ++-- 2 files changed, 66 insertions(+), 7 deletions(-) diff --git a/meshcore/KVM/Windows/kvm.c b/meshcore/KVM/Windows/kvm.c index 6e90e20..f60946e 100644 --- a/meshcore/KVM/Windows/kvm.c +++ b/meshcore/KVM/Windows/kvm.c @@ -741,7 +741,7 @@ void kvm_server_SetResolution(ILibKVM_WriteHandler writeHandler, void *reserved) #define BUFSIZE 65535 #ifdef _WINSERVICE -DWORD WINAPI kvm_mainloopinput(LPVOID Param) +DWORD WINAPI kvm_mainloopinput_ex(LPVOID Param) { int ptr = 0; int ptr2 = 0; @@ -779,10 +779,33 @@ DWORD WINAPI kvm_mainloopinput(LPVOID Param) return 0; } + +DWORD WINAPI kvm_mainloopinput(LPVOID Param) +{ + DWORD ret = 0; + if (((int*)&(((void**)Param)[3]))[0] == 1) + { + ILib_DumpEnabledContext winException; + __try + { + ret = kvm_mainloopinput_ex(Param); + } + __except (ILib_WindowsExceptionFilterEx(GetExceptionCode(), GetExceptionInformation(), &winException)) + { + ILib_WindowsExceptionDebugEx(&winException); + } + } + else + { + ret = kvm_mainloopinput_ex(Param); + } + return(ret); +} #endif + // This is the main KVM pooling loop. It will look at the display and see if any changes occur. [Runs as daemon if Windows Service] -DWORD WINAPI kvm_server_mainloop(LPVOID parm) +DWORD WINAPI kvm_server_mainloop_ex(LPVOID parm) { //long cur_timestamp = 0; //long prev_timestamp = 0; @@ -846,6 +869,7 @@ DWORD WINAPI kvm_server_mainloop(LPVOID parm) #endif kvm_server_SetResolution(writeHandler, reserved); + #ifdef _WINSERVICE if (!kvmConsoleMode) { @@ -979,6 +1003,39 @@ DWORD WINAPI kvm_server_mainloop(LPVOID parm) return 0; } +DWORD WINAPI kvm_server_mainloop(LPVOID parm) +{ + DWORD ret = 0; + if (((int*)&(((void**)parm)[3]))[0] == 1) + { + // Enable Core Dump in KVM Child + ILib_DumpEnabledContext winException; + WCHAR str[_MAX_PATH]; + DWORD strLen; + if ((strLen = GetModuleFileNameW(NULL, str, _MAX_PATH)) > 5) + { + str[strLen - 4] = 0; // We're going to convert .exe to _kvm.dmp + g_ILibCrashDump_path = ILibMemory_Allocate((strLen * 2) + 10, 0, NULL, NULL); // Add enough space to add '.dmp' to the end of the path + swprintf_s((wchar_t*)g_ILibCrashDump_path, strLen + 5, L"%s_kvm.dmp", str); + } + + __try + { + ret = kvm_server_mainloop_ex(parm); + } + __except (ILib_WindowsExceptionFilterEx(GetExceptionCode(), GetExceptionInformation(), &winException)) + { + ILib_WindowsExceptionDebugEx(&winException); + } + } + else + { + // Core Dump not enabled in KVM Child + ret = kvm_server_mainloop_ex(parm); + } + return(ret); +} + #ifdef _WINSERVICE void kvm_relay_ExitHandler(ILibProcessPipe_Process sender, int exitCode, void* user) { @@ -1063,14 +1120,14 @@ void kvm_relay_StdErrHandler(ILibProcessPipe_Process sender, char *buffer, int b int kvm_relay_restart(int paused, void *pipeMgr, char *exePath, ILibKVM_WriteHandler writeHandler, void *reserved) { - char * parms0[] = { " -kvm0", NULL }; - char * parms1[] = { " -kvm1", NULL }; + char * parms0[] = { " -kvm0", g_ILibCrashDump_path != NULL ? "-coredump" : NULL, NULL }; + char * parms1[] = { " -kvm1", g_ILibCrashDump_path != NULL ? "-coredump" : NULL, NULL }; void **user = (void**)ILibMemory_Allocate(4 * sizeof(void*), 0, NULL, NULL); user[0] = writeHandler; user[1] = reserved; user[2] = pipeMgr; user[3] = exePath; - + KVMDEBUG("kvm_relay_restart / start", paused); // If we are re-launching the child process, wait a bit. The computer may be switching desktop, etc. diff --git a/meshservice/ServiceMain.c b/meshservice/ServiceMain.c index c824817..7abdf8c 100644 --- a/meshservice/ServiceMain.c +++ b/meshservice/ServiceMain.c @@ -1033,9 +1033,10 @@ int wmain(int argc, char* wargv[]) #if defined(_LINKVM) if (argc > 1 && strcasecmp(argv[1], "-kvm0") == 0) { - void **parm = (void**)ILibMemory_Allocate(3 * sizeof(void*), 0, 0, NULL); + void **parm = (void**)ILibMemory_Allocate(4 * sizeof(void*), 0, 0, NULL); parm[0] = kvm_serviceWriteSink; ((int*)&(parm[2]))[0] = 0; + ((int*)&(parm[3]))[0] = (argc > 2 && strcasecmp(argv[2], "-coredump") == 0) ? 1 : 0; HMODULE shCORE = LoadLibraryExA((LPCSTR)"Shcore.dll", NULL, LOAD_LIBRARY_SEARCH_SYSTEM32); DpiAwarenessFunc dpiAwareness = NULL; @@ -1064,9 +1065,10 @@ int wmain(int argc, char* wargv[]) } else if (argc > 1 && strcasecmp(argv[1], "-kvm1") == 0) { - void **parm = (void**)ILibMemory_Allocate(3 * sizeof(void*), 0, 0, NULL); + void **parm = (void**)ILibMemory_Allocate(4 * sizeof(void*), 0, 0, NULL); parm[0] = kvm_serviceWriteSink; ((int*)&(parm[2]))[0] = 1; + ((int*)&(parm[3]))[0] = (argc > 2 && strcasecmp(argv[2], "-coredump") == 0) ? 1 : 0; HMODULE shCORE = LoadLibraryExA((LPCSTR)"Shcore.dll", NULL, LOAD_LIBRARY_SEARCH_SYSTEM32); DpiAwarenessFunc dpiAwareness = NULL;