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