Add support for the #line directive to sdb. Fixes part of #3217.
authorZoltan Varga <vargaz@gmail.com>
Sat, 4 Feb 2012 11:35:30 +0000 (12:35 +0100)
committerZoltan Varga <vargaz@gmail.com>
Sat, 4 Feb 2012 11:35:30 +0000 (12:35 +0100)
mcs/class/Mono.Debugger.Soft/Mono.Debugger.Soft/Connection.cs
mcs/class/Mono.Debugger.Soft/Mono.Debugger.Soft/MethodMirror.cs
mcs/class/Mono.Debugger.Soft/Mono.Debugger.Soft/StackFrame.cs
mcs/class/Mono.Debugger.Soft/Test/dtest-app.cs
mcs/class/Mono.Debugger.Soft/Test/dtest.cs
mono/metadata/debug-mono-symfile.c
mono/metadata/debug-mono-symfile.h
mono/mini/debugger-agent.c

index 904d24c434aa64d67c781eb7b96c6c99e42c3c18..98116a60518dddeb496809626adde5b947ede1fa 100644 (file)
@@ -36,9 +36,9 @@ namespace Mono.Debugger.Soft
 
        class DebugInfo {
                public int max_il_offset;
-               public string filename;
                public int[] il_offsets;
                public int[] line_numbers;
+               public string[] source_files;
        }
 
        struct FrameInfo {
@@ -363,7 +363,7 @@ namespace Mono.Debugger.Soft
                 * with newer runtimes, and vice versa.
                 */
                internal const int MAJOR_VERSION = 2;
-               internal const int MINOR_VERSION = 12;
+               internal const int MINOR_VERSION = 13;
 
                enum WPSuspendPolicy {
                        NONE = 0,
@@ -1635,14 +1635,31 @@ namespace Mono.Debugger.Soft
 
                        DebugInfo info = new DebugInfo ();
                        info.max_il_offset = res.ReadInt ();
-                       info.filename = res.ReadString ();
+
+                       string[] filenames = null;
+                       if (Version.AtLeast (2, 12)) {
+                               int n = res.ReadInt ();
+                               filenames = new string [n];
+                               for (int i = 0; i < n; ++i)
+                                       filenames [i] = res.ReadString ();
+                       } else {
+                               filenames = new string [1];
+                               filenames [0] = res.ReadString ();
+                       }
 
                        int n_il_offsets = res.ReadInt ();
                        info.il_offsets = new int [n_il_offsets];
                        info.line_numbers = new int [n_il_offsets];
+                       info.source_files = new string [n_il_offsets];
                        for (int i = 0; i < n_il_offsets; ++i) {
                                info.il_offsets [i] = res.ReadInt ();
                                info.line_numbers [i] = res.ReadInt ();
+                               if (Version.AtLeast (2, 12)) {
+                                       int idx = res.ReadInt ();
+                                       info.source_files [i] = idx >= 0 ? filenames [idx] : null;
+                               } else {
+                                       info.source_files [i] = filenames [0];
+                               }
                        }
 
                        return info;
index b53ae37a5ebe0e3844c87cf4f862523d342a5b5a..053c91db10371c124a111c973cd24f01507eb377 100644 (file)
@@ -275,7 +275,7 @@ namespace Mono.Debugger.Soft
                        get {
                                if (debug_info == null)
                                        debug_info = vm.conn.Method_GetDebugInfo (id);
-                               return debug_info.filename;
+                               return debug_info.source_files.Length > 0 ? debug_info.source_files [0] : null;
                        }
                }
 
@@ -286,21 +286,24 @@ namespace Mono.Debugger.Soft
                                        var line_numbers = LineNumbers;
                                        IList<Location> res = new Location [ILOffsets.Count];
                                        for (int i = 0; i < il_offsets.Count; ++i)
-                                               res [i] = new Location (vm, this, -1, il_offsets [i], SourceFile, line_numbers [i], 0);
+                                               res [i] = new Location (vm, this, -1, il_offsets [i], debug_info.source_files [i], line_numbers [i], 0);
                                        locations = res;
                                }
                                return locations;
                        }
                }                               
 
-               internal int il_offset_to_line_number (int il_offset) {
+               internal int il_offset_to_line_number (int il_offset, out string src_file) {
                        if (debug_info == null)
                                debug_info = vm.conn.Method_GetDebugInfo (id);
 
                        // FIXME: Optimize this
+                       src_file = null;
                        for (int i = debug_info.il_offsets.Length - 1; i >= 0; --i) {
-                               if (debug_info.il_offsets [i] <= il_offset)
+                               if (debug_info.il_offsets [i] <= il_offset) {
+                                       src_file = debug_info.source_files [i];
                                        return debug_info.line_numbers [i];
+                               }
                        }
                        return -1;
                }
index 71fa126645f7e7f715d661e216b3e460184609bb..3f6cf7fc97a369c6c46a5e3707026f4a8c9eabac 100644 (file)
@@ -42,13 +42,14 @@ namespace Mono.Debugger.Soft
                        get {
                                if (location == null) {
                                        int line_number;
+                                       string src_file = null;
 
                                        if (il_offset == -1)
                                                line_number = -1;
                                        else
-                                               line_number = method.il_offset_to_line_number (il_offset);
+                                               line_number = method.il_offset_to_line_number (il_offset, out src_file);
 
-                                       location = new Location (vm, Method, 0, il_offset, method.SourceFile, line_number, 0);
+                                       location = new Location (vm, Method, 0, il_offset, src_file != null ? src_file : method.SourceFile, line_number, 0);
                                }
                                return location;
                        }
index 707cfdeab5bb832ed6bc56ff97fc6d04566d0c43..abcc873395ecfba0cb35eb4c73511a06c6df2e56 100644 (file)
@@ -1029,5 +1029,9 @@ public class LineNumbers
 
        [MethodImplAttribute (MethodImplOptions.NoInlining)]
        public static void ln3 () {
+#pragma warning disable 0219
+               int i = 5;
+#pragma warning restore 0219
+               #line 55 "FOO"
        }
 }
index 31403b88fae4635a09227c3a3bd282e3c3afd5dd..175b6f37c769a91abb086698705ff7b4782211b3 100644 (file)
@@ -1561,6 +1561,7 @@ public class DebuggerTests
        }
 
        [Test]
+       [Category("only88")]
        public void LineNumbers () {
                Event e = run_until ("line_numbers");
 
@@ -1601,7 +1602,15 @@ public class DebuggerTests
                Assert.IsTrue (e is StepEvent);
                l = e.Thread.GetFrames ()[0].Location;
                Assert.AreEqual ("ln3", l.Method.Name);
-               Assert.AreEqual (line_base + 10, l.LineNumber);
+               Assert.AreEqual (line_base + 11, l.LineNumber);
+
+               vm.Resume ();
+               e = GetNextEvent ();
+               Assert.IsTrue (e is StepEvent);
+               l = e.Thread.GetFrames ()[0].Location;
+               Assert.AreEqual ("ln3", l.Method.Name);
+               Assert.IsTrue (l.SourceFile.EndsWith ("FOO"));
+               Assert.AreEqual (55, l.LineNumber);
 
                vm.Resume ();
                e = GetNextEvent ();
@@ -1612,12 +1621,14 @@ public class DebuggerTests
 
                // GetSourceFiles ()
                string[] sources = l.Method.DeclaringType.GetSourceFiles ();
-               Assert.AreEqual (1, sources.Length);
+               Assert.AreEqual (2, sources.Length);
                Assert.AreEqual ("dtest-app.cs", sources [0]);
+               Assert.AreEqual ("FOO", sources [1]);
 
                sources = l.Method.DeclaringType.GetSourceFiles (true);
-               Assert.AreEqual (1, sources.Length);
+               Assert.AreEqual (2, sources.Length);
                Assert.IsTrue (sources [0].EndsWith ("dtest-app.cs"));
+               Assert.IsTrue (sources [1].EndsWith ("FOO"));
        }
 
        [Test]
@@ -2724,6 +2735,7 @@ public class DebuggerTests
        }
 
        [Test]
+       [Category ("only88")]
        public void TypeLoadSourceFileFilter () {
                Event e = run_until ("type_load");
 
index 3cd177283650a25033d2279b82647b83a8612027..feb2949612c87b3d86393badec42957a3d10b06d 100644 (file)
@@ -390,11 +390,12 @@ mono_debug_symfile_lookup_location (MonoDebugMethodInfo *minfo, uint32_t offset)
 }
 
 static void
-add_line (StatementMachine *stm, GPtrArray *il_offset_array, GPtrArray *line_number_array)
+add_line (StatementMachine *stm, GPtrArray *il_offset_array, GPtrArray *line_number_array, GPtrArray *source_file_array)
 {
        if (stm->line > 0) {
                g_ptr_array_add (il_offset_array, GUINT_TO_POINTER (stm->offset));
                g_ptr_array_add (line_number_array, GUINT_TO_POINTER (stm->line));
+               g_ptr_array_add (source_file_array, GUINT_TO_POINTER (stm->file));
        }
 
        if (!stm->is_hidden && !stm->first_file)
@@ -415,30 +416,36 @@ mono_debug_symfile_free_location   (MonoDebugSourceLocation  *location)
 }
 
 /*
- * mono_debug_symfile_get_line_numbers:
+ * mono_debug_symfile_get_line_numbers_full:
  *
- *   All the output parameters can be NULL.
- */ 
+ * On return, SOURCE_FILE_LIST will point to a GPtrArray of malloc-ed strings, and
+ * SOURCE_FILES will contain indexes into this array.
+ */
 void
-mono_debug_symfile_get_line_numbers (MonoDebugMethodInfo *minfo, char **source_file, int *n_il_offsets, int **il_offsets, int **line_numbers)
+mono_debug_symfile_get_line_numbers_full (MonoDebugMethodInfo *minfo, char **source_file, GPtrArray **source_file_list, int *n_il_offsets, int **il_offsets, int **line_numbers, int **source_files)
 {
        // FIXME: Unify this with mono_debug_symfile_lookup_location
        MonoSymbolFile *symfile;
        const unsigned char *ptr;
        StatementMachine stm;
        uint32_t i;
-       GPtrArray *il_offset_array, *line_number_array;
+       GPtrArray *il_offset_array, *line_number_array, *source_file_array;
 
-       if (source_file)
-               *source_file = NULL;
+       if (source_file_list)
+               *source_file_list = NULL;
        if (n_il_offsets)
                *n_il_offsets = 0;
+       if (source_files)
+               *source_files = NULL;
+       if (source_file)
+               *source_file = NULL;
 
        if ((symfile = minfo->handle->symfile) == NULL)
                return;
 
        il_offset_array = g_ptr_array_new ();
        line_number_array = g_ptr_array_new ();
+       source_file_array = g_ptr_array_new ();
 
        stm.line_base = read32 (&symfile->offset_table->_line_number_table_line_base);
        stm.line_range = read32 (&symfile->offset_table->_line_number_table_line_range);
@@ -471,7 +478,7 @@ mono_debug_symfile_get_line_numbers (MonoDebugMethodInfo *minfo, char **source_f
                                if (il_offset_array->len == 0)
                                        /* Empty table */
                                        break;
-                               add_line (&stm, il_offset_array, line_number_array);
+                               add_line (&stm, il_offset_array, line_number_array, source_file_array);
                                break;
                        } else if (opcode == DW_LNE_MONO_negate_is_hidden) {
                                stm.is_hidden = !stm.is_hidden;
@@ -487,7 +494,7 @@ mono_debug_symfile_get_line_numbers (MonoDebugMethodInfo *minfo, char **source_f
                } else if (opcode < stm.opcode_base) {
                        switch (opcode) {
                        case DW_LNS_copy:
-                               add_line (&stm, il_offset_array, line_number_array);
+                               add_line (&stm, il_offset_array, line_number_array, source_file_array);
                                break;
                        case DW_LNS_advance_pc:
                                stm.offset += read_leb128 (ptr, &ptr);
@@ -511,14 +518,14 @@ mono_debug_symfile_get_line_numbers (MonoDebugMethodInfo *minfo, char **source_f
                        stm.offset += opcode / stm.line_range;
                        stm.line += stm.line_base + (opcode % stm.line_range);
 
-                       add_line (&stm, il_offset_array, line_number_array);
+                       add_line (&stm, il_offset_array, line_number_array, source_file_array);
                }
        }
 
        if (!stm.file && stm.first_file)
                stm.file = stm.first_file;
 
-       if (stm.file) {
+       if (stm.file && source_file) {
                int offset = read32(&(stm.symfile->offset_table->_source_table_offset)) +
                        (stm.file - 1) * sizeof (MonoSymbolFileSourceEntry);
                MonoSymbolFileSourceEntry *se = (MonoSymbolFileSourceEntry *)
@@ -528,6 +535,40 @@ mono_debug_symfile_get_line_numbers (MonoDebugMethodInfo *minfo, char **source_f
                        *source_file = read_string (stm.symfile->raw_contents + read32(&(se->_data_offset)));
        }
 
+       if (source_file_list) {
+               int file, last_file = 0;
+               char *s;
+
+               *source_file_list = g_ptr_array_new ();
+               if (source_files)
+                       *source_files = g_malloc (il_offset_array->len * sizeof (int));
+
+               for (i = 0; i < il_offset_array->len; ++i) {
+                       file = GPOINTER_TO_UINT (g_ptr_array_index (source_file_array, i));
+                       if (file && file != last_file) {
+                               int offset = read32(&(stm.symfile->offset_table->_source_table_offset)) +
+                                       (file - 1) * sizeof (MonoSymbolFileSourceEntry);
+                               MonoSymbolFileSourceEntry *se = (MonoSymbolFileSourceEntry *)
+                                       (stm.symfile->raw_contents + offset);
+
+                               s = read_string (stm.symfile->raw_contents + read32(&(se->_data_offset)));
+                               g_ptr_array_add (*source_file_list, s);
+                       }
+                       last_file = file;
+                       if (source_files)
+                               (*source_files) [i] = (*source_file_list)->len - 1;
+               }
+               if ((*source_file_list)->len == 0 && stm.file) {
+                       int offset = read32(&(stm.symfile->offset_table->_source_table_offset)) +
+                               (stm.file - 1) * sizeof (MonoSymbolFileSourceEntry);
+                       MonoSymbolFileSourceEntry *se = (MonoSymbolFileSourceEntry *)
+                               (stm.symfile->raw_contents + offset);
+
+                       s = read_string (stm.symfile->raw_contents + read32(&(se->_data_offset)));
+                       g_ptr_array_add (*source_file_list, s);
+               }
+       }                               
+
        if (n_il_offsets)
                *n_il_offsets = il_offset_array->len;
        if (il_offsets && line_numbers) {
@@ -545,6 +586,17 @@ mono_debug_symfile_get_line_numbers (MonoDebugMethodInfo *minfo, char **source_f
        return;
 }
 
+/*
+ * mono_debug_symfile_get_line_numbers:
+ *
+ *   All the output parameters can be NULL.
+ */ 
+void
+mono_debug_symfile_get_line_numbers (MonoDebugMethodInfo *minfo, char **source_file, int *n_il_offsets, int **il_offsets, int **line_numbers)
+{
+       mono_debug_symfile_get_line_numbers_full (minfo, source_file, NULL, n_il_offsets, il_offsets, line_numbers, NULL);
+}
+       
 int32_t
 _mono_debug_address_from_il_offset (MonoDebugMethodJitInfo *jit, uint32_t il_offset)
 {
index e36753b1a67c159b516eafdacddab4cfa5088147..a6f5a4ac971b6508b1d4f032a058ee6495d293cc 100644 (file)
@@ -6,6 +6,7 @@
 #ifndef __MONO_DEBUG_MONO_SYMFILE_H__
 #define __MONO_DEBUG_MONO_SYMFILE_H__
 
+#include <glib.h>
 #include <mono/metadata/class.h>
 #include <mono/metadata/reflection.h>
 #include <mono/metadata/mono-debug.h>
@@ -147,6 +148,9 @@ mono_debug_symfile_free_locals (MonoDebugLocalsInfo *info);
 void
 mono_debug_symfile_get_line_numbers (MonoDebugMethodInfo *minfo, char **source_file, int *n_il_offsets, int **il_offsets, int **line_numbers);
 
+void
+mono_debug_symfile_get_line_numbers_full (MonoDebugMethodInfo *minfo, char **source_file, GPtrArray **source_file_list, int *n_il_offsets, int **il_offsets, int **line_numbers, int **source_files);
+
 MONO_END_DECLS
 
 #endif /* __MONO_SYMFILE_H__ */
index a8776fb4828b6ab5431e7dd626d9bf8f25450bfd..8ac47c81a8f970cd7ca8a4b311186e3436d4caef 100644 (file)
@@ -270,7 +270,7 @@ typedef struct {
 #define HEADER_LENGTH 11
 
 #define MAJOR_VERSION 2
-#define MINOR_VERSION 12
+#define MINOR_VERSION 13
 
 typedef enum {
        CMD_SET_VM = 1,
@@ -3218,24 +3218,27 @@ create_event_list (EventKind event, GPtrArray *reqs, MonoJitInfo *ji, EventInfo
                                        MonoMethod *method;
                                        char *source_file, *s;
                                        gboolean found = FALSE;
+                                       int i;
+                                       GPtrArray *source_file_list;
 
                                        while ((method = mono_class_get_methods (ei->klass, &iter))) {
                                                MonoDebugMethodInfo *minfo = mono_debug_lookup_method (method);
 
                                                if (minfo) {
-                                                       mono_debug_symfile_get_line_numbers (minfo, &source_file, NULL, NULL, NULL);
-                                                       if (!source_file)
-                                                               continue;
-                                                       // FIXME: flags
-                                                       /*
-                                                        * Do a case-insesitive match by converting the file name to
-                                                        * lowercase.
-                                                        */
-                                                       s = strdup_tolower (source_file);
-                                                       if (g_hash_table_lookup (mod->data.source_files, s))
-                                                               found = TRUE;
-                                                       g_free (s);
-                                                       g_free (source_file);
+                                                       mono_debug_symfile_get_line_numbers_full (minfo, &source_file, &source_file_list, NULL, NULL, NULL, NULL);
+                                                       for (i = 0; i < source_file_list->len; ++i) {
+                                                               source_file = g_ptr_array_index (source_file_list, i);
+                                                               /*
+                                                                * Do a case-insesitive match by converting the file name to
+                                                                * lowercase.
+                                                                */
+                                                               s = strdup_tolower (source_file);
+                                                               if (g_hash_table_lookup (mod->data.source_files, s))
+                                                                       found = TRUE;
+                                                               g_free (s);
+                                                               g_free (source_file);
+                                                       }
+                                                       g_ptr_array_free (source_file_list, TRUE);
                                                }
                                        }
                                        if (!found)
@@ -5930,24 +5933,26 @@ get_source_files_for_type (MonoClass *klass)
        MonoMethod *method;
        char *source_file;
        GPtrArray *files;
-       int i;
+       int i, j;
 
        files = g_ptr_array_new ();
 
        while ((method = mono_class_get_methods (klass, &iter))) {
                MonoDebugMethodInfo *minfo = mono_debug_lookup_method (method);
+               GPtrArray *source_file_list;
 
                if (minfo) {
-                       mono_debug_symfile_get_line_numbers (minfo, &source_file, NULL, NULL, NULL);
-                       if (!source_file)
-                               continue;
-
-                       for (i = 0; i < files->len; ++i)
-                               if (!strcmp (g_ptr_array_index (files, i), source_file))
-                                       break;
-                       if (i == files->len)
-                               g_ptr_array_add (files, g_strdup (source_file));
-                       g_free (source_file);
+                       mono_debug_symfile_get_line_numbers_full (minfo, NULL, &source_file_list, NULL, NULL, NULL, NULL);
+                       for (j = 0; j < source_file_list->len; ++j) {
+                               source_file = g_ptr_array_index (source_file_list, j);
+                               for (i = 0; i < files->len; ++i)
+                                       if (!strcmp (g_ptr_array_index (files, i), source_file))
+                                               break;
+                               if (i == files->len)
+                                       g_ptr_array_add (files, g_strdup (source_file));
+                               g_free (source_file);
+                       }
+                       g_ptr_array_free (source_file_list, TRUE);
                }
        }
 
@@ -7326,6 +7331,8 @@ method_commands_internal (int command, MonoMethod *method, MonoDomain *domain, g
                int i, n_il_offsets;
                int *il_offsets;
                int *line_numbers;
+               int *source_files;
+               GPtrArray *source_file_list;
 
                header = mono_method_get_header (method);
                if (!header) {
@@ -7344,19 +7351,32 @@ method_commands_internal (int command, MonoMethod *method, MonoDomain *domain, g
                        break;
                }
 
-               mono_debug_symfile_get_line_numbers (minfo, &source_file, &n_il_offsets, &il_offsets, &line_numbers);
+               mono_debug_symfile_get_line_numbers_full (minfo, &source_file, &source_file_list, &n_il_offsets, &il_offsets, &line_numbers, &source_files);
                buffer_add_int (buf, header->code_size);
-               buffer_add_string (buf, source_file);
+               if (CHECK_PROTOCOL_VERSION (2, 13)) {
+                       buffer_add_int (buf, source_file_list->len);
+                       for (i = 0; i < source_file_list->len; ++i)
+                               buffer_add_string (buf, g_ptr_array_index (source_file_list, i));
+               } else {
+                       buffer_add_string (buf, source_file);
+               }
                buffer_add_int (buf, n_il_offsets);
                DEBUG (10, printf ("Line number table for method %s:\n", mono_method_full_name (method,  TRUE)));
                for (i = 0; i < n_il_offsets; ++i) {
-                       DEBUG (10, printf ("IL%x -> %d\n", il_offsets [i], line_numbers [i]));
+                       const char *srcfile = source_files [i] != -1 ? g_ptr_array_index (source_file_list, source_files [i]) : "";
+                       DEBUG (10, printf ("IL%x -> %s:%d\n", il_offsets [i], srcfile, line_numbers [i]));
                        buffer_add_int (buf, il_offsets [i]);
                        buffer_add_int (buf, line_numbers [i]);
+                       if (CHECK_PROTOCOL_VERSION (2, 13))
+                               buffer_add_int (buf, source_files [i]);
                }
                g_free (source_file);
                g_free (il_offsets);
                g_free (line_numbers);
+               g_free (source_files);
+               for (i = 0; i < source_file_list->len; ++i)
+                       g_free (g_ptr_array_index (source_file_list, i));
+               g_ptr_array_free (source_file_list, TRUE);
                mono_metadata_free_mh (header);
                break;
        }