internal unsafe bool WriteEventRaw(ref EventDescriptor eventDescriptor, Guid *activityID, Guid *relatedActivityID, int dataCount, IntPtr data)
        {
            int num = UnsafeNativeMethods.ManifestEtw.EventWriteTransferWrapper(this.m_regHandle, ref eventDescriptor, activityID, relatedActivityID, dataCount, (EventProvider.EventData *)((void *)data));

            if (num != 0)
            {
                EventProvider.SetLastError(num);
                return(false);
            }
            return(true);
        }
Beispiel #2
0
        public IntPtr GetOrCreateEventHandle(EventProvider provider, TraceLoggingEventHandleTable eventHandleTable, EventDescriptor descriptor, TraceLoggingEventTypes eventTypes)
        {
            IntPtr eventHandle;

            if ((eventHandle = eventHandleTable[descriptor.EventId]) == IntPtr.Zero)
            {
                lock (eventHandleTable)
                {
                    if ((eventHandle = eventHandleTable[descriptor.EventId]) == IntPtr.Zero)
                    {
                        byte[]? metadata = EventPipeMetadataGenerator.Instance.GenerateEventMetadata(
                            descriptor.EventId,
                            name,
                            (EventKeywords)descriptor.Keywords,
                            (EventLevel)descriptor.Level,
                            descriptor.Version,
                            (EventOpcode)descriptor.Opcode,
                            eventTypes);
                        uint metadataLength = (metadata != null) ? (uint)metadata.Length : 0;

                        unsafe
                        {
                            fixed(byte *pMetadataBlob = metadata)
                            {
                                // Define the event.
                                eventHandle = provider.m_eventProvider.DefineEventHandle(
                                    (uint)descriptor.EventId,
                                    name,
                                    descriptor.Keywords,
                                    descriptor.Version,
                                    descriptor.Level,
                                    pMetadataBlob,
                                    metadataLength);
                            }
                        }

                        // Cache the event handle.
                        eventHandleTable.SetEventHandle(descriptor.EventId, eventHandle);
                    }
                }
            }

            return(eventHandle);
        }
        // Token: 0x06003343 RID: 13123 RVA: 0x000C2DE0 File Offset: 0x000C0FE0
        private static void GetSessionInfoCallback(int etwSessionId, long matchAllKeywords, ref List <EventProvider.SessionInfo> sessionList)
        {
            uint n = (uint)SessionMask.FromEventKeywords((ulong)matchAllKeywords);

            if (EventProvider.bitcount(n) > 1)
            {
                return;
            }
            if (sessionList == null)
            {
                sessionList = new List <EventProvider.SessionInfo>(8);
            }
            if (EventProvider.bitcount(n) == 1)
            {
                sessionList.Add(new EventProvider.SessionInfo(EventProvider.bitindex(n) + 1, etwSessionId));
                return;
            }
            sessionList.Add(new EventProvider.SessionInfo(EventProvider.bitcount((uint)SessionMask.All) + 1, etwSessionId));
        }
Beispiel #4
0
        private List <Tuple <EventProvider.SessionInfo, bool> > GetSessions()
        {
            List <EventProvider.SessionInfo> liveSessionList = (List <EventProvider.SessionInfo>)null;

            this.GetSessionInfo((Action <int, long>)((etwSessionId, matchAllKeywords) => EventProvider.GetSessionInfoCallback(etwSessionId, matchAllKeywords, ref liveSessionList)));
            List <Tuple <EventProvider.SessionInfo, bool> > tupleList = new List <Tuple <EventProvider.SessionInfo, bool> >();

            if (this.m_liveSessions != null)
            {
                foreach (EventProvider.SessionInfo mLiveSession in this.m_liveSessions)
                {
                    int index;
                    if ((index = EventProvider.IndexOfSessionInList(liveSessionList, mLiveSession.etwSessionId)) < 0 || liveSessionList[index].sessionIdBit != mLiveSession.sessionIdBit)
                    {
                        tupleList.Add(Tuple.Create <EventProvider.SessionInfo, bool>(mLiveSession, false));
                    }
                }
            }
            if (liveSessionList != null)
            {
                foreach (EventProvider.SessionInfo sessionInfo in liveSessionList)
                {
                    int index;
                    if ((index = EventProvider.IndexOfSessionInList(this.m_liveSessions, sessionInfo.etwSessionId)) < 0 || this.m_liveSessions[index].sessionIdBit != sessionInfo.sessionIdBit)
                    {
                        tupleList.Add(Tuple.Create <EventProvider.SessionInfo, bool>(sessionInfo, true));
                    }
                }
            }
            this.m_liveSessions = liveSessionList;
            return(tupleList);
        }
Beispiel #5
0
 private unsafe void EtwEnableCallBack([In] ref Guid sourceId, [In] int controlCode, [In] byte setLevel, [In] long anyKeyword, [In] long allKeyword, [In] UnsafeNativeMethods.ManifestEtw.EVENT_FILTER_DESCRIPTOR *filterData, [In] void *callbackContext)
 {
     try
     {
         ControllerCommand            command   = ControllerCommand.Update;
         IDictionary <string, string> arguments = (IDictionary <string, string>)null;
         bool flag1 = false;
         if (controlCode == 1)
         {
             this.m_enabled        = true;
             this.m_level          = setLevel;
             this.m_anyKeywordMask = anyKeyword;
             this.m_allKeywordMask = allKeyword;
             List <Tuple <EventProvider.SessionInfo, bool> > sessions = this.GetSessions();
             foreach (Tuple <EventProvider.SessionInfo, bool> tuple in sessions)
             {
                 int  num1         = tuple.Item1.sessionIdBit;
                 int  etwSessionId = tuple.Item1.etwSessionId;
                 bool flag2        = tuple.Item2;
                 flag1     = true;
                 arguments = (IDictionary <string, string>)null;
                 if (sessions.Count > 1)
                 {
                     filterData = (UnsafeNativeMethods.ManifestEtw.EVENT_FILTER_DESCRIPTOR *)null;
                 }
                 byte[] data;
                 int    dataStart;
                 if (flag2 && this.GetDataFromController(etwSessionId, filterData, out command, out data, out dataStart))
                 {
                     arguments = (IDictionary <string, string>) new Dictionary <string, string>(4);
                     int null1;
                     for (; dataStart < data.Length; dataStart = null1 + 1)
                     {
                         int null2 = EventProvider.FindNull(data, dataStart);
                         int num2  = null2 + 1;
                         null1 = EventProvider.FindNull(data, num2);
                         if (null1 < data.Length)
                         {
                             string string1 = Encoding.UTF8.GetString(data, dataStart, null2 - dataStart);
                             string string2 = Encoding.UTF8.GetString(data, num2, null1 - num2);
                             arguments[string1] = string2;
                         }
                     }
                 }
                 this.OnControllerCommand(command, arguments, flag2 ? num1 : -num1, etwSessionId);
             }
         }
         else if (controlCode == 0)
         {
             this.m_enabled        = false;
             this.m_level          = (byte)0;
             this.m_anyKeywordMask = 0L;
             this.m_allKeywordMask = 0L;
             this.m_liveSessions   = (List <EventProvider.SessionInfo>)null;
         }
         else
         {
             if (controlCode != 2)
             {
                 return;
             }
             command = ControllerCommand.SendManifest;
         }
         if (flag1)
         {
             return;
         }
         this.OnControllerCommand(command, arguments, 0, 0);
     }
     catch (Exception ex)
     {
     }
 }
        internal unsafe bool WriteEvent(ref EventDescriptor eventDescriptor, Guid *activityID, Guid *childActivityID, params object[] eventPayload)
        {
            int num = 0;

            if (this.IsEnabled(eventDescriptor.Level, eventDescriptor.Keywords))
            {
                int num2 = eventPayload.Length;
                if (num2 > 128)
                {
                    EventProvider.s_returnCode = EventProvider.WriteEventErrorCode.TooManyArgs;
                    return(false);
                }
                uint                     num3  = 0U;
                int                      i     = 0;
                List <int>               list  = new List <int>(8);
                List <object>            list2 = new List <object>(8);
                EventProvider.EventData *ptr;
                EventProvider.EventData *ptr2;
                byte *                   ptr4;
                bool                     flag;
                checked
                {
                    ptr  = stackalloc EventProvider.EventData[unchecked ((UIntPtr)(2 * num2)) * (UIntPtr)sizeof(EventProvider.EventData)];
                    ptr2 = ptr;
                    byte *ptr3 = stackalloc byte[unchecked ((UIntPtr)(32 * num2)) * 1];
                    ptr4 = ptr3;
                    flag = false;
                }
                for (int j = 0; j < eventPayload.Length; j++)
                {
                    if (eventPayload[j] == null)
                    {
                        EventProvider.s_returnCode = EventProvider.WriteEventErrorCode.NullInput;
                        return(false);
                    }
                    object obj = EventProvider.EncodeObject(ref eventPayload[j], ref ptr2, ref ptr4, ref num3);
                    if (obj != null)
                    {
                        int num4 = (int)((long)(ptr2 - ptr) - 1L);
                        if (!(obj is string))
                        {
                            if (eventPayload.Length + num4 + 1 - j > 128)
                            {
                                EventProvider.s_returnCode = EventProvider.WriteEventErrorCode.TooManyArgs;
                                return(false);
                            }
                            flag = true;
                        }
                        list2.Add(obj);
                        list.Add(num4);
                        i++;
                    }
                }
                num2 = (int)((long)(ptr2 - ptr));
                if (num3 > 65482U)
                {
                    EventProvider.s_returnCode = EventProvider.WriteEventErrorCode.EventTooBig;
                    return(false);
                }
                if (!flag && i < 8)
                {
                    while (i < 8)
                    {
                        list2.Add(null);
                        i++;
                    }

                    fixed(string text = (string)list2[0])
                    {
                        char *ptr5 = text;

                        if (ptr5 != null)
                        {
                            ptr5 += RuntimeHelpers.OffsetToStringData / 2;
                        }

                        fixed(string text2 = (string)list2[1])
                        {
                            char *ptr6 = text2;

                            if (ptr6 != null)
                            {
                                ptr6 += RuntimeHelpers.OffsetToStringData / 2;
                            }

                            fixed(string text3 = (string)list2[2])
                            {
                                char *ptr7 = text3;

                                if (ptr7 != null)
                                {
                                    ptr7 += RuntimeHelpers.OffsetToStringData / 2;
                                }

                                fixed(string text4 = (string)list2[3])
                                {
                                    char *ptr8 = text4;

                                    if (ptr8 != null)
                                    {
                                        ptr8 += RuntimeHelpers.OffsetToStringData / 2;
                                    }

                                    fixed(string text5 = (string)list2[4])
                                    {
                                        char *ptr9 = text5;

                                        if (ptr9 != null)
                                        {
                                            ptr9 += RuntimeHelpers.OffsetToStringData / 2;
                                        }

                                        fixed(string text6 = (string)list2[5])
                                        {
                                            char *ptr10 = text6;

                                            if (ptr10 != null)
                                            {
                                                ptr10 += RuntimeHelpers.OffsetToStringData / 2;
                                            }

                                            fixed(string text7 = (string)list2[6])
                                            {
                                                char *ptr11 = text7;

                                                if (ptr11 != null)
                                                {
                                                    ptr11 += RuntimeHelpers.OffsetToStringData / 2;
                                                }

                                                fixed(string text8 = (string)list2[7])
                                                {
                                                    char *ptr12 = text8;

                                                    if (ptr12 != null)
                                                    {
                                                        ptr12 += RuntimeHelpers.OffsetToStringData / 2;
                                                    }
                                                    ptr2 = ptr;
                                                    if (list2[0] != null)
                                                    {
                                                        ptr2[list[0]].Ptr = ptr5;
                                                    }
                                                    if (list2[1] != null)
                                                    {
                                                        ptr2[list[1]].Ptr = ptr6;
                                                    }
                                                    if (list2[2] != null)
                                                    {
                                                        ptr2[list[2]].Ptr = ptr7;
                                                    }
                                                    if (list2[3] != null)
                                                    {
                                                        ptr2[list[3]].Ptr = ptr8;
                                                    }
                                                    if (list2[4] != null)
                                                    {
                                                        ptr2[list[4]].Ptr = ptr9;
                                                    }
                                                    if (list2[5] != null)
                                                    {
                                                        ptr2[list[5]].Ptr = ptr10;
                                                    }
                                                    if (list2[6] != null)
                                                    {
                                                        ptr2[list[6]].Ptr = ptr11;
                                                    }
                                                    if (list2[7] != null)
                                                    {
                                                        ptr2[list[7]].Ptr = ptr12;
                                                    }
                                                    num   = UnsafeNativeMethods.ManifestEtw.EventWriteTransferWrapper(this.m_regHandle, ref eventDescriptor, activityID, childActivityID, num2, ptr);
                                                    text  = null;
                                                    text2 = null;
                                                    text3 = null;
                                                    text4 = null;
                                                    text5 = null;
                                                    text6 = null;
                                                    text7 = null;
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                else
                {
                    ptr2 = ptr;
                    GCHandle[] array = new GCHandle[i];
                    for (int k = 0; k < i; k++)
                    {
                        array[k] = GCHandle.Alloc(list2[k], GCHandleType.Pinned);
                        if (list2[k] is string)
                        {
                            fixed(string text9 = (string)list2[k])
                            {
                                char *ptr13 = text9;

                                if (ptr13 != null)
                                {
                                    ptr13 += RuntimeHelpers.OffsetToStringData / 2;
                                }
                                ptr2[list[k]].Ptr = ptr13;
                            }
                        }
                        else
                        {
                            fixed(byte *ptr14 = (byte[])list2[k])
                            {
                                ptr2[list[k]].Ptr = ptr14;
                            }
                        }
                    }
                    num = UnsafeNativeMethods.ManifestEtw.EventWriteTransferWrapper(this.m_regHandle, ref eventDescriptor, activityID, childActivityID, num2, ptr);
                    for (int l = 0; l < i; l++)
                    {
                        array[l].Free();
                    }
                }
            }
            if (num != 0)
            {
                EventProvider.SetLastError(num);
                return(false);
            }
            return(true);
        }
        private unsafe void EtwEnableCallBack([In] ref Guid sourceId, [In] int controlCode, [In] byte setLevel, [In] long anyKeyword, [In] long allKeyword, [In] UnsafeNativeMethods.ManifestEtw.EVENT_FILTER_DESCRIPTOR *filterData, [In] void *callbackContext)
        {
            try
            {
                ControllerCommand            command    = ControllerCommand.Update;
                IDictionary <string, string> dictionary = null;
                bool flag = false;
                if (controlCode == 1)
                {
                    this.m_enabled        = true;
                    this.m_level          = setLevel;
                    this.m_anyKeywordMask = anyKeyword;
                    this.m_allKeywordMask = allKeyword;
                    List <Tuple <EventProvider.SessionInfo, bool> > sessions = this.GetSessions();
                    using (List <Tuple <EventProvider.SessionInfo, bool> > .Enumerator enumerator = sessions.GetEnumerator())
                    {
                        while (enumerator.MoveNext())
                        {
                            Tuple <EventProvider.SessionInfo, bool> tuple = enumerator.Current;
                            int  sessionIdBit = tuple.Item1.sessionIdBit;
                            int  etwSessionId = tuple.Item1.etwSessionId;
                            bool item         = tuple.Item2;
                            flag       = true;
                            dictionary = null;
                            if (sessions.Count > 1)
                            {
                                filterData = null;
                            }
                            byte[] array;
                            int    i;
                            if (item && this.GetDataFromController(etwSessionId, filterData, out command, out array, out i))
                            {
                                dictionary = new Dictionary <string, string>(4);
                                while (i < array.Length)
                                {
                                    int num  = EventProvider.FindNull(array, i);
                                    int num2 = num + 1;
                                    int num3 = EventProvider.FindNull(array, num2);
                                    if (num3 < array.Length)
                                    {
                                        string @string = Encoding.UTF8.GetString(array, i, num - i);
                                        string string2 = Encoding.UTF8.GetString(array, num2, num3 - num2);
                                        dictionary[@string] = string2;
                                    }
                                    i = num3 + 1;
                                }
                            }
                            this.OnControllerCommand(command, dictionary, item ? sessionIdBit : (-sessionIdBit), etwSessionId);
                        }
                        goto IL_162;
                    }
                }
                if (controlCode == 0)
                {
                    this.m_enabled        = false;
                    this.m_level          = 0;
                    this.m_anyKeywordMask = 0L;
                    this.m_allKeywordMask = 0L;
                    this.m_liveSessions   = null;
                }
                else
                {
                    if (controlCode != 2)
                    {
                        return;
                    }
                    command = ControllerCommand.SendManifest;
                }
IL_162:
                if (!flag)
                {
                    this.OnControllerCommand(command, dictionary, 0, 0);
                }
            }
            catch (Exception)
            {
            }
        }
Beispiel #8
0
            /// <summary>
            ///  Call the ETW native API EventWriteTransfer and checks for invalid argument error. 
            ///  The implementation of EventWriteTransfer on some older OSes (Windows 2008) does not accept null relatedActivityId.
            ///  So, for these cases we will retry the call with an empty Guid.
            /// </summary>
            internal static int EventWriteTransferWrapper(long registrationHandle,
                                                         ref EventDescriptor eventDescriptor,
                                                         Guid* activityId,
                                                         Guid* relatedActivityId,
                                                         int userDataCount,
                                                         EventProvider.EventData* userData)
            {
                int HResult = EventWriteTransfer(registrationHandle, ref eventDescriptor, activityId, relatedActivityId, userDataCount, userData);
                if (HResult == ERROR_INVALID_PARAMETER && relatedActivityId == null)
                {
                    Guid emptyGuid = Guid.Empty;
                    HResult = EventWriteTransfer(registrationHandle, ref eventDescriptor, activityId, &emptyGuid, userDataCount, userData);
                }

                return HResult;
            }