Exemplo n.º 1
0
        /// <summary>
        /// Adds frames and binaries from this exception and all inner exceptions to the given telemetry result.
        /// </summary>
        /// <param name="exception">The exception to examine.</param>
        /// <param name="telemetry">The telemetry object to add the frames to.</param>
        /// <param name="seenBinaries">The set of binaries we have already added to the frame list.</param>
        /// <param name="exceptionMessages">The list of exception types and messages seen.</param>
        /// <remarks>This will produce a flat list with no indication of which frame belongs to which exception. This is a workaround
        /// for the fact that the server side only knows how to process a flat list.</remarks>
        private static void AddExceptionInformation(Exception exception, CrashTelemetry telemetry, HashSet <long> seenBinaries, StringBuilder exceptionMessages)
        {
            if (exception.InnerException != null)
            {
                var aggregateException = exception as AggregateException;
                if (aggregateException != null)
                {
                    foreach (Exception innerException in aggregateException.InnerExceptions)
                    {
                        AddExceptionInformation(innerException, telemetry, seenBinaries, exceptionMessages);
                    }
                }
                else
                {
                    AddExceptionInformation(exception.InnerException, telemetry, seenBinaries, exceptionMessages);
                }
            }

            exceptionMessages.AppendLine(string.Format(CultureInfo.CurrentCulture, "{0}: {1}", exception.GetType().FullName, exception.Message));

            StackTrace stackTrace = new StackTrace(exception, true);
            var        frames     = stackTrace.GetFrames();

            // stackTrace.GetFrames may return null (happened on Outlook Groups application).
            // HasNativeImage() method invoke on first frame is required to understand whether an application is compiled in native tool chain
            // and we can extract the frame addresses or not.
            if (frames != null && frames.Length > 0 && frames[0].HasNativeImage())
            {
                foreach (StackFrame frame in stackTrace.GetFrames())
                {
                    CrashTelemetryThreadFrame crashFrame = new CrashTelemetryThreadFrame
                    {
                        Address = string.Format(CultureInfo.InvariantCulture, "0x{0:x16}", frame.GetNativeIP().ToInt64())
                    };

                    telemetry.Threads[0].Frames.Add(crashFrame);
                    long nativeImageBase = frame.GetNativeImageBase().ToInt64();
                    if (seenBinaries.Contains(nativeImageBase) || nativeImageBase == 0)
                    {
                        continue;
                    }

                    PEImageReader reader = new PEImageReader(frame.GetNativeImageBase());
                    PEImageReader.CodeViewDebugData codeView = reader.Parse();
                    if (codeView == null)
                    {
                        continue;
                    }

                    CrashTelemetryBinary crashBinary = new CrashTelemetryBinary
                    {
                        StartAddress                     = string.Format(CultureInfo.InvariantCulture, "0x{0:x16}", nativeImageBase),
                        EndAddress                       = string.Format(CultureInfo.InvariantCulture, "0x{0:x16}", codeView.EndAddress.ToInt64()),
                        Uuid                             = string.Format(CultureInfo.InvariantCulture, "{0:N}-{1}", codeView.Signature, codeView.Age),
                        Path                             = codeView.PdbPath,
                        Name                             = string.IsNullOrEmpty(codeView.PdbPath) == false?Path.GetFileNameWithoutExtension(codeView.PdbPath) : null,
                                                 CpuType = GetProcessorArchitecture()
                    };

                    telemetry.Binaries.Add(crashBinary);
                    seenBinaries.Add(nativeImageBase);
                }
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Creates <see cref="CrashTelemetry"/> instance.
        /// </summary>
        /// <param name="exception">The exception to initialize the class with.</param>
        /// <param name="handledAt">Determines whether exception is handled or unhandled.</param>
        public ITelemetry CreateCrashTelemetry(Exception exception, ExceptionHandledAt handledAt)
        {
            CrashTelemetry result = new CrashTelemetry();

            result.HandledAt               = handledAt;
            result.Headers.Id              = Guid.NewGuid().ToString("D");
            result.Headers.CrashThreadId   = Environment.CurrentManagedThreadId;
            result.Headers.ExceptionType   = exception.GetType().FullName;
            result.Headers.ExceptionReason = exception.Message;

            var description = string.Empty;

            if (HockeyClient.Current.AsInternal().DescriptionLoader != null)
            {
                try
                {
                    result.Attachments.Description = HockeyClient.Current.AsInternal().DescriptionLoader(exception);
                }
                catch (Exception ex)
                {
                    CoreEventSource.Log.LogError("An exception occured in TelemetryConfiguration.Active.DescriptionLoader callback : " + ex);
                }
            }

            CrashTelemetryThread thread = new CrashTelemetryThread {
                Id = Environment.CurrentManagedThreadId
            };

            result.Threads.Add(thread);
            HashSet <long> seenBinaries = new HashSet <long>();

            StackTrace stackTrace = new StackTrace(exception, true);
            var        frames     = stackTrace.GetFrames();

            // stackTrace.GetFrames may return null (happened on Outlook Groups application).
            // HasNativeImage() method invoke on first frame is required to understand whether an application is compiled in native tool chain
            // and we can extract the frame addresses or not.
            if (frames != null && frames.Length > 0 && frames[0].HasNativeImage())
            {
                foreach (StackFrame frame in stackTrace.GetFrames())
                {
                    CrashTelemetryThreadFrame crashFrame = new CrashTelemetryThreadFrame
                    {
                        Address = string.Format(CultureInfo.InvariantCulture, "0x{0:x16}", frame.GetNativeIP().ToInt64())
                    };

                    thread.Frames.Add(crashFrame);
                    long nativeImageBase = frame.GetNativeImageBase().ToInt64();
                    if (seenBinaries.Contains(nativeImageBase) == true)
                    {
                        continue;
                    }

                    PEImageReader reader = new PEImageReader(frame.GetNativeImageBase());
                    PEImageReader.CodeViewDebugData codeView = reader.Parse();
                    if (codeView == null)
                    {
                        continue;
                    }

                    CrashTelemetryBinary crashBinary = new CrashTelemetryBinary
                    {
                        StartAddress                     = string.Format(CultureInfo.InvariantCulture, "0x{0:x16}", nativeImageBase),
                        EndAddress                       = string.Format(CultureInfo.InvariantCulture, "0x{0:x16}", codeView.EndAddress.ToInt64()),
                        Uuid                             = string.Format(CultureInfo.InvariantCulture, "{0:N}-{1}", codeView.Signature, codeView.Age),
                        Path                             = codeView.PdbPath,
                        Name                             = string.IsNullOrEmpty(codeView.PdbPath) == false?Path.GetFileNameWithoutExtension(codeView.PdbPath) : null,
                                                 CpuType = GetProcessorArchitecture()
                    };

                    result.Binaries.Add(crashBinary);
                    seenBinaries.Add(nativeImageBase);
                }
            }

            result.StackTrace = GetStrackTrace(exception);
            return(result);
        }