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/io-layer/io-layer.h"
23 #include "mono/utils/mono-dl.h"
28 #ifdef HAVE_SYS_TIME_H
31 #ifdef HAVE_BACKTRACE_SYMBOLS
35 typedef struct _ProfilerDesc ProfilerDesc;
36 struct _ProfilerDesc {
38 MonoProfiler *profiler;
39 MonoProfileFlags events;
41 MonoProfileAppDomainFunc domain_start_load;
42 MonoProfileAppDomainResult domain_end_load;
43 MonoProfileAppDomainFunc domain_start_unload;
44 MonoProfileAppDomainFunc domain_end_unload;
46 MonoProfileAssemblyFunc assembly_start_load;
47 MonoProfileAssemblyResult assembly_end_load;
48 MonoProfileAssemblyFunc assembly_start_unload;
49 MonoProfileAssemblyFunc assembly_end_unload;
51 MonoProfileModuleFunc module_start_load;
52 MonoProfileModuleResult module_end_load;
53 MonoProfileModuleFunc module_start_unload;
54 MonoProfileModuleFunc module_end_unload;
56 MonoProfileClassFunc class_start_load;
57 MonoProfileClassResult class_end_load;
58 MonoProfileClassFunc class_start_unload;
59 MonoProfileClassFunc class_end_unload;
61 MonoProfileMethodFunc jit_start;
62 MonoProfileMethodResult jit_end;
63 MonoProfileJitResult jit_end2;
64 MonoProfileMethodFunc method_free;
65 MonoProfileMethodFunc method_start_invoke;
66 MonoProfileMethodFunc method_end_invoke;
67 MonoProfileMethodResult man_unman_transition;
68 MonoProfileAllocFunc allocation_cb;
69 MonoProfileMonitorFunc monitor_event_cb;
70 MonoProfileStatFunc statistical_cb;
71 MonoProfileStatCallChainFunc statistical_call_chain_cb;
72 int statistical_call_chain_depth;
73 MonoProfilerCallChainStrategy statistical_call_chain_strategy;
74 MonoProfileMethodFunc method_enter;
75 MonoProfileMethodFunc method_leave;
77 MonoProfileExceptionFunc exception_throw_cb;
78 MonoProfileMethodFunc exception_method_leave_cb;
79 MonoProfileExceptionClauseFunc exception_clause_cb;
81 MonoProfileIomapFunc iomap_cb;
83 MonoProfileThreadFunc thread_start;
84 MonoProfileThreadFunc thread_end;
85 MonoProfileThreadNameFunc thread_name;
87 MonoProfileCoverageFilterFunc coverage_filter_cb;
89 MonoProfileFunc shutdown_callback;
91 MonoProfileGCFunc gc_event;
92 MonoProfileGCResizeFunc gc_heap_resize;
93 MonoProfileGCMoveFunc gc_moves;
94 MonoProfileGCHandleFunc gc_handle;
95 MonoProfileGCRootFunc gc_roots;
97 MonoProfileFunc runtime_initialized_event;
99 MonoProfilerCodeChunkNew code_chunk_new;
100 MonoProfilerCodeChunkDestroy code_chunk_destroy;
101 MonoProfilerCodeBufferNew code_buffer_new;
104 static ProfilerDesc *prof_list = NULL;
106 #define mono_profiler_coverage_lock() mono_mutex_lock (&profiler_coverage_mutex)
107 #define mono_profiler_coverage_unlock() mono_mutex_unlock (&profiler_coverage_mutex)
108 static mono_mutex_t profiler_coverage_mutex;
110 /* this is directly accessible to other mono libs.
111 * It is the ORed value of all the profiler's events.
113 MonoProfileFlags mono_profiler_events;
116 * mono_profiler_install:
117 * @prof: a MonoProfiler structure pointer, or a pointer to a derived structure.
118 * @callback: the function to invoke at shutdown
120 * Use mono_profiler_install to activate profiling in the Mono runtime.
121 * Typically developers of new profilers will create a new structure whose
122 * first field is a MonoProfiler and put any extra information that they need
123 * to access from the various profiling callbacks there.
127 mono_profiler_install (MonoProfiler *prof, MonoProfileFunc callback)
129 ProfilerDesc *desc = g_new0 (ProfilerDesc, 1);
131 mono_mutex_init_recursive (&profiler_coverage_mutex);
132 desc->profiler = prof;
133 desc->shutdown_callback = callback;
134 desc->next = prof_list;
139 * mono_profiler_set_events:
140 * @events: an ORed set of values made up of MONO_PROFILER_ flags
142 * The events descriped in the @events argument is a set of flags
143 * that represent which profiling events must be triggered. For
144 * example if you have registered a set of methods for tracking
145 * JIT compilation start and end with mono_profiler_install_jit_compile,
146 * you will want to pass the MONO_PROFILE_JIT_COMPILATION flag to
149 * You can call mono_profile_set_events more than once and you can
150 * do this at runtime to modify which methods are invoked.
153 mono_profiler_set_events (MonoProfileFlags events)
156 MonoProfileFlags value = 0;
158 prof_list->events = events;
159 for (prof = prof_list; prof; prof = prof->next)
160 value |= prof->events;
161 mono_profiler_events = value;
165 * mono_profiler_get_events:
167 * Returns a list of active events that will be intercepted.
170 mono_profiler_get_events (void)
172 return mono_profiler_events;
176 * mono_profiler_install_enter_leave:
177 * @enter: the routine to be called on each method entry
178 * @fleave: the routine to be called each time a method returns
180 * Use this routine to install routines that will be called everytime
181 * a method enters and leaves. The routines will receive as an argument
182 * the MonoMethod representing the method that is entering or leaving.
185 mono_profiler_install_enter_leave (MonoProfileMethodFunc enter, MonoProfileMethodFunc fleave)
189 prof_list->method_enter = enter;
190 prof_list->method_leave = fleave;
194 * mono_profiler_install_jit_compile:
195 * @start: the routine to be called when the JIT process starts.
196 * @end: the routine to be called when the JIT process ends.
198 * Use this routine to install routines that will be called when JIT
199 * compilation of a method starts and completes.
202 mono_profiler_install_jit_compile (MonoProfileMethodFunc start, MonoProfileMethodResult end)
206 prof_list->jit_start = start;
207 prof_list->jit_end = end;
211 mono_profiler_install_jit_end (MonoProfileJitResult end)
215 prof_list->jit_end2 = end;
219 mono_profiler_install_method_free (MonoProfileMethodFunc callback)
223 prof_list->method_free = callback;
227 mono_profiler_install_method_invoke (MonoProfileMethodFunc start, MonoProfileMethodFunc end)
231 prof_list->method_start_invoke = start;
232 prof_list->method_end_invoke = end;
236 mono_profiler_install_thread (MonoProfileThreadFunc start, MonoProfileThreadFunc end)
240 prof_list->thread_start = start;
241 prof_list->thread_end = end;
245 mono_profiler_install_thread_name (MonoProfileThreadNameFunc thread_name_cb)
249 prof_list->thread_name = thread_name_cb;
253 mono_profiler_install_transition (MonoProfileMethodResult callback)
257 prof_list->man_unman_transition = callback;
261 mono_profiler_install_allocation (MonoProfileAllocFunc callback)
265 prof_list->allocation_cb = callback;
269 mono_profiler_install_monitor (MonoProfileMonitorFunc callback)
273 prof_list->monitor_event_cb = callback;
276 static MonoProfileSamplingMode sampling_mode = MONO_PROFILER_STAT_MODE_PROCESS;
277 static int64_t sampling_frequency = 1000; //1ms
280 * mono_profiler_set_statistical_mode:
281 * @mode the sampling mode used.
282 * @sample_frequency_is_us the sampling frequency in microseconds.
284 * Set the sampling parameters for the profiler. Sampling mode affects the effective sampling rate as in samples/s you'll witness.
285 * The default sampling mode is process mode, which only reports samples when there's activity in the process.
287 * Sampling frequency should be interpreted as a suggestion that can't always be honored due to how most kernels expose alarms.
289 * Said that, when using statistical sampling, always assume variable rate sampling as all sort of external factors can interfere.
292 mono_profiler_set_statistical_mode (MonoProfileSamplingMode mode, int64_t sampling_frequency_is_us)
294 sampling_mode = mode;
295 sampling_frequency = sampling_frequency_is_us;
299 mono_profiler_install_statistical (MonoProfileStatFunc callback)
303 prof_list->statistical_cb = callback;
307 mono_profiler_get_sampling_rate (void)
309 return sampling_frequency;
312 MonoProfileSamplingMode
313 mono_profiler_get_sampling_mode (void)
315 return sampling_mode;
319 mono_profiler_install_statistical_call_chain (MonoProfileStatCallChainFunc callback, int call_chain_depth, MonoProfilerCallChainStrategy call_chain_strategy) {
322 if (call_chain_depth > MONO_PROFILER_MAX_STAT_CALL_CHAIN_DEPTH) {
323 call_chain_depth = MONO_PROFILER_MAX_STAT_CALL_CHAIN_DEPTH;
325 if ((call_chain_strategy >= MONO_PROFILER_CALL_CHAIN_INVALID) || (call_chain_strategy < MONO_PROFILER_CALL_CHAIN_NONE)) {
326 call_chain_strategy = MONO_PROFILER_CALL_CHAIN_NONE;
328 prof_list->statistical_call_chain_cb = callback;
329 prof_list->statistical_call_chain_depth = call_chain_depth;
330 prof_list->statistical_call_chain_strategy = call_chain_strategy;
334 mono_profiler_stat_get_call_chain_depth (void) {
335 if (prof_list && prof_list->statistical_call_chain_cb != NULL) {
336 return prof_list->statistical_call_chain_depth;
342 MonoProfilerCallChainStrategy
343 mono_profiler_stat_get_call_chain_strategy (void) {
344 if (prof_list && prof_list->statistical_call_chain_cb != NULL) {
345 return prof_list->statistical_call_chain_strategy;
347 return MONO_PROFILER_CALL_CHAIN_NONE;
351 void mono_profiler_install_exception (MonoProfileExceptionFunc throw_callback, MonoProfileMethodFunc exc_method_leave, MonoProfileExceptionClauseFunc clause_callback)
355 prof_list->exception_throw_cb = throw_callback;
356 prof_list->exception_method_leave_cb = exc_method_leave;
357 prof_list->exception_clause_cb = clause_callback;
361 mono_profiler_install_coverage_filter (MonoProfileCoverageFilterFunc callback)
365 prof_list->coverage_filter_cb = callback;
369 mono_profiler_install_appdomain (MonoProfileAppDomainFunc start_load, MonoProfileAppDomainResult end_load,
370 MonoProfileAppDomainFunc start_unload, MonoProfileAppDomainFunc end_unload)
375 prof_list->domain_start_load = start_load;
376 prof_list->domain_end_load = end_load;
377 prof_list->domain_start_unload = start_unload;
378 prof_list->domain_end_unload = end_unload;
382 mono_profiler_install_assembly (MonoProfileAssemblyFunc start_load, MonoProfileAssemblyResult end_load,
383 MonoProfileAssemblyFunc start_unload, MonoProfileAssemblyFunc end_unload)
387 prof_list->assembly_start_load = start_load;
388 prof_list->assembly_end_load = end_load;
389 prof_list->assembly_start_unload = start_unload;
390 prof_list->assembly_end_unload = end_unload;
394 mono_profiler_install_module (MonoProfileModuleFunc start_load, MonoProfileModuleResult end_load,
395 MonoProfileModuleFunc start_unload, MonoProfileModuleFunc end_unload)
399 prof_list->module_start_load = start_load;
400 prof_list->module_end_load = end_load;
401 prof_list->module_start_unload = start_unload;
402 prof_list->module_end_unload = end_unload;
406 mono_profiler_install_class (MonoProfileClassFunc start_load, MonoProfileClassResult end_load,
407 MonoProfileClassFunc start_unload, MonoProfileClassFunc end_unload)
411 prof_list->class_start_load = start_load;
412 prof_list->class_end_load = end_load;
413 prof_list->class_start_unload = start_unload;
414 prof_list->class_end_unload = end_unload;
418 mono_profiler_method_enter (MonoMethod *method)
421 for (prof = prof_list; prof; prof = prof->next) {
422 if ((prof->events & MONO_PROFILE_ENTER_LEAVE) && prof->method_enter)
423 prof->method_enter (prof->profiler, method);
428 mono_profiler_method_leave (MonoMethod *method)
431 for (prof = prof_list; prof; prof = prof->next) {
432 if ((prof->events & MONO_PROFILE_ENTER_LEAVE) && prof->method_leave)
433 prof->method_leave (prof->profiler, method);
438 mono_profiler_method_jit (MonoMethod *method)
441 for (prof = prof_list; prof; prof = prof->next) {
442 if ((prof->events & MONO_PROFILE_JIT_COMPILATION) && prof->jit_start)
443 prof->jit_start (prof->profiler, method);
448 mono_profiler_method_end_jit (MonoMethod *method, MonoJitInfo* jinfo, int result)
451 for (prof = prof_list; prof; prof = prof->next) {
452 if ((prof->events & MONO_PROFILE_JIT_COMPILATION)) {
454 prof->jit_end (prof->profiler, method, result);
456 prof->jit_end2 (prof->profiler, method, jinfo, result);
462 mono_profiler_method_free (MonoMethod *method)
465 for (prof = prof_list; prof; prof = prof->next) {
466 if ((prof->events & MONO_PROFILE_METHOD_EVENTS) && prof->method_free)
467 prof->method_free (prof->profiler, method);
472 mono_profiler_method_start_invoke (MonoMethod *method)
475 for (prof = prof_list; prof; prof = prof->next) {
476 if ((prof->events & MONO_PROFILE_METHOD_EVENTS) && prof->method_start_invoke)
477 prof->method_start_invoke (prof->profiler, method);
482 mono_profiler_method_end_invoke (MonoMethod *method)
485 for (prof = prof_list; prof; prof = prof->next) {
486 if ((prof->events & MONO_PROFILE_METHOD_EVENTS) && prof->method_end_invoke)
487 prof->method_end_invoke (prof->profiler, method);
492 mono_profiler_code_transition (MonoMethod *method, int result)
495 for (prof = prof_list; prof; prof = prof->next) {
496 if ((prof->events & MONO_PROFILE_TRANSITIONS) && prof->man_unman_transition)
497 prof->man_unman_transition (prof->profiler, method, result);
502 mono_profiler_allocation (MonoObject *obj, MonoClass *klass)
505 for (prof = prof_list; prof; prof = prof->next) {
506 if ((prof->events & MONO_PROFILE_ALLOCATIONS) && prof->allocation_cb)
507 prof->allocation_cb (prof->profiler, obj, klass);
512 mono_profiler_monitor_event (MonoObject *obj, MonoProfilerMonitorEvent event) {
514 for (prof = prof_list; prof; prof = prof->next) {
515 if ((prof->events & MONO_PROFILE_MONITOR_EVENTS) && prof->monitor_event_cb)
516 prof->monitor_event_cb (prof->profiler, obj, event);
521 mono_profiler_stat_hit (guchar *ip, void *context)
524 for (prof = prof_list; prof; prof = prof->next) {
525 if ((prof->events & MONO_PROFILE_STATISTICAL) && prof->statistical_cb)
526 prof->statistical_cb (prof->profiler, ip, context);
531 mono_profiler_stat_call_chain (int call_chain_depth, guchar **ips, void *context)
534 for (prof = prof_list; prof; prof = prof->next) {
535 if ((prof->events & MONO_PROFILE_STATISTICAL) && prof->statistical_call_chain_cb)
536 prof->statistical_call_chain_cb (prof->profiler, call_chain_depth, ips, context);
541 mono_profiler_exception_thrown (MonoObject *exception)
544 for (prof = prof_list; prof; prof = prof->next) {
545 if ((prof->events & MONO_PROFILE_EXCEPTIONS) && prof->exception_throw_cb)
546 prof->exception_throw_cb (prof->profiler, exception);
551 mono_profiler_exception_method_leave (MonoMethod *method)
554 for (prof = prof_list; prof; prof = prof->next) {
555 if ((prof->events & MONO_PROFILE_EXCEPTIONS) && prof->exception_method_leave_cb)
556 prof->exception_method_leave_cb (prof->profiler, method);
561 mono_profiler_exception_clause_handler (MonoMethod *method, int clause_type, int clause_num)
564 for (prof = prof_list; prof; prof = prof->next) {
565 if ((prof->events & MONO_PROFILE_EXCEPTIONS) && prof->exception_clause_cb)
566 prof->exception_clause_cb (prof->profiler, method, clause_type, clause_num);
571 mono_profiler_thread_start (gsize tid)
574 for (prof = prof_list; prof; prof = prof->next) {
575 if ((prof->events & MONO_PROFILE_THREADS) && prof->thread_start)
576 prof->thread_start (prof->profiler, tid);
581 mono_profiler_thread_end (gsize tid)
584 for (prof = prof_list; prof; prof = prof->next) {
585 if ((prof->events & MONO_PROFILE_THREADS) && prof->thread_end)
586 prof->thread_end (prof->profiler, tid);
591 mono_profiler_thread_name (gsize tid, const char *name)
594 for (prof = prof_list; prof; prof = prof->next) {
595 if ((prof->events & MONO_PROFILE_THREADS) && prof->thread_name)
596 prof->thread_name (prof->profiler, tid, name);
601 mono_profiler_assembly_event (MonoAssembly *assembly, int code)
604 for (prof = prof_list; prof; prof = prof->next) {
605 if (!(prof->events & MONO_PROFILE_ASSEMBLY_EVENTS))
609 case MONO_PROFILE_START_LOAD:
610 if (prof->assembly_start_load)
611 prof->assembly_start_load (prof->profiler, assembly);
613 case MONO_PROFILE_START_UNLOAD:
614 if (prof->assembly_start_unload)
615 prof->assembly_start_unload (prof->profiler, assembly);
617 case MONO_PROFILE_END_UNLOAD:
618 if (prof->assembly_end_unload)
619 prof->assembly_end_unload (prof->profiler, assembly);
622 g_assert_not_reached ();
628 mono_profiler_assembly_loaded (MonoAssembly *assembly, int result)
631 for (prof = prof_list; prof; prof = prof->next) {
632 if ((prof->events & MONO_PROFILE_ASSEMBLY_EVENTS) && prof->assembly_end_load)
633 prof->assembly_end_load (prof->profiler, assembly, result);
637 void mono_profiler_iomap (char *report, const char *pathname, const char *new_pathname)
640 for (prof = prof_list; prof; prof = prof->next) {
641 if ((prof->events & MONO_PROFILE_IOMAP_EVENTS) && prof->iomap_cb)
642 prof->iomap_cb (prof->profiler, report, pathname, new_pathname);
647 mono_profiler_module_event (MonoImage *module, int code)
650 for (prof = prof_list; prof; prof = prof->next) {
651 if (!(prof->events & MONO_PROFILE_MODULE_EVENTS))
655 case MONO_PROFILE_START_LOAD:
656 if (prof->module_start_load)
657 prof->module_start_load (prof->profiler, module);
659 case MONO_PROFILE_START_UNLOAD:
660 if (prof->module_start_unload)
661 prof->module_start_unload (prof->profiler, module);
663 case MONO_PROFILE_END_UNLOAD:
664 if (prof->module_end_unload)
665 prof->module_end_unload (prof->profiler, module);
668 g_assert_not_reached ();
674 mono_profiler_module_loaded (MonoImage *module, int result)
677 for (prof = prof_list; prof; prof = prof->next) {
678 if ((prof->events & MONO_PROFILE_MODULE_EVENTS) && prof->module_end_load)
679 prof->module_end_load (prof->profiler, module, result);
684 mono_profiler_class_event (MonoClass *klass, int code)
687 for (prof = prof_list; prof; prof = prof->next) {
688 if (!(prof->events & MONO_PROFILE_CLASS_EVENTS))
692 case MONO_PROFILE_START_LOAD:
693 if (prof->class_start_load)
694 prof->class_start_load (prof->profiler, klass);
696 case MONO_PROFILE_START_UNLOAD:
697 if (prof->class_start_unload)
698 prof->class_start_unload (prof->profiler, klass);
700 case MONO_PROFILE_END_UNLOAD:
701 if (prof->class_end_unload)
702 prof->class_end_unload (prof->profiler, klass);
705 g_assert_not_reached ();
711 mono_profiler_class_loaded (MonoClass *klass, int result)
714 for (prof = prof_list; prof; prof = prof->next) {
715 if ((prof->events & MONO_PROFILE_CLASS_EVENTS) && prof->class_end_load)
716 prof->class_end_load (prof->profiler, klass, result);
721 mono_profiler_appdomain_event (MonoDomain *domain, int code)
724 for (prof = prof_list; prof; prof = prof->next) {
725 if (!(prof->events & MONO_PROFILE_APPDOMAIN_EVENTS))
729 case MONO_PROFILE_START_LOAD:
730 if (prof->domain_start_load)
731 prof->domain_start_load (prof->profiler, domain);
733 case MONO_PROFILE_START_UNLOAD:
734 if (prof->domain_start_unload)
735 prof->domain_start_unload (prof->profiler, domain);
737 case MONO_PROFILE_END_UNLOAD:
738 if (prof->domain_end_unload)
739 prof->domain_end_unload (prof->profiler, domain);
742 g_assert_not_reached ();
748 mono_profiler_appdomain_loaded (MonoDomain *domain, int result)
751 for (prof = prof_list; prof; prof = prof->next) {
752 if ((prof->events & MONO_PROFILE_APPDOMAIN_EVENTS) && prof->domain_end_load)
753 prof->domain_end_load (prof->profiler, domain, result);
758 mono_profiler_shutdown (void)
761 for (prof = prof_list; prof; prof = prof->next) {
762 if (prof->shutdown_callback)
763 prof->shutdown_callback (prof->profiler);
766 mono_profiler_set_events (0);
770 mono_profiler_gc_heap_resize (gint64 new_size)
773 for (prof = prof_list; prof; prof = prof->next) {
774 if ((prof->events & MONO_PROFILE_GC) && prof->gc_heap_resize)
775 prof->gc_heap_resize (prof->profiler, new_size);
780 mono_profiler_gc_event (MonoGCEvent event, int generation)
783 for (prof = prof_list; prof; prof = prof->next) {
784 if ((prof->events & MONO_PROFILE_GC) && prof->gc_event)
785 prof->gc_event (prof->profiler, event, generation);
790 mono_profiler_gc_moves (void **objects, int num)
793 for (prof = prof_list; prof; prof = prof->next) {
794 if ((prof->events & MONO_PROFILE_GC_MOVES) && prof->gc_moves)
795 prof->gc_moves (prof->profiler, objects, num);
800 mono_profiler_gc_handle (int op, int type, uintptr_t handle, MonoObject *obj)
803 for (prof = prof_list; prof; prof = prof->next) {
804 if ((prof->events & MONO_PROFILE_GC_ROOTS) && prof->gc_handle)
805 prof->gc_handle (prof->profiler, op, type, handle, obj);
810 mono_profiler_gc_roots (int num, void **objects, int *root_types, uintptr_t *extra_info)
813 for (prof = prof_list; prof; prof = prof->next) {
814 if ((prof->events & MONO_PROFILE_GC_ROOTS) && prof->gc_roots)
815 prof->gc_roots (prof->profiler, num, objects, root_types, extra_info);
820 mono_profiler_install_gc (MonoProfileGCFunc callback, MonoProfileGCResizeFunc heap_resize_callback)
822 mono_gc_enable_events ();
825 prof_list->gc_event = callback;
826 prof_list->gc_heap_resize = heap_resize_callback;
830 * mono_profiler_install_gc_moves:
831 * @callback: callback function
833 * Install the @callback function that the GC will call when moving objects.
834 * The callback receives an array of pointers and the number of elements
835 * in the array. Every even element in the array is the original object location
836 * and the following odd element is the new location of the object in memory.
837 * So the number of elements argument will always be a multiple of 2.
838 * Since this callback happens during the GC, it is a restricted environment:
839 * no locks can be taken and the object pointers can be inspected only once
840 * the GC is finished (of course the original location pointers will not
841 * point to valid objects anymore).
844 mono_profiler_install_gc_moves (MonoProfileGCMoveFunc callback)
848 prof_list->gc_moves = callback;
852 * mono_profiler_install_gc_roots:
853 * @handle_callback: callback function
854 * @roots_callback: callback function
856 * Install the @handle_callback function that the GC will call when GC
857 * handles are created or destroyed.
858 * The callback receives an operation, which is either #MONO_PROFILER_GC_HANDLE_CREATED
859 * or #MONO_PROFILER_GC_HANDLE_DESTROYED, the handle type, the handle value and the
860 * object pointer, if present.
861 * Install the @roots_callback function that the GC will call when tracing
862 * the roots for a collection.
863 * The callback receives the number of elements and three arrays: an array
864 * of objects, an array of root types and flags and an array of extra info.
865 * The size of each array is given by the first argument.
868 mono_profiler_install_gc_roots (MonoProfileGCHandleFunc handle_callback, MonoProfileGCRootFunc roots_callback)
872 prof_list->gc_handle = handle_callback;
873 prof_list->gc_roots = roots_callback;
877 mono_profiler_install_runtime_initialized (MonoProfileFunc runtime_initialized_callback)
881 prof_list->runtime_initialized_event = runtime_initialized_callback;
885 mono_profiler_runtime_initialized (void) {
887 for (prof = prof_list; prof; prof = prof->next) {
888 if (prof->runtime_initialized_event)
889 prof->runtime_initialized_event (prof->profiler);
894 mono_profiler_install_code_chunk_new (MonoProfilerCodeChunkNew callback) {
897 prof_list->code_chunk_new = callback;
900 mono_profiler_code_chunk_new (gpointer chunk, int size) {
902 for (prof = prof_list; prof; prof = prof->next) {
903 if (prof->code_chunk_new)
904 prof->code_chunk_new (prof->profiler, chunk, size);
909 mono_profiler_install_code_chunk_destroy (MonoProfilerCodeChunkDestroy callback) {
912 prof_list->code_chunk_destroy = callback;
915 mono_profiler_code_chunk_destroy (gpointer chunk) {
917 for (prof = prof_list; prof; prof = prof->next) {
918 if (prof->code_chunk_destroy)
919 prof->code_chunk_destroy (prof->profiler, chunk);
924 mono_profiler_install_code_buffer_new (MonoProfilerCodeBufferNew callback) {
927 prof_list->code_buffer_new = callback;
931 mono_profiler_install_iomap (MonoProfileIomapFunc callback)
935 prof_list->iomap_cb = callback;
939 mono_profiler_code_buffer_new (gpointer buffer, int size, MonoProfilerCodeBufferType type, gconstpointer data) {
941 for (prof = prof_list; prof; prof = prof->next) {
942 if (prof->code_buffer_new)
943 prof->code_buffer_new (prof->profiler, buffer, size, type, (void*)data);
947 static GHashTable *coverage_hash = NULL;
949 MonoProfileCoverageInfo*
950 mono_profiler_coverage_alloc (MonoMethod *method, int entries)
952 MonoProfileCoverageInfo *res;
953 int instrument = FALSE;
956 for (prof = prof_list; prof; prof = prof->next) {
957 /* note that we call the filter on all the profilers even if just
958 * a single one would be enough to instrument a method
960 if (prof->coverage_filter_cb)
961 if (prof->coverage_filter_cb (prof->profiler, method))
967 mono_profiler_coverage_lock ();
969 coverage_hash = g_hash_table_new (NULL, NULL);
971 res = g_malloc0 (sizeof (MonoProfileCoverageInfo) + sizeof (void*) * 2 * entries);
973 res->entries = entries;
975 g_hash_table_insert (coverage_hash, method, res);
976 mono_profiler_coverage_unlock ();
981 /* safe only when the method antive code has been unloaded */
983 mono_profiler_coverage_free (MonoMethod *method)
985 MonoProfileCoverageInfo* info;
987 mono_profiler_coverage_lock ();
988 if (!coverage_hash) {
989 mono_profiler_coverage_unlock ();
993 info = g_hash_table_lookup (coverage_hash, method);
996 g_hash_table_remove (coverage_hash, method);
998 mono_profiler_coverage_unlock ();
1002 * mono_profiler_coverage_get:
1003 * @prof: The profiler handle, installed with mono_profiler_install
1004 * @method: the method to gather information from.
1005 * @func: A routine that will be called back with the results
1007 * If the MONO_PROFILER_INS_COVERAGE flag was active during JIT compilation
1008 * it is posisble to obtain coverage information about a give method.
1010 * The function @func will be invoked repeatedly with instances of the
1011 * MonoProfileCoverageEntry structure.
1014 mono_profiler_coverage_get (MonoProfiler *prof, MonoMethod *method, MonoProfileCoverageFunc func)
1016 MonoProfileCoverageInfo* info = NULL;
1019 const unsigned char *start, *end, *cil_code;
1020 MonoMethodHeader *header;
1021 MonoProfileCoverageEntry entry;
1022 MonoDebugMethodInfo *debug_minfo;
1024 mono_profiler_coverage_lock ();
1026 info = g_hash_table_lookup (coverage_hash, method);
1027 mono_profiler_coverage_unlock ();
1032 header = mono_method_get_header (method);
1033 start = mono_method_header_get_code (header, &code_size, NULL);
1034 debug_minfo = mono_debug_lookup_method (method);
1036 end = start + code_size;
1037 for (i = 0; i < info->entries; ++i) {
1038 cil_code = info->data [i].cil_code;
1039 if (cil_code && cil_code >= start && cil_code < end) {
1041 offset = cil_code - start;
1042 entry.iloffset = offset;
1043 entry.method = method;
1044 entry.counter = info->data [i].count;
1045 entry.line = entry.col = 1;
1046 entry.filename = NULL;
1048 MonoDebugSourceLocation *location;
1050 location = mono_debug_symfile_lookup_location (debug_minfo, offset);
1052 entry.line = location->row;
1053 entry.col = location->column;
1054 entry.filename = fname = g_strdup (location->source_file);
1055 mono_debug_free_source_location (location);
1059 func (prof, &entry);
1063 mono_metadata_free_mh (header);
1066 typedef void (*ProfilerInitializer) (const char*);
1067 #define INITIALIZER_NAME "mono_profiler_startup"
1071 load_profiler (MonoDl *pmodule, const char *desc, const char *symbol)
1074 ProfilerInitializer func;
1079 if ((err = mono_dl_symbol (pmodule, symbol, (gpointer *) &func))) {
1089 load_embedded_profiler (const char *desc, const char *name)
1093 MonoDl *pmodule = NULL;
1096 pmodule = mono_dl_open (NULL, MONO_DL_LAZY, &err);
1098 g_warning ("Could not open main executable (%s)", err);
1103 symbol = g_strdup_printf (INITIALIZER_NAME "_%s", name);
1104 result = load_profiler (pmodule, desc, symbol);
1111 load_profiler_from_directory (const char *directory, const char *libname, const char *desc)
1113 MonoDl *pmodule = NULL;
1120 while ((path = mono_dl_build_path (directory, libname, &iter))) {
1121 pmodule = mono_dl_open (path, MONO_DL_LAZY, &err);
1125 return load_profiler (pmodule, desc, INITIALIZER_NAME);
1132 load_profiler_from_mono_instalation (const char *libname, const char *desc)
1135 MonoDl *pmodule = mono_dl_open_runtime_lib (libname, MONO_DL_LAZY, &err);
1138 return load_profiler (pmodule, desc, INITIALIZER_NAME);
1143 * mono_profiler_load:
1144 * @desc: arguments to configure the profiler
1146 * Invoke this method to initialize the profiler. This will drive the
1147 * loading of the internal ("default") or any external profilers.
1149 * This routine is invoked by Mono's driver, but must be called manually
1150 * if you embed Mono into your application.
1153 mono_profiler_load (const char *desc)
1156 mono_gc_base_init ();
1158 if (!desc || (strcmp ("default", desc) == 0)) {
1159 desc = "log:report";
1161 /* we keep command-line compat with the old version here */
1162 if (strncmp (desc, "default:", 8) == 0) {
1163 gchar **args, **ptr;
1164 GString *str = g_string_new ("log:report");
1165 args = g_strsplit (desc + 8, ",", -1);
1166 for (ptr = args; ptr && *ptr; ptr++) {
1167 const char *arg = *ptr;
1169 if (!strcmp (arg, "time"))
1170 g_string_append (str, ",calls");
1171 else if (!strcmp (arg, "alloc"))
1172 g_string_append (str, ",alloc");
1173 else if (!strcmp (arg, "stat"))
1174 g_string_append (str, ",sample");
1175 else if (!strcmp (arg, "jit"))
1176 continue; /* accept and do nothing */
1177 else if (strncmp (arg, "file=", 5) == 0) {
1178 g_string_append_printf (str, ",output=%s", arg + 5);
1180 fprintf (stderr, "profiler : Unknown argument '%s'.\n", arg);
1184 desc = cdesc = g_string_free (str, FALSE);
1187 const char* col = strchr (desc, ':');
1190 gboolean res = FALSE;
1193 mname = g_memdup (desc, col - desc + 1);
1194 mname [col - desc] = 0;
1196 mname = g_strdup (desc);
1198 if (!load_embedded_profiler (desc, mname)) {
1199 libname = g_strdup_printf ("mono-profiler-%s", mname);
1200 #if defined (MONO_ASSEMBLIES)
1201 res = load_profiler_from_directory (mono_assembly_getrootdir (), libname, desc);
1204 res = load_profiler_from_directory (NULL, libname, desc);
1206 res = load_profiler_from_mono_instalation (libname, desc);
1208 g_warning ("The '%s' profiler wasn't found in the main executable nor could it be loaded from '%s'.", mname, libname);