Skip to content

Commit 786dc5a

Browse files
authored
[lldb-dap] Simplify readMemory (#109485)
The `readMemory` request used the `MemoryRegionInfo` so it could also support short reads. Since #106532, this is no longer necessary, as mentioned by @labath in a comment on #104317. With this commit, we no longer set the `unreadableBytes` in the result. But this is optional, anyway, according to the spec, and afaik the VS Code UI does not make good use of `unreadableBytes`, anyway. We prefer `SBTarget::ReadMemory` over `SBProcess::ReadMemory`, because the `memory read` command also reads memory through the target instead of the process, and because users would expect the UI view and the results from memory read to be in-sync.
1 parent 2a29d24 commit 786dc5a

File tree

2 files changed

+23
-54
lines changed

2 files changed

+23
-54
lines changed

lldb/test/API/tools/lldb-dap/memory/TestDAP_memory.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,20 +93,22 @@ def test_readMemory(self):
9393

9494
# We can read the complete string
9595
mem = self.dap_server.request_readMemory(memref, 0, 5)["body"]
96-
self.assertEqual(mem["unreadableBytes"], 0)
9796
self.assertEqual(b64decode(mem["data"]), b"dead\0")
9897

98+
# We can read large chunks, potentially returning partial results
99+
mem = self.dap_server.request_readMemory(memref, 0, 4096)["body"]
100+
self.assertEqual(b64decode(mem["data"])[0:5], b"dead\0")
101+
99102
# Use an offset
100103
mem = self.dap_server.request_readMemory(memref, 2, 3)["body"]
101104
self.assertEqual(b64decode(mem["data"]), b"ad\0")
102105

103106
# Reads of size 0 are successful
104-
# VS-Code sends those in order to check if a `memoryReference` can actually be dereferenced.
107+
# VS Code sends those in order to check if a `memoryReference` can actually be dereferenced.
105108
mem = self.dap_server.request_readMemory(memref, 0, 0)
106109
self.assertEqual(mem["success"], True)
107110
self.assertEqual(mem["body"]["data"], "")
108111

109112
# Reads at offset 0x0 fail
110113
mem = self.dap_server.request_readMemory("0x0", 0, 6)
111114
self.assertEqual(mem["success"], False)
112-
self.assertEqual(mem["message"], "Memory region is not readable")

lldb/tools/lldb-dap/lldb-dap.cpp

Lines changed: 18 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -4422,14 +4422,6 @@ void request_readMemory(const llvm::json::Object &request) {
44224422
FillResponse(request, response);
44234423
auto *arguments = request.getObject("arguments");
44244424

4425-
lldb::SBProcess process = g_dap.target.GetProcess();
4426-
if (!process.IsValid()) {
4427-
response["success"] = false;
4428-
response["message"] = "No process running";
4429-
g_dap.SendJSON(llvm::json::Value(std::move(response)));
4430-
return;
4431-
}
4432-
44334425
llvm::StringRef memoryReference = GetString(arguments, "memoryReference");
44344426
auto addr_opt = DecodeMemoryReference(memoryReference);
44354427
if (!addr_opt.has_value()) {
@@ -4439,57 +4431,32 @@ void request_readMemory(const llvm::json::Object &request) {
44394431
g_dap.SendJSON(llvm::json::Value(std::move(response)));
44404432
return;
44414433
}
4442-
lldb::addr_t addr = *addr_opt;
4443-
4444-
addr += GetSigned(arguments, "offset", 0);
4445-
const uint64_t requested_count = GetUnsigned(arguments, "count", 0);
4446-
lldb::SBMemoryRegionInfo region_info;
4447-
lldb::SBError memreg_error = process.GetMemoryRegionInfo(addr, region_info);
4448-
if (memreg_error.Fail()) {
4449-
response["success"] = false;
4450-
EmplaceSafeString(response, "message",
4451-
"Unable to find memory region: " +
4452-
std::string(memreg_error.GetCString()));
4453-
g_dap.SendJSON(llvm::json::Value(std::move(response)));
4454-
return;
4455-
}
4456-
if (!region_info.IsReadable()) {
4434+
lldb::addr_t addr_int = *addr_opt;
4435+
addr_int += GetSigned(arguments, "offset", 0);
4436+
const uint64_t count_requested = GetUnsigned(arguments, "count", 0);
4437+
4438+
// We also need support reading 0 bytes
4439+
// VS Code sends those requests to check if a `memoryReference`
4440+
// can be dereferenced.
4441+
const uint64_t count_read = std::max<uint64_t>(count_requested, 1);
4442+
std::vector<uint8_t> buf;
4443+
buf.resize(count_read);
4444+
lldb::SBError error;
4445+
lldb::SBAddress addr{addr_int, g_dap.target};
4446+
size_t count_result =
4447+
g_dap.target.ReadMemory(addr, buf.data(), count_read, error);
4448+
if (count_result == 0) {
44574449
response["success"] = false;
4458-
response.try_emplace("message", "Memory region is not readable");
4450+
EmplaceSafeString(response, "message", error.GetCString());
44594451
g_dap.SendJSON(llvm::json::Value(std::move(response)));
44604452
return;
44614453
}
4462-
const uint64_t available_count =
4463-
std::min(requested_count, region_info.GetRegionEnd() - addr);
4464-
const uint64_t unavailable_count = requested_count - available_count;
4465-
4466-
std::vector<uint8_t> buf;
4467-
buf.resize(available_count);
4468-
if (available_count > 0) {
4469-
lldb::SBError memread_error;
4470-
uint64_t bytes_read =
4471-
process.ReadMemory(addr, buf.data(), available_count, memread_error);
4472-
if (memread_error.Fail()) {
4473-
response["success"] = false;
4474-
EmplaceSafeString(response, "message",
4475-
"Unable to read memory: " +
4476-
std::string(memread_error.GetCString()));
4477-
g_dap.SendJSON(llvm::json::Value(std::move(response)));
4478-
return;
4479-
}
4480-
if (bytes_read != available_count) {
4481-
response["success"] = false;
4482-
EmplaceSafeString(response, "message", "Unexpected, short read");
4483-
g_dap.SendJSON(llvm::json::Value(std::move(response)));
4484-
return;
4485-
}
4486-
}
4454+
buf.resize(std::min(count_result, count_requested));
44874455

44884456
llvm::json::Object body;
4489-
std::string formatted_addr = "0x" + llvm::utohexstr(addr);
4457+
std::string formatted_addr = "0x" + llvm::utohexstr(addr_int);
44904458
body.try_emplace("address", formatted_addr);
44914459
body.try_emplace("data", llvm::encodeBase64(buf));
4492-
body.try_emplace("unreadableBytes", unavailable_count);
44934460
response.try_emplace("body", std::move(body));
44944461
g_dap.SendJSON(llvm::json::Value(std::move(response)));
44954462
}

0 commit comments

Comments
 (0)