Skip to content

Commit 2e8ff5a

Browse files
committed
Move code over to thread_sp, and remove optional for process_sp. Add private get_sp for ThreadSP.
1 parent e0ccc47 commit 2e8ff5a

File tree

8 files changed

+38
-36
lines changed

8 files changed

+38
-36
lines changed

lldb/include/lldb/API/SBSaveCoreOptions.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ class LLDB_API SBSaveCoreOptions {
6565
/// Add a thread to save in the core file.
6666
///
6767
/// \param thread The thread to save.
68-
/// \note This will set the process if it is not already set.
68+
/// \note This will set the process if it is not already set, or return
69+
/// and error if the SBThread is not from the set process.
6970
SBError AddThread(lldb::SBThread thread);
7071

7172
/// Remove a thread from the list of threads to save.

lldb/include/lldb/API/SBThread.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,8 @@ class LLDB_API SBThread {
256256

257257
lldb::ExecutionContextRefSP m_opaque_sp;
258258

259+
lldb::ThreadSP get_sp() const;
260+
259261
lldb_private::Thread *operator->();
260262

261263
lldb_private::Thread *get();

lldb/include/lldb/Symbol/SaveCoreOptions.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ class SaveCoreOptions {
3535

3636
Status SetProcess(lldb::ProcessSP process_sp);
3737

38-
Status AddThread(lldb_private::Thread *thread);
39-
bool RemoveThread(lldb_private::Thread *thread);
40-
bool ShouldSaveThread(lldb::tid_t tid) const;
38+
Status AddThread(lldb::ThreadSP thread_sp);
39+
bool RemoveThread(lldb::ThreadSP thread_sp);
40+
bool ShouldThreadBeSaved(lldb::tid_t tid) const;
4141

4242
Status EnsureValidConfiguration(lldb::ProcessSP process_to_save) const;
4343

@@ -49,7 +49,7 @@ class SaveCoreOptions {
4949
std::optional<std::string> m_plugin_name;
5050
std::optional<lldb_private::FileSpec> m_file;
5151
std::optional<lldb::SaveCoreStyle> m_style;
52-
std::optional<lldb::ProcessSP> m_process_sp;
52+
lldb::ProcessSP m_process_sp;
5353
std::map<lldb::tid_t, lldb::ThreadSP> m_threads_to_save;
5454
};
5555
} // namespace lldb_private

lldb/include/lldb/Target/Process.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -743,6 +743,7 @@ class Process : public std::enable_shared_from_this<Process>,
743743

744744
/// Helper function for Process::SaveCore(...) that calculates the thread list
745745
/// based upon options set within a given \a core_options object.
746+
/// \note If there is no thread list defined, all threads will be saved.
746747
std::vector<lldb::ThreadSP>
747748
CalculateCoreFileThreadList(const SaveCoreOptions &core_options);
748749

lldb/source/API/SBSaveCoreOptions.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,11 @@ SBError SBSaveCoreOptions::SetProcess(lldb::SBProcess process) {
8282
}
8383

8484
SBError SBSaveCoreOptions::AddThread(lldb::SBThread thread) {
85-
return m_opaque_up->AddThread(thread.get());
85+
return m_opaque_up->AddThread(thread.get_sp());
8686
}
8787

8888
bool SBSaveCoreOptions::RemoveThread(lldb::SBThread thread) {
89-
return m_opaque_up->RemoveThread(thread.get());
89+
return m_opaque_up->RemoveThread(thread.get_sp());
9090
}
9191

9292
void SBSaveCoreOptions::Clear() {

lldb/source/API/SBThread.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1331,6 +1331,8 @@ bool SBThread::SafeToCallFunctions() {
13311331
return true;
13321332
}
13331333

1334+
lldb::ThreadSP SBThread::get_sp() const { return m_opaque_sp->GetThreadSP(); }
1335+
13341336
lldb_private::Thread *SBThread::operator->() {
13351337
return get();
13361338
}

lldb/source/Plugins/ObjectFile/Minidump/MinidumpFileBuilder.cpp

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,9 @@ Status MinidumpFileBuilder::AddHeaderAndCalculateDirectories() {
6969
m_expected_directories += 9;
7070

7171
// Go through all of the threads and check for exceptions.
72-
lldb_private::ThreadList thread_list = m_process_sp->GetThreadList();
73-
const uint32_t num_threads = thread_list.GetSize();
74-
for (uint32_t thread_idx = 0; thread_idx < num_threads; ++thread_idx) {
75-
ThreadSP thread_sp(thread_list.GetThreadAtIndex(thread_idx));
72+
std::vector<lldb::ThreadSP> threads =
73+
m_process_sp->CalculateCoreFileThreadList(m_save_core_options);
74+
for (const ThreadSP &thread_sp : threads) {
7675
StopInfoSP stop_info_sp = thread_sp->GetStopInfo();
7776
if (stop_info_sp) {
7877
const StopReason &stop_reason = stop_info_sp->GetStopReason();
@@ -610,10 +609,8 @@ Status MinidumpFileBuilder::AddThreadList() {
610609
m_thread_list_start = GetCurrentDataEndOffset();
611610
DataBufferHeap helper_data;
612611

613-
const uint32_t num_threads = thread_list.size();
614612
Log *log = GetLog(LLDBLog::Object);
615-
for (uint32_t thread_idx = 0; thread_idx < num_threads; ++thread_idx) {
616-
ThreadSP thread_sp = thread_list.at(thread_idx);
613+
for (const ThreadSP &thread_sp : thread_list) {
617614
RegisterContextSP reg_ctx_sp(thread_sp->GetRegisterContext());
618615

619616
if (!reg_ctx_sp) {
@@ -651,7 +648,7 @@ Status MinidumpFileBuilder::AddThreadList() {
651648
m_tid_to_reg_ctx[thread_sp->GetID()] = thread_context_memory_locator;
652649

653650
LLDB_LOGF(log, "AddThreadList for thread %d: thread_context %zu bytes",
654-
thread_idx, thread_context.size());
651+
thread_sp->GetIndexID(), thread_context.size());
655652
helper_data.AppendData(thread_context.data(), thread_context.size());
656653

657654
llvm::minidump::Thread t;
@@ -675,11 +672,10 @@ Status MinidumpFileBuilder::AddThreadList() {
675672
}
676673

677674
Status MinidumpFileBuilder::AddExceptions() {
678-
lldb_private::ThreadList thread_list = m_process_sp->GetThreadList();
675+
std::vector<ThreadSP> thread_list =
676+
m_process_sp->CalculateCoreFileThreadList(m_save_core_options);
679677
Status error;
680-
const uint32_t num_threads = thread_list.GetSize();
681-
for (uint32_t thread_idx = 0; thread_idx < num_threads; ++thread_idx) {
682-
ThreadSP thread_sp(thread_list.GetThreadAtIndex(thread_idx));
678+
for (const ThreadSP &thread_sp : thread_list) {
683679
StopInfoSP stop_info_sp = thread_sp->GetStopInfo();
684680
bool add_exception = false;
685681
if (stop_info_sp) {

lldb/source/Symbol/SaveCoreOptions.cpp

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ Status SaveCoreOptions::SetProcess(lldb::ProcessSP process_sp) {
5252
Status error;
5353
if (!process_sp) {
5454
ClearProcessSpecificData();
55-
m_process_sp = std::nullopt;
55+
m_process_sp = nullptr;
5656
return error;
5757
}
5858

@@ -61,38 +61,38 @@ Status SaveCoreOptions::SetProcess(lldb::ProcessSP process_sp) {
6161
return error;
6262
}
6363

64-
if (m_process_sp.has_value())
64+
if (m_process_sp)
6565
ClearProcessSpecificData();
6666

6767
m_process_sp = process_sp;
6868
return error;
6969
}
7070

71-
Status SaveCoreOptions::AddThread(lldb_private::Thread *thread) {
71+
Status SaveCoreOptions::AddThread(lldb::ThreadSP thread_sp) {
7272
Status error;
73-
if (!thread) {
73+
if (!thread_sp) {
7474
error.SetErrorString("Thread is null");
75+
return error;
7576
}
7677

77-
if (!m_process_sp.has_value())
78-
m_process_sp = thread->GetProcess();
79-
80-
if (m_process_sp.value() != thread->GetProcess()) {
81-
error.SetErrorString("Cannot add thread from a different process.");
82-
return error;
78+
if (m_process_sp) {
79+
if (m_process_sp != thread_sp->GetProcess()) {
80+
error.SetErrorString("Cannot add a thread from a different process.");
81+
return error;
82+
}
83+
} else {
84+
m_process_sp = thread_sp->GetProcess();
8385
}
8486

85-
std::pair<lldb::tid_t, lldb::ThreadSP> tid_pair(thread->GetID(),
86-
thread->GetBackingThread());
87-
m_threads_to_save.insert(tid_pair);
87+
m_threads_to_save[thread_sp->GetID()];
8888
return error;
8989
}
9090

91-
bool SaveCoreOptions::RemoveThread(lldb_private::Thread *thread) {
92-
return thread && m_threads_to_save.erase(thread->GetID()) > 0;
91+
bool SaveCoreOptions::RemoveThread(lldb::ThreadSP thread_sp) {
92+
return thread_sp && m_threads_to_save.erase(thread_sp->GetID()) > 0;
9393
}
9494

95-
bool SaveCoreOptions::ShouldSaveThread(lldb::tid_t tid) const {
95+
bool SaveCoreOptions::ShouldThreadBeSaved(lldb::tid_t tid) const {
9696
// If the user specified no threads to save, then we save all threads.
9797
if (m_threads_to_save.empty())
9898
return true;
@@ -106,7 +106,7 @@ Status SaveCoreOptions::EnsureValidConfiguration(
106106
if (!m_threads_to_save.empty() && GetStyle() == lldb::eSaveCoreFull)
107107
error_str += "Cannot save a full core with a subset of threads\n";
108108

109-
if (m_process_sp.has_value() && m_process_sp != process_to_save)
109+
if (m_process_sp != process_to_save)
110110
error_str += "Cannot save core for process using supplied core options. "
111111
"Options were constructed targeting a different process. \n";
112112

0 commit comments

Comments
 (0)