|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | +using System.Runtime; |
| 4 | +using System.Runtime.InteropServices; |
| 5 | + |
| 6 | +#if FEATURE_PERFTRACING |
| 7 | + |
| 8 | +namespace System.Diagnostics.Tracing |
| 9 | +{ |
| 10 | + // |
| 11 | + // NOTE: |
| 12 | + // |
| 13 | + // The implementation below takes some manual marshaling actions to ensure arguments are in |
| 14 | + // primitive form before they are passed through to the underlying RuntimeImports.Rh* |
| 15 | + // function. |
| 16 | + // |
| 17 | + // These extra steps are necessary only if the RuntimeImports mechanism represents "raw" |
| 18 | + // calls into the native runtime (as has been the case at least in the distant past). |
| 19 | + // |
| 20 | + // If the RuntimeImports mechanism automatically applies rich p/invoke marshaling to all of |
| 21 | + // these calls, then all of the manual steps below are unnecessary and can be removed (by |
| 22 | + // making the RuntimeImports.Rh* function signatures generally match the corresponding |
| 23 | + // EventPipeInternal function signatures; in other words, by making the RuntimeImports.Rh* |
| 24 | + // functions look like the QCalls in EventPipe.CoreCLR.cs). |
| 25 | + // |
| 26 | + internal static partial class EventPipeInternal |
| 27 | + { |
| 28 | + // |
| 29 | + // These PInvokes are used by the configuration APIs to interact with EventPipe. |
| 30 | + // |
| 31 | + private static unsafe ulong Enable( |
| 32 | + char* outputFile, |
| 33 | + EventPipeSerializationFormat format, |
| 34 | + uint circularBufferSizeInMB, |
| 35 | + EventPipeProviderConfigurationNative* providers, |
| 36 | + uint numProviders) |
| 37 | + { |
| 38 | + return RuntimeImports.RhEventPipeInternal_Enable( |
| 39 | + outputFile, |
| 40 | + (int)format, |
| 41 | + circularBufferSizeInMB, |
| 42 | + providers, |
| 43 | + numProviders); |
| 44 | + } |
| 45 | + |
| 46 | + internal static void Disable(ulong sessionID) |
| 47 | + { |
| 48 | + RuntimeImports.RhEventPipeInternal_Disable(sessionID); |
| 49 | + } |
| 50 | + |
| 51 | + // |
| 52 | + // These PInvokes are used by EventSource to interact with the EventPipe. |
| 53 | + // |
| 54 | + |
| 55 | +// private static extern unsafe IntPtr CreateProvider(string providerName, IntPtr callbackFunc, IntPtr callbackContext); |
| 56 | + |
| 57 | + internal static unsafe IntPtr CreateProvider(string providerName, |
| 58 | + delegate* unmanaged<byte*, int, byte, long, long, Interop.Advapi32.EVENT_FILTER_DESCRIPTOR*, void*, void> callbackFunc, |
| 59 | + void* callbackContext) |
| 60 | + => CreateProvider(providerName, (IntPtr)callbackFunc, (IntPtr)callbackContext); |
| 61 | + //internal static unsafe IntPtr CreateProvider(string providerName, IntPtr callbackFunc, IntPtr callbackContext); |
| 62 | + |
| 63 | + internal static unsafe IntPtr CreateProvider(string providerName, IntPtr callbackFunc, IntPtr callbackContext) |
| 64 | + { |
| 65 | + fixed (char* pProviderName = providerName) |
| 66 | + { |
| 67 | + return RuntimeImports.RhEventPipeInternal_CreateProvider( |
| 68 | + pProviderName, |
| 69 | + callbackFunc, |
| 70 | + callbackContext); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + internal static unsafe IntPtr DefineEvent( |
| 75 | + IntPtr provHandle, |
| 76 | + uint eventID, |
| 77 | + long keywords, |
| 78 | + uint eventVersion, |
| 79 | + uint level, |
| 80 | + void *pMetadata, |
| 81 | + uint metadataLength) |
| 82 | + { |
| 83 | + return RuntimeImports.RhEventPipeInternal_DefineEvent( |
| 84 | + provHandle, |
| 85 | + eventID, |
| 86 | + keywords, |
| 87 | + eventVersion, |
| 88 | + level, |
| 89 | + pMetadata, |
| 90 | + metadataLength); |
| 91 | + } |
| 92 | + |
| 93 | + internal static unsafe IntPtr GetProvider(string providerName) |
| 94 | + { |
| 95 | + fixed (char* pProviderName = providerName) |
| 96 | + { |
| 97 | + return RuntimeImports.RhEventPipeInternal_GetProvider(pProviderName); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + internal static void DeleteProvider(IntPtr provHandle) |
| 102 | + { |
| 103 | + RuntimeImports.RhEventPipeInternal_DeleteProvider(provHandle); |
| 104 | + } |
| 105 | + |
| 106 | + internal static unsafe int EventActivityIdControl(uint controlCode, ref Guid activityId) |
| 107 | + { |
| 108 | + // |
| 109 | + // Ensure that the address passed to native code is never on the managed heap, while still |
| 110 | + // managing the supplied byref in an in/out manner. |
| 111 | + // |
| 112 | + Guid localActivityId = activityId; |
| 113 | + try { return RuntimeImports.RhEventPipeInternal_EventActivityIdControl(controlCode, &localActivityId); } |
| 114 | + finally { activityId = localActivityId; } |
| 115 | + } |
| 116 | + |
| 117 | + internal static unsafe void WriteEventData( |
| 118 | + IntPtr eventHandle, |
| 119 | + EventProvider.EventData* pEventData, |
| 120 | + uint dataCount, |
| 121 | + Guid* activityId, |
| 122 | + Guid* relatedActivityId) |
| 123 | + { |
| 124 | + RuntimeImports.RhEventPipeInternal_WriteEventData( |
| 125 | + eventHandle, |
| 126 | + pEventData, |
| 127 | + dataCount, |
| 128 | + activityId, |
| 129 | + relatedActivityId); |
| 130 | + } |
| 131 | + |
| 132 | + // |
| 133 | + // These PInvokes are used as part of the EventPipeEventDispatcher. |
| 134 | + // |
| 135 | + internal static unsafe bool GetSessionInfo(ulong sessionID, EventPipeSessionInfo* pSessionInfo) |
| 136 | + { |
| 137 | + uint rawBool = RuntimeImports.RhEventPipeInternal_GetSessionInfo(sessionID, pSessionInfo); |
| 138 | + return (rawBool != 0); |
| 139 | + } |
| 140 | + |
| 141 | + internal static unsafe bool GetNextEvent(ulong sessionID, EventPipeEventInstanceData* pInstance) |
| 142 | + { |
| 143 | + uint rawBool = RuntimeImports.RhEventPipeInternal_GetNextEvent(sessionID, pInstance); |
| 144 | + return (rawBool != 0); |
| 145 | + } |
| 146 | + |
| 147 | + internal static bool SignalSession(ulong sessionID) |
| 148 | + { |
| 149 | + uint rawBool = RuntimeImports.RhEventPipeInternal_SignalSession(sessionID); |
| 150 | + return (rawBool != 0); |
| 151 | + } |
| 152 | + |
| 153 | + internal static bool WaitForSessionSignal(ulong sessionID, int timeoutMs) |
| 154 | + { |
| 155 | + uint rawBool = RuntimeImports.RhEventPipeInternal_WaitForSessionSignal(sessionID, timeoutMs); |
| 156 | + return (rawBool != 0); |
| 157 | + } |
| 158 | + } |
| 159 | +} |
| 160 | + |
| 161 | +#endif // FEATURE_PERFTRACING |
| 162 | + |
0 commit comments