2 * profiler.c: Profiler interface for Mono
5 * Paolo Molaro (lupus@ximian.com)
6 * Alex Rønne Petersen (alexrp@xamarin.com)
8 * Copyright 2001-2003 Ximian, Inc (http://www.ximian.com)
9 * Copyright 2004-2009 Novell, Inc (http://www.novell.com)
10 * Copyright 2011 Xamarin Inc (http://www.xamarin.com).
14 #include "mono/metadata/profiler-private.h"
15 #include "mono/metadata/assembly.h"
16 #include "mono/metadata/debug-helpers.h"
17 #include "mono/metadata/mono-debug.h"
18 #include "mono/metadata/debug-mono-symfile.h"
19 #include "mono/metadata/metadata-internals.h"
20 #include "mono/metadata/class-internals.h"
21 #include "mono/metadata/domain-internals.h"
22 #include "mono/metadata/gc-internal.h"
23 #include "mono/metadata/mono-config-dirs.h"
24 #include "mono/io-layer/io-layer.h"
25 #include "mono/utils/mono-dl.h"
30 #ifdef HAVE_SYS_TIME_H
33 #ifdef HAVE_BACKTRACE_SYMBOLS
37 typedef struct _ProfilerDesc ProfilerDesc;
38 struct _ProfilerDesc {
40 MonoProfiler *profiler;
41 MonoProfileFlags events;
43 MonoProfileAppDomainFunc domain_start_load;
44 MonoProfileAppDomainResult domain_end_load;
45 MonoProfileAppDomainFunc domain_start_unload;
46 MonoProfileAppDomainFunc domain_end_unload;
48 MonoProfileAssemblyFunc assembly_start_load;
49 MonoProfileAssemblyResult assembly_end_load;
50 MonoProfileAssemblyFunc assembly_start_unload;
51 MonoProfileAssemblyFunc assembly_end_unload;
53 MonoProfileModuleFunc module_start_load;
54 MonoProfileModuleResult module_end_load;
55 MonoProfileModuleFunc module_start_unload;
56 MonoProfileModuleFunc module_end_unload;
58 MonoProfileClassFunc class_start_load;
59 MonoProfileClassResult class_end_load;
60 MonoProfileClassFunc class_start_unload;
61 MonoProfileClassFunc class_end_unload;
63 MonoProfileMethodFunc jit_start;
64 MonoProfileMethodResult jit_end;
65 MonoProfileJitResult jit_end2;
66 MonoProfileMethodFunc method_free;
67 MonoProfileMethodFunc method_start_invoke;
68 MonoProfileMethodFunc method_end_invoke;
69 MonoProfileMethodResult man_unman_transition;
70 MonoProfileAllocFunc allocation_cb;
71 MonoProfileMonitorFunc monitor_event_cb;
72 MonoProfileStatFunc statistical_cb;
73 MonoProfileStatCallChainFunc statistical_call_chain_cb;
74 int statistical_call_chain_depth;
75 MonoProfilerCallChainStrategy statistical_call_chain_strategy;
76 MonoProfileMethodFunc method_enter;
77 MonoProfileMethodFunc method_leave;
79 MonoProfileExceptionFunc exception_throw_cb;
80 MonoProfileMethodFunc exception_method_leave_cb;
81 MonoProfileExceptionClauseFunc exception_clause_cb;
83 MonoProfileIomapFunc iomap_cb;
85 MonoProfileThreadFunc thread_start;
86 MonoProfileThreadFunc thread_end;
87 MonoProfileThreadNameFunc thread_name;
89 MonoProfileCoverageFilterFunc coverage_filter_cb;
91 MonoProfileFunc shutdown_callback;
93 MonoProfileGCFunc gc_event;
94 MonoProfileGCResizeFunc gc_heap_resize;
95 MonoProfileGCMoveFunc gc_moves;
96 MonoProfileGCHandleFunc gc_handle;
97 MonoProfileGCRootFunc gc_roots;
99 MonoProfileFunc runtime_initialized_event;
101 MonoProfilerCodeChunkNew code_chunk_new;
102 MonoProfilerCodeChunkDestroy code_chunk_destroy;
103 MonoProfilerCodeBufferNew code_buffer_new;
106 static ProfilerDesc *prof_list = NULL;
108 #define mono_profiler_coverage_lock() mono_mutex_lock (&profiler_coverage_mutex)
109 #define mono_profiler_coverage_unlock() mono_mutex_unlock (&profiler_coverage_mutex)
110 static mono_mutex_t profiler_coverage_mutex;
112 /* this is directly accessible to other mono libs.
113 * It is the ORed value of all the profiler's events.
115 MonoProfileFlags mono_profiler_events;
118 * mono_profiler_install:
119 * @prof: a MonoProfiler structure pointer, or a pointer to a derived structure.
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, MonoProfileFunc callback)
131 ProfilerDesc *desc = g_new0 (ProfilerDesc, 1);
133 mono_mutex_init_recursive (&profiler_coverage_mutex);
134 desc->profiler = prof;
135 desc->shutdown_callback = callback;
136 desc->next = prof_list;
141 * mono_profiler_set_events:
142 * @events: an ORed set of values made up of MONO_PROFILER_ flags
144 * The events descriped in the @events argument is a set of flags
145 * that represent which profiling events must be triggered. For
146 * example if you have registered a set of methods for tracking
147 * JIT compilation start and end with mono_profiler_install_jit_compile,
148 * you will want to pass the MONO_PROFILE_JIT_COMPILATION flag to
151 * You can call mono_profile_set_events more than once and you can
152 * do this at runtime to modify which methods are invoked.
155 mono_profiler_set_events (MonoProfileFlags events)
158 MonoProfileFlags value = 0;
160 prof_list->events = events;
161 for (prof = prof_list; prof; prof = prof->next)
162 value |= prof->events;
163 mono_profiler_events = value;
167 * mono_profiler_get_events:
169 * Returns a list of active events that will be intercepted.
172 mono_profiler_get_events (void)
174 return mono_profiler_events;
178 * mono_profiler_install_enter_leave:
179 * @enter: the routine to be called on each method entry
180 * @fleave: the routine to be called each time a method returns
182 * Use this routine to install routines that will be called everytime
183 * a method enters and leaves. The routines will receive as an argument
184 * the MonoMethod representing the method that is entering or leaving.
187 mono_profiler_install_enter_leave (MonoProfileMethodFunc enter, MonoProfileMethodFunc fleave)
191 prof_list->method_enter = enter;
192 prof_list->method_leave = fleave;
196 * mono_profiler_install_jit_compile:
197 * @start: the routine to be called when the JIT process starts.
198 * @end: the routine to be called when the JIT process ends.
200 * Use this routine to install routines that will be called when JIT
201 * compilation of a method starts and completes.
204 mono_profiler_install_jit_compile (MonoProfileMethodFunc start, MonoProfileMethodResult end)
208 prof_list->jit_start = start;
209 prof_list->jit_end = end;
213 mono_profiler_install_jit_end (MonoProfileJitResult end)
217 prof_list->jit_end2 = end;
221 mono_profiler_install_method_free (MonoProfileMethodFunc callback)
225 prof_list->method_free = callback;
229 mono_profiler_install_method_invoke (MonoProfileMethodFunc start, MonoProfileMethodFunc end)
233 prof_list->method_start_invoke = start;
234 prof_list->method_end_invoke = end;
238 mono_profiler_install_thread (MonoProfileThreadFunc start, MonoProfileThreadFunc end)
242 prof_list->thread_start = start;
243 prof_list->thread_end = end;
247 mono_profiler_install_thread_name (MonoProfileThreadNameFunc thread_name_cb)
251 prof_list->thread_name = thread_name_cb;
255 mono_profiler_install_transition (MonoProfileMethodResult callback)
259 prof_list->man_unman_transition = callback;
263 mono_profiler_install_allocation (MonoProfileAllocFunc callback)
265 mono_gc_enable_alloc_events ();
268 prof_list->allocation_cb = callback;
272 mono_profiler_install_monitor (MonoProfileMonitorFunc callback)
276 prof_list->monitor_event_cb = callback;
279 static MonoProfileSamplingMode sampling_mode = MONO_PROFILER_STAT_MODE_PROCESS;
280 static int64_t sampling_frequency = 1000; //1ms
283 * mono_profiler_set_statistical_mode:
284 * @mode the sampling mode used.
285 * @sample_frequency_is_us the sampling frequency in microseconds.
287 * Set the sampling parameters for the profiler. Sampling mode affects the effective sampling rate as in samples/s you'll witness.
288 * The default sampling mode is process mode, which only reports samples when there's activity in the process.
290 * Sampling frequency should be interpreted as a suggestion that can't always be honored due to how most kernels expose alarms.
292 * Said that, when using statistical sampling, always assume variable rate sampling as all sort of external factors can interfere.
295 mono_profiler_set_statistical_mode (MonoProfileSamplingMode mode, int64_t sampling_frequency_is_us)
297 sampling_mode = mode;
298 sampling_frequency = sampling_frequency_is_us;
302 mono_profiler_install_statistical (MonoProfileStatFunc callback)
306 prof_list->statistical_cb = callback;
310 mono_profiler_get_sampling_rate (void)
312 return sampling_frequency;
315 MonoProfileSamplingMode
316 mono_profiler_get_sampling_mode (void)
318 return sampling_mode;
322 mono_profiler_install_statistical_call_chain (MonoProfileStatCallChainFunc callback, int call_chain_depth, MonoProfilerCallChainStrategy call_chain_strategy) {
325 if (call_chain_depth > MONO_PROFILER_MAX_STAT_CALL_CHAIN_DEPTH) {
326 call_chain_depth = MONO_PROFILER_MAX_STAT_CALL_CHAIN_DEPTH;
328 if ((call_chain_strategy >= MONO_PROFILER_CALL_CHAIN_INVALID) || (call_chain_strategy < MONO_PROFILER_CALL_CHAIN_NONE)) {
329 call_chain_strategy = MONO_PROFILER_CALL_CHAIN_NONE;
331 prof_list->statistical_call_chain_cb = callback;
332 prof_list->statistical_call_chain_depth = call_chain_depth;
333 prof_list->statistical_call_chain_strategy = call_chain_strategy;
337 mono_profiler_stat_get_call_chain_depth (void) {
338 if (prof_list && prof_list->statistical_call_chain_cb != NULL) {
339 return prof_list->statistical_call_chain_depth;
345 MonoProfilerCallChainStrategy
346 mono_profiler_stat_get_call_chain_strategy (void) {
347 if (prof_list && prof_list->statistical_call_chain_cb != NULL) {
348 return prof_list->statistical_call_chain_strategy;
350 return MONO_PROFILER_CALL_CHAIN_NONE;
354 void mono_profiler_install_exception (MonoProfileExceptionFunc throw_callback, MonoProfileMethodFunc exc_method_leave, MonoProfileExceptionClauseFunc clause_callback)
358 prof_list->exception_throw_cb = throw_callback;
359 prof_list->exception_method_leave_cb = exc_method_leave;
360 prof_list->exception_clause_cb = clause_callback;
364 mono_profiler_install_coverage_filter (MonoProfileCoverageFilterFunc callback)
368 prof_list->coverage_filter_cb = callback;
372 mono_profiler_install_appdomain (MonoProfileAppDomainFunc start_load, MonoProfileAppDomainResult end_load,
373 MonoProfileAppDomainFunc start_unload, MonoProfileAppDomainFunc end_unload)
378 prof_list->domain_start_load = start_load;
379 prof_list->domain_end_load = end_load;
380 prof_list->domain_start_unload = start_unload;
381 prof_list->domain_end_unload = end_unload;
385 mono_profiler_install_assembly (MonoProfileAssemblyFunc start_load, MonoProfileAssemblyResult end_load,
386 MonoProfileAssemblyFunc start_unload, MonoProfileAssemblyFunc end_unload)
390 prof_list->assembly_start_load = start_load;
391 prof_list->assembly_end_load = end_load;
392 prof_list->assembly_start_unload = start_unload;
393 prof_list->assembly_end_unload = end_unload;
397 mono_profiler_install_module (MonoProfileModuleFunc start_load, MonoProfileModuleResult end_load,
398 MonoProfileModuleFunc start_unload, MonoProfileModuleFunc end_unload)
402 prof_list->module_start_load = start_load;
403 prof_list->module_end_load = end_load;
404 prof_list->module_start_unload = start_unload;
405 prof_list->module_end_unload = end_unload;
409 mono_profiler_install_class (MonoProfileClassFunc start_load, MonoProfileClassResult end_load,
410 MonoProfileClassFunc start_unload, MonoProfileClassFunc end_unload)
414 prof_list->class_start_load = start_load;
415 prof_list->class_end_load = end_load;
416 prof_list->class_start_unload = start_unload;
417 prof_list->class_end_unload = end_unload;
421 mono_profiler_method_enter (MonoMethod *method)
424 for (prof = prof_list; prof; prof = prof->next) {
425 if ((prof->events & MONO_PROFILE_ENTER_LEAVE) && prof->method_enter)
426 prof->method_enter (prof->profiler, method);
431 mono_profiler_method_leave (MonoMethod *method)
434 for (prof = prof_list; prof; prof = prof->next) {
435 if ((prof->events & MONO_PROFILE_ENTER_LEAVE) && prof->method_leave)
436 prof->method_leave (prof->profiler, method);
441 mono_profiler_method_jit (MonoMethod *method)
444 for (prof = prof_list; prof; prof = prof->next) {
445 if ((prof->events & MONO_PROFILE_JIT_COMPILATION) && prof->jit_start)
446 prof->jit_start (prof->profiler, method);
451 mono_profiler_method_end_jit (MonoMethod *method, MonoJitInfo* jinfo, int result)
454 for (prof = prof_list; prof; prof = prof->next) {
455 if ((prof->events & MONO_PROFILE_JIT_COMPILATION)) {
457 prof->jit_end (prof->profiler, method, result);
459 prof->jit_end2 (prof->profiler, method, jinfo, result);
465 mono_profiler_method_free (MonoMethod *method)
468 for (prof = prof_list; prof; prof = prof->next) {
469 if ((prof->events & MONO_PROFILE_METHOD_EVENTS) && prof->method_free)
470 prof->method_free (prof->profiler, method);
475 mono_profiler_method_start_invoke (MonoMethod *method)
478 for (prof = prof_list; prof; prof = prof->next) {
479 if ((prof->events & MONO_PROFILE_METHOD_EVENTS) && prof->method_start_invoke)
480 prof->method_start_invoke (prof->profiler, method);
485 mono_profiler_method_end_invoke (MonoMethod *method)
488 for (prof = prof_list; prof; prof = prof->next) {
489 if ((prof->events & MONO_PROFILE_METHOD_EVENTS) && prof->method_end_invoke)
490 prof->method_end_invoke (prof->profiler, method);
495 mono_profiler_code_transition (MonoMethod *method, int result)
498 for (prof = prof_list; prof; prof = prof->next) {
499 if ((prof->events & MONO_PROFILE_TRANSITIONS) && prof->man_unman_transition)
500 prof->man_unman_transition (prof->profiler, method, result);
505 mono_profiler_allocation (MonoObject *obj)
508 for (prof = prof_list; prof; prof = prof->next) {
509 if ((prof->events & MONO_PROFILE_ALLOCATIONS) && prof->allocation_cb)
510 prof->allocation_cb (prof->profiler, obj, obj->vtable->klass);
515 mono_profiler_monitor_event (MonoObject *obj, MonoProfilerMonitorEvent event) {
517 for (prof = prof_list; prof; prof = prof->next) {
518 if ((prof->events & MONO_PROFILE_MONITOR_EVENTS) && prof->monitor_event_cb)
519 prof->monitor_event_cb (prof->profiler, obj, event);
524 mono_profiler_stat_hit (guchar *ip, void *context)
527 for (prof = prof_list; prof; prof = prof->next) {
528 if ((prof->events & MONO_PROFILE_STATISTICAL) && prof->statistical_cb)
529 prof->statistical_cb (prof->profiler, ip, context);
534 mono_profiler_stat_call_chain (int call_chain_depth, guchar **ips, void *context)
537 for (prof = prof_list; prof; prof = prof->next) {
538 if ((prof->events & MONO_PROFILE_STATISTICAL) && prof->statistical_call_chain_cb)
539 prof->statistical_call_chain_cb (prof->profiler, call_chain_depth, ips, context);
544 mono_profiler_exception_thrown (MonoObject *exception)
547 for (prof = prof_list; prof; prof = prof->next) {
548 if ((prof->events & MONO_PROFILE_EXCEPTIONS) && prof->exception_throw_cb)
549 prof->exception_throw_cb (prof->profiler, exception);
554 mono_profiler_exception_method_leave (MonoMethod *method)
557 for (prof = prof_list; prof; prof = prof->next) {
558 if ((prof->events & MONO_PROFILE_EXCEPTIONS) && prof->exception_method_leave_cb)
559 prof->exception_method_leave_cb (prof->profiler, method);
564 mono_profiler_exception_clause_handler (MonoMethod *method, int clause_type, int clause_num)
567 for (prof = prof_list; prof; prof = prof->next) {
568 if ((prof->events & MONO_PROFILE_EXCEPTIONS) && prof->exception_clause_cb)
569 prof->exception_clause_cb (prof->profiler, method, clause_type, clause_num);
574 mono_profiler_thread_start (gsize tid)
577 for (prof = prof_list; prof; prof = prof->next) {
578 if ((prof->events & MONO_PROFILE_THREADS) && prof->thread_start)
579 prof->thread_start (prof->profiler, tid);
584 mono_profiler_thread_end (gsize tid)
587 for (prof = prof_list; prof; prof = prof->next) {
588 if ((prof->events & MONO_PROFILE_THREADS) && prof->thread_end)
589 prof->thread_end (prof->profiler, tid);
594 mono_profiler_thread_name (gsize tid, const char *name)
597 for (prof = prof_list; prof; prof = prof->next) {
598 if ((prof->events & MONO_PROFILE_THREADS) && prof->thread_name)
599 prof->thread_name (prof->profiler, tid, name);
604 mono_profiler_assembly_event (MonoAssembly *assembly, int code)
607 for (prof = prof_list; prof; prof = prof->next) {
608 if (!(prof->events & MONO_PROFILE_ASSEMBLY_EVENTS))
612 case MONO_PROFILE_START_LOAD:
613 if (prof->assembly_start_load)
614 prof->assembly_start_load (prof->profiler, assembly);
616 case MONO_PROFILE_START_UNLOAD:
617 if (prof->assembly_start_unload)
618 prof->assembly_start_unload (prof->profiler, assembly);
620 case MONO_PROFILE_END_UNLOAD:
621 if (prof->assembly_end_unload)
622 prof->assembly_end_unload (prof->profiler, assembly);
625 g_assert_not_reached ();
631 mono_profiler_assembly_loaded (MonoAssembly *assembly, int result)
634 for (prof = prof_list; prof; prof = prof->next) {
635 if ((prof->events & MONO_PROFILE_ASSEMBLY_EVENTS) && prof->assembly_end_load)
636 prof->assembly_end_load (prof->profiler, assembly, result);
640 void mono_profiler_iomap (char *report, const char *pathname, const char *new_pathname)
643 for (prof = prof_list; prof; prof = prof->next) {
644 if ((prof->events & MONO_PROFILE_IOMAP_EVENTS) && prof->iomap_cb)
645 prof->iomap_cb (prof->profiler, report, pathname, new_pathname);
650 mono_profiler_module_event (MonoImage *module, int code)
653 for (prof = prof_list; prof; prof = prof->next) {
654 if (!(prof->events & MONO_PROFILE_MODULE_EVENTS))
658 case MONO_PROFILE_START_LOAD:
659 if (prof->module_start_load)
660 prof->module_start_load (prof->profiler, module);
662 case MONO_PROFILE_START_UNLOAD:
663 if (prof->module_start_unload)
664 prof->module_start_unload (prof->profiler, module);
666 case MONO_PROFILE_END_UNLOAD:
667 if (prof->module_end_unload)
668 prof->module_end_unload (prof->profiler, module);
671 g_assert_not_reached ();
677 mono_profiler_module_loaded (MonoImage *module, int result)
680 for (prof = prof_list; prof; prof = prof->next) {
681 if ((prof->events & MONO_PROFILE_MODULE_EVENTS) && prof->module_end_load)
682 prof->module_end_load (prof->profiler, module, result);
687 mono_profiler_class_event (MonoClass *klass, int code)
690 for (prof = prof_list; prof; prof = prof->next) {
691 if (!(prof->events & MONO_PROFILE_CLASS_EVENTS))
695 case MONO_PROFILE_START_LOAD:
696 if (prof->class_start_load)
697 prof->class_start_load (prof->profiler, klass);
699 case MONO_PROFILE_START_UNLOAD:
700 if (prof->class_start_unload)
701 prof->class_start_unload (prof->profiler, klass);
703 case MONO_PROFILE_END_UNLOAD:
704 if (prof->class_end_unload)
705 prof->class_end_unload (prof->profiler, klass);
708 g_assert_not_reached ();
714 mono_profiler_class_loaded (MonoClass *klass, int result)
717 for (prof = prof_list; prof; prof = prof->next) {
718 if ((prof->events & MONO_PROFILE_CLASS_EVENTS) && prof->class_end_load)
719 prof->class_end_load (prof->profiler, klass, result);
724 mono_profiler_appdomain_event (MonoDomain *domain, int code)
727 for (prof = prof_list; prof; prof = prof->next) {
728 if (!(prof->events & MONO_PROFILE_APPDOMAIN_EVENTS))
732 case MONO_PROFILE_START_LOAD:
733 if (prof->domain_start_load)
734 prof->domain_start_load (prof->profiler, domain);
736 case MONO_PROFILE_START_UNLOAD:
737 if (prof->domain_start_unload)
738 prof->domain_start_unload (prof->profiler, domain);
740 case MONO_PROFILE_END_UNLOAD:
741 if (prof->domain_end_unload)
742 prof->domain_end_unload (prof->profiler, domain);
745 g_assert_not_reached ();
751 mono_profiler_appdomain_loaded (MonoDomain *domain, int result)
754 for (prof = prof_list; prof; prof = prof->next) {
755 if ((prof->events & MONO_PROFILE_APPDOMAIN_EVENTS) && prof->domain_end_load)
756 prof->domain_end_load (prof->profiler, domain, result);
761 mono_profiler_shutdown (void)
764 for (prof = prof_list; prof; prof = prof->next) {
765 if (prof->shutdown_callback)
766 prof->shutdown_callback (prof->profiler);
769 mono_profiler_set_events (0);
773 mono_profiler_gc_heap_resize (gint64 new_size)
776 for (prof = prof_list; prof; prof = prof->next) {
777 if ((prof->events & MONO_PROFILE_GC) && prof->gc_heap_resize)
778 prof->gc_heap_resize (prof->profiler, new_size);
783 mono_profiler_gc_event (MonoGCEvent event, int generation)
786 for (prof = prof_list; prof; prof = prof->next) {
787 if ((prof->events & MONO_PROFILE_GC) && prof->gc_event)
788 prof->gc_event (prof->profiler, event, generation);
793 mono_profiler_gc_moves (void **objects, int num)
796 for (prof = prof_list; prof; prof = prof->next) {
797 if ((prof->events & MONO_PROFILE_GC_MOVES) && prof->gc_moves)
798 prof->gc_moves (prof->profiler, objects, num);
803 mono_profiler_gc_handle (int op, int type, uintptr_t handle, MonoObject *obj)
806 for (prof = prof_list; prof; prof = prof->next) {
807 if ((prof->events & MONO_PROFILE_GC_ROOTS) && prof->gc_handle)
808 prof->gc_handle (prof->profiler, op, type, handle, obj);
813 mono_profiler_gc_roots (int num, void **objects, int *root_types, uintptr_t *extra_info)
816 for (prof = prof_list; prof; prof = prof->next) {
817 if ((prof->events & MONO_PROFILE_GC_ROOTS) && prof->gc_roots)
818 prof->gc_roots (prof->profiler, num, objects, root_types, extra_info);
823 mono_profiler_install_gc (MonoProfileGCFunc callback, MonoProfileGCResizeFunc heap_resize_callback)
825 mono_gc_enable_events ();
828 prof_list->gc_event = callback;
829 prof_list->gc_heap_resize = heap_resize_callback;
833 * mono_profiler_install_gc_moves:
834 * @callback: callback function
836 * Install the @callback function that the GC will call when moving objects.
837 * The callback receives an array of pointers and the number of elements
838 * in the array. Every even element in the array is the original object location
839 * and the following odd element is the new location of the object in memory.
840 * So the number of elements argument will always be a multiple of 2.
841 * Since this callback happens during the GC, it is a restricted environment:
842 * no locks can be taken and the object pointers can be inspected only once
843 * the GC is finished (of course the original location pointers will not
844 * point to valid objects anymore).
847 mono_profiler_install_gc_moves (MonoProfileGCMoveFunc callback)
851 prof_list->gc_moves = callback;
855 * mono_profiler_install_gc_roots:
856 * @handle_callback: callback function
857 * @roots_callback: callback function
859 * Install the @handle_callback function that the GC will call when GC
860 * handles are created or destroyed.
861 * The callback receives an operation, which is either #MONO_PROFILER_GC_HANDLE_CREATED
862 * or #MONO_PROFILER_GC_HANDLE_DESTROYED, the handle type, the handle value and the
863 * object pointer, if present.
864 * Install the @roots_callback function that the GC will call when tracing
865 * the roots for a collection.
866 * The callback receives the number of elements and three arrays: an array
867 * of objects, an array of root types and flags and an array of extra info.
868 * The size of each array is given by the first argument.
871 mono_profiler_install_gc_roots (MonoProfileGCHandleFunc handle_callback, MonoProfileGCRootFunc roots_callback)
875 prof_list->gc_handle = handle_callback;
876 prof_list->gc_roots = roots_callback;
880 mono_profiler_install_runtime_initialized (MonoProfileFunc runtime_initialized_callback)
884 prof_list->runtime_initialized_event = runtime_initialized_callback;
888 mono_profiler_runtime_initialized (void) {
890 for (prof = prof_list; prof; prof = prof->next) {
891 if (prof->runtime_initialized_event)
892 prof->runtime_initialized_event (prof->profiler);
897 mono_profiler_install_code_chunk_new (MonoProfilerCodeChunkNew callback) {
900 prof_list->code_chunk_new = callback;
903 mono_profiler_code_chunk_new (gpointer chunk, int size) {
905 for (prof = prof_list; prof; prof = prof->next) {
906 if (prof->code_chunk_new)
907 prof->code_chunk_new (prof->profiler, chunk, size);
912 mono_profiler_install_code_chunk_destroy (MonoProfilerCodeChunkDestroy callback) {
915 prof_list->code_chunk_destroy = callback;
918 mono_profiler_code_chunk_destroy (gpointer chunk) {
920 for (prof = prof_list; prof; prof = prof->next) {
921 if (prof->code_chunk_destroy)
922 prof->code_chunk_destroy (prof->profiler, chunk);
927 mono_profiler_install_code_buffer_new (MonoProfilerCodeBufferNew callback) {
930 prof_list->code_buffer_new = callback;
934 mono_profiler_install_iomap (MonoProfileIomapFunc callback)
938 prof_list->iomap_cb = callback;
942 mono_profiler_code_buffer_new (gpointer buffer, int size, MonoProfilerCodeBufferType type, gconstpointer data) {
944 for (prof = prof_list; prof; prof = prof->next) {
945 if (prof->code_buffer_new)
946 prof->code_buffer_new (prof->profiler, buffer, size, type, (void*)data);
950 static GHashTable *coverage_hash = NULL;
952 MonoProfileCoverageInfo*
953 mono_profiler_coverage_alloc (MonoMethod *method, int entries)
955 MonoProfileCoverageInfo *res;
956 int instrument = FALSE;
959 for (prof = prof_list; prof; prof = prof->next) {
960 /* note that we call the filter on all the profilers even if just
961 * a single one would be enough to instrument a method
963 if (prof->coverage_filter_cb)
964 if (prof->coverage_filter_cb (prof->profiler, method))
970 mono_profiler_coverage_lock ();
972 coverage_hash = g_hash_table_new (NULL, NULL);
974 res = g_malloc0 (sizeof (MonoProfileCoverageInfo) + sizeof (void*) * 2 * entries);
976 res->entries = entries;
978 g_hash_table_insert (coverage_hash, method, res);
979 mono_profiler_coverage_unlock ();
984 /* safe only when the method antive code has been unloaded */
986 mono_profiler_coverage_free (MonoMethod *method)
988 MonoProfileCoverageInfo* info;
990 mono_profiler_coverage_lock ();
991 if (!coverage_hash) {
992 mono_profiler_coverage_unlock ();
996 info = g_hash_table_lookup (coverage_hash, method);
999 g_hash_table_remove (coverage_hash, method);
1001 mono_profiler_coverage_unlock ();
1005 * mono_profiler_coverage_get:
1006 * @prof: The profiler handle, installed with mono_profiler_install
1007 * @method: the method to gather information from.
1008 * @func: A routine that will be called back with the results
1010 * If the MONO_PROFILER_INS_COVERAGE flag was active during JIT compilation
1011 * it is posisble to obtain coverage information about a give method.
1013 * The function @func will be invoked repeatedly with instances of the
1014 * MonoProfileCoverageEntry structure.
1017 mono_profiler_coverage_get (MonoProfiler *prof, MonoMethod *method, MonoProfileCoverageFunc func)
1019 MonoProfileCoverageInfo* info = NULL;
1022 const unsigned char *start, *end, *cil_code;
1023 MonoMethodHeader *header;
1024 MonoProfileCoverageEntry entry;
1025 MonoDebugMethodInfo *debug_minfo;
1027 mono_profiler_coverage_lock ();
1029 info = g_hash_table_lookup (coverage_hash, method);
1030 mono_profiler_coverage_unlock ();
1035 header = mono_method_get_header (method);
1036 start = mono_method_header_get_code (header, &code_size, NULL);
1037 debug_minfo = mono_debug_lookup_method (method);
1039 end = start + code_size;
1040 for (i = 0; i < info->entries; ++i) {
1041 cil_code = info->data [i].cil_code;
1042 if (cil_code && cil_code >= start && cil_code < end) {
1044 offset = cil_code - start;
1045 entry.iloffset = offset;
1046 entry.method = method;
1047 entry.counter = info->data [i].count;
1048 entry.line = entry.col = 1;
1049 entry.filename = NULL;
1051 MonoDebugSourceLocation *location;
1053 location = mono_debug_symfile_lookup_location (debug_minfo, offset);
1055 entry.line = location->row;
1056 entry.col = location->column;
1057 entry.filename = fname = g_strdup (location->source_file);
1058 mono_debug_free_source_location (location);
1062 func (prof, &entry);
1066 mono_metadata_free_mh (header);
1069 typedef void (*ProfilerInitializer) (const char*);
1070 #define INITIALIZER_NAME "mono_profiler_startup"
1074 load_profiler (MonoDl *pmodule, const char *desc, const char *symbol)
1077 ProfilerInitializer func;
1082 if ((err = mono_dl_symbol (pmodule, symbol, (gpointer *) &func))) {
1092 load_embedded_profiler (const char *desc, const char *name)
1096 MonoDl *pmodule = NULL;
1099 pmodule = mono_dl_open (NULL, MONO_DL_LAZY, &err);
1101 g_warning ("Could not open main executable (%s)", err);
1106 symbol = g_strdup_printf (INITIALIZER_NAME "_%s", name);
1107 result = load_profiler (pmodule, desc, symbol);
1114 load_profiler_from_directory (const char *directory, const char *libname, const char *desc)
1116 MonoDl *pmodule = NULL;
1123 while ((path = mono_dl_build_path (directory, libname, &iter))) {
1124 pmodule = mono_dl_open (path, MONO_DL_LAZY, &err);
1128 return load_profiler (pmodule, desc, INITIALIZER_NAME);
1135 load_profiler_from_mono_instalation (const char *libname, const char *desc)
1138 MonoDl *pmodule = mono_dl_open_runtime_lib (libname, MONO_DL_LAZY, &err);
1141 return load_profiler (pmodule, desc, INITIALIZER_NAME);
1146 * mono_profiler_load:
1147 * @desc: arguments to configure the profiler
1149 * Invoke this method to initialize the profiler. This will drive the
1150 * loading of the internal ("default") or any external profilers.
1152 * This routine is invoked by Mono's driver, but must be called manually
1153 * if you embed Mono into your application.
1156 mono_profiler_load (const char *desc)
1159 mono_gc_base_init ();
1161 if (!desc || (strcmp ("default", desc) == 0)) {
1162 desc = "log:report";
1164 /* we keep command-line compat with the old version here */
1165 if (strncmp (desc, "default:", 8) == 0) {
1166 gchar **args, **ptr;
1167 GString *str = g_string_new ("log:report");
1168 args = g_strsplit (desc + 8, ",", -1);
1169 for (ptr = args; ptr && *ptr; ptr++) {
1170 const char *arg = *ptr;
1172 if (!strcmp (arg, "time"))
1173 g_string_append (str, ",calls");
1174 else if (!strcmp (arg, "alloc"))
1175 g_string_append (str, ",alloc");
1176 else if (!strcmp (arg, "stat"))
1177 g_string_append (str, ",sample");
1178 else if (!strcmp (arg, "jit"))
1179 continue; /* accept and do nothing */
1180 else if (strncmp (arg, "file=", 5) == 0) {
1181 g_string_append_printf (str, ",output=%s", arg + 5);
1183 fprintf (stderr, "profiler : Unknown argument '%s'.\n", arg);
1187 desc = cdesc = g_string_free (str, FALSE);
1190 const char* col = strchr (desc, ':');
1193 gboolean res = FALSE;
1196 mname = g_memdup (desc, col - desc + 1);
1197 mname [col - desc] = 0;
1199 mname = g_strdup (desc);
1201 if (!load_embedded_profiler (desc, mname)) {
1202 libname = g_strdup_printf ("mono-profiler-%s", mname);
1203 if (mono_config_get_assemblies_dir ())
1204 res = load_profiler_from_directory (mono_assembly_getrootdir (), libname, desc);
1206 res = load_profiler_from_directory (NULL, libname, desc);
1208 res = load_profiler_from_mono_instalation (libname, desc);
1210 g_warning ("The '%s' profiler wasn't found in the main executable nor could it be loaded from '%s'.", mname, libname);