Skip to content

Commit 4871864

Browse files
committed
[lldb-dap] Simplify readMemory
The `readMemory` request used the `MemoryRegionInfo` so it could also support short reads. Since llvm#106532, this is no longer necessary. We no longer set the `unreadableBytes` in the result. But this is optional, anyway, and afaik the VS Code UI would not yet make good use of `unreadableBytes`, anyway.
1 parent 65c5706 commit 4871864

File tree

2 files changed

+21
-53
lines changed

2 files changed

+21
-53
lines changed

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,20 +93,19 @@ 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

9998
# Use an offset
10099
mem = self.dap_server.request_readMemory(memref, 2, 3)["body"]
101100
self.assertEqual(b64decode(mem["data"]), b"ad\0")
102101

103102
# Reads of size 0 are successful
104-
# VS-Code sends those in order to check if a `memoryReference` can actually be dereferenced.
103+
# VS Code sends those in order to check if a `memoryReference` can actually be dereferenced.
105104
mem = self.dap_server.request_readMemory(memref, 0, 0)
106105
self.assertEqual(mem["success"], True)
107106
self.assertEqual(mem["body"]["data"], "")
108107

109108
# Reads at offset 0x0 fail
110109
mem = self.dap_server.request_readMemory("0x0", 0, 6)
111110
self.assertEqual(mem["success"], False)
112-
self.assertEqual(mem["message"], "Memory region is not readable")
111+
self.assertEqual(mem["message"], "memory read failed for 0x0")

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

Lines changed: 19 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -4405,14 +4405,6 @@ void request_readMemory(const llvm::json::Object &request) {
44054405
FillResponse(request, response);
44064406
auto *arguments = request.getObject("arguments");
44074407

4408-
lldb::SBProcess process = g_dap.target.GetProcess();
4409-
if (!process.IsValid()) {
4410-
response["success"] = false;
4411-
response["message"] = "No process running";
4412-
g_dap.SendJSON(llvm::json::Value(std::move(response)));
4413-
return;
4414-
}
4415-
44164408
llvm::StringRef memoryReference = GetString(arguments, "memoryReference");
44174409
auto addr_opt = DecodeMemoryReference(memoryReference);
44184410
if (!addr_opt.has_value()) {
@@ -4422,57 +4414,34 @@ void request_readMemory(const llvm::json::Object &request) {
44224414
g_dap.SendJSON(llvm::json::Value(std::move(response)));
44234415
return;
44244416
}
4425-
lldb::addr_t addr = *addr_opt;
4417+
lldb::addr_t addr_int = *addr_opt;
4418+
addr_int += GetSigned(arguments, "offset", 0);
4419+
const uint64_t count_requested = GetUnsigned(arguments, "count", 0);
44264420

4427-
addr += GetSigned(arguments, "offset", 0);
4428-
const uint64_t requested_count = GetUnsigned(arguments, "count", 0);
4429-
lldb::SBMemoryRegionInfo region_info;
4430-
lldb::SBError memreg_error = process.GetMemoryRegionInfo(addr, region_info);
4431-
if (memreg_error.Fail()) {
4432-
response["success"] = false;
4433-
EmplaceSafeString(response, "message",
4434-
"Unable to find memory region: " +
4435-
std::string(memreg_error.GetCString()));
4436-
g_dap.SendJSON(llvm::json::Value(std::move(response)));
4437-
return;
4438-
}
4439-
if (!region_info.IsReadable()) {
4421+
lldb::SBAddress addr =
4422+
g_dap.target.ResolveLoadAddress(addr_int);
4423+
4424+
// We also need support reading 0 bytes
4425+
// VS Code sends those requests to check if a `memoryReference`
4426+
// can be dereferenced.
4427+
const uint64_t count_read = std::max<uint64_t>(count_requested, 1);
4428+
std::vector<uint8_t> buf;
4429+
buf.resize(count_read);
4430+
lldb::SBError error;
4431+
size_t count_result =
4432+
g_dap.target.ReadMemory(addr, buf.data(), count_read, error);
4433+
if (error.Fail()) {
44404434
response["success"] = false;
4441-
response.try_emplace("message", "Memory region is not readable");
4435+
EmplaceSafeString(response, "message", error.GetCString());
44424436
g_dap.SendJSON(llvm::json::Value(std::move(response)));
44434437
return;
44444438
}
4445-
const uint64_t available_count =
4446-
std::min(requested_count, region_info.GetRegionEnd() - addr);
4447-
const uint64_t unavailable_count = requested_count - available_count;
4448-
4449-
std::vector<uint8_t> buf;
4450-
buf.resize(available_count);
4451-
if (available_count > 0) {
4452-
lldb::SBError memread_error;
4453-
uint64_t bytes_read =
4454-
process.ReadMemory(addr, buf.data(), available_count, memread_error);
4455-
if (memread_error.Fail()) {
4456-
response["success"] = false;
4457-
EmplaceSafeString(response, "message",
4458-
"Unable to read memory: " +
4459-
std::string(memread_error.GetCString()));
4460-
g_dap.SendJSON(llvm::json::Value(std::move(response)));
4461-
return;
4462-
}
4463-
if (bytes_read != available_count) {
4464-
response["success"] = false;
4465-
EmplaceSafeString(response, "message", "Unexpected, short read");
4466-
g_dap.SendJSON(llvm::json::Value(std::move(response)));
4467-
return;
4468-
}
4469-
}
4439+
buf.resize(std::min(count_result, count_requested));
44704440

44714441
llvm::json::Object body;
4472-
std::string formatted_addr = "0x" + llvm::utohexstr(addr);
4442+
std::string formatted_addr = "0x" + llvm::utohexstr(addr_int);
44734443
body.try_emplace("address", formatted_addr);
44744444
body.try_emplace("data", llvm::encodeBase64(buf));
4475-
body.try_emplace("unreadableBytes", unavailable_count);
44764445
response.try_emplace("body", std::move(body));
44774446
g_dap.SendJSON(llvm::json::Value(std::move(response)));
44784447
}

0 commit comments

Comments
 (0)