| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System.Diagnostics; |
| | | 5 | | using System.Diagnostics.CodeAnalysis; |
| | | 6 | | using System.Diagnostics.Tracing; |
| | | 7 | | using System.Runtime.CompilerServices; |
| | | 8 | | |
| | | 9 | | namespace System.Net |
| | | 10 | | { |
| | | 11 | | [EventSource(Name = "Private.InternalDiagnostics.System.Net.WebSockets")] |
| | | 12 | | internal sealed partial class NetEventSource |
| | | 13 | | { |
| | | 14 | | // NOTE |
| | | 15 | | // - The 'Start' and 'Stop' suffixes on the following event names have special meaning in EventSource. They |
| | | 16 | | // enable creating 'activities'. |
| | | 17 | | // For more information, take a look at the following blog post: |
| | | 18 | | // https://blogs.msdn.microsoft.com/vancem/2015/09/14/exploring-eventsource-activity-correlation-and-causation |
| | | 19 | | // - A stop event's event id must be next one after its start event. |
| | | 20 | | |
| | | 21 | | private const int KeepAliveSentId = NextAvailableEventId; |
| | | 22 | | private const int KeepAliveAckedId = KeepAliveSentId + 1; |
| | | 23 | | |
| | | 24 | | private const int WsTraceId = KeepAliveAckedId + 1; |
| | | 25 | | |
| | | 26 | | private const int CloseStartId = WsTraceId + 1; |
| | | 27 | | private const int CloseStopId = CloseStartId + 1; |
| | | 28 | | |
| | | 29 | | private const int ReceiveStartId = CloseStopId + 1; |
| | | 30 | | private const int ReceiveStopId = ReceiveStartId + 1; |
| | | 31 | | |
| | | 32 | | private const int SendStartId = ReceiveStopId + 1; |
| | | 33 | | private const int SendStopId = SendStartId + 1; |
| | | 34 | | |
| | | 35 | | private const int MutexEnterId = SendStopId + 1; |
| | | 36 | | private const int MutexExitId = MutexEnterId + 1; |
| | | 37 | | |
| | | 38 | | // |
| | | 39 | | // Keep-Alive |
| | | 40 | | // |
| | | 41 | | |
| | | 42 | | private const string Ping = "Ping"; |
| | | 43 | | private const string Pong = "Pong"; |
| | | 44 | | |
| | | 45 | | [Event(KeepAliveSentId, Level = EventLevel.Informational)] |
| | | 46 | | private void KeepAliveSent(string objName, string opcode, long payload) => |
| | 0 | 47 | | WriteEvent(KeepAliveSentId, objName, opcode, payload); |
| | | 48 | | |
| | | 49 | | [Event(KeepAliveAckedId, Level = EventLevel.Informational)] |
| | | 50 | | private void KeepAliveAcked(string objName, long payload) => |
| | 0 | 51 | | WriteEvent(KeepAliveAckedId, objName, payload); |
| | | 52 | | |
| | | 53 | | [NonEvent] |
| | | 54 | | public static void KeepAlivePingSent(object? obj, long payload) |
| | 0 | 55 | | { |
| | 0 | 56 | | Debug.Assert(Log.IsEnabled()); |
| | 0 | 57 | | Log.KeepAliveSent(IdOf(obj), Ping, payload); |
| | 0 | 58 | | } |
| | | 59 | | |
| | | 60 | | [NonEvent] |
| | | 61 | | public static void UnsolicitedPongSent(object? obj) |
| | | 62 | | { |
| | | 63 | | Debug.Assert(Log.IsEnabled()); |
| | | 64 | | Log.KeepAliveSent(IdOf(obj), Pong, 0); |
| | | 65 | | } |
| | | 66 | | |
| | | 67 | | [NonEvent] |
| | | 68 | | public static void PongResponseReceived(object? obj, long payload) |
| | 0 | 69 | | { |
| | 0 | 70 | | Debug.Assert(Log.IsEnabled()); |
| | 0 | 71 | | Log.KeepAliveAcked(IdOf(obj), payload); |
| | 0 | 72 | | } |
| | | 73 | | |
| | | 74 | | // |
| | | 75 | | // Debug Messages |
| | | 76 | | // |
| | | 77 | | |
| | | 78 | | [Event(WsTraceId, Level = EventLevel.Verbose)] |
| | | 79 | | private void WsTrace(string objName, string memberName, string message) => |
| | 0 | 80 | | WriteEvent(WsTraceId, objName, memberName, message); |
| | | 81 | | |
| | | 82 | | [NonEvent] |
| | | 83 | | public static void TraceErrorMsg(object? obj, Exception exception, [CallerMemberName] string? memberName = null) |
| | 0 | 84 | | => Trace(obj, $"{exception.GetType().Name}: {exception.Message}", memberName); |
| | | 85 | | |
| | | 86 | | [NonEvent] |
| | | 87 | | public static void TraceException(object? obj, Exception exception, [CallerMemberName] string? memberName = null |
| | 0 | 88 | | => Trace(obj, exception.ToString(), memberName); |
| | | 89 | | |
| | | 90 | | [NonEvent] |
| | | 91 | | public static void Trace(object? obj, string? message = null, [CallerMemberName] string? memberName = null) |
| | 0 | 92 | | { |
| | 0 | 93 | | Debug.Assert(Log.IsEnabled()); |
| | 0 | 94 | | Log.WsTrace(IdOf(obj), memberName ?? MissingMember, message ?? memberName ?? string.Empty); |
| | 0 | 95 | | } |
| | | 96 | | |
| | | 97 | | // |
| | | 98 | | // Close |
| | | 99 | | // |
| | | 100 | | |
| | | 101 | | [Event(CloseStartId, Level = EventLevel.Verbose)] |
| | | 102 | | private void CloseStart(string objName, string memberName) => |
| | 0 | 103 | | WriteEvent(CloseStartId, objName, memberName); |
| | | 104 | | |
| | | 105 | | [Event(CloseStopId, Level = EventLevel.Verbose)] |
| | | 106 | | private void CloseStop(string objName, string memberName) => |
| | 0 | 107 | | WriteEvent(CloseStopId, objName, memberName); |
| | | 108 | | |
| | | 109 | | [NonEvent] |
| | | 110 | | public static void CloseAsyncPrivateStarted(object? obj, [CallerMemberName] string? memberName = null) |
| | 0 | 111 | | { |
| | 0 | 112 | | Debug.Assert(Log.IsEnabled()); |
| | 0 | 113 | | Log.CloseStart(IdOf(obj), memberName ?? MissingMember); |
| | 0 | 114 | | } |
| | | 115 | | |
| | | 116 | | [NonEvent] |
| | | 117 | | public static void CloseAsyncPrivateCompleted(object? obj, [CallerMemberName] string? memberName = null) |
| | 0 | 118 | | { |
| | 0 | 119 | | Debug.Assert(Log.IsEnabled()); |
| | 0 | 120 | | Log.CloseStop(IdOf(obj), memberName ?? MissingMember); |
| | 0 | 121 | | } |
| | | 122 | | |
| | | 123 | | // |
| | | 124 | | // ReceiveAsyncPrivate |
| | | 125 | | // |
| | | 126 | | |
| | | 127 | | [Event(ReceiveStartId, Level = EventLevel.Informational)] |
| | | 128 | | private void ReceiveStart(string objName, string memberName, int bufferLength) => |
| | 0 | 129 | | WriteEvent(ReceiveStartId, objName, memberName, bufferLength); |
| | | 130 | | |
| | | 131 | | [Event(ReceiveStopId, Level = EventLevel.Informational)] |
| | | 132 | | private void ReceiveStop(string objName, string memberName) => |
| | 0 | 133 | | WriteEvent(ReceiveStopId, objName, memberName); |
| | | 134 | | |
| | | 135 | | [NonEvent] |
| | | 136 | | public static void ReceiveAsyncPrivateStarted(object? obj, int bufferLength, [CallerMemberName] string? memberNa |
| | 0 | 137 | | { |
| | 0 | 138 | | Debug.Assert(Log.IsEnabled()); |
| | 0 | 139 | | Log.ReceiveStart(IdOf(obj), memberName ?? MissingMember, bufferLength); |
| | 0 | 140 | | } |
| | | 141 | | |
| | | 142 | | [NonEvent] |
| | | 143 | | public static void ReceiveAsyncPrivateCompleted(object? obj, [CallerMemberName] string? memberName = null) |
| | 0 | 144 | | { |
| | 0 | 145 | | Debug.Assert(Log.IsEnabled()); |
| | 0 | 146 | | Log.ReceiveStop(IdOf(obj), memberName ?? MissingMember); |
| | 0 | 147 | | } |
| | | 148 | | |
| | | 149 | | // |
| | | 150 | | // SendFrameAsync |
| | | 151 | | // |
| | | 152 | | |
| | | 153 | | [Event(SendStartId, Level = EventLevel.Verbose)] |
| | | 154 | | private void SendStart(string objName, string memberName, string opcode, int bufferLength) => |
| | 0 | 155 | | WriteEvent(SendStartId, objName, memberName, opcode, bufferLength); |
| | | 156 | | |
| | | 157 | | [Event(SendStopId, Level = EventLevel.Verbose)] |
| | | 158 | | private void SendStop(string objName, string memberName) => |
| | 0 | 159 | | WriteEvent(SendStopId, objName, memberName); |
| | | 160 | | |
| | | 161 | | [NonEvent] |
| | | 162 | | public static void SendFrameAsyncStarted(object? obj, string opcode, int bufferLength, [CallerMemberName] string |
| | 0 | 163 | | { |
| | 0 | 164 | | Debug.Assert(Log.IsEnabled()); |
| | 0 | 165 | | Log.SendStart(IdOf(obj), memberName ?? MissingMember, opcode, bufferLength); |
| | 0 | 166 | | } |
| | | 167 | | |
| | | 168 | | [NonEvent] |
| | | 169 | | public static void SendFrameAsyncCompleted(object? obj, [CallerMemberName] string? memberName = null) |
| | 0 | 170 | | { |
| | 0 | 171 | | Debug.Assert(Log.IsEnabled()); |
| | 0 | 172 | | Log.SendStop(IdOf(obj), memberName ?? MissingMember); |
| | 0 | 173 | | } |
| | | 174 | | |
| | | 175 | | // |
| | | 176 | | // AsyncMutex |
| | | 177 | | // |
| | | 178 | | |
| | | 179 | | [Event(MutexEnterId, Level = EventLevel.Verbose)] |
| | | 180 | | private void MutexEnter(string objName, string memberName) => |
| | 0 | 181 | | WriteEvent(MutexEnterId, objName, memberName); |
| | | 182 | | |
| | | 183 | | [Event(MutexExitId, Level = EventLevel.Verbose)] |
| | | 184 | | private void MutexExit(string objName, string memberName) => |
| | 0 | 185 | | WriteEvent(MutexExitId, objName, memberName); |
| | | 186 | | |
| | | 187 | | [NonEvent] |
| | | 188 | | public static void MutexEntered(object? obj, [CallerMemberName] string? memberName = null) |
| | 0 | 189 | | { |
| | 0 | 190 | | Debug.Assert(Log.IsEnabled()); |
| | 0 | 191 | | Log.MutexEnter(IdOf(obj), memberName ?? MissingMember); |
| | 0 | 192 | | } |
| | | 193 | | |
| | | 194 | | [NonEvent] |
| | | 195 | | public static void MutexExited(object? obj, [CallerMemberName] string? memberName = null) |
| | 0 | 196 | | { |
| | 0 | 197 | | Debug.Assert(Log.IsEnabled()); |
| | 0 | 198 | | Log.MutexExit(IdOf(obj), memberName ?? MissingMember); |
| | 0 | 199 | | } |
| | | 200 | | |
| | | 201 | | // |
| | | 202 | | // WriteEvent overloads |
| | | 203 | | // |
| | | 204 | | |
| | | 205 | | [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026:UnrecognizedReflectionPattern", |
| | | 206 | | Justification = EventSourceSuppressMessage)] |
| | | 207 | | [NonEvent] |
| | | 208 | | private unsafe void WriteEvent(int eventId, string arg1, string arg2, long arg3) |
| | 0 | 209 | | { |
| | 0 | 210 | | fixed (char* arg1Ptr = arg1) |
| | 0 | 211 | | fixed (char* arg2Ptr = arg2) |
| | 0 | 212 | | { |
| | | 213 | | const int NumEventDatas = 3; |
| | 0 | 214 | | EventData* descrs = stackalloc EventData[NumEventDatas]; |
| | | 215 | | |
| | 0 | 216 | | descrs[0] = new EventData |
| | 0 | 217 | | { |
| | 0 | 218 | | DataPointer = (IntPtr)(arg1Ptr), |
| | 0 | 219 | | Size = (arg1.Length + 1) * sizeof(char) |
| | 0 | 220 | | }; |
| | 0 | 221 | | descrs[1] = new EventData |
| | 0 | 222 | | { |
| | 0 | 223 | | DataPointer = (IntPtr)(arg2Ptr), |
| | 0 | 224 | | Size = (arg2.Length + 1) * sizeof(char) |
| | 0 | 225 | | }; |
| | 0 | 226 | | descrs[2] = new EventData |
| | 0 | 227 | | { |
| | 0 | 228 | | DataPointer = (IntPtr)(&arg3), |
| | 0 | 229 | | Size = sizeof(long) |
| | 0 | 230 | | }; |
| | | 231 | | |
| | 0 | 232 | | WriteEventCore(eventId, NumEventDatas, descrs); |
| | 0 | 233 | | } |
| | 0 | 234 | | } |
| | | 235 | | |
| | | 236 | | [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026:UnrecognizedReflectionPattern", |
| | | 237 | | Justification = EventSourceSuppressMessage)] |
| | | 238 | | [NonEvent] |
| | | 239 | | private unsafe void WriteEvent(int eventId, string arg1, string arg2, string arg3, int arg4) |
| | 0 | 240 | | { |
| | 0 | 241 | | fixed (char* arg1Ptr = arg1) |
| | 0 | 242 | | fixed (char* arg2Ptr = arg2) |
| | 0 | 243 | | fixed (char* arg3Ptr = arg3) |
| | 0 | 244 | | { |
| | | 245 | | const int NumEventDatas = 4; |
| | 0 | 246 | | EventData* descrs = stackalloc EventData[NumEventDatas]; |
| | | 247 | | |
| | 0 | 248 | | descrs[0] = new EventData |
| | 0 | 249 | | { |
| | 0 | 250 | | DataPointer = (IntPtr)(arg1Ptr), |
| | 0 | 251 | | Size = (arg1.Length + 1) * sizeof(char) |
| | 0 | 252 | | }; |
| | 0 | 253 | | descrs[1] = new EventData |
| | 0 | 254 | | { |
| | 0 | 255 | | DataPointer = (IntPtr)(arg2Ptr), |
| | 0 | 256 | | Size = (arg2.Length + 1) * sizeof(char) |
| | 0 | 257 | | }; |
| | 0 | 258 | | descrs[2] = new EventData |
| | 0 | 259 | | { |
| | 0 | 260 | | DataPointer = (IntPtr)(arg3Ptr), |
| | 0 | 261 | | Size = (arg3.Length + 1) * sizeof(char) |
| | 0 | 262 | | }; |
| | 0 | 263 | | descrs[3] = new EventData |
| | 0 | 264 | | { |
| | 0 | 265 | | DataPointer = (IntPtr)(&arg4), |
| | 0 | 266 | | Size = sizeof(int) |
| | 0 | 267 | | }; |
| | | 268 | | |
| | 0 | 269 | | WriteEventCore(eventId, NumEventDatas, descrs); |
| | 0 | 270 | | } |
| | 0 | 271 | | } |
| | | 272 | | |
| | | 273 | | } |
| | | 274 | | } |
| | | 275 | | |