From 1510ce6276619bb6a8529e74e303e873d50a702b Mon Sep 17 00:00:00 2001 From: Filip Navara Date: Sun, 14 Apr 2019 22:50:50 +0200 Subject: [PATCH] Move EventSource to shared CoreLib partition Commit migrated from https://github.com/dotnet/corefx/commit/00279f8fd11a96f57b8a56855d313f9e2c9bb513 --- .../src/ManifestEtw.cs | 216 ------------------ ...t.Diagnostics.Tracing.EventSource.Redist.csproj | 13 +- .../src/RuntimeSpecific.cs | 245 --------------------- 3 files changed, 12 insertions(+), 462 deletions(-) delete mode 100644 src/libraries/Microsoft.Diagnostics.Tracing.EventSource.Redist/src/ManifestEtw.cs delete mode 100644 src/libraries/Microsoft.Diagnostics.Tracing.EventSource.Redist/src/RuntimeSpecific.cs diff --git a/src/libraries/Microsoft.Diagnostics.Tracing.EventSource.Redist/src/ManifestEtw.cs b/src/libraries/Microsoft.Diagnostics.Tracing.EventSource.Redist/src/ManifestEtw.cs deleted file mode 100644 index abc4933..0000000 --- a/src/libraries/Microsoft.Diagnostics.Tracing.EventSource.Redist/src/ManifestEtw.cs +++ /dev/null @@ -1,216 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. - -namespace Microsoft.Win32 -{ - using Microsoft.Win32.SafeHandles; - using System; -#if ES_BUILD_STANDALONE - using Microsoft.Diagnostics.Tracing; -#else - using System.Diagnostics.Tracing; -#endif - using System.Runtime.InteropServices; - using System.Security; - using System.Text; - - [SuppressUnmanagedCodeSecurityAttribute()] - internal static partial class UnsafeNativeMethods - { - [SuppressUnmanagedCodeSecurityAttribute()] - internal static unsafe class ManifestEtw - { -// Disable warning about unused fields as these structures are defined by the corresponding Windows APIs. -#pragma warning disable CS0649 - // - // ETW Library - // - private const string ADVAPI32 = "advapi32.dll"; - - // - // Constants error coded returned by ETW APIs - // - - // The event size is larger than the allowed maximum (64k - header). - internal const int ERROR_ARITHMETIC_OVERFLOW = 534; - - // Occurs when filled buffers are trying to flush to disk, - // but disk IOs are not happening fast enough. - // This happens when the disk is slow and event traffic is heavy. - // Eventually, there are no more free (empty) buffers and the event is dropped. - internal const int ERROR_NOT_ENOUGH_MEMORY = 8; - - internal const int ERROR_MORE_DATA = 0xEA; - internal const int ERROR_NOT_SUPPORTED = 50; - internal const int ERROR_INVALID_PARAMETER = 0x57; - - // - // ETW Methods - // - - internal const int EVENT_CONTROL_CODE_DISABLE_PROVIDER = 0; - internal const int EVENT_CONTROL_CODE_ENABLE_PROVIDER = 1; - internal const int EVENT_CONTROL_CODE_CAPTURE_STATE = 2; - - // - // Callback - // - internal unsafe delegate void EtwEnableCallback( - [In] ref Guid sourceId, - [In] int isEnabled, - [In] byte level, - [In] long matchAnyKeywords, - [In] long matchAllKeywords, - [In] EVENT_FILTER_DESCRIPTOR* filterData, - [In] void* callbackContext - ); - - // - // Registration APIs - // - [DllImport(ADVAPI32, ExactSpelling = true, EntryPoint = "EventRegister", CharSet = System.Runtime.InteropServices.CharSet.Unicode)] - internal static extern unsafe uint EventRegister( - [In] ref Guid providerId, - [In]EtwEnableCallback enableCallback, - [In]void* callbackContext, - [In][Out]ref long registrationHandle - ); - - // - [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2118:ReviewSuppressUnmanagedCodeSecurityUsage")] - [DllImport(ADVAPI32, ExactSpelling = true, EntryPoint = "EventUnregister", CharSet = System.Runtime.InteropServices.CharSet.Unicode)] - internal static extern uint EventUnregister([In] long registrationHandle); - - [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2118:ReviewSuppressUnmanagedCodeSecurityUsage")] - [DllImport(ADVAPI32, ExactSpelling = true, EntryPoint = "EventWriteString", CharSet = System.Runtime.InteropServices.CharSet.Unicode)] - internal static extern unsafe int EventWriteString( - [In] long registrationHandle, - [In] byte level, - [In] long keyword, - [In] string msg - ); - - [StructLayout(LayoutKind.Sequential)] - unsafe internal struct EVENT_FILTER_DESCRIPTOR - { - public long Ptr; - public int Size; - public int Type; - }; - - /// - /// 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. - /// - 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; - } - - [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2118:ReviewSuppressUnmanagedCodeSecurityUsage")] - [DllImport(ADVAPI32, ExactSpelling = true, EntryPoint = "EventWriteTransfer", CharSet = System.Runtime.InteropServices.CharSet.Unicode)] - [SuppressUnmanagedCodeSecurityAttribute] // Don't do security checks - private static extern int EventWriteTransfer( - [In] long registrationHandle, - [In] ref EventDescriptor eventDescriptor, - [In] Guid* activityId, - [In] Guid* relatedActivityId, - [In] int userDataCount, - [In] EventProvider.EventData* userData - ); - - internal enum ActivityControl : uint - { - EVENT_ACTIVITY_CTRL_GET_ID = 1, - EVENT_ACTIVITY_CTRL_SET_ID = 2, - EVENT_ACTIVITY_CTRL_CREATE_ID = 3, - EVENT_ACTIVITY_CTRL_GET_SET_ID = 4, - EVENT_ACTIVITY_CTRL_CREATE_SET_ID = 5 - }; - - [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2118:ReviewSuppressUnmanagedCodeSecurityUsage")] - [DllImport(ADVAPI32, ExactSpelling = true, EntryPoint = "EventActivityIdControl", CharSet = System.Runtime.InteropServices.CharSet.Unicode)] - [SuppressUnmanagedCodeSecurityAttribute] // Don't do security checks - internal static extern int EventActivityIdControl([In] ActivityControl ControlCode, [In][Out] ref Guid ActivityId); - - internal enum EVENT_INFO_CLASS - { - BinaryTrackInfo, - SetEnableAllKeywords, - SetTraits, - } - - [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2118:ReviewSuppressUnmanagedCodeSecurityUsage")] - [DllImport(ADVAPI32, ExactSpelling = true, EntryPoint = "EventSetInformation", CharSet = System.Runtime.InteropServices.CharSet.Unicode)] - [SuppressUnmanagedCodeSecurityAttribute] // Don't do security checks - internal static extern int EventSetInformation( - [In] long registrationHandle, - [In] EVENT_INFO_CLASS informationClass, - [In] void* eventInformation, - [In] int informationLength); - - // Support for EnumerateTraceGuidsEx - internal enum TRACE_QUERY_INFO_CLASS - { - TraceGuidQueryList, - TraceGuidQueryInfo, - TraceGuidQueryProcess, - TraceStackTracingInfo, - MaxTraceSetInfoClass - }; - - internal struct TRACE_GUID_INFO - { - public int InstanceCount; - public int Reserved; - }; - - internal struct TRACE_PROVIDER_INSTANCE_INFO - { - public int NextOffset; - public int EnableCount; - public int Pid; - public int Flags; - }; - - internal struct TRACE_ENABLE_INFO - { - public int IsEnabled; - public byte Level; - public byte Reserved1; - public ushort LoggerId; - public int EnableProperty; - public int Reserved2; - public long MatchAnyKeyword; - public long MatchAllKeyword; - }; - - [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2118:ReviewSuppressUnmanagedCodeSecurityUsage")] - [DllImport(ADVAPI32, ExactSpelling = true, EntryPoint = "EnumerateTraceGuidsEx", CharSet = System.Runtime.InteropServices.CharSet.Unicode)] - [SuppressUnmanagedCodeSecurityAttribute] // Don't do security checks - internal static extern int EnumerateTraceGuidsEx( - TRACE_QUERY_INFO_CLASS TraceQueryInfoClass, - void* InBuffer, - int InBufferSize, - void* OutBuffer, - int OutBufferSize, - ref int ReturnLength); - -#pragma warning restore CS0649 - } - } -} diff --git a/src/libraries/Microsoft.Diagnostics.Tracing.EventSource.Redist/src/Microsoft.Diagnostics.Tracing.EventSource.Redist.csproj b/src/libraries/Microsoft.Diagnostics.Tracing.EventSource.Redist/src/Microsoft.Diagnostics.Tracing.EventSource.Redist.csproj index 153b687..4507b8a 100644 --- a/src/libraries/Microsoft.Diagnostics.Tracing.EventSource.Redist/src/Microsoft.Diagnostics.Tracing.EventSource.Redist.csproj +++ b/src/libraries/Microsoft.Diagnostics.Tracing.EventSource.Redist/src/Microsoft.Diagnostics.Tracing.EventSource.Redist.csproj @@ -12,7 +12,18 @@ - + + + + + + + + + + + + diff --git a/src/libraries/Microsoft.Diagnostics.Tracing.EventSource.Redist/src/RuntimeSpecific.cs b/src/libraries/Microsoft.Diagnostics.Tracing.EventSource.Redist/src/RuntimeSpecific.cs deleted file mode 100644 index cc14f9b..0000000 --- a/src/libraries/Microsoft.Diagnostics.Tracing.EventSource.Redist/src/RuntimeSpecific.cs +++ /dev/null @@ -1,245 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. - -using System; -using System.Runtime.InteropServices; -using System.Threading.Tasks; -using Microsoft.Reflection; -using Microsoft.Win32; - -#if ES_BUILD_STANDALONE -namespace Microsoft.Diagnostics.Tracing -#else -namespace System.Diagnostics.Tracing -#endif -{ - public partial class EventSource - { -#if FEATURE_MANAGED_ETW && FEATURE_PERFTRACING - // For non-Windows, we use a thread-local variable to hold the activity ID. - // On Windows, ETW has it's own thread-local variable and we participate in its use. - [ThreadStatic] - private static Guid s_currentThreadActivityId; -#endif // FEATURE_MANAGED_ETW && FEATURE_PERFTRACING - - // ActivityID support (see also WriteEventWithRelatedActivityIdCore) - /// - /// When a thread starts work that is on behalf of 'something else' (typically another - /// thread or network request) it should mark the thread as working on that other work. - /// This API marks the current thread as working on activity 'activityID'. This API - /// should be used when the caller knows the thread's current activity (the one being - /// overwritten) has completed. Otherwise, callers should prefer the overload that - /// return the oldActivityThatWillContinue (below). - /// - /// All events created with the EventSource on this thread are also tagged with the - /// activity ID of the thread. - /// - /// It is common, and good practice after setting the thread to an activity to log an event - /// with a 'start' opcode to indicate that precise time/thread where the new activity - /// started. - /// - /// A Guid that represents the new activity with which to mark - /// the current thread - public static void SetCurrentThreadActivityId(Guid activityId) - { - if (TplEventSource.Log != null) - TplEventSource.Log.SetActivityId(activityId); -#if FEATURE_MANAGED_ETW -#if FEATURE_ACTIVITYSAMPLING - Guid newId = activityId; -#endif // FEATURE_ACTIVITYSAMPLING - // We ignore errors to keep with the convention that EventSources do not throw errors. - // Note we can't access m_throwOnWrites because this is a static method. - -#if FEATURE_PERFTRACING - s_currentThreadActivityId = activityId; -#elif PLATFORM_WINDOWS - if (UnsafeNativeMethods.ManifestEtw.EventActivityIdControl( - UnsafeNativeMethods.ManifestEtw.ActivityControl.EVENT_ACTIVITY_CTRL_GET_SET_ID, - ref activityId) == 0) -#endif // FEATURE_PERFTRACING - { -#if FEATURE_ACTIVITYSAMPLING - var activityDying = s_activityDying; - if (activityDying != null && newId != activityId) - { - if (activityId == Guid.Empty) - { - activityId = FallbackActivityId; - } - // OutputDebugString(string.Format("Activity dying: {0} -> {1}", activityId, newId)); - activityDying(activityId); // This is actually the OLD activity ID. - } -#endif // FEATURE_ACTIVITYSAMPLING - } -#endif // FEATURE_MANAGED_ETW - } - - /// - /// When a thread starts work that is on behalf of 'something else' (typically another - /// thread or network request) it should mark the thread as working on that other work. - /// This API marks the current thread as working on activity 'activityID'. It returns - /// whatever activity the thread was previously marked with. There is a convention that - /// callers can assume that callees restore this activity mark before the callee returns. - /// To encourage this, this API returns the old activity, so that it can be restored later. - /// - /// All events created with the EventSource on this thread are also tagged with the - /// activity ID of the thread. - /// - /// It is common, and good practice after setting the thread to an activity to log an event - /// with a 'start' opcode to indicate that precise time/thread where the new activity - /// started. - /// - /// A Guid that represents the new activity with which to mark - /// the current thread - /// The Guid that represents the current activity - /// which will continue at some point in the future, on the current thread - public static void SetCurrentThreadActivityId(Guid activityId, out Guid oldActivityThatWillContinue) - { - oldActivityThatWillContinue = activityId; -#if FEATURE_MANAGED_ETW - // We ignore errors to keep with the convention that EventSources do not throw errors. - // Note we can't access m_throwOnWrites because this is a static method. - -#if FEATURE_PERFTRACING - oldActivityThatWillContinue = s_currentThreadActivityId; - s_currentThreadActivityId = activityId; -#elif PLATFORM_WINDOWS - UnsafeNativeMethods.ManifestEtw.EventActivityIdControl( - UnsafeNativeMethods.ManifestEtw.ActivityControl.EVENT_ACTIVITY_CTRL_GET_SET_ID, - ref oldActivityThatWillContinue); -#endif // FEATURE_PERFTRACING -#endif // FEATURE_MANAGED_ETW - - // We don't call the activityDying callback here because the caller has declared that - // it is not dying. - if (TplEventSource.Log != null) - TplEventSource.Log.SetActivityId(activityId); - } - - /// - /// Retrieves the ETW activity ID associated with the current thread. - /// - public static Guid CurrentThreadActivityId - { - get - { - // We ignore errors to keep with the convention that EventSources do not throw - // errors. Note we can't access m_throwOnWrites because this is a static method. - Guid retVal = new Guid(); -#if FEATURE_MANAGED_ETW -#if FEATURE_PERFTRACING - retVal = s_currentThreadActivityId; -#elif PLATFORM_WINDOWS - UnsafeNativeMethods.ManifestEtw.EventActivityIdControl( - UnsafeNativeMethods.ManifestEtw.ActivityControl.EVENT_ACTIVITY_CTRL_GET_ID, - ref retVal); -#endif // FEATURE_PERFTRACING -#endif // FEATURE_MANAGED_ETW - return retVal; - } - } - - private int GetParameterCount(EventMetadata eventData) - { - return eventData.Parameters.Length; - } - - private Type GetDataType(EventMetadata eventData, int parameterId) - { - return eventData.Parameters[parameterId].ParameterType; - } - - private static string GetResourceString(string key, params object[] args) - { - return string.Format(Resources.GetResourceString(key), args); - } - - private static readonly bool m_EventSourcePreventRecursion = false; - } - - internal partial class ManifestBuilder - { - private string GetTypeNameHelper(Type type) - { - switch (type.GetTypeCode()) - { - case TypeCode.Boolean: - return "win:Boolean"; - case TypeCode.Byte: - return "win:UInt8"; - case TypeCode.Char: - case TypeCode.UInt16: - return "win:UInt16"; - case TypeCode.UInt32: - return "win:UInt32"; - case TypeCode.UInt64: - return "win:UInt64"; - case TypeCode.SByte: - return "win:Int8"; - case TypeCode.Int16: - return "win:Int16"; - case TypeCode.Int32: - return "win:Int32"; - case TypeCode.Int64: - return "win:Int64"; - case TypeCode.String: - return "win:UnicodeString"; - case TypeCode.Single: - return "win:Float"; - case TypeCode.Double: - return "win:Double"; - case TypeCode.DateTime: - return "win:FILETIME"; - default: - if (type == typeof(Guid)) - return "win:GUID"; - else if (type == typeof(IntPtr)) - return "win:Pointer"; - else if ((type.IsArray || type.IsPointer) && type.GetElementType() == typeof(byte)) - return "win:Binary"; - - ManifestError(SR.Format(SR.EventSource_UnsupportedEventTypeInManifest, type.Name), true); - return string.Empty; - } - } - } - - internal partial class EventProvider - { - internal unsafe int SetInformation( - UnsafeNativeMethods.ManifestEtw.EVENT_INFO_CLASS eventInfoClass, - IntPtr data, - uint dataSize) - { - int status = UnsafeNativeMethods.ManifestEtw.ERROR_NOT_SUPPORTED; - - if (!m_setInformationMissing) - { - try - { - status = UnsafeNativeMethods.ManifestEtw.EventSetInformation( - m_regHandle, - eventInfoClass, - (void*)data, - (int)dataSize); - } - catch (TypeLoadException) - { - m_setInformationMissing = true; - } - } - - return status; - } - } - - internal static class Resources - { - internal static string GetResourceString(string key, params object[] args) - { - return string.Format(Resources.GetResourceString(key), args); - } - } -} -- 2.7.4