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;
89 MonoProfileIomapFunc iomap_cb;
91 MonoProfileThreadFunc thread_start;
92 MonoProfileThreadFunc thread_end;
93 MonoProfileThreadNameFunc thread_name;
95 MonoProfileCoverageFilterFunc coverage_filter_cb;
97 MonoProfileFunc shutdown_callback;
99 MonoProfileGCFunc gc_event;
100 MonoProfileGCResizeFunc gc_heap_resize;
101 MonoProfileGCMoveFunc gc_moves;
102 MonoProfileGCHandleFunc gc_handle;
103 MonoProfileGCRootFunc gc_roots;
105 MonoProfileGCFinalizeFunc gc_finalize_begin;
106 MonoProfileGCFinalizeObjectFunc gc_finalize_object_begin;
107 MonoProfileGCFinalizeObjectFunc gc_finalize_object_end;
108 MonoProfileGCFinalizeFunc gc_finalize_end;
110 MonoProfileFunc runtime_initialized_event;
112 MonoProfilerCodeChunkNew code_chunk_new;
113 MonoProfilerCodeChunkDestroy code_chunk_destroy;
114 MonoProfilerCodeBufferNew code_buffer_new;
117 static ProfilerDesc *prof_list = NULL;
119 #define mono_profiler_coverage_lock() mono_os_mutex_lock (&profiler_coverage_mutex)
120 #define mono_profiler_coverage_unlock() mono_os_mutex_unlock (&profiler_coverage_mutex)
121 static mono_mutex_t profiler_coverage_mutex;
123 /* this is directly accessible to other mono libs.
124 * It is the ORed value of all the profiler's events.
126 MonoProfileFlags mono_profiler_events;
129 * mono_profiler_install:
130 * \param prof a \c MonoProfiler structure pointer, or a pointer to a derived structure.
131 * \param callback the function to invoke at shutdown
132 * Use \c mono_profiler_install to activate profiling in the Mono runtime.
133 * Typically developers of new profilers will create a new structure whose
134 * first field is a \c MonoProfiler and put any extra information that they need
135 * to access from the various profiling callbacks there.
138 mono_profiler_install (MonoProfiler *prof, MonoProfileFunc callback)
140 ProfilerDesc *desc = g_new0 (ProfilerDesc, 1);
142 mono_os_mutex_init_recursive (&profiler_coverage_mutex);
143 desc->profiler = prof;
144 desc->shutdown_callback = callback;
145 desc->next = prof_list;
150 * mono_profiler_set_events:
151 * \param events an ORed set of values made up of \c MONO_PROFILER_ flags
152 * The events described in the \p events argument is a set of flags
153 * that represent which profiling events must be triggered. For
154 * example if you have registered a set of methods for tracking
155 * JIT compilation start and end with \c mono_profiler_install_jit_compile,
156 * you will want to pass the \c MONO_PROFILE_JIT_COMPILATION flag to
159 * You can call \c mono_profile_set_events more than once and you can
160 * do this at runtime to modify which methods are invoked.
163 mono_profiler_set_events (MonoProfileFlags events)
166 MonoProfileFlags value = (MonoProfileFlags)0;
168 prof_list->events = events;
169 for (prof = prof_list; prof; prof = prof->next)
170 value = (MonoProfileFlags)(value | prof->events);
171 mono_profiler_events = value;
175 * mono_profiler_get_events:
177 * Returns a list of active events that will be intercepted.
180 mono_profiler_get_events (void)
182 return mono_profiler_events;
186 * mono_profiler_install_enter_leave:
187 * \param enter the routine to be called on each method entry
188 * \param fleave the routine to be called each time a method returns
190 * Use this routine to install routines that will be called everytime
191 * a method enters and leaves. The routines will receive as an argument
192 * the \c MonoMethod representing the method that is entering or leaving.
195 mono_profiler_install_enter_leave (MonoProfileMethodFunc enter, MonoProfileMethodFunc fleave)
199 prof_list->method_enter = enter;
200 prof_list->method_leave = fleave;
204 * mono_profiler_install_jit_compile:
205 * \param start the routine to be called when the JIT process starts.
206 * \param end the routine to be called when the JIT process ends.
208 * Use this routine to install routines that will be called when JIT
209 * compilation of a method starts and completes.
212 mono_profiler_install_jit_compile (MonoProfileMethodFunc start, MonoProfileMethodResult end)
216 prof_list->jit_start = start;
217 prof_list->jit_end = end;
221 mono_profiler_install_jit_end (MonoProfileJitResult end)
225 prof_list->jit_end2 = end;
229 mono_profiler_install_method_free (MonoProfileMethodFunc callback)
233 prof_list->method_free = callback;
237 mono_profiler_install_method_invoke (MonoProfileMethodFunc start, MonoProfileMethodFunc end)
241 prof_list->method_start_invoke = start;
242 prof_list->method_end_invoke = end;
246 * mono_profiler_install_thread:
249 mono_profiler_install_thread (MonoProfileThreadFunc start, MonoProfileThreadFunc end)
253 prof_list->thread_start = start;
254 prof_list->thread_end = end;
258 mono_profiler_install_thread_name (MonoProfileThreadNameFunc thread_name_cb)
262 prof_list->thread_name = thread_name_cb;
266 * mono_profiler_install_transition:
269 mono_profiler_install_transition (MonoProfileMethodResult callback)
273 prof_list->man_unman_transition = callback;
277 * mono_profiler_install_allocation:
280 mono_profiler_install_allocation (MonoProfileAllocFunc callback)
284 prof_list->allocation_cb = callback;
288 mono_profiler_install_monitor (MonoProfileMonitorFunc callback)
292 prof_list->monitor_event_cb = callback;
295 static MonoProfileSamplingMode sampling_mode = MONO_PROFILER_STAT_MODE_PROCESS;
296 static int64_t sampling_frequency = 100; // Hz
299 * mono_profiler_set_statistical_mode:
300 * \param mode the sampling mode used.
301 * \param sample_frequency_is_us the sampling frequency in microseconds.
303 * Set the sampling parameters for the profiler. Sampling mode affects the effective sampling rate as in samples/s you'll witness.
304 * The default sampling mode is process mode, which only reports samples when there's activity in the process.
306 * Sampling frequency should be interpreted as a suggestion that can't always be honored due to how most kernels expose alarms.
308 * Said that, when using statistical sampling, always assume variable rate sampling as all sort of external factors can interfere.
311 mono_profiler_set_statistical_mode (MonoProfileSamplingMode mode, int64_t sampling_frequency_hz)
313 sampling_mode = mode;
314 sampling_frequency = sampling_frequency_hz;
318 * mono_profiler_install_statistical:
321 mono_profiler_install_statistical (MonoProfileStatFunc callback)
325 prof_list->statistical_cb = callback;
329 mono_profiler_get_sampling_rate (void)
331 return sampling_frequency;
334 MonoProfileSamplingMode
335 mono_profiler_get_sampling_mode (void)
337 return sampling_mode;
341 mono_profiler_install_statistical_call_chain (MonoProfileStatCallChainFunc callback, int call_chain_depth, MonoProfilerCallChainStrategy call_chain_strategy) {
344 if (call_chain_depth > MONO_PROFILER_MAX_STAT_CALL_CHAIN_DEPTH) {
345 call_chain_depth = MONO_PROFILER_MAX_STAT_CALL_CHAIN_DEPTH;
347 if ((call_chain_strategy >= MONO_PROFILER_CALL_CHAIN_INVALID) || (call_chain_strategy < MONO_PROFILER_CALL_CHAIN_NONE)) {
348 call_chain_strategy = MONO_PROFILER_CALL_CHAIN_NONE;
350 prof_list->statistical_call_chain_cb = callback;
351 prof_list->statistical_call_chain_depth = call_chain_depth;
352 prof_list->statistical_call_chain_strategy = call_chain_strategy;
356 mono_profiler_stat_get_call_chain_depth (void) {
357 if (prof_list && prof_list->statistical_call_chain_cb != NULL) {
358 return prof_list->statistical_call_chain_depth;
364 MonoProfilerCallChainStrategy
365 mono_profiler_stat_get_call_chain_strategy (void) {
366 if (prof_list && prof_list->statistical_call_chain_cb != NULL) {
367 return prof_list->statistical_call_chain_strategy;
369 return MONO_PROFILER_CALL_CHAIN_NONE;
373 void mono_profiler_install_exception (MonoProfileExceptionFunc throw_callback, MonoProfileMethodFunc exc_method_leave, MonoProfileExceptionClauseFunc clause_callback)
377 prof_list->exception_throw_cb = throw_callback;
378 prof_list->exception_method_leave_cb = exc_method_leave;
379 prof_list->exception_clause_cb = clause_callback;
383 * mono_profiler_install_coverage_filter:
386 mono_profiler_install_coverage_filter (MonoProfileCoverageFilterFunc callback)
390 prof_list->coverage_filter_cb = callback;
394 * mono_profiler_install_appdomain:
397 mono_profiler_install_appdomain (MonoProfileAppDomainFunc start_load, MonoProfileAppDomainResult end_load,
398 MonoProfileAppDomainFunc start_unload, MonoProfileAppDomainFunc end_unload)
403 prof_list->domain_start_load = start_load;
404 prof_list->domain_end_load = end_load;
405 prof_list->domain_start_unload = start_unload;
406 prof_list->domain_end_unload = end_unload;
410 mono_profiler_install_appdomain_name (MonoProfileAppDomainFriendlyNameFunc domain_name_cb)
415 prof_list->domain_name = domain_name_cb;
419 mono_profiler_install_context (MonoProfileContextFunc load, MonoProfileContextFunc unload)
424 prof_list->context_load = load;
425 prof_list->context_unload = unload;
429 * mono_profiler_install_assembly:
432 mono_profiler_install_assembly (MonoProfileAssemblyFunc start_load, MonoProfileAssemblyResult end_load,
433 MonoProfileAssemblyFunc start_unload, MonoProfileAssemblyFunc end_unload)
437 prof_list->assembly_start_load = start_load;
438 prof_list->assembly_end_load = end_load;
439 prof_list->assembly_start_unload = start_unload;
440 prof_list->assembly_end_unload = end_unload;
444 * mono_profiler_install_module:
447 mono_profiler_install_module (MonoProfileModuleFunc start_load, MonoProfileModuleResult end_load,
448 MonoProfileModuleFunc start_unload, MonoProfileModuleFunc end_unload)
452 prof_list->module_start_load = start_load;
453 prof_list->module_end_load = end_load;
454 prof_list->module_start_unload = start_unload;
455 prof_list->module_end_unload = end_unload;
459 * mono_profiler_install_class:
462 mono_profiler_install_class (MonoProfileClassFunc start_load, MonoProfileClassResult end_load,
463 MonoProfileClassFunc start_unload, MonoProfileClassFunc end_unload)
467 prof_list->class_start_load = start_load;
468 prof_list->class_end_load = end_load;
469 prof_list->class_start_unload = start_unload;
470 prof_list->class_end_unload = end_unload;
474 * mono_profiler_method_enter:
477 mono_profiler_method_enter (MonoMethod *method)
480 for (prof = prof_list; prof; prof = prof->next) {
481 if ((prof->events & MONO_PROFILE_ENTER_LEAVE) && prof->method_enter)
482 prof->method_enter (prof->profiler, method);
487 * mono_profiler_method_leave:
490 mono_profiler_method_leave (MonoMethod *method)
493 for (prof = prof_list; prof; prof = prof->next) {
494 if ((prof->events & MONO_PROFILE_ENTER_LEAVE) && prof->method_leave)
495 prof->method_leave (prof->profiler, method);
500 * mono_profiler_method_jit:
503 mono_profiler_method_jit (MonoMethod *method)
506 for (prof = prof_list; prof; prof = prof->next) {
507 if ((prof->events & MONO_PROFILE_JIT_COMPILATION) && prof->jit_start)
508 prof->jit_start (prof->profiler, method);
513 * mono_profiler_method_end_jit:
516 mono_profiler_method_end_jit (MonoMethod *method, MonoJitInfo* jinfo, int result)
519 for (prof = prof_list; prof; prof = prof->next) {
520 if ((prof->events & MONO_PROFILE_JIT_COMPILATION)) {
522 prof->jit_end (prof->profiler, method, result);
524 prof->jit_end2 (prof->profiler, method, jinfo, result);
530 mono_profiler_method_free (MonoMethod *method)
533 for (prof = prof_list; prof; prof = prof->next) {
534 if ((prof->events & MONO_PROFILE_METHOD_EVENTS) && prof->method_free)
535 prof->method_free (prof->profiler, method);
540 mono_profiler_method_start_invoke (MonoMethod *method)
543 for (prof = prof_list; prof; prof = prof->next) {
544 if ((prof->events & MONO_PROFILE_METHOD_EVENTS) && prof->method_start_invoke)
545 prof->method_start_invoke (prof->profiler, method);
550 mono_profiler_method_end_invoke (MonoMethod *method)
553 for (prof = prof_list; prof; prof = prof->next) {
554 if ((prof->events & MONO_PROFILE_METHOD_EVENTS) && prof->method_end_invoke)
555 prof->method_end_invoke (prof->profiler, method);
560 * mono_profiler_code_transition:
563 mono_profiler_code_transition (MonoMethod *method, int result)
566 for (prof = prof_list; prof; prof = prof->next) {
567 if ((prof->events & MONO_PROFILE_TRANSITIONS) && prof->man_unman_transition)
568 prof->man_unman_transition (prof->profiler, method, result);
573 * mono_profiler_allocation:
576 mono_profiler_allocation (MonoObject *obj)
579 for (prof = prof_list; prof; prof = prof->next) {
580 if ((prof->events & MONO_PROFILE_ALLOCATIONS) && prof->allocation_cb)
581 prof->allocation_cb (prof->profiler, obj, obj->vtable->klass);
586 mono_profiler_monitor_event (MonoObject *obj, MonoProfilerMonitorEvent event) {
588 for (prof = prof_list; prof; prof = prof->next) {
589 if ((prof->events & MONO_PROFILE_MONITOR_EVENTS) && prof->monitor_event_cb)
590 prof->monitor_event_cb (prof->profiler, obj, event);
595 * mono_profiler_stat_hit:
598 mono_profiler_stat_hit (guchar *ip, void *context)
601 for (prof = prof_list; prof; prof = prof->next) {
602 if ((prof->events & MONO_PROFILE_STATISTICAL) && prof->statistical_cb)
603 prof->statistical_cb (prof->profiler, ip, context);
608 mono_profiler_stat_call_chain (int call_chain_depth, guchar **ips, void *context)
611 for (prof = prof_list; prof; prof = prof->next) {
612 if ((prof->events & MONO_PROFILE_STATISTICAL) && prof->statistical_call_chain_cb)
613 prof->statistical_call_chain_cb (prof->profiler, call_chain_depth, ips, context);
618 mono_profiler_exception_thrown (MonoObject *exception)
621 for (prof = prof_list; prof; prof = prof->next) {
622 if ((prof->events & MONO_PROFILE_EXCEPTIONS) && prof->exception_throw_cb)
623 prof->exception_throw_cb (prof->profiler, exception);
628 mono_profiler_exception_method_leave (MonoMethod *method)
631 for (prof = prof_list; prof; prof = prof->next) {
632 if ((prof->events & MONO_PROFILE_EXCEPTIONS) && prof->exception_method_leave_cb)
633 prof->exception_method_leave_cb (prof->profiler, method);
638 mono_profiler_exception_clause_handler (MonoMethod *method, int clause_type, int clause_num)
641 for (prof = prof_list; prof; prof = prof->next) {
642 if ((prof->events & MONO_PROFILE_EXCEPTIONS) && prof->exception_clause_cb)
643 prof->exception_clause_cb (prof->profiler, method, clause_type, clause_num);
648 * mono_profiler_thread_start:
651 mono_profiler_thread_start (gsize tid)
654 for (prof = prof_list; prof; prof = prof->next) {
655 if ((prof->events & MONO_PROFILE_THREADS) && prof->thread_start)
656 prof->thread_start (prof->profiler, tid);
661 * mono_profiler_thread_end:
664 mono_profiler_thread_end (gsize tid)
667 for (prof = prof_list; prof; prof = prof->next) {
668 if ((prof->events & MONO_PROFILE_THREADS) && prof->thread_end)
669 prof->thread_end (prof->profiler, tid);
674 mono_profiler_thread_name (gsize tid, const char *name)
677 for (prof = prof_list; prof; prof = prof->next) {
678 if ((prof->events & MONO_PROFILE_THREADS) && prof->thread_name)
679 prof->thread_name (prof->profiler, tid, name);
684 * mono_profiler_assembly_event:
687 mono_profiler_assembly_event (MonoAssembly *assembly, int code)
690 for (prof = prof_list; prof; prof = prof->next) {
691 if (!(prof->events & MONO_PROFILE_ASSEMBLY_EVENTS))
695 case MONO_PROFILE_START_LOAD:
696 if (prof->assembly_start_load)
697 prof->assembly_start_load (prof->profiler, assembly);
699 case MONO_PROFILE_START_UNLOAD:
700 if (prof->assembly_start_unload)
701 prof->assembly_start_unload (prof->profiler, assembly);
703 case MONO_PROFILE_END_UNLOAD:
704 if (prof->assembly_end_unload)
705 prof->assembly_end_unload (prof->profiler, assembly);
708 g_assert_not_reached ();
714 * mono_profiler_assembly_loaded:
717 mono_profiler_assembly_loaded (MonoAssembly *assembly, int result)
720 for (prof = prof_list; prof; prof = prof->next) {
721 if ((prof->events & MONO_PROFILE_ASSEMBLY_EVENTS) && prof->assembly_end_load)
722 prof->assembly_end_load (prof->profiler, assembly, result);
726 void mono_profiler_iomap (char *report, const char *pathname, const char *new_pathname)
729 for (prof = prof_list; prof; prof = prof->next) {
730 if ((prof->events & MONO_PROFILE_IOMAP_EVENTS) && prof->iomap_cb)
731 prof->iomap_cb (prof->profiler, report, pathname, new_pathname);
736 * mono_profiler_module_event:
739 mono_profiler_module_event (MonoImage *module, int code)
742 for (prof = prof_list; prof; prof = prof->next) {
743 if (!(prof->events & MONO_PROFILE_MODULE_EVENTS))
747 case MONO_PROFILE_START_LOAD:
748 if (prof->module_start_load)
749 prof->module_start_load (prof->profiler, module);
751 case MONO_PROFILE_START_UNLOAD:
752 if (prof->module_start_unload)
753 prof->module_start_unload (prof->profiler, module);
755 case MONO_PROFILE_END_UNLOAD:
756 if (prof->module_end_unload)
757 prof->module_end_unload (prof->profiler, module);
760 g_assert_not_reached ();
766 * mono_profiler_module_loaded:
769 mono_profiler_module_loaded (MonoImage *module, int result)
772 for (prof = prof_list; prof; prof = prof->next) {
773 if ((prof->events & MONO_PROFILE_MODULE_EVENTS) && prof->module_end_load)
774 prof->module_end_load (prof->profiler, module, result);
779 * mono_profiler_class_event:
782 mono_profiler_class_event (MonoClass *klass, int code)
785 for (prof = prof_list; prof; prof = prof->next) {
786 if (!(prof->events & MONO_PROFILE_CLASS_EVENTS))
790 case MONO_PROFILE_START_LOAD:
791 if (prof->class_start_load)
792 prof->class_start_load (prof->profiler, klass);
794 case MONO_PROFILE_START_UNLOAD:
795 if (prof->class_start_unload)
796 prof->class_start_unload (prof->profiler, klass);
798 case MONO_PROFILE_END_UNLOAD:
799 if (prof->class_end_unload)
800 prof->class_end_unload (prof->profiler, klass);
803 g_assert_not_reached ();
809 * mono_profiler_class_loaded:
812 mono_profiler_class_loaded (MonoClass *klass, int result)
815 for (prof = prof_list; prof; prof = prof->next) {
816 if ((prof->events & MONO_PROFILE_CLASS_EVENTS) && prof->class_end_load)
817 prof->class_end_load (prof->profiler, klass, result);
822 * mono_profiler_appdomain_event:
825 mono_profiler_appdomain_event (MonoDomain *domain, int code)
828 for (prof = prof_list; prof; prof = prof->next) {
829 if (!(prof->events & MONO_PROFILE_APPDOMAIN_EVENTS))
833 case MONO_PROFILE_START_LOAD:
834 if (prof->domain_start_load)
835 prof->domain_start_load (prof->profiler, domain);
837 case MONO_PROFILE_START_UNLOAD:
838 if (prof->domain_start_unload)
839 prof->domain_start_unload (prof->profiler, domain);
841 case MONO_PROFILE_END_UNLOAD:
842 if (prof->domain_end_unload)
843 prof->domain_end_unload (prof->profiler, domain);
846 g_assert_not_reached ();
852 * mono_profiler_appdomain_loaded:
855 mono_profiler_appdomain_loaded (MonoDomain *domain, int result)
858 for (prof = prof_list; prof; prof = prof->next) {
859 if ((prof->events & MONO_PROFILE_APPDOMAIN_EVENTS) && prof->domain_end_load)
860 prof->domain_end_load (prof->profiler, domain, result);
865 mono_profiler_appdomain_name (MonoDomain *domain, const char *name)
867 for (ProfilerDesc *prof = prof_list; prof; prof = prof->next)
868 if ((prof->events & MONO_PROFILE_APPDOMAIN_EVENTS) && prof->domain_name)
869 prof->domain_name (prof->profiler, domain, name);
873 mono_profiler_context_loaded (MonoAppContext *context)
875 for (ProfilerDesc *prof = prof_list; prof; prof = prof->next)
876 if ((prof->events & MONO_PROFILE_CONTEXT_EVENTS) && prof->context_load)
877 prof->context_load (prof->profiler, context);
881 mono_profiler_context_unloaded (MonoAppContext *context)
883 for (ProfilerDesc *prof = prof_list; prof; prof = prof->next)
884 if ((prof->events & MONO_PROFILE_CONTEXT_EVENTS) && prof->context_unload)
885 prof->context_unload (prof->profiler, context);
889 * mono_profiler_shutdown:
892 mono_profiler_shutdown (void)
895 for (prof = prof_list; prof; prof = prof->next) {
896 if (prof->shutdown_callback)
897 prof->shutdown_callback (prof->profiler);
900 mono_profiler_set_events ((MonoProfileFlags)0);
904 * mono_profiler_gc_heap_resize:
907 mono_profiler_gc_heap_resize (gint64 new_size)
910 for (prof = prof_list; prof; prof = prof->next) {
911 if ((prof->events & MONO_PROFILE_GC) && prof->gc_heap_resize)
912 prof->gc_heap_resize (prof->profiler, new_size);
917 * mono_profiler_gc_event:
920 mono_profiler_gc_event (MonoGCEvent event, int generation)
923 for (prof = prof_list; prof; prof = prof->next) {
924 if ((prof->events & MONO_PROFILE_GC) && prof->gc_event)
925 prof->gc_event (prof->profiler, event, generation);
930 mono_profiler_gc_moves (void **objects, int num)
933 for (prof = prof_list; prof; prof = prof->next) {
934 if ((prof->events & MONO_PROFILE_GC_MOVES) && prof->gc_moves)
935 prof->gc_moves (prof->profiler, objects, num);
940 mono_profiler_gc_handle (int op, int type, uintptr_t handle, MonoObject *obj)
943 for (prof = prof_list; prof; prof = prof->next) {
944 if ((prof->events & MONO_PROFILE_GC_ROOTS) && prof->gc_handle)
945 prof->gc_handle (prof->profiler, op, type, handle, obj);
950 mono_profiler_gc_roots (int num, void **objects, int *root_types, uintptr_t *extra_info)
953 for (prof = prof_list; prof; prof = prof->next) {
954 if ((prof->events & MONO_PROFILE_GC_ROOTS) && prof->gc_roots)
955 prof->gc_roots (prof->profiler, num, objects, root_types, extra_info);
960 * mono_profiler_install_gc:
963 mono_profiler_install_gc (MonoProfileGCFunc callback, MonoProfileGCResizeFunc heap_resize_callback)
967 prof_list->gc_event = callback;
968 prof_list->gc_heap_resize = heap_resize_callback;
972 * mono_profiler_install_gc_moves:
973 * \param callback callback function
975 * Install the \p callback function that the GC will call when moving objects.
976 * The callback receives an array of pointers and the number of elements
977 * in the array. Every even element in the array is the original object location
978 * and the following odd element is the new location of the object in memory.
979 * So the number of elements argument will always be a multiple of 2.
980 * Since this callback happens during the GC, it is a restricted environment:
981 * no locks can be taken and the object pointers can be inspected only once
982 * the GC is finished (of course the original location pointers will not
983 * point to valid objects anymore).
986 mono_profiler_install_gc_moves (MonoProfileGCMoveFunc callback)
990 prof_list->gc_moves = callback;
994 * mono_profiler_install_gc_roots:
995 * \param handle_callback callback function
996 * \param roots_callback callback function
998 * Install the \p handle_callback function that the GC will call when GC
999 * handles are created or destroyed.
1000 * The callback receives an operation, which is either \c MONO_PROFILER_GC_HANDLE_CREATED
1001 * or \c MONO_PROFILER_GC_HANDLE_DESTROYED, the handle type, the handle value and the
1002 * object pointer, if present.
1003 * Install the \p roots_callback function that the GC will call when tracing
1004 * the roots for a collection.
1005 * The callback receives the number of elements and three arrays: an array
1006 * of objects, an array of root types and flags and an array of extra info.
1007 * The size of each array is given by the first argument.
1010 mono_profiler_install_gc_roots (MonoProfileGCHandleFunc handle_callback, MonoProfileGCRootFunc roots_callback)
1014 prof_list->gc_handle = handle_callback;
1015 prof_list->gc_roots = roots_callback;
1019 mono_profiler_gc_finalize_begin (void)
1021 for (ProfilerDesc *prof = prof_list; prof; prof = prof->next)
1022 if ((prof->events & MONO_PROFILE_GC_FINALIZATION) && prof->gc_finalize_begin)
1023 prof->gc_finalize_begin (prof->profiler);
1027 mono_profiler_gc_finalize_object_begin (MonoObject *obj)
1029 for (ProfilerDesc *prof = prof_list; prof; prof = prof->next)
1030 if ((prof->events & MONO_PROFILE_GC_FINALIZATION) && prof->gc_finalize_object_begin)
1031 prof->gc_finalize_object_begin (prof->profiler, obj);
1035 mono_profiler_gc_finalize_object_end (MonoObject *obj)
1037 for (ProfilerDesc *prof = prof_list; prof; prof = prof->next)
1038 if ((prof->events & MONO_PROFILE_GC_FINALIZATION) && prof->gc_finalize_object_end)
1039 prof->gc_finalize_object_end (prof->profiler, obj);
1043 mono_profiler_gc_finalize_end (void)
1045 for (ProfilerDesc *prof = prof_list; prof; prof = prof->next)
1046 if ((prof->events & MONO_PROFILE_GC_FINALIZATION) && prof->gc_finalize_end)
1047 prof->gc_finalize_end (prof->profiler);
1051 mono_profiler_install_gc_finalize (MonoProfileGCFinalizeFunc begin, MonoProfileGCFinalizeObjectFunc begin_obj, MonoProfileGCFinalizeObjectFunc end_obj, MonoProfileGCFinalizeFunc end)
1056 prof_list->gc_finalize_begin = begin;
1057 prof_list->gc_finalize_object_begin = begin_obj;
1058 prof_list->gc_finalize_object_end = end_obj;
1059 prof_list->gc_finalize_end = end;
1063 mono_profiler_install_runtime_initialized (MonoProfileFunc runtime_initialized_callback)
1067 prof_list->runtime_initialized_event = runtime_initialized_callback;
1071 mono_profiler_runtime_initialized (void) {
1073 for (prof = prof_list; prof; prof = prof->next) {
1074 if (prof->runtime_initialized_event)
1075 prof->runtime_initialized_event (prof->profiler);
1080 mono_profiler_install_code_chunk_new (MonoProfilerCodeChunkNew callback) {
1083 prof_list->code_chunk_new = callback;
1086 mono_profiler_code_chunk_new (gpointer chunk, int size) {
1088 for (prof = prof_list; prof; prof = prof->next) {
1089 if (prof->code_chunk_new)
1090 prof->code_chunk_new (prof->profiler, chunk, size);
1095 mono_profiler_install_code_chunk_destroy (MonoProfilerCodeChunkDestroy callback) {
1098 prof_list->code_chunk_destroy = callback;
1101 mono_profiler_code_chunk_destroy (gpointer chunk) {
1103 for (prof = prof_list; prof; prof = prof->next) {
1104 if (prof->code_chunk_destroy)
1105 prof->code_chunk_destroy (prof->profiler, chunk);
1110 mono_profiler_install_code_buffer_new (MonoProfilerCodeBufferNew callback) {
1113 prof_list->code_buffer_new = callback;
1117 mono_profiler_install_iomap (MonoProfileIomapFunc callback)
1121 prof_list->iomap_cb = callback;
1125 mono_profiler_code_buffer_new (gpointer buffer, int size, MonoProfilerCodeBufferType type, gconstpointer data) {
1127 for (prof = prof_list; prof; prof = prof->next) {
1128 if (prof->code_buffer_new)
1129 prof->code_buffer_new (prof->profiler, buffer, size, type, (void*)data);
1133 static GHashTable *coverage_hash = NULL;
1135 MonoProfileCoverageInfo*
1136 mono_profiler_coverage_alloc (MonoMethod *method, int entries)
1138 MonoProfileCoverageInfo *res;
1139 int instrument = FALSE;
1142 for (prof = prof_list; prof; prof = prof->next) {
1143 /* note that we call the filter on all the profilers even if just
1144 * a single one would be enough to instrument a method
1146 if (prof->coverage_filter_cb)
1147 if (prof->coverage_filter_cb (prof->profiler, method))
1153 mono_profiler_coverage_lock ();
1155 coverage_hash = g_hash_table_new (NULL, NULL);
1157 res = (MonoProfileCoverageInfo *)g_malloc0 (sizeof (MonoProfileCoverageInfo) + sizeof (void*) * 2 * entries);
1159 res->entries = entries;
1161 g_hash_table_insert (coverage_hash, method, res);
1162 mono_profiler_coverage_unlock ();
1167 /* safe only when the method antive code has been unloaded */
1169 mono_profiler_coverage_free (MonoMethod *method)
1171 MonoProfileCoverageInfo* info;
1173 mono_profiler_coverage_lock ();
1174 if (!coverage_hash) {
1175 mono_profiler_coverage_unlock ();
1179 info = (MonoProfileCoverageInfo *)g_hash_table_lookup (coverage_hash, method);
1182 g_hash_table_remove (coverage_hash, method);
1184 mono_profiler_coverage_unlock ();
1188 * mono_profiler_coverage_get:
1189 * \param prof The profiler handle, installed with mono_profiler_install
1190 * \param method the method to gather information from.
1191 * \param func A routine that will be called back with the results
1193 * If the \c MONO_PROFILER_INS_COVERAGE flag was active during JIT compilation
1194 * it is possible to obtain coverage information about a give method.
1196 * The function \p func will be invoked repeatedly with instances of the
1197 * \c MonoProfileCoverageEntry structure.
1200 mono_profiler_coverage_get (MonoProfiler *prof, MonoMethod *method, MonoProfileCoverageFunc func)
1203 MonoProfileCoverageInfo* info = NULL;
1206 const unsigned char *start, *end, *cil_code;
1207 MonoMethodHeader *header;
1208 MonoProfileCoverageEntry entry;
1209 MonoDebugMethodInfo *debug_minfo;
1211 mono_profiler_coverage_lock ();
1213 info = (MonoProfileCoverageInfo *)g_hash_table_lookup (coverage_hash, method);
1214 mono_profiler_coverage_unlock ();
1219 header = mono_method_get_header_checked (method, &error);
1220 mono_error_assert_ok (&error);
1221 start = mono_method_header_get_code (header, &code_size, NULL);
1222 debug_minfo = mono_debug_lookup_method (method);
1224 end = start + code_size;
1225 for (i = 0; i < info->entries; ++i) {
1226 cil_code = info->data [i].cil_code;
1227 if (cil_code && cil_code >= start && cil_code < end) {
1229 offset = cil_code - start;
1230 entry.iloffset = offset;
1231 entry.method = method;
1232 entry.counter = info->data [i].count;
1233 entry.line = entry.col = 1;
1234 entry.filename = NULL;
1236 MonoDebugSourceLocation *location;
1238 location = mono_debug_method_lookup_location (debug_minfo, offset);
1240 entry.line = location->row;
1241 entry.col = location->column;
1242 entry.filename = fname = g_strdup (location->source_file);
1243 mono_debug_free_source_location (location);
1247 func (prof, &entry);
1251 mono_metadata_free_mh (header);
1254 typedef void (*ProfilerInitializer) (const char*);
1255 #define INITIALIZER_NAME "mono_profiler_startup"
1259 load_profiler (MonoDl *pmodule, const char *desc, const char *symbol)
1262 ProfilerInitializer func;
1267 if ((err = mono_dl_symbol (pmodule, symbol, (gpointer *) &func))) {
1277 load_embedded_profiler (const char *desc, const char *name)
1281 MonoDl *pmodule = NULL;
1285 * Some profilers (such as ours) may need to call back into the runtime
1286 * from their sampling callback (which is called in async-signal context).
1287 * They need to be able to know that all references back to the runtime
1288 * have been resolved; otherwise, calling runtime functions may result in
1289 * invoking the dynamic linker which is not async-signal-safe. Passing
1290 * MONO_DL_EAGER will ask the dynamic linker to resolve everything upfront.
1292 pmodule = mono_dl_open (NULL, MONO_DL_EAGER, &err);
1294 g_warning ("Could not open main executable (%s)", err);
1299 symbol = g_strdup_printf (INITIALIZER_NAME "_%s", name);
1300 result = load_profiler (pmodule, desc, symbol);
1306 // TODO: Much of the library loading code here is custom. It would be better to merge this with mono-dl
1308 load_profiler_from_directory (const char *directory, const char *libname, const char *desc)
1310 MonoDl *pmodule = NULL;
1315 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT, "Attempting to load profiler %s from %s (desc %s)", libname, directory, desc);
1319 while ((path = mono_dl_build_path (directory, libname, &iter))) {
1320 pmodule = mono_dl_open (path, MONO_DL_EAGER, &err);
1321 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT, "Attempting to load profiler: %s, %ssuccessful, err: %s", path, pmodule?"":"not ", err);
1325 return load_profiler (pmodule, desc, INITIALIZER_NAME);
1332 load_profiler_from_mono_installation (const char *libname, const char *desc)
1335 MonoDl *pmodule = mono_dl_open_runtime_lib (libname, MONO_DL_EAGER, &err);
1336 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT, "Attempting to load profiler from runtime libs: %s, %ssuccessful, err: %s", libname, pmodule?"":"not ", err);
1339 return load_profiler (pmodule, desc, INITIALIZER_NAME);
1344 * mono_profiler_load:
1345 * \param desc arguments to configure the profiler
1347 * Invoke this method to initialize the profiler. This will drive the
1348 * loading of the internal ("default") or any external profilers.
1350 * This routine is invoked by Mono's driver, but must be called manually
1351 * if you embed Mono into your application.
1354 mono_profiler_load (const char *desc)
1357 mono_gc_base_init ();
1359 if (!desc || (strcmp ("default", desc) == 0)) {
1360 desc = "log:report";
1362 /* we keep command-line compat with the old version here */
1363 if (strncmp (desc, "default:", 8) == 0) {
1364 gchar **args, **ptr;
1365 GString *str = g_string_new ("log:report");
1366 args = g_strsplit (desc + 8, ",", -1);
1367 for (ptr = args; ptr && *ptr; ptr++) {
1368 const char *arg = *ptr;
1370 if (!strcmp (arg, "time"))
1371 g_string_append (str, ",calls");
1372 else if (!strcmp (arg, "alloc"))
1373 g_string_append (str, ",alloc");
1374 else if (!strcmp (arg, "stat"))
1375 g_string_append (str, ",sample");
1376 else if (!strcmp (arg, "jit"))
1377 continue; /* accept and do nothing */
1378 else if (strncmp (arg, "file=", 5) == 0) {
1379 g_string_append_printf (str, ",output=%s", arg + 5);
1381 fprintf (stderr, "profiler : Unknown argument '%s'.\n", arg);
1385 desc = cdesc = g_string_free (str, FALSE);
1388 const char* col = strchr (desc, ':');
1391 gboolean res = FALSE;
1394 mname = (char *)g_memdup (desc, col - desc + 1);
1395 mname [col - desc] = 0;
1397 mname = g_strdup (desc);
1399 if (!load_embedded_profiler (desc, mname)) {
1400 libname = g_strdup_printf ("mono-profiler-%s", mname);
1401 res = load_profiler_from_mono_installation (libname, desc);
1402 if (!res && mono_config_get_assemblies_dir ())
1403 res = load_profiler_from_directory (mono_assembly_getrootdir (), libname, desc);
1405 res = load_profiler_from_directory (NULL, libname, desc);
1407 g_warning ("The '%s' profiler wasn't found in the main executable nor could it be loaded from '%s'.", mname, libname);