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/io-layer/io-layer.h"
26 #include "mono/utils/mono-dl.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 MonoProfileFunc runtime_initialized_event;
106 MonoProfilerCodeChunkNew code_chunk_new;
107 MonoProfilerCodeChunkDestroy code_chunk_destroy;
108 MonoProfilerCodeBufferNew code_buffer_new;
111 static ProfilerDesc *prof_list = NULL;
113 #define mono_profiler_coverage_lock() mono_os_mutex_lock (&profiler_coverage_mutex)
114 #define mono_profiler_coverage_unlock() mono_os_mutex_unlock (&profiler_coverage_mutex)
115 static mono_mutex_t profiler_coverage_mutex;
117 /* this is directly accessible to other mono libs.
118 * It is the ORed value of all the profiler's events.
120 MonoProfileFlags mono_profiler_events;
123 * mono_profiler_install:
124 * @prof: a MonoProfiler structure pointer, or a pointer to a derived structure.
125 * @callback: the function to invoke at shutdown
127 * Use mono_profiler_install to activate profiling in the Mono runtime.
128 * Typically developers of new profilers will create a new structure whose
129 * first field is a MonoProfiler and put any extra information that they need
130 * to access from the various profiling callbacks there.
134 mono_profiler_install (MonoProfiler *prof, MonoProfileFunc callback)
136 ProfilerDesc *desc = g_new0 (ProfilerDesc, 1);
138 mono_os_mutex_init_recursive (&profiler_coverage_mutex);
139 desc->profiler = prof;
140 desc->shutdown_callback = callback;
141 desc->next = prof_list;
146 * mono_profiler_set_events:
147 * @events: an ORed set of values made up of MONO_PROFILER_ flags
149 * The events descriped in the @events argument is a set of flags
150 * that represent which profiling events must be triggered. For
151 * example if you have registered a set of methods for tracking
152 * JIT compilation start and end with mono_profiler_install_jit_compile,
153 * you will want to pass the MONO_PROFILE_JIT_COMPILATION flag to
156 * You can call mono_profile_set_events more than once and you can
157 * do this at runtime to modify which methods are invoked.
160 mono_profiler_set_events (MonoProfileFlags events)
163 MonoProfileFlags value = (MonoProfileFlags)0;
165 prof_list->events = events;
166 for (prof = prof_list; prof; prof = prof->next)
167 value = (MonoProfileFlags)(value | prof->events);
168 mono_profiler_events = value;
172 * mono_profiler_get_events:
174 * Returns a list of active events that will be intercepted.
177 mono_profiler_get_events (void)
179 return mono_profiler_events;
183 * mono_profiler_install_enter_leave:
184 * @enter: the routine to be called on each method entry
185 * @fleave: the routine to be called each time a method returns
187 * Use this routine to install routines that will be called everytime
188 * a method enters and leaves. The routines will receive as an argument
189 * the MonoMethod representing the method that is entering or leaving.
192 mono_profiler_install_enter_leave (MonoProfileMethodFunc enter, MonoProfileMethodFunc fleave)
196 prof_list->method_enter = enter;
197 prof_list->method_leave = fleave;
201 * mono_profiler_install_jit_compile:
202 * @start: the routine to be called when the JIT process starts.
203 * @end: the routine to be called when the JIT process ends.
205 * Use this routine to install routines that will be called when JIT
206 * compilation of a method starts and completes.
209 mono_profiler_install_jit_compile (MonoProfileMethodFunc start, MonoProfileMethodResult end)
213 prof_list->jit_start = start;
214 prof_list->jit_end = end;
218 mono_profiler_install_jit_end (MonoProfileJitResult end)
222 prof_list->jit_end2 = end;
226 mono_profiler_install_method_free (MonoProfileMethodFunc callback)
230 prof_list->method_free = callback;
234 mono_profiler_install_method_invoke (MonoProfileMethodFunc start, MonoProfileMethodFunc end)
238 prof_list->method_start_invoke = start;
239 prof_list->method_end_invoke = end;
243 mono_profiler_install_thread (MonoProfileThreadFunc start, MonoProfileThreadFunc end)
247 prof_list->thread_start = start;
248 prof_list->thread_end = end;
252 mono_profiler_install_thread_name (MonoProfileThreadNameFunc thread_name_cb)
256 prof_list->thread_name = thread_name_cb;
260 mono_profiler_install_transition (MonoProfileMethodResult callback)
264 prof_list->man_unman_transition = callback;
268 mono_profiler_install_allocation (MonoProfileAllocFunc callback)
270 mono_gc_enable_alloc_events ();
273 prof_list->allocation_cb = callback;
277 mono_profiler_install_monitor (MonoProfileMonitorFunc callback)
281 prof_list->monitor_event_cb = callback;
284 static MonoProfileSamplingMode sampling_mode = MONO_PROFILER_STAT_MODE_PROCESS;
285 static int64_t sampling_frequency = 1000; //1ms
288 * mono_profiler_set_statistical_mode:
289 * @mode the sampling mode used.
290 * @sample_frequency_is_us the sampling frequency in microseconds.
292 * Set the sampling parameters for the profiler. Sampling mode affects the effective sampling rate as in samples/s you'll witness.
293 * The default sampling mode is process mode, which only reports samples when there's activity in the process.
295 * Sampling frequency should be interpreted as a suggestion that can't always be honored due to how most kernels expose alarms.
297 * Said that, when using statistical sampling, always assume variable rate sampling as all sort of external factors can interfere.
300 mono_profiler_set_statistical_mode (MonoProfileSamplingMode mode, int64_t sampling_frequency_is_us)
302 sampling_mode = mode;
303 sampling_frequency = sampling_frequency_is_us;
307 mono_profiler_install_statistical (MonoProfileStatFunc callback)
311 prof_list->statistical_cb = callback;
315 mono_profiler_get_sampling_rate (void)
317 return sampling_frequency;
320 MonoProfileSamplingMode
321 mono_profiler_get_sampling_mode (void)
323 return sampling_mode;
327 mono_profiler_install_statistical_call_chain (MonoProfileStatCallChainFunc callback, int call_chain_depth, MonoProfilerCallChainStrategy call_chain_strategy) {
330 if (call_chain_depth > MONO_PROFILER_MAX_STAT_CALL_CHAIN_DEPTH) {
331 call_chain_depth = MONO_PROFILER_MAX_STAT_CALL_CHAIN_DEPTH;
333 if ((call_chain_strategy >= MONO_PROFILER_CALL_CHAIN_INVALID) || (call_chain_strategy < MONO_PROFILER_CALL_CHAIN_NONE)) {
334 call_chain_strategy = MONO_PROFILER_CALL_CHAIN_NONE;
336 prof_list->statistical_call_chain_cb = callback;
337 prof_list->statistical_call_chain_depth = call_chain_depth;
338 prof_list->statistical_call_chain_strategy = call_chain_strategy;
342 mono_profiler_stat_get_call_chain_depth (void) {
343 if (prof_list && prof_list->statistical_call_chain_cb != NULL) {
344 return prof_list->statistical_call_chain_depth;
350 MonoProfilerCallChainStrategy
351 mono_profiler_stat_get_call_chain_strategy (void) {
352 if (prof_list && prof_list->statistical_call_chain_cb != NULL) {
353 return prof_list->statistical_call_chain_strategy;
355 return MONO_PROFILER_CALL_CHAIN_NONE;
359 void mono_profiler_install_exception (MonoProfileExceptionFunc throw_callback, MonoProfileMethodFunc exc_method_leave, MonoProfileExceptionClauseFunc clause_callback)
363 prof_list->exception_throw_cb = throw_callback;
364 prof_list->exception_method_leave_cb = exc_method_leave;
365 prof_list->exception_clause_cb = clause_callback;
369 mono_profiler_install_coverage_filter (MonoProfileCoverageFilterFunc callback)
373 prof_list->coverage_filter_cb = callback;
377 mono_profiler_install_appdomain (MonoProfileAppDomainFunc start_load, MonoProfileAppDomainResult end_load,
378 MonoProfileAppDomainFunc start_unload, MonoProfileAppDomainFunc end_unload)
383 prof_list->domain_start_load = start_load;
384 prof_list->domain_end_load = end_load;
385 prof_list->domain_start_unload = start_unload;
386 prof_list->domain_end_unload = end_unload;
390 mono_profiler_install_appdomain_name (MonoProfileAppDomainFriendlyNameFunc domain_name_cb)
395 prof_list->domain_name = domain_name_cb;
399 mono_profiler_install_context (MonoProfileContextFunc load, MonoProfileContextFunc unload)
404 prof_list->context_load = load;
405 prof_list->context_unload = unload;
409 mono_profiler_install_assembly (MonoProfileAssemblyFunc start_load, MonoProfileAssemblyResult end_load,
410 MonoProfileAssemblyFunc start_unload, MonoProfileAssemblyFunc end_unload)
414 prof_list->assembly_start_load = start_load;
415 prof_list->assembly_end_load = end_load;
416 prof_list->assembly_start_unload = start_unload;
417 prof_list->assembly_end_unload = end_unload;
421 mono_profiler_install_module (MonoProfileModuleFunc start_load, MonoProfileModuleResult end_load,
422 MonoProfileModuleFunc start_unload, MonoProfileModuleFunc end_unload)
426 prof_list->module_start_load = start_load;
427 prof_list->module_end_load = end_load;
428 prof_list->module_start_unload = start_unload;
429 prof_list->module_end_unload = end_unload;
433 mono_profiler_install_class (MonoProfileClassFunc start_load, MonoProfileClassResult end_load,
434 MonoProfileClassFunc start_unload, MonoProfileClassFunc end_unload)
438 prof_list->class_start_load = start_load;
439 prof_list->class_end_load = end_load;
440 prof_list->class_start_unload = start_unload;
441 prof_list->class_end_unload = end_unload;
445 mono_profiler_method_enter (MonoMethod *method)
448 for (prof = prof_list; prof; prof = prof->next) {
449 if ((prof->events & MONO_PROFILE_ENTER_LEAVE) && prof->method_enter)
450 prof->method_enter (prof->profiler, method);
455 mono_profiler_method_leave (MonoMethod *method)
458 for (prof = prof_list; prof; prof = prof->next) {
459 if ((prof->events & MONO_PROFILE_ENTER_LEAVE) && prof->method_leave)
460 prof->method_leave (prof->profiler, method);
465 mono_profiler_method_jit (MonoMethod *method)
468 for (prof = prof_list; prof; prof = prof->next) {
469 if ((prof->events & MONO_PROFILE_JIT_COMPILATION) && prof->jit_start)
470 prof->jit_start (prof->profiler, method);
475 mono_profiler_method_end_jit (MonoMethod *method, MonoJitInfo* jinfo, int result)
478 for (prof = prof_list; prof; prof = prof->next) {
479 if ((prof->events & MONO_PROFILE_JIT_COMPILATION)) {
481 prof->jit_end (prof->profiler, method, result);
483 prof->jit_end2 (prof->profiler, method, jinfo, result);
489 mono_profiler_method_free (MonoMethod *method)
492 for (prof = prof_list; prof; prof = prof->next) {
493 if ((prof->events & MONO_PROFILE_METHOD_EVENTS) && prof->method_free)
494 prof->method_free (prof->profiler, method);
499 mono_profiler_method_start_invoke (MonoMethod *method)
502 for (prof = prof_list; prof; prof = prof->next) {
503 if ((prof->events & MONO_PROFILE_METHOD_EVENTS) && prof->method_start_invoke)
504 prof->method_start_invoke (prof->profiler, method);
509 mono_profiler_method_end_invoke (MonoMethod *method)
512 for (prof = prof_list; prof; prof = prof->next) {
513 if ((prof->events & MONO_PROFILE_METHOD_EVENTS) && prof->method_end_invoke)
514 prof->method_end_invoke (prof->profiler, method);
519 mono_profiler_code_transition (MonoMethod *method, int result)
522 for (prof = prof_list; prof; prof = prof->next) {
523 if ((prof->events & MONO_PROFILE_TRANSITIONS) && prof->man_unman_transition)
524 prof->man_unman_transition (prof->profiler, method, result);
529 mono_profiler_allocation (MonoObject *obj)
532 for (prof = prof_list; prof; prof = prof->next) {
533 if ((prof->events & MONO_PROFILE_ALLOCATIONS) && prof->allocation_cb)
534 prof->allocation_cb (prof->profiler, obj, obj->vtable->klass);
539 mono_profiler_monitor_event (MonoObject *obj, MonoProfilerMonitorEvent event) {
541 for (prof = prof_list; prof; prof = prof->next) {
542 if ((prof->events & MONO_PROFILE_MONITOR_EVENTS) && prof->monitor_event_cb)
543 prof->monitor_event_cb (prof->profiler, obj, event);
548 mono_profiler_stat_hit (guchar *ip, void *context)
551 for (prof = prof_list; prof; prof = prof->next) {
552 if ((prof->events & MONO_PROFILE_STATISTICAL) && prof->statistical_cb)
553 prof->statistical_cb (prof->profiler, ip, context);
558 mono_profiler_stat_call_chain (int call_chain_depth, guchar **ips, void *context)
561 for (prof = prof_list; prof; prof = prof->next) {
562 if ((prof->events & MONO_PROFILE_STATISTICAL) && prof->statistical_call_chain_cb)
563 prof->statistical_call_chain_cb (prof->profiler, call_chain_depth, ips, context);
568 mono_profiler_exception_thrown (MonoObject *exception)
571 for (prof = prof_list; prof; prof = prof->next) {
572 if ((prof->events & MONO_PROFILE_EXCEPTIONS) && prof->exception_throw_cb)
573 prof->exception_throw_cb (prof->profiler, exception);
578 mono_profiler_exception_method_leave (MonoMethod *method)
581 for (prof = prof_list; prof; prof = prof->next) {
582 if ((prof->events & MONO_PROFILE_EXCEPTIONS) && prof->exception_method_leave_cb)
583 prof->exception_method_leave_cb (prof->profiler, method);
588 mono_profiler_exception_clause_handler (MonoMethod *method, int clause_type, int clause_num)
591 for (prof = prof_list; prof; prof = prof->next) {
592 if ((prof->events & MONO_PROFILE_EXCEPTIONS) && prof->exception_clause_cb)
593 prof->exception_clause_cb (prof->profiler, method, clause_type, clause_num);
598 mono_profiler_thread_start (gsize tid)
601 for (prof = prof_list; prof; prof = prof->next) {
602 if ((prof->events & MONO_PROFILE_THREADS) && prof->thread_start)
603 prof->thread_start (prof->profiler, tid);
608 mono_profiler_thread_end (gsize tid)
611 for (prof = prof_list; prof; prof = prof->next) {
612 if ((prof->events & MONO_PROFILE_THREADS) && prof->thread_end)
613 prof->thread_end (prof->profiler, tid);
618 mono_profiler_thread_name (gsize tid, const char *name)
621 for (prof = prof_list; prof; prof = prof->next) {
622 if ((prof->events & MONO_PROFILE_THREADS) && prof->thread_name)
623 prof->thread_name (prof->profiler, tid, name);
628 mono_profiler_assembly_event (MonoAssembly *assembly, int code)
631 for (prof = prof_list; prof; prof = prof->next) {
632 if (!(prof->events & MONO_PROFILE_ASSEMBLY_EVENTS))
636 case MONO_PROFILE_START_LOAD:
637 if (prof->assembly_start_load)
638 prof->assembly_start_load (prof->profiler, assembly);
640 case MONO_PROFILE_START_UNLOAD:
641 if (prof->assembly_start_unload)
642 prof->assembly_start_unload (prof->profiler, assembly);
644 case MONO_PROFILE_END_UNLOAD:
645 if (prof->assembly_end_unload)
646 prof->assembly_end_unload (prof->profiler, assembly);
649 g_assert_not_reached ();
655 mono_profiler_assembly_loaded (MonoAssembly *assembly, int result)
658 for (prof = prof_list; prof; prof = prof->next) {
659 if ((prof->events & MONO_PROFILE_ASSEMBLY_EVENTS) && prof->assembly_end_load)
660 prof->assembly_end_load (prof->profiler, assembly, result);
664 void mono_profiler_iomap (char *report, const char *pathname, const char *new_pathname)
667 for (prof = prof_list; prof; prof = prof->next) {
668 if ((prof->events & MONO_PROFILE_IOMAP_EVENTS) && prof->iomap_cb)
669 prof->iomap_cb (prof->profiler, report, pathname, new_pathname);
674 mono_profiler_module_event (MonoImage *module, int code)
677 for (prof = prof_list; prof; prof = prof->next) {
678 if (!(prof->events & MONO_PROFILE_MODULE_EVENTS))
682 case MONO_PROFILE_START_LOAD:
683 if (prof->module_start_load)
684 prof->module_start_load (prof->profiler, module);
686 case MONO_PROFILE_START_UNLOAD:
687 if (prof->module_start_unload)
688 prof->module_start_unload (prof->profiler, module);
690 case MONO_PROFILE_END_UNLOAD:
691 if (prof->module_end_unload)
692 prof->module_end_unload (prof->profiler, module);
695 g_assert_not_reached ();
701 mono_profiler_module_loaded (MonoImage *module, int result)
704 for (prof = prof_list; prof; prof = prof->next) {
705 if ((prof->events & MONO_PROFILE_MODULE_EVENTS) && prof->module_end_load)
706 prof->module_end_load (prof->profiler, module, result);
711 mono_profiler_class_event (MonoClass *klass, int code)
714 for (prof = prof_list; prof; prof = prof->next) {
715 if (!(prof->events & MONO_PROFILE_CLASS_EVENTS))
719 case MONO_PROFILE_START_LOAD:
720 if (prof->class_start_load)
721 prof->class_start_load (prof->profiler, klass);
723 case MONO_PROFILE_START_UNLOAD:
724 if (prof->class_start_unload)
725 prof->class_start_unload (prof->profiler, klass);
727 case MONO_PROFILE_END_UNLOAD:
728 if (prof->class_end_unload)
729 prof->class_end_unload (prof->profiler, klass);
732 g_assert_not_reached ();
738 mono_profiler_class_loaded (MonoClass *klass, int result)
741 for (prof = prof_list; prof; prof = prof->next) {
742 if ((prof->events & MONO_PROFILE_CLASS_EVENTS) && prof->class_end_load)
743 prof->class_end_load (prof->profiler, klass, result);
748 mono_profiler_appdomain_event (MonoDomain *domain, int code)
751 for (prof = prof_list; prof; prof = prof->next) {
752 if (!(prof->events & MONO_PROFILE_APPDOMAIN_EVENTS))
756 case MONO_PROFILE_START_LOAD:
757 if (prof->domain_start_load)
758 prof->domain_start_load (prof->profiler, domain);
760 case MONO_PROFILE_START_UNLOAD:
761 if (prof->domain_start_unload)
762 prof->domain_start_unload (prof->profiler, domain);
764 case MONO_PROFILE_END_UNLOAD:
765 if (prof->domain_end_unload)
766 prof->domain_end_unload (prof->profiler, domain);
769 g_assert_not_reached ();
775 mono_profiler_appdomain_loaded (MonoDomain *domain, int result)
778 for (prof = prof_list; prof; prof = prof->next) {
779 if ((prof->events & MONO_PROFILE_APPDOMAIN_EVENTS) && prof->domain_end_load)
780 prof->domain_end_load (prof->profiler, domain, result);
785 mono_profiler_appdomain_name (MonoDomain *domain, const char *name)
787 for (ProfilerDesc *prof = prof_list; prof; prof = prof->next)
788 if ((prof->events & MONO_PROFILE_APPDOMAIN_EVENTS) && prof->domain_name)
789 prof->domain_name (prof->profiler, domain, name);
793 mono_profiler_context_loaded (MonoAppContext *context)
795 for (ProfilerDesc *prof = prof_list; prof; prof = prof->next)
796 if ((prof->events & MONO_PROFILE_CONTEXT_EVENTS) && prof->context_load)
797 prof->context_load (prof->profiler, context);
801 mono_profiler_context_unloaded (MonoAppContext *context)
803 for (ProfilerDesc *prof = prof_list; prof; prof = prof->next)
804 if ((prof->events & MONO_PROFILE_CONTEXT_EVENTS) && prof->context_unload)
805 prof->context_unload (prof->profiler, context);
809 mono_profiler_shutdown (void)
812 for (prof = prof_list; prof; prof = prof->next) {
813 if (prof->shutdown_callback)
814 prof->shutdown_callback (prof->profiler);
817 mono_profiler_set_events ((MonoProfileFlags)0);
821 mono_profiler_gc_heap_resize (gint64 new_size)
824 for (prof = prof_list; prof; prof = prof->next) {
825 if ((prof->events & MONO_PROFILE_GC) && prof->gc_heap_resize)
826 prof->gc_heap_resize (prof->profiler, new_size);
831 mono_profiler_gc_event (MonoGCEvent event, int generation)
834 for (prof = prof_list; prof; prof = prof->next) {
835 if ((prof->events & MONO_PROFILE_GC) && prof->gc_event)
836 prof->gc_event (prof->profiler, event, generation);
841 mono_profiler_gc_moves (void **objects, int num)
844 for (prof = prof_list; prof; prof = prof->next) {
845 if ((prof->events & MONO_PROFILE_GC_MOVES) && prof->gc_moves)
846 prof->gc_moves (prof->profiler, objects, num);
851 mono_profiler_gc_handle (int op, int type, uintptr_t handle, MonoObject *obj)
854 for (prof = prof_list; prof; prof = prof->next) {
855 if ((prof->events & MONO_PROFILE_GC_ROOTS) && prof->gc_handle)
856 prof->gc_handle (prof->profiler, op, type, handle, obj);
861 mono_profiler_gc_roots (int num, void **objects, int *root_types, uintptr_t *extra_info)
864 for (prof = prof_list; prof; prof = prof->next) {
865 if ((prof->events & MONO_PROFILE_GC_ROOTS) && prof->gc_roots)
866 prof->gc_roots (prof->profiler, num, objects, root_types, extra_info);
871 mono_profiler_install_gc (MonoProfileGCFunc callback, MonoProfileGCResizeFunc heap_resize_callback)
873 mono_gc_enable_events ();
876 prof_list->gc_event = callback;
877 prof_list->gc_heap_resize = heap_resize_callback;
881 * mono_profiler_install_gc_moves:
882 * @callback: callback function
884 * Install the @callback function that the GC will call when moving objects.
885 * The callback receives an array of pointers and the number of elements
886 * in the array. Every even element in the array is the original object location
887 * and the following odd element is the new location of the object in memory.
888 * So the number of elements argument will always be a multiple of 2.
889 * Since this callback happens during the GC, it is a restricted environment:
890 * no locks can be taken and the object pointers can be inspected only once
891 * the GC is finished (of course the original location pointers will not
892 * point to valid objects anymore).
895 mono_profiler_install_gc_moves (MonoProfileGCMoveFunc callback)
899 prof_list->gc_moves = callback;
903 * mono_profiler_install_gc_roots:
904 * @handle_callback: callback function
905 * @roots_callback: callback function
907 * Install the @handle_callback function that the GC will call when GC
908 * handles are created or destroyed.
909 * The callback receives an operation, which is either #MONO_PROFILER_GC_HANDLE_CREATED
910 * or #MONO_PROFILER_GC_HANDLE_DESTROYED, the handle type, the handle value and the
911 * object pointer, if present.
912 * Install the @roots_callback function that the GC will call when tracing
913 * the roots for a collection.
914 * The callback receives the number of elements and three arrays: an array
915 * of objects, an array of root types and flags and an array of extra info.
916 * The size of each array is given by the first argument.
919 mono_profiler_install_gc_roots (MonoProfileGCHandleFunc handle_callback, MonoProfileGCRootFunc roots_callback)
923 prof_list->gc_handle = handle_callback;
924 prof_list->gc_roots = roots_callback;
928 mono_profiler_install_runtime_initialized (MonoProfileFunc runtime_initialized_callback)
932 prof_list->runtime_initialized_event = runtime_initialized_callback;
936 mono_profiler_runtime_initialized (void) {
938 for (prof = prof_list; prof; prof = prof->next) {
939 if (prof->runtime_initialized_event)
940 prof->runtime_initialized_event (prof->profiler);
945 mono_profiler_install_code_chunk_new (MonoProfilerCodeChunkNew callback) {
948 prof_list->code_chunk_new = callback;
951 mono_profiler_code_chunk_new (gpointer chunk, int size) {
953 for (prof = prof_list; prof; prof = prof->next) {
954 if (prof->code_chunk_new)
955 prof->code_chunk_new (prof->profiler, chunk, size);
960 mono_profiler_install_code_chunk_destroy (MonoProfilerCodeChunkDestroy callback) {
963 prof_list->code_chunk_destroy = callback;
966 mono_profiler_code_chunk_destroy (gpointer chunk) {
968 for (prof = prof_list; prof; prof = prof->next) {
969 if (prof->code_chunk_destroy)
970 prof->code_chunk_destroy (prof->profiler, chunk);
975 mono_profiler_install_code_buffer_new (MonoProfilerCodeBufferNew callback) {
978 prof_list->code_buffer_new = callback;
982 mono_profiler_install_iomap (MonoProfileIomapFunc callback)
986 prof_list->iomap_cb = callback;
990 mono_profiler_code_buffer_new (gpointer buffer, int size, MonoProfilerCodeBufferType type, gconstpointer data) {
992 for (prof = prof_list; prof; prof = prof->next) {
993 if (prof->code_buffer_new)
994 prof->code_buffer_new (prof->profiler, buffer, size, type, (void*)data);
998 static GHashTable *coverage_hash = NULL;
1000 MonoProfileCoverageInfo*
1001 mono_profiler_coverage_alloc (MonoMethod *method, int entries)
1003 MonoProfileCoverageInfo *res;
1004 int instrument = FALSE;
1007 for (prof = prof_list; prof; prof = prof->next) {
1008 /* note that we call the filter on all the profilers even if just
1009 * a single one would be enough to instrument a method
1011 if (prof->coverage_filter_cb)
1012 if (prof->coverage_filter_cb (prof->profiler, method))
1018 mono_profiler_coverage_lock ();
1020 coverage_hash = g_hash_table_new (NULL, NULL);
1022 res = (MonoProfileCoverageInfo *)g_malloc0 (sizeof (MonoProfileCoverageInfo) + sizeof (void*) * 2 * entries);
1024 res->entries = entries;
1026 g_hash_table_insert (coverage_hash, method, res);
1027 mono_profiler_coverage_unlock ();
1032 /* safe only when the method antive code has been unloaded */
1034 mono_profiler_coverage_free (MonoMethod *method)
1036 MonoProfileCoverageInfo* info;
1038 mono_profiler_coverage_lock ();
1039 if (!coverage_hash) {
1040 mono_profiler_coverage_unlock ();
1044 info = (MonoProfileCoverageInfo *)g_hash_table_lookup (coverage_hash, method);
1047 g_hash_table_remove (coverage_hash, method);
1049 mono_profiler_coverage_unlock ();
1053 * mono_profiler_coverage_get:
1054 * @prof: The profiler handle, installed with mono_profiler_install
1055 * @method: the method to gather information from.
1056 * @func: A routine that will be called back with the results
1058 * If the MONO_PROFILER_INS_COVERAGE flag was active during JIT compilation
1059 * it is posisble to obtain coverage information about a give method.
1061 * The function @func will be invoked repeatedly with instances of the
1062 * MonoProfileCoverageEntry structure.
1065 mono_profiler_coverage_get (MonoProfiler *prof, MonoMethod *method, MonoProfileCoverageFunc func)
1068 MonoProfileCoverageInfo* info = NULL;
1071 const unsigned char *start, *end, *cil_code;
1072 MonoMethodHeader *header;
1073 MonoProfileCoverageEntry entry;
1074 MonoDebugMethodInfo *debug_minfo;
1076 mono_profiler_coverage_lock ();
1078 info = (MonoProfileCoverageInfo *)g_hash_table_lookup (coverage_hash, method);
1079 mono_profiler_coverage_unlock ();
1084 header = mono_method_get_header_checked (method, &error);
1085 mono_error_assert_ok (&error);
1086 start = mono_method_header_get_code (header, &code_size, NULL);
1087 debug_minfo = mono_debug_lookup_method (method);
1089 end = start + code_size;
1090 for (i = 0; i < info->entries; ++i) {
1091 cil_code = info->data [i].cil_code;
1092 if (cil_code && cil_code >= start && cil_code < end) {
1094 offset = cil_code - start;
1095 entry.iloffset = offset;
1096 entry.method = method;
1097 entry.counter = info->data [i].count;
1098 entry.line = entry.col = 1;
1099 entry.filename = NULL;
1101 MonoDebugSourceLocation *location;
1103 location = mono_debug_symfile_lookup_location (debug_minfo, offset);
1105 entry.line = location->row;
1106 entry.col = location->column;
1107 entry.filename = fname = g_strdup (location->source_file);
1108 mono_debug_free_source_location (location);
1112 func (prof, &entry);
1116 mono_metadata_free_mh (header);
1119 typedef void (*ProfilerInitializer) (const char*);
1120 #define INITIALIZER_NAME "mono_profiler_startup"
1124 load_profiler (MonoDl *pmodule, const char *desc, const char *symbol)
1127 ProfilerInitializer func;
1132 if ((err = mono_dl_symbol (pmodule, symbol, (gpointer *) &func))) {
1142 load_embedded_profiler (const char *desc, const char *name)
1146 MonoDl *pmodule = NULL;
1149 pmodule = mono_dl_open (NULL, MONO_DL_LAZY, &err);
1151 g_warning ("Could not open main executable (%s)", err);
1156 symbol = g_strdup_printf (INITIALIZER_NAME "_%s", name);
1157 result = load_profiler (pmodule, desc, symbol);
1164 load_profiler_from_directory (const char *directory, const char *libname, const char *desc)
1166 MonoDl *pmodule = NULL;
1173 while ((path = mono_dl_build_path (directory, libname, &iter))) {
1174 pmodule = mono_dl_open (path, MONO_DL_LAZY, &err);
1178 return load_profiler (pmodule, desc, INITIALIZER_NAME);
1185 load_profiler_from_mono_instalation (const char *libname, const char *desc)
1188 MonoDl *pmodule = mono_dl_open_runtime_lib (libname, MONO_DL_LAZY, &err);
1191 return load_profiler (pmodule, desc, INITIALIZER_NAME);
1196 * mono_profiler_load:
1197 * @desc: arguments to configure the profiler
1199 * Invoke this method to initialize the profiler. This will drive the
1200 * loading of the internal ("default") or any external profilers.
1202 * This routine is invoked by Mono's driver, but must be called manually
1203 * if you embed Mono into your application.
1206 mono_profiler_load (const char *desc)
1209 mono_gc_base_init ();
1211 if (!desc || (strcmp ("default", desc) == 0)) {
1212 desc = "log:report";
1214 /* we keep command-line compat with the old version here */
1215 if (strncmp (desc, "default:", 8) == 0) {
1216 gchar **args, **ptr;
1217 GString *str = g_string_new ("log:report");
1218 args = g_strsplit (desc + 8, ",", -1);
1219 for (ptr = args; ptr && *ptr; ptr++) {
1220 const char *arg = *ptr;
1222 if (!strcmp (arg, "time"))
1223 g_string_append (str, ",calls");
1224 else if (!strcmp (arg, "alloc"))
1225 g_string_append (str, ",alloc");
1226 else if (!strcmp (arg, "stat"))
1227 g_string_append (str, ",sample");
1228 else if (!strcmp (arg, "jit"))
1229 continue; /* accept and do nothing */
1230 else if (strncmp (arg, "file=", 5) == 0) {
1231 g_string_append_printf (str, ",output=%s", arg + 5);
1233 fprintf (stderr, "profiler : Unknown argument '%s'.\n", arg);
1237 desc = cdesc = g_string_free (str, FALSE);
1240 const char* col = strchr (desc, ':');
1243 gboolean res = FALSE;
1246 mname = (char *)g_memdup (desc, col - desc + 1);
1247 mname [col - desc] = 0;
1249 mname = g_strdup (desc);
1251 if (!load_embedded_profiler (desc, mname)) {
1252 libname = g_strdup_printf ("mono-profiler-%s", mname);
1253 if (mono_config_get_assemblies_dir ())
1254 res = load_profiler_from_directory (mono_assembly_getrootdir (), libname, desc);
1256 res = load_profiler_from_directory (NULL, libname, desc);
1258 res = load_profiler_from_mono_instalation (libname, desc);
1260 g_warning ("The '%s' profiler wasn't found in the main executable nor could it be loaded from '%s'.", mname, libname);