| | | 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 | | |
| | | 4 | | using System.Collections.Generic; |
| | | 5 | | using System.Collections.Immutable; |
| | | 6 | | using System.Diagnostics; |
| | | 7 | | using System.Text; |
| | | 8 | | |
| | | 9 | | namespace System.Reflection.Metadata.Ecma335 |
| | | 10 | | { |
| | | 11 | | /// <summary> |
| | | 12 | | /// Builder of a Portable PDB image. |
| | | 13 | | /// </summary> |
| | | 14 | | public sealed class PortablePdbBuilder |
| | | 15 | | { |
| | 0 | 16 | | public string MetadataVersion => PortablePdbVersions.DefaultMetadataVersion; |
| | 0 | 17 | | public ushort FormatVersion => PortablePdbVersions.DefaultFormatVersion; |
| | | 18 | | |
| | | 19 | | private Blob _pdbIdBlob; |
| | | 20 | | private readonly MethodDefinitionHandle _entryPoint; |
| | | 21 | | private readonly MetadataBuilder _builder; |
| | | 22 | | private readonly SerializedMetadata _serializedMetadata; |
| | | 23 | | |
| | 0 | 24 | | public Func<IEnumerable<Blob>, BlobContentId> IdProvider { get; } |
| | | 25 | | |
| | | 26 | | /// <summary> |
| | | 27 | | /// Creates a builder of a Portable PDB image. |
| | | 28 | | /// </summary> |
| | | 29 | | /// <param name="tablesAndHeaps"> |
| | | 30 | | /// Builder populated with debug metadata entities stored in tables and values stored in heaps. |
| | | 31 | | /// The entities and values will be enumerated when serializing the Portable PDB image. |
| | | 32 | | /// </param> |
| | | 33 | | /// <param name="typeSystemRowCounts"> |
| | | 34 | | /// Row counts of all tables that the associated type-system metadata contain. |
| | | 35 | | /// Each slot in the array corresponds to a table (<see cref="TableIndex"/>). |
| | | 36 | | /// The length of the array must be equal to <see cref="MetadataTokens.TableCount"/>. |
| | | 37 | | /// </param> |
| | | 38 | | /// <param name="entryPoint"> |
| | | 39 | | /// Entry point method definition handle. |
| | | 40 | | /// </param> |
| | | 41 | | /// <param name="idProvider"> |
| | | 42 | | /// Function calculating id of content represented as a sequence of blobs. |
| | | 43 | | /// If not specified a default function that ignores the content and returns current time-based content id is us |
| | | 44 | | /// (<see cref="BlobContentId.GetTimeBasedProvider()"/>). |
| | | 45 | | /// You must specify a deterministic function to produce a deterministic Portable PDB image. |
| | | 46 | | /// </param> |
| | | 47 | | /// <exception cref="ArgumentNullException"><paramref name="tablesAndHeaps"/> or <paramref name="typeSystemRowCo |
| | 0 | 48 | | public PortablePdbBuilder( |
| | 0 | 49 | | MetadataBuilder tablesAndHeaps, |
| | 0 | 50 | | ImmutableArray<int> typeSystemRowCounts, |
| | 0 | 51 | | MethodDefinitionHandle entryPoint, |
| | 0 | 52 | | Func<IEnumerable<Blob>, BlobContentId>? idProvider = null) |
| | 0 | 53 | | { |
| | 0 | 54 | | if (tablesAndHeaps is null) |
| | 0 | 55 | | { |
| | 0 | 56 | | Throw.ArgumentNull(nameof(tablesAndHeaps)); |
| | | 57 | | } |
| | | 58 | | |
| | 0 | 59 | | ValidateTypeSystemRowCounts(typeSystemRowCounts); |
| | | 60 | | |
| | 0 | 61 | | _builder = tablesAndHeaps; |
| | 0 | 62 | | _entryPoint = entryPoint; |
| | | 63 | | |
| | 0 | 64 | | Debug.Assert(Encoding.UTF8.GetByteCount(MetadataVersion) == MetadataVersion.Length); |
| | 0 | 65 | | _serializedMetadata = tablesAndHeaps.GetSerializedMetadata(typeSystemRowCounts, MetadataVersion.Length, isSt |
| | | 66 | | |
| | 0 | 67 | | IdProvider = idProvider ?? BlobContentId.GetTimeBasedProvider(); |
| | 0 | 68 | | } |
| | | 69 | | |
| | | 70 | | private static void ValidateTypeSystemRowCounts(ImmutableArray<int> typeSystemRowCounts) |
| | 0 | 71 | | { |
| | 0 | 72 | | if (typeSystemRowCounts.IsDefault) |
| | 0 | 73 | | { |
| | 0 | 74 | | Throw.ArgumentNull(nameof(typeSystemRowCounts)); |
| | | 75 | | } |
| | | 76 | | |
| | 0 | 77 | | if (typeSystemRowCounts.Length != MetadataTokens.TableCount) |
| | 0 | 78 | | { |
| | 0 | 79 | | throw new ArgumentException(SR.Format(SR.ExpectedArrayOfSize, MetadataTokens.TableCount), nameof(typeSys |
| | | 80 | | } |
| | | 81 | | |
| | 0 | 82 | | for (int i = 0; i < typeSystemRowCounts.Length; i++) |
| | 0 | 83 | | { |
| | 0 | 84 | | if (typeSystemRowCounts[i] == 0) |
| | 0 | 85 | | { |
| | 0 | 86 | | continue; |
| | | 87 | | } |
| | | 88 | | |
| | 0 | 89 | | if ((unchecked((uint)typeSystemRowCounts[i]) & ~TokenTypeIds.RIDMask) != 0) |
| | 0 | 90 | | { |
| | 0 | 91 | | throw new ArgumentOutOfRangeException(nameof(typeSystemRowCounts), SR.Format(SR.RowCountOutOfRange, |
| | | 92 | | } |
| | | 93 | | |
| | 0 | 94 | | if (((1UL << i) & (ulong)TableMask.ValidPortablePdbExternalTables) == 0) |
| | 0 | 95 | | { |
| | 0 | 96 | | throw new ArgumentException(SR.Format(SR.RowCountMustBeZero, i), nameof(typeSystemRowCounts)); |
| | | 97 | | } |
| | 0 | 98 | | } |
| | 0 | 99 | | } |
| | | 100 | | |
| | | 101 | | /// <summary> |
| | | 102 | | /// Serialized #Pdb stream. |
| | | 103 | | /// </summary> |
| | | 104 | | private void SerializeStandalonePdbStream(BlobBuilder builder) |
| | 0 | 105 | | { |
| | 0 | 106 | | int startPosition = builder.Count; |
| | | 107 | | |
| | | 108 | | // the id will be filled in later |
| | 0 | 109 | | _pdbIdBlob = builder.ReserveBytes(MetadataSizes.PdbIdSize); |
| | | 110 | | |
| | 0 | 111 | | builder.WriteInt32(_entryPoint.IsNil ? 0 : MetadataTokens.GetToken(_entryPoint)); |
| | | 112 | | |
| | 0 | 113 | | builder.WriteUInt64(_serializedMetadata.Sizes.ExternalTablesMask); |
| | 0 | 114 | | MetadataWriterUtilities.SerializeRowCounts(builder, _serializedMetadata.Sizes.ExternalRowCounts); |
| | | 115 | | |
| | 0 | 116 | | int endPosition = builder.Count; |
| | 0 | 117 | | Debug.Assert(_serializedMetadata.Sizes.CalculateStandalonePdbStreamSize() == endPosition - startPosition); |
| | 0 | 118 | | } |
| | | 119 | | |
| | | 120 | | /// <summary> |
| | | 121 | | /// Serializes Portable PDB content into the given <see cref="BlobBuilder"/>. |
| | | 122 | | /// </summary> |
| | | 123 | | /// <param name="builder">Builder to write to.</param> |
| | | 124 | | /// <returns>The id of the serialized content.</returns> |
| | | 125 | | /// <exception cref="ArgumentNullException"><paramref name="builder"/> is null.</exception> |
| | | 126 | | public BlobContentId Serialize(BlobBuilder builder) |
| | 0 | 127 | | { |
| | 0 | 128 | | if (builder is null) |
| | 0 | 129 | | { |
| | 0 | 130 | | Throw.ArgumentNull(nameof(builder)); |
| | | 131 | | } |
| | | 132 | | |
| | | 133 | | // header: |
| | 0 | 134 | | MetadataBuilder.SerializeMetadataHeader(builder, MetadataVersion, _serializedMetadata.Sizes); |
| | | 135 | | |
| | | 136 | | // #Pdb stream |
| | 0 | 137 | | SerializeStandalonePdbStream(builder); |
| | | 138 | | |
| | | 139 | | // #~ or #- stream: |
| | 0 | 140 | | _builder.SerializeMetadataTables(builder, _serializedMetadata.Sizes, _serializedMetadata.StringMap, methodBo |
| | | 141 | | |
| | | 142 | | // #Strings, #US, #Guid and #Blob streams: |
| | 0 | 143 | | _builder.WriteHeapsTo(builder, _serializedMetadata.StringHeap); |
| | | 144 | | |
| | 0 | 145 | | var contentId = IdProvider(builder.GetBlobs()); |
| | | 146 | | |
| | | 147 | | // fill in the id: |
| | 0 | 148 | | var idWriter = new BlobWriter(_pdbIdBlob); |
| | 0 | 149 | | idWriter.WriteGuid(contentId.Guid); |
| | 0 | 150 | | idWriter.WriteUInt32(contentId.Stamp); |
| | 0 | 151 | | Debug.Assert(idWriter.RemainingBytes == 0); |
| | | 152 | | |
| | 0 | 153 | | return contentId; |
| | 0 | 154 | | } |
| | | 155 | | } |
| | | 156 | | } |
| | | 157 | | |