New test.
[mono.git] / mono / metadata / mono-debug.c
index da156b1f95b71b84e2514c3e3f2812a5868caa2f..0c66d7e9729134459be8b11d8c8425ab1197e0f4 100644 (file)
@@ -1,3 +1,13 @@
+/*
+ * mono-debug.c: 
+ *
+ * Author:
+ *     Mono Project (http://www.mono-project.com)
+ *
+ * Copyright 2001-2003 Ximian, Inc (http://www.ximian.com)
+ * Copyright 2004-2009 Novell, Inc (http://www.novell.com)
+ */
+
 #include <config.h>
 #include <mono/metadata/assembly.h>
 #include <mono/metadata/tabledefs.h>
 #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 <string.h>
 
-#define SYMFILE_TABLE_CHUNK_SIZE       16
-
-#define DATA_TABLE_PTR_CHUNK_SIZE      256
-#define DATA_TABLE_CHUNK_SIZE          4096
+#define DATA_TABLE_CHUNK_SIZE          16384
 
 #define ALIGN_TO(val,align) ((((guint64)val) + ((align) - 1)) & ~((align) - 1))
 
        }
 #define WRITE_UNALIGNED(type, addr, val) \
        memcpy(addr, &val, sizeof(type))
+#define READ_UNALIGNED(type, addr, val) \
+       memcpy(&val, addr, sizeof(type))
 #else
 #define RETURN_UNALIGNED(type, addr) \
        return *(type*)(p + offset);
 #define WRITE_UNALIGNED(type, addr, val) \
        (*(type *)(addr) = (val))
+#define READ_UNALIGNED(type, addr, val) \
+       val = (*(type *)(addr))
 #endif
 
 typedef enum {
        MONO_DEBUG_DATA_ITEM_UNKNOWN            = 0,
        MONO_DEBUG_DATA_ITEM_CLASS,
-       MONO_DEBUG_DATA_ITEM_METHOD
+       MONO_DEBUG_DATA_ITEM_METHOD,
+       MONO_DEBUG_DATA_ITEM_DELEGATE_TRAMPOLINE
 } MonoDebugDataItemType;
 
-struct _MonoDebugDataTable {
+typedef struct _MonoDebugDataChunk MonoDebugDataChunk;
+
+struct _MonoDebugDataChunk {
        guint32 total_size;
        guint32 allocated_size;
        guint32 current_offset;
        guint32 dummy;
-       MonoDebugDataTable *next;
+       MonoDebugDataChunk *next;
        guint8 data [MONO_ZERO_LEN_ARRAY];
 };
 
+struct _MonoDebugDataTable {
+       gint32 domain;
+       gint32 _dummy; /* alignment for next field. */
+       MonoDebugDataChunk *first_chunk;
+       MonoDebugDataChunk *current_chunk;
+       GHashTable *method_hash;
+       GHashTable *method_address_hash;
+};
+
 typedef struct {
        const gchar *method_name;
-       const gchar *cil_code;
+       const gchar *obsolete_cil_code;
        guint32 wrapper_type;
 } MonoDebugWrapperData;
 
@@ -71,56 +95,123 @@ struct _MonoDebugMethodAddress {
        guint8 data [MONO_ZERO_LEN_ARRAY];
 };
 
+struct _MonoDebugClassEntry {
+       guint32 size;
+       guint8 data [MONO_ZERO_LEN_ARRAY];
+};
+
 typedef struct {
-       MonoMethod *method;
-       guint32 domain_id;
-} MonoDebugMethodHash;
+       gpointer code;
+       guint32 size;
+} MonoDebugDelegateTrampolineEntry;
 
 MonoSymbolTable *mono_symbol_table = NULL;
-MonoDebugDataTable *mono_debug_data_table = NULL;
 MonoDebugFormat mono_debug_format = MONO_DEBUG_FORMAT_NONE;
-gint32 mono_debug_debugger_version = 2;
+gint32 mono_debug_debugger_version = 5;
+gint32 _mono_debug_using_mono_debugger = 0;
 
-static gboolean in_the_mono_debugger = FALSE;
 static gboolean mono_debug_initialized = FALSE;
-GHashTable *mono_debug_handles = NULL;
-
-static MonoDebugDataTable *current_data_table = NULL;
+static GHashTable *mono_debug_handles = NULL;
 
-static GHashTable *method_address_hash = NULL;
-static GHashTable *method_hash = NULL;
+static GHashTable *data_table_hash = NULL;
+static int next_symbol_file_id = 0;
 
 static MonoDebugHandle     *mono_debug_open_image      (MonoImage *image, const guint8 *raw_contents, int size);
 
-static void                 mono_debug_close_image     (MonoDebugHandle *debug);
-
 static MonoDebugHandle     *_mono_debug_get_image      (MonoImage *image);
 static void                 mono_debug_add_assembly    (MonoAssembly *assembly,
                                                        gpointer user_data);
-static void                 mono_debug_start_add_type  (MonoClass *klass);
 static void                 mono_debug_add_type        (MonoClass *klass);
 
+static MonoDebugHandle     *open_symfile_from_bundle   (MonoImage *image);
+
+void _mono_debug_init_corlib (MonoDomain *domain);
+
 extern void (*mono_debugger_class_init_func) (MonoClass *klass);
-extern void (*mono_debugger_start_class_init_func) (MonoClass *klass);
+extern void (*mono_debugger_class_loaded_methods_func) (MonoClass *klass);
 
-static guint
-method_hash_hash (gconstpointer data)
+static MonoDebugDataTable *
+create_data_table (MonoDomain *domain)
 {
-       const MonoDebugMethodHash *hash = (const MonoDebugMethodHash *) data;
-       return hash->method->token | (hash->domain_id << 16);
+       MonoDebugDataTable *table;
+       MonoDebugDataChunk *chunk;
+
+       table = g_new0 (MonoDebugDataTable, 1);
+       table->domain = domain ? mono_domain_get_id (domain) : -1;
+
+       table->method_address_hash = g_hash_table_new (NULL, NULL);
+       table->method_hash = g_hash_table_new (NULL, NULL);
+
+       chunk = g_malloc0 (sizeof (MonoDebugDataChunk) + DATA_TABLE_CHUNK_SIZE);
+       chunk->total_size = DATA_TABLE_CHUNK_SIZE;
+
+       table->first_chunk = table->current_chunk = chunk;
+
+       if (domain) {
+               mono_debug_list_add (&mono_symbol_table->data_tables, table);
+               g_hash_table_insert (data_table_hash, domain, table);
+       }
+
+       return table;
 }
 
-static gint
-method_hash_equal (gconstpointer ka, gconstpointer kb)
+static void
+free_header_data (gpointer key, gpointer value, gpointer user_data)
 {
-       const MonoDebugMethodHash *a = (const MonoDebugMethodHash *) ka;
-       const MonoDebugMethodHash *b = (const MonoDebugMethodHash *) kb;
+       MonoDebugMethodHeader *header = (MonoDebugMethodHeader*)value;
 
-       if ((a->method != b->method) || (a->domain_id != b->domain_id))
-               return 0;
-       return 1;
+       if (header->wrapper_data) {
+               g_free ((gpointer)header->wrapper_data->method_name);
+               g_slist_free (header->address_list);
+               g_free (header->wrapper_data);
+       }
 }
 
+static void
+free_data_table (MonoDebugDataTable *table)
+{
+       MonoDebugDataChunk *chunk, *next_chunk;
+
+       g_hash_table_foreach (table->method_hash, free_header_data, NULL);
+       g_hash_table_destroy (table->method_hash);
+       g_hash_table_destroy (table->method_address_hash);
+
+       table->method_hash = NULL;
+       table->method_address_hash = NULL;
+
+       chunk = table->first_chunk;
+       while (chunk) {
+               next_chunk = chunk->next;
+               g_free (chunk);
+               chunk = next_chunk;
+       }
+
+       table->first_chunk = table->current_chunk = NULL;
+       mono_debug_list_remove (&mono_symbol_table->data_tables, table);
+       g_free (table);
+}
+
+static MonoDebugDataTable *
+lookup_data_table (MonoDomain *domain)
+{
+       MonoDebugDataTable *table;
+
+       table = g_hash_table_lookup (data_table_hash, domain);
+       g_assert (table);
+       return table;
+}
+
+static void
+free_debug_handle (MonoDebugHandle *handle)
+{
+       if (handle->symfile)
+               mono_debug_close_mono_symbol_file (handle->symfile);
+       /* decrease the refcount added with mono_image_addref () */
+       free_data_table (handle->type_table);
+       mono_image_close (handle->image);
+       g_free (handle->image_file);
+       g_free (handle);
+}
 
 /*
  * Initialize debugging support.
@@ -134,63 +225,58 @@ mono_debug_init (MonoDebugFormat format)
 {
        g_assert (!mono_debug_initialized);
 
+       if (_mono_debug_using_mono_debugger)
+               format = MONO_DEBUG_FORMAT_DEBUGGER;
+
        mono_debug_initialized = TRUE;
        mono_debug_format = format;
-       in_the_mono_debugger = format == MONO_DEBUG_FORMAT_DEBUGGER;
 
-       mono_debugger_initialize (in_the_mono_debugger);
+       /*
+        * This must be called before mono_debugger_initialize(), because the
+        * latter registers GC roots.
+        */
+       mono_gc_base_init ();
+
+       mono_debugger_initialize (_mono_debug_using_mono_debugger);
 
        mono_debugger_lock ();
 
        mono_symbol_table = g_new0 (MonoSymbolTable, 1);
        mono_symbol_table->magic = MONO_DEBUGGER_MAGIC;
-       mono_symbol_table->version = MONO_DEBUGGER_VERSION;
+       mono_symbol_table->version = MONO_DEBUGGER_MAJOR_VERSION;
        mono_symbol_table->total_size = sizeof (MonoSymbolTable);
 
-       mono_debug_data_table = g_malloc0 (sizeof (MonoDebugDataTable) + DATA_TABLE_CHUNK_SIZE);
-       mono_debug_data_table->total_size = DATA_TABLE_CHUNK_SIZE;
-
-       current_data_table = mono_debug_data_table;
-
        mono_debug_handles = g_hash_table_new_full
-               (NULL, NULL, NULL, (GDestroyNotify) mono_debug_close_image);
-       method_address_hash = g_hash_table_new (method_hash_hash, method_hash_equal);
-       method_hash = g_hash_table_new (NULL, NULL);
+               (NULL, NULL, NULL, (GDestroyNotify) free_debug_handle);
+
+       data_table_hash = g_hash_table_new_full (
+               NULL, NULL, NULL, (GDestroyNotify) free_data_table);
 
-       mono_debugger_start_class_init_func = mono_debug_start_add_type;
        mono_debugger_class_init_func = mono_debug_add_type;
+       mono_debugger_class_loaded_methods_func = mono_debugger_class_initialized;
        mono_install_assembly_load_hook (mono_debug_add_assembly, NULL);
 
-       if (!in_the_mono_debugger)
-               mono_debugger_unlock ();
-}
+       mono_symbol_table->global_data_table = create_data_table (NULL);
 
-void
-mono_debug_init_1 (MonoDomain *domain)
-{
-       MonoDebugHandle *handle = mono_debug_open_image (mono_get_corlib (), NULL, 0);
-
-       mono_symbol_table->corlib = handle;
+       mono_debugger_unlock ();
 }
 
 /*
- * Initialize debugging support - part 2.
- *
- * This method must be called after loading the application's main assembly.
+ * INTERNAL USE ONLY !
  */
 void
-mono_debug_init_2 (MonoAssembly *assembly)
+_mono_debug_init_corlib (MonoDomain *domain)
 {
-       mono_debug_open_image (mono_assembly_get_image (assembly), NULL, 0);
+       if (!mono_debug_initialized)
+               return;
+
+       mono_symbol_table->corlib = mono_debug_open_image (mono_defaults.corlib, NULL, 0);
+       mono_debugger_event (MONO_DEBUGGER_EVENT_INITIALIZE_CORLIB,
+                            (guint64) (gsize) mono_symbol_table->corlib, 0);
 }
 
-/*
- * Initialize debugging support - part 2.
- *
- * This method must be called between loading the image and loading the assembly.
- */
 void
-mono_debug_init_2_memory (MonoImage *image, const guint8 *raw_contents, int size)
+mono_debug_open_image_from_memory (MonoImage *image, const guint8 *raw_contents, int size)
 {
        mono_debug_open_image (image, raw_contents, size);
 }
@@ -199,54 +285,106 @@ mono_debug_init_2_memory (MonoImage *image, const guint8 *raw_contents, int size
 gboolean
 mono_debug_using_mono_debugger (void)
 {
-       return in_the_mono_debugger;
+       return _mono_debug_using_mono_debugger;
 }
 
 void
 mono_debug_cleanup (void)
 {
-       MonoDebugDataTable *table, *next_table;
-
-       mono_debugger_cleanup ();
-
        if (mono_debug_handles)
                g_hash_table_destroy (mono_debug_handles);
        mono_debug_handles = NULL;
 
-       table = mono_debug_data_table;
-       while (table) {
-               next_table = table->next;
-               g_free (table);
-               table = next_table;
+       if (data_table_hash) {
+               g_hash_table_destroy (data_table_hash);
+               data_table_hash = NULL;
+       }
+
+       if (mono_symbol_table) {
+               if (mono_symbol_table->global_data_table)
+                       free_data_table (mono_symbol_table->global_data_table);
+
+               g_free (mono_symbol_table);
+               mono_symbol_table = NULL;
+       }
+}
+
+void
+mono_debug_domain_create (MonoDomain *domain)
+{
+       MonoDebugDataTable *table;
+
+       if (!mono_debug_initialized)
+               return;
+
+       mono_debugger_lock ();
+
+       table = create_data_table (domain);
+
+       mono_debugger_event (MONO_DEBUGGER_EVENT_DOMAIN_CREATE, (guint64) (gsize) table,
+                            mono_domain_get_id (domain));
+
+       mono_debugger_unlock ();
+}
+
+void
+mono_debug_domain_unload (MonoDomain *domain)
+{
+       MonoDebugDataTable *table;
+
+       if (!mono_debug_initialized)
+               return;
+
+       mono_debugger_lock ();
+
+       table = g_hash_table_lookup (data_table_hash, domain);
+       if (!table) {
+               g_warning (G_STRLOC ": unloading unknown domain %p / %d",
+                          domain, mono_domain_get_id (domain));
+               mono_debugger_unlock ();
+               return;
        }
 
-       mono_debug_data_table = current_data_table = NULL;
+       mono_debugger_event (MONO_DEBUGGER_EVENT_DOMAIN_UNLOAD, (guint64) (gsize) table,
+                            mono_domain_get_id (domain));
+
+       g_hash_table_remove (data_table_hash, domain);
+
+       mono_debugger_unlock ();
 }
 
+/*
+ * LOCKING: Assumes the debug lock is held.
+ */
 static MonoDebugHandle *
 _mono_debug_get_image (MonoImage *image)
 {
        return g_hash_table_lookup (mono_debug_handles, image);
 }
 
-static MonoDebugHandle *
-allocate_debug_handle (MonoSymbolTable *table)
+void
+mono_debug_close_image (MonoImage *image)
 {
        MonoDebugHandle *handle;
 
-       if (!table->symbol_files)
-               table->symbol_files = g_new0 (MonoDebugHandle *, SYMFILE_TABLE_CHUNK_SIZE);
-       else if (!((table->num_symbol_files + 1) % SYMFILE_TABLE_CHUNK_SIZE)) {
-               guint32 chunks = (table->num_symbol_files + 1) / SYMFILE_TABLE_CHUNK_SIZE;
-               guint32 size = sizeof (MonoDebugHandle *) * SYMFILE_TABLE_CHUNK_SIZE * (chunks + 1);
+       if (!mono_debug_initialized)
+               return;
+
+       mono_debugger_lock ();
 
-               table->symbol_files = g_realloc (table->symbol_files, size);
+       handle = _mono_debug_get_image (image);
+       if (!handle) {
+               mono_debugger_unlock ();
+               return;
        }
 
-       handle = g_new0 (MonoDebugHandle, 1);
-       handle->index = table->num_symbol_files;
-       table->symbol_files [table->num_symbol_files++] = handle;
-       return handle;
+       mono_debugger_event (MONO_DEBUGGER_EVENT_UNLOAD_MODULE, (guint64) (gsize) handle,
+                            handle->index);
+
+       mono_debug_list_remove (&mono_symbol_table->symbol_files, handle);
+       g_hash_table_remove (mono_debug_handles, image);
+
+       mono_debugger_unlock ();
 }
 
 static MonoDebugHandle *
@@ -257,47 +395,55 @@ mono_debug_open_image (MonoImage *image, const guint8 *raw_contents, int size)
        if (mono_image_is_dynamic (image))
                return NULL;
 
+       mono_debugger_lock ();
+
        handle = _mono_debug_get_image (image);
-       if (handle != NULL)
+       if (handle != NULL) {
+               mono_debugger_unlock ();
                return handle;
+       }
 
-       handle = allocate_debug_handle (mono_symbol_table);
+       handle = g_new0 (MonoDebugHandle, 1);
+       handle->index = ++next_symbol_file_id;
 
        handle->image = image;
        mono_image_addref (image);
        handle->image_file = g_strdup (mono_image_get_filename (image));
 
+       handle->type_table = create_data_table (NULL);
+
+       handle->symfile = mono_debug_open_mono_symbols (
+               handle, raw_contents, size, _mono_debug_using_mono_debugger);
+
+       mono_debug_list_add (&mono_symbol_table->symbol_files, handle);
+
        g_hash_table_insert (mono_debug_handles, image, handle);
 
-       handle->symfile = mono_debug_open_mono_symbols (handle, raw_contents, size, in_the_mono_debugger);
-       if (in_the_mono_debugger)
-               mono_debugger_add_symbol_file (handle);
+       if (mono_symbol_table->corlib)
+               mono_debugger_event (MONO_DEBUGGER_EVENT_LOAD_MODULE,
+                                    (guint64) (gsize) handle, 0);
 
-       return handle;
-}
+       mono_debugger_unlock ();
 
-static void
-mono_debug_close_image (MonoDebugHandle *handle)
-{
-       if (handle->symfile)
-               mono_debug_close_mono_symbol_file (handle->symfile);
-       /* decrease the refcount added with mono_image_addref () */
-       mono_image_close (handle->image);
-       g_free (handle->image_file);
-       g_free (handle->_priv);
-       g_free (handle);
+       return handle;
 }
 
 static void
 mono_debug_add_assembly (MonoAssembly *assembly, gpointer user_data)
 {
+       MonoDebugHandle *handle;
+       MonoImage *image;
+
        mono_debugger_lock ();
-       mono_debug_open_image (mono_assembly_get_image (assembly), NULL, 0);
+       image = mono_assembly_get_image (assembly);
+       handle = open_symfile_from_bundle (image);
+       if (!handle)
+               mono_debug_open_image (image, NULL, 0);
        mono_debugger_unlock ();
 }
 
 static guint8 *
-allocate_data_item (MonoDebugDataItemType type, guint32 size)
+allocate_data_item (MonoDebugDataTable *table, MonoDebugDataItemType type, guint32 size)
 {
        guint32 chunk_size;
        guint8 *data;
@@ -309,21 +455,20 @@ allocate_data_item (MonoDebugDataItemType type, guint32 size)
        else
                chunk_size = size + 16;
 
-       g_assert (current_data_table);
-       g_assert (current_data_table->current_offset == current_data_table->allocated_size);
+       g_assert (table->current_chunk->current_offset == table->current_chunk->allocated_size);
 
-       if (current_data_table->allocated_size + size + 8 >= current_data_table->total_size) {
-               MonoDebugDataTable *new_table;
+       if (table->current_chunk->allocated_size + size + 8 >= table->current_chunk->total_size) {
+               MonoDebugDataChunk *new_chunk;
 
-               new_table = g_malloc0 (sizeof (MonoDebugDataTable) + chunk_size);
-               new_table->total_size = chunk_size;
+               new_chunk = g_malloc0 (sizeof (MonoDebugDataChunk) + chunk_size);
+               new_chunk->total_size = chunk_size;
 
-               current_data_table->next = new_table;
-               current_data_table = new_table;
+               table->current_chunk->next = new_chunk;
+               table->current_chunk = new_chunk;
        }
 
-       data = &current_data_table->data [current_data_table->allocated_size];
-       current_data_table->allocated_size += size + 8;
+       data = &table->current_chunk->data [table->current_chunk->allocated_size];
+       table->current_chunk->allocated_size += size + 8;
 
        * ((guint32 *) data) = size;
        data += 4;
@@ -333,12 +478,13 @@ allocate_data_item (MonoDebugDataItemType type, guint32 size)
 }
 
 static void
-write_data_item (const guint8 *data)
+write_data_item (MonoDebugDataTable *table, const guint8 *data)
 {
+       MonoDebugDataChunk *current_chunk = table->current_chunk;
        guint32 size = * ((guint32 *) (data - 8));
 
-       g_assert (current_data_table->current_offset + size + 8 == current_data_table->allocated_size);
-       current_data_table->current_offset = current_data_table->allocated_size;
+       g_assert (current_chunk->current_offset + size + 8 == current_chunk->allocated_size);
+       current_chunk->current_offset = current_chunk->allocated_size;
 }
 
 struct LookupMethodData
@@ -434,6 +580,8 @@ write_variable (MonoDebugVarInfo *var, guint8 *ptr, guint8 **rptr)
        write_leb128 (var->size, ptr, &ptr);
        write_leb128 (var->begin_scope, ptr, &ptr);
        write_leb128 (var->end_scope, ptr, &ptr);
+       WRITE_UNALIGNED (gpointer, ptr, var->type);
+       ptr += sizeof (gpointer);
        *rptr = ptr;
 }
 
@@ -441,7 +589,7 @@ MonoDebugMethodAddress *
 mono_debug_add_method (MonoMethod *method, MonoDebugMethodJitInfo *jit, MonoDomain *domain)
 {
        MonoMethod *declaring;
-       MonoDebugMethodHash *hash;
+       MonoDebugDataTable *table;
        MonoDebugMethodHeader *header;
        MonoDebugMethodAddress *address;
        MonoDebugMethodInfo *minfo;
@@ -449,11 +597,12 @@ mono_debug_add_method (MonoMethod *method, MonoDebugMethodJitInfo *jit, MonoDoma
        guint8 buffer [BUFSIZ];
        guint8 *ptr, *oldptr;
        guint32 i, size, total_size, max_size;
-       gint32 last_il_offset = 0, last_native_offset = 0;
        gboolean is_wrapper = FALSE;
 
        mono_debugger_lock ();
 
+       table = lookup_data_table (domain);
+
        handle = _mono_debug_get_image (method->klass->image);
        minfo = _mono_debug_lookup_method (method);
 
@@ -465,10 +614,8 @@ mono_debug_add_method (MonoMethod *method, MonoDebugMethodJitInfo *jit, MonoDoma
                is_wrapper = TRUE;
        }
 
-       jit->num_lexical_blocks = minfo ? minfo->num_lexical_blocks : 0;
-
-       max_size = 24 + 8 * jit->num_line_numbers + 16 * jit->num_lexical_blocks +
-               20 * (1 + jit->num_params + jit->num_locals);
+       max_size = (5 * 5) + 1 + (10 * jit->num_line_numbers) +
+               (25 + sizeof (gpointer)) * (1 + jit->num_params + jit->num_locals);
 
        if (max_size > BUFSIZ)
                ptr = oldptr = g_malloc (max_size);
@@ -482,41 +629,8 @@ mono_debug_add_method (MonoMethod *method, MonoDebugMethodJitInfo *jit, MonoDoma
        for (i = 0; i < jit->num_line_numbers; i++) {
                MonoDebugLineNumberEntry *lne = &jit->line_numbers [i];
 
-               write_sleb128 (lne->il_offset - last_il_offset, ptr, &ptr);
-               write_sleb128 (lne->native_offset - last_native_offset, ptr, &ptr);
-
-               last_il_offset = lne->il_offset;
-               last_native_offset = lne->native_offset;
-       }
-
-       jit->lexical_blocks = g_new0 (MonoDebugLexicalBlockEntry, jit->num_lexical_blocks);
-       for (i = 0; i < jit->num_lexical_blocks; i ++) {
-               MonoDebugLexicalBlockEntry *jit_lbe = &jit->lexical_blocks [i];
-               MonoSymbolFileLexicalBlockEntry *minfo_lbe = &minfo->lexical_blocks [i];
-               jit_lbe->il_start_offset = read32 (&(minfo_lbe->_start_offset));
-               jit_lbe->native_start_offset = _mono_debug_address_from_il_offset (jit, jit_lbe->il_start_offset);
-
-               jit_lbe->il_end_offset = read32 (&(minfo_lbe->_end_offset));
-               jit_lbe->native_end_offset = _mono_debug_address_from_il_offset (jit, jit_lbe->il_end_offset);
-       }
-
-       last_il_offset = 0;
-       last_native_offset = 0;
-       write_leb128 (jit->num_lexical_blocks, ptr, &ptr);
-       for (i = 0; i < jit->num_lexical_blocks; i++) {
-               MonoDebugLexicalBlockEntry *lbe = &jit->lexical_blocks [i];
-
-               write_sleb128 (lbe->il_start_offset - last_il_offset, ptr, &ptr);
-               write_sleb128 (lbe->native_start_offset - last_native_offset, ptr, &ptr);
-
-               last_il_offset = lbe->il_start_offset;
-               last_native_offset = lbe->native_start_offset;
-
-               write_sleb128 (lbe->il_end_offset - last_il_offset, ptr, &ptr);
-               write_sleb128 (lbe->native_end_offset - last_native_offset, ptr, &ptr);
-
-               last_il_offset = lbe->il_end_offset;
-               last_native_offset = lbe->native_end_offset;
+               write_sleb128 (lne->il_offset, ptr, &ptr);
+               write_sleb128 (lne->native_offset, ptr, &ptr);
        }
 
        *ptr++ = jit->this_var ? 1 : 0;
@@ -535,7 +649,8 @@ mono_debug_add_method (MonoMethod *method, MonoDebugMethodJitInfo *jit, MonoDoma
        g_assert (size < max_size);
        total_size = size + sizeof (MonoDebugMethodAddress);
 
-       address = (MonoDebugMethodAddress *) allocate_data_item (MONO_DEBUG_DATA_ITEM_METHOD, total_size);
+       address = (MonoDebugMethodAddress *) allocate_data_item (
+               table, MONO_DEBUG_DATA_ITEM_METHOD, total_size);
 
        address->header.size = total_size;
        address->header.symfile_id = handle ? handle->index : 0;
@@ -551,45 +666,55 @@ mono_debug_add_method (MonoMethod *method, MonoDebugMethodJitInfo *jit, MonoDoma
                g_free (oldptr);
 
        declaring = method->is_inflated ? ((MonoMethodInflated *) method)->declaring : method;
-       header = g_hash_table_lookup (method_hash, declaring);
+       header = g_hash_table_lookup (table->method_hash, declaring);
 
        if (!header) {
                header = &address->header;
-               g_hash_table_insert (method_hash, declaring, header);
+               g_hash_table_insert (table->method_hash, declaring, header);
 
                if (is_wrapper) {
-                       const unsigned char* il_code;
-                       MonoMethodHeader *mheader;
                        MonoDebugWrapperData *wrapper;
-                       guint32 il_codesize;
-
-                       mheader = mono_method_get_header (declaring);
-                       il_code = mono_method_header_get_code (mheader, &il_codesize, NULL);
 
                        header->wrapper_data = wrapper = g_new0 (MonoDebugWrapperData, 1);
 
                        wrapper->wrapper_type = method->wrapper_type;
                        wrapper->method_name = mono_method_full_name (declaring, TRUE);
-                       wrapper->cil_code = mono_disasm_code (
-                               NULL, declaring, il_code, il_code + il_codesize);
+                       wrapper->obsolete_cil_code = "";
                }
        } else {
                address->header.wrapper_data = header->wrapper_data;
                header->address_list = g_slist_prepend (header->address_list, address);
        }
 
-       hash = g_new0 (MonoDebugMethodHash, 1);
-       hash->method = method;
-       hash->domain_id = mono_domain_get_id (domain);
-
-       g_hash_table_insert (method_address_hash, hash, address);
+       g_hash_table_insert (table->method_address_hash, method, address);
 
-       write_data_item ((guint8 *) address);
+       write_data_item (table, (guint8 *) address);
 
        mono_debugger_unlock ();
        return address;
 }
 
+void
+mono_debug_add_delegate_trampoline (gpointer code, int size)
+{
+       MonoDebugDelegateTrampolineEntry *entry;
+
+       if (!mono_debug_initialized)
+               return;
+
+       mono_debugger_lock ();
+
+       entry = (MonoDebugDelegateTrampolineEntry *) allocate_data_item (
+               mono_symbol_table->global_data_table, MONO_DEBUG_DATA_ITEM_DELEGATE_TRAMPOLINE,
+               sizeof (MonoDebugDelegateTrampolineEntry));
+       entry->code = code;
+       entry->size = size;
+
+       write_data_item (mono_symbol_table->global_data_table, (guint8 *) entry);
+
+       mono_debugger_unlock ();
+}
+
 static inline guint32
 read_leb128 (guint8 *ptr, guint8 **rptr)
 {
@@ -640,14 +765,28 @@ read_variable (MonoDebugVarInfo *var, guint8 *ptr, guint8 **rptr)
        var->size = read_leb128 (ptr, &ptr);
        var->begin_scope = read_leb128 (ptr, &ptr);
        var->end_scope = read_leb128 (ptr, &ptr);
+       READ_UNALIGNED (gpointer, ptr, var->type);
+       ptr += sizeof (gpointer);
        *rptr = ptr;
 }
 
+void
+mono_debug_free_method_jit_info (MonoDebugMethodJitInfo *jit)
+{
+       if (!jit)
+               return;
+       g_free (jit->line_numbers);
+       g_free (jit->this_var);
+       g_free (jit->params);
+       g_free (jit->locals);
+       g_free (jit);
+}
+
 static MonoDebugMethodJitInfo *
 mono_debug_read_method (MonoDebugMethodAddress *address)
 {
        MonoDebugMethodJitInfo *jit;
-       guint32 i, il_offset = 0, native_offset = 0;
+       guint32 i;
        guint8 *ptr;
 
        jit = g_new0 (MonoDebugMethodJitInfo, 1);
@@ -665,31 +804,8 @@ mono_debug_read_method (MonoDebugMethodAddress *address)
        for (i = 0; i < jit->num_line_numbers; i++) {
                MonoDebugLineNumberEntry *lne = &jit->line_numbers [i];
 
-               il_offset += read_sleb128 (ptr, &ptr);
-               native_offset += read_sleb128 (ptr, &ptr);
-
-               lne->il_offset = il_offset;
-               lne->native_offset = native_offset;
-       }
-
-       il_offset = 0;
-       native_offset = 0;
-       jit->num_lexical_blocks = read_leb128 (ptr, &ptr);
-       jit->lexical_blocks = g_new0 (MonoDebugLexicalBlockEntry, jit->num_lexical_blocks);
-       for (i = 0; i < jit->num_lexical_blocks; i ++) {
-               MonoDebugLexicalBlockEntry *lbe = &jit->lexical_blocks [i];
-
-               il_offset += read_sleb128 (ptr, &ptr);
-               native_offset += read_sleb128 (ptr, &ptr);
-
-               lbe->il_start_offset = il_offset;
-               lbe->native_start_offset = native_offset;
-
-               il_offset += read_sleb128 (ptr, &ptr);
-               native_offset += read_sleb128 (ptr, &ptr);
-
-               lbe->il_end_offset = il_offset;
-               lbe->native_end_offset = native_offset;
+               lne->il_offset = read_sleb128 (ptr, &ptr);
+               lne->native_offset = read_sleb128 (ptr, &ptr);
        }
 
        if (*ptr++) {
@@ -710,20 +826,6 @@ mono_debug_read_method (MonoDebugMethodAddress *address)
        return jit;
 }
 
-/*
- * This is called via the `mono_debugger_class_init_func' from mono_class_init() each time
- * a new class is initialized.
- */
-static void
-mono_debug_start_add_type (MonoClass *klass)
-{
-       MonoDebugHandle *handle;
-
-       handle = _mono_debug_get_image (klass->image);
-       if (!handle)
-               return;
-}
-
 static void
 mono_debug_add_type (MonoClass *klass)
 {
@@ -734,16 +836,18 @@ mono_debug_add_type (MonoClass *klass)
        guint32 size, total_size, max_size;
        int base_offset = 0;
 
-       handle = _mono_debug_get_image (klass->image);
-       if (!handle)
-               return;
-
        if (klass->generic_class || klass->rank ||
            (klass->byval_arg.type == MONO_TYPE_VAR) || (klass->byval_arg.type == MONO_TYPE_MVAR))
                return;
 
        mono_debugger_lock ();
 
+       handle = _mono_debug_get_image (klass->image);
+       if (!handle) {
+               mono_debugger_unlock ();
+               return;
+       }
+
        max_size = 12 + sizeof (gpointer);
        if (max_size > BUFSIZ)
                ptr = oldptr = g_malloc (max_size);
@@ -764,34 +868,30 @@ mono_debug_add_type (MonoClass *klass)
 
        g_assert (total_size + 9 < DATA_TABLE_CHUNK_SIZE);
 
-       entry = (MonoDebugClassEntry *) allocate_data_item (MONO_DEBUG_DATA_ITEM_CLASS, total_size);
+       entry = (MonoDebugClassEntry *) allocate_data_item (
+               handle->type_table, MONO_DEBUG_DATA_ITEM_CLASS, total_size);
 
        entry->size = total_size;
-       entry->symfile_id = handle->index;
 
        memcpy (&entry->data, oldptr, size);
 
-       if (mono_debug_debugger_version >= 2)
-               write_data_item ((guint8 *) entry);
+       write_data_item (handle->type_table, (guint8 *) entry);
 
        if (max_size > BUFSIZ)
                g_free (oldptr);
 
-       mono_debugger_add_type (handle, klass);
-
        mono_debugger_unlock ();
 }
 
 static MonoDebugMethodJitInfo *
 find_method (MonoMethod *method, MonoDomain *domain)
 {
-       MonoDebugMethodHash lookup;
+       MonoDebugDataTable *table;
        MonoDebugMethodAddress *address;
 
-       lookup.method = method;
-       lookup.domain_id = mono_domain_get_id (domain);
+       table = lookup_data_table (domain);
+       address = g_hash_table_lookup (table->method_address_hash, method);
 
-       address = g_hash_table_lookup (method_address_hash, &lookup);
        if (!address)
                return NULL;
 
@@ -802,28 +902,56 @@ MonoDebugMethodJitInfo *
 mono_debug_find_method (MonoMethod *method, MonoDomain *domain)
 {
        MonoDebugMethodJitInfo *res;
+
+       if (mono_debug_format == MONO_DEBUG_FORMAT_NONE)
+               return NULL;
+
        mono_debugger_lock ();
        res = find_method (method, domain);
        mono_debugger_unlock ();
        return res;
 }
 
+struct LookupMethodAddressData
+{
+       MonoMethod *method;
+       MonoDebugMethodHeader *result;
+};
+
+static void
+lookup_method_address_func (gpointer key, gpointer value, gpointer user_data)
+{
+       MonoDebugDataTable *table = (MonoDebugDataTable *) value;
+       struct LookupMethodAddressData *data = (struct LookupMethodAddressData *) user_data;
+       MonoDebugMethodHeader *header;
+
+       header = g_hash_table_lookup (table->method_hash, data->method);
+       if (header)
+               data->result = header;
+}
+
 MonoDebugMethodAddressList *
 mono_debug_lookup_method_addresses (MonoMethod *method)
 {
        MonoDebugMethodAddressList *info;
-       MonoDebugMethodHeader *header;
+       MonoDebugMethodHeader *header = NULL;
+       struct LookupMethodAddressData data;
        MonoMethod *declaring;
        int count, size;
        GSList *list;
        guint8 *ptr;
 
-       g_assert (mono_debug_debugger_version == 2);
+       g_assert ((mono_debug_debugger_version == 4) || (mono_debug_debugger_version == 5));
 
        mono_debugger_lock ();
 
        declaring = method->is_inflated ? ((MonoMethodInflated *) method)->declaring : method;
-       header = g_hash_table_lookup (method_hash, declaring);
+
+       data.method = declaring;
+       data.result = NULL;
+
+       g_hash_table_foreach (data_table_hash, lookup_method_address_func, &data);
+       header = data.result;
 
        if (!header) {
                mono_debugger_unlock ();
@@ -859,18 +987,42 @@ il_offset_from_address (MonoMethod *method, MonoDomain *domain, guint32 native_o
 
        jit = find_method (method, domain);
        if (!jit || !jit->line_numbers)
-               return -1;
+               goto cleanup_and_fail;
 
        for (i = jit->num_line_numbers - 1; i >= 0; i--) {
                MonoDebugLineNumberEntry lne = jit->line_numbers [i];
 
-               if (lne.native_offset <= native_offset)
+               if (lne.native_offset <= native_offset) {
+                       mono_debug_free_method_jit_info (jit);
                        return lne.il_offset;
+               }
        }
 
+cleanup_and_fail:
+       mono_debug_free_method_jit_info (jit);
        return -1;
 }
 
+/**
+ * mono_debug_il_offset_from_address:
+ *
+ *   Compute the IL offset corresponding to NATIVE_OFFSET inside the native
+ * code of METHOD in DOMAIN.
+ */
+gint32
+mono_debug_il_offset_from_address (MonoMethod *method, MonoDomain *domain, guint32 native_offset)
+{
+       gint32 res;
+
+       mono_debugger_lock ();
+
+       res = il_offset_from_address (method, domain, native_offset);
+
+       mono_debugger_unlock ();
+
+       return res;
+}
+
 /**
  * mono_debug_lookup_source_location:
  * @address: Native offset within the @method's machine code.
@@ -894,7 +1046,7 @@ mono_debug_lookup_source_location (MonoMethod *method, guint32 address, MonoDoma
 
        mono_debugger_lock ();
        minfo = _mono_debug_lookup_method (method);
-       if (!minfo || !minfo->handle || !minfo->handle->symfile || !minfo->handle->symfile->offset_table) {
+       if (!minfo || !minfo->handle || !minfo->handle->symfile || !mono_debug_symfile_is_loaded (minfo->handle->symfile)) {
                mono_debugger_unlock ();
                return NULL;
        }
@@ -910,6 +1062,34 @@ mono_debug_lookup_source_location (MonoMethod *method, guint32 address, MonoDoma
        return location;
 }
 
+/*
+ * mono_debug_lookup_locals:
+ *
+ *   Return information about the local variables of MINFO.
+ * The result should be freed using mono_debug_symfile_free_locals ().
+ */
+MonoDebugLocalsInfo*
+mono_debug_lookup_locals (MonoMethod *method)
+{
+       MonoDebugMethodInfo *minfo;
+       MonoDebugLocalsInfo *res;
+
+       if (mono_debug_format == MONO_DEBUG_FORMAT_NONE)
+               return NULL;
+
+       mono_debugger_lock ();
+       minfo = _mono_debug_lookup_method (method);
+       if (!minfo || !minfo->handle || !minfo->handle->symfile || !mono_debug_symfile_is_loaded (minfo->handle->symfile)) {
+               mono_debugger_unlock ();
+               return NULL;
+       }
+
+       res = mono_debug_symfile_lookup_locals (minfo);
+       mono_debugger_unlock ();
+
+       return res;
+}
+
 /**
  * mono_debug_free_source_location:
  * @location: A `MonoDebugSourceLocation'.
@@ -937,6 +1117,7 @@ mono_debug_print_stack_frame (MonoMethod *method, guint32 native_offset, MonoDom
 {
        MonoDebugSourceLocation *location;
        gchar *fname, *ptr, *res;
+       int offset;
 
        fname = mono_method_full_name (method, TRUE);
        for (ptr = fname; *ptr; ptr++) {
@@ -946,7 +1127,18 @@ mono_debug_print_stack_frame (MonoMethod *method, guint32 native_offset, MonoDom
        location = mono_debug_lookup_source_location (method, native_offset, domain);
 
        if (!location) {
-               res = g_strdup_printf ("at %s <0x%05x>", fname, native_offset);
+               if (mono_debug_initialized) {
+                       mono_debugger_lock ();
+                       offset = il_offset_from_address (method, domain, native_offset);
+                       mono_debugger_unlock ();
+               } else {
+                       offset = -1;
+               }
+
+               if (offset < 0)
+                       res = g_strdup_printf ("at %s <0x%05x>", fname, native_offset);
+               else
+                       res = g_strdup_printf ("at %s <IL 0x%05x, 0x%05x>", fname, offset, native_offset);
                g_free (fname);
                return res;
        }
@@ -959,3 +1151,90 @@ mono_debug_print_stack_frame (MonoMethod *method, guint32 native_offset, MonoDom
        return res;
 }
 
+void
+mono_debug_list_add (MonoDebugList **list, gconstpointer data)
+{
+       MonoDebugList *element, **ptr;
+
+       element = g_new0 (MonoDebugList, 1);
+       element->data = data;
+
+       for (ptr = list; *ptr; ptr = &(*ptr)->next)
+               ;
+
+       *ptr = element;
+}
+
+void
+mono_debug_list_remove (MonoDebugList **list, gconstpointer data)
+{
+       MonoDebugList **ptr;
+       MonoDebugList *next;
+
+       for (ptr = list; *ptr; ptr = &(*ptr)->next) {
+               if ((*ptr)->data != data)
+                       continue;
+
+               next = (*ptr)->next;
+               g_free ((*ptr));
+               *ptr = next;
+               break;
+       }
+}
+
+static gboolean is_attached = FALSE;
+
+void
+mono_set_is_debugger_attached (gboolean attached)
+{
+       is_attached = attached;
+}
+
+gboolean
+mono_is_debugger_attached (void)
+{
+       return is_attached;
+}
+
+/*
+ * Bundles
+ */
+
+typedef struct _BundledSymfile BundledSymfile;
+
+struct _BundledSymfile {
+       BundledSymfile *next;
+       const char *aname;
+       const mono_byte *raw_contents;
+       int size;
+};
+
+static BundledSymfile *bundled_symfiles = NULL;
+
+void
+mono_register_symfile_for_assembly (const char *assembly_name, const mono_byte *raw_contents, int size)
+{
+       BundledSymfile *bsymfile;
+
+       bsymfile = g_new0 (BundledSymfile, 1);
+       bsymfile->aname = assembly_name;
+       bsymfile->raw_contents = raw_contents;
+       bsymfile->size = size;
+       bsymfile->next = bundled_symfiles;
+       bundled_symfiles = bsymfile;
+}
+
+static MonoDebugHandle *
+open_symfile_from_bundle (MonoImage *image)
+{
+       BundledSymfile *bsymfile;
+
+       for (bsymfile = bundled_symfiles; bsymfile; bsymfile = bsymfile->next) {
+               if (strcmp (bsymfile->aname, image->module_name))
+                       continue;
+
+               return mono_debug_open_image (image, bsymfile->raw_contents, bsymfile->size);
+       }
+
+       return NULL;
+}