[corlib] Add partial EncodingHelper type to let XI define its own default encoding...
[mono.git] / mcs / tools / pdb2mdb / Driver.cs
index bdec17678153d8d8088615d8acd30a75274d2aff..93d9e8a4544535b8f1c568064365d818bebbf195 100644 (file)
@@ -21,17 +21,30 @@ using Mono.CompilerServices.SymbolWriter;
 
 namespace Pdb2Mdb {
 
-       class Converter {
+       public class Converter {
 
                MonoSymbolWriter mdb;
                Dictionary<string, SourceFile> files = new Dictionary<string, SourceFile> ();
 
-               public Converter (MonoSymbolWriter mdb)
+               public static void Convert (string filename)
+               {
+                       var asm = AssemblyDefinition.ReadAssembly (filename);
+
+                       var pdb = asm.Name.Name + ".pdb";
+                       pdb = Path.Combine (Path.GetDirectoryName (filename), pdb);
+
+                       using (var stream = File.OpenRead (pdb)) {
+                               var funcs = PdbFile.LoadFunctions (stream, true);
+                               Converter.Convert (asm, funcs, new MonoSymbolWriter (filename));
+                       }
+               }
+
+               internal Converter (MonoSymbolWriter mdb)
                {
                        this.mdb = mdb;
                }
 
-               public static void Convert (AssemblyDefinition assembly, IEnumerable<PdbFunction> functions, MonoSymbolWriter mdb)
+               internal static void Convert (AssemblyDefinition assembly, IEnumerable<PdbFunction> functions, MonoSymbolWriter mdb)
                {
                        var converter = new Converter (mdb);
 
@@ -43,6 +56,9 @@ namespace Pdb2Mdb {
 
                void ConvertFunction (PdbFunction function)
                {
+                       if (function.lines == null)
+                               return;
+
                        var method = new SourceMethod { Name = function.name, Token = (int) function.token };
 
                        var file = GetSourceFile (mdb, function);
@@ -58,15 +74,19 @@ namespace Pdb2Mdb {
 
                void ConvertSequencePoints (PdbFunction function, SourceFile file, SourceMethodBuilder builder)
                {
-                       if (function.lines == null)
-                               return;
-
-                       foreach (var line in function.lines.SelectMany (lines => lines.lines))
+                       int last_line = 0;
+                       foreach (var line in function.lines.SelectMany (lines => lines.lines)) {
+                               // 0xfeefee is an MS convention, we can't pass it into mdb files, so we use the last non-hidden line
+                               bool is_hidden = line.lineBegin == 0xfeefee;
                                builder.MarkSequencePoint (
                                        (int) line.offset,
                                        file.CompilationUnit.SourceFile,
-                                       (int) line.lineBegin,
-                                       (int) line.colBegin, line.lineBegin == 0xfeefee);
+                                       is_hidden ? last_line : (int) line.lineBegin,
+                                       (int) line.colBegin, is_hidden ? -1 : (int)line.lineEnd, is_hidden ? -1 : (int)line.colEnd,
+                                       is_hidden);
+                               if (!is_hidden)
+                                       last_line = (int) line.lineBegin;
+                       }
                }
 
                void ConvertVariables (PdbFunction function)
@@ -77,16 +97,20 @@ namespace Pdb2Mdb {
 
                void ConvertScope (PdbScope scope)
                {
-                       ConvertSlots (scope.slots);
+                       ConvertSlots (scope, scope.slots);
 
                        foreach (var s in scope.scopes)
                                ConvertScope (s);
                }
 
-               void ConvertSlots (IEnumerable<PdbSlot> slots)
+               void ConvertSlots (PdbScope scope, IEnumerable<PdbSlot> slots)
                {
-                       foreach (var slot in slots)
+                       int scope_idx = mdb.OpenScope ((int)scope.address);
+                       foreach (var slot in slots) {
                                mdb.DefineLocalVariable ((int) slot.slot, slot.name);
+                               mdb.DefineScopeVariable (scope_idx, (int)slot.slot);
+                       }
+                       mdb.CloseScope ((int)(scope.address + scope.length));
                }
 
                SourceFile GetSourceFile (MonoSymbolWriter mdb, PdbFunction function)
@@ -149,6 +173,7 @@ namespace Pdb2Mdb {
                        var assembly = AssemblyDefinition.ReadAssembly (asm);
 
                        var pdb = assembly.Name.Name + ".pdb";
+                       pdb = Path.Combine (Path.GetDirectoryName (asm), pdb);
 
                        if (!File.Exists (pdb))
                                Usage ();