Re-Factor EventSource to Support Writing to EventPipe (#11435)
[platform/upstream/coreclr.git] / src / mscorlib / src / System / Diagnostics / Eventing / EventPipeEventProvider.cs
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 // See the LICENSE file in the project root for more information.
4 using System.Collections.Concurrent;
5 using System.Runtime.CompilerServices;
6 using System.Runtime.InteropServices;
7 using System.Security;
8 using Microsoft.Win32;
9
10 namespace System.Diagnostics.Tracing
11 {
12     internal sealed class EventPipeEventProvider : IEventProvider
13     {
14         // The EventPipeProvider handle.
15         private IntPtr m_provHandle = IntPtr.Zero;
16
17         // Register an event provider.
18         unsafe uint IEventProvider.EventRegister(
19             ref Guid providerId,
20             UnsafeNativeMethods.ManifestEtw.EtwEnableCallback enableCallback,
21             void* callbackContext,
22             ref long registrationHandle)
23         {
24             uint returnStatus = 0;
25             m_provHandle = EventPipeInternal.CreateProvider(providerId, enableCallback);
26             if(m_provHandle != IntPtr.Zero)
27             {
28                 // Fixed registration handle because a new EventPipeEventProvider
29                 // will be created for each new EventSource.
30                 registrationHandle = 1;
31             }
32             else
33             {
34                 // Unable to create the provider.
35                 returnStatus = 1;
36             }
37
38             return returnStatus;
39         }
40
41         // Unregister an event provider.
42         uint IEventProvider.EventUnregister(long registrationHandle)
43         {
44             EventPipeInternal.DeleteProvider(m_provHandle);
45             return 0;
46         }
47
48         // Write an event.
49         unsafe int IEventProvider.EventWriteTransferWrapper(
50             long registrationHandle,
51             ref EventDescriptor eventDescriptor,
52             Guid* activityId,
53             Guid* relatedActivityId,
54             int userDataCount,
55             EventProvider.EventData* userData)
56         {
57             return 0;
58         }
59
60         // Get or set the per-thread activity ID.
61         int IEventProvider.EventActivityIdControl(UnsafeNativeMethods.ManifestEtw.ActivityControl ControlCode, ref Guid ActivityId)
62         {
63             return 0;
64         }
65     }
66
67     // PInvokes into the runtime used to interact with the EventPipe.
68     internal static class EventPipeInternal
69     {
70         [DllImport(JitHelpers.QCall, CharSet = CharSet.Unicode)]
71         [SuppressUnmanagedCodeSecurity]
72         internal static extern IntPtr CreateProvider(Guid providerID, UnsafeNativeMethods.ManifestEtw.EtwEnableCallback callbackFunc);
73
74         [DllImport(JitHelpers.QCall, CharSet = CharSet.Unicode)]
75         [SuppressUnmanagedCodeSecurity]
76         internal static extern IntPtr AddEvent(IntPtr provHandle, Int64 keywords, uint eventID, uint eventVersion, uint level, bool needStack);
77
78         [DllImport(JitHelpers.QCall, CharSet = CharSet.Unicode)]
79         [SuppressUnmanagedCodeSecurity]
80         internal static extern void DeleteProvider(IntPtr provHandle);
81
82         [DllImport(JitHelpers.QCall, CharSet = CharSet.Unicode)]
83         [SuppressUnmanagedCodeSecurity]
84         internal static extern unsafe void WriteEvent(IntPtr eventHandle, void* data, uint length);
85     }
86 }