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-exception
6//
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 capability
19// `supportsReadMemoryRequest` is true
20llvm::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(state: process.GetState()))
26 return llvm::make_error<NotStoppedError>();
27
28 const uint64_t count_read = std::max<uint64_t>(a: args.count, b: 1);
29 // We also need support reading 0 bytes
30 // 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(new_size: count_read);
35
36 lldb::SBError error;
37 const size_t memory_count = dap.target.GetProcess().ReadMemory(
38 addr: raw_address, buf: buffer.data(), size: 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 from
44 // the last readable byte.
45 if (error.Fail() && (memory_count < count_read)) {
46 response.unreadableBytes = count_read - memory_count;
47 }
48
49 buffer.resize(new_size: std::min<size_t>(a: memory_count, b: args.count));
50 return response;
51}
52
53} // namespace lldb_dap
54

source code of lldb/tools/lldb-dap/Handler/ReadMemoryRequestHandler.cpp