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;
|
|
|
|
|
|
2022-10-01 21:01:37 +01:00
|
|
|
ReadMemory::ReadMemory(const RawPacket& rawPacket, const TargetDescriptor& gdbTargetDescriptor)
|
2024-07-23 21:14:22 +01:00
|
|
|
: ReadMemory(rawPacket, gdbTargetDescriptor, ReadMemory::extractPacketData(rawPacket))
|
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
void ReadMemory::handle(
|
|
|
|
|
Gdb::DebugSession& debugSession,
|
|
|
|
|
const Gdb::TargetDescriptor& gdbTargetDescriptor,
|
|
|
|
|
const Targets::TargetDescriptor& targetDescriptor,
|
|
|
|
|
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) {
|
|
|
|
|
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-07-23 21:14:22 +01:00
|
|
|
/*
|
|
|
|
|
* GDB will sometimes request an excess of up to two bytes outside the memory segment address range, even
|
|
|
|
|
* though we provide it with a memory map. I don't know why it does this, but I do know that we must
|
|
|
|
|
* tolerate it, otherwise GDB will moan.
|
|
|
|
|
*/
|
|
|
|
|
if (accessibleBytes < this->bytes && (this->bytes - accessibleBytes) > 2) {
|
2022-05-04 19:57:41 +01:00
|
|
|
/*
|
2024-07-23 21:14:22 +01:00
|
|
|
* GDB has requested memory that, at least partially, does not reside in any known memory segment.
|
|
|
|
|
*
|
|
|
|
|
* 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
|
|
|
|
|
* probes the target by continuously issuing read memory commands until the server responds with an
|
|
|
|
|
* 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 {
|
|
|
|
|
StringService::toUint32(command.substr(0, delimiterPos), 16),
|
|
|
|
|
StringService::toUint32(command.substr(delimiterPos + 1), 16)
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ReadMemory::ReadMemory(
|
|
|
|
|
const RawPacket& rawPacket,
|
|
|
|
|
const Gdb::TargetDescriptor& gdbTargetDescriptor,
|
|
|
|
|
ReadMemory::PacketData&& packetData
|
|
|
|
|
)
|
|
|
|
|
: CommandPacket(rawPacket)
|
|
|
|
|
, addressSpaceDescriptor(gdbTargetDescriptor.addressSpaceDescriptorFromGdbAddress(packetData.gdbStartAddress))
|
|
|
|
|
, startAddress(gdbTargetDescriptor.translateGdbAddress(packetData.gdbStartAddress))
|
|
|
|
|
, bytes(packetData.bytes)
|
|
|
|
|
{}
|
2022-03-24 19:17:41 +00:00
|
|
|
}
|