5 using System.Reflection;
6 using System.Diagnostics;
7 using System.Collections.Generic;
9 using Mono.CompilerServices.SymbolWriter;
10 using System.Runtime.InteropServices;
15 public string FileName;
19 class LocationProvider {
20 class AssemblyLocationProvider {
22 MonoSymbolFile symbolFile;
23 string seqPointDataPath;
25 public AssemblyLocationProvider (Assembly assembly, MonoSymbolFile symbolFile, string seqPointDataPath)
27 this.assembly = assembly;
28 this.symbolFile = symbolFile;
29 this.seqPointDataPath = seqPointDataPath;
32 public bool TryGetLocation (string methodStr, string typeFullName, int offset, bool isOffsetIL, uint methodIndex, out Location location)
34 location = default (Location);
35 if (symbolFile == null)
38 var type = assembly.GetTypes().FirstOrDefault (t => t.FullName == typeFullName);
42 var bindingflags = BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic;
43 var method = type.GetMethods(bindingflags).FirstOrDefault (m => GetMethodFullName (m) == methodStr);
47 int ilOffset = (isOffsetIL)? offset : GetILOffsetFromFile (method.MetadataToken, methodIndex, offset);
51 var methodSymbol = symbolFile.Methods [(method.MetadataToken & 0x00ffffff) - 1];
53 var lineNumbers = methodSymbol.GetLineNumberTable ().LineNumbers;
54 var lineNumber = lineNumbers.FirstOrDefault (l => l.Offset >= ilOffset) ?? lineNumbers.Last ();
56 location.FileName = symbolFile.Sources [lineNumber.File-1].FileName;
57 location.Line = lineNumber.Row;
61 SeqPointInfo seqPointInfo;
62 private int GetILOffsetFromFile (int methodToken, uint methodIndex, int nativeOffset)
64 if (seqPointInfo == null)
65 seqPointInfo = SeqPointInfo.Read (seqPointDataPath);
67 return seqPointInfo.GetILOffset (methodToken, methodIndex, nativeOffset);
70 private string GetMethodFullName (MethodBase m)
72 StringBuilder sb = new StringBuilder ();
74 StackTraceHelper.GetFullNameForStackTrace (sb, m);
76 return sb.ToString ();
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 = Assembly.ReflectionOnlyLoadFrom (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 (assemblyPath + ".mdb");
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))