Skip to content

Commit a1c2d9d

Browse files
committed
Refactor to a thread collection SP
1 parent 7638844 commit a1c2d9d

File tree

6 files changed

+28
-73
lines changed

6 files changed

+28
-73
lines changed

lldb/include/lldb/API/SBSaveCoreOptions.h

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "lldb/API/SBFileSpec.h"
1515
#include "lldb/API/SBProcess.h"
1616
#include "lldb/API/SBThread.h"
17+
#include "lldb/API/SBThreadCollection.h"
1718

1819
namespace lldb {
1920

@@ -111,26 +112,18 @@ class LLDB_API SBSaveCoreOptions {
111112
/// style specific regions.
112113
SBError AddMemoryRegionToSave(const SBMemoryRegionInfo &region);
113114

114-
/// Get the number of Threads to be saved
115+
/// Get an unsorted collection of the threads to save
115116
///
116117
/// \returns
117-
/// The count of Threads to be saved.
118-
uint32_t GetNumThreads() const;
119-
120-
/// Get the Thread at the specified index.
121-
///
122-
/// \param [in] idx
123-
/// The zero based index of the thread to return.
124-
/// \returns
125-
/// The thread at the specified index, or an invalid SBThread if the index
126-
/// is greater than or equal to the number of threads.
127-
lldb::SBThread GetThreadAtIndex(uint32_t idx) const;
118+
/// An unsorted collection with zero or more threads.
119+
SBThreadCollection GetThreadsToSave() const;
128120

129121
/// Reset all options.
130122
void Clear();
131123

132124
protected:
133125
friend class SBProcess;
126+
friend class SBThreadCollection;
134127
lldb_private::SaveCoreOptions &ref() const;
135128

136129
private:

lldb/include/lldb/API/SBThreadCollection.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ class LLDB_API SBThreadCollection {
4848
private:
4949
friend class SBProcess;
5050
friend class SBThread;
51-
51+
friend class SBSaveCoreOptions;
5252
lldb::ThreadCollectionSP m_opaque_sp;
5353
};
5454

lldb/include/lldb/Symbol/SaveCoreOptions.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,7 @@ class SaveCoreOptions {
4646

4747
void AddMemoryRegionToSave(const lldb_private::MemoryRegionInfo &region);
4848

49-
std::optional<lldb::ThreadSP> GetThreadAtIndex(uint32_t idx) const;
50-
uint32_t GetNumThreads() const;
49+
lldb::ThreadCollectionSP GetThreadsToSave() const;
5150

5251
void Clear();
5352

@@ -60,8 +59,6 @@ class SaveCoreOptions {
6059
lldb::ProcessSP m_process_sp;
6160
std::unordered_map<lldb::tid_t, lldb::ThreadSP> m_threads_to_save;
6261
MemoryRanges m_regions_to_save;
63-
64-
std::vector<lldb::tid_t> m_thread_indexes;
6562
};
6663
} // namespace lldb_private
6764

lldb/source/API/SBSaveCoreOptions.cpp

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -100,17 +100,9 @@ SBSaveCoreOptions::AddMemoryRegionToSave(const SBMemoryRegionInfo &region) {
100100
return SBError();
101101
}
102102

103-
uint32_t lldb::SBSaveCoreOptions::GetNumThreads() const {
103+
lldb::SBThreadCollection SBSaveCoreOptions::GetThreadsToSave() const {
104104
LLDB_INSTRUMENT_VA(this);
105-
return m_opaque_up->GetNumThreads();
106-
}
107-
108-
SBThread SBSaveCoreOptions::GetThreadAtIndex(uint32_t idx) const {
109-
LLDB_INSTRUMENT_VA(this, idx);
110-
std::optional<lldb::ThreadSP> thread_sp = m_opaque_up->GetThreadAtIndex(idx);
111-
if (thread_sp)
112-
return SBThread(thread_sp.value());
113-
return SBThread();
105+
return SBThreadCollection(m_opaque_up->GetThreadsToSave());
114106
}
115107

116108
void SBSaveCoreOptions::Clear() {

lldb/source/Symbol/SaveCoreOptions.cpp

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -88,32 +88,11 @@ Status SaveCoreOptions::AddThread(lldb::ThreadSP thread_sp) {
8888
}
8989

9090
m_threads_to_save.insert({thread_sp->GetID(), thread_sp});
91-
m_thread_indexes.push_back(thread_sp->GetID());
9291
return error;
9392
}
9493

9594
bool SaveCoreOptions::RemoveThread(lldb::ThreadSP thread_sp) {
96-
if (!thread_sp)
97-
return false;
98-
if (m_threads_to_save.erase(thread_sp->GetID()) == 0)
99-
return false;
100-
101-
auto it = std::find(m_thread_indexes.begin(), m_thread_indexes.end(),
102-
thread_sp->GetID());
103-
m_thread_indexes.erase(it);
104-
return true;
105-
}
106-
107-
uint32_t SaveCoreOptions::GetNumThreads() const {
108-
return m_threads_to_save.size();
109-
}
110-
111-
std::optional<lldb::ThreadSP>
112-
SaveCoreOptions::GetThreadAtIndex(uint32_t idx) const {
113-
if (idx >= m_thread_indexes.size())
114-
return std::nullopt;
115-
lldb::tid_t tid = m_thread_indexes[idx];
116-
return m_threads_to_save.find(tid)->second;
95+
return thread_sp && m_threads_to_save.erase(thread_sp->GetID()) > 0;
11796
}
11897

11998
bool SaveCoreOptions::ShouldThreadBeSaved(lldb::tid_t tid) const {
@@ -136,6 +115,15 @@ const MemoryRanges &SaveCoreOptions::GetCoreFileMemoryRanges() const {
136115
return m_regions_to_save;
137116
}
138117

118+
lldb::ThreadCollectionSP SaveCoreOptions::GetThreadsToSave() const {
119+
lldb::ThreadCollectionSP threadcollection_sp =
120+
std::make_shared<ThreadCollection>();
121+
for (const auto &thread : m_threads_to_save) {
122+
threadcollection_sp->AddThread(thread.second);
123+
}
124+
return threadcollection_sp;
125+
}
126+
139127
Status
140128
SaveCoreOptions::EnsureValidConfiguration(lldb::ProcessSP process_sp) const {
141129
Status error;
@@ -166,5 +154,4 @@ void SaveCoreOptions::Clear() {
166154
m_threads_to_save.clear();
167155
m_process_sp.reset();
168156
m_regions_to_save.Clear();
169-
m_thread_indexes.clear();
170157
}

lldb/test/API/python_api/sbsavecoreoptions/TestSBSaveCoreOptions.py

Lines changed: 9 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -93,28 +93,14 @@ def test_removing_and_adding_insertion_order(self):
9393
error = options.AddThread(thread)
9494
self.assertTrue(error.Success())
9595

96-
# Get the middle thread, remove it, and insert it at the end.
96+
# Get the middle thread, remove it, and insert it back.
9797
middle_thread = threads[1]
9898
self.assertTrue(options.RemoveThread(middle_thread))
99-
num_threads = options.GetNumThreads()
100-
self.assertEqual(num_threads, 2)
101-
error = options.AddThread(middle_thread)
102-
self.assertTrue(error.Success())
103-
num_threads = options.GetNumThreads()
104-
self.assertEqual(num_threads, 3)
105-
thread_at_last_index = options.GetThreadAtIndex(2)
106-
self.assertEqual(thread_at_last_index.id, middle_thread.id)
107-
thread_at_middle_index = options.GetThreadAtIndex(1)
108-
self.assertEqual(thread_at_middle_index.id, threads[2].id)
109-
110-
# Pop the front thread, remove it, and insert it at the end.
111-
front_thread = threads[0]
112-
self.assertTrue(options.RemoveThread(front_thread))
113-
num_threads = options.GetNumThreads()
114-
self.assertEqual(num_threads, 2)
115-
error = options.AddThread(front_thread)
116-
self.assertTrue(error.Success())
117-
num_threads = options.GetNumThreads()
118-
self.assertEqual(num_threads, 3)
119-
thread_at_last_index = options.GetThreadAtIndex(2)
120-
self.assertEqual(thread_at_last_index.id, front_thread.id)
99+
thread_collection = options.GetThreadsToSave()
100+
self.assertTrue(thread_collection is not None)
101+
self.assertEqual(thread_collection.GetSize(), 2)
102+
# error = options.AddThread(middle_thread)
103+
# self.assertTrue(error.Success())
104+
# thread_collection = options.GetThreadsToSave()
105+
# self.assertEqual(thread_collection.GetSize(), 3)
106+
# self.assertIn(middle_thread, thread_collection)

0 commit comments

Comments
 (0)