aboutsummaryrefslogtreecommitdiff
path: root/src/Ryujinx.HLE/Exceptions/ServiceNotImplementedException.cs
blob: 408b5baa4829f09cc10092e2192868025daf4f49 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
using Ryujinx.Common;
using Ryujinx.HLE.HOS;
using Ryujinx.HLE.HOS.Ipc;
using Ryujinx.HLE.HOS.Services;
using System;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.Serialization;
using System.Text;

namespace Ryujinx.HLE.Exceptions
{
    [Serializable]
    internal class ServiceNotImplementedException : Exception
    {
        public IpcService Service { get; }
        public ServiceCtx Context { get; }
        public IpcMessage Request { get; }

        public ServiceNotImplementedException(IpcService service, ServiceCtx context)
            : this(service, context, "The service call is not implemented.") { }

        public ServiceNotImplementedException(IpcService service, ServiceCtx context, string message) : base(message)
        {
            Service = service;
            Context = context;
            Request = context.Request;
        }

        public ServiceNotImplementedException(IpcService service, ServiceCtx context, string message, Exception inner) : base(message, inner)
        {
            Service = service;
            Context = context;
            Request = context.Request;
        }

        public override string Message
        {
            get
            {
                return base.Message + Environment.NewLine + Environment.NewLine + BuildMessage();
            }
        }

        private string BuildMessage()
        {
            StringBuilder sb = new();

            // Print the IPC command details (service name, command ID, and handler)
            (Type callingType, MethodBase callingMethod) = WalkStackTrace(new StackTrace(this));

            if (callingType != null && callingMethod != null)
            {
                // If the type is past 0xF, we are using TIPC
                var ipcCommands = Request.Type > IpcMessageType.TipcCloseSession ? Service.TipcCommands : Service.CmifCommands;

                // Find the handler for the method called
                var ipcHandler = ipcCommands.FirstOrDefault(x => x.Value == callingMethod);
                var ipcCommandId = ipcHandler.Key;
                var ipcMethod = ipcHandler.Value;

                if (ipcMethod != null)
                {
                    sb.AppendLine($"Service Command: {Service.GetType().FullName}: {ipcCommandId} ({ipcMethod.Name})");
                    sb.AppendLine();
                }
            }

            sb.AppendLine("Guest Stack Trace:");
            sb.AppendLine(Context.Thread.GetGuestStackTrace());

            // Print buffer information
            if (Request.PtrBuff.Count > 0 ||
                Request.SendBuff.Count > 0 ||
                Request.ReceiveBuff.Count > 0 ||
                Request.ExchangeBuff.Count > 0 ||
                Request.RecvListBuff.Count > 0)
            {
                sb.AppendLine("Buffer Information:");

                if (Request.PtrBuff.Count > 0)
                {
                    sb.AppendLine("\tPtrBuff:");

                    foreach (var buff in Request.PtrBuff)
                    {
                        sb.AppendLine($"\t[{buff.Index}] Position: 0x{buff.Position:x16} Size: 0x{buff.Size:x16}");
                    }
                }

                if (Request.SendBuff.Count > 0)
                {
                    sb.AppendLine("\tSendBuff:");

                    foreach (var buff in Request.SendBuff)
                    {
                        sb.AppendLine($"\tPosition: 0x{buff.Position:x16} Size: 0x{buff.Size:x16} Flags: {buff.Flags}");
                    }
                }

                if (Request.ReceiveBuff.Count > 0)
                {
                    sb.AppendLine("\tReceiveBuff:");

                    foreach (var buff in Request.ReceiveBuff)
                    {
                        sb.AppendLine($"\tPosition: 0x{buff.Position:x16} Size: 0x{buff.Size:x16} Flags: {buff.Flags}");
                    }
                }

                if (Request.ExchangeBuff.Count > 0)
                {
                    sb.AppendLine("\tExchangeBuff:");

                    foreach (var buff in Request.ExchangeBuff)
                    {
                        sb.AppendLine($"\tPosition: 0x{buff.Position:x16} Size: 0x{buff.Size:x16} Flags: {buff.Flags}");
                    }
                }

                if (Request.RecvListBuff.Count > 0)
                {
                    sb.AppendLine("\tRecvListBuff:");

                    foreach (var buff in Request.RecvListBuff)
                    {
                        sb.AppendLine($"\tPosition: 0x{buff.Position:x16} Size: 0x{buff.Size:x16}");
                    }
                }

                sb.AppendLine();
            }

            sb.AppendLine("Raw Request Data:");
            sb.Append(HexUtils.HexTable(Request.RawData));

            return sb.ToString();
        }

        private static (Type, MethodBase) WalkStackTrace(StackTrace trace)
        {
            int i = 0;

            StackFrame frame;

            // Find the IIpcService method that threw this exception
            while ((frame = trace.GetFrame(i++)) != null)
            {
                var method = frame.GetMethod();
                var declType = method.DeclaringType;

                if (typeof(IpcService).IsAssignableFrom(declType))
                {
                    return (declType, method);
                }
            }

            return (null, null);
        }
    }
}