2004-07-05 Zoltan Varga <vargaz@freemail.hu>
[mono.git] / mono / metadata / mono-debug.c
index 783059a5790eebe4c33b0f438daa57ca3e8a4551..b8503ecbc4be7be428935b57fb52d1f1e03bca25 100644 (file)
@@ -3,8 +3,10 @@
 #include <mono/metadata/tabledefs.h>
 #include <mono/metadata/tokentype.h>
 #include <mono/metadata/appdomain.h>
+#include <mono/metadata/class-internals.h>
 #include <mono/metadata/mono-debug.h>
 #include <mono/metadata/mono-debug-debugger.h>
+#include <mono/metadata/mono-endian.h>
 
 struct _MonoDebugHandlePriv
 {
@@ -62,9 +64,12 @@ mono_debug_init (MonoDomain *domain, MonoDebugFormat format)
        mono_debugger_class_init_func = mono_debug_add_type;
        mono_install_assembly_load_hook (mono_debug_add_assembly, NULL);
 
-       mono_debug_open_image (mono_defaults.corlib);
+       mono_debug_open_image (mono_get_corlib ());
+       /*
+        * FIXME: Ugh: what is this code supposed to do? corlib has no references.
        for (ass = mono_defaults.corlib->references; ass && *ass; ass++)
                mono_debug_open_image ((*ass)->image);
+       */
 }
 
 /*
@@ -77,12 +82,10 @@ mono_debug_init_2 (MonoAssembly *assembly)
 {
        MonoDebugHandle *handle;
 
-       mono_debug_open_image (assembly->image);
+       mono_debug_open_image (mono_assembly_get_image (assembly));
 
-       handle = _mono_debug_get_image (mono_defaults.corlib);
+       handle = _mono_debug_get_image (mono_get_corlib ());
        g_assert (handle);
-
-       mono_debugger_unlock ();
 }
 
 void
@@ -112,19 +115,19 @@ mono_debug_open_image (MonoImage *image)
 
        handle = g_new0 (MonoDebugHandle, 1);
        handle->image = image;
-       handle->image->ref_count++;
-       handle->image_file = g_strdup (image->name);
+       mono_image_addref (image);
+       handle->image_file = g_strdup (mono_image_get_filename (image));
        handle->_priv = g_new0 (MonoDebugHandlePriv, 1);
 
        g_hash_table_insert (mono_debug_handles, image, handle);
 
-       if (image->assembly->dynamic)
+       if (mono_image_is_dynamic (image))
                return handle;
 
        handle->symfile = mono_debug_open_mono_symbol_file (handle, in_the_mono_debugger);
        if (in_the_mono_debugger) {
                handle->_priv->debugger_info = mono_debugger_add_symbol_file (handle);
-               if (image == mono_defaults.corlib)
+               if (image == mono_get_corlib ())
                        mono_debugger_add_builtin_types (handle->_priv->debugger_info);
        }
 
@@ -136,7 +139,9 @@ mono_debug_close_image (MonoDebugHandle *handle)
 {
        if (handle->symfile)
                mono_debug_close_mono_symbol_file (handle->symfile);
-       handle->image->ref_count--;
+       /* decrease the refcount added with mono_image_addref () */
+       mono_image_close (handle->image);
+       /* FIXME: should also free handle->image_file? */
        g_free (handle->_priv);
        g_free (handle);
 }
@@ -145,7 +150,7 @@ static void
 mono_debug_add_assembly (MonoAssembly *assembly, gpointer user_data)
 {
        mono_debugger_lock ();
-       mono_debug_open_image (assembly->image);
+       mono_debug_open_image (mono_assembly_get_image (assembly));
        mono_debugger_unlock ();
 }
 
@@ -159,7 +164,8 @@ mono_debug_add_type (MonoClass *klass)
        MonoDebugHandle *handle;
 
        handle = _mono_debug_get_image (klass->image);
-       g_assert (handle);
+       if (!handle)
+               return;
 
        if (handle->_priv->debugger_info)
                mono_debugger_add_type (handle->_priv->debugger_info, klass);
@@ -181,7 +187,7 @@ lookup_method_func (gpointer key, gpointer value, gpointer user_data)
                return;
 
        if (handle->symfile)
-               data->minfo = mono_debug_find_method (handle->symfile, data->method);
+               data->minfo = mono_debug_find_method (handle, data->method);
 }
 
 static MonoDebugMethodInfo *
@@ -206,13 +212,15 @@ _mono_debug_lookup_method (MonoMethod *method)
 void
 mono_debug_add_wrapper (MonoMethod *method, MonoMethod *wrapper_method, MonoDomain *domain)
 {
-       MonoClass *klass = method->klass;
+       MonoClass *klass = mono_method_get_class (method);
        MonoDebugHandle *handle;
        MonoDebugMethodInfo *minfo;
        MonoDebugMethodJitInfo *jit;
        MonoDebugDomainData *domain_data;
+       guint32 iflags;
 
-       if (!(method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL))
+       mono_method_get_flags (method, &iflags);
+       if (!(iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL))
                return;
 
        mono_class_init (klass);
@@ -226,8 +234,9 @@ mono_debug_add_wrapper (MonoMethod *method, MonoMethod *wrapper_method, MonoDoma
 
        domain_data = mono_debug_get_domain_data (handle, domain);
        if (domain_data->jit [minfo->index]) {
-               // FIXME FIXME FIXME
+               /* FIXME FIXME FIXME
                // This is bug #48591.
+               */
                return;
        }
 
@@ -239,7 +248,7 @@ mono_debug_add_wrapper (MonoMethod *method, MonoMethod *wrapper_method, MonoDoma
        domain_data->jit [minfo->index] = jit;
        jit->wrapper_addr = method->addr;
 
-       if (handle->_priv->debugger_info && (domain == mono_root_domain))
+       if (handle->_priv->debugger_info && (domain == mono_get_root_domain ()))
                mono_debugger_add_method (handle->_priv->debugger_info, minfo, jit);
 
        mono_debugger_unlock ();
@@ -257,27 +266,36 @@ mono_debug_add_method (MonoMethod *method, MonoDebugMethodJitInfo *jit, MonoDoma
        MonoDebugHandle *handle;
        MonoDebugMethodInfo *minfo;
 
+       mono_debugger_lock ();
+
        mono_class_init (klass);
 
        if ((method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
            (method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) ||
            (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
-           (method->flags & METHOD_ATTRIBUTE_ABSTRACT))
+           (method->flags & METHOD_ATTRIBUTE_ABSTRACT)) {
+               mono_debugger_unlock ();
                return;
+       }
 
        handle = _mono_debug_get_image (klass->image);
-       g_assert (handle);
+       if (!handle) {
+               mono_debugger_unlock ();
+               return;
+       }
 
        minfo = _mono_debug_lookup_method (method);
-       if (!minfo)
+       if (!minfo) {
+               mono_debugger_unlock ();
                return;
-
-       mono_debugger_lock ();
+       }
 
        domain_data = mono_debug_get_domain_data (handle, domain);
        if (domain_data->jit [minfo->index]) {
-               // FIXME FIXME FIXME
+               /* FIXME FIXME FIXME
                // This is bug #48591.
+               */
+               mono_debugger_unlock ();
                return;
        }
 
@@ -289,7 +307,7 @@ mono_debug_add_method (MonoMethod *method, MonoDebugMethodJitInfo *jit, MonoDoma
 
        domain_data->jit [minfo->index] = jit;
 
-       if (handle->_priv->debugger_info && (domain == mono_root_domain))
+       if (handle->_priv->debugger_info && (domain == mono_get_root_domain ()))
                mono_debugger_add_method (handle->_priv->debugger_info, minfo, jit);
 
        mono_debugger_unlock ();
@@ -419,14 +437,15 @@ MonoDebugDomainData *
 mono_debug_get_domain_data (MonoDebugHandle *handle, MonoDomain *domain)
 {
        MonoDebugDomainData *data;
+       int domain_id = mono_domain_get_id (domain);
 
        for (data = handle->_priv->domain_table; data; data = data->_priv->next)
-               if (data->domain_id == domain->domain_id)
+               if (data->domain_id == domain_id)
                        return data;
 
        data = g_new0 (MonoDebugDomainData, 1);
-       data->domain_id = domain->domain_id;
-       data->jit = g_new0 (MonoDebugMethodJitInfo *, handle->symfile->offset_table->method_count + 1);
+       data->domain_id = domain_id;
+       data->jit = g_new0 (MonoDebugMethodJitInfo *, read32(&(handle->symfile->offset_table->_method_count)) + 1);
 
        data->_priv = g_new0 (MonoDebugDomainDataPriv, 1);
        data->_priv->next = handle->_priv->domain_table;