General tidying, addressing issues found by static analysis tool.

This commit is contained in:
Nav
2021-06-22 23:52:31 +01:00
parent 69cee4d579
commit d365f6348b
151 changed files with 386 additions and 420 deletions

View File

@@ -1,5 +1,4 @@
#include <variant>
#include <cstdint>
#include "DebugServer.hpp"
#include "src/Exceptions/InvalidConfig.hpp"

View File

@@ -98,7 +98,7 @@ namespace Bloom::DebugServers
virtual void close() = 0;
public:
DebugServer(EventManager& eventManager): eventManager(eventManager) {};
explicit DebugServer(EventManager& eventManager): eventManager(eventManager) {};
void setApplicationConfig(const ApplicationConfig& applicationConfig) {
this->applicationConfig = applicationConfig;

View File

@@ -116,10 +116,10 @@ namespace Bloom::DebugServers::Gdb
};
public:
AvrGdbRsp(EventManager& eventManager): GdbRspDebugServer(eventManager) {};
explicit AvrGdbRsp(EventManager& eventManager): GdbRspDebugServer(eventManager) {};
std::string getName() const override {
return "AVR GDB Remote Serial Protocol Debug Server";
}
};
}
}

View File

@@ -37,7 +37,7 @@ namespace Bloom::DebugServers::Gdb::CommandPackets
class CommandPacket: public Packet
{
public:
CommandPacket(const std::vector<unsigned char>& rawPacket): Packet(rawPacket) {}
explicit CommandPacket(const std::vector<unsigned char>& rawPacket): Packet(rawPacket) {}
/**
* Double dispatches the packet to the appropriate overload of handleGdbPacket(), within the passed instance of

View File

@@ -8,7 +8,6 @@ using namespace Bloom::DebugServers::Gdb;
using namespace Bloom::DebugServers::Gdb::CommandPackets;
std::unique_ptr<CommandPacket> CommandPacketFactory::create(std::vector<unsigned char> rawPacket) {
if (rawPacket.size() == 5 && rawPacket[1] == 0x03) {
// This is an interrupt request - create a fake packet for it
return std::make_unique<CommandPackets::InterruptExecution>(rawPacket);
@@ -27,8 +26,8 @@ std::unique_ptr<CommandPacket> CommandPacketFactory::create(std::vector<unsigned
} else if (rawPacketString[1] == 'g' || rawPacketString[1] == 'p') {
return std::make_unique<CommandPackets::ReadGeneralRegisters>(rawPacket);
} else if (rawPacketString[1] == 'G' || rawPacketString[1] == 'P') {
return std::make_unique<CommandPackets::WriteGeneralRegisters>(rawPacket);
} else if (rawPacketString[1] == 'P') {
return std::make_unique<CommandPackets::WriteGeneralRegister>(rawPacket);
} else if (rawPacketString[1] == 'c') {
return std::make_unique<CommandPackets::ContinueExecution>(rawPacket);
@@ -127,4 +126,4 @@ std::vector<std::vector<unsigned char>> CommandPacketFactory::extractRawPackets(
}
return output;
}
}

View File

@@ -8,7 +8,7 @@
#include "InterruptExecution.hpp"
#include "SupportedFeaturesQuery.hpp"
#include "ReadGeneralRegisters.hpp"
#include "WriteGeneralRegisters.hpp"
#include "WriteGeneralRegister.hpp"
#include "ReadMemory.hpp"
#include "WriteMemory.hpp"
#include "StepExecution.hpp"

View File

@@ -27,10 +27,10 @@ namespace Bloom::DebugServers::Gdb::CommandPackets
*/
std::optional<std::uint32_t> fromProgramCounter;
ContinueExecution(std::vector<unsigned char> rawPacket): CommandPacket(rawPacket) {
explicit ContinueExecution(const std::vector<unsigned char>& rawPacket): CommandPacket(rawPacket) {
init();
}
virtual void dispatchToHandler(Gdb::GdbRspDebugServer& gdbRspDebugServer) override;
void dispatchToHandler(Gdb::GdbRspDebugServer& gdbRspDebugServer) override;
};
}

View File

@@ -15,8 +15,8 @@ namespace Bloom::DebugServers::Gdb::CommandPackets
class InterruptExecution: public CommandPacket
{
public:
InterruptExecution(std::vector<unsigned char> rawPacket): CommandPacket(rawPacket) {}
explicit InterruptExecution(const std::vector<unsigned char>& rawPacket): CommandPacket(rawPacket) {}
virtual void dispatchToHandler(Gdb::GdbRspDebugServer& gdbRspDebugServer) override;
void dispatchToHandler(Gdb::GdbRspDebugServer& gdbRspDebugServer) override;
};
}

View File

@@ -26,10 +26,10 @@ namespace Bloom::DebugServers::Gdb::CommandPackets
*/
std::optional<int> registerNumber;
ReadGeneralRegisters(std::vector<unsigned char> rawPacket): CommandPacket(rawPacket) {
explicit ReadGeneralRegisters(const std::vector<unsigned char>& rawPacket): CommandPacket(rawPacket) {
init();
};
virtual void dispatchToHandler(Gdb::GdbRspDebugServer& gdbRspDebugServer) override;
void dispatchToHandler(Gdb::GdbRspDebugServer& gdbRspDebugServer) override;
};
}

View File

@@ -24,17 +24,17 @@ namespace Bloom::DebugServers::Gdb::CommandPackets
*
* For an example of where GDB does this, see the AvrGdbRsp class.
*/
std::uint32_t startAddress;
std::uint32_t startAddress = 0;
/**
* Number of bytes to read.
*/
std::uint32_t bytes;
std::uint32_t bytes = 0;
ReadMemory(std::vector<unsigned char> rawPacket): CommandPacket(rawPacket) {
explicit ReadMemory(const std::vector<unsigned char>& rawPacket): CommandPacket(rawPacket) {
init();
};
virtual void dispatchToHandler(Gdb::GdbRspDebugServer& gdbRspDebugServer) override;
void dispatchToHandler(Gdb::GdbRspDebugServer& gdbRspDebugServer) override;
};
}

View File

@@ -32,12 +32,12 @@ namespace Bloom::DebugServers::Gdb::CommandPackets
/**
* Address at which the breakpoint should be located.
*/
std::uint32_t address;
std::uint32_t address = 0;
RemoveBreakpoint(std::vector<unsigned char> rawPacket): CommandPacket(rawPacket) {
explicit RemoveBreakpoint(const std::vector<unsigned char>& rawPacket): CommandPacket(rawPacket) {
this->init();
};
virtual void dispatchToHandler(Gdb::GdbRspDebugServer& gdbRspDebugServer) override;
void dispatchToHandler(Gdb::GdbRspDebugServer& gdbRspDebugServer) override;
};
}

View File

@@ -32,12 +32,12 @@ namespace Bloom::DebugServers::Gdb::CommandPackets
/**
* Address at which the breakpoint should be located.
*/
std::uint32_t address;
std::uint32_t address = 0;
SetBreakpoint(std::vector<unsigned char> rawPacket): CommandPacket(rawPacket) {
explicit SetBreakpoint(const std::vector<unsigned char>& rawPacket): CommandPacket(rawPacket) {
this->init();
};
virtual void dispatchToHandler(Gdb::GdbRspDebugServer& gdbRspDebugServer) override;
void dispatchToHandler(Gdb::GdbRspDebugServer& gdbRspDebugServer) override;
};
}

View File

@@ -21,10 +21,10 @@ namespace Bloom::DebugServers::Gdb::CommandPackets
*/
std::optional<size_t> fromProgramCounter;
StepExecution(std::vector<unsigned char> rawPacket): CommandPacket(rawPacket) {
explicit StepExecution(const std::vector<unsigned char>& rawPacket): CommandPacket(rawPacket) {
init();
};
virtual void dispatchToHandler(Gdb::GdbRspDebugServer& gdbRspDebugServer) override;
void dispatchToHandler(Gdb::GdbRspDebugServer& gdbRspDebugServer) override;
};
}

View File

@@ -27,18 +27,18 @@ namespace Bloom::DebugServers::Gdb::CommandPackets
void init();
public:
SupportedFeaturesQuery(std::vector<unsigned char> rawPacket): CommandPacket(rawPacket) {
explicit SupportedFeaturesQuery(const std::vector<unsigned char>& rawPacket): CommandPacket(rawPacket) {
this->init();
};
bool isFeatureSupported(const Feature& feature) const {
[[nodiscard]] bool isFeatureSupported(const Feature& feature) const {
return this->supportedFeatures.find(feature) != this->supportedFeatures.end();
}
const std::set<Feature>& getSupportedFeatures() const {
[[nodiscard]] const std::set<Feature>& getSupportedFeatures() const {
return this->supportedFeatures;
}
virtual void dispatchToHandler(Gdb::GdbRspDebugServer& gdbRspDebugServer) override;
void dispatchToHandler(Gdb::GdbRspDebugServer& gdbRspDebugServer) override;
};
}

View File

@@ -1,11 +1,11 @@
#include "WriteGeneralRegisters.hpp"
#include "WriteGeneralRegister.hpp"
#include "src/DebugServers/GdbRsp/GdbRspDebugServer.hpp"
#include "src/Exceptions/Exception.hpp"
using namespace Bloom::DebugServers::Gdb::CommandPackets;
using namespace Bloom::Exceptions;
void WriteGeneralRegisters::init() {
void WriteGeneralRegister::init() {
// The P packet updates a single register
auto packet = std::string(this->data.begin(), this->data.end());
@@ -13,16 +13,16 @@ void WriteGeneralRegisters::init() {
throw Exception("Invalid P command packet - insufficient data in packet.");
}
if (packet.find("=") == std::string::npos) {
if (packet.find('=') == std::string::npos) {
throw Exception("Invalid P command packet - unexpected format");
}
auto packetSegments = QString::fromStdString(packet).split("=");
this->registerNumber = packetSegments.front().mid(1).toUInt(nullptr, 16);
this->registerValue = this->hexToData(packetSegments.back().toStdString());
this->registerNumber = static_cast<int>(packetSegments.front().midRef(1).toUInt(nullptr, 16));
this->registerValue = Packet::hexToData(packetSegments.back().toStdString());
std::reverse(this->registerValue.begin(), this->registerValue.end());
}
void WriteGeneralRegisters::dispatchToHandler(Gdb::GdbRspDebugServer& gdbRspDebugServer) {
void WriteGeneralRegister::dispatchToHandler(Gdb::GdbRspDebugServer& gdbRspDebugServer) {
gdbRspDebugServer.handleGdbPacket(*this);
}

View File

@@ -0,0 +1,29 @@
#pragma once
#include <optional>
#include "CommandPacket.hpp"
#include "src/Targets/TargetRegister.hpp"
namespace Bloom::DebugServers::Gdb::CommandPackets
{
/**
* The WriteGeneralRegisters class implements the structure for "P" packets. Upon receiving this packet,
* server is expected to update a register value to the target.
*/
class WriteGeneralRegister: public CommandPacket
{
private:
void init();
public:
int registerNumber = 0;
std::vector<unsigned char> registerValue;
explicit WriteGeneralRegister(const std::vector<unsigned char>& rawPacket): CommandPacket(rawPacket) {
init();
};
void dispatchToHandler(Gdb::GdbRspDebugServer& gdbRspDebugServer) override;
};
}

View File

@@ -1,30 +0,0 @@
#pragma once
#include <optional>
#include "CommandPacket.hpp"
#include "src/Targets/TargetRegister.hpp"
namespace Bloom::DebugServers::Gdb::CommandPackets
{
/**
* The WriteGeneralRegisters class implements the structure for "G" and "P" packets. Upon receiving this packet,
* server is expected to write register values to the target.
*/
class WriteGeneralRegisters: public CommandPacket
{
private:
void init();
public:
Bloom::Targets::TargetRegisterMap registerMap;
int registerNumber;
std::vector<unsigned char> registerValue;
WriteGeneralRegisters(std::vector<unsigned char> rawPacket): CommandPacket(rawPacket) {
init();
};
virtual void dispatchToHandler(Gdb::GdbRspDebugServer& gdbRspDebugServer) override;
};
}

View File

@@ -40,7 +40,7 @@ void WriteMemory::init() {
throw Exception("Failed to parse write length from write memory packet data");
}
this->buffer = this->hexToData(lengthAndBufferSegments.at(1).toStdString());
this->buffer = Packet::hexToData(lengthAndBufferSegments.at(1).toStdString());
if (this->buffer.size() != bufferSize) {
throw Exception("Buffer size does not match length value given in write memory packet");

View File

@@ -22,14 +22,14 @@ namespace Bloom::DebugServers::Gdb::CommandPackets
* Like with the ReadMemory command packet, the start address carries additional bits that indicate
* the memory type.
*/
std::uint32_t startAddress;
std::uint32_t startAddress = 0;
Targets::TargetMemoryBuffer buffer;
WriteMemory(std::vector<unsigned char> rawPacket): CommandPacket(rawPacket) {
explicit WriteMemory(const std::vector<unsigned char>& rawPacket): CommandPacket(rawPacket) {
init();
};
virtual void dispatchToHandler(Gdb::GdbRspDebugServer& gdbRspDebugServer) override;
void dispatchToHandler(Gdb::GdbRspDebugServer& gdbRspDebugServer) override;
};
}

View File

@@ -1,6 +1,6 @@
#include <sys/socket.h>
#include <sys/epoll.h>
#include <errno.h>
#include <cerrno>
#include <fcntl.h>
#include "src/Logger/Logger.hpp"
@@ -117,7 +117,7 @@ void Connection::writePacket(const ResponsePacket& packet) {
std::vector<unsigned char> Connection::read(size_t bytes, bool interruptible, std::optional<int> msTimeout) {
auto output = std::vector<unsigned char>();
constexpr size_t bufferSize = 1024;
std::array<unsigned char, bufferSize> buffer;
std::array<unsigned char, bufferSize> buffer = {};
ssize_t bytesRead;
if (interruptible) {

View File

@@ -1,6 +1,7 @@
#pragma once
#include <cstdint>
#include <utility>
#include <vector>
#include <netinet/in.h>
#include <queue>
@@ -83,8 +84,8 @@ namespace Bloom::DebugServers::Gdb
*/
bool waitingForBreak = false;
Connection(std::shared_ptr<EventNotifier> interruptEventNotifier)
: interruptEventNotifier(interruptEventNotifier) {};
explicit Connection(std::shared_ptr<EventNotifier> interruptEventNotifier)
: interruptEventNotifier(std::move(interruptEventNotifier)) {};
/**
* Accepts a connection on serverSocketFileDescriptor.
@@ -104,7 +105,7 @@ namespace Bloom::DebugServers::Gdb
* @return
*/
std::string getIpAddress() {
std::array<char, INET_ADDRSTRLEN> ipAddress;
std::array<char, INET_ADDRSTRLEN> ipAddress = {};
if (::inet_ntop(AF_INET, &(socketAddress.sin_addr), ipAddress.data(), INET_ADDRSTRLEN) == nullptr) {
throw Exceptions::Exception("Failed to convert client IP address to text form.");
@@ -127,7 +128,7 @@ namespace Bloom::DebugServers::Gdb
*/
void writePacket(const ResponsePackets::ResponsePacket& packet);
int getMaxPacketSize() {
[[nodiscard]] int getMaxPacketSize() const {
return this->maxPacketSize;
}
};

View File

@@ -330,7 +330,7 @@ void GdbRspDebugServer::handleGdbPacket(CommandPackets::ReadGeneralRegisters& pa
}
}
void GdbRspDebugServer::handleGdbPacket(CommandPackets::WriteGeneralRegisters& packet) {
void GdbRspDebugServer::handleGdbPacket(CommandPackets::WriteGeneralRegister& packet) {
Logger::debug("Handling WriteGeneralRegisters packet");
try {

View File

@@ -200,11 +200,11 @@ namespace Bloom::DebugServers::Gdb
virtual void handleGdbPacket(CommandPackets::ReadGeneralRegisters& packet);
/**
* Handles the write general registers ("G" and "P") command packet.
* Handles the write general register ("P") command packet.
*
* @param packet
*/
virtual void handleGdbPacket(CommandPackets::WriteGeneralRegisters& packet);
virtual void handleGdbPacket(CommandPackets::WriteGeneralRegister& packet);
/**
* Handles the continue execution ("c") command packet.

View File

@@ -28,11 +28,11 @@ namespace Bloom::DebugServers::Gdb
}
public:
Packet() = default;
Packet(const std::vector<unsigned char>& rawPacket) {
explicit Packet(const std::vector<unsigned char>& rawPacket) {
this->init(rawPacket);
}
virtual std::vector<unsigned char> getData() const {
[[nodiscard]] virtual std::vector<unsigned char> getData() const {
return this->data;
}
@@ -45,7 +45,7 @@ namespace Bloom::DebugServers::Gdb
*
* @return
*/
std::vector<unsigned char> toRawPacket() const {
[[nodiscard]] std::vector<unsigned char> toRawPacket() const {
std::vector<unsigned char> packet = {'$'};
auto data = this->getData();
@@ -90,7 +90,7 @@ namespace Bloom::DebugServers::Gdb
* @param data
* @return
*/
static std::string dataToHex(std::vector<unsigned char> data) {
static std::string dataToHex(const std::vector<unsigned char>& data) {
std::stringstream stream;
stream << std::hex << std::setfill('0');

View File

@@ -18,7 +18,7 @@ namespace Bloom::DebugServers::Gdb::ResponsePackets
public:
Ok() = default;
std::vector<unsigned char> getData() const override {
[[nodiscard]] std::vector<unsigned char> getData() const override {
return {'O', 'K'};
}
};

View File

@@ -1,6 +1,7 @@
#pragma once
#include <set>
#include <utility>
#include "ResponsePacket.hpp"
#include "../Feature.hpp"
@@ -17,9 +18,9 @@ namespace Bloom::DebugServers::Gdb::ResponsePackets
public:
SupportedFeaturesResponse() = default;
SupportedFeaturesResponse(const std::set<std::pair<Feature, std::optional<std::string>>>& supportedFeatures)
: supportedFeatures(supportedFeatures) {};
explicit SupportedFeaturesResponse(std::set<std::pair<Feature, std::optional<std::string>>> supportedFeatures)
: supportedFeatures(std::move(supportedFeatures)) {};
std::vector<unsigned char> getData() const override;
[[nodiscard]] std::vector<unsigned char> getData() const override;
};
}

View File

@@ -18,10 +18,10 @@ namespace Bloom::DebugServers::Gdb::ResponsePackets
std::optional<Targets::TargetRegisterMap> registerMap;
std::optional<StopReason> stopReason;
TargetStopped(Signal signal): signal(signal) {}
explicit TargetStopped(Signal signal): signal(signal) {}
std::vector<unsigned char> getData() const override {
std::string output = "T" + this->dataToHex({static_cast<unsigned char>(this->signal)});
[[nodiscard]] std::vector<unsigned char> getData() const override {
std::string output = "T" + Packet::dataToHex({static_cast<unsigned char>(this->signal)});
if (this->stopReason.has_value()) {
auto stopReasonMapping = getStopReasonToNameMapping();
@@ -34,8 +34,8 @@ namespace Bloom::DebugServers::Gdb::ResponsePackets
if (this->registerMap.has_value()) {
for (const auto& [registerId, registerValue] : this->registerMap.value()) {
output += this->dataToHex({static_cast<unsigned char>(registerId)});
output += ":" + this->dataToHex(registerValue.value) + ";";
output += Packet::dataToHex({static_cast<unsigned char>(registerId)});
output += ":" + Packet::dataToHex(registerValue.value) + ";";
}
}