Skip to content

Commit

Permalink
Fix Diagnostics.Tracing ILLinkTrim entries
Browse files Browse the repository at this point in the history
- 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 dotnet#35199
  • Loading branch information
eerhardt committed Jun 18, 2020
1 parent 6a8fd0b commit a7f8504
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 9 deletions.
4 changes: 4 additions & 0 deletions src/coreclr/src/System.Private.CoreLib/ILLinkTrim.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,9 @@
<method name="_RegisterFrozenSegment" />
<method name="_UnregisterFrozenSegment" />
</type>
<!-- Accessed via private reflection by an external tracing controller. -->
<type fullname="System.Diagnostics.Tracing.EventPipe*" />
<!-- The private Event methods are accessed by private reflection in the base EventSource class. -->
<type fullname="System.Diagnostics.Tracing.NativeRuntimeEventSource" />
</assembly>
</linker>
Original file line number Diff line number Diff line change
@@ -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;

/// <summary>
/// Tests that using EventSource with a reference type EventData works on a
/// trimmed application.
/// See System.Diagnostics.Tracing.PropertyValue.GetReferenceTypePropertyGetter.
/// </summary>
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<object> 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<string, object>)listener.LogDataPayload[1])["SubInt"] == testData.SubData.SubInt)
{
return 100;
}

return -1;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<Project DefaultTargets="Build">
<Import Project="$([MSBuild]::GetPathOfFileAbove(Directory.Build.props))" />

<Import Project="$([MSBuild]::GetPathOfFileAbove(Directory.Build.targets))" />
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,6 @@
<!-- Methods is used by VS Tasks Window. -->
<method name="GetActiveTaskFromId" />
</type>
<!-- Accessed via private reflection by tracing controller. -->
<type fullname="System.Diagnostics.Tracing.EventPipe*" />
<!-- Accessed via private reflection and by native code. -->
<type fullname="System.Diagnostics.Tracing.RuntimeEventSource" />
<type fullname="System.Diagnostics.Tracing.NativeRuntimeEventSource" />
<type fullname="System.Diagnostics.Tracing.PropertyValue/ReferenceTypeHelper`1">
<!-- Instantiated via reflection -->
<method name=".ctor" />
</type>
<!-- Accessed via native code. -->
<type fullname="System.Runtime.InteropServices.ComTypes.IEnumerable" />
<type fullname="System.Runtime.InteropServices.ComTypes.IEnumerator" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System;
using System.Diagnostics;
#endif
using System.Diagnostics.CodeAnalysis;
using System.Reflection;
using System.Runtime.InteropServices;

Expand Down Expand Up @@ -196,6 +197,7 @@ private static Func<PropertyValue, PropertyValue> GetBoxedValueTypePropertyGette
/// </summary>
/// <param name="property"></param>
/// <returns></returns>
[DynamicDependency("#ctor", typeof(ReferenceTypeHelper<>))]
private static Func<PropertyValue, PropertyValue> GetReferenceTypePropertyGetter(PropertyInfo property)
{
var helper = (TypeHelper)Activator.CreateInstance(typeof(ReferenceTypeHelper<>).MakeGenericType(property.DeclaringType!))!;
Expand Down

0 comments on commit a7f8504

Please sign in to comment.