예제 #1
0
        LogEvent ReadEvent()
        {
            var type      = Reader.ReadByte();
            var basicType = (LogEventType)(type & 0xf);
            var extType   = (LogEventType)(type & 0xf0);

            _time = ReadTime();
            LogEvent ev = null;

            switch (basicType)
            {
            case LogEventType.Allocation:
                switch (extType)
                {
                case LogEventType.AllocationBacktrace:
                case LogEventType.AllocationNoBacktrace:
                    ev = new AllocationEvent {
                        ClassPointer  = ReadPointer(),
                        ObjectPointer = ReadObject(),
                        ObjectSize    = (long)Reader.ReadULeb128(),
                        Backtrace     = ReadBacktrace(extType == LogEventType.AllocationBacktrace),
                    };
                    break;

                default:
                    throw new LogException($"Invalid extended event type ({extType}).");
                }
                break;

            case LogEventType.GC:
                switch (extType)
                {
                case LogEventType.GCEvent:
                    ev = new GCEvent {
                        Type       = (LogGCEvent)Reader.ReadByte(),
                        Generation = Reader.ReadByte(),
                    };
                    break;

                case LogEventType.GCResize:
                    ev = new GCResizeEvent {
                        NewSize = (long)Reader.ReadULeb128(),
                    };
                    break;

                case LogEventType.GCMove: {
                    var list = new long [(int)Reader.ReadULeb128()];

                    for (var i = 0; i < list.Length; i++)
                    {
                        list [i] = ReadObject();
                    }

                    ev = new GCMoveEvent {
                        OldObjectPointers = list.Where((_, i) => i % 2 == 0).ToArray(),
                        NewObjectPointers = list.Where((_, i) => i % 2 != 0).ToArray(),
                    };
                    break;
                }

                case LogEventType.GCHandleCreationNoBacktrace:
                case LogEventType.GCHandleCreationBacktrace:
                    ev = new GCHandleCreationEvent {
                        Type          = (LogGCHandleType)Reader.ReadULeb128(),
                        Handle        = (long)Reader.ReadULeb128(),
                        ObjectPointer = ReadObject(),
                        Backtrace     = ReadBacktrace(extType == LogEventType.GCHandleCreationBacktrace),
                    };
                    break;

                case LogEventType.GCHandleDeletionNoBacktrace:
                case LogEventType.GCHandleDeletionBacktrace:
                    ev = new GCHandleDeletionEvent {
                        Type      = (LogGCHandleType)Reader.ReadULeb128(),
                        Handle    = (long)Reader.ReadULeb128(),
                        Backtrace = ReadBacktrace(extType == LogEventType.GCHandleDeletionBacktrace),
                    };
                    break;

                case LogEventType.GCFinalizeBegin:
                    ev = new GCFinalizeBeginEvent();
                    break;

                case LogEventType.GCFinalizeEnd:
                    ev = new GCFinalizeEndEvent();
                    break;

                case LogEventType.GCFinalizeObjectBegin:
                    ev = new GCFinalizeObjectBeginEvent {
                        ObjectPointer = ReadObject(),
                    };
                    break;

                case LogEventType.GCFinalizeObjectEnd:
                    ev = new GCFinalizeObjectEndEvent {
                        ObjectPointer = ReadObject(),
                    };
                    break;

                default:
                    throw new LogException($"Invalid extended event type ({extType}).");
                }
                break;

            case LogEventType.Metadata: {
                var load   = false;
                var unload = false;

                switch (extType)
                {
                case LogEventType.MetadataExtra:
                    break;

                case LogEventType.MetadataEndLoad:
                    load = true;
                    break;

                case LogEventType.MetadataEndUnload:
                    unload = true;
                    break;

                default:
                    throw new LogException($"Invalid extended event type ({extType}).");
                }

                var metadataType = (LogMetadataType)Reader.ReadByte();

                switch (metadataType)
                {
                case LogMetadataType.Class:
                    if (load)
                    {
                        ev = new ClassLoadEvent {
                            ClassPointer = ReadPointer(),
                            ImagePointer = ReadPointer(),
                            Name         = Reader.ReadCString(),
                        };
                    }
                    else
                    {
                        throw new LogException("Invalid class metadata event.");
                    }
                    break;

                case LogMetadataType.Image:
                    if (load)
                    {
                        ev = new ImageLoadEvent {
                            ImagePointer = ReadPointer(),
                            Name         = Reader.ReadCString(),
                        };
                    }
                    else if (unload)
                    {
                        ev = new ImageUnloadEvent {
                            ImagePointer = ReadPointer(),
                            Name         = Reader.ReadCString(),
                        };
                    }
                    else
                    {
                        throw new LogException("Invalid image metadata event.");
                    }
                    break;

                case LogMetadataType.Assembly:
                    if (load)
                    {
                        ev = new AssemblyLoadEvent {
                            AssemblyPointer = ReadPointer(),
                            ImagePointer    = ReadPointer(),
                            Name            = Reader.ReadCString(),
                        };
                    }
                    else if (unload)
                    {
                        ev = new AssemblyUnloadEvent {
                            AssemblyPointer = ReadPointer(),
                            ImagePointer    = ReadPointer(),
                            Name            = Reader.ReadCString(),
                        };
                    }
                    else
                    {
                        throw new LogException("Invalid assembly metadata event.");
                    }
                    break;

                case LogMetadataType.AppDomain:
                    if (load)
                    {
                        ev = new AppDomainLoadEvent {
                            AppDomainId = ReadPointer(),
                        };
                    }
                    else if (unload)
                    {
                        ev = new AppDomainUnloadEvent {
                            AppDomainId = ReadPointer(),
                        };
                    }
                    else
                    {
                        ev = new AppDomainNameEvent {
                            AppDomainId = ReadPointer(),
                            Name        = Reader.ReadCString(),
                        };
                    }
                    break;

                case LogMetadataType.Thread:
                    if (load)
                    {
                        ev = new ThreadStartEvent {
                            ThreadId = ReadPointer(),
                        };
                    }
                    else if (unload)
                    {
                        ev = new ThreadEndEvent {
                            ThreadId = ReadPointer(),
                        };
                    }
                    else
                    {
                        ev = new ThreadNameEvent {
                            ThreadId = ReadPointer(),
                            Name     = Reader.ReadCString(),
                        };
                    }
                    break;

                case LogMetadataType.Context:
                    if (load)
                    {
                        ev = new ContextLoadEvent {
                            ContextId   = ReadPointer(),
                            AppDomainId = ReadPointer(),
                        };
                    }
                    else if (unload)
                    {
                        ev = new ContextUnloadEvent {
                            ContextId   = ReadPointer(),
                            AppDomainId = ReadPointer(),
                        };
                    }
                    else
                    {
                        throw new LogException("Invalid context metadata event.");
                    }
                    break;

                default:
                    throw new LogException($"Invalid metadata type ({metadataType}).");
                }
                break;
            }

            case LogEventType.Method:
                switch (extType)
                {
                case LogEventType.MethodLeave:
                    ev = new LeaveEvent {
                        MethodPointer = ReadMethod(),
                    };
                    break;

                case LogEventType.MethodEnter:
                    ev = new EnterEvent {
                        MethodPointer = ReadMethod(),
                    };
                    break;

                case LogEventType.MethodLeaveExceptional:
                    ev = new ExceptionalLeaveEvent {
                        MethodPointer = ReadMethod(),
                    };
                    break;

                case LogEventType.MethodJit:
                    ev = new JitEvent {
                        MethodPointer = ReadMethod(),
                        CodePointer   = ReadPointer(),
                        CodeSize      = (long)Reader.ReadULeb128(),
                        Name          = Reader.ReadCString(),
                    };
                    break;

                default:
                    throw new LogException($"Invalid extended event type ({extType}).");
                }
                break;

            case LogEventType.Exception:
                switch (extType)
                {
                case LogEventType.ExceptionThrowNoBacktrace:
                case LogEventType.ExceptionThrowBacktrace:
                    ev = new ThrowEvent {
                        ObjectPointer = ReadObject(),
                        Backtrace     = ReadBacktrace(extType == LogEventType.ExceptionThrowBacktrace),
                    };
                    break;

                case LogEventType.ExceptionClause:
                    ev = new ExceptionClauseEvent {
                        Type          = (LogExceptionClause)Reader.ReadByte(),
                        Index         = (long)Reader.ReadULeb128(),
                        MethodPointer = ReadMethod(),
                        ObjectPointer = ReadObject(),
                    };
                    break;

                default:
                    throw new LogException($"Invalid extended event type ({extType}).");
                }
                break;

            case LogEventType.Monitor:
                switch (extType)
                {
                case LogEventType.MonitorNoBacktrace:
                case LogEventType.MonitorBacktrace:
                    ev = new MonitorEvent {
                        Event         = (LogMonitorEvent)Reader.ReadByte(),
                        ObjectPointer = ReadObject(),
                        Backtrace     = ReadBacktrace(extType == LogEventType.MonitorBacktrace),
                    };
                    break;

                default:
                    throw new LogException($"Invalid extended event type ({extType}).");
                }
                break;

            case LogEventType.Heap:
                switch (extType)
                {
                case LogEventType.HeapBegin:
                    ev = new HeapBeginEvent();
                    break;

                case LogEventType.HeapEnd:
                    ev = new HeapEndEvent();
                    break;

                case LogEventType.HeapObject: {
                    HeapObjectEvent hoe = new HeapObjectEvent {
                        ObjectPointer = ReadObject(),
                        ClassPointer  = ReadPointer(),
                        ObjectSize    = (long)Reader.ReadULeb128(),
                    };

                    var list = new HeapObjectEvent.HeapObjectReference [(int)Reader.ReadULeb128()];

                    for (var i = 0; i < list.Length; i++)
                    {
                        list [i] = new HeapObjectEvent.HeapObjectReference {
                            Offset        = (long)Reader.ReadULeb128(),
                            ObjectPointer = ReadObject(),
                        };
                    }

                    hoe.References = list;
                    ev             = hoe;

                    break;
                }

                case LogEventType.HeapRoots: {
                    // TODO: This entire event makes no sense.
                    var hre  = new HeapRootsEvent();
                    var list = new HeapRootsEvent.HeapRoot [(int)Reader.ReadULeb128()];

                    hre.MaxGenerationCollectionCount = (long)Reader.ReadULeb128();

                    for (var i = 0; i < list.Length; i++)
                    {
                        list [i] = new HeapRootsEvent.HeapRoot {
                            ObjectPointer = ReadObject(),
                            Attributes    = (LogHeapRootAttributes)Reader.ReadULeb128(),
                            ExtraInfo     = (long)Reader.ReadULeb128(),
                        };
                    }

                    hre.Roots = list;
                    ev        = hre;

                    break;
                }

                default:
                    throw new LogException($"Invalid extended event type ({extType}).");
                }
                break;

            case LogEventType.Sample:
                switch (extType)
                {
                case LogEventType.SampleHit:
                    ev = new SampleHitEvent {
                        ThreadId           = ReadPointer(),
                        UnmanagedBacktrace = ReadBacktrace(true, false),
                        ManagedBacktrace   = ReadBacktrace(true),
                    };
                    break;

                case LogEventType.SampleUnmanagedSymbol:
                    ev = new UnmanagedSymbolEvent {
                        CodePointer = ReadPointer(),
                        CodeSize    = (long)Reader.ReadULeb128(),
                        Name        = Reader.ReadCString(),
                    };
                    break;

                case LogEventType.SampleUnmanagedBinary:
                    ev = new UnmanagedBinaryEvent {
                        SegmentPointer = ReadPointer(),
                        SegmentOffset  = (long)Reader.ReadULeb128(),
                        SegmentSize    = (long)Reader.ReadULeb128(),
                        FileName       = Reader.ReadCString(),
                    };
                    break;

                case LogEventType.SampleCounterDescriptions: {
                    var cde  = new CounterDescriptionsEvent();
                    var list = new CounterDescriptionsEvent.CounterDescription [(int)Reader.ReadULeb128()];

                    for (var i = 0; i < list.Length; i++)
                    {
                        var section = (LogCounterSection)Reader.ReadULeb128();

                        list [i] = new CounterDescriptionsEvent.CounterDescription {
                            Section     = section,
                            SectionName = section == LogCounterSection.User ? Reader.ReadCString() : string.Empty,
                            CounterName = Reader.ReadCString(),
                            Type        = (LogCounterType)Reader.ReadByte(),
                            Unit        = (LogCounterUnit)Reader.ReadByte(),
                            Variance    = (LogCounterVariance)Reader.ReadByte(),
                            Index       = (long)Reader.ReadULeb128(),
                        };
                    }

                    cde.Descriptions = list;
                    ev = cde;

                    break;
                }

                case LogEventType.SampleCounters: {
                    var cse  = new CounterSamplesEvent();
                    var list = new List <CounterSamplesEvent.CounterSample> ();

                    while (true)
                    {
                        var index = (long)Reader.ReadULeb128();

                        if (index == 0)
                        {
                            break;
                        }

                        var counterType = (LogCounterType)Reader.ReadByte();

                        object value = null;

                        switch (counterType)
                        {
                        case LogCounterType.String:
                            value = Reader.ReadByte() == 1 ? Reader.ReadCString() : null;
                            break;

                        case LogCounterType.Int32:
                        case LogCounterType.Word:
                        case LogCounterType.Int64:
                        case LogCounterType.Interval:
                            value = Reader.ReadSLeb128();
                            break;

                        case LogCounterType.UInt32:
                        case LogCounterType.UInt64:
                            value = Reader.ReadULeb128();
                            break;

                        case LogCounterType.Double:
                            value = Reader.ReadDouble();
                            break;

                        default:
                            throw new LogException($"Invalid counter type ({counterType}).");
                        }

                        list.Add(new CounterSamplesEvent.CounterSample {
                                Index = index,
                                Type  = counterType,
                                Value = value,
                            });
                    }

                    cse.Samples = list;
                    ev          = cse;

                    break;
                }

                default:
                    throw new LogException($"Invalid extended event type ({extType}).");
                }
                break;

            case LogEventType.Runtime:
                switch (extType)
                {
                case LogEventType.RuntimeJitHelper: {
                    var helperType = (LogJitHelper)Reader.ReadByte();

                    ev = new JitHelperEvent {
                        Type          = helperType,
                        BufferPointer = ReadPointer(),
                        BufferSize    = (long)Reader.ReadULeb128(),
                        Name          = helperType == LogJitHelper.SpecificTrampoline ? Reader.ReadCString() : string.Empty,
                    };
                    break;
                }

                default:
                    throw new LogException($"Invalid extended event type ({extType}).");
                }
                break;

            case LogEventType.Meta:
                switch (extType)
                {
                case LogEventType.MetaSynchronizationPoint:
                    ev = new SynchronizationPointEvent {
                        Type = (LogSynchronizationPoint)Reader.ReadByte(),
                    };
                    break;

                default:
                    throw new LogException($"Invalid extended event type ({extType}).");
                }
                break;

            default:
                throw new LogException($"Invalid basic event type ({basicType}).");
            }

            ev.Timestamp = _time;
            ev.Buffer    = _bufferHeader;

            return(ev);
        }