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).
47 * This function may only be called from your profiler's init function.
51 * struct _MonoProfiler {
56 * MonoProfiler *prof = calloc (1, sizeof (MonoProfiler));
57 * MonoProfilerHandle handle = mono_profiler_create (prof);
58 * mono_profiler_set_shutdown_callback (handle, my_shutdown_cb);
60 * This function is not async safe.
62 MONO_API MonoProfilerHandle mono_profiler_create (MonoProfiler *prof);
64 typedef mono_bool (*MonoProfilerCoverageFilterCallback) (MonoProfiler *prof, MonoMethod *method);
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.
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.
77 * This function is async safe.
79 MONO_API void mono_profiler_set_coverage_filter_callback (MonoProfilerHandle handle, MonoProfilerCoverageFilterCallback cb);
85 const char *file_name;
88 } MonoProfilerCoverageData;
90 typedef void (*MonoProfilerCoverageCallback) (MonoProfiler *prof, const MonoProfilerCoverageData *data);
93 * Retrieves all coverage data for the specified method and invokes the given
94 * callback for each entry.
96 * This function is not async safe.
98 MONO_API void mono_profiler_get_coverage_data (MonoProfilerHandle handle, MonoMethod *method, MonoProfilerCoverageCallback cb);
102 * Do not perform sampling. Will make the sampling thread sleep until the
103 * sampling mode is changed to one of the below modes.
105 MONO_PROFILER_SAMPLE_MODE_NONE = 0,
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.
110 MONO_PROFILER_SAMPLE_MODE_PROCESS = 1,
112 * Base sampling frequency on wall clock time. Uses a monotonic clock when
113 * available (all major platforms).
115 MONO_PROFILER_SAMPLE_MODE_REAL = 2,
116 } MonoProfilerSampleMode;
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
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.
130 * This function may only be called from your profiler's init function.
132 * This function is not async safe.
134 MONO_API mono_bool mono_profiler_enable_sampling (MonoProfilerHandle handle);
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.
142 * This function is async safe.
144 MONO_API mono_bool mono_profiler_set_sample_mode (MonoProfilerHandle handle, MonoProfilerSampleMode mode, uint32_t freq);
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,
151 * This function is async safe.
153 MONO_API mono_bool mono_profiler_get_sample_mode (MonoProfilerHandle handle, MonoProfilerSampleMode *mode, uint32_t *freq);
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.
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_allocations (void);
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;
181 typedef MonoProfilerCallInstrumentationFlags (*MonoProfilerCallInstrumentationFilterCallback) (MonoProfiler *prof, MonoMethod *method);
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
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.
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.
204 * This function is async safe.
206 MONO_API void mono_profiler_set_call_instrumentation_filter_callback (MonoProfilerHandle handle, MonoProfilerCallInstrumentationFilterCallback cb);
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.
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.
223 * This function may only be called from your profiler's init function.
225 * This function is not async safe.
227 MONO_API mono_bool mono_profiler_enable_call_context_introspection (void);
229 typedef struct _MonoProfilerCallContext MonoProfilerCallContext;
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.
236 * The buffer returned by this function must be freed with
237 * mono_profiler_call_context_free_buffer.
239 * Please note that a call context is only valid for the duration of the
240 * enter/leave callback it was passed to.
242 * This function is not async safe.
244 MONO_API void *mono_profiler_call_context_get_this (MonoProfilerCallContext *context);
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.
251 * The buffer returned by this function must be freed with
252 * mono_profiler_call_context_free_buffer.
254 * Please note that a call context is only valid for the duration of the
255 * enter/leave callback it was passed to.
257 * This function is not async safe.
259 MONO_API void *mono_profiler_call_context_get_argument (MonoProfilerCallContext *context, uint32_t position);
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.
266 * The buffer returned by this function must be freed with
267 * mono_profiler_call_context_free_buffer.
269 * Please note that a call context is only valid for the duration of the
270 * enter/leave callback it was passed to.
272 * This function is not async safe.
274 MONO_API void *mono_profiler_call_context_get_local (MonoProfilerCallContext *context, uint32_t position);
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.
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_result (MonoProfilerCallContext *context);
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.
297 * This function is not async safe.
299 MONO_API void mono_profiler_call_context_free_buffer (void *buffer);
301 #ifdef MONO_PROFILER_UNSTABLE_GC_ROOTS
304 MONO_PROFILER_GC_ROOT_PINNING = 1 << 8,
305 MONO_PROFILER_GC_ROOT_WEAKREF = 2 << 8,
306 MONO_PROFILER_GC_ROOT_INTERIOR = 4 << 8,
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,
315 MONO_PROFILER_GC_ROOT_TYPEMASK = 0xff,
316 } MonoProfilerGCRootType;
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;
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;
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
352 * typedef void (*MonoProfilerRuntimeInitializedCallback (MonoProfiler *prof);
353 * MONO_API void mono_profiler_set_runtime_initialized_callback (MonoProfiler *prof, MonoProfilerRuntimeInitializedCallback cb);
355 * typedef void (*MonoProfilerRuntimeShutdownCallback (MonoProfiler *prof);
356 * MONO_API void mono_profiler_set_runtime_shutdown_callback (MonoProfiler *prof, MonoProfilerRuntimeShutdownCallback cb);
358 * typedef void (*MonoProfilerContextLoadedCallback (MonoProfiler *prof);
359 * MONO_API void mono_profiler_set_context_loaded_callback (MonoProfiler *prof, MonoProfilerContextLoadedCallback cb);
361 * typedef void (*MonoProfilerContextUnloadedCallback (MonoProfiler *prof);
362 * MONO_API void mono_profiler_set_context_unloaded_callback (MonoProfiler *prof, MonoProfilerContextUnloadedCallback cb);
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.
371 * These functions are async safe.
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
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
416 #endif // __MONO_PROFILER_H__