public void PdbFileNotFound()
        {
            var dir     = Temp.CreateDirectory();
            var dllFile = dir.CreateFile("lib.dll");

            var source = @"class C { public static void Main() { int x = 1; } }";

            var compilation     = CSharpTestBase.CreateCompilationWithMscorlib40AndSystemCore(source, options: TestOptions.DebugDll, assemblyName: "lib");
            var pdbStream       = new MemoryStream();
            var debugDirPdbPath = Path.Combine(dir.Path, "nonexistent.pdb");
            var peImage         = compilation.EmitToArray(new EmitOptions(debugInformationFormat: DebugInformationFormat.PortablePdb, pdbFilePath: debugDirPdbPath), pdbStream: pdbStream);

            pdbStream.Position = 0;

            dllFile.WriteAllBytes(peImage);

            var outputs = new CompilationOutputFilesWithImplicitPdbPath(dllFile.Path);

            Assert.Null(outputs.OpenPdb());

            using (var metadata = outputs.OpenAssemblyMetadata(prefetch: false))
            {
                var mdReader = metadata.GetMetadataReader();
                Assert.Equal("lib", mdReader.GetString(mdReader.GetAssemblyDefinition().Name));
            }

            // make sure all files are closed and can be deleted
            Directory.Delete(dir.Path, recursive: true);
        }
Esempio n. 2
0
        public void AssemblyAndPdb()
        {
            var source = @"class C { public static void Main() { int x = 1; } }";

            var compilation = CSharpTestBase.CreateCompilationWithMscorlib40AndSystemCore(source, options: TestOptions.DebugDll, assemblyName: "lib");
            var pdbStream   = new MemoryStream();
            var peImage     = compilation.EmitToArray(new EmitOptions(debugInformationFormat: DebugInformationFormat.PortablePdb), pdbStream: pdbStream);

            pdbStream.Position = 0;

            var dir     = Temp.CreateDirectory();
            var dllFile = dir.CreateFile("a.dll").WriteAllBytes(peImage);
            var pdbFile = dir.CreateFile("a.pdb").WriteAllBytes(pdbStream.ToArray());

            var outputs = new CompilationOutputFiles(dllFile.Path, pdbFile.Path);

            using (var pdb = outputs.OpenPdb())
            {
                var encReader = pdb.CreateEditAndContinueMethodDebugInfoReader();
                Assert.True(encReader.IsPortable);
                var localSig = encReader.GetLocalSignature(MetadataTokens.MethodDefinitionHandle(1));
                Assert.Equal(MetadataTokens.StandaloneSignatureHandle(1), localSig);
            }

            using (var metadata = outputs.OpenAssemblyMetadata(prefetch: false))
            {
                var mdReader = metadata.GetMetadataReader();
                Assert.Equal("lib", mdReader.GetString(mdReader.GetAssemblyDefinition().Name));
            }

            // make sure all files are closed and can be deleted
            Directory.Delete(dir.Path, recursive: true);
        }
Esempio n. 3
0
        public void DebugInfo(DebugInformationFormat format, bool useSymReader)
        {
            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, sourceFileName: "/a/c.cs");

            var pdbStream = new MemoryStream();

            compilation.EmitToArray(new EmitOptions(debugInformationFormat: format), pdbStream: pdbStream);
            pdbStream.Position = 0;

            DebugInformationReaderProvider       provider;
            EditAndContinueMethodDebugInfoReader reader;

            if (format == DebugInformationFormat.PortablePdb && useSymReader)
            {
                var pdbStreamCom           = SymUnmanagedStreamFactory.CreateStream(pdbStream);
                var metadataImportProvider = new DummyMetadataImportProvider();
                Assert.Equal(0, new SymBinder().GetReaderFromPdbStream(metadataImportProvider, pdbStreamCom, out var symReader));
                reader = EditAndContinueMethodDebugInfoReader.Create((ISymUnmanagedReader5)symReader, version: 1);
            }
            else
            {
                provider = DebugInformationReaderProvider.CreateFromStream(pdbStream);
                reader   = provider.CreateEditAndContinueMethodDebugInfoReader();
            }

            // 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);
Esempio n. 4
0
        public void AssemblyAndPdb(DebugInformationFormat format)
        {
            var source      = @"class C { public static void Main() { int x = 1; } }";
            var compilation = CSharpTestBase.CreateCompilationWithMscorlib40AndSystemCore(source, options: TestOptions.DebugDll, assemblyName: "lib");

            var pdbStream = (format != DebugInformationFormat.Embedded) ? new MemoryStream() : null;
            var peImage   = compilation.EmitToArray(new EmitOptions(debugInformationFormat: format), pdbStream: pdbStream);

            Stream currentPEStream  = null;
            Stream currentPdbStream = null;

            var outputs = new TestCompilationOutputs(
                openAssemblyStream: () => currentPEStream = new MemoryStream(peImage.ToArray()),
                openPdbStream: () =>
            {
                if (pdbStream == null)
                {
                    return(null);
                }

                currentPdbStream   = new MemoryStream();
                pdbStream.Position = 0;
                pdbStream.CopyTo(currentPdbStream);
                currentPdbStream.Position = 0;
                return(currentPdbStream);
            });

            using (var pdb = outputs.OpenPdb())
            {
                var encReader = pdb.CreateEditAndContinueMethodDebugInfoReader();
                Assert.Equal(format != DebugInformationFormat.Pdb, encReader.IsPortable);
                var localSig = encReader.GetLocalSignature(MetadataTokens.MethodDefinitionHandle(1));
                Assert.Equal(MetadataTokens.StandaloneSignatureHandle(1), localSig);
            }

            if (format == DebugInformationFormat.Embedded)
            {
                Assert.Throws <ObjectDisposedException>(() => currentPEStream.Length);
            }
            else
            {
                Assert.Throws <ObjectDisposedException>(() => currentPdbStream.Length);
            }

            using (var metadata = outputs.OpenAssemblyMetadata(prefetch: false))
            {
                Assert.NotEqual(0, currentPEStream.Length);

                var mdReader = metadata.GetMetadataReader();
                Assert.Equal("lib", mdReader.GetString(mdReader.GetAssemblyDefinition().Name));
            }

            Assert.Throws <ObjectDisposedException>(() => currentPEStream.Length);

            using (var metadata = outputs.OpenAssemblyMetadata(prefetch: true))
            {
                // the stream has been closed since we prefetched the metadata:
                Assert.Throws <ObjectDisposedException>(() => currentPEStream.Length);

                var mdReader = metadata.GetMetadataReader();
                Assert.Equal("lib", mdReader.GetString(mdReader.GetAssemblyDefinition().Name));
            }

            Assert.NotEqual(Guid.Empty, outputs.ReadAssemblyModuleVersionId());
        }