6 using System.Diagnostics;
7 using System.Collections.Generic;
8 using Mono.CompilerServices.SymbolWriter;
9 using System.Runtime.InteropServices;
14 public string FileName;
18 class LocationProvider {
19 class AssemblyLocationProvider {
21 MonoSymbolFile symbolFile;
22 string seqPointDataPath;
24 public AssemblyLocationProvider (Assembly assembly, MonoSymbolFile symbolFile, string seqPointDataPath)
26 this.assembly = assembly;
27 this.symbolFile = symbolFile;
28 this.seqPointDataPath = seqPointDataPath;
31 public bool TryGetLocation (string methodStr, string typeFullName, int offset, bool isOffsetIL, uint methodIndex, out Location location)
33 location = default (Location);
34 if (symbolFile == null)
37 var type = assembly.GetTypes().FirstOrDefault (t => t.FullName == typeFullName);
41 var bindingflags = BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic;
42 var method = type.GetMethods(bindingflags).FirstOrDefault (m => GetMethodFullName (m) == methodStr);
46 int ilOffset = (isOffsetIL)? offset : GetILOffsetFromFile (method.MetadataToken, methodIndex, offset);
50 var methodSymbol = symbolFile.Methods [(method.MetadataToken & 0x00ffffff) - 1];
52 var lineNumbers = methodSymbol.GetLineNumberTable ().LineNumbers;
53 var lineNumber = lineNumbers.FirstOrDefault (l => l.Offset >= ilOffset) ?? lineNumbers.Last ();
55 location.FileName = symbolFile.Sources [lineNumber.File-1].FileName;
56 location.Line = lineNumber.Row;
60 SeqPointInfo seqPointInfo;
61 private int GetILOffsetFromFile (int methodToken, uint methodIndex, int nativeOffset)
63 if (seqPointInfo == null)
64 seqPointInfo = SeqPointInfo.Read (seqPointDataPath);
66 return seqPointInfo.GetILOffset (methodToken, methodIndex, nativeOffset);
69 private string GetMethodFullName (MethodBase m)
71 StringBuilder sb = new StringBuilder ();
73 StackTraceHelper.GetFullNameForStackTrace (sb, m);
75 return sb.ToString ();
79 static readonly Universe ikvm_reflection = new Universe ();
80 Dictionary<string, AssemblyLocationProvider> assemblies;
81 HashSet<string> directories;
83 public LocationProvider () {
84 assemblies = new Dictionary<string, AssemblyLocationProvider> ();
85 directories = new HashSet<string> ();
88 public void AddAssembly (string assemblyPath)
90 assemblyPath = Path.GetFullPath (assemblyPath);
91 if (assemblies.ContainsKey (assemblyPath))
94 if (!File.Exists (assemblyPath))
95 throw new ArgumentException ("assemblyPath does not exist: "+ assemblyPath);
97 var assembly = ikvm_reflection.LoadFile (assemblyPath);
98 MonoSymbolFile symbolFile = null;
100 var symbolPath = assemblyPath + ".mdb";
101 if (!File.Exists (symbolPath))
102 Debug.WriteLine (".mdb file was not found for " + assemblyPath);
104 symbolFile = MonoSymbolFile.ReadSymbolFile (symbolPath);
106 var seqPointDataPath = assemblyPath + ".msym";
107 if (!File.Exists (seqPointDataPath))
108 seqPointDataPath = null;
110 assemblies.Add (assemblyPath, new AssemblyLocationProvider (assembly, symbolFile, seqPointDataPath));
112 directories.Add (Path.GetDirectoryName (assemblyPath));
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))
120 refPath = Path.Combine (dir, assemblyRef.Name + ".dll");
121 if (File.Exists (refPath))
123 refPath = Path.Combine (dir, assemblyRef.Name + ".exe");
124 if (File.Exists (refPath))
129 AddAssembly (refPath);
133 public void AddDirectory (string directory)
135 directory = Path.GetFullPath (directory);
136 if (!Directory.Exists (directory)) {
137 Console.Error.WriteLine ("Directory " + directory + " does not exist.");
141 directories.Add (directory);
144 public bool TryGetLocation (string method, string typeFullName, int offset, bool isOffsetIL, uint methodIndex, out Location location)
146 location = default (Location);
147 foreach (var assembly in assemblies.Values) {
148 if (assembly.TryGetLocation (method, typeFullName, offset, isOffsetIL, methodIndex, out location))