2009-12-09 Rodrigo Kumpera <rkumpera@novell.com>
[mono.git] / mono / metadata / profiler.c
index 27eba5f606239b7596d957892eba8283dc04cf96..d8ae324f2b86ba0b1e806dfd752b92b27dcaf790 100644 (file)
@@ -1,15 +1,31 @@
+/*
+ * profiler.c: Profiler interface for Mono
+ *
+ * Author:
+ *   Paolo Molaro (lupus@ximian.com)
+ *
+ * Copyright 2001-2003 Ximian, Inc (http://www.ximian.com)
+ * Copyright 2004-2009 Novell, Inc (http://www.novell.com)
+ */
 
 #include "config.h"
 #include "mono/metadata/profiler-private.h"
 #include "mono/metadata/debug-helpers.h"
 #include "mono/metadata/mono-debug.h"
+#include "mono/metadata/debug-mono-symfile.h"
+#include "mono/metadata/metadata-internals.h"
 #include "mono/metadata/class-internals.h"
 #include "mono/metadata/domain-internals.h"
 #include "mono/metadata/gc-internal.h"
 #include "mono/io-layer/io-layer.h"
+#include "mono/utils/mono-dl.h"
 #include <string.h>
+#ifdef HAVE_UNISTD_H
+#include <unistd.h>
+#endif
+#ifdef HAVE_SYS_TIME_H
 #include <sys/time.h>
-#include <gmodule.h>
+#endif
 #ifdef HAVE_BACKTRACE_SYMBOLS
 #include <execinfo.h>
 #endif
@@ -38,12 +54,24 @@ static MonoProfileClassFunc   class_end_unload;
 
 static MonoProfileMethodFunc   jit_start;
 static MonoProfileMethodResult jit_end;
+static MonoProfileJitResult    jit_end2;
+static MonoProfileMethodFunc   method_free;
+static MonoProfileMethodFunc   method_start_invoke;
+static MonoProfileMethodFunc   method_end_invoke;
 static MonoProfileMethodResult man_unman_transition;
 static MonoProfileAllocFunc    allocation_cb;
+static MonoProfileMonitorFunc  monitor_event_cb;
 static MonoProfileStatFunc     statistical_cb;
+static MonoProfileStatCallChainFunc statistical_call_chain_cb;
+static int                     statistical_call_chain_depth;
+static MonoProfilerCallChainStrategy  statistical_call_chain_strategy;
 static MonoProfileMethodFunc   method_enter;
 static MonoProfileMethodFunc   method_leave;
 
+static MonoProfileExceptionFunc        exception_throw_cb;
+static MonoProfileMethodFunc exception_method_leave_cb;
+static MonoProfileExceptionClauseFunc exception_clause_cb;
+
 static MonoProfileThreadFunc   thread_start;
 static MonoProfileThreadFunc   thread_end;
 
@@ -54,6 +82,8 @@ static MonoProfileFunc shutdown_callback;
 static MonoProfileGCFunc        gc_event;
 static MonoProfileGCResizeFunc  gc_heap_resize;
 
+static MonoProfileFunc          runtime_initialized_event;
+
 #define mono_profiler_coverage_lock() EnterCriticalSection (&profiler_coverage_mutex)
 #define mono_profiler_coverage_unlock() LeaveCriticalSection (&profiler_coverage_mutex)
 static CRITICAL_SECTION profiler_coverage_mutex;
@@ -61,6 +91,17 @@ static CRITICAL_SECTION profiler_coverage_mutex;
 /* this is directly accessible to other mono libs. */
 MonoProfileFlags mono_profiler_events;
 
+/**
+ * mono_profiler_install:
+ * @prof: a MonoProfiler structure pointer, or a pointer to a derived structure.
+ * @callback: the function to invoke at shutdown
+ *
+ * Use mono_profiler_install to activate profiling in the Mono runtime.
+ * Typically developers of new profilers will create a new structure whose
+ * first field is a MonoProfiler and put any extra information that they need
+ * to access from the various profiling callbacks there.
+ *
+ */
 void
 mono_profiler_install (MonoProfiler *prof, MonoProfileFunc callback)
 {
@@ -71,18 +112,46 @@ mono_profiler_install (MonoProfiler *prof, MonoProfileFunc callback)
        InitializeCriticalSection (&profiler_coverage_mutex);
 }
 
+/**
+ * mono_profiler_set_events:
+ * @events: an ORed set of values made up of MONO_PROFILER_ flags
+ *
+ * The events descriped in the @events argument is a set of flags
+ * that represent which profiling events must be triggered.  For
+ * example if you have registered a set of methods for tracking
+ * JIT compilation start and end with mono_profiler_install_jit_compile,
+ * you will want to pass the MONO_PROFILE_JIT_COMPILATION flag to
+ * this routine.
+ *
+ * You can call mono_profile_set_events more than once and you can
+ * do this at runtime to modify which methods are invoked.
+ */
 void
 mono_profiler_set_events (MonoProfileFlags events)
 {
        mono_profiler_events = events;
 }
 
+/**
+ * mono_profiler_get_events:
+ *
+ * Returns a list of active events that will be intercepted. 
+ */
 MonoProfileFlags
 mono_profiler_get_events (void)
 {
        return mono_profiler_events;
 }
 
+/**
+ * mono_profiler_install_enter_leave:
+ * @enter: the routine to be called on each method entry
+ * @fleave: the routine to be called each time a method returns
+ *
+ * Use this routine to install routines that will be called everytime
+ * a method enters and leaves.   The routines will receive as an argument
+ * the MonoMethod representing the method that is entering or leaving.
+ */
 void
 mono_profiler_install_enter_leave (MonoProfileMethodFunc enter, MonoProfileMethodFunc fleave)
 {
@@ -90,6 +159,14 @@ mono_profiler_install_enter_leave (MonoProfileMethodFunc enter, MonoProfileMetho
        method_leave = fleave;
 }
 
+/**
+ * mono_profiler_install_jit_compile:
+ * @start: the routine to be called when the JIT process starts.
+ * @end: the routine to be called when the JIT process ends.
+ *
+ * Use this routine to install routines that will be called when JIT 
+ * compilation of a method starts and completes.
+ */
 void 
 mono_profiler_install_jit_compile (MonoProfileMethodFunc start, MonoProfileMethodResult end)
 {
@@ -97,6 +174,25 @@ mono_profiler_install_jit_compile (MonoProfileMethodFunc start, MonoProfileMetho
        jit_end = end;
 }
 
+void 
+mono_profiler_install_jit_end (MonoProfileJitResult end)
+{
+       jit_end2 = end;
+}
+
+void 
+mono_profiler_install_method_free (MonoProfileMethodFunc callback)
+{
+       method_free = callback;
+}
+
+void
+mono_profiler_install_method_invoke (MonoProfileMethodFunc start, MonoProfileMethodFunc end)
+{
+       method_start_invoke = start;
+       method_end_invoke = end;
+}
+
 void 
 mono_profiler_install_thread (MonoProfileThreadFunc start, MonoProfileThreadFunc end)
 {
@@ -116,12 +212,56 @@ mono_profiler_install_allocation (MonoProfileAllocFunc callback)
        allocation_cb = callback;
 }
 
+void
+mono_profiler_install_monitor  (MonoProfileMonitorFunc callback)
+{
+       monitor_event_cb = callback;
+}
+
 void 
 mono_profiler_install_statistical (MonoProfileStatFunc callback)
 {
        statistical_cb = callback;
 }
 
+void 
+mono_profiler_install_statistical_call_chain (MonoProfileStatCallChainFunc callback, int call_chain_depth, MonoProfilerCallChainStrategy call_chain_strategy) {
+       statistical_call_chain_cb = callback;
+       statistical_call_chain_depth = call_chain_depth;
+       if (statistical_call_chain_depth > MONO_PROFILER_MAX_STAT_CALL_CHAIN_DEPTH) {
+               statistical_call_chain_depth = MONO_PROFILER_MAX_STAT_CALL_CHAIN_DEPTH;
+       }
+       statistical_call_chain_strategy = call_chain_strategy;
+       if ((statistical_call_chain_strategy >= MONO_PROFILER_CALL_CHAIN_INVALID) || (statistical_call_chain_strategy < MONO_PROFILER_CALL_CHAIN_NONE)) {
+               statistical_call_chain_strategy = MONO_PROFILER_CALL_CHAIN_NONE;
+       }
+}
+
+int
+mono_profiler_stat_get_call_chain_depth (void) {
+       if (statistical_call_chain_cb != NULL) {
+               return statistical_call_chain_depth;
+       } else {
+               return 0;
+       }
+}
+
+MonoProfilerCallChainStrategy
+mono_profiler_stat_get_call_chain_strategy (void) {
+       if (statistical_call_chain_cb != NULL) {
+               return statistical_call_chain_strategy;
+       } else {
+               return MONO_PROFILER_CALL_CHAIN_NONE;
+       }
+}
+
+void mono_profiler_install_exception (MonoProfileExceptionFunc throw_callback, MonoProfileMethodFunc exc_method_leave, MonoProfileExceptionClauseFunc clause_callback)
+{
+       exception_throw_cb = throw_callback;
+       exception_method_leave_cb = exc_method_leave;
+       exception_clause_cb = clause_callback;
+}
+
 void 
 mono_profiler_install_coverage_filter (MonoProfileCoverageFilterFunc callback)
 {
@@ -191,10 +331,35 @@ mono_profiler_method_jit (MonoMethod *method)
 }
 
 void 
-mono_profiler_method_end_jit (MonoMethod *method, int result)
+mono_profiler_method_end_jit (MonoMethod *method, MonoJitInfo* jinfo, int result)
 {
-       if ((mono_profiler_events & MONO_PROFILE_JIT_COMPILATION) && jit_end)
-               jit_end (current_profiler, method, result);
+       if ((mono_profiler_events & MONO_PROFILE_JIT_COMPILATION)) {
+               if (jit_end)
+                       jit_end (current_profiler, method, result);
+               if (jit_end2)
+                       jit_end2 (current_profiler, method, jinfo, result);
+       }
+}
+
+void 
+mono_profiler_method_free (MonoMethod *method)
+{
+       if ((mono_profiler_events & MONO_PROFILE_METHOD_EVENTS) && method_free)
+               method_free (current_profiler, method);
+}
+
+void
+mono_profiler_method_start_invoke (MonoMethod *method)
+{
+       if ((mono_profiler_events & MONO_PROFILE_METHOD_EVENTS) && method_start_invoke)
+               method_start_invoke (current_profiler, method);
+}
+
+void
+mono_profiler_method_end_invoke (MonoMethod *method)
+{
+       if ((mono_profiler_events & MONO_PROFILE_METHOD_EVENTS) && method_end_invoke)
+               method_end_invoke (current_profiler, method);
 }
 
 void 
@@ -211,6 +376,13 @@ mono_profiler_allocation (MonoObject *obj, MonoClass *klass)
                allocation_cb (current_profiler, obj, klass);
 }
 
+void
+mono_profiler_monitor_event      (MonoObject *obj, MonoProfilerMonitorEvent event) {
+       if ((mono_profiler_events & MONO_PROFILE_MONITOR_EVENTS) && monitor_event_cb) {
+               monitor_event_cb (current_profiler, obj, event);
+       }
+}
+
 void
 mono_profiler_stat_hit (guchar *ip, void *context)
 {
@@ -218,6 +390,34 @@ mono_profiler_stat_hit (guchar *ip, void *context)
                statistical_cb (current_profiler, ip, context);
 }
 
+void
+mono_profiler_stat_call_chain (int call_chain_depth, guchar **ips, void *context)
+{
+       if ((mono_profiler_events & MONO_PROFILE_STATISTICAL) && statistical_call_chain_cb)
+               statistical_call_chain_cb (current_profiler, call_chain_depth, ips, context);
+}
+
+void
+mono_profiler_exception_thrown (MonoObject *exception)
+{
+       if ((mono_profiler_events & MONO_PROFILE_EXCEPTIONS) && exception_throw_cb)
+               exception_throw_cb (current_profiler, exception);
+}
+
+void
+mono_profiler_exception_method_leave (MonoMethod *method)
+{
+       if ((mono_profiler_events & MONO_PROFILE_EXCEPTIONS) && exception_method_leave_cb)
+               exception_method_leave_cb (current_profiler, method);
+}
+
+void
+mono_profiler_exception_clause_handler (MonoMethod *method, int clause_type, int clause_num)
+{
+       if ((mono_profiler_events & MONO_PROFILE_EXCEPTIONS) && exception_clause_cb)
+               exception_clause_cb (current_profiler, method, clause_type, clause_num);
+}
+
 void
 mono_profiler_thread_start (gsize tid)
 {
@@ -385,6 +585,51 @@ mono_profiler_install_gc (MonoProfileGCFunc callback, MonoProfileGCResizeFunc he
        gc_heap_resize = heap_resize_callback;
 }
 
+void
+mono_profiler_install_runtime_initialized (MonoProfileFunc runtime_initialized_callback)
+{
+       runtime_initialized_event = runtime_initialized_callback;
+}
+
+void
+mono_profiler_runtime_initialized (void) {
+       if (runtime_initialized_event)
+               runtime_initialized_event (current_profiler);
+}
+
+static MonoProfilerCodeChunkNew code_chunk_new = NULL;
+void
+mono_profiler_install_code_chunk_new (MonoProfilerCodeChunkNew callback) {
+       code_chunk_new = callback;
+}
+void
+mono_profiler_code_chunk_new (gpointer chunk, int size) {
+       if (code_chunk_new)
+               code_chunk_new (current_profiler, chunk, size);
+}
+
+static MonoProfilerCodeChunkDestroy code_chunk_destroy = NULL;
+void
+mono_profiler_install_code_chunk_destroy (MonoProfilerCodeChunkDestroy callback) {
+       code_chunk_destroy = callback;
+}
+void
+mono_profiler_code_chunk_destroy (gpointer chunk) {
+       if (code_chunk_destroy)
+               code_chunk_destroy (current_profiler, chunk);
+}
+
+static MonoProfilerCodeBufferNew code_buffer_new = NULL;
+void
+mono_profiler_install_code_buffer_new (MonoProfilerCodeBufferNew callback) {
+       code_buffer_new = callback;
+}
+void
+mono_profiler_code_buffer_new (gpointer buffer, int size, MonoProfilerCodeBufferType type, void *data) {
+       if (code_buffer_new)
+               code_buffer_new (current_profiler, buffer, size, type, data);
+}
+
 static GHashTable *coverage_hash = NULL;
 
 MonoProfileCoverageInfo* 
@@ -430,15 +675,28 @@ mono_profiler_coverage_free (MonoMethod *method)
        mono_profiler_coverage_unlock ();
 }
 
+/**
+ * mono_profiler_coverage_get:
+ * @prof: The profiler handle, installed with mono_profiler_install
+ * @method: the method to gather information from.
+ * @func: A routine that will be called back with the results
+ *
+ * If the MONO_PROFILER_INS_COVERAGE flag was active during JIT compilation
+ * it is posisble to obtain coverage information about a give method.
+ *
+ * The function @func will be invoked repeatedly with instances of the
+ * MonoProfileCoverageEntry structure.
+ */
 void 
 mono_profiler_coverage_get (MonoProfiler *prof, MonoMethod *method, MonoProfileCoverageFunc func)
 {
        MonoProfileCoverageInfo* info;
        int i, offset;
-       guint32 line, col;
-       unsigned char *start, *end, *cil_code;
+       guint32 code_size;
+       const unsigned char *start, *end, *cil_code;
        MonoMethodHeader *header;
        MonoProfileCoverageEntry entry;
+       MonoDebugMethodInfo *debug_minfo;
 
        mono_profiler_coverage_lock ();
        info = g_hash_table_lookup (coverage_hash, method);
@@ -448,21 +706,34 @@ mono_profiler_coverage_get (MonoProfiler *prof, MonoMethod *method, MonoProfileC
                return;
 
        header = mono_method_get_header (method);
-       start = (unsigned char*)header->code;
-       end = start + header->code_size;
+       start = mono_method_header_get_code (header, &code_size, NULL);
+       debug_minfo = mono_debug_lookup_method (method);
+
+       end = start + code_size;
        for (i = 0; i < info->entries; ++i) {
                cil_code = info->data [i].cil_code;
                if (cil_code && cil_code >= start && cil_code < end) {
+                       char *fname = NULL;
                        offset = cil_code - start;
                        entry.iloffset = offset;
                        entry.method = method;
                        entry.counter = info->data [i].count;
-                       /* the debug interface doesn't support column info, sigh */
-                       col = line = 1;
-                       entry.filename = mono_debug_source_location_from_il_offset (method, offset, &line);
-                       entry.line = line;
-                       entry.col = col;
+                       entry.line = entry.col = 1;
+                       entry.filename = NULL;
+                       if (debug_minfo) {
+                               MonoDebugSourceLocation *location;
+
+                               location = mono_debug_symfile_lookup_location (debug_minfo, offset);
+                               if (location) {
+                                       entry.line = location->row;
+                                       entry.col = location->column;
+                                       entry.filename = fname = g_strdup (location->source_file);
+                                       mono_debug_free_source_location (location);
+                               }
+                       }
+
                        func (prof, &entry);
+                       g_free (fname);
                }
        }
 }
@@ -473,6 +744,8 @@ mono_profiler_coverage_get (MonoProfiler *prof, MonoMethod *method, MonoProfileC
  * and improve it to do graphs and more accurate timestamping with rdtsc.
  */
 
+static FILE* poutput = NULL;
+
 #define USE_X86TSC 0
 #define USE_WIN32COUNTER 0
 #if USE_X86TSC
@@ -599,6 +872,7 @@ typedef struct _LastCallerInfo LastCallerInfo;
 struct _MonoProfiler {
        GHashTable *methods;
        MonoMemPool *mempool;
+       GSList *domains;
        /* info about JIT time */
        MONO_TIMER_TYPE jit_timer;
        double      jit_time;
@@ -654,7 +928,7 @@ create_profiler (void)
 {
        MonoProfiler *prof = g_new0 (MonoProfiler, 1);
 
-       prof->methods = g_hash_table_new (NULL, NULL);
+       prof->methods = g_hash_table_new (mono_aligned_addr_hash, NULL);
        MONO_TIMER_INIT (prof->jit_timer);
        prof->mempool = mono_mempool_new ();
        return prof;
@@ -715,6 +989,14 @@ method_get_name (MonoMethod* method)
 
 static void output_callers (MethodProfile *p);
 
+/* This isn't defined on older glib versions and on some platforms */
+#ifndef G_GUINT64_FORMAT
+#define G_GUINT64_FORMAT "ul"
+#endif
+#ifndef G_GINT64_FORMAT
+#define G_GINT64_FORMAT "lld"
+#endif
+
 static void
 output_profile (GList *funcs)
 {
@@ -724,24 +1006,24 @@ output_profile (GList *funcs)
        guint64 total_calls = 0;
 
        if (funcs)
-               g_print ("Time(ms) Count   P/call(ms) Method name\n");
+               fprintf (poutput, "Time(ms) Count   P/call(ms) Method name\n");
        for (tmp = funcs; tmp; tmp = tmp->next) {
                p = tmp->data;
                total_calls += p->count;
                if (!(gint)(p->total*1000))
                        continue;
                m = method_get_name (p->method);
-               printf ("########################\n");
-               printf ("% 8.3f ", (double) (p->total * 1000));
-               printf ("%7llu ", (unsigned long long)p->count);
-               printf ("% 8.3f ", (double) (p->total * 1000)/(double)p->count);
-               printf ("  %s\n", m);
+               fprintf (poutput, "########################\n");
+               fprintf (poutput, "% 8.3f ", (double) (p->total * 1000));
+               fprintf (poutput, "%7" G_GUINT64_FORMAT " ", (guint64)p->count);
+               fprintf (poutput, "% 8.3f ", (double) (p->total * 1000)/(double)p->count);
+               fprintf (poutput, "  %s\n", m);
 
                g_free (m);
                /* callers */
                output_callers (p);
        }
-       printf ("Total number of calls: %lld\n", (long long)total_calls);
+       fprintf (poutput, "Total number of calls: %" G_GINT64_FORMAT "\n", (gint64)total_calls);
 }
 
 typedef struct {
@@ -814,7 +1096,7 @@ output_callers (MethodProfile *p) {
        CallerInfo *cinfo;
        char *m;
        
-       g_print ("  Callers (with count) that contribute at least for 1%%:\n");
+       fprintf (poutput, "  Callers (with count) that contribute at least for 1%%:\n");
        total_callers = 0;
        for (cinfo = p->caller_info; cinfo; cinfo = cinfo->next) {
                total_callers += cinfo->count;
@@ -826,16 +1108,11 @@ output_callers (MethodProfile *p) {
                if (percent < 1)
                        continue;
                m = method_get_name (cinfo->caller);
-               g_print ("    %8d % 3d %% %s\n", cinfo->count, percent, m);
+               fprintf (poutput, "    %8d % 3d %% %s\n", cinfo->count, percent, m);
                g_free (m);
        }
 }
 
-/* This isn't defined on older glib versions and on some platforms */
-#ifndef G_GUINT64_FORMAT
-#define G_GUINT64_FORMAT "ul"
-#endif
-
 static void
 output_newobj_profile (GList *proflist)
 {
@@ -850,10 +1127,10 @@ output_newobj_profile (GList *proflist)
        guint64 total = 0;
        GSList *sorted, *tmps;
 
-       g_print ("\nAllocation profiler\n");
+       fprintf (poutput, "\nAllocation profiler\n");
 
        if (proflist)
-               g_print ("%-9s %s\n", "Total mem", "Method");
+               fprintf (poutput, "%-9s %s\n", "Total mem", "Method");
        for (tmp = proflist; tmp; tmp = tmp->next) {
                p = tmp->data;
                total += p->count;
@@ -861,7 +1138,7 @@ output_newobj_profile (GList *proflist)
                        continue;
                mp = p->mp;
                m = method_get_name (mp->method);
-               g_print ("########################\n%8" G_GUINT64_FORMAT " KB %s\n", (p->count / 1024), m);
+               fprintf (poutput, "########################\n%8" G_GUINT64_FORMAT " KB %s\n", (p->count / 1024), m);
                g_free (m);
                sorted = sort_alloc_list (mp->alloc_info);
                for (tmps = sorted; tmps; tmps = tmps->next) {
@@ -875,14 +1152,14 @@ output_newobj_profile (GList *proflist)
                        } else {
                                isarray = "";
                        }
-                       g_snprintf (buf, sizeof (buf), "%s%%s%s",
+                       g_snprintf (buf, sizeof (buf), "%s%s%s%s",
                                klass->name_space, klass->name_space ? "." : "", klass->name, isarray);
-                       g_print ("    %8" G_GUINT64_FORMAT " KB %8" G_GUINT64_FORMAT " %-48s\n", (ainfo->mem / 1024), ainfo->count, buf);
+                       fprintf (poutput, "    %8" G_GUINT64_FORMAT " KB %8" G_GUINT64_FORMAT " %-48s\n", (ainfo->mem / 1024), ainfo->count, buf);
                }
                /* callers */
                output_callers (mp);
        }
-       g_print ("Total memory allocated: %" G_GUINT64_FORMAT " KB\n", total / 1024);
+       fprintf (poutput, "Total memory allocated: %" G_GUINT64_FORMAT " KB\n", total / 1024);
 }
 
 static void
@@ -1027,7 +1304,7 @@ simple_allocation (MonoProfiler *prof, MonoObject *obj, MonoClass *klass)
                MonoMethod *caller = prof->callers->method;
 
                /* Otherwise all allocations are attributed to icall_wrapper_mono_object_new */
-               if (caller->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE)
+               if (caller->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE && prof->callers->next)
                        caller = prof->callers->next->method;
 
                if (!(profile_info = g_hash_table_lookup (prof->methods, caller)))
@@ -1132,6 +1409,17 @@ try_addr2line (const char* binary, gpointer ip)
                const char *addr_argv[] = {"addr2line", "-f", "-e", binary, NULL};
                int child_pid;
                int ch_in, ch_out;
+#ifdef __linux__
+               char monobin [1024];
+               /* non-linux platforms will need different code here */
+               if (strcmp (binary, "mono") == 0) {
+                       int count = readlink ("/proc/self/exe", monobin, sizeof (monobin));
+                       if (count >= 0 && count < sizeof (monobin)) {
+                               monobin [count] = 0;
+                               addr_argv [3] = monobin;
+                       }
+               }
+#endif
                if (!g_spawn_async_with_pipes (NULL, (char**)addr_argv, NULL, G_SPAWN_SEARCH_PATH, NULL, NULL,
                                &child_pid, &ch_in, &ch_out, NULL, NULL)) {
                        return g_strdup (binary);
@@ -1161,7 +1449,7 @@ try_addr2line (const char* binary, gpointer ip)
 }
 
 static void
-stat_prof_report (void)
+stat_prof_report (MonoProfiler *prof)
 {
        MonoJitInfo *ji;
        int count = prof_counts;
@@ -1169,12 +1457,19 @@ stat_prof_report (void)
        char *mn;
        gpointer ip;
        GList *tmp, *sorted = NULL;
+       GSList *l;
        int pcount = ++ prof_counts;
 
        prof_counts = MAX_PROF_SAMPLES;
        for (i = 0; i < count; ++i) {
                ip = prof_addresses [i];
                ji = mono_jit_info_table_find (mono_domain_get (), ip);
+
+               if (!ji) {
+                       for (l = prof->domains; l && !ji; l = l->next)
+                               ji = mono_jit_info_table_find (l->data, ip);
+               }
+
                if (ji) {
                        mn = mono_method_full_name (ji->method, TRUE);
                } else {
@@ -1214,27 +1509,34 @@ stat_prof_report (void)
                if (c > 1)
                        g_free (mn);
        }
-       g_print ("prof counts: total/unmanaged: %d/%d\n", pcount, prof_ucounts);
+       fprintf (poutput, "prof counts: total/unmanaged: %d/%d\n", pcount, prof_ucounts);
        g_hash_table_foreach (prof_table, (GHFunc)prof_foreach, &sorted);
        for (tmp = sorted; tmp; tmp = tmp->next) {
                double perc;
                c = GPOINTER_TO_UINT (g_hash_table_lookup (prof_table, tmp->data));
                perc = c*100.0/count;
-               g_print ("%7d\t%5.2f %% %s\n", c, perc, (char*)tmp->data);
+               fprintf (poutput, "%7d\t%5.2f %% %s\n", c, perc, (char*)tmp->data);
        }
        g_list_free (sorted);
 }
 
+static void
+simple_appdomain_load (MonoProfiler *prof, MonoDomain *domain, int result)
+{
+       prof->domains = g_slist_prepend (prof->domains, domain);
+}
+
 static void
 simple_appdomain_unload (MonoProfiler *prof, MonoDomain *domain)
 {
        /* FIXME: we should actually record partial data for each domain, 
-        * since the ip->ji->method mappings are going away at domain unload time.
+        * but at this point it's must easier using the new logging profiler.
         */
-       if (domain == mono_get_root_domain ())
-               stat_prof_report ();
+       mono_profiler_shutdown ();
 }
 
+static gint32 simple_shutdown_done = FALSE;
+
 static void
 simple_shutdown (MonoProfiler *prof)
 {
@@ -1242,16 +1544,33 @@ simple_shutdown (MonoProfiler *prof)
        MonoProfiler *tprof;
        GSList *tmp;
        char *str;
+       gint32 see_shutdown_done;
 
+#ifndef HOST_WIN32
+       mono_thread_attach(mono_get_root_domain());
+#endif
+
+       // Make sure we execute simple_shutdown only once
+       see_shutdown_done = InterlockedExchange(& simple_shutdown_done, TRUE);
+       if (see_shutdown_done)
+               return;
+
+       if (mono_profiler_events & MONO_PROFILE_STATISTICAL) {
+               stat_prof_report (prof);
+       }
+
+       // Stop all incoming events
+       mono_profiler_set_events (0);
+       
        for (tmp = prof->per_thread; tmp; tmp = tmp->next) {
                tprof = tmp->data;
                merge_thread_data (prof, tprof);
        }
 
-       printf("Total time spent compiling %d methods (sec): %.4g\n", prof->methods_jitted, prof->jit_time);
+       fprintf (poutput, "Total time spent compiling %d methods (sec): %.4g\n", prof->methods_jitted, prof->jit_time);
        if (prof->max_jit_method) {
                str = method_get_name (prof->max_jit_method);
-               printf("Slowest method to compile (sec): %.4g: %s\n", prof->max_jit_time, str);
+               fprintf (poutput, "Slowest method to compile (sec): %.4g: %s\n", prof->max_jit_time, str);
                g_free (str);
        }
        g_hash_table_foreach (prof->methods, (GHFunc)build_profile, &profile);
@@ -1276,6 +1595,7 @@ mono_profiler_install_simple (const char *desc)
        MonoProfileFlags flags = 0;
 
        MONO_TIMER_STARTUP;
+       poutput = stdout;
 
        if (!desc)
                desc = "alloc,time,jit";
@@ -1291,20 +1611,32 @@ mono_profiler_install_simple (const char *desc)
                for (ptr = args; ptr && *ptr; ptr++) {
                        const char *arg = *ptr;
 
+                       // Alwais listen to appdomaon events to shutdown at the first unload
+                       flags |= MONO_PROFILE_APPDOMAIN_EVENTS;
                        if (!strcmp (arg, "time"))
-                               flags |= MONO_PROFILE_ENTER_LEAVE;
+                               flags |= MONO_PROFILE_ENTER_LEAVE | MONO_PROFILE_EXCEPTIONS;
                        else if (!strcmp (arg, "alloc"))
                                flags |= MONO_PROFILE_ALLOCATIONS;
                        else if (!strcmp (arg, "stat"))
-                               flags |= MONO_PROFILE_STATISTICAL | MONO_PROFILE_APPDOMAIN_EVENTS;
+                               flags |= MONO_PROFILE_STATISTICAL;
                        else if (!strcmp (arg, "jit"))
                                flags |= MONO_PROFILE_JIT_COMPILATION;
-                       else {
+                       else if (strncmp (arg, "file=", 5) == 0) {
+                               poutput = fopen (arg + 5, "wb");
+                               if (!poutput) {
+                                       poutput = stdout;
+                                       fprintf (stderr, "profiler : cannot open profile output file '%s'.\n", arg + 5);
+                               }
+                       } else {
                                fprintf (stderr, "profiler : Unknown argument '%s'.\n", arg);
                                return;
                        }
                }
        }
+       if (flags & MONO_PROFILE_ALLOCATIONS)
+               flags |= MONO_PROFILE_ENTER_LEAVE | MONO_PROFILE_EXCEPTIONS;
+       if (!flags)
+               flags = MONO_PROFILE_ENTER_LEAVE | MONO_PROFILE_ALLOCATIONS | MONO_PROFILE_JIT_COMPILATION | MONO_PROFILE_EXCEPTIONS;
 
        prof = create_profiler ();
        ALLOC_PROFILER ();
@@ -1316,9 +1648,10 @@ mono_profiler_install_simple (const char *desc)
 
        mono_profiler_install (prof, simple_shutdown);
        mono_profiler_install_enter_leave (simple_method_enter, simple_method_leave);
+       mono_profiler_install_exception (NULL, simple_method_leave, NULL);
        mono_profiler_install_jit_compile (simple_method_jit, simple_method_end_jit);
        mono_profiler_install_allocation (simple_allocation);
-       mono_profiler_install_appdomain (NULL, NULL, simple_appdomain_unload, NULL);
+       mono_profiler_install_appdomain (NULL, simple_appdomain_load, simple_appdomain_unload, NULL);
        mono_profiler_install_statistical (simple_stat_hit);
        mono_profiler_set_events (flags);
 }
@@ -1328,9 +1661,21 @@ mono_profiler_install_simple (const char *desc)
 typedef void (*ProfilerInitializer) (const char*);
 #define INITIALIZER_NAME "mono_profiler_startup"
 
+/**
+ * mono_profiler_load:
+ * @desc: arguments to configure the profiler
+ *
+ * Invoke this method to initialize the profiler.   This will drive the
+ * loading of the internal ("default") or any external profilers.
+ *
+ * This routine is invoked by Mono's driver, but must be called manually
+ * if you embed Mono into your application.
+ */
 void 
 mono_profiler_load (const char *desc)
 {
+       mono_gc_base_init ();
+
 #ifndef DISABLE_PROFILER
        if (!desc || (strcmp ("default", desc) == 0) || (strncmp (desc, "default:", 8) == 0)) {
                mono_profiler_install_simple (desc);
@@ -1342,31 +1687,42 @@ mono_profiler_load (const char *desc)
        }
 #endif
        {
-               GModule *pmodule;
+               MonoDl *pmodule = NULL;
                const char* col = strchr (desc, ':');
                char* libname;
                char* path;
                char *mname;
+               char *err;
+               void *iter;
                if (col != NULL) {
-                       mname = g_memdup (desc, col - desc);
+                       mname = g_memdup (desc, col - desc + 1);
                        mname [col - desc] = 0;
                } else {
                        mname = g_strdup (desc);
                }
                libname = g_strdup_printf ("mono-profiler-%s", mname);
-               path = g_module_build_path (NULL, libname);
-               pmodule = g_module_open (path, G_MODULE_BIND_LAZY);
-               if (pmodule) {
-                       ProfilerInitializer func;
-                       if (!g_module_symbol (pmodule, INITIALIZER_NAME, (gpointer *)&func)) {
-                               g_warning ("Cannot find initializer function %s in profiler module: %s", INITIALIZER_NAME, libname);
-                       } else {
-                               func (desc);
+               iter = NULL;
+               err = NULL;
+               while ((path = mono_dl_build_path (NULL, libname, &iter))) {
+                       g_free (err);
+                       pmodule = mono_dl_open (path, MONO_DL_LAZY, &err);
+                       if (pmodule) {
+                               ProfilerInitializer func;
+                               if ((err = mono_dl_symbol (pmodule, INITIALIZER_NAME, (gpointer *)&func))) {
+                                       g_warning ("Cannot find initializer function %s in profiler module: %s (%s)", INITIALIZER_NAME, libname, err);
+                                       g_free (err);
+                                       err = NULL;
+                               } else {
+                                       func (desc);
+                               }
+                               break;
                        }
-               } else {
-                       g_warning ("Error loading profiler module '%s': %s", libname, g_module_error ());
+                       g_free (path);
+               }
+               if (!pmodule) {
+                       g_warning ("Error loading profiler module '%s': %s", libname, err);
+                       g_free (err);
                }
-
                g_free (libname);
                g_free (mname);
                g_free (path);