ServiceNotImplementedException.cs 5.0 KB

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