ServiceNotImplementedException.cs 5.4 KB

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