Merge pull request #2698 from esdrubal/iosxmlarray
[mono.git] / mcs / tools / mono-symbolicate / LocationProvider.cs
1 using System;
2 using System.IO;
3 using System.Linq;
4 using System.Text;
5 using System.Reflection;
6 using System.Diagnostics;
7 using System.Collections.Generic;
8 using Mono.Cecil;
9 using Mono.CompilerServices.SymbolWriter;
10 using System.Runtime.InteropServices;
11
12 namespace Symbolicate
13 {
14         struct Location {
15                 public string FileName;
16                 public int Line;
17         }
18
19         class LocationProvider {
20                 class AssemblyLocationProvider {
21                         Assembly assembly;
22                         MonoSymbolFile symbolFile;
23                         string seqPointDataPath;
24
25                         public AssemblyLocationProvider (Assembly assembly, MonoSymbolFile symbolFile, string seqPointDataPath)
26                         {
27                                 this.assembly = assembly;
28                                 this.symbolFile = symbolFile;
29                                 this.seqPointDataPath = seqPointDataPath;
30                         }
31
32                         public bool TryGetLocation (string methodStr, string typeFullName, int offset, bool isOffsetIL, uint methodIndex, out Location location)
33                         {
34                                 location = default (Location);
35                                 if (symbolFile == null)
36                                         return false;
37
38                                 var type = assembly.GetTypes().FirstOrDefault (t => t.FullName == typeFullName);
39                                 if (type == null)
40                                         return false;
41
42                                 var bindingflags = BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic;
43                                 var method = type.GetMethods(bindingflags).FirstOrDefault (m => GetMethodFullName (m) == methodStr);
44                                 if (method == null)
45                                         return false;
46
47                                 int ilOffset = (isOffsetIL)? offset : GetILOffsetFromFile (method.MetadataToken, methodIndex, offset);
48                                 if (ilOffset < 0)
49                                         return false;
50
51                                 var methodSymbol = symbolFile.Methods [(method.MetadataToken & 0x00ffffff) - 1];
52
53                                 var lineNumbers = methodSymbol.GetLineNumberTable ().LineNumbers;
54                                 var lineNumber = lineNumbers.FirstOrDefault (l => l.Offset >= ilOffset) ?? lineNumbers.Last ();
55
56                                 location.FileName = symbolFile.Sources [lineNumber.File-1].FileName;
57                                 location.Line = lineNumber.Row;
58                                 return true;
59                         }
60
61                         SeqPointInfo seqPointInfo;
62                         private int GetILOffsetFromFile (int methodToken, uint methodIndex, int nativeOffset)
63                         {
64                                 if (seqPointInfo == null)
65                                         seqPointInfo = SeqPointInfo.Read (seqPointDataPath);
66
67                                 return seqPointInfo.GetILOffset (methodToken, methodIndex, nativeOffset);
68                         }
69
70                         private string GetMethodFullName (MethodBase m)
71                         {
72                                 StringBuilder sb = new StringBuilder ();
73
74                                 StackTraceHelper.GetFullNameForStackTrace (sb, m);
75
76                                 return sb.ToString ();
77                         }
78                 }
79
80                 Dictionary<string, AssemblyLocationProvider> assemblies;
81                 HashSet<string> directories;
82
83                 public LocationProvider () {
84                         assemblies = new Dictionary<string, AssemblyLocationProvider> ();
85                         directories = new HashSet<string> ();
86                 }
87
88                 public void AddAssembly (string assemblyPath)
89                 {
90                         assemblyPath = Path.GetFullPath (assemblyPath);
91                         if (assemblies.ContainsKey (assemblyPath))
92                                 return;
93
94                         if (!File.Exists (assemblyPath))
95                                 throw new ArgumentException ("assemblyPath does not exist: "+ assemblyPath);
96
97                         var assembly = Assembly.ReflectionOnlyLoadFrom (assemblyPath);
98                         MonoSymbolFile symbolFile = null;
99
100                         var symbolPath = assemblyPath + ".mdb";
101                         if (!File.Exists (symbolPath))
102                                 Debug.WriteLine (".mdb file was not found for " + assemblyPath);
103                         else
104                                 symbolFile = MonoSymbolFile.ReadSymbolFile (assemblyPath + ".mdb");
105
106                         var seqPointDataPath = assemblyPath + ".msym";
107                         if (!File.Exists (seqPointDataPath))
108                                 seqPointDataPath = null;
109
110                         assemblies.Add (assemblyPath, new AssemblyLocationProvider (assembly, symbolFile, seqPointDataPath));
111
112                         directories.Add (Path.GetDirectoryName (assemblyPath));
113
114                         foreach (var assemblyRef in assembly.GetReferencedAssemblies ()) {
115                                 string refPath = null;
116                                 foreach (var dir in directories) {
117                                         refPath = Path.Combine (dir, assemblyRef.Name);
118                                         if (File.Exists (refPath))
119                                                 break;
120                                         refPath = Path.Combine (dir, assemblyRef.Name + ".dll");
121                                         if (File.Exists (refPath))
122                                                 break;
123                                         refPath = Path.Combine (dir, assemblyRef.Name + ".exe");
124                                         if (File.Exists (refPath))
125                                                 break;
126                                         refPath = null;
127                                 }
128                                 if (refPath != null)
129                                         AddAssembly (refPath);
130                         }
131                 }
132
133                 public void AddDirectory (string directory)
134                 {
135                         directory = Path.GetFullPath (directory);
136                         if (!Directory.Exists (directory)) {
137                                 Console.Error.WriteLine ("Directory " + directory + " does not exist.");
138                                 return;
139                         }
140
141                         directories.Add (directory);
142                 }
143
144                 public bool TryGetLocation (string method, string typeFullName, int offset, bool isOffsetIL, uint methodIndex, out Location location)
145                 {
146                         location = default (Location);
147                         foreach (var assembly in assemblies.Values) {
148                                 if (assembly.TryGetLocation (method, typeFullName, offset, isOffsetIL, methodIndex, out location))
149                                         return true;
150                         }
151
152                         return false;
153                 }
154         }
155 }
156