diff --git a/eng/Version.Details.xml b/eng/Version.Details.xml index 442156328906d3..263da61086f5b5 100644 --- a/eng/Version.Details.xml +++ b/eng/Version.Details.xml @@ -46,9 +46,9 @@ https://github.com/dotnet/arcade 71b580038fb704df63e03c6b7ae7d2c6a4fdd71d - + https://github.com/dotnet/arcade - 71b580038fb704df63e03c6b7ae7d2c6a4fdd71d + b91c34a908efe4d95ac487a33b88521c76c6c3cf https://github.com/dotnet/arcade diff --git a/eng/Versions.props b/eng/Versions.props index 9cf7f5a06a922d..406d1dacad9f20 100644 --- a/eng/Versions.props +++ b/eng/Versions.props @@ -59,7 +59,7 @@ 5.0.0-beta.20316.1 2.5.1-beta.20316.1 5.0.0-beta.20316.1 - 5.0.0-beta.20316.1 + 5.0.0-beta.20325.16 5.0.0-beta.20316.1 5.0.0-preview.4.20202.18 diff --git a/src/libraries/System.Diagnostics.Tracing/tests/BasicEventSourceTest/TestNotSupported.cs b/src/libraries/System.Diagnostics.Tracing/tests/BasicEventSourceTest/TestNotSupported.cs new file mode 100644 index 00000000000000..01338e8aea8c7d --- /dev/null +++ b/src/libraries/System.Diagnostics.Tracing/tests/BasicEventSourceTest/TestNotSupported.cs @@ -0,0 +1,51 @@ +// 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.Collections.Generic; +using Xunit; +using SdtEventSources; +using Microsoft.DotNet.RemoteExecutor; +#if USE_MDT_EVENTSOURCE +using Microsoft.Diagnostics.Tracing; +#else +using System.Diagnostics.Tracing; +#endif + +namespace BasicEventSourceTests +{ + public class TestNotSupported + { + /// + /// Tests using EventSource when the System.Diagnostics.Tracing.EventSource.IsSupported + /// feature switch is set to disable all EventSource operations. + /// + [ConditionalFact(typeof(RemoteExecutor), nameof(RemoteExecutor.IsSupported))] + public void TestBasicOperations_IsSupported_False() + { + RemoteInvokeOptions options = new RemoteInvokeOptions(); + options.RuntimeConfigurationOptions.Add("System.Diagnostics.Tracing.EventSource.IsSupported", false); + + RemoteExecutor.Invoke(() => + { + using (var log = new EventSourceTest()) + { + using (var listener = new LoudListener(log)) + { + IEnumerable sources = EventSource.GetSources(); + Assert.Empty(sources); + + Assert.Null(EventSource.GenerateManifest(typeof(SimpleEventSource), string.Empty, EventManifestOptions.OnlyIfNeededForRegistration)); + Assert.Null(EventSource.GenerateManifest(typeof(EventSourceTest), string.Empty, EventManifestOptions.AllCultures)); + + log.Event0(); + Assert.Null(LoudListener.LastEvent); + + EventSource.SendCommand(log, EventCommand.Enable, commandArguments: null); + Assert.False(log.IsEnabled()); + } + } + }, options).Dispose(); + } + } +} diff --git a/src/libraries/System.Diagnostics.Tracing/tests/System.Diagnostics.Tracing.Tests.csproj b/src/libraries/System.Diagnostics.Tracing/tests/System.Diagnostics.Tracing.Tests.csproj index 35c33d0095d48d..29df8cd940d035 100644 --- a/src/libraries/System.Diagnostics.Tracing/tests/System.Diagnostics.Tracing.Tests.csproj +++ b/src/libraries/System.Diagnostics.Tracing/tests/System.Diagnostics.Tracing.Tests.csproj @@ -22,6 +22,7 @@ + diff --git a/src/libraries/System.Private.CoreLib/src/ILLink/ILLink.Substitutions.Shared.xml b/src/libraries/System.Private.CoreLib/src/ILLink/ILLink.Substitutions.Shared.xml index 3af7868a176f86..44149875d2b0a4 100644 --- a/src/libraries/System.Private.CoreLib/src/ILLink/ILLink.Substitutions.Shared.xml +++ b/src/libraries/System.Private.CoreLib/src/ILLink/ILLink.Substitutions.Shared.xml @@ -1,5 +1,11 @@ + + + + + + diff --git a/src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/EventSource.cs b/src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/EventSource.cs index 7216b349a61333..9853437a153e33 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/EventSource.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/EventSource.cs @@ -233,6 +233,11 @@ public partial class EventSource : IDisposable #pragma warning restore CA1823 #endif //FEATURE_EVENTSOURCE_XPLAT + private static bool IsSupported { get; } = InitializeIsSupported(); + + private static bool InitializeIsSupported() => + AppContext.TryGetSwitch("System.Diagnostics.Tracing.EventSource.IsSupported", out bool isSupported) ? isSupported : true; + /// /// The human-friendly name of the eventSource. It defaults to the simple name of the class /// @@ -277,7 +282,7 @@ public bool IsEnabled(EventLevel level, EventKeywords keywords) /// public bool IsEnabled(EventLevel level, EventKeywords keywords, EventChannel channel) { - if (!m_eventSourceEnabled) + if (!IsEnabled()) return false; if (!IsEnabledCommon(m_eventSourceEnabled, m_level, m_matchAnyKeyword, level, keywords, channel)) @@ -381,6 +386,11 @@ public static string GetName(Type eventSourceType) string? assemblyPathToIncludeInManifest, EventManifestOptions flags) { + if (!IsSupported) + { + return null; + } + if (eventSourceType == null) throw new ArgumentNullException(nameof(eventSourceType)); @@ -395,6 +405,11 @@ public static string GetName(Type eventSourceType) /// public static IEnumerable GetSources() { + if (!IsSupported) + { + return Array.Empty(); + } + var ret = new List(); lock (EventListener.EventListenersLock) { @@ -420,6 +435,11 @@ public static IEnumerable GetSources() /// A set of (name-argument, value-argument) pairs associated with the command public static void SendCommand(EventSource eventSource, EventCommand command, IDictionary? commandArguments) { + if (!IsSupported) + { + return; + } + if (eventSource == null) throw new ArgumentNullException(nameof(eventSource)); @@ -522,6 +542,11 @@ public event EventHandler? EventCommandExecuted /// the current thread public static void SetCurrentThreadActivityId(Guid activityId) { + if (!IsSupported) + { + return; + } + if (TplEventSource.Log != null) TplEventSource.Log.SetActivityId(activityId); @@ -550,6 +575,11 @@ public static Guid CurrentThreadActivityId { get { + if (!IsSupported) + { + return default; + } + // 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 = default; @@ -589,6 +619,12 @@ public static Guid CurrentThreadActivityId /// which will continue at some point in the future, on the current thread public static void SetCurrentThreadActivityId(Guid activityId, out Guid oldActivityThatWillContinue) { + if (!IsSupported) + { + oldActivityThatWillContinue = default; + return; + } + oldActivityThatWillContinue = activityId; #if FEATURE_MANAGED_ETW // We ignore errors to keep with the convention that EventSources do not throw errors. @@ -663,13 +699,19 @@ protected EventSource(EventSourceSettings settings) : this(settings, null) { } /// A collection of key-value strings (must be an even number). protected EventSource(EventSourceSettings settings, params string[]? traits) { - m_config = ValidateSettings(settings); + if (IsSupported) + { +#if FEATURE_PERFTRACING + m_eventHandleTable = new TraceLoggingEventHandleTable(); +#endif + m_config = ValidateSettings(settings); - Type myType = this.GetType(); - Guid eventSourceGuid = GetGuid(myType); - string eventSourceName = GetName(myType); + Type myType = this.GetType(); + Guid eventSourceGuid = GetGuid(myType); + string eventSourceName = GetName(myType); - Initialize(eventSourceGuid, eventSourceName, traits); + Initialize(eventSourceGuid, eventSourceName, traits); + } } #if FEATURE_PERFTRACING @@ -734,7 +776,7 @@ protected unsafe void WriteEvent(int eventId) // optimized for common signatures (ints) protected unsafe void WriteEvent(int eventId, int arg1) { - if (m_eventSourceEnabled) + if (IsEnabled()) { EventSource.EventData* descrs = stackalloc EventSource.EventData[1]; descrs[0].DataPointer = (IntPtr)(&arg1); @@ -746,7 +788,7 @@ protected unsafe void WriteEvent(int eventId, int arg1) protected unsafe void WriteEvent(int eventId, int arg1, int arg2) { - if (m_eventSourceEnabled) + if (IsEnabled()) { EventSource.EventData* descrs = stackalloc EventSource.EventData[2]; descrs[0].DataPointer = (IntPtr)(&arg1); @@ -761,7 +803,7 @@ protected unsafe void WriteEvent(int eventId, int arg1, int arg2) protected unsafe void WriteEvent(int eventId, int arg1, int arg2, int arg3) { - if (m_eventSourceEnabled) + if (IsEnabled()) { EventSource.EventData* descrs = stackalloc EventSource.EventData[3]; descrs[0].DataPointer = (IntPtr)(&arg1); @@ -780,7 +822,7 @@ protected unsafe void WriteEvent(int eventId, int arg1, int arg2, int arg3) // optimized for common signatures (longs) protected unsafe void WriteEvent(int eventId, long arg1) { - if (m_eventSourceEnabled) + if (IsEnabled()) { EventSource.EventData* descrs = stackalloc EventSource.EventData[1]; descrs[0].DataPointer = (IntPtr)(&arg1); @@ -792,7 +834,7 @@ protected unsafe void WriteEvent(int eventId, long arg1) protected unsafe void WriteEvent(int eventId, long arg1, long arg2) { - if (m_eventSourceEnabled) + if (IsEnabled()) { EventSource.EventData* descrs = stackalloc EventSource.EventData[2]; descrs[0].DataPointer = (IntPtr)(&arg1); @@ -807,7 +849,7 @@ protected unsafe void WriteEvent(int eventId, long arg1, long arg2) protected unsafe void WriteEvent(int eventId, long arg1, long arg2, long arg3) { - if (m_eventSourceEnabled) + if (IsEnabled()) { EventSource.EventData* descrs = stackalloc EventSource.EventData[3]; descrs[0].DataPointer = (IntPtr)(&arg1); @@ -826,7 +868,7 @@ protected unsafe void WriteEvent(int eventId, long arg1, long arg2, long arg3) // optimized for common signatures (strings) protected unsafe void WriteEvent(int eventId, string? arg1) { - if (m_eventSourceEnabled) + if (IsEnabled()) { arg1 ??= ""; fixed (char* string1Bytes = arg1) @@ -842,7 +884,7 @@ protected unsafe void WriteEvent(int eventId, string? arg1) protected unsafe void WriteEvent(int eventId, string? arg1, string? arg2) { - if (m_eventSourceEnabled) + if (IsEnabled()) { arg1 ??= ""; arg2 ??= ""; @@ -863,7 +905,7 @@ protected unsafe void WriteEvent(int eventId, string? arg1, string? arg2) protected unsafe void WriteEvent(int eventId, string? arg1, string? arg2, string? arg3) { - if (m_eventSourceEnabled) + if (IsEnabled()) { arg1 ??= ""; arg2 ??= ""; @@ -890,7 +932,7 @@ protected unsafe void WriteEvent(int eventId, string? arg1, string? arg2, string // optimized for common signatures (string and ints) protected unsafe void WriteEvent(int eventId, string? arg1, int arg2) { - if (m_eventSourceEnabled) + if (IsEnabled()) { arg1 ??= ""; fixed (char* string1Bytes = arg1) @@ -909,7 +951,7 @@ protected unsafe void WriteEvent(int eventId, string? arg1, int arg2) protected unsafe void WriteEvent(int eventId, string? arg1, int arg2, int arg3) { - if (m_eventSourceEnabled) + if (IsEnabled()) { arg1 ??= ""; fixed (char* string1Bytes = arg1) @@ -932,7 +974,7 @@ protected unsafe void WriteEvent(int eventId, string? arg1, int arg2, int arg3) // optimized for common signatures (string and longs) protected unsafe void WriteEvent(int eventId, string? arg1, long arg2) { - if (m_eventSourceEnabled) + if (IsEnabled()) { arg1 ??= ""; fixed (char* string1Bytes = arg1) @@ -952,7 +994,7 @@ protected unsafe void WriteEvent(int eventId, string? arg1, long arg2) // optimized for common signatures (long and string) protected unsafe void WriteEvent(int eventId, long arg1, string? arg2) { - if (m_eventSourceEnabled) + if (IsEnabled()) { arg2 ??= ""; fixed (char* string2Bytes = arg2) @@ -972,7 +1014,7 @@ protected unsafe void WriteEvent(int eventId, long arg1, string? arg2) // optimized for common signatures (int and string) protected unsafe void WriteEvent(int eventId, int arg1, string? arg2) { - if (m_eventSourceEnabled) + if (IsEnabled()) { arg2 ??= ""; fixed (char* string2Bytes = arg2) @@ -991,7 +1033,7 @@ protected unsafe void WriteEvent(int eventId, int arg1, string? arg2) protected unsafe void WriteEvent(int eventId, byte[]? arg1) { - if (m_eventSourceEnabled) + if (IsEnabled()) { EventSource.EventData* descrs = stackalloc EventSource.EventData[2]; if (arg1 == null || arg1.Length == 0) @@ -1024,7 +1066,7 @@ protected unsafe void WriteEvent(int eventId, byte[]? arg1) protected unsafe void WriteEvent(int eventId, long arg1, byte[]? arg2) { - if (m_eventSourceEnabled) + if (IsEnabled()) { EventSource.EventData* descrs = stackalloc EventSource.EventData[3]; descrs[0].DataPointer = (IntPtr)(&arg1); @@ -1179,7 +1221,7 @@ protected unsafe void WriteEventCore(int eventId, int eventDataCount, EventSourc [CLSCompliant(false)] protected unsafe void WriteEventWithRelatedActivityIdCore(int eventId, Guid* relatedActivityId, int eventDataCount, EventSource.EventData* data) { - if (m_eventSourceEnabled) + if (IsEnabled()) { Debug.Assert(m_eventData != null); // You must have initialized this if you enabled the source. try @@ -1311,6 +1353,11 @@ public void Dispose() /// True if called from Dispose(), false if called from the finalizer. protected virtual void Dispose(bool disposing) { + if (!IsSupported) + { + return; + } + if (disposing) { #if FEATURE_MANAGED_ETW @@ -1396,8 +1443,15 @@ internal EventSource(Guid eventSourceGuid, string eventSourceName) // Used by the internal FrameworkEventSource constructor and the TraceLogging-style event source constructor internal EventSource(Guid eventSourceGuid, string eventSourceName, EventSourceSettings settings, string[]? traits = null) { - m_config = ValidateSettings(settings); - Initialize(eventSourceGuid, eventSourceName, traits); + if (IsSupported) + { +#if FEATURE_PERFTRACING + m_eventHandleTable = new TraceLoggingEventHandleTable(); +#endif + + m_config = ValidateSettings(settings); + Initialize(eventSourceGuid, eventSourceName, traits); + } } /// @@ -1862,7 +1916,7 @@ private static Guid GenerateGuidFromName(string name) private unsafe void WriteEventVarargs(int eventId, Guid* childActivityID, object?[] args) { - if (m_eventSourceEnabled) + if (IsEnabled()) { Debug.Assert(m_eventData != null); // You must have initialized this if you enabled the source. try @@ -2506,6 +2560,11 @@ internal void SendCommand(EventListener? listener, EventProviderType eventProvid EventLevel level, EventKeywords matchAnyKeyword, IDictionary? commandArguments) { + if (!IsSupported) + { + return; + } + var commandArgs = new EventCommandEventArgs(command, commandArguments, this, listener, eventProviderType, perEventSourceSessionId, etwSessionId, enable, level, matchAnyKeyword); lock (EventListener.EventListenersLock) { @@ -2543,6 +2602,11 @@ internal void SendCommand(EventListener? listener, EventProviderType eventProvid /// internal void DoCommand(EventCommandEventArgs commandArgs) { + if (!IsSupported) + { + return; + } + // PRECONDITION: We should be holding the EventListener.EventListenersLock // We defer commands until we are completely inited. This allows error messages to be sent. Debug.Assert(m_completelyInited); @@ -2709,6 +2773,9 @@ internal void DoCommand(EventCommandEventArgs commandArgs) /// internal bool EnableEventForDispatcher(EventDispatcher? dispatcher, EventProviderType eventProviderType, int eventId, bool value) { + if (!IsSupported) + return false; + Debug.Assert(m_eventData != null); if (dispatcher == null) diff --git a/src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/TraceLogging/TraceLoggingEventSource.cs b/src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/TraceLogging/TraceLoggingEventSource.cs index 168e529daa84e8..3eaa3753991361 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/TraceLogging/TraceLoggingEventSource.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/TraceLogging/TraceLoggingEventSource.cs @@ -38,7 +38,7 @@ public partial class EventSource #endif #if FEATURE_PERFTRACING - private readonly TraceLoggingEventHandleTable m_eventHandleTable = new TraceLoggingEventHandleTable(); + private readonly TraceLoggingEventHandleTable m_eventHandleTable = null!; #endif /// diff --git a/src/libraries/System.Private.CoreLib/src/System/Threading/Tasks/TplEventSource.cs b/src/libraries/System.Private.CoreLib/src/System/Threading/Tasks/TplEventSource.cs index c1a1d20b0e1d84..e81d5c67f51a76 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Threading/Tasks/TplEventSource.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Threading/Tasks/TplEventSource.cs @@ -269,7 +269,7 @@ public void TaskCompleted( int OriginatingTaskSchedulerID, int OriginatingTaskID, // PFX_COMMON_EVENT_HEADER int TaskID, bool IsExceptional) { - if (IsEnabled(EventLevel.Informational, Keywords.Tasks)) + if (IsEnabled() && IsEnabled(EventLevel.Informational, Keywords.Tasks)) { unsafe { @@ -538,7 +538,7 @@ public void NewID(int TaskID) public void IncompleteAsyncMethod(IAsyncStateMachineBox stateMachineBox) { System.Diagnostics.Debug.Assert(stateMachineBox != null); - if (IsEnabled(EventLevel.Warning, Keywords.AsyncMethod)) + if (IsEnabled() && IsEnabled(EventLevel.Warning, Keywords.AsyncMethod)) { IAsyncStateMachine stateMachine = stateMachineBox.GetStateMachineObject(); if (stateMachine != null)