2002-08-16 Nick Drochak <ndrochak@gol.com>
[mono.git] / mono / metadata / debug-symfile.c
index bfa627dd8adf22e6b93383707c08d22be5e2b2c8..4cf9fa90029866d512595d6c19613d927b399496 100644 (file)
@@ -1,8 +1,10 @@
 #include <config.h>
 #include <stdlib.h>
 #include <string.h>
+#include <signal.h>
 #include <mono/metadata/metadata.h>
 #include <mono/metadata/rawbuffer.h>
+#include <mono/metadata/tokentype.h>
 #include <mono/metadata/appdomain.h>
 #include <mono/metadata/exception.h>
 #include <mono/metadata/debug-symfile.h>
 #define MRT_mono_string_fieldsize      0x11
 #define MRT_mono_array_fieldsize       0x12
 #define MRT_type_field_fieldsize       0x13
+#define MRT_mono_string_string_length  0x14
+#define MRT_mono_string_byte_size      0x15
+#define MRT_mono_string_data_location  0x16
+#define MRT_static_type_field_offset   0x17
+#define MRT_mono_array_data_location   0x18
+#define MRT_mono_array_max_length      0x19
+#define MRT_mono_array_length_byte_size        0x1a
 
 #define MRI_string_offset_length       0x00
 #define MRI_string_offset_chars                0x01
@@ -51,6 +60,7 @@
 #define MRS_debug_line                 0x03
 #define MRS_mono_reloc_table           0x04
 
+#define DW_OP_const4u                  0x0c
 #define DW_OP_const4s                  0x0d
 #define DW_OP_plus                     0x22
 #define DW_OP_reg0                     0x50
@@ -129,6 +139,20 @@ get_sections_elf32 (MonoDebugSymbolFile *symfile, gboolean emit_warnings)
                        sfs->type = MONO_DEBUG_SYMBOL_SECTION_MONO_RELOC_TABLE;
                        sfs->file_offset = section->sh_offset;
                        sfs->size = section->sh_size;
+               } else if (!strcmp (name, ".mono_line_numbers")) {
+                       MonoDebugSymbolFileSection *sfs;
+
+                       sfs = &symfile->section_offsets [MONO_DEBUG_SYMBOL_SECTION_MONO_LINE_NUMBERS];
+                       sfs->type = MONO_DEBUG_SYMBOL_SECTION_MONO_LINE_NUMBERS;
+                       sfs->file_offset = section->sh_offset;
+                       sfs->size = section->sh_size;
+               } else if (!strcmp (name, ".mono_symbol_table")) {
+                       MonoDebugSymbolFileSection *sfs;
+
+                       sfs = &symfile->section_offsets [MONO_DEBUG_SYMBOL_SECTION_MONO_SYMBOL_TABLE];
+                       sfs->type = MONO_DEBUG_SYMBOL_SECTION_MONO_SYMBOL_TABLE;
+                       sfs->file_offset = section->sh_offset;
+                       sfs->size = section->sh_size;
                }
        }
 
@@ -154,17 +178,140 @@ get_sections (MonoDebugSymbolFile *symfile, gboolean emit_warnings)
        return FALSE;
 }
 
+static void
+read_line_numbers (MonoDebugSymbolFile *symfile)
+{
+       const char *ptr, *start, *end;
+       int version;
+       long section_size;
+
+       if (!symfile->section_offsets [MONO_DEBUG_SYMBOL_SECTION_MONO_LINE_NUMBERS].file_offset)
+               return;
+
+       ptr = start = symfile->raw_contents +
+               symfile->section_offsets [MONO_DEBUG_SYMBOL_SECTION_MONO_LINE_NUMBERS].file_offset;
+
+       version = *((guint16 *) ptr)++;
+       if (version != MONO_DEBUG_SYMBOL_FILE_VERSION) {
+               g_warning ("Symbol file %s has incorrect line number table version "
+                          "(expected %d, got %d)", symfile->file_name,
+                          MONO_DEBUG_SYMBOL_FILE_VERSION, version);
+               return;
+       }
+
+       section_size = *((guint32 *) ptr)++;
+       end = ptr + section_size;
+
+       symfile->line_number_table = g_hash_table_new_full (g_direct_hash, g_direct_equal,
+                                                           NULL, (GDestroyNotify) g_free);
+
+       while (ptr < end) {
+               MonoDebugLineNumberBlock *lnb;
+               guint32 token, source_offset;
+               MonoMethod *method;
+
+               token = * ((guint32 *) ptr)++;
+               method = mono_get_method (symfile->image, token, NULL);
+               if (!method)
+                       continue;
+
+               lnb = g_new0 (MonoDebugLineNumberBlock, 1);
+               lnb->token = token;
+               source_offset = * ((guint32 *) ptr)++;
+               lnb->source_file = (const char *) start + source_offset;
+               lnb->start_line = * ((guint32 *) ptr)++;
+               lnb->file_offset = * ((guint32 *) ptr)++;
+
+               g_hash_table_insert (symfile->line_number_table, method, lnb);
+       }
+}
+
 static MonoClass *
-mono_debug_class_get (MonoDebugSymbolFile *symfile, guint32 type_token)
+find_class_by_name (MonoDebugSymbolFile *symfile, const char *nspace, const char *name)
 {
+       MonoAssembly **assembly;
        MonoClass *klass;
 
-       if ((klass = g_hash_table_lookup (symfile->image->class_cache, GUINT_TO_POINTER (type_token))))
+       klass = mono_class_from_name (symfile->image, nspace, name);
+
+       if (klass)
                return klass;
 
+       for (assembly = symfile->image->references; assembly && *assembly; assembly++) {
+               klass = mono_class_from_name ((*assembly)->image, nspace, name);
+
+               if (klass)
+                       return klass;
+       }
+
+       g_message (G_STRLOC ": Can't find class %s.%s", nspace, name);
+
        return NULL;
 }
 
+static void
+read_symbol_table (MonoDebugSymbolFile *symfile)
+{
+       const char *ptr, *start, *end;
+       int version, i;
+       long section_size;
+
+       if (!symfile->section_offsets [MONO_DEBUG_SYMBOL_SECTION_MONO_SYMBOL_TABLE].file_offset)
+               return;
+
+       ptr = start = symfile->raw_contents +
+               symfile->section_offsets [MONO_DEBUG_SYMBOL_SECTION_MONO_SYMBOL_TABLE].file_offset;
+
+       version = *((guint16 *) ptr)++;
+       if (version != MONO_DEBUG_SYMBOL_FILE_VERSION) {
+               g_warning ("Symbol file %s has incorrect line number table version "
+                          "(expected %d, got %d)", symfile->file_name,
+                          MONO_DEBUG_SYMBOL_FILE_VERSION, version);
+               return;
+       }
+
+       section_size = *((guint32 *) ptr)++;
+       end = ptr + section_size;
+
+       g_free (symfile->type_table);
+
+       symfile->num_types = * ((guint32 *) ptr)++;
+       symfile->type_table = g_new0 (MonoClass *, symfile->num_types);
+
+       for (i = 0; i < symfile->num_types; i++) {
+               const char *name, *namespace;
+               MonoClass *klass;
+
+               namespace = ptr;
+               ptr += strlen (namespace) + 1;
+
+               name = ptr;
+               ptr += strlen (name) + 1;
+
+               klass = find_class_by_name (symfile, namespace, name);
+
+               if (klass)
+                       symfile->type_table [i] = klass;
+       }
+}
+
+static MonoClass *
+mono_debug_class_get (MonoDebugSymbolFile *symfile, guint32 type_token)
+{
+       MonoClass *klass;
+
+       if (!type_token)
+               return mono_defaults.object_class;
+
+       if (type_token <= symfile->num_types)
+               return symfile->type_table [type_token - 1];
+
+       if ((klass = g_hash_table_lookup (symfile->image->class_cache, GUINT_TO_POINTER (type_token))))
+                return klass;
+
+        return NULL;
+}
+
 MonoDebugSymbolFile *
 mono_debug_open_symbol_file (MonoImage *image, const char *filename, gboolean emit_warnings)
 {
@@ -208,6 +355,10 @@ mono_debug_open_symbol_file (MonoImage *image, const char *filename, gboolean em
                return NULL;
        }
 
+       read_symbol_table (symfile);
+
+       read_line_numbers (symfile);
+
        return symfile;
 }
 
@@ -222,6 +373,8 @@ mono_debug_close_symbol_file (MonoDebugSymbolFile *symfile)
        if (symfile->fd)
                close (symfile->fd);
 
+       if (symfile->line_number_table)
+               g_hash_table_destroy (symfile->line_number_table);
        g_free (symfile->file_name);
        g_free (symfile->section_offsets);
        g_free (symfile);
@@ -318,6 +471,8 @@ mono_debug_update_symbol_file (MonoDebugSymbolFile *symfile,
        int version, already_relocated = 0;
        long reloc_size;
 
+       read_symbol_table (symfile);
+
        if (!symfile->section_offsets [MONO_DEBUG_SYMBOL_SECTION_MONO_RELOC_TABLE].file_offset)
                return;
 
@@ -383,7 +538,8 @@ mono_debug_update_symbol_file (MonoDebugSymbolFile *symfile,
                        }
 
 #if 0
-                       g_message ("Start of `%s' relocated to %p", minfo->method->name, minfo->code_start);
+                       g_message ("Start of `%s' (%ld) relocated to %p", minfo->method->name,
+                                  token, minfo->code_start);
 #endif
 
                        * (void **) base_ptr = minfo->code_start;
@@ -457,6 +613,8 @@ mono_debug_update_symbol_file (MonoDebugSymbolFile *symfile,
                                           "local variable %d, but method %s only has %d local variables.",
                                           symfile->file_name, original, minfo->method->name,
                                           minfo->num_locals);
+                               g_message (G_STRLOC ": %d", token);
+                               G_BREAKPOINT ();
                                continue;
                        }
 
@@ -527,6 +685,8 @@ mono_debug_update_symbol_file (MonoDebugSymbolFile *symfile,
                        if (original > klass->field.count) {
                                g_warning ("Symbol file %s contains invalid field offset entry.",
                                           symfile->file_name);
+                               g_message (G_STRLOC ": %d", token);
+                               /* G_BREAKPOINT (); */
                                continue;
                        }
 
@@ -644,7 +804,7 @@ mono_debug_update_symbol_file (MonoDebugSymbolFile *symfile,
 
                        minfo = method_info_func (symfile, token, user_data);
 
-                       if (!minfo) {
+                       if (!minfo || !minfo->locals) {
                                * (void **) base_ptr = 0;
                                continue;
                        }
@@ -672,7 +832,7 @@ mono_debug_update_symbol_file (MonoDebugSymbolFile *symfile,
 
                        minfo = method_info_func (symfile, token, user_data);
 
-                       if (!minfo) {
+                       if (!minfo || !minfo->locals) {
                                * (void **) base_ptr = 0;
                                continue;
                        }
@@ -742,6 +902,108 @@ mono_debug_update_symbol_file (MonoDebugSymbolFile *symfile,
                        break;
                }
 
+               case MRT_mono_string_string_length: {
+                       MonoString string;
+                       guint32 offset = (guchar *) &string.length - (guchar *) &string;
+
+                       * ((guint8 *) base_ptr)++ = DW_OP_const4u;
+                       * ((gint32 *) base_ptr)++ = offset;
+                       * ((guint8 *) base_ptr)++ = DW_OP_nop;
+                       * ((guint8 *) base_ptr)++ = DW_OP_nop;
+                       * ((guint8 *) base_ptr)++ = DW_OP_nop;
+
+                       break;
+               }
+
+               case MRT_mono_string_byte_size: {
+                       MonoString string;
+
+                       * (guint32 *) base_ptr = sizeof (string.length);
+
+                       break;
+               }
+
+               case MRT_mono_string_data_location: {
+                       MonoString string;
+                       guint32 offset = (guchar *) &string.chars - (guchar *) &string;
+
+                       * ((guint8 *) base_ptr)++ = DW_OP_const4u;
+                       * ((gint32 *) base_ptr)++ = offset;
+                       * ((guint8 *) base_ptr)++ = DW_OP_nop;
+                       * ((guint8 *) base_ptr)++ = DW_OP_nop;
+                       * ((guint8 *) base_ptr)++ = DW_OP_nop;
+
+                       break;
+               }
+
+               case MRT_static_type_field_offset: {
+                       guint32 token = *((guint32 *) tmp_ptr)++;
+                       guint32 original = *((guint32 *) tmp_ptr)++;
+                       MonoClass *klass = mono_debug_class_get (symfile, token);
+                       MonoVTable *vtable;
+                       guint32 off;
+
+                       if (!klass)
+                               continue;
+
+                       mono_class_init (klass);
+
+                       if (original > klass->field.count) {
+                               g_warning ("Symbol file %s contains invalid field offset entry.",
+                                          symfile->file_name);
+                               continue;
+                       }
+
+                       if (!klass->fields)
+                               continue;
+
+                       vtable = mono_class_vtable (mono_domain_get (), klass);
+
+                       off = klass->fields [original].offset;
+
+#if 0
+                       g_message ("Setting field %d of type %u (%p) to offset %d", original,
+                                  token, klass, off);
+#endif
+
+                       * (void **) base_ptr = (char *) vtable->data + off;
+
+                       break;
+               }
+
+               case MRT_mono_array_data_location: {
+                       MonoArray array;
+                       guint32 offset = (guchar *) &array.vector - (guchar *) &array;
+
+                       * ((guint8 *) base_ptr)++ = DW_OP_const4u;
+                       * ((gint32 *) base_ptr)++ = offset;
+                       * ((guint8 *) base_ptr)++ = DW_OP_nop;
+                       * ((guint8 *) base_ptr)++ = DW_OP_nop;
+                       * ((guint8 *) base_ptr)++ = DW_OP_nop;
+
+                       break;
+               }
+
+               case MRT_mono_array_max_length: {
+                       MonoArray array;
+                       guint32 offset = (guchar *) &array.max_length - (guchar *) &array;
+
+                       * ((guint8 *) base_ptr)++ = DW_OP_const4u;
+                       * ((gint32 *) base_ptr)++ = offset;
+                       * ((guint8 *) base_ptr)++ = DW_OP_nop;
+                       * ((guint8 *) base_ptr)++ = DW_OP_nop;
+                       * ((guint8 *) base_ptr)++ = DW_OP_nop;
+
+                       break;
+               }
+
+               case MRT_mono_array_length_byte_size: {
+                       MonoArray array;
+
+                       * (guint32 *) base_ptr = sizeof (array.max_length);
+
+                       break;
+               }
 
                default:
                        g_warning ("Symbol file %s contains unknown relocation entry %d",
@@ -753,55 +1015,44 @@ mono_debug_update_symbol_file (MonoDebugSymbolFile *symfile,
        mono_raw_buffer_update (symfile->raw_contents, symfile->raw_contents_size);
 }
 
-MonoReflectionType *
-ves_icall_Debugger_MonoSymbolWriter_get_local_type_from_sig (MonoReflectionAssembly *assembly,
-                                                            MonoArray *signature)
+gchar *
+mono_debug_find_source_location (MonoDebugSymbolFile *symfile, MonoMethod *method, guint32 offset,
+                                guint32 *line_number)
 {
-       MonoDomain *domain; 
-       MonoImage *image;
-       MonoType *type;
+       MonoDebugLineNumberBlock *lnb;
        const char *ptr;
-       int len = 0;
-
-       MONO_CHECK_ARG_NULL (assembly);
-       MONO_CHECK_ARG_NULL (signature);
-
-       domain = mono_domain_get();
-       image = assembly->assembly->image;
-
-       ptr = mono_array_addr (signature, char, 0);
-       g_assert (*ptr++ == 0x07);
-       len = mono_metadata_decode_value (ptr, &ptr);
-       g_assert (len == 1);
-
-       type = mono_metadata_parse_type (image, MONO_PARSE_LOCAL, 0, ptr, &ptr);
 
-       return mono_type_get_object (domain, type);
-}
+       if (!symfile->line_number_table)
+               return NULL;
 
-MonoReflectionMethod *
-ves_icall_Debugger_MonoSymbolWriter_method_from_token (MonoReflectionAssembly *assembly, guint32 token)
-{
-       MonoDomain *domain; 
-       MonoImage *image;
-       MonoMethod *method;
+       lnb = g_hash_table_lookup (symfile->line_number_table, method);
+       if (!lnb)
+               return NULL;
 
-       MONO_CHECK_ARG_NULL (assembly);
+       ptr = symfile->raw_contents +
+               symfile->section_offsets [MONO_DEBUG_SYMBOL_SECTION_MONO_LINE_NUMBERS].file_offset;
 
-       domain = mono_domain_get();
-       image = assembly->assembly->image;
+       ptr += lnb->file_offset;
 
-       method = mono_get_method (image, token, NULL);
+       do {
+               guint32 row, iloffset;
 
-       return mono_method_get_object (domain, method);
-}
+               row = * ((guint32 *) ptr)++;
+               iloffset = * ((guint32 *) ptr)++;
 
-guint32
-ves_icall_Debugger_DwarfFileWriter_get_type_token (MonoReflectionType *type)
-{
-       MonoClass *klass = mono_class_from_mono_type (type->type);
+               if (!row && !offset)
+                       return NULL;
+               if (!row)
+                       continue;
 
-       mono_class_init (klass);
+               if (iloffset >= offset) {
+                       if (line_number) {
+                               *line_number = row;
+                               return g_strdup (lnb->source_file);
+                       } else
+                               return g_strdup_printf ("%s:%d", lnb->source_file, row);
+               }
+       } while (1);
 
-       return klass->type_token;
+       return NULL;
 }