Skip to content

Commit 06edefa

Browse files
authored
[LLDB] Make the thread list for SBSaveCoreOptions iterable (#122541)
This patch adds the ability to get a thread at a give index, based on insertion order, for SBSaveCore Options. This is primarily to benefit scripts using SBSaveCore, and remove the need to have both options and a second collection if your script is tracking what threads need to be saved. Such as if you want to collect the source of all the threads to be saved after the Core is generated.
1 parent 842ce4e commit 06edefa

File tree

7 files changed

+78
-10
lines changed

7 files changed

+78
-10
lines changed

lldb/include/lldb/API/SBSaveCoreOptions.h

Lines changed: 9 additions & 0 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,11 +112,19 @@ class LLDB_API SBSaveCoreOptions {
111112
/// style specific regions.
112113
SBError AddMemoryRegionToSave(const SBMemoryRegionInfo &region);
113114

115+
/// Get an unsorted copy of all threads to save
116+
///
117+
/// \returns
118+
/// An unsorted copy of all threads to save. If no process is specified
119+
/// an empty collection will be returned.
120+
SBThreadCollection GetThreadsToSave() const;
121+
114122
/// Reset all options.
115123
void Clear();
116124

117125
protected:
118126
friend class SBProcess;
127+
friend class SBThreadCollection;
119128
lldb_private::SaveCoreOptions &ref() const;
120129

121130
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: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@
99
#ifndef LLDB_SOURCE_PLUGINS_OBJECTFILE_SaveCoreOPTIONS_H
1010
#define LLDB_SOURCE_PLUGINS_OBJECTFILE_SaveCoreOPTIONS_H
1111

12+
#include "lldb/Target/ThreadCollection.h"
1213
#include "lldb/Utility/FileSpec.h"
1314
#include "lldb/Utility/RangeMap.h"
1415

1516
#include <optional>
16-
#include <set>
1717
#include <string>
1818
#include <unordered_set>
1919

@@ -47,6 +47,8 @@ class SaveCoreOptions {
4747

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

50+
lldb_private::ThreadCollection::collection GetThreadsToSave() const;
51+
5052
void Clear();
5153

5254
private:

lldb/source/API/SBSaveCoreOptions.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "lldb/API/SBMemoryRegionInfo.h"
1111
#include "lldb/Host/FileSystem.h"
1212
#include "lldb/Symbol/SaveCoreOptions.h"
13+
#include "lldb/Target/ThreadCollection.h"
1314
#include "lldb/Utility/Instrumentation.h"
1415

1516
#include "Utils.h"
@@ -100,6 +101,14 @@ SBSaveCoreOptions::AddMemoryRegionToSave(const SBMemoryRegionInfo &region) {
100101
return SBError();
101102
}
102103

104+
lldb::SBThreadCollection SBSaveCoreOptions::GetThreadsToSave() const {
105+
LLDB_INSTRUMENT_VA(this);
106+
lldb::ThreadCollectionSP threadcollection_sp =
107+
std::make_shared<lldb_private::ThreadCollection>(
108+
m_opaque_up->GetThreadsToSave());
109+
return SBThreadCollection(threadcollection_sp);
110+
}
111+
103112
void SBSaveCoreOptions::Clear() {
104113
LLDB_INSTRUMENT_VA(this);
105114
m_opaque_up->Clear();

lldb/source/Symbol/SaveCoreOptions.cpp

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -114,9 +114,8 @@ void SaveCoreOptions::AddMemoryRegionToSave(
114114
const MemoryRanges &SaveCoreOptions::GetCoreFileMemoryRanges() const {
115115
return m_regions_to_save;
116116
}
117-
118-
Status SaveCoreOptions::EnsureValidConfiguration(
119-
lldb::ProcessSP process_sp) const {
117+
Status
118+
SaveCoreOptions::EnsureValidConfiguration(lldb::ProcessSP process_sp) const {
120119
Status error;
121120
std::string error_str;
122121
if (!m_threads_to_save.empty() && GetStyle() == lldb::eSaveCoreFull)
@@ -132,10 +131,24 @@ Status SaveCoreOptions::EnsureValidConfiguration(
132131
return error;
133132
}
134133

135-
void SaveCoreOptions::ClearProcessSpecificData() {
134+
lldb_private::ThreadCollection::collection
135+
SaveCoreOptions::GetThreadsToSave() const {
136+
lldb_private::ThreadCollection::collection thread_collection;
137+
// In cases where no process is set, such as when no threads are specified.
138+
if (!m_process_sp)
139+
return thread_collection;
140+
141+
ThreadList &thread_list = m_process_sp->GetThreadList();
142+
for (const auto &tid : m_threads_to_save)
143+
thread_collection.push_back(thread_list.FindThreadByID(tid));
144+
145+
return thread_collection;
146+
}
147+
148+
void SaveCoreOptions::ClearProcessSpecificData() {
136149
// Deliberately not following the formatter style here to indicate that
137150
// this method will be expanded in the future.
138-
m_threads_to_save.clear();
151+
m_threads_to_save.clear();
139152
}
140153

141154
void SaveCoreOptions::Clear() {

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

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,18 @@
44
from lldbsuite.test.decorators import *
55
from lldbsuite.test.lldbtest import *
66

7+
78
class SBSaveCoreOptionsAPICase(TestBase):
89
basic_minidump = "basic_minidump.yaml"
910
basic_minidump_different_pid = "basic_minidump_different_pid.yaml"
1011

1112
def get_process_from_yaml(self, yaml_file):
1213
minidump_path = self.getBuildArtifact(os.path.basename(yaml_file) + ".dmp")
13-
print ("minidump_path: " + minidump_path)
14+
print("minidump_path: " + minidump_path)
1415
self.yaml2obj(yaml_file, minidump_path)
15-
self.assertTrue(os.path.exists(minidump_path), "yaml2obj did not emit a minidump file")
16+
self.assertTrue(
17+
os.path.exists(minidump_path), "yaml2obj did not emit a minidump file"
18+
)
1619
target = self.dbg.CreateTarget(None)
1720
process = target.LoadCore(minidump_path)
1821
self.assertTrue(process.IsValid(), "Process is not valid")
@@ -59,7 +62,6 @@ def test_adding_and_removing_thread(self):
5962
removed_success = options.RemoveThread(thread)
6063
self.assertFalse(removed_success)
6164

62-
6365
def test_adding_thread_different_process(self):
6466
"""Test adding and removing a thread from save core options."""
6567
options = lldb.SBSaveCoreOptions()
@@ -79,3 +81,26 @@ def test_adding_thread_different_process(self):
7981
self.assertTrue(error.Fail())
8082
error = options.AddThread(thread)
8183
self.assertTrue(error.Success())
84+
85+
def test_removing_and_adding_insertion_order(self):
86+
"""Test insertion order is maintained when removing and adding threads."""
87+
options = lldb.SBSaveCoreOptions()
88+
process = self.get_basic_process()
89+
threads = []
90+
for x in range(0, 3):
91+
thread = process.GetThreadAtIndex(x)
92+
threads.append(thread)
93+
error = options.AddThread(thread)
94+
self.assertTrue(error.Success())
95+
96+
# Get the middle thread, remove it, and insert it back.
97+
middle_thread = threads[1]
98+
self.assertTrue(options.RemoveThread(middle_thread))
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)

lldb/test/API/python_api/sbsavecoreoptions/basic_minidump.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,13 @@ Streams:
2424
Stack:
2525
Start of Memory Range: 0x00007FFFC8D0E000
2626
Content: 'DEADBEEF'
27+
- Thread Id: 0x000074DE
28+
Context: 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000B0010000000000033000000000000000000000002020100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000040109600000000000100000000000000000000000000000068E7D0C8FF7F000068E7D0C8FF7F000097E6D0C8FF7F000010109600000000000000000000000000020000000000000088E4D0C8FF7F0000603FFF85C77F0000F00340000000000080E7D0C8FF7F000000000000000000000000000000000000E0034000000000007F0300000000000000000000000000000000000000000000801F0000FFFF00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF252525252525252525252525252525250000000000000000000000000000000000000000000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000FF00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
29+
Stack:
30+
Start of Memory Range: 0x00007FFFC8D0A000
31+
Content: 'BEEFDEAD'
32+
- Thread Id: 0x000074DF
33+
Context: 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000B0010000000000033000000000000000000000002020100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000040109600000000000100000000000000000000000000000068E7D0C8FF7F000068E7D0C8FF7F000097E6D0C8FF7F000010109600000000000000000000000000020000000000000088E4D0C8FF7F0000603FFF85C77F0000F00340000000000080E7D0C8FF7F000000000000000000000000000000000000E0034000000000007F0300000000000000000000000000000000000000000000801F0000FFFF00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF252525252525252525252525252525250000000000000000000000000000000000000000000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000FF00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
34+
Stack:
35+
Start of Memory Range: 0x00007FFFC8DFF000
36+
Content: 'BAADBEEF'

0 commit comments

Comments
 (0)