ServiceNotImplementedException.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. using Ryujinx.Common;
  2. using Ryujinx.HLE.HOS;
  3. using Ryujinx.HLE.HOS.Ipc;
  4. using Ryujinx.HLE.HOS.Kernel.Ipc;
  5. using Ryujinx.HLE.HOS.Services;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Diagnostics;
  9. using System.Linq;
  10. using System.Reflection;
  11. using System.Runtime.Serialization;
  12. using System.Text;
  13. namespace Ryujinx.HLE.Exceptions
  14. {
  15. [Serializable]
  16. internal class ServiceNotImplementedException : Exception
  17. {
  18. public ServiceCtx Context { get; }
  19. public IpcMessage Request { get; }
  20. public ServiceNotImplementedException(ServiceCtx context)
  21. : this(context, "The service call is not implemented.")
  22. { }
  23. public ServiceNotImplementedException(ServiceCtx context, string message)
  24. : base(message)
  25. {
  26. Context = context;
  27. Request = context.Request;
  28. }
  29. public ServiceNotImplementedException(ServiceCtx context, string message, Exception inner)
  30. : base(message, inner)
  31. {
  32. Context = context;
  33. Request = context.Request;
  34. }
  35. protected ServiceNotImplementedException(SerializationInfo info, StreamingContext context)
  36. : base(info, context)
  37. { }
  38. public override string Message
  39. {
  40. get
  41. {
  42. return base.Message +
  43. Environment.NewLine +
  44. Environment.NewLine +
  45. BuildMessage();
  46. }
  47. }
  48. private string BuildMessage()
  49. {
  50. StringBuilder sb = new StringBuilder();
  51. // Print the IPC command details (service name, command ID, and handler)
  52. (Type callingType, MethodBase callingMethod) = WalkStackTrace(new StackTrace(this));
  53. if (callingType != null && callingMethod != null)
  54. {
  55. var ipcService = Context.Session.Service;
  56. var ipcCommands = ipcService.Commands;
  57. // Find the handler for the method called
  58. var ipcHandler = ipcCommands.FirstOrDefault(x => x.Value.Method == callingMethod);
  59. var ipcCommandId = ipcHandler.Key;
  60. var ipcMethod = ipcHandler.Value;
  61. if (ipcMethod != null)
  62. {
  63. sb.AppendLine($"Service Command: {ipcService.GetType().FullName}: {ipcCommandId} ({ipcMethod.Method.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(IIpcService).IsAssignableFrom(declType))
  133. {
  134. return (declType, method);
  135. }
  136. }
  137. return (null, null);
  138. }
  139. }
  140. }