Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement SourceLink for Windows PDBs #18539

Merged
merged 1 commit into from
May 9, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion docs/compilers/CSharp/CommandLine.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@
| `/linkresource`:*resinfo* | Link the specified resource to this assembly (Short form: `/linkres`) Where the *resinfo* format is *file*{`,`*string name*{`,``public``|``private`}}
| **CODE GENERATION**
| `/debug`{`+`|`-`} | Emit (or do not Emit) debugging information
| `/debug`:{`full`|`pdbonly`|`portable`} | Specify debugging type (`full` is default, and enables attaching a debugger to a running program. `portable` is a cross-platform format)
| `/debug`:`full` | Emit debugging information to .pdb file using default format for the current platform: _Windows PDB_ on Windows, _Portable PDB_ on other systems
| `/debug`:`pdbonly` | Same as `/debug:full`. For backward compatibility.
| `/debug`:`portable` | Emit debugging information to to .pdb file using cross-platform [Portable PDB format](https://github.com/dotnet/core/blob/master/Documentation/diagnostics/portable_pdb.md)
| `/debug`:`embedded` | Emit debugging information into the .dll/.exe itself (.pdb file is not produced) using [Portable PDB format](https://github.com/dotnet/core/blob/master/Documentation/diagnostics/portable_pdb.md).
| `/sourcelink`:*file* | [Source link](https://github.com/dotnet/core/blob/master/Documentation/diagnostics/source_link.md) info to embed into PDB.
| `/optimize`{`+`|`-`} | Enable optimizations (Short form: `/o`)
| `/deterministic` | Produce a deterministic assembly (including module version GUID and timestamp)
| **ERRORS AND WARNINGS**
Expand Down
7 changes: 6 additions & 1 deletion docs/compilers/Visual Basic/CommandLine.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,14 @@
| `/win32manifest:`*file* | The provided file is embedded in the manifest section of the output PE.
| `/win32resource:`*file* | Specifies a Win32 resource file (.res).
| **CODE GENERATION**
| `/debug`{`+`|`-`} | Emit debugging information.
| `/debug`:`full` | Emit debugging information to .pdb file using default format for the current platform: _Windows PDB_ on Windows, _Portable PDB_ on other systems
| `/debug`:`pdbonly` | Same as `/debug:full`. For backward compatibility.
| `/debug`:`portable` | Emit debugging information to to .pdb file using cross-platform [Portable PDB format](https://github.com/dotnet/core/blob/master/Documentation/diagnostics/portable_pdb.md)
| `/debug`:`embedded` | Emit debugging information into the .dll/.exe itself (.pdb file is not produced) using [Portable PDB format](https://github.com/dotnet/core/blob/master/Documentation/diagnostics/portable_pdb.md).
| `/sourcelink`:*file* | [Source link](https://github.com/dotnet/core/blob/master/Documentation/diagnostics/source_link.md) info to embed into PDB.
| `/optimize`{`+`|`-`} | Enable optimizations.
| `/removeintchecks`{`+`|`-`} | Remove integer checks. Default off.
| `/debug`{`+`|`-`} | Emit debugging information.
| `/debug:full` | Emit full debugging information (default).
| `/debug:portable` | Emit debugging information in the portable format.
| `/debug:pdbonly` | Emit PDB file only.
Expand Down
6 changes: 3 additions & 3 deletions src/Compilers/CSharp/Portable/CSharpResources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions src/Compilers/CSharp/Portable/CSharpResources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -4450,7 +4450,7 @@ To remove the warning, you can use /reference instead (set the Embed Interop Typ
(including module version GUID and timestamp)
/instrument:TestCoverage Produce an assembly instrumented to collect
coverage information
/sourcelink:<file> Source link info to embed into Portable PDB.
/sourcelink:<file> Source link info to embed into PDB.

- ERRORS AND WARNINGS -
/warnaserror[+|-] Report all warnings as errors
Expand Down Expand Up @@ -4971,8 +4971,8 @@ To remove the warning, you can use /reference instead (set the Embed Interop Typ
<data name="ERR_ExpressionTreeContainsTupleConversion" xml:space="preserve">
<value>An expression tree may not contain a tuple conversion.</value>
</data>
<data name="ERR_SourceLinkRequiresPortablePdb" xml:space="preserve">
<value>/sourcelink switch is only supported when emitting Portable PDB (/debug:portable or /debug:embedded must be specified).</value>
<data name="ERR_SourceLinkRequiresPdb" xml:space="preserve">
<value>/sourcelink switch is only supported when emitting PDB.</value>
</data>
<data name="ERR_CannotEmbedWithoutPdb" xml:space="preserve">
<value>/embed switch is only supported when emitting Portable PDB (/debug:portable or /debug:embedded).</value>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1193,12 +1193,9 @@ internal sealed override CommandLineArguments CommonParse(IEnumerable<string> ar
keyFileSetting = ParseGenericPathToFile(keyFileSetting, diagnostics, baseDirectory);
}

if (sourceLink != null)
if (sourceLink != null && !emitPdb)
Copy link
Member

@jcouv jcouv Apr 27, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should the VB command-line parser have a similar change? #Resolved

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, forgot about that.


In reply to: 113746766 [](ancestors = 113746766)

{
if (!emitPdb || !debugInformationFormat.IsPortable())
{
AddDiagnostic(diagnostics, ErrorCode.ERR_SourceLinkRequiresPortablePdb);
}
AddDiagnostic(diagnostics, ErrorCode.ERR_SourceLinkRequiresPdb);
}

if (embedAllSourceFiles)
Expand Down
2 changes: 1 addition & 1 deletion src/Compilers/CSharp/Portable/Errors/ErrorCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1060,7 +1060,7 @@ internal enum ErrorCode
ERR_InvalidOutputName = 2041,
ERR_InvalidDebugInformationFormat = 2042,
ERR_LegacyObjectIdSyntax = 2043,
ERR_SourceLinkRequiresPortablePdb = 2044,
ERR_SourceLinkRequiresPdb = 2044,
ERR_CannotEmbedWithoutPdb = 2045,
// unused 2046-2999
WRN_CLS_NoVarArgs = 3000,
Expand Down
46 changes: 36 additions & 10 deletions src/Compilers/CSharp/Test/CommandLine/CommandLineTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,8 @@
using Roslyn.Test.Utilities;
using Roslyn.Utilities;
using Xunit;

using static Roslyn.Test.Utilities.SharedResourceHelpers;
using static Microsoft.CodeAnalysis.CommonDiagnosticAnalyzers;
using static Roslyn.Test.Utilities.SharedResourceHelpers;

namespace Microsoft.CodeAnalysis.CSharp.CommandLine.UnitTests
{
Expand Down Expand Up @@ -1725,19 +1724,19 @@ public void SourceLink()
Assert.Equal(Path.Combine(_baseDirectory, "s l.json"), parsedArgs.SourceLink);

parsedArgs = DefaultParse(new[] { "/sourcelink:sl.json", "/debug:full", "a.cs" }, _baseDirectory);
parsedArgs.Errors.Verify(Diagnostic(ErrorCode.ERR_SourceLinkRequiresPortablePdb));
parsedArgs.Errors.Verify();

parsedArgs = DefaultParse(new[] { "/sourcelink:sl.json", "/debug:pdbonly", "a.cs" }, _baseDirectory);
parsedArgs.Errors.Verify(Diagnostic(ErrorCode.ERR_SourceLinkRequiresPortablePdb));
parsedArgs.Errors.Verify();

parsedArgs = DefaultParse(new[] { "/sourcelink:sl.json", "/debug-", "a.cs" }, _baseDirectory);
parsedArgs.Errors.Verify(Diagnostic(ErrorCode.ERR_SourceLinkRequiresPortablePdb));
parsedArgs.Errors.Verify(Diagnostic(ErrorCode.ERR_SourceLinkRequiresPdb));

parsedArgs = DefaultParse(new[] { "/sourcelink:sl.json", "/debug+", "a.cs" }, _baseDirectory);
parsedArgs.Errors.Verify(Diagnostic(ErrorCode.ERR_SourceLinkRequiresPortablePdb));
parsedArgs.Errors.Verify();

parsedArgs = DefaultParse(new[] { "/sourcelink:sl.json", "a.cs" }, _baseDirectory);
parsedArgs.Errors.Verify(Diagnostic(ErrorCode.ERR_SourceLinkRequiresPortablePdb));
parsedArgs.Errors.Verify(Diagnostic(ErrorCode.ERR_SourceLinkRequiresPdb));
}

[Fact]
Expand Down Expand Up @@ -1800,6 +1799,31 @@ public void SourceLink_EndToEnd_Portable()
CleanupAllGeneratedFiles(src.Path);
}

[Fact]
public void SourceLink_EndToEnd_Windows()
{
var dir = Temp.CreateDirectory();

var src = dir.CreateFile("a.cs");
src.WriteAllText(@"class C { public static void Main() {} }");

var sl = dir.CreateFile("sl.json");
byte[] slContent = Encoding.UTF8.GetBytes(@"{ ""documents"" : {} }");
sl.WriteAllBytes(slContent);

var outWriter = new StringWriter(CultureInfo.InvariantCulture);
var csc = new MockCSharpCompiler(null, dir.Path, new[] { "/nologo", "/debug:full", "/sourcelink:sl.json", "a.cs" });
int exitCode = csc.Run(outWriter);
Assert.Equal(0, exitCode);

var pdbStream = File.OpenRead(Path.Combine(dir.Path, "a.pdb"));
var actualData = PdbValidation.GetSourceLinkData(pdbStream);
AssertEx.Equal(slContent, actualData);

// Clean up temp files
CleanupAllGeneratedFiles(src.Path);
}

[Fact]
public void Embed()
{
Expand Down Expand Up @@ -7275,12 +7299,14 @@ public void IOFailure_DisposeXmlFile()
Assert.Equal($"error CS0016: Could not write to output file '{xmlPath}' -- 'Fake IOException'{Environment.NewLine}", outWriter.ToString());
}

[Fact]
public void IOFailure_DisposeSourceLinkFile()
[Theory]
[InlineData("portable")]
[InlineData("full")]
public void IOFailure_DisposeSourceLinkFile(string format)
{
var srcPath = MakeTrivialExe(Temp.CreateDirectory().Path);
var sourceLinkPath = Path.Combine(Path.GetDirectoryName(srcPath), "test.json");
var csc = new MockCSharpCompiler(null, _baseDirectory, new[] { "/nologo", "/preferreduilang:en", "/debug:portable", $"/sourcelink:{sourceLinkPath}", srcPath });
var csc = new MockCSharpCompiler(null, _baseDirectory, new[] { "/nologo", "/preferreduilang:en", "/debug:" + format, $"/sourcelink:{sourceLinkPath}", srcPath });
csc.FileOpen = (file, mode, access, share) =>
{
if (file == sourceLinkPath)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@
<Compile Include="PDB\CSharpPDBTestBase.cs" />
<Compile Include="PDB\PDBIteratorTests.cs" />
<Compile Include="PDB\PDBLambdaTests.cs" />
<Compile Include="PDB\PDBSourceLinkTests.cs" />
<Compile Include="PDB\PDBTests.cs" />
<Compile Include="PDB\PDBTupleTests.cs" />
<Compile Include="PDB\PDBUsingTests.cs" />
Expand Down
153 changes: 153 additions & 0 deletions src/Compilers/CSharp/Test/Emit/PDB/PDBSourceLinkTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection.Metadata;
using System.Reflection.PortableExecutable;
using System.Text;
using Microsoft.CodeAnalysis.CSharp.Test.Utilities;
using Microsoft.CodeAnalysis.Debugging;
using Microsoft.CodeAnalysis.Emit;
using Microsoft.CodeAnalysis.Test.Utilities;
using Roslyn.Test.Utilities;
using Xunit;

namespace Microsoft.CodeAnalysis.CSharp.UnitTests.PDB
{
public class PDBSourceLinkTests : CSharpPDBTestBase
{
[Theory]
[InlineData(DebugInformationFormat.Pdb)]
[InlineData(DebugInformationFormat.PortablePdb)]
public void SourceLink(DebugInformationFormat format)
{
string source = @"
using System;

class C
{
public static void Main()
{
Console.WriteLine();
}
}
";
var sourceLinkBlob = Encoding.UTF8.GetBytes(@"
{
""documents"": {
Copy link
Member

@jcouv jcouv Apr 27, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: open brace on new line #WontFix

Copy link
Member Author

@tmat tmat Apr 27, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not C# ;) #WontFix

""f:/build/*"" : ""https://raw.githubusercontent.com/my-org/my-project/1111111111111111111111111111111111111111/*""
}
}
");

var c = CreateStandardCompilation(Parse(source, "f:/build/foo.cs"), options: TestOptions.DebugDll);

var pdbStream = new MemoryStream();
c.EmitToArray(EmitOptions.Default.WithDebugInformationFormat(format), pdbStream: pdbStream, sourceLinkStream: new MemoryStream(sourceLinkBlob));

var actualData = PdbValidation.GetSourceLinkData(pdbStream);
AssertEx.Equal(sourceLinkBlob, actualData);
}

[Fact]
public void SourceLink_Embedded()
{
string source = @"
using System;

class C
{
public static void Main()
{
Console.WriteLine();
}
}
";
var sourceLinkBlob = Encoding.UTF8.GetBytes(@"
{
""documents"": {
""f:/build/*"" : ""https://raw.githubusercontent.com/my-org/my-project/1111111111111111111111111111111111111111/*""
}
}
");
var c = CreateStandardCompilation(Parse(source, "f:/build/foo.cs"), options: TestOptions.DebugDll);

var peBlob = c.EmitToArray(EmitOptions.Default.WithDebugInformationFormat(DebugInformationFormat.Embedded), sourceLinkStream: new MemoryStream(sourceLinkBlob));

using (var peReader = new PEReader(peBlob))
{
var embeddedEntry = peReader.ReadDebugDirectory().Single(e => e.Type == DebugDirectoryEntryType.EmbeddedPortablePdb);

using (var embeddedMetadataProvider = peReader.ReadEmbeddedPortablePdbDebugDirectoryData(embeddedEntry))
{
var pdbReader = embeddedMetadataProvider.GetMetadataReader();

var actualBlob =
(from cdiHandle in pdbReader.GetCustomDebugInformation(EntityHandle.ModuleDefinition)
let cdi = pdbReader.GetCustomDebugInformation(cdiHandle)
where pdbReader.GetGuid(cdi.Kind) == PortableCustomDebugInfoKinds.SourceLink
select pdbReader.GetBlobBytes(cdi.Value)).Single();

AssertEx.Equal(sourceLinkBlob, actualBlob);
}
}
}

[Theory]
[InlineData(DebugInformationFormat.Pdb)]
[InlineData(DebugInformationFormat.Embedded)]
[InlineData(DebugInformationFormat.PortablePdb)]
public void SourceLink_Errors(DebugInformationFormat format)
{
string source = @"
using System;

class C
{
public static void Main()
{
Console.WriteLine();
}
}
";
var sourceLinkStream = new TestStream(canRead: true, readFunc: (_, __, ___) => { throw new Exception("Error!"); });
Copy link
Member

@jcouv jcouv Apr 27, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hopefully, discards will be supported in lambdas soon ;-) #Resolved

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1


In reply to: 113744881 [](ancestors = 113744881)


var c = CreateStandardCompilation(Parse(source, "f:/build/foo.cs"), options: TestOptions.DebugDll);
var pdbStream = format != DebugInformationFormat.Embedded ? new MemoryStream() : null;
var result = c.Emit(new MemoryStream(), pdbStream, options: EmitOptions.Default.WithDebugInformationFormat(format), sourceLinkStream: sourceLinkStream);
result.Diagnostics.Verify(
// error CS0041: Unexpected error writing debug information -- 'Error!'
Diagnostic(ErrorCode.FTL_DebugEmitFailure).WithArguments("Error!").WithLocation(1, 1));
}

[Theory]
[InlineData(DebugInformationFormat.Pdb)]
[InlineData(DebugInformationFormat.PortablePdb)]
public void SourceLink_Empty(DebugInformationFormat format)
{
string source = @"
using System;

class C
{
public static void Main()
{
Console.WriteLine();
}
}
";
var sourceLinkBlob = new byte[0];

var c = CreateStandardCompilation(Parse(source, "f:/build/foo.cs"), options: TestOptions.DebugDll);

var pdbStream = new MemoryStream();
c.EmitToArray(EmitOptions.Default.WithDebugInformationFormat(format), pdbStream: pdbStream, sourceLinkStream: new MemoryStream(sourceLinkBlob));
pdbStream.Position = 0;
Copy link
Member

@jcouv jcouv Apr 27, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this position reset necessary? Maybe GetSourceLinkData should handle the reset, if it doesn't already. #Resolved

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved to GetSourceLinkData.


In reply to: 113743925 [](ancestors = 113743925)

var bs = Roslyn.Utilities.StreamExtensions.ReadAllBytes(pdbStream);
var actualData = PdbValidation.GetSourceLinkData(pdbStream);
AssertEx.Equal(sourceLinkBlob, actualData);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -210,18 +210,6 @@ public void Emit_BadArgs()
options: EmitOptions.Default.WithDebugInformationFormat(DebugInformationFormat.PortablePdb),
sourceLinkStream: new TestStream(canRead: false, canWrite: true, canSeek: true)));

Assert.Throws<ArgumentException>("sourceLinkStream", () => comp.Emit(
peStream: new MemoryStream(),
pdbStream: new MemoryStream(),
options: EmitOptions.Default.WithDebugInformationFormat(DebugInformationFormat.Pdb),
sourceLinkStream: new MemoryStream()));

Assert.Throws<ArgumentException>("sourceLinkStream", () => comp.Emit(
peStream: new MemoryStream(),
pdbStream: null,
options: EmitOptions.Default.WithDebugInformationFormat(DebugInformationFormat.PortablePdb),
sourceLinkStream: new MemoryStream()));

Assert.Throws<ArgumentException>("embeddedTexts", () => comp.Emit(
peStream: new MemoryStream(),
pdbStream: new MemoryStream(),
Expand Down
Loading