static ModuleDefinition RoundtripModule(ModuleDefinition module, RoundtripType roundtripType) { if (roundtripType == RoundtripType.None) { return(module); } var file = Path.Combine(Path.GetTempPath(), "TestILProcessor.dll"); if (File.Exists(file)) { File.Delete(file); } ISymbolWriterProvider symbolWriterProvider; switch (roundtripType) { case RoundtripType.Pdb when Platform.HasNativePdbSupport: symbolWriterProvider = new PdbWriterProvider(); break; case RoundtripType.PortablePdb: default: symbolWriterProvider = new PortablePdbWriterProvider(); break; } module.Write(file, new WriterParameters { SymbolWriterProvider = symbolWriterProvider, }); module.Dispose(); ISymbolReaderProvider symbolReaderProvider; switch (roundtripType) { case RoundtripType.Pdb when Platform.HasNativePdbSupport: symbolReaderProvider = new PdbReaderProvider(); break; case RoundtripType.PortablePdb: default: symbolReaderProvider = new PortablePdbReaderProvider(); break; } return(ModuleDefinition.ReadModule(file, new ReaderParameters { SymbolReaderProvider = symbolReaderProvider, InMemory = true })); }
// returns the source paths used to create an assembly that has a pdb file public static HashSet <String> GetFilePathsFromPdb(string pdbFile) { var pdb = Path.ChangeExtension(pdbFile, ".dll"); var result = new HashSet <String> (StringComparer.OrdinalIgnoreCase); if (!File.Exists(pdbFile)) { return(result); } var assemblyResolver = new DefaultAssemblyResolver(); var assemblyLocation = Path.GetDirectoryName(pdb); assemblyResolver.AddSearchDirectory(assemblyLocation); var readerParameters = new ReaderParameters { AssemblyResolver = assemblyResolver }; var writerParameters = new WriterParameters(); var symbolReaderProvider = new PortablePdbReaderProvider(); readerParameters.SymbolReaderProvider = symbolReaderProvider; readerParameters.ReadSymbols = true; var assemblyDefinition = AssemblyDefinition.ReadAssembly(pdb, readerParameters); var mainModule = assemblyDefinition.MainModule; foreach (var type in mainModule.Types) { foreach (var method in type.Methods) { if (method.DebugInformation.SequencePoints.Any()) { var sequence_point = method.DebugInformation.SequencePoints[0]; var document = sequence_point.Document; result.Add(document.Url); } } } return(result); }
private Stream GetSymbolInformation(out ISymbolReaderProvider symbolReaderProvider, out ISymbolWriterProvider symbolWriterProvider) { if (string.Equals("none", DebugType, StringComparison.OrdinalIgnoreCase)) { Logger.Info("No symbols"); symbolReaderProvider = null; symbolWriterProvider = null; return(null); } if (string.Equals("embedded", DebugType, StringComparison.OrdinalIgnoreCase)) { Logger.Info("Using embedded symbols"); symbolReaderProvider = new EmbeddedPortablePdbReaderProvider(); symbolWriterProvider = new EmbeddedPortablePdbWriterProvider(); return(null); } string pdbPath = FindPdbPath(); string mdbPath = FindMdbPath(); if (pdbPath != null && mdbPath != null) { if (File.GetLastWriteTimeUtc(pdbPath) >= File.GetLastWriteTimeUtc(mdbPath)) { mdbPath = null; Logger.Debug("Found mdb and pdb debug symbols. Selected pdb (newer).", DebugLogLevel.Verbose); } else { pdbPath = null; Logger.Debug("Found mdb and pdb debug symbols. Selected mdb (newer).", DebugLogLevel.Verbose); } } if (pdbPath != null) { if (IsPortablePdb(pdbPath)) { Logger.Info($"Using portable symbol file {pdbPath}"); symbolReaderProvider = new PortablePdbReaderProvider(); symbolWriterProvider = new PortablePdbWriterProvider(); } else { Logger.Info($"Using symbol file {pdbPath}"); symbolReaderProvider = new PdbReaderProvider(); symbolWriterProvider = new PdbWriterProvider(); } string tempPath = pdbPath + ".tmp"; File.Copy(pdbPath, tempPath, true); return(new FileStream(tempPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)); } else if (mdbPath != null) { Logger.Info($"Using symbol file {mdbPath}"); symbolReaderProvider = new MdbReaderProvider(); symbolWriterProvider = new MdbWriterProvider(); string tempPath = mdbPath + ".tmp"; File.Copy(mdbPath, tempPath, true); return(new FileStream(tempPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)); } symbolReaderProvider = null; symbolWriterProvider = null; return(null); string FindPdbPath() { // because UWP use a wacky convention for symbols string path = Path.ChangeExtension(AssemblyFile, "compile.pdb"); if (File.Exists(path)) { Logger.Debug($"Found debug symbols at '{path}'.", DebugLogLevel.Verbose); return(path); } path = Path.ChangeExtension(AssemblyFile, "pdb"); if (File.Exists(path)) { Logger.Debug($"Found debug symbols at '{path}'.", DebugLogLevel.Verbose); return(path); } return(null); } bool IsPortablePdb(string symbolsPath) { using (var fileStream = File.OpenRead(symbolsPath)) using (var reader = new BinaryReader(fileStream)) { return(reader.ReadBytes(4).SequenceEqual(new byte[] { 0x42, 0x4a, 0x53, 0x42 })); } } string FindMdbPath() { string path = AssemblyFile + ".mdb"; if (File.Exists(path)) { Logger.Debug($"Found debug symbols at '{path}'.", DebugLogLevel.Verbose); return(path); } return(null); } }