2007-03-09 Jonathan Chambers <joncham@gmail.com>
[mono.git] / mono / metadata / profiler.c
index d9bda6f7292079a556dde092e97cb71635fdcb61..36ec3a382a99560c7b377530c13e6bf520655d70 100644 (file)
 #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>
+#include <unistd.h>
 #include <sys/time.h>
-#include <gmodule.h>
 #ifdef HAVE_BACKTRACE_SYMBOLS
 #include <execinfo.h>
 #endif
@@ -537,6 +538,7 @@ mono_profiler_coverage_get (MonoProfiler *prof, MonoMethod *method, MonoProfileC
        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;
@@ -550,13 +552,13 @@ mono_profiler_coverage_get (MonoProfiler *prof, MonoMethod *method, MonoProfileC
                                if (location) {
                                        entry.line = location->row;
                                        entry.col = location->column;
-                                       entry.filename = g_strdup (location->source_file);
+                                       entry.filename = fname = g_strdup (location->source_file);
                                        mono_debug_free_source_location (location);
                                }
                        }
 
                        func (prof, &entry);
-                       g_free (entry.filename);
+                       g_free (fname);
                }
        }
 }
@@ -811,6 +813,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)
 {
@@ -829,7 +839,7 @@ output_profile (GList *funcs)
                m = method_get_name (p->method);
                fprintf (poutput, "########################\n");
                fprintf (poutput, "% 8.3f ", (double) (p->total * 1000));
-               fprintf (poutput, "%7llu ", (unsigned long long)p->count);
+               fprintf (poutput, "%7" G_GUINT64_FORMAT " ", (unsigned long long)p->count);
                fprintf (poutput, "% 8.3f ", (double) (p->total * 1000)/(double)p->count);
                fprintf (poutput, "  %s\n", m);
 
@@ -837,7 +847,7 @@ output_profile (GList *funcs)
                /* callers */
                output_callers (p);
        }
-       fprintf (poutput, "Total number of calls: %lld\n", (long long)total_calls);
+       fprintf (poutput, "Total number of calls: %" G_GINT64_FORMAT "\n", (long long)total_calls);
 }
 
 typedef struct {
@@ -927,11 +937,6 @@ 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
-
 static void
 output_newobj_profile (GList *proflist)
 {
@@ -1228,6 +1233,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);
@@ -1408,6 +1424,10 @@ mono_profiler_install_simple (const char *desc)
                        }
                }
        }
+       if (flags & MONO_PROFILE_ALLOCATIONS)
+               flags |= MONO_PROFILE_ENTER_LEAVE;
+       if (!flags)
+               flags = MONO_PROFILE_ENTER_LEAVE | MONO_PROFILE_ALLOCATIONS | MONO_PROFILE_JIT_COMPILATION;
 
        prof = create_profiler ();
        ALLOC_PROFILER ();
@@ -1455,11 +1475,13 @@ mono_profiler_load (const char *desc)
        }
 #endif
        {
-               GModule *pmodule;
+               MonoDl *pmodule;
                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 [col - desc] = 0;
@@ -1467,19 +1489,28 @@ mono_profiler_load (const char *desc)
                        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);