public static ISymUnmanagedReader5 OpenDummySymReader(ImmutableArray <byte> pdbImage) { var symBinder = new SymBinder(); var metadataImportProvider = new DummyMetadataImportProvider(); var pdbStream = new MemoryStream(); pdbImage.WriteToStream(pdbStream); var pdbStreamCom = SymUnmanagedStreamFactory.CreateStream(pdbStream); if ( pdbImage.Length > 4 && pdbImage[0] == 'B' && pdbImage[1] == 'S' && pdbImage[2] == 'J' && pdbImage[3] == 'B' ) { var hr = symBinder.GetReaderFromPdbStream( metadataImportProvider, pdbStreamCom, out var symReader ); Assert.Equal(0, hr); return((ISymUnmanagedReader5)symReader); } else { return(SymUnmanagedReaderFactory.CreateReader <ISymUnmanagedReader5>( pdbStream, new DummySymReaderMetadataProvider() )); } }
NativePdbReader(string path) { pdbStream = new FileStream(path, FileMode.Open); var metadataProvider = new MdProvider(); reader = SymUnmanagedReaderFactory.CreateReader <ISymUnmanagedReader5>(pdbStream, metadataProvider); }
public void Create() { // TODO: Ideally we would run each of these tests in a separate process so they don't interfere with each other. // Native library being loaded makes following loads successful. var pdbStream = new MemoryStream(TestResources.SourceLink.WindowsPdb); Assert.Throws <DllNotFoundException>(() => SymUnmanagedReaderFactory.CreateReader <ISymUnmanagedReader5>(pdbStream, DummySymReaderMetadataProvider.Instance, SymUnmanagedReaderCreationOptions.Default)); Assert.Throws <DllNotFoundException>(() => SymUnmanagedWriterFactory.CreateWriter(DummySymWriterMetadataProvider.Instance)); Assert.Throws <DllNotFoundException>(() => SymUnmanagedWriterFactory.CreateWriter(DummySymWriterMetadataProvider.Instance, SymUnmanagedWriterCreationOptions.Deterministic)); Assert.Throws <NotSupportedException>(() => SymUnmanagedReaderFactory.CreateReader <ISymUnmanagedReader5>(pdbStream, DummySymReaderMetadataProvider.Instance, SymUnmanagedReaderCreationOptions.UseComRegistry)); Assert.Throws <SymUnmanagedWriterException>(() => SymUnmanagedWriterFactory.CreateWriter(DummySymWriterMetadataProvider.Instance, SymUnmanagedWriterCreationOptions.UseComRegistry | SymUnmanagedWriterCreationOptions.Deterministic)); Assert.NotNull(SymUnmanagedReaderFactory.CreateReader <ISymUnmanagedReader5>(pdbStream, DummySymReaderMetadataProvider.Instance, SymUnmanagedReaderCreationOptions.UseAlternativeLoadPath)); Assert.NotNull(SymUnmanagedReaderFactory.CreateReader <ISymUnmanagedReader5>(pdbStream, DummySymReaderMetadataProvider.Instance, SymUnmanagedReaderCreationOptions.UseAlternativeLoadPath | SymUnmanagedReaderCreationOptions.UseComRegistry)); Assert.NotNull(SymUnmanagedWriterFactory.CreateWriter(DummySymWriterMetadataProvider.Instance, SymUnmanagedWriterCreationOptions.UseComRegistry)); Assert.NotNull(SymUnmanagedWriterFactory.CreateWriter(DummySymWriterMetadataProvider.Instance, SymUnmanagedWriterCreationOptions.UseAlternativeLoadPath)); Assert.NotNull(SymUnmanagedWriterFactory.CreateWriter(DummySymWriterMetadataProvider.Instance, SymUnmanagedWriterCreationOptions.UseAlternativeLoadPath | SymUnmanagedWriterCreationOptions.UseComRegistry | SymUnmanagedWriterCreationOptions.Deterministic)); }
private ChecksumAlgorithmType ChecksumAlgorithmForFullPdb() { string fileName = Path.GetFileName(this.FileName); string extension = Path.GetExtension(fileName); string pdbPath = this.FileName.Replace(fileName, fileName.Replace(extension, ".pdb")); if (!File.Exists(pdbPath)) { return(ChecksumAlgorithmType.Unknown); } using var pdbStream = new FileStream(pdbPath, FileMode.Open, FileAccess.Read); var metadataProvider = new SymMetadataProvider(this.metadataReader); object importer = SymUnmanagedReaderFactory.CreateSymReaderMetadataImport(metadataProvider); ISymUnmanagedReader3 reader = SymUnmanagedReaderFactory.CreateReaderWithMetadataImport <ISymUnmanagedReader3>(pdbStream, importer, SymUnmanagedReaderCreationOptions.UseComRegistry); try { Guid algorithm = Guid.Empty; foreach (ISymUnmanagedDocument document in reader.GetDocuments()) { document.GetChecksumAlgorithmId(ref algorithm); return(algorithm == sha256guid ? ChecksumAlgorithmType.Sha256 : ChecksumAlgorithmType.Sha1); } } finally { _ = ((ISymUnmanagedDispose)reader).Destroy(); } return(ChecksumAlgorithmType.Unknown); }
public static (ImmutableArray <byte> PEImage, ISymUnmanagedReader5 SymReader) EmitAndOpenDummySymReader(Compilation compilation, DebugInformationFormat pdbFormat) { var symBinder = new SymBinder(); var metadataImportProvider = new DummyMetadataImportProvider(); var pdbStream = new MemoryStream(); var peImage = compilation.EmitToArray(new EmitOptions(debugInformationFormat: pdbFormat), pdbStream: pdbStream); pdbStream.Position = 0; var pdbStreamCom = SymUnmanagedStreamFactory.CreateStream(pdbStream); ISymUnmanagedReader5 symReader5; if (pdbFormat == DebugInformationFormat.PortablePdb) { int hr = symBinder.GetReaderFromPdbStream(metadataImportProvider, pdbStreamCom, out var symReader); Assert.Equal(0, hr); symReader5 = (ISymUnmanagedReader5)symReader; } else { symReader5 = SymUnmanagedReaderFactory.CreateReader <ISymUnmanagedReader5>(pdbStream, new DummySymReaderMetadataProvider()); } return(peImage, symReader5); }
private void CollectMethods(Stream pdbStream) { reader = SymUnmanagedReaderFactory.CreateReader <ISymUnmanagedReader5>(pdbStream, new DummyMetadataProvider()); var docs = reader.GetDocuments(); foreach (var doc in docs) { var language = doc.GetLanguage(); // Note that we're lying here by specifying "cpp" as the language when it is C# in reality // (this is so we get some syntax-highlighting in Sourcetrail) var languageName = language == SymLanguageType.CSharp ? "cpp" : language == SymLanguageType.Basic ? "basic" : "c"; var methods = reader.GetMethodsInDocument(doc); foreach (var method in methods) { var token = method.GetToken(); var pdbMethod = new PdbMethod(token, doc.GetName(), languageName); var sequencePoints = method.GetSequencePoints(); foreach (var sp in sequencePoints) { if (sp.IsHidden) { continue; } pdbMethod.AddSequence(sp.Offset, sp.StartLine, sp.StartColumn, sp.EndLine, sp.EndColumn); } methodsByToken[token] = pdbMethod; } } }
public DiaSymNativeDebugInfoProvider(PEFile module, string pdbFileName, Stream stream) { this.module = module; this.pdbFileName = pdbFileName; this.stream = stream; this.metadata = module.Metadata; this.reader = SymUnmanagedReaderFactory.CreateReader <ISymUnmanagedReader5>(stream, this); }
private ISymUnmanagedReader GetReaderForFrame(ClrStackFrame frame) { ClrModule module = frame.Method?.Type?.Module; PdbInfo info = module?.Pdb; ISymUnmanagedReader reader = null; string name = string.Empty; if (info != null) { if (_pdbReaders.TryGetValue(info, out reader)) { return(reader); } name = Path.GetFileName(info.FileName); if (!File.Exists(name)) { _logger?.LogTrace("Symbol file {0} missing", name); return(null); } try { Stream stream = File.OpenRead(name); if (IsPortablePdb(stream)) { var bindar = new SymBinder(); int result = bindar.GetReaderFromPdbFile(new MetaDataImportProvider(module.MetadataImport), name, out reader); } else { reader = SymUnmanagedReaderFactory.CreateReaderWithMetadataImport <ISymUnmanagedReader3>(stream, module.MetadataImport, SymUnmanagedReaderCreationOptions.Default); } } catch (Exception e) { _logger?.LogError(e, "Unable to obtain symbol reader for {0}", name); } } if (reader != null) { _logger?.LogTrace("Symbol file {0} found, reader created", name); _pdbReaders.Add(info, reader); } else { _logger?.LogTrace("Unable to obtain symbol reader for {0}", name); } return(reader); }
private static bool IsMatchWindowsPdb(IEnumerable <PdbInfo> debugDirectory, string pdbFilePath) { using (var pdbStream = File.OpenRead(pdbFilePath)) { var metadataProvider = new SymReaderMetadataProvider(); var reader = SymUnmanagedReaderFactory.CreateReader <ISymUnmanagedReader5>(pdbStream, metadataProvider); foreach (var item in debugDirectory) { bool isMatch = false; int result = reader.MatchesModule(item.Guid, item.Stamp, item.Age, out isMatch); if (isMatch) { return(true); } } } return(false); }
public static ISymUnmanagedReader5 CreateReader(Stream pdbStream, Stream peStream) { pdbStream.Position = 0; bool isPortable = pdbStream.ReadByte() == 'B' && pdbStream.ReadByte() == 'S' && pdbStream.ReadByte() == 'J' && pdbStream.ReadByte() == 'B'; pdbStream.Position = 0; var metadataProvider = new SymMetadataProvider(peStream); if (isPortable) { return((ISymUnmanagedReader5) new SymBinder().GetReaderFromStream( pdbStream, SymUnmanagedReaderFactory.CreateSymReaderMetadataImport(metadataProvider))); } else { return(SymUnmanagedReaderFactory.CreateReader <ISymUnmanagedReader5>(pdbStream, metadataProvider)); } }
public void DebugInfo(DebugInformationFormat format) { var symBinder = new SymBinder(); var metadataImportProvider = new DummyMetadataImportProvider(); var source = @" using System; delegate void D(); class C { public static void Main() { int x = 1; D d = () => Console.Write(x); d(); } } "; var compilation = CSharpTestBase.CreateCompilationWithMscorlib40AndSystemCore(source, options: TestOptions.DebugDll); var pdbStream = new MemoryStream(); compilation.EmitToArray(new EmitOptions(debugInformationFormat: format), pdbStream: pdbStream); pdbStream.Position = 0; var pdbStreamCom = SymUnmanagedStreamFactory.CreateStream(pdbStream); ISymUnmanagedReader5 symReader5; if (format == DebugInformationFormat.PortablePdb) { int hr = symBinder.GetReaderFromPdbStream(metadataImportProvider, pdbStreamCom, out var symReader); Assert.Equal(0, hr); symReader5 = (ISymUnmanagedReader5)symReader; } else { symReader5 = SymUnmanagedReaderFactory.CreateReader <ISymUnmanagedReader5>(pdbStream, new DummySymReaderMetadataProvider()); } var reader = EditAndContinueMethodDebugInfoReader.Create(symReader5, version: 1); // Main method var debugInfo = reader.GetDebugInfo(MetadataTokens.MethodDefinitionHandle(5)); Assert.Equal(0, debugInfo.GetMethodOrdinal()); AssertEx.Equal(new[] { "Offset=0 Ordinal=0 Kind=LambdaDisplayClass", "Offset=33 Ordinal=0 Kind=UserDefined" }, debugInfo.InspectLocalSlots()); AssertEx.Equal(new[] { "Offset=43 Id=0#0 Closure=0" }, debugInfo.InspectLambdas()); AssertEx.Equal(new[] { "Offset=0 Id=0#0" }, debugInfo.InspectClosures()); var localSig = reader.GetLocalSignature(MetadataTokens.MethodDefinitionHandle(5)); Assert.Equal(MetadataTokens.StandaloneSignatureHandle(1), localSig); // method without debug information: debugInfo = reader.GetDebugInfo(MetadataTokens.MethodDefinitionHandle(1)); Assert.Equal(-1, debugInfo.GetMethodOrdinal()); Assert.Null(debugInfo.InspectLocalSlots()); Assert.Null(debugInfo.InspectLambdas()); Assert.Null(debugInfo.InspectClosures()); localSig = reader.GetLocalSignature(MetadataTokens.MethodDefinitionHandle(1)); Assert.Equal(default, localSig);
public static ISymUnmanagedReader5 CreateWindowsPdbReader(Stream pdbStream, MetadataReader metadataReader) { return(SymUnmanagedReaderFactory.CreateReader <ISymUnmanagedReader5>(pdbStream, new SymMetadataProvider(metadataReader))); }
public static ISymUnmanagedReader5 CreateWindowsPdbReader(Stream pdbStream) { return(SymUnmanagedReaderFactory.CreateReader <ISymUnmanagedReader5>(pdbStream, DummySymReaderMetadataProvider.Instance)); }