[Lldb-commits] [lldb] [lldb][lldb-dap] Added readMemory and WriteMemory, var type correction (PR #108036)

Pavel Labath via lldb-commits lldb-commits at lists.llvm.org
Wed Sep 11 00:34:11 PDT 2024


================
@@ -4332,6 +4339,232 @@ void request_setInstructionBreakpoints(const llvm::json::Object &request) {
   g_dap.SendJSON(llvm::json::Value(std::move(response)));
 }
 
+// "ReadMemoryRequest": {
+//   "allOf": [ { "$ref": "#/definitions/Request" }, {
+//     "type": "object",
+//     "description": "Reads bytes from memory at the provided location.\n
+//     Clients should only call this request if the corresponding capability
+//     `supportsReadMemoryRequest` is true.",
+//     "properties": {
+//       "command": {
+//         "type": "string",
+//         "enum": [ "readMemory" ]
+//       },
+//       "arguments": {
+//         "$ref": "#/definitions/ReadMemoryArguments"
+//       }
+//     },
+//     "required": [ "command", "arguments" ]
+//   }]
+// },
+// "ReadMemoryArguments": {
+//   "type": "object",
+//   "description": "Arguments for `readMemory` request.",
+//   "properties": {
+//     "memoryReference": {
+//       "type": "string",
+//       "description": "Memory reference to the base location from which data
+//       should be read."
+//     },
+//     "offset": {
+//       "type": "integer",
+//       "description": "Offset (in bytes) to be applied to the reference
+//       location before reading data. Can be negative."
+//     },
+//     "count": {
+//       "type": "integer",
+//       "description": "Number of bytes to read at the specified location and
+//       offset."
+//     }
+//   },
+//   "required": [ "memoryReference", "count" ]
+// },
+// "ReadMemoryResponse": {
+//   "allOf": [ { "$ref": "#/definitions/Response" }, {
+//     "type": "object",
+//     "description": "Response to `readMemory` request.",
+//     "properties": {
+//       "body": {
+//         "type": "object",
+//         "properties": {
+//           "address": {
+//             "type": "string",
+//             "description": "The address of the first byte of data returned.
+//             \nTreated as a hex value if prefixed with `0x`, or as a decimal
+//             value otherwise."
+//           },
+//           "unreadableBytes": {
+//             "type": "integer",
+//             "description": "The number of unreadable bytes encountered after
+//             the last successfully read byte.\nThis can be used to determine
+//             the number of bytes that should be skipped before a subsequent
+//             `readMemory` request succeeds."
+//           },
+//           "data": {
+//             "type": "string",
+//             "description": "The bytes read from memory, encoded using
+//             base64. If the decoded length of `data` is less than the
+//             requested `count` in the original `readMemory` request, and
+//             `unreadableBytes` is zero or omitted, then the client should
+//             assume it's reached the end of readable memory."
+//           }
+//         },
+//         "required": [ "address" ]
+//       }
+//     }
+//   }]
+// },
+void request_readMemory(const llvm::json::Object &request) {
+  llvm::json::Object response;
+  llvm::json::Array response_readMemory;
+  llvm::json::Object body;
+  lldb::SBError error;
+  FillResponse(request, response);
+
+  auto arguments = request.getObject("arguments");
+  llvm::StringRef memoryReference = GetString(arguments, "memoryReference");
+  const size_t count = GetUnsigned(arguments, "count", 0);
+  const auto offset = GetSigned(arguments, "offset", 0);
+
+  lldb::addr_t address;
+  memoryReference.getAsInteger(0, address);
+  lldb::addr_t address_offset = address + offset;
+  // address as string
+  std::string address_offset_str(std::to_string(address_offset));
+
+  // make storage for the memory to read
+  //  need to use a vector because VC++ 2019 errors out on char buffer[count]
+  std::vector<char> buffer(count);
+
+  // read the memory add to storage
+  lldb::SBProcess process = g_dap.target.GetProcess();
+  size_t countRead =
+      process.ReadMemory(address_offset, buffer.data(), count, error);
+  const llvm::StringRef inputBytes = {buffer.data(), countRead};
+  // return the read memory in base64
+  std::string data = llvm::encodeBase64(inputBytes);
+
+  // body: address, data? in spec formats
+  body.try_emplace("address", std::move(address_offset_str));
+  body.try_emplace("data", std::move(data));
----------------
labath wrote:

```suggestion
  // body: address, data? in spec formats
  body.try_emplace("address", std::move(address_offset_str));
  body.try_emplace("data", llvm::encodeBase64(inputBytes));
```

If you don't make `data` a variable, then there's no need to move from it. You can do the same thing with `address_offset_str`

https://github.com/llvm/llvm-project/pull/108036


More information about the lldb-commits mailing list