Merge pull request #2802 from BrzVlad/feature-evacuation-opt2
[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 IKVM.Reflection;
6 using System.Diagnostics;
7 using System.Collections.Generic;
8 using Mono.CompilerServices.SymbolWriter;
9 using System.Runtime.InteropServices;
10
11 namespace Symbolicate
12 {
13         struct Location {
14                 public string FileName;
15                 public int Line;
16         }
17
18         class LocationProvider {
19                 class AssemblyLocationProvider {
20                         Assembly assembly;
21                         MonoSymbolFile symbolFile;
22                         string seqPointDataPath;
23
24                         public AssemblyLocationProvider (Assembly assembly, MonoSymbolFile symbolFile, string seqPointDataPath)
25                         {
26                                 this.assembly = assembly;
27                                 this.symbolFile = symbolFile;
28                                 this.seqPointDataPath = seqPointDataPath;
29                         }
30
31                         public bool TryGetLocation (string methodStr, string typeFullName, int offset, bool isOffsetIL, uint methodIndex, out Location location)
32                         {
33                                 location = default (Location);
34                                 if (symbolFile == null)
35                                         return false;
36
37                                 var type = assembly.GetTypes().FirstOrDefault (t => t.FullName == typeFullName);
38                                 if (type == null)
39                                         return false;
40
41                                 var bindingflags = BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic;
42                                 var method = type.GetMethods(bindingflags).FirstOrDefault (m => GetMethodFullName (m) == methodStr);
43                                 if (method == null)
44                                         return false;
45
46                                 int ilOffset = (isOffsetIL)? offset : GetILOffsetFromFile (method.MetadataToken, methodIndex, offset);
47                                 if (ilOffset < 0)
48                                         return false;
49
50                                 var methodSymbol = symbolFile.Methods [(method.MetadataToken & 0x00ffffff) - 1];
51
52                                 var lineNumbers = methodSymbol.GetLineNumberTable ().LineNumbers;
53                                 var lineNumber = lineNumbers.FirstOrDefault (l => l.Offset >= ilOffset) ?? lineNumbers.Last ();
54
55                                 location.FileName = symbolFile.Sources [lineNumber.File-1].FileName;
56                                 location.Line = lineNumber.Row;
57                                 return true;
58                         }
59
60                         SeqPointInfo seqPointInfo;
61                         private int GetILOffsetFromFile (int methodToken, uint methodIndex, int nativeOffset)
62                         {
63                                 if (seqPointInfo == null)
64                                         seqPointInfo = SeqPointInfo.Read (seqPointDataPath);
65
66                                 return seqPointInfo.GetILOffset (methodToken, methodIndex, nativeOffset);
67                         }
68
69                         private string GetMethodFullName (MethodBase m)
70                         {
71                                 StringBuilder sb = new StringBuilder ();
72
73                                 StackTraceHelper.GetFullNameForStackTrace (sb, m);
74
75                                 return sb.ToString ();
76                         }
77                 }
78
79                 static readonly Universe ikvm_reflection = new Universe ();
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 = ikvm_reflection.LoadFile (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 (symbolPath);
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