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