WCH RISC-V software breakpoints, and a few other bits of refactoring/tidying

This commit is contained in:
Nav
2024-12-05 23:09:01 +00:00
parent 966244a01a
commit 33ed399337
55 changed files with 1530 additions and 686 deletions

View File

@@ -0,0 +1,76 @@
#include "RemoveBreakpoint.hpp"
#include <string>
#include "src/DebugServer/Gdb/ResponsePackets/OkResponsePacket.hpp"
#include "src/DebugServer/Gdb/ResponsePackets/EmptyResponsePacket.hpp"
#include "src/DebugServer/Gdb/ResponsePackets/ErrorResponsePacket.hpp"
#include "src/Targets/TargetBreakpoint.hpp"
#include "src/Services/StringService.hpp"
#include "src/Logger/Logger.hpp"
#include "src/Exceptions/Exception.hpp"
namespace DebugServer::Gdb::AvrGdb::CommandPackets
{
using Services::TargetControllerService;
using ResponsePackets::OkResponsePacket;
using ResponsePackets::ErrorResponsePacket;
using ResponsePackets::EmptyResponsePacket;
using ::Exceptions::Exception;
RemoveBreakpoint::RemoveBreakpoint(const RawPacket& rawPacket)
: CommandPacket(rawPacket)
{
const auto packetString = std::string{this->data.begin(), this->data.end()};
if (packetString.size() < 6) {
throw Exception{"Unexpected RemoveBreakpoint packet size"};
}
this->type = (packetString[1] == '0')
? BreakpointType::SOFTWARE_BREAKPOINT
: (packetString[1] == '1') ? BreakpointType::HARDWARE_BREAKPOINT : BreakpointType::UNKNOWN;
const auto firstCommaPos = packetString.find_first_of(',');
const auto secondCommaPos = packetString.find_first_of(',', firstCommaPos + 1);
if (firstCommaPos == std::string::npos || secondCommaPos == std::string::npos) {
throw Exception{"Invalid RemoveBreakpoint packet"};
}
const auto addressString = packetString.substr(firstCommaPos + 1, secondCommaPos - firstCommaPos - 1);
const auto sizeString = packetString.substr(
secondCommaPos + 1,
packetString.find_first_of(';', secondCommaPos) - secondCommaPos - 1
);
this->address = Services::StringService::toUint32(addressString, 16);
this->size = Services::StringService::toUint32(sizeString, 10);
}
void RemoveBreakpoint::handle(
DebugSession& debugSession,
const AvrGdbTargetDescriptor& gdbTargetDescriptor,
const Targets::TargetDescriptor&,
TargetControllerService& targetControllerService
) {
Logger::info("Handling RemoveBreakpoint packet");
try {
debugSession.removeExternalBreakpoint(
gdbTargetDescriptor.programAddressSpaceDescriptor,
this->address,
targetControllerService
);
debugSession.connection.writePacket(OkResponsePacket{});
} catch (const Exception& exception) {
Logger::error("Failed to remove breakpoint on target - " + exception.getMessage());
debugSession.connection.writePacket(ErrorResponsePacket{});
}
}
}

View File

@@ -0,0 +1,42 @@
#pragma once
#include <cstdint>
#include "AvrGdbCommandPacketInterface.hpp"
#include "src/DebugServer/Gdb/CommandPackets/CommandPacket.hpp"
#include "src/DebugServer/Gdb/BreakpointType.hpp"
#include "src/Targets/TargetMemory.hpp"
namespace DebugServer::Gdb::AvrGdb::CommandPackets
{
/**
* The RemoveBreakpoint class implements the structure for "Z" command packets. Upon receiving this command, the
* server is expected to set a breakpoint at the specified address.
*/
class RemoveBreakpoint
: public CommandPackets::AvrGdbCommandPacketInterface
, private Gdb::CommandPackets::CommandPacket
{
public:
/**
* The requested breakpoint type.
*
* We don't actually honor this. GDB assumes flash memory cannot be written to outside of a programming
* session, so it refuses to even attempt to insert software breakpoints in flash memory. It always requests
* hardware breakpoints. This is why ignore the requested breakpoint type and just insert whatever type we can.
*/
BreakpointType type = BreakpointType::UNKNOWN;
Targets::TargetMemoryAddress address = 0;
Targets::TargetMemorySize size = 0;
explicit RemoveBreakpoint(const RawPacket& rawPacket);
void handle(
DebugSession& debugSession,
const AvrGdbTargetDescriptor& gdbTargetDescriptor,
const Targets::TargetDescriptor& targetDescriptor,
Services::TargetControllerService& targetControllerService
) override;
};
}

View File

@@ -0,0 +1,86 @@
#include "SetBreakpoint.hpp"
#include <string>
#include "src/DebugServer/Gdb/ResponsePackets/OkResponsePacket.hpp"
#include "src/DebugServer/Gdb/ResponsePackets/EmptyResponsePacket.hpp"
#include "src/DebugServer/Gdb/ResponsePackets/ErrorResponsePacket.hpp"
#include "src/Targets/TargetBreakpoint.hpp"
#include "src/Services/StringService.hpp"
#include "src/Logger/Logger.hpp"
#include "src/Exceptions/Exception.hpp"
namespace DebugServer::Gdb::AvrGdb::CommandPackets
{
using Services::TargetControllerService;
using ResponsePackets::OkResponsePacket;
using ResponsePackets::ErrorResponsePacket;
using ResponsePackets::EmptyResponsePacket;
using ::Exceptions::Exception;
SetBreakpoint::SetBreakpoint(const RawPacket& rawPacket)
: CommandPacket(rawPacket)
{
const auto packetString = std::string{this->data.begin(), this->data.end()};
if (packetString.size() < 6) {
throw Exception{"Unexpected SetBreakpoint packet size"};
}
// Z0 = SW breakpoint, Z1 = HW breakpoint
this->type = (packetString[1] == '0')
? BreakpointType::SOFTWARE_BREAKPOINT
: (packetString[1] == '1') ? BreakpointType::HARDWARE_BREAKPOINT : BreakpointType::UNKNOWN;
const auto firstCommaPos = packetString.find_first_of(',');
const auto secondCommaPos = packetString.find_first_of(',', firstCommaPos + 1);
if (firstCommaPos == std::string::npos || secondCommaPos == std::string::npos) {
throw Exception{"Invalid SetBreakpoint packet"};
}
const auto addressString = packetString.substr(firstCommaPos + 1, secondCommaPos - firstCommaPos - 1);
const auto sizeString = packetString.substr(
secondCommaPos + 1,
packetString.find_first_of(';', secondCommaPos) - secondCommaPos - 1
);
this->address = Services::StringService::toUint32(addressString, 16);
this->size = Services::StringService::toUint32(sizeString, 10);
}
void SetBreakpoint::handle(
DebugSession& debugSession,
const AvrGdbTargetDescriptor& gdbTargetDescriptor,
const Targets::TargetDescriptor&,
TargetControllerService& targetControllerService
) {
Logger::info("Handling SetBreakpoint packet");
try {
if (this->type == BreakpointType::UNKNOWN) {
Logger::debug(
"Rejecting breakpoint at address " + std::to_string(this->address)
+ " - unsupported breakpoint type"
);
debugSession.connection.writePacket(EmptyResponsePacket{});
return;
}
debugSession.setExternalBreakpoint(
gdbTargetDescriptor.programAddressSpaceDescriptor,
gdbTargetDescriptor.programMemorySegmentDescriptor,
this->address, this->size, targetControllerService
);
debugSession.connection.writePacket(OkResponsePacket{});
} catch (const Exception& exception) {
Logger::error("Failed to set breakpoint on target - " + exception.getMessage());
debugSession.connection.writePacket(ErrorResponsePacket{});
}
}
}

View File

@@ -0,0 +1,31 @@
#pragma once
#include <cstdint>
#include "AvrGdbCommandPacketInterface.hpp"
#include "src/DebugServer/Gdb/CommandPackets/CommandPacket.hpp"
#include "src/DebugServer/Gdb/BreakpointType.hpp"
#include "src/Targets/TargetMemory.hpp"
namespace DebugServer::Gdb::AvrGdb::CommandPackets
{
class SetBreakpoint
: public CommandPackets::AvrGdbCommandPacketInterface
, private Gdb::CommandPackets::CommandPacket
{
public:
BreakpointType type = BreakpointType::UNKNOWN;
Targets::TargetMemoryAddress address = 0;
Targets::TargetMemorySize size = 0;
explicit SetBreakpoint(const RawPacket& rawPacket);
void handle(
DebugSession& debugSession,
const AvrGdbTargetDescriptor& gdbTargetDescriptor,
const Targets::TargetDescriptor& targetDescriptor,
Services::TargetControllerService& targetControllerService
) override;
};
}

View File

@@ -84,7 +84,12 @@ namespace DebugServer::Gdb::AvrGdb::CommandPackets
return;
}
auto rangeSteppingSession = RangeSteppingSession{stepAddressRange, {}};
auto rangeSteppingSession = RangeSteppingSession{
gdbTargetDescriptor.programAddressSpaceDescriptor,
gdbTargetDescriptor.programMemorySegmentDescriptor,
stepAddressRange,
{}
};
const auto instructionsByAddress = Decoder::decode(
stepAddressRange.startAddress,