Skip to content

Commit

Permalink
Remove WinRT specific code from EventSource. (dotnet#37508)
Browse files Browse the repository at this point in the history
* Remove WinRT specific code from EventSource.

Follow up to dotnet#36715

* Remove EventSource.GetMetadata method, which was only used by ProjectN.
  • Loading branch information
eerhardt authored Jun 6, 2020
1 parent e27077c commit aa4d705
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 257 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ public void Enable()
}
catch (NotImplementedException)
{
#if (!ES_BUILD_PCL && !ES_BUILD_PN)
#if (!ES_BUILD_PCL)
// send message to debugger without delay
System.Diagnostics.Debugger.Log(0, null, "Activity Enabled() called but AsyncLocals Not Supported (pre V4.6). Ignoring Enable");
#endif
Expand Down Expand Up @@ -381,7 +381,7 @@ private unsafe void CreateActivityPathGuid(out Guid idRet, out int activityPathG
{
// TODO FIXME - differentiate between AD inside PCL
int appDomainID = 0;
#if (!ES_BUILD_STANDALONE && !ES_BUILD_PN)
#if (!ES_BUILD_STANDALONE)
appDomainID = System.Threading.Thread.GetDomainID();
#endif
// We start with the appdomain number to make this unique among appdomains.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,7 @@ namespace System.Diagnostics.Tracing
#if ES_BUILD_STANDALONE
[System.Security.Permissions.HostProtection(MayLeakOnAbort = true)]
#endif

/*
EventDescriptor was public in the separate System.Diagnostics.Tracing assembly(pre NS2.0),
now the move to CoreLib marked them as private.
While they are technically private (it's a contract used between the library and the ILC toolchain),
we need them to be rooted and exported from shared library for the system to work.
For now I'm simply marking them as public again.A cleaner solution might be to use.rd.xml to
root them and modify shared library definition to force export them.
*/
#if ES_BUILD_PN
public
#else
internal
#endif
struct EventDescriptor
internal struct EventDescriptor
{
#region private
[FieldOffset(0)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -610,7 +610,7 @@ private unsafe bool GetDataFromController(int etwSessionId,
dataStart = 0;
if (filterData == null)
{
#if (!ES_BUILD_PCL && !ES_BUILD_PN && TARGET_WINDOWS)
#if (!ES_BUILD_PCL && TARGET_WINDOWS)
string regKey = @"\Microsoft\Windows\CurrentVersion\Winevt\Publishers\{" + m_providerId + "}";
if (IntPtr.Size == 8)
regKey = @"Software\Wow6432Node" + regKey;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -649,15 +649,9 @@ protected EventSource(EventSourceSettings settings, params string[]? traits)
{
m_config = ValidateSettings(settings);


GetMetadata(out Guid eventSourceGuid, out string? eventSourceName, out _, out _);

if (eventSourceGuid.Equals(Guid.Empty) || eventSourceName == null)
{
Type myType = this.GetType();
eventSourceGuid = GetGuid(myType);
eventSourceName = GetName(myType);
}
Type myType = this.GetType();
Guid eventSourceGuid = GetGuid(myType);
string eventSourceName = GetName(myType);

Initialize(eventSourceGuid, eventSourceName, traits);
}
Expand Down Expand Up @@ -709,25 +703,6 @@ private unsafe void DefineEventPipeEvents()
}
#endif

internal virtual void GetMetadata(out Guid eventSourceGuid, out string? eventSourceName, out EventMetadata[]? eventData, out byte[]? manifestBytes)
{
//
// In ProjectN subclasses need to override this method, and return the data from their EventSourceAttribute and EventAttribute annotations.
// On other architectures it is a no-op.
//
// eventDescriptors needs to contain one EventDescriptor for each event; the event's ID should be the same as its index in this array.
// manifestBytes is a UTF-8 encoding of the ETW manifest for the type.
//
// This will be implemented by an IL rewriter, so we can't make this method abstract or the initial build of the subclass would fail.
//
eventSourceGuid = Guid.Empty;
eventSourceName = null;
eventData = null;
manifestBytes = null;

return;
}

/// <summary>
/// This method is called when the eventSource is updated by the controller.
/// </summary>
Expand Down Expand Up @@ -1469,7 +1444,7 @@ private unsafe void Initialize(Guid eventSourceGuid, string eventSourceName, str
m_etwProvider = etwProvider;

#if TARGET_WINDOWS
#if (!ES_BUILD_STANDALONE && !ES_BUILD_PN)
#if (!ES_BUILD_STANDALONE)
// API available on OS >= Win 8 and patched Win 7.
// Disable only for FrameworkEventSource to avoid recursion inside exception handling.
if (this.Name != "System.Diagnostics.Eventing.FrameworkEventSource" || Environment.IsWindows8OrAbove)
Expand Down Expand Up @@ -1831,11 +1806,8 @@ private static Guid GenerateGuidFromName(string name)
if (dataType.IsEnum())
{
dataType = Enum.GetUnderlyingType(dataType);
#if ES_BUILD_PN
int dataTypeSize = (int)dataType.TypeHandle.ToEETypePtr().ValueTypeSize;
#else

int dataTypeSize = System.Runtime.InteropServices.Marshal.SizeOf(dataType);
#endif
if (dataTypeSize < sizeof(int))
dataType = typeof(int);
goto Again;
Expand Down Expand Up @@ -2452,49 +2424,8 @@ protected override void OnControllerCommand(ControllerCommand command, IDictiona
/// code:m_eventData for where we use this.
/// </summary>

/*
EventMetadata was public in the separate System.Diagnostics.Tracing assembly(pre NS2.0),
now the move to CoreLib marked them as private.
While they are technically private (it's a contract used between the library and the ILC toolchain),
we need them to be rooted and exported from shared library for the system to work.
For now I'm simply marking them as public again.A cleaner solution might be to use.rd.xml to
root them and modify shared library definition to force export them.
*/
#if ES_BUILD_PN
public
#else
internal
#endif
partial struct EventMetadata
{
#if ES_BUILD_PN
public EventMetadata(EventDescriptor descriptor,
EventTags tags,
bool enabledForAnyListener,
bool enabledForETW,
string name,
string message,
EventParameterType[] parameterTypes)
{
this.Descriptor = descriptor;
this.Tags = tags;
this.EnabledForAnyListener = enabledForAnyListener;
this.EnabledForETW = enabledForETW;
#if FEATURE_PERFTRACING
this.EnabledForEventPipe = false;
#endif
this.TriggersActivityTracking = 0;
this.Name = name;
this.Message = message;
this.Parameters = null!;
this.TraceLoggingEventTypes = null;
this.ActivityOptions = EventActivityOptions.None;
this.ParameterTypes = parameterTypes;
this.HasRelatedActivityID = false;
this.EventHandle = IntPtr.Zero;
}
#endif

internal partial struct EventMetadata
{
public EventDescriptor Descriptor;
public IntPtr EventHandle; // EventPipeEvent handle.
public EventTags Tags;
Expand All @@ -2514,13 +2445,8 @@ public EventMetadata(EventDescriptor descriptor,

public TraceLoggingEventTypes? TraceLoggingEventTypes;
public EventActivityOptions ActivityOptions;

#if ES_BUILD_PN
public EventParameterType[] ParameterTypes;
#endif
}

#if !ES_BUILD_PN
private static int GetParameterCount(EventMetadata eventData)
{
return eventData.Parameters.Length;
Expand All @@ -2532,101 +2458,6 @@ private static Type GetDataType(EventMetadata eventData, int parameterId)
}

private const bool m_EventSourcePreventRecursion = false;
#else
private static int GetParameterCount(EventMetadata eventData)
{
int paramCount;
if (eventData.Parameters == null)
{
paramCount = eventData.ParameterTypes.Length;
}
else
{
paramCount = eventData.Parameters.Length;
}

return paramCount;
}

private static Type GetDataType(EventMetadata eventData, int parameterId)
{
Type dataType;
if (eventData.Parameters == null)
{
dataType = EventTypeToType(eventData.ParameterTypes[parameterId]);
}
else
{
dataType = eventData.Parameters[parameterId].ParameterType;
}

return dataType;
}

private static readonly bool m_EventSourcePreventRecursion = true;

public enum EventParameterType
{
Boolean,
Byte,
SByte,
Char,
Int16,
UInt16,
Int32,
UInt32,
Int64,
UInt64,
IntPtr,
Single,
Double,
Decimal,
Guid,
String
}

private static Type EventTypeToType(EventParameterType type)
{
switch (type)
{
case EventParameterType.Boolean:
return typeof(bool);
case EventParameterType.Byte:
return typeof(byte);
case EventParameterType.SByte:
return typeof(sbyte);
case EventParameterType.Char:
return typeof(char);
case EventParameterType.Int16:
return typeof(short);
case EventParameterType.UInt16:
return typeof(ushort);
case EventParameterType.Int32:
return typeof(int);
case EventParameterType.UInt32:
return typeof(uint);
case EventParameterType.Int64:
return typeof(long);
case EventParameterType.UInt64:
return typeof(ulong);
case EventParameterType.IntPtr:
return typeof(IntPtr);
case EventParameterType.Single:
return typeof(float);
case EventParameterType.Double:
return typeof(double);
case EventParameterType.Decimal:
return typeof(decimal);
case EventParameterType.Guid:
return typeof(Guid);
case EventParameterType.String:
return typeof(string);
default:
// TODO: should I throw an exception here?
return null!;
}
}
#endif

// This is the internal entry point that code:EventListeners call when wanting to send a command to a
// eventSource. The logic is as follows
Expand Down Expand Up @@ -2915,28 +2746,11 @@ private void EnsureDescriptorsInitialized()
#endif
if (m_eventData == null)
{
Guid eventSourceGuid = Guid.Empty;

// Try the GetMetadata provided by the ILTransform in ProjectN. The default sets all to null, and in that case we fall back
// to the reflection approach.
GetMetadata(out eventSourceGuid, out string? eventSourceName, out EventMetadata[]? eventData, out byte[]? manifest);

if (eventSourceGuid.Equals(Guid.Empty) || eventSourceName == null || eventData == null || manifest == null)
{
// GetMetadata failed, so we have to set it via reflection.
Debug.Assert(m_rawManifest == null);
// get the metadata via reflection.
Debug.Assert(m_rawManifest == null);
m_rawManifest = CreateManifestAndDescriptors(this.GetType(), Name, this);
Debug.Assert(m_eventData != null);

m_rawManifest = CreateManifestAndDescriptors(this.GetType(), Name, this);
Debug.Assert(m_eventData != null);
}
else
{
// GetMetadata worked, so set the fields as appropriate.
m_name = eventSourceName;
m_guid = eventSourceGuid;
m_eventData = eventData;
m_rawManifest = manifest;
}
// TODO Enforce singleton pattern
Debug.Assert(EventListener.s_EventSources != null, "should be called within lock on EventListener.EventListenersLock which ensures s_EventSources to be initialized");
foreach (WeakReference<EventSource> eventSourceRef in EventListener.s_EventSources)
Expand Down Expand Up @@ -3058,11 +2872,9 @@ internal static Attribute GetCustomAttributeHelper(Type type, Type attributeType
// When that is the case, we have the build the custom assemblies on a member by hand.
internal static Attribute? GetCustomAttributeHelper(MemberInfo member, Type attributeType, EventManifestOptions flags = EventManifestOptions.None)
{
#if !ES_BUILD_PN
// On ProjectN, ReflectionOnly() always equals false. AllowEventSourceOverride is an option that allows either Microsoft.Diagnostics.Tracing or
// AllowEventSourceOverride is an option that allows either Microsoft.Diagnostics.Tracing or
// System.Diagnostics.Tracing EventSource to be considered valid. This should not mattter anywhere but in Microsoft.Diagnostics.Tracing (nuget package).
if (!member.Module.Assembly.ReflectionOnly() && (flags & EventManifestOptions.AllowEventSourceOverride) == 0)
#endif // !ES_BUILD_PN
{
// Let the runtime to the work for us, since we can execute code in this context.
Attribute? firstAttribute = null;
Expand All @@ -3074,7 +2886,7 @@ internal static Attribute GetCustomAttributeHelper(Type type, Type attributeType
return firstAttribute;
}

#if (!ES_BUILD_PCL && !ES_BUILD_PN)
#if (!ES_BUILD_PCL)
foreach (CustomAttributeData data in CustomAttributeData.GetCustomAttributes(member))
{
if (AttributeTypeNamesMatch(attributeType, data.Constructor.ReflectedType!))
Expand Down Expand Up @@ -3116,7 +2928,7 @@ internal static Attribute GetCustomAttributeHelper(Type type, Type attributeType
}

return null;
#else // ES_BUILD_PCL && ES_BUILD_PN
#else // ES_BUILD_PCL
// Don't use nameof here because the resource doesn't exist on some platforms, which results in a compilation error.
throw new ArgumentException("EventSource_PCLPlatformNotSupportedReflection", "EventSource");
#endif
Expand Down Expand Up @@ -3705,7 +3517,7 @@ private static void DebugCheckEvent(ref Dictionary<string, string>? eventsByName
/// <returns>The literal value or -1 if the value could not be determined. </returns>
private static int GetHelperCallFirstArg(MethodInfo method)
{
#if (!ES_BUILD_PCL && !ES_BUILD_PN)
#if (!ES_BUILD_PCL)
// Currently searches for the following pattern
//
// ... // CAN ONLY BE THE INSTRUCTIONS BELOW
Expand Down Expand Up @@ -3834,7 +3646,7 @@ internal void ReportOutOfBandMessage(string msg)
{
try
{
#if (!ES_BUILD_PCL && !ES_BUILD_PN)
#if (!ES_BUILD_PCL)
// send message to debugger without delay
System.Diagnostics.Debugger.Log(0, null, string.Format("EventSource Error: {0}{1}", msg, Environment.NewLine));
#endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// See the LICENSE file in the project root for more information.

using System;
#if ES_BUILD_PCL || ES_BUILD_PN
#if ES_BUILD_PCL
using System.Collections.Generic;
#endif
using System.Reflection;
Expand Down Expand Up @@ -209,7 +209,7 @@ public enum TypeCode {
#endif
internal static class ReflectionExtensions
{
#if (!ES_BUILD_PCL && !ES_BUILD_PN)
#if (!ES_BUILD_PCL)

//
// Type extension methods
Expand Down Expand Up @@ -239,11 +239,7 @@ internal static class ReflectionExtensions
public static Assembly Assembly(this Type type) { return type.GetTypeInfo().Assembly; }
public static IEnumerable<PropertyInfo> GetProperties(this Type type)
{
#if ES_BUILD_PN
return type.GetProperties();
#else
return type.GetRuntimeProperties();
#endif
}
public static MethodInfo? GetGetMethod(this PropertyInfo propInfo) { return propInfo.GetMethod; }
public static Type[] GetGenericArguments(this Type type) { return type.GenericTypeArguments; }
Expand Down
Loading

0 comments on commit aa4d705

Please sign in to comment.