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

@ -25,9 +25,9 @@ namespace Ryujinx.HLE.Debugger
private readonly Debugger _debugger; private readonly Debugger _debugger;
private readonly ConcurrentDictionary<ulong, Breakpoint> _breakpoints = new(); private readonly ConcurrentDictionary<ulong, Breakpoint> _breakpoints = new();
private static readonly byte[] _aarch64BreakInstruction = { 0x00, 0x00, 0x20, 0xD4 }; // BRK #0 private static readonly byte[] _aarch64BreakInstruction = [0x00, 0x00, 0x20, 0xD4]; // BRK #0
private static readonly byte[] _aarch32BreakInstruction = { 0xFE, 0xDE, 0xFF, 0xE7 }; // TRAP private static readonly byte[] _aarch32BreakInstruction = [0xFE, 0xDE, 0xFF, 0xE7]; // TRAP
private static readonly byte[] _aarch32ThumbBreakInstruction = { 0x80, 0xB6 }; private static readonly byte[] _aarch32ThumbBreakInstruction = [0x80, 0xB6];
public BreakpointManager(Debugger debugger) public BreakpointManager(Debugger debugger)
{ {
@ -123,7 +123,7 @@ namespace Ryujinx.HLE.Debugger
private byte[] GetBreakInstruction(ulong length) private byte[] GetBreakInstruction(ulong length)
{ {
if (_debugger.IsProcessAarch32) if (_debugger.IsProcess32Bit)
{ {
if (length == 2) if (length == 2)
{ {

View file

@ -0,0 +1,115 @@
using Ryujinx.Common.Logging;
using Ryujinx.HLE.Debugger.Gdb;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace Ryujinx.HLE.Debugger
{
public partial class Debugger
{
private void DebuggerThreadMain()
{
IPEndPoint endpoint = new(IPAddress.Any, GdbStubPort);
_listenerSocket = new TcpListener(endpoint);
_listenerSocket.Start();
Logger.Notice.Print(LogClass.GdbStub, $"Currently waiting on {endpoint} for GDB client");
while (!_shuttingDown)
{
try
{
_clientSocket = _listenerSocket.AcceptSocket();
}
catch (SocketException)
{
return;
}
// If the user connects before the application is running, wait for the application to start.
int retries = 10;
while ((DebugProcess == null || GetThreads().Length == 0) && retries-- > 0)
{
Thread.Sleep(200);
}
if (DebugProcess == null || GetThreads().Length == 0)
{
Logger.Warning?.Print(LogClass.GdbStub,
"Application is not running, cannot accept GDB client connection");
_clientSocket.Close();
continue;
}
_clientSocket.NoDelay = true;
_readStream = new NetworkStream(_clientSocket, System.IO.FileAccess.Read);
_writeStream = new NetworkStream(_clientSocket, System.IO.FileAccess.Write);
_commands = new GdbCommands(_listenerSocket, _clientSocket, _readStream, _writeStream, this);
_commandProcessor = _commands.CreateProcessor();
Logger.Notice.Print(LogClass.GdbStub, "GDB client connected");
while (true)
{
try
{
switch (_readStream.ReadByte())
{
case -1:
goto EndOfLoop;
case '+':
continue;
case '-':
Logger.Notice.Print(LogClass.GdbStub, "NACK received!");
continue;
case '\x03':
_messages.Add(new BreakInMessage());
break;
case '$':
string cmd = "";
while (true)
{
int x = _readStream.ReadByte();
if (x == -1)
goto EndOfLoop;
if (x == '#')
break;
cmd += (char)x;
}
string checksum = $"{(char)_readStream.ReadByte()}{(char)_readStream.ReadByte()}";
if (checksum == $"{Helpers.CalculateChecksum(cmd):x2}")
{
_messages.Add(new CommandMessage(cmd));
}
else
{
_messages.Add(new SendNackMessage());
}
break;
}
}
catch (IOException)
{
goto EndOfLoop;
}
}
EndOfLoop:
Logger.Notice.Print(LogClass.GdbStub, "GDB client lost connection");
_readStream.Close();
_readStream = null;
_writeStream.Close();
_writeStream = null;
_clientSocket.Close();
_clientSocket = null;
_commandProcessor = null;
_commands = null;
BreakpointManager.ClearAll();
}
}
}
}

View file

@ -6,37 +6,39 @@ using System;
using System.Collections.Concurrent; using System.Collections.Concurrent;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Net;
using System.Net.Sockets; using System.Net.Sockets;
using System.Text; using System.Text;
using System.Threading; using System.Threading;
using IExecutionContext = Ryujinx.Cpu.IExecutionContext; using IExecutionContext = Ryujinx.Cpu.IExecutionContext;
using static Ryujinx.HLE.Debugger.Helpers;
namespace Ryujinx.HLE.Debugger namespace Ryujinx.HLE.Debugger
{ {
public class Debugger : IDisposable public partial class Debugger : IDisposable
{ {
internal Switch Device { get; private set; } internal Switch Device { get; private set; }
public ushort GdbStubPort { get; private set; } public ushort GdbStubPort { get; private set; }
private TcpListener ListenerSocket; private readonly BlockingCollection<IMessage> _messages = new(1);
private Socket ClientSocket = null; private readonly Thread _debuggerThread;
private NetworkStream ReadStream = null; private readonly Thread _messageHandlerThread;
private NetworkStream WriteStream = null;
private BlockingCollection<IMessage> Messages = new(1);
private Thread DebuggerThread;
private Thread MessageHandlerThread;
private bool _shuttingDown = false;
private ManualResetEventSlim _breakHandlerEvent = new(false);
private GdbCommandProcessor CommandProcessor = null; private TcpListener _listenerSocket;
private Socket _clientSocket;
private NetworkStream _readStream;
private NetworkStream _writeStream;
private GdbCommandProcessor _commandProcessor;
private GdbCommands _commands;
private bool _shuttingDown;
private readonly ManualResetEventSlim _breakHandlerEvent = new(false);
internal ulong? CThread; internal ulong? CThread;
internal ulong? GThread; internal ulong? GThread;
internal BreakpointManager BreakpointManager; public readonly BreakpointManager BreakpointManager;
public Debugger(Switch device, ushort port) public Debugger(Switch device, ushort port)
{ {
@ -45,10 +47,10 @@ namespace Ryujinx.HLE.Debugger
ARMeilleure.Optimizations.EnableDebugging = true; ARMeilleure.Optimizations.EnableDebugging = true;
DebuggerThread = new Thread(DebuggerThreadMain); _debuggerThread = new Thread(DebuggerThreadMain);
DebuggerThread.Start(); _debuggerThread.Start();
MessageHandlerThread = new Thread(MessageHandlerMain); _messageHandlerThread = new Thread(MessageHandlerMain);
MessageHandlerThread.Start(); _messageHandlerThread.Start();
BreakpointManager = new BreakpointManager(this); BreakpointManager = new BreakpointManager(this);
} }
@ -58,37 +60,37 @@ namespace Ryujinx.HLE.Debugger
internal KThread[] GetThreads() => internal KThread[] GetThreads() =>
DebugProcess.GetThreadUids().Select(x => DebugProcess.GetThread(x)).ToArray(); DebugProcess.GetThreadUids().Select(x => DebugProcess.GetThread(x)).ToArray();
internal bool IsProcessAarch32 => DebugProcess.GetThread(GThread.Value).Context.IsAarch32; internal bool IsProcess32Bit => DebugProcess.GetThread(GThread.Value).Context.IsAarch32;
private void MessageHandlerMain() private void MessageHandlerMain()
{ {
while (!_shuttingDown) while (!_shuttingDown)
{ {
IMessage msg = Messages.Take(); IMessage msg = _messages.Take();
try try
{ {
switch (msg) switch (msg)
{ {
case BreakInMessage: case BreakInMessage:
Logger.Notice.Print(LogClass.GdbStub, "Break-in requested"); Logger.Notice.Print(LogClass.GdbStub, "Break-in requested");
CommandProcessor.Commands.CommandInterrupt(); _commandProcessor.Commands.Interrupt();
break; break;
case SendNackMessage: case SendNackMessage:
WriteStream.WriteByte((byte)'-'); _writeStream.WriteByte((byte)'-');
break; break;
case CommandMessage { Command: var cmd }: case CommandMessage { Command: var cmd }:
Logger.Debug?.Print(LogClass.GdbStub, $"Received Command: {cmd}"); Logger.Debug?.Print(LogClass.GdbStub, $"Received Command: {cmd}");
WriteStream.WriteByte((byte)'+'); _writeStream.WriteByte((byte)'+');
CommandProcessor.Process(cmd); _commandProcessor.Process(cmd);
break; break;
case ThreadBreakMessage { Context: var ctx }: case ThreadBreakMessage { Context: var ctx }:
DebugProcess.DebugStop(); DebugProcess.DebugStop();
GThread = CThread = ctx.ThreadUid; GThread = CThread = ctx.ThreadUid;
_breakHandlerEvent.Set(); _breakHandlerEvent.Set();
CommandProcessor.Commands.Reply($"T05thread:{ctx.ThreadUid:x};"); _commandProcessor.Reply($"T05thread:{ctx.ThreadUid:x};");
break; break;
case KillMessage: case KillMessage:
@ -214,106 +216,6 @@ namespace Ryujinx.HLE.Debugger
} }
} }
private void DebuggerThreadMain()
{
var endpoint = new IPEndPoint(IPAddress.Any, GdbStubPort);
ListenerSocket = new TcpListener(endpoint);
ListenerSocket.Start();
Logger.Notice.Print(LogClass.GdbStub, $"Currently waiting on {endpoint} for GDB client");
while (!_shuttingDown)
{
try
{
ClientSocket = ListenerSocket.AcceptSocket();
}
catch (SocketException)
{
return;
}
// If the user connects before the application is running, wait for the application to start.
int retries = 10;
while ((DebugProcess == null || GetThreads().Length == 0) && retries-- > 0)
{
Thread.Sleep(200);
}
if (DebugProcess == null || GetThreads().Length == 0)
{
Logger.Warning?.Print(LogClass.GdbStub,
"Application is not running, cannot accept GDB client connection");
ClientSocket.Close();
continue;
}
ClientSocket.NoDelay = true;
ReadStream = new NetworkStream(ClientSocket, System.IO.FileAccess.Read);
WriteStream = new NetworkStream(ClientSocket, System.IO.FileAccess.Write);
CommandProcessor = new GdbCommandProcessor(ListenerSocket, ClientSocket, ReadStream, WriteStream, this);
Logger.Notice.Print(LogClass.GdbStub, "GDB client connected");
while (true)
{
try
{
switch (ReadStream.ReadByte())
{
case -1:
goto EndOfLoop;
case '+':
continue;
case '-':
Logger.Notice.Print(LogClass.GdbStub, "NACK received!");
continue;
case '\x03':
Messages.Add(new BreakInMessage());
break;
case '$':
string cmd = "";
while (true)
{
int x = ReadStream.ReadByte();
if (x == -1)
goto EndOfLoop;
if (x == '#')
break;
cmd += (char)x;
}
string checksum = $"{(char)ReadStream.ReadByte()}{(char)ReadStream.ReadByte()}";
if (checksum == $"{CalculateChecksum(cmd):x2}")
{
Messages.Add(new CommandMessage(cmd));
}
else
{
Messages.Add(new SendNackMessage());
}
break;
}
}
catch (IOException)
{
goto EndOfLoop;
}
}
EndOfLoop:
Logger.Notice.Print(LogClass.GdbStub, "GDB client lost connection");
ReadStream.Close();
ReadStream = null;
WriteStream.Close();
WriteStream = null;
ClientSocket.Close();
ClientSocket = null;
CommandProcessor = null;
BreakpointManager.ClearAll();
}
}
public void Dispose() public void Dispose()
{ {
Dispose(true); Dispose(true);
@ -325,15 +227,15 @@ namespace Ryujinx.HLE.Debugger
{ {
_shuttingDown = true; _shuttingDown = true;
ListenerSocket.Stop(); _listenerSocket.Stop();
ClientSocket?.Shutdown(SocketShutdown.Both); _clientSocket?.Shutdown(SocketShutdown.Both);
ClientSocket?.Close(); _clientSocket?.Close();
ReadStream?.Close(); _readStream?.Close();
WriteStream?.Close(); _writeStream?.Close();
DebuggerThread.Join(); _debuggerThread.Join();
Messages.Add(new KillMessage()); _messages.Add(new KillMessage());
MessageHandlerThread.Join(); _messageHandlerThread.Join();
Messages.Dispose(); _messages.Dispose();
_breakHandlerEvent.Dispose(); _breakHandlerEvent.Dispose();
} }
} }
@ -343,7 +245,7 @@ namespace Ryujinx.HLE.Debugger
DebugProcess.DebugInterruptHandler(ctx); DebugProcess.DebugInterruptHandler(ctx);
_breakHandlerEvent.Reset(); _breakHandlerEvent.Reset();
Messages.Add(new ThreadBreakMessage(ctx, address, imm)); _messages.Add(new ThreadBreakMessage(ctx, address, imm));
// Messages.Add can block, so we log it after adding the message to make sure user can see the log at the same time GDB receives the break message // Messages.Add can block, so we log it after adding the message to make sure user can see the log at the same time GDB receives the break message
Logger.Notice.Print(LogClass.GdbStub, $"Break hit on thread {ctx.ThreadUid} at pc {address:x016}"); Logger.Notice.Print(LogClass.GdbStub, $"Break hit on thread {ctx.ThreadUid} at pc {address:x016}");
// Wait for the process to stop before returning to avoid BreakHandler being called multiple times from the same breakpoint // Wait for the process to stop before returning to avoid BreakHandler being called multiple times from the same breakpoint

View file

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

View file

@ -1,10 +1,11 @@
using Ryujinx.Common.Logging; using Ryujinx.Common.Logging;
using Ryujinx.Cpu;
using Ryujinx.HLE.HOS.Kernel.Threading;
using Ryujinx.Memory; using Ryujinx.Memory;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Net.Sockets; using System.Net.Sockets;
using System.Text;
namespace Ryujinx.HLE.Debugger.Gdb namespace Ryujinx.HLE.Debugger.Gdb
{ {
@ -15,41 +16,48 @@ namespace Ryujinx.HLE.Debugger.Gdb
public readonly Debugger Debugger; public readonly Debugger Debugger;
private readonly TcpListener _listenerSocket; public GdbCommandProcessor Processor { get; private set; }
private readonly Socket _clientSocket;
private readonly NetworkStream _readStream; internal readonly TcpListener ListenerSocket;
private readonly NetworkStream _writeStream; internal readonly Socket ClientSocket;
internal readonly NetworkStream ReadStream;
internal readonly NetworkStream WriteStream;
public GdbCommands(TcpListener listenerSocket, Socket clientSocket, NetworkStream readStream, public GdbCommands(TcpListener listenerSocket, Socket clientSocket, NetworkStream readStream,
NetworkStream writeStream, Debugger debugger) NetworkStream writeStream, Debugger debugger)
{ {
_listenerSocket = listenerSocket; ListenerSocket = listenerSocket;
_clientSocket = clientSocket; ClientSocket = clientSocket;
_readStream = readStream; ReadStream = readStream;
_writeStream = writeStream; WriteStream = writeStream;
Debugger = debugger; Debugger = debugger;
} }
public void Reply(string cmd) public void SetProcessor(GdbCommandProcessor commandProcessor)
{ {
Logger.Debug?.Print(LogClass.GdbStub, $"Reply: {cmd}"); if (Processor != null) return;
_writeStream.Write(Encoding.ASCII.GetBytes($"${cmd}#{Helpers.CalculateChecksum(cmd):x2}"));
Processor = commandProcessor;
} }
public void ReplyOK() => Reply("OK"); public GdbCommandProcessor CreateProcessor()
{
if (Processor != null)
return Processor;
public void ReplyError() => Reply("E01"); return Processor = new GdbCommandProcessor(this);
}
internal void CommandQuery() internal void Query()
{ {
// GDB is performing initial contact. Stop everything. // GDB is performing initial contact. Stop everything.
Debugger.DebugProcess.DebugStop(); Debugger.DebugProcess.DebugStop();
Debugger.GThread = Debugger.CThread = Debugger.DebugProcess.GetThreadUids().First(); Debugger.GThread = Debugger.CThread = Debugger.DebugProcess.GetThreadUids().First();
Reply($"T05thread:{Debugger.CThread:x};"); Processor.Reply($"T05thread:{Debugger.CThread:x};");
} }
internal void CommandInterrupt() internal void Interrupt()
{ {
// GDB is requesting an interrupt. Stop everything. // GDB is requesting an interrupt. Stop everything.
Debugger.DebugProcess.DebugStop(); Debugger.DebugProcess.DebugStop();
@ -58,16 +66,16 @@ namespace Ryujinx.HLE.Debugger.Gdb
Debugger.GThread = Debugger.CThread = Debugger.DebugProcess.GetThreadUids().First(); Debugger.GThread = Debugger.CThread = Debugger.DebugProcess.GetThreadUids().First();
} }
Reply($"T02thread:{Debugger.GThread:x};"); Processor.Reply($"T02thread:{Debugger.GThread:x};");
} }
internal void CommandContinue(ulong? newPc) internal void Continue(ulong? newPc)
{ {
if (newPc.HasValue) if (newPc.HasValue)
{ {
if (Debugger.CThread == null) if (Debugger.CThread == null)
{ {
ReplyError(); Processor.ReplyError();
return; return;
} }
@ -77,23 +85,23 @@ namespace Ryujinx.HLE.Debugger.Gdb
Debugger.DebugProcess.DebugContinue(); Debugger.DebugProcess.DebugContinue();
} }
internal void CommandDetach() internal void Detach()
{ {
Debugger.BreakpointManager.ClearAll(); Debugger.BreakpointManager.ClearAll();
CommandContinue(null); Continue(null);
} }
internal void CommandReadRegisters() internal void ReadRegisters()
{ {
if (Debugger.GThread == null) if (Debugger.GThread == null)
{ {
ReplyError(); Processor.ReplyError();
return; return;
} }
var ctx = Debugger.DebugProcess.GetThread(Debugger.GThread.Value).Context; IExecutionContext ctx = Debugger.DebugProcess.GetThread(Debugger.GThread.Value).Context;
string registers = ""; string registers = string.Empty;
if (Debugger.IsProcessAarch32) if (Debugger.IsProcess32Bit)
{ {
for (int i = 0; i < GdbRegisterCount32; i++) for (int i = 0; i < GdbRegisterCount32; i++)
{ {
@ -108,25 +116,25 @@ namespace Ryujinx.HLE.Debugger.Gdb
} }
} }
Reply(registers); Processor.Reply(registers);
} }
internal void CommandWriteRegisters(StringStream ss) internal void WriteRegisters(StringStream ss)
{ {
if (Debugger.GThread == null) if (Debugger.GThread == null)
{ {
ReplyError(); Processor.ReplyError();
return; return;
} }
var ctx = Debugger.DebugProcess.GetThread(Debugger.GThread.Value).Context; IExecutionContext ctx = Debugger.DebugProcess.GetThread(Debugger.GThread.Value).Context;
if (Debugger.IsProcessAarch32) if (Debugger.IsProcess32Bit)
{ {
for (int i = 0; i < GdbRegisterCount32; i++) for (int i = 0; i < GdbRegisterCount32; i++)
{ {
if (!GdbRegisters.Write32(ctx, i, ss)) if (!GdbRegisters.Write32(ctx, i, ss))
{ {
ReplyError(); Processor.ReplyError();
return; return;
} }
} }
@ -137,30 +145,23 @@ namespace Ryujinx.HLE.Debugger.Gdb
{ {
if (!GdbRegisters.Write64(ctx, i, ss)) if (!GdbRegisters.Write64(ctx, i, ss))
{ {
ReplyError(); Processor.ReplyError();
return; return;
} }
} }
} }
if (ss.IsEmpty()) Processor.Reply(ss.IsEmpty());
{
ReplyOK();
}
else
{
ReplyError();
}
} }
internal void CommandSetThread(char op, ulong? threadId) internal void SetThread(char op, ulong? threadId)
{ {
if (threadId is 0 or null) if (threadId is 0 or null)
{ {
var threads = Debugger.GetThreads(); KThread[] threads = Debugger.GetThreads();
if (threads.Length == 0) if (threads.Length == 0)
{ {
ReplyError(); Processor.ReplyError();
return; return;
} }
@ -169,7 +170,7 @@ namespace Ryujinx.HLE.Debugger.Gdb
if (Debugger.DebugProcess.GetThread(threadId.Value) == null) if (Debugger.DebugProcess.GetThread(threadId.Value) == null)
{ {
ReplyError(); Processor.ReplyError();
return; return;
} }
@ -177,36 +178,36 @@ namespace Ryujinx.HLE.Debugger.Gdb
{ {
case 'c': case 'c':
Debugger.CThread = threadId; Debugger.CThread = threadId;
ReplyOK(); Processor.ReplyOK();
return; return;
case 'g': case 'g':
Debugger.GThread = threadId; Debugger.GThread = threadId;
ReplyOK(); Processor.ReplyOK();
return; return;
default: default:
ReplyError(); Processor.ReplyError();
return; return;
} }
} }
internal void CommandReadMemory(ulong addr, ulong len) internal void ReadMemory(ulong addr, ulong len)
{ {
try try
{ {
var data = new byte[len]; var data = new byte[len];
Debugger.DebugProcess.CpuMemory.Read(addr, data); Debugger.DebugProcess.CpuMemory.Read(addr, data);
Reply(Helpers.ToHex(data)); Processor.Reply(Helpers.ToHex(data));
} }
catch (InvalidMemoryRegionException) catch (InvalidMemoryRegionException)
{ {
// InvalidAccessHandler will show an error message, we log it again to tell user the error is from GDB (which can be ignored) // InvalidAccessHandler will show an error message, we log it again to tell user the error is from GDB (which can be ignored)
// TODO: Do not let InvalidAccessHandler show the error message // TODO: Do not let InvalidAccessHandler show the error message
Logger.Notice.Print(LogClass.GdbStub, $"GDB failed to read memory at 0x{addr:X16}"); Logger.Notice.Print(LogClass.GdbStub, $"GDB failed to read memory at 0x{addr:X16}");
ReplyError(); Processor.ReplyError();
} }
} }
internal void CommandWriteMemory(ulong addr, ulong len, StringStream ss) internal void WriteMemory(ulong addr, ulong len, StringStream ss)
{ {
try try
{ {
@ -218,92 +219,58 @@ namespace Ryujinx.HLE.Debugger.Gdb
Debugger.DebugProcess.CpuMemory.Write(addr, data); Debugger.DebugProcess.CpuMemory.Write(addr, data);
Debugger.DebugProcess.InvalidateCacheRegion(addr, len); Debugger.DebugProcess.InvalidateCacheRegion(addr, len);
ReplyOK(); Processor.ReplyOK();
} }
catch (InvalidMemoryRegionException) catch (InvalidMemoryRegionException)
{ {
ReplyError(); Processor.ReplyError();
} }
} }
internal void CommandReadRegister(int gdbRegId) internal void ReadRegister(int gdbRegId)
{ {
if (Debugger.GThread == null) if (Debugger.GThread == null)
{ {
ReplyError(); Processor.ReplyError();
return; return;
} }
var ctx = Debugger.DebugProcess.GetThread(Debugger.GThread.Value).Context; IExecutionContext ctx = Debugger.DebugProcess.GetThread(Debugger.GThread.Value).Context;
string result; string result = Debugger.IsProcess32Bit
if (Debugger.IsProcessAarch32) ? GdbRegisters.Read32(ctx, gdbRegId)
{ : GdbRegisters.Read64(ctx, gdbRegId);
result = GdbRegisters.Read32(ctx, gdbRegId);
if (result != null) Processor.Reply(result != null, result);
{
Reply(result);
}
else
{
ReplyError();
}
}
else
{
result = GdbRegisters.Read64(ctx, gdbRegId);
if (result != null)
{
Reply(result);
}
else
{
ReplyError();
}
}
} }
internal void CommandWriteRegister(int gdbRegId, StringStream ss) internal void WriteRegister(int gdbRegId, StringStream ss)
{ {
if (Debugger.GThread == null) if (Debugger.GThread == null)
{ {
ReplyError(); Processor.ReplyError();
return; return;
} }
var ctx = Debugger.DebugProcess.GetThread(Debugger.GThread.Value).Context; IExecutionContext ctx = Debugger.DebugProcess.GetThread(Debugger.GThread.Value).Context;
if (Debugger.IsProcessAarch32) if (Debugger.IsProcess32Bit)
{ {
if (GdbRegisters.Write32(ctx, gdbRegId, ss) && ss.IsEmpty()) Processor.Reply(GdbRegisters.Write32(ctx, gdbRegId, ss) && ss.IsEmpty());
{
ReplyOK();
}
else
{
ReplyError();
}
} }
else else
{ {
if (GdbRegisters.Write64(ctx, gdbRegId, ss) && ss.IsEmpty()) Processor.Reply(GdbRegisters.Write64(ctx, gdbRegId, ss) && ss.IsEmpty());
{
ReplyOK();
}
else
{
ReplyError();
}
} }
} }
internal void CommandStep(ulong? newPc) internal void Step(ulong? newPc)
{ {
if (Debugger.CThread == null) if (Debugger.CThread == null)
{ {
ReplyError(); Processor.ReplyError();
return; return;
} }
var thread = Debugger.DebugProcess.GetThread(Debugger.CThread.Value); KThread thread = Debugger.DebugProcess.GetThread(Debugger.CThread.Value);
if (newPc.HasValue) if (newPc.HasValue)
{ {
@ -312,24 +279,24 @@ namespace Ryujinx.HLE.Debugger.Gdb
if (!Debugger.DebugProcess.DebugStep(thread)) if (!Debugger.DebugProcess.DebugStep(thread))
{ {
ReplyError(); Processor.ReplyError();
} }
else else
{ {
Debugger.GThread = Debugger.CThread = thread.ThreadUid; Debugger.GThread = Debugger.CThread = thread.ThreadUid;
Reply($"T05thread:{thread.ThreadUid:x};"); Processor.Reply($"T05thread:{thread.ThreadUid:x};");
} }
} }
internal void CommandIsAlive(ulong? threadId) internal void IsAlive(ulong? threadId)
{ {
if (Debugger.GetThreads().Any(x => x.ThreadUid == threadId)) if (Debugger.GetThreads().Any(x => x.ThreadUid == threadId))
{ {
ReplyOK(); Processor.ReplyOK();
} }
else else
{ {
Reply("E00"); Processor.Reply("E00");
} }
} }
@ -341,14 +308,14 @@ namespace Ryujinx.HLE.Debugger.Gdb
Step Step
} }
record VContPendingAction(VContAction Action, ushort? Signal = null); record VContPendingAction(VContAction Action/*, ushort? Signal = null*/);
internal void HandleVContCommand(StringStream ss) internal void VCont(StringStream ss)
{ {
string[] rawActions = ss.ReadRemaining().Split(';', StringSplitOptions.RemoveEmptyEntries); string[] rawActions = ss.ReadRemaining().Split(';', StringSplitOptions.RemoveEmptyEntries);
var threadActionMap = new Dictionary<ulong, VContPendingAction>(); Dictionary<ulong, VContPendingAction> threadActionMap = new();
foreach (var thread in Debugger.GetThreads()) foreach (KThread thread in Debugger.GetThreads())
{ {
threadActionMap[thread.ThreadUid] = new VContPendingAction(VContAction.None); threadActionMap[thread.ThreadUid] = new VContPendingAction(VContAction.None);
} }
@ -358,8 +325,8 @@ namespace Ryujinx.HLE.Debugger.Gdb
// For each inferior thread, the *leftmost* action with a matching thread-id is applied. // For each inferior thread, the *leftmost* action with a matching thread-id is applied.
for (int i = rawActions.Length - 1; i >= 0; i--) for (int i = rawActions.Length - 1; i >= 0; i--)
{ {
var rawAction = rawActions[i]; string rawAction = rawActions[i];
var stream = new StringStream(rawAction); StringStream stream = new(rawAction);
char cmd = stream.ReadChar(); char cmd = stream.ReadChar();
VContAction action = cmd switch VContAction action = cmd switch
@ -371,10 +338,12 @@ namespace Ryujinx.HLE.Debugger.Gdb
}; };
// Note: We don't support signals yet. // Note: We don't support signals yet.
ushort? signal = null; //ushort? signal = null;
if (cmd is 'C' or 'S') if (cmd is 'C' or 'S')
{ {
signal = (ushort)stream.ReadLengthAsHex(2); /*signal = (ushort)*/stream.ReadLengthAsHex(2);
// we still call the read length method even if we have signals commented
// since that method advances the underlying string position
} }
ulong? threadId = null; ulong? threadId = null;
@ -387,14 +356,14 @@ namespace Ryujinx.HLE.Debugger.Gdb
{ {
if (threadActionMap.ContainsKey(threadId.Value)) if (threadActionMap.ContainsKey(threadId.Value))
{ {
threadActionMap[threadId.Value] = new VContPendingAction(action, signal); threadActionMap[threadId.Value] = new VContPendingAction(action/*, signal*/);
} }
} }
else else
{ {
foreach (var row in threadActionMap.ToList()) foreach (ulong thread in threadActionMap.Keys)
{ {
threadActionMap[row.Key] = new VContPendingAction(action, signal); threadActionMap[thread] = new VContPendingAction(action/*, signal*/);
} }
if (action == VContAction.Continue) if (action == VContAction.Continue)
@ -411,11 +380,11 @@ namespace Ryujinx.HLE.Debugger.Gdb
bool hasError = false; bool hasError = false;
foreach (var (threadUid, action) in threadActionMap) foreach ((ulong threadUid, VContPendingAction action) in threadActionMap)
{ {
if (action.Action == VContAction.Step) if (action.Action == VContAction.Step)
{ {
var thread = Debugger.DebugProcess.GetThread(threadUid); KThread thread = Debugger.DebugProcess.GetThread(threadUid);
if (!Debugger.DebugProcess.DebugStep(thread)) if (!Debugger.DebugProcess.DebugStep(thread))
{ {
hasError = true; hasError = true;
@ -432,7 +401,7 @@ namespace Ryujinx.HLE.Debugger.Gdb
} }
else if (defaultAction == VContAction.None) else if (defaultAction == VContAction.None)
{ {
foreach (var (threadUid, action) in threadActionMap) foreach ((ulong threadUid, VContPendingAction action) in threadActionMap)
{ {
if (action.Action == VContAction.Continue) if (action.Action == VContAction.Continue)
{ {
@ -441,26 +410,19 @@ namespace Ryujinx.HLE.Debugger.Gdb
} }
} }
if (hasError) Processor.Reply(!hasError);
{
ReplyError();
}
else
{
ReplyOK();
}
foreach (var (threadUid, action) in threadActionMap) foreach ((ulong threadUid, VContPendingAction action) in threadActionMap)
{ {
if (action.Action == VContAction.Step) if (action.Action == VContAction.Step)
{ {
Debugger.GThread = Debugger.CThread = threadUid; Debugger.GThread = Debugger.CThread = threadUid;
Reply($"T05thread:{threadUid:x};"); Processor.Reply($"T05thread:{threadUid:x};");
} }
} }
} }
internal void HandleQRcmdCommand(string hexCommand) internal void Q_Rcmd(string hexCommand)
{ {
try try
{ {
@ -477,12 +439,12 @@ namespace Ryujinx.HLE.Debugger.Gdb
_ => $"Unknown command: {command}\n" _ => $"Unknown command: {command}\n"
}; };
Reply(Helpers.ToHex(response)); Processor.Reply(Helpers.ToHex(response));
} }
catch (Exception e) catch (Exception e)
{ {
Logger.Error?.Print(LogClass.GdbStub, $"Error processing Rcmd: {e.Message}"); Logger.Error?.Print(LogClass.GdbStub, $"Error processing Rcmd: {e.Message}");
ReplyError(); Processor.ReplyError();
} }
} }
} }

View file

@ -43,7 +43,7 @@ namespace Ryujinx.HLE.Debugger
(byte)'$' => "}\x04", (byte)'$' => "}\x04",
(byte)'*' => "}\x0a", (byte)'*' => "}\x0a",
(byte)'}' => "}\x5d", (byte)'}' => "}\x5d",
_ => Convert.ToChar(x).ToString(), _ => Convert.ToChar(x).ToString()
} }
).JoinToString(string.Empty); ).JoinToString(string.Empty);
} }

View file

@ -18,7 +18,7 @@ namespace Ryujinx.HLE.Debugger
private static string GetEmbeddedResourceContent(string resourceName) private static string GetEmbeddedResourceContent(string resourceName)
{ {
Stream stream = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("Ryujinx.HLE.Debugger.Gdb.Xml." + resourceName); Stream stream = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("Ryujinx.HLE.Debugger.Gdb.Xml." + resourceName);
StreamReader reader = new StreamReader(stream); StreamReader reader = new(stream);
string result = reader.ReadToEnd(); string result = reader.ReadToEnd();
reader.Dispose(); reader.Dispose();
stream.Dispose(); stream.Dispose();

View file

@ -1092,7 +1092,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Process
MemoryManager = new KPageTable(KernelContext, CpuMemory, Context.AddressSpaceSize); MemoryManager = new KPageTable(KernelContext, CpuMemory, Context.AddressSpaceSize);
} }
private bool InvalidAccessHandler(ulong va) private static bool InvalidAccessHandler(ulong va)
{ {
KernelStatic.GetCurrentThread()?.PrintGuestStackTrace(); KernelStatic.GetCurrentThread()?.PrintGuestStackTrace();
KernelStatic.GetCurrentThread()?.PrintGuestRegisterPrintout(); KernelStatic.GetCurrentThread()?.PrintGuestRegisterPrintout();
@ -1104,7 +1104,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Process
return false; return false;
} }
private void UndefinedInstructionHandler(IExecutionContext context, ulong address, int opCode) private static void UndefinedInstructionHandler(IExecutionContext context, ulong address, int opCode)
{ {
KernelStatic.GetCurrentThread().PrintGuestStackTrace(); KernelStatic.GetCurrentThread().PrintGuestStackTrace();
KernelStatic.GetCurrentThread()?.PrintGuestRegisterPrintout(); KernelStatic.GetCurrentThread()?.PrintGuestRegisterPrintout();
@ -1208,16 +1208,16 @@ namespace Ryujinx.HLE.HOS.Kernel.Process
private class DebuggerInterface : IDebuggableProcess private class DebuggerInterface : IDebuggableProcess
{ {
private Barrier StepBarrier; private readonly Barrier _stepBarrier;
private readonly KProcess _parent; private readonly KProcess _parent;
private readonly KernelContext _kernelContext; private readonly KernelContext _kernelContext;
private KThread steppingThread; private KThread _steppingThread;
public DebuggerInterface(KProcess p) public DebuggerInterface(KProcess p)
{ {
_parent = p; _parent = p;
_kernelContext = p.KernelContext; _kernelContext = p.KernelContext;
StepBarrier = new(2); _stepBarrier = new(2);
} }
public void DebugStop() public void DebugStop()
@ -1285,7 +1285,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Process
} }
_kernelContext.CriticalSection.Enter(); _kernelContext.CriticalSection.Enter();
steppingThread = target; _steppingThread = target;
bool waiting = target.MutexOwner != null || target.WaitingSync || target.WaitingInArbitration; bool waiting = target.MutexOwner != null || target.WaitingSync || target.WaitingInArbitration;
target.Context.RequestDebugStep(); target.Context.RequestDebugStep();
if (waiting) if (waiting)
@ -1305,14 +1305,14 @@ namespace Ryujinx.HLE.HOS.Kernel.Process
_kernelContext.CriticalSection.Leave(); _kernelContext.CriticalSection.Leave();
bool stepTimedOut = false; bool stepTimedOut = false;
if (!StepBarrier.SignalAndWait(TimeSpan.FromMilliseconds(2000))) if (!_stepBarrier.SignalAndWait(TimeSpan.FromMilliseconds(2000)))
{ {
Logger.Warning?.Print(LogClass.Kernel, $"Failed to step thread {target.ThreadUid} in time."); Logger.Warning?.Print(LogClass.Kernel, $"Failed to step thread {target.ThreadUid} in time.");
stepTimedOut = true; stepTimedOut = true;
} }
_kernelContext.CriticalSection.Enter(); _kernelContext.CriticalSection.Enter();
steppingThread = null; _steppingThread = null;
if (waiting) if (waiting)
{ {
lock (_parent._threadingLock) lock (_parent._threadingLock)
@ -1334,7 +1334,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Process
return false; return false;
} }
StepBarrier.SignalAndWait(); _stepBarrier.SignalAndWait();
return true; return true;
} }
@ -1369,12 +1369,12 @@ namespace Ryujinx.HLE.HOS.Kernel.Process
public void DebugInterruptHandler(IExecutionContext ctx) public void DebugInterruptHandler(IExecutionContext ctx)
{ {
_kernelContext.CriticalSection.Enter(); _kernelContext.CriticalSection.Enter();
bool stepping = steppingThread != null; bool stepping = _steppingThread != null;
_kernelContext.CriticalSection.Leave(); _kernelContext.CriticalSection.Leave();
if (stepping) if (stepping)
{ {
StepBarrier.SignalAndWait(); _stepBarrier.SignalAndWait();
StepBarrier.SignalAndWait(); _stepBarrier.SignalAndWait();
} }
_parent.InterruptHandler(ctx); _parent.InterruptHandler(ctx);
} }