2006-06-08 Zoltan Varga <vargaz@gmail.com>
[mono.git] / mono / metadata / mono-debug.c
index 1d48a484c61f0788f82deeaa8ca8678122ef1f53..d4e1ef58e8faf2ed528a92193fcbda6caa950ae3 100644 (file)
 #define DATA_TABLE_PTR_CHUNK_SIZE      256
 #define DATA_TABLE_CHUNK_SIZE          32768
 
+#define ALIGN_TO(val,align) ((((guint64)val) + ((align) - 1)) & ~((align) - 1))
+
+#if NO_UNALIGNED_ACCESS
+#define RETURN_UNALIGNED(type, addr) \
+       { \
+               type val; \
+               memcpy(&val, p + offset, sizeof(val)); \
+               return val; \
+       }
+#define WRITE_UNALIGNED(type, addr, val) \
+       memcpy(addr, &val, sizeof(type))
+#else
+#define RETURN_UNALIGNED(type, addr) \
+       return *(type*)(p + offset);
+#define WRITE_UNALIGNED(type, addr, val) \
+       (*(type *)(addr) = (val))
+#endif
+
 MonoSymbolTable *mono_symbol_table = NULL;
 MonoDebugFormat mono_debug_format = MONO_DEBUG_FORMAT_NONE;
 
@@ -90,6 +108,9 @@ mono_debug_init (MonoDebugFormat format)
        mono_debugger_start_class_init_func = mono_debug_start_add_type;
        mono_debugger_class_init_func = mono_debug_add_type;
        mono_install_assembly_load_hook (mono_debug_add_assembly, NULL);
+
+       if (!in_the_mono_debugger)
+               mono_debugger_unlock ();
 }
 
 void
@@ -97,8 +118,7 @@ mono_debug_init_1 (MonoDomain *domain)
 {
        MonoDebugHandle *handle = mono_debug_open_image (mono_get_corlib ());
 
-       if (in_the_mono_debugger)
-               mono_debugger_add_builtin_types (handle);
+       mono_symbol_table->corlib = handle;
 }
 
 /*
@@ -112,6 +132,12 @@ mono_debug_init_2 (MonoAssembly *assembly)
        mono_debug_open_image (mono_assembly_get_image (assembly));
 }
 
+gboolean
+mono_debug_using_mono_debugger (void)
+{
+       return in_the_mono_debugger;
+}
+
 void
 mono_debug_cleanup (void)
 {
@@ -208,16 +234,18 @@ allocate_data_item (MonoDebugDataItemType type, guint32 size)
 
        g_assert (mono_symbol_table);
 
-       if (size + 12 < DATA_TABLE_CHUNK_SIZE)
+       size = ALIGN_TO (size, sizeof (gpointer));
+
+       if (size + 16 < DATA_TABLE_CHUNK_SIZE)
                chunk_size = DATA_TABLE_CHUNK_SIZE;
        else
-               chunk_size = size + 12;
+               chunk_size = size + 16;
 
        /* Initialize things if necessary. */
        if (!mono_symbol_table->current_data_table) {
                mono_symbol_table->current_data_table = g_malloc0 (chunk_size);
                mono_symbol_table->current_data_table_size = chunk_size;
-               mono_symbol_table->current_data_table_offset = 4;
+               mono_symbol_table->current_data_table_offset = sizeof (gpointer);
 
                * ((guint32 *) mono_symbol_table->current_data_table) = chunk_size;
        }
@@ -253,7 +281,7 @@ allocate_data_item (MonoDebugDataItemType type, guint32 size)
        /* .... allocate a new current_data_table. */
        mono_symbol_table->current_data_table = g_malloc0 (chunk_size);
        mono_symbol_table->current_data_table_size = chunk_size;
-       mono_symbol_table->current_data_table_offset = 4;
+       mono_symbol_table->current_data_table_offset = sizeof (gpointer);
        * ((guint32 *) mono_symbol_table->current_data_table) = chunk_size;
 
        goto again;
@@ -275,7 +303,7 @@ lookup_method_func (gpointer key, gpointer value, gpointer user_data)
                return;
 
        if (handle->symfile)
-               data->minfo = mono_debug_find_method (handle, data->method);
+               data->minfo = mono_debug_symfile_lookup_method (handle, data->method);
 }
 
 static MonoDebugMethodInfo *
@@ -293,6 +321,24 @@ _mono_debug_lookup_method (MonoMethod *method)
        return data.minfo;
 }
 
+/**
+ * mono_debug_lookup_method:
+ *
+ * Lookup symbol file information for the method @method.  The returned
+ * `MonoDebugMethodInfo' is a private structure, but it can be passed to
+ * mono_debug_symfile_lookup_location().
+ */
+MonoDebugMethodInfo *
+mono_debug_lookup_method (MonoMethod *method)
+{
+       MonoDebugMethodInfo *minfo;
+
+       mono_debugger_lock ();
+       minfo = _mono_debug_lookup_method (method);
+       mono_debugger_unlock ();
+       return minfo;
+}
+
 static inline void
 write_leb128 (guint32 value, guint8 *ptr, guint8 **rptr)
 {
@@ -337,6 +383,81 @@ write_variable (MonoDebugVarInfo *var, guint8 *ptr, guint8 **rptr)
        *rptr = ptr;
 }
 
+static MonoDebugWrapperData *
+mono_debug_add_wrapper (MonoMethod *method, MonoDebugMethodJitInfo *jit)
+{
+       MonoMethodHeader *header;
+       MonoDebugWrapperData *wrapper;
+       char buffer [BUFSIZ];
+       guint8 *ptr, *oldptr;
+       guint32 i, size, total_size, max_size;
+       gint32 last_il_offset = 0, last_native_offset = 0;
+       const unsigned char* il_code;
+       guint32 il_codesize;
+
+       if (!in_the_mono_debugger)
+               return NULL;
+
+       mono_debugger_lock ();
+
+       header = mono_method_get_header (method);
+
+       max_size = 28 * jit->num_line_numbers;
+       if (max_size > BUFSIZ)
+               ptr = oldptr = g_malloc (max_size);
+       else
+               ptr = oldptr = buffer;
+
+       write_leb128 (jit->prologue_end, ptr, &ptr);
+       write_leb128 (jit->epilogue_begin, ptr, &ptr);
+       write_leb128 (jit->num_line_numbers, ptr, &ptr);
+       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;
+       }
+
+       write_leb128 (method->wrapper_type, ptr, &ptr);
+
+       size = ptr - oldptr;
+       g_assert (size < max_size);
+       total_size = size + sizeof (MonoDebugWrapperData);
+
+       if (total_size + 9 >= DATA_TABLE_CHUNK_SIZE) {
+               // FIXME: Maybe we should print a warning here.
+               //        This should only happen for very big methods, for instance
+               //        with more than 40.000 line numbers and more than 5.000
+               //        local variables.
+               mono_debugger_unlock ();
+               return NULL;
+       }
+
+       wrapper = (MonoDebugWrapperData *) allocate_data_item (MONO_DEBUG_DATA_ITEM_WRAPPER, total_size);
+
+       wrapper->method = method;
+       wrapper->size = total_size;
+       wrapper->code_start = jit->code_start;
+       wrapper->code_size = jit->code_size;
+       wrapper->name = mono_method_full_name (method, TRUE);
+
+       il_code = mono_method_header_get_code (header, &il_codesize, NULL);
+       wrapper->cil_code = mono_disasm_code (
+               NULL, method, il_code, il_code + il_codesize);
+
+       memcpy (&wrapper->data, oldptr, size);
+
+       if (max_size > BUFSIZ)
+               g_free (oldptr);
+
+       mono_debugger_unlock ();
+
+       return wrapper;
+}
+
 /*
  * This is called by the JIT to tell the debugging code about a newly
  * compiled method.
@@ -356,16 +477,17 @@ mono_debug_add_method (MonoMethod *method, MonoDebugMethodJitInfo *jit, MonoDoma
        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))
-               return NULL;
-
-       if (method->wrapper_type != MONO_WRAPPER_NONE)
+           (method->flags & METHOD_ATTRIBUTE_ABSTRACT) ||
+           (method->wrapper_type != MONO_WRAPPER_NONE)) {
+               mono_debug_add_wrapper (method, jit);
                return NULL;
+       }
 
        mono_debugger_lock ();
 
        handle = _mono_debug_get_image (method->klass->image);
        if (!handle || !handle->symfile || !handle->symfile->offset_table) {
+               mono_debug_add_wrapper (method, jit);
                mono_debugger_unlock ();
                return NULL;
        }
@@ -401,11 +523,10 @@ mono_debug_add_method (MonoMethod *method, MonoDebugMethodJitInfo *jit, MonoDoma
        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 = minfo_lbe->_start_offset;
+               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 = minfo_lbe->_end_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);
        }
 
@@ -449,6 +570,7 @@ mono_debug_add_method (MonoMethod *method, MonoDebugMethodJitInfo *jit, MonoDoma
                //        This should only happen for very big methods, for instance
                //        with more than 40.000 line numbers and more than 5.000
                //        local variables.
+               mono_debugger_unlock ();
                return NULL;
        }
 
@@ -474,9 +596,6 @@ mono_debug_add_method (MonoMethod *method, MonoDebugMethodJitInfo *jit, MonoDoma
 
        g_hash_table_insert (method_hash, hash, address);
 
-       if (in_the_mono_debugger)
-               mono_debugger_add_method (jit);
-
        mono_debugger_unlock ();
 
        return address;
@@ -630,18 +749,6 @@ mono_debug_start_add_type (MonoClass *klass)
        handle = _mono_debug_get_image (klass->image);
        if (!handle)
                return;
-
-       if (in_the_mono_debugger)
-               mono_debugger_add_type (handle, klass);
-}
-
-static guint32
-get_token (MonoClass *klass)
-{
-       while (klass->rank)
-               klass = klass->element_class;
-
-       return klass->type_token;
 }
 
 static void
@@ -651,14 +758,14 @@ mono_debug_add_type (MonoClass *klass)
        MonoDebugClassEntry *entry;
        char buffer [BUFSIZ];
        guint8 *ptr, *oldptr;
-       guint32 token, size, total_size, max_size;
+       guint32 size, total_size, max_size;
        int base_offset = 0;
 
        handle = _mono_debug_get_image (klass->image);
        if (!handle)
                return;
 
-       if (klass->generic_class ||
+       if (klass->generic_class || klass->rank ||
            (klass->byval_arg.type == MONO_TYPE_VAR) || (klass->byval_arg.type == MONO_TYPE_MVAR))
                return;
 
@@ -668,30 +775,19 @@ mono_debug_add_type (MonoClass *klass)
        else
                ptr = oldptr = buffer;
 
-       token = get_token (klass);
-       if (!token)
-               return;
-
        if (klass->valuetype)
                base_offset = - (int)(sizeof (MonoObject));
 
-       write_leb128 (token, ptr, &ptr);
-       write_leb128 (klass->rank, ptr, &ptr);
+       write_leb128 (klass->type_token, ptr, &ptr);
        write_leb128 (klass->instance_size + base_offset, ptr, &ptr);
-       * ((gpointer *) ptr) = klass;
+       WRITE_UNALIGNED (gpointer, ptr, klass);
        ptr += sizeof (gpointer);
 
        size = ptr - oldptr;
        g_assert (size < max_size);
        total_size = size + sizeof (MonoDebugClassEntry);
 
-       if (total_size + 9 >= DATA_TABLE_CHUNK_SIZE) {
-               // FIXME: Maybe we should print a warning here.
-               //        This should only happen for very big methods, for instance
-               //        with more than 40.000 line numbers and more than 5.000
-               //        local variables.
-               return;
-       }
+       g_assert (total_size + 9 < DATA_TABLE_CHUNK_SIZE);
 
        entry = (MonoDebugClassEntry *) allocate_data_item (MONO_DEBUG_DATA_ITEM_CLASS, total_size);
 
@@ -744,143 +840,89 @@ il_offset_from_address (MonoDebugMethodInfo *minfo, MonoDomain *domain, guint32
 }
 
 /**
- * mono_debug_source_location_from_address:
- * @method:
- * @address:
- * @line_number:
- * @domain:
- *
- * Used by the exception code to get a source location from a machine address.
+ * mono_debug_lookup_source_location:
+ * @address: Native offset within the @method's machine code.
  *
- * Returns: a textual representation of the specified address which is suitable to be displayed to
- * the user (for instance "/home/martin/monocvs/debugger/test/Y.cs:8").
+ * Lookup the source code corresponding to the machine instruction located at
+ * native offset @address within @method.
  *
- * If the optional @line_number argument is not NULL, the line number is stored there and just the
- * source file is returned (ie. it'd return "/home/martin/monocvs/debugger/test/Y.cs" and store the
- * line number 8 in the variable pointed to by @line_number).
+ * The returned `MonoDebugSourceLocation' contains both file / line number
+ * information and the corresponding IL offset.  It must be freed by
+ * mono_debug_free_source_location().
  */
-gchar *
-mono_debug_source_location_from_address (MonoMethod *method, guint32 address, guint32 *line_number,
-                                        MonoDomain *domain)
+MonoDebugSourceLocation *
+mono_debug_lookup_source_location (MonoMethod *method, guint32 address, MonoDomain *domain)
 {
        MonoDebugMethodInfo *minfo;
-       char *res = NULL;
+       MonoDebugSourceLocation *location;
        gint32 offset;
 
-       mono_loader_lock ();
+       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 || !minfo->handle->symfile->offset_table) {
-               mono_loader_unlock ();
+               mono_debugger_unlock ();
                return NULL;
        }
 
        offset = il_offset_from_address (minfo, domain, address);
-               
-       if (offset >= 0)
-               res = mono_debug_find_source_location (minfo->handle->symfile, method, offset, line_number);
-
-       mono_loader_unlock ();
-       return res;
-}
-
-/**
- * mono_debug_source_location_from_il_offset:
- * @method:
- * @offset:
- * @line_number:
- *
- * Used by the exception code to get a source location from an IL offset.
- *
- * Returns a textual representation of the specified address which is suitable to be displayed to
- * the user (for instance "/home/martin/monocvs/debugger/test/Y.cs:8").
- *
- * If the optional @line_number argument is not NULL, the line number is stored there and just the
- * source file is returned (ie. it'd return "/home/martin/monocvs/debugger/test/Y.cs" and store the
- * line number 8 in the variable pointed to by @line_number).
- */
-gchar *
-mono_debug_source_location_from_il_offset (MonoMethod *method, guint32 offset, guint32 *line_number)
-{
-       char *res;
-       MonoDebugMethodInfo *minfo;
-
-       mono_loader_lock ();
-       minfo = _mono_debug_lookup_method (method);
-       if (!minfo || !minfo->handle || !minfo->handle->symfile) {
-               mono_loader_unlock ();
+       if (offset < 0) {
+               mono_debugger_unlock ();
                return NULL;
        }
 
-       res = mono_debug_find_source_location (minfo->handle->symfile, method, offset, line_number);
-       mono_loader_unlock ();
-       return res;
+       location = mono_debug_symfile_lookup_location (minfo, offset);
+       mono_debugger_unlock ();
+       return location;
 }
 
 /**
- * mono_debug_il_offset_from_address:
- * @method:
- * @address:
- * @domain:
+ * mono_debug_free_source_location:
+ * @location: A `MonoDebugSourceLocation'.
  *
- * Returns: the IL offset corresponding to machine address @address which is an offset
- * relative to the beginning of the method @method.
+ * Frees the @location.
  */
-gint32
-mono_debug_il_offset_from_address (MonoMethod *method, gint32 address, MonoDomain *domain)
+void
+mono_debug_free_source_location (MonoDebugSourceLocation *location)
 {
-       MonoDebugMethodInfo *minfo;
-       gint32 res;
-
-       if (address < 0)
-               return -1;
-
-       mono_loader_lock ();
-       minfo = _mono_debug_lookup_method (method);
-       if (!minfo || !minfo->il_offsets || !minfo->handle || !minfo->handle->symfile ||
-           !minfo->handle->symfile->offset_table) {
-               mono_loader_unlock ();
-               return -1;
+       if (location) {
+               g_free (location->source_file);
+               g_free (location);
        }
-
-       res = il_offset_from_address (minfo, domain, address);
-       mono_loader_unlock ();
-       return res;
 }
 
 /**
- * mono_debug_address_from_il_offset:
- * @method:
- * @il_offset:
- * @domain:
+ * mono_debug_print_stack_frame:
+ * @native_offset: Native offset within the @method's machine code.
  *
- * Returns: the machine address corresponding to IL offset @il_offset.
- * The returned value is an offset relative to the beginning of the method @method.
+ * Conventient wrapper around mono_debug_lookup_source_location() which can be
+ * used if you only want to use the location to print a stack frame.
  */
-gint32
-mono_debug_address_from_il_offset (MonoMethod *method, gint32 il_offset, MonoDomain *domain)
+gchar *
+mono_debug_print_stack_frame (MonoMethod *method, guint32 native_offset, MonoDomain *domain)
 {
-       MonoDebugMethodInfo *minfo;
-       MonoDebugMethodJitInfo *jit;
-       gint32 res;
+       MonoDebugSourceLocation *location;
+       gchar *fname, *ptr, *res;
 
-       if (il_offset < 0)
-               return -1;
-
-       mono_loader_lock ();
-       minfo = _mono_debug_lookup_method (method);
-       if (!minfo || !minfo->il_offsets || !minfo->handle || !minfo->handle->symfile ||
-           !minfo->handle->symfile->offset_table) {
-               mono_loader_unlock ();
-               return -1;
+       fname = mono_method_full_name (method, TRUE);
+       for (ptr = fname; *ptr; ptr++) {
+               if (*ptr == ':') *ptr = '.';
        }
 
-       jit = find_method (minfo, domain);
-       if (!jit) {
-               mono_loader_unlock ();
-               return -1;
+       location = mono_debug_lookup_source_location (method, native_offset, domain);
+
+       if (!location) {
+               res = g_strdup_printf ("at %s <0x%05x>", fname, native_offset);
+               g_free (fname);
+               return res;
        }
 
-       res = _mono_debug_address_from_il_offset (jit, il_offset);
-       mono_loader_unlock ();
+       res = g_strdup_printf ("at %s [0x%05x] in %s:%d", fname, location->il_offset,
+                              location->source_file, location->row);
+
+       g_free (fname);
+       mono_debug_free_source_location (location);
        return res;
 }