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