gdb: Code cleanup pass #2

Moved the reply functionality into the command processor, move the main debugger thread into a dedicated class part, and more
This commit is contained in:
GreemDev 2025-10-17 00:09:51 -05:00
parent 8e941e4a8f
commit 2a2ab523cb
9 changed files with 391 additions and 398 deletions

View file

@ -1,7 +1,8 @@
using Gommon;
using Ryujinx.Common;
using Ryujinx.Common.Logging;
using Ryujinx.HLE.HOS.Kernel.Threading;
using System.Linq;
using System.Net.Sockets;
using System.Text;
namespace Ryujinx.HLE.Debugger.Gdb
@ -10,13 +11,41 @@ namespace Ryujinx.HLE.Debugger.Gdb
{
public readonly GdbCommands Commands;
public GdbCommandProcessor(TcpListener listenerSocket, Socket clientSocket, NetworkStream readStream, NetworkStream writeStream, Debugger debugger)
private Debugger Debugger => Commands.Debugger;
private BreakpointManager BreakpointManager => Commands.Debugger.BreakpointManager;
private IDebuggableProcess DebugProcess => Commands.Debugger.DebugProcess;
public GdbCommandProcessor(GdbCommands commands)
{
Commands = new GdbCommands(listenerSocket, clientSocket, readStream, writeStream, debugger);
Commands = commands;
}
private string previousThreadListXml = "";
public void Reply(string cmd)
{
Logger.Debug?.Print(LogClass.GdbStub, $"Reply: {cmd}");
Commands.WriteStream.Write(Encoding.ASCII.GetBytes($"${cmd}#{Helpers.CalculateChecksum(cmd):x2}"));
}
public void ReplyOK() => Reply("OK");
public void ReplyError() => Reply("E01");
public void Reply(bool success)
{
if (success)
ReplyOK();
else ReplyError();
}
public void Reply(bool success, string cmd)
{
if (success)
Reply(cmd);
else ReplyError();
}
private string _previousThreadListXml = string.Empty;
public void Process(string cmd)
{
StringStream ss = new(cmd);
@ -30,7 +59,7 @@ namespace Ryujinx.HLE.Debugger.Gdb
}
// Enable extended mode
Commands.ReplyOK();
ReplyOK();
break;
case '?':
if (!ss.IsEmpty())
@ -38,10 +67,10 @@ namespace Ryujinx.HLE.Debugger.Gdb
goto unknownCommand;
}
Commands.CommandQuery();
Commands.Query();
break;
case 'c':
Commands.CommandContinue(ss.IsEmpty() ? null : ss.ReadRemainingAsHex());
Commands.Continue(ss.IsEmpty() ? null : ss.ReadRemainingAsHex());
break;
case 'D':
if (!ss.IsEmpty())
@ -49,7 +78,7 @@ namespace Ryujinx.HLE.Debugger.Gdb
goto unknownCommand;
}
Commands.CommandDetach();
Commands.Detach();
break;
case 'g':
if (!ss.IsEmpty())
@ -57,110 +86,99 @@ namespace Ryujinx.HLE.Debugger.Gdb
goto unknownCommand;
}
Commands.CommandReadRegisters();
Commands.ReadRegisters();
break;
case 'G':
Commands.CommandWriteRegisters(ss);
Commands.WriteRegisters(ss);
break;
case 'H':
{
char op = ss.ReadChar();
ulong? threadId = ss.ReadRemainingAsThreadUid();
Commands.CommandSetThread(op, threadId);
Commands.SetThread(op, threadId);
break;
}
case 'k':
Logger.Notice.Print(LogClass.GdbStub, "Kill request received, detach instead");
Commands.Reply("");
Commands.CommandDetach();
Reply(string.Empty);
Commands.Detach();
break;
case 'm':
{
ulong addr = ss.ReadUntilAsHex(',');
ulong len = ss.ReadRemainingAsHex();
Commands.CommandReadMemory(addr, len);
Commands.ReadMemory(addr, len);
break;
}
case 'M':
{
ulong addr = ss.ReadUntilAsHex(',');
ulong len = ss.ReadUntilAsHex(':');
Commands.CommandWriteMemory(addr, len, ss);
Commands.WriteMemory(addr, len, ss);
break;
}
case 'p':
{
ulong gdbRegId = ss.ReadRemainingAsHex();
Commands.CommandReadRegister((int)gdbRegId);
Commands.ReadRegister((int)gdbRegId);
break;
}
case 'P':
{
ulong gdbRegId = ss.ReadUntilAsHex('=');
Commands.CommandWriteRegister((int)gdbRegId, ss);
Commands.WriteRegister((int)gdbRegId, ss);
break;
}
case 'q':
if (ss.ConsumeRemaining("GDBServerVersion"))
{
Commands.Reply($"name:Ryujinx;version:{ReleaseInformation.Version};");
Reply($"name:Ryujinx;version:{ReleaseInformation.Version};");
break;
}
if (ss.ConsumeRemaining("HostInfo"))
{
if (Commands.Debugger.IsProcessAarch32)
{
Commands.Reply(
$"triple:{Helpers.ToHex("arm-unknown-linux-android")};endian:little;ptrsize:4;hostname:{Helpers.ToHex("Ryujinx")};");
}
else
{
Commands.Reply(
$"triple:{Helpers.ToHex("aarch64-unknown-linux-android")};endian:little;ptrsize:8;hostname:{Helpers.ToHex("Ryujinx")};");
}
Reply(
Debugger.IsProcess32Bit
? $"triple:{Helpers.ToHex("arm-unknown-linux-android")};endian:little;ptrsize:4;hostname:{Helpers.ToHex("Ryujinx")};"
: $"triple:{Helpers.ToHex("aarch64-unknown-linux-android")};endian:little;ptrsize:8;hostname:{Helpers.ToHex("Ryujinx")};");
break;
}
if (ss.ConsumeRemaining("ProcessInfo"))
{
if (Commands.Debugger.IsProcessAarch32)
{
Commands.Reply(
$"pid:1;cputype:12;cpusubtype:0;triple:{Helpers.ToHex("arm-unknown-linux-android")};ostype:unknown;vendor:none;endian:little;ptrsize:4;");
}
else
{
Commands.Reply(
$"pid:1;cputype:100000c;cpusubtype:0;triple:{Helpers.ToHex("aarch64-unknown-linux-android")};ostype:unknown;vendor:none;endian:little;ptrsize:8;");
}
Reply(
Debugger.IsProcess32Bit
? $"pid:1;cputype:12;cpusubtype:0;triple:{Helpers.ToHex("arm-unknown-linux-android")};ostype:unknown;vendor:none;endian:little;ptrsize:4;"
: $"pid:1;cputype:100000c;cpusubtype:0;triple:{Helpers.ToHex("aarch64-unknown-linux-android")};ostype:unknown;vendor:none;endian:little;ptrsize:8;");
break;
}
if (ss.ConsumePrefix("Supported:") || ss.ConsumeRemaining("Supported"))
{
Commands.Reply("PacketSize=10000;qXfer:features:read+;qXfer:threads:read+;vContSupported+");
Reply("PacketSize=10000;qXfer:features:read+;qXfer:threads:read+;vContSupported+");
break;
}
if (ss.ConsumePrefix("Rcmd,"))
{
string hexCommand = ss.ReadRemaining();
Commands.HandleQRcmdCommand(hexCommand);
Commands.Q_Rcmd(hexCommand);
break;
}
if (ss.ConsumeRemaining("fThreadInfo"))
{
Commands. Reply($"m{string.Join(",", Commands.Debugger.DebugProcess.GetThreadUids().Select(x => $"{x:x}"))}");
Reply(
$"m{Debugger.DebugProcess.GetThreadUids().Select(x => $"{x:x}").JoinToString(",")}");
break;
}
if (ss.ConsumeRemaining("sThreadInfo"))
{
Commands.Reply("l");
Reply("l");
break;
}
@ -169,15 +187,14 @@ namespace Ryujinx.HLE.Debugger.Gdb
ulong? threadId = ss.ReadRemainingAsThreadUid();
if (threadId == null)
{
Commands.ReplyError();
ReplyError();
break;
}
Commands.Reply(Helpers.ToHex(
Commands.Debugger.DebugProcess.IsThreadPaused(
Commands.Debugger.DebugProcess.GetThread(threadId.Value))
? "Paused"
: "Running"
Reply(Helpers.ToHex(
DebugProcess.IsThreadPaused(DebugProcess.GetThread(threadId.Value))
? "Paused"
: "Running"
)
);
@ -190,32 +207,32 @@ namespace Ryujinx.HLE.Debugger.Gdb
ulong offset = ss.ReadUntilAsHex(',');
ulong len = ss.ReadRemainingAsHex();
var data = "";
string data;
if (offset > 0)
{
data = previousThreadListXml;
data = _previousThreadListXml;
}
else
{
previousThreadListXml = data = GetThreadListXml();
_previousThreadListXml = data = GetThreadListXml();
}
if (offset >= (ulong)data.Length)
{
Commands.Reply("l");
Reply("l");
break;
}
if (len >= (ulong)data.Length - offset)
{
Commands.Reply("l" + Helpers.ToBinaryFormat(data.Substring((int)offset)));
break;
Reply("l" + Helpers.ToBinaryFormat(data.Substring((int)offset)));
}
else
{
Commands.Reply("m" + Helpers.ToBinaryFormat(data.Substring((int)offset, (int)len)));
break;
Reply("m" + Helpers.ToBinaryFormat(data.Substring((int)offset, (int)len)));
}
break;
}
if (ss.ConsumePrefix("Xfer:features:read:"))
@ -226,46 +243,43 @@ namespace Ryujinx.HLE.Debugger.Gdb
if (feature == "target.xml")
{
feature = Commands.Debugger.IsProcessAarch32 ? "target32.xml" : "target64.xml";
feature = Debugger.IsProcess32Bit ? "target32.xml" : "target64.xml";
}
string data;
if (RegisterInformation.Features.TryGetValue(feature, out data))
if (!RegisterInformation.Features.TryGetValue(feature, out string data))
{
if (offset >= (ulong)data.Length)
{
Commands.Reply("l");
break;
}
Reply("E00"); // Invalid annex
break;
}
if (len >= (ulong)data.Length - offset)
{
Commands.Reply("l" + Helpers.ToBinaryFormat(data.Substring((int)offset)));
break;
}
else
{
Commands.Reply("m" + Helpers.ToBinaryFormat(data.Substring((int)offset, (int)len)));
break;
}
if (offset >= (ulong)data.Length)
{
Reply("l");
break;
}
if (len >= (ulong)data.Length - offset)
{
Reply("l" + Helpers.ToBinaryFormat(data[(int)offset..]));
}
else
{
Commands.Reply("E00"); // Invalid annex
break;
Reply("m" + Helpers.ToBinaryFormat(data.Substring((int)offset, (int)len)));
}
break;
}
goto unknownCommand;
case 'Q':
goto unknownCommand;
case 's':
Commands.CommandStep(ss.IsEmpty() ? null : ss.ReadRemainingAsHex());
Commands.Step(ss.IsEmpty() ? null : ss.ReadRemainingAsHex());
break;
case 'T':
{
ulong? threadId = ss.ReadRemainingAsThreadUid();
Commands.CommandIsAlive(threadId);
Commands.IsAlive(threadId);
break;
}
case 'v':
@ -273,13 +287,13 @@ namespace Ryujinx.HLE.Debugger.Gdb
{
if (ss.ConsumeRemaining("?"))
{
Commands.Reply("vCont;c;C;s;S");
Reply("vCont;c;C;s;S");
break;
}
if (ss.ConsumePrefix(";"))
{
Commands.HandleVContCommand(ss);
Commands.VCont(ss);
break;
}
@ -288,7 +302,7 @@ namespace Ryujinx.HLE.Debugger.Gdb
if (ss.ConsumeRemaining("MustReplyEmpty"))
{
Commands.Reply("");
Reply(string.Empty);
break;
}
@ -303,29 +317,29 @@ namespace Ryujinx.HLE.Debugger.Gdb
if (extra.Length > 0)
{
Logger.Notice.Print(LogClass.GdbStub, $"Unsupported Z command extra data: {extra}");
Commands.ReplyError();
ReplyError();
return;
}
switch (type)
{
case "0": // Software breakpoint
if (!Commands.Debugger.BreakpointManager.SetBreakPoint(addr, len))
if (!BreakpointManager.SetBreakPoint(addr, len))
{
Commands.ReplyError();
ReplyError();
return;
}
Commands.ReplyOK();
ReplyOK();
return;
// ReSharper disable RedundantCaseLabel
case "1": // Hardware breakpoint
case "2": // Write watchpoint
case "3": // Read watchpoint
case "4": // Access watchpoint
Commands.ReplyError();
return;
// ReSharper restore RedundantCaseLabel
default:
Commands.ReplyError();
ReplyError();
return;
}
}
@ -340,50 +354,50 @@ namespace Ryujinx.HLE.Debugger.Gdb
if (extra.Length > 0)
{
Logger.Notice.Print(LogClass.GdbStub, $"Unsupported z command extra data: {extra}");
Commands.ReplyError();
ReplyError();
return;
}
switch (type)
{
case "0": // Software breakpoint
if (!Commands.Debugger.BreakpointManager.ClearBreakPoint(addr, len))
if (!BreakpointManager.ClearBreakPoint(addr, len))
{
Commands.ReplyError();
ReplyError();
return;
}
Commands.ReplyOK();
ReplyOK();
return;
// ReSharper disable RedundantCaseLabel
case "1": // Hardware breakpoint
case "2": // Write watchpoint
case "3": // Read watchpoint
case "4": // Access watchpoint
Commands.ReplyError();
return;
// ReSharper restore RedundantCaseLabel
default:
Commands.ReplyError();
ReplyError();
return;
}
}
default:
unknownCommand:
Logger.Notice.Print(LogClass.GdbStub, $"Unknown command: {cmd}");
Commands.Reply("");
Reply(string.Empty);
break;
}
}
private string GetThreadListXml()
{
var sb = new StringBuilder();
StringBuilder sb = new();
sb.Append("<?xml version=\"1.0\"?><threads>\n");
foreach (var thread in Commands.Debugger.GetThreads())
foreach (KThread thread in Debugger.GetThreads())
{
string threadName = System.Security.SecurityElement.Escape(thread.GetThreadName());
sb.Append(
$"<thread id=\"{thread.ThreadUid:x}\" name=\"{threadName}\">{(Commands.Debugger.DebugProcess.IsThreadPaused(thread) ? "Paused" : "Running")}</thread>\n");
$"<thread id=\"{thread.ThreadUid:x}\" name=\"{threadName}\">{(DebugProcess.IsThreadPaused(thread) ? "Paused" : "Running")}</thread>\n");
}
sb.Append("</threads>");