Skip to content

[NFC][lldb] Clean up LLDBMemoryReader changes. #11030

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 37 additions & 28 deletions lldb/source/Plugins/LanguageRuntime/Swift/LLDBMemoryReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -315,11 +315,11 @@ LLDBMemoryReader::resolvePointer(swift::remote::RemoteAddress address,

bool LLDBMemoryReader::readBytes(swift::remote::RemoteAddress address,
uint8_t *dest, uint64_t size) {
auto [success, _] = readBytesImpl(address, dest, size);
return success;
auto read_bytes_result = readBytesImpl(address, dest, size);
return read_bytes_result != ReadBytesResult::fail;
}

std::pair<bool, bool>
LLDBMemoryReader::ReadBytesResult
LLDBMemoryReader::readBytesImpl(swift::remote::RemoteAddress address,
uint8_t *dest, uint64_t size) {
Log *log = GetLog(LLDBLog::Types);
Expand All @@ -330,15 +330,15 @@ LLDBMemoryReader::readBytesImpl(swift::remote::RemoteAddress address,
if (overflow) {
LLDB_LOGV(log, "[MemoryReader] address {0:x} + size {1} overflows", addr,
size);
return {false, false};
return ReadBytesResult::fail;
}
if (addr >= *m_local_buffer &&
end <= *m_local_buffer + m_local_buffer_size) {
// If this crashes, the assumptions stated in
// GetDynamicTypeAndAddress_Protocol() most likely no longer
// hold.
memcpy(dest, (void *)addr, size);
return {true, false};
return ReadBytesResult::success_from_file;
}
}

Expand All @@ -353,7 +353,7 @@ LLDBMemoryReader::readBytesImpl(swift::remote::RemoteAddress address,
if (!maybeAddr) {
LLDB_LOGV(log, "[MemoryReader] could not resolve address {0:x}",
address.getRawAddress());
return {false, false};
return ReadBytesResult::fail;
}
auto addr = *maybeAddr;
if (addr.IsSectionOffset()) {
Expand All @@ -362,15 +362,16 @@ LLDBMemoryReader::readBytesImpl(swift::remote::RemoteAddress address,
if (object_file->GetType() == ObjectFile::Type::eTypeDebugInfo) {
LLDB_LOGV(log, "[MemoryReader] Reading memory from symbol rich binary");

bool success = object_file->ReadSectionData(section.get(),
addr.GetOffset(), dest, size);
return {success, false};
if (object_file->ReadSectionData(section.get(), addr.GetOffset(), dest,
size))
return ReadBytesResult::success_from_file;
return ReadBytesResult::fail;
}
}

if (size > m_max_read_amount) {
LLDB_LOGV(log, "[MemoryReader] memory read exceeds maximum allowed size");
return {false, false};
return ReadBytesResult::fail;
}
Target &target(m_process.GetTarget());
Status error;
Expand All @@ -384,12 +385,12 @@ LLDBMemoryReader::readBytesImpl(swift::remote::RemoteAddress address,
&did_read_live_memory)) {
LLDB_LOGV(log,
"[MemoryReader] memory read returned fewer bytes than asked for");
return {false, did_read_live_memory};
return ReadBytesResult::fail;
}
if (error.Fail()) {
LLDB_LOGV(log, "[MemoryReader] memory read returned error: {0}",
error.AsCString());
return {false, did_read_live_memory};
return ReadBytesResult::fail;
}

auto format_data = [](auto dest, auto size) {
Expand All @@ -403,7 +404,8 @@ LLDBMemoryReader::readBytesImpl(swift::remote::RemoteAddress address,
LLDB_LOGV(log, "[MemoryReader] memory read returned data: {0}",
format_data(dest, size));

return {true, did_read_live_memory};
return did_read_live_memory ? ReadBytesResult::success_from_memory
: ReadBytesResult::success_from_file;
}

bool LLDBMemoryReader::readString(swift::remote::RemoteAddress address,
Expand Down Expand Up @@ -613,24 +615,31 @@ bool LLDBMemoryReader::readRemoteAddressImpl(
std::size_t size) {
assert((size == 4 || size == 8) &&
"Only 32 or 64 bit architectures are supported!");
auto *dest = (uint8_t *)std::malloc(size);
auto defer = llvm::make_scope_exit([&] { free(dest); });

auto [success, did_read_live_memory] = readBytesImpl(address, dest, size);
if (!success)
if (size != 4 && size != 8)
return false;

uint8_t addressSpace = did_read_live_memory
? swift::remote::RemoteAddress::DefaultAddressSpace
: LLDBAddressSpace;
if (size == 4)
out = swift::remote::RemoteAddress(*reinterpret_cast<uint32_t *>(dest),
addressSpace);
else if (size == 8)
out = swift::remote::RemoteAddress(*reinterpret_cast<uint64_t *>(dest),
addressSpace);
else
uint64_t buf;
auto read_bytes_result =
readBytesImpl(address, reinterpret_cast<uint8_t *>(&buf), size);

uint8_t addressSpace;
switch (read_bytes_result) {
case ReadBytesResult::success_from_file:
addressSpace = LLDBAddressSpace;
break;
case ReadBytesResult::success_from_memory:
addressSpace = swift::remote::RemoteAddress::DefaultAddressSpace;
break;
case ReadBytesResult::fail:
return false;
}
ByteOrder byte_order = m_process.GetTarget().GetArchitecture().GetByteOrder();
uint32_t byte_size =
m_process.GetTarget().GetArchitecture().GetAddressByteSize();
DataExtractor extractor((const void *)&buf, size, byte_order, byte_size);
lldb::offset_t offset = 0;
auto data = extractor.GetMaxU64(&offset, size);
out = swift::remote::RemoteAddress(data, addressSpace);
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ class MemoryReaderLocalBufferHolder {

class LLDBMemoryReader : public swift::remote::MemoryReader {
public:
/// Besides address space 0 (the DefaultAddressSpace), subclasses are free to
/// use any address space for their own implementation purposes. LLDB uses
/// this address space to track file addresses it sends to RemoteInspection.
static constexpr uint8_t LLDBAddressSpace = 1;

LLDBMemoryReader(Process &p,
Expand Down Expand Up @@ -124,11 +127,12 @@ class LLDBMemoryReader : public swift::remote::MemoryReader {
std::optional<Address>
remoteAddressToLLDBAddress(swift::remote::RemoteAddress address) const;

enum class ReadBytesResult { fail, success_from_file, success_from_memory };
/// Implementation detail of readBytes. Returns a pair where the first element
/// indicates whether the memory was read successfully, the second element
/// indicates whether live memory was read.
std::pair<bool, bool> readBytesImpl(swift::remote::RemoteAddress address,
uint8_t *dest, uint64_t size);
ReadBytesResult readBytesImpl(swift::remote::RemoteAddress address,
uint8_t *dest, uint64_t size);

/// Reads memory from the symbol rich binary from the address into dest.
/// \return true if it was able to successfully read memory.
Expand Down