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;
85 MonoProfileCoverageFilterFunc coverage_filter_cb;
87 MonoProfileFunc shutdown_callback;
89 MonoProfileGCFunc gc_event;
90 MonoProfileGCResizeFunc gc_heap_resize;
91 MonoProfileGCMoveFunc gc_moves;
93 MonoProfileFunc runtime_initialized_event;
95 MonoProfilerCodeChunkNew code_chunk_new;
96 MonoProfilerCodeChunkDestroy code_chunk_destroy;
97 MonoProfilerCodeBufferNew code_buffer_new;
100 static ProfilerDesc *prof_list = NULL;
102 #define mono_profiler_coverage_lock() EnterCriticalSection (&profiler_coverage_mutex)
103 #define mono_profiler_coverage_unlock() LeaveCriticalSection (&profiler_coverage_mutex)
104 static CRITICAL_SECTION profiler_coverage_mutex;
106 /* this is directly accessible to other mono libs.
107 * It is the ORed value of all the profiler's events.
109 MonoProfileFlags mono_profiler_events;
112 * mono_profiler_install:
113 * @prof: a MonoProfiler structure pointer, or a pointer to a derived structure.
114 * @callback: the function to invoke at shutdown
116 * Use mono_profiler_install to activate profiling in the Mono runtime.
117 * Typically developers of new profilers will create a new structure whose
118 * first field is a MonoProfiler and put any extra information that they need
119 * to access from the various profiling callbacks there.
123 mono_profiler_install (MonoProfiler *prof, MonoProfileFunc callback)
125 ProfilerDesc *desc = g_new0 (ProfilerDesc, 1);
127 InitializeCriticalSection (&profiler_coverage_mutex);
128 desc->profiler = prof;
129 desc->shutdown_callback = callback;
130 desc->next = prof_list;
135 * mono_profiler_set_events:
136 * @events: an ORed set of values made up of MONO_PROFILER_ flags
138 * The events descriped in the @events argument is a set of flags
139 * that represent which profiling events must be triggered. For
140 * example if you have registered a set of methods for tracking
141 * JIT compilation start and end with mono_profiler_install_jit_compile,
142 * you will want to pass the MONO_PROFILE_JIT_COMPILATION flag to
145 * You can call mono_profile_set_events more than once and you can
146 * do this at runtime to modify which methods are invoked.
149 mono_profiler_set_events (MonoProfileFlags events)
152 MonoProfileFlags value = 0;
154 prof_list->events = events;
155 for (prof = prof_list; prof; prof = prof->next)
156 value |= prof->events;
157 mono_profiler_events = value;
161 * mono_profiler_get_events:
163 * Returns a list of active events that will be intercepted.
166 mono_profiler_get_events (void)
168 return mono_profiler_events;
172 * mono_profiler_install_enter_leave:
173 * @enter: the routine to be called on each method entry
174 * @fleave: the routine to be called each time a method returns
176 * Use this routine to install routines that will be called everytime
177 * a method enters and leaves. The routines will receive as an argument
178 * the MonoMethod representing the method that is entering or leaving.
181 mono_profiler_install_enter_leave (MonoProfileMethodFunc enter, MonoProfileMethodFunc fleave)
185 prof_list->method_enter = enter;
186 prof_list->method_leave = fleave;
190 * mono_profiler_install_jit_compile:
191 * @start: the routine to be called when the JIT process starts.
192 * @end: the routine to be called when the JIT process ends.
194 * Use this routine to install routines that will be called when JIT
195 * compilation of a method starts and completes.
198 mono_profiler_install_jit_compile (MonoProfileMethodFunc start, MonoProfileMethodResult end)
202 prof_list->jit_start = start;
203 prof_list->jit_end = end;
207 mono_profiler_install_jit_end (MonoProfileJitResult end)
211 prof_list->jit_end2 = end;
215 mono_profiler_install_method_free (MonoProfileMethodFunc callback)
219 prof_list->method_free = callback;
223 mono_profiler_install_method_invoke (MonoProfileMethodFunc start, MonoProfileMethodFunc end)
227 prof_list->method_start_invoke = start;
228 prof_list->method_end_invoke = end;
232 mono_profiler_install_thread (MonoProfileThreadFunc start, MonoProfileThreadFunc end)
236 prof_list->thread_start = start;
237 prof_list->thread_end = end;
241 mono_profiler_install_transition (MonoProfileMethodResult callback)
245 prof_list->man_unman_transition = callback;
249 mono_profiler_install_allocation (MonoProfileAllocFunc callback)
253 prof_list->allocation_cb = callback;
257 mono_profiler_install_monitor (MonoProfileMonitorFunc callback)
261 prof_list->monitor_event_cb = callback;
265 mono_profiler_install_statistical (MonoProfileStatFunc callback)
269 prof_list->statistical_cb = callback;
273 mono_profiler_install_statistical_call_chain (MonoProfileStatCallChainFunc callback, int call_chain_depth, MonoProfilerCallChainStrategy call_chain_strategy) {
276 if (call_chain_depth > MONO_PROFILER_MAX_STAT_CALL_CHAIN_DEPTH) {
277 call_chain_depth = MONO_PROFILER_MAX_STAT_CALL_CHAIN_DEPTH;
279 if ((call_chain_strategy >= MONO_PROFILER_CALL_CHAIN_INVALID) || (call_chain_strategy < MONO_PROFILER_CALL_CHAIN_NONE)) {
280 call_chain_strategy = MONO_PROFILER_CALL_CHAIN_NONE;
282 prof_list->statistical_call_chain_cb = callback;
283 prof_list->statistical_call_chain_depth = call_chain_depth;
284 prof_list->statistical_call_chain_strategy = call_chain_strategy;
288 mono_profiler_stat_get_call_chain_depth (void) {
289 if (prof_list && prof_list->statistical_call_chain_cb != NULL) {
290 return prof_list->statistical_call_chain_depth;
296 MonoProfilerCallChainStrategy
297 mono_profiler_stat_get_call_chain_strategy (void) {
298 if (prof_list && prof_list->statistical_call_chain_cb != NULL) {
299 return prof_list->statistical_call_chain_strategy;
301 return MONO_PROFILER_CALL_CHAIN_NONE;
305 void mono_profiler_install_exception (MonoProfileExceptionFunc throw_callback, MonoProfileMethodFunc exc_method_leave, MonoProfileExceptionClauseFunc clause_callback)
309 prof_list->exception_throw_cb = throw_callback;
310 prof_list->exception_method_leave_cb = exc_method_leave;
311 prof_list->exception_clause_cb = clause_callback;
315 mono_profiler_install_coverage_filter (MonoProfileCoverageFilterFunc callback)
319 prof_list->coverage_filter_cb = callback;
323 mono_profiler_install_appdomain (MonoProfileAppDomainFunc start_load, MonoProfileAppDomainResult end_load,
324 MonoProfileAppDomainFunc start_unload, MonoProfileAppDomainFunc end_unload)
329 prof_list->domain_start_load = start_load;
330 prof_list->domain_end_load = end_load;
331 prof_list->domain_start_unload = start_unload;
332 prof_list->domain_end_unload = end_unload;
336 mono_profiler_install_assembly (MonoProfileAssemblyFunc start_load, MonoProfileAssemblyResult end_load,
337 MonoProfileAssemblyFunc start_unload, MonoProfileAssemblyFunc end_unload)
341 prof_list->assembly_start_load = start_load;
342 prof_list->assembly_end_load = end_load;
343 prof_list->assembly_start_unload = start_unload;
344 prof_list->assembly_end_unload = end_unload;
348 mono_profiler_install_module (MonoProfileModuleFunc start_load, MonoProfileModuleResult end_load,
349 MonoProfileModuleFunc start_unload, MonoProfileModuleFunc end_unload)
353 prof_list->module_start_load = start_load;
354 prof_list->module_end_load = end_load;
355 prof_list->module_start_unload = start_unload;
356 prof_list->module_end_unload = end_unload;
360 mono_profiler_install_class (MonoProfileClassFunc start_load, MonoProfileClassResult end_load,
361 MonoProfileClassFunc start_unload, MonoProfileClassFunc end_unload)
365 prof_list->class_start_load = start_load;
366 prof_list->class_end_load = end_load;
367 prof_list->class_start_unload = start_unload;
368 prof_list->class_end_unload = end_unload;
372 mono_profiler_method_enter (MonoMethod *method)
375 for (prof = prof_list; prof; prof = prof->next) {
376 if ((prof->events & MONO_PROFILE_ENTER_LEAVE) && prof->method_enter)
377 prof->method_enter (prof->profiler, method);
382 mono_profiler_method_leave (MonoMethod *method)
385 for (prof = prof_list; prof; prof = prof->next) {
386 if ((prof->events & MONO_PROFILE_ENTER_LEAVE) && prof->method_leave)
387 prof->method_leave (prof->profiler, method);
392 mono_profiler_method_jit (MonoMethod *method)
395 for (prof = prof_list; prof; prof = prof->next) {
396 if ((prof->events & MONO_PROFILE_JIT_COMPILATION) && prof->jit_start)
397 prof->jit_start (prof->profiler, method);
402 mono_profiler_method_end_jit (MonoMethod *method, MonoJitInfo* jinfo, int result)
405 for (prof = prof_list; prof; prof = prof->next) {
406 if ((prof->events & MONO_PROFILE_JIT_COMPILATION)) {
408 prof->jit_end (prof->profiler, method, result);
410 prof->jit_end2 (prof->profiler, method, jinfo, result);
416 mono_profiler_method_free (MonoMethod *method)
419 for (prof = prof_list; prof; prof = prof->next) {
420 if ((prof->events & MONO_PROFILE_METHOD_EVENTS) && prof->method_free)
421 prof->method_free (prof->profiler, method);
426 mono_profiler_method_start_invoke (MonoMethod *method)
429 for (prof = prof_list; prof; prof = prof->next) {
430 if ((prof->events & MONO_PROFILE_METHOD_EVENTS) && prof->method_start_invoke)
431 prof->method_start_invoke (prof->profiler, method);
436 mono_profiler_method_end_invoke (MonoMethod *method)
439 for (prof = prof_list; prof; prof = prof->next) {
440 if ((prof->events & MONO_PROFILE_METHOD_EVENTS) && prof->method_end_invoke)
441 prof->method_end_invoke (prof->profiler, method);
446 mono_profiler_code_transition (MonoMethod *method, int result)
449 for (prof = prof_list; prof; prof = prof->next) {
450 if ((prof->events & MONO_PROFILE_TRANSITIONS) && prof->man_unman_transition)
451 prof->man_unman_transition (prof->profiler, method, result);
456 mono_profiler_allocation (MonoObject *obj, MonoClass *klass)
459 for (prof = prof_list; prof; prof = prof->next) {
460 if ((prof->events & MONO_PROFILE_ALLOCATIONS) && prof->allocation_cb)
461 prof->allocation_cb (prof->profiler, obj, klass);
466 mono_profiler_monitor_event (MonoObject *obj, MonoProfilerMonitorEvent event) {
468 for (prof = prof_list; prof; prof = prof->next) {
469 if ((prof->events & MONO_PROFILE_MONITOR_EVENTS) && prof->monitor_event_cb)
470 prof->monitor_event_cb (prof->profiler, obj, event);
475 mono_profiler_stat_hit (guchar *ip, void *context)
478 for (prof = prof_list; prof; prof = prof->next) {
479 if ((prof->events & MONO_PROFILE_STATISTICAL) && prof->statistical_cb)
480 prof->statistical_cb (prof->profiler, ip, context);
485 mono_profiler_stat_call_chain (int call_chain_depth, guchar **ips, void *context)
488 for (prof = prof_list; prof; prof = prof->next) {
489 if ((prof->events & MONO_PROFILE_STATISTICAL) && prof->statistical_call_chain_cb)
490 prof->statistical_call_chain_cb (prof->profiler, call_chain_depth, ips, context);
495 mono_profiler_exception_thrown (MonoObject *exception)
498 for (prof = prof_list; prof; prof = prof->next) {
499 if ((prof->events & MONO_PROFILE_EXCEPTIONS) && prof->exception_throw_cb)
500 prof->exception_throw_cb (prof->profiler, exception);
505 mono_profiler_exception_method_leave (MonoMethod *method)
508 for (prof = prof_list; prof; prof = prof->next) {
509 if ((prof->events & MONO_PROFILE_EXCEPTIONS) && prof->exception_method_leave_cb)
510 prof->exception_method_leave_cb (prof->profiler, method);
515 mono_profiler_exception_clause_handler (MonoMethod *method, int clause_type, int clause_num)
518 for (prof = prof_list; prof; prof = prof->next) {
519 if ((prof->events & MONO_PROFILE_EXCEPTIONS) && prof->exception_clause_cb)
520 prof->exception_clause_cb (prof->profiler, method, clause_type, clause_num);
525 mono_profiler_thread_start (gsize tid)
528 for (prof = prof_list; prof; prof = prof->next) {
529 if ((prof->events & MONO_PROFILE_THREADS) && prof->thread_start)
530 prof->thread_start (prof->profiler, tid);
535 mono_profiler_thread_end (gsize tid)
538 for (prof = prof_list; prof; prof = prof->next) {
539 if ((prof->events & MONO_PROFILE_THREADS) && prof->thread_end)
540 prof->thread_end (prof->profiler, tid);
545 mono_profiler_assembly_event (MonoAssembly *assembly, int code)
548 for (prof = prof_list; prof; prof = prof->next) {
549 if (!(prof->events & MONO_PROFILE_ASSEMBLY_EVENTS))
553 case MONO_PROFILE_START_LOAD:
554 if (prof->assembly_start_load)
555 prof->assembly_start_load (prof->profiler, assembly);
557 case MONO_PROFILE_START_UNLOAD:
558 if (prof->assembly_start_unload)
559 prof->assembly_start_unload (prof->profiler, assembly);
561 case MONO_PROFILE_END_UNLOAD:
562 if (prof->assembly_end_unload)
563 prof->assembly_end_unload (prof->profiler, assembly);
566 g_assert_not_reached ();
572 mono_profiler_assembly_loaded (MonoAssembly *assembly, int result)
575 for (prof = prof_list; prof; prof = prof->next) {
576 if ((prof->events & MONO_PROFILE_ASSEMBLY_EVENTS) && prof->assembly_end_load)
577 prof->assembly_end_load (prof->profiler, assembly, result);
581 void mono_profiler_iomap (char *report, const char *pathname, const char *new_pathname)
584 for (prof = prof_list; prof; prof = prof->next) {
585 if ((prof->events & MONO_PROFILE_IOMAP_EVENTS) && prof->iomap_cb)
586 prof->iomap_cb (prof->profiler, report, pathname, new_pathname);
591 mono_profiler_module_event (MonoImage *module, int code)
594 for (prof = prof_list; prof; prof = prof->next) {
595 if (!(prof->events & MONO_PROFILE_MODULE_EVENTS))
599 case MONO_PROFILE_START_LOAD:
600 if (prof->module_start_load)
601 prof->module_start_load (prof->profiler, module);
603 case MONO_PROFILE_START_UNLOAD:
604 if (prof->module_start_unload)
605 prof->module_start_unload (prof->profiler, module);
607 case MONO_PROFILE_END_UNLOAD:
608 if (prof->module_end_unload)
609 prof->module_end_unload (prof->profiler, module);
612 g_assert_not_reached ();
618 mono_profiler_module_loaded (MonoImage *module, int result)
621 for (prof = prof_list; prof; prof = prof->next) {
622 if ((prof->events & MONO_PROFILE_MODULE_EVENTS) && prof->module_end_load)
623 prof->module_end_load (prof->profiler, module, result);
628 mono_profiler_class_event (MonoClass *klass, int code)
631 for (prof = prof_list; prof; prof = prof->next) {
632 if (!(prof->events & MONO_PROFILE_CLASS_EVENTS))
636 case MONO_PROFILE_START_LOAD:
637 if (prof->class_start_load)
638 prof->class_start_load (prof->profiler, klass);
640 case MONO_PROFILE_START_UNLOAD:
641 if (prof->class_start_unload)
642 prof->class_start_unload (prof->profiler, klass);
644 case MONO_PROFILE_END_UNLOAD:
645 if (prof->class_end_unload)
646 prof->class_end_unload (prof->profiler, klass);
649 g_assert_not_reached ();
655 mono_profiler_class_loaded (MonoClass *klass, int result)
658 for (prof = prof_list; prof; prof = prof->next) {
659 if ((prof->events & MONO_PROFILE_CLASS_EVENTS) && prof->class_end_load)
660 prof->class_end_load (prof->profiler, klass, result);
665 mono_profiler_appdomain_event (MonoDomain *domain, int code)
668 for (prof = prof_list; prof; prof = prof->next) {
669 if (!(prof->events & MONO_PROFILE_APPDOMAIN_EVENTS))
673 case MONO_PROFILE_START_LOAD:
674 if (prof->domain_start_load)
675 prof->domain_start_load (prof->profiler, domain);
677 case MONO_PROFILE_START_UNLOAD:
678 if (prof->domain_start_unload)
679 prof->domain_start_unload (prof->profiler, domain);
681 case MONO_PROFILE_END_UNLOAD:
682 if (prof->domain_end_unload)
683 prof->domain_end_unload (prof->profiler, domain);
686 g_assert_not_reached ();
692 mono_profiler_appdomain_loaded (MonoDomain *domain, int result)
695 for (prof = prof_list; prof; prof = prof->next) {
696 if ((prof->events & MONO_PROFILE_APPDOMAIN_EVENTS) && prof->domain_end_load)
697 prof->domain_end_load (prof->profiler, domain, result);
702 mono_profiler_shutdown (void)
705 for (prof = prof_list; prof; prof = prof->next) {
706 if (prof->shutdown_callback)
707 prof->shutdown_callback (prof->profiler);
710 mono_profiler_set_events (0);
714 mono_profiler_gc_heap_resize (gint64 new_size)
717 for (prof = prof_list; prof; prof = prof->next) {
718 if ((prof->events & MONO_PROFILE_GC) && prof->gc_heap_resize)
719 prof->gc_heap_resize (prof->profiler, new_size);
724 mono_profiler_gc_event (MonoGCEvent event, int generation)
727 for (prof = prof_list; prof; prof = prof->next) {
728 if ((prof->events & MONO_PROFILE_GC) && prof->gc_event)
729 prof->gc_event (prof->profiler, event, generation);
734 mono_profiler_gc_moves (void **objects, int num)
737 for (prof = prof_list; prof; prof = prof->next) {
738 if ((prof->events & MONO_PROFILE_GC_MOVES) && prof->gc_moves)
739 prof->gc_moves (prof->profiler, objects, num);
744 mono_profiler_install_gc (MonoProfileGCFunc callback, MonoProfileGCResizeFunc heap_resize_callback)
746 mono_gc_enable_events ();
749 prof_list->gc_event = callback;
750 prof_list->gc_heap_resize = heap_resize_callback;
754 * mono_profiler_install_gc_moves:
755 * @callback: callback function
757 * Install the @callback function that the GC will call when moving objects.
758 * The callback receives an array of pointers and the number of elements
759 * in the array. Every even element in the array is the original object location
760 * and the following odd element is the new location of the object in memory.
761 * So the number of elements argument will always be a multiple of 2.
762 * Since this callback happens during the GC, it is a restricted environment:
763 * no locks can be taken and the object pointers can be inspected only once
764 * the GC is finished (of course the original location pointers will not
765 * point to valid objects anymore).
768 mono_profiler_install_gc_moves (MonoProfileGCMoveFunc callback)
772 prof_list->gc_moves = callback;
776 mono_profiler_install_runtime_initialized (MonoProfileFunc runtime_initialized_callback)
780 prof_list->runtime_initialized_event = runtime_initialized_callback;
784 mono_profiler_runtime_initialized (void) {
786 for (prof = prof_list; prof; prof = prof->next) {
787 if (prof->runtime_initialized_event)
788 prof->runtime_initialized_event (prof->profiler);
793 mono_profiler_install_code_chunk_new (MonoProfilerCodeChunkNew callback) {
796 prof_list->code_chunk_new = callback;
799 mono_profiler_code_chunk_new (gpointer chunk, int size) {
801 for (prof = prof_list; prof; prof = prof->next) {
802 if (prof->code_chunk_new)
803 prof->code_chunk_new (prof->profiler, chunk, size);
808 mono_profiler_install_code_chunk_destroy (MonoProfilerCodeChunkDestroy callback) {
811 prof_list->code_chunk_destroy = callback;
814 mono_profiler_code_chunk_destroy (gpointer chunk) {
816 for (prof = prof_list; prof; prof = prof->next) {
817 if (prof->code_chunk_destroy)
818 prof->code_chunk_destroy (prof->profiler, chunk);
823 mono_profiler_install_code_buffer_new (MonoProfilerCodeBufferNew callback) {
826 prof_list->code_buffer_new = callback;
830 mono_profiler_install_iomap (MonoProfileIomapFunc callback)
834 prof_list->iomap_cb = callback;
838 mono_profiler_code_buffer_new (gpointer buffer, int size, MonoProfilerCodeBufferType type, void *data) {
840 for (prof = prof_list; prof; prof = prof->next) {
841 if (prof->code_buffer_new)
842 prof->code_buffer_new (prof->profiler, buffer, size, type, data);
846 static GHashTable *coverage_hash = NULL;
848 MonoProfileCoverageInfo*
849 mono_profiler_coverage_alloc (MonoMethod *method, int entries)
851 MonoProfileCoverageInfo *res;
852 int instrument = FALSE;
855 for (prof = prof_list; prof; prof = prof->next) {
856 /* note that we call the filter on all the profilers even if just
857 * a single one would be enough to instrument a method
859 if (prof->coverage_filter_cb)
860 if (prof->coverage_filter_cb (prof->profiler, method))
866 mono_profiler_coverage_lock ();
868 coverage_hash = g_hash_table_new (NULL, NULL);
870 res = g_malloc0 (sizeof (MonoProfileCoverageInfo) + sizeof (void*) * 2 * entries);
872 res->entries = entries;
874 g_hash_table_insert (coverage_hash, method, res);
875 mono_profiler_coverage_unlock ();
880 /* safe only when the method antive code has been unloaded */
882 mono_profiler_coverage_free (MonoMethod *method)
884 MonoProfileCoverageInfo* info;
886 mono_profiler_coverage_lock ();
887 if (!coverage_hash) {
888 mono_profiler_coverage_unlock ();
892 info = g_hash_table_lookup (coverage_hash, method);
895 g_hash_table_remove (coverage_hash, method);
897 mono_profiler_coverage_unlock ();
901 * mono_profiler_coverage_get:
902 * @prof: The profiler handle, installed with mono_profiler_install
903 * @method: the method to gather information from.
904 * @func: A routine that will be called back with the results
906 * If the MONO_PROFILER_INS_COVERAGE flag was active during JIT compilation
907 * it is posisble to obtain coverage information about a give method.
909 * The function @func will be invoked repeatedly with instances of the
910 * MonoProfileCoverageEntry structure.
913 mono_profiler_coverage_get (MonoProfiler *prof, MonoMethod *method, MonoProfileCoverageFunc func)
915 MonoProfileCoverageInfo* info;
918 const unsigned char *start, *end, *cil_code;
919 MonoMethodHeader *header;
920 MonoProfileCoverageEntry entry;
921 MonoDebugMethodInfo *debug_minfo;
923 mono_profiler_coverage_lock ();
924 info = g_hash_table_lookup (coverage_hash, method);
925 mono_profiler_coverage_unlock ();
930 header = mono_method_get_header (method);
931 start = mono_method_header_get_code (header, &code_size, NULL);
932 debug_minfo = mono_debug_lookup_method (method);
934 end = start + code_size;
935 for (i = 0; i < info->entries; ++i) {
936 cil_code = info->data [i].cil_code;
937 if (cil_code && cil_code >= start && cil_code < end) {
939 offset = cil_code - start;
940 entry.iloffset = offset;
941 entry.method = method;
942 entry.counter = info->data [i].count;
943 entry.line = entry.col = 1;
944 entry.filename = NULL;
946 MonoDebugSourceLocation *location;
948 location = mono_debug_symfile_lookup_location (debug_minfo, offset);
950 entry.line = location->row;
951 entry.col = location->column;
952 entry.filename = fname = g_strdup (location->source_file);
953 mono_debug_free_source_location (location);
961 mono_metadata_free_mh (header);
964 typedef void (*ProfilerInitializer) (const char*);
965 #define INITIALIZER_NAME "mono_profiler_startup"
968 * mono_profiler_load:
969 * @desc: arguments to configure the profiler
971 * Invoke this method to initialize the profiler. This will drive the
972 * loading of the internal ("default") or any external profilers.
974 * This routine is invoked by Mono's driver, but must be called manually
975 * if you embed Mono into your application.
978 mono_profiler_load (const char *desc)
980 mono_gc_base_init ();
982 #ifndef DISABLE_PROFILER
983 if (!desc || (strcmp ("default", desc) == 0) || (strncmp (desc, "default:", 8) == 0)) {
984 mono_profiler_install_simple (desc);
993 MonoDl *pmodule = NULL;
994 const char* col = strchr (desc, ':');
1001 mname = g_memdup (desc, col - desc + 1);
1002 mname [col - desc] = 0;
1004 mname = g_strdup (desc);
1006 libname = g_strdup_printf ("mono-profiler-%s", mname);
1009 while ((path = mono_dl_build_path (NULL, libname, &iter))) {
1011 pmodule = mono_dl_open (path, MONO_DL_LAZY, &err);
1013 ProfilerInitializer func;
1014 if ((err = mono_dl_symbol (pmodule, INITIALIZER_NAME, (gpointer *)&func))) {
1015 g_warning ("Cannot find initializer function %s in profiler module: %s (%s)", INITIALIZER_NAME, libname, err);
1026 g_warning ("Error loading profiler module '%s': %s", libname, err);