2 * profiler.c: Profiler interface for Mono
5 * Paolo Molaro (lupus@ximian.com)
6 * Alex Rønne Petersen (alexrp@xamarin.com)
8 * Copyright 2001-2003 Ximian, Inc (http://www.ximian.com)
9 * Copyright 2004-2009 Novell, Inc (http://www.novell.com)
10 * Copyright 2011 Xamarin Inc (http://www.xamarin.com).
11 * Licensed under the MIT license. See LICENSE file in the project root for full license information.
15 #include "mono/metadata/profiler-private.h"
16 #include "mono/metadata/assembly.h"
17 #include "mono/metadata/debug-helpers.h"
18 #include "mono/metadata/mono-debug.h"
19 #include "mono/metadata/debug-mono-symfile.h"
20 #include "mono/metadata/metadata-internals.h"
21 #include "mono/metadata/class-internals.h"
22 #include "mono/metadata/domain-internals.h"
23 #include "mono/metadata/gc-internals.h"
24 #include "mono/metadata/mono-config-dirs.h"
25 #include "mono/utils/mono-dl.h"
26 #include <mono/utils/mono-logger-internals.h>
31 #ifdef HAVE_SYS_TIME_H
34 #ifdef HAVE_BACKTRACE_SYMBOLS
38 typedef struct _ProfilerDesc ProfilerDesc;
39 struct _ProfilerDesc {
41 MonoProfiler *profiler;
42 MonoProfileFlags events;
44 MonoProfileAppDomainFunc domain_start_load;
45 MonoProfileAppDomainResult domain_end_load;
46 MonoProfileAppDomainFunc domain_start_unload;
47 MonoProfileAppDomainFunc domain_end_unload;
48 MonoProfileAppDomainFriendlyNameFunc domain_name;
50 MonoProfileContextFunc context_load;
51 MonoProfileContextFunc context_unload;
53 MonoProfileAssemblyFunc assembly_start_load;
54 MonoProfileAssemblyResult assembly_end_load;
55 MonoProfileAssemblyFunc assembly_start_unload;
56 MonoProfileAssemblyFunc assembly_end_unload;
58 MonoProfileModuleFunc module_start_load;
59 MonoProfileModuleResult module_end_load;
60 MonoProfileModuleFunc module_start_unload;
61 MonoProfileModuleFunc module_end_unload;
63 MonoProfileClassFunc class_start_load;
64 MonoProfileClassResult class_end_load;
65 MonoProfileClassFunc class_start_unload;
66 MonoProfileClassFunc class_end_unload;
68 MonoProfileMethodFunc jit_start;
69 MonoProfileMethodResult jit_end;
70 MonoProfileJitResult jit_end2;
71 MonoProfileMethodFunc method_free;
72 MonoProfileMethodFunc method_start_invoke;
73 MonoProfileMethodFunc method_end_invoke;
74 MonoProfileMethodResult man_unman_transition;
75 MonoProfileAllocFunc allocation_cb;
76 MonoProfileMonitorFunc monitor_event_cb;
77 MonoProfileStatFunc statistical_cb;
78 MonoProfileStatCallChainFunc statistical_call_chain_cb;
79 int statistical_call_chain_depth;
80 MonoProfilerCallChainStrategy statistical_call_chain_strategy;
81 MonoProfileMethodFunc method_enter;
82 MonoProfileMethodFunc method_leave;
84 MonoProfileExceptionFunc exception_throw_cb;
85 MonoProfileMethodFunc exception_method_leave_cb;
86 MonoProfileExceptionClauseFunc exception_clause_cb;
88 MonoProfileIomapFunc iomap_cb;
90 MonoProfileThreadFunc thread_start;
91 MonoProfileThreadFunc thread_end;
92 MonoProfileThreadNameFunc thread_name;
94 MonoProfileCoverageFilterFunc coverage_filter_cb;
96 MonoProfileFunc shutdown_callback;
98 MonoProfileGCFunc gc_event;
99 MonoProfileGCResizeFunc gc_heap_resize;
100 MonoProfileGCMoveFunc gc_moves;
101 MonoProfileGCHandleFunc gc_handle;
102 MonoProfileGCRootFunc gc_roots;
104 MonoProfileGCFinalizeFunc gc_finalize_begin;
105 MonoProfileGCFinalizeObjectFunc gc_finalize_object_begin;
106 MonoProfileGCFinalizeObjectFunc gc_finalize_object_end;
107 MonoProfileGCFinalizeFunc gc_finalize_end;
109 MonoProfileFunc runtime_initialized_event;
111 MonoProfilerCodeChunkNew code_chunk_new;
112 MonoProfilerCodeChunkDestroy code_chunk_destroy;
113 MonoProfilerCodeBufferNew code_buffer_new;
116 static ProfilerDesc *prof_list = NULL;
118 #define mono_profiler_coverage_lock() mono_os_mutex_lock (&profiler_coverage_mutex)
119 #define mono_profiler_coverage_unlock() mono_os_mutex_unlock (&profiler_coverage_mutex)
120 static mono_mutex_t profiler_coverage_mutex;
122 /* this is directly accessible to other mono libs.
123 * It is the ORed value of all the profiler's events.
125 MonoProfileFlags mono_profiler_events;
128 * mono_profiler_install:
129 * @prof: a MonoProfiler structure pointer, or a pointer to a derived structure.
130 * @callback: the function to invoke at shutdown
132 * Use 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 MonoProfiler and put any extra information that they need
135 * 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 * @events: an ORed set of values made up of MONO_PROFILER_ flags
154 * The events descriped in the @events argument is a set of flags
155 * that represent which profiling events must be triggered. For
156 * example if you have registered a set of methods for tracking
157 * JIT compilation start and end with mono_profiler_install_jit_compile,
158 * you will want to pass the MONO_PROFILE_JIT_COMPILATION flag to
161 * You can call mono_profile_set_events more than once and you can
162 * do this at runtime to modify which methods are invoked.
165 mono_profiler_set_events (MonoProfileFlags events)
168 MonoProfileFlags value = (MonoProfileFlags)0;
170 prof_list->events = events;
171 for (prof = prof_list; prof; prof = prof->next)
172 value = (MonoProfileFlags)(value | prof->events);
173 mono_profiler_events = value;
177 * mono_profiler_get_events:
179 * Returns a list of active events that will be intercepted.
182 mono_profiler_get_events (void)
184 return mono_profiler_events;
188 * mono_profiler_install_enter_leave:
189 * @enter: the routine to be called on each method entry
190 * @fleave: the routine to be called each time a method returns
192 * Use this routine to install routines that will be called everytime
193 * a method enters and leaves. The routines will receive as an argument
194 * the MonoMethod representing the method that is entering or leaving.
197 mono_profiler_install_enter_leave (MonoProfileMethodFunc enter, MonoProfileMethodFunc fleave)
201 prof_list->method_enter = enter;
202 prof_list->method_leave = fleave;
206 * mono_profiler_install_jit_compile:
207 * @start: the routine to be called when the JIT process starts.
208 * @end: the routine to be called when the JIT process ends.
210 * Use this routine to install routines that will be called when JIT
211 * compilation of a method starts and completes.
214 mono_profiler_install_jit_compile (MonoProfileMethodFunc start, MonoProfileMethodResult end)
218 prof_list->jit_start = start;
219 prof_list->jit_end = end;
223 mono_profiler_install_jit_end (MonoProfileJitResult end)
227 prof_list->jit_end2 = end;
231 mono_profiler_install_method_free (MonoProfileMethodFunc callback)
235 prof_list->method_free = callback;
239 mono_profiler_install_method_invoke (MonoProfileMethodFunc start, MonoProfileMethodFunc end)
243 prof_list->method_start_invoke = start;
244 prof_list->method_end_invoke = end;
248 mono_profiler_install_thread (MonoProfileThreadFunc start, MonoProfileThreadFunc end)
252 prof_list->thread_start = start;
253 prof_list->thread_end = end;
257 mono_profiler_install_thread_name (MonoProfileThreadNameFunc thread_name_cb)
261 prof_list->thread_name = thread_name_cb;
265 mono_profiler_install_transition (MonoProfileMethodResult callback)
269 prof_list->man_unman_transition = callback;
273 mono_profiler_install_allocation (MonoProfileAllocFunc callback)
277 prof_list->allocation_cb = callback;
281 mono_profiler_install_monitor (MonoProfileMonitorFunc callback)
285 prof_list->monitor_event_cb = callback;
288 static MonoProfileSamplingMode sampling_mode = MONO_PROFILER_STAT_MODE_PROCESS;
289 static int64_t sampling_frequency = 100; // Hz
292 * mono_profiler_set_statistical_mode:
293 * @mode the sampling mode used.
294 * @sample_frequency_is_us the sampling frequency in microseconds.
296 * Set the sampling parameters for the profiler. Sampling mode affects the effective sampling rate as in samples/s you'll witness.
297 * The default sampling mode is process mode, which only reports samples when there's activity in the process.
299 * Sampling frequency should be interpreted as a suggestion that can't always be honored due to how most kernels expose alarms.
301 * Said that, when using statistical sampling, always assume variable rate sampling as all sort of external factors can interfere.
304 mono_profiler_set_statistical_mode (MonoProfileSamplingMode mode, int64_t sampling_frequency_hz)
306 sampling_mode = mode;
307 sampling_frequency = sampling_frequency_hz;
311 mono_profiler_install_statistical (MonoProfileStatFunc callback)
315 prof_list->statistical_cb = callback;
319 mono_profiler_get_sampling_rate (void)
321 return sampling_frequency;
324 MonoProfileSamplingMode
325 mono_profiler_get_sampling_mode (void)
327 return sampling_mode;
331 mono_profiler_install_statistical_call_chain (MonoProfileStatCallChainFunc callback, int call_chain_depth, MonoProfilerCallChainStrategy call_chain_strategy) {
334 if (call_chain_depth > MONO_PROFILER_MAX_STAT_CALL_CHAIN_DEPTH) {
335 call_chain_depth = MONO_PROFILER_MAX_STAT_CALL_CHAIN_DEPTH;
337 if ((call_chain_strategy >= MONO_PROFILER_CALL_CHAIN_INVALID) || (call_chain_strategy < MONO_PROFILER_CALL_CHAIN_NONE)) {
338 call_chain_strategy = MONO_PROFILER_CALL_CHAIN_NONE;
340 prof_list->statistical_call_chain_cb = callback;
341 prof_list->statistical_call_chain_depth = call_chain_depth;
342 prof_list->statistical_call_chain_strategy = call_chain_strategy;
346 mono_profiler_stat_get_call_chain_depth (void) {
347 if (prof_list && prof_list->statistical_call_chain_cb != NULL) {
348 return prof_list->statistical_call_chain_depth;
354 MonoProfilerCallChainStrategy
355 mono_profiler_stat_get_call_chain_strategy (void) {
356 if (prof_list && prof_list->statistical_call_chain_cb != NULL) {
357 return prof_list->statistical_call_chain_strategy;
359 return MONO_PROFILER_CALL_CHAIN_NONE;
363 void mono_profiler_install_exception (MonoProfileExceptionFunc throw_callback, MonoProfileMethodFunc exc_method_leave, MonoProfileExceptionClauseFunc clause_callback)
367 prof_list->exception_throw_cb = throw_callback;
368 prof_list->exception_method_leave_cb = exc_method_leave;
369 prof_list->exception_clause_cb = clause_callback;
373 mono_profiler_install_coverage_filter (MonoProfileCoverageFilterFunc callback)
377 prof_list->coverage_filter_cb = callback;
381 mono_profiler_install_appdomain (MonoProfileAppDomainFunc start_load, MonoProfileAppDomainResult end_load,
382 MonoProfileAppDomainFunc start_unload, MonoProfileAppDomainFunc end_unload)
387 prof_list->domain_start_load = start_load;
388 prof_list->domain_end_load = end_load;
389 prof_list->domain_start_unload = start_unload;
390 prof_list->domain_end_unload = end_unload;
394 mono_profiler_install_appdomain_name (MonoProfileAppDomainFriendlyNameFunc domain_name_cb)
399 prof_list->domain_name = domain_name_cb;
403 mono_profiler_install_context (MonoProfileContextFunc load, MonoProfileContextFunc unload)
408 prof_list->context_load = load;
409 prof_list->context_unload = unload;
413 mono_profiler_install_assembly (MonoProfileAssemblyFunc start_load, MonoProfileAssemblyResult end_load,
414 MonoProfileAssemblyFunc start_unload, MonoProfileAssemblyFunc end_unload)
418 prof_list->assembly_start_load = start_load;
419 prof_list->assembly_end_load = end_load;
420 prof_list->assembly_start_unload = start_unload;
421 prof_list->assembly_end_unload = end_unload;
425 mono_profiler_install_module (MonoProfileModuleFunc start_load, MonoProfileModuleResult end_load,
426 MonoProfileModuleFunc start_unload, MonoProfileModuleFunc end_unload)
430 prof_list->module_start_load = start_load;
431 prof_list->module_end_load = end_load;
432 prof_list->module_start_unload = start_unload;
433 prof_list->module_end_unload = end_unload;
437 mono_profiler_install_class (MonoProfileClassFunc start_load, MonoProfileClassResult end_load,
438 MonoProfileClassFunc start_unload, MonoProfileClassFunc end_unload)
442 prof_list->class_start_load = start_load;
443 prof_list->class_end_load = end_load;
444 prof_list->class_start_unload = start_unload;
445 prof_list->class_end_unload = end_unload;
449 mono_profiler_method_enter (MonoMethod *method)
452 for (prof = prof_list; prof; prof = prof->next) {
453 if ((prof->events & MONO_PROFILE_ENTER_LEAVE) && prof->method_enter)
454 prof->method_enter (prof->profiler, method);
459 mono_profiler_method_leave (MonoMethod *method)
462 for (prof = prof_list; prof; prof = prof->next) {
463 if ((prof->events & MONO_PROFILE_ENTER_LEAVE) && prof->method_leave)
464 prof->method_leave (prof->profiler, method);
469 mono_profiler_method_jit (MonoMethod *method)
472 for (prof = prof_list; prof; prof = prof->next) {
473 if ((prof->events & MONO_PROFILE_JIT_COMPILATION) && prof->jit_start)
474 prof->jit_start (prof->profiler, method);
479 mono_profiler_method_end_jit (MonoMethod *method, MonoJitInfo* jinfo, int result)
482 for (prof = prof_list; prof; prof = prof->next) {
483 if ((prof->events & MONO_PROFILE_JIT_COMPILATION)) {
485 prof->jit_end (prof->profiler, method, result);
487 prof->jit_end2 (prof->profiler, method, jinfo, result);
493 mono_profiler_method_free (MonoMethod *method)
496 for (prof = prof_list; prof; prof = prof->next) {
497 if ((prof->events & MONO_PROFILE_METHOD_EVENTS) && prof->method_free)
498 prof->method_free (prof->profiler, method);
503 mono_profiler_method_start_invoke (MonoMethod *method)
506 for (prof = prof_list; prof; prof = prof->next) {
507 if ((prof->events & MONO_PROFILE_METHOD_EVENTS) && prof->method_start_invoke)
508 prof->method_start_invoke (prof->profiler, method);
513 mono_profiler_method_end_invoke (MonoMethod *method)
516 for (prof = prof_list; prof; prof = prof->next) {
517 if ((prof->events & MONO_PROFILE_METHOD_EVENTS) && prof->method_end_invoke)
518 prof->method_end_invoke (prof->profiler, method);
523 mono_profiler_code_transition (MonoMethod *method, int result)
526 for (prof = prof_list; prof; prof = prof->next) {
527 if ((prof->events & MONO_PROFILE_TRANSITIONS) && prof->man_unman_transition)
528 prof->man_unman_transition (prof->profiler, method, result);
533 mono_profiler_allocation (MonoObject *obj)
536 for (prof = prof_list; prof; prof = prof->next) {
537 if ((prof->events & MONO_PROFILE_ALLOCATIONS) && prof->allocation_cb)
538 prof->allocation_cb (prof->profiler, obj, obj->vtable->klass);
543 mono_profiler_monitor_event (MonoObject *obj, MonoProfilerMonitorEvent event) {
545 for (prof = prof_list; prof; prof = prof->next) {
546 if ((prof->events & MONO_PROFILE_MONITOR_EVENTS) && prof->monitor_event_cb)
547 prof->monitor_event_cb (prof->profiler, obj, event);
552 mono_profiler_stat_hit (guchar *ip, void *context)
555 for (prof = prof_list; prof; prof = prof->next) {
556 if ((prof->events & MONO_PROFILE_STATISTICAL) && prof->statistical_cb)
557 prof->statistical_cb (prof->profiler, ip, context);
562 mono_profiler_stat_call_chain (int call_chain_depth, guchar **ips, void *context)
565 for (prof = prof_list; prof; prof = prof->next) {
566 if ((prof->events & MONO_PROFILE_STATISTICAL) && prof->statistical_call_chain_cb)
567 prof->statistical_call_chain_cb (prof->profiler, call_chain_depth, ips, context);
572 mono_profiler_exception_thrown (MonoObject *exception)
575 for (prof = prof_list; prof; prof = prof->next) {
576 if ((prof->events & MONO_PROFILE_EXCEPTIONS) && prof->exception_throw_cb)
577 prof->exception_throw_cb (prof->profiler, exception);
582 mono_profiler_exception_method_leave (MonoMethod *method)
585 for (prof = prof_list; prof; prof = prof->next) {
586 if ((prof->events & MONO_PROFILE_EXCEPTIONS) && prof->exception_method_leave_cb)
587 prof->exception_method_leave_cb (prof->profiler, method);
592 mono_profiler_exception_clause_handler (MonoMethod *method, int clause_type, int clause_num)
595 for (prof = prof_list; prof; prof = prof->next) {
596 if ((prof->events & MONO_PROFILE_EXCEPTIONS) && prof->exception_clause_cb)
597 prof->exception_clause_cb (prof->profiler, method, clause_type, clause_num);
602 mono_profiler_thread_start (gsize tid)
605 for (prof = prof_list; prof; prof = prof->next) {
606 if ((prof->events & MONO_PROFILE_THREADS) && prof->thread_start)
607 prof->thread_start (prof->profiler, tid);
612 mono_profiler_thread_end (gsize tid)
615 for (prof = prof_list; prof; prof = prof->next) {
616 if ((prof->events & MONO_PROFILE_THREADS) && prof->thread_end)
617 prof->thread_end (prof->profiler, tid);
622 mono_profiler_thread_name (gsize tid, const char *name)
625 for (prof = prof_list; prof; prof = prof->next) {
626 if ((prof->events & MONO_PROFILE_THREADS) && prof->thread_name)
627 prof->thread_name (prof->profiler, tid, name);
632 mono_profiler_assembly_event (MonoAssembly *assembly, int code)
635 for (prof = prof_list; prof; prof = prof->next) {
636 if (!(prof->events & MONO_PROFILE_ASSEMBLY_EVENTS))
640 case MONO_PROFILE_START_LOAD:
641 if (prof->assembly_start_load)
642 prof->assembly_start_load (prof->profiler, assembly);
644 case MONO_PROFILE_START_UNLOAD:
645 if (prof->assembly_start_unload)
646 prof->assembly_start_unload (prof->profiler, assembly);
648 case MONO_PROFILE_END_UNLOAD:
649 if (prof->assembly_end_unload)
650 prof->assembly_end_unload (prof->profiler, assembly);
653 g_assert_not_reached ();
659 mono_profiler_assembly_loaded (MonoAssembly *assembly, int result)
662 for (prof = prof_list; prof; prof = prof->next) {
663 if ((prof->events & MONO_PROFILE_ASSEMBLY_EVENTS) && prof->assembly_end_load)
664 prof->assembly_end_load (prof->profiler, assembly, result);
668 void mono_profiler_iomap (char *report, const char *pathname, const char *new_pathname)
671 for (prof = prof_list; prof; prof = prof->next) {
672 if ((prof->events & MONO_PROFILE_IOMAP_EVENTS) && prof->iomap_cb)
673 prof->iomap_cb (prof->profiler, report, pathname, new_pathname);
678 mono_profiler_module_event (MonoImage *module, int code)
681 for (prof = prof_list; prof; prof = prof->next) {
682 if (!(prof->events & MONO_PROFILE_MODULE_EVENTS))
686 case MONO_PROFILE_START_LOAD:
687 if (prof->module_start_load)
688 prof->module_start_load (prof->profiler, module);
690 case MONO_PROFILE_START_UNLOAD:
691 if (prof->module_start_unload)
692 prof->module_start_unload (prof->profiler, module);
694 case MONO_PROFILE_END_UNLOAD:
695 if (prof->module_end_unload)
696 prof->module_end_unload (prof->profiler, module);
699 g_assert_not_reached ();
705 mono_profiler_module_loaded (MonoImage *module, int result)
708 for (prof = prof_list; prof; prof = prof->next) {
709 if ((prof->events & MONO_PROFILE_MODULE_EVENTS) && prof->module_end_load)
710 prof->module_end_load (prof->profiler, module, result);
715 mono_profiler_class_event (MonoClass *klass, int code)
718 for (prof = prof_list; prof; prof = prof->next) {
719 if (!(prof->events & MONO_PROFILE_CLASS_EVENTS))
723 case MONO_PROFILE_START_LOAD:
724 if (prof->class_start_load)
725 prof->class_start_load (prof->profiler, klass);
727 case MONO_PROFILE_START_UNLOAD:
728 if (prof->class_start_unload)
729 prof->class_start_unload (prof->profiler, klass);
731 case MONO_PROFILE_END_UNLOAD:
732 if (prof->class_end_unload)
733 prof->class_end_unload (prof->profiler, klass);
736 g_assert_not_reached ();
742 mono_profiler_class_loaded (MonoClass *klass, int result)
745 for (prof = prof_list; prof; prof = prof->next) {
746 if ((prof->events & MONO_PROFILE_CLASS_EVENTS) && prof->class_end_load)
747 prof->class_end_load (prof->profiler, klass, result);
752 mono_profiler_appdomain_event (MonoDomain *domain, int code)
755 for (prof = prof_list; prof; prof = prof->next) {
756 if (!(prof->events & MONO_PROFILE_APPDOMAIN_EVENTS))
760 case MONO_PROFILE_START_LOAD:
761 if (prof->domain_start_load)
762 prof->domain_start_load (prof->profiler, domain);
764 case MONO_PROFILE_START_UNLOAD:
765 if (prof->domain_start_unload)
766 prof->domain_start_unload (prof->profiler, domain);
768 case MONO_PROFILE_END_UNLOAD:
769 if (prof->domain_end_unload)
770 prof->domain_end_unload (prof->profiler, domain);
773 g_assert_not_reached ();
779 mono_profiler_appdomain_loaded (MonoDomain *domain, int result)
782 for (prof = prof_list; prof; prof = prof->next) {
783 if ((prof->events & MONO_PROFILE_APPDOMAIN_EVENTS) && prof->domain_end_load)
784 prof->domain_end_load (prof->profiler, domain, result);
789 mono_profiler_appdomain_name (MonoDomain *domain, const char *name)
791 for (ProfilerDesc *prof = prof_list; prof; prof = prof->next)
792 if ((prof->events & MONO_PROFILE_APPDOMAIN_EVENTS) && prof->domain_name)
793 prof->domain_name (prof->profiler, domain, name);
797 mono_profiler_context_loaded (MonoAppContext *context)
799 for (ProfilerDesc *prof = prof_list; prof; prof = prof->next)
800 if ((prof->events & MONO_PROFILE_CONTEXT_EVENTS) && prof->context_load)
801 prof->context_load (prof->profiler, context);
805 mono_profiler_context_unloaded (MonoAppContext *context)
807 for (ProfilerDesc *prof = prof_list; prof; prof = prof->next)
808 if ((prof->events & MONO_PROFILE_CONTEXT_EVENTS) && prof->context_unload)
809 prof->context_unload (prof->profiler, context);
813 mono_profiler_shutdown (void)
816 for (prof = prof_list; prof; prof = prof->next) {
817 if (prof->shutdown_callback)
818 prof->shutdown_callback (prof->profiler);
821 mono_profiler_set_events ((MonoProfileFlags)0);
825 mono_profiler_gc_heap_resize (gint64 new_size)
828 for (prof = prof_list; prof; prof = prof->next) {
829 if ((prof->events & MONO_PROFILE_GC) && prof->gc_heap_resize)
830 prof->gc_heap_resize (prof->profiler, new_size);
835 mono_profiler_gc_event (MonoGCEvent event, int generation)
838 for (prof = prof_list; prof; prof = prof->next) {
839 if ((prof->events & MONO_PROFILE_GC) && prof->gc_event)
840 prof->gc_event (prof->profiler, event, generation);
845 mono_profiler_gc_moves (void **objects, int num)
848 for (prof = prof_list; prof; prof = prof->next) {
849 if ((prof->events & MONO_PROFILE_GC_MOVES) && prof->gc_moves)
850 prof->gc_moves (prof->profiler, objects, num);
855 mono_profiler_gc_handle (int op, int type, uintptr_t handle, MonoObject *obj)
858 for (prof = prof_list; prof; prof = prof->next) {
859 if ((prof->events & MONO_PROFILE_GC_ROOTS) && prof->gc_handle)
860 prof->gc_handle (prof->profiler, op, type, handle, obj);
865 mono_profiler_gc_roots (int num, void **objects, int *root_types, uintptr_t *extra_info)
868 for (prof = prof_list; prof; prof = prof->next) {
869 if ((prof->events & MONO_PROFILE_GC_ROOTS) && prof->gc_roots)
870 prof->gc_roots (prof->profiler, num, objects, root_types, extra_info);
875 mono_profiler_install_gc (MonoProfileGCFunc callback, MonoProfileGCResizeFunc heap_resize_callback)
879 prof_list->gc_event = callback;
880 prof_list->gc_heap_resize = heap_resize_callback;
884 * mono_profiler_install_gc_moves:
885 * @callback: callback function
887 * Install the @callback function that the GC will call when moving objects.
888 * The callback receives an array of pointers and the number of elements
889 * in the array. Every even element in the array is the original object location
890 * and the following odd element is the new location of the object in memory.
891 * So the number of elements argument will always be a multiple of 2.
892 * Since this callback happens during the GC, it is a restricted environment:
893 * no locks can be taken and the object pointers can be inspected only once
894 * the GC is finished (of course the original location pointers will not
895 * point to valid objects anymore).
898 mono_profiler_install_gc_moves (MonoProfileGCMoveFunc callback)
902 prof_list->gc_moves = callback;
906 * mono_profiler_install_gc_roots:
907 * @handle_callback: callback function
908 * @roots_callback: callback function
910 * Install the @handle_callback function that the GC will call when GC
911 * handles are created or destroyed.
912 * The callback receives an operation, which is either #MONO_PROFILER_GC_HANDLE_CREATED
913 * or #MONO_PROFILER_GC_HANDLE_DESTROYED, the handle type, the handle value and the
914 * object pointer, if present.
915 * Install the @roots_callback function that the GC will call when tracing
916 * the roots for a collection.
917 * The callback receives the number of elements and three arrays: an array
918 * of objects, an array of root types and flags and an array of extra info.
919 * The size of each array is given by the first argument.
922 mono_profiler_install_gc_roots (MonoProfileGCHandleFunc handle_callback, MonoProfileGCRootFunc roots_callback)
926 prof_list->gc_handle = handle_callback;
927 prof_list->gc_roots = roots_callback;
931 mono_profiler_gc_finalize_begin (void)
933 for (ProfilerDesc *prof = prof_list; prof; prof = prof->next)
934 if ((prof->events & MONO_PROFILE_GC_FINALIZATION) && prof->gc_finalize_begin)
935 prof->gc_finalize_begin (prof->profiler);
939 mono_profiler_gc_finalize_object_begin (MonoObject *obj)
941 for (ProfilerDesc *prof = prof_list; prof; prof = prof->next)
942 if ((prof->events & MONO_PROFILE_GC_FINALIZATION) && prof->gc_finalize_object_begin)
943 prof->gc_finalize_object_begin (prof->profiler, obj);
947 mono_profiler_gc_finalize_object_end (MonoObject *obj)
949 for (ProfilerDesc *prof = prof_list; prof; prof = prof->next)
950 if ((prof->events & MONO_PROFILE_GC_FINALIZATION) && prof->gc_finalize_object_end)
951 prof->gc_finalize_object_end (prof->profiler, obj);
955 mono_profiler_gc_finalize_end (void)
957 for (ProfilerDesc *prof = prof_list; prof; prof = prof->next)
958 if ((prof->events & MONO_PROFILE_GC_FINALIZATION) && prof->gc_finalize_end)
959 prof->gc_finalize_end (prof->profiler);
963 mono_profiler_install_gc_finalize (MonoProfileGCFinalizeFunc begin, MonoProfileGCFinalizeObjectFunc begin_obj, MonoProfileGCFinalizeObjectFunc end_obj, MonoProfileGCFinalizeFunc end)
968 prof_list->gc_finalize_begin = begin;
969 prof_list->gc_finalize_object_begin = begin_obj;
970 prof_list->gc_finalize_object_end = end_obj;
971 prof_list->gc_finalize_end = end;
975 mono_profiler_install_runtime_initialized (MonoProfileFunc runtime_initialized_callback)
979 prof_list->runtime_initialized_event = runtime_initialized_callback;
983 mono_profiler_runtime_initialized (void) {
985 for (prof = prof_list; prof; prof = prof->next) {
986 if (prof->runtime_initialized_event)
987 prof->runtime_initialized_event (prof->profiler);
992 mono_profiler_install_code_chunk_new (MonoProfilerCodeChunkNew callback) {
995 prof_list->code_chunk_new = callback;
998 mono_profiler_code_chunk_new (gpointer chunk, int size) {
1000 for (prof = prof_list; prof; prof = prof->next) {
1001 if (prof->code_chunk_new)
1002 prof->code_chunk_new (prof->profiler, chunk, size);
1007 mono_profiler_install_code_chunk_destroy (MonoProfilerCodeChunkDestroy callback) {
1010 prof_list->code_chunk_destroy = callback;
1013 mono_profiler_code_chunk_destroy (gpointer chunk) {
1015 for (prof = prof_list; prof; prof = prof->next) {
1016 if (prof->code_chunk_destroy)
1017 prof->code_chunk_destroy (prof->profiler, chunk);
1022 mono_profiler_install_code_buffer_new (MonoProfilerCodeBufferNew callback) {
1025 prof_list->code_buffer_new = callback;
1029 mono_profiler_install_iomap (MonoProfileIomapFunc callback)
1033 prof_list->iomap_cb = callback;
1037 mono_profiler_code_buffer_new (gpointer buffer, int size, MonoProfilerCodeBufferType type, gconstpointer data) {
1039 for (prof = prof_list; prof; prof = prof->next) {
1040 if (prof->code_buffer_new)
1041 prof->code_buffer_new (prof->profiler, buffer, size, type, (void*)data);
1045 static GHashTable *coverage_hash = NULL;
1047 MonoProfileCoverageInfo*
1048 mono_profiler_coverage_alloc (MonoMethod *method, int entries)
1050 MonoProfileCoverageInfo *res;
1051 int instrument = FALSE;
1054 for (prof = prof_list; prof; prof = prof->next) {
1055 /* note that we call the filter on all the profilers even if just
1056 * a single one would be enough to instrument a method
1058 if (prof->coverage_filter_cb)
1059 if (prof->coverage_filter_cb (prof->profiler, method))
1065 mono_profiler_coverage_lock ();
1067 coverage_hash = g_hash_table_new (NULL, NULL);
1069 res = (MonoProfileCoverageInfo *)g_malloc0 (sizeof (MonoProfileCoverageInfo) + sizeof (void*) * 2 * entries);
1071 res->entries = entries;
1073 g_hash_table_insert (coverage_hash, method, res);
1074 mono_profiler_coverage_unlock ();
1079 /* safe only when the method antive code has been unloaded */
1081 mono_profiler_coverage_free (MonoMethod *method)
1083 MonoProfileCoverageInfo* info;
1085 mono_profiler_coverage_lock ();
1086 if (!coverage_hash) {
1087 mono_profiler_coverage_unlock ();
1091 info = (MonoProfileCoverageInfo *)g_hash_table_lookup (coverage_hash, method);
1094 g_hash_table_remove (coverage_hash, method);
1096 mono_profiler_coverage_unlock ();
1100 * mono_profiler_coverage_get:
1101 * @prof: The profiler handle, installed with mono_profiler_install
1102 * @method: the method to gather information from.
1103 * @func: A routine that will be called back with the results
1105 * If the MONO_PROFILER_INS_COVERAGE flag was active during JIT compilation
1106 * it is posisble to obtain coverage information about a give method.
1108 * The function @func will be invoked repeatedly with instances of the
1109 * MonoProfileCoverageEntry structure.
1112 mono_profiler_coverage_get (MonoProfiler *prof, MonoMethod *method, MonoProfileCoverageFunc func)
1115 MonoProfileCoverageInfo* info = NULL;
1118 const unsigned char *start, *end, *cil_code;
1119 MonoMethodHeader *header;
1120 MonoProfileCoverageEntry entry;
1121 MonoDebugMethodInfo *debug_minfo;
1123 mono_profiler_coverage_lock ();
1125 info = (MonoProfileCoverageInfo *)g_hash_table_lookup (coverage_hash, method);
1126 mono_profiler_coverage_unlock ();
1131 header = mono_method_get_header_checked (method, &error);
1132 mono_error_assert_ok (&error);
1133 start = mono_method_header_get_code (header, &code_size, NULL);
1134 debug_minfo = mono_debug_lookup_method (method);
1136 end = start + code_size;
1137 for (i = 0; i < info->entries; ++i) {
1138 cil_code = info->data [i].cil_code;
1139 if (cil_code && cil_code >= start && cil_code < end) {
1141 offset = cil_code - start;
1142 entry.iloffset = offset;
1143 entry.method = method;
1144 entry.counter = info->data [i].count;
1145 entry.line = entry.col = 1;
1146 entry.filename = NULL;
1148 MonoDebugSourceLocation *location;
1150 location = mono_debug_symfile_lookup_location (debug_minfo, offset);
1152 entry.line = location->row;
1153 entry.col = location->column;
1154 entry.filename = fname = g_strdup (location->source_file);
1155 mono_debug_free_source_location (location);
1159 func (prof, &entry);
1163 mono_metadata_free_mh (header);
1166 typedef void (*ProfilerInitializer) (const char*);
1167 #define INITIALIZER_NAME "mono_profiler_startup"
1171 load_profiler (MonoDl *pmodule, const char *desc, const char *symbol)
1174 ProfilerInitializer func;
1179 if ((err = mono_dl_symbol (pmodule, symbol, (gpointer *) &func))) {
1189 load_embedded_profiler (const char *desc, const char *name)
1193 MonoDl *pmodule = NULL;
1197 * Some profilers (such as ours) may need to call back into the runtime
1198 * from their sampling callback (which is called in async-signal context).
1199 * They need to be able to know that all references back to the runtime
1200 * have been resolved; otherwise, calling runtime functions may result in
1201 * invoking the dynamic linker which is not async-signal-safe. Passing
1202 * MONO_DL_EAGER will ask the dynamic linker to resolve everything upfront.
1204 pmodule = mono_dl_open (NULL, MONO_DL_EAGER, &err);
1206 g_warning ("Could not open main executable (%s)", err);
1211 symbol = g_strdup_printf (INITIALIZER_NAME "_%s", name);
1212 result = load_profiler (pmodule, desc, symbol);
1218 // TODO: Much of the library loading code here is custom. It would be better to merge this with mono-dl
1220 load_profiler_from_directory (const char *directory, const char *libname, const char *desc)
1222 MonoDl *pmodule = NULL;
1227 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT, "Attempting to load profiler %s from %s (desc %s)", libname, directory, desc);
1231 while ((path = mono_dl_build_path (directory, libname, &iter))) {
1232 pmodule = mono_dl_open (path, MONO_DL_EAGER, &err);
1233 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT, "Attempting to load profiler: %s, %ssuccessful, err: %s", path, pmodule?"":"not ", err);
1237 return load_profiler (pmodule, desc, INITIALIZER_NAME);
1244 load_profiler_from_mono_installation (const char *libname, const char *desc)
1247 MonoDl *pmodule = mono_dl_open_runtime_lib (libname, MONO_DL_EAGER, &err);
1248 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT, "Attempting to load profiler from runtime libs: %s, %ssuccessful, err: %s", libname, pmodule?"":"not ", err);
1251 return load_profiler (pmodule, desc, INITIALIZER_NAME);
1256 * mono_profiler_load:
1257 * @desc: arguments to configure the profiler
1259 * Invoke this method to initialize the profiler. This will drive the
1260 * loading of the internal ("default") or any external profilers.
1262 * This routine is invoked by Mono's driver, but must be called manually
1263 * if you embed Mono into your application.
1266 mono_profiler_load (const char *desc)
1269 mono_gc_base_init ();
1271 if (!desc || (strcmp ("default", desc) == 0)) {
1272 desc = "log:report";
1274 /* we keep command-line compat with the old version here */
1275 if (strncmp (desc, "default:", 8) == 0) {
1276 gchar **args, **ptr;
1277 GString *str = g_string_new ("log:report");
1278 args = g_strsplit (desc + 8, ",", -1);
1279 for (ptr = args; ptr && *ptr; ptr++) {
1280 const char *arg = *ptr;
1282 if (!strcmp (arg, "time"))
1283 g_string_append (str, ",calls");
1284 else if (!strcmp (arg, "alloc"))
1285 g_string_append (str, ",alloc");
1286 else if (!strcmp (arg, "stat"))
1287 g_string_append (str, ",sample");
1288 else if (!strcmp (arg, "jit"))
1289 continue; /* accept and do nothing */
1290 else if (strncmp (arg, "file=", 5) == 0) {
1291 g_string_append_printf (str, ",output=%s", arg + 5);
1293 fprintf (stderr, "profiler : Unknown argument '%s'.\n", arg);
1297 desc = cdesc = g_string_free (str, FALSE);
1300 const char* col = strchr (desc, ':');
1303 gboolean res = FALSE;
1306 mname = (char *)g_memdup (desc, col - desc + 1);
1307 mname [col - desc] = 0;
1309 mname = g_strdup (desc);
1311 if (!load_embedded_profiler (desc, mname)) {
1312 libname = g_strdup_printf ("mono-profiler-%s", mname);
1313 res = load_profiler_from_mono_installation (libname, desc);
1314 if (!res && mono_config_get_assemblies_dir ())
1315 res = load_profiler_from_directory (mono_assembly_getrootdir (), libname, desc);
1317 res = load_profiler_from_directory (NULL, libname, desc);
1319 g_warning ("The '%s' profiler wasn't found in the main executable nor could it be loaded from '%s'.", mname, libname);