[Lldb-commits] [PATCH] D90298: [lldb] [Process/FreeBSDRemote] Implement thread GetName()

Michał Górny via Phabricator via lldb-commits lldb-commits at lists.llvm.org
Wed Oct 28 04:43:12 PDT 2020


mgorny created this revision.
mgorny added reviewers: labath, emaste, krytarowski.
Herald added a subscriber: arichardson.
mgorny requested review of this revision.

Implement NativeThreadFreeBSD::GetName().  This is based
on the equivalent code in the legacy FreeBSD plugin, except it is
modernized a bit to use llvm::Optional and std::vector for data storage.


https://reviews.llvm.org/D90298

Files:
  lldb/source/Plugins/Process/FreeBSDRemote/NativeThreadFreeBSD.cpp
  lldb/source/Plugins/Process/FreeBSDRemote/NativeThreadFreeBSD.h


Index: lldb/source/Plugins/Process/FreeBSDRemote/NativeThreadFreeBSD.h
===================================================================
--- lldb/source/Plugins/Process/FreeBSDRemote/NativeThreadFreeBSD.h
+++ lldb/source/Plugins/Process/FreeBSDRemote/NativeThreadFreeBSD.h
@@ -74,6 +74,7 @@
   using WatchpointIndexMap = std::map<lldb::addr_t, uint32_t>;
   WatchpointIndexMap m_watchpoint_index_map;
   WatchpointIndexMap m_hw_break_index_map;
+  llvm::Optional<std::string> m_thread_name;
 };
 
 typedef std::shared_ptr<NativeThreadFreeBSD> NativeThreadFreeBSDSP;
Index: lldb/source/Plugins/Process/FreeBSDRemote/NativeThreadFreeBSD.cpp
===================================================================
--- lldb/source/Plugins/Process/FreeBSDRemote/NativeThreadFreeBSD.cpp
+++ lldb/source/Plugins/Process/FreeBSDRemote/NativeThreadFreeBSD.cpp
@@ -22,9 +22,11 @@
 #include <sys/types.h>
 #include <sys/ptrace.h>
 #include <sys/sysctl.h>
+#include <sys/user.h>
 // clang-format on
 
 #include <sstream>
+#include <vector>
 
 using namespace lldb;
 using namespace lldb_private;
@@ -146,7 +148,44 @@
   m_stop_info.reason = StopReason::eStopReasonNone;
 }
 
-std::string NativeThreadFreeBSD::GetName() { return ""; }
+std::string NativeThreadFreeBSD::GetName() {
+  if (!m_thread_name) {
+    Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_THREAD));
+
+    std::vector<struct kinfo_proc> kp;
+    int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_PID | KERN_PROC_INC_THREAD,
+                  static_cast<int>(GetProcess().GetID())};
+
+    while (1) {
+      size_t len = kp.size() * sizeof(struct kinfo_proc);
+      void *ptr = len == 0 ? nullptr : kp.data();
+      int error = ::sysctl(mib, 4, ptr, &len, nullptr, 0);
+      if (ptr == nullptr || (error != 0 && errno == ENOMEM)) {
+        // Add extra space in case threads are added before next call.
+        kp.resize((len / sizeof(struct kinfo_proc)) + 10);
+        continue;
+      }
+      if (error != 0) {
+        len = 0;
+        LLDB_LOG(log, "tid = {0} in state {1} failed to get thread name: {2}", GetID(),
+                 StateAsCString(m_state), strerror(errno));
+      }
+      kp.resize(len / sizeof(struct kinfo_proc));
+      break;
+    }
+
+    // empty == unknown
+    m_thread_name = std::string();
+    for (auto& procinfo : kp) {
+      if (procinfo.ki_tid == (lwpid_t)GetID()) {
+        m_thread_name = procinfo.ki_tdname;
+        break;
+      }
+    }
+  }
+
+  return m_thread_name.getValue();
+}
 
 lldb::StateType NativeThreadFreeBSD::GetState() { return m_state; }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D90298.301240.patch
Type: text/x-patch
Size: 2576 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/lldb-commits/attachments/20201028/d8b06e71/attachment.bin>


More information about the lldb-commits mailing list