|
| 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.IO; |
| 6 | +using System.Runtime; |
| 7 | +using System.Text; |
| 8 | +using System.Threading; |
| 9 | + |
| 10 | +namespace System |
| 11 | +{ |
| 12 | + internal class EventReporter |
| 13 | + { |
| 14 | + private readonly RhFailFastReason _eventType; |
| 15 | + private readonly StringBuilder _description = new StringBuilder(); |
| 16 | + private bool _bufferFull; |
| 17 | + |
| 18 | + public unsafe EventReporter(RhFailFastReason eventType) |
| 19 | + { |
| 20 | + _eventType = eventType; |
| 21 | + |
| 22 | + string? processPath = Environment.ProcessPath; |
| 23 | + |
| 24 | + _description.Append("Application: "); |
| 25 | + |
| 26 | + // If we were able to get an app name. |
| 27 | + if (processPath != null) |
| 28 | + { |
| 29 | + // If app name has a '\', consider the part after that; otherwise consider whole name. |
| 30 | + _description.AppendLine(Path.GetFileName(processPath)); |
| 31 | + } |
| 32 | + else |
| 33 | + { |
| 34 | + _description.AppendLine("unknown"); |
| 35 | + } |
| 36 | + |
| 37 | + _description.Append("CoreCLR Version: "); |
| 38 | + |
| 39 | + byte* utf8version = RuntimeImports.RhGetRuntimeVersion(out int cbLength); |
| 40 | + _description.AppendLine(new string((sbyte*)utf8version)); |
| 41 | + |
| 42 | + switch (_eventType) |
| 43 | + { |
| 44 | + case RhFailFastReason.UnhandledException: |
| 45 | + case RhFailFastReason.UnhandledExceptionFromPInvoke: |
| 46 | + _description.AppendLine("Description: The process was terminated due to an unhandled exception."); |
| 47 | + break; |
| 48 | + case RhFailFastReason.EnvironmentFailFast: |
| 49 | + case RhFailFastReason.AssertionFailure: |
| 50 | + _description.AppendLine("Description: The application requested process termination through System.Environment.FailFast."); |
| 51 | + break; |
| 52 | + case RhFailFastReason.InternalError: |
| 53 | + _description.AppendLine("Description: The process was terminated due to an internal error in the .NET Runtime "); |
| 54 | + break; |
| 55 | + default: |
| 56 | + Debug.Fail($"Unknown {nameof(RhFailFastReason)}"); |
| 57 | + break; |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + public void AddDescription(string s) |
| 62 | + { |
| 63 | + Debug.Assert(_eventType is RhFailFastReason.UnhandledException |
| 64 | + or RhFailFastReason.EnvironmentFailFast or RhFailFastReason.AssertionFailure |
| 65 | + or RhFailFastReason.UnhandledExceptionFromPInvoke or RhFailFastReason.InternalError); |
| 66 | + if (_eventType is RhFailFastReason.EnvironmentFailFast or RhFailFastReason.AssertionFailure) |
| 67 | + { |
| 68 | + _description.Append("Message: "); |
| 69 | + } |
| 70 | + else if (_eventType == RhFailFastReason.UnhandledException) |
| 71 | + { |
| 72 | + _description.Append("Exception Info: "); |
| 73 | + } |
| 74 | + _description.AppendLine(s); |
| 75 | + } |
| 76 | + |
| 77 | + public void BeginStackTrace() |
| 78 | + { |
| 79 | + Debug.Assert(_eventType is RhFailFastReason.UnhandledException |
| 80 | + or RhFailFastReason.EnvironmentFailFast or RhFailFastReason.AssertionFailure |
| 81 | + or RhFailFastReason.UnhandledExceptionFromPInvoke); |
| 82 | + _description.AppendLine("Stack:"); |
| 83 | + } |
| 84 | + |
| 85 | + public void AddStackTrace(string s) |
| 86 | + { |
| 87 | + // The (approx.) maximum size that EventLog appears to allow. |
| 88 | + // |
| 89 | + // An event entry comprises of string to be written and event header information. |
| 90 | + // The total permissible length of the string and event header is 32K. |
| 91 | + const int MAX_SIZE_EVENTLOG_ENTRY_STRING = 0x7C62; // decimal 31842 |
| 92 | + |
| 93 | + // Continue to append to the buffer until we are full |
| 94 | + if (!_bufferFull) |
| 95 | + { |
| 96 | + _description.AppendLine(s); |
| 97 | + |
| 98 | + // Truncate the buffer if we have exceeded the limit based upon the OS we are on |
| 99 | + if (_description.Length > MAX_SIZE_EVENTLOG_ENTRY_STRING) |
| 100 | + { |
| 101 | + // Load the truncation message |
| 102 | + string truncate = "\nThe remainder of the message was truncated.\n"; |
| 103 | + |
| 104 | + int truncCount = truncate.Length; |
| 105 | + |
| 106 | + // Go back "truncCount" characters from the end of the string. |
| 107 | + int ext = MAX_SIZE_EVENTLOG_ENTRY_STRING - truncCount; |
| 108 | + |
| 109 | + // Now look for a "\n" from the last position we got |
| 110 | + for (; ext > 0 && _description[ext] != '\n'; ext--) ; |
| 111 | + |
| 112 | + // Truncate the string till our current position and append |
| 113 | + // the truncation message |
| 114 | + _description.Length = ext; |
| 115 | + |
| 116 | + _description.Append(truncate); |
| 117 | + |
| 118 | + // Set the flag that we are full - no point appending more stack details |
| 119 | + _bufferFull = true; |
| 120 | + } |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + public void Report() |
| 125 | + { |
| 126 | + uint eventID; |
| 127 | + switch (_eventType) |
| 128 | + { |
| 129 | + case RhFailFastReason.UnhandledException: |
| 130 | + case RhFailFastReason.UnhandledExceptionFromPInvoke: |
| 131 | + eventID = 1026; |
| 132 | + break; |
| 133 | + case RhFailFastReason.EnvironmentFailFast: |
| 134 | + case RhFailFastReason.AssertionFailure: |
| 135 | + eventID = 1025; |
| 136 | + break; |
| 137 | + case RhFailFastReason.InternalError: |
| 138 | + eventID = 1023; |
| 139 | + break; |
| 140 | + default: |
| 141 | + Debug.Fail("Invalid event type"); |
| 142 | + eventID = 1023; |
| 143 | + break; |
| 144 | + } |
| 145 | + |
| 146 | + if (_description.Length > 0) |
| 147 | + { |
| 148 | + ClrReportEvent(".NET Runtime", |
| 149 | + 1 /* EVENTLOG_ERROR_TYPE */, |
| 150 | + 0, |
| 151 | + eventID, |
| 152 | + _description.ToString() |
| 153 | + ); |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + private static unsafe void ClrReportEvent(string eventSource, short type, ushort category, uint eventId, string message) |
| 158 | + { |
| 159 | + IntPtr handle = Interop.Advapi32.RegisterEventSource( |
| 160 | + null, // uses local computer |
| 161 | + eventSource); |
| 162 | + |
| 163 | + if (handle == IntPtr.Zero) |
| 164 | + return; |
| 165 | + |
| 166 | + fixed (char* pMessage = message) |
| 167 | + { |
| 168 | + Interop.Advapi32.ReportEvent(handle, type, category, eventId, null, 1, 0, (nint)(&pMessage), null); |
| 169 | + } |
| 170 | + |
| 171 | + Interop.Advapi32.DeregisterEventSource(handle); |
| 172 | + } |
| 173 | + |
| 174 | + private static byte s_once; |
| 175 | + |
| 176 | + public static bool ShouldLogInEventLog |
| 177 | + { |
| 178 | + get |
| 179 | + { |
| 180 | + if (Interop.Kernel32.IsDebuggerPresent()) |
| 181 | + return false; |
| 182 | + |
| 183 | + if (s_once == 1 || Interlocked.Exchange(ref s_once, 1) == 1) |
| 184 | + return false; |
| 185 | + |
| 186 | + return true; |
| 187 | + } |
| 188 | + } |
| 189 | + } |
| 190 | +} |
0 commit comments