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.
5 #ifndef __EVENTPIPE_H__
6 #define __EVENTPIPE_H__
8 #ifdef FEATURE_PERFTRACING
11 #include "eventpipeprovider.h"
12 #include "stackwalk.h"
14 class EventPipeConfiguration;
17 class EventPipeJsonFile;
19 class SampleProfilerEventInstance;
25 const static unsigned int MAX_STACK_DEPTH = 100;
27 // Array of IP values from a stack crawl.
28 // Top of stack is at index 0.
29 UINT_PTR m_stackFrames[MAX_STACK_DEPTH];
31 // Parallel array of MethodDesc pointers.
32 // Used for debug-only stack printing.
33 MethodDesc* m_methods[MAX_STACK_DEPTH];
35 // The next available slot in StackFrames.
36 unsigned int m_nextAvailableFrame;
42 LIMITED_METHOD_CONTRACT;
49 LIMITED_METHOD_CONTRACT;
51 m_nextAvailableFrame = 0;
56 LIMITED_METHOD_CONTRACT;
58 return (m_nextAvailableFrame == 0);
61 unsigned int GetLength()
63 LIMITED_METHOD_CONTRACT;
65 return m_nextAvailableFrame;
68 UINT_PTR GetIP(unsigned int frameIndex)
70 LIMITED_METHOD_CONTRACT;
71 _ASSERTE(frameIndex < MAX_STACK_DEPTH);
73 if (frameIndex >= MAX_STACK_DEPTH)
78 return m_stackFrames[frameIndex];
81 MethodDesc* GetMethod(unsigned int frameIndex)
83 LIMITED_METHOD_CONTRACT;
84 _ASSERTE(frameIndex < MAX_STACK_DEPTH);
86 if (frameIndex >= MAX_STACK_DEPTH)
91 return m_methods[frameIndex];
94 void Append(UINT_PTR controlPC, MethodDesc *pMethod)
96 LIMITED_METHOD_CONTRACT;
98 if(m_nextAvailableFrame < MAX_STACK_DEPTH)
100 m_stackFrames[m_nextAvailableFrame] = controlPC;
101 m_methods[m_nextAvailableFrame] = pMethod;
102 m_nextAvailableFrame++;
106 BYTE* GetPointer() const
108 LIMITED_METHOD_CONTRACT;
110 return (BYTE*)m_stackFrames;
113 unsigned int GetSize() const
115 LIMITED_METHOD_CONTRACT;
117 return (m_nextAvailableFrame * sizeof(UINT_PTR));
124 friend class EventPipeConfiguration;
125 friend class EventPipeFile;
126 friend class EventPipeProvider;
127 friend class SampleProfiler;
131 // Initialize the event pipe.
132 static void Initialize();
134 // Shutdown the event pipe.
135 static void Shutdown();
137 // Enable tracing from the start-up path based on COMPLUS variable.
138 static void EnableOnStartup();
140 // Enable tracing via the event pipe.
141 static void Enable();
143 // Disable tracing via the event pipe.
144 static void Disable();
146 // Write out an event.
147 // Data is written as a serialized blob matching the ETW serialization conventions.
148 static void WriteEvent(EventPipeEvent &event, BYTE *pData, unsigned int length);
150 // Write out a sample profile event.
151 static void WriteSampleProfileEvent(SampleProfilerEventInstance &instance);
153 // Get the managed call stack for the current thread.
154 static bool WalkManagedStackForCurrentThread(StackContents &stackContents);
156 // Get the managed call stack for the specified thread.
157 static bool WalkManagedStackForThread(Thread *pThread, StackContents &stackContents);
161 // Callback function for the stack walker. For each frame walked, this callback is invoked.
162 static StackWalkAction StackWalkCallback(CrawlFrame *pCf, StackContents *pData);
164 // Get the configuration object.
165 // This is called directly by the EventPipeProvider constructor to register the new provider.
166 static EventPipeConfiguration* GetConfiguration();
168 // Get the event pipe configuration lock.
169 static CrstStatic* GetLock();
171 static CrstStatic s_configCrst;
172 static bool s_tracingInitialized;
173 static EventPipeConfiguration *s_pConfig;
174 static EventPipeFile *s_pFile;
175 static EventPipeJsonFile *s_pJsonFile;
178 class EventPipeInternal
183 static INT_PTR QCALLTYPE CreateProvider(
185 EventPipeCallback pCallbackFunc);
187 static INT_PTR QCALLTYPE AddEvent(
190 unsigned int eventID,
191 unsigned int eventVersion,
195 static void QCALLTYPE DeleteProvider(
198 static void QCALLTYPE WriteEvent(
201 unsigned int length);
204 #endif // FEATURE_PERFTRACING
206 #endif // __EVENTPIPE_H__