Merge pull request #4327 from vkargov/vk-abcremedy
[mono.git] / mono / metadata / profiler.c
1 /**
2  * \file
3  * Profiler interface for Mono
4  *
5  * Author:
6  *   Paolo Molaro (lupus@ximian.com)
7  *   Alex Rønne Petersen (alexrp@xamarin.com)
8  *
9  * Copyright 2001-2003 Ximian, Inc (http://www.ximian.com)
10  * Copyright 2004-2009 Novell, Inc (http://www.novell.com)
11  * Copyright 2011 Xamarin Inc (http://www.xamarin.com).
12  * Licensed under the MIT license. See LICENSE file in the project root for full license information.
13  */
14
15 #include "config.h"
16 #include "mono/metadata/profiler-private.h"
17 #include "mono/metadata/assembly.h"
18 #include "mono/metadata/debug-helpers.h"
19 #include "mono/metadata/mono-debug.h"
20 #include "mono/metadata/debug-internals.h"
21 #include "mono/metadata/metadata-internals.h"
22 #include "mono/metadata/class-internals.h"
23 #include "mono/metadata/domain-internals.h"
24 #include "mono/metadata/gc-internals.h"
25 #include "mono/metadata/mono-config-dirs.h"
26 #include "mono/utils/mono-dl.h"
27 #include <mono/utils/mono-logger-internals.h>
28 #include <string.h>
29 #ifdef HAVE_UNISTD_H
30 #include <unistd.h>
31 #endif
32 #ifdef HAVE_SYS_TIME_H
33 #include <sys/time.h>
34 #endif
35 #ifdef HAVE_BACKTRACE_SYMBOLS
36 #include <execinfo.h>
37 #endif
38
39 typedef struct _ProfilerDesc ProfilerDesc;
40 struct _ProfilerDesc {
41         ProfilerDesc *next;
42         MonoProfiler *profiler;
43         MonoProfileFlags events;
44
45         MonoProfileAppDomainFunc   domain_start_load;
46         MonoProfileAppDomainResult domain_end_load;
47         MonoProfileAppDomainFunc   domain_start_unload;
48         MonoProfileAppDomainFunc   domain_end_unload;
49         MonoProfileAppDomainFriendlyNameFunc   domain_name;
50
51         MonoProfileContextFunc context_load;
52         MonoProfileContextFunc context_unload;
53
54         MonoProfileAssemblyFunc   assembly_start_load;
55         MonoProfileAssemblyResult assembly_end_load;
56         MonoProfileAssemblyFunc   assembly_start_unload;
57         MonoProfileAssemblyFunc   assembly_end_unload;
58
59         MonoProfileModuleFunc   module_start_load;
60         MonoProfileModuleResult module_end_load;
61         MonoProfileModuleFunc   module_start_unload;
62         MonoProfileModuleFunc   module_end_unload;
63
64         MonoProfileClassFunc   class_start_load;
65         MonoProfileClassResult class_end_load;
66         MonoProfileClassFunc   class_start_unload;
67         MonoProfileClassFunc   class_end_unload;
68
69         MonoProfileMethodFunc   jit_start;
70         MonoProfileMethodResult jit_end;
71         MonoProfileJitResult    jit_end2;
72         MonoProfileMethodFunc   method_free;
73         MonoProfileMethodFunc   method_start_invoke;
74         MonoProfileMethodFunc   method_end_invoke;
75         MonoProfileMethodResult man_unman_transition;
76         MonoProfileAllocFunc    allocation_cb;
77         MonoProfileMonitorFunc  monitor_event_cb;
78         MonoProfileStatFunc     statistical_cb;
79         MonoProfileStatCallChainFunc statistical_call_chain_cb;
80         int                     statistical_call_chain_depth;
81         MonoProfilerCallChainStrategy  statistical_call_chain_strategy;
82         MonoProfileMethodFunc   method_enter;
83         MonoProfileMethodFunc   method_leave;
84
85         MonoProfileExceptionFunc        exception_throw_cb;
86         MonoProfileMethodFunc exception_method_leave_cb;
87         MonoProfileExceptionClauseFunc exception_clause_cb;
88
89         MonoProfileIomapFunc iomap_cb;
90
91         MonoProfileThreadFunc   thread_start;
92         MonoProfileThreadFunc   thread_end;
93         MonoProfileThreadNameFunc   thread_name;
94
95         MonoProfileCoverageFilterFunc coverage_filter_cb;
96
97         MonoProfileFunc shutdown_callback;
98
99         MonoProfileGCFunc        gc_event;
100         MonoProfileGCResizeFunc  gc_heap_resize;
101         MonoProfileGCMoveFunc    gc_moves;
102         MonoProfileGCHandleFunc  gc_handle;
103         MonoProfileGCRootFunc    gc_roots;
104
105         MonoProfileGCFinalizeFunc gc_finalize_begin;
106         MonoProfileGCFinalizeObjectFunc gc_finalize_object_begin;
107         MonoProfileGCFinalizeObjectFunc gc_finalize_object_end;
108         MonoProfileGCFinalizeFunc gc_finalize_end;
109
110         MonoProfileFunc          runtime_initialized_event;
111
112         MonoProfilerCodeChunkNew code_chunk_new;
113         MonoProfilerCodeChunkDestroy code_chunk_destroy;
114         MonoProfilerCodeBufferNew code_buffer_new;
115 };
116
117 static ProfilerDesc *prof_list = NULL;
118
119 #define mono_profiler_coverage_lock() mono_os_mutex_lock (&profiler_coverage_mutex)
120 #define mono_profiler_coverage_unlock() mono_os_mutex_unlock (&profiler_coverage_mutex)
121 static mono_mutex_t profiler_coverage_mutex;
122
123 /* this is directly accessible to other mono libs.
124  * It is the ORed value of all the profiler's events.
125  */
126 MonoProfileFlags mono_profiler_events;
127
128 /**
129  * mono_profiler_install:
130  * \param prof a \c MonoProfiler structure pointer, or a pointer to a derived structure.
131  * \param callback the function to invoke at shutdown
132  * Use \c mono_profiler_install to activate profiling in the Mono runtime.
133  * Typically developers of new profilers will create a new structure whose
134  * first field is a \c MonoProfiler and put any extra information that they need
135  * to access from the various profiling callbacks there.
136  */
137 void
138 mono_profiler_install (MonoProfiler *prof, MonoProfileFunc callback)
139 {
140         ProfilerDesc *desc = g_new0 (ProfilerDesc, 1);
141         if (!prof_list)
142                 mono_os_mutex_init_recursive (&profiler_coverage_mutex);
143         desc->profiler = prof;
144         desc->shutdown_callback = callback;
145         desc->next = prof_list;
146         prof_list = desc;
147 }
148
149 /**
150  * mono_profiler_set_events:
151  * \param events an ORed set of values made up of \c MONO_PROFILER_ flags
152  * The events described in the \p events argument is a set of flags
153  * that represent which profiling events must be triggered.  For
154  * example if you have registered a set of methods for tracking
155  * JIT compilation start and end with \c mono_profiler_install_jit_compile,
156  * you will want to pass the \c MONO_PROFILE_JIT_COMPILATION flag to
157  * this routine.
158  *
159  * You can call \c mono_profile_set_events more than once and you can
160  * do this at runtime to modify which methods are invoked.
161  */
162 void
163 mono_profiler_set_events (MonoProfileFlags events)
164 {
165         ProfilerDesc *prof;
166         MonoProfileFlags value = (MonoProfileFlags)0;
167         if (prof_list)
168                 prof_list->events = events;
169         for (prof = prof_list; prof; prof = prof->next)
170                 value = (MonoProfileFlags)(value | prof->events);
171         mono_profiler_events = value;
172 }
173
174 /**
175  * mono_profiler_get_events:
176  *
177  * Returns a list of active events that will be intercepted. 
178  */
179 MonoProfileFlags
180 mono_profiler_get_events (void)
181 {
182         return mono_profiler_events;
183 }
184
185 /**
186  * mono_profiler_install_enter_leave:
187  * \param enter the routine to be called on each method entry
188  * \param fleave the routine to be called each time a method returns
189  *
190  * Use this routine to install routines that will be called everytime
191  * a method enters and leaves.   The routines will receive as an argument
192  * the \c MonoMethod representing the method that is entering or leaving.
193  */
194 void
195 mono_profiler_install_enter_leave (MonoProfileMethodFunc enter, MonoProfileMethodFunc fleave)
196 {
197         if (!prof_list)
198                 return;
199         prof_list->method_enter = enter;
200         prof_list->method_leave = fleave;
201 }
202
203 /**
204  * mono_profiler_install_jit_compile:
205  * \param start the routine to be called when the JIT process starts.
206  * \param end the routine to be called when the JIT process ends.
207  *
208  * Use this routine to install routines that will be called when JIT 
209  * compilation of a method starts and completes.
210  */
211 void 
212 mono_profiler_install_jit_compile (MonoProfileMethodFunc start, MonoProfileMethodResult end)
213 {
214         if (!prof_list)
215                 return;
216         prof_list->jit_start = start;
217         prof_list->jit_end = end;
218 }
219
220 void 
221 mono_profiler_install_jit_end (MonoProfileJitResult end)
222 {
223         if (!prof_list)
224                 return;
225         prof_list->jit_end2 = end;
226 }
227
228 void 
229 mono_profiler_install_method_free (MonoProfileMethodFunc callback)
230 {
231         if (!prof_list)
232                 return;
233         prof_list->method_free = callback;
234 }
235
236 void
237 mono_profiler_install_method_invoke (MonoProfileMethodFunc start, MonoProfileMethodFunc end)
238 {
239         if (!prof_list)
240                 return;
241         prof_list->method_start_invoke = start;
242         prof_list->method_end_invoke = end;
243 }
244
245 /**
246  * mono_profiler_install_thread:
247  */
248 void 
249 mono_profiler_install_thread (MonoProfileThreadFunc start, MonoProfileThreadFunc end)
250 {
251         if (!prof_list)
252                 return;
253         prof_list->thread_start = start;
254         prof_list->thread_end = end;
255 }
256
257 void 
258 mono_profiler_install_thread_name (MonoProfileThreadNameFunc thread_name_cb)
259 {
260         if (!prof_list)
261                 return;
262         prof_list->thread_name = thread_name_cb;
263 }
264
265 /**
266  * mono_profiler_install_transition:
267  */
268 void 
269 mono_profiler_install_transition (MonoProfileMethodResult callback)
270 {
271         if (!prof_list)
272                 return;
273         prof_list->man_unman_transition = callback;
274 }
275
276 /**
277  * mono_profiler_install_allocation:
278  */
279 void 
280 mono_profiler_install_allocation (MonoProfileAllocFunc callback)
281 {
282         if (!prof_list)
283                 return;
284         prof_list->allocation_cb = callback;
285 }
286
287 void
288 mono_profiler_install_monitor  (MonoProfileMonitorFunc callback)
289 {
290         if (!prof_list)
291                 return;
292         prof_list->monitor_event_cb = callback;
293 }
294
295 static MonoProfileSamplingMode sampling_mode = MONO_PROFILER_STAT_MODE_PROCESS;
296 static int64_t sampling_frequency = 100; // Hz
297
298 /**
299  * mono_profiler_set_statistical_mode:
300  * \param mode the sampling mode used.
301  * \param sample_frequency_is_us the sampling frequency in microseconds.
302  *
303  * Set the sampling parameters for the profiler. Sampling mode affects the effective sampling rate as in samples/s you'll witness.
304  * The default sampling mode is process mode, which only reports samples when there's activity in the process.
305  *
306  * Sampling frequency should be interpreted as a suggestion that can't always be honored due to how most kernels expose alarms.
307  *
308  * Said that, when using statistical sampling, always assume variable rate sampling as all sort of external factors can interfere.
309  */
310 void
311 mono_profiler_set_statistical_mode (MonoProfileSamplingMode mode, int64_t sampling_frequency_hz)
312 {
313         sampling_mode = mode;
314         sampling_frequency = sampling_frequency_hz;
315 }
316
317 /**
318  * mono_profiler_install_statistical:
319  */
320 void 
321 mono_profiler_install_statistical (MonoProfileStatFunc callback)
322 {
323         if (!prof_list)
324                 return;
325         prof_list->statistical_cb = callback;
326 }
327
328 int64_t
329 mono_profiler_get_sampling_rate (void)
330 {
331         return sampling_frequency;
332 }
333
334 MonoProfileSamplingMode
335 mono_profiler_get_sampling_mode (void)
336 {
337         return sampling_mode;
338 }
339
340 void 
341 mono_profiler_install_statistical_call_chain (MonoProfileStatCallChainFunc callback, int call_chain_depth, MonoProfilerCallChainStrategy call_chain_strategy) {
342         if (!prof_list)
343                 return;
344         if (call_chain_depth > MONO_PROFILER_MAX_STAT_CALL_CHAIN_DEPTH) {
345                 call_chain_depth = MONO_PROFILER_MAX_STAT_CALL_CHAIN_DEPTH;
346         }
347         if ((call_chain_strategy >= MONO_PROFILER_CALL_CHAIN_INVALID) || (call_chain_strategy < MONO_PROFILER_CALL_CHAIN_NONE)) {
348                 call_chain_strategy = MONO_PROFILER_CALL_CHAIN_NONE;
349         }
350         prof_list->statistical_call_chain_cb = callback;
351         prof_list->statistical_call_chain_depth = call_chain_depth;
352         prof_list->statistical_call_chain_strategy = call_chain_strategy;
353 }
354
355 int
356 mono_profiler_stat_get_call_chain_depth (void) {
357         if (prof_list && prof_list->statistical_call_chain_cb != NULL) {
358                 return prof_list->statistical_call_chain_depth;
359         } else {
360                 return 0;
361         }
362 }
363
364 MonoProfilerCallChainStrategy
365 mono_profiler_stat_get_call_chain_strategy (void) {
366         if (prof_list && prof_list->statistical_call_chain_cb != NULL) {
367                 return prof_list->statistical_call_chain_strategy;
368         } else {
369                 return MONO_PROFILER_CALL_CHAIN_NONE;
370         }
371 }
372
373 void mono_profiler_install_exception (MonoProfileExceptionFunc throw_callback, MonoProfileMethodFunc exc_method_leave, MonoProfileExceptionClauseFunc clause_callback)
374 {
375         if (!prof_list)
376                 return;
377         prof_list->exception_throw_cb = throw_callback;
378         prof_list->exception_method_leave_cb = exc_method_leave;
379         prof_list->exception_clause_cb = clause_callback;
380 }
381
382 /**
383  * mono_profiler_install_coverage_filter:
384  */
385 void 
386 mono_profiler_install_coverage_filter (MonoProfileCoverageFilterFunc callback)
387 {
388         if (!prof_list)
389                 return;
390         prof_list->coverage_filter_cb = callback;
391 }
392
393 /**
394  * mono_profiler_install_appdomain:
395  */
396 void 
397 mono_profiler_install_appdomain   (MonoProfileAppDomainFunc start_load, MonoProfileAppDomainResult end_load,
398                                    MonoProfileAppDomainFunc start_unload, MonoProfileAppDomainFunc end_unload)
399
400 {
401         if (!prof_list)
402                 return;
403         prof_list->domain_start_load = start_load;
404         prof_list->domain_end_load = end_load;
405         prof_list->domain_start_unload = start_unload;
406         prof_list->domain_end_unload = end_unload;
407 }
408
409 void
410 mono_profiler_install_appdomain_name (MonoProfileAppDomainFriendlyNameFunc domain_name_cb)
411 {
412         if (!prof_list)
413                 return;
414
415         prof_list->domain_name = domain_name_cb;
416 }
417
418 void
419 mono_profiler_install_context (MonoProfileContextFunc load, MonoProfileContextFunc unload)
420 {
421         if (!prof_list)
422                 return;
423
424         prof_list->context_load = load;
425         prof_list->context_unload = unload;
426 }
427
428 /**
429  * mono_profiler_install_assembly:
430  */
431 void 
432 mono_profiler_install_assembly    (MonoProfileAssemblyFunc start_load, MonoProfileAssemblyResult end_load,
433                                    MonoProfileAssemblyFunc start_unload, MonoProfileAssemblyFunc end_unload)
434 {
435         if (!prof_list)
436                 return;
437         prof_list->assembly_start_load = start_load;
438         prof_list->assembly_end_load = end_load;
439         prof_list->assembly_start_unload = start_unload;
440         prof_list->assembly_end_unload = end_unload;
441 }
442
443 /**
444  * mono_profiler_install_module:
445  */
446 void 
447 mono_profiler_install_module      (MonoProfileModuleFunc start_load, MonoProfileModuleResult end_load,
448                                    MonoProfileModuleFunc start_unload, MonoProfileModuleFunc end_unload)
449 {
450         if (!prof_list)
451                 return;
452         prof_list->module_start_load = start_load;
453         prof_list->module_end_load = end_load;
454         prof_list->module_start_unload = start_unload;
455         prof_list->module_end_unload = end_unload;
456 }
457
458 /**
459  * mono_profiler_install_class:
460  */
461 void
462 mono_profiler_install_class       (MonoProfileClassFunc start_load, MonoProfileClassResult end_load,
463                                    MonoProfileClassFunc start_unload, MonoProfileClassFunc end_unload)
464 {
465         if (!prof_list)
466                 return;
467         prof_list->class_start_load = start_load;
468         prof_list->class_end_load = end_load;
469         prof_list->class_start_unload = start_unload;
470         prof_list->class_end_unload = end_unload;
471 }
472
473 /**
474  * mono_profiler_method_enter:
475  */
476 void
477 mono_profiler_method_enter (MonoMethod *method)
478 {
479         ProfilerDesc *prof;
480         for (prof = prof_list; prof; prof = prof->next) {
481                 if ((prof->events & MONO_PROFILE_ENTER_LEAVE) && prof->method_enter)
482                         prof->method_enter (prof->profiler, method);
483         }
484 }
485
486 /**
487  * mono_profiler_method_leave:
488  */
489 void
490 mono_profiler_method_leave (MonoMethod *method)
491 {
492         ProfilerDesc *prof;
493         for (prof = prof_list; prof; prof = prof->next) {
494                 if ((prof->events & MONO_PROFILE_ENTER_LEAVE) && prof->method_leave)
495                         prof->method_leave (prof->profiler, method);
496         }
497 }
498
499 /**
500  * mono_profiler_method_jit:
501  */
502 void 
503 mono_profiler_method_jit (MonoMethod *method)
504 {
505         ProfilerDesc *prof;
506         for (prof = prof_list; prof; prof = prof->next) {
507                 if ((prof->events & MONO_PROFILE_JIT_COMPILATION) && prof->jit_start)
508                         prof->jit_start (prof->profiler, method);
509         }
510 }
511
512 /**
513  * mono_profiler_method_end_jit:
514  */
515 void 
516 mono_profiler_method_end_jit (MonoMethod *method, MonoJitInfo* jinfo, int result)
517 {
518         ProfilerDesc *prof;
519         for (prof = prof_list; prof; prof = prof->next) {
520                 if ((prof->events & MONO_PROFILE_JIT_COMPILATION)) {
521                         if (prof->jit_end)
522                                 prof->jit_end (prof->profiler, method, result);
523                         if (prof->jit_end2)
524                                 prof->jit_end2 (prof->profiler, method, jinfo, result);
525                 }
526         }
527 }
528
529 void 
530 mono_profiler_method_free (MonoMethod *method)
531 {
532         ProfilerDesc *prof;
533         for (prof = prof_list; prof; prof = prof->next) {
534                 if ((prof->events & MONO_PROFILE_METHOD_EVENTS) && prof->method_free)
535                         prof->method_free (prof->profiler, method);
536         }
537 }
538
539 void
540 mono_profiler_method_start_invoke (MonoMethod *method)
541 {
542         ProfilerDesc *prof;
543         for (prof = prof_list; prof; prof = prof->next) {
544                 if ((prof->events & MONO_PROFILE_METHOD_EVENTS) && prof->method_start_invoke)
545                         prof->method_start_invoke (prof->profiler, method);
546         }
547 }
548
549 void
550 mono_profiler_method_end_invoke (MonoMethod *method)
551 {
552         ProfilerDesc *prof;
553         for (prof = prof_list; prof; prof = prof->next) {
554                 if ((prof->events & MONO_PROFILE_METHOD_EVENTS) && prof->method_end_invoke)
555                         prof->method_end_invoke (prof->profiler, method);
556         }
557 }
558
559 /**
560  * mono_profiler_code_transition:
561  */
562 void 
563 mono_profiler_code_transition (MonoMethod *method, int result)
564 {
565         ProfilerDesc *prof;
566         for (prof = prof_list; prof; prof = prof->next) {
567                 if ((prof->events & MONO_PROFILE_TRANSITIONS) && prof->man_unman_transition)
568                         prof->man_unman_transition (prof->profiler, method, result);
569         }
570 }
571
572 /**
573  * mono_profiler_allocation:
574  */
575 void 
576 mono_profiler_allocation (MonoObject *obj)
577 {
578         ProfilerDesc *prof;
579         for (prof = prof_list; prof; prof = prof->next) {
580                 if ((prof->events & MONO_PROFILE_ALLOCATIONS) && prof->allocation_cb)
581                         prof->allocation_cb (prof->profiler, obj, obj->vtable->klass);
582         }
583 }
584
585 void
586 mono_profiler_monitor_event      (MonoObject *obj, MonoProfilerMonitorEvent event) {
587         ProfilerDesc *prof;
588         for (prof = prof_list; prof; prof = prof->next) {
589                 if ((prof->events & MONO_PROFILE_MONITOR_EVENTS) && prof->monitor_event_cb)
590                         prof->monitor_event_cb (prof->profiler, obj, event);
591         }
592 }
593
594 /**
595  * mono_profiler_stat_hit:
596  */
597 void
598 mono_profiler_stat_hit (guchar *ip, void *context)
599 {
600         ProfilerDesc *prof;
601         for (prof = prof_list; prof; prof = prof->next) {
602                 if ((prof->events & MONO_PROFILE_STATISTICAL) && prof->statistical_cb)
603                         prof->statistical_cb (prof->profiler, ip, context);
604         }
605 }
606
607 void
608 mono_profiler_stat_call_chain (int call_chain_depth, guchar **ips, void *context)
609 {
610         ProfilerDesc *prof;
611         for (prof = prof_list; prof; prof = prof->next) {
612                 if ((prof->events & MONO_PROFILE_STATISTICAL) && prof->statistical_call_chain_cb)
613                         prof->statistical_call_chain_cb (prof->profiler, call_chain_depth, ips, context);
614         }
615 }
616
617 void
618 mono_profiler_exception_thrown (MonoObject *exception)
619 {
620         ProfilerDesc *prof;
621         for (prof = prof_list; prof; prof = prof->next) {
622                 if ((prof->events & MONO_PROFILE_EXCEPTIONS) && prof->exception_throw_cb)
623                         prof->exception_throw_cb (prof->profiler, exception);
624         }
625 }
626
627 void
628 mono_profiler_exception_method_leave (MonoMethod *method)
629 {
630         ProfilerDesc *prof;
631         for (prof = prof_list; prof; prof = prof->next) {
632                 if ((prof->events & MONO_PROFILE_EXCEPTIONS) && prof->exception_method_leave_cb)
633                         prof->exception_method_leave_cb (prof->profiler, method);
634         }
635 }
636
637 void
638 mono_profiler_exception_clause_handler (MonoMethod *method, int clause_type, int clause_num)
639 {
640         ProfilerDesc *prof;
641         for (prof = prof_list; prof; prof = prof->next) {
642                 if ((prof->events & MONO_PROFILE_EXCEPTIONS) && prof->exception_clause_cb)
643                         prof->exception_clause_cb (prof->profiler, method, clause_type, clause_num);
644         }
645 }
646
647 /**
648  * mono_profiler_thread_start:
649  */
650 void
651 mono_profiler_thread_start (gsize tid)
652 {
653         ProfilerDesc *prof;
654         for (prof = prof_list; prof; prof = prof->next) {
655                 if ((prof->events & MONO_PROFILE_THREADS) && prof->thread_start)
656                         prof->thread_start (prof->profiler, tid);
657         }
658 }
659
660 /**
661  * mono_profiler_thread_end:
662  */
663 void 
664 mono_profiler_thread_end (gsize tid)
665 {
666         ProfilerDesc *prof;
667         for (prof = prof_list; prof; prof = prof->next) {
668                 if ((prof->events & MONO_PROFILE_THREADS) && prof->thread_end)
669                         prof->thread_end (prof->profiler, tid);
670         }
671 }
672
673 void
674 mono_profiler_thread_name (gsize tid, const char *name)
675 {
676         ProfilerDesc *prof;
677         for (prof = prof_list; prof; prof = prof->next) {
678                 if ((prof->events & MONO_PROFILE_THREADS) && prof->thread_name)
679                         prof->thread_name (prof->profiler, tid, name);
680         }
681 }
682
683 /**
684  * mono_profiler_assembly_event:
685  */
686 void 
687 mono_profiler_assembly_event  (MonoAssembly *assembly, int code)
688 {
689         ProfilerDesc *prof;
690         for (prof = prof_list; prof; prof = prof->next) {
691                 if (!(prof->events & MONO_PROFILE_ASSEMBLY_EVENTS))
692                         continue;
693
694                 switch (code) {
695                 case MONO_PROFILE_START_LOAD:
696                         if (prof->assembly_start_load)
697                                 prof->assembly_start_load (prof->profiler, assembly);
698                         break;
699                 case MONO_PROFILE_START_UNLOAD:
700                         if (prof->assembly_start_unload)
701                                 prof->assembly_start_unload (prof->profiler, assembly);
702                         break;
703                 case MONO_PROFILE_END_UNLOAD:
704                         if (prof->assembly_end_unload)
705                                 prof->assembly_end_unload (prof->profiler, assembly);
706                         break;
707                 default:
708                         g_assert_not_reached ();
709                 }
710         }
711 }
712
713 /**
714  * mono_profiler_assembly_loaded:
715  */
716 void 
717 mono_profiler_assembly_loaded (MonoAssembly *assembly, int result)
718 {
719         ProfilerDesc *prof;
720         for (prof = prof_list; prof; prof = prof->next) {
721                 if ((prof->events & MONO_PROFILE_ASSEMBLY_EVENTS) && prof->assembly_end_load)
722                         prof->assembly_end_load (prof->profiler, assembly, result);
723         }
724 }
725
726 void mono_profiler_iomap (char *report, const char *pathname, const char *new_pathname)
727 {
728         ProfilerDesc *prof;
729         for (prof = prof_list; prof; prof = prof->next) {
730                 if ((prof->events & MONO_PROFILE_IOMAP_EVENTS) && prof->iomap_cb)
731                         prof->iomap_cb (prof->profiler, report, pathname, new_pathname);
732         }
733 }
734
735 /**
736  * mono_profiler_module_event:
737  */
738 void 
739 mono_profiler_module_event  (MonoImage *module, int code)
740 {
741         ProfilerDesc *prof;
742         for (prof = prof_list; prof; prof = prof->next) {
743                 if (!(prof->events & MONO_PROFILE_MODULE_EVENTS))
744                         continue;
745
746                 switch (code) {
747                 case MONO_PROFILE_START_LOAD:
748                         if (prof->module_start_load)
749                                 prof->module_start_load (prof->profiler, module);
750                         break;
751                 case MONO_PROFILE_START_UNLOAD:
752                         if (prof->module_start_unload)
753                                 prof->module_start_unload (prof->profiler, module);
754                         break;
755                 case MONO_PROFILE_END_UNLOAD:
756                         if (prof->module_end_unload)
757                                 prof->module_end_unload (prof->profiler, module);
758                         break;
759                 default:
760                         g_assert_not_reached ();
761                 }
762         }
763 }
764
765 /**
766  * mono_profiler_module_loaded:
767  */
768 void 
769 mono_profiler_module_loaded (MonoImage *module, int result)
770 {
771         ProfilerDesc *prof;
772         for (prof = prof_list; prof; prof = prof->next) {
773                 if ((prof->events & MONO_PROFILE_MODULE_EVENTS) && prof->module_end_load)
774                         prof->module_end_load (prof->profiler, module, result);
775         }
776 }
777
778 /**
779  * mono_profiler_class_event:
780  */
781 void 
782 mono_profiler_class_event  (MonoClass *klass, int code)
783 {
784         ProfilerDesc *prof;
785         for (prof = prof_list; prof; prof = prof->next) {
786                 if (!(prof->events & MONO_PROFILE_CLASS_EVENTS))
787                         continue;
788
789                 switch (code) {
790                 case MONO_PROFILE_START_LOAD:
791                         if (prof->class_start_load)
792                                 prof->class_start_load (prof->profiler, klass);
793                         break;
794                 case MONO_PROFILE_START_UNLOAD:
795                         if (prof->class_start_unload)
796                                 prof->class_start_unload (prof->profiler, klass);
797                         break;
798                 case MONO_PROFILE_END_UNLOAD:
799                         if (prof->class_end_unload)
800                                 prof->class_end_unload (prof->profiler, klass);
801                         break;
802                 default:
803                         g_assert_not_reached ();
804                 }
805         }
806 }
807
808 /**
809  * mono_profiler_class_loaded:
810  */
811 void 
812 mono_profiler_class_loaded (MonoClass *klass, int result)
813 {
814         ProfilerDesc *prof;
815         for (prof = prof_list; prof; prof = prof->next) {
816                 if ((prof->events & MONO_PROFILE_CLASS_EVENTS) && prof->class_end_load)
817                         prof->class_end_load (prof->profiler, klass, result);
818         }
819 }
820
821 /**
822  * mono_profiler_appdomain_event:
823  */
824 void 
825 mono_profiler_appdomain_event  (MonoDomain *domain, int code)
826 {
827         ProfilerDesc *prof;
828         for (prof = prof_list; prof; prof = prof->next) {
829                 if (!(prof->events & MONO_PROFILE_APPDOMAIN_EVENTS))
830                         continue;
831
832                 switch (code) {
833                 case MONO_PROFILE_START_LOAD:
834                         if (prof->domain_start_load)
835                                 prof->domain_start_load (prof->profiler, domain);
836                         break;
837                 case MONO_PROFILE_START_UNLOAD:
838                         if (prof->domain_start_unload)
839                                 prof->domain_start_unload (prof->profiler, domain);
840                         break;
841                 case MONO_PROFILE_END_UNLOAD:
842                         if (prof->domain_end_unload)
843                                 prof->domain_end_unload (prof->profiler, domain);
844                         break;
845                 default:
846                         g_assert_not_reached ();
847                 }
848         }
849 }
850
851 /**
852  * mono_profiler_appdomain_loaded:
853  */
854 void 
855 mono_profiler_appdomain_loaded (MonoDomain *domain, int result)
856 {
857         ProfilerDesc *prof;
858         for (prof = prof_list; prof; prof = prof->next) {
859                 if ((prof->events & MONO_PROFILE_APPDOMAIN_EVENTS) && prof->domain_end_load)
860                         prof->domain_end_load (prof->profiler, domain, result);
861         }
862 }
863
864 void
865 mono_profiler_appdomain_name (MonoDomain *domain, const char *name)
866 {
867         for (ProfilerDesc *prof = prof_list; prof; prof = prof->next)
868                 if ((prof->events & MONO_PROFILE_APPDOMAIN_EVENTS) && prof->domain_name)
869                         prof->domain_name (prof->profiler, domain, name);
870 }
871
872 void
873 mono_profiler_context_loaded (MonoAppContext *context)
874 {
875         for (ProfilerDesc *prof = prof_list; prof; prof = prof->next)
876                 if ((prof->events & MONO_PROFILE_CONTEXT_EVENTS) && prof->context_load)
877                         prof->context_load (prof->profiler, context);
878 }
879
880 void
881 mono_profiler_context_unloaded (MonoAppContext *context)
882 {
883         for (ProfilerDesc *prof = prof_list; prof; prof = prof->next)
884                 if ((prof->events & MONO_PROFILE_CONTEXT_EVENTS) && prof->context_unload)
885                         prof->context_unload (prof->profiler, context);
886 }
887
888 /**
889  * mono_profiler_shutdown:
890  */
891 void 
892 mono_profiler_shutdown (void)
893 {
894         ProfilerDesc *prof;
895         for (prof = prof_list; prof; prof = prof->next) {
896                 if (prof->shutdown_callback)
897                         prof->shutdown_callback (prof->profiler);
898         }
899
900         mono_profiler_set_events ((MonoProfileFlags)0);
901 }
902
903 /**
904  * mono_profiler_gc_heap_resize:
905  */
906 void
907 mono_profiler_gc_heap_resize (gint64 new_size)
908 {
909         ProfilerDesc *prof;
910         for (prof = prof_list; prof; prof = prof->next) {
911                 if ((prof->events & MONO_PROFILE_GC) && prof->gc_heap_resize)
912                         prof->gc_heap_resize (prof->profiler, new_size);
913         }
914 }
915
916 /**
917  * mono_profiler_gc_event:
918  */
919 void
920 mono_profiler_gc_event (MonoGCEvent event, int generation)
921 {
922         ProfilerDesc *prof;
923         for (prof = prof_list; prof; prof = prof->next) {
924                 if ((prof->events & MONO_PROFILE_GC) && prof->gc_event)
925                         prof->gc_event (prof->profiler, event, generation);
926         }
927 }
928
929 void
930 mono_profiler_gc_moves (void **objects, int num)
931 {
932         ProfilerDesc *prof;
933         for (prof = prof_list; prof; prof = prof->next) {
934                 if ((prof->events & MONO_PROFILE_GC_MOVES) && prof->gc_moves)
935                         prof->gc_moves (prof->profiler, objects, num);
936         }
937 }
938
939 void
940 mono_profiler_gc_handle (int op, int type, uintptr_t handle, MonoObject *obj)
941 {
942         ProfilerDesc *prof;
943         for (prof = prof_list; prof; prof = prof->next) {
944                 if ((prof->events & MONO_PROFILE_GC_ROOTS) && prof->gc_handle)
945                         prof->gc_handle (prof->profiler, op, type, handle, obj);
946         }
947 }
948
949 void
950 mono_profiler_gc_roots (int num, void **objects, int *root_types, uintptr_t *extra_info)
951 {
952         ProfilerDesc *prof;
953         for (prof = prof_list; prof; prof = prof->next) {
954                 if ((prof->events & MONO_PROFILE_GC_ROOTS) && prof->gc_roots)
955                         prof->gc_roots (prof->profiler, num, objects, root_types, extra_info);
956         }
957 }
958
959 /**
960  * mono_profiler_install_gc:
961  */
962 void
963 mono_profiler_install_gc (MonoProfileGCFunc callback, MonoProfileGCResizeFunc heap_resize_callback)
964 {
965         if (!prof_list)
966                 return;
967         prof_list->gc_event = callback;
968         prof_list->gc_heap_resize = heap_resize_callback;
969 }
970
971 /**
972  * mono_profiler_install_gc_moves:
973  * \param callback callback function
974  *
975  * Install the \p callback function that the GC will call when moving objects.
976  * The callback receives an array of pointers and the number of elements
977  * in the array. Every even element in the array is the original object location
978  * and the following odd element is the new location of the object in memory.
979  * So the number of elements argument will always be a multiple of 2.
980  * Since this callback happens during the GC, it is a restricted environment:
981  * no locks can be taken and the object pointers can be inspected only once
982  * the GC is finished (of course the original location pointers will not
983  * point to valid objects anymore).
984  */
985 void
986 mono_profiler_install_gc_moves (MonoProfileGCMoveFunc callback)
987 {
988         if (!prof_list)
989                 return;
990         prof_list->gc_moves = callback;
991 }
992
993 /**
994  * mono_profiler_install_gc_roots:
995  * \param handle_callback callback function
996  * \param roots_callback callback function
997  *
998  * Install the \p handle_callback function that the GC will call when GC
999  * handles are created or destroyed.
1000  * The callback receives an operation, which is either \c MONO_PROFILER_GC_HANDLE_CREATED
1001  * or \c MONO_PROFILER_GC_HANDLE_DESTROYED, the handle type, the handle value and the
1002  * object pointer, if present.
1003  * Install the \p roots_callback function that the GC will call when tracing
1004  * the roots for a collection.
1005  * The callback receives the number of elements and three arrays: an array
1006  * of objects, an array of root types and flags and an array of extra info.
1007  * The size of each array is given by the first argument.
1008  */
1009 void
1010 mono_profiler_install_gc_roots (MonoProfileGCHandleFunc handle_callback, MonoProfileGCRootFunc roots_callback)
1011 {
1012         if (!prof_list)
1013                 return;
1014         prof_list->gc_handle = handle_callback;
1015         prof_list->gc_roots = roots_callback;
1016 }
1017
1018 void
1019 mono_profiler_gc_finalize_begin (void)
1020 {
1021         for (ProfilerDesc *prof = prof_list; prof; prof = prof->next)
1022                 if ((prof->events & MONO_PROFILE_GC_FINALIZATION) && prof->gc_finalize_begin)
1023                         prof->gc_finalize_begin (prof->profiler);
1024 }
1025
1026 void
1027 mono_profiler_gc_finalize_object_begin (MonoObject *obj)
1028 {
1029         for (ProfilerDesc *prof = prof_list; prof; prof = prof->next)
1030                 if ((prof->events & MONO_PROFILE_GC_FINALIZATION) && prof->gc_finalize_object_begin)
1031                         prof->gc_finalize_object_begin (prof->profiler, obj);
1032 }
1033
1034 void
1035 mono_profiler_gc_finalize_object_end (MonoObject *obj)
1036 {
1037         for (ProfilerDesc *prof = prof_list; prof; prof = prof->next)
1038                 if ((prof->events & MONO_PROFILE_GC_FINALIZATION) && prof->gc_finalize_object_end)
1039                         prof->gc_finalize_object_end (prof->profiler, obj);
1040 }
1041
1042 void
1043 mono_profiler_gc_finalize_end (void)
1044 {
1045         for (ProfilerDesc *prof = prof_list; prof; prof = prof->next)
1046                 if ((prof->events & MONO_PROFILE_GC_FINALIZATION) && prof->gc_finalize_end)
1047                         prof->gc_finalize_end (prof->profiler);
1048 }
1049
1050 void
1051 mono_profiler_install_gc_finalize (MonoProfileGCFinalizeFunc begin, MonoProfileGCFinalizeObjectFunc begin_obj, MonoProfileGCFinalizeObjectFunc end_obj, MonoProfileGCFinalizeFunc end)
1052 {
1053         if (!prof_list)
1054                 return;
1055
1056         prof_list->gc_finalize_begin = begin;
1057         prof_list->gc_finalize_object_begin = begin_obj;
1058         prof_list->gc_finalize_object_end = end_obj;
1059         prof_list->gc_finalize_end = end;
1060 }
1061
1062 void
1063 mono_profiler_install_runtime_initialized (MonoProfileFunc runtime_initialized_callback)
1064 {
1065         if (!prof_list)
1066                 return;
1067         prof_list->runtime_initialized_event = runtime_initialized_callback;
1068 }
1069
1070 void
1071 mono_profiler_runtime_initialized (void) {
1072         ProfilerDesc *prof;
1073         for (prof = prof_list; prof; prof = prof->next) {
1074                 if (prof->runtime_initialized_event)
1075                         prof->runtime_initialized_event (prof->profiler);
1076         }
1077 }
1078
1079 void
1080 mono_profiler_install_code_chunk_new (MonoProfilerCodeChunkNew callback) {
1081         if (!prof_list)
1082                 return;
1083         prof_list->code_chunk_new = callback;
1084 }
1085 void
1086 mono_profiler_code_chunk_new (gpointer chunk, int size) {
1087         ProfilerDesc *prof;
1088         for (prof = prof_list; prof; prof = prof->next) {
1089                 if (prof->code_chunk_new)
1090                         prof->code_chunk_new (prof->profiler, chunk, size);
1091         }
1092 }
1093
1094 void
1095 mono_profiler_install_code_chunk_destroy (MonoProfilerCodeChunkDestroy callback) {
1096         if (!prof_list)
1097                 return;
1098         prof_list->code_chunk_destroy = callback;
1099 }
1100 void
1101 mono_profiler_code_chunk_destroy (gpointer chunk) {
1102         ProfilerDesc *prof;
1103         for (prof = prof_list; prof; prof = prof->next) {
1104                 if (prof->code_chunk_destroy)
1105                         prof->code_chunk_destroy (prof->profiler, chunk);
1106         }
1107 }
1108
1109 void
1110 mono_profiler_install_code_buffer_new (MonoProfilerCodeBufferNew callback) {
1111         if (!prof_list)
1112                 return;
1113         prof_list->code_buffer_new = callback;
1114 }
1115
1116 void
1117 mono_profiler_install_iomap (MonoProfileIomapFunc callback)
1118 {
1119         if (!prof_list)
1120                 return;
1121         prof_list->iomap_cb = callback;
1122 }
1123
1124 void
1125 mono_profiler_code_buffer_new (gpointer buffer, int size, MonoProfilerCodeBufferType type, gconstpointer data) {
1126         ProfilerDesc *prof;
1127         for (prof = prof_list; prof; prof = prof->next) {
1128                 if (prof->code_buffer_new)
1129                         prof->code_buffer_new (prof->profiler, buffer, size, type, (void*)data);
1130         }
1131 }
1132
1133 static GHashTable *coverage_hash = NULL;
1134
1135 MonoProfileCoverageInfo* 
1136 mono_profiler_coverage_alloc (MonoMethod *method, int entries)
1137 {
1138         MonoProfileCoverageInfo *res;
1139         int instrument = FALSE;
1140         ProfilerDesc *prof;
1141
1142         for (prof = prof_list; prof; prof = prof->next) {
1143                 /* note that we call the filter on all the profilers even if just
1144                  * a single one would be enough to instrument a method
1145                  */
1146                 if (prof->coverage_filter_cb)
1147                         if (prof->coverage_filter_cb (prof->profiler, method))
1148                                 instrument = TRUE;
1149         }
1150         if (!instrument)
1151                 return NULL;
1152
1153         mono_profiler_coverage_lock ();
1154         if (!coverage_hash)
1155                 coverage_hash = g_hash_table_new (NULL, NULL);
1156
1157         res = (MonoProfileCoverageInfo *)g_malloc0 (sizeof (MonoProfileCoverageInfo) + sizeof (void*) * 2 * entries);
1158
1159         res->entries = entries;
1160
1161         g_hash_table_insert (coverage_hash, method, res);
1162         mono_profiler_coverage_unlock ();
1163
1164         return res;
1165 }
1166
1167 /* safe only when the method antive code has been unloaded */
1168 void
1169 mono_profiler_coverage_free (MonoMethod *method)
1170 {
1171         MonoProfileCoverageInfo* info;
1172
1173         mono_profiler_coverage_lock ();
1174         if (!coverage_hash) {
1175                 mono_profiler_coverage_unlock ();
1176                 return;
1177         }
1178
1179         info = (MonoProfileCoverageInfo *)g_hash_table_lookup (coverage_hash, method);
1180         if (info) {
1181                 g_free (info);
1182                 g_hash_table_remove (coverage_hash, method);
1183         }
1184         mono_profiler_coverage_unlock ();
1185 }
1186
1187 /**
1188  * mono_profiler_coverage_get:
1189  * \param prof The profiler handle, installed with mono_profiler_install
1190  * \param method the method to gather information from.
1191  * \param func A routine that will be called back with the results
1192  *
1193  * If the \c MONO_PROFILER_INS_COVERAGE flag was active during JIT compilation
1194  * it is possible to obtain coverage information about a give method.
1195  *
1196  * The function \p func will be invoked repeatedly with instances of the
1197  * \c MonoProfileCoverageEntry structure.
1198  */
1199 void 
1200 mono_profiler_coverage_get (MonoProfiler *prof, MonoMethod *method, MonoProfileCoverageFunc func)
1201 {
1202         MonoError error;
1203         MonoProfileCoverageInfo* info = NULL;
1204         int i, offset;
1205         guint32 code_size;
1206         const unsigned char *start, *end, *cil_code;
1207         MonoMethodHeader *header;
1208         MonoProfileCoverageEntry entry;
1209         MonoDebugMethodInfo *debug_minfo;
1210
1211         mono_profiler_coverage_lock ();
1212         if (coverage_hash)
1213                 info = (MonoProfileCoverageInfo *)g_hash_table_lookup (coverage_hash, method);
1214         mono_profiler_coverage_unlock ();
1215
1216         if (!info)
1217                 return;
1218
1219         header = mono_method_get_header_checked (method, &error);
1220         mono_error_assert_ok (&error);
1221         start = mono_method_header_get_code (header, &code_size, NULL);
1222         debug_minfo = mono_debug_lookup_method (method);
1223
1224         end = start + code_size;
1225         for (i = 0; i < info->entries; ++i) {
1226                 cil_code = info->data [i].cil_code;
1227                 if (cil_code && cil_code >= start && cil_code < end) {
1228                         char *fname = NULL;
1229                         offset = cil_code - start;
1230                         entry.iloffset = offset;
1231                         entry.method = method;
1232                         entry.counter = info->data [i].count;
1233                         entry.line = entry.col = 1;
1234                         entry.filename = NULL;
1235                         if (debug_minfo) {
1236                                 MonoDebugSourceLocation *location;
1237
1238                                 location = mono_debug_method_lookup_location (debug_minfo, offset);
1239                                 if (location) {
1240                                         entry.line = location->row;
1241                                         entry.col = location->column;
1242                                         entry.filename = fname = g_strdup (location->source_file);
1243                                         mono_debug_free_source_location (location);
1244                                 }
1245                         }
1246
1247                         func (prof, &entry);
1248                         g_free (fname);
1249                 }
1250         }
1251         mono_metadata_free_mh (header);
1252 }
1253
1254 typedef void (*ProfilerInitializer) (const char*);
1255 #define INITIALIZER_NAME "mono_profiler_startup"
1256
1257
1258 static gboolean
1259 load_profiler (MonoDl *pmodule, const char *desc, const char *symbol)
1260 {
1261         char *err;
1262         ProfilerInitializer func;
1263
1264         if (!pmodule)
1265                 return FALSE;
1266
1267         if ((err = mono_dl_symbol (pmodule, symbol, (gpointer *) &func))) {
1268                 g_free (err);
1269                 return FALSE;
1270         } else {
1271                 func (desc);
1272         }
1273         return TRUE;
1274 }
1275
1276 static gboolean
1277 load_embedded_profiler (const char *desc, const char *name)
1278 {
1279         char *err = NULL;
1280         char *symbol;
1281         MonoDl *pmodule = NULL;
1282         gboolean result;
1283
1284         /*
1285          * Some profilers (such as ours) may need to call back into the runtime
1286          * from their sampling callback (which is called in async-signal context).
1287          * They need to be able to know that all references back to the runtime
1288          * have been resolved; otherwise, calling runtime functions may result in
1289          * invoking the dynamic linker which is not async-signal-safe. Passing
1290          * MONO_DL_EAGER will ask the dynamic linker to resolve everything upfront.
1291          */
1292         pmodule = mono_dl_open (NULL, MONO_DL_EAGER, &err);
1293         if (!pmodule) {
1294                 g_warning ("Could not open main executable (%s)", err);
1295                 g_free (err);
1296                 return FALSE;
1297         }
1298
1299         symbol = g_strdup_printf (INITIALIZER_NAME "_%s", name);
1300         result = load_profiler (pmodule, desc, symbol);
1301         g_free (symbol);
1302
1303         return result;
1304 }
1305
1306 // TODO: Much of the library loading code here is custom. It would be better to merge this with mono-dl
1307 static gboolean
1308 load_profiler_from_directory (const char *directory, const char *libname, const char *desc)
1309 {
1310         MonoDl *pmodule = NULL;
1311         char* path;
1312         char *err;
1313         void *iter;
1314
1315         mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT, "Attempting to load profiler %s from %s (desc %s)", libname, directory, desc);
1316
1317         iter = NULL;
1318         err = NULL;
1319         while ((path = mono_dl_build_path (directory, libname, &iter))) {
1320                 pmodule = mono_dl_open (path, MONO_DL_EAGER, &err);
1321                 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT, "Attempting to load profiler: %s, %ssuccessful, err: %s", path, pmodule?"":"not ", err);
1322                 g_free (path);
1323                 g_free (err);
1324                 if (pmodule)
1325                         return load_profiler (pmodule, desc, INITIALIZER_NAME);
1326         }
1327                 
1328         return FALSE;
1329 }
1330
1331 static gboolean
1332 load_profiler_from_mono_installation (const char *libname, const char *desc)
1333 {
1334         char *err = NULL;
1335         MonoDl *pmodule = mono_dl_open_runtime_lib (libname, MONO_DL_EAGER, &err);
1336         mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT, "Attempting to load profiler from runtime libs: %s, %ssuccessful, err: %s", libname, pmodule?"":"not ", err);
1337         g_free (err);
1338         if (pmodule)
1339                 return load_profiler (pmodule, desc, INITIALIZER_NAME);
1340         return FALSE;
1341 }
1342
1343 /**
1344  * mono_profiler_load:
1345  * \param desc arguments to configure the profiler
1346  *
1347  * Invoke this method to initialize the profiler.   This will drive the
1348  * loading of the internal ("default") or any external profilers.
1349  *
1350  * This routine is invoked by Mono's driver, but must be called manually
1351  * if you embed Mono into your application.
1352  */
1353 void 
1354 mono_profiler_load (const char *desc)
1355 {
1356         char *cdesc = NULL;
1357         mono_gc_base_init ();
1358
1359         if (!desc || (strcmp ("default", desc) == 0)) {
1360                 desc = "log:report";
1361         }
1362         /* we keep command-line compat with the old version here */
1363         if (strncmp (desc, "default:", 8) == 0) {
1364                 gchar **args, **ptr;
1365                 GString *str = g_string_new ("log:report");
1366                 args = g_strsplit (desc + 8, ",", -1);
1367                 for (ptr = args; ptr && *ptr; ptr++) {
1368                         const char *arg = *ptr;
1369
1370                         if (!strcmp (arg, "time"))
1371                                 g_string_append (str, ",calls");
1372                         else if (!strcmp (arg, "alloc"))
1373                                 g_string_append (str, ",alloc");
1374                         else if (!strcmp (arg, "stat"))
1375                                 g_string_append (str, ",sample");
1376                         else if (!strcmp (arg, "jit"))
1377                                 continue; /* accept and do nothing */
1378                         else if (strncmp (arg, "file=", 5) == 0) {
1379                                 g_string_append_printf (str, ",output=%s", arg + 5);
1380                         } else {
1381                                 fprintf (stderr, "profiler : Unknown argument '%s'.\n", arg);
1382                                 return;
1383                         }
1384                 }
1385                 desc = cdesc = g_string_free (str, FALSE);
1386         }
1387         {
1388                 const char* col = strchr (desc, ':');
1389                 char* libname;
1390                 char *mname;
1391                 gboolean res = FALSE;
1392
1393                 if (col != NULL) {
1394                         mname = (char *)g_memdup (desc, col - desc + 1);
1395                         mname [col - desc] = 0;
1396                 } else {
1397                         mname = g_strdup (desc);
1398                 }
1399                 if (!load_embedded_profiler (desc, mname)) {
1400                         libname = g_strdup_printf ("mono-profiler-%s", mname);
1401                         res = load_profiler_from_mono_installation (libname, desc);
1402                         if (!res && mono_config_get_assemblies_dir ())
1403                                 res = load_profiler_from_directory (mono_assembly_getrootdir (), libname, desc);
1404                         if (!res)
1405                                 res = load_profiler_from_directory (NULL, libname, desc);
1406                         if (!res)
1407                                 g_warning ("The '%s' profiler wasn't found in the main executable nor could it be loaded from '%s'.", mname, libname);
1408                         g_free (libname);
1409                 }
1410                 g_free (mname);
1411         }
1412         g_free (cdesc);
1413 }
1414