Extract the default profiler into its own file.
[mono.git] / mono / metadata / profiler.c
1 /*
2  * profiler.c: Profiler interface for Mono
3  *
4  * Author:
5  *   Paolo Molaro (lupus@ximian.com)
6  *
7  * Copyright 2001-2003 Ximian, Inc (http://www.ximian.com)
8  * Copyright 2004-2009 Novell, Inc (http://www.novell.com)
9  */
10
11 #include "config.h"
12 #include "mono/metadata/profiler-private.h"
13 #include "mono/metadata/profiler-default.h"
14 #include "mono/metadata/debug-helpers.h"
15 #include "mono/metadata/mono-debug.h"
16 #include "mono/metadata/debug-mono-symfile.h"
17 #include "mono/metadata/metadata-internals.h"
18 #include "mono/metadata/class-internals.h"
19 #include "mono/metadata/domain-internals.h"
20 #include "mono/metadata/gc-internal.h"
21 #include "mono/io-layer/io-layer.h"
22 #include "mono/utils/mono-dl.h"
23 #include <string.h>
24 #ifdef HAVE_UNISTD_H
25 #include <unistd.h>
26 #endif
27 #ifdef HAVE_SYS_TIME_H
28 #include <sys/time.h>
29 #endif
30 #ifdef HAVE_BACKTRACE_SYMBOLS
31 #include <execinfo.h>
32 #endif
33
34 typedef struct _ProfilerDesc ProfilerDesc;
35 struct _ProfilerDesc {
36         ProfilerDesc *next;
37         MonoProfiler *profiler;
38         MonoProfileFlags events;
39
40         MonoProfileAppDomainFunc   domain_start_load;
41         MonoProfileAppDomainResult domain_end_load;
42         MonoProfileAppDomainFunc   domain_start_unload;
43         MonoProfileAppDomainFunc   domain_end_unload;
44
45         MonoProfileAssemblyFunc   assembly_start_load;
46         MonoProfileAssemblyResult assembly_end_load;
47         MonoProfileAssemblyFunc   assembly_start_unload;
48         MonoProfileAssemblyFunc   assembly_end_unload;
49
50         MonoProfileModuleFunc   module_start_load;
51         MonoProfileModuleResult module_end_load;
52         MonoProfileModuleFunc   module_start_unload;
53         MonoProfileModuleFunc   module_end_unload;
54
55         MonoProfileClassFunc   class_start_load;
56         MonoProfileClassResult class_end_load;
57         MonoProfileClassFunc   class_start_unload;
58         MonoProfileClassFunc   class_end_unload;
59
60         MonoProfileMethodFunc   jit_start;
61         MonoProfileMethodResult jit_end;
62         MonoProfileJitResult    jit_end2;
63         MonoProfileMethodFunc   method_free;
64         MonoProfileMethodFunc   method_start_invoke;
65         MonoProfileMethodFunc   method_end_invoke;
66         MonoProfileMethodResult man_unman_transition;
67         MonoProfileAllocFunc    allocation_cb;
68         MonoProfileMonitorFunc  monitor_event_cb;
69         MonoProfileStatFunc     statistical_cb;
70         MonoProfileStatCallChainFunc statistical_call_chain_cb;
71         int                     statistical_call_chain_depth;
72         MonoProfilerCallChainStrategy  statistical_call_chain_strategy;
73         MonoProfileMethodFunc   method_enter;
74         MonoProfileMethodFunc   method_leave;
75
76         MonoProfileExceptionFunc        exception_throw_cb;
77         MonoProfileMethodFunc exception_method_leave_cb;
78         MonoProfileExceptionClauseFunc exception_clause_cb;
79
80         MonoProfileIomapFunc iomap_cb;
81
82         MonoProfileThreadFunc   thread_start;
83         MonoProfileThreadFunc   thread_end;
84
85         MonoProfileCoverageFilterFunc coverage_filter_cb;
86
87         MonoProfileFunc shutdown_callback;
88
89         MonoProfileGCFunc        gc_event;
90         MonoProfileGCResizeFunc  gc_heap_resize;
91         MonoProfileGCMoveFunc    gc_moves;
92
93         MonoProfileFunc          runtime_initialized_event;
94
95         MonoProfilerCodeChunkNew code_chunk_new;
96         MonoProfilerCodeChunkDestroy code_chunk_destroy;
97         MonoProfilerCodeBufferNew code_buffer_new;
98 };
99
100 static ProfilerDesc *prof_list = NULL;
101
102 #define mono_profiler_coverage_lock() EnterCriticalSection (&profiler_coverage_mutex)
103 #define mono_profiler_coverage_unlock() LeaveCriticalSection (&profiler_coverage_mutex)
104 static CRITICAL_SECTION profiler_coverage_mutex;
105
106 /* this is directly accessible to other mono libs.
107  * It is the ORed value of all the profiler's events.
108  */
109 MonoProfileFlags mono_profiler_events;
110
111 /**
112  * mono_profiler_install:
113  * @prof: a MonoProfiler structure pointer, or a pointer to a derived structure.
114  * @callback: the function to invoke at shutdown
115  *
116  * Use mono_profiler_install to activate profiling in the Mono runtime.
117  * Typically developers of new profilers will create a new structure whose
118  * first field is a MonoProfiler and put any extra information that they need
119  * to access from the various profiling callbacks there.
120  *
121  */
122 void
123 mono_profiler_install (MonoProfiler *prof, MonoProfileFunc callback)
124 {
125         ProfilerDesc *desc = g_new0 (ProfilerDesc, 1);
126         if (!prof_list)
127                 InitializeCriticalSection (&profiler_coverage_mutex);
128         desc->profiler = prof;
129         desc->shutdown_callback = callback;
130         desc->next = prof_list;
131         prof_list = desc;
132 }
133
134 /**
135  * mono_profiler_set_events:
136  * @events: an ORed set of values made up of MONO_PROFILER_ flags
137  *
138  * The events descriped in the @events argument is a set of flags
139  * that represent which profiling events must be triggered.  For
140  * example if you have registered a set of methods for tracking
141  * JIT compilation start and end with mono_profiler_install_jit_compile,
142  * you will want to pass the MONO_PROFILE_JIT_COMPILATION flag to
143  * this routine.
144  *
145  * You can call mono_profile_set_events more than once and you can
146  * do this at runtime to modify which methods are invoked.
147  */
148 void
149 mono_profiler_set_events (MonoProfileFlags events)
150 {
151         ProfilerDesc *prof;
152         MonoProfileFlags value = 0;
153         if (prof_list)
154                 prof_list->events = events;
155         for (prof = prof_list; prof; prof = prof->next)
156                 value |= prof->events;
157         mono_profiler_events = value;
158 }
159
160 /**
161  * mono_profiler_get_events:
162  *
163  * Returns a list of active events that will be intercepted. 
164  */
165 MonoProfileFlags
166 mono_profiler_get_events (void)
167 {
168         return mono_profiler_events;
169 }
170
171 /**
172  * mono_profiler_install_enter_leave:
173  * @enter: the routine to be called on each method entry
174  * @fleave: the routine to be called each time a method returns
175  *
176  * Use this routine to install routines that will be called everytime
177  * a method enters and leaves.   The routines will receive as an argument
178  * the MonoMethod representing the method that is entering or leaving.
179  */
180 void
181 mono_profiler_install_enter_leave (MonoProfileMethodFunc enter, MonoProfileMethodFunc fleave)
182 {
183         if (!prof_list)
184                 return;
185         prof_list->method_enter = enter;
186         prof_list->method_leave = fleave;
187 }
188
189 /**
190  * mono_profiler_install_jit_compile:
191  * @start: the routine to be called when the JIT process starts.
192  * @end: the routine to be called when the JIT process ends.
193  *
194  * Use this routine to install routines that will be called when JIT 
195  * compilation of a method starts and completes.
196  */
197 void 
198 mono_profiler_install_jit_compile (MonoProfileMethodFunc start, MonoProfileMethodResult end)
199 {
200         if (!prof_list)
201                 return;
202         prof_list->jit_start = start;
203         prof_list->jit_end = end;
204 }
205
206 void 
207 mono_profiler_install_jit_end (MonoProfileJitResult end)
208 {
209         if (!prof_list)
210                 return;
211         prof_list->jit_end2 = end;
212 }
213
214 void 
215 mono_profiler_install_method_free (MonoProfileMethodFunc callback)
216 {
217         if (!prof_list)
218                 return;
219         prof_list->method_free = callback;
220 }
221
222 void
223 mono_profiler_install_method_invoke (MonoProfileMethodFunc start, MonoProfileMethodFunc end)
224 {
225         if (!prof_list)
226                 return;
227         prof_list->method_start_invoke = start;
228         prof_list->method_end_invoke = end;
229 }
230
231 void 
232 mono_profiler_install_thread (MonoProfileThreadFunc start, MonoProfileThreadFunc end)
233 {
234         if (!prof_list)
235                 return;
236         prof_list->thread_start = start;
237         prof_list->thread_end = end;
238 }
239
240 void 
241 mono_profiler_install_transition (MonoProfileMethodResult callback)
242 {
243         if (!prof_list)
244                 return;
245         prof_list->man_unman_transition = callback;
246 }
247
248 void 
249 mono_profiler_install_allocation (MonoProfileAllocFunc callback)
250 {
251         if (!prof_list)
252                 return;
253         prof_list->allocation_cb = callback;
254 }
255
256 void
257 mono_profiler_install_monitor  (MonoProfileMonitorFunc callback)
258 {
259         if (!prof_list)
260                 return;
261         prof_list->monitor_event_cb = callback;
262 }
263
264 void 
265 mono_profiler_install_statistical (MonoProfileStatFunc callback)
266 {
267         if (!prof_list)
268                 return;
269         prof_list->statistical_cb = callback;
270 }
271
272 void 
273 mono_profiler_install_statistical_call_chain (MonoProfileStatCallChainFunc callback, int call_chain_depth, MonoProfilerCallChainStrategy call_chain_strategy) {
274         if (!prof_list)
275                 return;
276         if (call_chain_depth > MONO_PROFILER_MAX_STAT_CALL_CHAIN_DEPTH) {
277                 call_chain_depth = MONO_PROFILER_MAX_STAT_CALL_CHAIN_DEPTH;
278         }
279         if ((call_chain_strategy >= MONO_PROFILER_CALL_CHAIN_INVALID) || (call_chain_strategy < MONO_PROFILER_CALL_CHAIN_NONE)) {
280                 call_chain_strategy = MONO_PROFILER_CALL_CHAIN_NONE;
281         }
282         prof_list->statistical_call_chain_cb = callback;
283         prof_list->statistical_call_chain_depth = call_chain_depth;
284         prof_list->statistical_call_chain_strategy = call_chain_strategy;
285 }
286
287 int
288 mono_profiler_stat_get_call_chain_depth (void) {
289         if (prof_list && prof_list->statistical_call_chain_cb != NULL) {
290                 return prof_list->statistical_call_chain_depth;
291         } else {
292                 return 0;
293         }
294 }
295
296 MonoProfilerCallChainStrategy
297 mono_profiler_stat_get_call_chain_strategy (void) {
298         if (prof_list && prof_list->statistical_call_chain_cb != NULL) {
299                 return prof_list->statistical_call_chain_strategy;
300         } else {
301                 return MONO_PROFILER_CALL_CHAIN_NONE;
302         }
303 }
304
305 void mono_profiler_install_exception (MonoProfileExceptionFunc throw_callback, MonoProfileMethodFunc exc_method_leave, MonoProfileExceptionClauseFunc clause_callback)
306 {
307         if (!prof_list)
308                 return;
309         prof_list->exception_throw_cb = throw_callback;
310         prof_list->exception_method_leave_cb = exc_method_leave;
311         prof_list->exception_clause_cb = clause_callback;
312 }
313
314 void 
315 mono_profiler_install_coverage_filter (MonoProfileCoverageFilterFunc callback)
316 {
317         if (!prof_list)
318                 return;
319         prof_list->coverage_filter_cb = callback;
320 }
321
322 void 
323 mono_profiler_install_appdomain   (MonoProfileAppDomainFunc start_load, MonoProfileAppDomainResult end_load,
324                                    MonoProfileAppDomainFunc start_unload, MonoProfileAppDomainFunc end_unload)
325
326 {
327         if (!prof_list)
328                 return;
329         prof_list->domain_start_load = start_load;
330         prof_list->domain_end_load = end_load;
331         prof_list->domain_start_unload = start_unload;
332         prof_list->domain_end_unload = end_unload;
333 }
334
335 void 
336 mono_profiler_install_assembly    (MonoProfileAssemblyFunc start_load, MonoProfileAssemblyResult end_load,
337                                    MonoProfileAssemblyFunc start_unload, MonoProfileAssemblyFunc end_unload)
338 {
339         if (!prof_list)
340                 return;
341         prof_list->assembly_start_load = start_load;
342         prof_list->assembly_end_load = end_load;
343         prof_list->assembly_start_unload = start_unload;
344         prof_list->assembly_end_unload = end_unload;
345 }
346
347 void 
348 mono_profiler_install_module      (MonoProfileModuleFunc start_load, MonoProfileModuleResult end_load,
349                                    MonoProfileModuleFunc start_unload, MonoProfileModuleFunc end_unload)
350 {
351         if (!prof_list)
352                 return;
353         prof_list->module_start_load = start_load;
354         prof_list->module_end_load = end_load;
355         prof_list->module_start_unload = start_unload;
356         prof_list->module_end_unload = end_unload;
357 }
358
359 void
360 mono_profiler_install_class       (MonoProfileClassFunc start_load, MonoProfileClassResult end_load,
361                                    MonoProfileClassFunc start_unload, MonoProfileClassFunc end_unload)
362 {
363         if (!prof_list)
364                 return;
365         prof_list->class_start_load = start_load;
366         prof_list->class_end_load = end_load;
367         prof_list->class_start_unload = start_unload;
368         prof_list->class_end_unload = end_unload;
369 }
370
371 void
372 mono_profiler_method_enter (MonoMethod *method)
373 {
374         ProfilerDesc *prof;
375         for (prof = prof_list; prof; prof = prof->next) {
376                 if ((prof->events & MONO_PROFILE_ENTER_LEAVE) && prof->method_enter)
377                         prof->method_enter (prof->profiler, method);
378         }
379 }
380
381 void
382 mono_profiler_method_leave (MonoMethod *method)
383 {
384         ProfilerDesc *prof;
385         for (prof = prof_list; prof; prof = prof->next) {
386                 if ((prof->events & MONO_PROFILE_ENTER_LEAVE) && prof->method_leave)
387                         prof->method_leave (prof->profiler, method);
388         }
389 }
390
391 void 
392 mono_profiler_method_jit (MonoMethod *method)
393 {
394         ProfilerDesc *prof;
395         for (prof = prof_list; prof; prof = prof->next) {
396                 if ((prof->events & MONO_PROFILE_JIT_COMPILATION) && prof->jit_start)
397                         prof->jit_start (prof->profiler, method);
398         }
399 }
400
401 void 
402 mono_profiler_method_end_jit (MonoMethod *method, MonoJitInfo* jinfo, int result)
403 {
404         ProfilerDesc *prof;
405         for (prof = prof_list; prof; prof = prof->next) {
406                 if ((prof->events & MONO_PROFILE_JIT_COMPILATION)) {
407                         if (prof->jit_end)
408                                 prof->jit_end (prof->profiler, method, result);
409                         if (prof->jit_end2)
410                                 prof->jit_end2 (prof->profiler, method, jinfo, result);
411                 }
412         }
413 }
414
415 void 
416 mono_profiler_method_free (MonoMethod *method)
417 {
418         ProfilerDesc *prof;
419         for (prof = prof_list; prof; prof = prof->next) {
420                 if ((prof->events & MONO_PROFILE_METHOD_EVENTS) && prof->method_free)
421                         prof->method_free (prof->profiler, method);
422         }
423 }
424
425 void
426 mono_profiler_method_start_invoke (MonoMethod *method)
427 {
428         ProfilerDesc *prof;
429         for (prof = prof_list; prof; prof = prof->next) {
430                 if ((prof->events & MONO_PROFILE_METHOD_EVENTS) && prof->method_start_invoke)
431                         prof->method_start_invoke (prof->profiler, method);
432         }
433 }
434
435 void
436 mono_profiler_method_end_invoke (MonoMethod *method)
437 {
438         ProfilerDesc *prof;
439         for (prof = prof_list; prof; prof = prof->next) {
440                 if ((prof->events & MONO_PROFILE_METHOD_EVENTS) && prof->method_end_invoke)
441                         prof->method_end_invoke (prof->profiler, method);
442         }
443 }
444
445 void 
446 mono_profiler_code_transition (MonoMethod *method, int result)
447 {
448         ProfilerDesc *prof;
449         for (prof = prof_list; prof; prof = prof->next) {
450                 if ((prof->events & MONO_PROFILE_TRANSITIONS) && prof->man_unman_transition)
451                         prof->man_unman_transition (prof->profiler, method, result);
452         }
453 }
454
455 void 
456 mono_profiler_allocation (MonoObject *obj, MonoClass *klass)
457 {
458         ProfilerDesc *prof;
459         for (prof = prof_list; prof; prof = prof->next) {
460                 if ((prof->events & MONO_PROFILE_ALLOCATIONS) && prof->allocation_cb)
461                         prof->allocation_cb (prof->profiler, obj, klass);
462         }
463 }
464
465 void
466 mono_profiler_monitor_event      (MonoObject *obj, MonoProfilerMonitorEvent event) {
467         ProfilerDesc *prof;
468         for (prof = prof_list; prof; prof = prof->next) {
469                 if ((prof->events & MONO_PROFILE_MONITOR_EVENTS) && prof->monitor_event_cb)
470                         prof->monitor_event_cb (prof->profiler, obj, event);
471         }
472 }
473
474 void
475 mono_profiler_stat_hit (guchar *ip, void *context)
476 {
477         ProfilerDesc *prof;
478         for (prof = prof_list; prof; prof = prof->next) {
479                 if ((prof->events & MONO_PROFILE_STATISTICAL) && prof->statistical_cb)
480                         prof->statistical_cb (prof->profiler, ip, context);
481         }
482 }
483
484 void
485 mono_profiler_stat_call_chain (int call_chain_depth, guchar **ips, void *context)
486 {
487         ProfilerDesc *prof;
488         for (prof = prof_list; prof; prof = prof->next) {
489                 if ((prof->events & MONO_PROFILE_STATISTICAL) && prof->statistical_call_chain_cb)
490                         prof->statistical_call_chain_cb (prof->profiler, call_chain_depth, ips, context);
491         }
492 }
493
494 void
495 mono_profiler_exception_thrown (MonoObject *exception)
496 {
497         ProfilerDesc *prof;
498         for (prof = prof_list; prof; prof = prof->next) {
499                 if ((prof->events & MONO_PROFILE_EXCEPTIONS) && prof->exception_throw_cb)
500                         prof->exception_throw_cb (prof->profiler, exception);
501         }
502 }
503
504 void
505 mono_profiler_exception_method_leave (MonoMethod *method)
506 {
507         ProfilerDesc *prof;
508         for (prof = prof_list; prof; prof = prof->next) {
509                 if ((prof->events & MONO_PROFILE_EXCEPTIONS) && prof->exception_method_leave_cb)
510                         prof->exception_method_leave_cb (prof->profiler, method);
511         }
512 }
513
514 void
515 mono_profiler_exception_clause_handler (MonoMethod *method, int clause_type, int clause_num)
516 {
517         ProfilerDesc *prof;
518         for (prof = prof_list; prof; prof = prof->next) {
519                 if ((prof->events & MONO_PROFILE_EXCEPTIONS) && prof->exception_clause_cb)
520                         prof->exception_clause_cb (prof->profiler, method, clause_type, clause_num);
521         }
522 }
523
524 void
525 mono_profiler_thread_start (gsize tid)
526 {
527         ProfilerDesc *prof;
528         for (prof = prof_list; prof; prof = prof->next) {
529                 if ((prof->events & MONO_PROFILE_THREADS) && prof->thread_start)
530                         prof->thread_start (prof->profiler, tid);
531         }
532 }
533
534 void 
535 mono_profiler_thread_end (gsize tid)
536 {
537         ProfilerDesc *prof;
538         for (prof = prof_list; prof; prof = prof->next) {
539                 if ((prof->events & MONO_PROFILE_THREADS) && prof->thread_end)
540                         prof->thread_end (prof->profiler, tid);
541         }
542 }
543
544 void 
545 mono_profiler_assembly_event  (MonoAssembly *assembly, int code)
546 {
547         ProfilerDesc *prof;
548         for (prof = prof_list; prof; prof = prof->next) {
549                 if (!(prof->events & MONO_PROFILE_ASSEMBLY_EVENTS))
550                         continue;
551
552                 switch (code) {
553                 case MONO_PROFILE_START_LOAD:
554                         if (prof->assembly_start_load)
555                                 prof->assembly_start_load (prof->profiler, assembly);
556                         break;
557                 case MONO_PROFILE_START_UNLOAD:
558                         if (prof->assembly_start_unload)
559                                 prof->assembly_start_unload (prof->profiler, assembly);
560                         break;
561                 case MONO_PROFILE_END_UNLOAD:
562                         if (prof->assembly_end_unload)
563                                 prof->assembly_end_unload (prof->profiler, assembly);
564                         break;
565                 default:
566                         g_assert_not_reached ();
567                 }
568         }
569 }
570
571 void 
572 mono_profiler_assembly_loaded (MonoAssembly *assembly, int result)
573 {
574         ProfilerDesc *prof;
575         for (prof = prof_list; prof; prof = prof->next) {
576                 if ((prof->events & MONO_PROFILE_ASSEMBLY_EVENTS) && prof->assembly_end_load)
577                         prof->assembly_end_load (prof->profiler, assembly, result);
578         }
579 }
580
581 void mono_profiler_iomap (char *report, const char *pathname, const char *new_pathname)
582 {
583         ProfilerDesc *prof;
584         for (prof = prof_list; prof; prof = prof->next) {
585                 if ((prof->events & MONO_PROFILE_IOMAP_EVENTS) && prof->iomap_cb)
586                         prof->iomap_cb (prof->profiler, report, pathname, new_pathname);
587         }
588 }
589
590 void 
591 mono_profiler_module_event  (MonoImage *module, int code)
592 {
593         ProfilerDesc *prof;
594         for (prof = prof_list; prof; prof = prof->next) {
595                 if (!(prof->events & MONO_PROFILE_MODULE_EVENTS))
596                         continue;
597
598                 switch (code) {
599                 case MONO_PROFILE_START_LOAD:
600                         if (prof->module_start_load)
601                                 prof->module_start_load (prof->profiler, module);
602                         break;
603                 case MONO_PROFILE_START_UNLOAD:
604                         if (prof->module_start_unload)
605                                 prof->module_start_unload (prof->profiler, module);
606                         break;
607                 case MONO_PROFILE_END_UNLOAD:
608                         if (prof->module_end_unload)
609                                 prof->module_end_unload (prof->profiler, module);
610                         break;
611                 default:
612                         g_assert_not_reached ();
613                 }
614         }
615 }
616
617 void 
618 mono_profiler_module_loaded (MonoImage *module, int result)
619 {
620         ProfilerDesc *prof;
621         for (prof = prof_list; prof; prof = prof->next) {
622                 if ((prof->events & MONO_PROFILE_MODULE_EVENTS) && prof->module_end_load)
623                         prof->module_end_load (prof->profiler, module, result);
624         }
625 }
626
627 void 
628 mono_profiler_class_event  (MonoClass *klass, int code)
629 {
630         ProfilerDesc *prof;
631         for (prof = prof_list; prof; prof = prof->next) {
632                 if (!(prof->events & MONO_PROFILE_CLASS_EVENTS))
633                         continue;
634
635                 switch (code) {
636                 case MONO_PROFILE_START_LOAD:
637                         if (prof->class_start_load)
638                                 prof->class_start_load (prof->profiler, klass);
639                         break;
640                 case MONO_PROFILE_START_UNLOAD:
641                         if (prof->class_start_unload)
642                                 prof->class_start_unload (prof->profiler, klass);
643                         break;
644                 case MONO_PROFILE_END_UNLOAD:
645                         if (prof->class_end_unload)
646                                 prof->class_end_unload (prof->profiler, klass);
647                         break;
648                 default:
649                         g_assert_not_reached ();
650                 }
651         }
652 }
653
654 void 
655 mono_profiler_class_loaded (MonoClass *klass, int result)
656 {
657         ProfilerDesc *prof;
658         for (prof = prof_list; prof; prof = prof->next) {
659                 if ((prof->events & MONO_PROFILE_CLASS_EVENTS) && prof->class_end_load)
660                         prof->class_end_load (prof->profiler, klass, result);
661         }
662 }
663
664 void 
665 mono_profiler_appdomain_event  (MonoDomain *domain, int code)
666 {
667         ProfilerDesc *prof;
668         for (prof = prof_list; prof; prof = prof->next) {
669                 if (!(prof->events & MONO_PROFILE_APPDOMAIN_EVENTS))
670                         continue;
671
672                 switch (code) {
673                 case MONO_PROFILE_START_LOAD:
674                         if (prof->domain_start_load)
675                                 prof->domain_start_load (prof->profiler, domain);
676                         break;
677                 case MONO_PROFILE_START_UNLOAD:
678                         if (prof->domain_start_unload)
679                                 prof->domain_start_unload (prof->profiler, domain);
680                         break;
681                 case MONO_PROFILE_END_UNLOAD:
682                         if (prof->domain_end_unload)
683                                 prof->domain_end_unload (prof->profiler, domain);
684                         break;
685                 default:
686                         g_assert_not_reached ();
687                 }
688         }
689 }
690
691 void 
692 mono_profiler_appdomain_loaded (MonoDomain *domain, int result)
693 {
694         ProfilerDesc *prof;
695         for (prof = prof_list; prof; prof = prof->next) {
696                 if ((prof->events & MONO_PROFILE_APPDOMAIN_EVENTS) && prof->domain_end_load)
697                         prof->domain_end_load (prof->profiler, domain, result);
698         }
699 }
700
701 void 
702 mono_profiler_shutdown (void)
703 {
704         ProfilerDesc *prof;
705         for (prof = prof_list; prof; prof = prof->next) {
706                 if (prof->shutdown_callback)
707                         prof->shutdown_callback (prof->profiler);
708         }
709
710         mono_profiler_set_events (0);
711 }
712
713 void
714 mono_profiler_gc_heap_resize (gint64 new_size)
715 {
716         ProfilerDesc *prof;
717         for (prof = prof_list; prof; prof = prof->next) {
718                 if ((prof->events & MONO_PROFILE_GC) && prof->gc_heap_resize)
719                         prof->gc_heap_resize (prof->profiler, new_size);
720         }
721 }
722
723 void
724 mono_profiler_gc_event (MonoGCEvent event, int generation)
725 {
726         ProfilerDesc *prof;
727         for (prof = prof_list; prof; prof = prof->next) {
728                 if ((prof->events & MONO_PROFILE_GC) && prof->gc_event)
729                         prof->gc_event (prof->profiler, event, generation);
730         }
731 }
732
733 void
734 mono_profiler_gc_moves (void **objects, int num)
735 {
736         ProfilerDesc *prof;
737         for (prof = prof_list; prof; prof = prof->next) {
738                 if ((prof->events & MONO_PROFILE_GC_MOVES) && prof->gc_moves)
739                         prof->gc_moves (prof->profiler, objects, num);
740         }
741 }
742
743 void
744 mono_profiler_install_gc (MonoProfileGCFunc callback, MonoProfileGCResizeFunc heap_resize_callback)
745 {
746         mono_gc_enable_events ();
747         if (!prof_list)
748                 return;
749         prof_list->gc_event = callback;
750         prof_list->gc_heap_resize = heap_resize_callback;
751 }
752
753 /**
754  * mono_profiler_install_gc_moves:
755  * @callback: callback function
756  *
757  * Install the @callback function that the GC will call when moving objects.
758  * The callback receives an array of pointers and the number of elements
759  * in the array. Every even element in the array is the original object location
760  * and the following odd element is the new location of the object in memory.
761  * So the number of elements argument will always be a multiple of 2.
762  * Since this callback happens during the GC, it is a restricted environment:
763  * no locks can be taken and the object pointers can be inspected only once
764  * the GC is finished (of course the original location pointers will not
765  * point to valid objects anymore).
766  */
767 void
768 mono_profiler_install_gc_moves (MonoProfileGCMoveFunc callback)
769 {
770         if (!prof_list)
771                 return;
772         prof_list->gc_moves = callback;
773 }
774
775 void
776 mono_profiler_install_runtime_initialized (MonoProfileFunc runtime_initialized_callback)
777 {
778         if (!prof_list)
779                 return;
780         prof_list->runtime_initialized_event = runtime_initialized_callback;
781 }
782
783 void
784 mono_profiler_runtime_initialized (void) {
785         ProfilerDesc *prof;
786         for (prof = prof_list; prof; prof = prof->next) {
787                 if (prof->runtime_initialized_event)
788                         prof->runtime_initialized_event (prof->profiler);
789         }
790 }
791
792 void
793 mono_profiler_install_code_chunk_new (MonoProfilerCodeChunkNew callback) {
794         if (!prof_list)
795                 return;
796         prof_list->code_chunk_new = callback;
797 }
798 void
799 mono_profiler_code_chunk_new (gpointer chunk, int size) {
800         ProfilerDesc *prof;
801         for (prof = prof_list; prof; prof = prof->next) {
802                 if (prof->code_chunk_new)
803                         prof->code_chunk_new (prof->profiler, chunk, size);
804         }
805 }
806
807 void
808 mono_profiler_install_code_chunk_destroy (MonoProfilerCodeChunkDestroy callback) {
809         if (!prof_list)
810                 return;
811         prof_list->code_chunk_destroy = callback;
812 }
813 void
814 mono_profiler_code_chunk_destroy (gpointer chunk) {
815         ProfilerDesc *prof;
816         for (prof = prof_list; prof; prof = prof->next) {
817                 if (prof->code_chunk_destroy)
818                         prof->code_chunk_destroy (prof->profiler, chunk);
819         }
820 }
821
822 void
823 mono_profiler_install_code_buffer_new (MonoProfilerCodeBufferNew callback) {
824         if (!prof_list)
825                 return;
826         prof_list->code_buffer_new = callback;
827 }
828
829 void
830 mono_profiler_install_iomap (MonoProfileIomapFunc callback)
831 {
832         if (!prof_list)
833                 return;
834         prof_list->iomap_cb = callback;
835 }
836
837 void
838 mono_profiler_code_buffer_new (gpointer buffer, int size, MonoProfilerCodeBufferType type, void *data) {
839         ProfilerDesc *prof;
840         for (prof = prof_list; prof; prof = prof->next) {
841                 if (prof->code_buffer_new)
842                         prof->code_buffer_new (prof->profiler, buffer, size, type, data);
843         }
844 }
845
846 static GHashTable *coverage_hash = NULL;
847
848 MonoProfileCoverageInfo* 
849 mono_profiler_coverage_alloc (MonoMethod *method, int entries)
850 {
851         MonoProfileCoverageInfo *res;
852         int instrument = FALSE;
853         ProfilerDesc *prof;
854
855         for (prof = prof_list; prof; prof = prof->next) {
856                 /* note that we call the filter on all the profilers even if just
857                  * a single one would be enough to instrument a method
858                  */
859                 if (prof->coverage_filter_cb)
860                         if (prof->coverage_filter_cb (prof->profiler, method))
861                                 instrument = TRUE;
862         }
863         if (!instrument)
864                 return NULL;
865
866         mono_profiler_coverage_lock ();
867         if (!coverage_hash)
868                 coverage_hash = g_hash_table_new (NULL, NULL);
869
870         res = g_malloc0 (sizeof (MonoProfileCoverageInfo) + sizeof (void*) * 2 * entries);
871
872         res->entries = entries;
873
874         g_hash_table_insert (coverage_hash, method, res);
875         mono_profiler_coverage_unlock ();
876
877         return res;
878 }
879
880 /* safe only when the method antive code has been unloaded */
881 void
882 mono_profiler_coverage_free (MonoMethod *method)
883 {
884         MonoProfileCoverageInfo* info;
885
886         mono_profiler_coverage_lock ();
887         if (!coverage_hash) {
888                 mono_profiler_coverage_unlock ();
889                 return;
890         }
891
892         info = g_hash_table_lookup (coverage_hash, method);
893         if (info) {
894                 g_free (info);
895                 g_hash_table_remove (coverage_hash, method);
896         }
897         mono_profiler_coverage_unlock ();
898 }
899
900 /**
901  * mono_profiler_coverage_get:
902  * @prof: The profiler handle, installed with mono_profiler_install
903  * @method: the method to gather information from.
904  * @func: A routine that will be called back with the results
905  *
906  * If the MONO_PROFILER_INS_COVERAGE flag was active during JIT compilation
907  * it is posisble to obtain coverage information about a give method.
908  *
909  * The function @func will be invoked repeatedly with instances of the
910  * MonoProfileCoverageEntry structure.
911  */
912 void 
913 mono_profiler_coverage_get (MonoProfiler *prof, MonoMethod *method, MonoProfileCoverageFunc func)
914 {
915         MonoProfileCoverageInfo* info;
916         int i, offset;
917         guint32 code_size;
918         const unsigned char *start, *end, *cil_code;
919         MonoMethodHeader *header;
920         MonoProfileCoverageEntry entry;
921         MonoDebugMethodInfo *debug_minfo;
922
923         mono_profiler_coverage_lock ();
924         info = g_hash_table_lookup (coverage_hash, method);
925         mono_profiler_coverage_unlock ();
926
927         if (!info)
928                 return;
929
930         header = mono_method_get_header (method);
931         start = mono_method_header_get_code (header, &code_size, NULL);
932         debug_minfo = mono_debug_lookup_method (method);
933
934         end = start + code_size;
935         for (i = 0; i < info->entries; ++i) {
936                 cil_code = info->data [i].cil_code;
937                 if (cil_code && cil_code >= start && cil_code < end) {
938                         char *fname = NULL;
939                         offset = cil_code - start;
940                         entry.iloffset = offset;
941                         entry.method = method;
942                         entry.counter = info->data [i].count;
943                         entry.line = entry.col = 1;
944                         entry.filename = NULL;
945                         if (debug_minfo) {
946                                 MonoDebugSourceLocation *location;
947
948                                 location = mono_debug_symfile_lookup_location (debug_minfo, offset);
949                                 if (location) {
950                                         entry.line = location->row;
951                                         entry.col = location->column;
952                                         entry.filename = fname = g_strdup (location->source_file);
953                                         mono_debug_free_source_location (location);
954                                 }
955                         }
956
957                         func (prof, &entry);
958                         g_free (fname);
959                 }
960         }
961         mono_metadata_free_mh (header);
962 }
963
964 typedef void (*ProfilerInitializer) (const char*);
965 #define INITIALIZER_NAME "mono_profiler_startup"
966
967 /**
968  * mono_profiler_load:
969  * @desc: arguments to configure the profiler
970  *
971  * Invoke this method to initialize the profiler.   This will drive the
972  * loading of the internal ("default") or any external profilers.
973  *
974  * This routine is invoked by Mono's driver, but must be called manually
975  * if you embed Mono into your application.
976  */
977 void 
978 mono_profiler_load (const char *desc)
979 {
980         mono_gc_base_init ();
981
982 #ifndef DISABLE_PROFILER
983         if (!desc || (strcmp ("default", desc) == 0) || (strncmp (desc, "default:", 8) == 0)) {
984                 mono_profiler_install_simple (desc);
985                 return;
986         }
987 #else
988         if (!desc) {
989                 desc = "default";
990         }
991 #endif
992         {
993                 MonoDl *pmodule = NULL;
994                 const char* col = strchr (desc, ':');
995                 char* libname;
996                 char* path;
997                 char *mname;
998                 char *err;
999                 void *iter;
1000                 if (col != NULL) {
1001                         mname = g_memdup (desc, col - desc + 1);
1002                         mname [col - desc] = 0;
1003                 } else {
1004                         mname = g_strdup (desc);
1005                 }
1006                 libname = g_strdup_printf ("mono-profiler-%s", mname);
1007                 iter = NULL;
1008                 err = NULL;
1009                 while ((path = mono_dl_build_path (NULL, libname, &iter))) {
1010                         g_free (err);
1011                         pmodule = mono_dl_open (path, MONO_DL_LAZY, &err);
1012                         if (pmodule) {
1013                                 ProfilerInitializer func;
1014                                 if ((err = mono_dl_symbol (pmodule, INITIALIZER_NAME, (gpointer *)&func))) {
1015                                         g_warning ("Cannot find initializer function %s in profiler module: %s (%s)", INITIALIZER_NAME, libname, err);
1016                                         g_free (err);
1017                                         err = NULL;
1018                                 } else {
1019                                         func (desc);
1020                                 }
1021                                 break;
1022                         }
1023                         g_free (path);
1024                 }
1025                 if (!pmodule) {
1026                         g_warning ("Error loading profiler module '%s': %s", libname, err);
1027                         g_free (err);
1028                 }
1029                 g_free (libname);
1030                 g_free (mname);
1031                 g_free (path);
1032         }
1033 }
1034