From 94d0aa193d33f591e89c48ba04e7b53e90fccc59 Mon Sep 17 00:00:00 2001 From: Shengliang Guan Date: Wed, 12 Aug 2020 14:44:09 +0800 Subject: [PATCH] TD-1057 set crash in windows --- src/os/src/windows/w64Env.c | 1 + src/os/src/windows/w64Sysinfo.c | 86 +++++++++++++++++++++++---------- 2 files changed, 61 insertions(+), 26 deletions(-) diff --git a/src/os/src/windows/w64Env.c b/src/os/src/windows/w64Env.c index 57d34d4003..5544c4ba39 100644 --- a/src/os/src/windows/w64Env.c +++ b/src/os/src/windows/w64Env.c @@ -21,6 +21,7 @@ extern void taosWinSocketInit(); void osInit() { + taosSetCoreDump(); if (configDir[0] == 0) { strcpy(configDir, "C:/TDengine/cfg"); } diff --git a/src/os/src/windows/w64Sysinfo.c b/src/os/src/windows/w64Sysinfo.c index 70abe939b2..eb252a0b86 100644 --- a/src/os/src/windows/w64Sysinfo.c +++ b/src/os/src/windows/w64Sysinfo.c @@ -16,21 +16,23 @@ #define _DEFAULT_SOURCE #include "os.h" #include "taosdef.h" -#include "tglobal.h" #include "tconfig.h" +#include "tglobal.h" #include "ttimer.h" #include "tulog.h" #include "tutil.h" #if (_WIN64) -#include #include -#include -#include -#include #include +#include +#include +#include +#include #pragma comment(lib, "Mswsock.lib ") #endif +#include + static void taosGetSystemTimezone() { // get and set default timezone SGlobalCfg *cfg_timezone = taosGetConfigOption("timezone"); @@ -52,7 +54,7 @@ static void taosGetSystemLocale() { if (cfg_locale && cfg_locale->cfgStatus < TAOS_CFG_CSTATUS_DEFAULT) { char *locale = setlocale(LC_CTYPE, "chs"); if (locale != NULL) { - tstrncpy(tsLocale, locale, TSDB_LOCALE_LEN);; + tstrncpy(tsLocale, locale, TSDB_LOCALE_LEN); cfg_locale->cfgStatus = TAOS_CFG_CSTATUS_DEFAULT; uInfo("locale not configured, set to default:%s", tsLocale); } @@ -79,7 +81,7 @@ void taosGetSystemInfo() { } bool taosGetDisk() { - const double unit = 1024 * 1024 * 1024; + const double unit = 1024 * 1024 * 1024; BOOL fResult; unsigned _int64 i64FreeBytesToCaller; unsigned _int64 i64TotalBytes; @@ -88,15 +90,15 @@ bool taosGetDisk() { int drive_type; if (tscEmbedded) { - drive_type = GetDriveTypeA(dir); + drive_type = GetDriveTypeA(dir); if (drive_type == DRIVE_FIXED) { - fResult = GetDiskFreeSpaceExA(dir, (PULARGE_INTEGER)&i64FreeBytesToCaller, (PULARGE_INTEGER)&i64TotalBytes, - (PULARGE_INTEGER)&i64FreeBytes); - if (fResult) { - tsTotalDataDirGB = tsTotalLogDirGB = tsTotalTmpDirGB = (float)(i64TotalBytes / unit); - tsAvailDataDirGB = tsAvailLogDirGB = tsAvailTmpDirectorySpace = (float)(i64FreeBytes / unit); - } - } + fResult = GetDiskFreeSpaceExA(dir, (PULARGE_INTEGER)&i64FreeBytesToCaller, (PULARGE_INTEGER)&i64TotalBytes, + (PULARGE_INTEGER)&i64FreeBytes); + if (fResult) { + tsTotalDataDirGB = tsTotalLogDirGB = tsTotalTmpDirGB = (float)(i64TotalBytes / unit); + tsAvailDataDirGB = tsAvailLogDirGB = tsAvailTmpDirectorySpace = (float)(i64FreeBytes / unit); + } + } } return true; } @@ -172,10 +174,12 @@ bool taosGetSysMemory(float *memoryUsedMB) { float nMemTotal; memsStat.dwLength = sizeof(memsStat); - if (!GlobalMemoryStatusEx(&memsStat)) { return false; } + if (!GlobalMemoryStatusEx(&memsStat)) { + return false; + } nMemFree = memsStat.ullAvailPhys / (1024.0f * 1024.0f); nMemTotal = memsStat.ullTotalPhys / (1024.0f * 1024.0f); - *memoryUsedMB = nMemTotal - nMemFree; + *memoryUsedMB = nMemTotal - nMemFree; return true; } @@ -184,16 +188,46 @@ int taosSystem(const char *cmd) { return -1; } -int flock(int fd, int option) { - return 0; -} +int flock(int fd, int option) { return 0; } -int fsync(int filedes) { - return 0; -} +int fsync(int filedes) { return 0; } + +int sigaction(int sig, struct sigaction *d, void *p) { return 0; } + +LONG WINAPI FlCrashDump(PEXCEPTION_POINTERS ep) { + typedef BOOL(WINAPI * FxMiniDumpWriteDump)(IN HANDLE hProcess, IN DWORD ProcessId, IN HANDLE hFile, + IN MINIDUMP_TYPE DumpType, + IN CONST PMINIDUMP_EXCEPTION_INFORMATION ExceptionParam, + IN CONST PMINIDUMP_USER_STREAM_INFORMATION UserStreamParam, + IN CONST PMINIDUMP_CALLBACK_INFORMATION CallbackParam); + + HMODULE dll = LoadLibrary("dbghelp.dll"); + if (dll == NULL) return EXCEPTION_CONTINUE_SEARCH; + FxMiniDumpWriteDump mdwd = (FxMiniDumpWriteDump)(GetProcAddress(dll, "MiniDumpWriteDump")); + if (mdwd == NULL) { + FreeLibrary(dll); + return EXCEPTION_CONTINUE_SEARCH; + } + + TCHAR path[MAX_PATH]; + DWORD len = GetModuleFileName(NULL, path, _countof(path)); + path[len - 3] = 'd'; + path[len - 2] = 'm'; + path[len - 1] = 'p'; + + HANDLE file = CreateFile(path, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); + + MINIDUMP_EXCEPTION_INFORMATION mei; + mei.ThreadId = GetCurrentThreadId(); + mei.ExceptionPointers = ep; + mei.ClientPointers = FALSE; + + (*mdwd)(GetCurrentProcess(), GetCurrentProcessId(), file, MiniDumpWithHandleData, &mei, NULL, NULL); + + CloseHandle(file); + FreeLibrary(dll); -int sigaction(int sig, struct sigaction *d, void *p) { - return 0; + return EXCEPTION_CONTINUE_SEARCH; } -void taosSetCoreDump() {} \ No newline at end of file +void taosSetCoreDump() { SetUnhandledExceptionFilter(&FlCrashDump); } \ No newline at end of file -- GitLab