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 #include <mono/metadata/assembly.h>
8 #include <mono/metadata/gc-internals.h>
9 #include <mono/metadata/mono-config-dirs.h>
10 #include <mono/metadata/mono-debug.h>
11 #include <mono/metadata/profiler-private.h>
12 #include <mono/utils/mono-dl.h>
13 #include <mono/utils/mono-error-internals.h>
14 #include <mono/utils/mono-logger-internals.h>
16 MonoProfilerState mono_profiler_state;
18 typedef void (*MonoProfilerInitializer) (const char *);
20 #define OLD_INITIALIZER_NAME "mono_profiler_startup"
21 #define NEW_INITIALIZER_NAME "mono_profiler_init"
24 load_profiler (MonoDl *module, const char *name, const char *desc)
29 char *err, *old_name = g_strdup_printf (OLD_INITIALIZER_NAME);
30 MonoProfilerInitializer func;
32 if (!(err = mono_dl_symbol (module, old_name, (gpointer) &func))) {
33 mono_profiler_printf_err ("Found old-style startup symbol '%s' for the '%s' profiler; it has not been migrated to the new API.", old_name, name);
41 char *new_name = g_strdup_printf (NEW_INITIALIZER_NAME "_%s", name);
43 if ((err = mono_dl_symbol (module, new_name, (gpointer *) &func))) {
57 load_profiler_from_executable (const char *name, const char *desc)
62 * Some profilers (such as ours) may need to call back into the runtime
63 * from their sampling callback (which is called in async-signal context).
64 * They need to be able to know that all references back to the runtime
65 * have been resolved; otherwise, calling runtime functions may result in
66 * invoking the dynamic linker which is not async-signal-safe. Passing
67 * MONO_DL_EAGER will ask the dynamic linker to resolve everything upfront.
69 MonoDl *module = mono_dl_open (NULL, MONO_DL_EAGER, &err);
72 mono_profiler_printf_err ("Could not open main executable: %s", err);
77 return load_profiler (module, name, desc);
81 load_profiler_from_directory (const char *directory, const char *libname, const char *name, const char *desc)
86 while ((path = mono_dl_build_path (directory, libname, &iter))) {
87 // See the comment in load_embedded_profiler ().
88 MonoDl *module = mono_dl_open (path, MONO_DL_EAGER, NULL);
93 return load_profiler (module, name, desc);
100 load_profiler_from_installation (const char *libname, const char *name, const char *desc)
103 MonoDl *module = mono_dl_open_runtime_lib (libname, MONO_DL_EAGER, &err);
108 return load_profiler (module, name, desc);
114 mono_profiler_load (const char *desc)
116 if (!desc || !strcmp ("default", desc))
119 const char *col = strchr (desc, ':');
123 mname = (char *) g_memdup (desc, col - desc + 1);
124 mname [col - desc] = 0;
126 mname = g_strdup (desc);
128 if (!load_profiler_from_executable (mname, desc)) {
129 char *libname = g_strdup_printf ("mono-profiler-%s", mname);
130 gboolean res = load_profiler_from_installation (libname, mname, desc);
132 if (!res && mono_config_get_assemblies_dir ())
133 res = load_profiler_from_directory (mono_assembly_getrootdir (), libname, mname, desc);
136 res = load_profiler_from_directory (NULL, libname, mname, desc);
139 mono_profiler_printf_err ("The '%s' profiler wasn't found in the main executable nor could it be loaded from '%s'.", mname, libname);
148 mono_profiler_create (MonoProfiler *prof)
150 MonoProfilerHandle handle = g_new0 (struct _MonoProfilerDesc, 1);
153 handle->next = mono_profiler_state.profilers;
155 mono_profiler_state.profilers = handle;
161 mono_profiler_set_cleanup_callback (MonoProfilerHandle handle, MonoProfilerCleanupCallback cb)
163 InterlockedWritePointer (&handle->cleanup_callback, (gpointer) cb);
167 mono_profiler_set_coverage_filter_callback (MonoProfilerHandle handle, MonoProfilerCoverageFilterCallback cb)
169 InterlockedWritePointer (&handle->coverage_filter, (gpointer) cb);
173 mono_profiler_enable_coverage (void)
175 if (mono_profiler_state.startup_done)
178 mono_os_mutex_init (&mono_profiler_state.coverage_mutex);
179 mono_profiler_state.coverage_hash = g_hash_table_new (NULL, NULL);
181 if (!mono_debug_enabled ())
182 mono_debug_init (MONO_DEBUG_FORMAT_MONO);
184 return mono_profiler_state.code_coverage = TRUE;
190 mono_os_mutex_lock (&mono_profiler_state.coverage_mutex);
194 coverage_unlock (void)
196 mono_os_mutex_unlock (&mono_profiler_state.coverage_mutex);
200 mono_profiler_get_coverage_data (MonoProfilerHandle handle, MonoMethod *method, MonoProfilerCoverageCallback cb)
202 if (!mono_profiler_state.code_coverage)
207 MonoProfilerCoverageInfo *info = g_hash_table_lookup (mono_profiler_state.coverage_hash, method);
215 MonoMethodHeader *header = mono_method_get_header_checked (method, &error);
216 mono_error_assert_ok (&error);
220 const unsigned char *start = mono_method_header_get_code (header, &size, NULL);
221 const unsigned char *end = start + size;
222 MonoDebugMethodInfo *minfo = mono_debug_lookup_method (method);
224 for (guint32 i = 0; i < info->entries; i++) {
225 guchar *cil_code = info->data [i].cil_code;
227 if (cil_code && cil_code >= start && cil_code < end) {
228 guint32 offset = cil_code - start;
230 MonoProfilerCoverageData data = {
233 .counter = info->data [i].count,
239 MonoDebugSourceLocation *loc = mono_debug_method_lookup_location (minfo, offset);
242 data.file_name = g_strdup (loc->source_file);
243 data.line = loc->row;
244 data.column = loc->column;
246 mono_debug_free_source_location (loc);
250 cb (handle->prof, &data);
252 g_free ((char *) data.file_name);
256 mono_metadata_free_mh (header);
261 MonoProfilerCoverageInfo *
262 mono_profiler_coverage_alloc (MonoMethod *method, guint32 entries)
264 if (!mono_profiler_state.code_coverage)
267 gboolean cover = FALSE;
269 for (MonoProfilerHandle handle = mono_profiler_state.profilers; handle; handle = handle->next) {
270 MonoProfilerCoverageFilterCallback cb = handle->coverage_filter;
273 cover |= cb (handle->prof, method);
281 MonoProfilerCoverageInfo *info = g_malloc0 (sizeof (MonoProfilerCoverageInfo) + SIZEOF_VOID_P * 2 * entries);
283 info->entries = entries;
285 g_hash_table_insert (mono_profiler_state.coverage_hash, method, info);
293 mono_profiler_enable_sampling (MonoProfilerHandle handle)
295 if (mono_profiler_state.startup_done)
298 if (mono_profiler_state.sampling_owner)
301 mono_profiler_state.sampling_owner = handle;
302 mono_profiler_state.sample_mode = MONO_PROFILER_SAMPLE_MODE_NONE;
303 mono_profiler_state.sample_freq = 100;
304 mono_os_sem_init (&mono_profiler_state.sampling_semaphore, 0);
310 mono_profiler_set_sample_mode (MonoProfilerHandle handle, MonoProfilerSampleMode mode, uint32_t freq)
312 if (handle != mono_profiler_state.sampling_owner)
315 mono_profiler_state.sample_mode = mode;
316 mono_profiler_state.sample_freq = freq;
318 mono_profiler_sampling_thread_post ();
324 mono_profiler_get_sample_mode (MonoProfilerHandle handle, MonoProfilerSampleMode *mode, uint32_t *freq)
327 *mode = mono_profiler_state.sample_mode;
330 *freq = mono_profiler_state.sample_freq;
332 return handle == mono_profiler_state.sampling_owner;
336 mono_profiler_sampling_enabled (void)
338 return !!mono_profiler_state.sampling_owner;
342 mono_profiler_sampling_thread_post (void)
344 mono_os_sem_post (&mono_profiler_state.sampling_semaphore);
348 mono_profiler_sampling_thread_wait (void)
350 mono_os_sem_wait (&mono_profiler_state.sampling_semaphore, MONO_SEM_FLAGS_NONE);
354 mono_profiler_enable_allocations (void)
356 if (mono_profiler_state.startup_done)
359 return mono_profiler_state.allocations = TRUE;
363 mono_profiler_set_call_instrumentation_filter_callback (MonoProfilerHandle handle, MonoProfilerCallInstrumentationFilterCallback cb)
365 InterlockedWritePointer (&handle->call_instrumentation_filter, (gpointer) cb);
369 mono_profiler_enable_call_context_introspection (void)
371 if (mono_profiler_state.startup_done)
374 mono_profiler_state.context_enable ();
376 return mono_profiler_state.call_contexts = TRUE;
380 mono_profiler_call_context_get_this (MonoProfilerCallContext *context)
382 if (!mono_profiler_state.call_contexts)
385 return mono_profiler_state.context_get_this (context);
389 mono_profiler_call_context_get_argument (MonoProfilerCallContext *context, uint32_t position)
391 if (!mono_profiler_state.call_contexts)
394 return mono_profiler_state.context_get_argument (context, position);
398 mono_profiler_call_context_get_local (MonoProfilerCallContext *context, uint32_t position)
400 if (!mono_profiler_state.call_contexts)
403 return mono_profiler_state.context_get_local (context, position);
407 mono_profiler_call_context_get_result (MonoProfilerCallContext *context)
409 if (!mono_profiler_state.call_contexts)
412 return mono_profiler_state.context_get_result (context);
416 mono_profiler_call_context_free_buffer (void *buffer)
418 mono_profiler_state.context_free_buffer (buffer);
421 MonoProfilerCallInstrumentationFlags
422 mono_profiler_get_call_instrumentation_flags (MonoMethod *method)
424 MonoProfilerCallInstrumentationFlags flags = MONO_PROFILER_CALL_INSTRUMENTATION_NONE;
426 for (MonoProfilerHandle handle = mono_profiler_state.profilers; handle; handle = handle->next) {
427 MonoProfilerCallInstrumentationFilterCallback cb = handle->call_instrumentation_filter;
430 flags |= cb (handle->prof, method);
437 mono_profiler_started (void)
439 mono_profiler_state.startup_done = TRUE;
443 mono_profiler_cleanup (void)
445 for (MonoProfilerHandle handle = mono_profiler_state.profilers; handle; handle = handle->next) {
446 #define _MONO_PROFILER_EVENT(name) \
447 mono_profiler_set_ ## name ## _callback (handle, NULL); \
448 g_assert (!handle->name ## _cb);
449 #define MONO_PROFILER_EVENT_0(name, type) \
450 _MONO_PROFILER_EVENT(name)
451 #define MONO_PROFILER_EVENT_1(name, type, arg1_type, arg1_name) \
452 _MONO_PROFILER_EVENT(name)
453 #define MONO_PROFILER_EVENT_2(name, type, arg1_type, arg1_name, arg2_type, arg2_name) \
454 _MONO_PROFILER_EVENT(name)
455 #define MONO_PROFILER_EVENT_3(name, type, arg1_type, arg1_name, arg2_type, arg2_name, arg3_type, arg3_name) \
456 _MONO_PROFILER_EVENT(name)
457 #define MONO_PROFILER_EVENT_4(name, type, arg1_type, arg1_name, arg2_type, arg2_name, arg3_type, arg3_name, arg4_type, arg4_name) \
458 _MONO_PROFILER_EVENT(name)
459 #include <mono/metadata/profiler-events.h>
460 #undef MONO_PROFILER_EVENT_0
461 #undef MONO_PROFILER_EVENT_1
462 #undef MONO_PROFILER_EVENT_2
463 #undef MONO_PROFILER_EVENT_3
464 #undef MONO_PROFILER_EVENT_4
465 #undef _MONO_PROFILER_EVENT
468 #define _MONO_PROFILER_EVENT(name, type) \
469 g_assert (!mono_profiler_state.name ## _count);
470 #define MONO_PROFILER_EVENT_0(name, type) \
471 _MONO_PROFILER_EVENT(name, type)
472 #define MONO_PROFILER_EVENT_1(name, type, arg1_type, arg1_name) \
473 _MONO_PROFILER_EVENT(name, type)
474 #define MONO_PROFILER_EVENT_2(name, type, arg1_type, arg1_name, arg2_type, arg2_name) \
475 _MONO_PROFILER_EVENT(name, type)
476 #define MONO_PROFILER_EVENT_3(name, type, arg1_type, arg1_name, arg2_type, arg2_name, arg3_type, arg3_name) \
477 _MONO_PROFILER_EVENT(name, type)
478 #define MONO_PROFILER_EVENT_4(name, type, arg1_type, arg1_name, arg2_type, arg2_name, arg3_type, arg3_name, arg4_type, arg4_name) \
479 _MONO_PROFILER_EVENT(name, type)
480 #include <mono/metadata/profiler-events.h>
481 #undef MONO_PROFILER_EVENT_0
482 #undef MONO_PROFILER_EVENT_1
483 #undef MONO_PROFILER_EVENT_2
484 #undef MONO_PROFILER_EVENT_3
485 #undef MONO_PROFILER_EVENT_4
486 #undef _MONO_PROFILER_EVENT
488 MonoProfilerHandle head = mono_profiler_state.profilers;
491 MonoProfilerCleanupCallback cb = head->cleanup_callback;
496 MonoProfilerHandle cur = head;
502 if (mono_profiler_state.code_coverage) {
503 mono_os_mutex_destroy (&mono_profiler_state.coverage_mutex);
507 g_hash_table_iter_init (&iter, mono_profiler_state.coverage_hash);
509 MonoProfilerCoverageInfo *info;
511 while (g_hash_table_iter_next (&iter, NULL, (gpointer *) &info))
514 g_hash_table_destroy (mono_profiler_state.coverage_hash);
517 if (mono_profiler_state.sampling_owner)
518 mono_os_sem_destroy (&mono_profiler_state.sampling_semaphore);
522 update_callback (volatile gpointer *location, gpointer new_, volatile gint32 *counter)
527 old = InterlockedReadPointer (location);
528 } while (InterlockedCompareExchangePointer (location, new_, old) != old);
531 * At this point, we could have installed a NULL callback while the counter
532 * is still non-zero, i.e. setting the callback and modifying the counter
533 * is not a single atomic operation. This is fine as we make sure callbacks
534 * are non-NULL before invoking them (see the code below that generates the
535 * raise functions), and besides, updating callbacks at runtime is an
536 * inherently racy operation.
540 InterlockedDecrement (counter);
543 InterlockedIncrement (counter);
546 #define _MONO_PROFILER_EVENT(name, type) \
548 mono_profiler_set_ ## name ## _callback (MonoProfilerHandle handle, MonoProfiler ## type ## Callback cb) \
550 update_callback (&handle->name ## _cb, (gpointer) cb, &mono_profiler_state.name ## _count); \
552 #define MONO_PROFILER_EVENT_0(name, type) \
553 _MONO_PROFILER_EVENT(name, type)
554 #define MONO_PROFILER_EVENT_1(name, type, arg1_type, arg1_name) \
555 _MONO_PROFILER_EVENT(name, type)
556 #define MONO_PROFILER_EVENT_2(name, type, arg1_type, arg1_name, arg2_type, arg2_name) \
557 _MONO_PROFILER_EVENT(name, type)
558 #define MONO_PROFILER_EVENT_3(name, type, arg1_type, arg1_name, arg2_type, arg2_name, arg3_type, arg3_name) \
559 _MONO_PROFILER_EVENT(name, type)
560 #define MONO_PROFILER_EVENT_4(name, type, arg1_type, arg1_name, arg2_type, arg2_name, arg3_type, arg3_name, arg4_type, arg4_name) \
561 _MONO_PROFILER_EVENT(name, type)
562 #include <mono/metadata/profiler-events.h>
563 #undef MONO_PROFILER_EVENT_0
564 #undef MONO_PROFILER_EVENT_1
565 #undef MONO_PROFILER_EVENT_2
566 #undef MONO_PROFILER_EVENT_3
567 #undef MONO_PROFILER_EVENT_4
568 #undef _MONO_PROFILER_EVENT
570 #define _MONO_PROFILER_EVENT(name, type, params, args) \
572 mono_profiler_raise_ ## name params \
574 for (MonoProfilerHandle h = mono_profiler_state.profilers; h; h = h->next) { \
575 MonoProfiler ## type ## Callback cb = h->name ## _cb; \
580 #define MONO_PROFILER_EVENT_0(name, type) \
581 _MONO_PROFILER_EVENT(name, type, (void), (h->prof))
582 #define MONO_PROFILER_EVENT_1(name, type, arg1_type, arg1_name) \
583 _MONO_PROFILER_EVENT(name, type, (arg1_type arg1_name), (h->prof, arg1_name))
584 #define MONO_PROFILER_EVENT_2(name, type, arg1_type, arg1_name, arg2_type, arg2_name) \
585 _MONO_PROFILER_EVENT(name, type, (arg1_type arg1_name, arg2_type arg2_name), (h->prof, arg1_name, arg2_name))
586 #define MONO_PROFILER_EVENT_3(name, type, arg1_type, arg1_name, arg2_type, arg2_name, arg3_type, arg3_name) \
587 _MONO_PROFILER_EVENT(name, type, (arg1_type arg1_name, arg2_type arg2_name, arg3_type arg3_name), (h->prof, arg1_name, arg2_name, arg3_name))
588 #define MONO_PROFILER_EVENT_4(name, type, arg1_type, arg1_name, arg2_type, arg2_name, arg3_type, arg3_name, arg4_type, arg4_name) \
589 _MONO_PROFILER_EVENT(name, type, (arg1_type arg1_name, arg2_type arg2_name, arg3_type arg3_name, arg4_type arg4_name), (h->prof, arg1_name, arg2_name, arg3_name, arg4_name))
590 #include <mono/metadata/profiler-events.h>
591 #undef MONO_PROFILER_EVENT_0
592 #undef MONO_PROFILER_EVENT_1
593 #undef MONO_PROFILER_EVENT_2
594 #undef MONO_PROFILER_EVENT_3
595 #undef MONO_PROFILER_EVENT_4
596 #undef _MONO_PROFILER_EVENT
599 * The following code is here to maintain compatibility with a few profiler API
600 * functions used by Xamarin.{Android,iOS,Mac} so that they keep working
601 * regardless of which system Mono version is used.
603 * TODO: Remove this some day if we're OK with breaking compatibility.
606 typedef void *MonoLegacyProfiler;
608 typedef void (*MonoLegacyProfileFunc) (MonoLegacyProfiler *prof);
609 typedef void (*MonoLegacyProfileThreadFunc) (MonoLegacyProfiler *prof, uintptr_t tid);
610 typedef void (*MonoLegacyProfileGCFunc) (MonoLegacyProfiler *prof, MonoProfilerGCEvent event, int generation);
611 typedef void (*MonoLegacyProfileGCResizeFunc) (MonoLegacyProfiler *prof, int64_t new_size);
612 typedef void (*MonoLegacyProfileJitResult) (MonoLegacyProfiler *prof, MonoMethod *method, MonoJitInfo *jinfo, int result);
614 struct _MonoProfiler {
615 MonoProfilerHandle handle;
616 MonoLegacyProfiler *profiler;
617 MonoLegacyProfileFunc shutdown_callback;
618 MonoLegacyProfileThreadFunc thread_start, thread_end;
619 MonoLegacyProfileGCFunc gc_event;
620 MonoLegacyProfileGCResizeFunc gc_heap_resize;
621 MonoLegacyProfileJitResult jit_end2;
624 static MonoProfiler *current;
626 MONO_API void mono_profiler_install (MonoLegacyProfiler *prof, MonoLegacyProfileFunc callback);
627 MONO_API void mono_profiler_install_thread (MonoLegacyProfileThreadFunc start, MonoLegacyProfileThreadFunc end);
628 MONO_API void mono_profiler_install_gc (MonoLegacyProfileGCFunc callback, MonoLegacyProfileGCResizeFunc heap_resize_callback);
629 MONO_API void mono_profiler_install_jit_end (MonoLegacyProfileJitResult end);
630 MONO_API void mono_profiler_set_events (int flags);
633 shutdown_cb (MonoProfiler *prof)
635 prof->shutdown_callback (prof->profiler);
639 mono_profiler_install (MonoLegacyProfiler *prof, MonoLegacyProfileFunc callback)
641 current = g_new0 (MonoProfiler, 1);
642 current->handle = mono_profiler_create (current);
643 current->profiler = prof;
644 current->shutdown_callback = callback;
647 mono_profiler_set_runtime_shutdown_end_callback (current->handle, shutdown_cb);
651 thread_start_cb (MonoProfiler *prof, uintptr_t tid)
653 prof->thread_start (prof->profiler, tid);
657 thread_stop_cb (MonoProfiler *prof, uintptr_t tid)
659 prof->thread_end (prof->profiler, tid);
663 mono_profiler_install_thread (MonoLegacyProfileThreadFunc start, MonoLegacyProfileThreadFunc end)
665 current->thread_start = start;
666 current->thread_end = end;
669 mono_profiler_set_thread_started_callback (current->handle, thread_start_cb);
672 mono_profiler_set_thread_stopped_callback (current->handle, thread_stop_cb);
676 gc_event_cb (MonoProfiler *prof, MonoProfilerGCEvent event, uint32_t generation)
678 prof->gc_event (prof->profiler, event, generation);
682 gc_resize_cb (MonoProfiler *prof, uintptr_t size)
684 prof->gc_heap_resize (prof->profiler, size);
688 mono_profiler_install_gc (MonoLegacyProfileGCFunc callback, MonoLegacyProfileGCResizeFunc heap_resize_callback)
690 current->gc_event = callback;
691 current->gc_heap_resize = heap_resize_callback;
694 mono_profiler_set_gc_event_callback (current->handle, gc_event_cb);
696 if (heap_resize_callback)
697 mono_profiler_set_gc_resize_callback (current->handle, gc_resize_cb);
701 jit_done_cb (MonoProfiler *prof, MonoMethod *method, MonoJitInfo *jinfo)
703 prof->jit_end2 (prof->profiler, method, jinfo, 0);
707 jit_failed_cb (MonoProfiler *prof, MonoMethod *method)
709 prof->jit_end2 (prof->profiler, method, NULL, 1);
713 mono_profiler_install_jit_end (MonoLegacyProfileJitResult end)
715 current->jit_end2 = end;
718 mono_profiler_set_jit_done_callback (current->handle, jit_done_cb);
719 mono_profiler_set_jit_failed_callback (current->handle, jit_failed_cb);
724 mono_profiler_set_events (int flags)