Skip to content

Commit 02bcc26

Browse files
committed
Implemplement a thread list that is currently unused for SBSaveCore. Run formatter
1 parent 0ee0eeb commit 02bcc26

File tree

7 files changed

+123
-0
lines changed

7 files changed

+123
-0
lines changed

lldb/include/lldb/API/SBSaveCoreOptions.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,30 @@ class LLDB_API SBSaveCoreOptions {
5353
/// \return The output file spec.
5454
SBFileSpec GetOutputFile() const;
5555

56+
/// Add a thread to save in the core file.
57+
///
58+
/// \param thread_id The thread ID to save.
59+
void AddThread(lldb::tid_t thread_id);
60+
61+
/// Remove a thread from the list of threads to save.
62+
///
63+
/// \param thread_id The thread ID to remove.
64+
/// \return True if the thread was removed, false if it was not in the list.
65+
bool RemoveThread(lldb::tid_t thread_id);
66+
67+
/// Get the number of threads to save. If this list is empty all threads will
68+
/// be saved.
69+
///
70+
/// \return The number of threads to save.
71+
uint32_t GetNumThreads() const;
72+
73+
/// Get the thread ID at the given index.
74+
///
75+
/// \param[in] index The index of the thread ID to get.
76+
/// \return The thread ID at the given index, or an error
77+
/// if there is no thread at the index.
78+
lldb::tid_t GetThreadAtIndex(uint32_t index, SBError &error) const;
79+
5680
/// Reset all options.
5781
void Clear();
5882

lldb/include/lldb/Symbol/SaveCoreOptions.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "lldb/lldb-types.h"
1515

1616
#include <optional>
17+
#include <set>
1718
#include <string>
1819

1920
namespace lldb_private {
@@ -32,12 +33,21 @@ class SaveCoreOptions {
3233
void SetOutputFile(lldb_private::FileSpec file);
3334
const std::optional<lldb_private::FileSpec> GetOutputFile() const;
3435

36+
void AddThread(lldb::tid_t tid);
37+
bool RemoveThread(lldb::tid_t tid);
38+
size_t GetNumThreads() const;
39+
int64_t GetThreadAtIndex(size_t index) const;
40+
bool ShouldSaveThread(lldb::tid_t tid) const;
41+
42+
Status EnsureValidConfiguration() const;
43+
3544
void Clear();
3645

3746
private:
3847
std::optional<std::string> m_plugin_name;
3948
std::optional<lldb_private::FileSpec> m_file;
4049
std::optional<lldb::SaveCoreStyle> m_style;
50+
std::set<lldb::tid_t> m_threads_to_save;
4151
};
4252
} // namespace lldb_private
4353

lldb/include/lldb/Target/Process.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -741,6 +741,11 @@ class Process : public std::enable_shared_from_this<Process>,
741741
Status CalculateCoreFileSaveRanges(lldb::SaveCoreStyle core_style,
742742
CoreFileMemoryRanges &ranges);
743743

744+
/// Helper function for Process::SaveCore(...) that calculates the thread list
745+
/// based upon options set within a given \a core_options object.
746+
ThreadCollection::ThreadIterable
747+
CalculateCoreFileThreadList(SaveCoreOptions &core_options);
748+
744749
protected:
745750
virtual JITLoaderList &GetJITLoaders();
746751

lldb/source/API/SBSaveCoreOptions.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,26 @@ lldb::SaveCoreStyle SBSaveCoreOptions::GetStyle() const {
7575
return m_opaque_up->GetStyle();
7676
}
7777

78+
void SBSaveCoreOptions::AddThread(lldb::tid_t tid) {
79+
m_opaque_up->AddThread(tid);
80+
}
81+
82+
bool SBSaveCoreOptions::RemoveThread(lldb::tid_t tid) {
83+
return m_opaque_up->RemoveThread(tid);
84+
}
85+
86+
uint32_t SBSaveCoreOptions::GetNumThreads() const {
87+
return m_opaque_up->GetNumThreads();
88+
}
89+
90+
lldb::tid_t SBSaveCoreOptions::GetThreadAtIndex(uint32_t idx,
91+
SBError &error) const {
92+
int64_t tid = m_opaque_up->GetThreadAtIndex(idx);
93+
if (tid == -1)
94+
error.SetErrorString("Invalid index");
95+
return 0;
96+
}
97+
7898
void SBSaveCoreOptions::Clear() {
7999
LLDB_INSTRUMENT_VA(this);
80100
m_opaque_up->Clear();

lldb/source/Core/PluginManager.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -714,6 +714,10 @@ Status PluginManager::SaveCore(const lldb::ProcessSP &process_sp,
714714
return error;
715715
}
716716

717+
error = options.EnsureValidConfiguration();
718+
if (error.Fail())
719+
return error;
720+
717721
if (!options.GetPluginName().has_value()) {
718722
// Try saving core directly from the process plugin first.
719723
llvm::Expected<bool> ret =

lldb/source/Symbol/SaveCoreOptions.cpp

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,56 @@ SaveCoreOptions::GetOutputFile() const {
4646
return m_file;
4747
}
4848

49+
void SaveCoreOptions::AddThread(lldb::tid_t tid) {
50+
if (m_threads_to_save.count(tid) == 0)
51+
m_threads_to_save.emplace(tid);
52+
}
53+
54+
bool SaveCoreOptions::RemoveThread(lldb::tid_t tid) {
55+
if (m_threads_to_save.count(tid) == 0) {
56+
m_threads_to_save.erase(tid);
57+
return true;
58+
}
59+
60+
return false;
61+
}
62+
63+
size_t SaveCoreOptions::GetNumThreads() const {
64+
return m_threads_to_save.size();
65+
}
66+
67+
int64_t SaveCoreOptions::GetThreadAtIndex(size_t index) const {
68+
auto iter = m_threads_to_save.begin();
69+
while (index >= 0 && iter != m_threads_to_save.end()) {
70+
if (index == 0)
71+
return *iter;
72+
index--;
73+
iter++;
74+
}
75+
76+
return -1;
77+
}
78+
79+
bool SaveCoreOptions::ShouldSaveThread(lldb::tid_t tid) const {
80+
return m_threads_to_save.count(tid) > 0;
81+
}
82+
83+
Status SaveCoreOptions::EnsureValidConfiguration() const {
84+
Status error;
85+
std::string error_str;
86+
if (!m_threads_to_save.empty() && GetStyle() == lldb::eSaveCoreFull) {
87+
error_str += "Cannot save a full core with a subset of threads\n";
88+
}
89+
90+
if (!error_str.empty())
91+
error.SetErrorString(error_str);
92+
93+
return error;
94+
}
95+
4996
void SaveCoreOptions::Clear() {
5097
m_file = std::nullopt;
5198
m_plugin_name = std::nullopt;
5299
m_style = std::nullopt;
100+
m_threads_to_save.clear();
53101
}

lldb/source/Target/Process.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6668,6 +6668,18 @@ Status Process::CalculateCoreFileSaveRanges(lldb::SaveCoreStyle core_style,
66686668
return Status(); // Success!
66696669
}
66706670

6671+
ThreadCollection::ThreadIterable
6672+
Process::CalculateCoreFileThreadList(SaveCoreOptions &core_options) {
6673+
ThreadCollection thread_list;
6674+
for (const auto &thread : m_thread_list.Threads()) {
6675+
if (core_options.ShouldSaveThread(thread->GetID())) {
6676+
thread_list.AddThread(thread);
6677+
}
6678+
}
6679+
6680+
return thread_list.Threads();
6681+
}
6682+
66716683
void Process::SetAddressableBitMasks(AddressableBits bit_masks) {
66726684
uint32_t low_memory_addr_bits = bit_masks.GetLowmemAddressableBits();
66736685
uint32_t high_memory_addr_bits = bit_masks.GetHighmemAddressableBits();

0 commit comments

Comments
 (0)