2 * profiler.c: Profiler interface for Mono
5 * Paolo Molaro (lupus@ximian.com)
7 * Copyright 2001-2003 Ximian, Inc (http://www.ximian.com)
8 * Copyright 2004-2009 Novell, Inc (http://www.novell.com)
9 * Copyright 2011 Xamarin Inc (http://www.xamarin.com).
13 #include "mono/metadata/profiler-private.h"
14 #include "mono/metadata/assembly.h"
15 #include "mono/metadata/debug-helpers.h"
16 #include "mono/metadata/mono-debug.h"
17 #include "mono/metadata/debug-mono-symfile.h"
18 #include "mono/metadata/metadata-internals.h"
19 #include "mono/metadata/class-internals.h"
20 #include "mono/metadata/domain-internals.h"
21 #include "mono/metadata/gc-internal.h"
22 #include "mono/metadata/mono-config-dirs.h"
23 #include "mono/io-layer/io-layer.h"
24 #include "mono/utils/mono-dl.h"
29 #ifdef HAVE_SYS_TIME_H
32 #ifdef HAVE_BACKTRACE_SYMBOLS
36 typedef struct _ProfilerDesc ProfilerDesc;
37 struct _ProfilerDesc {
39 MonoProfiler *profiler;
40 MonoProfileFlags events;
42 MonoProfileAppDomainFunc domain_start_load;
43 MonoProfileAppDomainResult domain_end_load;
44 MonoProfileAppDomainFunc domain_start_unload;
45 MonoProfileAppDomainFunc domain_end_unload;
47 MonoProfileAssemblyFunc assembly_start_load;
48 MonoProfileAssemblyResult assembly_end_load;
49 MonoProfileAssemblyFunc assembly_start_unload;
50 MonoProfileAssemblyFunc assembly_end_unload;
52 MonoProfileModuleFunc module_start_load;
53 MonoProfileModuleResult module_end_load;
54 MonoProfileModuleFunc module_start_unload;
55 MonoProfileModuleFunc module_end_unload;
57 MonoProfileClassFunc class_start_load;
58 MonoProfileClassResult class_end_load;
59 MonoProfileClassFunc class_start_unload;
60 MonoProfileClassFunc class_end_unload;
62 MonoProfileMethodFunc jit_start;
63 MonoProfileMethodResult jit_end;
64 MonoProfileJitResult jit_end2;
65 MonoProfileMethodFunc method_free;
66 MonoProfileMethodFunc method_start_invoke;
67 MonoProfileMethodFunc method_end_invoke;
68 MonoProfileMethodResult man_unman_transition;
69 MonoProfileAllocFunc allocation_cb;
70 MonoProfileMonitorFunc monitor_event_cb;
71 MonoProfileStatFunc statistical_cb;
72 MonoProfileStatCallChainFunc statistical_call_chain_cb;
73 int statistical_call_chain_depth;
74 MonoProfilerCallChainStrategy statistical_call_chain_strategy;
75 MonoProfileMethodFunc method_enter;
76 MonoProfileMethodFunc method_leave;
78 MonoProfileExceptionFunc exception_throw_cb;
79 MonoProfileMethodFunc exception_method_leave_cb;
80 MonoProfileExceptionClauseFunc exception_clause_cb;
82 MonoProfileIomapFunc iomap_cb;
84 MonoProfileThreadFunc thread_start;
85 MonoProfileThreadFunc thread_end;
86 MonoProfileThreadNameFunc thread_name;
88 MonoProfileCoverageFilterFunc coverage_filter_cb;
90 MonoProfileFunc shutdown_callback;
92 MonoProfileGCFunc gc_event;
93 MonoProfileGCResizeFunc gc_heap_resize;
94 MonoProfileGCMoveFunc gc_moves;
95 MonoProfileGCHandleFunc gc_handle;
96 MonoProfileGCRootFunc gc_roots;
98 MonoProfileFunc runtime_initialized_event;
100 MonoProfilerCodeChunkNew code_chunk_new;
101 MonoProfilerCodeChunkDestroy code_chunk_destroy;
102 MonoProfilerCodeBufferNew code_buffer_new;
105 static ProfilerDesc *prof_list = NULL;
107 #define mono_profiler_coverage_lock() mono_mutex_lock (&profiler_coverage_mutex)
108 #define mono_profiler_coverage_unlock() mono_mutex_unlock (&profiler_coverage_mutex)
109 static mono_mutex_t profiler_coverage_mutex;
111 /* this is directly accessible to other mono libs.
112 * It is the ORed value of all the profiler's events.
114 MonoProfileFlags mono_profiler_events;
117 * mono_profiler_install:
118 * @prof: a MonoProfiler structure pointer, or a pointer to a derived structure.
119 * @version: profiler API version (see profiler.h)
120 * @callback: the function to invoke at shutdown
122 * Use mono_profiler_install to activate profiling in the Mono runtime.
123 * Typically developers of new profilers will create a new structure whose
124 * first field is a MonoProfiler and put any extra information that they need
125 * to access from the various profiling callbacks there.
129 mono_profiler_install (MonoProfiler *prof, int version, MonoProfileFunc callback)
131 if (version != MONO_PROFILER_VERSION)
132 g_warning ("Profiler module API version (%i) potentially incompatible with current API version (%i)", version, MONO_PROFILER_VERSION);
134 ProfilerDesc *desc = g_new0 (ProfilerDesc, 1);
136 mono_mutex_init_recursive (&profiler_coverage_mutex);
137 desc->profiler = prof;
138 desc->shutdown_callback = callback;
139 desc->next = prof_list;
144 * mono_profiler_set_events:
145 * @events: an ORed set of values made up of MONO_PROFILER_ flags
147 * The events descriped in the @events argument is a set of flags
148 * that represent which profiling events must be triggered. For
149 * example if you have registered a set of methods for tracking
150 * JIT compilation start and end with mono_profiler_install_jit_compile,
151 * you will want to pass the MONO_PROFILE_JIT_COMPILATION flag to
154 * You can call mono_profile_set_events more than once and you can
155 * do this at runtime to modify which methods are invoked.
158 mono_profiler_set_events (MonoProfileFlags events)
161 MonoProfileFlags value = 0;
163 prof_list->events = events;
164 for (prof = prof_list; prof; prof = prof->next)
165 value |= prof->events;
166 mono_profiler_events = value;
170 * mono_profiler_get_events:
172 * Returns a list of active events that will be intercepted.
175 mono_profiler_get_events (void)
177 return mono_profiler_events;
181 * mono_profiler_install_enter_leave:
182 * @enter: the routine to be called on each method entry
183 * @fleave: the routine to be called each time a method returns
185 * Use this routine to install routines that will be called everytime
186 * a method enters and leaves. The routines will receive as an argument
187 * the MonoMethod representing the method that is entering or leaving.
190 mono_profiler_install_enter_leave (MonoProfileMethodFunc enter, MonoProfileMethodFunc fleave)
194 prof_list->method_enter = enter;
195 prof_list->method_leave = fleave;
199 * mono_profiler_install_jit_compile:
200 * @start: the routine to be called when the JIT process starts.
201 * @end: the routine to be called when the JIT process ends.
203 * Use this routine to install routines that will be called when JIT
204 * compilation of a method starts and completes.
207 mono_profiler_install_jit_compile (MonoProfileMethodFunc start, MonoProfileMethodResult end)
211 prof_list->jit_start = start;
212 prof_list->jit_end = end;
216 mono_profiler_install_jit_end (MonoProfileJitResult end)
220 prof_list->jit_end2 = end;
224 mono_profiler_install_method_free (MonoProfileMethodFunc callback)
228 prof_list->method_free = callback;
232 mono_profiler_install_method_invoke (MonoProfileMethodFunc start, MonoProfileMethodFunc end)
236 prof_list->method_start_invoke = start;
237 prof_list->method_end_invoke = end;
241 mono_profiler_install_thread (MonoProfileThreadFunc start, MonoProfileThreadFunc end)
245 prof_list->thread_start = start;
246 prof_list->thread_end = end;
250 mono_profiler_install_thread_name (MonoProfileThreadNameFunc thread_name_cb)
254 prof_list->thread_name = thread_name_cb;
258 mono_profiler_install_transition (MonoProfileMethodResult callback)
262 prof_list->man_unman_transition = callback;
266 mono_profiler_install_allocation (MonoProfileAllocFunc callback)
270 prof_list->allocation_cb = callback;
274 mono_profiler_install_monitor (MonoProfileMonitorFunc callback)
278 prof_list->monitor_event_cb = callback;
281 static MonoProfileSamplingMode sampling_mode = MONO_PROFILER_STAT_MODE_PROCESS;
282 static int64_t sampling_frequency = 1000; //1ms
285 * mono_profiler_set_statistical_mode:
286 * @mode the sampling mode used.
287 * @sample_frequency_is_us the sampling frequency in microseconds.
289 * Set the sampling parameters for the profiler. Sampling mode affects the effective sampling rate as in samples/s you'll witness.
290 * The default sampling mode is process mode, which only reports samples when there's activity in the process.
292 * Sampling frequency should be interpreted as a suggestion that can't always be honored due to how most kernels expose alarms.
294 * Said that, when using statistical sampling, always assume variable rate sampling as all sort of external factors can interfere.
297 mono_profiler_set_statistical_mode (MonoProfileSamplingMode mode, int64_t sampling_frequency_is_us)
299 sampling_mode = mode;
300 sampling_frequency = sampling_frequency_is_us;
304 mono_profiler_install_statistical (MonoProfileStatFunc callback)
308 prof_list->statistical_cb = callback;
312 mono_profiler_get_sampling_rate (void)
314 return sampling_frequency;
317 MonoProfileSamplingMode
318 mono_profiler_get_sampling_mode (void)
320 return sampling_mode;
324 mono_profiler_install_statistical_call_chain (MonoProfileStatCallChainFunc callback, int call_chain_depth, MonoProfilerCallChainStrategy call_chain_strategy) {
327 if (call_chain_depth > MONO_PROFILER_MAX_STAT_CALL_CHAIN_DEPTH) {
328 call_chain_depth = MONO_PROFILER_MAX_STAT_CALL_CHAIN_DEPTH;
330 if ((call_chain_strategy >= MONO_PROFILER_CALL_CHAIN_INVALID) || (call_chain_strategy < MONO_PROFILER_CALL_CHAIN_NONE)) {
331 call_chain_strategy = MONO_PROFILER_CALL_CHAIN_NONE;
333 prof_list->statistical_call_chain_cb = callback;
334 prof_list->statistical_call_chain_depth = call_chain_depth;
335 prof_list->statistical_call_chain_strategy = call_chain_strategy;
339 mono_profiler_stat_get_call_chain_depth (void) {
340 if (prof_list && prof_list->statistical_call_chain_cb != NULL) {
341 return prof_list->statistical_call_chain_depth;
347 MonoProfilerCallChainStrategy
348 mono_profiler_stat_get_call_chain_strategy (void) {
349 if (prof_list && prof_list->statistical_call_chain_cb != NULL) {
350 return prof_list->statistical_call_chain_strategy;
352 return MONO_PROFILER_CALL_CHAIN_NONE;
356 void mono_profiler_install_exception (MonoProfileExceptionFunc throw_callback, MonoProfileMethodFunc exc_method_leave, MonoProfileExceptionClauseFunc clause_callback)
360 prof_list->exception_throw_cb = throw_callback;
361 prof_list->exception_method_leave_cb = exc_method_leave;
362 prof_list->exception_clause_cb = clause_callback;
366 mono_profiler_install_coverage_filter (MonoProfileCoverageFilterFunc callback)
370 prof_list->coverage_filter_cb = callback;
374 mono_profiler_install_appdomain (MonoProfileAppDomainFunc start_load, MonoProfileAppDomainResult end_load,
375 MonoProfileAppDomainFunc start_unload, MonoProfileAppDomainFunc end_unload)
380 prof_list->domain_start_load = start_load;
381 prof_list->domain_end_load = end_load;
382 prof_list->domain_start_unload = start_unload;
383 prof_list->domain_end_unload = end_unload;
387 mono_profiler_install_assembly (MonoProfileAssemblyFunc start_load, MonoProfileAssemblyResult end_load,
388 MonoProfileAssemblyFunc start_unload, MonoProfileAssemblyFunc end_unload)
392 prof_list->assembly_start_load = start_load;
393 prof_list->assembly_end_load = end_load;
394 prof_list->assembly_start_unload = start_unload;
395 prof_list->assembly_end_unload = end_unload;
399 mono_profiler_install_module (MonoProfileModuleFunc start_load, MonoProfileModuleResult end_load,
400 MonoProfileModuleFunc start_unload, MonoProfileModuleFunc end_unload)
404 prof_list->module_start_load = start_load;
405 prof_list->module_end_load = end_load;
406 prof_list->module_start_unload = start_unload;
407 prof_list->module_end_unload = end_unload;
411 mono_profiler_install_class (MonoProfileClassFunc start_load, MonoProfileClassResult end_load,
412 MonoProfileClassFunc start_unload, MonoProfileClassFunc end_unload)
416 prof_list->class_start_load = start_load;
417 prof_list->class_end_load = end_load;
418 prof_list->class_start_unload = start_unload;
419 prof_list->class_end_unload = end_unload;
423 mono_profiler_method_enter (MonoMethod *method)
426 for (prof = prof_list; prof; prof = prof->next) {
427 if ((prof->events & MONO_PROFILE_ENTER_LEAVE) && prof->method_enter)
428 prof->method_enter (prof->profiler, method);
433 mono_profiler_method_leave (MonoMethod *method)
436 for (prof = prof_list; prof; prof = prof->next) {
437 if ((prof->events & MONO_PROFILE_ENTER_LEAVE) && prof->method_leave)
438 prof->method_leave (prof->profiler, method);
443 mono_profiler_method_jit (MonoMethod *method)
446 for (prof = prof_list; prof; prof = prof->next) {
447 if ((prof->events & MONO_PROFILE_JIT_COMPILATION) && prof->jit_start)
448 prof->jit_start (prof->profiler, method);
453 mono_profiler_method_end_jit (MonoMethod *method, MonoJitInfo* jinfo, int result)
456 for (prof = prof_list; prof; prof = prof->next) {
457 if ((prof->events & MONO_PROFILE_JIT_COMPILATION)) {
459 prof->jit_end (prof->profiler, method, result);
461 prof->jit_end2 (prof->profiler, method, jinfo, result);
467 mono_profiler_method_free (MonoMethod *method)
470 for (prof = prof_list; prof; prof = prof->next) {
471 if ((prof->events & MONO_PROFILE_METHOD_EVENTS) && prof->method_free)
472 prof->method_free (prof->profiler, method);
477 mono_profiler_method_start_invoke (MonoMethod *method)
480 for (prof = prof_list; prof; prof = prof->next) {
481 if ((prof->events & MONO_PROFILE_METHOD_EVENTS) && prof->method_start_invoke)
482 prof->method_start_invoke (prof->profiler, method);
487 mono_profiler_method_end_invoke (MonoMethod *method)
490 for (prof = prof_list; prof; prof = prof->next) {
491 if ((prof->events & MONO_PROFILE_METHOD_EVENTS) && prof->method_end_invoke)
492 prof->method_end_invoke (prof->profiler, method);
497 mono_profiler_code_transition (MonoMethod *method, int result)
500 for (prof = prof_list; prof; prof = prof->next) {
501 if ((prof->events & MONO_PROFILE_TRANSITIONS) && prof->man_unman_transition)
502 prof->man_unman_transition (prof->profiler, method, result);
507 mono_profiler_allocation (MonoObject *obj, MonoClass *klass)
510 for (prof = prof_list; prof; prof = prof->next) {
511 if ((prof->events & MONO_PROFILE_ALLOCATIONS) && prof->allocation_cb)
512 prof->allocation_cb (prof->profiler, obj, klass);
517 mono_profiler_monitor_event (MonoObject *obj, MonoProfilerMonitorEvent event) {
519 for (prof = prof_list; prof; prof = prof->next) {
520 if ((prof->events & MONO_PROFILE_MONITOR_EVENTS) && prof->monitor_event_cb)
521 prof->monitor_event_cb (prof->profiler, obj, event);
526 mono_profiler_stat_hit (guchar *ip, void *context)
529 for (prof = prof_list; prof; prof = prof->next) {
530 if ((prof->events & MONO_PROFILE_STATISTICAL) && prof->statistical_cb)
531 prof->statistical_cb (prof->profiler, ip, context);
536 mono_profiler_stat_call_chain (int call_chain_depth, guchar **ips, void *context)
539 for (prof = prof_list; prof; prof = prof->next) {
540 if ((prof->events & MONO_PROFILE_STATISTICAL) && prof->statistical_call_chain_cb)
541 prof->statistical_call_chain_cb (prof->profiler, call_chain_depth, ips, context);
546 mono_profiler_exception_thrown (MonoObject *exception)
549 for (prof = prof_list; prof; prof = prof->next) {
550 if ((prof->events & MONO_PROFILE_EXCEPTIONS) && prof->exception_throw_cb)
551 prof->exception_throw_cb (prof->profiler, exception);
556 mono_profiler_exception_method_leave (MonoMethod *method)
559 for (prof = prof_list; prof; prof = prof->next) {
560 if ((prof->events & MONO_PROFILE_EXCEPTIONS) && prof->exception_method_leave_cb)
561 prof->exception_method_leave_cb (prof->profiler, method);
566 mono_profiler_exception_clause_handler (MonoMethod *method, int clause_type, int clause_num)
569 for (prof = prof_list; prof; prof = prof->next) {
570 if ((prof->events & MONO_PROFILE_EXCEPTIONS) && prof->exception_clause_cb)
571 prof->exception_clause_cb (prof->profiler, method, clause_type, clause_num);
576 mono_profiler_thread_start (gsize tid)
579 for (prof = prof_list; prof; prof = prof->next) {
580 if ((prof->events & MONO_PROFILE_THREADS) && prof->thread_start)
581 prof->thread_start (prof->profiler, tid);
586 mono_profiler_thread_end (gsize tid)
589 for (prof = prof_list; prof; prof = prof->next) {
590 if ((prof->events & MONO_PROFILE_THREADS) && prof->thread_end)
591 prof->thread_end (prof->profiler, tid);
596 mono_profiler_thread_name (gsize tid, const char *name)
599 for (prof = prof_list; prof; prof = prof->next) {
600 if ((prof->events & MONO_PROFILE_THREADS) && prof->thread_name)
601 prof->thread_name (prof->profiler, tid, name);
606 mono_profiler_assembly_event (MonoAssembly *assembly, int code)
609 for (prof = prof_list; prof; prof = prof->next) {
610 if (!(prof->events & MONO_PROFILE_ASSEMBLY_EVENTS))
614 case MONO_PROFILE_START_LOAD:
615 if (prof->assembly_start_load)
616 prof->assembly_start_load (prof->profiler, assembly);
618 case MONO_PROFILE_START_UNLOAD:
619 if (prof->assembly_start_unload)
620 prof->assembly_start_unload (prof->profiler, assembly);
622 case MONO_PROFILE_END_UNLOAD:
623 if (prof->assembly_end_unload)
624 prof->assembly_end_unload (prof->profiler, assembly);
627 g_assert_not_reached ();
633 mono_profiler_assembly_loaded (MonoAssembly *assembly, int result)
636 for (prof = prof_list; prof; prof = prof->next) {
637 if ((prof->events & MONO_PROFILE_ASSEMBLY_EVENTS) && prof->assembly_end_load)
638 prof->assembly_end_load (prof->profiler, assembly, result);
642 void mono_profiler_iomap (char *report, const char *pathname, const char *new_pathname)
645 for (prof = prof_list; prof; prof = prof->next) {
646 if ((prof->events & MONO_PROFILE_IOMAP_EVENTS) && prof->iomap_cb)
647 prof->iomap_cb (prof->profiler, report, pathname, new_pathname);
652 mono_profiler_module_event (MonoImage *module, int code)
655 for (prof = prof_list; prof; prof = prof->next) {
656 if (!(prof->events & MONO_PROFILE_MODULE_EVENTS))
660 case MONO_PROFILE_START_LOAD:
661 if (prof->module_start_load)
662 prof->module_start_load (prof->profiler, module);
664 case MONO_PROFILE_START_UNLOAD:
665 if (prof->module_start_unload)
666 prof->module_start_unload (prof->profiler, module);
668 case MONO_PROFILE_END_UNLOAD:
669 if (prof->module_end_unload)
670 prof->module_end_unload (prof->profiler, module);
673 g_assert_not_reached ();
679 mono_profiler_module_loaded (MonoImage *module, int result)
682 for (prof = prof_list; prof; prof = prof->next) {
683 if ((prof->events & MONO_PROFILE_MODULE_EVENTS) && prof->module_end_load)
684 prof->module_end_load (prof->profiler, module, result);
689 mono_profiler_class_event (MonoClass *klass, int code)
692 for (prof = prof_list; prof; prof = prof->next) {
693 if (!(prof->events & MONO_PROFILE_CLASS_EVENTS))
697 case MONO_PROFILE_START_LOAD:
698 if (prof->class_start_load)
699 prof->class_start_load (prof->profiler, klass);
701 case MONO_PROFILE_START_UNLOAD:
702 if (prof->class_start_unload)
703 prof->class_start_unload (prof->profiler, klass);
705 case MONO_PROFILE_END_UNLOAD:
706 if (prof->class_end_unload)
707 prof->class_end_unload (prof->profiler, klass);
710 g_assert_not_reached ();
716 mono_profiler_class_loaded (MonoClass *klass, int result)
719 for (prof = prof_list; prof; prof = prof->next) {
720 if ((prof->events & MONO_PROFILE_CLASS_EVENTS) && prof->class_end_load)
721 prof->class_end_load (prof->profiler, klass, result);
726 mono_profiler_appdomain_event (MonoDomain *domain, int code)
729 for (prof = prof_list; prof; prof = prof->next) {
730 if (!(prof->events & MONO_PROFILE_APPDOMAIN_EVENTS))
734 case MONO_PROFILE_START_LOAD:
735 if (prof->domain_start_load)
736 prof->domain_start_load (prof->profiler, domain);
738 case MONO_PROFILE_START_UNLOAD:
739 if (prof->domain_start_unload)
740 prof->domain_start_unload (prof->profiler, domain);
742 case MONO_PROFILE_END_UNLOAD:
743 if (prof->domain_end_unload)
744 prof->domain_end_unload (prof->profiler, domain);
747 g_assert_not_reached ();
753 mono_profiler_appdomain_loaded (MonoDomain *domain, int result)
756 for (prof = prof_list; prof; prof = prof->next) {
757 if ((prof->events & MONO_PROFILE_APPDOMAIN_EVENTS) && prof->domain_end_load)
758 prof->domain_end_load (prof->profiler, domain, result);
763 mono_profiler_shutdown (void)
766 for (prof = prof_list; prof; prof = prof->next) {
767 if (prof->shutdown_callback)
768 prof->shutdown_callback (prof->profiler);
771 mono_profiler_set_events (0);
775 mono_profiler_gc_heap_resize (gint64 new_size)
778 for (prof = prof_list; prof; prof = prof->next) {
779 if ((prof->events & MONO_PROFILE_GC) && prof->gc_heap_resize)
780 prof->gc_heap_resize (prof->profiler, new_size);
785 mono_profiler_gc_event (MonoGCEvent event, int generation)
788 for (prof = prof_list; prof; prof = prof->next) {
789 if ((prof->events & MONO_PROFILE_GC) && prof->gc_event)
790 prof->gc_event (prof->profiler, event, generation);
795 mono_profiler_gc_moves (void **objects, int num)
798 for (prof = prof_list; prof; prof = prof->next) {
799 if ((prof->events & MONO_PROFILE_GC_MOVES) && prof->gc_moves)
800 prof->gc_moves (prof->profiler, objects, num);
805 mono_profiler_gc_handle (int op, int type, uintptr_t handle, MonoObject *obj)
808 for (prof = prof_list; prof; prof = prof->next) {
809 if ((prof->events & MONO_PROFILE_GC_ROOTS) && prof->gc_handle)
810 prof->gc_handle (prof->profiler, op, type, handle, obj);
815 mono_profiler_gc_roots (int num, void **objects, int *root_types, uintptr_t *extra_info)
818 for (prof = prof_list; prof; prof = prof->next) {
819 if ((prof->events & MONO_PROFILE_GC_ROOTS) && prof->gc_roots)
820 prof->gc_roots (prof->profiler, num, objects, root_types, extra_info);
825 mono_profiler_install_gc (MonoProfileGCFunc callback, MonoProfileGCResizeFunc heap_resize_callback)
827 mono_gc_enable_events ();
830 prof_list->gc_event = callback;
831 prof_list->gc_heap_resize = heap_resize_callback;
835 * mono_profiler_install_gc_moves:
836 * @callback: callback function
838 * Install the @callback function that the GC will call when moving objects.
839 * The callback receives an array of pointers and the number of elements
840 * in the array. Every even element in the array is the original object location
841 * and the following odd element is the new location of the object in memory.
842 * So the number of elements argument will always be a multiple of 2.
843 * Since this callback happens during the GC, it is a restricted environment:
844 * no locks can be taken and the object pointers can be inspected only once
845 * the GC is finished (of course the original location pointers will not
846 * point to valid objects anymore).
849 mono_profiler_install_gc_moves (MonoProfileGCMoveFunc callback)
853 prof_list->gc_moves = callback;
857 * mono_profiler_install_gc_roots:
858 * @handle_callback: callback function
859 * @roots_callback: callback function
861 * Install the @handle_callback function that the GC will call when GC
862 * handles are created or destroyed.
863 * The callback receives an operation, which is either #MONO_PROFILER_GC_HANDLE_CREATED
864 * or #MONO_PROFILER_GC_HANDLE_DESTROYED, the handle type, the handle value and the
865 * object pointer, if present.
866 * Install the @roots_callback function that the GC will call when tracing
867 * the roots for a collection.
868 * The callback receives the number of elements and three arrays: an array
869 * of objects, an array of root types and flags and an array of extra info.
870 * The size of each array is given by the first argument.
873 mono_profiler_install_gc_roots (MonoProfileGCHandleFunc handle_callback, MonoProfileGCRootFunc roots_callback)
877 prof_list->gc_handle = handle_callback;
878 prof_list->gc_roots = roots_callback;
882 mono_profiler_install_runtime_initialized (MonoProfileFunc runtime_initialized_callback)
886 prof_list->runtime_initialized_event = runtime_initialized_callback;
890 mono_profiler_runtime_initialized (void) {
892 for (prof = prof_list; prof; prof = prof->next) {
893 if (prof->runtime_initialized_event)
894 prof->runtime_initialized_event (prof->profiler);
899 mono_profiler_install_code_chunk_new (MonoProfilerCodeChunkNew callback) {
902 prof_list->code_chunk_new = callback;
905 mono_profiler_code_chunk_new (gpointer chunk, int size) {
907 for (prof = prof_list; prof; prof = prof->next) {
908 if (prof->code_chunk_new)
909 prof->code_chunk_new (prof->profiler, chunk, size);
914 mono_profiler_install_code_chunk_destroy (MonoProfilerCodeChunkDestroy callback) {
917 prof_list->code_chunk_destroy = callback;
920 mono_profiler_code_chunk_destroy (gpointer chunk) {
922 for (prof = prof_list; prof; prof = prof->next) {
923 if (prof->code_chunk_destroy)
924 prof->code_chunk_destroy (prof->profiler, chunk);
929 mono_profiler_install_code_buffer_new (MonoProfilerCodeBufferNew callback) {
932 prof_list->code_buffer_new = callback;
936 mono_profiler_install_iomap (MonoProfileIomapFunc callback)
940 prof_list->iomap_cb = callback;
944 mono_profiler_code_buffer_new (gpointer buffer, int size, MonoProfilerCodeBufferType type, gconstpointer data) {
946 for (prof = prof_list; prof; prof = prof->next) {
947 if (prof->code_buffer_new)
948 prof->code_buffer_new (prof->profiler, buffer, size, type, (void*)data);
952 static GHashTable *coverage_hash = NULL;
954 MonoProfileCoverageInfo*
955 mono_profiler_coverage_alloc (MonoMethod *method, int entries)
957 MonoProfileCoverageInfo *res;
958 int instrument = FALSE;
961 for (prof = prof_list; prof; prof = prof->next) {
962 /* note that we call the filter on all the profilers even if just
963 * a single one would be enough to instrument a method
965 if (prof->coverage_filter_cb)
966 if (prof->coverage_filter_cb (prof->profiler, method))
972 mono_profiler_coverage_lock ();
974 coverage_hash = g_hash_table_new (NULL, NULL);
976 res = g_malloc0 (sizeof (MonoProfileCoverageInfo) + sizeof (void*) * 2 * entries);
978 res->entries = entries;
980 g_hash_table_insert (coverage_hash, method, res);
981 mono_profiler_coverage_unlock ();
986 /* safe only when the method antive code has been unloaded */
988 mono_profiler_coverage_free (MonoMethod *method)
990 MonoProfileCoverageInfo* info;
992 mono_profiler_coverage_lock ();
993 if (!coverage_hash) {
994 mono_profiler_coverage_unlock ();
998 info = g_hash_table_lookup (coverage_hash, method);
1001 g_hash_table_remove (coverage_hash, method);
1003 mono_profiler_coverage_unlock ();
1007 * mono_profiler_coverage_get:
1008 * @prof: The profiler handle, installed with mono_profiler_install
1009 * @method: the method to gather information from.
1010 * @func: A routine that will be called back with the results
1012 * If the MONO_PROFILER_INS_COVERAGE flag was active during JIT compilation
1013 * it is posisble to obtain coverage information about a give method.
1015 * The function @func will be invoked repeatedly with instances of the
1016 * MonoProfileCoverageEntry structure.
1019 mono_profiler_coverage_get (MonoProfiler *prof, MonoMethod *method, MonoProfileCoverageFunc func)
1021 MonoProfileCoverageInfo* info = NULL;
1024 const unsigned char *start, *end, *cil_code;
1025 MonoMethodHeader *header;
1026 MonoProfileCoverageEntry entry;
1027 MonoDebugMethodInfo *debug_minfo;
1029 mono_profiler_coverage_lock ();
1031 info = g_hash_table_lookup (coverage_hash, method);
1032 mono_profiler_coverage_unlock ();
1037 header = mono_method_get_header (method);
1038 start = mono_method_header_get_code (header, &code_size, NULL);
1039 debug_minfo = mono_debug_lookup_method (method);
1041 end = start + code_size;
1042 for (i = 0; i < info->entries; ++i) {
1043 cil_code = info->data [i].cil_code;
1044 if (cil_code && cil_code >= start && cil_code < end) {
1046 offset = cil_code - start;
1047 entry.iloffset = offset;
1048 entry.method = method;
1049 entry.counter = info->data [i].count;
1050 entry.line = entry.col = 1;
1051 entry.filename = NULL;
1053 MonoDebugSourceLocation *location;
1055 location = mono_debug_symfile_lookup_location (debug_minfo, offset);
1057 entry.line = location->row;
1058 entry.col = location->column;
1059 entry.filename = fname = g_strdup (location->source_file);
1060 mono_debug_free_source_location (location);
1064 func (prof, &entry);
1068 mono_metadata_free_mh (header);
1071 typedef void (*ProfilerInitializer) (const char*);
1072 #define INITIALIZER_NAME "mono_profiler_startup"
1076 load_profiler (MonoDl *pmodule, const char *desc, const char *symbol)
1079 ProfilerInitializer func;
1084 if ((err = mono_dl_symbol (pmodule, symbol, (gpointer *) &func))) {
1094 load_embedded_profiler (const char *desc, const char *name)
1098 MonoDl *pmodule = NULL;
1101 pmodule = mono_dl_open (NULL, MONO_DL_LAZY, &err);
1103 g_warning ("Could not open main executable (%s)", err);
1108 symbol = g_strdup_printf (INITIALIZER_NAME "_%s", name);
1109 result = load_profiler (pmodule, desc, symbol);
1116 load_profiler_from_directory (const char *directory, const char *libname, const char *desc)
1118 MonoDl *pmodule = NULL;
1125 while ((path = mono_dl_build_path (directory, libname, &iter))) {
1126 pmodule = mono_dl_open (path, MONO_DL_LAZY, &err);
1130 return load_profiler (pmodule, desc, INITIALIZER_NAME);
1137 load_profiler_from_mono_instalation (const char *libname, const char *desc)
1140 MonoDl *pmodule = mono_dl_open_runtime_lib (libname, MONO_DL_LAZY, &err);
1143 return load_profiler (pmodule, desc, INITIALIZER_NAME);
1148 * mono_profiler_load:
1149 * @desc: arguments to configure the profiler
1151 * Invoke this method to initialize the profiler. This will drive the
1152 * loading of the internal ("default") or any external profilers.
1154 * This routine is invoked by Mono's driver, but must be called manually
1155 * if you embed Mono into your application.
1158 mono_profiler_load (const char *desc)
1161 mono_gc_base_init ();
1163 if (!desc || (strcmp ("default", desc) == 0)) {
1164 desc = "log:report";
1166 /* we keep command-line compat with the old version here */
1167 if (strncmp (desc, "default:", 8) == 0) {
1168 gchar **args, **ptr;
1169 GString *str = g_string_new ("log:report");
1170 args = g_strsplit (desc + 8, ",", -1);
1171 for (ptr = args; ptr && *ptr; ptr++) {
1172 const char *arg = *ptr;
1174 if (!strcmp (arg, "time"))
1175 g_string_append (str, ",calls");
1176 else if (!strcmp (arg, "alloc"))
1177 g_string_append (str, ",alloc");
1178 else if (!strcmp (arg, "stat"))
1179 g_string_append (str, ",sample");
1180 else if (!strcmp (arg, "jit"))
1181 continue; /* accept and do nothing */
1182 else if (strncmp (arg, "file=", 5) == 0) {
1183 g_string_append_printf (str, ",output=%s", arg + 5);
1185 fprintf (stderr, "profiler : Unknown argument '%s'.\n", arg);
1189 desc = cdesc = g_string_free (str, FALSE);
1192 const char* col = strchr (desc, ':');
1195 gboolean res = FALSE;
1198 mname = g_memdup (desc, col - desc + 1);
1199 mname [col - desc] = 0;
1201 mname = g_strdup (desc);
1203 if (!load_embedded_profiler (desc, mname)) {
1204 libname = g_strdup_printf ("mono-profiler-%s", mname);
1205 if (mono_config_get_assemblies_dir ())
1206 res = load_profiler_from_directory (mono_assembly_getrootdir (), libname, desc);
1208 res = load_profiler_from_directory (NULL, libname, desc);
1210 res = load_profiler_from_mono_instalation (libname, desc);
1212 g_warning ("The '%s' profiler wasn't found in the main executable nor could it be loaded from '%s'.", mname, libname);