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.
9 // Declaration of helper classes used for miscellaneous purposes within the
13 // ======================================================================================
15 #ifndef __PROFILING_HELPER_H__
16 #define __PROFILING_HELPER_H__
18 #ifndef PROFILING_SUPPORTED
19 #error PROFILING_SUPPORTED is not set. Do not include ProfilingHelper.h.
25 #include "eeprofinterfaces.h"
27 #define COM_METHOD HRESULT STDMETHODCALLTYPE
30 // On DEBUG builds, setting the COMPlus_ProfAPIFault to a bitmask of the flags
31 // below forces the Profiling API to return failures at various points.
32 // Useful for event log testing. Also see code:ProfilingAPIUtility.ShouldInjectProfAPIFault
33 enum ProfAPIFaultFlags
35 // Forces the startup path to log an IDS_E_PROF_INTERNAL_INIT error
36 kProfAPIFault_StartupInternal = 0x00001,
42 //---------------------------------------------------------------------------------------
43 // Static-only class to coordinate initialization of the various profiling API
44 // structures, plus other utility stuff.
46 class ProfilingAPIUtility
49 enum ProfilerCompatibilityFlag
51 // Default: disable V2 profiler
52 kDisableV2Profiler = 0x0,
54 // Enable V2 profilers
55 kEnableV2Profiler = 0x1,
62 static HRESULT InitializeProfiling();
63 static HRESULT LoadProfilerForAttach(
65 LPCWSTR wszProfilerDLL,
68 DWORD dwConcurrentGCWaitTimeoutInMs);
70 static BOOL IsProfilerEvacuated();
71 static void TerminateProfiling();
72 static void LogProfError(int iStringResourceID, ...);
73 static void LogProfInfo(int iStringResourceID, ...);
74 static void LogNoInterfaceError(REFIID iidRequested, LPCWSTR wszClsid);
75 INDEBUG(static BOOL ShouldInjectProfAPIFault(ProfAPIFaultFlags faultFlag);)
77 static HRESULT GetCurrentProcessUserSid(PSID * ppsid);
78 #endif // !FEATURE_PAL
80 #ifdef FEATURE_PROFAPI_ATTACH_DETACH
81 // ----------------------------------------------------------------------------
82 // ProfilingAPIUtility::IncEvacuationCounter
85 // Simple helper to increase the evacuation counter inside an EE thread by one
88 // * pThread - pointer to an EE Thread
90 template<typename ThreadType>
91 static FORCEINLINE void IncEvacuationCounter(ThreadType * pThread)
93 LIMITED_METHOD_CONTRACT;
96 pThread->IncProfilerEvacuationCounter();
99 // ----------------------------------------------------------------------------
100 // ProfilingAPIUtility::DecEvacuationCounter
103 // Simple helper to decrease the evacuation counter inside an EE thread by one
106 // * pThread - pointer to an EE Thread
108 template<typename ThreadType>
109 static FORCEINLINE void DecEvacuationCounter(ThreadType * pThread)
111 LIMITED_METHOD_CONTRACT;
114 pThread->DecProfilerEvacuationCounter();
117 #endif // FEATURE_PROFAPI_ATTACH_DETACH
119 // See code:ProfilingAPIUtility::InitializeProfiling#LoadUnloadCallbackSynchronization
120 static CRITSEC_COOKIE GetStatusCrst();
123 // ---------------------------------------------------------------------------------------
124 // Enum used in LoadProfiler() to differentiate whether we're loading the profiler
125 // for startup or for attach
132 // Allocated lazily the first time it's needed, and then remains allocated until the
134 static SidBuffer * s_pSidBuffer;
136 // See code:ProfilingAPIUtility::InitializeProfiling#LoadUnloadCallbackSynchronization
137 static CRITSEC_COOKIE s_csStatus;
139 // Static-only class. Private constructor enforces you don't try to make an instance
140 ProfilingAPIUtility() {}
142 static HRESULT PerformDeferredInit();
143 static HRESULT DoPreInitialization(
144 EEToProfInterfaceImpl *pEEProf,
147 LPCWSTR wszProfilerDLL,
149 DWORD dwConcurrentGCWaitTimeoutInMs);
150 static HRESULT LoadProfiler(
152 const CLSID * pClsid,
154 LPCWSTR wszProfilerDLL,
157 DWORD dwConcurrentGCWaitTimeoutInMs = INFINITE);
158 static HRESULT ProfilerCLSIDFromString(__inout_z LPWSTR wszClsid, CLSID * pClsid);
159 static HRESULT AttemptLoadProfilerForStartup();
161 static void AppendSupplementaryInformation(int iStringResource, SString * pString);
163 static void LogProfEventVA(
164 int iStringResourceID,
166 va_list insertionArgs);
170 //---------------------------------------------------------------------------------------
171 // When we call into profiler code, we push one of these babies onto the stack to
172 // remember on the Thread how the profiler was called. If the profiler calls back into us,
173 // we use the flags that this set to authorize.
175 class SetCallbackStateFlagsHolder
178 SetCallbackStateFlagsHolder(DWORD dwFlags);
179 ~SetCallbackStateFlagsHolder();
183 DWORD m_dwOriginalFullState;
186 #endif //__PROFILING_HELPER_H__