From a5eba5fef0b6eade2c0a39f8e8fc90bdeb08dff2 Mon Sep 17 00:00:00 2001 From: Mike Kaganski Date: Wed, 2 Jun 2021 11:05:21 +0200 Subject: Make optional use of SetThreadDescription in osl_setThreadName ... so that thread name could appear in crash dumps, and be potentially available for logging on Windows 10 version 1607 and later. Change-Id: I176ca1fce57e0532a226f85836b3889a8ffb2984 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/116462 Tested-by: Jenkins Reviewed-by: Mike Kaganski --- sal/osl/w32/thread.cxx | 32 +++++++++++++++++++++++++------- 1 file changed, 25 insertions(+), 7 deletions(-) (limited to 'sal') diff --git a/sal/osl/w32/thread.cxx b/sal/osl/w32/thread.cxx index d4a9d6d67060..e269e7cbd455 100644 --- a/sal/osl/w32/thread.cxx +++ b/sal/osl/w32/thread.cxx @@ -358,25 +358,43 @@ void SAL_CALL osl_yieldThread(void) Sleep(0); } +static void impSetThreadDescription(char const * name) { + // SetThreadDescription is only available since Windows 10 version 1607 + typedef HRESULT(WINAPI * TSetThreadDescription)(HANDLE, PCWSTR); + static const auto pSetThreadDescription = reinterpret_cast( + GetProcAddress(GetModuleHandleA("KernelBase.dll"), "SetThreadDescription")); + if (pSetThreadDescription) + { + if (const int nReqCCh = MultiByteToWideChar(CP_ACP, 0, name, -1, nullptr, 0)) + { + if (PWSTR wStr = static_cast(malloc(nReqCCh * sizeof(WCHAR)))) + { + if (MultiByteToWideChar(CP_ACP, 0, name, -1, wStr, nReqCCh)) + pSetThreadDescription(GetCurrentThread(), wStr); + free(wStr); + } + } + } +} + void SAL_CALL osl_setThreadName(char const * name) { - /* See : */ + /* See < https://docs.microsoft.com/en-us/visualstudio/debugger/how-to-set-a-thread-name-in-native-code >: */ #pragma pack(push, 8) struct { - DWORD dwType; + DWORD dwType = 0x1000; LPCSTR szName; - DWORD dwThreadID; - DWORD dwFlags; + DWORD dwThreadID = DWORD(-1); + DWORD dwFlags = 0; } info; #pragma pack(pop) - info.dwType = 0x1000; info.szName = name; - info.dwThreadID = DWORD(-1); - info.dwFlags = 0; __try { RaiseException( 0x406D1388, 0, sizeof info / sizeof (ULONG_PTR), reinterpret_cast(&info)); } __except (EXCEPTION_EXECUTE_HANDLER) {} + + impSetThreadDescription(name); } namespace { -- cgit