[Lldb-commits] [lldb] 65ad02b - [lldb][NFC] Break ThreadMemory into smaller abstractions (#132905)
via lldb-commits
lldb-commits at lists.llvm.org
Tue Mar 25 02:50:55 PDT 2025
Author: Felipe de Azevedo Piovezan
Date: 2025-03-25T06:50:52-03:00
New Revision: 65ad02b882ba545dafbfc195a78e204c218e93ed
URL: https://github.com/llvm/llvm-project/commit/65ad02b882ba545dafbfc195a78e204c218e93ed
DIFF: https://github.com/llvm/llvm-project/commit/65ad02b882ba545dafbfc195a78e204c218e93ed.diff
LOG: [lldb][NFC] Break ThreadMemory into smaller abstractions (#132905)
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.
Added:
Modified:
lldb/source/Plugins/OperatingSystem/Python/OperatingSystemPython.cpp
lldb/source/Plugins/Process/Utility/ThreadMemory.cpp
lldb/source/Plugins/Process/Utility/ThreadMemory.h
Removed:
################################################################################
diff --git a/lldb/source/Plugins/OperatingSystem/Python/OperatingSystemPython.cpp b/lldb/source/Plugins/OperatingSystem/Python/OperatingSystemPython.cpp
index aff521890858c..96b2b9d9ee088 100644
--- a/lldb/source/Plugins/OperatingSystem/Python/OperatingSystemPython.cpp
+++ b/lldb/source/Plugins/OperatingSystem/Python/OperatingSystemPython.cpp
@@ -259,8 +259,8 @@ ThreadSP OperatingSystemPython::CreateThreadFromThreadInfo(
if (!thread_sp) {
if (did_create_ptr)
*did_create_ptr = true;
- thread_sp = std::make_shared<ThreadMemory>(*m_process, tid, name, queue,
- reg_data_addr);
+ thread_sp = std::make_shared<ThreadMemoryProvidingNameAndQueue>(
+ *m_process, tid, name, queue, reg_data_addr);
}
if (core_number < core_thread_list.GetSize(false)) {
diff --git a/lldb/source/Plugins/Process/Utility/ThreadMemory.cpp b/lldb/source/Plugins/Process/Utility/ThreadMemory.cpp
index 550b53688fd39..f4bfba117226f 100644
--- a/lldb/source/Plugins/Process/Utility/ThreadMemory.cpp
+++ b/lldb/source/Plugins/Process/Utility/ThreadMemory.cpp
@@ -20,18 +20,17 @@
using namespace lldb;
using namespace lldb_private;
-ThreadMemory::ThreadMemory(Process &process, lldb::tid_t tid,
- const ValueObjectSP &thread_info_valobj_sp)
- : Thread(process, tid), m_backing_thread_sp(),
- m_thread_info_valobj_sp(thread_info_valobj_sp), m_name(), m_queue(),
- m_register_data_addr(LLDB_INVALID_ADDRESS) {}
-
-ThreadMemory::ThreadMemory(Process &process, lldb::tid_t tid,
- llvm::StringRef name, llvm::StringRef queue,
- lldb::addr_t register_data_addr)
- : Thread(process, tid), m_backing_thread_sp(), m_thread_info_valobj_sp(),
- m_name(std::string(name)), m_queue(std::string(queue)),
- m_register_data_addr(register_data_addr) {}
+ThreadMemoryProvidingNameAndQueue::ThreadMemoryProvidingNameAndQueue(
+ Process &process, lldb::tid_t tid,
+ const ValueObjectSP &thread_info_valobj_sp)
+ : ThreadMemoryProvidingName(process, tid, LLDB_INVALID_ADDRESS, ""),
+ m_thread_info_valobj_sp(thread_info_valobj_sp), m_queue() {}
+
+ThreadMemoryProvidingNameAndQueue::ThreadMemoryProvidingNameAndQueue(
+ Process &process, lldb::tid_t tid, llvm::StringRef name,
+ llvm::StringRef queue, lldb::addr_t register_data_addr)
+ : ThreadMemoryProvidingName(process, tid, register_data_addr, name),
+ m_thread_info_valobj_sp(), m_queue(std::string(queue)) {}
ThreadMemory::~ThreadMemory() { DestroyThread(); }
diff --git a/lldb/source/Plugins/Process/Utility/ThreadMemory.h b/lldb/source/Plugins/Process/Utility/ThreadMemory.h
index cebb31538eaf2..7cabde202518e 100644
--- a/lldb/source/Plugins/Process/Utility/ThreadMemory.h
+++ b/lldb/source/Plugins/Process/Utility/ThreadMemory.h
@@ -13,14 +13,16 @@
#include "lldb/Target/Thread.h"
+/// A memory thread with its own ID, optionally backed by a real thread.
+/// Most methods of this class dispatch to the real thread if it is not null.
+/// Notable exceptions are the methods calculating the StopInfo and
+/// RegisterContext of the thread, those may query the OS plugin that created
+/// the thread.
class ThreadMemory : public lldb_private::Thread {
public:
ThreadMemory(lldb_private::Process &process, lldb::tid_t tid,
- const lldb::ValueObjectSP &thread_info_valobj_sp);
-
- ThreadMemory(lldb_private::Process &process, lldb::tid_t tid,
- llvm::StringRef name, llvm::StringRef queue,
- lldb::addr_t register_data_addr);
+ lldb::addr_t register_data_addr)
+ : Thread(process, tid), m_register_data_addr(register_data_addr) {}
~ThreadMemory() override;
@@ -38,16 +40,12 @@ class ThreadMemory : public lldb_private::Thread {
}
const char *GetName() override {
- if (!m_name.empty())
- return m_name.c_str();
if (m_backing_thread_sp)
return m_backing_thread_sp->GetName();
return nullptr;
}
const char *GetQueueName() override {
- if (!m_queue.empty())
- return m_queue.c_str();
if (m_backing_thread_sp)
return m_backing_thread_sp->GetQueueName();
return nullptr;
@@ -68,8 +66,6 @@ class ThreadMemory : public lldb_private::Thread {
void RefreshStateAfterStop() override;
- lldb::ValueObjectSP &GetValueObject() { return m_thread_info_valobj_sp; }
-
void ClearStackFrames() override;
void ClearBackingThread() override {
@@ -79,34 +75,76 @@ class ThreadMemory : public lldb_private::Thread {
}
bool SetBackingThread(const lldb::ThreadSP &thread_sp) override {
- // printf ("Thread 0x%llx is being backed by thread 0x%llx\n", GetID(),
- // thread_sp->GetID());
m_backing_thread_sp = thread_sp;
thread_sp->SetBackedThread(*this);
- return (bool)thread_sp;
+ return thread_sp.get();
}
lldb::ThreadSP GetBackingThread() const override {
return m_backing_thread_sp;
}
-protected:
bool IsOperatingSystemPluginThread() const override { return true; }
- // If this memory thread is actually represented by a thread from the
- // lldb_private::Process subclass, then fill in the thread here and
- // all APIs will be routed through this thread object. If m_backing_thread_sp
- // is empty, then this thread is simply in memory with no representation
- // through the process plug-in.
+private:
+ lldb::addr_t m_register_data_addr;
lldb::ThreadSP m_backing_thread_sp;
- lldb::ValueObjectSP m_thread_info_valobj_sp;
+
+ ThreadMemory(const ThreadMemory &) = delete;
+ const ThreadMemory &operator=(const ThreadMemory &) = delete;
+};
+
+/// A ThreadMemory that optionally overrides the thread name.
+class ThreadMemoryProvidingName : public ThreadMemory {
+public:
+ ThreadMemoryProvidingName(lldb_private::Process &process, lldb::tid_t tid,
+ lldb::addr_t register_data_addr,
+ llvm::StringRef name)
+ : ThreadMemory(process, tid, register_data_addr), m_name(name) {}
+
+ const char *GetName() override {
+ if (!m_name.empty())
+ return m_name.c_str();
+ return ThreadMemory::GetName();
+ }
+
+ ~ThreadMemoryProvidingName() override = default;
+
+private:
std::string m_name;
+};
+
+/// A ThreadMemoryProvidingName that optionally overrides queue information.
+class ThreadMemoryProvidingNameAndQueue : public ThreadMemoryProvidingName {
+public:
+ ThreadMemoryProvidingNameAndQueue(
+ lldb_private::Process &process, lldb::tid_t tid,
+ const lldb::ValueObjectSP &thread_info_valobj_sp);
+
+ ThreadMemoryProvidingNameAndQueue(lldb_private::Process &process,
+ lldb::tid_t tid, llvm::StringRef name,
+ llvm::StringRef queue,
+ lldb::addr_t register_data_addr);
+
+ ~ThreadMemoryProvidingNameAndQueue() override = default;
+
+ const char *GetQueueName() override {
+ if (!m_queue.empty())
+ return m_queue.c_str();
+ return ThreadMemory::GetQueueName();
+ }
+
+ lldb::ValueObjectSP &GetValueObject() { return m_thread_info_valobj_sp; }
+
+protected:
+ lldb::ValueObjectSP m_thread_info_valobj_sp;
std::string m_queue;
- lldb::addr_t m_register_data_addr;
private:
- ThreadMemory(const ThreadMemory &) = delete;
- const ThreadMemory &operator=(const ThreadMemory &) = delete;
+ ThreadMemoryProvidingNameAndQueue(const ThreadMemoryProvidingNameAndQueue &) =
+ delete;
+ const ThreadMemoryProvidingNameAndQueue &
+ operator=(const ThreadMemoryProvidingNameAndQueue &) = delete;
};
#endif // LLDB_SOURCE_PLUGINS_PROCESS_UTILITY_THREADMEMORY_H
More information about the lldb-commits
mailing list