-
Notifications
You must be signed in to change notification settings - Fork 14.5k
[lldb] add support for thread names on Windows #74731
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,7 @@ | |
#include "lldb/Utility/LLDBLog.h" | ||
#include "lldb/Utility/Log.h" | ||
#include "lldb/Utility/State.h" | ||
#include "llvm/Support/ConvertUTF.h" | ||
|
||
#include "ProcessWindows.h" | ||
#include "ProcessWindowsLog.h" | ||
|
@@ -33,6 +34,9 @@ | |
using namespace lldb; | ||
using namespace lldb_private; | ||
|
||
using GetThreadDescriptionFunctionPtr = HRESULT | ||
WINAPI (*)(HANDLE hThread, PWSTR *ppszThreadDescription); | ||
|
||
TargetThreadWindows::TargetThreadWindows(ProcessWindows &process, | ||
const HostThread &thread) | ||
: Thread(process, thread.GetNativeThread().GetThreadId()), | ||
|
@@ -175,3 +179,30 @@ Status TargetThreadWindows::DoResume() { | |
|
||
return Status(); | ||
} | ||
|
||
const char *TargetThreadWindows::GetName() { | ||
Log *log = GetLog(LLDBLog::Thread); | ||
HMODULE hModule = ::LoadLibraryW(L"Kernel32.dll"); | ||
if (hModule) { | ||
auto GetThreadDescription = | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be pulled out into a lambda and store the lookup rather than re-evaluating it each time. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
reinterpret_cast<GetThreadDescriptionFunctionPtr>( | ||
::GetProcAddress(hModule, "GetThreadDescription")); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This code is only compiled on windows, why do we need to get the library handle manually and get the function pointer. Can't we just There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The function was added in Windows 10 1607, which was also the only build (excluding Server 2016) where it isn't available in the header: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If there is compatibility that we are concerned about, I think that we should consider falling back to more ... esoteric solutions. __try {
RaiseException(MS_VC_EXCEPTION, 0, sizeof(info) / sizeof(ULONG_PTR), (ULONG_PTR*)&info);
} __except (EXCEPTION_EXECUTE_HANDLER) {
} Should be far more portable and is what VS also uses. This is documented at https://learn.microsoft.com/en-us/previous-versions/visualstudio/visual-studio-2015/debugger/how-to-set-a-thread-name-in-native-code?view=vs-2015&redirectedfrom=MSDN. I would be okay with also raising the requirements to a newer version of Windows as 1607 is RS1 which makes it more than 8 years old at this point. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You are talking about setting thread names, but I implemented getting a thread name from Windows. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is the set name below though. |
||
LLDB_LOGF(log, "GetProcAddress: %p", | ||
reinterpret_cast<void *>(GetThreadDescription)); | ||
if (GetThreadDescription) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. An early out here would be nice to reduce indentation. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
PWSTR pszThreadName; | ||
if (SUCCEEDED(GetThreadDescription( | ||
m_host_thread.GetNativeThread().GetSystemHandle(), | ||
&pszThreadName))) { | ||
LLDB_LOGF(log, "GetThreadDescription: %ls", pszThreadName); | ||
llvm::convertUTF16ToUTF8String( | ||
llvm::ArrayRef(reinterpret_cast<char *>(pszThreadName), | ||
wcslen(pszThreadName) * sizeof(wchar_t)), | ||
m_name); | ||
::LocalFree(pszThreadName); | ||
} | ||
} | ||
} | ||
|
||
return m_name.c_str(); | ||
} |
Uh oh!
There was an error while loading. Please reload this page.