Skip to content

Commit 71f027c

Browse files
committed
[lldb-dap] add review changes
1 parent 1df2622 commit 71f027c

File tree

7 files changed

+95
-78
lines changed

7 files changed

+95
-78
lines changed

lldb/tools/lldb-dap/Handler/ReadMemoryRequestHandler.cpp

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,17 @@
1010
#include "JSONUtils.h"
1111
#include "RequestHandler.h"
1212
#include "llvm/ADT/StringExtras.h"
13-
#include "llvm/Support/Base64.h"
1413

1514
namespace lldb_dap {
1615

1716
// Reads bytes from memory at the provided location.
1817
//
1918
// Clients should only call this request if the corresponding capability
2019
// `supportsReadMemoryRequest` is true
21-
llvm::Expected<protocol::ReadMemoryResponse>
20+
llvm::Expected<protocol::ReadMemoryResponseBody>
2221
ReadMemoryRequestHandler::Run(const protocol::ReadMemoryArguments &args) const {
23-
24-
const std::optional<lldb::addr_t> addr_opt =
25-
DecodeMemoryReference(args.memoryReference);
26-
if (!addr_opt)
27-
return llvm::make_error<DAPError>(
28-
llvm::formatv("Malformed memory reference: {}", args.memoryReference));
29-
30-
const lldb::addr_t raw_address = *addr_opt + args.offset.value_or(0);
22+
const lldb::addr_t raw_address =
23+
args.memoryReference + args.offset.value_or(0);
3124

3225
lldb::SBProcess process = dap.target.GetProcess();
3326
if (!lldb::SBDebugger::StateIsStoppedState(process.GetState()))
@@ -37,13 +30,14 @@ ReadMemoryRequestHandler::Run(const protocol::ReadMemoryArguments &args) const {
3730
// We also need support reading 0 bytes
3831
// VS Code sends those requests to check if a `memoryReference`
3932
// can be dereferenced.
40-
auto buffer = std::vector<uint8_t>(count_read);
33+
protocol::ReadMemoryResponseBody response;
34+
std::vector<std::byte> &buffer = response.data;
35+
buffer.resize(count_read);
4136

4237
lldb::SBError error;
4338
const size_t memory_count = dap.target.GetProcess().ReadMemory(
4439
raw_address, buffer.data(), buffer.size(), error);
4540

46-
protocol::ReadMemoryResponse response;
4741
response.address = "0x" + llvm::utohexstr(raw_address);
4842

4943
// reading memory may fail for multiple reasons. memory not readable,
@@ -54,9 +48,6 @@ ReadMemoryRequestHandler::Run(const protocol::ReadMemoryArguments &args) const {
5448
}
5549

5650
buffer.resize(std::min<size_t>(memory_count, args.count));
57-
if (!buffer.empty())
58-
response.data = llvm::encodeBase64(buffer);
59-
6051
return response;
6152
}
6253

lldb/tools/lldb-dap/Handler/RequestHandler.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -566,14 +566,14 @@ class DisassembleRequestHandler final
566566

567567
class ReadMemoryRequestHandler final
568568
: public RequestHandler<protocol::ReadMemoryArguments,
569-
llvm::Expected<protocol::ReadMemoryResponse>> {
569+
llvm::Expected<protocol::ReadMemoryResponseBody>> {
570570
public:
571571
using RequestHandler::RequestHandler;
572572
static llvm::StringLiteral GetCommand() { return "readMemory"; }
573573
FeatureSet GetSupportedFeatures() const override {
574574
return {protocol::eAdapterFeatureReadMemoryRequest};
575575
}
576-
llvm::Expected<protocol::ReadMemoryResponse>
576+
llvm::Expected<protocol::ReadMemoryResponseBody>
577577
Run(const protocol::ReadMemoryArguments &args) const override;
578578
};
579579

lldb/tools/lldb-dap/Protocol/ProtocolRequests.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "Protocol/ProtocolRequests.h"
10+
#include "JSONUtils.h"
1011
#include "llvm/ADT/DenseMap.h"
1112
#include "llvm/ADT/StringMap.h"
1213
#include "llvm/ADT/StringRef.h"
14+
#include "llvm/Support/Base64.h"
1315
#include "llvm/Support/JSON.h"
1416
#include <utility>
1517

@@ -480,4 +482,39 @@ json::Value toJSON(const DisassembleResponseBody &DRB) {
480482
return json::Object{{"instructions", DRB.instructions}};
481483
}
482484

485+
bool fromJSON(const json::Value &Params, ReadMemoryArguments &RMA,
486+
json::Path P) {
487+
json::ObjectMapper O(Params, P);
488+
489+
const json::Object *rma_obj = Params.getAsObject();
490+
constexpr llvm::StringRef ref_key = "memoryReference";
491+
const std::optional<llvm::StringRef> memory_ref = rma_obj->getString(ref_key);
492+
if (!memory_ref) {
493+
P.field(ref_key).report("missing value");
494+
return false;
495+
}
496+
497+
const std::optional<lldb::addr_t> addr_opt =
498+
DecodeMemoryReference(*memory_ref);
499+
if (!addr_opt) {
500+
P.field(ref_key).report("Malformed memory reference");
501+
return false;
502+
}
503+
504+
RMA.memoryReference = *addr_opt;
505+
506+
return O && O.map("count", RMA.count) && O.mapOptional("offset", RMA.offset);
507+
}
508+
509+
json::Value toJSON(const ReadMemoryResponseBody &RMR) {
510+
json::Object result{{"address", RMR.address}};
511+
512+
if (RMR.unreadableBytes != 0)
513+
result.insert({"unreadableBytes", RMR.unreadableBytes});
514+
if (!RMR.data.empty())
515+
result.insert({"data", llvm::encodeBase64(RMR.data)});
516+
517+
return result;
518+
}
519+
483520
} // namespace lldb_dap::protocol

lldb/tools/lldb-dap/Protocol/ProtocolRequests.h

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -839,6 +839,43 @@ bool fromJSON(const llvm::json::Value &, DisassembleResponseBody &,
839839
llvm::json::Path);
840840
llvm::json::Value toJSON(const DisassembleResponseBody &);
841841

842+
/// Arguments for `readMemory` request.
843+
struct ReadMemoryArguments {
844+
/// Memory reference to the base location from which data should be read.
845+
lldb::addr_t memoryReference;
846+
847+
/// Offset (in bytes) to be applied to the reference location before reading
848+
/// data. Can be negative.
849+
std::optional<int64_t> offset;
850+
851+
/// Number of bytes to read at the specified location and offset.
852+
uint64_t count;
853+
};
854+
bool fromJSON(const llvm::json::Value &, ReadMemoryArguments &,
855+
llvm::json::Path);
856+
857+
/// Response to `readMemory` request.
858+
struct ReadMemoryResponseBody {
859+
/// The address of the first byte of data returned.
860+
/// Treated as a hex value if prefixed with `0x`, or as a decimal value
861+
/// otherwise.
862+
std::string address;
863+
864+
/// The number of unreadable bytes encountered after the last successfully
865+
/// read byte.
866+
/// This can be used to determine the number of bytes that should be skipped
867+
/// before a subsequent `readMemory` request succeeds.
868+
uint64_t unreadableBytes = 0;
869+
870+
/// The bytes read from memory, encoded using base64. If the decoded length
871+
/// of `data` is less than the requested `count` in the original `readMemory`
872+
/// request, and `unreadableBytes` is zero or omitted, then the client should
873+
/// assume it's reached the end of readable memory.
874+
std::vector<std::byte> data;
875+
// std::optional<std::string> data;
876+
};
877+
llvm::json::Value toJSON(const ReadMemoryResponseBody &);
878+
842879
} // namespace lldb_dap::protocol
843880

844881
#endif

lldb/tools/lldb-dap/Protocol/ProtocolTypes.cpp

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -926,22 +926,4 @@ llvm::json::Value toJSON(const DisassembledInstruction &DI) {
926926
return result;
927927
}
928928

929-
bool fromJSON(const json::Value &Params, ReadMemoryArguments &RMA,
930-
json::Path P) {
931-
json::ObjectMapper O(Params, P);
932-
return O && O.map("memoryReference", RMA.memoryReference) &&
933-
O.map("count", RMA.count) && O.mapOptional("offset", RMA.offset);
934-
}
935-
936-
json::Value toJSON(const ReadMemoryResponse &RMR) {
937-
json::Object result{{"address", RMR.address}};
938-
939-
if (RMR.unreadableBytes)
940-
result.insert({"unreadableBytes", RMR.unreadableBytes});
941-
if (RMR.data)
942-
result.insert({"data", RMR.data});
943-
944-
return result;
945-
}
946-
947929
} // namespace lldb_dap::protocol

lldb/tools/lldb-dap/Protocol/ProtocolTypes.h

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -742,42 +742,6 @@ bool fromJSON(const llvm::json::Value &, DisassembledInstruction &,
742742
llvm::json::Path);
743743
llvm::json::Value toJSON(const DisassembledInstruction &);
744744

745-
/// Arguments for `readMemory` request.
746-
struct ReadMemoryArguments {
747-
/// Memory reference to the base location from which data should be read.
748-
std::string memoryReference;
749-
750-
/// Offset (in bytes) to be applied to the reference location before reading
751-
/// data. Can be negative.
752-
std::optional<int64_t> offset;
753-
754-
/// Number of bytes to read at the specified location and offset.
755-
uint64_t count;
756-
};
757-
bool fromJSON(const llvm::json::Value &, ReadMemoryArguments &,
758-
llvm::json::Path);
759-
760-
/// Response to `readMemory` request.
761-
struct ReadMemoryResponse {
762-
/// The address of the first byte of data returned.
763-
/// Treated as a hex value if prefixed with `0x`, or as a decimal value
764-
/// otherwise.
765-
std::string address;
766-
767-
/// The number of unreadable bytes encountered after the last successfully
768-
/// read byte.
769-
/// This can be used to determine the number of bytes that should be skipped
770-
/// before a subsequent `readMemory` request succeeds.
771-
std::optional<uint64_t> unreadableBytes;
772-
773-
/// The bytes read from memory, encoded using base64. If the decoded length
774-
/// of `data` is less than the requested `count` in the original `readMemory`
775-
/// request, and `unreadableBytes` is zero or omitted, then the client should
776-
/// assume it's reached the end of readable memory.
777-
std::optional<std::string> data;
778-
};
779-
llvm::json::Value toJSON(const ReadMemoryResponse &);
780-
781745
} // namespace lldb_dap::protocol
782746

783747
#endif

lldb/unittests/DAP/ProtocolTypesTest.cpp

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -769,10 +769,13 @@ TEST(ProtocolTypesTest, StepInTarget) {
769769
TEST(ProtocolTypesTest, ReadMemoryArguments) {
770770
ReadMemoryArguments args;
771771
args.count = 20;
772-
args.memoryReference = "0xabba";
772+
args.memoryReference = 43962;
773773
args.offset = std::nullopt;
774774

775-
llvm::Expected<ReadMemoryArguments> expected = parse<ReadMemoryArguments>(
775+
llvm::Expected<ReadMemoryArguments> expected =
776+
parse<ReadMemoryArguments>(R"({"memoryReference":"-4000", "count": 20})");
777+
ASSERT_THAT_EXPECTED(expected, llvm::Failed());
778+
expected = parse<ReadMemoryArguments>(
776779
R"({"memoryReference":"0xabba", "count": 20})");
777780
ASSERT_THAT_EXPECTED(expected, llvm::Succeeded());
778781

@@ -781,14 +784,17 @@ TEST(ProtocolTypesTest, ReadMemoryArguments) {
781784
EXPECT_EQ(args.offset, expected->offset);
782785
}
783786

784-
TEST(ProtocolTypesTest, ReadMemoryResponse) {
785-
ReadMemoryResponse response;
787+
TEST(ProtocolTypesTest, ReadMemoryResponseBody) {
788+
ReadMemoryResponseBody response;
786789
response.address = "0xdeadbeef";
787-
response.data = "aGVsbG8gd29ybGQhCg==";
788-
response.unreadableBytes = 0;
790+
const std::string data_str = "hello world!";
791+
std::transform(data_str.begin(), data_str.end(),
792+
std::back_inserter(response.data),
793+
[](char letter) { return std::byte(letter); });
794+
response.unreadableBytes = 1;
789795

790796
Expected<Value> expected = json::parse(
791-
R"({ "address": "0xdeadbeef", "data": "aGVsbG8gd29ybGQhCg==", "unreadableBytes": 0})");
797+
R"({ "address": "0xdeadbeef", "data": "aGVsbG8gd29ybGQh", "unreadableBytes": 1})");
792798
ASSERT_THAT_EXPECTED(expected, llvm::Succeeded());
793799
EXPECT_EQ(pp(*expected), pp(response));
794800
}

0 commit comments

Comments
 (0)