From 81413839a8c02815442495c09848ddd24678aff4 Mon Sep 17 00:00:00 2001 From: Graeme Kelly Date: Fri, 11 Jan 2019 18:00:35 +0000 Subject: [PATCH] call SetThreadDescription() to set thread name if available (#200) * Update thread.cpp call SetThreadDescription() (if available) when setting the thread name, so that it is available in minidumps and to any process that has permission to call GetThreadDescription (as opposed to just being available inside the debugger) * Update thread.cpp whitespace fix * Update thread.cpp another bit of whitespace, I've become so blind to this * Update thread.cpp whoops - would help if it actually compiled * Update thread.cpp actually this one was unnecessary - already checked above as first #if condition * Update thread.cpp change to use GetModuleHandleA --- src/thread.cpp | 67 +++++++++++++++++++++++++++++++------------------- 1 file changed, 42 insertions(+), 25 deletions(-) diff --git a/src/thread.cpp b/src/thread.cpp index f52e0bd..a0d4c89 100644 --- a/src/thread.cpp +++ b/src/thread.cpp @@ -8,6 +8,10 @@ #if BX_CONFIG_SUPPORTS_THREADING +#if BX_PLATFORM_WINDOWS && !BX_CRT_NONE +# include +#endif + #if BX_CRT_NONE # include "crt0.h" #elif BX_PLATFORM_ANDROID \ @@ -240,33 +244,46 @@ namespace bx # else pthread_set_name_np(ti->m_handle, _name); # endif // defined(__NetBSD__) -#elif BX_PLATFORM_WINDOWS && BX_COMPILER_MSVC -# pragma pack(push, 8) - struct ThreadName +#elif BX_PLATFORM_WINDOWS + // Try to use the new thread naming API from Win10 Creators update onwards if we have it + typedef HRESULT (WINAPI *SetThreadDescriptionProc)(HANDLE, PCWSTR); + SetThreadDescriptionProc SetThreadDescription = (SetThreadDescriptionProc)(GetProcAddress(GetModuleHandleA("Kernel32.dll"), "SetThreadDescription")); + if (SetThreadDescription) { - DWORD type; - LPCSTR name; - DWORD id; - DWORD flags; - }; -# pragma pack(pop) - ThreadName tn; - tn.type = 0x1000; - tn.name = _name; - tn.id = ti->m_threadId; - tn.flags = 0; + uint32_t length = (uint32_t)bx::strLen(_name)+1; + uint32_t size = length*sizeof(wchar_t); + wchar_t* name = (wchar_t*)alloca(size); + mbstowcs(name, _name, size-2); + SetThreadDescription(ti->m_handle, name); + } +# if BX_COMPILER_MSVC +# pragma pack(push, 8) + struct ThreadName + { + DWORD type; + LPCSTR name; + DWORD id; + DWORD flags; + }; +# pragma pack(pop) + ThreadName tn; + tn.type = 0x1000; + tn.name = _name; + tn.id = ti->m_threadId; + tn.flags = 0; - __try - { - RaiseException(0x406d1388 - , 0 - , sizeof(tn)/4 - , reinterpret_cast(&tn) - ); - } - __except(EXCEPTION_EXECUTE_HANDLER) - { - } + __try + { + RaiseException(0x406d1388 + , 0 + , sizeof(tn)/4 + , reinterpret_cast(&tn) + ); + } + __except(EXCEPTION_EXECUTE_HANDLER) + { + } +# endif // BX_COMPILER_MSVC #else BX_UNUSED(_name); #endif // BX_PLATFORM_