public void UrlMappingTest() { const string rawUrl = @"https://raw.githubusercontent.com/microsoft/qsharp-runtime/af6262c05522d645d0a0952272443e84eeab677a/src/Xunit/TestCaseDiscoverer.cs"; const string expectedURL = @"https://github.com/microsoft/qsharp-runtime/blob/af6262c05522d645d0a0952272443e84eeab677a/src/Xunit/TestCaseDiscoverer.cs#L13"; Assert.Equal(expectedURL, PortablePdbSymbolReader.TryFormatGitHubUrl(rawUrl, 13)); }
// // Symbols // private PdbSymbolReader OpenAssociatedSymbolFile(string peFilePath, PEReader peReader) { string pdbFileName = null; BlobContentId pdbContentId = default; foreach (DebugDirectoryEntry debugEntry in peReader.ReadDebugDirectory()) { if (debugEntry.Type != DebugDirectoryEntryType.CodeView) { continue; } CodeViewDebugDirectoryData debugDirectoryData = peReader.ReadCodeViewDebugDirectoryData(debugEntry); string candidatePath = debugDirectoryData.Path; if (!Path.IsPathRooted(candidatePath) || !File.Exists(candidatePath)) { // Also check next to the PE file candidatePath = Path.Combine(Path.GetDirectoryName(peFilePath), Path.GetFileName(candidatePath)); if (!File.Exists(candidatePath)) { continue; } } pdbFileName = candidatePath; pdbContentId = new BlobContentId(debugDirectoryData.Guid, debugEntry.Stamp); break; } if (pdbFileName == null) { return(null); } // Try to open the symbol file as portable pdb first PdbSymbolReader reader = PortablePdbSymbolReader.TryOpen(pdbFileName, GetMetadataStringDecoder(), pdbContentId); if (reader == null) { // Fallback to the diasymreader for non-portable pdbs reader = UnmanagedPdbSymbolReader.TryOpenSymbolReaderForMetadataFile(peFilePath, Path.GetDirectoryName(pdbFileName)); } return(reader); }
private PdbSymbolReader OpenAssociatedSymbolFile(string peFilePath, PEReader peReader) { // Assume that the .pdb file is next to the binary var pdbFilename = Path.ChangeExtension(peFilePath, ".pdb"); string searchPath = ""; if (!File.Exists(pdbFilename)) { pdbFilename = null; // If the file doesn't exist, try the path specified in the CodeView section of the image foreach (DebugDirectoryEntry debugEntry in peReader.ReadDebugDirectory()) { if (debugEntry.Type != DebugDirectoryEntryType.CodeView) { continue; } string candidateFileName = peReader.ReadCodeViewDebugDirectoryData(debugEntry).Path; if (Path.IsPathRooted(candidateFileName) && File.Exists(candidateFileName)) { pdbFilename = candidateFileName; searchPath = Path.GetDirectoryName(pdbFilename); break; } } if (pdbFilename == null) { return(null); } } // Try to open the symbol file as portable pdb first PdbSymbolReader reader = PortablePdbSymbolReader.TryOpen(pdbFilename, MetadataHelper.GetMetadataStringDecoder()); if (reader == null) { // Fallback to the diasymreader for non-portable pdbs reader = UnmanagedPdbSymbolReader.TryOpenSymbolReaderForMetadataFile(peFilePath, searchPath); } return(reader); }
// // Symbols // private PdbSymbolReader OpenAssociatedSymbolFile(string peFilePath) { // Assume that the .pdb file is next to the binary var pdbFilename = Path.ChangeExtension(peFilePath, ".pdb"); if (!File.Exists(pdbFilename)) { return(null); } // Try to open the symbol file as portable pdb first PdbSymbolReader reader = PortablePdbSymbolReader.TryOpen(pdbFilename, GetMetadataStringDecoder()); if (reader == null) { // Fallback to the diasymreader for non-portable pdbs reader = UnmanagedPdbSymbolReader.TryOpenSymbolReaderForMetadataFile(peFilePath); } return(reader); }
// // Symbols // private void InitializeSymbolReader(ModuleData moduleData) { // Assume that the .pdb file is next to the binary var pdbFilename = Path.ChangeExtension(moduleData.FilePath, ".pdb"); if (!File.Exists(pdbFilename)) { return; } // Try to open the symbol file as portable pdb first PdbSymbolReader reader = PortablePdbSymbolReader.TryOpen(pdbFilename, GetMetadataStringDecoder()); if (reader == null) { // Fallback to the diasymreader for non-portable pdbs reader = UnmanagedPdbSymbolReader.TryOpenSymbolReaderForMetadataFile(moduleData.FilePath); } moduleData.PdbReader = reader; }
private EcmaModule AddModule(string filePath, string expectedSimpleName, bool useForBinding, ModuleData oldModuleData = null, bool throwOnFailureToLoad = true) { filePath = Path.GetFullPath(filePath); PEReader peReader = null; MemoryMappedViewAccessor mappedViewAccessor = null; PdbSymbolReader pdbReader = null; try { if (oldModuleData == null) { peReader = OpenPEFile(filePath, out mappedViewAccessor); #if !READYTORUN if (peReader.HasMetadata && (peReader.PEHeaders.CorHeader.Flags & (CorFlags.ILLibrary | CorFlags.ILOnly)) == 0) { throw new NotSupportedException($"Error: C++/CLI is not supported: '{filePath}'"); } #endif pdbReader = PortablePdbSymbolReader.TryOpenEmbedded(peReader, GetMetadataStringDecoder()) ?? OpenAssociatedSymbolFile(filePath, peReader); } else { filePath = oldModuleData.FilePath; peReader = oldModuleData.Module.PEReader; mappedViewAccessor = oldModuleData.MappedViewAccessor; pdbReader = oldModuleData.Module.PdbReader; } if (!peReader.HasMetadata && !throwOnFailureToLoad) { return(null); } EcmaModule module = EcmaModule.Create(this, peReader, containingAssembly: null, pdbReader); MetadataReader metadataReader = module.MetadataReader; string simpleName = metadataReader.GetString(metadataReader.GetAssemblyDefinition().Name); if (expectedSimpleName != null && !simpleName.Equals(expectedSimpleName, StringComparison.OrdinalIgnoreCase)) { throw new FileNotFoundException("Assembly name does not match filename " + filePath); } ModuleData moduleData = new ModuleData() { SimpleName = simpleName, FilePath = filePath, Module = module, MappedViewAccessor = mappedViewAccessor }; lock (this) { if (useForBinding) { ModuleData actualModuleData = _simpleNameHashtable.AddOrGetExisting(moduleData); if (actualModuleData != moduleData) { if (actualModuleData.FilePath != filePath) { throw new FileNotFoundException("Module with same simple name already exists " + filePath); } return(actualModuleData.Module); } } mappedViewAccessor = null; // Ownership has been transfered pdbReader = null; // Ownership has been transferred _moduleHashtable.AddOrGetExisting(moduleData); } return(module); } finally { if (mappedViewAccessor != null) { mappedViewAccessor.Dispose(); } if (pdbReader != null) { pdbReader.Dispose(); } } }
private EcmaModule AddModule(string filePath, string expectedSimpleName, bool useForBinding) { MemoryMappedViewAccessor mappedViewAccessor = null; PdbSymbolReader pdbReader = null; try { PEReader peReader = OpenPEFile(filePath, out mappedViewAccessor); pdbReader = PortablePdbSymbolReader.TryOpenEmbedded(peReader, GetMetadataStringDecoder()) ?? OpenAssociatedSymbolFile(filePath, peReader); EcmaModule module = EcmaModule.Create(this, peReader, containingAssembly: null, pdbReader); MetadataReader metadataReader = module.MetadataReader; string simpleName = metadataReader.GetString(metadataReader.GetAssemblyDefinition().Name); if (expectedSimpleName != null && !simpleName.Equals(expectedSimpleName, StringComparison.OrdinalIgnoreCase)) { throw new FileNotFoundException("Assembly name does not match filename " + filePath); } ModuleData moduleData = new ModuleData() { SimpleName = simpleName, FilePath = filePath, Module = module, MappedViewAccessor = mappedViewAccessor }; lock (this) { if (useForBinding) { ModuleData actualModuleData = _simpleNameHashtable.AddOrGetExisting(moduleData); if (actualModuleData != moduleData) { if (actualModuleData.FilePath != filePath) { throw new FileNotFoundException("Module with same simple name already exists " + filePath); } return(actualModuleData.Module); } } mappedViewAccessor = null; // Ownership has been transfered pdbReader = null; // Ownership has been transferred _moduleHashtable.AddOrGetExisting(moduleData); } return(module); } finally { if (mappedViewAccessor != null) { mappedViewAccessor.Dispose(); } if (pdbReader != null) { pdbReader.Dispose(); } } }