Skip to content

Commit 0d31bb6

Browse files
committed
Add read in chunks to process
1 parent c5b1f49 commit 0d31bb6

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

lldb/include/lldb/Target/Process.h

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1589,6 +1589,42 @@ class Process : public std::enable_shared_from_this<Process>,
15891589
size_t ReadMemoryFromInferior(lldb::addr_t vm_addr, void *buf, size_t size,
15901590
Status &error);
15911591

1592+
typedef lldb::IterationAction(ReadMemoryChunkCallback)(lldb::Status &,
1593+
DataBufferHeap &, lldb
1594+
: addr_t,
1595+
lldb::offset_t);
1596+
/// Read of memory from a process in discrete chunks, terminating
1597+
/// either when all bytes are read, or the supplied callback returns
1598+
/// IterationAction::Stop
1599+
///
1600+
/// \param[in] vm_addr
1601+
/// A virtual load address that indicates where to start reading
1602+
/// memory from.
1603+
///
1604+
/// \param[in] data
1605+
/// The data buffer heap to use to read the chunk. The chunk size
1606+
/// depends upon the byte size of the buffer.
1607+
///
1608+
/// \param[in] size
1609+
/// The number of bytes to read.
1610+
///
1611+
/// \param[in] callback
1612+
/// The callback to invoke when a chunk is read from memory.
1613+
/// \param[in] cacheReads
1614+
/// Whether to use the ReadMemory instead of ReadMemory inferior, defaults
1615+
/// to false.
1616+
///
1617+
/// \return
1618+
/// The number of bytes that were actually read into \a buf and
1619+
/// written to the provided callback.
1620+
/// If the returned number is greater than zero, yet less than \a
1621+
/// size, then this function will get called again with \a
1622+
/// vm_addr, \a buf, and \a size updated appropriately. Zero is
1623+
/// returned in the case of an error.
1624+
size_t ReadMemoryInChunks(lldb::addr_t vm_addr, DataBufferHeap &data,
1625+
size_t size, ReadMemoryChunkCallback callback,
1626+
bool cacheReads = false);
1627+
15921628
/// Read a NULL terminated C string from memory
15931629
///
15941630
/// This function will read a cache page at a time until the NULL

lldb/source/Target/Process.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2233,6 +2233,27 @@ size_t Process::ReadMemoryFromInferior(addr_t addr, void *buf, size_t size,
22332233
return bytes_read;
22342234
}
22352235

2236+
size_t Process::ReadMemoryInChunks(lldb::addr_t vm_addr, DataBufferHeap &data,
2237+
size_t size, ReadMemoryChunkCallback callback) {
2238+
uint64_t bytes_remaining = size;
2239+
uint64_t bytes_read = 0;
2240+
Status error;
2241+
while (bytes_remaining > 0) {
2242+
// Get the next read chunk size as the minimum of the remaining bytes and
2243+
// the write chunk max size.
2244+
const size_t bytes_to_read =
2245+
std::min(bytes_remaining, data.GetByteSize());
2246+
const lldb::addr_t current_addr = vm_addr + bytes_read
2247+
const size_t bytes_read_for_chunk =
2248+
m_process_sp->ReadMemory(current_addr,
2249+
data.GetBytes(), bytes_to_read, error);
2250+
if (callback(error, data, current_addr, bytes_read_for_chunk) == lldb::IterationAction::Stop)
2251+
break;
2252+
}
2253+
2254+
return bytes_read;
2255+
}
2256+
22362257
uint64_t Process::ReadUnsignedIntegerFromMemory(lldb::addr_t vm_addr,
22372258
size_t integer_byte_size,
22382259
uint64_t fail_value,

0 commit comments

Comments
 (0)