Skip to content

Commit ef04502

Browse files
committed
Add new API to expose the expected size in bytes of a core before generation
1 parent 7d4e6ff commit ef04502

File tree

6 files changed

+64
-0
lines changed

6 files changed

+64
-0
lines changed

lldb/include/lldb/API/SBSaveCoreOptions.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,19 @@ class LLDB_API SBSaveCoreOptions {
119119
/// an empty collection will be returned.
120120
SBThreadCollection GetThreadsToSave() const;
121121

122+
/// Get the current total number of bytes the core is expected to be but not
123+
/// including the overhead of the core file format. Requires a Process and
124+
/// Style to be specified.
125+
///
126+
/// \note
127+
/// This can cause some modification of the underlying data store
128+
/// as regions with no permissions, or invalid permissions will be removed
129+
/// and stacks will be minified up to their stack pointer + the redzone.
130+
///
131+
/// \returns
132+
/// The expected size of the data contained in the core in bytes.
133+
uint64_t GetCurrentSizeInBytes(SBError &error);
134+
122135
/// Reset all options.
123136
void Clear();
124137

lldb/include/lldb/Symbol/SaveCoreOptions.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ class SaveCoreOptions {
4949

5050
lldb_private::ThreadCollection::collection GetThreadsToSave() const;
5151

52+
uint64_t GetCurrentSizeInBytes(Status &error);
53+
5254
void Clear();
5355

5456
private:

lldb/source/API/SBSaveCoreOptions.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,11 @@ void SBSaveCoreOptions::Clear() {
114114
m_opaque_up->Clear();
115115
}
116116

117+
uint64_t SBSaveCoreOptions::GetCurrentSizeInBytes(SBError &error) {
118+
LLDB_INSTRUMENT_VA(this, error);
119+
return m_opaque_up->GetCurrentSizeInBytes(error.ref());
120+
}
121+
117122
lldb_private::SaveCoreOptions &SBSaveCoreOptions::ref() const {
118123
return *m_opaque_up.get();
119124
}

lldb/source/Symbol/SaveCoreOptions.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,24 @@ SaveCoreOptions::GetThreadsToSave() const {
145145
return thread_collection;
146146
}
147147

148+
uint64_t SaveCoreOptions::GetCurrentSizeInBytes(Status &error) {
149+
if (!m_process_sp) {
150+
error = Status::FromErrorString("Requires a process to be set.");
151+
return 0;
152+
}
153+
154+
CoreFileMemoryRanges ranges;
155+
error = m_process_sp->CalculateCoreFileSaveRanges(*this, ranges);
156+
if (error.Fail())
157+
return 0;
158+
159+
uint64_t total_in_bytes = 0;
160+
for (auto& core_range : ranges)
161+
total_in_bytes += core_range.data.range.size();
162+
163+
return total_in_bytes;
164+
}
165+
148166
void SaveCoreOptions::ClearProcessSpecificData() {
149167
// Deliberately not following the formatter style here to indicate that
150168
// this method will be expanded in the future.

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,3 +104,24 @@ def test_removing_and_adding_insertion_order(self):
104104
thread_collection = options.GetThreadsToSave()
105105
self.assertEqual(thread_collection.GetSize(), 3)
106106
self.assertIn(middle_thread, thread_collection)
107+
108+
def test_get_total_in_bytes(self):
109+
"""
110+
Tests that get total in bytes properly returns an error without a process,
111+
and the readable regions with a process.
112+
"""
113+
114+
options = lldb.SBSaveCoreOptions()
115+
options.SetStyle(lldb.eSaveCoreCustomOnly)
116+
process = self.get_basic_process()
117+
memory_range = lldb.SBMemoryRegionInfo()
118+
process.GetMemoryRegionInfo(0x7FFF12A84030, memory_range)
119+
options.AddMemoryRegionToSave(memory_range)
120+
error = lldb.SBError()
121+
total = options.GetCurrentSizeInBytes(error)
122+
self.assertTrue(error.Fail(), error.GetCString())
123+
options.SetProcess(process)
124+
total = options.GetCurrentSizeInBytes(error)
125+
self.assertTrue(error.Success(), error.GetCString())
126+
expected_size = memory_range.GetRegionEnd() - memory_range.GetRegionBase()
127+
self.assertEqual(total, expected_size)

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,8 @@ Streams:
3434
Stack:
3535
Start of Memory Range: 0x00007FFFC8DFF000
3636
Content: 'BAADBEEF'
37+
- Type: Memory64List
38+
Memory Ranges:
39+
- Start of Memory Range: 0x7FFF12A84030
40+
Data Size: 0x2FD0
41+
Content : ''

0 commit comments

Comments
 (0)