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