Skip to content
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

[lldb] add support for thread names on Windows #74731

Merged
merged 3 commits into from
Dec 21, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -33,6 +34,9 @@
using namespace lldb;
using namespace lldb_private;

using GetThreadDescriptionFunctionPtr = HRESULT
WINAPI (*)(HANDLE hThread, PWSTR *ppszThreadDescription);

oltolm marked this conversation as resolved.
Show resolved Hide resolved
TargetThreadWindows::TargetThreadWindows(ProcessWindows &process,
const HostThread &thread)
: Thread(process, thread.GetNativeThread().GetThreadId()),
Expand Down Expand Up @@ -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 =
Copy link
Member

Choose a reason for hiding this comment

The 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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

reinterpret_cast<GetThreadDescriptionFunctionPtr>(
::GetProcAddress(hModule, "GetThreadDescription"));
Copy link
Collaborator

Choose a reason for hiding this comment

The 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 #include <processthreadsapi.h> and then call the function directly?

Copy link

@Fulgen301 Fulgen301 Dec 7, 2023

Choose a reason for hiding this comment

The 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:

Windows Server 2016, Windows 10 LTSB 2016 and Windows 10 version 1607: GetThreadDescription is only available by Run Time Dynamic Linking in KernelBase.dll.

Copy link
Member

Choose a reason for hiding this comment

The 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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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.

Copy link
Member

Choose a reason for hiding this comment

The 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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An early out here would be nice to reduce indentation.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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();
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class TargetThreadWindows : public lldb_private::Thread {
lldb::RegisterContextSP
CreateRegisterContextForFrame(StackFrame *frame) override;
bool CalculateStopInfo() override;
const char *GetName() override;

Status DoResume();

Expand All @@ -42,6 +43,7 @@ class TargetThreadWindows : public lldb_private::Thread {
private:
lldb::RegisterContextSP m_thread_reg_ctx_sp;
HostThread m_host_thread;
std::string m_name;
};
} // namespace lldb_private

Expand Down