diff options
author | Mike Kaganski <mike.kaganski@collabora.com> | 2021-06-02 11:05:21 +0200 |
---|---|---|
committer | Mike Kaganski <mike.kaganski@collabora.com> | 2021-06-02 15:27:09 +0200 |
commit | a5eba5fef0b6eade2c0a39f8e8fc90bdeb08dff2 (patch) | |
tree | 2af4e1487840b74928cf88dfcaff53b121f0c591 | |
parent | f1be88688cf1ca4ab14f93ae1a75e0331a6577cb (diff) |
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 <mike.kaganski@collabora.com>
-rw-r--r-- | sal/osl/w32/thread.cxx | 32 |
1 files changed, 25 insertions, 7 deletions
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<TSetThreadDescription>( + GetProcAddress(GetModuleHandleA("KernelBase.dll"), "SetThreadDescription")); + if (pSetThreadDescription) + { + if (const int nReqCCh = MultiByteToWideChar(CP_ACP, 0, name, -1, nullptr, 0)) + { + if (PWSTR wStr = static_cast<PWSTR>(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 <http://msdn.microsoft.com/en-us/library/xcb2z8hs.aspx>: */ + /* 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<ULONG_PTR *>(&info)); } __except (EXCEPTION_EXECUTE_HANDLER) {} + + impSetThreadDescription(name); } namespace { |