@@ -4405,14 +4405,6 @@ void request_readMemory(const llvm::json::Object &request) {
4405
4405
FillResponse (request, response);
4406
4406
auto *arguments = request.getObject (" arguments" );
4407
4407
4408
- lldb::SBProcess process = g_dap.target .GetProcess ();
4409
- if (!process.IsValid ()) {
4410
- response[" success" ] = false ;
4411
- response[" message" ] = " No process running" ;
4412
- g_dap.SendJSON (llvm::json::Value (std::move (response)));
4413
- return ;
4414
- }
4415
-
4416
4408
llvm::StringRef memoryReference = GetString (arguments, " memoryReference" );
4417
4409
auto addr_opt = DecodeMemoryReference (memoryReference);
4418
4410
if (!addr_opt.has_value ()) {
@@ -4422,57 +4414,34 @@ void request_readMemory(const llvm::json::Object &request) {
4422
4414
g_dap.SendJSON (llvm::json::Value (std::move (response)));
4423
4415
return ;
4424
4416
}
4425
- lldb::addr_t addr = *addr_opt;
4417
+ lldb::addr_t addr_int = *addr_opt;
4418
+ addr_int += GetSigned (arguments, " offset" , 0 );
4419
+ const uint64_t count_requested = GetUnsigned (arguments, " count" , 0 );
4426
4420
4427
- addr += GetSigned (arguments, " offset " , 0 );
4428
- const uint64_t requested_count = GetUnsigned (arguments, " count " , 0 );
4429
- lldb::SBMemoryRegionInfo region_info;
4430
- lldb::SBError memreg_error = process. GetMemoryRegionInfo (addr, region_info);
4431
- if (memreg_error. Fail ()) {
4432
- response[ " success " ] = false ;
4433
- EmplaceSafeString (response, " message " ,
4434
- " Unable to find memory region: " +
4435
- std::string (memreg_error. GetCString ()) );
4436
- g_dap. SendJSON ( llvm::json::Value ( std::move (response))) ;
4437
- return ;
4438
- }
4439
- if (!region_info. IsReadable ()) {
4421
+ lldb::SBAddress addr =
4422
+ g_dap. target . ResolveLoadAddress (addr_int );
4423
+
4424
+ // We also need support reading 0 bytes
4425
+ // VS Code sends those requests to check if a `memoryReference`
4426
+ // can be dereferenced.
4427
+ const uint64_t count_read = std::max< uint64_t >(count_requested, 1 );
4428
+ std::vector< uint8_t > buf;
4429
+ buf. resize (count_read );
4430
+ lldb::SBError error ;
4431
+ size_t count_result =
4432
+ g_dap. target . ReadMemory (addr, buf. data (), count_read, error);
4433
+ if (error. Fail ()) {
4440
4434
response[" success" ] = false ;
4441
- response. try_emplace ( " message" , " Memory region is not readable " );
4435
+ EmplaceSafeString (response, " message" , error. GetCString () );
4442
4436
g_dap.SendJSON (llvm::json::Value (std::move (response)));
4443
4437
return ;
4444
4438
}
4445
- const uint64_t available_count =
4446
- std::min (requested_count, region_info.GetRegionEnd () - addr);
4447
- const uint64_t unavailable_count = requested_count - available_count;
4448
-
4449
- std::vector<uint8_t > buf;
4450
- buf.resize (available_count);
4451
- if (available_count > 0 ) {
4452
- lldb::SBError memread_error;
4453
- uint64_t bytes_read =
4454
- process.ReadMemory (addr, buf.data (), available_count, memread_error);
4455
- if (memread_error.Fail ()) {
4456
- response[" success" ] = false ;
4457
- EmplaceSafeString (response, " message" ,
4458
- " Unable to read memory: " +
4459
- std::string (memread_error.GetCString ()));
4460
- g_dap.SendJSON (llvm::json::Value (std::move (response)));
4461
- return ;
4462
- }
4463
- if (bytes_read != available_count) {
4464
- response[" success" ] = false ;
4465
- EmplaceSafeString (response, " message" , " Unexpected, short read" );
4466
- g_dap.SendJSON (llvm::json::Value (std::move (response)));
4467
- return ;
4468
- }
4469
- }
4439
+ buf.resize (std::min (count_result, count_requested));
4470
4440
4471
4441
llvm::json::Object body;
4472
- std::string formatted_addr = " 0x" + llvm::utohexstr (addr );
4442
+ std::string formatted_addr = " 0x" + llvm::utohexstr (addr_int );
4473
4443
body.try_emplace (" address" , formatted_addr);
4474
4444
body.try_emplace (" data" , llvm::encodeBase64 (buf));
4475
- body.try_emplace (" unreadableBytes" , unavailable_count);
4476
4445
response.try_emplace (" body" , std::move (body));
4477
4446
g_dap.SendJSON (llvm::json::Value (std::move (response)));
4478
4447
}
0 commit comments