diff --git a/src/libraries/System.Private.CoreLib/src/System.Private.CoreLib.Shared.projitems b/src/libraries/System.Private.CoreLib/src/System.Private.CoreLib.Shared.projitems index 74ab9db6256a6..feafd3b2ff447 100644 --- a/src/libraries/System.Private.CoreLib/src/System.Private.CoreLib.Shared.projitems +++ b/src/libraries/System.Private.CoreLib/src/System.Private.CoreLib.Shared.projitems @@ -1392,7 +1392,6 @@ - diff --git a/src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/EventPipeEventProvider.cs b/src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/EventPipeEventProvider.cs index 2780b5f29f41d..a17e9a585fbab 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/EventPipeEventProvider.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/EventPipeEventProvider.cs @@ -5,27 +5,29 @@ namespace System.Diagnostics.Tracing { - internal sealed class EventPipeEventProvider : IEventProvider + internal sealed class EventPipeEventProvider : EventProviderImpl { - private EventEnableCallback? _enableCallback; + private readonly WeakReference _eventProvider; private IntPtr _provHandle; private GCHandle _gcHandle; + internal EventPipeEventProvider(EventProvider eventProvider) + { + _eventProvider = new WeakReference(eventProvider); + } + [UnmanagedCallersOnly] private static unsafe void Callback(byte* sourceId, int isEnabled, byte level, long matchAnyKeywords, long matchAllKeywords, Interop.Advapi32.EVENT_FILTER_DESCRIPTOR* filterData, void* callbackContext) { - ((EventPipeEventProvider)GCHandle.FromIntPtr((IntPtr)callbackContext).Target!)._enableCallback!( - isEnabled, level, matchAnyKeywords, matchAllKeywords, filterData); + EventPipeEventProvider _this = (EventPipeEventProvider)GCHandle.FromIntPtr((IntPtr)callbackContext).Target!; + if (_this._eventProvider.TryGetTarget(out EventProvider? target)) + target.EnableCallback(isEnabled, level, matchAnyKeywords, matchAllKeywords, filterData); } // Register an event provider. - unsafe void IEventProvider.EventRegister( - EventSource eventSource, - EventEnableCallback enableCallback) + internal override unsafe void Register(EventSource eventSource) { - _enableCallback = enableCallback; - Debug.Assert(!_gcHandle.IsAllocated); _gcHandle = GCHandle.Alloc(this); @@ -39,7 +41,7 @@ unsafe void IEventProvider.EventRegister( } // Unregister an event provider. - void IEventProvider.EventUnregister() + internal override void Unregister() { if (_provHandle != 0) { @@ -53,7 +55,7 @@ void IEventProvider.EventUnregister() } // Write an event. - unsafe EventProvider.WriteEventErrorCode IEventProvider.EventWriteTransfer( + internal override unsafe EventProvider.WriteEventErrorCode EventWriteTransfer( in EventDescriptor eventDescriptor, IntPtr eventHandle, Guid* activityId, @@ -85,13 +87,13 @@ unsafe EventProvider.WriteEventErrorCode IEventProvider.EventWriteTransfer( } // Get or set the per-thread activity ID. - int IEventProvider.EventActivityIdControl(Interop.Advapi32.ActivityControl controlCode, ref Guid activityId) + internal override int ActivityIdControl(Interop.Advapi32.ActivityControl controlCode, ref Guid activityId) { return EventActivityIdControl(controlCode, ref activityId); } // Define an EventPipeEvent handle. - unsafe IntPtr IEventProvider.DefineEventHandle(uint eventID, string eventName, long keywords, uint eventVersion, uint level, + internal override unsafe IntPtr DefineEventHandle(uint eventID, string eventName, long keywords, uint eventVersion, uint level, byte *pMetadata, uint metadataLength) { return EventPipeInternal.DefineEvent(_provHandle, eventID, keywords, eventVersion, level, pMetadata, metadataLength); diff --git a/src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/EventProvider.cs b/src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/EventProvider.cs index b9c2ff1449e7b..e1ec3c2f21f38 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/EventProvider.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/EventProvider.cs @@ -32,13 +32,6 @@ internal enum ControllerCommand Disable = -3, } - internal unsafe delegate void EventEnableCallback( - int isEnabled, - byte level, - long matchAnyKeywords, - long matchAllKeywords, - Interop.Advapi32.EVENT_FILTER_DESCRIPTOR* filterData); - /// /// Only here because System.Diagnostics.EventProvider needs one more extensibility hook (when it gets a /// controller callback) @@ -72,7 +65,7 @@ internal SessionInfo(int sessionIdBit_, int etwSessionId_) { sessionIdBit = sessionIdBit_; etwSessionId = etwSessionId_; } } - internal IEventProvider m_eventProvider; // The interface that implements the specific logging mechanism functions. + internal EventProviderImpl m_eventProvider; // The implementation of the specific logging mechanism functions. private byte m_level; // Tracing Level private long m_anyKeywordMask; // Trace Enable Flags private long m_allKeywordMask; // Match all keyword @@ -113,12 +106,12 @@ internal EventProvider(EventProviderType providerType) m_eventProvider = providerType switch { #if TARGET_WINDOWS - EventProviderType.ETW => new EtwEventProvider(), + EventProviderType.ETW => new EtwEventProvider(this), #endif #if FEATURE_PERFTRACING - EventProviderType.EventPipe => new EventPipeEventProvider(), + EventProviderType.EventPipe => new EventPipeEventProvider(this), #endif - _ => new NoOpEventProvider(), + _ => new EventProviderImpl(), }; } @@ -132,7 +125,7 @@ internal unsafe void Register(EventSource eventSource) m_providerName = eventSource.Name; m_providerId = eventSource.Guid; - m_eventProvider.EventRegister(eventSource, new EventEnableCallback(EnableCallBack)); + m_eventProvider.Register(eventSource); } // @@ -185,7 +178,7 @@ protected virtual void Dispose(bool disposing) // // We solve by Unregistering after releasing the EventListenerLock. Debug.Assert(!Monitor.IsEntered(EventListener.EventListenersLock)); - m_eventProvider.EventUnregister(); + m_eventProvider.Unregister(); } /// @@ -202,7 +195,7 @@ public virtual void Close() Dispose(false); } - private unsafe void EnableCallBack( + internal unsafe void EnableCallback( int controlCode, byte setLevel, long anyKeyword, @@ -1129,27 +1122,30 @@ internal unsafe int SetInformation( #if TARGET_WINDOWS // A wrapper around the ETW-specific API calls. - internal sealed class EtwEventProvider : IEventProvider + internal sealed class EtwEventProvider : EventProviderImpl { - private EventEnableCallback? _enableCallback; + private readonly WeakReference _eventProvider; private long _registrationHandle; private GCHandle _gcHandle; + internal EtwEventProvider(EventProvider eventProvider) + { + _eventProvider = new WeakReference(eventProvider); + } + [UnmanagedCallersOnly] private static unsafe void Callback(Guid* sourceId, int isEnabled, byte level, long matchAnyKeywords, long matchAllKeywords, Interop.Advapi32.EVENT_FILTER_DESCRIPTOR* filterData, void* callbackContext) { - ((EtwEventProvider)GCHandle.FromIntPtr((IntPtr)callbackContext).Target!)._enableCallback!( - isEnabled, level, matchAnyKeywords, matchAllKeywords, filterData); + EtwEventProvider _this = (EtwEventProvider)GCHandle.FromIntPtr((IntPtr)callbackContext).Target!; + + if (_this._eventProvider.TryGetTarget(out EventProvider? target)) + target.EnableCallback(isEnabled, level, matchAnyKeywords, matchAllKeywords, filterData); } // Register an event provider. - unsafe void IEventProvider.EventRegister( - EventSource eventSource, - EventEnableCallback enableCallback) + internal override unsafe void Register(EventSource eventSource) { - _enableCallback = enableCallback; - Debug.Assert(!_gcHandle.IsAllocated); _gcHandle = GCHandle.Alloc(this); @@ -1170,7 +1166,7 @@ unsafe void IEventProvider.EventRegister( } // Unregister an event provider. - void IEventProvider.EventUnregister() + internal override void Unregister() { if (_registrationHandle != 0) { @@ -1184,7 +1180,7 @@ void IEventProvider.EventUnregister() } // Write an event. - unsafe EventProvider.WriteEventErrorCode IEventProvider.EventWriteTransfer( + internal override unsafe EventProvider.WriteEventErrorCode EventWriteTransfer( in EventDescriptor eventDescriptor, IntPtr eventHandle, Guid* activityId, @@ -1213,7 +1209,7 @@ unsafe EventProvider.WriteEventErrorCode IEventProvider.EventWriteTransfer( } // Get or set the per-thread activity ID. - int IEventProvider.EventActivityIdControl(Interop.Advapi32.ActivityControl ControlCode, ref Guid ActivityId) + internal override int ActivityIdControl(Interop.Advapi32.ActivityControl ControlCode, ref Guid ActivityId) { return Interop.Advapi32.EventActivityIdControl( ControlCode, @@ -1221,7 +1217,7 @@ int IEventProvider.EventActivityIdControl(Interop.Advapi32.ActivityControl Contr } // Define an EventPipeEvent handle. - unsafe IntPtr IEventProvider.DefineEventHandle(uint eventID, string eventName, long keywords, uint eventVersion, + internal override unsafe IntPtr DefineEventHandle(uint eventID, string eventName, long keywords, uint eventVersion, uint level, byte* pMetadata, uint metadataLength) { throw new System.NotSupportedException(); @@ -1258,19 +1254,19 @@ internal unsafe int SetInformation( } #endif - internal sealed class NoOpEventProvider : IEventProvider +#pragma warning disable CA1852 // EventProviderImpl is not derived from in all targets + + internal class EventProviderImpl { - void IEventProvider.EventRegister( - EventSource eventSource, - EventEnableCallback enableCallback) + internal virtual void Register(EventSource eventSource) { } - void IEventProvider.EventUnregister() + internal virtual void Unregister() { } - unsafe EventProvider.WriteEventErrorCode IEventProvider.EventWriteTransfer( + internal virtual unsafe EventProvider.WriteEventErrorCode EventWriteTransfer( in EventDescriptor eventDescriptor, IntPtr eventHandle, Guid* activityId, @@ -1281,16 +1277,19 @@ unsafe EventProvider.WriteEventErrorCode IEventProvider.EventWriteTransfer( return EventProvider.WriteEventErrorCode.NoError; } - int IEventProvider.EventActivityIdControl(Interop.Advapi32.ActivityControl ControlCode, ref Guid ActivityId) + internal virtual int ActivityIdControl(Interop.Advapi32.ActivityControl ControlCode, ref Guid ActivityId) { return 0; } // Define an EventPipeEvent handle. - unsafe IntPtr IEventProvider.DefineEventHandle(uint eventID, string eventName, long keywords, uint eventVersion, + internal virtual unsafe IntPtr DefineEventHandle(uint eventID, string eventName, long keywords, uint eventVersion, uint level, byte* pMetadata, uint metadataLength) { return IntPtr.Zero; } } + +#pragma warning restore CA1852 + } diff --git a/src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/IEventProvider.cs b/src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/IEventProvider.cs deleted file mode 100644 index 56d4b20738c9c..0000000000000 --- a/src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/IEventProvider.cs +++ /dev/null @@ -1,33 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. - -namespace System.Diagnostics.Tracing -{ - // Represents the interface between EventProvider and an external logging mechanism. - internal interface IEventProvider - { - // Register an event provider. - unsafe void EventRegister( - EventSource eventSource, - EventEnableCallback enableCallback); - - // Unregister an event provider. - void EventUnregister(); - - // Write an event. - unsafe EventProvider.WriteEventErrorCode EventWriteTransfer( - in EventDescriptor eventDescriptor, - IntPtr eventHandle, - Guid* activityId, - Guid* relatedActivityId, - int userDataCount, - EventProvider.EventData* userData); - - // Get or set the per-thread activity ID. - int EventActivityIdControl(Interop.Advapi32.ActivityControl controlCode, ref Guid activityId); - - // Define an EventPipeEvent handle. - unsafe IntPtr DefineEventHandle(uint eventID, string eventName, long keywords, uint eventVersion, - uint level, byte *pMetadata, uint metadataLength); - } -}