2021-04-04 21:04:12 +01:00
|
|
|
#include "WriteMemory.hpp"
|
2021-10-02 17:39:27 +01:00
|
|
|
|
2022-03-31 21:52:46 +01:00
|
|
|
#include "src/DebugServer/Gdb/ResponsePackets/ErrorResponsePacket.hpp"
|
|
|
|
|
#include "src/DebugServer/Gdb/ResponsePackets/OkResponsePacket.hpp"
|
2021-04-04 21:04:12 +01:00
|
|
|
|
2024-07-23 21:14:22 +01:00
|
|
|
#include "src/Services/StringService.hpp"
|
2022-03-24 19:17:41 +00:00
|
|
|
#include "src/Logger/Logger.hpp"
|
|
|
|
|
#include "src/Exceptions/Exception.hpp"
|
|
|
|
|
|
2023-08-13 15:47:51 +01:00
|
|
|
namespace DebugServer::Gdb::AvrGdb::CommandPackets
|
2022-02-05 15:32:08 +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::OkResponsePacket;
|
2021-04-04 21:04:12 +01:00
|
|
|
|
2023-08-13 15:47:51 +01:00
|
|
|
using namespace Exceptions;
|
2021-04-04 21:04:12 +01:00
|
|
|
|
2022-10-01 21:01:37 +01:00
|
|
|
WriteMemory::WriteMemory(const RawPacket& rawPacket, const TargetDescriptor& gdbTargetDescriptor)
|
2024-07-23 21:14:22 +01:00
|
|
|
: WriteMemory(rawPacket, gdbTargetDescriptor, WriteMemory::extractPacketData(rawPacket))
|
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
void WriteMemory::handle(
|
|
|
|
|
Gdb::DebugSession& debugSession,
|
|
|
|
|
const Gdb::TargetDescriptor& gdbTargetDescriptor,
|
|
|
|
|
const Targets::TargetDescriptor& targetDescriptor,
|
|
|
|
|
TargetControllerService& targetControllerService
|
|
|
|
|
) {
|
|
|
|
|
Logger::info("Handling WriteMemory packet");
|
2022-02-05 15:32:08 +00:00
|
|
|
|
2024-07-23 21:14:22 +01:00
|
|
|
try {
|
|
|
|
|
if (this->buffer.size() == 0) {
|
|
|
|
|
debugSession.connection.writePacket(OkResponsePacket{});
|
|
|
|
|
return;
|
|
|
|
|
}
|
2022-02-05 15:32:08 +00:00
|
|
|
|
2024-07-23 21:14:22 +01:00
|
|
|
const auto addressRange = Targets::TargetMemoryAddressRange{
|
|
|
|
|
this->startAddress,
|
|
|
|
|
this->startAddress + static_cast<Targets::TargetMemorySize>(this->buffer.size()) - 1
|
|
|
|
|
};
|
2022-04-03 20:35:53 +01:00
|
|
|
|
2024-07-23 21:14:22 +01:00
|
|
|
const auto memorySegmentDescriptors = this->addressSpaceDescriptor.getIntersectingMemorySegmentDescriptors(
|
|
|
|
|
addressRange
|
2022-02-05 15:32:08 +00:00
|
|
|
);
|
|
|
|
|
|
2024-07-23 21:14:22 +01:00
|
|
|
auto accessibleBytes = Targets::TargetMemorySize{0};
|
|
|
|
|
for (const auto* memorySegmentDescriptor : memorySegmentDescriptors) {
|
|
|
|
|
if (!memorySegmentDescriptor->debugModeAccess.writeable) {
|
|
|
|
|
throw Exception{
|
|
|
|
|
"Attempted to access restricted memory segment (" + memorySegmentDescriptor->key
|
|
|
|
|
+ ") - segment not writeable in debug mode"
|
|
|
|
|
};
|
|
|
|
|
}
|
2022-03-24 19:17:41 +00:00
|
|
|
|
2024-07-23 21:14:22 +01:00
|
|
|
accessibleBytes += memorySegmentDescriptor->addressRange.intersectingSize(addressRange);
|
2022-05-04 19:57:41 +01:00
|
|
|
}
|
|
|
|
|
|
2024-07-23 21:14:22 +01:00
|
|
|
if (accessibleBytes < this->bytes) {
|
|
|
|
|
throw Exception{"GDB requested access to memory which does not reside within any memory segment"};
|
2022-10-28 23:31:16 +01:00
|
|
|
}
|
|
|
|
|
|
2024-07-23 21:14:22 +01:00
|
|
|
{
|
|
|
|
|
const auto atomicSession = targetControllerService.makeAtomicSession();
|
|
|
|
|
|
|
|
|
|
for (const auto* memorySegmentDescriptor : memorySegmentDescriptors) {
|
|
|
|
|
const auto segmentStartAddress = std::max(
|
|
|
|
|
this->startAddress,
|
|
|
|
|
memorySegmentDescriptor->addressRange.startAddress
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const auto bufferOffsetIt = buffer.begin() + (segmentStartAddress - this->startAddress);
|
|
|
|
|
targetControllerService.writeMemory(
|
|
|
|
|
this->addressSpaceDescriptor,
|
|
|
|
|
*memorySegmentDescriptor,
|
|
|
|
|
segmentStartAddress,
|
|
|
|
|
Targets::TargetMemoryBuffer{
|
|
|
|
|
bufferOffsetIt,
|
|
|
|
|
bufferOffsetIt + memorySegmentDescriptor->addressRange.intersectingSize(addressRange)
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
}
|
2022-05-04 19:57:41 +01:00
|
|
|
}
|
|
|
|
|
|
2024-07-23 21:14:22 +01:00
|
|
|
debugSession.connection.writePacket(OkResponsePacket{});
|
2022-05-04 19:57:41 +01:00
|
|
|
|
2024-07-23 21:14:22 +01:00
|
|
|
} catch (const Exception& exception) {
|
|
|
|
|
Logger::error("Failed to write memory to target - " + exception.getMessage());
|
|
|
|
|
debugSession.connection.writePacket(ErrorResponsePacket{});
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-12-11 15:26:14 +00:00
|
|
|
|
2024-07-23 21:14:22 +01:00
|
|
|
WriteMemory::PacketData WriteMemory::extractPacketData(const RawPacket& rawPacket) {
|
|
|
|
|
using Services::StringService;
|
2022-05-04 19:57:41 +01:00
|
|
|
|
2024-07-23 21:14:22 +01:00
|
|
|
if (rawPacket.size() < 8) {
|
|
|
|
|
throw Exception{"Invalid packet length"};
|
|
|
|
|
}
|
2022-03-24 19:17:41 +00:00
|
|
|
|
2024-07-23 21:14:22 +01:00
|
|
|
const auto command = std::string{rawPacket.begin() + 2, rawPacket.end() - 3};
|
2022-03-24 19:17:41 +00:00
|
|
|
|
2024-07-23 21:14:22 +01:00
|
|
|
const auto commaDelimiterPos = command.find_first_of(',');
|
|
|
|
|
const auto colonDelimiterPos = command.find_first_of(':');
|
|
|
|
|
if (commaDelimiterPos == std::string::npos || colonDelimiterPos == std::string::npos) {
|
|
|
|
|
throw Exception{"Invalid packet"};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
StringService::toUint32(command.substr(0, commaDelimiterPos), 16),
|
|
|
|
|
StringService::toUint32(
|
|
|
|
|
command.substr(commaDelimiterPos + 1, colonDelimiterPos - (commaDelimiterPos + 1)),
|
|
|
|
|
16
|
|
|
|
|
),
|
|
|
|
|
StringService::dataFromHex(command.substr(colonDelimiterPos + 1))
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
WriteMemory::WriteMemory(
|
|
|
|
|
const RawPacket& rawPacket,
|
|
|
|
|
const Gdb::TargetDescriptor& gdbTargetDescriptor,
|
|
|
|
|
PacketData&& packetData
|
|
|
|
|
)
|
|
|
|
|
: CommandPacket(rawPacket)
|
|
|
|
|
, addressSpaceDescriptor(gdbTargetDescriptor.addressSpaceDescriptorFromGdbAddress(packetData.gdbStartAddress))
|
|
|
|
|
, startAddress(gdbTargetDescriptor.translateGdbAddress(packetData.gdbStartAddress))
|
|
|
|
|
, bytes(packetData.bytes)
|
|
|
|
|
, buffer(std::move(packetData.buffer))
|
|
|
|
|
{
|
|
|
|
|
if (this->buffer.size() != this->bytes) {
|
|
|
|
|
throw Exception{"Buffer size does not match length value given in write memory packet"};
|
2022-03-24 19:17:41 +00:00
|
|
|
}
|
|
|
|
|
}
|
2021-04-04 21:04:12 +01:00
|
|
|
}
|