|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | +// See the LICENSE file in the project root for more information. |
| 4 | + |
| 5 | +using System; |
| 6 | +using System.Collections; |
| 7 | +using System.Collections.Generic; |
| 8 | +using System.IO; |
| 9 | +using System.Linq; |
| 10 | +using System.Reflection.Metadata; |
| 11 | +using System.Reflection.PortableExecutable; |
| 12 | +using Microsoft.Build.Framework; |
| 13 | +using Microsoft.Build.Utilities; |
| 14 | + |
| 15 | +namespace Microsoft.DotNet.Build.Tasks |
| 16 | +{ |
| 17 | + // Creates a symbols layout that matches the SDK layout |
| 18 | + public class CreateSdkSymbolsLayout : Task |
| 19 | + { |
| 20 | + /// <summary> |
| 21 | + /// Path to SDK layout. |
| 22 | + /// </summary> |
| 23 | + [Required] |
| 24 | + public string SdkLayoutPath { get; set; } |
| 25 | + |
| 26 | + /// <summary> |
| 27 | + /// Path to all source-built symbols, flat or with folder hierarchy. |
| 28 | + /// </summary> |
| 29 | + [Required] |
| 30 | + public string AllSymbolsPath { get; set; } |
| 31 | + |
| 32 | + /// <summary> |
| 33 | + /// Path to SDK symbols layout - will be created if it doesn't exist. |
| 34 | + /// </summary> |
| 35 | + [Required] |
| 36 | + public string SdkSymbolsLayoutPath { get; set; } |
| 37 | + |
| 38 | + /// <summary> |
| 39 | + /// If true, fails the build if any PDBs are missing. |
| 40 | + /// </summary> |
| 41 | + public bool FailOnMissingPDBs { get; set; } |
| 42 | + |
| 43 | + public override bool Execute() |
| 44 | + { |
| 45 | + IList<string> filesWithoutPDBs = GenerateSymbolsLayout(IndexAllSymbols()); |
| 46 | + if (filesWithoutPDBs.Count > 0) |
| 47 | + { |
| 48 | + LogErrorOrWarning(FailOnMissingPDBs, $"Did not find PDBs for the following SDK files:"); |
| 49 | + foreach (string file in filesWithoutPDBs) |
| 50 | + { |
| 51 | + LogErrorOrWarning(FailOnMissingPDBs, file); |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + return !Log.HasLoggedErrors; |
| 56 | + } |
| 57 | + |
| 58 | + private void LogErrorOrWarning(bool isError, string message) |
| 59 | + { |
| 60 | + if (FailOnMissingPDBs) |
| 61 | + { |
| 62 | + Log.LogError(message); |
| 63 | + } |
| 64 | + else |
| 65 | + { |
| 66 | + Log.LogWarning(message); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + private IList<string> GenerateSymbolsLayout(Hashtable allPdbGuids) |
| 71 | + { |
| 72 | + List<string> filesWithoutPDBs = new List<string>(); |
| 73 | + |
| 74 | + if (Directory.Exists(SdkSymbolsLayoutPath)) |
| 75 | + { |
| 76 | + Directory.Delete(SdkSymbolsLayoutPath, true); |
| 77 | + } |
| 78 | + |
| 79 | + foreach (string file in Directory.GetFiles(SdkLayoutPath, "*", SearchOption.AllDirectories)) |
| 80 | + { |
| 81 | + if (file.EndsWith(".dll", StringComparison.InvariantCultureIgnoreCase) && |
| 82 | + !file.EndsWith(".resources.dll", StringComparison.InvariantCultureIgnoreCase)) |
| 83 | + { |
| 84 | + string guid = string.Empty; |
| 85 | + using var pdbStream = File.OpenRead(file); |
| 86 | + using var peReader = new PEReader(pdbStream); |
| 87 | + try |
| 88 | + { |
| 89 | + // Check if pdb is embedded |
| 90 | + if (peReader.ReadDebugDirectory().Any(entry => entry.Type == DebugDirectoryEntryType.EmbeddedPortablePdb)) |
| 91 | + { |
| 92 | + continue; |
| 93 | + } |
| 94 | + |
| 95 | + var debugDirectory = peReader.ReadDebugDirectory().First(entry => entry.Type == DebugDirectoryEntryType.CodeView); |
| 96 | + var codeViewData = peReader.ReadCodeViewDebugDirectoryData(debugDirectory); |
| 97 | + guid = $"{codeViewData.Guid.ToString("N").Replace("-", string.Empty)}"; |
| 98 | + } |
| 99 | + catch (Exception e) when (e is BadImageFormatException || e is InvalidOperationException) |
| 100 | + { |
| 101 | + // Ignore binaries without debug info |
| 102 | + continue; |
| 103 | + } |
| 104 | + |
| 105 | + if (guid != string.Empty) |
| 106 | + { |
| 107 | + if (!allPdbGuids.ContainsKey(guid)) |
| 108 | + { |
| 109 | + filesWithoutPDBs.Add(file.Substring(SdkLayoutPath.Length + 1)); |
| 110 | + } |
| 111 | + else |
| 112 | + { |
| 113 | + // Copy matching pdb to symbols path, preserving sdk binary's hierarchy |
| 114 | + string sourcePath = (string)allPdbGuids[guid]!; |
| 115 | + string destinationPath = |
| 116 | + file.Replace(SdkLayoutPath, SdkSymbolsLayoutPath) |
| 117 | + .Replace(Path.GetFileName(file), Path.GetFileName(sourcePath)); |
| 118 | + |
| 119 | + Directory.CreateDirectory(Path.GetDirectoryName(destinationPath)!); |
| 120 | + File.Copy(sourcePath, destinationPath, true); |
| 121 | + } |
| 122 | + } |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + return filesWithoutPDBs; |
| 127 | + } |
| 128 | + |
| 129 | + public Hashtable IndexAllSymbols() |
| 130 | + { |
| 131 | + Hashtable allPdbGuids = new Hashtable(); |
| 132 | + |
| 133 | + foreach (string file in Directory.GetFiles(AllSymbolsPath, "*.pdb", SearchOption.AllDirectories)) |
| 134 | + { |
| 135 | + using var pdbFileStream = File.OpenRead(file); |
| 136 | + var metadataProvider = MetadataReaderProvider.FromPortablePdbStream(pdbFileStream); |
| 137 | + var metadataReader = metadataProvider.GetMetadataReader(); |
| 138 | + if (metadataReader.DebugMetadataHeader == null) |
| 139 | + { |
| 140 | + continue; |
| 141 | + } |
| 142 | + |
| 143 | + var id = new BlobContentId(metadataReader.DebugMetadataHeader.Id); |
| 144 | + string guid = $"{id.Guid:N}"; |
| 145 | + if (!string.IsNullOrEmpty(guid) && !allPdbGuids.ContainsKey(guid)) |
| 146 | + { |
| 147 | + allPdbGuids.Add(guid, file); |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + return allPdbGuids; |
| 152 | + } |
| 153 | + } |
| 154 | +} |
0 commit comments