R/Ryujinx.HLE/Exceptions/ServiceNotImplementedException.cs
Mary 0746b83edf
Initial support for the new 12.x IPC system (#2182)
* Rename CommandAttribute as CommandHIpcAttribute to prepare for 12.x changes

* Implement inital support for TIPC and adds SM command ids

* *Ipc to *ipc

* Missed a ref in last commit...

* CommandAttributeTIpc to CommandAttributeTipc

* Addresses comment and fixes some bugs around

TIPC doesn't have any padding requirements as buffer C isn't a thing
Fix for RegisterService inverting two argument only on TIPC
2021-04-14 00:01:24 +02:00

174 lines
5.7 KiB
C#

using Ryujinx.Common;
using Ryujinx.HLE.HOS;
using Ryujinx.HLE.HOS.Ipc;
using Ryujinx.HLE.HOS.Services;
using System;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.Serialization;
using System.Text;
namespace Ryujinx.HLE.Exceptions
{
[Serializable]
internal class ServiceNotImplementedException : Exception
{
public IpcService Service { get; }
public ServiceCtx Context { get; }
public IpcMessage Request { get; }
private bool _isTipcCommand;
public ServiceNotImplementedException(IpcService service, ServiceCtx context, bool isTipcCommand)
: this(service, context, "The service call is not implemented.", isTipcCommand)
{ }
public ServiceNotImplementedException(IpcService service, ServiceCtx context, string message, bool isTipcCommand)
: base(message)
{
Service = service;
Context = context;
Request = context.Request;
_isTipcCommand = isTipcCommand;
}
public ServiceNotImplementedException(IpcService service, ServiceCtx context, string message, Exception inner, bool isTipcCommand)
: base(message, inner)
{
Service = service;
Context = context;
Request = context.Request;
_isTipcCommand = isTipcCommand;
}
protected ServiceNotImplementedException(SerializationInfo info, StreamingContext context)
: base(info, context)
{ }
public override string Message
{
get
{
return base.Message +
Environment.NewLine +
Environment.NewLine +
BuildMessage();
}
}
private string BuildMessage()
{
StringBuilder sb = new StringBuilder();
// Print the IPC command details (service name, command ID, and handler)
(Type callingType, MethodBase callingMethod) = WalkStackTrace(new StackTrace(this));
if (callingType != null && callingMethod != null)
{
var ipcCommands = _isTipcCommand ? Service.TipcCommands : Service.HipcCommands;
// Find the handler for the method called
var ipcHandler = ipcCommands.FirstOrDefault(x => x.Value == callingMethod);
var ipcCommandId = ipcHandler.Key;
var ipcMethod = ipcHandler.Value;
if (ipcMethod != null)
{
sb.AppendLine($"Service Command: {Service.GetType().FullName}: {ipcCommandId} ({ipcMethod.Name})");
sb.AppendLine();
}
}
sb.AppendLine("Guest Stack Trace:");
sb.AppendLine(Context.Thread.GetGuestStackTrace());
// Print buffer information
if (Request.PtrBuff.Count > 0 ||
Request.SendBuff.Count > 0 ||
Request.ReceiveBuff.Count > 0 ||
Request.ExchangeBuff.Count > 0 ||
Request.RecvListBuff.Count > 0)
{
sb.AppendLine("Buffer Information:");
if (Request.PtrBuff.Count > 0)
{
sb.AppendLine("\tPtrBuff:");
foreach (var buff in Request.PtrBuff)
{
sb.AppendLine($"\t[{buff.Index}] Position: 0x{buff.Position:x16} Size: 0x{buff.Size:x16}");
}
}
if (Request.SendBuff.Count > 0)
{
sb.AppendLine("\tSendBuff:");
foreach (var buff in Request.SendBuff)
{
sb.AppendLine($"\tPosition: 0x{buff.Position:x16} Size: 0x{buff.Size:x16} Flags: {buff.Flags}");
}
}
if (Request.ReceiveBuff.Count > 0)
{
sb.AppendLine("\tReceiveBuff:");
foreach (var buff in Request.ReceiveBuff)
{
sb.AppendLine($"\tPosition: 0x{buff.Position:x16} Size: 0x{buff.Size:x16} Flags: {buff.Flags}");
}
}
if (Request.ExchangeBuff.Count > 0)
{
sb.AppendLine("\tExchangeBuff:");
foreach (var buff in Request.ExchangeBuff)
{
sb.AppendLine($"\tPosition: 0x{buff.Position:x16} Size: 0x{buff.Size:x16} Flags: {buff.Flags}");
}
}
if (Request.RecvListBuff.Count > 0)
{
sb.AppendLine("\tRecvListBuff:");
foreach (var buff in Request.RecvListBuff)
{
sb.AppendLine($"\tPosition: 0x{buff.Position:x16} Size: 0x{buff.Size:x16}");
}
}
sb.AppendLine();
}
sb.AppendLine("Raw Request Data:");
sb.Append(HexUtils.HexTable(Request.RawData));
return sb.ToString();
}
private (Type, MethodBase) WalkStackTrace(StackTrace trace)
{
int i = 0;
StackFrame frame;
// Find the IIpcService method that threw this exception
while ((frame = trace.GetFrame(i++)) != null)
{
var method = frame.GetMethod();
var declType = method.DeclaringType;
if (typeof(IpcService).IsAssignableFrom(declType))
{
return (declType, method);
}
}
return (null, null);
}
}
}