Skip to content

Commit 93d9469

Browse files
authored
[lldb-dap] Updating protocol memory references to lldb::addr_t. (#148037)
This updates all the existing memory reference fields to use `lldb::addr_t` directly. A few places were using `std::string` and decoding the value in the request handler and other places had unique ways of parsing addresses. This unifies all of these references with the `DecodeMemoryReference` helper in JSONUtils.h. Additionally, for the types I updated, I tried to simplify the POD types some and moved default values out of RequestHandlers and into the protocol POD types.
1 parent ace1c83 commit 93d9469

File tree

11 files changed

+76
-121
lines changed

11 files changed

+76
-121
lines changed

lldb/packages/Python/lldbsuite/test/tools/lldb-dap/dap_server.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -831,13 +831,17 @@ def request_readMemory(self, memoryReference, offset, count):
831831
}
832832
return self.send_recv(command_dict)
833833

834-
def request_writeMemory(self, memoryReference, data, offset=0, allowPartial=True):
834+
def request_writeMemory(self, memoryReference, data, offset=0, allowPartial=False):
835835
args_dict = {
836836
"memoryReference": memoryReference,
837-
"offset": offset,
838-
"allowPartial": allowPartial,
839837
"data": data,
840838
}
839+
840+
if offset:
841+
args_dict["offset"] = offset
842+
if allowPartial:
843+
args_dict["allowPartial"] = allowPartial
844+
841845
command_dict = {
842846
"command": "writeMemory",
843847
"type": "request",

lldb/packages/Python/lldbsuite/test/tools/lldb-dap/lldbdap_testcase.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -508,7 +508,7 @@ def getBuiltinDebugServerTool(self):
508508
self.assertIsNotNone(server_tool, "debugserver not found.")
509509
return server_tool
510510

511-
def writeMemory(self, memoryReference, data=None, offset=None, allowPartial=None):
511+
def writeMemory(self, memoryReference, data=None, offset=0, allowPartial=False):
512512
# This function accepts data in decimal and hexadecimal format,
513513
# converts it to a Base64 string, and send it to the DAP,
514514
# which expects Base64 encoded data.

lldb/test/API/tools/lldb-dap/variables/TestDAP_variables.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,7 @@ def do_test_scopes_variables_setVariable_evaluate(
141141
self, enableAutoVariableSummaries: bool
142142
):
143143
"""
144-
Tests the "scopes", "variables", "setVariable", and "evaluate"
145-
packets.
144+
Tests the "scopes", "variables", "setVariable", and "evaluate" packets.
146145
"""
147146
program = self.getBuildArtifact("a.out")
148147
self.build_and_launch(
@@ -304,7 +303,6 @@ def do_test_scopes_variables_setVariable_evaluate(
304303
verify_response = {
305304
"type": "int",
306305
"value": str(variable_value),
307-
"variablesReference": 0,
308306
}
309307
for key, value in verify_response.items():
310308
self.assertEqual(value, response["body"][key])
@@ -723,7 +721,7 @@ def test_indexedVariables_with_raw_child_for_synthetics(self):
723721
self.do_test_indexedVariables(enableSyntheticChildDebugging=True)
724722

725723
@skipIfWindows
726-
@skipIfAsan # FIXME this fails with a non-asan issue on green dragon.
724+
@skipIfAsan # FIXME this fails with a non-asan issue on green dragon.
727725
def test_registers(self):
728726
"""
729727
Test that registers whose byte size is the size of a pointer on

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

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -182,22 +182,15 @@ static DisassembledInstruction ConvertSBInstructionToDisassembledInstruction(
182182
/// `supportsDisassembleRequest` is true.
183183
llvm::Expected<DisassembleResponseBody>
184184
DisassembleRequestHandler::Run(const DisassembleArguments &args) const {
185-
std::optional<lldb::addr_t> addr_opt =
186-
DecodeMemoryReference(args.memoryReference);
187-
if (!addr_opt.has_value())
188-
return llvm::make_error<DAPError>("Malformed memory reference: " +
189-
args.memoryReference);
190-
191-
lldb::addr_t addr_ptr = *addr_opt;
192-
addr_ptr += args.offset.value_or(0);
185+
const lldb::addr_t addr_ptr = args.memoryReference + args.offset;
193186
lldb::SBAddress addr(addr_ptr, dap.target);
194187
if (!addr.IsValid())
195188
return llvm::make_error<DAPError>(
196189
"Memory reference not found in the current binary.");
197190

198191
// Offset (in instructions) to be applied after the byte offset (if any)
199192
// before disassembling. Can be negative.
200-
int64_t instruction_offset = args.instructionOffset.value_or(0);
193+
const int64_t instruction_offset = args.instructionOffset;
201194

202195
// Calculate a sufficient address to start disassembling from.
203196
lldb::SBAddress disassemble_start_addr =
@@ -212,8 +205,8 @@ DisassembleRequestHandler::Run(const DisassembleArguments &args) const {
212205
return llvm::make_error<DAPError>(
213206
"Unexpected error while disassembling instructions.");
214207

215-
// Conver the found instructions to the DAP format.
216-
const bool resolve_symbols = args.resolveSymbols.value_or(false);
208+
// Convert the found instructions to the DAP format.
209+
const bool resolve_symbols = args.resolveSymbols;
217210
std::vector<DisassembledInstruction> instructions;
218211
size_t original_address_index = args.instructionCount;
219212
for (size_t i = 0; i < insts.GetSize(); ++i) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ ReadMemoryRequestHandler::Run(const protocol::ReadMemoryArguments &args) const {
3737
const size_t memory_count = dap.target.GetProcess().ReadMemory(
3838
raw_address, buffer.data(), buffer.size(), error);
3939

40-
response.address = "0x" + llvm::utohexstr(raw_address);
40+
response.address = raw_address;
4141

4242
// reading memory may fail for multiple reasons. memory not readable,
4343
// reading out of memory range and gaps in memory. return from

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,11 @@ SetVariableRequestHandler::Run(const SetVariableArguments &args) const {
6868
body.indexedVariables = variable.GetNumChildren();
6969
else
7070
body.namedVariables = variable.GetNumChildren();
71-
72-
} else {
73-
body.variablesReference = 0;
7471
}
7572

7673
if (const lldb::addr_t addr = variable.GetLoadAddress();
7774
addr != LLDB_INVALID_ADDRESS)
78-
body.memoryReference = EncodeMemoryReference(addr);
75+
body.memoryReference = addr;
7976

8077
if (ValuePointsToCode(variable))
8178
body.valueLocationReference = new_var_ref;

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@ namespace lldb_dap {
2222
llvm::Expected<protocol::WriteMemoryResponseBody>
2323
WriteMemoryRequestHandler::Run(
2424
const protocol::WriteMemoryArguments &args) const {
25-
const lldb::addr_t address = args.memoryReference + args.offset.value_or(0);
26-
;
25+
const lldb::addr_t address = args.memoryReference + args.offset;
2726

2827
lldb::SBProcess process = dap.target.GetProcess();
2928
if (!lldb::SBDebugger::StateIsStoppedState(process.GetState()))
@@ -54,7 +53,7 @@ WriteMemoryRequestHandler::Run(
5453
// If 'allowPartial' is false or missing, a debug adapter should attempt to
5554
// verify the region is writable before writing, and fail the response if it
5655
// is not.
57-
if (!args.allowPartial.value_or(false)) {
56+
if (!args.allowPartial) {
5857
// Start checking from the initial write address.
5958
lldb::addr_t start_address = address;
6059
// Compute the end of the write range.
@@ -74,7 +73,7 @@ WriteMemoryRequestHandler::Run(
7473
"Memory 0x" + llvm::utohexstr(args.memoryReference) +
7574
" region is not writable");
7675
}
77-
// If the current region covers the full requested range, stop futher
76+
// If the current region covers the full requested range, stop further
7877
// iterations.
7978
if (end_address <= region_info.GetRegionEnd()) {
8079
break;

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

Lines changed: 23 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#include "Protocol/ProtocolRequests.h"
1010
#include "JSONUtils.h"
11+
#include "lldb/lldb-defines.h"
1112
#include "llvm/ADT/DenseMap.h"
1213
#include "llvm/ADT/StringMap.h"
1314
#include "llvm/ADT/StringRef.h"
@@ -338,26 +339,24 @@ bool fromJSON(const json::Value &Params, SetVariableArguments &SVA,
338339

339340
json::Value toJSON(const SetVariableResponseBody &SVR) {
340341
json::Object Body{{"value", SVR.value}};
341-
if (SVR.type.has_value())
342-
Body.insert({"type", SVR.type});
343342

344-
if (SVR.variablesReference.has_value())
343+
if (!SVR.type.empty())
344+
Body.insert({"type", SVR.type});
345+
if (SVR.variablesReference)
345346
Body.insert({"variablesReference", SVR.variablesReference});
346-
347-
if (SVR.namedVariables.has_value())
347+
if (SVR.namedVariables)
348348
Body.insert({"namedVariables", SVR.namedVariables});
349-
350-
if (SVR.indexedVariables.has_value())
349+
if (SVR.indexedVariables)
351350
Body.insert({"indexedVariables", SVR.indexedVariables});
352-
353-
if (SVR.memoryReference.has_value())
354-
Body.insert({"memoryReference", SVR.memoryReference});
355-
356-
if (SVR.valueLocationReference.has_value())
351+
if (SVR.memoryReference != LLDB_INVALID_ADDRESS)
352+
Body.insert(
353+
{"memoryReference", EncodeMemoryReference(SVR.memoryReference)});
354+
if (SVR.valueLocationReference)
357355
Body.insert({"valueLocationReference", SVR.valueLocationReference});
358356

359357
return json::Value(std::move(Body));
360358
}
359+
361360
bool fromJSON(const json::Value &Params, ScopesArguments &SCA, json::Path P) {
362361
json::ObjectMapper O(Params, P);
363362
return O && O.map("frameId", SCA.frameId);
@@ -498,7 +497,9 @@ json::Value toJSON(const ThreadsResponseBody &TR) {
498497
bool fromJSON(const llvm::json::Value &Params, DisassembleArguments &DA,
499498
llvm::json::Path P) {
500499
json::ObjectMapper O(Params, P);
501-
return O && O.map("memoryReference", DA.memoryReference) &&
500+
return O &&
501+
DecodeMemoryReference(Params, "memoryReference", DA.memoryReference, P,
502+
/*required=*/true) &&
502503
O.mapOptional("offset", DA.offset) &&
503504
O.mapOptional("instructionOffset", DA.instructionOffset) &&
504505
O.map("instructionCount", DA.instructionCount) &&
@@ -512,29 +513,14 @@ json::Value toJSON(const DisassembleResponseBody &DRB) {
512513
bool fromJSON(const json::Value &Params, ReadMemoryArguments &RMA,
513514
json::Path P) {
514515
json::ObjectMapper O(Params, P);
515-
516-
const json::Object *rma_obj = Params.getAsObject();
517-
constexpr llvm::StringRef ref_key = "memoryReference";
518-
const std::optional<llvm::StringRef> memory_ref = rma_obj->getString(ref_key);
519-
if (!memory_ref) {
520-
P.field(ref_key).report("missing value");
521-
return false;
522-
}
523-
524-
const std::optional<lldb::addr_t> addr_opt =
525-
DecodeMemoryReference(*memory_ref);
526-
if (!addr_opt) {
527-
P.field(ref_key).report("Malformed memory reference");
528-
return false;
529-
}
530-
531-
RMA.memoryReference = *addr_opt;
532-
533-
return O && O.map("count", RMA.count) && O.mapOptional("offset", RMA.offset);
516+
return O &&
517+
DecodeMemoryReference(Params, "memoryReference", RMA.memoryReference,
518+
P, /*required=*/true) &&
519+
O.map("count", RMA.count) && O.mapOptional("offset", RMA.offset);
534520
}
535521

536522
json::Value toJSON(const ReadMemoryResponseBody &RMR) {
537-
json::Object result{{"address", RMR.address}};
523+
json::Object result{{"address", EncodeMemoryReference(RMR.address)}};
538524

539525
if (RMR.unreadableBytes != 0)
540526
result.insert({"unreadableBytes", RMR.unreadableBytes});
@@ -597,24 +583,10 @@ bool fromJSON(const json::Value &Params, WriteMemoryArguments &WMA,
597583
json::Path P) {
598584
json::ObjectMapper O(Params, P);
599585

600-
const json::Object *wma_obj = Params.getAsObject();
601-
constexpr llvm::StringRef ref_key = "memoryReference";
602-
const std::optional<llvm::StringRef> memory_ref = wma_obj->getString(ref_key);
603-
if (!memory_ref) {
604-
P.field(ref_key).report("missing value");
605-
return false;
606-
}
607-
608-
const std::optional<lldb::addr_t> addr_opt =
609-
DecodeMemoryReference(*memory_ref);
610-
if (!addr_opt) {
611-
P.field(ref_key).report("Malformed memory reference");
612-
return false;
613-
}
614-
615-
WMA.memoryReference = *addr_opt;
616-
617-
return O && O.mapOptional("allowPartial", WMA.allowPartial) &&
586+
return O &&
587+
DecodeMemoryReference(Params, "memoryReference", WMA.memoryReference,
588+
P, /*required=*/true) &&
589+
O.mapOptional("allowPartial", WMA.allowPartial) &&
618590
O.mapOptional("offset", WMA.offset) && O.map("data", WMA.data);
619591
}
620592

0 commit comments

Comments
 (0)