sal/osl/w32/thread.cxx | 32 +++++++++++++++++++++++++------- 1 file changed, 25 insertions(+), 7 deletions(-)
New commits: commit a5eba5fef0b6eade2c0a39f8e8fc90bdeb08dff2 Author: Mike Kaganski <mike.kagan...@collabora.com> AuthorDate: Wed Jun 2 11:05:21 2021 +0200 Commit: Mike Kaganski <mike.kagan...@collabora.com> CommitDate: Wed Jun 2 15:27:09 2021 +0200 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.kagan...@collabora.com> 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 { _______________________________________________ Libreoffice-commits mailing list libreoffice-comm...@lists.freedesktop.org https://lists.freedesktop.org/mailman/listinfo/libreoffice-commits