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"
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 * @prof: a MonoProfiler structure pointer, or a pointer to a derived structure.
131 * @callback: the function to invoke at shutdown
133 * Use mono_profiler_install to activate profiling in the Mono runtime.
134 * Typically developers of new profilers will create a new structure whose
135 * first field is a MonoProfiler and put any extra information that they need
136 * to access from the various profiling callbacks there.
140 mono_profiler_install (MonoProfiler *prof, MonoProfileFunc callback)
142 ProfilerDesc *desc = g_new0 (ProfilerDesc, 1);
144 mono_os_mutex_init_recursive (&profiler_coverage_mutex);
145 desc->profiler = prof;
146 desc->shutdown_callback = callback;
147 desc->next = prof_list;
152 * mono_profiler_set_events:
153 * @events: an ORed set of values made up of MONO_PROFILER_ flags
155 * The events descriped in the @events argument is a set of flags
156 * that represent which profiling events must be triggered. For
157 * example if you have registered a set of methods for tracking
158 * JIT compilation start and end with mono_profiler_install_jit_compile,
159 * you will want to pass the MONO_PROFILE_JIT_COMPILATION flag to
162 * You can call mono_profile_set_events more than once and you can
163 * do this at runtime to modify which methods are invoked.
166 mono_profiler_set_events (MonoProfileFlags events)
169 MonoProfileFlags value = (MonoProfileFlags)0;
171 prof_list->events = events;
172 for (prof = prof_list; prof; prof = prof->next)
173 value = (MonoProfileFlags)(value | prof->events);
174 mono_profiler_events = value;
178 * mono_profiler_get_events:
180 * Returns a list of active events that will be intercepted.
183 mono_profiler_get_events (void)
185 return mono_profiler_events;
189 * mono_profiler_install_enter_leave:
190 * @enter: the routine to be called on each method entry
191 * @fleave: the routine to be called each time a method returns
193 * Use this routine to install routines that will be called everytime
194 * a method enters and leaves. The routines will receive as an argument
195 * the MonoMethod representing the method that is entering or leaving.
198 mono_profiler_install_enter_leave (MonoProfileMethodFunc enter, MonoProfileMethodFunc fleave)
202 prof_list->method_enter = enter;
203 prof_list->method_leave = fleave;
207 * mono_profiler_install_jit_compile:
208 * @start: the routine to be called when the JIT process starts.
209 * @end: the routine to be called when the JIT process ends.
211 * Use this routine to install routines that will be called when JIT
212 * compilation of a method starts and completes.
215 mono_profiler_install_jit_compile (MonoProfileMethodFunc start, MonoProfileMethodResult end)
219 prof_list->jit_start = start;
220 prof_list->jit_end = end;
224 mono_profiler_install_jit_end (MonoProfileJitResult end)
228 prof_list->jit_end2 = end;
232 mono_profiler_install_method_free (MonoProfileMethodFunc callback)
236 prof_list->method_free = callback;
240 mono_profiler_install_method_invoke (MonoProfileMethodFunc start, MonoProfileMethodFunc end)
244 prof_list->method_start_invoke = start;
245 prof_list->method_end_invoke = end;
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 (MonoProfileMethodResult callback)
270 prof_list->man_unman_transition = callback;
274 mono_profiler_install_allocation (MonoProfileAllocFunc callback)
278 prof_list->allocation_cb = callback;
282 mono_profiler_install_monitor (MonoProfileMonitorFunc callback)
286 prof_list->monitor_event_cb = callback;
289 static MonoProfileSamplingMode sampling_mode = MONO_PROFILER_STAT_MODE_PROCESS;
290 static int64_t sampling_frequency = 100; // Hz
293 * mono_profiler_set_statistical_mode:
294 * @mode the sampling mode used.
295 * @sample_frequency_is_us the sampling frequency in microseconds.
297 * Set the sampling parameters for the profiler. Sampling mode affects the effective sampling rate as in samples/s you'll witness.
298 * The default sampling mode is process mode, which only reports samples when there's activity in the process.
300 * Sampling frequency should be interpreted as a suggestion that can't always be honored due to how most kernels expose alarms.
302 * Said that, when using statistical sampling, always assume variable rate sampling as all sort of external factors can interfere.
305 mono_profiler_set_statistical_mode (MonoProfileSamplingMode mode, int64_t sampling_frequency_hz)
307 sampling_mode = mode;
308 sampling_frequency = sampling_frequency_hz;
312 mono_profiler_install_statistical (MonoProfileStatFunc callback)
316 prof_list->statistical_cb = callback;
320 mono_profiler_get_sampling_rate (void)
322 return sampling_frequency;
325 MonoProfileSamplingMode
326 mono_profiler_get_sampling_mode (void)
328 return sampling_mode;
332 mono_profiler_install_statistical_call_chain (MonoProfileStatCallChainFunc callback, int call_chain_depth, MonoProfilerCallChainStrategy call_chain_strategy) {
335 if (call_chain_depth > MONO_PROFILER_MAX_STAT_CALL_CHAIN_DEPTH) {
336 call_chain_depth = MONO_PROFILER_MAX_STAT_CALL_CHAIN_DEPTH;
338 if ((call_chain_strategy >= MONO_PROFILER_CALL_CHAIN_INVALID) || (call_chain_strategy < MONO_PROFILER_CALL_CHAIN_NONE)) {
339 call_chain_strategy = MONO_PROFILER_CALL_CHAIN_NONE;
341 prof_list->statistical_call_chain_cb = callback;
342 prof_list->statistical_call_chain_depth = call_chain_depth;
343 prof_list->statistical_call_chain_strategy = call_chain_strategy;
347 mono_profiler_stat_get_call_chain_depth (void) {
348 if (prof_list && prof_list->statistical_call_chain_cb != NULL) {
349 return prof_list->statistical_call_chain_depth;
355 MonoProfilerCallChainStrategy
356 mono_profiler_stat_get_call_chain_strategy (void) {
357 if (prof_list && prof_list->statistical_call_chain_cb != NULL) {
358 return prof_list->statistical_call_chain_strategy;
360 return MONO_PROFILER_CALL_CHAIN_NONE;
364 void mono_profiler_install_exception (MonoProfileExceptionFunc throw_callback, MonoProfileMethodFunc exc_method_leave, MonoProfileExceptionClauseFunc clause_callback)
368 prof_list->exception_throw_cb = throw_callback;
369 prof_list->exception_method_leave_cb = exc_method_leave;
370 prof_list->exception_clause_cb = clause_callback;
374 mono_profiler_install_coverage_filter (MonoProfileCoverageFilterFunc callback)
378 prof_list->coverage_filter_cb = callback;
382 mono_profiler_install_appdomain (MonoProfileAppDomainFunc start_load, MonoProfileAppDomainResult end_load,
383 MonoProfileAppDomainFunc start_unload, MonoProfileAppDomainFunc end_unload)
388 prof_list->domain_start_load = start_load;
389 prof_list->domain_end_load = end_load;
390 prof_list->domain_start_unload = start_unload;
391 prof_list->domain_end_unload = end_unload;
395 mono_profiler_install_appdomain_name (MonoProfileAppDomainFriendlyNameFunc domain_name_cb)
400 prof_list->domain_name = domain_name_cb;
404 mono_profiler_install_context (MonoProfileContextFunc load, MonoProfileContextFunc unload)
409 prof_list->context_load = load;
410 prof_list->context_unload = unload;
414 mono_profiler_install_assembly (MonoProfileAssemblyFunc start_load, MonoProfileAssemblyResult end_load,
415 MonoProfileAssemblyFunc start_unload, MonoProfileAssemblyFunc end_unload)
419 prof_list->assembly_start_load = start_load;
420 prof_list->assembly_end_load = end_load;
421 prof_list->assembly_start_unload = start_unload;
422 prof_list->assembly_end_unload = end_unload;
426 mono_profiler_install_module (MonoProfileModuleFunc start_load, MonoProfileModuleResult end_load,
427 MonoProfileModuleFunc start_unload, MonoProfileModuleFunc end_unload)
431 prof_list->module_start_load = start_load;
432 prof_list->module_end_load = end_load;
433 prof_list->module_start_unload = start_unload;
434 prof_list->module_end_unload = end_unload;
438 mono_profiler_install_class (MonoProfileClassFunc start_load, MonoProfileClassResult end_load,
439 MonoProfileClassFunc start_unload, MonoProfileClassFunc end_unload)
443 prof_list->class_start_load = start_load;
444 prof_list->class_end_load = end_load;
445 prof_list->class_start_unload = start_unload;
446 prof_list->class_end_unload = end_unload;
450 mono_profiler_method_enter (MonoMethod *method)
453 for (prof = prof_list; prof; prof = prof->next) {
454 if ((prof->events & MONO_PROFILE_ENTER_LEAVE) && prof->method_enter)
455 prof->method_enter (prof->profiler, method);
460 mono_profiler_method_leave (MonoMethod *method)
463 for (prof = prof_list; prof; prof = prof->next) {
464 if ((prof->events & MONO_PROFILE_ENTER_LEAVE) && prof->method_leave)
465 prof->method_leave (prof->profiler, method);
470 mono_profiler_method_jit (MonoMethod *method)
473 for (prof = prof_list; prof; prof = prof->next) {
474 if ((prof->events & MONO_PROFILE_JIT_COMPILATION) && prof->jit_start)
475 prof->jit_start (prof->profiler, method);
480 mono_profiler_method_end_jit (MonoMethod *method, MonoJitInfo* jinfo, int result)
483 for (prof = prof_list; prof; prof = prof->next) {
484 if ((prof->events & MONO_PROFILE_JIT_COMPILATION)) {
486 prof->jit_end (prof->profiler, method, result);
488 prof->jit_end2 (prof->profiler, method, jinfo, result);
494 mono_profiler_method_free (MonoMethod *method)
497 for (prof = prof_list; prof; prof = prof->next) {
498 if ((prof->events & MONO_PROFILE_METHOD_EVENTS) && prof->method_free)
499 prof->method_free (prof->profiler, method);
504 mono_profiler_method_start_invoke (MonoMethod *method)
507 for (prof = prof_list; prof; prof = prof->next) {
508 if ((prof->events & MONO_PROFILE_METHOD_EVENTS) && prof->method_start_invoke)
509 prof->method_start_invoke (prof->profiler, method);
514 mono_profiler_method_end_invoke (MonoMethod *method)
517 for (prof = prof_list; prof; prof = prof->next) {
518 if ((prof->events & MONO_PROFILE_METHOD_EVENTS) && prof->method_end_invoke)
519 prof->method_end_invoke (prof->profiler, method);
524 mono_profiler_code_transition (MonoMethod *method, int result)
527 for (prof = prof_list; prof; prof = prof->next) {
528 if ((prof->events & MONO_PROFILE_TRANSITIONS) && prof->man_unman_transition)
529 prof->man_unman_transition (prof->profiler, method, result);
534 mono_profiler_allocation (MonoObject *obj)
537 for (prof = prof_list; prof; prof = prof->next) {
538 if ((prof->events & MONO_PROFILE_ALLOCATIONS) && prof->allocation_cb)
539 prof->allocation_cb (prof->profiler, obj, obj->vtable->klass);
544 mono_profiler_monitor_event (MonoObject *obj, MonoProfilerMonitorEvent event) {
546 for (prof = prof_list; prof; prof = prof->next) {
547 if ((prof->events & MONO_PROFILE_MONITOR_EVENTS) && prof->monitor_event_cb)
548 prof->monitor_event_cb (prof->profiler, obj, event);
553 mono_profiler_stat_hit (guchar *ip, void *context)
556 for (prof = prof_list; prof; prof = prof->next) {
557 if ((prof->events & MONO_PROFILE_STATISTICAL) && prof->statistical_cb)
558 prof->statistical_cb (prof->profiler, ip, context);
563 mono_profiler_stat_call_chain (int call_chain_depth, guchar **ips, void *context)
566 for (prof = prof_list; prof; prof = prof->next) {
567 if ((prof->events & MONO_PROFILE_STATISTICAL) && prof->statistical_call_chain_cb)
568 prof->statistical_call_chain_cb (prof->profiler, call_chain_depth, ips, context);
573 mono_profiler_exception_thrown (MonoObject *exception)
576 for (prof = prof_list; prof; prof = prof->next) {
577 if ((prof->events & MONO_PROFILE_EXCEPTIONS) && prof->exception_throw_cb)
578 prof->exception_throw_cb (prof->profiler, exception);
583 mono_profiler_exception_method_leave (MonoMethod *method)
586 for (prof = prof_list; prof; prof = prof->next) {
587 if ((prof->events & MONO_PROFILE_EXCEPTIONS) && prof->exception_method_leave_cb)
588 prof->exception_method_leave_cb (prof->profiler, method);
593 mono_profiler_exception_clause_handler (MonoMethod *method, int clause_type, int clause_num)
596 for (prof = prof_list; prof; prof = prof->next) {
597 if ((prof->events & MONO_PROFILE_EXCEPTIONS) && prof->exception_clause_cb)
598 prof->exception_clause_cb (prof->profiler, method, clause_type, clause_num);
603 mono_profiler_thread_start (gsize tid)
606 for (prof = prof_list; prof; prof = prof->next) {
607 if ((prof->events & MONO_PROFILE_THREADS) && prof->thread_start)
608 prof->thread_start (prof->profiler, tid);
613 mono_profiler_thread_end (gsize tid)
616 for (prof = prof_list; prof; prof = prof->next) {
617 if ((prof->events & MONO_PROFILE_THREADS) && prof->thread_end)
618 prof->thread_end (prof->profiler, tid);
623 mono_profiler_thread_name (gsize tid, const char *name)
626 for (prof = prof_list; prof; prof = prof->next) {
627 if ((prof->events & MONO_PROFILE_THREADS) && prof->thread_name)
628 prof->thread_name (prof->profiler, tid, name);
633 mono_profiler_assembly_event (MonoAssembly *assembly, int code)
636 for (prof = prof_list; prof; prof = prof->next) {
637 if (!(prof->events & MONO_PROFILE_ASSEMBLY_EVENTS))
641 case MONO_PROFILE_START_LOAD:
642 if (prof->assembly_start_load)
643 prof->assembly_start_load (prof->profiler, assembly);
645 case MONO_PROFILE_START_UNLOAD:
646 if (prof->assembly_start_unload)
647 prof->assembly_start_unload (prof->profiler, assembly);
649 case MONO_PROFILE_END_UNLOAD:
650 if (prof->assembly_end_unload)
651 prof->assembly_end_unload (prof->profiler, assembly);
654 g_assert_not_reached ();
660 mono_profiler_assembly_loaded (MonoAssembly *assembly, int result)
663 for (prof = prof_list; prof; prof = prof->next) {
664 if ((prof->events & MONO_PROFILE_ASSEMBLY_EVENTS) && prof->assembly_end_load)
665 prof->assembly_end_load (prof->profiler, assembly, result);
669 void mono_profiler_iomap (char *report, const char *pathname, const char *new_pathname)
672 for (prof = prof_list; prof; prof = prof->next) {
673 if ((prof->events & MONO_PROFILE_IOMAP_EVENTS) && prof->iomap_cb)
674 prof->iomap_cb (prof->profiler, report, pathname, new_pathname);
679 mono_profiler_module_event (MonoImage *module, int code)
682 for (prof = prof_list; prof; prof = prof->next) {
683 if (!(prof->events & MONO_PROFILE_MODULE_EVENTS))
687 case MONO_PROFILE_START_LOAD:
688 if (prof->module_start_load)
689 prof->module_start_load (prof->profiler, module);
691 case MONO_PROFILE_START_UNLOAD:
692 if (prof->module_start_unload)
693 prof->module_start_unload (prof->profiler, module);
695 case MONO_PROFILE_END_UNLOAD:
696 if (prof->module_end_unload)
697 prof->module_end_unload (prof->profiler, module);
700 g_assert_not_reached ();
706 mono_profiler_module_loaded (MonoImage *module, int result)
709 for (prof = prof_list; prof; prof = prof->next) {
710 if ((prof->events & MONO_PROFILE_MODULE_EVENTS) && prof->module_end_load)
711 prof->module_end_load (prof->profiler, module, result);
716 mono_profiler_class_event (MonoClass *klass, int code)
719 for (prof = prof_list; prof; prof = prof->next) {
720 if (!(prof->events & MONO_PROFILE_CLASS_EVENTS))
724 case MONO_PROFILE_START_LOAD:
725 if (prof->class_start_load)
726 prof->class_start_load (prof->profiler, klass);
728 case MONO_PROFILE_START_UNLOAD:
729 if (prof->class_start_unload)
730 prof->class_start_unload (prof->profiler, klass);
732 case MONO_PROFILE_END_UNLOAD:
733 if (prof->class_end_unload)
734 prof->class_end_unload (prof->profiler, klass);
737 g_assert_not_reached ();
743 mono_profiler_class_loaded (MonoClass *klass, int result)
746 for (prof = prof_list; prof; prof = prof->next) {
747 if ((prof->events & MONO_PROFILE_CLASS_EVENTS) && prof->class_end_load)
748 prof->class_end_load (prof->profiler, klass, result);
753 mono_profiler_appdomain_event (MonoDomain *domain, int code)
756 for (prof = prof_list; prof; prof = prof->next) {
757 if (!(prof->events & MONO_PROFILE_APPDOMAIN_EVENTS))
761 case MONO_PROFILE_START_LOAD:
762 if (prof->domain_start_load)
763 prof->domain_start_load (prof->profiler, domain);
765 case MONO_PROFILE_START_UNLOAD:
766 if (prof->domain_start_unload)
767 prof->domain_start_unload (prof->profiler, domain);
769 case MONO_PROFILE_END_UNLOAD:
770 if (prof->domain_end_unload)
771 prof->domain_end_unload (prof->profiler, domain);
774 g_assert_not_reached ();
780 mono_profiler_appdomain_loaded (MonoDomain *domain, int result)
783 for (prof = prof_list; prof; prof = prof->next) {
784 if ((prof->events & MONO_PROFILE_APPDOMAIN_EVENTS) && prof->domain_end_load)
785 prof->domain_end_load (prof->profiler, domain, result);
790 mono_profiler_appdomain_name (MonoDomain *domain, const char *name)
792 for (ProfilerDesc *prof = prof_list; prof; prof = prof->next)
793 if ((prof->events & MONO_PROFILE_APPDOMAIN_EVENTS) && prof->domain_name)
794 prof->domain_name (prof->profiler, domain, name);
798 mono_profiler_context_loaded (MonoAppContext *context)
800 for (ProfilerDesc *prof = prof_list; prof; prof = prof->next)
801 if ((prof->events & MONO_PROFILE_CONTEXT_EVENTS) && prof->context_load)
802 prof->context_load (prof->profiler, context);
806 mono_profiler_context_unloaded (MonoAppContext *context)
808 for (ProfilerDesc *prof = prof_list; prof; prof = prof->next)
809 if ((prof->events & MONO_PROFILE_CONTEXT_EVENTS) && prof->context_unload)
810 prof->context_unload (prof->profiler, context);
814 mono_profiler_shutdown (void)
817 for (prof = prof_list; prof; prof = prof->next) {
818 if (prof->shutdown_callback)
819 prof->shutdown_callback (prof->profiler);
822 mono_profiler_set_events ((MonoProfileFlags)0);
826 mono_profiler_gc_heap_resize (gint64 new_size)
829 for (prof = prof_list; prof; prof = prof->next) {
830 if ((prof->events & MONO_PROFILE_GC) && prof->gc_heap_resize)
831 prof->gc_heap_resize (prof->profiler, new_size);
836 mono_profiler_gc_event (MonoGCEvent event, int generation)
839 for (prof = prof_list; prof; prof = prof->next) {
840 if ((prof->events & MONO_PROFILE_GC) && prof->gc_event)
841 prof->gc_event (prof->profiler, event, generation);
846 mono_profiler_gc_moves (void **objects, int num)
849 for (prof = prof_list; prof; prof = prof->next) {
850 if ((prof->events & MONO_PROFILE_GC_MOVES) && prof->gc_moves)
851 prof->gc_moves (prof->profiler, objects, num);
856 mono_profiler_gc_handle (int op, int type, uintptr_t handle, MonoObject *obj)
859 for (prof = prof_list; prof; prof = prof->next) {
860 if ((prof->events & MONO_PROFILE_GC_ROOTS) && prof->gc_handle)
861 prof->gc_handle (prof->profiler, op, type, handle, obj);
866 mono_profiler_gc_roots (int num, void **objects, int *root_types, uintptr_t *extra_info)
869 for (prof = prof_list; prof; prof = prof->next) {
870 if ((prof->events & MONO_PROFILE_GC_ROOTS) && prof->gc_roots)
871 prof->gc_roots (prof->profiler, num, objects, root_types, extra_info);
876 mono_profiler_install_gc (MonoProfileGCFunc callback, MonoProfileGCResizeFunc heap_resize_callback)
880 prof_list->gc_event = callback;
881 prof_list->gc_heap_resize = heap_resize_callback;
885 * mono_profiler_install_gc_moves:
886 * @callback: callback function
888 * Install the @callback function that the GC will call when moving objects.
889 * The callback receives an array of pointers and the number of elements
890 * in the array. Every even element in the array is the original object location
891 * and the following odd element is the new location of the object in memory.
892 * So the number of elements argument will always be a multiple of 2.
893 * Since this callback happens during the GC, it is a restricted environment:
894 * no locks can be taken and the object pointers can be inspected only once
895 * the GC is finished (of course the original location pointers will not
896 * point to valid objects anymore).
899 mono_profiler_install_gc_moves (MonoProfileGCMoveFunc callback)
903 prof_list->gc_moves = callback;
907 * mono_profiler_install_gc_roots:
908 * @handle_callback: callback function
909 * @roots_callback: callback function
911 * Install the @handle_callback function that the GC will call when GC
912 * handles are created or destroyed.
913 * The callback receives an operation, which is either #MONO_PROFILER_GC_HANDLE_CREATED
914 * or #MONO_PROFILER_GC_HANDLE_DESTROYED, the handle type, the handle value and the
915 * object pointer, if present.
916 * Install the @roots_callback function that the GC will call when tracing
917 * the roots for a collection.
918 * The callback receives the number of elements and three arrays: an array
919 * of objects, an array of root types and flags and an array of extra info.
920 * The size of each array is given by the first argument.
923 mono_profiler_install_gc_roots (MonoProfileGCHandleFunc handle_callback, MonoProfileGCRootFunc roots_callback)
927 prof_list->gc_handle = handle_callback;
928 prof_list->gc_roots = roots_callback;
932 mono_profiler_gc_finalize_begin (void)
934 for (ProfilerDesc *prof = prof_list; prof; prof = prof->next)
935 if ((prof->events & MONO_PROFILE_GC_FINALIZATION) && prof->gc_finalize_begin)
936 prof->gc_finalize_begin (prof->profiler);
940 mono_profiler_gc_finalize_object_begin (MonoObject *obj)
942 for (ProfilerDesc *prof = prof_list; prof; prof = prof->next)
943 if ((prof->events & MONO_PROFILE_GC_FINALIZATION) && prof->gc_finalize_object_begin)
944 prof->gc_finalize_object_begin (prof->profiler, obj);
948 mono_profiler_gc_finalize_object_end (MonoObject *obj)
950 for (ProfilerDesc *prof = prof_list; prof; prof = prof->next)
951 if ((prof->events & MONO_PROFILE_GC_FINALIZATION) && prof->gc_finalize_object_end)
952 prof->gc_finalize_object_end (prof->profiler, obj);
956 mono_profiler_gc_finalize_end (void)
958 for (ProfilerDesc *prof = prof_list; prof; prof = prof->next)
959 if ((prof->events & MONO_PROFILE_GC_FINALIZATION) && prof->gc_finalize_end)
960 prof->gc_finalize_end (prof->profiler);
964 mono_profiler_install_gc_finalize (MonoProfileGCFinalizeFunc begin, MonoProfileGCFinalizeObjectFunc begin_obj, MonoProfileGCFinalizeObjectFunc end_obj, MonoProfileGCFinalizeFunc end)
969 prof_list->gc_finalize_begin = begin;
970 prof_list->gc_finalize_object_begin = begin_obj;
971 prof_list->gc_finalize_object_end = end_obj;
972 prof_list->gc_finalize_end = end;
976 mono_profiler_install_runtime_initialized (MonoProfileFunc runtime_initialized_callback)
980 prof_list->runtime_initialized_event = runtime_initialized_callback;
984 mono_profiler_runtime_initialized (void) {
986 for (prof = prof_list; prof; prof = prof->next) {
987 if (prof->runtime_initialized_event)
988 prof->runtime_initialized_event (prof->profiler);
993 mono_profiler_install_code_chunk_new (MonoProfilerCodeChunkNew callback) {
996 prof_list->code_chunk_new = callback;
999 mono_profiler_code_chunk_new (gpointer chunk, int size) {
1001 for (prof = prof_list; prof; prof = prof->next) {
1002 if (prof->code_chunk_new)
1003 prof->code_chunk_new (prof->profiler, chunk, size);
1008 mono_profiler_install_code_chunk_destroy (MonoProfilerCodeChunkDestroy callback) {
1011 prof_list->code_chunk_destroy = callback;
1014 mono_profiler_code_chunk_destroy (gpointer chunk) {
1016 for (prof = prof_list; prof; prof = prof->next) {
1017 if (prof->code_chunk_destroy)
1018 prof->code_chunk_destroy (prof->profiler, chunk);
1023 mono_profiler_install_code_buffer_new (MonoProfilerCodeBufferNew callback) {
1026 prof_list->code_buffer_new = callback;
1030 mono_profiler_install_iomap (MonoProfileIomapFunc callback)
1034 prof_list->iomap_cb = callback;
1038 mono_profiler_code_buffer_new (gpointer buffer, int size, MonoProfilerCodeBufferType type, gconstpointer data) {
1040 for (prof = prof_list; prof; prof = prof->next) {
1041 if (prof->code_buffer_new)
1042 prof->code_buffer_new (prof->profiler, buffer, size, type, (void*)data);
1046 static GHashTable *coverage_hash = NULL;
1048 MonoProfileCoverageInfo*
1049 mono_profiler_coverage_alloc (MonoMethod *method, int entries)
1051 MonoProfileCoverageInfo *res;
1052 int instrument = FALSE;
1055 for (prof = prof_list; prof; prof = prof->next) {
1056 /* note that we call the filter on all the profilers even if just
1057 * a single one would be enough to instrument a method
1059 if (prof->coverage_filter_cb)
1060 if (prof->coverage_filter_cb (prof->profiler, method))
1066 mono_profiler_coverage_lock ();
1068 coverage_hash = g_hash_table_new (NULL, NULL);
1070 res = (MonoProfileCoverageInfo *)g_malloc0 (sizeof (MonoProfileCoverageInfo) + sizeof (void*) * 2 * entries);
1072 res->entries = entries;
1074 g_hash_table_insert (coverage_hash, method, res);
1075 mono_profiler_coverage_unlock ();
1080 /* safe only when the method antive code has been unloaded */
1082 mono_profiler_coverage_free (MonoMethod *method)
1084 MonoProfileCoverageInfo* info;
1086 mono_profiler_coverage_lock ();
1087 if (!coverage_hash) {
1088 mono_profiler_coverage_unlock ();
1092 info = (MonoProfileCoverageInfo *)g_hash_table_lookup (coverage_hash, method);
1095 g_hash_table_remove (coverage_hash, method);
1097 mono_profiler_coverage_unlock ();
1101 * mono_profiler_coverage_get:
1102 * @prof: The profiler handle, installed with mono_profiler_install
1103 * @method: the method to gather information from.
1104 * @func: A routine that will be called back with the results
1106 * If the MONO_PROFILER_INS_COVERAGE flag was active during JIT compilation
1107 * it is posisble to obtain coverage information about a give method.
1109 * The function @func will be invoked repeatedly with instances of the
1110 * MonoProfileCoverageEntry structure.
1113 mono_profiler_coverage_get (MonoProfiler *prof, MonoMethod *method, MonoProfileCoverageFunc func)
1116 MonoProfileCoverageInfo* info = NULL;
1119 const unsigned char *start, *end, *cil_code;
1120 MonoMethodHeader *header;
1121 MonoProfileCoverageEntry entry;
1122 MonoDebugMethodInfo *debug_minfo;
1124 mono_profiler_coverage_lock ();
1126 info = (MonoProfileCoverageInfo *)g_hash_table_lookup (coverage_hash, method);
1127 mono_profiler_coverage_unlock ();
1132 header = mono_method_get_header_checked (method, &error);
1133 mono_error_assert_ok (&error);
1134 start = mono_method_header_get_code (header, &code_size, NULL);
1135 debug_minfo = mono_debug_lookup_method (method);
1137 end = start + code_size;
1138 for (i = 0; i < info->entries; ++i) {
1139 cil_code = info->data [i].cil_code;
1140 if (cil_code && cil_code >= start && cil_code < end) {
1142 offset = cil_code - start;
1143 entry.iloffset = offset;
1144 entry.method = method;
1145 entry.counter = info->data [i].count;
1146 entry.line = entry.col = 1;
1147 entry.filename = NULL;
1149 MonoDebugSourceLocation *location;
1151 location = mono_debug_symfile_lookup_location (debug_minfo, offset);
1153 entry.line = location->row;
1154 entry.col = location->column;
1155 entry.filename = fname = g_strdup (location->source_file);
1156 mono_debug_free_source_location (location);
1160 func (prof, &entry);
1164 mono_metadata_free_mh (header);
1167 typedef void (*ProfilerInitializer) (const char*);
1168 #define INITIALIZER_NAME "mono_profiler_startup"
1172 load_profiler (MonoDl *pmodule, const char *desc, const char *symbol)
1175 ProfilerInitializer func;
1180 if ((err = mono_dl_symbol (pmodule, symbol, (gpointer *) &func))) {
1190 load_embedded_profiler (const char *desc, const char *name)
1194 MonoDl *pmodule = NULL;
1198 * Some profilers (such as ours) may need to call back into the runtime
1199 * from their sampling callback (which is called in async-signal context).
1200 * They need to be able to know that all references back to the runtime
1201 * have been resolved; otherwise, calling runtime functions may result in
1202 * invoking the dynamic linker which is not async-signal-safe. Passing
1203 * MONO_DL_EAGER will ask the dynamic linker to resolve everything upfront.
1205 pmodule = mono_dl_open (NULL, MONO_DL_EAGER, &err);
1207 g_warning ("Could not open main executable (%s)", err);
1212 symbol = g_strdup_printf (INITIALIZER_NAME "_%s", name);
1213 result = load_profiler (pmodule, desc, symbol);
1219 // TODO: Much of the library loading code here is custom. It would be better to merge this with mono-dl
1221 load_profiler_from_directory (const char *directory, const char *libname, const char *desc)
1223 MonoDl *pmodule = NULL;
1228 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT, "Attempting to load profiler %s from %s (desc %s)", libname, directory, desc);
1232 while ((path = mono_dl_build_path (directory, libname, &iter))) {
1233 pmodule = mono_dl_open (path, MONO_DL_EAGER, &err);
1234 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT, "Attempting to load profiler: %s, %ssuccessful, err: %s", path, pmodule?"":"not ", err);
1238 return load_profiler (pmodule, desc, INITIALIZER_NAME);
1245 load_profiler_from_mono_installation (const char *libname, const char *desc)
1248 MonoDl *pmodule = mono_dl_open_runtime_lib (libname, MONO_DL_EAGER, &err);
1249 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT, "Attempting to load profiler from runtime libs: %s, %ssuccessful, err: %s", libname, pmodule?"":"not ", err);
1252 return load_profiler (pmodule, desc, INITIALIZER_NAME);
1257 * mono_profiler_load:
1258 * @desc: arguments to configure the profiler
1260 * Invoke this method to initialize the profiler. This will drive the
1261 * loading of the internal ("default") or any external profilers.
1263 * This routine is invoked by Mono's driver, but must be called manually
1264 * if you embed Mono into your application.
1267 mono_profiler_load (const char *desc)
1270 mono_gc_base_init ();
1272 if (!desc || (strcmp ("default", desc) == 0)) {
1273 desc = "log:report";
1275 /* we keep command-line compat with the old version here */
1276 if (strncmp (desc, "default:", 8) == 0) {
1277 gchar **args, **ptr;
1278 GString *str = g_string_new ("log:report");
1279 args = g_strsplit (desc + 8, ",", -1);
1280 for (ptr = args; ptr && *ptr; ptr++) {
1281 const char *arg = *ptr;
1283 if (!strcmp (arg, "time"))
1284 g_string_append (str, ",calls");
1285 else if (!strcmp (arg, "alloc"))
1286 g_string_append (str, ",alloc");
1287 else if (!strcmp (arg, "stat"))
1288 g_string_append (str, ",sample");
1289 else if (!strcmp (arg, "jit"))
1290 continue; /* accept and do nothing */
1291 else if (strncmp (arg, "file=", 5) == 0) {
1292 g_string_append_printf (str, ",output=%s", arg + 5);
1294 fprintf (stderr, "profiler : Unknown argument '%s'.\n", arg);
1298 desc = cdesc = g_string_free (str, FALSE);
1301 const char* col = strchr (desc, ':');
1304 gboolean res = FALSE;
1307 mname = (char *)g_memdup (desc, col - desc + 1);
1308 mname [col - desc] = 0;
1310 mname = g_strdup (desc);
1312 if (!load_embedded_profiler (desc, mname)) {
1313 libname = g_strdup_printf ("mono-profiler-%s", mname);
1314 res = load_profiler_from_mono_installation (libname, desc);
1315 if (!res && mono_config_get_assemblies_dir ())
1316 res = load_profiler_from_directory (mono_assembly_getrootdir (), libname, desc);
1318 res = load_profiler_from_directory (NULL, libname, desc);
1320 g_warning ("The '%s' profiler wasn't found in the main executable nor could it be loaded from '%s'.", mname, libname);