Skip to content

Commit f2330e6

Browse files
authored
Merge pull request #4176 from apple/jdevlieghere/rdar/74890607
[lldb] Refactor DataBuffer so we can map files as read-only
2 parents da18402 + d90d25c commit f2330e6

File tree

145 files changed

+1391
-581
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

145 files changed

+1391
-581
lines changed

lldb/include/lldb/Core/ValueObject.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -688,7 +688,7 @@ class ValueObject {
688688
bool IsCStringContainer(bool check_pointer = false);
689689

690690
std::pair<size_t, bool>
691-
ReadPointedString(lldb::DataBufferSP &buffer_sp, Status &error,
691+
ReadPointedString(lldb::WritableDataBufferSP &buffer_sp, Status &error,
692692
uint32_t max_length = 0, bool honor_array = true,
693693
lldb::Format item_format = lldb::eFormatCharArray);
694694

lldb/include/lldb/Host/FileSystem.h

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#define LLDB_HOST_FILESYSTEM_H
1111

1212
#include "lldb/Host/File.h"
13-
#include "lldb/Utility/DataBufferLLVM.h"
13+
#include "lldb/Utility/DataBuffer.h"
1414
#include "lldb/Utility/FileSpec.h"
1515
#include "lldb/Utility/Status.h"
1616

@@ -144,12 +144,18 @@ class FileSystem {
144144

145145
//// Create memory buffer from path.
146146
/// \{
147-
std::shared_ptr<DataBufferLLVM> CreateDataBuffer(const llvm::Twine &path,
148-
uint64_t size = 0,
149-
uint64_t offset = 0);
150-
std::shared_ptr<DataBufferLLVM> CreateDataBuffer(const FileSpec &file_spec,
151-
uint64_t size = 0,
152-
uint64_t offset = 0);
147+
std::shared_ptr<DataBuffer> CreateDataBuffer(const llvm::Twine &path,
148+
uint64_t size = 0,
149+
uint64_t offset = 0);
150+
std::shared_ptr<DataBuffer> CreateDataBuffer(const FileSpec &file_spec,
151+
uint64_t size = 0,
152+
uint64_t offset = 0);
153+
std::shared_ptr<WritableDataBuffer>
154+
CreateWritableDataBuffer(const llvm::Twine &path, uint64_t size = 0,
155+
uint64_t offset = 0);
156+
std::shared_ptr<WritableDataBuffer>
157+
CreateWritableDataBuffer(const FileSpec &file_spec, uint64_t size = 0,
158+
uint64_t offset = 0);
153159
/// \}
154160

155161
/// Call into the Host to see if it can help find the file.

lldb/include/lldb/Host/common/NativeRegisterContext.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ class NativeRegisterContext
5151
virtual Status WriteRegister(const RegisterInfo *reg_info,
5252
const RegisterValue &reg_value) = 0;
5353

54-
virtual Status ReadAllRegisterValues(lldb::DataBufferSP &data_sp) = 0;
54+
virtual Status ReadAllRegisterValues(lldb::WritableDataBufferSP &data_sp) = 0;
5555

5656
virtual Status WriteAllRegisterValues(const lldb::DataBufferSP &data_sp) = 0;
5757

lldb/include/lldb/Symbol/CompactUnwindInfo.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,9 +138,10 @@ class CompactUnwindInfo {
138138

139139
ObjectFile &m_objfile;
140140
lldb::SectionSP m_section_sp;
141-
lldb::DataBufferSP m_section_contents_if_encrypted; // if the binary is
142-
// encrypted, read the
143-
// sect contents
141+
lldb::WritableDataBufferSP
142+
m_section_contents_if_encrypted; // if the binary is
143+
// encrypted, read the
144+
// sect contents
144145
// out of live memory and cache them here
145146
std::mutex m_mutex;
146147
std::vector<UnwindIndex> m_indexes;

lldb/include/lldb/Symbol/ObjectFile.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,10 +118,10 @@ class ObjectFile : public std::enable_shared_from_this<ObjectFile>,
118118
/// more than one architecture or object.
119119
ObjectFile(const lldb::ModuleSP &module_sp, const FileSpec *file_spec_ptr,
120120
lldb::offset_t file_offset, lldb::offset_t length,
121-
const lldb::DataBufferSP &data_sp, lldb::offset_t data_offset);
121+
lldb::DataBufferSP data_sp, lldb::offset_t data_offset);
122122

123123
ObjectFile(const lldb::ModuleSP &module_sp, const lldb::ProcessSP &process_sp,
124-
lldb::addr_t header_addr, lldb::DataBufferSP &data_sp);
124+
lldb::addr_t header_addr, lldb::DataBufferSP data_sp);
125125

126126
/// Destructor.
127127
///
@@ -185,7 +185,7 @@ class ObjectFile : public std::enable_shared_from_this<ObjectFile>,
185185
static lldb::ObjectFileSP FindPlugin(const lldb::ModuleSP &module_sp,
186186
const lldb::ProcessSP &process_sp,
187187
lldb::addr_t header_addr,
188-
lldb::DataBufferSP &file_data_sp);
188+
lldb::WritableDataBufferSP file_data_sp);
189189

190190
static size_t
191191
GetModuleSpecifications(const FileSpec &file, lldb::offset_t file_offset,
@@ -744,6 +744,9 @@ class ObjectFile : public std::enable_shared_from_this<ObjectFile>,
744744
/// false otherwise.
745745
bool SetModulesArchitecture(const ArchSpec &new_arch);
746746

747+
/// The number of bytes to read when going through the plugins.
748+
static size_t g_initial_bytes_to_read;
749+
747750
static lldb::DataBufferSP MapFileData(const FileSpec &file, uint64_t Size,
748751
uint64_t Offset);
749752

lldb/include/lldb/Target/ProcessStructReader.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ class ProcessStructReader {
6969
auto total_size = struct_type.GetByteSize(nullptr);
7070
if (!total_size)
7171
return;
72-
lldb::DataBufferSP buffer_sp(new DataBufferHeap(*total_size, 0));
72+
lldb::WritableDataBufferSP buffer_sp(new DataBufferHeap(*total_size, 0));
7373
Status error;
7474
process->ReadMemoryFromInferior(base_addr, buffer_sp->GetBytes(),
7575
*total_size, error);

lldb/include/lldb/Target/RegisterCheckpoint.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,12 @@ class RegisterCheckpoint : public UserID {
3636

3737
~RegisterCheckpoint() = default;
3838

39-
lldb::DataBufferSP &GetData() { return m_data_sp; }
39+
lldb::WritableDataBufferSP &GetData() { return m_data_sp; }
4040

41-
const lldb::DataBufferSP &GetData() const { return m_data_sp; }
41+
const lldb::WritableDataBufferSP &GetData() const { return m_data_sp; }
4242

4343
protected:
44-
lldb::DataBufferSP m_data_sp;
44+
lldb::WritableDataBufferSP m_data_sp;
4545
Reason m_reason;
4646

4747
// Make RegisterCheckpointSP if you wish to share the data in this class.

lldb/include/lldb/Target/RegisterContext.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class RegisterContext : public std::enable_shared_from_this<RegisterContext>,
4343
virtual bool WriteRegister(const RegisterInfo *reg_info,
4444
const RegisterValue &reg_value) = 0;
4545

46-
virtual bool ReadAllRegisterValues(lldb::DataBufferSP &data_sp) {
46+
virtual bool ReadAllRegisterValues(lldb::WritableDataBufferSP &data_sp) {
4747
return false;
4848
}
4949

lldb/include/lldb/Target/RegisterContextUnwind.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ class RegisterContextUnwind : public lldb_private::RegisterContext {
5050
bool WriteRegister(const lldb_private::RegisterInfo *reg_info,
5151
const lldb_private::RegisterValue &value) override;
5252

53-
bool ReadAllRegisterValues(lldb::DataBufferSP &data_sp) override;
53+
bool ReadAllRegisterValues(lldb::WritableDataBufferSP &data_sp) override;
5454

5555
bool WriteAllRegisterValues(const lldb::DataBufferSP &data_sp) override;
5656

lldb/include/lldb/Utility/DataBuffer.h

Lines changed: 79 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@
2020
namespace lldb_private {
2121

2222
/// \class DataBuffer DataBuffer.h "lldb/Core/DataBuffer.h"
23-
/// A pure virtual protocol class for abstracted data buffers.
23+
/// A pure virtual protocol class for abstracted read only data buffers.
2424
///
25-
/// DataBuffer is an abstract class that gets packaged into a shared pointer
26-
/// that can use to implement various ways to store data (on the heap, memory
27-
/// mapped, cached inferior memory). It gets used by DataExtractor so many
28-
/// DataExtractor objects can share the same data and sub-ranges of that
25+
/// DataBuffer is an abstract class that gets packaged into a shared
26+
/// pointer that can use to implement various ways to store data (on the heap,
27+
/// memory mapped, cached inferior memory). It gets used by DataExtractor so
28+
/// many DataExtractor objects can share the same data and sub-ranges of that
2929
/// shared data, and the last object that contains a reference to the shared
3030
/// data will free it.
3131
///
@@ -42,52 +42,106 @@ namespace lldb_private {
4242
/// with some extra function calls to load the data before it gets accessed.
4343
class DataBuffer {
4444
public:
45-
/// Destructor
46-
///
47-
/// The destructor is virtual as other classes will inherit from this class
48-
/// and be downcast to the DataBuffer pure virtual interface. The virtual
49-
/// destructor ensures that destructing the base class will destruct the
50-
/// class that inherited from it correctly.
5145
virtual ~DataBuffer() = default;
5246

53-
/// Get a pointer to the data.
47+
/// Get the number of bytes in the data buffer.
5448
///
5549
/// \return
56-
/// A pointer to the bytes owned by this object, or NULL if the
57-
/// object contains no bytes.
58-
virtual uint8_t *GetBytes() = 0;
50+
/// The number of bytes this object currently contains.
51+
virtual lldb::offset_t GetByteSize() const = 0;
5952

6053
/// Get a const pointer to the data.
6154
///
6255
/// \return
6356
/// A const pointer to the bytes owned by this object, or NULL
6457
/// if the object contains no bytes.
65-
virtual const uint8_t *GetBytes() const = 0;
66-
67-
/// Get the number of bytes in the data buffer.
68-
///
69-
/// \return
70-
/// The number of bytes this object currently contains.
71-
virtual lldb::offset_t GetByteSize() const = 0;
58+
const uint8_t *GetBytes() const { return GetBytesImpl(); }
7259

7360
llvm::ArrayRef<uint8_t> GetData() const {
7461
return llvm::ArrayRef<uint8_t>(GetBytes(), GetByteSize());
7562
}
7663

64+
/// LLVM RTTI support.
65+
/// {
66+
static char ID;
67+
virtual bool isA(const void *ClassID) const { return ClassID == &ID; }
68+
static bool classof(const DataBuffer *data_buffer) {
69+
return data_buffer->isA(&ID);
70+
}
71+
/// }
72+
73+
protected:
74+
/// Get a const pointer to the data.
75+
///
76+
/// \return
77+
/// A const pointer to the bytes owned by this object, or NULL
78+
/// if the object contains no bytes.
79+
virtual const uint8_t *GetBytesImpl() const = 0;
80+
};
81+
82+
/// \class DataBuffer DataBuffer.h "lldb/Core/DataBuffer.h"
83+
/// A pure virtual protocol class for abstracted writable data buffers.
84+
///
85+
/// DataBuffer is an abstract class that gets packaged into a shared pointer
86+
/// that can use to implement various ways to store data (on the heap, memory
87+
/// mapped, cached inferior memory). It gets used by DataExtractor so many
88+
/// DataExtractor objects can share the same data and sub-ranges of that
89+
/// shared data, and the last object that contains a reference to the shared
90+
/// data will free it.
91+
class WritableDataBuffer : public DataBuffer {
92+
public:
93+
/// Destructor
94+
///
95+
/// The destructor is virtual as other classes will inherit from this class
96+
/// and be downcast to the DataBuffer pure virtual interface. The virtual
97+
/// destructor ensures that destructing the base class will destruct the
98+
/// class that inherited from it correctly.
99+
virtual ~WritableDataBuffer() = default;
100+
101+
using DataBuffer::GetBytes;
102+
using DataBuffer::GetData;
103+
104+
/// Get a pointer to the data.
105+
///
106+
/// \return
107+
/// A pointer to the bytes owned by this object, or NULL if the
108+
/// object contains no bytes.
109+
uint8_t *GetBytes() { return const_cast<uint8_t *>(GetBytesImpl()); }
110+
77111
llvm::MutableArrayRef<uint8_t> GetData() {
78112
return llvm::MutableArrayRef<uint8_t>(GetBytes(), GetByteSize());
79113
}
114+
115+
/// LLVM RTTI support.
116+
/// {
117+
static char ID;
118+
bool isA(const void *ClassID) const override {
119+
return ClassID == &ID || DataBuffer::isA(ClassID);
120+
}
121+
static bool classof(const DataBuffer *data_buffer) {
122+
return data_buffer->isA(&ID);
123+
}
124+
/// }
80125
};
81126

82-
class DataBufferUnowned : public DataBuffer {
127+
class DataBufferUnowned : public WritableDataBuffer {
83128
public:
84129
DataBufferUnowned(uint8_t *bytes, lldb::offset_t size)
85130
: m_bytes(bytes), m_size(size) {}
86131

87-
uint8_t *GetBytes() override { return m_bytes; }
88-
const uint8_t *GetBytes() const override { return m_bytes; }
132+
const uint8_t *GetBytesImpl() const override { return m_bytes; }
89133
lldb::offset_t GetByteSize() const override { return m_size; }
90134

135+
/// LLVM RTTI support.
136+
/// {
137+
static char ID;
138+
bool isA(const void *ClassID) const override {
139+
return ClassID == &ID || WritableDataBuffer::isA(ClassID);
140+
}
141+
static bool classof(const DataBuffer *data_buffer) {
142+
return data_buffer->isA(&ID);
143+
}
144+
/// }
91145
private:
92146
uint8_t *m_bytes;
93147
lldb::offset_t m_size;

lldb/include/lldb/Utility/DataBufferHeap.h

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ namespace lldb_private {
2727
/// pages in. Large amounts of data that comes from files should probably use
2828
/// DataBufferLLVM, which can intelligently determine when memory mapping is
2929
/// optimal.
30-
class DataBufferHeap : public DataBuffer {
30+
class DataBufferHeap : public WritableDataBuffer {
3131
public:
3232
/// Default constructor
3333
///
@@ -54,17 +54,20 @@ class DataBufferHeap : public DataBuffer {
5454
/// The number of bytes in \a src to copy.
5555
DataBufferHeap(const void *src, lldb::offset_t src_len);
5656

57+
/// Construct by making a copy of a DataBuffer.
58+
///
59+
/// \param[in] data_buffer
60+
/// A read only data buffer to copy.
61+
DataBufferHeap(const DataBuffer &data_buffer);
62+
5763
/// Destructor.
5864
///
5965
/// Virtual destructor since this class inherits from a pure virtual base
6066
/// class #DataBuffer.
6167
~DataBufferHeap() override;
6268

63-
/// \copydoc DataBuffer::GetBytes()
64-
uint8_t *GetBytes() override;
65-
6669
/// \copydoc DataBuffer::GetBytes() const
67-
const uint8_t *GetBytes() const override;
70+
const uint8_t *GetBytesImpl() const override;
6871

6972
/// \copydoc DataBuffer::GetByteSize() const
7073
lldb::offset_t GetByteSize() const override;
@@ -100,6 +103,17 @@ class DataBufferHeap : public DataBuffer {
100103

101104
void Clear();
102105

106+
/// LLVM RTTI support.
107+
/// {
108+
static char ID;
109+
bool isA(const void *ClassID) const override {
110+
return ClassID == &ID || WritableDataBuffer::isA(ClassID);
111+
}
112+
static bool classof(const DataBuffer *data_buffer) {
113+
return data_buffer->isA(&ID);
114+
}
115+
/// }
116+
103117
private:
104118
// This object uses a std::vector<uint8_t> to store its data. This takes care
105119
// of free the data when the object is deleted.

0 commit comments

Comments
 (0)