Skip to content

Commit fa220c0

Browse files
Jlalondadrian-prantl
authored andcommitted
[LLDB][SBSaveCore] Add selectable memory regions to SBSaveCore (llvm#105442)
This patch adds the option to specify specific memory ranges to be included in a given core file. The current implementation lets user specified ranges either be in addition to a certain save style, or independent of them via the newly added custom enum. To achieve being inclusive of save style, I've moved from a std::vector of ranges to a RangeDataVector, and to join overlapping ranges to prevent duplication of memory ranges in the core file. As a non function bonus, when SBSavecore was initially created, the header was included in the lldb-private interfaces, and I've fixed that and moved it the forward declare as an oversight. CC @bulbazord in case we need to include that into swift. (cherry picked from commit d517b22)
1 parent c1160b5 commit fa220c0

20 files changed

+302
-32
lines changed

lldb/include/lldb/API/SBMemoryRegionInfo.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ class LLDB_API SBMemoryRegionInfo {
120120
private:
121121
friend class SBProcess;
122122
friend class SBMemoryRegionInfoList;
123-
123+
friend class SBSaveCoreOptions;
124124
friend class lldb_private::ScriptInterpreter;
125125

126126
lldb_private::MemoryRegionInfo &ref();

lldb/include/lldb/API/SBSaveCoreOptions.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,17 @@ class LLDB_API SBSaveCoreOptions {
8080
/// \return True if the thread was removed, false if it was not in the list.
8181
bool RemoveThread(lldb::SBThread thread);
8282

83+
/// Add a memory region to save in the core file.
84+
///
85+
/// \param region The memory region to save.
86+
/// \returns An empty SBError upon success, or an error if the region is
87+
/// invalid.
88+
/// \note Ranges that overlapped will be unioned into a single region, this
89+
/// also supercedes stack minification. Specifying full regions and a
90+
/// non-custom core style will include the specified regions and union them
91+
/// with all style specific regions.
92+
SBError AddMemoryRegionToSave(const SBMemoryRegionInfo &region);
93+
8394
/// Reset all options.
8495
void Clear();
8596

lldb/include/lldb/Symbol/SaveCoreOptions.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,15 @@
1010
#define LLDB_SOURCE_PLUGINS_OBJECTFILE_SaveCoreOPTIONS_H
1111

1212
#include "lldb/Utility/FileSpec.h"
13-
#include "lldb/lldb-forward.h"
14-
#include "lldb/lldb-types.h"
13+
#include "lldb/Utility/RangeMap.h"
1514

1615
#include <optional>
16+
#include <set>
1717
#include <string>
1818
#include <unordered_set>
1919

20+
using MemoryRanges = lldb_private::RangeVector<lldb::addr_t, lldb::addr_t>;
21+
2022
namespace lldb_private {
2123

2224
class SaveCoreOptions {
@@ -38,8 +40,12 @@ class SaveCoreOptions {
3840
Status AddThread(lldb::ThreadSP thread_sp);
3941
bool RemoveThread(lldb::ThreadSP thread_sp);
4042
bool ShouldThreadBeSaved(lldb::tid_t tid) const;
43+
bool HasSpecifiedThreads() const;
4144

4245
Status EnsureValidConfiguration(lldb::ProcessSP process_sp) const;
46+
const MemoryRanges &GetCoreFileMemoryRanges() const;
47+
48+
void AddMemoryRegionToSave(const lldb_private::MemoryRegionInfo &region);
4349

4450
void Clear();
4551

@@ -51,6 +57,7 @@ class SaveCoreOptions {
5157
std::optional<lldb::SaveCoreStyle> m_style;
5258
lldb::ProcessSP m_process_sp;
5359
std::unordered_set<lldb::tid_t> m_threads_to_save;
60+
MemoryRanges m_regions_to_save;
5461
};
5562
} // namespace lldb_private
5663

lldb/include/lldb/Target/Process.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#include "lldb/Host/ProcessLaunchInfo.h"
3636
#include "lldb/Host/ProcessRunLock.h"
3737
#include "lldb/Symbol/ObjectFile.h"
38+
#include "lldb/Symbol/SaveCoreOptions.h"
3839
#include "lldb/Target/ExecutionContextScope.h"
3940
#include "lldb/Target/InstrumentationRuntime.h"
4041
#include "lldb/Target/Memory.h"
@@ -736,7 +737,9 @@ class Process : public std::enable_shared_from_this<Process>,
736737
}
737738
};
738739

739-
using CoreFileMemoryRanges = std::vector<CoreFileMemoryRange>;
740+
using CoreFileMemoryRanges =
741+
lldb_private::RangeDataVector<lldb::addr_t, lldb::addr_t,
742+
CoreFileMemoryRange>;
740743

741744
/// Helper function for Process::SaveCore(...) that calculates the address
742745
/// ranges that should be saved. This allows all core file plug-ins to save

lldb/include/lldb/Utility/RangeMap.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,8 @@ class RangeDataVector {
450450

451451
void Append(const Entry &entry) { m_entries.emplace_back(entry); }
452452

453+
void Append(B &&b, S &&s, T &&t) { m_entries.emplace_back(Entry(b, s, t)); }
454+
453455
bool Erase(uint32_t start, uint32_t end) {
454456
if (start >= end || end > m_entries.size())
455457
return false;

lldb/include/lldb/lldb-enumerations.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1251,6 +1251,7 @@ enum SaveCoreStyle {
12511251
eSaveCoreFull = 1,
12521252
eSaveCoreDirtyOnly = 2,
12531253
eSaveCoreStackOnly = 3,
1254+
eSaveCoreCustomOnly = 4,
12541255
};
12551256

12561257
/// Events that might happen during a trace session.

lldb/include/lldb/lldb-forward.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,7 @@ class StackFrameRecognizer;
206206
class StackFrameRecognizerManager;
207207
class StackID;
208208
class Status;
209+
class SaveCoreOptions;
209210
class StopInfo;
210211
class Stoppoint;
211212
class StoppointCallbackContext;

lldb/include/lldb/lldb-private-interfaces.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
#ifndef LLDB_LLDB_PRIVATE_INTERFACES_H
1010
#define LLDB_LLDB_PRIVATE_INTERFACES_H
1111

12-
#include "lldb/Symbol/SaveCoreOptions.h"
1312
#include "lldb/lldb-enumerations.h"
1413
#include "lldb/lldb-forward.h"
1514
#include "lldb/lldb-private-enumerations.h"

lldb/source/API/SBSaveCoreOptions.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "lldb/API/SBSaveCoreOptions.h"
10+
#include "lldb/API/SBMemoryRegionInfo.h"
1011
#include "lldb/Host/FileSystem.h"
1112
#include "lldb/Symbol/SaveCoreOptions.h"
1213
#include "lldb/Utility/Instrumentation.h"
@@ -90,6 +91,16 @@ bool SBSaveCoreOptions::RemoveThread(lldb::SBThread thread) {
9091
return m_opaque_up->RemoveThread(thread.GetSP());
9192
}
9293

94+
lldb::SBError
95+
SBSaveCoreOptions::AddMemoryRegionToSave(const SBMemoryRegionInfo &region) {
96+
LLDB_INSTRUMENT_VA(this, region);
97+
// Currently add memory region can't fail, so we always return a success
98+
// SBerror, but because these API's live forever, this is the most future
99+
// proof thing to do.
100+
m_opaque_up->AddMemoryRegionToSave(region.ref());
101+
return SBError();
102+
}
103+
93104
void SBSaveCoreOptions::Clear() {
94105
LLDB_INSTRUMENT_VA(this);
95106
m_opaque_up->Clear();

lldb/source/Commands/CommandObjectProcess.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "lldb/Interpreter/OptionArgParser.h"
2626
#include "lldb/Interpreter/OptionGroupPythonClassWithDict.h"
2727
#include "lldb/Interpreter/Options.h"
28+
#include "lldb/Symbol/SaveCoreOptions.h"
2829
#include "lldb/Target/Platform.h"
2930
#include "lldb/Target/Process.h"
3031
#include "lldb/Target/StopInfo.h"

0 commit comments

Comments
 (0)