[profiler] Properly clean up the resources used by the profiler API on shutdown.
[mono.git] / mono / metadata / profiler.h
1 /*
2  * Licensed to the .NET Foundation under one or more agreements.
3  * The .NET Foundation licenses this file to you under the MIT license.
4  * See the LICENSE file in the project root for more information.
5  */
6
7 #ifndef __MONO_PROFILER_H__
8 #define __MONO_PROFILER_H__
9
10 #include <mono/metadata/appdomain.h>
11 #include <mono/metadata/mono-gc.h>
12 #include <mono/metadata/object.h>
13
14 MONO_BEGIN_DECLS
15
16 /*
17  * This value will be incremented whenever breaking changes to the profiler API
18  * are made. This macro is intended for use in profiler modules that wish to
19  * support older versions of the profiler API.
20  */
21 #define MONO_PROFILER_API_VERSION 2
22
23 /*
24  * Loads a profiler module based on the specified description. The description
25  * can be of the form "name:args" or just "name". For example, "log:sample" and
26  * "log" will both load "libmono-profiler-log.so". The description is passed to
27  * the module after it has been loaded. If the specified module has already
28  * been loaded, this function has no effect.
29  *
30  * A module called foo should declare an entry point like so:
31  *
32  * void mono_profiler_init_foo (const char *desc)
33  * {
34  * }
35  *
36  * This function is not async safe.
37  */
38 MONO_API void mono_profiler_load (const char *desc);
39
40 typedef struct _MonoProfiler MonoProfiler;
41 typedef struct _MonoProfilerDesc *MonoProfilerHandle;
42
43 /*
44  * Installs a profiler and returns a handle for it. The handle is used with the
45  * other functions in the profiler API (e.g. for setting up callbacks). The
46  * given structure pointer will be passed to all callbacks from the profiler
47  * API. It can be NULL.
48  *
49  * This function may only be called from your profiler's init function.
50  *
51  * Example usage:
52  *
53  * struct _MonoProfiler {
54  *      int my_stuff;
55  *      // ...
56  * };
57  *
58  * MonoProfiler *prof = calloc (1, sizeof (MonoProfiler));
59  * MonoProfilerHandle handle = mono_profiler_create (prof);
60  * mono_profiler_set_shutdown_callback (handle, my_shutdown_cb);
61  *
62  * This function is not async safe.
63  */
64 MONO_API MonoProfilerHandle mono_profiler_create (MonoProfiler *prof);
65
66 typedef void (*MonoProfilerCleanupCallback) (MonoProfiler *prof);
67
68 /*
69  * Sets a profiler cleanup function. This function will be invoked at shutdown
70  * when the profiler API is cleaning up its internal structures. It's mainly
71  * intended for a profiler to free the structure pointer that was passed to
72  * mono_profiler_create, if necessary.
73  *
74  * This function is async safe.
75  */
76 MONO_API void mono_profiler_set_cleanup_callback (MonoProfilerHandle handle, MonoProfilerCleanupCallback cb);
77
78 /*
79  * Enables support for code coverage instrumentation. At the moment, this means
80  * enabling the debug info subsystem. If you do not call this function, you
81  * will not be able to use mono_profiler_get_coverage_data. Returns TRUE if
82  * code coverage support was enabled, or FALSE if the function was called too
83  * late for this to be possible.
84  *
85  * This function may only be called from your profiler's init function.
86  *
87  * This function is not async safe.
88  */
89 MONO_API mono_bool mono_profiler_enable_coverage (void);
90
91 typedef mono_bool (*MonoProfilerCoverageFilterCallback) (MonoProfiler *prof, MonoMethod *method);
92
93 /*
94  * Sets a code coverage filter function. The profiler API will invoke filter
95  * functions from all installed profilers. If any of them return TRUE, then the
96  * given method will be instrumented for coverage analysis. All filters are
97  * guaranteed to be called at least once per method, even if an earlier filter
98  * has already returned TRUE.
99  *
100  * Note that filter functions must be installed before a method is compiled in
101  * order to have any effect, i.e. you should register your filter function in
102  * your profiler's init function.
103  *
104  * This function is async safe.
105  */
106 MONO_API void mono_profiler_set_coverage_filter_callback (MonoProfilerHandle handle, MonoProfilerCoverageFilterCallback cb);
107
108 typedef struct {
109         MonoMethod *method;
110         uint32_t il_offset;
111         uint32_t counter;
112         const char *file_name;
113         uint32_t line;
114         uint32_t column;
115 } MonoProfilerCoverageData;
116
117 typedef void (*MonoProfilerCoverageCallback) (MonoProfiler *prof, const MonoProfilerCoverageData *data);
118
119 /*
120  * Retrieves all coverage data for the specified method and invokes the given
121  * callback for each entry. Source location information will only be filled out
122  * if the given method has debug info available. Returns TRUE if the given
123  * method was instrumented for code coverage; otherwise, FALSE.
124  *
125  * Please note that the structure passed to the callback is only valid for the
126  * duration of the callback.
127  *
128  * This function is not async safe.
129  */
130 MONO_API mono_bool mono_profiler_get_coverage_data (MonoProfilerHandle handle, MonoMethod *method, MonoProfilerCoverageCallback cb);
131
132 typedef enum {
133         /*
134          * Do not perform sampling. Will make the sampling thread sleep until the
135          * sampling mode is changed to one of the below modes.
136          */
137         MONO_PROFILER_SAMPLE_MODE_NONE = 0,
138         /*
139          * Try to base sampling frequency on process activity. Falls back to
140          * MONO_PROFILER_SAMPLE_MODE_REAL if such a clock is not available.
141          */
142         MONO_PROFILER_SAMPLE_MODE_PROCESS = 1,
143         /*
144          * Base sampling frequency on wall clock time. Uses a monotonic clock when
145          * available (all major platforms).
146          */
147         MONO_PROFILER_SAMPLE_MODE_REAL = 2,
148 } MonoProfilerSampleMode;
149
150 /*
151  * Enables the sampling thread. You must call this function if you intend to use
152  * statistical sampling; mono_profiler_set_sample_mode will have no effect if
153  * this function has not been called. The first profiler to call this function
154  * will get ownership over sampling settings (mode and frequency) so that no
155  * other profiler can change those settings. Returns TRUE if the sampling
156  * thread was enabled, or FALSE if the function was called too late for this
157  * to be possible.
158  *
159  * Note that you still need to call mono_profiler_set_sample_mode with a mode
160  * other than MONO_PROFILER_SAMPLE_MODE_NONE to actually start sampling.
161  *
162  * This function may only be called from your profiler's init function.
163  *
164  * This function is not async safe.
165  */
166 MONO_API mono_bool mono_profiler_enable_sampling (MonoProfilerHandle handle);
167
168 /*
169  * Sets the sampling mode and frequency (in Hz). The frequency must be a
170  * positive number. If the calling profiler has ownership over sampling
171  * settings, the settings will be changed and this function will return TRUE;
172  * otherwise, it returns FALSE without changing any settings.
173  *
174  * This function is async safe.
175  */
176 MONO_API mono_bool mono_profiler_set_sample_mode (MonoProfilerHandle handle, MonoProfilerSampleMode mode, uint32_t freq);
177
178 /*
179  * Retrieves the current sampling mode and/or frequency (in Hz). Returns TRUE if
180  * the calling profiler is allowed to change the sampling settings; otherwise,
181  * FALSE.
182  *
183  * This function is async safe.
184  */
185 MONO_API mono_bool mono_profiler_get_sample_mode (MonoProfilerHandle handle, MonoProfilerSampleMode *mode, uint32_t *freq);
186
187 /*
188  * Enables instrumentation of GC allocations. This is necessary so that managed
189  * allocators can be instrumented with a call into the profiler API. Allocations
190  * will not be reported unless this function is called. Returns TRUE if
191  * allocation instrumentation was enabled, or FALSE if the function was called
192  * too late for this to be possible.
193  *
194  * This function may only be called from your profiler's init function.
195  *
196  * This function is not async safe.
197  */
198 MONO_API mono_bool mono_profiler_enable_allocations (void);
199
200 typedef enum {
201         /* Do not instrument calls. */
202         MONO_PROFILER_CALL_INSTRUMENTATION_NONE = 0,
203         /* Instrument method prologues. */
204         MONO_PROFILER_CALL_INSTRUMENTATION_PROLOGUE = 1 << 1,
205         /* Also capture a call context for prologues. */
206         MONO_PROFILER_CALL_INSTRUMENTATION_PROLOGUE_CONTEXT = 1 << 2,
207         /* Instrument method epilogues. */
208         MONO_PROFILER_CALL_INSTRUMENTATION_EPILOGUE = 1 << 3,
209         /* Also capture a call context for epilogues. */
210         MONO_PROFILER_CALL_INSTRUMENTATION_EPILOGUE_CONTEXT = 1 << 4,
211 } MonoProfilerCallInstrumentationFlags;
212
213 typedef MonoProfilerCallInstrumentationFlags (*MonoProfilerCallInstrumentationFilterCallback) (MonoProfiler *prof, MonoMethod *method);
214
215 /*
216  * Sets a call instrumentation filter function. The profiler API will invoke
217  * filter functions from all installed profilers. If any of them return flags
218  * other than MONO_PROFILER_CALL_INSTRUMENTATION_NONE, then the given method
219  * will be instrumented as requested. All filters are guaranteed to be called
220  * exactly once per method, even if earlier filters have already specified all
221  * flags.
222  *
223  * Note that filter functions must be installed before a method is compiled in
224  * order to have any effect, i.e. you should register your filter function in
225  * your profiler's init function. Also, if you want to instrument a method
226  * that's going to be AOT-compiled, you must attach your profiler and install a
227  * call instrumentation filter function at AOT time. This can be done in
228  * exactly the same way as you would normally, i.e. by passing the --profile
229  * option on the command line, by calling mono_profiler_load, or simply by
230  * using the profiler API as an embedder.
231  *
232  * Keep in mind that indiscriminate method instrumentation is extremely heavy
233  * and will slow down most applications to a crawl. Consider using sampling
234  * instead if it would work for your use case.
235  *
236  * This function is async safe.
237  */
238 MONO_API void mono_profiler_set_call_instrumentation_filter_callback (MonoProfilerHandle handle, MonoProfilerCallInstrumentationFilterCallback cb);
239
240 /*
241  * Enables support for retrieving stack frame data from a call context. At the
242  * moment, this means enabling the debug info subsystem. If you do not call
243  * this function, you will not be able to use the call context introspection
244  * functions (they will simply return NULL). Returns TRUE if call context
245  * introspection was enabled, or FALSE if the function was called too late for
246  * this to be possible.
247  *
248  * Please note: Mono's LLVM backend does not support this feature. This means
249  * that methods with call context instrumentation will be handled by Mono's
250  * JIT even in LLVM mode. There is also a special case when Mono is compiling
251  * in LLVM-only mode: Since LLVM does not provide a way to implement call
252  * contexts, a NULL context will always be passed to enter/leave events even
253  * though this method returns TRUE.
254  *
255  * This function may only be called from your profiler's init function.
256  *
257  * This function is not async safe.
258  */
259 MONO_API mono_bool mono_profiler_enable_call_context_introspection (void);
260
261 typedef struct _MonoProfilerCallContext MonoProfilerCallContext;
262
263 /*
264  * Given a valid call context from an enter/leave event, retrieves a pointer to
265  * the this reference for the method. Returns NULL if none exists (i.e. it's a
266  * static method) or if call context introspection was not enabled.
267  *
268  * The buffer returned by this function must be freed with
269  * mono_profiler_call_context_free_buffer.
270  *
271  * Please note that a call context is only valid for the duration of the
272  * enter/leave callback it was passed to.
273  *
274  * This function is not async safe.
275  */
276 MONO_API void *mono_profiler_call_context_get_this (MonoProfilerCallContext *context);
277
278 /*
279  * Given a valid call context from an enter/leave event, retrieves a pointer to
280  * the method argument at the given position. Returns NULL if position is out
281  * of bounds or if call context introspection was not enabled.
282  *
283  * The buffer returned by this function must be freed with
284  * mono_profiler_call_context_free_buffer.
285  *
286  * Please note that a call context is only valid for the duration of the
287  * enter/leave callback it was passed to.
288  *
289  * This function is not async safe.
290  */
291 MONO_API void *mono_profiler_call_context_get_argument (MonoProfilerCallContext *context, uint32_t position);
292
293 /*
294  * Given a valid call context from an enter/leave event, retrieves a pointer to
295  * the local variable at the given position. Returns NULL if position is out of
296  * bounds or if call context introspection was not enabled.
297  *
298  * The buffer returned by this function must be freed with
299  * mono_profiler_call_context_free_buffer.
300  *
301  * Please note that a call context is only valid for the duration of the
302  * enter/leave callback it was passed to.
303  *
304  * This function is not async safe.
305  */
306 MONO_API void *mono_profiler_call_context_get_local (MonoProfilerCallContext *context, uint32_t position);
307
308 /*
309  * Given a valid call context from an enter/leave event, retrieves a pointer to
310  * return value of a method. Returns NULL if the method has no return value
311  * (i.e. it returns void), if the leave event was the result of a tail call, if
312  * the function is called on a context from an enter event, or if call context
313  * introspection was not enabled.
314  *
315  * The buffer returned by this function must be freed with
316  * mono_profiler_call_context_free_buffer.
317  *
318  * Please note that a call context is only valid for the duration of the
319  * enter/leave callback it was passed to.
320  *
321  * This function is not async safe.
322  */
323 MONO_API void *mono_profiler_call_context_get_result (MonoProfilerCallContext *context);
324
325 /*
326  * Frees a buffer returned by one of the call context introspection functions.
327  * Passing a NULL buffer is allowed, which makes this function a no-op.
328  *
329  * This function is not async safe.
330  */
331 MONO_API void mono_profiler_call_context_free_buffer (void *buffer);
332
333 #ifdef MONO_PROFILER_UNSTABLE_GC_ROOTS
334 typedef enum {
335         /* Upper 2 bytes. */
336         MONO_PROFILER_GC_ROOT_PINNING = 1 << 8,
337         MONO_PROFILER_GC_ROOT_WEAKREF = 2 << 8,
338         MONO_PROFILER_GC_ROOT_INTERIOR = 4 << 8,
339
340         /* Lower 2 bytes (flags). */
341         MONO_PROFILER_GC_ROOT_STACK = 1 << 0,
342         MONO_PROFILER_GC_ROOT_FINALIZER = 1 << 1,
343         MONO_PROFILER_GC_ROOT_HANDLE = 1 << 2,
344         MONO_PROFILER_GC_ROOT_OTHER = 1 << 3,
345         MONO_PROFILER_GC_ROOT_MISC = 1 << 4,
346
347         MONO_PROFILER_GC_ROOT_TYPEMASK = 0xff,
348 } MonoProfilerGCRootType;
349 #endif
350
351 typedef enum {
352         /* data = MonoMethod *method */
353         MONO_PROFILER_CODE_BUFFER_METHOD = 0,
354         MONO_PROFILER_CODE_BUFFER_METHOD_TRAMPOLINE = 1,
355         MONO_PROFILER_CODE_BUFFER_UNBOX_TRAMPOLINE = 2,
356         MONO_PROFILER_CODE_BUFFER_IMT_TRAMPOLINE = 3,
357         MONO_PROFILER_CODE_BUFFER_GENERICS_TRAMPOLINE = 4,
358         /* data = const char *name */
359         MONO_PROFILER_CODE_BUFFER_SPECIFIC_TRAMPOLINE = 5,
360         MONO_PROFILER_CODE_BUFFER_HELPER = 6,
361         MONO_PROFILER_CODE_BUFFER_MONITOR = 7,
362         MONO_PROFILER_CODE_BUFFER_DELEGATE_INVOKE = 8,
363         MONO_PROFILER_CODE_BUFFER_EXCEPTION_HANDLING = 9,
364 } MonoProfilerCodeBufferType;
365
366 typedef enum {
367         MONO_GC_EVENT_PRE_STOP_WORLD = 6,
368         /* When this event arrives, the GC and suspend locks are acquired. */
369         MONO_GC_EVENT_PRE_STOP_WORLD_LOCKED = 10,
370         MONO_GC_EVENT_POST_STOP_WORLD = 7,
371         MONO_GC_EVENT_START = 0,
372         MONO_GC_EVENT_END = 5,
373         MONO_GC_EVENT_PRE_START_WORLD = 8,
374         /* When this event arrives, the GC and suspend locks are released. */
375         MONO_GC_EVENT_POST_START_WORLD_UNLOCKED = 11,
376         MONO_GC_EVENT_POST_START_WORLD = 9,
377 } MonoProfilerGCEvent;
378
379 /*
380  * The macros below will generate the majority of the callback API. Refer to
381  * mono/metadata/profiler-events.h for a list of callbacks. They are expanded
382  * like so:
383  *
384  * typedef void (*MonoProfilerRuntimeInitializedCallback (MonoProfiler *prof);
385  * MONO_API void mono_profiler_set_runtime_initialized_callback (MonoProfiler *prof, MonoProfilerRuntimeInitializedCallback cb);
386  *
387  * typedef void (*MonoProfilerRuntimeShutdownCallback (MonoProfiler *prof);
388  * MONO_API void mono_profiler_set_runtime_shutdown_callback (MonoProfiler *prof, MonoProfilerRuntimeShutdownCallback cb);
389  *
390  * typedef void (*MonoProfilerContextLoadedCallback (MonoProfiler *prof);
391  * MONO_API void mono_profiler_set_context_loaded_callback (MonoProfiler *prof, MonoProfilerContextLoadedCallback cb);
392  *
393  * typedef void (*MonoProfilerContextUnloadedCallback (MonoProfiler *prof);
394  * MONO_API void mono_profiler_set_context_unloaded_callback (MonoProfiler *prof, MonoProfilerContextUnloadedCallback cb);
395  *
396  * Etc.
397  *
398  * To remove a callback, pass NULL instead of a valid function pointer.
399  * Callbacks can be changed at any point, but note that doing so is inherently
400  * racy with respect to threads that aren't suspended, i.e. you may still see a
401  * call from another thread right after you change a callback.
402  *
403  * These functions are async safe.
404  */
405
406 #define _MONO_PROFILER_EVENT(type, ...) \
407         typedef void (*MonoProfiler ## type ## Callback) (__VA_ARGS__);
408 #define MONO_PROFILER_EVENT_0(name, type) \
409                 _MONO_PROFILER_EVENT(type, MonoProfiler *prof)
410 #define MONO_PROFILER_EVENT_1(name, type, arg1_type, arg1_name) \
411                 _MONO_PROFILER_EVENT(type, MonoProfiler *prof, arg1_type arg1_name)
412 #define MONO_PROFILER_EVENT_2(name, type, arg1_type, arg1_name, arg2_type, arg2_name) \
413                 _MONO_PROFILER_EVENT(type, MonoProfiler *prof, arg1_type arg1_name, arg2_type arg2_name)
414 #define MONO_PROFILER_EVENT_3(name, type, arg1_type, arg1_name, arg2_type, arg2_name, arg3_type, arg3_name) \
415                 _MONO_PROFILER_EVENT(type, MonoProfiler *prof, arg1_type arg1_name, arg2_type arg2_name, arg3_type arg3_name)
416 #define MONO_PROFILER_EVENT_4(name, type, arg1_type, arg1_name, arg2_type, arg2_name, arg3_type, arg3_name, arg4_type, arg4_name) \
417                 _MONO_PROFILER_EVENT(type, MonoProfiler *prof, arg1_type arg1_name, arg2_type arg2_name, arg3_type arg3_name, arg4_type arg4_name)
418 #include <mono/metadata/profiler-events.h>
419 #undef MONO_PROFILER_EVENT_0
420 #undef MONO_PROFILER_EVENT_1
421 #undef MONO_PROFILER_EVENT_2
422 #undef MONO_PROFILER_EVENT_3
423 #undef MONO_PROFILER_EVENT_4
424 #undef _MONO_PROFILER_EVENT
425
426 #define _MONO_PROFILER_EVENT(name, type) \
427         MONO_API void mono_profiler_set_ ## name ## _callback (MonoProfilerHandle handle, MonoProfiler ## type ## Callback cb);
428 #define MONO_PROFILER_EVENT_0(name, type) \
429         _MONO_PROFILER_EVENT(name, type)
430 #define MONO_PROFILER_EVENT_1(name, type, arg1_type, arg1_name) \
431         _MONO_PROFILER_EVENT(name, type)
432 #define MONO_PROFILER_EVENT_2(name, type, arg1_type, arg1_name, arg2_type, arg2_name) \
433         _MONO_PROFILER_EVENT(name, type)
434 #define MONO_PROFILER_EVENT_3(name, type, arg1_type, arg1_name, arg2_type, arg2_name, arg3_type, arg3_name) \
435         _MONO_PROFILER_EVENT(name, type)
436 #define MONO_PROFILER_EVENT_4(name, type, arg1_type, arg1_name, arg2_type, arg2_name, arg3_type, arg3_name, arg4_type, arg4_name) \
437         _MONO_PROFILER_EVENT(name, type)
438 #include <mono/metadata/profiler-events.h>
439 #undef MONO_PROFILER_EVENT_0
440 #undef MONO_PROFILER_EVENT_1
441 #undef MONO_PROFILER_EVENT_2
442 #undef MONO_PROFILER_EVENT_3
443 #undef MONO_PROFILER_EVENT_4
444 #undef _MONO_PROFILER_EVENT
445
446 MONO_END_DECLS
447
448 #endif // __MONO_PROFILER_H__