3 * Profiler interface for Mono
6 * Paolo Molaro (lupus@ximian.com)
7 * Alex Rønne Petersen (alexrp@xamarin.com)
9 * Copyright 2001-2003 Ximian, Inc (http://www.ximian.com)
10 * Copyright 2004-2009 Novell, Inc (http://www.novell.com)
11 * Copyright 2011 Xamarin Inc (http://www.xamarin.com).
12 * Licensed under the MIT license. See LICENSE file in the project root for full license information.
16 #include "mono/metadata/profiler-private.h"
17 #include "mono/metadata/assembly.h"
18 #include "mono/metadata/debug-helpers.h"
19 #include "mono/metadata/mono-debug.h"
20 #include "mono/metadata/debug-internals.h"
21 #include "mono/metadata/metadata-internals.h"
22 #include "mono/metadata/class-internals.h"
23 #include "mono/metadata/domain-internals.h"
24 #include "mono/metadata/gc-internals.h"
25 #include "mono/metadata/mono-config-dirs.h"
26 #include "mono/utils/mono-dl.h"
27 #include <mono/utils/mono-logger-internals.h>
32 #ifdef HAVE_SYS_TIME_H
35 #ifdef HAVE_BACKTRACE_SYMBOLS
39 typedef struct _ProfilerDesc ProfilerDesc;
40 struct _ProfilerDesc {
42 MonoProfiler *profiler;
43 MonoProfileFlags events;
45 MonoProfileAppDomainFunc domain_start_load;
46 MonoProfileAppDomainResult domain_end_load;
47 MonoProfileAppDomainFunc domain_start_unload;
48 MonoProfileAppDomainFunc domain_end_unload;
49 MonoProfileAppDomainFriendlyNameFunc domain_name;
51 MonoProfileContextFunc context_load;
52 MonoProfileContextFunc context_unload;
54 MonoProfileAssemblyFunc assembly_start_load;
55 MonoProfileAssemblyResult assembly_end_load;
56 MonoProfileAssemblyFunc assembly_start_unload;
57 MonoProfileAssemblyFunc assembly_end_unload;
59 MonoProfileModuleFunc module_start_load;
60 MonoProfileModuleResult module_end_load;
61 MonoProfileModuleFunc module_start_unload;
62 MonoProfileModuleFunc module_end_unload;
64 MonoProfileClassFunc class_start_load;
65 MonoProfileClassResult class_end_load;
66 MonoProfileClassFunc class_start_unload;
67 MonoProfileClassFunc class_end_unload;
69 MonoProfileMethodFunc jit_start;
70 MonoProfileMethodResult jit_end;
71 MonoProfileJitResult jit_end2;
72 MonoProfileMethodFunc method_free;
73 MonoProfileMethodFunc method_start_invoke;
74 MonoProfileMethodFunc method_end_invoke;
75 MonoProfileMethodResult man_unman_transition;
76 MonoProfileAllocFunc allocation_cb;
77 MonoProfileMonitorFunc monitor_event_cb;
78 MonoProfileStatFunc statistical_cb;
79 MonoProfileStatCallChainFunc statistical_call_chain_cb;
80 int statistical_call_chain_depth;
81 MonoProfilerCallChainStrategy statistical_call_chain_strategy;
82 MonoProfileMethodFunc method_enter;
83 MonoProfileMethodFunc method_leave;
85 MonoProfileExceptionFunc exception_throw_cb;
86 MonoProfileMethodFunc exception_method_leave_cb;
87 MonoProfileExceptionClauseFunc exception_clause_cb;
88 MonoProfileExceptionClauseFunc2 exception_clause_cb2;
90 MonoProfileIomapFunc iomap_cb;
92 MonoProfileThreadFunc thread_start;
93 MonoProfileThreadFunc thread_end;
94 MonoProfileThreadNameFunc thread_name;
96 MonoProfileCoverageFilterFunc coverage_filter_cb;
98 MonoProfileFunc shutdown_callback;
100 MonoProfileGCFunc gc_event;
101 MonoProfileGCResizeFunc gc_heap_resize;
102 MonoProfileGCMoveFunc gc_moves;
103 MonoProfileGCHandleFunc gc_handle;
104 MonoProfileGCRootFunc gc_roots;
106 MonoProfileGCFinalizeFunc gc_finalize_begin;
107 MonoProfileGCFinalizeObjectFunc gc_finalize_object_begin;
108 MonoProfileGCFinalizeObjectFunc gc_finalize_object_end;
109 MonoProfileGCFinalizeFunc gc_finalize_end;
111 MonoProfileFunc runtime_initialized_event;
113 MonoProfilerCodeChunkNew code_chunk_new;
114 MonoProfilerCodeChunkDestroy code_chunk_destroy;
115 MonoProfilerCodeBufferNew code_buffer_new;
118 static ProfilerDesc *prof_list = NULL;
120 #define mono_profiler_coverage_lock() mono_os_mutex_lock (&profiler_coverage_mutex)
121 #define mono_profiler_coverage_unlock() mono_os_mutex_unlock (&profiler_coverage_mutex)
122 static mono_mutex_t profiler_coverage_mutex;
124 /* this is directly accessible to other mono libs.
125 * It is the ORed value of all the profiler's events.
127 MonoProfileFlags mono_profiler_events;
130 * mono_profiler_install:
131 * \param prof a \c MonoProfiler structure pointer, or a pointer to a derived structure.
132 * \param callback the function to invoke at shutdown
133 * Use \c mono_profiler_install to activate profiling in the Mono runtime.
134 * Typically developers of new profilers will create a new structure whose
135 * first field is a \c MonoProfiler and put any extra information that they need
136 * to access from the various profiling callbacks there.
139 mono_profiler_install (MonoProfiler *prof, MonoProfileFunc callback)
141 ProfilerDesc *desc = g_new0 (ProfilerDesc, 1);
143 mono_os_mutex_init_recursive (&profiler_coverage_mutex);
144 desc->profiler = prof;
145 desc->shutdown_callback = callback;
146 desc->next = prof_list;
151 * mono_profiler_set_events:
152 * \param events an ORed set of values made up of \c MONO_PROFILER_ flags
153 * The events described in the \p events argument is a set of flags
154 * that represent which profiling events must be triggered. For
155 * example if you have registered a set of methods for tracking
156 * JIT compilation start and end with \c mono_profiler_install_jit_compile,
157 * you will want to pass the \c MONO_PROFILE_JIT_COMPILATION flag to
160 * You can call \c mono_profile_set_events more than once and you can
161 * do this at runtime to modify which methods are invoked.
164 mono_profiler_set_events (MonoProfileFlags events)
167 MonoProfileFlags value = (MonoProfileFlags)0;
169 prof_list->events = events;
170 for (prof = prof_list; prof; prof = prof->next)
171 value = (MonoProfileFlags)(value | prof->events);
172 mono_profiler_events = value;
176 * mono_profiler_get_events:
178 * Returns a list of active events that will be intercepted.
181 mono_profiler_get_events (void)
183 return mono_profiler_events;
187 * mono_profiler_install_enter_leave:
188 * \param enter the routine to be called on each method entry
189 * \param fleave the routine to be called each time a method returns
191 * Use this routine to install routines that will be called everytime
192 * a method enters and leaves. The routines will receive as an argument
193 * the \c MonoMethod representing the method that is entering or leaving.
196 mono_profiler_install_enter_leave (MonoProfileMethodFunc enter, MonoProfileMethodFunc fleave)
200 prof_list->method_enter = enter;
201 prof_list->method_leave = fleave;
205 * mono_profiler_install_jit_compile:
206 * \param start the routine to be called when the JIT process starts.
207 * \param end the routine to be called when the JIT process ends.
209 * Use this routine to install routines that will be called when JIT
210 * compilation of a method starts and completes.
213 mono_profiler_install_jit_compile (MonoProfileMethodFunc start, MonoProfileMethodResult end)
217 prof_list->jit_start = start;
218 prof_list->jit_end = end;
222 mono_profiler_install_jit_end (MonoProfileJitResult end)
226 prof_list->jit_end2 = end;
230 mono_profiler_install_method_free (MonoProfileMethodFunc callback)
234 prof_list->method_free = callback;
238 mono_profiler_install_method_invoke (MonoProfileMethodFunc start, MonoProfileMethodFunc end)
242 prof_list->method_start_invoke = start;
243 prof_list->method_end_invoke = end;
247 * mono_profiler_install_thread:
250 mono_profiler_install_thread (MonoProfileThreadFunc start, MonoProfileThreadFunc end)
254 prof_list->thread_start = start;
255 prof_list->thread_end = end;
259 mono_profiler_install_thread_name (MonoProfileThreadNameFunc thread_name_cb)
263 prof_list->thread_name = thread_name_cb;
267 * mono_profiler_install_transition:
270 mono_profiler_install_transition (MonoProfileMethodResult callback)
274 prof_list->man_unman_transition = callback;
278 * mono_profiler_install_allocation:
281 mono_profiler_install_allocation (MonoProfileAllocFunc callback)
285 prof_list->allocation_cb = callback;
289 mono_profiler_install_monitor (MonoProfileMonitorFunc callback)
293 prof_list->monitor_event_cb = callback;
296 static MonoProfileSamplingMode sampling_mode = MONO_PROFILER_STAT_MODE_PROCESS;
297 static int64_t sampling_frequency = 100; // Hz
300 * mono_profiler_set_statistical_mode:
301 * \param mode the sampling mode used.
302 * \param sample_frequency_is_us the sampling frequency in microseconds.
304 * Set the sampling parameters for the profiler. Sampling mode affects the effective sampling rate as in samples/s you'll witness.
305 * The default sampling mode is process mode, which only reports samples when there's activity in the process.
307 * Sampling frequency should be interpreted as a suggestion that can't always be honored due to how most kernels expose alarms.
309 * Said that, when using statistical sampling, always assume variable rate sampling as all sort of external factors can interfere.
312 mono_profiler_set_statistical_mode (MonoProfileSamplingMode mode, int64_t sampling_frequency_hz)
314 sampling_mode = mode;
315 sampling_frequency = sampling_frequency_hz;
319 * mono_profiler_install_statistical:
322 mono_profiler_install_statistical (MonoProfileStatFunc callback)
326 prof_list->statistical_cb = callback;
330 mono_profiler_get_sampling_rate (void)
332 return sampling_frequency;
335 MonoProfileSamplingMode
336 mono_profiler_get_sampling_mode (void)
338 return sampling_mode;
342 mono_profiler_install_statistical_call_chain (MonoProfileStatCallChainFunc callback, int call_chain_depth, MonoProfilerCallChainStrategy call_chain_strategy) {
345 if (call_chain_depth > MONO_PROFILER_MAX_STAT_CALL_CHAIN_DEPTH) {
346 call_chain_depth = MONO_PROFILER_MAX_STAT_CALL_CHAIN_DEPTH;
348 if ((call_chain_strategy >= MONO_PROFILER_CALL_CHAIN_INVALID) || (call_chain_strategy < MONO_PROFILER_CALL_CHAIN_NONE)) {
349 call_chain_strategy = MONO_PROFILER_CALL_CHAIN_NONE;
351 prof_list->statistical_call_chain_cb = callback;
352 prof_list->statistical_call_chain_depth = call_chain_depth;
353 prof_list->statistical_call_chain_strategy = call_chain_strategy;
357 mono_profiler_stat_get_call_chain_depth (void) {
358 if (prof_list && prof_list->statistical_call_chain_cb != NULL) {
359 return prof_list->statistical_call_chain_depth;
365 MonoProfilerCallChainStrategy
366 mono_profiler_stat_get_call_chain_strategy (void) {
367 if (prof_list && prof_list->statistical_call_chain_cb != NULL) {
368 return prof_list->statistical_call_chain_strategy;
370 return MONO_PROFILER_CALL_CHAIN_NONE;
374 void mono_profiler_install_exception (MonoProfileExceptionFunc throw_callback, MonoProfileMethodFunc exc_method_leave, MonoProfileExceptionClauseFunc clause_callback)
378 prof_list->exception_throw_cb = throw_callback;
379 prof_list->exception_method_leave_cb = exc_method_leave;
380 prof_list->exception_clause_cb = clause_callback;
383 void mono_profiler_install_exception_clause (MonoProfileExceptionClauseFunc2 clause_callback)
388 prof_list->exception_clause_cb2 = clause_callback;
392 * mono_profiler_install_coverage_filter:
395 mono_profiler_install_coverage_filter (MonoProfileCoverageFilterFunc callback)
399 prof_list->coverage_filter_cb = callback;
403 * mono_profiler_install_appdomain:
406 mono_profiler_install_appdomain (MonoProfileAppDomainFunc start_load, MonoProfileAppDomainResult end_load,
407 MonoProfileAppDomainFunc start_unload, MonoProfileAppDomainFunc end_unload)
412 prof_list->domain_start_load = start_load;
413 prof_list->domain_end_load = end_load;
414 prof_list->domain_start_unload = start_unload;
415 prof_list->domain_end_unload = end_unload;
419 mono_profiler_install_appdomain_name (MonoProfileAppDomainFriendlyNameFunc domain_name_cb)
424 prof_list->domain_name = domain_name_cb;
428 mono_profiler_install_context (MonoProfileContextFunc load, MonoProfileContextFunc unload)
433 prof_list->context_load = load;
434 prof_list->context_unload = unload;
438 * mono_profiler_install_assembly:
441 mono_profiler_install_assembly (MonoProfileAssemblyFunc start_load, MonoProfileAssemblyResult end_load,
442 MonoProfileAssemblyFunc start_unload, MonoProfileAssemblyFunc end_unload)
446 prof_list->assembly_start_load = start_load;
447 prof_list->assembly_end_load = end_load;
448 prof_list->assembly_start_unload = start_unload;
449 prof_list->assembly_end_unload = end_unload;
453 * mono_profiler_install_module:
456 mono_profiler_install_module (MonoProfileModuleFunc start_load, MonoProfileModuleResult end_load,
457 MonoProfileModuleFunc start_unload, MonoProfileModuleFunc end_unload)
461 prof_list->module_start_load = start_load;
462 prof_list->module_end_load = end_load;
463 prof_list->module_start_unload = start_unload;
464 prof_list->module_end_unload = end_unload;
468 * mono_profiler_install_class:
471 mono_profiler_install_class (MonoProfileClassFunc start_load, MonoProfileClassResult end_load,
472 MonoProfileClassFunc start_unload, MonoProfileClassFunc end_unload)
476 prof_list->class_start_load = start_load;
477 prof_list->class_end_load = end_load;
478 prof_list->class_start_unload = start_unload;
479 prof_list->class_end_unload = end_unload;
483 * mono_profiler_method_enter:
486 mono_profiler_method_enter (MonoMethod *method)
489 for (prof = prof_list; prof; prof = prof->next) {
490 if ((prof->events & MONO_PROFILE_ENTER_LEAVE) && prof->method_enter)
491 prof->method_enter (prof->profiler, method);
496 * mono_profiler_method_leave:
499 mono_profiler_method_leave (MonoMethod *method)
502 for (prof = prof_list; prof; prof = prof->next) {
503 if ((prof->events & MONO_PROFILE_ENTER_LEAVE) && prof->method_leave)
504 prof->method_leave (prof->profiler, method);
509 * mono_profiler_method_jit:
512 mono_profiler_method_jit (MonoMethod *method)
515 for (prof = prof_list; prof; prof = prof->next) {
516 if ((prof->events & MONO_PROFILE_JIT_COMPILATION) && prof->jit_start)
517 prof->jit_start (prof->profiler, method);
522 * mono_profiler_method_end_jit:
525 mono_profiler_method_end_jit (MonoMethod *method, MonoJitInfo* jinfo, int result)
528 for (prof = prof_list; prof; prof = prof->next) {
529 if ((prof->events & MONO_PROFILE_JIT_COMPILATION)) {
531 prof->jit_end (prof->profiler, method, result);
533 prof->jit_end2 (prof->profiler, method, jinfo, result);
539 mono_profiler_method_free (MonoMethod *method)
542 for (prof = prof_list; prof; prof = prof->next) {
543 if ((prof->events & MONO_PROFILE_METHOD_EVENTS) && prof->method_free)
544 prof->method_free (prof->profiler, method);
549 mono_profiler_method_start_invoke (MonoMethod *method)
552 for (prof = prof_list; prof; prof = prof->next) {
553 if ((prof->events & MONO_PROFILE_METHOD_EVENTS) && prof->method_start_invoke)
554 prof->method_start_invoke (prof->profiler, method);
559 mono_profiler_method_end_invoke (MonoMethod *method)
562 for (prof = prof_list; prof; prof = prof->next) {
563 if ((prof->events & MONO_PROFILE_METHOD_EVENTS) && prof->method_end_invoke)
564 prof->method_end_invoke (prof->profiler, method);
569 * mono_profiler_code_transition:
572 mono_profiler_code_transition (MonoMethod *method, int result)
575 for (prof = prof_list; prof; prof = prof->next) {
576 if ((prof->events & MONO_PROFILE_TRANSITIONS) && prof->man_unman_transition)
577 prof->man_unman_transition (prof->profiler, method, result);
582 * mono_profiler_allocation:
585 mono_profiler_allocation (MonoObject *obj)
588 for (prof = prof_list; prof; prof = prof->next) {
589 if ((prof->events & MONO_PROFILE_ALLOCATIONS) && prof->allocation_cb)
590 prof->allocation_cb (prof->profiler, obj, obj->vtable->klass);
595 mono_profiler_monitor_event (MonoObject *obj, MonoProfilerMonitorEvent event) {
597 for (prof = prof_list; prof; prof = prof->next) {
598 if ((prof->events & MONO_PROFILE_MONITOR_EVENTS) && prof->monitor_event_cb)
599 prof->monitor_event_cb (prof->profiler, obj, event);
604 * mono_profiler_stat_hit:
607 mono_profiler_stat_hit (guchar *ip, void *context)
610 for (prof = prof_list; prof; prof = prof->next) {
611 if ((prof->events & MONO_PROFILE_STATISTICAL) && prof->statistical_cb)
612 prof->statistical_cb (prof->profiler, ip, context);
617 mono_profiler_stat_call_chain (int call_chain_depth, guchar **ips, void *context)
620 for (prof = prof_list; prof; prof = prof->next) {
621 if ((prof->events & MONO_PROFILE_STATISTICAL) && prof->statistical_call_chain_cb)
622 prof->statistical_call_chain_cb (prof->profiler, call_chain_depth, ips, context);
627 mono_profiler_exception_thrown (MonoObject *exception)
630 for (prof = prof_list; prof; prof = prof->next) {
631 if ((prof->events & MONO_PROFILE_EXCEPTIONS) && prof->exception_throw_cb)
632 prof->exception_throw_cb (prof->profiler, exception);
637 mono_profiler_exception_method_leave (MonoMethod *method)
640 for (prof = prof_list; prof; prof = prof->next) {
641 if ((prof->events & MONO_PROFILE_EXCEPTIONS) && prof->exception_method_leave_cb)
642 prof->exception_method_leave_cb (prof->profiler, method);
647 mono_profiler_exception_clause_handler (MonoMethod *method, int clause_type, int clause_num, MonoObject *exc)
650 for (prof = prof_list; prof; prof = prof->next) {
651 if (prof->events & MONO_PROFILE_EXCEPTIONS) {
652 if (prof->exception_clause_cb)
653 prof->exception_clause_cb (prof->profiler, method, clause_type, clause_num);
655 if (prof->exception_clause_cb2)
656 prof->exception_clause_cb2 (prof->profiler, method, clause_type, clause_num, exc);
662 * mono_profiler_thread_start:
665 mono_profiler_thread_start (gsize tid)
668 for (prof = prof_list; prof; prof = prof->next) {
669 if ((prof->events & MONO_PROFILE_THREADS) && prof->thread_start)
670 prof->thread_start (prof->profiler, tid);
675 * mono_profiler_thread_end:
678 mono_profiler_thread_end (gsize tid)
681 for (prof = prof_list; prof; prof = prof->next) {
682 if ((prof->events & MONO_PROFILE_THREADS) && prof->thread_end)
683 prof->thread_end (prof->profiler, tid);
688 mono_profiler_thread_name (gsize tid, const char *name)
691 for (prof = prof_list; prof; prof = prof->next) {
692 if ((prof->events & MONO_PROFILE_THREADS) && prof->thread_name)
693 prof->thread_name (prof->profiler, tid, name);
698 * mono_profiler_assembly_event:
701 mono_profiler_assembly_event (MonoAssembly *assembly, int code)
704 for (prof = prof_list; prof; prof = prof->next) {
705 if (!(prof->events & MONO_PROFILE_ASSEMBLY_EVENTS))
709 case MONO_PROFILE_START_LOAD:
710 if (prof->assembly_start_load)
711 prof->assembly_start_load (prof->profiler, assembly);
713 case MONO_PROFILE_START_UNLOAD:
714 if (prof->assembly_start_unload)
715 prof->assembly_start_unload (prof->profiler, assembly);
717 case MONO_PROFILE_END_UNLOAD:
718 if (prof->assembly_end_unload)
719 prof->assembly_end_unload (prof->profiler, assembly);
722 g_assert_not_reached ();
728 * mono_profiler_assembly_loaded:
731 mono_profiler_assembly_loaded (MonoAssembly *assembly, int result)
734 for (prof = prof_list; prof; prof = prof->next) {
735 if ((prof->events & MONO_PROFILE_ASSEMBLY_EVENTS) && prof->assembly_end_load)
736 prof->assembly_end_load (prof->profiler, assembly, result);
740 void mono_profiler_iomap (char *report, const char *pathname, const char *new_pathname)
743 for (prof = prof_list; prof; prof = prof->next) {
744 if ((prof->events & MONO_PROFILE_IOMAP_EVENTS) && prof->iomap_cb)
745 prof->iomap_cb (prof->profiler, report, pathname, new_pathname);
750 * mono_profiler_module_event:
753 mono_profiler_module_event (MonoImage *module, int code)
756 for (prof = prof_list; prof; prof = prof->next) {
757 if (!(prof->events & MONO_PROFILE_MODULE_EVENTS))
761 case MONO_PROFILE_START_LOAD:
762 if (prof->module_start_load)
763 prof->module_start_load (prof->profiler, module);
765 case MONO_PROFILE_START_UNLOAD:
766 if (prof->module_start_unload)
767 prof->module_start_unload (prof->profiler, module);
769 case MONO_PROFILE_END_UNLOAD:
770 if (prof->module_end_unload)
771 prof->module_end_unload (prof->profiler, module);
774 g_assert_not_reached ();
780 * mono_profiler_module_loaded:
783 mono_profiler_module_loaded (MonoImage *module, int result)
786 for (prof = prof_list; prof; prof = prof->next) {
787 if ((prof->events & MONO_PROFILE_MODULE_EVENTS) && prof->module_end_load)
788 prof->module_end_load (prof->profiler, module, result);
793 * mono_profiler_class_event:
796 mono_profiler_class_event (MonoClass *klass, int code)
799 for (prof = prof_list; prof; prof = prof->next) {
800 if (!(prof->events & MONO_PROFILE_CLASS_EVENTS))
804 case MONO_PROFILE_START_LOAD:
805 if (prof->class_start_load)
806 prof->class_start_load (prof->profiler, klass);
808 case MONO_PROFILE_START_UNLOAD:
809 if (prof->class_start_unload)
810 prof->class_start_unload (prof->profiler, klass);
812 case MONO_PROFILE_END_UNLOAD:
813 if (prof->class_end_unload)
814 prof->class_end_unload (prof->profiler, klass);
817 g_assert_not_reached ();
823 * mono_profiler_class_loaded:
826 mono_profiler_class_loaded (MonoClass *klass, int result)
829 for (prof = prof_list; prof; prof = prof->next) {
830 if ((prof->events & MONO_PROFILE_CLASS_EVENTS) && prof->class_end_load)
831 prof->class_end_load (prof->profiler, klass, result);
836 * mono_profiler_appdomain_event:
839 mono_profiler_appdomain_event (MonoDomain *domain, int code)
842 for (prof = prof_list; prof; prof = prof->next) {
843 if (!(prof->events & MONO_PROFILE_APPDOMAIN_EVENTS))
847 case MONO_PROFILE_START_LOAD:
848 if (prof->domain_start_load)
849 prof->domain_start_load (prof->profiler, domain);
851 case MONO_PROFILE_START_UNLOAD:
852 if (prof->domain_start_unload)
853 prof->domain_start_unload (prof->profiler, domain);
855 case MONO_PROFILE_END_UNLOAD:
856 if (prof->domain_end_unload)
857 prof->domain_end_unload (prof->profiler, domain);
860 g_assert_not_reached ();
866 * mono_profiler_appdomain_loaded:
869 mono_profiler_appdomain_loaded (MonoDomain *domain, int result)
872 for (prof = prof_list; prof; prof = prof->next) {
873 if ((prof->events & MONO_PROFILE_APPDOMAIN_EVENTS) && prof->domain_end_load)
874 prof->domain_end_load (prof->profiler, domain, result);
879 mono_profiler_appdomain_name (MonoDomain *domain, const char *name)
881 for (ProfilerDesc *prof = prof_list; prof; prof = prof->next)
882 if ((prof->events & MONO_PROFILE_APPDOMAIN_EVENTS) && prof->domain_name)
883 prof->domain_name (prof->profiler, domain, name);
887 mono_profiler_context_loaded (MonoAppContext *context)
889 for (ProfilerDesc *prof = prof_list; prof; prof = prof->next)
890 if ((prof->events & MONO_PROFILE_CONTEXT_EVENTS) && prof->context_load)
891 prof->context_load (prof->profiler, context);
895 mono_profiler_context_unloaded (MonoAppContext *context)
897 for (ProfilerDesc *prof = prof_list; prof; prof = prof->next)
898 if ((prof->events & MONO_PROFILE_CONTEXT_EVENTS) && prof->context_unload)
899 prof->context_unload (prof->profiler, context);
903 * mono_profiler_shutdown:
906 mono_profiler_shutdown (void)
909 for (prof = prof_list; prof; prof = prof->next) {
910 if (prof->shutdown_callback)
911 prof->shutdown_callback (prof->profiler);
914 mono_profiler_set_events ((MonoProfileFlags)0);
918 * mono_profiler_gc_heap_resize:
921 mono_profiler_gc_heap_resize (gint64 new_size)
924 for (prof = prof_list; prof; prof = prof->next) {
925 if ((prof->events & MONO_PROFILE_GC) && prof->gc_heap_resize)
926 prof->gc_heap_resize (prof->profiler, new_size);
931 * mono_profiler_gc_event:
934 mono_profiler_gc_event (MonoGCEvent event, int generation)
937 for (prof = prof_list; prof; prof = prof->next) {
938 if ((prof->events & MONO_PROFILE_GC) && prof->gc_event)
939 prof->gc_event (prof->profiler, event, generation);
944 mono_profiler_gc_moves (void **objects, int num)
947 for (prof = prof_list; prof; prof = prof->next) {
948 if ((prof->events & MONO_PROFILE_GC_MOVES) && prof->gc_moves)
949 prof->gc_moves (prof->profiler, objects, num);
954 mono_profiler_gc_handle (int op, int type, uintptr_t handle, MonoObject *obj)
957 for (prof = prof_list; prof; prof = prof->next) {
958 if ((prof->events & MONO_PROFILE_GC_ROOTS) && prof->gc_handle)
959 prof->gc_handle (prof->profiler, op, type, handle, obj);
964 mono_profiler_gc_roots (int num, void **objects, int *root_types, uintptr_t *extra_info)
967 for (prof = prof_list; prof; prof = prof->next) {
968 if ((prof->events & MONO_PROFILE_GC_ROOTS) && prof->gc_roots)
969 prof->gc_roots (prof->profiler, num, objects, root_types, extra_info);
974 * mono_profiler_install_gc:
977 mono_profiler_install_gc (MonoProfileGCFunc callback, MonoProfileGCResizeFunc heap_resize_callback)
981 prof_list->gc_event = callback;
982 prof_list->gc_heap_resize = heap_resize_callback;
986 * mono_profiler_install_gc_moves:
987 * \param callback callback function
989 * Install the \p callback function that the GC will call when moving objects.
990 * The callback receives an array of pointers and the number of elements
991 * in the array. Every even element in the array is the original object location
992 * and the following odd element is the new location of the object in memory.
993 * So the number of elements argument will always be a multiple of 2.
994 * Since this callback happens during the GC, it is a restricted environment:
995 * no locks can be taken and the object pointers can be inspected only once
996 * the GC is finished (of course the original location pointers will not
997 * point to valid objects anymore).
1000 mono_profiler_install_gc_moves (MonoProfileGCMoveFunc callback)
1004 prof_list->gc_moves = callback;
1008 * mono_profiler_install_gc_roots:
1009 * \param handle_callback callback function
1010 * \param roots_callback callback function
1012 * Install the \p handle_callback function that the GC will call when GC
1013 * handles are created or destroyed.
1014 * The callback receives an operation, which is either \c MONO_PROFILER_GC_HANDLE_CREATED
1015 * or \c MONO_PROFILER_GC_HANDLE_DESTROYED, the handle type, the handle value and the
1016 * object pointer, if present.
1017 * Install the \p roots_callback function that the GC will call when tracing
1018 * the roots for a collection.
1019 * The callback receives the number of elements and three arrays: an array
1020 * of objects, an array of root types and flags and an array of extra info.
1021 * The size of each array is given by the first argument.
1024 mono_profiler_install_gc_roots (MonoProfileGCHandleFunc handle_callback, MonoProfileGCRootFunc roots_callback)
1028 prof_list->gc_handle = handle_callback;
1029 prof_list->gc_roots = roots_callback;
1033 mono_profiler_gc_finalize_begin (void)
1035 for (ProfilerDesc *prof = prof_list; prof; prof = prof->next)
1036 if ((prof->events & MONO_PROFILE_GC_FINALIZATION) && prof->gc_finalize_begin)
1037 prof->gc_finalize_begin (prof->profiler);
1041 mono_profiler_gc_finalize_object_begin (MonoObject *obj)
1043 for (ProfilerDesc *prof = prof_list; prof; prof = prof->next)
1044 if ((prof->events & MONO_PROFILE_GC_FINALIZATION) && prof->gc_finalize_object_begin)
1045 prof->gc_finalize_object_begin (prof->profiler, obj);
1049 mono_profiler_gc_finalize_object_end (MonoObject *obj)
1051 for (ProfilerDesc *prof = prof_list; prof; prof = prof->next)
1052 if ((prof->events & MONO_PROFILE_GC_FINALIZATION) && prof->gc_finalize_object_end)
1053 prof->gc_finalize_object_end (prof->profiler, obj);
1057 mono_profiler_gc_finalize_end (void)
1059 for (ProfilerDesc *prof = prof_list; prof; prof = prof->next)
1060 if ((prof->events & MONO_PROFILE_GC_FINALIZATION) && prof->gc_finalize_end)
1061 prof->gc_finalize_end (prof->profiler);
1065 mono_profiler_install_gc_finalize (MonoProfileGCFinalizeFunc begin, MonoProfileGCFinalizeObjectFunc begin_obj, MonoProfileGCFinalizeObjectFunc end_obj, MonoProfileGCFinalizeFunc end)
1070 prof_list->gc_finalize_begin = begin;
1071 prof_list->gc_finalize_object_begin = begin_obj;
1072 prof_list->gc_finalize_object_end = end_obj;
1073 prof_list->gc_finalize_end = end;
1077 mono_profiler_install_runtime_initialized (MonoProfileFunc runtime_initialized_callback)
1081 prof_list->runtime_initialized_event = runtime_initialized_callback;
1085 mono_profiler_runtime_initialized (void) {
1087 for (prof = prof_list; prof; prof = prof->next) {
1088 if (prof->runtime_initialized_event)
1089 prof->runtime_initialized_event (prof->profiler);
1094 mono_profiler_install_code_chunk_new (MonoProfilerCodeChunkNew callback) {
1097 prof_list->code_chunk_new = callback;
1100 mono_profiler_code_chunk_new (gpointer chunk, int size) {
1102 for (prof = prof_list; prof; prof = prof->next) {
1103 if (prof->code_chunk_new)
1104 prof->code_chunk_new (prof->profiler, chunk, size);
1109 mono_profiler_install_code_chunk_destroy (MonoProfilerCodeChunkDestroy callback) {
1112 prof_list->code_chunk_destroy = callback;
1115 mono_profiler_code_chunk_destroy (gpointer chunk) {
1117 for (prof = prof_list; prof; prof = prof->next) {
1118 if (prof->code_chunk_destroy)
1119 prof->code_chunk_destroy (prof->profiler, chunk);
1124 mono_profiler_install_code_buffer_new (MonoProfilerCodeBufferNew callback) {
1127 prof_list->code_buffer_new = callback;
1131 mono_profiler_install_iomap (MonoProfileIomapFunc callback)
1135 prof_list->iomap_cb = callback;
1139 mono_profiler_code_buffer_new (gpointer buffer, int size, MonoProfilerCodeBufferType type, gconstpointer data) {
1141 for (prof = prof_list; prof; prof = prof->next) {
1142 if (prof->code_buffer_new)
1143 prof->code_buffer_new (prof->profiler, buffer, size, type, (void*)data);
1147 static GHashTable *coverage_hash = NULL;
1149 MonoProfileCoverageInfo*
1150 mono_profiler_coverage_alloc (MonoMethod *method, int entries)
1152 MonoProfileCoverageInfo *res;
1153 int instrument = FALSE;
1156 for (prof = prof_list; prof; prof = prof->next) {
1157 /* note that we call the filter on all the profilers even if just
1158 * a single one would be enough to instrument a method
1160 if (prof->coverage_filter_cb)
1161 if (prof->coverage_filter_cb (prof->profiler, method))
1167 mono_profiler_coverage_lock ();
1169 coverage_hash = g_hash_table_new (NULL, NULL);
1171 res = (MonoProfileCoverageInfo *)g_malloc0 (sizeof (MonoProfileCoverageInfo) + sizeof (void*) * 2 * entries);
1173 res->entries = entries;
1175 g_hash_table_insert (coverage_hash, method, res);
1176 mono_profiler_coverage_unlock ();
1181 /* safe only when the method antive code has been unloaded */
1183 mono_profiler_coverage_free (MonoMethod *method)
1185 MonoProfileCoverageInfo* info;
1187 mono_profiler_coverage_lock ();
1188 if (!coverage_hash) {
1189 mono_profiler_coverage_unlock ();
1193 info = (MonoProfileCoverageInfo *)g_hash_table_lookup (coverage_hash, method);
1196 g_hash_table_remove (coverage_hash, method);
1198 mono_profiler_coverage_unlock ();
1202 * mono_profiler_coverage_get:
1203 * \param prof The profiler handle, installed with mono_profiler_install
1204 * \param method the method to gather information from.
1205 * \param func A routine that will be called back with the results
1207 * If the \c MONO_PROFILER_INS_COVERAGE flag was active during JIT compilation
1208 * it is possible to obtain coverage information about a give method.
1210 * The function \p func will be invoked repeatedly with instances of the
1211 * \c MonoProfileCoverageEntry structure.
1214 mono_profiler_coverage_get (MonoProfiler *prof, MonoMethod *method, MonoProfileCoverageFunc func)
1217 MonoProfileCoverageInfo* info = NULL;
1220 const unsigned char *start, *end, *cil_code;
1221 MonoMethodHeader *header;
1222 MonoProfileCoverageEntry entry;
1223 MonoDebugMethodInfo *debug_minfo;
1225 mono_profiler_coverage_lock ();
1227 info = (MonoProfileCoverageInfo *)g_hash_table_lookup (coverage_hash, method);
1228 mono_profiler_coverage_unlock ();
1233 header = mono_method_get_header_checked (method, &error);
1234 mono_error_assert_ok (&error);
1235 start = mono_method_header_get_code (header, &code_size, NULL);
1236 debug_minfo = mono_debug_lookup_method (method);
1238 end = start + code_size;
1239 for (i = 0; i < info->entries; ++i) {
1240 cil_code = info->data [i].cil_code;
1241 if (cil_code && cil_code >= start && cil_code < end) {
1243 offset = cil_code - start;
1244 entry.iloffset = offset;
1245 entry.method = method;
1246 entry.counter = info->data [i].count;
1247 entry.line = entry.col = 1;
1248 entry.filename = NULL;
1250 MonoDebugSourceLocation *location;
1252 location = mono_debug_method_lookup_location (debug_minfo, offset);
1254 entry.line = location->row;
1255 entry.col = location->column;
1256 entry.filename = fname = g_strdup (location->source_file);
1257 mono_debug_free_source_location (location);
1261 func (prof, &entry);
1265 mono_metadata_free_mh (header);
1268 typedef void (*ProfilerInitializer) (const char*);
1269 #define INITIALIZER_NAME "mono_profiler_startup"
1273 load_profiler (MonoDl *pmodule, const char *desc, const char *symbol)
1276 ProfilerInitializer func;
1281 if ((err = mono_dl_symbol (pmodule, symbol, (gpointer *) &func))) {
1291 load_embedded_profiler (const char *desc, const char *name)
1295 MonoDl *pmodule = NULL;
1299 * Some profilers (such as ours) may need to call back into the runtime
1300 * from their sampling callback (which is called in async-signal context).
1301 * They need to be able to know that all references back to the runtime
1302 * have been resolved; otherwise, calling runtime functions may result in
1303 * invoking the dynamic linker which is not async-signal-safe. Passing
1304 * MONO_DL_EAGER will ask the dynamic linker to resolve everything upfront.
1306 pmodule = mono_dl_open (NULL, MONO_DL_EAGER, &err);
1308 g_warning ("Could not open main executable (%s)", err);
1313 symbol = g_strdup_printf (INITIALIZER_NAME "_%s", name);
1314 result = load_profiler (pmodule, desc, symbol);
1320 // TODO: Much of the library loading code here is custom. It would be better to merge this with mono-dl
1322 load_profiler_from_directory (const char *directory, const char *libname, const char *desc)
1324 MonoDl *pmodule = NULL;
1329 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT, "Attempting to load profiler %s from %s (desc %s)", libname, directory, desc);
1333 while ((path = mono_dl_build_path (directory, libname, &iter))) {
1334 pmodule = mono_dl_open (path, MONO_DL_EAGER, &err);
1335 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT, "Attempting to load profiler: %s, %ssuccessful, err: %s", path, pmodule?"":"not ", err);
1339 return load_profiler (pmodule, desc, INITIALIZER_NAME);
1346 load_profiler_from_mono_installation (const char *libname, const char *desc)
1349 MonoDl *pmodule = mono_dl_open_runtime_lib (libname, MONO_DL_EAGER, &err);
1350 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT, "Attempting to load profiler from runtime libs: %s, %ssuccessful, err: %s", libname, pmodule?"":"not ", err);
1353 return load_profiler (pmodule, desc, INITIALIZER_NAME);
1358 * mono_profiler_load:
1359 * \param desc arguments to configure the profiler
1361 * Invoke this method to initialize the profiler. This will drive the
1362 * loading of the internal ("default") or any external profilers.
1364 * This routine is invoked by Mono's driver, but must be called manually
1365 * if you embed Mono into your application.
1368 mono_profiler_load (const char *desc)
1371 mono_gc_base_init ();
1373 if (!desc || (strcmp ("default", desc) == 0)) {
1374 desc = "log:report";
1376 /* we keep command-line compat with the old version here */
1377 if (strncmp (desc, "default:", 8) == 0) {
1378 gchar **args, **ptr;
1379 GString *str = g_string_new ("log:report");
1380 args = g_strsplit (desc + 8, ",", -1);
1381 for (ptr = args; ptr && *ptr; ptr++) {
1382 const char *arg = *ptr;
1384 if (!strcmp (arg, "time"))
1385 g_string_append (str, ",calls");
1386 else if (!strcmp (arg, "alloc"))
1387 g_string_append (str, ",alloc");
1388 else if (!strcmp (arg, "stat"))
1389 g_string_append (str, ",sample");
1390 else if (!strcmp (arg, "jit"))
1391 continue; /* accept and do nothing */
1392 else if (strncmp (arg, "file=", 5) == 0) {
1393 g_string_append_printf (str, ",output=%s", arg + 5);
1395 fprintf (stderr, "profiler : Unknown argument '%s'.\n", arg);
1399 desc = cdesc = g_string_free (str, FALSE);
1402 const char* col = strchr (desc, ':');
1405 gboolean res = FALSE;
1408 mname = (char *)g_memdup (desc, col - desc + 1);
1409 mname [col - desc] = 0;
1411 mname = g_strdup (desc);
1413 if (!load_embedded_profiler (desc, mname)) {
1414 libname = g_strdup_printf ("mono-profiler-%s", mname);
1415 res = load_profiler_from_mono_installation (libname, desc);
1416 if (!res && mono_config_get_assemblies_dir ())
1417 res = load_profiler_from_directory (mono_assembly_getrootdir (), libname, desc);
1419 res = load_profiler_from_directory (NULL, libname, desc);
1421 g_warning ("The '%s' profiler wasn't found in the main executable nor could it be loaded from '%s'.", mname, libname);