mirror of
https://git.ryujinx.app/ryubing/ryujinx.git
synced 2025-10-25 07:34:25 +00:00
gdb: Cleanup Debugger.cs
by moving the GDB command handlers and command processor out of the class and into their own
This commit is contained in:
File diff suppressed because it is too large
Load Diff
393
src/Ryujinx.HLE/Debugger/Gdb/CommandProcessor.cs
Normal file
393
src/Ryujinx.HLE/Debugger/Gdb/CommandProcessor.cs
Normal file
@@ -0,0 +1,393 @@
|
||||
using Ryujinx.Common;
|
||||
using Ryujinx.Common.Logging;
|
||||
using System.Linq;
|
||||
using System.Net.Sockets;
|
||||
using System.Text;
|
||||
|
||||
namespace Ryujinx.HLE.Debugger.Gdb
|
||||
{
|
||||
class GdbCommandProcessor
|
||||
{
|
||||
public readonly GdbCommands Commands;
|
||||
|
||||
public GdbCommandProcessor(TcpListener listenerSocket, Socket clientSocket, NetworkStream readStream, NetworkStream writeStream, Debugger debugger)
|
||||
{
|
||||
Commands = new GdbCommands(listenerSocket, clientSocket, readStream, writeStream, debugger);
|
||||
}
|
||||
|
||||
private string previousThreadListXml = "";
|
||||
|
||||
public void Process(string cmd)
|
||||
{
|
||||
StringStream ss = new(cmd);
|
||||
|
||||
switch (ss.ReadChar())
|
||||
{
|
||||
case '!':
|
||||
if (!ss.IsEmpty())
|
||||
{
|
||||
goto unknownCommand;
|
||||
}
|
||||
|
||||
// Enable extended mode
|
||||
Commands.ReplyOK();
|
||||
break;
|
||||
case '?':
|
||||
if (!ss.IsEmpty())
|
||||
{
|
||||
goto unknownCommand;
|
||||
}
|
||||
|
||||
Commands.CommandQuery();
|
||||
break;
|
||||
case 'c':
|
||||
Commands.CommandContinue(ss.IsEmpty() ? null : ss.ReadRemainingAsHex());
|
||||
break;
|
||||
case 'D':
|
||||
if (!ss.IsEmpty())
|
||||
{
|
||||
goto unknownCommand;
|
||||
}
|
||||
|
||||
Commands.CommandDetach();
|
||||
break;
|
||||
case 'g':
|
||||
if (!ss.IsEmpty())
|
||||
{
|
||||
goto unknownCommand;
|
||||
}
|
||||
|
||||
Commands.CommandReadRegisters();
|
||||
break;
|
||||
case 'G':
|
||||
Commands.CommandWriteRegisters(ss);
|
||||
break;
|
||||
case 'H':
|
||||
{
|
||||
char op = ss.ReadChar();
|
||||
ulong? threadId = ss.ReadRemainingAsThreadUid();
|
||||
Commands.CommandSetThread(op, threadId);
|
||||
break;
|
||||
}
|
||||
case 'k':
|
||||
Logger.Notice.Print(LogClass.GdbStub, "Kill request received, detach instead");
|
||||
Commands.Reply("");
|
||||
Commands.CommandDetach();
|
||||
break;
|
||||
case 'm':
|
||||
{
|
||||
ulong addr = ss.ReadUntilAsHex(',');
|
||||
ulong len = ss.ReadRemainingAsHex();
|
||||
Commands.CommandReadMemory(addr, len);
|
||||
break;
|
||||
}
|
||||
case 'M':
|
||||
{
|
||||
ulong addr = ss.ReadUntilAsHex(',');
|
||||
ulong len = ss.ReadUntilAsHex(':');
|
||||
Commands.CommandWriteMemory(addr, len, ss);
|
||||
break;
|
||||
}
|
||||
case 'p':
|
||||
{
|
||||
ulong gdbRegId = ss.ReadRemainingAsHex();
|
||||
Commands.CommandReadRegister((int)gdbRegId);
|
||||
break;
|
||||
}
|
||||
case 'P':
|
||||
{
|
||||
ulong gdbRegId = ss.ReadUntilAsHex('=');
|
||||
Commands.CommandWriteRegister((int)gdbRegId, ss);
|
||||
break;
|
||||
}
|
||||
case 'q':
|
||||
if (ss.ConsumeRemaining("GDBServerVersion"))
|
||||
{
|
||||
Commands.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")};");
|
||||
}
|
||||
|
||||
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;");
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
if (ss.ConsumePrefix("Supported:") || ss.ConsumeRemaining("Supported"))
|
||||
{
|
||||
Commands.Reply("PacketSize=10000;qXfer:features:read+;qXfer:threads:read+;vContSupported+");
|
||||
break;
|
||||
}
|
||||
|
||||
if (ss.ConsumePrefix("Rcmd,"))
|
||||
{
|
||||
string hexCommand = ss.ReadRemaining();
|
||||
Commands.HandleQRcmdCommand(hexCommand);
|
||||
break;
|
||||
}
|
||||
|
||||
if (ss.ConsumeRemaining("fThreadInfo"))
|
||||
{
|
||||
Commands. Reply($"m{string.Join(",", Commands.Debugger.DebugProcess.GetThreadUids().Select(x => $"{x:x}"))}");
|
||||
break;
|
||||
}
|
||||
|
||||
if (ss.ConsumeRemaining("sThreadInfo"))
|
||||
{
|
||||
Commands.Reply("l");
|
||||
break;
|
||||
}
|
||||
|
||||
if (ss.ConsumePrefix("ThreadExtraInfo,"))
|
||||
{
|
||||
ulong? threadId = ss.ReadRemainingAsThreadUid();
|
||||
if (threadId == null)
|
||||
{
|
||||
Commands.ReplyError();
|
||||
break;
|
||||
}
|
||||
|
||||
Commands.Reply(Helpers.ToHex(
|
||||
Commands.Debugger.DebugProcess.IsThreadPaused(
|
||||
Commands.Debugger.DebugProcess.GetThread(threadId.Value))
|
||||
? "Paused"
|
||||
: "Running"
|
||||
)
|
||||
);
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
if (ss.ConsumePrefix("Xfer:threads:read:"))
|
||||
{
|
||||
ss.ReadUntil(':');
|
||||
ulong offset = ss.ReadUntilAsHex(',');
|
||||
ulong len = ss.ReadRemainingAsHex();
|
||||
|
||||
var data = "";
|
||||
if (offset > 0)
|
||||
{
|
||||
data = previousThreadListXml;
|
||||
}
|
||||
else
|
||||
{
|
||||
previousThreadListXml = data = GetThreadListXml();
|
||||
}
|
||||
|
||||
if (offset >= (ulong)data.Length)
|
||||
{
|
||||
Commands.Reply("l");
|
||||
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 (ss.ConsumePrefix("Xfer:features:read:"))
|
||||
{
|
||||
string feature = ss.ReadUntil(':');
|
||||
ulong offset = ss.ReadUntilAsHex(',');
|
||||
ulong len = ss.ReadRemainingAsHex();
|
||||
|
||||
if (feature == "target.xml")
|
||||
{
|
||||
feature = Commands.Debugger.IsProcessAarch32 ? "target32.xml" : "target64.xml";
|
||||
}
|
||||
|
||||
string data;
|
||||
if (RegisterInformation.Features.TryGetValue(feature, out data))
|
||||
{
|
||||
if (offset >= (ulong)data.Length)
|
||||
{
|
||||
Commands.Reply("l");
|
||||
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;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Commands.Reply("E00"); // Invalid annex
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
goto unknownCommand;
|
||||
case 'Q':
|
||||
goto unknownCommand;
|
||||
case 's':
|
||||
Commands.CommandStep(ss.IsEmpty() ? null : ss.ReadRemainingAsHex());
|
||||
break;
|
||||
case 'T':
|
||||
{
|
||||
ulong? threadId = ss.ReadRemainingAsThreadUid();
|
||||
Commands.CommandIsAlive(threadId);
|
||||
break;
|
||||
}
|
||||
case 'v':
|
||||
if (ss.ConsumePrefix("Cont"))
|
||||
{
|
||||
if (ss.ConsumeRemaining("?"))
|
||||
{
|
||||
Commands.Reply("vCont;c;C;s;S");
|
||||
break;
|
||||
}
|
||||
|
||||
if (ss.ConsumePrefix(";"))
|
||||
{
|
||||
Commands.HandleVContCommand(ss);
|
||||
break;
|
||||
}
|
||||
|
||||
goto unknownCommand;
|
||||
}
|
||||
|
||||
if (ss.ConsumeRemaining("MustReplyEmpty"))
|
||||
{
|
||||
Commands.Reply("");
|
||||
break;
|
||||
}
|
||||
|
||||
goto unknownCommand;
|
||||
case 'Z':
|
||||
{
|
||||
string type = ss.ReadUntil(',');
|
||||
ulong addr = ss.ReadUntilAsHex(',');
|
||||
ulong len = ss.ReadLengthAsHex(1);
|
||||
string extra = ss.ReadRemaining();
|
||||
|
||||
if (extra.Length > 0)
|
||||
{
|
||||
Logger.Notice.Print(LogClass.GdbStub, $"Unsupported Z command extra data: {extra}");
|
||||
Commands.ReplyError();
|
||||
return;
|
||||
}
|
||||
|
||||
switch (type)
|
||||
{
|
||||
case "0": // Software breakpoint
|
||||
if (!Commands.Debugger.BreakpointManager.SetBreakPoint(addr, len, false))
|
||||
{
|
||||
Commands.ReplyError();
|
||||
return;
|
||||
}
|
||||
|
||||
Commands.ReplyOK();
|
||||
return;
|
||||
case "1": // Hardware breakpoint
|
||||
case "2": // Write watchpoint
|
||||
case "3": // Read watchpoint
|
||||
case "4": // Access watchpoint
|
||||
Commands.ReplyError();
|
||||
return;
|
||||
default:
|
||||
Commands. ReplyError();
|
||||
return;
|
||||
}
|
||||
}
|
||||
case 'z':
|
||||
{
|
||||
string type = ss.ReadUntil(',');
|
||||
ss.ConsumePrefix(",");
|
||||
ulong addr = ss.ReadUntilAsHex(',');
|
||||
ulong len = ss.ReadLengthAsHex(1);
|
||||
string extra = ss.ReadRemaining();
|
||||
|
||||
if (extra.Length > 0)
|
||||
{
|
||||
Logger.Notice.Print(LogClass.GdbStub, $"Unsupported z command extra data: {extra}");
|
||||
Commands.ReplyError();
|
||||
return;
|
||||
}
|
||||
|
||||
switch (type)
|
||||
{
|
||||
case "0": // Software breakpoint
|
||||
if (!Commands.Debugger.BreakpointManager.ClearBreakPoint(addr, len))
|
||||
{
|
||||
Commands.ReplyError();
|
||||
return;
|
||||
}
|
||||
|
||||
Commands.ReplyOK();
|
||||
return;
|
||||
case "1": // Hardware breakpoint
|
||||
case "2": // Write watchpoint
|
||||
case "3": // Read watchpoint
|
||||
case "4": // Access watchpoint
|
||||
Commands.ReplyError();
|
||||
return;
|
||||
default:
|
||||
Commands.ReplyError();
|
||||
return;
|
||||
}
|
||||
}
|
||||
default:
|
||||
unknownCommand:
|
||||
Logger.Notice.Print(LogClass.GdbStub, $"Unknown command: {cmd}");
|
||||
Commands.Reply("");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private string GetThreadListXml()
|
||||
{
|
||||
var sb = new StringBuilder();
|
||||
sb.Append("<?xml version=\"1.0\"?><threads>\n");
|
||||
|
||||
foreach (var thread in Commands.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");
|
||||
}
|
||||
|
||||
sb.Append("</threads>");
|
||||
return sb.ToString();
|
||||
}
|
||||
}
|
||||
}
|
||||
489
src/Ryujinx.HLE/Debugger/Gdb/Commands.cs
Normal file
489
src/Ryujinx.HLE/Debugger/Gdb/Commands.cs
Normal file
@@ -0,0 +1,489 @@
|
||||
using Ryujinx.Common.Logging;
|
||||
using Ryujinx.Memory;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Net.Sockets;
|
||||
using System.Text;
|
||||
|
||||
namespace Ryujinx.HLE.Debugger.Gdb
|
||||
{
|
||||
class GdbCommands
|
||||
{
|
||||
const int GdbRegisterCount64 = 68;
|
||||
const int GdbRegisterCount32 = 66;
|
||||
|
||||
public readonly Debugger Debugger;
|
||||
|
||||
private readonly TcpListener _listenerSocket;
|
||||
private readonly Socket _clientSocket;
|
||||
private readonly NetworkStream _readStream;
|
||||
private readonly NetworkStream _writeStream;
|
||||
|
||||
|
||||
public GdbCommands(TcpListener listenerSocket, Socket clientSocket, NetworkStream readStream,
|
||||
NetworkStream writeStream, Debugger debugger)
|
||||
{
|
||||
_listenerSocket = listenerSocket;
|
||||
_clientSocket = clientSocket;
|
||||
_readStream = readStream;
|
||||
_writeStream = writeStream;
|
||||
Debugger = debugger;
|
||||
}
|
||||
|
||||
public void Reply(string cmd)
|
||||
{
|
||||
Logger.Debug?.Print(LogClass.GdbStub, $"Reply: {cmd}");
|
||||
_writeStream.Write(Encoding.ASCII.GetBytes($"${cmd}#{Helpers.CalculateChecksum(cmd):x2}"));
|
||||
}
|
||||
|
||||
public void ReplyOK() => Reply("OK");
|
||||
|
||||
public void ReplyError() => Reply("E01");
|
||||
|
||||
internal void CommandQuery()
|
||||
{
|
||||
// GDB is performing initial contact. Stop everything.
|
||||
Debugger.DebugProcess.DebugStop();
|
||||
Debugger.GThread = Debugger.CThread = Debugger.DebugProcess.GetThreadUids().First();
|
||||
Reply($"T05thread:{Debugger.CThread:x};");
|
||||
}
|
||||
|
||||
internal void CommandInterrupt()
|
||||
{
|
||||
// GDB is requesting an interrupt. Stop everything.
|
||||
Debugger.DebugProcess.DebugStop();
|
||||
if (Debugger.GThread == null || Debugger.GetThreads().All(x => x.ThreadUid != Debugger.GThread.Value))
|
||||
{
|
||||
Debugger.GThread = Debugger.CThread = Debugger.DebugProcess.GetThreadUids().First();
|
||||
}
|
||||
|
||||
Reply($"T02thread:{Debugger.GThread:x};");
|
||||
}
|
||||
|
||||
internal void CommandContinue(ulong? newPc)
|
||||
{
|
||||
if (newPc.HasValue)
|
||||
{
|
||||
if (Debugger.CThread == null)
|
||||
{
|
||||
ReplyError();
|
||||
return;
|
||||
}
|
||||
|
||||
Debugger.DebugProcess.GetThread(Debugger.CThread.Value).Context.DebugPc = newPc.Value;
|
||||
}
|
||||
|
||||
Debugger.DebugProcess.DebugContinue();
|
||||
}
|
||||
|
||||
internal void CommandDetach()
|
||||
{
|
||||
Debugger.BreakpointManager.ClearAll();
|
||||
CommandContinue(null);
|
||||
}
|
||||
|
||||
internal void CommandReadRegisters()
|
||||
{
|
||||
if (Debugger.GThread == null)
|
||||
{
|
||||
ReplyError();
|
||||
return;
|
||||
}
|
||||
|
||||
var ctx = Debugger.DebugProcess.GetThread(Debugger.GThread.Value).Context;
|
||||
string registers = "";
|
||||
if (Debugger.IsProcessAarch32)
|
||||
{
|
||||
for (int i = 0; i < GdbRegisterCount32; i++)
|
||||
{
|
||||
registers += GdbRegisters.Read32(ctx, i);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
for (int i = 0; i < GdbRegisterCount64; i++)
|
||||
{
|
||||
registers += GdbRegisters.Read64(ctx, i);
|
||||
}
|
||||
}
|
||||
|
||||
Reply(registers);
|
||||
}
|
||||
|
||||
internal void CommandWriteRegisters(StringStream ss)
|
||||
{
|
||||
if (Debugger.GThread == null)
|
||||
{
|
||||
ReplyError();
|
||||
return;
|
||||
}
|
||||
|
||||
var ctx = Debugger.DebugProcess.GetThread(Debugger.GThread.Value).Context;
|
||||
if (Debugger.IsProcessAarch32)
|
||||
{
|
||||
for (int i = 0; i < GdbRegisterCount32; i++)
|
||||
{
|
||||
if (!GdbRegisters.Write32(ctx, i, ss))
|
||||
{
|
||||
ReplyError();
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
for (int i = 0; i < GdbRegisterCount64; i++)
|
||||
{
|
||||
if (!GdbRegisters.Write64(ctx, i, ss))
|
||||
{
|
||||
ReplyError();
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (ss.IsEmpty())
|
||||
{
|
||||
ReplyOK();
|
||||
}
|
||||
else
|
||||
{
|
||||
ReplyError();
|
||||
}
|
||||
}
|
||||
|
||||
internal void CommandSetThread(char op, ulong? threadId)
|
||||
{
|
||||
if (threadId is 0 or null)
|
||||
{
|
||||
var threads = Debugger.GetThreads();
|
||||
if (threads.Length == 0)
|
||||
{
|
||||
ReplyError();
|
||||
return;
|
||||
}
|
||||
|
||||
threadId = threads.First().ThreadUid;
|
||||
}
|
||||
|
||||
if (Debugger.DebugProcess.GetThread(threadId.Value) == null)
|
||||
{
|
||||
ReplyError();
|
||||
return;
|
||||
}
|
||||
|
||||
switch (op)
|
||||
{
|
||||
case 'c':
|
||||
Debugger.CThread = threadId;
|
||||
ReplyOK();
|
||||
return;
|
||||
case 'g':
|
||||
Debugger.GThread = threadId;
|
||||
ReplyOK();
|
||||
return;
|
||||
default:
|
||||
ReplyError();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
internal void CommandReadMemory(ulong addr, ulong len)
|
||||
{
|
||||
try
|
||||
{
|
||||
var data = new byte[len];
|
||||
Debugger.DebugProcess.CpuMemory.Read(addr, data);
|
||||
Reply(Helpers.ToHex(data));
|
||||
}
|
||||
catch (InvalidMemoryRegionException)
|
||||
{
|
||||
// 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
|
||||
Logger.Notice.Print(LogClass.GdbStub, $"GDB failed to read memory at 0x{addr:X16}");
|
||||
ReplyError();
|
||||
}
|
||||
}
|
||||
|
||||
internal void CommandWriteMemory(ulong addr, ulong len, StringStream ss)
|
||||
{
|
||||
try
|
||||
{
|
||||
var data = new byte[len];
|
||||
for (ulong i = 0; i < len; i++)
|
||||
{
|
||||
data[i] = (byte)ss.ReadLengthAsHex(2);
|
||||
}
|
||||
|
||||
Debugger.DebugProcess.CpuMemory.Write(addr, data);
|
||||
Debugger.DebugProcess.InvalidateCacheRegion(addr, len);
|
||||
ReplyOK();
|
||||
}
|
||||
catch (InvalidMemoryRegionException)
|
||||
{
|
||||
ReplyError();
|
||||
}
|
||||
}
|
||||
|
||||
internal void CommandReadRegister(int gdbRegId)
|
||||
{
|
||||
if (Debugger.GThread == null)
|
||||
{
|
||||
ReplyError();
|
||||
return;
|
||||
}
|
||||
|
||||
var ctx = Debugger.DebugProcess.GetThread(Debugger.GThread.Value).Context;
|
||||
string result;
|
||||
if (Debugger.IsProcessAarch32)
|
||||
{
|
||||
result = GdbRegisters.Read32(ctx, gdbRegId);
|
||||
if (result != null)
|
||||
{
|
||||
Reply(result);
|
||||
}
|
||||
else
|
||||
{
|
||||
ReplyError();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
result = GdbRegisters.Read64(ctx, gdbRegId);
|
||||
if (result != null)
|
||||
{
|
||||
Reply(result);
|
||||
}
|
||||
else
|
||||
{
|
||||
ReplyError();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal void CommandWriteRegister(int gdbRegId, StringStream ss)
|
||||
{
|
||||
if (Debugger.GThread == null)
|
||||
{
|
||||
ReplyError();
|
||||
return;
|
||||
}
|
||||
|
||||
var ctx = Debugger.DebugProcess.GetThread(Debugger.GThread.Value).Context;
|
||||
if (Debugger.IsProcessAarch32)
|
||||
{
|
||||
if (GdbRegisters.Write32(ctx, gdbRegId, ss) && ss.IsEmpty())
|
||||
{
|
||||
ReplyOK();
|
||||
}
|
||||
else
|
||||
{
|
||||
ReplyError();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (GdbRegisters.Write64(ctx, gdbRegId, ss) && ss.IsEmpty())
|
||||
{
|
||||
ReplyOK();
|
||||
}
|
||||
else
|
||||
{
|
||||
ReplyError();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal void CommandStep(ulong? newPc)
|
||||
{
|
||||
if (Debugger.CThread == null)
|
||||
{
|
||||
ReplyError();
|
||||
return;
|
||||
}
|
||||
|
||||
var thread = Debugger.DebugProcess.GetThread(Debugger.CThread.Value);
|
||||
|
||||
if (newPc.HasValue)
|
||||
{
|
||||
thread.Context.DebugPc = newPc.Value;
|
||||
}
|
||||
|
||||
if (!Debugger.DebugProcess.DebugStep(thread))
|
||||
{
|
||||
ReplyError();
|
||||
}
|
||||
else
|
||||
{
|
||||
Debugger.GThread = Debugger.CThread = thread.ThreadUid;
|
||||
Reply($"T05thread:{thread.ThreadUid:x};");
|
||||
}
|
||||
}
|
||||
|
||||
internal void CommandIsAlive(ulong? threadId)
|
||||
{
|
||||
if (Debugger.GetThreads().Any(x => x.ThreadUid == threadId))
|
||||
{
|
||||
ReplyOK();
|
||||
}
|
||||
else
|
||||
{
|
||||
Reply("E00");
|
||||
}
|
||||
}
|
||||
|
||||
enum VContAction
|
||||
{
|
||||
None,
|
||||
Continue,
|
||||
Stop,
|
||||
Step
|
||||
}
|
||||
|
||||
record VContPendingAction(VContAction Action, ushort? Signal = null);
|
||||
|
||||
internal void HandleVContCommand(StringStream ss)
|
||||
{
|
||||
string[] rawActions = ss.ReadRemaining().Split(';', StringSplitOptions.RemoveEmptyEntries);
|
||||
|
||||
var threadActionMap = new Dictionary<ulong, VContPendingAction>();
|
||||
foreach (var thread in Debugger.GetThreads())
|
||||
{
|
||||
threadActionMap[thread.ThreadUid] = new VContPendingAction(VContAction.None);
|
||||
}
|
||||
|
||||
VContAction defaultAction = VContAction.None;
|
||||
|
||||
// For each inferior thread, the *leftmost* action with a matching thread-id is applied.
|
||||
for (int i = rawActions.Length - 1; i >= 0; i--)
|
||||
{
|
||||
var rawAction = rawActions[i];
|
||||
var stream = new StringStream(rawAction);
|
||||
|
||||
char cmd = stream.ReadChar();
|
||||
VContAction action = cmd switch
|
||||
{
|
||||
'c' or 'C' => VContAction.Continue,
|
||||
's' or 'S' => VContAction.Step,
|
||||
't' => VContAction.Stop,
|
||||
_ => VContAction.None
|
||||
};
|
||||
|
||||
// Note: We don't support signals yet.
|
||||
ushort? signal = null;
|
||||
if (cmd is 'C' or 'S')
|
||||
{
|
||||
signal = (ushort)stream.ReadLengthAsHex(2);
|
||||
}
|
||||
|
||||
ulong? threadId = null;
|
||||
if (stream.ConsumePrefix(":"))
|
||||
{
|
||||
threadId = stream.ReadRemainingAsThreadUid();
|
||||
}
|
||||
|
||||
if (threadId.HasValue)
|
||||
{
|
||||
if (threadActionMap.ContainsKey(threadId.Value))
|
||||
{
|
||||
threadActionMap[threadId.Value] = new VContPendingAction(action, signal);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
foreach (var row in threadActionMap.ToList())
|
||||
{
|
||||
threadActionMap[row.Key] = new VContPendingAction(action, signal);
|
||||
}
|
||||
|
||||
if (action == VContAction.Continue)
|
||||
{
|
||||
defaultAction = action;
|
||||
}
|
||||
else
|
||||
{
|
||||
Logger.Warning?.Print(LogClass.GdbStub,
|
||||
$"Received vCont command with unsupported default action: {rawAction}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool hasError = false;
|
||||
|
||||
foreach (var (threadUid, action) in threadActionMap)
|
||||
{
|
||||
if (action.Action == VContAction.Step)
|
||||
{
|
||||
var thread = Debugger.DebugProcess.GetThread(threadUid);
|
||||
if (!Debugger.DebugProcess.DebugStep(thread))
|
||||
{
|
||||
hasError = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// If we receive "vCont;c", just continue the process.
|
||||
// If we receive something like "vCont;c:2e;c:2f" (IDA Pro will send commands like this), continue these threads.
|
||||
// For "vCont;s:2f;c", `DebugProcess.DebugStep()` will continue and suspend other threads if needed, so we don't do anything here.
|
||||
if (threadActionMap.Values.All(a => a.Action == VContAction.Continue))
|
||||
{
|
||||
Debugger.DebugProcess.DebugContinue();
|
||||
}
|
||||
else if (defaultAction == VContAction.None)
|
||||
{
|
||||
foreach (var (threadUid, action) in threadActionMap)
|
||||
{
|
||||
if (action.Action == VContAction.Continue)
|
||||
{
|
||||
Debugger.DebugProcess.DebugContinue(Debugger.DebugProcess.GetThread(threadUid));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (hasError)
|
||||
{
|
||||
ReplyError();
|
||||
}
|
||||
else
|
||||
{
|
||||
ReplyOK();
|
||||
}
|
||||
|
||||
foreach (var (threadUid, action) in threadActionMap)
|
||||
{
|
||||
if (action.Action == VContAction.Step)
|
||||
{
|
||||
Debugger.GThread = Debugger.CThread = threadUid;
|
||||
Reply($"T05thread:{threadUid:x};");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal void HandleQRcmdCommand(string hexCommand)
|
||||
{
|
||||
try
|
||||
{
|
||||
string command = Helpers.FromHex(hexCommand);
|
||||
Logger.Debug?.Print(LogClass.GdbStub, $"Received Rcmd: {command}");
|
||||
|
||||
string response = command.Trim().ToLowerInvariant() switch
|
||||
{
|
||||
"help" => "backtrace\nbt\nregisters\nreg\nget info\nminidump\n",
|
||||
"get info" => Debugger.GetProcessInfo(),
|
||||
"backtrace" or "bt" => Debugger.GetStackTrace(),
|
||||
"registers" or "reg" => Debugger.GetRegisters(),
|
||||
"minidump" => Debugger.GetMinidump(),
|
||||
_ => $"Unknown command: {command}\n"
|
||||
};
|
||||
|
||||
Reply(Helpers.ToHex(response));
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Logger.Error?.Print(LogClass.GdbStub, $"Error processing Rcmd: {e.Message}");
|
||||
ReplyError();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
160
src/Ryujinx.HLE/Debugger/Gdb/Registers.cs
Normal file
160
src/Ryujinx.HLE/Debugger/Gdb/Registers.cs
Normal file
@@ -0,0 +1,160 @@
|
||||
using ARMeilleure.State;
|
||||
using Ryujinx.Cpu;
|
||||
using System;
|
||||
|
||||
namespace Ryujinx.HLE.Debugger.Gdb
|
||||
{
|
||||
static class GdbRegisters
|
||||
{
|
||||
/*
|
||||
FPCR = FPSR & ~FpcrMask
|
||||
All of FPCR's bits are reserved in FPCR and vice versa,
|
||||
see ARM's documentation.
|
||||
*/
|
||||
private const uint FpcrMask = 0xfc1fffff;
|
||||
|
||||
public static string Read64(IExecutionContext state, int gdbRegId)
|
||||
{
|
||||
switch (gdbRegId)
|
||||
{
|
||||
case >= 0 and <= 31:
|
||||
return Helpers.ToHex(BitConverter.GetBytes(state.GetX(gdbRegId)));
|
||||
case 32:
|
||||
return Helpers.ToHex(BitConverter.GetBytes(state.DebugPc));
|
||||
case 33:
|
||||
return Helpers.ToHex(BitConverter.GetBytes(state.Pstate));
|
||||
case >= 34 and <= 65:
|
||||
return Helpers.ToHex(state.GetV(gdbRegId - 34).ToArray());
|
||||
case 66:
|
||||
return Helpers.ToHex(BitConverter.GetBytes((uint)state.Fpsr));
|
||||
case 67:
|
||||
return Helpers.ToHex(BitConverter.GetBytes((uint)state.Fpcr));
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static bool Write64(IExecutionContext state, int gdbRegId, StringStream ss)
|
||||
{
|
||||
switch (gdbRegId)
|
||||
{
|
||||
case >= 0 and <= 31:
|
||||
{
|
||||
ulong value = ss.ReadLengthAsLEHex(16);
|
||||
state.SetX(gdbRegId, value);
|
||||
return true;
|
||||
}
|
||||
case 32:
|
||||
{
|
||||
ulong value = ss.ReadLengthAsLEHex(16);
|
||||
state.DebugPc = value;
|
||||
return true;
|
||||
}
|
||||
case 33:
|
||||
{
|
||||
ulong value = ss.ReadLengthAsLEHex(8);
|
||||
state.Pstate = (uint)value;
|
||||
return true;
|
||||
}
|
||||
case >= 34 and <= 65:
|
||||
{
|
||||
ulong value0 = ss.ReadLengthAsLEHex(16);
|
||||
ulong value1 = ss.ReadLengthAsLEHex(16);
|
||||
state.SetV(gdbRegId - 34, new V128(value0, value1));
|
||||
return true;
|
||||
}
|
||||
case 66:
|
||||
{
|
||||
ulong value = ss.ReadLengthAsLEHex(8);
|
||||
state.Fpsr = (uint)value;
|
||||
return true;
|
||||
}
|
||||
case 67:
|
||||
{
|
||||
ulong value = ss.ReadLengthAsLEHex(8);
|
||||
state.Fpcr = (uint)value;
|
||||
return true;
|
||||
}
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public static string Read32(IExecutionContext state, int gdbRegId)
|
||||
{
|
||||
switch (gdbRegId)
|
||||
{
|
||||
case >= 0 and <= 14:
|
||||
return Helpers.ToHex(BitConverter.GetBytes((uint)state.GetX(gdbRegId)));
|
||||
case 15:
|
||||
return Helpers.ToHex(BitConverter.GetBytes((uint)state.DebugPc));
|
||||
case 16:
|
||||
return Helpers.ToHex(BitConverter.GetBytes((uint)state.Pstate));
|
||||
case >= 17 and <= 32:
|
||||
return Helpers.ToHex(state.GetV(gdbRegId - 17).ToArray());
|
||||
case >= 33 and <= 64:
|
||||
int reg = (gdbRegId - 33);
|
||||
int n = reg / 2;
|
||||
int shift = reg % 2;
|
||||
ulong value = state.GetV(n).Extract<ulong>(shift);
|
||||
return Helpers.ToHex(BitConverter.GetBytes(value));
|
||||
case 65:
|
||||
uint fpscr = (uint)state.Fpsr | (uint)state.Fpcr;
|
||||
return Helpers.ToHex(BitConverter.GetBytes(fpscr));
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static bool Write32(IExecutionContext state, int gdbRegId, StringStream ss)
|
||||
{
|
||||
switch (gdbRegId)
|
||||
{
|
||||
case >= 0 and <= 14:
|
||||
{
|
||||
ulong value = ss.ReadLengthAsLEHex(8);
|
||||
state.SetX(gdbRegId, value);
|
||||
return true;
|
||||
}
|
||||
case 15:
|
||||
{
|
||||
ulong value = ss.ReadLengthAsLEHex(8);
|
||||
state.DebugPc = value;
|
||||
return true;
|
||||
}
|
||||
case 16:
|
||||
{
|
||||
ulong value = ss.ReadLengthAsLEHex(8);
|
||||
state.Pstate = (uint)value;
|
||||
return true;
|
||||
}
|
||||
case >= 17 and <= 32:
|
||||
{
|
||||
ulong value0 = ss.ReadLengthAsLEHex(16);
|
||||
ulong value1 = ss.ReadLengthAsLEHex(16);
|
||||
state.SetV(gdbRegId - 17, new V128(value0, value1));
|
||||
return true;
|
||||
}
|
||||
case >= 33 and <= 64:
|
||||
{
|
||||
ulong value = ss.ReadLengthAsLEHex(16);
|
||||
int regId = (gdbRegId - 33);
|
||||
int regNum = regId / 2;
|
||||
int shift = regId % 2;
|
||||
V128 reg = state.GetV(regNum);
|
||||
reg.Insert(shift, value);
|
||||
return true;
|
||||
}
|
||||
case 65:
|
||||
{
|
||||
ulong value = ss.ReadLengthAsLEHex(8);
|
||||
state.Fpsr = (uint)value & FpcrMask;
|
||||
state.Fpcr = (uint)value & ~FpcrMask;
|
||||
return true;
|
||||
}
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
50
src/Ryujinx.HLE/Debugger/Helpers.cs
Normal file
50
src/Ryujinx.HLE/Debugger/Helpers.cs
Normal file
@@ -0,0 +1,50 @@
|
||||
using Gommon;
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace Ryujinx.HLE.Debugger
|
||||
{
|
||||
public static class Helpers
|
||||
{
|
||||
public static byte CalculateChecksum(string cmd)
|
||||
{
|
||||
byte checksum = 0;
|
||||
foreach (char x in cmd)
|
||||
{
|
||||
unchecked
|
||||
{
|
||||
checksum += (byte)x;
|
||||
}
|
||||
}
|
||||
|
||||
return checksum;
|
||||
}
|
||||
|
||||
public static string FromHex(string hexString)
|
||||
{
|
||||
if (string.IsNullOrEmpty(hexString))
|
||||
return string.Empty;
|
||||
|
||||
byte[] bytes = Convert.FromHexString(hexString);
|
||||
return Encoding.ASCII.GetString(bytes);
|
||||
}
|
||||
|
||||
public static string ToHex(byte[] bytes) => string.Join("", bytes.Select(x => $"{x:x2}"));
|
||||
|
||||
public static string ToHex(string str) => ToHex(Encoding.ASCII.GetBytes(str));
|
||||
|
||||
public static string ToBinaryFormat(string str) => ToBinaryFormat(Encoding.ASCII.GetBytes(str));
|
||||
public static string ToBinaryFormat(byte[] bytes) =>
|
||||
bytes.Select(x =>
|
||||
x switch
|
||||
{
|
||||
(byte)'#' => "}\x03",
|
||||
(byte)'$' => "}\x04",
|
||||
(byte)'*' => "}\x0a",
|
||||
(byte)'}' => "}\x5d",
|
||||
_ => Convert.ToChar(x).ToString(),
|
||||
}
|
||||
).JoinToString(string.Empty);
|
||||
}
|
||||
}
|
||||
@@ -3,7 +3,7 @@ using System.Globalization;
|
||||
|
||||
namespace Ryujinx.HLE.Debugger
|
||||
{
|
||||
class StringStream
|
||||
internal class StringStream
|
||||
{
|
||||
private readonly string Data;
|
||||
private int Position;
|
||||
|
||||
@@ -33,12 +33,12 @@
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<None Remove="Debugger\GdbXml\aarch64-core.xml" />
|
||||
<None Remove="Debugger\GdbXml\aarch64-fpu.xml" />
|
||||
<None Remove="Debugger\GdbXml\arm-core.xml" />
|
||||
<None Remove="Debugger\GdbXml\arm-neon.xml" />
|
||||
<None Remove="Debugger\GdbXml\target64.xml" />
|
||||
<None Remove="Debugger\GdbXml\target32.xml" />
|
||||
<None Remove="Debugger\Gdb\Xml\aarch64-core.xml" />
|
||||
<None Remove="Debugger\Gdb\Xml\aarch64-fpu.xml" />
|
||||
<None Remove="Debugger\Gdb\Xml\arm-core.xml" />
|
||||
<None Remove="Debugger\Gdb\Xml\arm-neon.xml" />
|
||||
<None Remove="Debugger\Gdb\Xml\target64.xml" />
|
||||
<None Remove="Debugger\Gdb\Xml\target32.xml" />
|
||||
<None Remove="Homebrew.npdm" />
|
||||
<None Remove="HOS\Applets\SoftwareKeyboard\Resources\Logo_Ryujinx.png" />
|
||||
<None Remove="HOS\Applets\SoftwareKeyboard\Resources\Icon_BtnA.png" />
|
||||
@@ -48,12 +48,12 @@
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<EmbeddedResource Include="Debugger\GdbXml\aarch64-core.xml" />
|
||||
<EmbeddedResource Include="Debugger\GdbXml\aarch64-fpu.xml" />
|
||||
<EmbeddedResource Include="Debugger\GdbXml\arm-core.xml" />
|
||||
<EmbeddedResource Include="Debugger\GdbXml\arm-neon.xml" />
|
||||
<EmbeddedResource Include="Debugger\GdbXml\target64.xml" />
|
||||
<EmbeddedResource Include="Debugger\GdbXml\target32.xml" />
|
||||
<EmbeddedResource Include="Debugger\Gdb\Xml\aarch64-core.xml" />
|
||||
<EmbeddedResource Include="Debugger\Gdb\Xml\aarch64-fpu.xml" />
|
||||
<EmbeddedResource Include="Debugger\Gdb\Xml\arm-core.xml" />
|
||||
<EmbeddedResource Include="Debugger\Gdb\Xml\arm-neon.xml" />
|
||||
<EmbeddedResource Include="Debugger\Gdb\Xml\target32.xml" />
|
||||
<EmbeddedResource Include="Debugger\Gdb\Xml\target64.xml" />
|
||||
<EmbeddedResource Include="Homebrew.npdm" />
|
||||
<EmbeddedResource Include="HOS\Applets\SoftwareKeyboard\Resources\Logo_Ryujinx.png" />
|
||||
<EmbeddedResource Include="HOS\Applets\SoftwareKeyboard\Resources\Icon_BtnA.png" />
|
||||
|
||||
Reference in New Issue
Block a user