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