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.
7 #ifndef __MONO_PROFILER_H__
8 #define __MONO_PROFILER_H__
10 #include <mono/metadata/appdomain.h>
11 #include <mono/metadata/mono-gc.h>
12 #include <mono/metadata/object.h>
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.
21 #define MONO_PROFILER_API_VERSION 2
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.
30 * A module called foo should declare an entry point like so:
32 * void mono_profiler_init_foo (const char *desc)
36 * This function is not async safe.
38 MONO_API void mono_profiler_load (const char *desc);
40 typedef struct _MonoProfiler MonoProfiler;
41 typedef struct _MonoProfilerDesc *MonoProfilerHandle;
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.
49 * This function may only be called from your profiler's init function.
53 * struct _MonoProfiler {
58 * MonoProfiler *prof = calloc (1, sizeof (MonoProfiler));
59 * MonoProfilerHandle handle = mono_profiler_create (prof);
60 * mono_profiler_set_shutdown_callback (handle, my_shutdown_cb);
62 * This function is not async safe.
64 MONO_API MonoProfilerHandle mono_profiler_create (MonoProfiler *prof);
66 typedef void (*MonoProfilerCleanupCallback) (MonoProfiler *prof);
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.
74 * This function is async safe.
76 MONO_API void mono_profiler_set_cleanup_callback (MonoProfilerHandle handle, MonoProfilerCleanupCallback cb);
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.
85 * This function may only be called from your profiler's init function.
87 * This function is not async safe.
89 MONO_API mono_bool mono_profiler_enable_coverage (void);
91 typedef mono_bool (*MonoProfilerCoverageFilterCallback) (MonoProfiler *prof, MonoMethod *method);
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.
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.
104 * This function is async safe.
106 MONO_API void mono_profiler_set_coverage_filter_callback (MonoProfilerHandle handle, MonoProfilerCoverageFilterCallback cb);
112 const char *file_name;
115 } MonoProfilerCoverageData;
117 typedef void (*MonoProfilerCoverageCallback) (MonoProfiler *prof, const MonoProfilerCoverageData *data);
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.
125 * Please note that the structure passed to the callback is only valid for the
126 * duration of the callback.
128 * This function is not async safe.
130 MONO_API mono_bool mono_profiler_get_coverage_data (MonoProfilerHandle handle, MonoMethod *method, MonoProfilerCoverageCallback cb);
134 * Do not perform sampling. Will make the sampling thread sleep until the
135 * sampling mode is changed to one of the below modes.
137 MONO_PROFILER_SAMPLE_MODE_NONE = 0,
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.
142 MONO_PROFILER_SAMPLE_MODE_PROCESS = 1,
144 * Base sampling frequency on wall clock time. Uses a monotonic clock when
145 * available (all major platforms).
147 MONO_PROFILER_SAMPLE_MODE_REAL = 2,
148 } MonoProfilerSampleMode;
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
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.
162 * This function may only be called from your profiler's init function.
164 * This function is not async safe.
166 MONO_API mono_bool mono_profiler_enable_sampling (MonoProfilerHandle handle);
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.
174 * This function is async safe.
176 MONO_API mono_bool mono_profiler_set_sample_mode (MonoProfilerHandle handle, MonoProfilerSampleMode mode, uint32_t freq);
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,
183 * This function is async safe.
185 MONO_API mono_bool mono_profiler_get_sample_mode (MonoProfilerHandle handle, MonoProfilerSampleMode *mode, uint32_t *freq);
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.
194 * This function may only be called from your profiler's init function.
196 * This function is not async safe.
198 MONO_API mono_bool mono_profiler_enable_allocations (void);
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;
213 typedef MonoProfilerCallInstrumentationFlags (*MonoProfilerCallInstrumentationFilterCallback) (MonoProfiler *prof, MonoMethod *method);
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
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.
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.
236 * This function is async safe.
238 MONO_API void mono_profiler_set_call_instrumentation_filter_callback (MonoProfilerHandle handle, MonoProfilerCallInstrumentationFilterCallback cb);
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.
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.
255 * This function may only be called from your profiler's init function.
257 * This function is not async safe.
259 MONO_API mono_bool mono_profiler_enable_call_context_introspection (void);
261 typedef struct _MonoProfilerCallContext MonoProfilerCallContext;
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.
268 * The buffer returned by this function must be freed with
269 * mono_profiler_call_context_free_buffer.
271 * Please note that a call context is only valid for the duration of the
272 * enter/leave callback it was passed to.
274 * This function is not async safe.
276 MONO_API void *mono_profiler_call_context_get_this (MonoProfilerCallContext *context);
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.
283 * The buffer returned by this function must be freed with
284 * mono_profiler_call_context_free_buffer.
286 * Please note that a call context is only valid for the duration of the
287 * enter/leave callback it was passed to.
289 * This function is not async safe.
291 MONO_API void *mono_profiler_call_context_get_argument (MonoProfilerCallContext *context, uint32_t position);
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.
298 * The buffer returned by this function must be freed with
299 * mono_profiler_call_context_free_buffer.
301 * Please note that a call context is only valid for the duration of the
302 * enter/leave callback it was passed to.
304 * This function is not async safe.
306 MONO_API void *mono_profiler_call_context_get_local (MonoProfilerCallContext *context, uint32_t position);
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.
315 * The buffer returned by this function must be freed with
316 * mono_profiler_call_context_free_buffer.
318 * Please note that a call context is only valid for the duration of the
319 * enter/leave callback it was passed to.
321 * This function is not async safe.
323 MONO_API void *mono_profiler_call_context_get_result (MonoProfilerCallContext *context);
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.
329 * This function is not async safe.
331 MONO_API void mono_profiler_call_context_free_buffer (void *buffer);
333 #ifdef MONO_PROFILER_UNSTABLE_GC_ROOTS
336 MONO_PROFILER_GC_ROOT_PINNING = 1 << 8,
337 MONO_PROFILER_GC_ROOT_WEAKREF = 2 << 8,
338 MONO_PROFILER_GC_ROOT_INTERIOR = 4 << 8,
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,
347 MONO_PROFILER_GC_ROOT_TYPEMASK = 0xff,
348 } MonoProfilerGCRootType;
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;
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;
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
384 * typedef void (*MonoProfilerRuntimeInitializedCallback (MonoProfiler *prof);
385 * MONO_API void mono_profiler_set_runtime_initialized_callback (MonoProfiler *prof, MonoProfilerRuntimeInitializedCallback cb);
387 * typedef void (*MonoProfilerRuntimeShutdownCallback (MonoProfiler *prof);
388 * MONO_API void mono_profiler_set_runtime_shutdown_callback (MonoProfiler *prof, MonoProfilerRuntimeShutdownCallback cb);
390 * typedef void (*MonoProfilerContextLoadedCallback (MonoProfiler *prof);
391 * MONO_API void mono_profiler_set_context_loaded_callback (MonoProfiler *prof, MonoProfilerContextLoadedCallback cb);
393 * typedef void (*MonoProfilerContextUnloadedCallback (MonoProfiler *prof);
394 * MONO_API void mono_profiler_set_context_unloaded_callback (MonoProfiler *prof, MonoProfilerContextUnloadedCallback cb);
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.
403 * These functions are async safe.
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
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
448 #endif // __MONO_PROFILER_H__