Debian Codegraph @

mcs/tools/mono-symbolicate/SymbolManager.cs

raw
1 using System;
2 using System.IO;
3 using System.Linq;
4 using System.Text;
5 using System.Collections.Generic;
6 using Mono.Cecil;
7 using Mono.Cecil.Cil;
8 using Mono.Collections.Generic;
9
10 namespace Mono
11 {
12 // Should be (string File, int Line) but need to bump monodroid_tools to 4.7
13 readonly struct Location
14 {
15 public readonly string File;
16 public readonly int Line;
17
18 public Location (string file, int line)
19 {
20 File = file;
21 Line = line;
22 }
23 }
24
25 public class SymbolManager
26 {
27 string msymDir;
28 Logger logger;
29
30 public SymbolManager (string msymDir, Logger logger) {
31 this.msymDir = msymDir;
32 this.logger = logger;
33 }
34
35 internal bool TryResolveLocation (StackFrameData sfData, out Location location)
36 {
37 if (sfData.Mvid == null) {
38 location = default;
39 return false;
40 }
41
42 var assemblyLocProvider = GetOrCreateAssemblyLocationProvider (sfData.Mvid);
43 if (assemblyLocProvider == null) {
44 location = default;
45 return false;
46 }
47
48 SeqPointInfo seqPointInfo = null;
49 if (!sfData.IsILOffset && sfData.Aotid != null)
50 seqPointInfo = GetOrCreateSeqPointInfo (sfData.Aotid);
51
52
53 return assemblyLocProvider.TryResolveLocation (sfData, seqPointInfo, out location);
54 }
55
56 Dictionary<string, AssemblyLocationProvider> assemblies = new Dictionary<string, AssemblyLocationProvider> ();
57
58 private AssemblyLocationProvider GetOrCreateAssemblyLocationProvider (string mvid)
59 {
60 if (assemblies.ContainsKey (mvid))
61 return assemblies[mvid];
62
63 var mvidDir = Path.Combine (msymDir, mvid);
64 if (!Directory.Exists (mvidDir)) {
65 logger.LogWarning ("MVID directory does not exist: {0}", mvidDir);
66 return null;
67 }
68
69 string assemblyPath = null;
70 var exeFiles = Directory.GetFiles (mvidDir, "*.exe");
71 var dllFiles = Directory.GetFiles (mvidDir, "*.dll");
72
73 if (exeFiles.Length + dllFiles.Length != 1) {
74 logger.LogError ("MVID directory should include one assembly: {0}", mvidDir);
75 return null;
76 }
77
78 assemblyPath = exeFiles.Length > 0 ? exeFiles[0] : dllFiles[0];
79
80 var locProvider = new AssemblyLocationProvider (assemblyPath, logger);
81
82 assemblies.Add (mvid, locProvider);
83
84 return locProvider;
85 }
86
87 Dictionary<string, SeqPointInfo> seqPointInfos = new Dictionary<string, SeqPointInfo> ();
88
89 private SeqPointInfo GetOrCreateSeqPointInfo (string aotid)
90 {
91 if (seqPointInfos.ContainsKey (aotid))
92 return seqPointInfos[aotid];
93
94 var aotidDir = Path.Combine (msymDir, aotid);
95 if (!Directory.Exists (aotidDir)) {
96 logger.LogError ("AOTID directory does not exist: {0}", aotidDir);
97 return null;
98 }
99
100 string msymFile = null;
101 var msymFiles = Directory.GetFiles(aotidDir, "*.msym");
102 msymFile = msymFiles[0];
103
104 var seqPointInfo = SeqPointInfo.Read (msymFile);
105
106 seqPointInfos.Add (aotid, seqPointInfo);
107
108 return seqPointInfo;
109 }
110
111 public void StoreSymbols (params string[] lookupDirs)
112 {
113 foreach (var dir in lookupDirs) {
114 var exeFiles = Directory.GetFiles (dir, "*.exe");
115 var dllFiles = Directory.GetFiles (dir, "*.dll");
116 var assemblies = exeFiles.Concat (dllFiles);
117 foreach (var assemblyPath in assemblies) {
118
119 // TODO: Ignore embedded pdb
120 var symbolFile = GetSymbolFile (assemblyPath);
121
122 if (symbolFile == null) {
123 logger.LogWarning ("Directory {0} contains {1} but no debug symbols file was found.", dir, Path.GetFileName (assemblyPath));
124 // assemblies without debug symbols are useless
125 continue;
126 }
127
128 string mvidDir;
129 using (var assembly = AssemblyDefinition.ReadAssembly (assemblyPath)) {
130 var mvid = assembly.MainModule.Mvid.ToString ("N");
131 mvidDir = Path.Combine (msymDir, mvid);
132 }
133
134 if (Directory.Exists (mvidDir)) {
135 try {
136 Directory.Delete (mvidDir, true);
137 } catch (DirectoryNotFoundException) {}
138 }
139
140 Directory.CreateDirectory (mvidDir);
141
142 var mvidAssemblyPath = Path.Combine (mvidDir, Path.GetFileName (assemblyPath));
143 File.Copy (assemblyPath, mvidAssemblyPath);
144
145 var mvidDebugPath = Path.Combine (mvidDir, Path.GetFileName (symbolFile));
146 File.Copy (symbolFile, mvidDebugPath);
147
148 // TODO create MVID dir for non main modules with links to main module MVID
149 }
150 }
151 }
152
153 static string GetSymbolFile (string assembly)
154 {
155 var pdbName = Path.ChangeExtension (assembly, "pdb");
156 if (File.Exists (pdbName))
157 return pdbName;
158
159 var mdbName = assembly + ".mdb";
160
161 if (File.Exists (mdbName))
162 return mdbName;
163
164 return null;
165 }
166 }
167 }