-
Notifications
You must be signed in to change notification settings - Fork 147
/
Copy pathSymbolExtractor.cs
820 lines (706 loc) · 30 KB
/
SymbolExtractor.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
// <copyright file="SymbolExtractor.cs" company="Datadog">
// Unless explicitly stated otherwise all files in this repository are licensed under the Apache 2 License.
// This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2017 Datadog, Inc.
// </copyright>
#nullable enable
using System;
using System.Collections.Generic;
using System.Reflection;
using Datadog.Trace.Debugger.Symbols.Model;
using Datadog.Trace.Logging;
using Datadog.Trace.Pdb;
using Datadog.Trace.VendoredMicrosoftCode.System;
using Datadog.Trace.VendoredMicrosoftCode.System.Buffers;
using Datadog.Trace.VendoredMicrosoftCode.System.Collections.Immutable;
using Datadog.Trace.VendoredMicrosoftCode.System.Reflection.Metadata;
namespace Datadog.Trace.Debugger.Symbols
{
internal class SymbolExtractor : IDisposable
{
private static readonly IDatadogLogger Log = DatadogLogging.GetLoggerFor(typeof(SymbolExtractor));
private readonly string _assemblyPath;
private bool _disposed;
protected const int UnknownMethodStartLine = 0;
protected const int UnknownMethodEndLine = 0;
protected const int UnknownFieldAndArgLine = 0;
protected const MethodAttributes StaticFinalVirtualMethod = System.Reflection.MethodAttributes.Static | System.Reflection.MethodAttributes.Final | System.Reflection.MethodAttributes.Virtual;
protected SymbolExtractor(DatadogMetadataReader metadataReader, string assemblyPath)
{
_assemblyPath = assemblyPath;
DatadogMetadataReader = metadataReader;
MetadataReader = metadataReader.MetadataReader;
}
protected DatadogMetadataReader DatadogMetadataReader { get; }
protected MetadataReader MetadataReader { get; }
protected Dictionary<int, string> MethodAccess { get; } = new()
{
{ 0x0001, "private" },
{ 0x0002, "private protected" },
{ 0x0003, "internal" },
{ 0x0004, "protected" },
{ 0x0005, "protected internal" },
{ 0x0006, "public" }
};
protected Dictionary<int, string> MethodAttributes { get; } = new()
{
{ 0x0010, "static" },
{ 0x0020, "final" },
{ 0x0040, "virtual" },
{ 0x0060, "final virtual" }
};
protected Dictionary<int, string> FieldAttributes { get; } = new()
{
{ 0x0010, "static" },
{ 0x0020, "readonly" },
{ 0x0030, "static readonly" },
{ 0x0040, "const" },
{ 0x0050, "static const" },
};
public static SymbolExtractor? Create(Assembly assembly)
{
try
{
var datadogMetadataReader = DatadogMetadataReader.CreatePdbReader(assembly);
if (datadogMetadataReader == null)
{
Log.Debug("DatadogMetadataReader is null for assembly {Assembly}", assembly.FullName);
return null;
}
if (datadogMetadataReader.MetadataReader.TypeDefinitions.Count == 0)
{
Log.Debug("Could not found any type in assembly {Assembly}", assembly.FullName);
return null;
}
return datadogMetadataReader.IsPdbExist ? new SymbolPdbExtractor(datadogMetadataReader, assembly.Location) : new SymbolExtractor(datadogMetadataReader, assembly.Location);
}
catch (Exception e)
{
Log.Error(e, "Error while creating instance of SymbolExtractor with {Assembly}", assembly.FullName);
return null;
}
}
internal Model.Scope GetAssemblySymbol()
{
var assemblyNameHandle = MetadataReader.GetAssemblyDefinition().Name;
var assemblyName = assemblyNameHandle.IsNil ? null : MetadataReader.GetString(assemblyNameHandle);
var assemblyScope = new Model.Scope
{
Name = assemblyName,
ScopeType = ScopeType.Assembly,
SourceFile = string.IsNullOrEmpty(_assemblyPath) ? null : _assemblyPath,
};
return assemblyScope;
}
internal IEnumerable<Model.Scope?> GetClassSymbols()
{
foreach (var typeDefinitionHandle in MetadataReader.TypeDefinitions)
{
if (typeDefinitionHandle.IsNil)
{
continue;
}
if (!TryGetClassSymbols(typeDefinitionHandle, out var classScope))
{
continue;
}
yield return classScope;
}
}
/// <summary>
/// For testing
/// </summary>
internal Model.Scope? GetClassSymbols(string typeToExtract)
{
foreach (var typeDefinitionHandle in MetadataReader.TypeDefinitions)
{
if (typeDefinitionHandle.IsNil)
{
continue;
}
if (!typeToExtract.Equals(typeDefinitionHandle.FullName(MetadataReader)))
{
continue;
}
return TryGetClassSymbols(typeDefinitionHandle, out var classScope) ? classScope : null;
}
return null;
}
private bool TryGetClassSymbols(TypeDefinitionHandle typeDefinitionHandle, out Model.Scope? classScope)
{
classScope = null;
Model.Symbol[]? fieldSymbols = null;
Model.Scope[]? scopes = null;
try
{
if (DatadogMetadataReader.IsCompilerGeneratedAttributeDefinedOnType(typeDefinitionHandle.RowId))
{
return true;
}
var type = MetadataReader.GetTypeDefinition(typeDefinitionHandle);
if (type.IsInterfaceType())
{
return true;
}
var fields = type.GetFields();
if (fields.Count > 0)
{
fieldSymbols = GetFieldSymbols(fields, type);
}
var methods = type.GetMethods();
var nestedTypes = type.GetNestedTypes();
int scopesBufferLength = methods.Count + nestedTypes.Length;
scopes = ArrayPool<Model.Scope>.Shared.Rent(scopesBufferLength);
int methodsScopeIndex = 0;
int nestedClassesScopeIndex = 0;
if (methods.Count > 0)
{
PopulateMethodScopes(type, methods, scopes, ref methodsScopeIndex);
}
nestedClassesScopeIndex = methodsScopeIndex;
if (nestedTypes.Length > 0)
{
PopulateNestedNotCompileGeneratedClassScope(nestedTypes, scopes, ref nestedClassesScopeIndex);
}
var allScopes = nestedClassesScopeIndex == 0 ? null : new Model.Scope[nestedClassesScopeIndex];
if (allScopes == null)
{
return true;
}
if (methodsScopeIndex > 0)
{
Array.Copy(scopes!, 0, allScopes!, 0, methodsScopeIndex);
}
if ((nestedClassesScopeIndex - methodsScopeIndex) > 0)
{
int destIndex = methodsScopeIndex == 0 ? 0 : methodsScopeIndex;
Array.Copy(scopes!, destIndex, allScopes!, destIndex, nestedClassesScopeIndex - methodsScopeIndex);
}
var classLanguageSpecifics = GetClassLanguageSpecifics(type);
var linesAndSource = GetClassSourceLocationInfo(allScopes);
classScope = new Model.Scope
{
Name = typeDefinitionHandle.FullName(MetadataReader),
ScopeType = ScopeType.Class,
Symbols = fieldSymbols,
Scopes = allScopes,
StartLine = linesAndSource.StartLine,
EndLine = linesAndSource.EndLine,
SourceFile = linesAndSource.Path,
LanguageSpecifics = classLanguageSpecifics
};
}
catch (Exception e)
{
string? typeName = null;
try
{
typeName = typeDefinitionHandle.FullName(MetadataReader);
}
catch
{
// ignored
}
Log.Warning(e, "Error while trying to extract symbol info for type {Type}", typeName ?? "NA");
return false;
}
finally
{
if (scopes != null)
{
ArrayPool<Model.Scope>.Shared.Return(scopes, true);
}
}
return true;
}
private SourceLocationInfo GetClassSourceLocationInfo(Model.Scope[]? allScopes)
{
int classStartLine = int.MaxValue;
int classEndLine = 0;
string? classSourceFile = null;
if (allScopes == null)
{
return new SourceLocationInfo { StartLine = UnknownMethodStartLine, EndLine = UnknownMethodEndLine, Path = classSourceFile };
}
for (int i = 0; i < allScopes.Length; i++)
{
var scope = allScopes[i];
if (classStartLine > scope.StartLine && scope.StartLine > 0)
{
classStartLine = scope.StartLine;
}
if (classEndLine < scope.EndLine)
{
classEndLine = scope.EndLine;
}
classSourceFile ??= scope.SourceFile;
}
if (classStartLine == int.MaxValue)
{
for (int i = 0; i < allScopes.Length; i++)
{
var scope = allScopes[i];
if (scope.Scopes == null)
{
continue;
}
for (int j = 0; j < scope.Scopes.Length; j++)
{
var innerScope = scope.Scopes[j];
if (classStartLine > innerScope.StartLine)
{
classStartLine = innerScope.StartLine;
}
if (classEndLine < innerScope.EndLine)
{
classEndLine = innerScope.EndLine;
}
classSourceFile ??= innerScope.SourceFile;
}
}
}
if (classStartLine == int.MaxValue)
{
classStartLine = UnknownMethodStartLine;
}
if (classEndLine == 0)
{
classEndLine = UnknownMethodEndLine;
}
return new SourceLocationInfo { StartLine = classStartLine, EndLine = classEndLine, Path = classSourceFile };
}
private void PopulateNestedNotCompileGeneratedClassScope(ImmutableArray<TypeDefinitionHandle> nestedTypes, Model.Scope[] nestedClassScopes, ref int index)
{
for (var i = 0; i < nestedTypes.Length; i++)
{
Model.Scope? nestedClassScope = null;
try
{
var typeHandle = nestedTypes[i];
if (typeHandle.IsNil)
{
continue;
}
if (DatadogMetadataReader.IsCompilerGeneratedAttributeDefinedOnType(typeHandle.RowId))
{
continue;
}
if (!TryGetClassSymbols(typeHandle, out nestedClassScope) || nestedClassScope == null)
{
continue;
}
nestedClassScopes[index] = nestedClassScope.Value;
index++;
}
catch (Exception e)
{
Log.Warning(e, "Symbol extractor fail to get information for nested class {NestedClass}", nestedClassScope?.Name ?? "Unknown");
}
}
}
private LanguageSpecifics GetClassLanguageSpecifics(TypeDefinition type)
{
var interfaceNames = GetClassInterfaceNames(type);
var baseClassNames = GetClassBaseClassNames(type);
var accessModifiers = (type.Attributes & TypeAttributes.VisibilityMask);
var classLanguageSpecifics = new LanguageSpecifics
{
AccessModifiers = accessModifiers > 0 ? new[] { accessModifiers.ToString() } : null,
Interfaces = interfaceNames,
SuperClasses = baseClassNames,
IsPdbExist = DatadogMetadataReader.IsPdbExist
};
return classLanguageSpecifics;
}
private Symbol[] GetFieldSymbols(FieldDefinitionHandleCollection fieldsCollection, TypeDefinition parentType)
{
int index = 0;
Model.Symbol[] fieldSymbols = new Model.Symbol[fieldsCollection.Count];
foreach (var fieldDefHandle in fieldsCollection)
{
var fieldDef = MetadataReader.GetFieldDefinition(fieldDefHandle);
var fieldTypeName = fieldDef.DecodeSignature(new TypeProvider(false), 0);
if (fieldDef.Name.IsNil)
{
continue;
}
var fieldName = Datadog.Trace.VendoredMicrosoftCode.System.MemoryExtensions.AsSpan(MetadataReader.GetString(fieldDef.Name));
if (fieldName[0] == '<')
{
// properties
var endNameIndex = fieldName.IndexOf('>');
if (endNameIndex > 1)
{
fieldName = fieldName.Slice(1, endNameIndex - 1);
}
}
var accessModifiers = (fieldDef.Attributes & System.Reflection.FieldAttributes.FieldAccessMask) > 0 ? new[] { MethodAccess[Convert.ToUInt16(fieldDef.Attributes & System.Reflection.FieldAttributes.FieldAccessMask)] } : null;
var fieldAttributes = ((int)fieldDef.Attributes & 0x0070) > 0 ? new[] { FieldAttributes[Convert.ToUInt16((int)fieldDef.Attributes & 0x0070)] } : null;
var ls = new LanguageSpecifics() { AccessModifiers = accessModifiers, Annotations = fieldAttributes };
fieldSymbols[index] = new Symbol
{
Name = fieldName.ToString(),
Type = fieldTypeName,
SymbolType = ((fieldDef.Attributes & System.Reflection.FieldAttributes.Static) != 0) ? SymbolType.StaticField : SymbolType.Field,
Line = UnknownFieldAndArgLine,
LanguageSpecifics = ls
};
index++;
}
return fieldSymbols;
}
private void PopulateMethodScopes(TypeDefinition type, MethodDefinitionHandleCollection methods, Model.Scope[] classMethods, ref int index)
{
foreach (var methodDefHandle in methods)
{
Model.Scope methodScope = default;
try
{
if (methodDefHandle.IsNil)
{
continue;
}
if (DatadogMetadataReader.IsCompilerGeneratedAttributeDefinedOnMethod(methodDefHandle.RowId))
{
continue;
}
if (!DatadogMetadataReader.HasMethodBody(methodDefHandle.RowId))
{
continue;
}
var methodDef = MetadataReader.GetMethodDefinition(methodDefHandle);
methodScope = CreateMethodScope(type, methodDef);
if (methodScope == default)
{
continue;
}
if (methodScope.Scopes != null &&
methodScope is { StartLine: UnknownMethodStartLine, EndLine: UnknownMethodEndLine, SourceFile: null })
{
var sourcePdbInfo = GetMethodSourceLocationInfo(methodScope);
methodScope.StartLine = sourcePdbInfo.StartLine;
methodScope.EndLine = sourcePdbInfo.EndLine;
methodScope.SourceFile = sourcePdbInfo.Path;
if (sourcePdbInfo.EndColumn > 0)
{
var ls = new LanguageSpecifics
{
Annotations = methodScope.LanguageSpecifics?.Annotations,
AccessModifiers = methodScope.LanguageSpecifics?.AccessModifiers,
ReturnType = methodScope.LanguageSpecifics?.ReturnType,
StartColumn = sourcePdbInfo.StartColumn,
EndColumn = sourcePdbInfo.EndColumn,
};
methodScope.LanguageSpecifics = ls;
}
}
classMethods[index] = methodScope;
index++;
}
catch (Exception e)
{
var name = GetMethodName(methodScope.Name, methodDefHandle);
Log.Warning(e, "Symbol extractor fail to get information for method {Method}", name);
}
}
string GetMethodName(string? name, MethodDefinitionHandle handle)
{
try
{
return name ??
MetadataReader.GetString(MetadataReader.GetMethodDefinition(handle).Name);
}
catch
{
return "Unknown";
}
}
}
private SourceLocationInfo GetMethodSourceLocationInfo(Model.Scope methodScope)
{
var startLine = int.MaxValue;
var endLine = 0;
int? startColumn = null;
int? endColumn = null;
string? sourceFile = null;
if (methodScope.Scopes != null)
{
for (int i = 0; i < methodScope.Scopes.Length; i++)
{
var scope = methodScope.Scopes[i];
if (startLine > scope.StartLine && scope.StartLine > 0)
{
startLine = scope.StartLine;
}
if (endLine < scope.EndLine)
{
endLine = scope.EndLine;
}
sourceFile ??= scope.SourceFile;
if (!scope.LanguageSpecifics.HasValue)
{
continue;
}
if (startColumn == null || startColumn > scope.LanguageSpecifics.Value.StartColumn)
{
startColumn = scope.LanguageSpecifics.Value.StartColumn;
}
if (endColumn == null || endColumn < scope.LanguageSpecifics.Value.EndColumn)
{
endColumn = scope.LanguageSpecifics.Value.EndColumn;
}
}
}
if (startLine == int.MaxValue)
{
startLine = UnknownMethodStartLine;
}
if (endLine == 0)
{
endLine = UnknownMethodEndLine;
}
return new SourceLocationInfo { StartLine = startLine, EndLine = endLine, Path = sourceFile, StartColumn = startColumn, EndColumn = endColumn };
}
protected virtual Model.Scope CreateMethodScope(TypeDefinition type, MethodDefinition method)
{
if (method.Name.IsNil)
{
return default;
}
var methodName = MetadataReader.GetString(method.Name);
if (string.IsNullOrEmpty(methodName))
{
return default;
}
// arguments
var argsSymbol = GetArgsSymbol(method);
// closures
var closureScopes = GetClosureScopes(type, method);
var methodAttributes = method.Attributes & StaticFinalVirtualMethod;
var isAsyncMethod = DatadogMetadataReader.IsAsyncMethod(method.GetCustomAttributes());
var methodLanguageSpecifics = new LanguageSpecifics
{
ReturnType = method.DecodeSignature(new TypeProvider(false), 0).ReturnType,
AccessModifiers = (method.Attributes & System.Reflection.MethodAttributes.MemberAccessMask) > 0 ? new[] { MethodAccess[Convert.ToUInt16(method.Attributes & System.Reflection.MethodAttributes.MemberAccessMask)] } : null,
Annotations = methodAttributes > 0 && isAsyncMethod ? new[] { MethodAttributes[Convert.ToUInt16(methodAttributes)], "async" } :
methodAttributes > 0 ? new[] { MethodAttributes[Convert.ToUInt16(methodAttributes)] } :
isAsyncMethod ? new[] { "async" } : null
};
var methodScope = new Model.Scope
{
ScopeType = ScopeType.Method,
Name = methodName,
LanguageSpecifics = methodLanguageSpecifics,
Symbols = argsSymbol,
Scopes = closureScopes,
SourceFile = null,
StartLine = UnknownMethodStartLine,
EndLine = UnknownMethodEndLine
};
return methodScope;
}
private Model.Scope[]? GetClosureScopes(TypeDefinition typeDef, MethodDefinition methodDef)
{
Model.Scope[]? closureMethods = null;
int index = 0;
try
{
var nestedTypes = typeDef.GetNestedTypes();
var methods = typeDef.GetMethods();
closureMethods = ArrayPool<Model.Scope>.Shared.Rent(methods.Count + (nestedTypes.Length * 2));
for (int i = 0; i < nestedTypes.Length; i++)
{
if (!DatadogMetadataReader.IsCompilerGeneratedAttributeDefinedOnType(nestedTypes[i].RowId))
{
continue;
}
var nestedType = MetadataReader.GetTypeDefinition(nestedTypes[i]);
var generatedMethods = nestedType.GetMethods();
foreach (var generatedMethodHandle in generatedMethods)
{
if (generatedMethodHandle.IsNil)
{
continue;
}
var generatedMethodDef = MetadataReader.GetMethodDefinition(generatedMethodHandle);
PopulateClosureMethod(generatedMethodDef, nestedType);
}
}
foreach (var methodHandle in methods)
{
if (methodHandle.IsNil || methodHandle == methodDef.Handle)
{
continue;
}
if (!DatadogMetadataReader.IsCompilerGeneratedAttributeDefinedOnMethod(methodHandle.RowId))
{
continue;
}
var currentMethod = MetadataReader.GetMethodDefinition(methodHandle);
PopulateClosureMethod(currentMethod, typeDef);
}
if (index == 0)
{
return null;
}
var closureScopes = new Model.Scope[index];
for (int i = 0; i < index; i++)
{
closureScopes[i] = closureMethods[i];
}
return closureScopes;
}
finally
{
if (closureMethods != null)
{
ArrayPool<Model.Scope>.Shared.Return(closureMethods);
}
}
void PopulateClosureMethod(MethodDefinition generatedMethod, TypeDefinition ownerType)
{
var closureMethodScope = CreateMethodScopeForGeneratedMethod(methodDef, generatedMethod, ownerType);
if (closureMethodScope.HasValue)
{
if (index < closureMethods.Length)
{
closureMethods[index] = closureMethodScope.Value;
index++;
}
else
{
Log.Warning("Not enough space for all closure methods");
}
}
}
}
protected virtual Model.Scope? CreateMethodScopeForGeneratedMethod(MethodDefinition method, MethodDefinition generatedMethod, TypeDefinition nestedType)
{
return null;
}
private Symbol[]? GetArgsSymbol(MethodDefinition method)
{
var parameters = method.GetParameters();
if (parameters.Count == 0)
{
if (method.IsStaticMethod())
{
return null;
}
return [new Symbol { Name = "this", SymbolType = SymbolType.Arg, Line = UnknownFieldAndArgLine, Type = method.GetDeclaringType().FullName(MetadataReader) }];
}
var argsSymbol = method.IsStaticMethod() ? new Symbol[parameters.Count] : new Symbol[parameters.Count + 1]; // 'this'
int index = 0;
var methodSig = method.DecodeSignature(new TypeProvider(false), 0);
var typesIndex = 0;
if (!method.IsStaticMethod())
{
var thisSymbol = new Symbol { Name = "this", SymbolType = SymbolType.Arg, Line = UnknownFieldAndArgLine, Type = method.GetDeclaringType().FullName(MetadataReader) };
argsSymbol[0] = thisSymbol;
index++;
}
foreach (var parameterHandle in parameters)
{
if (parameterHandle.IsNil)
{
continue;
}
var parameterDef = MetadataReader.GetParameter(parameterHandle);
var parameterName = MetadataReader.GetString(parameterDef.Name);
if (string.IsNullOrEmpty(parameterName))
{
continue;
}
if (parameterDef.IsHiddenThis())
{
continue;
}
argsSymbol[index] = new Symbol
{
Name = parameterName,
SymbolType = SymbolType.Arg,
Line = UnknownFieldAndArgLine,
Type = typesIndex < methodSig.ParameterTypes.Length ? methodSig.ParameterTypes[typesIndex] : "Unknown"
};
index++;
typesIndex++;
}
return argsSymbol;
}
private string[]? GetClassBaseClassNames(TypeDefinition type)
{
EntityHandle? baseType = type.BaseType;
if (baseType.Value.IsNil)
{
return null;
}
var baseTypeName = type.BaseType.FullName(MetadataReader);
var objectTypeFullName = typeof(object).FullName;
if (baseTypeName.Equals(objectTypeFullName))
{
return new[] { baseTypeName };
}
var classNames = new List<string>();
classNames.Add(baseTypeName);
while ((baseType = GetBaseTypeHandle(baseType.Value)).HasValue && !baseTypeName.Equals(objectTypeFullName))
{
classNames.Add(baseTypeName);
baseTypeName = baseType.Value.FullName(MetadataReader);
}
return classNames.ToArray();
}
private EntityHandle? GetBaseTypeHandle(EntityHandle typeHandle)
{
switch (typeHandle)
{
case { Kind: HandleKind.TypeDefinition }:
{
var typeDef = MetadataReader.GetTypeDefinition((TypeDefinitionHandle)typeHandle);
return typeDef.BaseType;
}
case { Kind: HandleKind.TypeReference }:
{
var typeRef = MetadataReader.GetTypeReference((TypeReferenceHandle)typeHandle);
return typeRef.ResolutionScope.Kind == HandleKind.TypeReference ? typeRef.ResolutionScope : null;
}
default:
{
return null;
}
}
}
private string[]? GetClassInterfaceNames(TypeDefinition type)
{
var interfaces = type.GetInterfaceImplementations();
if (interfaces.Count <= 0)
{
return null;
}
var interfaceNames = new string[interfaces.Count];
int index = 0;
foreach (var interfaceHandle in interfaces)
{
interfaceNames[index] = MetadataReader.GetInterfaceImplementation(interfaceHandle).Interface.FullName(MetadataReader);
index++;
}
return interfaceNames;
}
public void Dispose()
{
if (_disposed)
{
return;
}
DatadogMetadataReader.Dispose();
_disposed = true;
GC.SuppressFinalize(this);
}
internal record struct SourceLocationInfo
{
internal int StartLine;
internal int EndLine;
internal string? Path;
internal int? StartColumn;
internal int? EndColumn;
}
}
}