コード例 #1
0
        private void ReadAndDispatchTraceEvent(EventMetadata eventMetadata)
        {
            // Read the thread id.
            var threadId = _deserializer.ReadInt();

            // Read the time stamp.
            var timeStamp = _deserializer.ReadInt64();

            // Ensure that timestamps are properly ordered.
            System.Diagnostics.Debug.Assert(_lastTimestamp <= timeStamp);
            _lastTimestamp = timeStamp;

            // Read the activity id.
            Guid activityId;

            _deserializer.Read(out activityId);

            // Read the related activity id.
            Guid relatedActivityId;

            _deserializer.Read(out relatedActivityId);

            // Read the event payload size.
            var payloadSize = _deserializer.ReadInt();

            Debug.Assert(payloadSize >= 0, "Payload size should not be negative.");

            // Allocate memory for the event payload.
            var payload = new byte[payloadSize];

            for (uint i = 0; i < payloadSize; i++)
            {
                // Copy the payload.
                payload[i] = _deserializer.ReadByte();
            }

            // Dispatch the event.
            Dispatch(SynthesizeTraceEvent(eventMetadata, threadId, timeStamp, activityId, relatedActivityId, payload));

            // Read the optional stack.
            var stackSize = _deserializer.ReadInt();

            Debug.Assert(stackSize >= 0, "Stack size should not be negative.");

            // The book keeping event itself will be discarded.
            // The call stack is not interesting.
            if (IsBookKeepingEvent(eventMetadata))
            {
                _deserializer.Goto(_deserializer.Current + (uint)stackSize);
            }
            else
            {
                if (stackSize > 0)
                {
                    // Allocate memory for the stack if it's not empty
                    var stack = new byte[stackSize];
                    // Copy the stack
                    for (uint i = 0; i < stackSize; i++)
                    {
                        stack[i] = _deserializer.ReadByte();
                    }

                    Dispatch(SynthesizeThreadStackWalkEvent(threadId, stack));
                }
            }
        }
コード例 #2
0
        private void ReadAndUpdateEventMetadataDictionary(StreamLabel eventStart)
        {
            var eventMetadata = new EventMetadata();

            // Skip the EventPipeEvent common fields which are not related to metedata
            int commonFieldSize = sizeof(int)     // ThreadId
                                  + sizeof(Int64) // Timestamp
                                  + sizeof(Guid)  // ActivityId
                                  + sizeof(Guid); // RelatedActivityId

            _deserializer.Goto(_deserializer.Current + (uint)commonFieldSize);

            // Read the event payload size.
            var payloadSize = _deserializer.ReadInt();

            Debug.Assert(payloadSize >= 0, "Payload size should not be negative.");

            if (_version == 1)
            {
                var providerId = Guid.Empty;
                _deserializer.Read(out providerId);
                eventMetadata.ProviderId = providerId;
            }
            else
            {
                eventMetadata.ProviderName = _deserializer.ReadNullTerminatedUnicodeString();
            }

            eventMetadata.EventId = (uint)_deserializer.ReadInt();
            eventMetadata.Version = (uint)_deserializer.ReadInt();
            var metadataPayloadLength = (uint)_deserializer.ReadInt();

            if (metadataPayloadLength > 0)
            {
                var payloadIdentifierLength = 0;
                if (_version == 1)
                {
                    payloadIdentifierLength = sizeof(Guid);
                }
                else
                {
                    payloadIdentifierLength = (eventMetadata.ProviderName.Length + 1) * sizeof(Char); // +1 for null-terminator
                }
                var actualPayloadSize = payloadIdentifierLength
                                        + sizeof(int) // EventId
                                        + sizeof(int) // Version
                                        + sizeof(int) // MetadataPayloadLength
                                        + metadataPayloadLength;
                Debug.Assert(payloadSize == actualPayloadSize, $"The total event payload size is {actualPayloadSize}. But it is expected to be {payloadSize}.");

                // Read EventSource definition: https://github.com/dotnet/coreclr/blob/release/2.0.0/src/mscorlib/shared/System/Diagnostics/Tracing/EventSource.cs#L708
                eventMetadata.EventId   = (uint)_deserializer.ReadInt();
                eventMetadata.EventName = _deserializer.ReadNullTerminatedUnicodeString();
                eventMetadata.Keywords  = (ulong)_deserializer.ReadInt64();
                eventMetadata.Version   = (uint)_deserializer.ReadInt();
                eventMetadata.Level     = (uint)_deserializer.ReadInt();

                int parameterCount = _deserializer.ReadInt();
                Debug.Assert(parameterCount >= 0, "Parameter count should not be negative.");

                if (parameterCount > 0)
                {
                    eventMetadata.ParameterDefinitions = new Tuple <TypeCode, string> [parameterCount];
                    for (int i = 0; i < parameterCount; i++)
                    {
                        var type = (uint)_deserializer.ReadInt();
                        var name = _deserializer.ReadNullTerminatedUnicodeString();
                        eventMetadata.ParameterDefinitions[i] = new Tuple <TypeCode, string>((TypeCode)type, name);
                    }
                }

                // add a new template to event parser
                _eventParser.AddTemplate(eventMetadata);
            }

            // Add the new event meta data into dictionary
            _eventMetadataDictionary.Add(eventStart, eventMetadata);

            // Read and verify stack size
            _deserializer.ReadIntAndVerify(0);
        }