Merge pull request #2271 from BrzVlad/fix-sizeof-monoarray
[mono.git] / mono / metadata / mono-debug.c
index f75453571d043d92656e568f5feae6f6a6911bde..5bede758df2a753132664602ee32272750e80ada 100644 (file)
@@ -18,8 +18,9 @@
 #include <mono/metadata/mono-debug.h>
 #include <mono/metadata/mono-debug-debugger.h>
 #include <mono/metadata/mono-endian.h>
-#include <mono/metadata/gc-internal.h>
+#include <mono/metadata/gc-internals.h>
 #include <mono/metadata/mempool.h>
+#include <mono/metadata/debug-mono-ppdb.h>
 #include <string.h>
 
 #define ALIGN_TO(val,align) ((((guint64)val) + ((align) - 1)) & ~((align) - 1))
@@ -59,7 +60,6 @@ static GHashTable *data_table_hash;
 
 static mono_mutex_t debugger_lock_mutex;
 
-static int initialized = 0;
 static gboolean is_attached = FALSE;
 
 static MonoDebugHandle     *mono_debug_open_image      (MonoImage *image, const guint8 *raw_contents, int size);
@@ -111,6 +111,8 @@ lookup_data_table (MonoDomain *domain)
 static void
 free_debug_handle (MonoDebugHandle *handle)
 {
+       if (handle->ppdb)
+               mono_ppdb_close (handle);
        if (handle->symfile)
                mono_debug_close_mono_symbol_file (handle->symfile);
        /* decrease the refcount added with mono_image_addref () */
@@ -135,7 +137,7 @@ mono_debug_init (MonoDebugFormat format)
        mono_debug_initialized = TRUE;
        mono_debug_format = format;
 
-       mono_debugger_initialize ();
+       mono_mutex_init_recursive (&debugger_lock_mutex);
 
        mono_debugger_lock ();
 
@@ -175,14 +177,12 @@ mono_debug_cleanup (void)
 void
 mono_debug_domain_create (MonoDomain *domain)
 {
-       MonoDebugDataTable *table;
-
        if (!mono_debug_initialized)
                return;
 
        mono_debugger_lock ();
 
-       table = create_data_table (domain);
+       create_data_table (domain);
 
        mono_debugger_unlock ();
 }
@@ -261,8 +261,11 @@ mono_debug_open_image (MonoImage *image, const guint8 *raw_contents, int size)
        handle->image = image;
        mono_image_addref (image);
 
-       handle->symfile = mono_debug_open_mono_symbols (
-               handle, raw_contents, size, FALSE);
+       /* Try a ppdb file first */
+       handle->ppdb = mono_ppdb_load_file (handle->image, raw_contents, size);
+
+       if (!handle->ppdb)
+               handle->symfile = mono_debug_open_mono_symbols (handle, raw_contents, size, FALSE);
 
        g_hash_table_insert (mono_debug_handles, image, handle);
 
@@ -300,7 +303,9 @@ lookup_method_func (gpointer key, gpointer value, gpointer user_data)
        if (data->minfo)
                return;
 
-       if (handle->symfile)
+       if (handle->ppdb)
+               data->minfo = mono_ppdb_lookup_method (handle, data->method);
+       else if (handle->symfile)
                data->minfo = mono_debug_symfile_lookup_method (handle, data->method);
 }
 
@@ -427,8 +432,6 @@ mono_debug_add_method (MonoMethod *method, MonoDebugMethodJitInfo *jit, MonoDoma
 {
        MonoDebugDataTable *table;
        MonoDebugMethodAddress *address;
-       MonoDebugMethodInfo *minfo;
-       MonoDebugHandle *handle;
        guint8 buffer [BUFSIZ];
        guint8 *ptr, *oldptr;
        guint32 i, size, total_size, max_size;
@@ -437,9 +440,6 @@ mono_debug_add_method (MonoMethod *method, MonoDebugMethodJitInfo *jit, MonoDoma
 
        table = lookup_data_table (domain);
 
-       handle = mono_debug_get_image (method->klass->image);
-       minfo = mono_debug_lookup_method_internal (method);
-
        max_size = (5 * 5) + 1 + (10 * jit->num_line_numbers) +
                (25 + sizeof (gpointer)) * (1 + jit->num_params + jit->num_locals);
 
@@ -751,7 +751,12 @@ mono_debug_lookup_source_location (MonoMethod *method, guint32 address, MonoDoma
 
        mono_debugger_lock ();
        minfo = mono_debug_lookup_method_internal (method);
-       if (!minfo || !minfo->handle || !minfo->handle->symfile || !mono_debug_symfile_is_loaded (minfo->handle->symfile)) {
+       if (!minfo || !minfo->handle) {
+               mono_debugger_unlock ();
+               return NULL;
+       }
+
+       if (!minfo->handle->ppdb && (!minfo->handle->symfile || !mono_debug_symfile_is_loaded (minfo->handle->symfile))) {
                mono_debugger_unlock ();
                return NULL;
        }
@@ -762,7 +767,24 @@ mono_debug_lookup_source_location (MonoMethod *method, guint32 address, MonoDoma
                return NULL;
        }
 
-       location = mono_debug_symfile_lookup_location (minfo, offset);
+       if (minfo->handle->ppdb)
+               location = mono_ppdb_lookup_location (minfo, offset);
+       else
+               location = mono_debug_symfile_lookup_location (minfo, offset);
+       mono_debugger_unlock ();
+       return location;
+}
+
+MonoDebugSourceLocation *
+mono_debug_method_lookup_location (MonoDebugMethodInfo *minfo, int il_offset)
+{
+       MonoDebugSourceLocation *location;
+
+       mono_debugger_lock ();
+       if (minfo->handle->ppdb)
+               location = mono_ppdb_lookup_location (minfo, il_offset);
+       else
+               location = mono_debug_symfile_lookup_location (minfo, il_offset);
        mono_debugger_unlock ();
        return location;
 }
@@ -771,7 +793,7 @@ mono_debug_lookup_source_location (MonoMethod *method, guint32 address, MonoDoma
  * mono_debug_lookup_locals:
  *
  *   Return information about the local variables of MINFO.
- * The result should be freed using mono_debug_symfile_free_locals ().
+ * The result should be freed using mono_debug_free_locals ().
  */
 MonoDebugLocalsInfo*
 mono_debug_lookup_locals (MonoMethod *method)
@@ -784,17 +806,41 @@ mono_debug_lookup_locals (MonoMethod *method)
 
        mono_debugger_lock ();
        minfo = mono_debug_lookup_method_internal (method);
-       if (!minfo || !minfo->handle || !minfo->handle->symfile || !mono_debug_symfile_is_loaded (minfo->handle->symfile)) {
+       if (!minfo || !minfo->handle) {
                mono_debugger_unlock ();
                return NULL;
        }
 
-       res = mono_debug_symfile_lookup_locals (minfo);
+       if (minfo->handle->ppdb) {
+               res = mono_ppdb_lookup_locals (minfo);
+       } else {
+               if (!minfo->handle->symfile || !mono_debug_symfile_is_loaded (minfo->handle->symfile))
+                       res = NULL;
+               else
+                       res = mono_debug_symfile_lookup_locals (minfo);
+       }
        mono_debugger_unlock ();
 
        return res;
 }
 
+/*
+ * mono_debug_free_locals:
+ *
+ *   Free all the data allocated by mono_debug_lookup_locals ().
+ */
+void
+mono_debug_free_locals (MonoDebugLocalsInfo *info)
+{
+       int i;
+
+       for (i = 0; i < info->num_locals; ++i)
+               g_free (info->locals [i].name);
+       g_free (info->locals);
+       g_free (info->code_blocks);
+       g_free (info);
+}
+
 /**
  * mono_debug_free_source_location:
  * @location: A `MonoDebugSourceLocation'.
@@ -914,24 +960,17 @@ open_symfile_from_bundle (MonoImage *image)
 void
 mono_debugger_lock (void)
 {
-       g_assert (initialized);
+       g_assert (mono_debug_initialized);
        mono_mutex_lock (&debugger_lock_mutex);
 }
 
 void
 mono_debugger_unlock (void)
 {
-       g_assert (initialized);
+       g_assert (mono_debug_initialized);
        mono_mutex_unlock (&debugger_lock_mutex);
 }
 
-void
-mono_debugger_initialize ()
-{
-       mono_mutex_init_recursive (&debugger_lock_mutex);
-       initialized = 1;
-}
-
 /**
  * mono_debug_enabled:
  *
@@ -942,3 +981,12 @@ mono_debug_enabled (void)
 {
        return mono_debug_format != MONO_DEBUG_FORMAT_NONE;
 }
+
+void
+mono_debug_get_seq_points (MonoDebugMethodInfo *minfo, char **source_file, GPtrArray **source_file_list, int **source_files, MonoSymSeqPoint **seq_points, int *n_seq_points)
+{
+       if (minfo->handle->ppdb)
+               mono_ppdb_get_seq_points (minfo, source_file, source_file_list, source_files, seq_points, n_seq_points);
+       else
+               mono_debug_symfile_get_seq_points (minfo, source_file, source_file_list, source_files, seq_points, n_seq_points);
+}