[Lldb-commits] [lldb] lldb: add support for thread names on Windows (PR #74731)

via lldb-commits lldb-commits at lists.llvm.org
Thu Dec 7 08:25:36 PST 2023


https://github.com/oltolm updated https://github.com/llvm/llvm-project/pull/74731

>From 9383b8b92849c71a96b4b4e7e55f615d8f2efedb Mon Sep 17 00:00:00 2001
From: oltolm <oleg.tolmatcev at gmail.com>
Date: Fri, 1 Dec 2023 16:49:13 +0100
Subject: [PATCH] lldb: add support for thread names on Windows

---
 .../Windows/Common/TargetThreadWindows.cpp    | 31 +++++++++++++++++++
 .../Windows/Common/TargetThreadWindows.h      |  2 ++
 2 files changed, 33 insertions(+)

diff --git a/lldb/source/Plugins/Process/Windows/Common/TargetThreadWindows.cpp b/lldb/source/Plugins/Process/Windows/Common/TargetThreadWindows.cpp
index 37dc8f6d6d14a..047019ac30f49 100644
--- a/lldb/source/Plugins/Process/Windows/Common/TargetThreadWindows.cpp
+++ b/lldb/source/Plugins/Process/Windows/Common/TargetThreadWindows.cpp
@@ -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 =
+        reinterpret_cast<GetThreadDescriptionFunctionPtr>(
+            ::GetProcAddress(hModule, "GetThreadDescription"));
+    LLDB_LOGF(log, "GetProcAddress: %p",
+              reinterpret_cast<void *>(GetThreadDescription));
+    if (GetThreadDescription) {
+      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();
+}
diff --git a/lldb/source/Plugins/Process/Windows/Common/TargetThreadWindows.h b/lldb/source/Plugins/Process/Windows/Common/TargetThreadWindows.h
index 2845847738f60..07e1db464ad59 100644
--- a/lldb/source/Plugins/Process/Windows/Common/TargetThreadWindows.h
+++ b/lldb/source/Plugins/Process/Windows/Common/TargetThreadWindows.h
@@ -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();
 
@@ -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
 



More information about the lldb-commits mailing list