Skip to content

Commit e0ccc47

Browse files
committed
Feedback, move over to a process specific world and operate on SBThread instead of tids. Run Formatter.
1 parent 2b03186 commit e0ccc47

File tree

9 files changed

+82
-64
lines changed

9 files changed

+82
-64
lines changed

lldb/include/lldb/API/SBProcess.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -586,6 +586,7 @@ class LLDB_API SBProcess {
586586
friend class SBBreakpointCallbackBaton;
587587
friend class SBBreakpointLocation;
588588
friend class SBCommandInterpreter;
589+
friend class SBSaveCoreOptions;
589590
friend class SBDebugger;
590591
friend class SBExecutionContext;
591592
friend class SBFunction;

lldb/include/lldb/API/SBSaveCoreOptions.h

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#define LLDB_API_SBSAVECOREOPTIONS_H
1111

1212
#include "lldb/API/SBDefines.h"
13+
#include "lldb/API/SBThread.h"
1314

1415
namespace lldb {
1516

@@ -53,29 +54,25 @@ class LLDB_API SBSaveCoreOptions {
5354
/// \return The output file spec.
5455
SBFileSpec GetOutputFile() const;
5556

57+
/// Set the process to save, or unset if supplied with a null process.
58+
///
59+
/// \param process The process to save.
60+
/// \return Success if process was set, otherwise an error
61+
/// \note This will clear all process specific options if
62+
/// an exisiting process is overriden.
63+
SBError SetProcess(lldb::SBProcess process);
64+
5665
/// Add a thread to save in the core file.
5766
///
58-
/// \param thread_id The thread ID to save.
59-
void AddThread(lldb::tid_t thread_id);
67+
/// \param thread The thread to save.
68+
/// \note This will set the process if it is not already set.
69+
SBError AddThread(lldb::SBThread thread);
6070

6171
/// Remove a thread from the list of threads to save.
6272
///
63-
/// \param thread_id The thread ID to remove.
73+
/// \param thread The thread to remove.
6474
/// \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;
75+
bool RemoveThread(lldb::SBThread thread);
7976

8077
/// Reset all options.
8178
void Clear();

lldb/include/lldb/API/SBThread.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,7 @@ class LLDB_API SBThread {
233233
friend class SBBreakpoint;
234234
friend class SBBreakpointLocation;
235235
friend class SBBreakpointCallbackBaton;
236+
friend class SBSaveCoreOptions;
236237
friend class SBExecutionContext;
237238
friend class SBFrame;
238239
friend class SBProcess;

lldb/include/lldb/Symbol/SaveCoreOptions.h

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
#include "lldb/lldb-forward.h"
1414
#include "lldb/lldb-types.h"
1515

16+
#include <map>
1617
#include <optional>
17-
#include <set>
1818
#include <string>
1919

2020
namespace lldb_private {
@@ -33,21 +33,24 @@ class SaveCoreOptions {
3333
void SetOutputFile(lldb_private::FileSpec file);
3434
const std::optional<lldb_private::FileSpec> GetOutputFile() const;
3535

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;
36+
Status SetProcess(lldb::ProcessSP process_sp);
37+
38+
Status AddThread(lldb_private::Thread *thread);
39+
bool RemoveThread(lldb_private::Thread *thread);
4040
bool ShouldSaveThread(lldb::tid_t tid) const;
4141

42-
Status EnsureValidConfiguration() const;
42+
Status EnsureValidConfiguration(lldb::ProcessSP process_to_save) const;
4343

4444
void Clear();
4545

4646
private:
47+
void ClearProcessSpecificData();
48+
4749
std::optional<std::string> m_plugin_name;
4850
std::optional<lldb_private::FileSpec> m_file;
4951
std::optional<lldb::SaveCoreStyle> m_style;
50-
std::set<lldb::tid_t> m_threads_to_save;
52+
std::optional<lldb::ProcessSP> m_process_sp;
53+
std::map<lldb::tid_t, lldb::ThreadSP> m_threads_to_save;
5154
};
5255
} // namespace lldb_private
5356

lldb/source/API/SBSaveCoreOptions.cpp

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
#include "lldb/API/SBSaveCoreOptions.h"
1010
#include "lldb/API/SBError.h"
1111
#include "lldb/API/SBFileSpec.h"
12+
#include "lldb/API/SBProcess.h"
13+
#include "lldb/API/SBThread.h"
1214
#include "lldb/Host/FileSystem.h"
1315
#include "lldb/Symbol/SaveCoreOptions.h"
1416
#include "lldb/Utility/Instrumentation.h"
@@ -75,24 +77,16 @@ lldb::SaveCoreStyle SBSaveCoreOptions::GetStyle() const {
7577
return m_opaque_up->GetStyle();
7678
}
7779

78-
void SBSaveCoreOptions::AddThread(lldb::tid_t tid) {
79-
m_opaque_up->AddThread(tid);
80+
SBError SBSaveCoreOptions::SetProcess(lldb::SBProcess process) {
81+
return m_opaque_up->SetProcess(process.GetSP());
8082
}
8183

82-
bool SBSaveCoreOptions::RemoveThread(lldb::tid_t tid) {
83-
return m_opaque_up->RemoveThread(tid);
84+
SBError SBSaveCoreOptions::AddThread(lldb::SBThread thread) {
85+
return m_opaque_up->AddThread(thread.get());
8486
}
8587

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;
88+
bool SBSaveCoreOptions::RemoveThread(lldb::SBThread thread) {
89+
return m_opaque_up->RemoveThread(thread.get());
9690
}
9791

9892
void SBSaveCoreOptions::Clear() {

lldb/source/Core/PluginManager.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -714,7 +714,7 @@ Status PluginManager::SaveCore(const lldb::ProcessSP &process_sp,
714714
return error;
715715
}
716716

717-
error = options.EnsureValidConfiguration();
717+
error = options.EnsureValidConfiguration(process_sp);
718718
if (error.Fail())
719719
return error;
720720

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ class MinidumpFileBuilder {
7878
const lldb::ProcessSP &process_sp,
7979
const lldb_private::SaveCoreOptions &save_core_options)
8080
: m_process_sp(process_sp), m_core_file(std::move(core_file)),
81-
m_save_core_options(save_core_options) {};
81+
m_save_core_options(save_core_options){};
8282

8383
MinidumpFileBuilder(const MinidumpFileBuilder &) = delete;
8484
MinidumpFileBuilder &operator=(const MinidumpFileBuilder &) = delete;

lldb/source/Symbol/SaveCoreOptions.cpp

Lines changed: 45 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
#include "lldb/Symbol/SaveCoreOptions.h"
1010
#include "lldb/Core/PluginManager.h"
11+
#include "lldb/Target/Process.h"
12+
#include "lldb/Target/Thread.h"
1113

1214
using namespace lldb;
1315
using namespace lldb_private;
@@ -46,34 +48,48 @@ SaveCoreOptions::GetOutputFile() const {
4648
return m_file;
4749
}
4850

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-
}
51+
Status SaveCoreOptions::SetProcess(lldb::ProcessSP process_sp) {
52+
Status error;
53+
if (!process_sp) {
54+
ClearProcessSpecificData();
55+
m_process_sp = std::nullopt;
56+
return error;
57+
}
5358

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;
59+
if (!process_sp->IsValid()) {
60+
error.SetErrorString("Cannot assign an invalid process.");
61+
return error;
5862
}
5963

60-
return false;
61-
}
64+
if (m_process_sp.has_value())
65+
ClearProcessSpecificData();
6266

63-
size_t SaveCoreOptions::GetNumThreads() const {
64-
return m_threads_to_save.size();
67+
m_process_sp = process_sp;
68+
return error;
6569
}
6670

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++;
71+
Status SaveCoreOptions::AddThread(lldb_private::Thread *thread) {
72+
Status error;
73+
if (!thread) {
74+
error.SetErrorString("Thread is null");
7475
}
7576

76-
return -1;
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;
83+
}
84+
85+
std::pair<lldb::tid_t, lldb::ThreadSP> tid_pair(thread->GetID(),
86+
thread->GetBackingThread());
87+
m_threads_to_save.insert(tid_pair);
88+
return error;
89+
}
90+
91+
bool SaveCoreOptions::RemoveThread(lldb_private::Thread *thread) {
92+
return thread && m_threads_to_save.erase(thread->GetID()) > 0;
7793
}
7894

7995
bool SaveCoreOptions::ShouldSaveThread(lldb::tid_t tid) const {
@@ -83,19 +99,25 @@ bool SaveCoreOptions::ShouldSaveThread(lldb::tid_t tid) const {
8399
return m_threads_to_save.count(tid) > 0;
84100
}
85101

86-
Status SaveCoreOptions::EnsureValidConfiguration() const {
102+
Status SaveCoreOptions::EnsureValidConfiguration(
103+
lldb::ProcessSP process_to_save) const {
87104
Status error;
88105
std::string error_str;
89-
if (!m_threads_to_save.empty() && GetStyle() == lldb::eSaveCoreFull) {
106+
if (!m_threads_to_save.empty() && GetStyle() == lldb::eSaveCoreFull)
90107
error_str += "Cannot save a full core with a subset of threads\n";
91-
}
108+
109+
if (m_process_sp.has_value() && m_process_sp != process_to_save)
110+
error_str += "Cannot save core for process using supplied core options. "
111+
"Options were constructed targeting a different process. \n";
92112

93113
if (!error_str.empty())
94114
error.SetErrorString(error_str);
95115

96116
return error;
97117
}
98118

119+
void SaveCoreOptions::ClearProcessSpecificData() { m_threads_to_save.clear(); }
120+
99121
void SaveCoreOptions::Clear() {
100122
m_file = std::nullopt;
101123
m_plugin_name = std::nullopt;

lldb/test/API/functionalities/process_save_core_minidump/TestProcessSaveCoreMinidump.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ def test_save_linux_mini_dump_thread_options(self):
216216
)
217217
self.assertState(process.GetState(), lldb.eStateStopped)
218218

219-
thread_to_include = process.GetThreadAtIndex(0).GetThreadID()
219+
thread_to_include = process.GetThreadAtIndex(0)
220220
options = lldb.SBSaveCoreOptions()
221221
thread_subset_spec = lldb.SBFileSpec(thread_subset_dmp)
222222
options.AddThread(thread_to_include)

0 commit comments

Comments
 (0)