-
Notifications
You must be signed in to change notification settings - Fork 35
/
PdbConverterPortableToWindows.cs
1262 lines (1077 loc) · 56.4 KB
/
PdbConverterPortableToWindows.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the License.txt file in the project root for more information.
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Reflection.Metadata;
using System.Reflection.Metadata.Ecma335;
using System.Reflection.PortableExecutable;
using System.Runtime.CompilerServices;
using System.Text;
using System.Text.Json;
using System.Text.RegularExpressions;
using Microsoft.CodeAnalysis.Collections;
using Microsoft.CodeAnalysis.Debugging;
using Microsoft.CodeAnalysis.PooledObjects;
using Microsoft.DiaSymReader.PortablePdb;
using Microsoft.SourceLink.Tools;
using Roslyn.Utilities;
namespace Microsoft.DiaSymReader.Tools
{
internal sealed class PdbConverterPortableToWindows
{
private readonly Action<PdbDiagnostic>? _diagnosticReporter;
public PdbConverterPortableToWindows(Action<PdbDiagnostic>? diagnosticReporter)
{
_diagnosticReporter = diagnosticReporter;
}
private void ReportDiagnostic(PdbDiagnosticId id, int token, params object[] args)
{
_diagnosticReporter?.Invoke(new PdbDiagnostic(id, token, args));
}
/// <summary>
/// This is the maximum length of a string in the PDB, assuming it is in UTF-8 format
/// and not (yet) null-terminated.
/// </summary>
/// <remarks>
/// Used for import strings, locals, and local constants.
/// </remarks>
private const int MaxEntityNameLength = 2046;
private static Guid GetLanguageVendorGuid(Guid languageGuid)
{
return (languageGuid == PdbGuids.Language.CSharp || languageGuid == PdbGuids.Language.VisualBasic || languageGuid == PdbGuids.Language.FSharp) ?
PdbGuids.LanguageVendor.Microsoft : default;
}
private readonly struct LocalScopeInfo
{
public readonly LocalScope? LocalScope;
public readonly int HoistedVariableStartIndex;
public readonly int HoistedVariableEndIndex;
public LocalScopeInfo(LocalScope? localScope, int hoistedVariableStartIndex, int hoistedVariableEndIndex)
{
LocalScope = localScope;
HoistedVariableStartIndex = hoistedVariableStartIndex;
HoistedVariableEndIndex = hoistedVariableEndIndex;
}
public bool IsDefault => !IsLocalScope && !IsHoistedScope;
public bool IsLocalScope => LocalScope.HasValue;
public bool IsHoistedScope => HoistedVariableStartIndex < HoistedVariableEndIndex;
}
internal void Convert(PEReader peReader, MetadataReader pdbReader, SymUnmanagedWriter pdbWriter, PortablePdbConversionOptions options)
{
if (!SymReaderHelpers.TryReadPdbId(peReader, out var pePdbId, out int peAge))
{
throw new InvalidDataException(ConverterResources.SpecifiedPEFileHasNoAssociatedPdb);
}
if (pdbReader.DebugMetadataHeader == null || !new BlobContentId(pdbReader.DebugMetadataHeader.Id).Equals(pePdbId))
{
throw new InvalidDataException(ConverterResources.PdbNotMatchingDebugDirectory);
}
string? vbDefaultNamespace = MetadataUtilities.GetVisualBasicDefaultNamespace(pdbReader);
bool vbSemantics = vbDefaultNamespace != null;
string? vbDefaultNamespaceImportString = string.IsNullOrEmpty(vbDefaultNamespace) ? null : "*" + vbDefaultNamespace;
var metadataReader = peReader.GetMetadataReader();
var metadataModel = new MetadataModel(metadataReader, vbSemantics);
var nonEmbeddedDocumentNames = new ArrayBuilder<string>(pdbReader.Documents.Count);
var symSequencePointsWriter = new SymUnmanagedSequencePointsWriter(pdbWriter, capacity: 64);
var declaredExternAliases = new HashSet<string>();
var importStringsBuilder = new List<string>();
var importGroups = new List<int>();
var cdiBuilder = new BlobBuilder();
var dynamicLocals = new List<(string LocalName, byte[] Flags, int Count, int SlotIndex)>();
var tupleLocals = new List<(string LocalName, int SlotIndex, int ScopeStart, int ScopeEnd, ImmutableArray<string?> Names)>();
var openScopeEndOffsets = new Stack<int>();
// state for calculating import string forwarding:
var lastImportScopeHandle = default(ImportScopeHandle);
var vbLastImportScopeNamespace = default(string);
var lastImportScopeMethodDefHandle = default(MethodDefinitionHandle);
var importStringsMap = new Dictionary<ImmutableArray<string>, MethodDefinitionHandle>(SequenceComparer<string>.Instance);
var aliasedAssemblyRefs = GetAliasedAssemblyRefs(pdbReader);
pdbWriter.DocumentTableCapacity = pdbReader.Documents.Count;
foreach (var documentHandle in pdbReader.Documents)
{
var document = pdbReader.GetDocument(documentHandle);
var languageGuid = pdbReader.GetGuid(document.Language);
var name = pdbReader.GetString(document.Name);
var embeddedSourceHandle = pdbReader.GetCustomDebugInformation(documentHandle, PortableCustomDebugInfoKinds.EmbeddedSource);
var sourceBlob = embeddedSourceHandle.IsNil ? null : pdbReader.GetBlobBytes(embeddedSourceHandle);
if (embeddedSourceHandle.IsNil)
{
nonEmbeddedDocumentNames.Add(name);
}
var algorithmId = pdbReader.GetGuid(document.HashAlgorithm);
var checksum = pdbReader.GetBlobBytes(document.Hash);
ValidateAndCorrectSourceChecksum(ref algorithmId, checksum, name);
pdbWriter.DefineDocument(
name: name,
language: languageGuid,
vendor: GetLanguageVendorGuid(languageGuid),
type: PdbGuids.DocumentType.Text,
algorithmId: algorithmId,
checksum: checksum,
source: sourceBlob);
}
int localScopeRowNumber = 0;
int localScopeCount = pdbReader.GetTableRowCount(TableIndex.LocalScope);
static int CompareScopeRanges(int leftStart, int leftEnd, int rightStart, int rightEnd)
{
int result = leftStart.CompareTo(rightStart);
return (result != 0) ? result : rightEnd.CompareTo(leftEnd);
}
// Handle of the method that is gonna contain list of AssemblyRef aliases.
// Other methods will forward to it.
var methodDefHandleWithAssemblyRefAliases = default(MethodDefinitionHandle);
foreach (var methodDebugInfoHandle in pdbReader.MethodDebugInformation)
{
var methodDebugInfo = pdbReader.GetMethodDebugInformation(methodDebugInfoHandle);
var methodDefHandle = methodDebugInfoHandle.ToDefinitionHandle();
int methodToken = MetadataTokens.GetToken(methodDefHandle);
var methodDef = metadataReader.GetMethodDefinition(methodDefHandle);
#if DEBUG
var declaringTypeDef = metadataReader.GetTypeDefinition(methodDef.GetDeclaringType());
var typeName = metadataReader.GetString(declaringTypeDef.Name);
var methodName = metadataReader.GetString(methodDef.Name);
#endif
bool methodOpened = false;
var methodBodyOpt = (methodDef.RelativeVirtualAddress != 0 && (methodDef.ImplAttributes & MethodImplAttributes.CodeTypeMask) == MethodImplAttributes.Managed) ?
peReader.GetMethodBody(methodDef.RelativeVirtualAddress) : null;
var vbCurrentMethodNamespace = vbSemantics ? GetMethodNamespace(metadataReader, methodDef) : null;
var moveNextHandle = metadataModel.FindStateMachineMoveNextMethod(methodDefHandle, vbSemantics);
bool isKickOffMethod = !moveNextHandle.IsNil;
var hoistedLocalScopes = isKickOffMethod ? default : GetStateMachineHoistedLocalScopes(pdbReader, methodDefHandle);
int vbHoistedScopeIndex = -1;
ImmutableArray<(StateMachineHoistedLocalScope Scope, int Index)> vbHoistedScopes;
// Names of local variables hoisted to state machine fields indexed by variable slot index extracted from the field name.
ImmutableArray<string> vbResumableLocalNames;
if (vbSemantics && !hoistedLocalScopes.IsDefault)
{
// we are in MoveNext method
vbHoistedScopes = hoistedLocalScopes.SelectWithIndex().Where(s => !s.Value.IsDefault).ToImmutableArray().
Sort((left, right) => CompareScopeRanges(left.Value.StartOffset, left.Value.EndOffset, right.Value.StartOffset, right.Value.EndOffset));
vbResumableLocalNames = metadataModel.GetVisualBasicHoistedLocalNames(methodDef.GetDeclaringType());
}
else
{
vbHoistedScopes = default;
vbResumableLocalNames = default;
}
var forwardImportScopesToMethodDef = default(MethodDefinitionHandle);
Debug.Assert(dynamicLocals.Count == 0);
Debug.Assert(tupleLocals.Count == 0);
Debug.Assert(openScopeEndOffsets.Count == 0);
void LazyOpenMethod()
{
if (!methodOpened)
{
#if DEBUG
Debug.WriteLine($"Open Method '{typeName}::{methodName}' {methodToken:X8}");
#endif
pdbWriter.OpenMethod(methodToken);
methodOpened = true;
}
}
LocalScopeInfo GetNextLocalScope()
{
Debug.Assert(localScopeRowNumber <= localScopeCount);
LocalScope? nextLocalScope = null;
int nextLocalScopeIndex = localScopeRowNumber + 1;
if (nextLocalScopeIndex <= localScopeCount)
{
var scope = pdbReader.GetLocalScope(MetadataTokens.LocalScopeHandle(nextLocalScopeIndex));
if (scope.Method == methodDefHandle)
{
nextLocalScope = scope;
}
}
StateMachineHoistedLocalScope? nextHoistedScope = null;
int nextHoistedScopeIndex = vbHoistedScopeIndex + 1;
if (!vbHoistedScopes.IsDefault && nextHoistedScopeIndex < vbHoistedScopes.Length)
{
nextHoistedScope = vbHoistedScopes[nextHoistedScopeIndex].Scope;
}
int c = nextLocalScope == null ? +1 :
nextHoistedScope == null ? -1 :
CompareScopeRanges(nextLocalScope.Value.StartOffset, nextLocalScope.Value.EndOffset, nextHoistedScope.Value.StartOffset, nextHoistedScope.Value.EndOffset);
int hoistedScopeStartIndex = 0;
int hoistedScopeEndIndex = 0;
if (c <= 0 && nextLocalScope != null)
{
localScopeRowNumber = nextLocalScopeIndex;
}
else
{
nextLocalScope = null;
}
if (c >= 0 && nextHoistedScope != null)
{
static bool ScopeEquals(StateMachineHoistedLocalScope left, StateMachineHoistedLocalScope right)
=> left.StartOffset == right.StartOffset && left.EndOffset == right.EndOffset;
// determine all hoisted variables that have equal scopes:
int i = nextHoistedScopeIndex + 1;
while (i < vbHoistedScopes.Length && ScopeEquals(nextHoistedScope.Value, vbHoistedScopes[i].Scope))
{
i++;
}
hoistedScopeStartIndex = nextHoistedScopeIndex;
hoistedScopeEndIndex = i;
vbHoistedScopeIndex = i - 1;
}
else
{
hoistedScopeStartIndex = hoistedScopeEndIndex = 0;
}
return new LocalScopeInfo(nextLocalScope, hoistedScopeStartIndex, hoistedScopeEndIndex);
}
void CloseOpenScopes(int currentScopeStartOffset)
{
// close all open scopes that end before this scope starts:
while (openScopeEndOffsets.Count > 0 && currentScopeStartOffset >= openScopeEndOffsets.Peek())
{
int scopeEnd = openScopeEndOffsets.Pop();
Debug.WriteLine($"Close Scope [.., {scopeEnd})");
// Note that the root scope end is not end-inclusive in VB:
pdbWriter.CloseScope(AdjustEndScopeOffset(scopeEnd, isEndInclusive: vbSemantics && openScopeEndOffsets.Count > 0));
}
}
bool hasAnyScopes = false;
void OpenScope(int startOffset, int endOffset)
{
CloseOpenScopes(startOffset);
Debug.WriteLine($"Open Scope [{startOffset}, {endOffset})");
pdbWriter.OpenScope(startOffset);
openScopeEndOffsets.Push(endOffset);
hasAnyScopes = true;
}
bool isFirstMethodLocalScope = true;
while (true)
{
var currentLocalScope = GetNextLocalScope();
if (currentLocalScope.IsDefault)
{
break;
}
// kickoff methods don't have any scopes emitted to Windows PDBs
if (methodBodyOpt == null)
{
ReportDiagnostic(PdbDiagnosticId.MethodAssociatedWithLocalScopeHasNoBody, MetadataTokens.GetToken(MetadataTokens.LocalScopeHandle(localScopeRowNumber)));
}
else if (!isKickOffMethod)
{
LazyOpenMethod();
if (currentLocalScope.IsLocalScope)
{
var localScope = currentLocalScope.LocalScope!.Value;
OpenScope(localScope.StartOffset, localScope.EndOffset);
if (isFirstMethodLocalScope)
{
if (lastImportScopeHandle == localScope.ImportScope && vbLastImportScopeNamespace == vbCurrentMethodNamespace)
{
// forward to a method that has the same imports:
forwardImportScopesToMethodDef = lastImportScopeMethodDefHandle;
}
else
{
Debug.Assert(importStringsBuilder.Count == 0);
Debug.Assert(declaredExternAliases.Count == 0);
Debug.Assert(importGroups.Count == 0);
AddImportStrings(importStringsBuilder, importGroups, declaredExternAliases, pdbReader, metadataModel, localScope.ImportScope, aliasedAssemblyRefs, vbDefaultNamespaceImportString, vbCurrentMethodNamespace, vbSemantics);
var importStrings = importStringsBuilder.ToImmutableArray();
importStringsBuilder.Clear();
if (importStringsMap.TryGetValue(importStrings, out forwardImportScopesToMethodDef))
{
// forward to a method that has the same imports:
lastImportScopeMethodDefHandle = forwardImportScopesToMethodDef;
}
else
{
// attach import strings to the current method:
WriteImports(pdbWriter, importStrings);
lastImportScopeMethodDefHandle = methodDefHandle;
importStringsMap[importStrings] = methodDefHandle;
}
lastImportScopeHandle = localScope.ImportScope;
vbLastImportScopeNamespace = vbCurrentMethodNamespace;
}
if (vbSemantics && !forwardImportScopesToMethodDef.IsNil)
{
pdbWriter.UsingNamespace("@" + MetadataTokens.GetToken(forwardImportScopesToMethodDef));
}
// This is the method that's gonna have AssemblyRef aliases attached:
if (methodDefHandleWithAssemblyRefAliases.IsNil)
{
foreach (var (assemblyRefHandle, alias) in aliasedAssemblyRefs)
{
var assemblyRef = metadataReader.GetAssemblyReference(assemblyRefHandle);
pdbWriter.UsingNamespace("Z" + alias + " " + AssemblyDisplayNameBuilder.GetAssemblyDisplayName(metadataReader, assemblyRef));
}
}
}
foreach (var localVariableHandle in localScope.GetLocalVariables())
{
var variable = pdbReader.GetLocalVariable(localVariableHandle);
string name = pdbReader.GetString(variable.Name);
if (name.Length > MaxEntityNameLength)
{
ReportDiagnostic(PdbDiagnosticId.LocalConstantNameTooLong, MetadataTokens.GetToken(localVariableHandle));
continue;
}
if (methodBodyOpt.LocalSignature.IsNil)
{
ReportDiagnostic(PdbDiagnosticId.MethodContainingLocalVariablesHasNoLocalSignature, methodToken);
continue;
}
pdbWriter.DefineLocalVariable(variable.Index, name, (int)variable.Attributes, MetadataTokens.GetToken(methodBodyOpt.LocalSignature));
var dynamicFlags = MetadataUtilities.ReadDynamicCustomDebugInformation(pdbReader, localVariableHandle);
if (TryGetDynamicLocal(name, variable.Index, dynamicFlags, out var dynamicLocal))
{
dynamicLocals.Add(dynamicLocal);
}
var tupleElementNames = MetadataUtilities.ReadTupleCustomDebugInformation(pdbReader, localVariableHandle);
if (!tupleElementNames.IsDefaultOrEmpty)
{
tupleLocals.Add((name, SlotIndex: variable.Index, ScopeStart: 0, ScopeEnd: 0, Names: tupleElementNames));
}
}
foreach (var localConstantHandle in localScope.GetLocalConstants())
{
var constant = pdbReader.GetLocalConstant(localConstantHandle);
string name = pdbReader.GetString(constant.Name);
if (name.Length > MaxEntityNameLength)
{
ReportDiagnostic(PdbDiagnosticId.LocalConstantNameTooLong, MetadataTokens.GetToken(localConstantHandle));
continue;
}
var (value, signature) = PortableConstantSignature.GetConstantValueAndSignature(pdbReader, localConstantHandle, metadataReader.GetQualifiedTypeName);
if (!metadataModel.TryGetStandaloneSignatureHandle(signature, out var constantSignatureHandle))
{
// Signature will be unspecified. At least we store the name and the value.
constantSignatureHandle = default;
}
pdbWriter.DefineLocalConstant(name, value, MetadataTokens.GetToken(constantSignatureHandle));
var dynamicFlags = MetadataUtilities.ReadDynamicCustomDebugInformation(pdbReader, localConstantHandle);
if (TryGetDynamicLocal(name, 0, dynamicFlags, out var dynamicLocal))
{
dynamicLocals.Add(dynamicLocal);
}
var tupleElementNames = MetadataUtilities.ReadTupleCustomDebugInformation(pdbReader, localConstantHandle);
if (!tupleElementNames.IsDefaultOrEmpty)
{
// Note that the end offset of tuple locals is always end-exclusive, regardless of whether the PDB uses VB semantics or not.
tupleLocals.Add((name, SlotIndex: -1, ScopeStart: localScope.StartOffset, ScopeEnd: localScope.EndOffset, Names: tupleElementNames));
}
}
}
if (currentLocalScope.IsHoistedScope)
{
Debug.Assert(vbSemantics);
Debug.Assert(!vbResumableLocalNames.IsDefault);
bool scopeOpened = currentLocalScope.IsLocalScope;
// add $VB$ResumableLocal pseudo-variables:
for (int i = currentLocalScope.HoistedVariableStartIndex; i < currentLocalScope.HoistedVariableEndIndex; i++)
{
var (scope, index) = vbHoistedScopes[i];
if (index >= vbResumableLocalNames.Length)
{
// TODO: report error
continue;
}
if (!scopeOpened)
{
OpenScope(scope.StartOffset, scope.EndOffset);
scopeOpened = true;
}
pdbWriter.DefineLocalVariable(
index,
vbResumableLocalNames[index],
attributes: 0,
localSignatureToken: MetadataTokens.GetToken(methodBodyOpt.LocalSignature));
}
}
}
isFirstMethodLocalScope = false;
}
CloseOpenScopes(int.MaxValue);
if (openScopeEndOffsets.Count > 0)
{
ReportDiagnostic(PdbDiagnosticId.LocalScopeRangesNestingIsInvalid, methodToken);
openScopeEndOffsets.Clear();
}
if (!methodDebugInfo.SequencePointsBlob.IsNil)
{
LazyOpenMethod();
WriteSequencePoints(symSequencePointsWriter, methodDebugInfo.GetSequencePoints(), pdbReader.Documents.Count, methodToken);
}
// async method data:
var asyncData = MetadataUtilities.ReadAsyncMethodData(pdbReader, methodDebugInfoHandle);
if (!asyncData.IsNone)
{
LazyOpenMethod();
pdbWriter.SetAsyncInfo(
moveNextMethodToken: methodToken,
kickoffMethodToken: MetadataTokens.GetToken(asyncData.KickoffMethod),
catchHandlerOffset: asyncData.CatchHandlerOffset,
yieldOffsets: asyncData.YieldOffsets.ToArray(),
resumeOffsets: asyncData.ResumeOffsets.ToArray());
}
// custom debug information:
var cdiEncoder = new CustomDebugInfoEncoder(cdiBuilder);
if (isKickOffMethod)
{
cdiEncoder.AddStateMachineTypeName(GetIteratorTypeName(metadataReader, moveNextHandle));
}
else
{
if (!vbSemantics && hasAnyScopes)
{
if (forwardImportScopesToMethodDef.IsNil)
{
// record the number of import strings in each scope:
cdiEncoder.AddUsingGroups(importGroups);
if (!methodDefHandleWithAssemblyRefAliases.IsNil)
{
// forward assembly ref aliases to the first method:
cdiEncoder.AddForwardModuleInfo(methodDefHandleWithAssemblyRefAliases);
}
}
else
{
// forward all imports to another method:
cdiEncoder.AddForwardMethodInfo(forwardImportScopesToMethodDef);
}
}
if (!vbSemantics && !hoistedLocalScopes.IsDefault)
{
cdiEncoder.AddStateMachineHoistedLocalScopes(hoistedLocalScopes);
}
if (dynamicLocals.Count > 0)
{
cdiEncoder.AddDynamicLocals(dynamicLocals);
dynamicLocals.Clear();
}
if (tupleLocals.Count > 0)
{
cdiEncoder.AddTupleElementNames(tupleLocals);
tupleLocals.Clear();
}
}
importGroups.Clear();
// the following blobs map 1:1
CopyCustomDebugInfoRecord(ref cdiEncoder, pdbReader, methodDefHandle, PortableCustomDebugInfoKinds.EncLocalSlotMap, CustomDebugInfoKind.EditAndContinueLocalSlotMap);
CopyCustomDebugInfoRecord(ref cdiEncoder, pdbReader, methodDefHandle, PortableCustomDebugInfoKinds.EncLambdaAndClosureMap, CustomDebugInfoKind.EditAndContinueLambdaMap);
CopyCustomDebugInfoRecord(ref cdiEncoder, pdbReader, methodDefHandle, SymReaderHelpers.EncStateMachineSuspensionPoints, SymReaderHelpers.CustomDebugInfoKind_EncStateMachineSuspensionPoints);
if (cdiEncoder.RecordCount > 0)
{
LazyOpenMethod();
pdbWriter.DefineCustomMetadata(cdiEncoder.ToArray());
}
cdiBuilder.Clear();
if (methodOpened && aliasedAssemblyRefs.Length > 0 && !isKickOffMethod && methodDefHandleWithAssemblyRefAliases.IsNil)
{
methodDefHandleWithAssemblyRefAliases = methodDefHandle;
}
if (methodOpened)
{
Debug.WriteLine($"Close Method {methodToken:X8}");
pdbWriter.CloseMethod();
}
}
if (!pdbReader.DebugMetadataHeader.EntryPoint.IsNil)
{
pdbWriter.SetEntryPoint(MetadataTokens.GetToken(pdbReader.DebugMetadataHeader.EntryPoint));
}
var sourceLinkHandle = pdbReader.GetCustomDebugInformation(EntityHandle.ModuleDefinition, PortableCustomDebugInfoKinds.SourceLink);
if (!sourceLinkHandle.IsNil)
{
// always include Source Link
pdbWriter.SetSourceLinkData(pdbReader.GetBlobBytes(sourceLinkHandle));
if (!options.SuppressSourceLinkConversion)
{
var srcsvrData = ConvertSourceServerData(pdbReader.GetStringUTF8(sourceLinkHandle), nonEmbeddedDocumentNames, options);
// an error has been reported:
if (srcsvrData != null)
{
pdbWriter.SetSourceServerData(Encoding.UTF8.GetBytes(srcsvrData));
}
}
}
var compilerOptions = ReadCompilationOptions(pdbReader).ToImmutableDictionary();
if (compilerOptions.TryGetValue("compiler-version", out var compilerVersionString) &&
compilerOptions.TryGetValue("language", out var language) &&
language is "C#" or "Visual Basic" or "F#" &&
TryConvertCompilerVersionToFileVersion(compilerVersionString, out var fileMajor, out var fileMinor, out var fileBuild, out var fileRevision))
{
pdbWriter.AddCompilerInfo(fileMajor, fileMinor, fileBuild, fileRevision, $"{language} - {compilerVersionString}");
}
SymReaderHelpers.GetWindowsPdbSignature(pdbReader.DebugMetadataHeader.Id, out var guid, out var stamp, out var age);
pdbWriter.UpdateSignature(guid, stamp, age);
}
private static IEnumerable<KeyValuePair<string, string>> ReadCompilationOptions(MetadataReader reader)
{
var compilerOptions = reader.GetCustomDebugInformation(EntityHandle.ModuleDefinition, PortableCustomDebugInfoKinds.CompilationOptions);
if (compilerOptions.IsNil)
{
yield break;
}
var blobReader = reader.GetBlobReader(compilerOptions);
// Compiler flag bytes are UTF-8 null-terminated key-value pairs
var nullIndex = blobReader.IndexOf(0);
while (nullIndex >= 0)
{
var key = blobReader.ReadUTF8(nullIndex);
// Skip the null terminator
blobReader.ReadByte();
nullIndex = blobReader.IndexOf(0);
var value = blobReader.ReadUTF8(nullIndex);
yield return new KeyValuePair<string, string>(key, value);
// Skip the null terminator
blobReader.ReadByte();
nullIndex = blobReader.IndexOf(0);
}
}
/// <summary>
/// Roslyn compilers use version in AssemblyFileVersionAttribute for the numeric version and
/// the semantic version stored in AssemblyInformationalVersionAttribute for the version string.
/// The latter is also stored in compiler-version, but the former is not stored in compiler options.
/// We calculate it based on the version scheme used by Roslyn build infrastructure.
/// See https://github.com/dotnet/arcade/blob/release/6.0/Documentation/CorePackages/Versioning.md
/// </summary>
internal static bool TryConvertCompilerVersionToFileVersion(string str, out ushort fileMajor, out ushort fileMinor, out ushort fileBuild, out ushort fileRevision)
{
// assumes format: major.minor.revision-labels.SHORT_DATE.REVISION+CommitSha
var match = Regex.Match(str, "([0-9]+)[.]([0-9]+)[.]([0-9]+)-[^.]+[.]([0-9]+)[.]([0-9]+).*");
if (match.Success &&
ushort.TryParse(match.Groups[1].Value, out var major) &&
ushort.TryParse(match.Groups[2].Value, out var minor) &&
ushort.TryParse(match.Groups[3].Value, out var build) &&
ushort.TryParse(match.Groups[4].Value, out var shortDate) &&
ushort.TryParse(match.Groups[5].Value, out var revision))
{
var dd = shortDate % 50;
var mm = (shortDate / 50) % 20;
var yy = shortDate / 1000;
fileMajor = major;
try
{
checked
{
fileMinor = (ushort)(minor * 100 + build / 100);
fileBuild = (ushort)((build % 100) * 100 + yy);
fileRevision = (ushort)((50 * mm + dd) * 100 + revision);
return true;
}
}
catch (OverflowException)
{
}
}
fileMajor = fileMinor = fileBuild = fileRevision = 0;
return false;
}
// internal for testing
internal void ValidateAndCorrectSourceChecksum(ref Guid algorithmId, byte[] checksum, string documentName)
{
const int SizeOfSHA1 = 20;
const int SizeOfSHA256 = 32;
int expectedSize;
string algorithmName;
if (algorithmId == default)
{
expectedSize = 0;
algorithmName = ConverterResources.None;
}
else if (algorithmId == PdbGuids.HashAlgorithm.SHA1)
{
expectedSize = SizeOfSHA1;
algorithmName = nameof(PdbGuids.HashAlgorithm.SHA1);
}
else if (algorithmId == PdbGuids.HashAlgorithm.SHA256)
{
expectedSize = SizeOfSHA256;
algorithmName = nameof(PdbGuids.HashAlgorithm.SHA256);
}
else
{
// ignore unknown algorithms
return;
}
if (checksum.Length != expectedSize)
{
ReportDiagnostic(PdbDiagnosticId.SourceChecksumAlgorithmSizeMismatch, 0, new[] { algorithmName, documentName });
if (algorithmId == default)
{
// guess correct algorithm id based on the checksum size:
switch (checksum.Length)
{
case SizeOfSHA1:
algorithmId = PdbGuids.HashAlgorithm.SHA1;
break;
case SizeOfSHA256:
algorithmId = PdbGuids.HashAlgorithm.SHA256;
break;
}
}
}
}
private static string? GetMethodNamespace(MetadataReader metadataReader, MethodDefinition methodDef)
{
var typeDefHandle = methodDef.GetDeclaringType();
if (typeDefHandle.IsNil)
{
return null;
}
while (true)
{
var typeDef = metadataReader.GetTypeDefinition(typeDefHandle);
typeDefHandle = typeDef.GetDeclaringType();
if (typeDefHandle.IsNil)
{
return metadataReader.GetString(typeDef.Namespace);
}
}
}
private static int AdjustEndScopeOffset(int exclusiveOffset, bool isEndInclusive) =>
isEndInclusive ? exclusiveOffset - 1 : exclusiveOffset;
private static bool TryGetDynamicLocal(
string name,
int slotIndex,
ImmutableArray<bool> flagsOpt,
out (string LocalName, byte[] Flags, int Count, int SlotIndex) result)
{
// C# compiler skips variables with too many flags or too long name:
if (flagsOpt.IsDefaultOrEmpty ||
flagsOpt.Length > CustomDebugInfoEncoder.DynamicAttributeSize ||
name.Length >= CustomDebugInfoEncoder.IdentifierSize)
{
result = default((string, byte[], int, int));
return false;
}
var bytes = new byte[Math.Min(flagsOpt.Length, CustomDebugInfoEncoder.DynamicAttributeSize)];
for (int k = 0; k < bytes.Length; k++)
{
if (flagsOpt[k])
{
bytes[k] = 1;
}
}
result = (name, bytes, flagsOpt.Length, slotIndex);
return true;
}
private static void CopyCustomDebugInfoRecord(
ref CustomDebugInfoEncoder cdiEncoder,
MetadataReader pdbReader,
MethodDefinitionHandle methodDefHandle,
Guid portableKind,
CustomDebugInfoKind windowsKind)
{
var cdiHandle = pdbReader.GetCustomDebugInformation(methodDefHandle, portableKind);
if (!cdiHandle.IsNil)
{
var bytes = pdbReader.GetBlobBytes(cdiHandle);
cdiEncoder.AddRecord(
windowsKind,
bytes,
(b, builder) => builder.WriteBytes(b));
}
}
private static ImmutableArray<StateMachineHoistedLocalScope> GetStateMachineHoistedLocalScopes(MetadataReader pdbReader, MethodDefinitionHandle methodDefHandle)
{
var cdiHandle = pdbReader.GetCustomDebugInformation(methodDefHandle, PortableCustomDebugInfoKinds.StateMachineHoistedLocalScopes);
if (cdiHandle.IsNil)
{
return default;
}
return MetadataUtilities.DecodeHoistedLocalScopes(pdbReader.GetBlobReader(cdiHandle));
}
private static string GetIteratorTypeName(MetadataReader metadataReader, MethodDefinitionHandle moveNextHandle)
{
// TODO: errors
var moveNextDef = metadataReader.GetMethodDefinition(moveNextHandle);
var iteratorType = moveNextDef.GetDeclaringType();
var name = metadataReader.GetString(metadataReader.GetTypeDefinition(iteratorType).Name);
// trim generic arity from the name:
int backtick = name.LastIndexOf('`');
return (backtick > 0) ? name.Substring(0, backtick) : name;
}
private static void WriteImports(SymUnmanagedWriter pdbWriter, ImmutableArray<string> importStrings)
{
foreach (var importString in importStrings)
{
pdbWriter.UsingNamespace(importString);
}
}
private void AddImportStrings(
List<string> importStrings,
List<int> importGroups,
HashSet<string> declaredExternAliases,
MetadataReader pdbReader,
MetadataModel metadataModel,
ImportScopeHandle importScopeHandle,
ImmutableArray<(AssemblyReferenceHandle, string)> aliasedAssemblyRefs,
string? vbDefaultNamespaceImportString,
string? vbCurrentMethodNamespace,
bool vbSemantics)
{
Debug.Assert(declaredExternAliases.Count == 0);
AddExternAliases(declaredExternAliases, pdbReader, importScopeHandle);
while (!importScopeHandle.IsNil)
{
var importScope = pdbReader.GetImportScope(importScopeHandle);
bool isProjectLevel = importScope.Parent.IsNil;
if (isProjectLevel && vbDefaultNamespaceImportString != null)
{
Debug.Assert(vbSemantics);
importStrings.Add(vbDefaultNamespaceImportString);
}
int importStringCount = 0;
foreach (var import in importScope.GetImports())
{
var importString = TryEncodeImport(pdbReader, metadataModel, importScopeHandle, import, declaredExternAliases, aliasedAssemblyRefs, isProjectLevel, vbSemantics);
if (importString == null)
{
// diagnostic already reported if applicable
continue;
}
if (importString.Length > MaxEntityNameLength)
{
ReportDiagnostic(PdbDiagnosticId.LocalScopeRangesNestingIsInvalid, MetadataTokens.GetToken(importScopeHandle), importString);
continue;
}
importStrings.Add(importString);
importStringCount++;
}
if (isProjectLevel && vbCurrentMethodNamespace != null)
{
Debug.Assert(vbSemantics);
importStrings.Add(vbCurrentMethodNamespace);
}
// Skip C# project-level scope if it doesn't include namespaces.
// Currently regular (non-scripting) C# doesn't support project-level namespace imports.
if (vbSemantics || !isProjectLevel || importStringCount > 0)
{
importGroups.Add(importStringCount);
}
importScopeHandle = importScope.Parent;
}
declaredExternAliases.Clear();
}
private static void AddExternAliases(HashSet<string> externAliases, MetadataReader pdbReader, ImportScopeHandle importScopeHandle)
{
while (!importScopeHandle.IsNil)
{
var importScope = pdbReader.GetImportScope(importScopeHandle);
foreach (var import in importScope.GetImports())
{
if (import.Kind == ImportDefinitionKind.ImportAssemblyReferenceAlias)
{
externAliases.Add(pdbReader.GetStringUTF8(import.Alias));
}
}
importScopeHandle = importScope.Parent;
}
}
private static ImmutableArray<(AssemblyReferenceHandle, string)> GetAliasedAssemblyRefs( MetadataReader pdbReader)
{
// F# doesn't emit import scopes
if (pdbReader.ImportScopes.Count == 0)
{
return ImmutableArray<(AssemblyReferenceHandle, string)>.Empty;
}
// C# serialized aliased assembly refs to the first import scope.
// In Windows PDBs they are attached as CDIs to any method in the assembly and the other methods
// have CDI that forwards to it.
return (from import in pdbReader.GetImportScope(MetadataTokens.ImportScopeHandle(1)).GetImports()
where import.Kind == ImportDefinitionKind.AliasAssemblyReference
select (import.TargetAssembly, pdbReader.GetStringUTF8(import.Alias))).ToImmutableArray();
}
private string? TryEncodeImport(
MetadataReader pdbReader,
MetadataModel metadataModel,
ImportScopeHandle importScopeHandle,
ImportDefinition import,
HashSet<string> declaredExternAliases,
ImmutableArray<(AssemblyReferenceHandle, string)> aliasedAssemblyRefs,
bool isProjectLevel,
bool vbSemantics)
{
string? typeName;
string namespaceName;
// See Roslyn implementation: PdbWriter.TryEncodeImport, MetadataWriter.SerializeImport
switch (import.Kind)
{
case ImportDefinitionKind.AliasType:
case ImportDefinitionKind.ImportType:
// C#, VB
if (vbSemantics)
{
// VB doesn't support generics in imports:
if (import.TargetType.Kind == HandleKind.TypeSpecification)
{
return null;
}
typeName = metadataModel.GetSerializedTypeName(import.TargetType);
if (typeName == null)
{
ReportDiagnostic(PdbDiagnosticId.UnsupportedImportType, MetadataTokens.GetToken(importScopeHandle), MetadataTokens.GetToken(import.TargetType));
return null;
}
if (import.Kind == ImportDefinitionKind.AliasType)
{
return (isProjectLevel ? "@PA:" : "@FA:") + pdbReader.GetStringUTF8(import.Alias) + "=" + typeName;
}
else
{
return (isProjectLevel ? "@PT:" : "@FT:") + typeName;
}
}
else
{
typeName = metadataModel.GetSerializedTypeName(import.TargetType);
if (typeName == null)
{
ReportDiagnostic(PdbDiagnosticId.UnsupportedImportType, MetadataTokens.GetToken(importScopeHandle), MetadataTokens.GetToken(import.TargetType));
return null;
}
if (import.Kind == ImportDefinitionKind.AliasType)
{
return "A" + pdbReader.GetStringUTF8(import.Alias) + " T" + typeName;
}
else
{
return "T" + typeName;
}
}
case ImportDefinitionKind.AliasNamespace:
// C#, VB
namespaceName = pdbReader.GetStringUTF8(import.TargetNamespace);
if (vbSemantics)
{