Exemplo n.º 1
0
        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));
        }
        NativePdbReader(string path)
        {
            pdbStream = new FileStream(path, FileMode.Open);
            var metadataProvider = new MdProvider();

            reader = SymUnmanagedReaderFactory.CreateReader <ISymUnmanagedReader5>(pdbStream, metadataProvider);
        }
Exemplo n.º 3
0
        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);
        }
Exemplo n.º 4
0
        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()
                           ));
            }
        }
Exemplo n.º 5
0
        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;
                }
            }
        }
Exemplo n.º 6
0
 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);
 }
Exemplo n.º 7
0
        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);
        }
Exemplo n.º 8
0
        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));
            }
        }
Exemplo n.º 9
0
        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);
Exemplo n.º 10
0
 public static ISymUnmanagedReader5 CreateWindowsPdbReader(Stream pdbStream, MetadataReader metadataReader)
 {
     return(SymUnmanagedReaderFactory.CreateReader <ISymUnmanagedReader5>(pdbStream, new SymMetadataProvider(metadataReader)));
 }
Exemplo n.º 11
0
 public static ISymUnmanagedReader5 CreateWindowsPdbReader(Stream pdbStream)
 {
     return(SymUnmanagedReaderFactory.CreateReader <ISymUnmanagedReader5>(pdbStream, DummySymReaderMetadataProvider.Instance));
 }