2004-07-05 Zoltan Varga <vargaz@freemail.hu>
[mono.git] / mono / metadata / mono-debug.c
index a4614b639263ee31c1ca9205269c8ed44ae0bdd1..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
 {
@@ -18,7 +20,7 @@ struct _MonoDebugDomainDataPriv
        MonoDebugDomainData *next;
 };
 
-MonoDebugFormat mono_debug_format;
+MonoDebugFormat mono_debug_format = MONO_DEBUG_FORMAT_NONE;
 
 static gboolean in_the_mono_debugger = FALSE;
 static gboolean mono_debug_initialized = FALSE;
@@ -41,7 +43,7 @@ extern void (*mono_debugger_class_init_func) (MonoClass *klass);
  * callbacks here.
  */
 void
-mono_debug_init (MonoDebugFormat format)
+mono_debug_init (MonoDomain *domain, MonoDebugFormat format)
 {
        MonoAssembly **ass;
 
@@ -52,7 +54,7 @@ mono_debug_init (MonoDebugFormat format)
        in_the_mono_debugger = format == MONO_DEBUG_FORMAT_DEBUGGER;
 
        if (in_the_mono_debugger)
-               mono_debugger_initialize ();
+               mono_debugger_initialize (domain);
 
        mono_debugger_lock ();
 
@@ -62,9 +64,12 @@ mono_debug_init (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);
+       */
 }
 
 /*
@@ -75,53 +80,12 @@ mono_debug_init (MonoDebugFormat format)
 void
 mono_debug_init_2 (MonoAssembly *assembly)
 {
-       mono_debug_open_image (assembly->image);
-
-       mono_debug_add_type (mono_defaults.object_class);
-       mono_debug_add_type (mono_defaults.object_class);
-       mono_debug_add_type (mono_defaults.byte_class);
-       mono_debug_add_type (mono_defaults.void_class);
-       mono_debug_add_type (mono_defaults.boolean_class);
-       mono_debug_add_type (mono_defaults.sbyte_class);
-       mono_debug_add_type (mono_defaults.int16_class);
-       mono_debug_add_type (mono_defaults.uint16_class);
-       mono_debug_add_type (mono_defaults.int32_class);
-       mono_debug_add_type (mono_defaults.uint32_class);
-       mono_debug_add_type (mono_defaults.int_class);
-       mono_debug_add_type (mono_defaults.uint_class);
-       mono_debug_add_type (mono_defaults.int64_class);
-       mono_debug_add_type (mono_defaults.uint64_class);
-       mono_debug_add_type (mono_defaults.single_class);
-       mono_debug_add_type (mono_defaults.double_class);
-       mono_debug_add_type (mono_defaults.char_class);
-       mono_debug_add_type (mono_defaults.string_class);
-       mono_debug_add_type (mono_defaults.enum_class);
-       mono_debug_add_type (mono_defaults.array_class);
-       mono_debug_add_type (mono_defaults.multicastdelegate_class);
-       mono_debug_add_type (mono_defaults.asyncresult_class);
-       mono_debug_add_type (mono_defaults.waithandle_class);
-       mono_debug_add_type (mono_defaults.typehandle_class);
-       mono_debug_add_type (mono_defaults.fieldhandle_class);
-       mono_debug_add_type (mono_defaults.methodhandle_class);
-       mono_debug_add_type (mono_defaults.monotype_class);
-       mono_debug_add_type (mono_defaults.exception_class);
-       mono_debug_add_type (mono_defaults.threadabortexception_class);
-       mono_debug_add_type (mono_defaults.thread_class);
-       mono_debug_add_type (mono_defaults.transparent_proxy_class);
-       mono_debug_add_type (mono_defaults.real_proxy_class);
-       mono_debug_add_type (mono_defaults.mono_method_message_class);
-       mono_debug_add_type (mono_defaults.appdomain_class);
-       mono_debug_add_type (mono_defaults.field_info_class);
-       mono_debug_add_type (mono_defaults.stringbuilder_class);
-       mono_debug_add_type (mono_defaults.math_class);
-       mono_debug_add_type (mono_defaults.stack_frame_class);
-       mono_debug_add_type (mono_defaults.stack_trace_class);
-       mono_debug_add_type (mono_defaults.marshal_class);
-       mono_debug_add_type (mono_defaults.iserializeable_class);
-       mono_debug_add_type (mono_defaults.serializationinfo_class);
-       mono_debug_add_type (mono_defaults.streamingcontext_class);
+       MonoDebugHandle *handle;
 
-       mono_debugger_unlock ();
+       mono_debug_open_image (mono_assembly_get_image (assembly));
+
+       handle = _mono_debug_get_image (mono_get_corlib ());
+       g_assert (handle);
 }
 
 void
@@ -151,18 +115,21 @@ 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)
+       if (in_the_mono_debugger) {
                handle->_priv->debugger_info = mono_debugger_add_symbol_file (handle);
+               if (image == mono_get_corlib ())
+                       mono_debugger_add_builtin_types (handle->_priv->debugger_info);
+       }
 
        return handle;
 }
@@ -172,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);
 }
@@ -181,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 ();
 }
 
@@ -195,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);
@@ -217,13 +187,16 @@ 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 *
 _mono_debug_lookup_method (MonoMethod *method)
 {
-       struct LookupMethodData data = { NULL, method };
+       struct LookupMethodData data;
+
+       data.minfo = NULL;
+       data.method = method;
 
        if (!mono_debug_handles)
                return NULL;
@@ -239,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);
@@ -258,7 +233,12 @@ mono_debug_add_wrapper (MonoMethod *method, MonoMethod *wrapper_method, MonoDoma
                return;
 
        domain_data = mono_debug_get_domain_data (handle, domain);
-       g_assert (!domain_data->jit [minfo->index]);
+       if (domain_data->jit [minfo->index]) {
+               /* FIXME FIXME FIXME
+               // This is bug #48591.
+               */
+               return;
+       }
 
        jit = g_hash_table_lookup (domain_data->_priv->wrapper_info, wrapper_method);
        g_assert (jit);
@@ -268,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 ();
@@ -286,25 +266,38 @@ 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);
-       g_assert (!domain_data->jit [minfo->index]);
+       if (domain_data->jit [minfo->index]) {
+               /* FIXME FIXME FIXME
+               // This is bug #48591.
+               */
+               mono_debugger_unlock ();
+               return;
+       }
 
        if (method->wrapper_type != MONO_WRAPPER_NONE) {
                g_hash_table_insert (domain_data->_priv->wrapper_info, method, jit);
@@ -314,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 ();
@@ -444,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;