Merge pull request #1745 from alexrp/warning-police
[mono.git] / mono / metadata / profiler.h
1 #ifndef __MONO_PROFILER_H__
2 #define __MONO_PROFILER_H__
3
4 #include <mono/metadata/object.h>
5 #include <mono/metadata/appdomain.h>
6
7 MONO_BEGIN_DECLS
8
9 /*
10  * The profiler API is considered unstable because it needs to evolve with the
11  * Mono runtime. Profilers will pass in the profiler API version they are built
12  * against when calling mono_profiler_install (). If the profiler API version
13  * of the runtime is not equal to the one passed in, Mono will print a warning
14  * letting developers know that a profiler module needs to be updated.
15  *
16  * When you have updated a module to be compatible with a version of the
17  * profiler API, make it pass in the current value of MONO_PROFILER_VERSION.
18  * Pass the value literally; using the macro will defeat the purpose.
19  * (Modules within Mono are exceptions to this rule.)
20  *
21  * mono_profiler_install () is the single function guaranteed to be stable.
22  *
23  * -- Change Log --
24  *
25  * Version 1:
26  *
27  *  - Introduced profiler API versioning.
28  */
29
30 #define MONO_PROFILER_VERSION 1
31
32 #define MONO_PROFILER_MAX_STAT_CALL_CHAIN_DEPTH 128
33
34 typedef enum {
35         MONO_PROFILE_NONE = 0,
36         MONO_PROFILE_APPDOMAIN_EVENTS = 1 << 0,
37         MONO_PROFILE_ASSEMBLY_EVENTS  = 1 << 1,
38         MONO_PROFILE_MODULE_EVENTS    = 1 << 2,
39         MONO_PROFILE_CLASS_EVENTS     = 1 << 3,
40         MONO_PROFILE_JIT_COMPILATION  = 1 << 4,
41         MONO_PROFILE_INLINING         = 1 << 5,
42         MONO_PROFILE_EXCEPTIONS       = 1 << 6,
43         MONO_PROFILE_ALLOCATIONS      = 1 << 7,
44         MONO_PROFILE_GC               = 1 << 8,
45         MONO_PROFILE_THREADS          = 1 << 9,
46         MONO_PROFILE_REMOTING         = 1 << 10,
47         MONO_PROFILE_TRANSITIONS      = 1 << 11,
48         MONO_PROFILE_ENTER_LEAVE      = 1 << 12,
49         MONO_PROFILE_COVERAGE         = 1 << 13,
50         MONO_PROFILE_INS_COVERAGE     = 1 << 14,
51         MONO_PROFILE_STATISTICAL      = 1 << 15,
52         MONO_PROFILE_METHOD_EVENTS    = 1 << 16,
53         MONO_PROFILE_MONITOR_EVENTS   = 1 << 17,
54         MONO_PROFILE_IOMAP_EVENTS     = 1 << 18, /* this should likely be removed, too */
55         MONO_PROFILE_GC_MOVES         = 1 << 19,
56         MONO_PROFILE_GC_ROOTS         = 1 << 20
57 } MonoProfileFlags;
58
59 typedef enum {
60         MONO_PROFILE_OK,
61         MONO_PROFILE_FAILED
62 } MonoProfileResult;
63
64 typedef enum {
65         MONO_GC_EVENT_START,
66         MONO_GC_EVENT_MARK_START,
67         MONO_GC_EVENT_MARK_END,
68         MONO_GC_EVENT_RECLAIM_START,
69         MONO_GC_EVENT_RECLAIM_END,
70         MONO_GC_EVENT_END,
71         MONO_GC_EVENT_PRE_STOP_WORLD,
72         MONO_GC_EVENT_POST_STOP_WORLD,
73         MONO_GC_EVENT_PRE_START_WORLD,
74         MONO_GC_EVENT_POST_START_WORLD
75 } MonoGCEvent;
76
77 /* coverage info */
78 typedef struct {
79         MonoMethod *method;
80         int iloffset;
81         int counter;
82         const char *filename;
83         int line;
84         int col;
85 } MonoProfileCoverageEntry;
86
87 /* executable code buffer info */
88 typedef enum {
89         MONO_PROFILER_CODE_BUFFER_UNKNOWN,
90         MONO_PROFILER_CODE_BUFFER_METHOD,
91         MONO_PROFILER_CODE_BUFFER_METHOD_TRAMPOLINE,
92         MONO_PROFILER_CODE_BUFFER_UNBOX_TRAMPOLINE,
93         MONO_PROFILER_CODE_BUFFER_IMT_TRAMPOLINE,
94         MONO_PROFILER_CODE_BUFFER_GENERICS_TRAMPOLINE,
95         MONO_PROFILER_CODE_BUFFER_SPECIFIC_TRAMPOLINE,
96         MONO_PROFILER_CODE_BUFFER_HELPER,
97         MONO_PROFILER_CODE_BUFFER_MONITOR,
98         MONO_PROFILER_CODE_BUFFER_DELEGATE_INVOKE,
99         MONO_PROFILER_CODE_BUFFER_EXCEPTION_HANDLING,
100         MONO_PROFILER_CODE_BUFFER_LAST
101 } MonoProfilerCodeBufferType;
102
103 typedef struct _MonoProfiler MonoProfiler;
104
105 typedef enum {
106         MONO_PROFILER_MONITOR_CONTENTION = 1,
107         MONO_PROFILER_MONITOR_DONE = 2,
108         MONO_PROFILER_MONITOR_FAIL = 3
109 } MonoProfilerMonitorEvent;
110
111 typedef enum {
112         MONO_PROFILER_CALL_CHAIN_NONE = 0,
113         MONO_PROFILER_CALL_CHAIN_NATIVE = 1,
114         MONO_PROFILER_CALL_CHAIN_GLIBC = 2,
115         MONO_PROFILER_CALL_CHAIN_MANAGED = 3,
116         MONO_PROFILER_CALL_CHAIN_INVALID = 4
117 } MonoProfilerCallChainStrategy;
118
119 typedef enum {
120         MONO_PROFILER_GC_HANDLE_CREATED,
121         MONO_PROFILER_GC_HANDLE_DESTROYED
122 } MonoProfileGCHandleEvent;
123
124 typedef enum {
125         MONO_PROFILE_GC_ROOT_PINNING  = 1 << 8,
126         MONO_PROFILE_GC_ROOT_WEAKREF  = 2 << 8,
127         MONO_PROFILE_GC_ROOT_INTERIOR = 4 << 8,
128         /* the above are flags, the type is in the low 2 bytes */
129         MONO_PROFILE_GC_ROOT_STACK = 0,
130         MONO_PROFILE_GC_ROOT_FINALIZER = 1,
131         MONO_PROFILE_GC_ROOT_HANDLE = 2,
132         MONO_PROFILE_GC_ROOT_OTHER = 3,
133         MONO_PROFILE_GC_ROOT_MISC = 4, /* could be stack, handle, etc. */
134         MONO_PROFILE_GC_ROOT_TYPEMASK = 0xff
135 } MonoProfileGCRootType;
136
137 /*
138  * Functions that the runtime will call on the profiler.
139  */
140
141 typedef void (*MonoProfileFunc) (MonoProfiler *prof);
142
143 typedef void (*MonoProfileAppDomainFunc) (MonoProfiler *prof, MonoDomain   *domain);
144 typedef void (*MonoProfileMethodFunc)   (MonoProfiler *prof, MonoMethod   *method);
145 typedef void (*MonoProfileClassFunc)    (MonoProfiler *prof, MonoClass    *klass);
146 typedef void (*MonoProfileModuleFunc)   (MonoProfiler *prof, MonoImage    *module);
147 typedef void (*MonoProfileAssemblyFunc) (MonoProfiler *prof, MonoAssembly *assembly);
148 typedef void (*MonoProfileMonitorFunc)  (MonoProfiler *prof, MonoObject *obj, MonoProfilerMonitorEvent event);
149
150 typedef void (*MonoProfileExceptionFunc) (MonoProfiler *prof, MonoObject *object);
151 typedef void (*MonoProfileExceptionClauseFunc) (MonoProfiler *prof, MonoMethod *method, int clause_type, int clause_num);
152
153 typedef void (*MonoProfileAppDomainResult)(MonoProfiler *prof, MonoDomain   *domain,   int result);
154 typedef void (*MonoProfileMethodResult)   (MonoProfiler *prof, MonoMethod   *method,   int result);
155 typedef void (*MonoProfileJitResult)      (MonoProfiler *prof, MonoMethod   *method,   MonoJitInfo* jinfo,   int result);
156 typedef void (*MonoProfileClassResult)    (MonoProfiler *prof, MonoClass    *klass,    int result);
157 typedef void (*MonoProfileModuleResult)   (MonoProfiler *prof, MonoImage    *module,   int result);
158 typedef void (*MonoProfileAssemblyResult) (MonoProfiler *prof, MonoAssembly *assembly, int result);
159
160 typedef void (*MonoProfileMethodInline)   (MonoProfiler *prof, MonoMethod   *parent, MonoMethod *child, int *ok);
161
162 typedef void (*MonoProfileThreadFunc)     (MonoProfiler *prof, uintptr_t tid);
163 typedef void (*MonoProfileThreadNameFunc) (MonoProfiler *prof, uintptr_t tid, const char *name);
164 typedef void (*MonoProfileAllocFunc)      (MonoProfiler *prof, MonoObject *obj, MonoClass *klass);
165 typedef void (*MonoProfileStatFunc)       (MonoProfiler *prof, mono_byte *ip, void *context);
166 typedef void (*MonoProfileStatCallChainFunc) (MonoProfiler *prof, int call_chain_depth, mono_byte **ip, void *context);
167 typedef void (*MonoProfileGCFunc)         (MonoProfiler *prof, MonoGCEvent event, int generation);
168 typedef void (*MonoProfileGCMoveFunc)     (MonoProfiler *prof, void **objects, int num);
169 typedef void (*MonoProfileGCResizeFunc)   (MonoProfiler *prof, int64_t new_size);
170 typedef void (*MonoProfileGCHandleFunc)   (MonoProfiler *prof, int op, int type, uintptr_t handle, MonoObject *obj);
171 typedef void (*MonoProfileGCRootFunc)     (MonoProfiler *prof, int num_roots, void **objects, int *root_types, uintptr_t *extra_info);
172
173 typedef void (*MonoProfileIomapFunc) (MonoProfiler *prof, const char *report, const char *pathname, const char *new_pathname);
174
175 typedef mono_bool (*MonoProfileCoverageFilterFunc)   (MonoProfiler *prof, MonoMethod *method);
176
177 typedef void (*MonoProfileCoverageFunc)   (MonoProfiler *prof, const MonoProfileCoverageEntry *entry);
178
179 typedef void (*MonoProfilerCodeChunkNew) (MonoProfiler *prof, void* chunk, int size);
180 typedef void (*MonoProfilerCodeChunkDestroy) (MonoProfiler *prof, void* chunk);
181 typedef void (*MonoProfilerCodeBufferNew) (MonoProfiler *prof, void* buffer, int size, MonoProfilerCodeBufferType type, void *data);
182
183 /*
184  * Function the profiler may call.
185  */
186 MONO_API void mono_profiler_install       (MonoProfiler *prof, int version, MonoProfileFunc shutdown_callback);
187 MONO_API void mono_profiler_set_events    (MonoProfileFlags events);
188
189 MONO_API MonoProfileFlags mono_profiler_get_events (void);
190
191 MONO_API void mono_profiler_install_appdomain   (MonoProfileAppDomainFunc start_load, MonoProfileAppDomainResult end_load,
192                                         MonoProfileAppDomainFunc start_unload, MonoProfileAppDomainFunc end_unload);
193 MONO_API void mono_profiler_install_assembly    (MonoProfileAssemblyFunc start_load, MonoProfileAssemblyResult end_load,
194                                         MonoProfileAssemblyFunc start_unload, MonoProfileAssemblyFunc end_unload);
195 MONO_API void mono_profiler_install_module      (MonoProfileModuleFunc start_load, MonoProfileModuleResult end_load,
196                                         MonoProfileModuleFunc start_unload, MonoProfileModuleFunc end_unload);
197 MONO_API void mono_profiler_install_class       (MonoProfileClassFunc start_load, MonoProfileClassResult end_load,
198                                         MonoProfileClassFunc start_unload, MonoProfileClassFunc end_unload);
199
200 MONO_API void mono_profiler_install_jit_compile (MonoProfileMethodFunc start, MonoProfileMethodResult end);
201 MONO_API void mono_profiler_install_jit_end (MonoProfileJitResult end);
202 MONO_API void mono_profiler_install_method_free (MonoProfileMethodFunc callback);
203 MONO_API void mono_profiler_install_method_invoke (MonoProfileMethodFunc start, MonoProfileMethodFunc end);
204 MONO_API void mono_profiler_install_enter_leave (MonoProfileMethodFunc enter, MonoProfileMethodFunc fleave);
205 MONO_API void mono_profiler_install_thread      (MonoProfileThreadFunc start, MonoProfileThreadFunc end);
206 MONO_API void mono_profiler_install_thread_name (MonoProfileThreadNameFunc thread_name_cb);
207 MONO_API void mono_profiler_install_transition  (MonoProfileMethodResult callback);
208 MONO_API void mono_profiler_install_allocation  (MonoProfileAllocFunc callback);
209 MONO_API void mono_profiler_install_monitor     (MonoProfileMonitorFunc callback);
210 MONO_API void mono_profiler_install_statistical (MonoProfileStatFunc callback);
211 MONO_API void mono_profiler_install_statistical_call_chain (MonoProfileStatCallChainFunc callback, int call_chain_depth, MonoProfilerCallChainStrategy call_chain_strategy);
212 MONO_API void mono_profiler_install_exception   (MonoProfileExceptionFunc throw_callback, MonoProfileMethodFunc exc_method_leave, MonoProfileExceptionClauseFunc clause_callback);
213 MONO_API void mono_profiler_install_coverage_filter (MonoProfileCoverageFilterFunc callback);
214 MONO_API void mono_profiler_coverage_get  (MonoProfiler *prof, MonoMethod *method, MonoProfileCoverageFunc func);
215 MONO_API void mono_profiler_install_gc    (MonoProfileGCFunc callback, MonoProfileGCResizeFunc heap_resize_callback);
216 MONO_API void mono_profiler_install_gc_moves    (MonoProfileGCMoveFunc callback);
217 MONO_API void mono_profiler_install_gc_roots    (MonoProfileGCHandleFunc handle_callback, MonoProfileGCRootFunc roots_callback);
218 MONO_API void mono_profiler_install_runtime_initialized (MonoProfileFunc runtime_initialized_callback);
219
220 MONO_API void mono_profiler_install_code_chunk_new (MonoProfilerCodeChunkNew callback);
221 MONO_API void mono_profiler_install_code_chunk_destroy (MonoProfilerCodeChunkDestroy callback);
222 MONO_API void mono_profiler_install_code_buffer_new (MonoProfilerCodeBufferNew callback);
223
224 MONO_API void mono_profiler_install_iomap (MonoProfileIomapFunc callback);
225
226 MONO_API void mono_profiler_load             (const char *desc);
227
228 typedef enum {
229         /* Elapsed time is tracked by user+kernel time of the process - this is the default*/
230         MONO_PROFILER_STAT_MODE_PROCESS = 0,
231         /* Elapsed time is tracked by wallclock time */
232         MONO_PROFILER_STAT_MODE_REAL = 1,
233 } MonoProfileSamplingMode;
234
235 MONO_API void mono_profiler_set_statistical_mode (MonoProfileSamplingMode mode, int64_t sampling_frequency_is_us);
236
237 MONO_END_DECLS
238
239 #endif /* __MONO_PROFILER_H__ */
240