From a7f8504f23a2b71dafd3553ee9c17d2b5f69fbc7 Mon Sep 17 00:00:00 2001 From: Eric Erhardt Date: Thu, 18 Jun 2020 17:48:03 -0500 Subject: [PATCH] Fix Diagnostics.Tracing ILLinkTrim entries - Fix PropertyValue/ReferenceTypeHelper with a DynamicDependency - RuntimeEventSource doesn't need to be rooted. This class is used by IL and doesn't get trimmed. - EventPipe* and NativeRuntimeEventSource only need to be rooted in CoreCLR, since they aren't used in Mono. So they are moved to just CoreCLR's ILLinkTrim file. Contributes to #35199 --- .../src/System.Private.CoreLib/ILLinkTrim.xml | 4 + .../EventSourcePropertyValueTest.cs | 93 +++++++++++++++++++ ...tem.Diagnostics.Tracing.TrimmingTests.proj | 5 + .../src/ILLink/ILLink.Descriptors.Shared.xml | 9 -- .../Tracing/TraceLogging/PropertyValue.cs | 2 + 5 files changed, 104 insertions(+), 9 deletions(-) create mode 100644 src/libraries/System.Diagnostics.Tracing/tests/TrimmingTests/EventSourcePropertyValueTest.cs create mode 100644 src/libraries/System.Diagnostics.Tracing/tests/TrimmingTests/System.Diagnostics.Tracing.TrimmingTests.proj diff --git a/src/coreclr/src/System.Private.CoreLib/ILLinkTrim.xml b/src/coreclr/src/System.Private.CoreLib/ILLinkTrim.xml index e5a4eeb311e5c..f24632de316bd 100644 --- a/src/coreclr/src/System.Private.CoreLib/ILLinkTrim.xml +++ b/src/coreclr/src/System.Private.CoreLib/ILLinkTrim.xml @@ -5,5 +5,9 @@ + + + + diff --git a/src/libraries/System.Diagnostics.Tracing/tests/TrimmingTests/EventSourcePropertyValueTest.cs b/src/libraries/System.Diagnostics.Tracing/tests/TrimmingTests/EventSourcePropertyValueTest.cs new file mode 100644 index 0000000000000..cf3d949b561dd --- /dev/null +++ b/src/libraries/System.Diagnostics.Tracing/tests/TrimmingTests/EventSourcePropertyValueTest.cs @@ -0,0 +1,93 @@ +// 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 System.Collections.ObjectModel; +using System.Diagnostics.Tracing; + +/// +/// Tests that using EventSource with a reference type EventData works on a +/// trimmed application. +/// See System.Diagnostics.Tracing.PropertyValue.GetReferenceTypePropertyGetter. +/// +internal class Program +{ + [EventData] + private class TestData + { + public int TestInt { get; set; } + public TestSubData SubData { get; set; } + } + + [EventData] + private class TestSubData + { + public int SubInt { get; set; } + } + + [EventSource(Name = EventSourceName)] + private class TestEventSource : EventSource + { + public const string EventSourceName = "MyTest"; + public static TestEventSource Log = new TestEventSource(); + + public TestEventSource() : base(EventSourceSettings.EtwSelfDescribingEventFormat) { } + + [Event(1)] + public void LogData(TestData data) + { + Write("LogData", data); + } + } + + private class TestEventListener : EventListener + { + public ReadOnlyCollection LogDataPayload { get; set; } + + protected override void OnEventSourceCreated(EventSource eventSource) + { + if (eventSource.Name == TestEventSource.EventSourceName) + { + EnableEvents(eventSource, EventLevel.Verbose); + } + + base.OnEventSourceCreated(eventSource); + } + + protected override void OnEventWritten(EventWrittenEventArgs eventData) + { + if (eventData.EventName == "LogData") + { + LogDataPayload = eventData.Payload; + } + + base.OnEventWritten(eventData); + } + } + + public static int Main() + { + using (var listener = new TestEventListener()) + { + var testData = new TestData() + { + TestInt = 5, + SubData = new TestSubData() + { + SubInt = 6 + } + }; + TestEventSource.Log.LogData(testData); + + if (listener.LogDataPayload?.Count == 2 && + (int)listener.LogDataPayload[0] == testData.TestInt && + (int)((IDictionary)listener.LogDataPayload[1])["SubInt"] == testData.SubData.SubInt) + { + return 100; + } + + return -1; + } + } +} diff --git a/src/libraries/System.Diagnostics.Tracing/tests/TrimmingTests/System.Diagnostics.Tracing.TrimmingTests.proj b/src/libraries/System.Diagnostics.Tracing/tests/TrimmingTests/System.Diagnostics.Tracing.TrimmingTests.proj new file mode 100644 index 0000000000000..da4a46f2ae141 --- /dev/null +++ b/src/libraries/System.Diagnostics.Tracing/tests/TrimmingTests/System.Diagnostics.Tracing.TrimmingTests.proj @@ -0,0 +1,5 @@ + + + + + diff --git a/src/libraries/System.Private.CoreLib/src/ILLink/ILLink.Descriptors.Shared.xml b/src/libraries/System.Private.CoreLib/src/ILLink/ILLink.Descriptors.Shared.xml index 87750a91e04a0..6a3fc8f74c100 100644 --- a/src/libraries/System.Private.CoreLib/src/ILLink/ILLink.Descriptors.Shared.xml +++ b/src/libraries/System.Private.CoreLib/src/ILLink/ILLink.Descriptors.Shared.xml @@ -37,15 +37,6 @@ - - - - - - - - - diff --git a/src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/TraceLogging/PropertyValue.cs b/src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/TraceLogging/PropertyValue.cs index 5db76aa09bcaa..edc0287194a74 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/TraceLogging/PropertyValue.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/TraceLogging/PropertyValue.cs @@ -6,6 +6,7 @@ using System; using System.Diagnostics; #endif +using System.Diagnostics.CodeAnalysis; using System.Reflection; using System.Runtime.InteropServices; @@ -196,6 +197,7 @@ private static Func GetBoxedValueTypePropertyGette /// /// /// + [DynamicDependency("#ctor", typeof(ReferenceTypeHelper<>))] private static Func GetReferenceTypePropertyGetter(PropertyInfo property) { var helper = (TypeHelper)Activator.CreateInstance(typeof(ReferenceTypeHelper<>).MakeGenericType(property.DeclaringType!))!;