ServiceNotImplementedException.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. private bool _isTipcCommand;
  20. public ServiceNotImplementedException(IpcService service, ServiceCtx context, bool isTipcCommand)
  21. : this(service, context, "The service call is not implemented.", isTipcCommand)
  22. { }
  23. public ServiceNotImplementedException(IpcService service, ServiceCtx context, string message, bool isTipcCommand)
  24. : base(message)
  25. {
  26. Service = service;
  27. Context = context;
  28. Request = context.Request;
  29. _isTipcCommand = isTipcCommand;
  30. }
  31. public ServiceNotImplementedException(IpcService service, ServiceCtx context, string message, Exception inner, bool isTipcCommand)
  32. : base(message, inner)
  33. {
  34. Service = service;
  35. Context = context;
  36. Request = context.Request;
  37. _isTipcCommand = isTipcCommand;
  38. }
  39. protected ServiceNotImplementedException(SerializationInfo info, StreamingContext context)
  40. : base(info, context)
  41. { }
  42. public override string Message
  43. {
  44. get
  45. {
  46. return base.Message +
  47. Environment.NewLine +
  48. Environment.NewLine +
  49. BuildMessage();
  50. }
  51. }
  52. private string BuildMessage()
  53. {
  54. StringBuilder sb = new StringBuilder();
  55. // Print the IPC command details (service name, command ID, and handler)
  56. (Type callingType, MethodBase callingMethod) = WalkStackTrace(new StackTrace(this));
  57. if (callingType != null && callingMethod != null)
  58. {
  59. var ipcCommands = _isTipcCommand ? Service.TipcCommands : Service.HipcCommands;
  60. // Find the handler for the method called
  61. var ipcHandler = ipcCommands.FirstOrDefault(x => x.Value == callingMethod);
  62. var ipcCommandId = ipcHandler.Key;
  63. var ipcMethod = ipcHandler.Value;
  64. if (ipcMethod != null)
  65. {
  66. sb.AppendLine($"Service Command: {Service.GetType().FullName}: {ipcCommandId} ({ipcMethod.Name})");
  67. sb.AppendLine();
  68. }
  69. }
  70. sb.AppendLine("Guest Stack Trace:");
  71. sb.AppendLine(Context.Thread.GetGuestStackTrace());
  72. // Print buffer information
  73. if (Request.PtrBuff.Count > 0 ||
  74. Request.SendBuff.Count > 0 ||
  75. Request.ReceiveBuff.Count > 0 ||
  76. Request.ExchangeBuff.Count > 0 ||
  77. Request.RecvListBuff.Count > 0)
  78. {
  79. sb.AppendLine("Buffer Information:");
  80. if (Request.PtrBuff.Count > 0)
  81. {
  82. sb.AppendLine("\tPtrBuff:");
  83. foreach (var buff in Request.PtrBuff)
  84. {
  85. sb.AppendLine($"\t[{buff.Index}] Position: 0x{buff.Position:x16} Size: 0x{buff.Size:x16}");
  86. }
  87. }
  88. if (Request.SendBuff.Count > 0)
  89. {
  90. sb.AppendLine("\tSendBuff:");
  91. foreach (var buff in Request.SendBuff)
  92. {
  93. sb.AppendLine($"\tPosition: 0x{buff.Position:x16} Size: 0x{buff.Size:x16} Flags: {buff.Flags}");
  94. }
  95. }
  96. if (Request.ReceiveBuff.Count > 0)
  97. {
  98. sb.AppendLine("\tReceiveBuff:");
  99. foreach (var buff in Request.ReceiveBuff)
  100. {
  101. sb.AppendLine($"\tPosition: 0x{buff.Position:x16} Size: 0x{buff.Size:x16} Flags: {buff.Flags}");
  102. }
  103. }
  104. if (Request.ExchangeBuff.Count > 0)
  105. {
  106. sb.AppendLine("\tExchangeBuff:");
  107. foreach (var buff in Request.ExchangeBuff)
  108. {
  109. sb.AppendLine($"\tPosition: 0x{buff.Position:x16} Size: 0x{buff.Size:x16} Flags: {buff.Flags}");
  110. }
  111. }
  112. if (Request.RecvListBuff.Count > 0)
  113. {
  114. sb.AppendLine("\tRecvListBuff:");
  115. foreach (var buff in Request.RecvListBuff)
  116. {
  117. sb.AppendLine($"\tPosition: 0x{buff.Position:x16} Size: 0x{buff.Size:x16}");
  118. }
  119. }
  120. sb.AppendLine();
  121. }
  122. sb.AppendLine("Raw Request Data:");
  123. sb.Append(HexUtils.HexTable(Request.RawData));
  124. return sb.ToString();
  125. }
  126. private (Type, MethodBase) WalkStackTrace(StackTrace trace)
  127. {
  128. int i = 0;
  129. StackFrame frame;
  130. // Find the IIpcService method that threw this exception
  131. while ((frame = trace.GetFrame(i++)) != null)
  132. {
  133. var method = frame.GetMethod();
  134. var declType = method.DeclaringType;
  135. if (typeof(IpcService).IsAssignableFrom(declType))
  136. {
  137. return (declType, method);
  138. }
  139. }
  140. return (null, null);
  141. }
  142. }
  143. }