Skip to content

Commit e095cdd

Browse files
committed
[lldb] Add a NativeProcessProtocol::Threads() iterable
Sponsored by: The FreeBSD Foundation Differential Revision: https://reviews.llvm.org/D128698
1 parent c72f22b commit e095cdd

4 files changed

Lines changed: 38 additions & 39 deletions

File tree

lldb/include/lldb/Host/common/NativeProcessProtocol.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "lldb/Host/Host.h"
1616
#include "lldb/Host/MainLoop.h"
1717
#include "lldb/Utility/ArchSpec.h"
18+
#include "lldb/Utility/Iterable.h"
1819
#include "lldb/Utility/Status.h"
1920
#include "lldb/Utility/TraceGDBRemotePackets.h"
2021
#include "lldb/Utility/UnimplementedError.h"
@@ -48,6 +49,16 @@ class NativeProcessProtocol {
4849
public:
4950
virtual ~NativeProcessProtocol() = default;
5051

52+
typedef std::vector<std::unique_ptr<NativeThreadProtocol>> thread_collection;
53+
template <typename I>
54+
static NativeThreadProtocol &thread_list_adapter(I &iter) {
55+
assert(*iter);
56+
return **iter;
57+
}
58+
typedef LockingAdaptedIterable<thread_collection, NativeThreadProtocol &,
59+
thread_list_adapter, std::recursive_mutex>
60+
ThreadIterable;
61+
5162
virtual Status Resume(const ResumeActionList &resume_actions) = 0;
5263

5364
virtual Status Halt() = 0;
@@ -210,6 +221,10 @@ class NativeProcessProtocol {
210221
return GetThreadByID(m_current_thread_id);
211222
}
212223

224+
ThreadIterable Threads() const {
225+
return ThreadIterable(m_threads, m_threads_mutex);
226+
}
227+
213228
// Access to inferior stdio
214229
virtual int GetTerminalFileDescriptor() { return m_terminal_fd; }
215230

lldb/source/Plugins/Process/Linux/IntelPTCollector.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,8 @@ Error IntelPTCollector::TraceStart(const TraceIntelPTStartRequest &request) {
9898
}
9999
} else {
100100
std::vector<lldb::tid_t> process_threads;
101-
for (size_t i = 0; m_process.GetThreadAtIndex(i); i++)
102-
process_threads.push_back(m_process.GetThreadAtIndex(i)->GetID());
101+
for (NativeThreadProtocol &thread : m_process.Threads())
102+
process_threads.push_back(thread.GetID());
103103

104104
// per-thread process tracing
105105
if (Expected<IntelPTProcessTraceUP> trace =

lldb/source/Plugins/Process/Linux/IntelPTMultiCoreTrace.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,9 @@ void IntelPTMultiCoreTrace::ProcessWillResume() {
107107
TraceIntelPTGetStateResponse IntelPTMultiCoreTrace::GetState() {
108108
TraceIntelPTGetStateResponse state;
109109

110-
for (size_t i = 0; m_process.GetThreadAtIndex(i); i++)
110+
for (NativeThreadProtocol &thread : m_process.Threads())
111111
state.traced_threads.push_back(
112-
TraceThreadState{m_process.GetThreadAtIndex(i)->GetID(), {}});
112+
TraceThreadState{thread.GetID(), {}});
113113

114114
state.cpus.emplace();
115115
ForEachCore([&](lldb::cpu_id_t cpu_id,

lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationServerLLGS.cpp

Lines changed: 19 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -724,17 +724,12 @@ GetJSONThreadsInfo(NativeProcessProtocol &process, bool abridged) {
724724
json::Array threads_array;
725725

726726
// Ensure we can get info on the given thread.
727-
uint32_t thread_idx = 0;
728-
for (NativeThreadProtocol *thread;
729-
(thread = process.GetThreadAtIndex(thread_idx)) != nullptr;
730-
++thread_idx) {
731-
732-
lldb::tid_t tid = thread->GetID();
733-
727+
for (NativeThreadProtocol &thread : process.Threads()) {
728+
lldb::tid_t tid = thread.GetID();
734729
// Grab the reason this thread stopped.
735730
struct ThreadStopInfo tid_stop_info;
736731
std::string description;
737-
if (!thread->GetStopReason(tid_stop_info, description))
732+
if (!thread.GetStopReason(tid_stop_info, description))
738733
return llvm::make_error<llvm::StringError>(
739734
"failed to get stop reason", llvm::inconvertibleErrorCode());
740735

@@ -751,7 +746,7 @@ GetJSONThreadsInfo(NativeProcessProtocol &process, bool abridged) {
751746
json::Object thread_obj;
752747

753748
if (!abridged) {
754-
if (llvm::Optional<json::Object> registers = GetRegistersAsJSON(*thread))
749+
if (llvm::Optional<json::Object> registers = GetRegistersAsJSON(thread))
755750
thread_obj.try_emplace("registers", std::move(*registers));
756751
}
757752

@@ -760,7 +755,7 @@ GetJSONThreadsInfo(NativeProcessProtocol &process, bool abridged) {
760755
if (signum != 0)
761756
thread_obj.try_emplace("signal", signum);
762757

763-
const std::string thread_name = thread->GetName();
758+
const std::string thread_name = thread.GetName();
764759
if (!thread_name.empty())
765760
thread_obj.try_emplace("name", thread_name);
766761

@@ -856,14 +851,12 @@ GDBRemoteCommunicationServerLLGS::PrepareStopReplyPacketForThread(
856851
if (m_list_threads_in_stop_reply) {
857852
response.PutCString("threads:");
858853

859-
uint32_t thread_index = 0;
860-
NativeThreadProtocol *listed_thread;
861-
for (listed_thread = process.GetThreadAtIndex(thread_index); listed_thread;
862-
++thread_index,
863-
listed_thread = process.GetThreadAtIndex(thread_index)) {
864-
if (thread_index > 0)
854+
uint32_t thread_num = 0;
855+
for (NativeThreadProtocol &listed_thread : process.Threads()) {
856+
if (thread_num > 0)
865857
response.PutChar(',');
866-
response.Printf("%" PRIx64, listed_thread->GetID());
858+
response.Printf("%" PRIx64, listed_thread.GetID());
859+
++thread_num;
867860
}
868861
response.PutChar(';');
869862

@@ -872,7 +865,7 @@ GDBRemoteCommunicationServerLLGS::PrepareStopReplyPacketForThread(
872865
// is hex ascii JSON that contains the thread IDs thread stop info only for
873866
// threads that have stop reasons. Only send this if we have more than one
874867
// thread otherwise this packet has all the info it needs.
875-
if (thread_index > 1) {
868+
if (thread_num > 1) {
876869
const bool threads_with_valid_stop_info_only = true;
877870
llvm::Expected<json::Array> threads_info = GetJSONThreadsInfo(
878871
*m_current_process, threads_with_valid_stop_info_only);
@@ -889,12 +882,10 @@ GDBRemoteCommunicationServerLLGS::PrepareStopReplyPacketForThread(
889882
}
890883
}
891884

892-
uint32_t i = 0;
893885
response.PutCString("thread-pcs");
894886
char delimiter = ':';
895-
for (NativeThreadProtocol *thread;
896-
(thread = process.GetThreadAtIndex(i)) != nullptr; ++i) {
897-
NativeRegisterContext &reg_ctx = thread->GetRegisterContext();
887+
for (NativeThreadProtocol &thread : process.Threads()) {
888+
NativeRegisterContext &reg_ctx = thread.GetRegisterContext();
898889

899890
uint32_t reg_to_read = reg_ctx.ConvertRegisterKindToRegisterNumber(
900891
eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC);
@@ -1024,12 +1015,10 @@ void GDBRemoteCommunicationServerLLGS::EnqueueStopReplyPackets(
10241015
if (!m_non_stop)
10251016
return;
10261017

1027-
uint32_t thread_index = 0;
1028-
while (NativeThreadProtocol *listed_thread =
1029-
m_current_process->GetThreadAtIndex(thread_index++)) {
1030-
if (listed_thread->GetID() != thread_to_skip)
1018+
for (NativeThreadProtocol &listed_thread : m_current_process->Threads()) {
1019+
if (listed_thread.GetID() != thread_to_skip)
10311020
m_stop_notification_queue.push_back(
1032-
PrepareStopReplyPacketForThread(*listed_thread).GetString().str());
1021+
PrepareStopReplyPacketForThread(listed_thread).GetString().str());
10331022
}
10341023
}
10351024

@@ -1990,15 +1979,10 @@ void GDBRemoteCommunicationServerLLGS::AddProcessThreads(
19901979
return;
19911980

19921981
LLDB_LOG(log, "iterating over threads of process {0}", process.GetID());
1993-
NativeThreadProtocol *thread;
1994-
uint32_t thread_index;
1995-
for (thread_index = 0, thread = process.GetThreadAtIndex(thread_index);
1996-
thread;
1997-
++thread_index, thread = process.GetThreadAtIndex(thread_index)) {
1998-
LLDB_LOG(log, "iterated thread {0} (tid={1})", thread_index,
1999-
thread->GetID());
1982+
for (NativeThreadProtocol &thread : process.Threads()) {
1983+
LLDB_LOG(log, "iterated thread tid={0}", thread.GetID());
20001984
response.PutChar(had_any ? ',' : 'm');
2001-
AppendThreadIDToResponse(response, pid, thread->GetID());
1985+
AppendThreadIDToResponse(response, pid, thread.GetID());
20021986
had_any = true;
20031987
}
20041988
}

0 commit comments

Comments
 (0)