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