Misc: Pass string_view by value
This commit is contained in:
@ -3,8 +3,8 @@
|
||||
|
||||
#include "gdb_protocol.h"
|
||||
#include "bus.h"
|
||||
#include "cpu_core_private.h"
|
||||
#include "cpu_core.h"
|
||||
#include "cpu_core_private.h"
|
||||
#include "system.h"
|
||||
|
||||
#include "common/log.h"
|
||||
@ -20,15 +20,16 @@
|
||||
|
||||
Log_SetChannel(GDBProtocol);
|
||||
|
||||
namespace GDBProtocol
|
||||
{
|
||||
namespace GDBProtocol {
|
||||
|
||||
static u8* GetMemoryPointer(PhysicalMemoryAddress address, u32 length)
|
||||
{
|
||||
auto region = Bus::GetMemoryRegionForAddress(address);
|
||||
if (region) {
|
||||
if (region)
|
||||
{
|
||||
u8* data = GetMemoryRegionPointer(*region);
|
||||
if (data && (address + length <= GetMemoryRegionEnd(*region))) {
|
||||
if (data && (address + length <= GetMemoryRegionEnd(*region)))
|
||||
{
|
||||
return data + (address - GetMemoryRegionStart(*region));
|
||||
}
|
||||
}
|
||||
@ -36,34 +37,38 @@ static u8* GetMemoryPointer(PhysicalMemoryAddress address, u32 length)
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
static u8 ComputeChecksum(const std::string_view& str)
|
||||
static u8 ComputeChecksum(std::string_view str)
|
||||
{
|
||||
u8 checksum = 0;
|
||||
for (char c : str) {
|
||||
for (char c : str)
|
||||
{
|
||||
checksum = (checksum + c) % 256;
|
||||
}
|
||||
return checksum;
|
||||
}
|
||||
|
||||
static std::optional<std::string_view> DeserializePacket(const std::string_view& in)
|
||||
static std::optional<std::string_view> DeserializePacket(std::string_view in)
|
||||
{
|
||||
if ((in.size() < 4) || (in[0] != '$') || (in[in.size()-3] != '#')) {
|
||||
if ((in.size() < 4) || (in[0] != '$') || (in[in.size() - 3] != '#'))
|
||||
{
|
||||
return std::nullopt;
|
||||
}
|
||||
std::string_view data = in.substr(1, in.size()-4);
|
||||
std::string_view data = in.substr(1, in.size() - 4);
|
||||
|
||||
u8 packetChecksum = StringUtil::FromChars<u8>(in.substr(in.size()-2, 2), 16).value_or(0);
|
||||
u8 packetChecksum = StringUtil::FromChars<u8>(in.substr(in.size() - 2, 2), 16).value_or(0);
|
||||
u8 computedChecksum = ComputeChecksum(data);
|
||||
|
||||
if (packetChecksum == computedChecksum) {
|
||||
return { data };
|
||||
if (packetChecksum == computedChecksum)
|
||||
{
|
||||
return {data};
|
||||
}
|
||||
else {
|
||||
else
|
||||
{
|
||||
return std::nullopt;
|
||||
}
|
||||
}
|
||||
|
||||
static std::string SerializePacket(const std::string_view& in)
|
||||
static std::string SerializePacket(std::string_view in)
|
||||
{
|
||||
std::stringstream ss;
|
||||
ss << '$' << in << '#' << TinyString::from_format("{:02x}", ComputeChecksum(in));
|
||||
@ -71,7 +76,7 @@ static std::string SerializePacket(const std::string_view& in)
|
||||
}
|
||||
|
||||
/// List of GDB remote protocol registers for MIPS III (excluding FP).
|
||||
static const std::array<u32*, 38> REGISTERS {
|
||||
static const std::array<u32*, 38> REGISTERS{
|
||||
&CPU::g_state.regs.r[0],
|
||||
&CPU::g_state.regs.r[1],
|
||||
&CPU::g_state.regs.r[2],
|
||||
@ -117,53 +122,59 @@ static const std::array<u32*, 38> REGISTERS {
|
||||
constexpr int NUM_GDB_REGISTERS = 73;
|
||||
|
||||
/// Get stop reason.
|
||||
static std::optional<std::string> Cmd$_questionMark(const std::string_view& data)
|
||||
static std::optional<std::string> Cmd$_questionMark(std::string_view data)
|
||||
{
|
||||
return { "S02" };
|
||||
return {"S02"};
|
||||
}
|
||||
|
||||
/// Get general registers.
|
||||
static std::optional<std::string> Cmd$g(const std::string_view& data)
|
||||
static std::optional<std::string> Cmd$g(std::string_view data)
|
||||
{
|
||||
std::stringstream ss;
|
||||
|
||||
for (u32* reg : REGISTERS) {
|
||||
for (u32* reg : REGISTERS)
|
||||
{
|
||||
// Data is in host order (little endian).
|
||||
ss << StringUtil::EncodeHex(reinterpret_cast<u8*>(reg), 4);
|
||||
}
|
||||
|
||||
// Pad with dummy data (FP registers stuff).
|
||||
for (int i = 0; i < NUM_GDB_REGISTERS - static_cast<int>(REGISTERS.size()); i++) {
|
||||
for (int i = 0; i < NUM_GDB_REGISTERS - static_cast<int>(REGISTERS.size()); i++)
|
||||
{
|
||||
ss << "00000000";
|
||||
}
|
||||
|
||||
return { ss.str() };
|
||||
return {ss.str()};
|
||||
}
|
||||
|
||||
/// Set general registers.
|
||||
static std::optional<std::string> Cmd$G(const std::string_view& data)
|
||||
static std::optional<std::string> Cmd$G(std::string_view data)
|
||||
{
|
||||
if (data.size() == NUM_GDB_REGISTERS*8) {
|
||||
if (data.size() == NUM_GDB_REGISTERS * 8)
|
||||
{
|
||||
int offset = 0;
|
||||
|
||||
for (u32* reg : REGISTERS) {
|
||||
for (u32* reg : REGISTERS)
|
||||
{
|
||||
// Data is in host order (little endian).
|
||||
auto value = StringUtil::DecodeHex({data.data()+offset, 8});
|
||||
if (value) {
|
||||
auto value = StringUtil::DecodeHex({data.data() + offset, 8});
|
||||
if (value)
|
||||
{
|
||||
*reg = *reinterpret_cast<u32*>(&(*value)[0]);
|
||||
}
|
||||
offset += 8;
|
||||
}
|
||||
}
|
||||
else {
|
||||
Log_ErrorPrintf("Wrong payload size for 'G' command, expected %d got %zu", NUM_GDB_REGISTERS*8, data.size());
|
||||
else
|
||||
{
|
||||
Log_ErrorPrintf("Wrong payload size for 'G' command, expected %d got %zu", NUM_GDB_REGISTERS * 8, data.size());
|
||||
}
|
||||
|
||||
return { "" };
|
||||
return {""};
|
||||
}
|
||||
|
||||
/// Get memory.
|
||||
static std::optional<std::string> Cmd$m(const std::string_view& data)
|
||||
static std::optional<std::string> Cmd$m(std::string_view data)
|
||||
{
|
||||
std::stringstream ss{std::string{data}};
|
||||
std::string dataAddress, dataLength;
|
||||
@ -174,20 +185,22 @@ static std::optional<std::string> Cmd$m(const std::string_view& data)
|
||||
auto address = StringUtil::FromChars<VirtualMemoryAddress>(dataAddress, 16);
|
||||
auto length = StringUtil::FromChars<u32>(dataLength, 16);
|
||||
|
||||
if (address && length) {
|
||||
if (address && length)
|
||||
{
|
||||
PhysicalMemoryAddress phys_addr = *address & CPU::PHYSICAL_MEMORY_ADDRESS_MASK;
|
||||
u32 phys_length = *length;
|
||||
|
||||
u8* ptr_data = GetMemoryPointer(phys_addr, phys_length);
|
||||
if (ptr_data) {
|
||||
return { StringUtil::EncodeHex(ptr_data, phys_length) };
|
||||
if (ptr_data)
|
||||
{
|
||||
return {StringUtil::EncodeHex(ptr_data, phys_length)};
|
||||
}
|
||||
}
|
||||
return { "E00" };
|
||||
return {"E00"};
|
||||
}
|
||||
|
||||
/// Set memory.
|
||||
static std::optional<std::string> Cmd$M(const std::string_view& data)
|
||||
static std::optional<std::string> Cmd$M(std::string_view data)
|
||||
{
|
||||
std::stringstream ss{std::string{data}};
|
||||
std::string dataAddress, dataLength, dataPayload;
|
||||
@ -200,95 +213,101 @@ static std::optional<std::string> Cmd$M(const std::string_view& data)
|
||||
auto length = StringUtil::FromChars<u32>(dataLength, 16);
|
||||
auto payload = StringUtil::DecodeHex(dataPayload);
|
||||
|
||||
if (address && length && payload && (payload->size() == *length)) {
|
||||
if (address && length && payload && (payload->size() == *length))
|
||||
{
|
||||
u32 phys_addr = *address & CPU::PHYSICAL_MEMORY_ADDRESS_MASK;
|
||||
u32 phys_length = *length;
|
||||
|
||||
u8* ptr_data = GetMemoryPointer(phys_addr, phys_length);
|
||||
if (ptr_data) {
|
||||
if (ptr_data)
|
||||
{
|
||||
memcpy(ptr_data, payload->data(), phys_length);
|
||||
return { "OK" };
|
||||
return {"OK"};
|
||||
}
|
||||
}
|
||||
|
||||
return { "E00" };
|
||||
return {"E00"};
|
||||
}
|
||||
|
||||
/// Remove hardware breakpoint.
|
||||
static std::optional<std::string> Cmd$z1(const std::string_view& data)
|
||||
static std::optional<std::string> Cmd$z1(std::string_view data)
|
||||
{
|
||||
auto address = StringUtil::FromChars<VirtualMemoryAddress>(data, 16);
|
||||
if (address) {
|
||||
if (address)
|
||||
{
|
||||
CPU::RemoveBreakpoint(CPU::BreakpointType::Execute, *address);
|
||||
return { "OK" };
|
||||
return {"OK"};
|
||||
}
|
||||
else {
|
||||
else
|
||||
{
|
||||
return std::nullopt;
|
||||
}
|
||||
}
|
||||
|
||||
/// Insert hardware breakpoint.
|
||||
static std::optional<std::string> Cmd$Z1(const std::string_view& data)
|
||||
static std::optional<std::string> Cmd$Z1(std::string_view data)
|
||||
{
|
||||
auto address = StringUtil::FromChars<VirtualMemoryAddress>(data, 16);
|
||||
if (address) {
|
||||
if (address)
|
||||
{
|
||||
CPU::AddBreakpoint(CPU::BreakpointType::Execute, *address, false);
|
||||
return { "OK" };
|
||||
return {"OK"};
|
||||
}
|
||||
else {
|
||||
else
|
||||
{
|
||||
return std::nullopt;
|
||||
}
|
||||
}
|
||||
|
||||
static std::optional<std::string> Cmd$vMustReplyEmpty(const std::string_view& data)
|
||||
static std::optional<std::string> Cmd$vMustReplyEmpty(std::string_view data)
|
||||
{
|
||||
return { "" };
|
||||
return {""};
|
||||
}
|
||||
|
||||
static std::optional<std::string> Cmd$qSupported(const std::string_view& data)
|
||||
static std::optional<std::string> Cmd$qSupported(std::string_view data)
|
||||
{
|
||||
return { "" };
|
||||
return {""};
|
||||
}
|
||||
|
||||
/// List of all GDB remote protocol packets supported by us.
|
||||
static const std::map<const char*, std::function<std::optional<std::string>(const std::string_view&)>> COMMANDS
|
||||
{
|
||||
{ "?", Cmd$_questionMark },
|
||||
{ "g", Cmd$g },
|
||||
{ "G", Cmd$G },
|
||||
{ "m", Cmd$m },
|
||||
{ "M", Cmd$M },
|
||||
{ "z0,", Cmd$z1 },
|
||||
{ "Z0,", Cmd$Z1 },
|
||||
{ "z1,", Cmd$z1 },
|
||||
{ "Z1,", Cmd$Z1 },
|
||||
{ "vMustReplyEmpty", Cmd$vMustReplyEmpty },
|
||||
{ "qSupported", Cmd$qSupported },
|
||||
static const std::map<const char*, std::function<std::optional<std::string>(std::string_view)>> COMMANDS{
|
||||
{"?", Cmd$_questionMark},
|
||||
{"g", Cmd$g},
|
||||
{"G", Cmd$G},
|
||||
{"m", Cmd$m},
|
||||
{"M", Cmd$M},
|
||||
{"z0,", Cmd$z1},
|
||||
{"Z0,", Cmd$Z1},
|
||||
{"z1,", Cmd$z1},
|
||||
{"Z1,", Cmd$Z1},
|
||||
{"vMustReplyEmpty", Cmd$vMustReplyEmpty},
|
||||
{"qSupported", Cmd$qSupported},
|
||||
};
|
||||
|
||||
bool IsPacketInterrupt(const std::string_view& data)
|
||||
bool IsPacketInterrupt(std::string_view data)
|
||||
{
|
||||
return (data.size() >= 1) && (data[data.size()-1] == '\003');
|
||||
return (data.size() >= 1) && (data[data.size() - 1] == '\003');
|
||||
}
|
||||
|
||||
bool IsPacketContinue(const std::string_view& data)
|
||||
bool IsPacketContinue(std::string_view data)
|
||||
{
|
||||
return (data.size() >= 5) && (data.substr(data.size()-5) == "$c#63");
|
||||
return (data.size() >= 5) && (data.substr(data.size() - 5) == "$c#63");
|
||||
}
|
||||
|
||||
bool IsPacketComplete(const std::string_view& data)
|
||||
bool IsPacketComplete(std::string_view data)
|
||||
{
|
||||
return ((data.size() == 1) && (data[0] == '\003')) ||
|
||||
((data.size() > 3) && (*(data.end()-3) == '#'));
|
||||
return ((data.size() == 1) && (data[0] == '\003')) || ((data.size() > 3) && (*(data.end() - 3) == '#'));
|
||||
}
|
||||
|
||||
std::string ProcessPacket(const std::string_view& data)
|
||||
std::string ProcessPacket(std::string_view data)
|
||||
{
|
||||
std::string_view trimmedData = data;
|
||||
|
||||
// Eat ACKs.
|
||||
while (!trimmedData.empty() && (trimmedData[0] == '+' || trimmedData[0] == '-')) {
|
||||
if (trimmedData[0] == '-') {
|
||||
while (!trimmedData.empty() && (trimmedData[0] == '+' || trimmedData[0] == '-'))
|
||||
{
|
||||
if (trimmedData[0] == '-')
|
||||
{
|
||||
Log_ErrorPrint("Received negative ack");
|
||||
}
|
||||
trimmedData = trimmedData.substr(1);
|
||||
@ -296,17 +315,20 @@ std::string ProcessPacket(const std::string_view& data)
|
||||
|
||||
// Validate packet.
|
||||
auto packet = DeserializePacket(trimmedData);
|
||||
if (!packet) {
|
||||
if (!packet)
|
||||
{
|
||||
Log_ErrorPrintf("Malformed packet '%*s'", static_cast<int>(trimmedData.size()), trimmedData.data());
|
||||
return "-";
|
||||
}
|
||||
|
||||
std::optional<std::string> reply = { "" };
|
||||
std::optional<std::string> reply = {""};
|
||||
|
||||
// Try to invoke packet command.
|
||||
bool processed = false;
|
||||
for (const auto& command : COMMANDS) {
|
||||
if (packet->starts_with(command.first)) {
|
||||
for (const auto& command : COMMANDS)
|
||||
{
|
||||
if (packet->starts_with(command.first))
|
||||
{
|
||||
Log_DebugPrintf("Processing command '%s'", command.first);
|
||||
|
||||
// Invoke command, remove command name from payload.
|
||||
@ -316,10 +338,11 @@ std::string ProcessPacket(const std::string_view& data)
|
||||
}
|
||||
}
|
||||
|
||||
if (!processed) {
|
||||
if (!processed)
|
||||
{
|
||||
Log_WarningPrintf("Failed to process packet '%*s'", static_cast<int>(trimmedData.size()), trimmedData.data());
|
||||
}
|
||||
return reply ? "+"+SerializePacket(*reply) : "+";
|
||||
return reply ? "+" + SerializePacket(*reply) : "+";
|
||||
}
|
||||
|
||||
} // namespace GDBProtocol
|
||||
|
||||
Reference in New Issue
Block a user