2008-01-09 Rodrigo Kumpera <rkumpera@novell.com>
[mono.git] / mono / metadata / profiler.c
index 36ec3a382a99560c7b377530c13e6bf520655d70..0f33a27214a9502f747b4174003f7420416cc9d1 100644 (file)
 #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>
+#endif
 #ifdef HAVE_BACKTRACE_SYMBOLS
 #include <execinfo.h>
 #endif
@@ -51,12 +55,17 @@ static MonoProfileClassFunc   class_end_unload;
 static MonoProfileMethodFunc   jit_start;
 static MonoProfileMethodResult jit_end;
 static MonoProfileJitResult    jit_end2;
+static MonoProfileMethodFunc   method_free;
 static MonoProfileMethodResult man_unman_transition;
 static MonoProfileAllocFunc    allocation_cb;
 static MonoProfileStatFunc     statistical_cb;
 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;
 
@@ -163,6 +172,12 @@ 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_thread (MonoProfileThreadFunc start, MonoProfileThreadFunc end)
 {
@@ -188,6 +203,13 @@ mono_profiler_install_statistical (MonoProfileStatFunc callback)
        statistical_cb = callback;
 }
 
+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)
 {
@@ -267,6 +289,13 @@ mono_profiler_method_end_jit (MonoMethod *method, MonoJitInfo* jinfo, int 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_code_transition (MonoMethod *method, int result)
 {
@@ -288,6 +317,27 @@ mono_profiler_stat_hit (guchar *ip, void *context)
                statistical_cb (current_profiler, ip, 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)
 {
@@ -1341,12 +1391,13 @@ 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)
 {
@@ -1354,7 +1405,20 @@ simple_shutdown (MonoProfiler *prof)
        MonoProfiler *tprof;
        GSList *tmp;
        char *str;
+       gint32 see_shutdown_done;
+       
+       // 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 ();
+       }
+
+       // 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);
@@ -1404,12 +1468,14 @@ 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 if (strncmp (arg, "file=", 5) == 0) {
@@ -1425,9 +1491,9 @@ mono_profiler_install_simple (const char *desc)
                }
        }
        if (flags & MONO_PROFILE_ALLOCATIONS)
-               flags |= MONO_PROFILE_ENTER_LEAVE;
+               flags |= MONO_PROFILE_ENTER_LEAVE | MONO_PROFILE_EXCEPTIONS;
        if (!flags)
-               flags = MONO_PROFILE_ENTER_LEAVE | MONO_PROFILE_ALLOCATIONS | MONO_PROFILE_JIT_COMPILATION;
+               flags = MONO_PROFILE_ENTER_LEAVE | MONO_PROFILE_ALLOCATIONS | MONO_PROFILE_JIT_COMPILATION | MONO_PROFILE_EXCEPTIONS;
 
        prof = create_profiler ();
        ALLOC_PROFILER ();
@@ -1439,6 +1505,7 @@ 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);
@@ -1483,7 +1550,7 @@ mono_profiler_load (const char *desc)
                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);