2022-03-24 19:17:41 +00:00
|
|
|
#include "ReadMemory.hpp"
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
#include "src/Logger/Logger.hpp"
|
|
|
|
|
#include "src/Exceptions/Exception.hpp"
|
|
|
|
|
|
2022-03-31 16:05:39 +01:00
|
|
|
namespace Bloom::DebugServer::Gdb::AvrGdb::CommandPackets
|
2022-03-24 19:17:41 +00:00
|
|
|
{
|
2022-04-09 15:57:24 +01:00
|
|
|
using TargetController::TargetControllerConsole;
|
|
|
|
|
|
2022-03-24 19:17:41 +00:00
|
|
|
using ResponsePackets::ErrorResponsePacket;
|
|
|
|
|
using ResponsePackets::ResponsePacket;
|
|
|
|
|
|
|
|
|
|
using Exceptions::Exception;
|
|
|
|
|
|
2022-04-08 23:39:51 +01:00
|
|
|
ReadMemory::ReadMemory(const RawPacketType& rawPacket)
|
2022-04-03 20:35:53 +01:00
|
|
|
: MemoryAccessCommandPacket(rawPacket)
|
2022-04-03 17:42:00 +01:00
|
|
|
{
|
2022-03-24 19:17:41 +00:00
|
|
|
if (this->data.size() < 4) {
|
|
|
|
|
throw Exception("Invalid packet length");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
auto packetString = QString::fromLocal8Bit(
|
|
|
|
|
reinterpret_cast<const char*>(this->data.data() + 1),
|
|
|
|
|
static_cast<int>(this->data.size() - 1)
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* The read memory ('m') packet consists of two segments, an address and a number of bytes to read.
|
|
|
|
|
* These are separated by a comma character.
|
|
|
|
|
*/
|
|
|
|
|
auto packetSegments = packetString.split(",");
|
|
|
|
|
|
|
|
|
|
if (packetSegments.size() != 2) {
|
|
|
|
|
throw Exception(
|
|
|
|
|
"Unexpected number of segments in packet data: " + std::to_string(packetSegments.size())
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool conversionStatus = false;
|
2022-04-03 20:35:53 +01:00
|
|
|
const auto gdbStartAddress = packetSegments.at(0).toUInt(&conversionStatus, 16);
|
2022-03-24 19:17:41 +00:00
|
|
|
|
|
|
|
|
if (!conversionStatus) {
|
|
|
|
|
throw Exception("Failed to parse start address from read memory packet data");
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-03 20:35:53 +01:00
|
|
|
this->memoryType = this->getMemoryTypeFromGdbAddress(gdbStartAddress);
|
|
|
|
|
this->startAddress = this->removeMemoryTypeIndicatorFromGdbAddress(gdbStartAddress);
|
|
|
|
|
|
2022-03-24 19:17:41 +00:00
|
|
|
this->bytes = packetSegments.at(1).toUInt(&conversionStatus, 16);
|
|
|
|
|
|
|
|
|
|
if (!conversionStatus) {
|
|
|
|
|
throw Exception("Failed to parse read length from read memory packet data");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ReadMemory::handle(DebugSession& debugSession, TargetControllerConsole& targetControllerConsole) {
|
|
|
|
|
Logger::debug("Handling ReadMemory packet");
|
|
|
|
|
|
|
|
|
|
try {
|
2022-05-04 19:57:41 +01:00
|
|
|
const auto& memoryDescriptorsByType = debugSession.gdbTargetDescriptor.targetDescriptor.memoryDescriptorsByType;
|
|
|
|
|
|
|
|
|
|
if (!memoryDescriptorsByType.contains(this->memoryType)) {
|
|
|
|
|
throw Exception("Target does not support the requested memory type.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (this->bytes == 0) {
|
|
|
|
|
debugSession.connection.writePacket(
|
|
|
|
|
ResponsePacket(std::vector<unsigned char>())
|
|
|
|
|
);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const auto& memoryDescriptor = memoryDescriptorsByType.at(this->memoryType);
|
|
|
|
|
|
|
|
|
|
if (
|
|
|
|
|
this->startAddress < memoryDescriptor.addressRange.startAddress
|
|
|
|
|
|| (this->startAddress + (this->bytes - 1)) > memoryDescriptor.addressRange.endAddress
|
|
|
|
|
) {
|
|
|
|
|
/*
|
|
|
|
|
* GDB can be configured to generate backtraces past the main function and the internal entry point
|
|
|
|
|
* of the application. Although this isn't very useful to most devs, CLion now seems to enable it 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.
|
|
|
|
|
*
|
|
|
|
|
* 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 ends up in a loop, continually issuing read
|
|
|
|
|
* memory commands. This has exposed an issue on our end - we need to validate the requested memory
|
|
|
|
|
* address range and reject any request for a range that's not within the target's memory. We do this
|
|
|
|
|
* here.
|
|
|
|
|
*
|
|
|
|
|
* 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(
|
|
|
|
|
"GDB requested access to memory which is outside the target's memory range - returning error response"
|
|
|
|
|
);
|
|
|
|
|
debugSession.connection.writePacket(ErrorResponsePacket());
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-03 20:35:53 +01:00
|
|
|
auto memoryBuffer = targetControllerConsole.readMemory(
|
|
|
|
|
this->memoryType,
|
|
|
|
|
this->startAddress,
|
|
|
|
|
this->bytes
|
|
|
|
|
);
|
2022-03-24 19:17:41 +00:00
|
|
|
|
|
|
|
|
debugSession.connection.writePacket(
|
2022-04-06 17:10:57 +01:00
|
|
|
ResponsePacket(Packet::toHex(memoryBuffer))
|
2022-03-24 19:17:41 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
} catch (const Exception& exception) {
|
|
|
|
|
Logger::error("Failed to read memory from target - " + exception.getMessage());
|
|
|
|
|
debugSession.connection.writePacket(ErrorResponsePacket());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|