diff --git a/src/DebugServer/Gdb/Connection.cpp b/src/DebugServer/Gdb/Connection.cpp index 8dff1041..0c316413 100644 --- a/src/DebugServer/Gdb/Connection.cpp +++ b/src/DebugServer/Gdb/Connection.cpp @@ -207,7 +207,7 @@ namespace DebugServer::Gdb bool interruptible, std::optional timeout ) { - auto output = std::vector(); + auto output = std::vector{}; if (this->readInterruptEnabled != interruptible) { if (interruptible) { diff --git a/src/DebugServer/Gdb/Connection.hpp b/src/DebugServer/Gdb/Connection.hpp index 7d1959ee..49bd0f13 100644 --- a/src/DebugServer/Gdb/Connection.hpp +++ b/src/DebugServer/Gdb/Connection.hpp @@ -13,8 +13,8 @@ #include "src/Helpers/EventFdNotifier.hpp" #include "src/Helpers/EpollInstance.hpp" -#include "src/DebugServer/Gdb/Packet.hpp" -#include "src/DebugServer/Gdb/ResponsePackets/ResponsePacket.hpp" +#include "Packet.hpp" +#include "ResponsePackets/ResponsePacket.hpp" namespace DebugServer::Gdb { diff --git a/src/DebugServer/Gdb/GdbRspDebugServer.cpp b/src/DebugServer/Gdb/GdbRspDebugServer.cpp index ec67b660..ef47d8b5 100644 --- a/src/DebugServer/Gdb/GdbRspDebugServer.cpp +++ b/src/DebugServer/Gdb/GdbRspDebugServer.cpp @@ -159,15 +159,17 @@ namespace DebugServer::Gdb } const auto commandPacket = this->waitForCommandPacket(); - if (commandPacket) { - commandPacket->handle( - *(this->getActiveDebugSession()), - this->getGdbTargetDescriptor(), - this->targetDescriptor, - this->targetControllerService - ); + if (!commandPacket) { + return; } + commandPacket->handle( + *(this->getActiveDebugSession()), + this->getGdbTargetDescriptor(), + this->targetDescriptor, + this->targetControllerService + ); + } catch (const ClientDisconnected&) { Logger::info("GDB RSP client disconnected"); this->endDebugSession(); diff --git a/src/DebugToolDrivers/Protocols/RiscVDebugSpec/DebugTranslator.cpp b/src/DebugToolDrivers/Protocols/RiscVDebugSpec/DebugTranslator.cpp index ff21140f..0a5581b5 100644 --- a/src/DebugToolDrivers/Protocols/RiscVDebugSpec/DebugTranslator.cpp +++ b/src/DebugToolDrivers/Protocols/RiscVDebugSpec/DebugTranslator.cpp @@ -361,8 +361,6 @@ namespace DebugToolDrivers::Protocols::RiscVDebugSpec TargetMemorySize bytes, const std::set& excludedAddressRanges ) { - using DebugModule::Registers::MemoryAccessControlField; - // TODO: excluded addresses const auto pageSize = 4; @@ -383,34 +381,7 @@ namespace DebugToolDrivers::Protocols::RiscVDebugSpec return TargetMemoryBuffer{offset, offset + bytes}; } - auto output = TargetMemoryBuffer{}; - output.reserve(bytes); - - /* - * We only need to set the address once. No need to update it as we use the post-increment function to - * increment the address. See MemoryAccessControlField::postIncrement - */ - this->dtmInterface.writeDebugModuleRegister(RegisterAddress::ABSTRACT_DATA_1, startAddress); - - constexpr auto command = AbstractCommandRegister{ - .control = MemoryAccessControlField{ - .postIncrement = true, - .size = MemoryAccessControlField::MemorySize::SIZE_32, - }.value(), - .commandType = AbstractCommandRegister::CommandType::MEMORY_ACCESS - }; - - for (auto address = startAddress; address <= (startAddress + bytes - 1); address += 4) { - this->executeAbstractCommand(command); - - const auto data = this->dtmInterface.readDebugModuleRegister(RegisterAddress::ABSTRACT_DATA_0); - output.emplace_back(static_cast(data)); - output.emplace_back(static_cast(data >> 8)); - output.emplace_back(static_cast(data >> 16)); - output.emplace_back(static_cast(data >> 24)); - } - - return output; + return this->readMemoryViaAbstractCommand(startAddress, bytes); } void DebugTranslator::writeMemory( @@ -462,30 +433,7 @@ namespace DebugToolDrivers::Protocols::RiscVDebugSpec ); } - this->dtmInterface.writeDebugModuleRegister(RegisterAddress::ABSTRACT_DATA_1, startAddress); - - constexpr auto command = AbstractCommandRegister{ - .control = MemoryAccessControlField{ - .write = true, - .postIncrement = true, - .size = MemoryAccessControlField::MemorySize::SIZE_32, - }.value(), - .commandType = AbstractCommandRegister::CommandType::MEMORY_ACCESS - }; - - for (TargetMemoryAddress offset = 0; offset < buffer.size(); offset += 4) { - this->dtmInterface.writeDebugModuleRegister( - RegisterAddress::ABSTRACT_DATA_0, - static_cast( - (buffer[offset + 3] << 24) - | (buffer[offset + 2] << 16) - | (buffer[offset + 1] << 8) - | (buffer[offset]) - ) - ); - - this->executeAbstractCommand(command); - } + return this->writeMemoryViaAbstractCommand(startAddress, buffer); } std::vector DebugTranslator::discoverHartIndices() { @@ -800,6 +748,74 @@ namespace DebugToolDrivers::Protocols::RiscVDebugSpec ) * alignTo; } + Targets::TargetMemoryBuffer DebugTranslator::readMemoryViaAbstractCommand( + Targets::TargetMemoryAddress startAddress, + Targets::TargetMemorySize bytes + ) { + using DebugModule::Registers::MemoryAccessControlField; + + auto output = TargetMemoryBuffer{}; + output.reserve(bytes); + + /* + * We only need to set the address once. No need to update it as we use the post-increment function to + * increment the address. See MemoryAccessControlField::postIncrement + */ + this->dtmInterface.writeDebugModuleRegister(RegisterAddress::ABSTRACT_DATA_1, startAddress); + + constexpr auto command = AbstractCommandRegister{ + .control = MemoryAccessControlField{ + .postIncrement = true, + .size = MemoryAccessControlField::MemorySize::SIZE_32, + }.value(), + .commandType = AbstractCommandRegister::CommandType::MEMORY_ACCESS + }; + + for (auto address = startAddress; address <= (startAddress + bytes - 1); address += 4) { + this->executeAbstractCommand(command); + + const auto data = this->dtmInterface.readDebugModuleRegister(RegisterAddress::ABSTRACT_DATA_0); + output.emplace_back(static_cast(data)); + output.emplace_back(static_cast(data >> 8)); + output.emplace_back(static_cast(data >> 16)); + output.emplace_back(static_cast(data >> 24)); + } + + return output; + } + + void DebugTranslator::writeMemoryViaAbstractCommand( + Targets::TargetMemoryAddress startAddress, + const TargetMemoryBuffer& buffer + ) { + using DebugModule::Registers::MemoryAccessControlField; + + this->dtmInterface.writeDebugModuleRegister(RegisterAddress::ABSTRACT_DATA_1, startAddress); + + constexpr auto command = AbstractCommandRegister{ + .control = MemoryAccessControlField{ + .write = true, + .postIncrement = true, + .size = MemoryAccessControlField::MemorySize::SIZE_32, + }.value(), + .commandType = AbstractCommandRegister::CommandType::MEMORY_ACCESS + }; + + for (TargetMemoryAddress offset = 0; offset < buffer.size(); offset += 4) { + this->dtmInterface.writeDebugModuleRegister( + RegisterAddress::ABSTRACT_DATA_0, + static_cast( + (buffer[offset + 3] << 24) + | (buffer[offset + 2] << 16) + | (buffer[offset + 1] << 8) + | (buffer[offset]) + ) + ); + + this->executeAbstractCommand(command); + } + } + std::optional< std::reference_wrapper > DebugTranslator::getAvailableTrigger() { diff --git a/src/DebugToolDrivers/Protocols/RiscVDebugSpec/DebugTranslator.hpp b/src/DebugToolDrivers/Protocols/RiscVDebugSpec/DebugTranslator.hpp index 75f5aacb..22eeacdf 100644 --- a/src/DebugToolDrivers/Protocols/RiscVDebugSpec/DebugTranslator.hpp +++ b/src/DebugToolDrivers/Protocols/RiscVDebugSpec/DebugTranslator.hpp @@ -140,6 +140,15 @@ namespace DebugToolDrivers::Protocols::RiscVDebugSpec ); Targets::TargetMemorySize alignMemorySize(Targets::TargetMemorySize size, Targets::TargetMemorySize alignTo); + Targets::TargetMemoryBuffer readMemoryViaAbstractCommand( + Targets::TargetMemoryAddress startAddress, + Targets::TargetMemorySize bytes + ); + void writeMemoryViaAbstractCommand( + Targets::TargetMemoryAddress startAddress, + const Targets::TargetMemoryBuffer& buffer + ); + std::optional> getAvailableTrigger(); void clearTrigger(const TriggerModule::TriggerDescriptor& triggerDescriptor); }; diff --git a/src/DebugToolDrivers/WCH/WchLinkBase.cpp b/src/DebugToolDrivers/WCH/WchLinkBase.cpp index 9e25ff37..534f5eb0 100644 --- a/src/DebugToolDrivers/WCH/WchLinkBase.cpp +++ b/src/DebugToolDrivers/WCH/WchLinkBase.cpp @@ -36,7 +36,7 @@ namespace DebugToolDrivers::Wch this->wchLinkUsbInterface->init(); this->wchLinkInterface = std::make_unique( - *(this->wchLinkUsbInterface.get()), + *(this->wchLinkUsbInterface), *this ); @@ -78,7 +78,7 @@ namespace DebugToolDrivers::Wch if (!this->wchRiscVTranslator) { this->wchRiscVTranslator = std::make_unique( - *(this->wchLinkInterface.get()), + *(this->wchLinkInterface), this->toolConfig.riscVDebugTranslatorConfig, targetDescriptionFile, targetConfig