[Tizen] Unify dnetmemoryenumlib terms to match the codebase (#291)
[platform/upstream/coreclr.git] / src / vm / profilinghelper.h
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 //
5 // ProfilingHelper.h
6 // 
7
8 //
9 // Declaration of helper classes used for miscellaneous purposes within the
10 // profiling API
11 //
12
13 // ======================================================================================
14
15 #ifndef __PROFILING_HELPER_H__
16 #define __PROFILING_HELPER_H__
17
18 #ifndef PROFILING_SUPPORTED
19 #error PROFILING_SUPPORTED is not set. Do not include ProfilingHelper.h.
20 #endif
21
22 #include <windows.h>
23
24 #include "corprof.h"
25 #include "eeprofinterfaces.h"
26
27 #define COM_METHOD HRESULT STDMETHODCALLTYPE
28
29 #ifdef _DEBUG
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
34 {
35     // Forces the startup path to log an IDS_E_PROF_INTERNAL_INIT error
36     kProfAPIFault_StartupInternal   = 0x00001,
37 };
38 #endif // _DEBUG
39
40 class SidBuffer;
41
42 //---------------------------------------------------------------------------------------
43 // Static-only class to coordinate initialization of the various profiling API
44 // structures, plus other utility stuff.
45 //
46 class ProfilingAPIUtility
47 {
48 private:    
49     enum ProfilerCompatibilityFlag
50     {
51         // Default: disable V2 profiler
52         kDisableV2Profiler = 0x0,
53
54         // Enable V2 profilers
55         kEnableV2Profiler  = 0x1,
56
57         // Disable Profiling
58         kPreventLoad       = 0x2,
59     };
60
61 public:
62     static HRESULT InitializeProfiling();
63     static HRESULT LoadProfilerForAttach(
64         const CLSID * pClsid,
65         LPCWSTR wszProfilerDLL,
66         LPVOID pvClientData,
67         UINT cbClientData,
68         DWORD dwConcurrentGCWaitTimeoutInMs);
69
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);)
76 #ifndef FEATURE_PAL
77     static HRESULT GetCurrentProcessUserSid(PSID * ppsid);
78 #endif // !FEATURE_PAL
79
80 #ifdef FEATURE_PROFAPI_ATTACH_DETACH
81     // ----------------------------------------------------------------------------
82     // ProfilingAPIUtility::IncEvacuationCounter
83     //
84     // Description: 
85     //    Simple helper to increase the evacuation counter inside an EE thread by one
86     //
87     // Arguments:
88     //    * pThread - pointer to an EE Thread
89     //
90     template<typename ThreadType>
91     static FORCEINLINE void IncEvacuationCounter(ThreadType * pThread) 
92     {
93         LIMITED_METHOD_CONTRACT;
94
95         if (pThread) 
96             pThread->IncProfilerEvacuationCounter();
97     }
98
99     // ----------------------------------------------------------------------------
100     // ProfilingAPIUtility::DecEvacuationCounter
101     //
102     // Description: 
103     //    Simple helper to decrease the evacuation counter inside an EE thread by one
104     //    
105     // Arguments:
106     //    * pThread - pointer to an EE Thread
107     //
108     template<typename ThreadType>
109     static FORCEINLINE void DecEvacuationCounter(ThreadType * pThread) 
110     {
111         LIMITED_METHOD_CONTRACT;
112
113         if (pThread) 
114             pThread->DecProfilerEvacuationCounter();
115     }
116
117 #endif // FEATURE_PROFAPI_ATTACH_DETACH
118
119     // See code:ProfilingAPIUtility::InitializeProfiling#LoadUnloadCallbackSynchronization
120     static CRITSEC_COOKIE GetStatusCrst();
121
122 private:
123     // ---------------------------------------------------------------------------------------
124     // Enum used in LoadProfiler() to differentiate whether we're loading the profiler
125     // for startup or for attach
126     enum LoadType
127     {
128         kStartupLoad,
129         kAttachLoad,
130     };
131
132     // Allocated lazily the first time it's needed, and then remains allocated until the
133     // process exits.
134     static SidBuffer * s_pSidBuffer;
135
136     // See code:ProfilingAPIUtility::InitializeProfiling#LoadUnloadCallbackSynchronization
137     static CRITSEC_COOKIE s_csStatus;
138
139     // Static-only class.  Private constructor enforces you don't try to make an instance
140     ProfilingAPIUtility() {}
141
142     static HRESULT PerformDeferredInit();
143     static HRESULT DoPreInitialization(
144         EEToProfInterfaceImpl *pEEProf,
145         const CLSID *pClsid, 
146         LPCWSTR wszClsid, 
147         LPCWSTR wszProfilerDLL, 
148         LoadType loadType, 
149         DWORD dwConcurrentGCWaitTimeoutInMs);
150     static HRESULT LoadProfiler(
151         LoadType loadType,
152         const CLSID * pClsid,
153         LPCWSTR wszClsid,
154         LPCWSTR wszProfilerDLL,
155         LPVOID pvClientData,
156         UINT cbClientData,
157         DWORD dwConcurrentGCWaitTimeoutInMs = INFINITE);
158     static HRESULT ProfilerCLSIDFromString(__inout_z LPWSTR wszClsid, CLSID * pClsid);
159     static HRESULT AttemptLoadProfilerForStartup();
160
161     static void AppendSupplementaryInformation(int iStringResource, SString * pString);
162
163     static void LogProfEventVA(
164         int iStringResourceID, 
165         WORD wEventType,
166         va_list insertionArgs);
167 };
168
169
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.
174 //
175 class SetCallbackStateFlagsHolder
176 {
177 public:
178     SetCallbackStateFlagsHolder(DWORD dwFlags);
179     ~SetCallbackStateFlagsHolder();
180     
181 private:
182     Thread *   m_pThread;
183     DWORD      m_dwOriginalFullState;
184 };
185
186 #endif //__PROFILING_HELPER_H__