ServiceNotImplementedException.cs 5.5 KB

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