Skip to content

Commit c30cbcf

Browse files
[lldb][NFC] Break ThreadMemory into smaller abstractions
ThreadMemory attempts to be a class abstracting the notion of a "fake" Thread that is backed by a "real" thread. According to its documentation, it is meant to be a class forwarding most methods to the backing thread, but it does so only for a handful of methods. Along the way, it also tries to represent a Thread that may or may not have a different name, and may or may not have a different queue from the backing thread. This can be problematic for a couple of reasons: 1. It forces all users into this optional behavior. 2. The forwarding behavior is incomplete: not all methods are currently being forwarded properly. Some of them involve queues and seem to have been intentionally left unimplemented. This commit creates the following separation: ThreadMemory <- ThreadMemoryProvidingName <- ThreadMemoryProvidingNameAndQueue ThreadMemory captures the notion of a backed thread that _really_ forwards all methods to the backing thread. (Missing methods should be implemented in a later commit, and allowing them to be implemented without changing behavior of other derived classes is the main purpose of this refactor). ThreadMemoryProvidingNameAndQueue is a ThreadMemory that allows users to override the thread name. If a name is present, it is used; otherwise the forwarding behavior is used. ThreadMemoryProvidingNameAndQueue is a ThreadMemoryProvidingName that allows users to override queue information. If queue information is present, it is used; otherwise, the forwarding behavior is used. With this separation, we can more explicitly implement missing methods of the base class and override them, if needed, in ThreadMemoryProvidingNameAndQueue. But this commit really is NFC, no new methods are implemented and no method implementation is changed.
1 parent f3fa54a commit c30cbcf

File tree

3 files changed

+72
-38
lines changed

3 files changed

+72
-38
lines changed

lldb/source/Plugins/OperatingSystem/Python/OperatingSystemPython.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -259,8 +259,8 @@ ThreadSP OperatingSystemPython::CreateThreadFromThreadInfo(
259259
if (!thread_sp) {
260260
if (did_create_ptr)
261261
*did_create_ptr = true;
262-
thread_sp = std::make_shared<ThreadMemory>(*m_process, tid, name, queue,
263-
reg_data_addr);
262+
thread_sp = std::make_shared<ThreadMemoryProvidingNameAndQueue>(
263+
*m_process, tid, name, queue, reg_data_addr);
264264
}
265265

266266
if (core_number < core_thread_list.GetSize(false)) {

lldb/source/Plugins/Process/Utility/ThreadMemory.cpp

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,17 @@
2020
using namespace lldb;
2121
using namespace lldb_private;
2222

23-
ThreadMemory::ThreadMemory(Process &process, lldb::tid_t tid,
24-
const ValueObjectSP &thread_info_valobj_sp)
25-
: Thread(process, tid), m_backing_thread_sp(),
26-
m_thread_info_valobj_sp(thread_info_valobj_sp), m_name(), m_queue(),
27-
m_register_data_addr(LLDB_INVALID_ADDRESS) {}
28-
29-
ThreadMemory::ThreadMemory(Process &process, lldb::tid_t tid,
30-
llvm::StringRef name, llvm::StringRef queue,
31-
lldb::addr_t register_data_addr)
32-
: Thread(process, tid), m_backing_thread_sp(), m_thread_info_valobj_sp(),
33-
m_name(std::string(name)), m_queue(std::string(queue)),
34-
m_register_data_addr(register_data_addr) {}
23+
ThreadMemoryProvidingNameAndQueue::ThreadMemoryProvidingNameAndQueue(
24+
Process &process, lldb::tid_t tid,
25+
const ValueObjectSP &thread_info_valobj_sp)
26+
: ThreadMemoryProvidingName(process, tid, LLDB_INVALID_ADDRESS, ""),
27+
m_thread_info_valobj_sp(thread_info_valobj_sp), m_queue() {}
28+
29+
ThreadMemoryProvidingNameAndQueue::ThreadMemoryProvidingNameAndQueue(
30+
Process &process, lldb::tid_t tid, llvm::StringRef name,
31+
llvm::StringRef queue, lldb::addr_t register_data_addr)
32+
: ThreadMemoryProvidingName(process, tid, register_data_addr, name),
33+
m_thread_info_valobj_sp(), m_queue(std::string(queue)) {}
3534

3635
ThreadMemory::~ThreadMemory() { DestroyThread(); }
3736

lldb/source/Plugins/Process/Utility/ThreadMemory.h

Lines changed: 59 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@
1313

1414
#include "lldb/Target/Thread.h"
1515

16+
/// A memory thread optionally backed by a real thread.
17+
/// All methods of this class dispatch to the real thread, if it is not null.
18+
/// Otherwise the methods are no-op.
1619
class ThreadMemory : public lldb_private::Thread {
1720
public:
1821
ThreadMemory(lldb_private::Process &process, lldb::tid_t tid,
19-
const lldb::ValueObjectSP &thread_info_valobj_sp);
20-
21-
ThreadMemory(lldb_private::Process &process, lldb::tid_t tid,
22-
llvm::StringRef name, llvm::StringRef queue,
23-
lldb::addr_t register_data_addr);
22+
lldb::addr_t register_data_addr)
23+
: Thread(process, tid), m_register_data_addr(register_data_addr) {}
2424

2525
~ThreadMemory() override;
2626

@@ -38,16 +38,12 @@ class ThreadMemory : public lldb_private::Thread {
3838
}
3939

4040
const char *GetName() override {
41-
if (!m_name.empty())
42-
return m_name.c_str();
4341
if (m_backing_thread_sp)
4442
return m_backing_thread_sp->GetName();
4543
return nullptr;
4644
}
4745

4846
const char *GetQueueName() override {
49-
if (!m_queue.empty())
50-
return m_queue.c_str();
5147
if (m_backing_thread_sp)
5248
return m_backing_thread_sp->GetQueueName();
5349
return nullptr;
@@ -68,8 +64,6 @@ class ThreadMemory : public lldb_private::Thread {
6864

6965
void RefreshStateAfterStop() override;
7066

71-
lldb::ValueObjectSP &GetValueObject() { return m_thread_info_valobj_sp; }
72-
7367
void ClearStackFrames() override;
7468

7569
void ClearBackingThread() override {
@@ -79,34 +73,75 @@ class ThreadMemory : public lldb_private::Thread {
7973
}
8074

8175
bool SetBackingThread(const lldb::ThreadSP &thread_sp) override {
82-
// printf ("Thread 0x%llx is being backed by thread 0x%llx\n", GetID(),
83-
// thread_sp->GetID());
8476
m_backing_thread_sp = thread_sp;
8577
thread_sp->SetBackedThread(*this);
86-
return (bool)thread_sp;
78+
return thread_sp.get();
8779
}
8880

8981
lldb::ThreadSP GetBackingThread() const override {
9082
return m_backing_thread_sp;
9183
}
9284

93-
protected:
9485
bool IsOperatingSystemPluginThread() const override { return true; }
9586

96-
// If this memory thread is actually represented by a thread from the
97-
// lldb_private::Process subclass, then fill in the thread here and
98-
// all APIs will be routed through this thread object. If m_backing_thread_sp
99-
// is empty, then this thread is simply in memory with no representation
100-
// through the process plug-in.
87+
private:
88+
lldb::addr_t m_register_data_addr;
10189
lldb::ThreadSP m_backing_thread_sp;
102-
lldb::ValueObjectSP m_thread_info_valobj_sp;
90+
91+
ThreadMemory(const ThreadMemory &) = delete;
92+
const ThreadMemory &operator=(const ThreadMemory &) = delete;
93+
};
94+
95+
class ThreadMemoryProvidingName : public ThreadMemory {
96+
public:
97+
ThreadMemoryProvidingName(lldb_private::Process &process, lldb::tid_t tid,
98+
lldb::addr_t register_data_addr,
99+
llvm::StringRef name)
100+
: ThreadMemory(process, tid, register_data_addr), m_name(name) {}
101+
102+
const char *GetName() override {
103+
if (!m_name.empty())
104+
return m_name.c_str();
105+
return ThreadMemory::GetName();
106+
}
107+
108+
~ThreadMemoryProvidingName() override = default;
109+
110+
private:
103111
std::string m_name;
112+
};
113+
114+
/// A NamedThreadMemory that has optional queue information.
115+
class ThreadMemoryProvidingNameAndQueue : public ThreadMemoryProvidingName {
116+
public:
117+
ThreadMemoryProvidingNameAndQueue(
118+
lldb_private::Process &process, lldb::tid_t tid,
119+
const lldb::ValueObjectSP &thread_info_valobj_sp);
120+
121+
ThreadMemoryProvidingNameAndQueue(lldb_private::Process &process,
122+
lldb::tid_t tid, llvm::StringRef name,
123+
llvm::StringRef queue,
124+
lldb::addr_t register_data_addr);
125+
126+
~ThreadMemoryProvidingNameAndQueue() override = default;
127+
128+
const char *GetQueueName() override {
129+
if (!m_queue.empty())
130+
return m_queue.c_str();
131+
return ThreadMemory::GetQueueName();
132+
}
133+
134+
lldb::ValueObjectSP &GetValueObject() { return m_thread_info_valobj_sp; }
135+
136+
protected:
137+
lldb::ValueObjectSP m_thread_info_valobj_sp;
104138
std::string m_queue;
105-
lldb::addr_t m_register_data_addr;
106139

107140
private:
108-
ThreadMemory(const ThreadMemory &) = delete;
109-
const ThreadMemory &operator=(const ThreadMemory &) = delete;
141+
ThreadMemoryProvidingNameAndQueue(const ThreadMemoryProvidingNameAndQueue &) =
142+
delete;
143+
const ThreadMemoryProvidingNameAndQueue &
144+
operator=(const ThreadMemoryProvidingNameAndQueue &) = delete;
110145
};
111146

112147
#endif // LLDB_SOURCE_PLUGINS_PROCESS_UTILITY_THREADMEMORY_H

0 commit comments

Comments
 (0)