2022-12-03 21:09:13 +00:00
|
|
|
// Copyright 2022 Citra Emulator Project
|
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
2023-03-31 19:05:57 +01:00
|
|
|
#include <fmt/ranges.h>
|
|
|
|
#include "common/string_util.h"
|
2022-12-03 01:31:31 +00:00
|
|
|
#include "core/core.h"
|
|
|
|
#include "core/gdbstub/gdbstub.h"
|
|
|
|
#include "core/gdbstub/hio.h"
|
|
|
|
|
|
|
|
namespace GDBStub {
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
static VAddr current_hio_request_addr;
|
|
|
|
static PackedGdbHioRequest current_hio_request;
|
2022-12-04 20:34:06 +00:00
|
|
|
|
|
|
|
enum class Status {
|
|
|
|
NoRequest,
|
|
|
|
NotSent,
|
|
|
|
SentWaitingReply,
|
|
|
|
};
|
|
|
|
|
|
|
|
static std::atomic<Status> request_status{Status::NoRequest};
|
2022-12-03 01:31:31 +00:00
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
void SetHioRequest(const VAddr addr) {
|
|
|
|
if (!IsServerEnabled()) {
|
|
|
|
LOG_WARNING(Debug_GDBStub, "HIO requested but GDB stub is not running");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-03-31 19:05:57 +01:00
|
|
|
if (HasPendingHioRequest() || WaitingForHioReply()) {
|
2022-12-03 01:31:31 +00:00
|
|
|
LOG_WARNING(Debug_GDBStub, "HIO requested while already in progress!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-03-31 19:05:57 +01:00
|
|
|
auto& memory = Core::System::GetInstance().Memory();
|
2022-12-03 21:09:13 +00:00
|
|
|
const auto process = Core::System::GetInstance().Kernel().GetCurrentProcess();
|
2023-03-31 19:05:57 +01:00
|
|
|
|
|
|
|
if (!memory.IsValidVirtualAddress(*process, addr)) {
|
2022-12-03 01:31:31 +00:00
|
|
|
LOG_WARNING(Debug_GDBStub, "Invalid address for HIO request");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-12-03 21:09:13 +00:00
|
|
|
memory.ReadBlock(*process, addr, ¤t_hio_request, sizeof(PackedGdbHioRequest));
|
|
|
|
|
2023-03-31 19:05:57 +01:00
|
|
|
std::string_view magic{current_hio_request.magic};
|
|
|
|
if (magic != "GDB") {
|
|
|
|
LOG_WARNING(Debug_GDBStub, "Invalid HIO request sent by application: magic '{}'", magic);
|
2022-12-03 21:48:21 +00:00
|
|
|
current_hio_request_addr = 0;
|
|
|
|
current_hio_request = {};
|
2022-12-04 20:34:06 +00:00
|
|
|
request_status = Status::NoRequest;
|
2023-03-31 19:05:57 +01:00
|
|
|
return;
|
2022-12-03 21:48:21 +00:00
|
|
|
}
|
2023-03-31 19:05:57 +01:00
|
|
|
|
|
|
|
LOG_DEBUG(Debug_GDBStub, "HIO request initiated at 0x{:X}", addr);
|
|
|
|
current_hio_request_addr = addr;
|
|
|
|
request_status = Status::NotSent;
|
|
|
|
|
|
|
|
// Now halt, so that no further instructions are executed until the request
|
|
|
|
// is processed by the client. We will continue after the reply comes back
|
|
|
|
Break();
|
|
|
|
SetCpuHaltFlag(true);
|
|
|
|
SetCpuStepFlag(false);
|
|
|
|
Core::GetRunningCore().ClearInstructionCache();
|
2022-12-03 01:31:31 +00:00
|
|
|
}
|
|
|
|
|
2022-12-03 21:09:13 +00:00
|
|
|
bool HandleHioReply(const u8* const command_buffer, const u32 command_length) {
|
2022-12-04 20:34:06 +00:00
|
|
|
if (!WaitingForHioReply()) {
|
2022-12-03 21:09:13 +00:00
|
|
|
LOG_WARNING(Debug_GDBStub, "Got HIO reply but never sent a request");
|
2022-12-03 01:31:31 +00:00
|
|
|
// TODO send error reply packet?
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-03-31 19:05:57 +01:00
|
|
|
// Skip 'F' header
|
2022-12-03 21:09:13 +00:00
|
|
|
auto* command_pos = command_buffer + 1;
|
2022-12-03 01:31:31 +00:00
|
|
|
|
|
|
|
if (*command_pos == 0 || *command_pos == ',') {
|
|
|
|
// return GDB_ReplyErrno(ctx, EILSEQ);
|
|
|
|
return false;
|
2022-12-03 21:09:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set the sign of the retval
|
|
|
|
if (*command_pos == '-') {
|
2022-12-03 01:31:31 +00:00
|
|
|
command_pos++;
|
|
|
|
current_hio_request.retval = -1;
|
|
|
|
} else {
|
2023-03-31 19:05:57 +01:00
|
|
|
if (*command_pos == '+') {
|
|
|
|
command_pos++;
|
|
|
|
}
|
|
|
|
|
2022-12-03 01:31:31 +00:00
|
|
|
current_hio_request.retval = 1;
|
|
|
|
}
|
|
|
|
|
2023-03-31 19:05:57 +01:00
|
|
|
const std::string command_str{command_pos, command_buffer + command_length};
|
|
|
|
std::vector<std::string> command_parts;
|
|
|
|
Common::SplitString(command_str, ',', command_parts);
|
2022-12-03 01:31:31 +00:00
|
|
|
|
2023-03-31 19:05:57 +01:00
|
|
|
if (command_parts.empty() || command_parts.size() > 3) {
|
|
|
|
LOG_WARNING(Debug_GDBStub, "unexpected reply packet size: {}", command_parts);
|
|
|
|
// return GDB_ReplyErrno(ctx, EILSEQ);
|
|
|
|
return false;
|
|
|
|
}
|
2022-12-03 01:31:31 +00:00
|
|
|
|
2023-03-31 19:05:57 +01:00
|
|
|
u64 unsigned_retval = HexToInt((u8*)command_parts[0].data(), command_parts[0].size());
|
|
|
|
current_hio_request.retval *= unsigned_retval;
|
2022-12-03 21:09:13 +00:00
|
|
|
|
2023-03-31 19:05:57 +01:00
|
|
|
if (command_parts.size() > 1) {
|
|
|
|
// Technically the errno could be signed but in practice this doesn't happen
|
|
|
|
current_hio_request.gdb_errno =
|
|
|
|
HexToInt((u8*)command_parts[1].data(), command_parts[1].size());
|
|
|
|
} else {
|
|
|
|
current_hio_request.gdb_errno = 0;
|
|
|
|
}
|
2022-12-03 01:31:31 +00:00
|
|
|
|
2023-03-31 19:05:57 +01:00
|
|
|
current_hio_request.ctrl_c = false;
|
2022-12-03 01:31:31 +00:00
|
|
|
|
2023-03-31 19:05:57 +01:00
|
|
|
if (command_parts.size() > 2 && !command_parts[2].empty()) {
|
|
|
|
if (command_parts[2][0] != 'C') {
|
|
|
|
return false;
|
|
|
|
// return GDB_ReplyErrno(ctx, EILSEQ);
|
2022-12-03 01:31:31 +00:00
|
|
|
}
|
2023-03-31 19:05:57 +01:00
|
|
|
|
|
|
|
// for now we just ignore any trailing ";..." attachments
|
|
|
|
current_hio_request.ctrl_c = true;
|
2022-12-03 01:31:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::fill(std::begin(current_hio_request.param_format),
|
|
|
|
std::end(current_hio_request.param_format), 0);
|
|
|
|
|
2023-03-31 19:05:57 +01:00
|
|
|
LOG_TRACE(Debug_GDBStub, "HIO reply: {{retval = {}, errno = {}, ctrl_c = {}}}",
|
2022-12-03 21:09:13 +00:00
|
|
|
current_hio_request.retval, current_hio_request.gdb_errno,
|
|
|
|
current_hio_request.ctrl_c);
|
|
|
|
|
|
|
|
const auto process = Core::System::GetInstance().Kernel().GetCurrentProcess();
|
2023-03-31 19:05:57 +01:00
|
|
|
auto& memory = Core::System::GetInstance().Memory();
|
|
|
|
|
2022-12-03 21:09:13 +00:00
|
|
|
// should have been checked when we first initialized the request,
|
|
|
|
// but just double check again before we write to memory
|
2023-03-31 19:05:57 +01:00
|
|
|
if (!memory.IsValidVirtualAddress(*process, current_hio_request_addr)) {
|
|
|
|
LOG_WARNING(Debug_GDBStub, "Invalid address {:#X} to write HIO reply",
|
2022-12-03 21:09:13 +00:00
|
|
|
current_hio_request_addr);
|
|
|
|
return false;
|
|
|
|
}
|
2022-12-03 01:31:31 +00:00
|
|
|
|
2022-12-03 21:09:13 +00:00
|
|
|
memory.WriteBlock(*process, current_hio_request_addr, ¤t_hio_request,
|
|
|
|
sizeof(PackedGdbHioRequest));
|
2022-12-03 01:31:31 +00:00
|
|
|
|
2022-12-04 20:34:06 +00:00
|
|
|
current_hio_request = {};
|
2022-12-03 01:31:31 +00:00
|
|
|
current_hio_request_addr = 0;
|
2022-12-04 20:34:06 +00:00
|
|
|
request_status = Status::NoRequest;
|
2022-12-03 01:31:31 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-12-04 20:34:06 +00:00
|
|
|
bool HasPendingHioRequest() {
|
|
|
|
return current_hio_request_addr != 0 && request_status == Status::NotSent;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool WaitingForHioReply() {
|
|
|
|
return current_hio_request_addr != 0 && request_status == Status::SentWaitingReply;
|
2022-12-03 01:31:31 +00:00
|
|
|
}
|
|
|
|
|
2022-12-03 21:09:13 +00:00
|
|
|
std::string BuildHioRequestPacket() {
|
2023-03-31 19:05:57 +01:00
|
|
|
std::string packet = fmt::format("F{}", current_hio_request.function_name);
|
|
|
|
// TODO: should we use the IntToGdbHex funcs instead of fmt::format_to ?
|
2022-12-03 01:31:31 +00:00
|
|
|
|
2022-12-03 21:48:21 +00:00
|
|
|
u32 nStr = 0;
|
2022-12-03 01:31:31 +00:00
|
|
|
|
|
|
|
for (u32 i = 0; i < 8 && current_hio_request.param_format[i] != 0; i++) {
|
2023-03-31 19:05:57 +01:00
|
|
|
u64 param = current_hio_request.parameters[i];
|
|
|
|
|
2022-12-03 01:31:31 +00:00
|
|
|
switch (current_hio_request.param_format[i]) {
|
|
|
|
case 'i':
|
|
|
|
case 'I':
|
|
|
|
case 'p':
|
2023-03-31 19:05:57 +01:00
|
|
|
// For pointers and 32-bit ints, truncate before sending
|
|
|
|
param = static_cast<u32>(param);
|
|
|
|
|
2022-12-03 01:31:31 +00:00
|
|
|
case 'l':
|
|
|
|
case 'L':
|
2023-03-31 19:05:57 +01:00
|
|
|
fmt::format_to(std::back_inserter(packet), ",{:x}", param);
|
2022-12-03 01:31:31 +00:00
|
|
|
break;
|
2023-03-31 19:05:57 +01:00
|
|
|
|
2022-12-03 01:31:31 +00:00
|
|
|
case 's':
|
2023-03-31 19:05:57 +01:00
|
|
|
// strings are written as {pointer}/{length}
|
|
|
|
fmt::format_to(std::back_inserter(packet), ",{:x}/{:x}",
|
|
|
|
(u32)current_hio_request.parameters[i],
|
|
|
|
current_hio_request.string_lengths[nStr++]);
|
2022-12-03 01:31:31 +00:00
|
|
|
break;
|
2023-03-31 19:05:57 +01:00
|
|
|
|
2022-12-03 01:31:31 +00:00
|
|
|
default:
|
2023-03-31 19:05:57 +01:00
|
|
|
// TODO: early return? Or break out of this loop?
|
2022-12-03 01:31:31 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-31 19:05:57 +01:00
|
|
|
LOG_TRACE(Debug_GDBStub, "HIO request packet: {}", packet);
|
2022-12-04 20:34:06 +00:00
|
|
|
|
2023-03-31 19:05:57 +01:00
|
|
|
// technically we should set this _after_ the reply is sent...
|
2022-12-04 20:34:06 +00:00
|
|
|
request_status = Status::SentWaitingReply;
|
2022-12-03 21:09:13 +00:00
|
|
|
|
2023-03-31 19:05:57 +01:00
|
|
|
return packet;
|
2022-12-03 01:31:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace GDBStub
|