2021-04-04 21:04:12 +01:00
|
|
|
#include "CommandPacket.hpp"
|
2021-10-02 17:39:27 +01:00
|
|
|
|
2022-03-31 21:52:46 +01:00
|
|
|
#include "src/DebugServer/Gdb/ResponsePackets/ResponsePacket.hpp"
|
|
|
|
|
#include "src/DebugServer/Gdb/ResponsePackets/OkResponsePacket.hpp"
|
|
|
|
|
#include "src/DebugServer/Gdb/ResponsePackets/TargetStopped.hpp"
|
2022-04-08 22:17:03 +01:00
|
|
|
#include "src/DebugServer/Gdb/ResponsePackets/EmptyResponsePacket.hpp"
|
2022-03-31 21:52:46 +01:00
|
|
|
#include "src/DebugServer/Gdb/ResponsePackets/ErrorResponsePacket.hpp"
|
2022-03-24 19:17:41 +00:00
|
|
|
|
2022-03-31 21:52:46 +01:00
|
|
|
#include "src/DebugServer/Gdb/Signal.hpp"
|
2022-03-24 19:17:41 +00:00
|
|
|
|
2024-07-23 21:14:22 +01:00
|
|
|
#include "src/DebugServer/Gdb/Exceptions/ClientCommunicationError.hpp"
|
2022-03-24 19:17:41 +00:00
|
|
|
#include "src/Logger/Logger.hpp"
|
2021-04-04 21:04:12 +01:00
|
|
|
|
2023-08-13 15:47:51 +01:00
|
|
|
namespace DebugServer::Gdb::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::ResponsePacket;
|
|
|
|
|
using ResponsePackets::OkResponsePacket;
|
|
|
|
|
using ResponsePackets::TargetStopped;
|
2022-04-08 22:17:03 +01:00
|
|
|
using ResponsePackets::EmptyResponsePacket;
|
2022-03-24 19:17:41 +00:00
|
|
|
using ResponsePackets::ErrorResponsePacket;
|
|
|
|
|
|
2024-07-23 21:14:22 +01:00
|
|
|
CommandPacket::CommandPacket(const RawPacket& rawPacket) {
|
|
|
|
|
if (rawPacket.size() < 5) {
|
|
|
|
|
throw Exceptions::ClientCommunicationError{"Invalid raw packet size"};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this->data.insert(this->data.begin(), rawPacket.begin() + 1, rawPacket.end() - 3);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CommandPacket::handle(
|
|
|
|
|
DebugSession& debugSession,
|
|
|
|
|
const TargetDescriptor&,
|
|
|
|
|
const Targets::TargetDescriptor&,
|
|
|
|
|
TargetControllerService&
|
|
|
|
|
) {
|
|
|
|
|
const auto packetString = std::string{this->data.begin(), this->data.end()};
|
2022-04-03 02:22:49 +01:00
|
|
|
|
|
|
|
|
if (packetString.empty()) {
|
|
|
|
|
Logger::error("Empty GDB RSP packet received.");
|
2024-07-23 21:14:22 +01:00
|
|
|
debugSession.connection.writePacket(ErrorResponsePacket{});
|
2022-04-03 02:22:49 +01:00
|
|
|
return;
|
|
|
|
|
}
|
2022-03-24 19:17:41 +00:00
|
|
|
|
|
|
|
|
if (packetString[0] == '?') {
|
|
|
|
|
// Status report
|
2024-07-23 21:14:22 +01:00
|
|
|
debugSession.connection.writePacket(TargetStopped{Signal::TRAP});
|
2022-04-03 02:22:49 +01:00
|
|
|
return;
|
|
|
|
|
}
|
2022-03-24 19:17:41 +00:00
|
|
|
|
2022-04-08 22:17:22 +01:00
|
|
|
if (packetString.find("vMustReplyEmpty") == 0) {
|
2023-05-07 20:17:33 +01:00
|
|
|
Logger::info("Handling vMustReplyEmpty");
|
2024-07-23 21:14:22 +01:00
|
|
|
debugSession.connection.writePacket(EmptyResponsePacket{});
|
2022-04-08 22:17:22 +01:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-03 02:22:49 +01:00
|
|
|
if (packetString.find("qAttached") == 0) {
|
2023-05-07 20:17:33 +01:00
|
|
|
Logger::info("Handling qAttached");
|
2024-07-23 21:14:22 +01:00
|
|
|
debugSession.connection.writePacket(ResponsePacket{std::vector<unsigned char>({1})});
|
2022-04-03 02:22:49 +01:00
|
|
|
return;
|
|
|
|
|
}
|
2022-03-24 19:17:41 +00:00
|
|
|
|
2024-10-19 23:10:34 +01:00
|
|
|
if (!debugSession.serverConfig.packetAcknowledgement && packetString.find("QStartNoAckMode") == 0) {
|
|
|
|
|
Logger::info("Handling QStartNoAckMode");
|
|
|
|
|
/*
|
|
|
|
|
* We respond to the command before actually disabling packet acknowledgement, because GDB will send one
|
|
|
|
|
* final acknowledgement byte to acknowledge the response.
|
|
|
|
|
*/
|
|
|
|
|
debugSession.connection.writePacket(OkResponsePacket{});
|
|
|
|
|
debugSession.connection.packetAcknowledgement = false;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-03 02:22:49 +01:00
|
|
|
Logger::debug("Unknown GDB RSP packet: " + packetString + " - returning empty response");
|
2022-03-24 19:17:41 +00:00
|
|
|
|
2024-07-23 21:14:22 +01:00
|
|
|
// GDB expects an empty response for all unsupported commands
|
|
|
|
|
debugSession.connection.writePacket(EmptyResponsePacket{});
|
2022-02-05 15:32:08 +00:00
|
|
|
}
|
2021-10-02 17:39:27 +01:00
|
|
|
}
|