2022-03-24 19:17:41 +00:00
|
|
|
#include "ReadMemory.hpp"
|
|
|
|
|
|
2024-07-23 21:14:22 +01:00
|
|
|
#include <cassert>
|
|
|
|
|
|
2022-03-31 21:52:46 +01:00
|
|
|
#include "src/DebugServer/Gdb/ResponsePackets/ErrorResponsePacket.hpp"
|
|
|
|
|
#include "src/DebugServer/Gdb/ResponsePackets/ResponsePacket.hpp"
|
2022-03-24 19:17:41 +00:00
|
|
|
|
2023-01-21 14:27:45 +00:00
|
|
|
#include "src/Services/StringService.hpp"
|
2022-03-24 19:17:41 +00:00
|
|
|
#include "src/Logger/Logger.hpp"
|
2023-01-21 13:37:56 +00:00
|
|
|
|
2022-03-24 19:17:41 +00:00
|
|
|
#include "src/Exceptions/Exception.hpp"
|
|
|
|
|
|
2023-08-13 15:47:51 +01:00
|
|
|
namespace DebugServer::Gdb::AvrGdb::CommandPackets
|
2022-03-24 19:17:41 +00:00
|
|
|
{
|
2022-12-26 21:27:19 +00:00
|
|
|
using Services::TargetControllerService;
|
2022-04-09 15:57:24 +01:00
|
|
|
|
2022-03-24 19:17:41 +00:00
|
|
|
using ResponsePackets::ErrorResponsePacket;
|
|
|
|
|
using ResponsePackets::ResponsePacket;
|
|
|
|
|
|
|
|
|
|
using Exceptions::Exception;
|
|
|
|
|
|
2024-10-25 22:22:25 +01:00
|
|
|
ReadMemory::ReadMemory(const RawPacket& rawPacket, const AvrGdbTargetDescriptor& gdbTargetDescriptor)
|
2024-07-23 21:14:22 +01:00
|
|
|
: ReadMemory(rawPacket, gdbTargetDescriptor, ReadMemory::extractPacketData(rawPacket))
|
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
void ReadMemory::handle(
|
2024-10-25 22:22:25 +01:00
|
|
|
DebugSession& debugSession,
|
|
|
|
|
const AvrGdbTargetDescriptor& gdbTargetDescriptor,
|
2024-07-23 21:14:22 +01:00
|
|
|
const Targets::TargetDescriptor& targetDescriptor,
|
2024-12-14 16:17:02 +00:00
|
|
|
const Targets::TargetState& targetState,
|
2024-07-23 21:14:22 +01:00
|
|
|
TargetControllerService& targetControllerService
|
|
|
|
|
) {
|
2023-05-07 20:17:33 +01:00
|
|
|
Logger::info("Handling ReadMemory packet");
|
2022-03-24 19:17:41 +00:00
|
|
|
|
|
|
|
|
try {
|
2022-05-04 19:57:41 +01:00
|
|
|
if (this->bytes == 0) {
|
2024-07-23 21:14:22 +01:00
|
|
|
debugSession.connection.writePacket(ResponsePacket{Targets::TargetMemoryBuffer{}});
|
2022-05-04 19:57:41 +01:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-23 21:14:22 +01:00
|
|
|
const auto addressRange = Targets::TargetMemoryAddressRange{
|
|
|
|
|
this->startAddress,
|
|
|
|
|
this->startAddress + this->bytes - 1
|
|
|
|
|
};
|
2022-05-04 19:57:41 +01:00
|
|
|
|
2024-07-23 21:14:22 +01:00
|
|
|
const auto memorySegmentDescriptors = this->addressSpaceDescriptor.getIntersectingMemorySegmentDescriptors(
|
|
|
|
|
addressRange
|
|
|
|
|
);
|
2022-12-11 15:26:14 +00:00
|
|
|
|
2022-05-04 20:47:48 +01:00
|
|
|
/*
|
2024-07-23 21:14:22 +01:00
|
|
|
* First pass to ensure that we can read all of the memory before attempting to do so. And to ensure that
|
|
|
|
|
* the requested address range completely resides within known memory segments.
|
2022-05-04 20:47:48 +01:00
|
|
|
*/
|
2024-07-23 21:14:22 +01:00
|
|
|
auto accessibleBytes = Targets::TargetMemorySize{0};
|
|
|
|
|
for (const auto* memorySegmentDescriptor : memorySegmentDescriptors) {
|
2024-10-26 19:26:56 +01:00
|
|
|
/*
|
|
|
|
|
* GDB assumes that it can only access SRAM (including mapped IO, GPRs, etc), EEPROM and Flash.
|
|
|
|
|
* So when it probes the stack (see comment below RE stack probing), it expects the server to
|
|
|
|
|
* reject any attempts to access memory that resides after SRAM, on the SRAM (data) address space.
|
|
|
|
|
*
|
|
|
|
|
* The problem with this is that there are some AVR targets that have memory segments after the
|
|
|
|
|
* SRAM segment, on the same address space. For example, the AVR128DA48 has the "mapped_progmem"
|
|
|
|
|
* segment, which comes right after the SRAM segment.
|
|
|
|
|
*
|
|
|
|
|
* And what if the EEPROM segment (which sometimes resides on the data address space), comes after
|
|
|
|
|
* the SRAM segment? We must account for all of this.
|
|
|
|
|
*
|
|
|
|
|
* Fortunately, mapped IO and GPR segments always come before SRAM (this is confirmed via TDF
|
|
|
|
|
* validation), so we can still allow GDB to access to those segments.
|
|
|
|
|
*
|
|
|
|
|
* For the reasons mentioned above, when GDB attempts to access memory in the data address space, from
|
|
|
|
|
* a segment which comes after the SRAM segment, and is not EEPROM, we just assume that its intention
|
|
|
|
|
* was to access SRAM only, and return an error for any out-of-bounds access attempts.
|
|
|
|
|
*/
|
|
|
|
|
if (
|
|
|
|
|
this->addressSpaceDescriptor == gdbTargetDescriptor.sramAddressSpaceDescriptor
|
|
|
|
|
&& memorySegmentDescriptor->type != Targets::TargetMemorySegmentType::RAM
|
|
|
|
|
&& memorySegmentDescriptor->type != Targets::TargetMemorySegmentType::EEPROM
|
|
|
|
|
&& memorySegmentDescriptor->addressRange.startAddress > gdbTargetDescriptor.sramMemorySegmentDescriptor.addressRange.endAddress
|
|
|
|
|
) {
|
|
|
|
|
/*
|
|
|
|
|
* Ignore this memory segment, as it resides beyond the SRAM segment and is not EEPROM, so it's
|
|
|
|
|
* unlikely GDB actually wanted to access it.
|
|
|
|
|
*/
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-23 21:14:22 +01:00
|
|
|
if (!memorySegmentDescriptor->debugModeAccess.readable) {
|
|
|
|
|
throw Exception{
|
|
|
|
|
"Attempted to access restricted memory segment (" + memorySegmentDescriptor->key
|
|
|
|
|
+ ") - segment not readable in debug mode"
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
accessibleBytes += memorySegmentDescriptor->addressRange.intersectingSize(addressRange);
|
|
|
|
|
}
|
2022-05-14 22:44:26 +01:00
|
|
|
|
2024-10-26 19:26:56 +01:00
|
|
|
if (accessibleBytes < this->bytes) {
|
2022-05-04 19:57:41 +01:00
|
|
|
/*
|
2024-10-26 19:26:56 +01:00
|
|
|
* GDB has requested memory that, at least partially, does not reside in any known/accessible memory
|
|
|
|
|
* segment.
|
2024-07-23 21:14:22 +01:00
|
|
|
*
|
|
|
|
|
* This could be a result of GDB being configured to generate backtraces past the main function and
|
|
|
|
|
* the internal entry point of the application. This means that GDB will attempt to walk down the stack
|
|
|
|
|
* to identify every frame. The problem is that GDB doesn't really know where the stack begins, so it
|
2024-10-26 19:26:56 +01:00
|
|
|
* probes the stack by continuously issuing read memory commands until the server responds with an
|
2024-07-23 21:14:22 +01:00
|
|
|
* error.
|
2022-05-04 19:57:41 +01:00
|
|
|
*
|
2024-07-23 21:14:22 +01:00
|
|
|
* CLion seems to enable this by default. Somewhere between CLion 2021.1 and 2022.1, it began issuing
|
|
|
|
|
* the "-gdb-set backtrace past-entry on" command to GDB, at the beginning of each debug session.
|
2022-05-04 19:57:41 +01:00
|
|
|
*
|
|
|
|
|
* We don't throw an exception here, because this isn't really an error and so it's best not to report
|
|
|
|
|
* it as such. I don't think it's an error because it's expected behaviour, even though we respond to
|
|
|
|
|
* GDB with an error response.
|
|
|
|
|
*/
|
|
|
|
|
Logger::debug(
|
2024-07-23 21:14:22 +01:00
|
|
|
"GDB requested access to memory which does not reside within any memory segment - returning error "
|
|
|
|
|
"response"
|
2022-05-04 19:57:41 +01:00
|
|
|
);
|
2024-07-23 21:14:22 +01:00
|
|
|
debugSession.connection.writePacket(ErrorResponsePacket{});
|
2022-05-04 19:57:41 +01:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-23 21:14:22 +01:00
|
|
|
auto buffer = Targets::TargetMemoryBuffer(this->bytes, 0x00);
|
2022-05-14 22:44:26 +01:00
|
|
|
|
2024-07-23 21:14:22 +01:00
|
|
|
{
|
|
|
|
|
const auto atomicSession = targetControllerService.makeAtomicSession();
|
2022-05-14 22:44:26 +01:00
|
|
|
|
2024-07-23 21:14:22 +01:00
|
|
|
for (const auto* memorySegmentDescriptor : memorySegmentDescriptors) {
|
|
|
|
|
const auto segmentStartAddress = std::max(
|
|
|
|
|
this->startAddress,
|
|
|
|
|
memorySegmentDescriptor->addressRange.startAddress
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const auto segmentBuffer = targetControllerService.readMemory(
|
|
|
|
|
this->addressSpaceDescriptor,
|
|
|
|
|
*memorySegmentDescriptor,
|
|
|
|
|
segmentStartAddress,
|
|
|
|
|
memorySegmentDescriptor->addressRange.intersectingSize(addressRange)
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const auto bufferOffsetIt = buffer.begin() + (segmentStartAddress - this->startAddress);
|
|
|
|
|
assert(segmentBuffer.size() <= std::distance(bufferOffsetIt, buffer.end()));
|
2022-05-14 22:44:26 +01:00
|
|
|
|
2024-07-23 21:14:22 +01:00
|
|
|
std::copy(segmentBuffer.begin(), segmentBuffer.end(), bufferOffsetIt);
|
|
|
|
|
}
|
2022-05-14 22:44:26 +01:00
|
|
|
}
|
2022-03-24 19:17:41 +00:00
|
|
|
|
2024-07-23 21:14:22 +01:00
|
|
|
debugSession.connection.writePacket(ResponsePacket{Services::StringService::toHex(buffer)});
|
2022-03-24 19:17:41 +00:00
|
|
|
|
|
|
|
|
} catch (const Exception& exception) {
|
|
|
|
|
Logger::error("Failed to read memory from target - " + exception.getMessage());
|
2024-07-23 21:14:22 +01:00
|
|
|
debugSession.connection.writePacket(ErrorResponsePacket{});
|
2022-03-24 19:17:41 +00:00
|
|
|
}
|
|
|
|
|
}
|
2024-07-23 21:14:22 +01:00
|
|
|
|
|
|
|
|
ReadMemory::PacketData ReadMemory::extractPacketData(const RawPacket& rawPacket) {
|
|
|
|
|
using Services::StringService;
|
|
|
|
|
|
|
|
|
|
if (rawPacket.size() < 8) {
|
|
|
|
|
throw Exception{"Invalid packet length"};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const auto command = std::string{rawPacket.begin() + 2, rawPacket.end() - 3};
|
|
|
|
|
|
|
|
|
|
const auto delimiterPos = command.find_first_of(',');
|
|
|
|
|
if (delimiterPos == std::string::npos) {
|
|
|
|
|
throw Exception{"Invalid packet"};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return {
|
2024-10-26 17:19:00 +01:00
|
|
|
.gdbStartAddress = StringService::toUint32(command.substr(0, delimiterPos), 16),
|
|
|
|
|
.bytes = StringService::toUint32(command.substr(delimiterPos + 1), 16)
|
2024-07-23 21:14:22 +01:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ReadMemory::ReadMemory(
|
|
|
|
|
const RawPacket& rawPacket,
|
2024-10-25 22:22:25 +01:00
|
|
|
const AvrGdbTargetDescriptor& gdbTargetDescriptor,
|
2024-07-23 21:14:22 +01:00
|
|
|
ReadMemory::PacketData&& packetData
|
|
|
|
|
)
|
2024-10-26 16:19:05 +01:00
|
|
|
: Gdb::CommandPackets::CommandPacket(rawPacket)
|
2024-07-23 21:14:22 +01:00
|
|
|
, addressSpaceDescriptor(gdbTargetDescriptor.addressSpaceDescriptorFromGdbAddress(packetData.gdbStartAddress))
|
|
|
|
|
, startAddress(gdbTargetDescriptor.translateGdbAddress(packetData.gdbStartAddress))
|
|
|
|
|
, bytes(packetData.bytes)
|
|
|
|
|
{}
|
2022-03-24 19:17:41 +00:00
|
|
|
}
|