private static void SubmitReport(Exception uncaughtException) { lock (WindowsErrorReporting.reportCreationLock) { if (uncaughtException == null) { throw new ArgumentNullException(nameof(uncaughtException)); } WindowsErrorReporting.ReportInformation reportInformation = new WindowsErrorReporting.ReportInformation(); reportInformation.dwSize = Marshal.SizeOf((object)reportInformation); reportInformation.hProcess = WindowsErrorReporting.hCurrentProcess; reportInformation.hwndParent = WindowsErrorReporting.hwndMainWindow; reportInformation.wzApplicationName = WindowsErrorReporting.applicationName; reportInformation.wzApplicationPath = WindowsErrorReporting.applicationPath; reportInformation.wzConsentKey = (string)null; reportInformation.wzDescription = (string)null; reportInformation.wzFriendlyEventName = (string)null; WindowsErrorReporting.ReportHandle reportHandle; WindowsErrorReporting.HandleHResult(WindowsErrorReporting.NativeMethods.WerReportCreate("PowerShell", WindowsErrorReporting.ReportType.WerReportCritical, reportInformation, out reportHandle)); using (reportHandle) { WindowsErrorReporting.SetBucketParameters(reportHandle, uncaughtException); WindowsErrorReporting.HandleHResult(WindowsErrorReporting.NativeMethods.WerReportAddDump(reportHandle, WindowsErrorReporting.hCurrentProcess, IntPtr.Zero, WindowsErrorReporting.DumpType.MiniDump, IntPtr.Zero, IntPtr.Zero, (WindowsErrorReporting.DumpFlags) 0)); WindowsErrorReporting.SubmitResult result = WindowsErrorReporting.SubmitResult.ReportFailed; WindowsErrorReporting.SubmitFlags flags = WindowsErrorReporting.SubmitFlags.HonorRecovery | WindowsErrorReporting.SubmitFlags.HonorRestart | WindowsErrorReporting.SubmitFlags.AddRegisteredData | WindowsErrorReporting.SubmitFlags.OutOfProcess; if (WindowsErrorReporting.unattendedServerMode) { flags |= WindowsErrorReporting.SubmitFlags.Queue; } WindowsErrorReporting.HandleHResult(WindowsErrorReporting.NativeMethods.WerReportSubmit(reportHandle, WindowsErrorReporting.Consent.NotAsked, flags, out result)); Environment.Exit((int)result); } } }
private static void SetBucketParameter( WindowsErrorReporting.ReportHandle reportHandle, WindowsErrorReporting.BucketParameterId bucketParameterId, string value) { WindowsErrorReporting.HandleHResult(WindowsErrorReporting.NativeMethods.WerReportSetParameter(reportHandle, bucketParameterId, bucketParameterId.ToString(), value)); }
/// <summary> /// Equivalent to "System.Environment.FailFast(string, System.Exception)" that also does custom Watson reports. /// This method suppresses all the exceptions as this is not important for any /// functionality. This feature is primarily used to help Microsoft fix /// bugs/crashes from customer data. /// </summary> /// <param name="exception"> /// exception causing the failure. It is good to make sure this is not null. /// However the code will handle null cases. /// </param> internal static void FailFast(Exception exception) { Dbg.Assert(false, "We shouldn't do to FailFast during normal operation"); try { if (s_registered && (null != exception)) { Debug.Assert(IsWindowsErrorReportingAvailable(), "Registration should succeed only if WER.dll is available"); WindowsErrorReporting.SubmitReport(exception); } } catch (Exception) { // SubmitReport can throw exceptions. suppressing those as they are not // important to report back. // Not calling CommandProcessorBase.CheckForSevereException(e) as it // would introduce a recursion. } finally { // FailFast if something went wrong and SubmitReport didn't terminate the process // (or simply if not registered) Environment.FailFast((null != exception) ? exception.Message : string.Empty); } }
private static string TruncateExeName(string nameOfExe, int maxLength) { nameOfExe = nameOfExe.Trim(); if (nameOfExe.Length > maxLength && nameOfExe.EndsWith(".exe", StringComparison.OrdinalIgnoreCase)) { nameOfExe = nameOfExe.Substring(0, nameOfExe.Length - ".exe".Length); } return(WindowsErrorReporting.TruncateBucketParameter(nameOfExe, maxLength)); }
private static void CurrentDomain_UnhandledException( object sender, UnhandledExceptionEventArgs e) { if (!(e.ExceptionObject is Exception exceptionObject)) { return; } WindowsErrorReporting.SubmitReport(exceptionObject); }
private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) { Dbg.Assert(false, "We shouldn't get unhandled exceptions during normal operation: " + e.ExceptionObject.ToString()); Exception exception = e.ExceptionObject as Exception; if (exception != null) { WindowsErrorReporting.SubmitReport(exception); } }
private static string TruncateExceptionType(string exceptionType, int maxLength) { if (exceptionType.Length > maxLength && exceptionType.EndsWith("Exception", StringComparison.OrdinalIgnoreCase)) { exceptionType = exceptionType.Substring(0, exceptionType.Length - "Exception".Length); } if (exceptionType.Length > maxLength) { exceptionType = WindowsErrorReporting.TruncateTypeName(exceptionType, maxLength); } return(WindowsErrorReporting.TruncateBucketParameter(exceptionType, maxLength)); }
private static string FindExecutable(string filename) { StringBuilder pathFound = new StringBuilder(1024); IntPtr num = (IntPtr)0; try { num = NativeCommandProcessor.FindExecutableW(filename, string.Empty, pathFound); } catch (IndexOutOfRangeException ex) { WindowsErrorReporting.FailFast((Exception)ex); } return((long)num >= 32L ? pathFound.ToString() : (string)null); }
internal static void CheckForSevereException(Exception e) { if ((e is AccessViolationException) || (e is StackOverflowException)) { try { if (!alreadyFailing) { alreadyFailing = true; MshLog.LogCommandHealthEvent(LocalPipeline.GetExecutionContextFromTLS(), e, Severity.Critical); } } finally { WindowsErrorReporting.FailFast(e); } } }
private static void SetBucketParameters( WindowsErrorReporting.ReportHandle reportHandle, Exception uncaughtException) { Exception exception = uncaughtException; while (exception.InnerException != null) { exception = exception.InnerException; } WindowsErrorReporting.SetBucketParameter(reportHandle, WindowsErrorReporting.BucketParameterId.NameOfExe, WindowsErrorReporting.TruncateExeName(WindowsErrorReporting.nameOfExe, 20)); WindowsErrorReporting.SetBucketParameter(reportHandle, WindowsErrorReporting.BucketParameterId.FileVersionOfSystemManagementAutomation, WindowsErrorReporting.TruncateBucketParameter(WindowsErrorReporting.versionOfPowerShellLibraries, 16)); WindowsErrorReporting.SetBucketParameter(reportHandle, WindowsErrorReporting.BucketParameterId.InnermostExceptionType, WindowsErrorReporting.TruncateExceptionType(exception.GetType().FullName, 40)); WindowsErrorReporting.SetBucketParameter(reportHandle, WindowsErrorReporting.BucketParameterId.OutermostExceptionType, WindowsErrorReporting.TruncateExceptionType(uncaughtException.GetType().FullName, 40)); WindowsErrorReporting.SetBucketParameter(reportHandle, WindowsErrorReporting.BucketParameterId.DeepestFrame, WindowsErrorReporting.GetDeepestFrame(uncaughtException, 50)); WindowsErrorReporting.SetBucketParameter(reportHandle, WindowsErrorReporting.BucketParameterId.DeepestPowerShellFrame, WindowsErrorReporting.GetDeepestPowerShellFrame(uncaughtException, 50)); WindowsErrorReporting.SetBucketParameter(reportHandle, WindowsErrorReporting.BucketParameterId.ThreadName, WindowsErrorReporting.TruncateBucketParameter(WindowsErrorReporting.GetThreadName(), 20)); }
internal static void FailFast(Exception exception) { if (exception == null) { throw new ArgumentNullException(nameof(exception)); } try { if (!WindowsErrorReporting.registered) { return; } WindowsErrorReporting.SubmitReport(exception); } finally { Environment.FailFast(exception.Message); } }
private static string StackFrame2BucketParameter(StackFrame frame, int maxLength) { MethodBase method = frame.GetMethod(); if (method == null) { return(string.Empty); } Type declaringType = method.DeclaringType; if (declaringType == null) { return(WindowsErrorReporting.TruncateBucketParameter(method.Name, maxLength)); } string fullName = declaringType.FullName; string str = "." + method.Name; return(WindowsErrorReporting.TruncateBucketParameter((maxLength <= str.Length ? WindowsErrorReporting.TruncateTypeName(fullName, 1) : WindowsErrorReporting.TruncateTypeName(fullName, maxLength - str.Length)) + str, maxLength)); }
private static string FindExecutable(string filename) { StringBuilder pathFound = new StringBuilder(0x400); IntPtr zero = IntPtr.Zero; try { zero = FindExecutableW(filename, string.Empty, pathFound); } catch (IndexOutOfRangeException exception) { WindowsErrorReporting.FailFast(exception); } if (((long)zero) >= 0x20L) { return(pathFound.ToString()); } return(null); }
private static string GetDeepestPowerShellFrame(Exception exception, int maxLength) { foreach (StackFrame frame in new StackTrace(exception).GetFrames()) { MethodBase method = frame.GetMethod(); if (method != null) { Module module = method.Module; if (module != null) { Type declaringType = method.DeclaringType; if (WindowsErrorReporting.IsPowerShellModule(module.Name, declaringType == null)) { return(WindowsErrorReporting.StackFrame2BucketParameter(frame, maxLength)); } } } } return(string.Empty); }
internal static void RegisterWindowsErrorReporting(bool unattendedServer) { lock (WindowsErrorReporting.registrationLock) { if (WindowsErrorReporting.registered || !WindowsErrorReporting.IsWindowsErrorReportingAvailable()) { return; } WindowsErrorReporting.FindStaticInformation(); WindowsErrorReporting.unattendedServerMode = unattendedServer; if (unattendedServer) { WindowsErrorReporting.HandleHResult(WindowsErrorReporting.NativeMethods.WerSetFlags(WindowsErrorReporting.ReportingFlags.Queue)); } else { WindowsErrorReporting.HandleHResult(WindowsErrorReporting.NativeMethods.WerSetFlags((WindowsErrorReporting.ReportingFlags) 0)); } AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(WindowsErrorReporting.CurrentDomain_UnhandledException); WindowsErrorReporting.registered = true; } }
// Keep in sync: // S.M.A.CommandProcessorBase.CheckForSevereException // S.M.A.Internal.ConsoleHost.CheckForSevereException // S.M.A.Commands.CommandsCommon.CheckForSevereException // S.M.A.Commands.UtilityCommon.CheckForSevereException /// <summary> /// Checks whether the exception is a severe exception which should /// cause immediate process failure. /// </summary> /// <param name="e"></param> /// <remarks> /// CB says 02/23/2005: I personally would err on the side /// of treating OOM like an application exception, rather than /// a critical system failure.I think this will be easier to justify /// in Orcas, if we tease apart the two cases of OOM better. /// But even in Whidbey, how likely is it that we couldnt JIT /// some backout code? At that point, the process or possibly /// the machine is likely to stop executing soon no matter /// what you do in this routine. So I would just consider /// AccessViolationException. (I understand why you have SO here, /// at least temporarily). /// </remarks> internal static void CheckForSevereException(Exception e) { if (e is AccessViolationException || e is StackOverflowException) { try { if (!alreadyFailing) { alreadyFailing = true; // Get the ExecutionContext from the thread. ExecutionContext context = Runspaces.LocalPipeline.GetExecutionContextFromTLS(); // Log a command health event for this critical error. MshLog.LogCommandHealthEvent(context, e, Severity.Critical); } } finally { WindowsErrorReporting.FailFast(e); } } }
internal static extern int WerSetFlags(WindowsErrorReporting.ReportingFlags flags);
internal static extern int WerReportSubmit(WindowsErrorReporting.ReportHandle reportHandle, WindowsErrorReporting.Consent consent, WindowsErrorReporting.SubmitFlags flags, out WindowsErrorReporting.SubmitResult result);
internal static extern int WerReportSetParameter(WindowsErrorReporting.ReportHandle reportHandle, WindowsErrorReporting.BucketParameterId bucketParameterId, [MarshalAs(UnmanagedType.LPWStr)] string name, [MarshalAs(UnmanagedType.LPWStr)] string value);
internal static extern int WerReportCreate([MarshalAs(UnmanagedType.LPWStr)] string pwzEventType, WindowsErrorReporting.ReportType repType, [MarshalAs(UnmanagedType.LPStruct)] WindowsErrorReporting.ReportInformation reportInformation, out WindowsErrorReporting.ReportHandle reportHandle);
internal static extern int WerReportAddDump(WindowsErrorReporting.ReportHandle reportHandle, IntPtr hProcess, IntPtr hThread, WindowsErrorReporting.DumpType dumpType, IntPtr pExceptionParam, IntPtr dumpCustomOptions, WindowsErrorReporting.DumpFlags dumpFlags);
internal static extern bool MiniDumpWriteDump(IntPtr hProcess, int processId, SafeFileHandle hFile, WindowsErrorReporting.MiniDumpType dumpType, IntPtr exceptionParam, IntPtr userStreamParam, IntPtr callackParam);
internal static void WriteMiniDump(string file) => WindowsErrorReporting.WriteMiniDump(file, WindowsErrorReporting.MiniDumpType.MiniDumpNormal);
private static string GetDeepestFrame(Exception exception, int maxLength) => WindowsErrorReporting.StackFrame2BucketParameter(new StackTrace(exception).GetFrame(0), maxLength);