Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ETW in Mono mscorlib #1207

Merged
merged 3 commits into from
Jul 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,235 @@
// ==++==
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// ==--==
/*============================================================
**
** Class: UnsafeNativeMethods
**
============================================================*/
namespace Microsoft.Win32 {
using Microsoft.Win32;
using Microsoft.Win32.SafeHandles;
using System;
using System.Runtime.CompilerServices;
using System.Runtime.ConstrainedExecution;
using System.Runtime.InteropServices;
using System.Runtime.Serialization;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using System.Text;
using System.Diagnostics.Tracing;

[System.Security.SecurityCritical] // auto-generated
[SuppressUnmanagedCodeSecurityAttribute()]
internal static class UnsafeNativeMethods
{
[SecurityCritical]
[SuppressUnmanagedCodeSecurityAttribute()]
internal static unsafe class ManifestEtw
{
//
// Constants error coded returned by ETW APIs
//

// The event size is larger than the allowed maximum (64k - header).
internal const int ERROR_ARITHMETIC_OVERFLOW = 534;

// Occurs when filled buffers are trying to flush to disk,
// but disk IOs are not happening fast enough.
// This happens when the disk is slow and event traffic is heavy.
// Eventually, there are no more free (empty) buffers and the event is dropped.
internal const int ERROR_NOT_ENOUGH_MEMORY = 8;

internal const int ERROR_MORE_DATA = 0xEA;
internal const int ERROR_NOT_SUPPORTED = 50;
internal const int ERROR_INVALID_PARAMETER = 0x57;

//
// ETW Methods
//

internal const int EVENT_CONTROL_CODE_DISABLE_PROVIDER = 0;
internal const int EVENT_CONTROL_CODE_ENABLE_PROVIDER = 1;
internal const int EVENT_CONTROL_CODE_CAPTURE_STATE = 2;

//
// Callback
//
[SecurityCritical]
internal unsafe delegate void EtwEnableCallback(
[In] ref Guid sourceId,
[In] int isEnabled,
[In] byte level,
[In] long matchAnyKeywords,
[In] long matchAllKeywords,
[In] EVENT_FILTER_DESCRIPTOR* filterData,
[In] void* callbackContext
);

//
// Registration APIs
//
[SecurityCritical]
[DllImport(Win32Native.ADVAPI32, ExactSpelling = true, EntryPoint = "EventRegister", CharSet = System.Runtime.InteropServices.CharSet.Unicode)]
internal static extern unsafe uint EventRegister(
[In] ref Guid providerId,
[In]EtwEnableCallback enableCallback,
[In]void* callbackContext,
[In][Out]ref long registrationHandle
);

//
[SecurityCritical]
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2118:ReviewSuppressUnmanagedCodeSecurityUsage")]
[DllImport(Win32Native.ADVAPI32, ExactSpelling = true, EntryPoint = "EventUnregister", CharSet = System.Runtime.InteropServices.CharSet.Unicode)]
internal static extern uint EventUnregister([In] long registrationHandle);

//
// Writing (Publishing/Logging) APIs
//
//
[SecurityCritical]
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2118:ReviewSuppressUnmanagedCodeSecurityUsage")]
[DllImport(Win32Native.ADVAPI32, ExactSpelling = true, EntryPoint = "EventWrite", CharSet = System.Runtime.InteropServices.CharSet.Unicode)]
internal static extern unsafe int EventWrite(
[In] long registrationHandle,
[In] ref EventDescriptor eventDescriptor,
[In] int userDataCount,
[In] EventProvider.EventData* userData
);

[SecurityCritical]
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2118:ReviewSuppressUnmanagedCodeSecurityUsage")]
[DllImport(Win32Native.ADVAPI32, ExactSpelling = true, EntryPoint = "EventWriteString", CharSet = System.Runtime.InteropServices.CharSet.Unicode)]
internal static extern unsafe int EventWriteString(
[In] long registrationHandle,
[In] byte level,
[In] long keyword,
[In] string msg
);

[StructLayout(LayoutKind.Sequential)]
unsafe internal struct EVENT_FILTER_DESCRIPTOR
{
public long Ptr;
public int Size;
public int Type;
};

/// <summary>
/// Call the ETW native API EventWriteTransfer and checks for invalid argument error.
/// The implementation of EventWriteTransfer on some older OSes (Windows 2008) does not accept null relatedActivityId.
/// So, for these cases we will retry the call with an empty Guid.
/// </summary>
internal static int EventWriteTransferWrapper(long registrationHandle,
ref EventDescriptor eventDescriptor,
Guid* activityId,
Guid* relatedActivityId,
int userDataCount,
EventProvider.EventData* userData)
{
int HResult = EventWriteTransfer(registrationHandle, ref eventDescriptor, activityId, relatedActivityId, userDataCount, userData);
if (HResult == ERROR_INVALID_PARAMETER && relatedActivityId == null)
{
Guid emptyGuid = Guid.Empty;
HResult = EventWriteTransfer(registrationHandle, ref eventDescriptor, activityId, &emptyGuid, userDataCount, userData);
}

return HResult;
}

[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2118:ReviewSuppressUnmanagedCodeSecurityUsage")]
[DllImport(Win32Native.ADVAPI32, ExactSpelling = true, EntryPoint = "EventWriteTransfer", CharSet = System.Runtime.InteropServices.CharSet.Unicode)]
[SuppressUnmanagedCodeSecurityAttribute] // Don't do security checks
private static extern int EventWriteTransfer(
[In] long registrationHandle,
[In] ref EventDescriptor eventDescriptor,
[In] Guid* activityId,
[In] Guid* relatedActivityId,
[In] int userDataCount,
[In] EventProvider.EventData* userData
);

internal enum ActivityControl : uint
{
EVENT_ACTIVITY_CTRL_GET_ID = 1,
EVENT_ACTIVITY_CTRL_SET_ID = 2,
EVENT_ACTIVITY_CTRL_CREATE_ID = 3,
EVENT_ACTIVITY_CTRL_GET_SET_ID = 4,
EVENT_ACTIVITY_CTRL_CREATE_SET_ID = 5
};

[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2118:ReviewSuppressUnmanagedCodeSecurityUsage")]
[DllImport(Win32Native.ADVAPI32, ExactSpelling = true, EntryPoint = "EventActivityIdControl", CharSet = System.Runtime.InteropServices.CharSet.Unicode)]
[SuppressUnmanagedCodeSecurityAttribute] // Don't do security checks
internal static extern int EventActivityIdControl([In] ActivityControl ControlCode, [In][Out] ref Guid ActivityId);

internal enum EVENT_INFO_CLASS
{
BinaryTrackInfo,
SetEnableAllKeywords,
SetTraits,
}

[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2118:ReviewSuppressUnmanagedCodeSecurityUsage")]
[DllImport(Win32Native.ADVAPI32, ExactSpelling = true, EntryPoint = "EventSetInformation", CharSet = System.Runtime.InteropServices.CharSet.Unicode)]
[SuppressUnmanagedCodeSecurityAttribute] // Don't do security checks
internal static extern int EventSetInformation(
[In] long registrationHandle,
[In] EVENT_INFO_CLASS informationClass,
[In] void* eventInformation,
[In] int informationLength);

// Support for EnumerateTraceGuidsEx
internal enum TRACE_QUERY_INFO_CLASS
{
TraceGuidQueryList,
TraceGuidQueryInfo,
TraceGuidQueryProcess,
TraceStackTracingInfo,
MaxTraceSetInfoClass
};

internal struct TRACE_GUID_INFO
{
public int InstanceCount;
public int Reserved;
};

internal struct TRACE_PROVIDER_INSTANCE_INFO
{
public int NextOffset;
public int EnableCount;
public int Pid;
public int Flags;
};

internal struct TRACE_ENABLE_INFO
{
public int IsEnabled;
public byte Level;
public byte Reserved1;
public ushort LoggerId;
public int EnableProperty;
public int Reserved2;
public long MatchAnyKeyword;
public long MatchAllKeyword;
};

[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2118:ReviewSuppressUnmanagedCodeSecurityUsage")]
[DllImport(Win32Native.ADVAPI32, ExactSpelling = true, EntryPoint = "EnumerateTraceGuidsEx", CharSet = System.Runtime.InteropServices.CharSet.Unicode)]
[SuppressUnmanagedCodeSecurityAttribute] // Don't do security checks
internal static extern int EnumerateTraceGuidsEx(
TRACE_QUERY_INFO_CLASS TraceQueryInfoClass,
void* InBuffer,
int InBufferSize,
void* OutBuffer,
int OutBufferSize,
ref int ReturnLength);

}
}
}
3 changes: 3 additions & 0 deletions mcs/class/corlib/ReferenceSources/win32native.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,5 +79,8 @@ internal class WIN32_FIND_DATA
internal int dwFileAttributes = 0;
internal String cFileName = null;
}

[DllImport("kernel32.dll", CharSet=CharSet.Auto, SetLastError=true)]
internal static extern uint GetCurrentProcessId();
}
}
54 changes: 37 additions & 17 deletions mcs/class/corlib/corlib-net_4_x.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -293,11 +293,47 @@
<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\eventing\eventactivityoptions.cs" />
<Compile Include="..\referencesource\mscorlib\system\diagnostics\eventing\eventdescriptor.cs" />
<Compile Include="..\referencesource\mscorlib\system\diagnostics\eventing\eventprovider.cs" />
<Compile Include="..\referencesource\mscorlib\system\diagnostics\eventing\eventsource.cs" />
<Compile Include="..\referencesource\mscorlib\system\diagnostics\eventing\eventsourceexception.cs" />
<Compile Include="..\referencesource\mscorlib\system\diagnostics\eventing\StubEnvironment.cs" />
<Compile Include="..\referencesource\mscorlib\system\diagnostics\eventing\winmeta.cs" />
<Compile Include="..\..\..\external\corefx-bugfix\src\Common\src\CoreLib\System\Diagnostics\Tracing\EventCounter.cs" />
<Compile Include="..\..\..\external\corefx-bugfix\src\Common\src\CoreLib\System\Diagnostics\Tracing\unsafenativemethods.cs" />
<Compile Include="..\referencesource\mscorlib\system\diagnostics\eventing\TraceLogging\ArrayTypeInfo.cs" />
<Compile Include="..\referencesource\mscorlib\system\diagnostics\eventing\TraceLogging\ConcurrentSet.cs" />
<Compile Include="..\referencesource\mscorlib\system\diagnostics\eventing\TraceLogging\ConcurrentSetItem.cs" />
<Compile Include="..\referencesource\mscorlib\system\diagnostics\eventing\TraceLogging\DataCollector.cs" />
<Compile Include="..\referencesource\mscorlib\system\diagnostics\eventing\TraceLogging\EmptyStruct.cs" />
<Compile Include="..\referencesource\mscorlib\system\diagnostics\eventing\TraceLogging\EnumerableTypeInfo.cs" />
<Compile Include="..\referencesource\mscorlib\system\diagnostics\eventing\TraceLogging\EnumHelper.cs" />
<Compile Include="..\referencesource\mscorlib\system\diagnostics\eventing\TraceLogging\EventDataAttribute.cs" />
<Compile Include="..\referencesource\mscorlib\system\diagnostics\eventing\TraceLogging\EventFieldAttribute.cs" />
<Compile Include="..\referencesource\mscorlib\system\diagnostics\eventing\TraceLogging\EventFieldFormat.cs" />
<Compile Include="..\referencesource\mscorlib\system\diagnostics\eventing\TraceLogging\EventIgnoreAttribute.cs" />
<Compile Include="..\referencesource\mscorlib\system\diagnostics\eventing\TraceLogging\EventPayload.cs" />
<Compile Include="..\referencesource\mscorlib\system\diagnostics\eventing\TraceLogging\EventSourceActivity.cs" />
<Compile Include="..\referencesource\mscorlib\system\diagnostics\eventing\TraceLogging\EventSourceOptions.cs" />
<Compile Include="..\referencesource\mscorlib\system\diagnostics\eventing\TraceLogging\FieldMetadata.cs" />
<Compile Include="..\referencesource\mscorlib\system\diagnostics\eventing\TraceLogging\InvokeTypeInfo.cs" />
<Compile Include="..\referencesource\mscorlib\system\diagnostics\eventing\TraceLogging\NameInfo.cs" />
<Compile Include="..\referencesource\mscorlib\system\diagnostics\eventing\TraceLogging\PropertyAccessor.cs" />
<Compile Include="..\referencesource\mscorlib\system\diagnostics\eventing\TraceLogging\PropertyAnalysis.cs" />
<Compile Include="..\referencesource\mscorlib\system\diagnostics\eventing\TraceLogging\SimpleEventTypes.cs" />
<Compile Include="..\referencesource\mscorlib\system\diagnostics\eventing\TraceLogging\SimpleTypeInfos.cs" />
<Compile Include="..\referencesource\mscorlib\system\diagnostics\eventing\TraceLogging\Statics.cs" />
<Compile Include="..\referencesource\mscorlib\system\diagnostics\eventing\TraceLogging\TraceLoggingDataCollector.cs" />
<Compile Include="..\referencesource\mscorlib\system\diagnostics\eventing\TraceLogging\TraceLoggingDataType.cs" />
<Compile Include="..\referencesource\mscorlib\system\diagnostics\eventing\TraceLogging\TraceLoggingEventSource.cs" />
<Compile Include="..\referencesource\mscorlib\system\diagnostics\eventing\TraceLogging\TraceLoggingEventTraits.cs" />
<Compile Include="..\referencesource\mscorlib\system\diagnostics\eventing\winmeta.cs" />
<Compile Include="..\referencesource\mscorlib\system\diagnostics\eventing\TraceLogging\TraceLoggingEventTypes.cs" />
<Compile Include="..\referencesource\mscorlib\system\diagnostics\eventing\TraceLogging\TraceLoggingMetadataCollector.cs" />
<Compile Include="..\referencesource\mscorlib\system\diagnostics\eventing\TraceLogging\TraceLoggingTypeInfo.cs" />
<Compile Include="..\referencesource\mscorlib\system\diagnostics\eventing\TraceLogging\TraceLoggingTypeInfo_T.cs" />
<Compile Include="..\referencesource\mscorlib\system\diagnostics\eventing\TraceLogging\TypeAnalysis.cs" />
<Compile Include="..\referencesource\mscorlib\system\dividebyzeroexception.cs" />
<Compile Include="..\referencesource\mscorlib\system\dllnotfoundexception.cs" />
<Compile Include="..\referencesource\mscorlib\system\double.cs" />
Expand Down Expand Up @@ -980,22 +1016,6 @@
<Compile Include="System.Diagnostics.SymbolStore\SymDocumentType.cs" />
<Compile Include="System.Diagnostics.SymbolStore\SymLanguageType.cs" />
<Compile Include="System.Diagnostics.SymbolStore\SymLanguageVendor.cs" />
<Compile Include="System.Diagnostics.Tracing\EventAttribute.cs" />
<Compile Include="System.Diagnostics.Tracing\EventCommand.cs" />
<Compile Include="System.Diagnostics.Tracing\EventCommandEventArgs.cs" />
<Compile Include="System.Diagnostics.Tracing\EventCounter.cs" />
<Compile Include="System.Diagnostics.Tracing\EventDataAttribute.cs" />
<Compile Include="System.Diagnostics.Tracing\EventFieldAttribute.cs" />
<Compile Include="System.Diagnostics.Tracing\EventFieldFormat.cs" />
<Compile Include="System.Diagnostics.Tracing\EventFieldTags.cs" />
<Compile Include="System.Diagnostics.Tracing\EventIgnoreAttribute.cs" />
<Compile Include="System.Diagnostics.Tracing\EventListener.cs" />
<Compile Include="System.Diagnostics.Tracing\EventManifestOptions.cs" />
<Compile Include="System.Diagnostics.Tracing\EventSource.cs" />
<Compile Include="System.Diagnostics.Tracing\EventSourceAttribute.cs" />
<Compile Include="System.Diagnostics.Tracing\EventSourceSettings.cs" />
<Compile Include="System.Diagnostics.Tracing\EventWrittenEventArgs.cs" />
<Compile Include="System.Diagnostics.Tracing\NonEventAttribute.cs" />
<Compile Include="System.Diagnostics\Debugger.cs" />
<Compile Include="System.Diagnostics\StackFrame.cs" />
<Compile Include="System.Diagnostics\StackTrace.cs" />
Expand Down
Loading