[Lldb-commits] [lldb] 326d235 - [lldb] [Process/FreeBSDRemote] Implement thread GetName()
Michał Górny via lldb-commits
lldb-commits at lists.llvm.org
Tue Nov 3 00:46:28 PST 2020
Author: Michał Górny
Date: 2020-11-03T09:45:49+01:00
New Revision: 326d235300814e2e38341580bdf04efd226ba83d
URL: https://github.com/llvm/llvm-project/commit/326d235300814e2e38341580bdf04efd226ba83d
DIFF: https://github.com/llvm/llvm-project/commit/326d235300814e2e38341580bdf04efd226ba83d.diff
LOG: [lldb] [Process/FreeBSDRemote] Implement thread GetName()
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.
Differential Revision: https://reviews.llvm.org/D90298
Added:
Modified:
lldb/source/Plugins/Process/FreeBSDRemote/NativeThreadFreeBSD.cpp
lldb/source/Plugins/Process/FreeBSDRemote/NativeThreadFreeBSD.h
Removed:
################################################################################
diff --git a/lldb/source/Plugins/Process/FreeBSDRemote/NativeThreadFreeBSD.cpp b/lldb/source/Plugins/Process/FreeBSDRemote/NativeThreadFreeBSD.cpp
index c5f0edc64806..2e62b0a25ed2 100644
--- a/lldb/source/Plugins/Process/FreeBSDRemote/NativeThreadFreeBSD.cpp
+++ b/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,43 @@ void NativeThreadFreeBSD::SetStepping() {
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)) {
+ kp.resize(len / sizeof(struct kinfo_proc));
+ continue;
+ }
+ if (error != 0) {
+ len = 0;
+ LLDB_LOG(log, "tid = {0} in state {1} failed to get thread name: {2}", GetID(),
+ 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; }
diff --git a/lldb/source/Plugins/Process/FreeBSDRemote/NativeThreadFreeBSD.h b/lldb/source/Plugins/Process/FreeBSDRemote/NativeThreadFreeBSD.h
index e4d494174736..665e4ea48971 100644
--- a/lldb/source/Plugins/Process/FreeBSDRemote/NativeThreadFreeBSD.h
+++ b/lldb/source/Plugins/Process/FreeBSDRemote/NativeThreadFreeBSD.h
@@ -74,6 +74,7 @@ class NativeThreadFreeBSD : public NativeThreadProtocol {
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;
More information about the lldb-commits
mailing list