|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | +// See the LICENSE file in the project root for more information. |
| 4 | +using System.Collections.Generic; |
| 5 | +using System.Runtime.CompilerServices; |
| 6 | +using System.Runtime.InteropServices; |
| 7 | +using System.Security; |
| 8 | +using Microsoft.Win32; |
| 9 | + |
| 10 | +namespace System.Diagnostics.Tracing |
| 11 | +{ |
| 12 | + [StructLayout(LayoutKind.Sequential)] |
| 13 | + internal struct EventPipeProviderConfiguration |
| 14 | + { |
| 15 | + [MarshalAs(UnmanagedType.LPWStr)] |
| 16 | + private string m_providerName; |
| 17 | + private UInt64 m_keywords; |
| 18 | + private uint m_loggingLevel; |
| 19 | + |
| 20 | + internal EventPipeProviderConfiguration( |
| 21 | + string providerName, |
| 22 | + UInt64 keywords, |
| 23 | + uint loggingLevel) |
| 24 | + { |
| 25 | + if(string.IsNullOrEmpty(providerName)) |
| 26 | + { |
| 27 | + throw new ArgumentNullException(nameof(providerName)); |
| 28 | + } |
| 29 | + if(loggingLevel > 5) // 5 == Verbose, the highest value in EventPipeLoggingLevel. |
| 30 | + { |
| 31 | + throw new ArgumentOutOfRangeException(nameof(loggingLevel)); |
| 32 | + } |
| 33 | + m_providerName = providerName; |
| 34 | + m_keywords = keywords; |
| 35 | + m_loggingLevel = loggingLevel; |
| 36 | + } |
| 37 | + |
| 38 | + internal string ProviderName |
| 39 | + { |
| 40 | + get { return m_providerName; } |
| 41 | + } |
| 42 | + |
| 43 | + internal UInt64 Keywords |
| 44 | + { |
| 45 | + get { return m_keywords; } |
| 46 | + } |
| 47 | + |
| 48 | + internal uint LoggingLevel |
| 49 | + { |
| 50 | + get { return m_loggingLevel; } |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + internal sealed class EventPipeConfiguration |
| 55 | + { |
| 56 | + private string m_outputFile; |
| 57 | + private uint m_circularBufferSizeInMB; |
| 58 | + private List<EventPipeProviderConfiguration> m_providers; |
| 59 | + |
| 60 | + internal EventPipeConfiguration( |
| 61 | + string outputFile, |
| 62 | + uint circularBufferSizeInMB) |
| 63 | + { |
| 64 | + if(string.IsNullOrEmpty(outputFile)) |
| 65 | + { |
| 66 | + throw new ArgumentNullException(nameof(outputFile)); |
| 67 | + } |
| 68 | + if(circularBufferSizeInMB == 0) |
| 69 | + { |
| 70 | + throw new ArgumentOutOfRangeException(nameof(circularBufferSizeInMB)); |
| 71 | + } |
| 72 | + m_outputFile = outputFile; |
| 73 | + m_circularBufferSizeInMB = circularBufferSizeInMB; |
| 74 | + m_providers = new List<EventPipeProviderConfiguration>(); |
| 75 | + } |
| 76 | + |
| 77 | + internal string OutputFile |
| 78 | + { |
| 79 | + get { return m_outputFile; } |
| 80 | + } |
| 81 | + |
| 82 | + internal uint CircularBufferSizeInMB |
| 83 | + { |
| 84 | + get { return m_circularBufferSizeInMB; } |
| 85 | + } |
| 86 | + |
| 87 | + internal EventPipeProviderConfiguration[] Providers |
| 88 | + { |
| 89 | + get { return m_providers.ToArray(); } |
| 90 | + } |
| 91 | + |
| 92 | + internal void EnableProvider(string providerName, UInt64 keywords, uint loggingLevel) |
| 93 | + { |
| 94 | + m_providers.Add(new EventPipeProviderConfiguration( |
| 95 | + providerName, |
| 96 | + keywords, |
| 97 | + loggingLevel)); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + internal static class EventPipe |
| 102 | + { |
| 103 | + internal static void Enable(EventPipeConfiguration configuration) |
| 104 | + { |
| 105 | + if(configuration == null) |
| 106 | + { |
| 107 | + throw new ArgumentNullException(nameof(configuration)); |
| 108 | + } |
| 109 | + |
| 110 | + EventPipeProviderConfiguration[] providers = configuration.Providers; |
| 111 | + |
| 112 | + EventPipeInternal.Enable( |
| 113 | + configuration.OutputFile, |
| 114 | + configuration.CircularBufferSizeInMB, |
| 115 | + providers, |
| 116 | + providers.Length); |
| 117 | + } |
| 118 | + |
| 119 | + internal static void Disable() |
| 120 | + { |
| 121 | + EventPipeInternal.Disable(); |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + internal static class EventPipeInternal |
| 126 | + { |
| 127 | + // |
| 128 | + // These PInvokes are used by the configuration APIs to interact with EventPipe. |
| 129 | + // |
| 130 | + [DllImport(JitHelpers.QCall, CharSet = CharSet.Unicode)] |
| 131 | + [SuppressUnmanagedCodeSecurity] |
| 132 | + internal static extern void Enable(string outputFile, uint circularBufferSizeInMB, EventPipeProviderConfiguration[] providers, int numProviders); |
| 133 | + |
| 134 | + [DllImport(JitHelpers.QCall, CharSet = CharSet.Unicode)] |
| 135 | + [SuppressUnmanagedCodeSecurity] |
| 136 | + internal static extern void Disable(); |
| 137 | + |
| 138 | + // |
| 139 | + // These PInvokes are used by EventSource to interact with the EventPipe. |
| 140 | + // |
| 141 | + [DllImport(JitHelpers.QCall, CharSet = CharSet.Unicode)] |
| 142 | + [SuppressUnmanagedCodeSecurity] |
| 143 | + internal static extern IntPtr CreateProvider(Guid providerID, UnsafeNativeMethods.ManifestEtw.EtwEnableCallback callbackFunc); |
| 144 | + |
| 145 | + [DllImport(JitHelpers.QCall, CharSet = CharSet.Unicode)] |
| 146 | + [SuppressUnmanagedCodeSecurity] |
| 147 | + internal static extern IntPtr AddEvent(IntPtr provHandle, Int64 keywords, uint eventID, uint eventVersion, uint level, bool needStack); |
| 148 | + |
| 149 | + [DllImport(JitHelpers.QCall, CharSet = CharSet.Unicode)] |
| 150 | + [SuppressUnmanagedCodeSecurity] |
| 151 | + internal static extern void DeleteProvider(IntPtr provHandle); |
| 152 | + |
| 153 | + [DllImport(JitHelpers.QCall, CharSet = CharSet.Unicode)] |
| 154 | + [SuppressUnmanagedCodeSecurity] |
| 155 | + internal static extern unsafe void WriteEvent(IntPtr eventHandle, void* data, uint length); |
| 156 | + } |
| 157 | +} |
0 commit comments