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