ServiceNotImplementedException.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. using Ryujinx.Common;
  2. using Ryujinx.HLE.HOS;
  3. using Ryujinx.HLE.HOS.Ipc;
  4. using Ryujinx.HLE.HOS.Services;
  5. using System;
  6. using System.Diagnostics;
  7. using System.Linq;
  8. using System.Reflection;
  9. using System.Runtime.Serialization;
  10. using System.Text;
  11. namespace Ryujinx.HLE.Exceptions
  12. {
  13. [Serializable]
  14. internal class ServiceNotImplementedException : Exception
  15. {
  16. public IpcService Service { get; }
  17. public ServiceCtx Context { get; }
  18. public IpcMessage Request { get; }
  19. public ServiceNotImplementedException(IpcService service, ServiceCtx context)
  20. : this(service, context, "The service call is not implemented.") { }
  21. public ServiceNotImplementedException(IpcService service, ServiceCtx context, string message) : base(message)
  22. {
  23. Service = service;
  24. Context = context;
  25. Request = context.Request;
  26. }
  27. public ServiceNotImplementedException(IpcService service, ServiceCtx context, string message, Exception inner) : base(message, inner)
  28. {
  29. Service = service;
  30. Context = context;
  31. Request = context.Request;
  32. }
  33. protected ServiceNotImplementedException(SerializationInfo info, StreamingContext context) : base(info, context) { }
  34. public override string Message
  35. {
  36. get
  37. {
  38. return base.Message + Environment.NewLine + Environment.NewLine + BuildMessage();
  39. }
  40. }
  41. private string BuildMessage()
  42. {
  43. StringBuilder sb = new StringBuilder();
  44. // Print the IPC command details (service name, command ID, and handler)
  45. (Type callingType, MethodBase callingMethod) = WalkStackTrace(new StackTrace(this));
  46. if (callingType != null && callingMethod != null)
  47. {
  48. // If the type is past 0xF, we are using TIPC
  49. var ipcCommands = Request.Type > IpcMessageType.TipcCloseSession ? Service.TipcCommands : Service.HipcCommands;
  50. // Find the handler for the method called
  51. var ipcHandler = ipcCommands.FirstOrDefault(x => x.Value == callingMethod);
  52. var ipcCommandId = ipcHandler.Key;
  53. var ipcMethod = ipcHandler.Value;
  54. if (ipcMethod != null)
  55. {
  56. sb.AppendLine($"Service Command: {Service.GetType().FullName}: {ipcCommandId} ({ipcMethod.Name})");
  57. sb.AppendLine();
  58. }
  59. }
  60. sb.AppendLine("Guest Stack Trace:");
  61. sb.AppendLine(Context.Thread.GetGuestStackTrace());
  62. // Print buffer information
  63. if (Request.PtrBuff.Count > 0 ||
  64. Request.SendBuff.Count > 0 ||
  65. Request.ReceiveBuff.Count > 0 ||
  66. Request.ExchangeBuff.Count > 0 ||
  67. Request.RecvListBuff.Count > 0)
  68. {
  69. sb.AppendLine("Buffer Information:");
  70. if (Request.PtrBuff.Count > 0)
  71. {
  72. sb.AppendLine("\tPtrBuff:");
  73. foreach (var buff in Request.PtrBuff)
  74. {
  75. sb.AppendLine($"\t[{buff.Index}] Position: 0x{buff.Position:x16} Size: 0x{buff.Size:x16}");
  76. }
  77. }
  78. if (Request.SendBuff.Count > 0)
  79. {
  80. sb.AppendLine("\tSendBuff:");
  81. foreach (var buff in Request.SendBuff)
  82. {
  83. sb.AppendLine($"\tPosition: 0x{buff.Position:x16} Size: 0x{buff.Size:x16} Flags: {buff.Flags}");
  84. }
  85. }
  86. if (Request.ReceiveBuff.Count > 0)
  87. {
  88. sb.AppendLine("\tReceiveBuff:");
  89. foreach (var buff in Request.ReceiveBuff)
  90. {
  91. sb.AppendLine($"\tPosition: 0x{buff.Position:x16} Size: 0x{buff.Size:x16} Flags: {buff.Flags}");
  92. }
  93. }
  94. if (Request.ExchangeBuff.Count > 0)
  95. {
  96. sb.AppendLine("\tExchangeBuff:");
  97. foreach (var buff in Request.ExchangeBuff)
  98. {
  99. sb.AppendLine($"\tPosition: 0x{buff.Position:x16} Size: 0x{buff.Size:x16} Flags: {buff.Flags}");
  100. }
  101. }
  102. if (Request.RecvListBuff.Count > 0)
  103. {
  104. sb.AppendLine("\tRecvListBuff:");
  105. foreach (var buff in Request.RecvListBuff)
  106. {
  107. sb.AppendLine($"\tPosition: 0x{buff.Position:x16} Size: 0x{buff.Size:x16}");
  108. }
  109. }
  110. sb.AppendLine();
  111. }
  112. sb.AppendLine("Raw Request Data:");
  113. sb.Append(HexUtils.HexTable(Request.RawData));
  114. return sb.ToString();
  115. }
  116. private static (Type, MethodBase) WalkStackTrace(StackTrace trace)
  117. {
  118. int i = 0;
  119. StackFrame frame;
  120. // Find the IIpcService method that threw this exception
  121. while ((frame = trace.GetFrame(i++)) != null)
  122. {
  123. var method = frame.GetMethod();
  124. var declType = method.DeclaringType;
  125. if (typeof(IpcService).IsAssignableFrom(declType))
  126. {
  127. return (declType, method);
  128. }
  129. }
  130. return (null, null);
  131. }
  132. }
  133. }