|
7 | 7 | //===----------------------------------------------------------------------===//
|
8 | 8 |
|
9 | 9 | #include "DAP.h"
|
10 |
| -#include "EventHelper.h" |
11 | 10 | #include "JSONUtils.h"
|
12 | 11 | #include "RequestHandler.h"
|
13 | 12 | #include "llvm/ADT/StringExtras.h"
|
14 |
| -#include "llvm/Support/Base64.h" |
15 | 13 |
|
16 | 14 | namespace lldb_dap {
|
17 | 15 |
|
18 |
| -// "ReadMemoryRequest": { |
19 |
| -// "allOf": [ { "$ref": "#/definitions/Request" }, { |
20 |
| -// "type": "object", |
21 |
| -// "description": "Reads bytes from memory at the provided location. Clients |
22 |
| -// should only call this request if the corresponding |
23 |
| -// capability `supportsReadMemoryRequest` is true.", |
24 |
| -// "properties": { |
25 |
| -// "command": { |
26 |
| -// "type": "string", |
27 |
| -// "enum": [ "readMemory" ] |
28 |
| -// }, |
29 |
| -// "arguments": { |
30 |
| -// "$ref": "#/definitions/ReadMemoryArguments" |
31 |
| -// } |
32 |
| -// }, |
33 |
| -// "required": [ "command", "arguments" ] |
34 |
| -// }] |
35 |
| -// }, |
36 |
| -// "ReadMemoryArguments": { |
37 |
| -// "type": "object", |
38 |
| -// "description": "Arguments for `readMemory` request.", |
39 |
| -// "properties": { |
40 |
| -// "memoryReference": { |
41 |
| -// "type": "string", |
42 |
| -// "description": "Memory reference to the base location from which data |
43 |
| -// should be read." |
44 |
| -// }, |
45 |
| -// "offset": { |
46 |
| -// "type": "integer", |
47 |
| -// "description": "Offset (in bytes) to be applied to the reference |
48 |
| -// location before reading data. Can be negative." |
49 |
| -// }, |
50 |
| -// "count": { |
51 |
| -// "type": "integer", |
52 |
| -// "description": "Number of bytes to read at the specified location and |
53 |
| -// offset." |
54 |
| -// } |
55 |
| -// }, |
56 |
| -// "required": [ "memoryReference", "count" ] |
57 |
| -// }, |
58 |
| -// "ReadMemoryResponse": { |
59 |
| -// "allOf": [ { "$ref": "#/definitions/Response" }, { |
60 |
| -// "type": "object", |
61 |
| -// "description": "Response to `readMemory` request.", |
62 |
| -// "properties": { |
63 |
| -// "body": { |
64 |
| -// "type": "object", |
65 |
| -// "properties": { |
66 |
| -// "address": { |
67 |
| -// "type": "string", |
68 |
| -// "description": "The address of the first byte of data returned. |
69 |
| -// Treated as a hex value if prefixed with `0x`, or |
70 |
| -// as a decimal value otherwise." |
71 |
| -// }, |
72 |
| -// "unreadableBytes": { |
73 |
| -// "type": "integer", |
74 |
| -// "description": "The number of unreadable bytes encountered after |
75 |
| -// the last successfully read byte.\nThis can be |
76 |
| -// used to determine the number of bytes that should |
77 |
| -// be skipped before a subsequent |
78 |
| -// `readMemory` request succeeds." |
79 |
| -// }, |
80 |
| -// "data": { |
81 |
| -// "type": "string", |
82 |
| -// "description": "The bytes read from memory, encoded using base64. |
83 |
| -// If the decoded length of `data` is less than the |
84 |
| -// requested `count` in the original `readMemory` |
85 |
| -// request, and `unreadableBytes` is zero or |
86 |
| -// omitted, then the client should assume it's |
87 |
| -// reached the end of readable memory." |
88 |
| -// } |
89 |
| -// }, |
90 |
| -// "required": [ "address" ] |
91 |
| -// } |
92 |
| -// } |
93 |
| -// }] |
94 |
| -// }, |
95 |
| -void ReadMemoryRequestHandler::operator()( |
96 |
| - const llvm::json::Object &request) const { |
97 |
| - llvm::json::Object response; |
98 |
| - FillResponse(request, response); |
99 |
| - auto *arguments = request.getObject("arguments"); |
| 16 | +// Reads bytes from memory at the provided location. |
| 17 | +// |
| 18 | +// Clients should only call this request if the corresponding capability |
| 19 | +// `supportsReadMemoryRequest` is true |
| 20 | +llvm::Expected<protocol::ReadMemoryResponseBody> |
| 21 | +ReadMemoryRequestHandler::Run(const protocol::ReadMemoryArguments &args) const { |
| 22 | + const lldb::addr_t raw_address = args.memoryReference + args.offset; |
100 | 23 |
|
101 |
| - llvm::StringRef memoryReference = |
102 |
| - GetString(arguments, "memoryReference").value_or(""); |
103 |
| - auto addr_opt = DecodeMemoryReference(memoryReference); |
104 |
| - if (!addr_opt.has_value()) { |
105 |
| - response["success"] = false; |
106 |
| - response["message"] = |
107 |
| - "Malformed memory reference: " + memoryReference.str(); |
108 |
| - dap.SendJSON(llvm::json::Value(std::move(response))); |
109 |
| - return; |
110 |
| - } |
111 |
| - lldb::addr_t addr_int = *addr_opt; |
112 |
| - addr_int += GetInteger<uint64_t>(arguments, "offset").value_or(0); |
113 |
| - const uint64_t count_requested = |
114 |
| - GetInteger<uint64_t>(arguments, "count").value_or(0); |
| 24 | + lldb::SBProcess process = dap.target.GetProcess(); |
| 25 | + if (!lldb::SBDebugger::StateIsStoppedState(process.GetState())) |
| 26 | + return llvm::make_error<NotStoppedError>(); |
115 | 27 |
|
| 28 | + const uint64_t count_read = std::max<uint64_t>(args.count, 1); |
116 | 29 | // We also need support reading 0 bytes
|
117 | 30 | // VS Code sends those requests to check if a `memoryReference`
|
118 | 31 | // can be dereferenced.
|
119 |
| - const uint64_t count_read = std::max<uint64_t>(count_requested, 1); |
120 |
| - std::vector<uint8_t> buf; |
121 |
| - buf.resize(count_read); |
| 32 | + protocol::ReadMemoryResponseBody response; |
| 33 | + std::vector<std::byte> &buffer = response.data; |
| 34 | + buffer.resize(count_read); |
| 35 | + |
122 | 36 | lldb::SBError error;
|
123 |
| - lldb::SBAddress addr{addr_int, dap.target}; |
124 |
| - size_t count_result = |
125 |
| - dap.target.ReadMemory(addr, buf.data(), count_read, error); |
126 |
| - if (count_result == 0) { |
127 |
| - response["success"] = false; |
128 |
| - EmplaceSafeString(response, "message", error.GetCString()); |
129 |
| - dap.SendJSON(llvm::json::Value(std::move(response))); |
130 |
| - return; |
| 37 | + const size_t memory_count = dap.target.GetProcess().ReadMemory( |
| 38 | + raw_address, buffer.data(), buffer.size(), error); |
| 39 | + |
| 40 | + response.address = "0x" + llvm::utohexstr(raw_address); |
| 41 | + |
| 42 | + // reading memory may fail for multiple reasons. memory not readable, |
| 43 | + // reading out of memory range and gaps in memory. return from |
| 44 | + // the last readable byte. |
| 45 | + if (error.Fail() && (memory_count < count_read)) { |
| 46 | + response.unreadableBytes = count_read - memory_count; |
131 | 47 | }
|
132 |
| - buf.resize(std::min<size_t>(count_result, count_requested)); |
133 | 48 |
|
134 |
| - llvm::json::Object body; |
135 |
| - std::string formatted_addr = "0x" + llvm::utohexstr(addr_int); |
136 |
| - body.try_emplace("address", formatted_addr); |
137 |
| - body.try_emplace("data", llvm::encodeBase64(buf)); |
138 |
| - response.try_emplace("body", std::move(body)); |
139 |
| - dap.SendJSON(llvm::json::Value(std::move(response))); |
| 49 | + buffer.resize(std::min<size_t>(memory_count, args.count)); |
| 50 | + return response; |
140 | 51 | }
|
141 | 52 |
|
142 | 53 | } // namespace lldb_dap
|
0 commit comments