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)
12 #include "mono/metadata/profiler-private.h"
13 #include "mono/metadata/profiler-default.h"
14 #include "mono/metadata/debug-helpers.h"
15 #include "mono/metadata/mono-debug.h"
16 #include "mono/metadata/debug-mono-symfile.h"
17 #include "mono/metadata/metadata-internals.h"
18 #include "mono/metadata/class-internals.h"
19 #include "mono/metadata/domain-internals.h"
20 #include "mono/metadata/gc-internal.h"
21 #include "mono/io-layer/io-layer.h"
22 #include "mono/utils/mono-dl.h"
27 #ifdef HAVE_SYS_TIME_H
30 #ifdef HAVE_BACKTRACE_SYMBOLS
34 typedef struct _ProfilerDesc ProfilerDesc;
35 struct _ProfilerDesc {
37 MonoProfiler *profiler;
38 MonoProfileFlags events;
40 MonoProfileAppDomainFunc domain_start_load;
41 MonoProfileAppDomainResult domain_end_load;
42 MonoProfileAppDomainFunc domain_start_unload;
43 MonoProfileAppDomainFunc domain_end_unload;
45 MonoProfileAssemblyFunc assembly_start_load;
46 MonoProfileAssemblyResult assembly_end_load;
47 MonoProfileAssemblyFunc assembly_start_unload;
48 MonoProfileAssemblyFunc assembly_end_unload;
50 MonoProfileModuleFunc module_start_load;
51 MonoProfileModuleResult module_end_load;
52 MonoProfileModuleFunc module_start_unload;
53 MonoProfileModuleFunc module_end_unload;
55 MonoProfileClassFunc class_start_load;
56 MonoProfileClassResult class_end_load;
57 MonoProfileClassFunc class_start_unload;
58 MonoProfileClassFunc class_end_unload;
60 MonoProfileMethodFunc jit_start;
61 MonoProfileMethodResult jit_end;
62 MonoProfileJitResult jit_end2;
63 MonoProfileMethodFunc method_free;
64 MonoProfileMethodFunc method_start_invoke;
65 MonoProfileMethodFunc method_end_invoke;
66 MonoProfileMethodResult man_unman_transition;
67 MonoProfileAllocFunc allocation_cb;
68 MonoProfileMonitorFunc monitor_event_cb;
69 MonoProfileStatFunc statistical_cb;
70 MonoProfileStatCallChainFunc statistical_call_chain_cb;
71 int statistical_call_chain_depth;
72 MonoProfilerCallChainStrategy statistical_call_chain_strategy;
73 MonoProfileMethodFunc method_enter;
74 MonoProfileMethodFunc method_leave;
76 MonoProfileExceptionFunc exception_throw_cb;
77 MonoProfileMethodFunc exception_method_leave_cb;
78 MonoProfileExceptionClauseFunc exception_clause_cb;
80 MonoProfileIomapFunc iomap_cb;
82 MonoProfileThreadFunc thread_start;
83 MonoProfileThreadFunc thread_end;
84 MonoProfileThreadNameFunc thread_name;
86 MonoProfileCoverageFilterFunc coverage_filter_cb;
88 MonoProfileFunc shutdown_callback;
90 MonoProfileGCFunc gc_event;
91 MonoProfileGCResizeFunc gc_heap_resize;
92 MonoProfileGCMoveFunc gc_moves;
93 MonoProfileGCHandleFunc gc_handle;
94 MonoProfileGCRootFunc gc_roots;
96 MonoProfileFunc runtime_initialized_event;
98 MonoProfilerCodeChunkNew code_chunk_new;
99 MonoProfilerCodeChunkDestroy code_chunk_destroy;
100 MonoProfilerCodeBufferNew code_buffer_new;
103 static ProfilerDesc *prof_list = NULL;
105 #define mono_profiler_coverage_lock() EnterCriticalSection (&profiler_coverage_mutex)
106 #define mono_profiler_coverage_unlock() LeaveCriticalSection (&profiler_coverage_mutex)
107 static CRITICAL_SECTION profiler_coverage_mutex;
109 /* this is directly accessible to other mono libs.
110 * It is the ORed value of all the profiler's events.
112 MonoProfileFlags mono_profiler_events;
115 * mono_profiler_install:
116 * @prof: a MonoProfiler structure pointer, or a pointer to a derived structure.
117 * @callback: the function to invoke at shutdown
119 * Use mono_profiler_install to activate profiling in the Mono runtime.
120 * Typically developers of new profilers will create a new structure whose
121 * first field is a MonoProfiler and put any extra information that they need
122 * to access from the various profiling callbacks there.
126 mono_profiler_install (MonoProfiler *prof, MonoProfileFunc callback)
128 ProfilerDesc *desc = g_new0 (ProfilerDesc, 1);
130 InitializeCriticalSection (&profiler_coverage_mutex);
131 desc->profiler = prof;
132 desc->shutdown_callback = callback;
133 desc->next = prof_list;
138 * mono_profiler_set_events:
139 * @events: an ORed set of values made up of MONO_PROFILER_ flags
141 * The events descriped in the @events argument is a set of flags
142 * that represent which profiling events must be triggered. For
143 * example if you have registered a set of methods for tracking
144 * JIT compilation start and end with mono_profiler_install_jit_compile,
145 * you will want to pass the MONO_PROFILE_JIT_COMPILATION flag to
148 * You can call mono_profile_set_events more than once and you can
149 * do this at runtime to modify which methods are invoked.
152 mono_profiler_set_events (MonoProfileFlags events)
155 MonoProfileFlags value = 0;
157 prof_list->events = events;
158 for (prof = prof_list; prof; prof = prof->next)
159 value |= prof->events;
160 mono_profiler_events = value;
164 * mono_profiler_get_events:
166 * Returns a list of active events that will be intercepted.
169 mono_profiler_get_events (void)
171 return mono_profiler_events;
175 * mono_profiler_install_enter_leave:
176 * @enter: the routine to be called on each method entry
177 * @fleave: the routine to be called each time a method returns
179 * Use this routine to install routines that will be called everytime
180 * a method enters and leaves. The routines will receive as an argument
181 * the MonoMethod representing the method that is entering or leaving.
184 mono_profiler_install_enter_leave (MonoProfileMethodFunc enter, MonoProfileMethodFunc fleave)
188 prof_list->method_enter = enter;
189 prof_list->method_leave = fleave;
193 * mono_profiler_install_jit_compile:
194 * @start: the routine to be called when the JIT process starts.
195 * @end: the routine to be called when the JIT process ends.
197 * Use this routine to install routines that will be called when JIT
198 * compilation of a method starts and completes.
201 mono_profiler_install_jit_compile (MonoProfileMethodFunc start, MonoProfileMethodResult end)
205 prof_list->jit_start = start;
206 prof_list->jit_end = end;
210 mono_profiler_install_jit_end (MonoProfileJitResult end)
214 prof_list->jit_end2 = end;
218 mono_profiler_install_method_free (MonoProfileMethodFunc callback)
222 prof_list->method_free = callback;
226 mono_profiler_install_method_invoke (MonoProfileMethodFunc start, MonoProfileMethodFunc end)
230 prof_list->method_start_invoke = start;
231 prof_list->method_end_invoke = end;
235 mono_profiler_install_thread (MonoProfileThreadFunc start, MonoProfileThreadFunc end)
239 prof_list->thread_start = start;
240 prof_list->thread_end = end;
244 mono_profiler_install_thread_name (MonoProfileThreadNameFunc thread_name_cb)
248 prof_list->thread_name = thread_name_cb;
252 mono_profiler_install_transition (MonoProfileMethodResult callback)
256 prof_list->man_unman_transition = callback;
260 mono_profiler_install_allocation (MonoProfileAllocFunc callback)
264 prof_list->allocation_cb = callback;
268 mono_profiler_install_monitor (MonoProfileMonitorFunc callback)
272 prof_list->monitor_event_cb = callback;
276 mono_profiler_install_statistical (MonoProfileStatFunc callback)
280 prof_list->statistical_cb = callback;
284 mono_profiler_install_statistical_call_chain (MonoProfileStatCallChainFunc callback, int call_chain_depth, MonoProfilerCallChainStrategy call_chain_strategy) {
287 if (call_chain_depth > MONO_PROFILER_MAX_STAT_CALL_CHAIN_DEPTH) {
288 call_chain_depth = MONO_PROFILER_MAX_STAT_CALL_CHAIN_DEPTH;
290 if ((call_chain_strategy >= MONO_PROFILER_CALL_CHAIN_INVALID) || (call_chain_strategy < MONO_PROFILER_CALL_CHAIN_NONE)) {
291 call_chain_strategy = MONO_PROFILER_CALL_CHAIN_NONE;
293 prof_list->statistical_call_chain_cb = callback;
294 prof_list->statistical_call_chain_depth = call_chain_depth;
295 prof_list->statistical_call_chain_strategy = call_chain_strategy;
299 mono_profiler_stat_get_call_chain_depth (void) {
300 if (prof_list && prof_list->statistical_call_chain_cb != NULL) {
301 return prof_list->statistical_call_chain_depth;
307 MonoProfilerCallChainStrategy
308 mono_profiler_stat_get_call_chain_strategy (void) {
309 if (prof_list && prof_list->statistical_call_chain_cb != NULL) {
310 return prof_list->statistical_call_chain_strategy;
312 return MONO_PROFILER_CALL_CHAIN_NONE;
316 void mono_profiler_install_exception (MonoProfileExceptionFunc throw_callback, MonoProfileMethodFunc exc_method_leave, MonoProfileExceptionClauseFunc clause_callback)
320 prof_list->exception_throw_cb = throw_callback;
321 prof_list->exception_method_leave_cb = exc_method_leave;
322 prof_list->exception_clause_cb = clause_callback;
326 mono_profiler_install_coverage_filter (MonoProfileCoverageFilterFunc callback)
330 prof_list->coverage_filter_cb = callback;
334 mono_profiler_install_appdomain (MonoProfileAppDomainFunc start_load, MonoProfileAppDomainResult end_load,
335 MonoProfileAppDomainFunc start_unload, MonoProfileAppDomainFunc end_unload)
340 prof_list->domain_start_load = start_load;
341 prof_list->domain_end_load = end_load;
342 prof_list->domain_start_unload = start_unload;
343 prof_list->domain_end_unload = end_unload;
347 mono_profiler_install_assembly (MonoProfileAssemblyFunc start_load, MonoProfileAssemblyResult end_load,
348 MonoProfileAssemblyFunc start_unload, MonoProfileAssemblyFunc end_unload)
352 prof_list->assembly_start_load = start_load;
353 prof_list->assembly_end_load = end_load;
354 prof_list->assembly_start_unload = start_unload;
355 prof_list->assembly_end_unload = end_unload;
359 mono_profiler_install_module (MonoProfileModuleFunc start_load, MonoProfileModuleResult end_load,
360 MonoProfileModuleFunc start_unload, MonoProfileModuleFunc end_unload)
364 prof_list->module_start_load = start_load;
365 prof_list->module_end_load = end_load;
366 prof_list->module_start_unload = start_unload;
367 prof_list->module_end_unload = end_unload;
371 mono_profiler_install_class (MonoProfileClassFunc start_load, MonoProfileClassResult end_load,
372 MonoProfileClassFunc start_unload, MonoProfileClassFunc end_unload)
376 prof_list->class_start_load = start_load;
377 prof_list->class_end_load = end_load;
378 prof_list->class_start_unload = start_unload;
379 prof_list->class_end_unload = end_unload;
383 mono_profiler_method_enter (MonoMethod *method)
386 for (prof = prof_list; prof; prof = prof->next) {
387 if ((prof->events & MONO_PROFILE_ENTER_LEAVE) && prof->method_enter)
388 prof->method_enter (prof->profiler, method);
393 mono_profiler_method_leave (MonoMethod *method)
396 for (prof = prof_list; prof; prof = prof->next) {
397 if ((prof->events & MONO_PROFILE_ENTER_LEAVE) && prof->method_leave)
398 prof->method_leave (prof->profiler, method);
403 mono_profiler_method_jit (MonoMethod *method)
406 for (prof = prof_list; prof; prof = prof->next) {
407 if ((prof->events & MONO_PROFILE_JIT_COMPILATION) && prof->jit_start)
408 prof->jit_start (prof->profiler, method);
413 mono_profiler_method_end_jit (MonoMethod *method, MonoJitInfo* jinfo, int result)
416 for (prof = prof_list; prof; prof = prof->next) {
417 if ((prof->events & MONO_PROFILE_JIT_COMPILATION)) {
419 prof->jit_end (prof->profiler, method, result);
421 prof->jit_end2 (prof->profiler, method, jinfo, result);
427 mono_profiler_method_free (MonoMethod *method)
430 for (prof = prof_list; prof; prof = prof->next) {
431 if ((prof->events & MONO_PROFILE_METHOD_EVENTS) && prof->method_free)
432 prof->method_free (prof->profiler, method);
437 mono_profiler_method_start_invoke (MonoMethod *method)
440 for (prof = prof_list; prof; prof = prof->next) {
441 if ((prof->events & MONO_PROFILE_METHOD_EVENTS) && prof->method_start_invoke)
442 prof->method_start_invoke (prof->profiler, method);
447 mono_profiler_method_end_invoke (MonoMethod *method)
450 for (prof = prof_list; prof; prof = prof->next) {
451 if ((prof->events & MONO_PROFILE_METHOD_EVENTS) && prof->method_end_invoke)
452 prof->method_end_invoke (prof->profiler, method);
457 mono_profiler_code_transition (MonoMethod *method, int result)
460 for (prof = prof_list; prof; prof = prof->next) {
461 if ((prof->events & MONO_PROFILE_TRANSITIONS) && prof->man_unman_transition)
462 prof->man_unman_transition (prof->profiler, method, result);
467 mono_profiler_allocation (MonoObject *obj, MonoClass *klass)
470 for (prof = prof_list; prof; prof = prof->next) {
471 if ((prof->events & MONO_PROFILE_ALLOCATIONS) && prof->allocation_cb)
472 prof->allocation_cb (prof->profiler, obj, klass);
477 mono_profiler_monitor_event (MonoObject *obj, MonoProfilerMonitorEvent event) {
479 for (prof = prof_list; prof; prof = prof->next) {
480 if ((prof->events & MONO_PROFILE_MONITOR_EVENTS) && prof->monitor_event_cb)
481 prof->monitor_event_cb (prof->profiler, obj, event);
486 mono_profiler_stat_hit (guchar *ip, void *context)
489 for (prof = prof_list; prof; prof = prof->next) {
490 if ((prof->events & MONO_PROFILE_STATISTICAL) && prof->statistical_cb)
491 prof->statistical_cb (prof->profiler, ip, context);
496 mono_profiler_stat_call_chain (int call_chain_depth, guchar **ips, void *context)
499 for (prof = prof_list; prof; prof = prof->next) {
500 if ((prof->events & MONO_PROFILE_STATISTICAL) && prof->statistical_call_chain_cb)
501 prof->statistical_call_chain_cb (prof->profiler, call_chain_depth, ips, context);
506 mono_profiler_exception_thrown (MonoObject *exception)
509 for (prof = prof_list; prof; prof = prof->next) {
510 if ((prof->events & MONO_PROFILE_EXCEPTIONS) && prof->exception_throw_cb)
511 prof->exception_throw_cb (prof->profiler, exception);
516 mono_profiler_exception_method_leave (MonoMethod *method)
519 for (prof = prof_list; prof; prof = prof->next) {
520 if ((prof->events & MONO_PROFILE_EXCEPTIONS) && prof->exception_method_leave_cb)
521 prof->exception_method_leave_cb (prof->profiler, method);
526 mono_profiler_exception_clause_handler (MonoMethod *method, int clause_type, int clause_num)
529 for (prof = prof_list; prof; prof = prof->next) {
530 if ((prof->events & MONO_PROFILE_EXCEPTIONS) && prof->exception_clause_cb)
531 prof->exception_clause_cb (prof->profiler, method, clause_type, clause_num);
536 mono_profiler_thread_start (gsize tid)
539 for (prof = prof_list; prof; prof = prof->next) {
540 if ((prof->events & MONO_PROFILE_THREADS) && prof->thread_start)
541 prof->thread_start (prof->profiler, tid);
546 mono_profiler_thread_end (gsize tid)
549 for (prof = prof_list; prof; prof = prof->next) {
550 if ((prof->events & MONO_PROFILE_THREADS) && prof->thread_end)
551 prof->thread_end (prof->profiler, tid);
556 mono_profiler_thread_name (gsize tid, const char *name)
559 for (prof = prof_list; prof; prof = prof->next) {
560 if ((prof->events & MONO_PROFILE_THREADS) && prof->thread_name)
561 prof->thread_name (prof->profiler, tid, name);
566 mono_profiler_assembly_event (MonoAssembly *assembly, int code)
569 for (prof = prof_list; prof; prof = prof->next) {
570 if (!(prof->events & MONO_PROFILE_ASSEMBLY_EVENTS))
574 case MONO_PROFILE_START_LOAD:
575 if (prof->assembly_start_load)
576 prof->assembly_start_load (prof->profiler, assembly);
578 case MONO_PROFILE_START_UNLOAD:
579 if (prof->assembly_start_unload)
580 prof->assembly_start_unload (prof->profiler, assembly);
582 case MONO_PROFILE_END_UNLOAD:
583 if (prof->assembly_end_unload)
584 prof->assembly_end_unload (prof->profiler, assembly);
587 g_assert_not_reached ();
593 mono_profiler_assembly_loaded (MonoAssembly *assembly, int result)
596 for (prof = prof_list; prof; prof = prof->next) {
597 if ((prof->events & MONO_PROFILE_ASSEMBLY_EVENTS) && prof->assembly_end_load)
598 prof->assembly_end_load (prof->profiler, assembly, result);
602 void mono_profiler_iomap (char *report, const char *pathname, const char *new_pathname)
605 for (prof = prof_list; prof; prof = prof->next) {
606 if ((prof->events & MONO_PROFILE_IOMAP_EVENTS) && prof->iomap_cb)
607 prof->iomap_cb (prof->profiler, report, pathname, new_pathname);
612 mono_profiler_module_event (MonoImage *module, int code)
615 for (prof = prof_list; prof; prof = prof->next) {
616 if (!(prof->events & MONO_PROFILE_MODULE_EVENTS))
620 case MONO_PROFILE_START_LOAD:
621 if (prof->module_start_load)
622 prof->module_start_load (prof->profiler, module);
624 case MONO_PROFILE_START_UNLOAD:
625 if (prof->module_start_unload)
626 prof->module_start_unload (prof->profiler, module);
628 case MONO_PROFILE_END_UNLOAD:
629 if (prof->module_end_unload)
630 prof->module_end_unload (prof->profiler, module);
633 g_assert_not_reached ();
639 mono_profiler_module_loaded (MonoImage *module, int result)
642 for (prof = prof_list; prof; prof = prof->next) {
643 if ((prof->events & MONO_PROFILE_MODULE_EVENTS) && prof->module_end_load)
644 prof->module_end_load (prof->profiler, module, result);
649 mono_profiler_class_event (MonoClass *klass, int code)
652 for (prof = prof_list; prof; prof = prof->next) {
653 if (!(prof->events & MONO_PROFILE_CLASS_EVENTS))
657 case MONO_PROFILE_START_LOAD:
658 if (prof->class_start_load)
659 prof->class_start_load (prof->profiler, klass);
661 case MONO_PROFILE_START_UNLOAD:
662 if (prof->class_start_unload)
663 prof->class_start_unload (prof->profiler, klass);
665 case MONO_PROFILE_END_UNLOAD:
666 if (prof->class_end_unload)
667 prof->class_end_unload (prof->profiler, klass);
670 g_assert_not_reached ();
676 mono_profiler_class_loaded (MonoClass *klass, int result)
679 for (prof = prof_list; prof; prof = prof->next) {
680 if ((prof->events & MONO_PROFILE_CLASS_EVENTS) && prof->class_end_load)
681 prof->class_end_load (prof->profiler, klass, result);
686 mono_profiler_appdomain_event (MonoDomain *domain, int code)
689 for (prof = prof_list; prof; prof = prof->next) {
690 if (!(prof->events & MONO_PROFILE_APPDOMAIN_EVENTS))
694 case MONO_PROFILE_START_LOAD:
695 if (prof->domain_start_load)
696 prof->domain_start_load (prof->profiler, domain);
698 case MONO_PROFILE_START_UNLOAD:
699 if (prof->domain_start_unload)
700 prof->domain_start_unload (prof->profiler, domain);
702 case MONO_PROFILE_END_UNLOAD:
703 if (prof->domain_end_unload)
704 prof->domain_end_unload (prof->profiler, domain);
707 g_assert_not_reached ();
713 mono_profiler_appdomain_loaded (MonoDomain *domain, int result)
716 for (prof = prof_list; prof; prof = prof->next) {
717 if ((prof->events & MONO_PROFILE_APPDOMAIN_EVENTS) && prof->domain_end_load)
718 prof->domain_end_load (prof->profiler, domain, result);
723 mono_profiler_shutdown (void)
726 for (prof = prof_list; prof; prof = prof->next) {
727 if (prof->shutdown_callback)
728 prof->shutdown_callback (prof->profiler);
731 mono_profiler_set_events (0);
735 mono_profiler_gc_heap_resize (gint64 new_size)
738 for (prof = prof_list; prof; prof = prof->next) {
739 if ((prof->events & MONO_PROFILE_GC) && prof->gc_heap_resize)
740 prof->gc_heap_resize (prof->profiler, new_size);
745 mono_profiler_gc_event (MonoGCEvent event, int generation)
748 for (prof = prof_list; prof; prof = prof->next) {
749 if ((prof->events & MONO_PROFILE_GC) && prof->gc_event)
750 prof->gc_event (prof->profiler, event, generation);
755 mono_profiler_gc_moves (void **objects, int num)
758 for (prof = prof_list; prof; prof = prof->next) {
759 if ((prof->events & MONO_PROFILE_GC_MOVES) && prof->gc_moves)
760 prof->gc_moves (prof->profiler, objects, num);
765 mono_profiler_gc_handle (int op, int type, uintptr_t handle, MonoObject *obj)
768 for (prof = prof_list; prof; prof = prof->next) {
769 if ((prof->events & MONO_PROFILE_GC_ROOTS) && prof->gc_handle)
770 prof->gc_handle (prof->profiler, op, type, handle, obj);
775 mono_profiler_gc_roots (int num, void **objects, int *root_types, uintptr_t *extra_info)
778 for (prof = prof_list; prof; prof = prof->next) {
779 if ((prof->events & MONO_PROFILE_GC_ROOTS) && prof->gc_roots)
780 prof->gc_roots (prof->profiler, num, objects, root_types, extra_info);
785 mono_profiler_install_gc (MonoProfileGCFunc callback, MonoProfileGCResizeFunc heap_resize_callback)
787 mono_gc_enable_events ();
790 prof_list->gc_event = callback;
791 prof_list->gc_heap_resize = heap_resize_callback;
795 * mono_profiler_install_gc_moves:
796 * @callback: callback function
798 * Install the @callback function that the GC will call when moving objects.
799 * The callback receives an array of pointers and the number of elements
800 * in the array. Every even element in the array is the original object location
801 * and the following odd element is the new location of the object in memory.
802 * So the number of elements argument will always be a multiple of 2.
803 * Since this callback happens during the GC, it is a restricted environment:
804 * no locks can be taken and the object pointers can be inspected only once
805 * the GC is finished (of course the original location pointers will not
806 * point to valid objects anymore).
809 mono_profiler_install_gc_moves (MonoProfileGCMoveFunc callback)
813 prof_list->gc_moves = callback;
817 * mono_profiler_install_gc_roots:
818 * @handle_callback: callback function
819 * @roots_callback: callback function
821 * Install the @handle_callback function that the GC will call when GC
822 * handles are created or destroyed.
823 * The callback receives an operation, which is either #MONO_PROFILER_GC_HANDLE_CREATED
824 * or #MONO_PROFILER_GC_HANDLE_DESTROYED, the handle type, the handle value and the
825 * object pointer, if present.
826 * Install the @roots_callback function that the GC will call when tracing
827 * the roots for a collection.
828 * The callback receives the number of elements and three arrays: an array
829 * of objects, an array of root types and flags and an array of extra info.
830 * The size of each array is given by the first argument.
833 mono_profiler_install_gc_roots (MonoProfileGCHandleFunc handle_callback, MonoProfileGCRootFunc roots_callback)
837 prof_list->gc_handle = handle_callback;
838 prof_list->gc_roots = roots_callback;
842 mono_profiler_install_runtime_initialized (MonoProfileFunc runtime_initialized_callback)
846 prof_list->runtime_initialized_event = runtime_initialized_callback;
850 mono_profiler_runtime_initialized (void) {
852 for (prof = prof_list; prof; prof = prof->next) {
853 if (prof->runtime_initialized_event)
854 prof->runtime_initialized_event (prof->profiler);
859 mono_profiler_install_code_chunk_new (MonoProfilerCodeChunkNew callback) {
862 prof_list->code_chunk_new = callback;
865 mono_profiler_code_chunk_new (gpointer chunk, int size) {
867 for (prof = prof_list; prof; prof = prof->next) {
868 if (prof->code_chunk_new)
869 prof->code_chunk_new (prof->profiler, chunk, size);
874 mono_profiler_install_code_chunk_destroy (MonoProfilerCodeChunkDestroy callback) {
877 prof_list->code_chunk_destroy = callback;
880 mono_profiler_code_chunk_destroy (gpointer chunk) {
882 for (prof = prof_list; prof; prof = prof->next) {
883 if (prof->code_chunk_destroy)
884 prof->code_chunk_destroy (prof->profiler, chunk);
889 mono_profiler_install_code_buffer_new (MonoProfilerCodeBufferNew callback) {
892 prof_list->code_buffer_new = callback;
896 mono_profiler_install_iomap (MonoProfileIomapFunc callback)
900 prof_list->iomap_cb = callback;
904 mono_profiler_code_buffer_new (gpointer buffer, int size, MonoProfilerCodeBufferType type, void *data) {
906 for (prof = prof_list; prof; prof = prof->next) {
907 if (prof->code_buffer_new)
908 prof->code_buffer_new (prof->profiler, buffer, size, type, data);
912 static GHashTable *coverage_hash = NULL;
914 MonoProfileCoverageInfo*
915 mono_profiler_coverage_alloc (MonoMethod *method, int entries)
917 MonoProfileCoverageInfo *res;
918 int instrument = FALSE;
921 for (prof = prof_list; prof; prof = prof->next) {
922 /* note that we call the filter on all the profilers even if just
923 * a single one would be enough to instrument a method
925 if (prof->coverage_filter_cb)
926 if (prof->coverage_filter_cb (prof->profiler, method))
932 mono_profiler_coverage_lock ();
934 coverage_hash = g_hash_table_new (NULL, NULL);
936 res = g_malloc0 (sizeof (MonoProfileCoverageInfo) + sizeof (void*) * 2 * entries);
938 res->entries = entries;
940 g_hash_table_insert (coverage_hash, method, res);
941 mono_profiler_coverage_unlock ();
946 /* safe only when the method antive code has been unloaded */
948 mono_profiler_coverage_free (MonoMethod *method)
950 MonoProfileCoverageInfo* info;
952 mono_profiler_coverage_lock ();
953 if (!coverage_hash) {
954 mono_profiler_coverage_unlock ();
958 info = g_hash_table_lookup (coverage_hash, method);
961 g_hash_table_remove (coverage_hash, method);
963 mono_profiler_coverage_unlock ();
967 * mono_profiler_coverage_get:
968 * @prof: The profiler handle, installed with mono_profiler_install
969 * @method: the method to gather information from.
970 * @func: A routine that will be called back with the results
972 * If the MONO_PROFILER_INS_COVERAGE flag was active during JIT compilation
973 * it is posisble to obtain coverage information about a give method.
975 * The function @func will be invoked repeatedly with instances of the
976 * MonoProfileCoverageEntry structure.
979 mono_profiler_coverage_get (MonoProfiler *prof, MonoMethod *method, MonoProfileCoverageFunc func)
981 MonoProfileCoverageInfo* info;
984 const unsigned char *start, *end, *cil_code;
985 MonoMethodHeader *header;
986 MonoProfileCoverageEntry entry;
987 MonoDebugMethodInfo *debug_minfo;
989 mono_profiler_coverage_lock ();
990 info = g_hash_table_lookup (coverage_hash, method);
991 mono_profiler_coverage_unlock ();
996 header = mono_method_get_header (method);
997 start = mono_method_header_get_code (header, &code_size, NULL);
998 debug_minfo = mono_debug_lookup_method (method);
1000 end = start + code_size;
1001 for (i = 0; i < info->entries; ++i) {
1002 cil_code = info->data [i].cil_code;
1003 if (cil_code && cil_code >= start && cil_code < end) {
1005 offset = cil_code - start;
1006 entry.iloffset = offset;
1007 entry.method = method;
1008 entry.counter = info->data [i].count;
1009 entry.line = entry.col = 1;
1010 entry.filename = NULL;
1012 MonoDebugSourceLocation *location;
1014 location = mono_debug_symfile_lookup_location (debug_minfo, offset);
1016 entry.line = location->row;
1017 entry.col = location->column;
1018 entry.filename = fname = g_strdup (location->source_file);
1019 mono_debug_free_source_location (location);
1023 func (prof, &entry);
1027 mono_metadata_free_mh (header);
1030 typedef void (*ProfilerInitializer) (const char*);
1031 #define INITIALIZER_NAME "mono_profiler_startup"
1034 * mono_profiler_load:
1035 * @desc: arguments to configure the profiler
1037 * Invoke this method to initialize the profiler. This will drive the
1038 * loading of the internal ("default") or any external profilers.
1040 * This routine is invoked by Mono's driver, but must be called manually
1041 * if you embed Mono into your application.
1044 mono_profiler_load (const char *desc)
1046 mono_gc_base_init ();
1048 #ifndef DISABLE_PROFILER
1049 if (!desc || (strcmp ("default", desc) == 0) || (strncmp (desc, "default:", 8) == 0)) {
1050 mono_profiler_install_simple (desc);
1059 MonoDl *pmodule = NULL;
1060 const char* col = strchr (desc, ':');
1067 mname = g_memdup (desc, col - desc + 1);
1068 mname [col - desc] = 0;
1070 mname = g_strdup (desc);
1072 libname = g_strdup_printf ("mono-profiler-%s", mname);
1075 while ((path = mono_dl_build_path (NULL, libname, &iter))) {
1077 pmodule = mono_dl_open (path, MONO_DL_LAZY, &err);
1079 ProfilerInitializer func;
1080 if ((err = mono_dl_symbol (pmodule, INITIALIZER_NAME, (gpointer *)&func))) {
1081 g_warning ("Cannot find initializer function %s in profiler module: %s (%s)", INITIALIZER_NAME, libname, err);
1092 g_warning ("Error loading profiler module '%s': %s", libname, err);