提交 4e29e54e 编写于 作者: T Tautvydas Žilys

Make ETW event registering AOT friendly.

上级 92839420
......@@ -292,7 +292,8 @@
<Compile Include="..\referencesource\mscorlib\system\diagnostics\conditionalattribute.cs" />
<Compile Include="..\referencesource\mscorlib\system\diagnostics\contracts\contracts.cs" />
<Compile Include="..\referencesource\mscorlib\system\diagnostics\contracts\contractsbcl.cs" />
<Compile Include="..\referencesource\mscorlib\system\diagnostics\debuggerattributes.cs" /> <Compile Include="..\referencesource\mscorlib\system\diagnostics\eventing\activitytracker.cs" />
<Compile Include="..\referencesource\mscorlib\system\diagnostics\debuggerattributes.cs" />
<Compile Include="..\referencesource\mscorlib\system\diagnostics\eventing\activitytracker.cs" />
<Compile Include="..\referencesource\mscorlib\system\diagnostics\eventing\eventactivityoptions.cs" />
<Compile Include="..\referencesource\mscorlib\system\diagnostics\eventing\eventdescriptor.cs" />
<Compile Include="..\referencesource\mscorlib\system\diagnostics\eventing\eventprovider.cs" />
......
......@@ -78,6 +78,7 @@ namespace System.Diagnostics.Tracing
[SecurityCritical]
UnsafeNativeMethods.ManifestEtw.EtwEnableCallback m_etwCallback; // Trace Callback function
GCHandle m_thisGCHandle;
private long m_regHandle; // Trace Registration Handle
private byte m_level; // Tracing Level
private long m_anyKeywordMask; // Trace Enable Flags
......@@ -149,13 +150,25 @@ namespace System.Diagnostics.Tracing
internal unsafe void Register(Guid providerGuid)
{
m_providerId = providerGuid;
uint status;
m_etwCallback = new UnsafeNativeMethods.ManifestEtw.EtwEnableCallback(EtwEnableCallBack);
status = EventRegister(ref m_providerId, m_etwCallback);
if (status != 0)
if (m_thisGCHandle.IsAllocated)
m_thisGCHandle.Free();
m_thisGCHandle = GCHandle.Alloc(this);
try
{
var status = UnsafeNativeMethods.ManifestEtw.EventRegister(ref providerGuid, m_etwCallback, GCHandle.ToIntPtr(m_thisGCHandle).ToPointer(), ref m_regHandle);
if (status != 0)
{
throw new ArgumentException(Win32Native.GetMessage(unchecked((int)status)));
}
}
catch
{
throw new ArgumentException(Win32Native.GetMessage(unchecked((int)status)));
m_thisGCHandle.Free();
throw;
}
}
......@@ -264,22 +277,44 @@ namespace System.Diagnostics.Tracing
{
EventUnregister();
m_regHandle = 0;
m_thisGCHandle.Free();
}
}
[AttributeUsage(AttributeTargets.Method)]
private sealed class MonoPInvokeCallbackAttribute : Attribute
{
public MonoPInvokeCallbackAttribute(Type t)
{
}
}
[MonoPInvokeCallback(typeof(UnsafeNativeMethods.ManifestEtw.EtwEnableCallback))]
unsafe static void EtwEnableCallBack(
[In] ref System.Guid sourceId,
[In] int controlCode,
[In] byte setLevel,
[In] long anyKeyword,
[In] long allKeyword,
[In] UnsafeNativeMethods.ManifestEtw.EVENT_FILTER_DESCRIPTOR* filterData,
[In] void* callbackContext
)
{
var _this = (EventProvider)GCHandle.FromIntPtr(new IntPtr(callbackContext)).Target;
_this.EtwEnableCallBackImpl(controlCode, setLevel, anyKeyword, allKeyword, filterData);
}
// <SecurityKernel Critical="True" Ring="0">
// <UsesUnsafeCode Name="Parameter filterData of type: Void*" />
// <UsesUnsafeCode Name="Parameter callbackContext of type: Void*" />
// </SecurityKernel>
[System.Security.SecurityCritical]
unsafe void EtwEnableCallBack(
[In] ref System.Guid sourceId,
unsafe void EtwEnableCallBackImpl(
[In] int controlCode,
[In] byte setLevel,
[In] long anyKeyword,
[In] long allKeyword,
[In] UnsafeNativeMethods.ManifestEtw.EVENT_FILTER_DESCRIPTOR* filterData,
[In] void* callbackContext
[In] UnsafeNativeMethods.ManifestEtw.EVENT_FILTER_DESCRIPTOR* filterData
)
{
// This is an optional callback API. We will therefore ignore any failures that happen as a
......@@ -539,7 +574,7 @@ namespace System.Diagnostics.Tracing
dataStart = 0;
if (filterData == null)
{
#if !ES_BUILD_PCL
#if !ES_BUILD_PCL && !MONO
string regKey = @"\Microsoft\Windows\CurrentVersion\Winevt\Publishers\{" + m_providerId + "}";
if (System.Runtime.InteropServices.Marshal.SizeOf(typeof(IntPtr)) == 8)
regKey = @"HKEY_LOCAL_MACHINE\Software" + @"\Wow6432Node" + regKey;
......@@ -1161,13 +1196,6 @@ namespace System.Diagnostics.Tracing
// These are look-alikes to the Manifest based ETW OS APIs that have been shimmed to work
// either with Manifest ETW or Classic ETW (if Manifest based ETW is not available).
[SecurityCritical]
private unsafe uint EventRegister(ref Guid providerId, UnsafeNativeMethods.ManifestEtw.EtwEnableCallback enableCallback)
{
m_providerId = providerId;
m_etwCallback = enableCallback;
return UnsafeNativeMethods.ManifestEtw.EventRegister(ref providerId, enableCallback, null, ref m_regHandle);
}
[SecurityCritical]
private uint EventUnregister()
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册