54 lines · cpp
1//===-- ReadMemoryRequestHandler.cpp --------------------------------------===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//8 9#include "DAP.h"10#include "JSONUtils.h"11#include "RequestHandler.h"12#include "llvm/ADT/StringExtras.h"13 14namespace lldb_dap {15 16// Reads bytes from memory at the provided location.17//18// Clients should only call this request if the corresponding capability19// `supportsReadMemoryRequest` is true20llvm::Expected<protocol::ReadMemoryResponseBody>21ReadMemoryRequestHandler::Run(const protocol::ReadMemoryArguments &args) const {22 const lldb::addr_t raw_address = args.memoryReference + args.offset;23 24 lldb::SBProcess process = dap.target.GetProcess();25 if (!lldb::SBDebugger::StateIsStoppedState(process.GetState()))26 return llvm::make_error<NotStoppedError>();27 28 const uint64_t count_read = std::max<uint64_t>(args.count, 1);29 // We also need support reading 0 bytes30 // VS Code sends those requests to check if a `memoryReference`31 // can be dereferenced.32 protocol::ReadMemoryResponseBody response;33 std::vector<std::byte> &buffer = response.data;34 buffer.resize(count_read);35 36 lldb::SBError error;37 const size_t memory_count = dap.target.GetProcess().ReadMemory(38 raw_address, buffer.data(), buffer.size(), error);39 40 response.address = 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 from44 // the last readable byte.45 if (error.Fail() && (memory_count < count_read)) {46 response.unreadableBytes = count_read - memory_count;47 }48 49 buffer.resize(std::min<size_t>(memory_count, args.count));50 return response;51}52 53} // namespace lldb_dap54