Skip to content

Commit 90e95ae

Browse files
committed
WIP - Replace dnlib with System.Reflection.Metadata #2
1 parent 286a20f commit 90e95ae

File tree

5 files changed

+253
-87
lines changed

5 files changed

+253
-87
lines changed

tracer/src/Datadog.Trace/Debugger/Symbols/SymbolExtractor.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,7 @@ protected virtual Model.Scope CreateMethodScope(TypeDefinition type, MethodDefin
419419
var methodAttributes = method.Attributes & StaticFinalVirtual;
420420
var methodLanguageSpecifics = new LanguageSpecifics
421421
{
422-
// ReturnType = method.ReturnType.FullName, // decode signature
422+
ReturnType = method.DecodeSignature(new TypeProvider(), 0).ReturnType.Name,
423423
AccessModifiers = (method.Attributes & MethodAttributes.MemberAccessMask) > 0 ? new[] { _methodAccess[Convert.ToUInt16(method.Attributes & MethodAttributes.MemberAccessMask)] } : null,
424424
Annotations = methodAttributes > 0 ? new[] { _methodAttributes[Convert.ToUInt16(methodAttributes)] } : null
425425
};
@@ -628,19 +628,25 @@ private string ReadKickoffMethodNameFromBlob(BlobReader blobReader)
628628

629629
var argsSymbol = ArrayPool<Symbol>.Shared.Rent(parameters.Count);
630630
int index = 0;
631+
631632
foreach (var parameterHandle in parameters)
632633
{
633634
var parameterDef = MetadataReader.GetParameter(parameterHandle);
634635
argsSymbol[index] = new Symbol
635636
{
636637
Name = MetadataReader.GetString(parameterDef.Name),
637-
// Type = parameterDef.Type.FullName, // ??
638638
SymbolType = SymbolType.Arg,
639639
Line = UnknownStartLine
640640
};
641641
index++;
642642
}
643643

644+
var methodSig = method.DecodeSignature(new TypeProvider(), 0);
645+
for (int i = 0; i < argsSymbol.Length; i++)
646+
{
647+
argsSymbol[i].Type = methodSig.ParameterTypes[i].Name;
648+
}
649+
644650
return argsSymbol;
645651
}
646652

tracer/src/Datadog.Trace/Debugger/Symbols/SymbolPdbExtractor.cs

Lines changed: 34 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@
66
#nullable enable
77
using System;
88
using System.Linq;
9+
using Datadog.System.Buffers;
910
using Datadog.System.Reflection.Metadata;
11+
using Datadog.System.Reflection.Metadata.Ecma335;
1012
using Datadog.Trace.Debugger.Symbols.Model;
1113
using Datadog.Trace.Pdb;
12-
using Datadog.Trace.Vendors.dnlib.DotNet.Emit;
1314
using Datadog.Trace.Vendors.dnlib.DotNet.Pdb.Symbols;
1415

1516
namespace Datadog.Trace.Debugger.Symbols;
@@ -66,117 +67,70 @@ protected override Model.Scope CreateMethodScope(TypeDefinition type, MethodDefi
6667
return methodScope;
6768
}
6869

69-
public void GetLocalVariables(MethodDefinition method)
70+
private StandaloneSignature GetLocalSignature(MethodDefinition method)
7071
{
7172
var methodBodyBlock = _pdbReader.PEReader?.GetMethodBody(method.RelativeVirtualAddress);
72-
var methodDebugInfo = _pdbReader.MetadataReader.GetMethodDebugInformation(method.Handle);
73-
foreach (var localScope in MetadataReader.GetLocalScopes(method.Handle))
74-
{
75-
MetadataReader.GetLocalVariableRange(localScope, out int first, out int last);
76-
foreach (var localVariable in MetadataReader.LocalVariables)
77-
{
78-
// localVariable.RowId
79-
}
73+
return MetadataReader.GetStandaloneSignature(methodBodyBlock!.LocalSignature);
74+
}
8075

81-
// MetadataReader.LocalVariableTable.GetName(MetadataReader.LocalVariables)
82-
}
76+
private int GetLocalVariablesCount(MethodDefinition method)
77+
{
78+
var signature = GetLocalSignature(method);
79+
BlobReader blobReader = MetadataReader.GetBlobReader(signature.Signature);
8380

84-
MetadataReader.GetLocalScopes(method.Handle);
85-
foreach (var sequencePoint in methodDebugInfo.GetSequencePoints())
81+
if (blobReader.ReadByte() == (byte)SignatureKind.LocalVariables)
8682
{
87-
// var localScopes = _pdbReader.MetadataReader.GetLocalScopes(methodDebugInfo.);
88-
// foreach (var localScope in localScopes)
89-
// {
90-
// _pdbReader.PEReader.GetSectionData()
91-
// foreach (var localVariableHandle in localScope..GetLocalVariables())
92-
// {
93-
// var localVariable = pdbReader.GetLocalVariable(localVariableHandle);
94-
// Console.WriteLine($"Index: {localVariable.Index}, Name: {pdbReader.GetString(localVariable.Name)}");
95-
// }
96-
// }
83+
int variableCount = blobReader.ReadCompressedInteger();
84+
return variableCount;
9785
}
9886

99-
// var localVarSig = MetadataReader.GetLocalVariable(methodBodyBlock.LocalSignature);
100-
// MethodBodyBlock methodBody = MetadataReader.MethodDefTable..GetMethodImplementation(method.Handle).MethodBody;
101-
// StandAloneSignature signature = MetadataReader.GetStandaloneSignature(methodBody.LocalSignature);
102-
103-
// BlobReader blobReader = MetadataReader.GetBlobReader(signature.Signature);
104-
105-
// if (blobReader.ReadByte() == (byte)SignatureKind.LocalVariables)
106-
// {
107-
// int variableCount = blobReader.ReadCompressedInteger();
108-
109-
// for (int i = 0; i < variableCount; i++)
110-
// {
111-
// SignatureTypeCode typeCode = (SignatureTypeCode)blobReader.ReadCompressedInteger();
112-
113-
// For simple types, the type code maps directly to the variable type.
114-
// For other types, more detailed parsing will be necessary.
115-
// }
116-
// }
87+
return 0;
11788
}
11889

11990
private Symbol[]? GetLocalsSymbol(MethodDefinition method, int startLine, SymbolMethod symbolMethod, out int localsCount)
12091
{
12192
localsCount = 0;
122-
var methodBody = _pdbReader.PEReader?.GetMethodBody(method.RelativeVirtualAddress);
123-
// if (method.Body is not { Variables.Count: > 0 })
124-
// {
125-
// return null;
126-
// }
127-
128-
var methodLocals = new int[1]; // methodBody.Variables;
129-
var localsSymbol = new Symbol[methodLocals.Length];
130-
var allMethodScopes = GetAllScopes(symbolMethod);
131-
Symbol[]? allLocals = null;
93+
var methodLocalsCount = GetLocalVariablesCount(method);
94+
if (methodLocalsCount == 0)
95+
{
96+
return null;
97+
}
13298

133-
for (var k = 0; k < allMethodScopes.Count; k++)
99+
var localsSymbol = ArrayPool<Symbol>.Shared.Rent(methodLocalsCount);
100+
var signature = GetLocalSignature(method);
101+
var localTypes = signature.DecodeLocalSignature(new TypeProvider(), 0);
102+
103+
Symbol[]? allLocals = null;
104+
foreach (var scopeHandle in MetadataReader.GetLocalScopes(method.Handle.ToDebugInformationHandle()))
134105
{
135-
var currentScope = allMethodScopes[k];
136-
for (var l = 0; l < currentScope.Locals.Count; l++)
106+
var localScope = MetadataReader.GetLocalScope(scopeHandle);
107+
foreach (var localVarHandle in localScope.GetLocalVariables())
137108
{
138-
var localSymbol = currentScope.Locals[l];
139-
if (localSymbol.Index > methodLocals.Length || string.IsNullOrEmpty(localSymbol.Name))
109+
var local = MetadataReader.GetLocalVariable(localVarHandle);
110+
if (local.Index > methodLocalsCount || string.IsNullOrEmpty(MetadataReader.GetString(local.Name)))
140111
{
141112
continue;
142113
}
143114

144115
var line = UnknownEndLineEntireScope;
145-
for (var m = 0; m < symbolMethod.SequencePoints.Count; m++)
116+
foreach (var sequencePoint in MetadataReader.GetMethodDebugInformation(method.Handle.ToDebugInformationHandle()).GetSequencePoints())
146117
{
147-
if (symbolMethod.SequencePoints[m].Offset >= currentScope.StartOffset)
118+
if (sequencePoint.Offset >= localScope.StartOffset)
148119
{
149-
line = symbolMethod.SequencePoints[m].Line;
120+
line = sequencePoint.StartLine;
150121
break;
151122
}
152123
}
153124

154-
Local? local = null;
155-
// for (var i = 0; i < methodLocals.Length; i++)
156-
// {
157-
// if (methodLocals[i].Index != localSymbol.Index)
158-
// {
159-
// continue;
160-
// }
161-
162-
// local = methodLocals[i];
163-
// break;
164-
// }
165-
166-
if (local == null)
167-
{
168-
continue;
169-
}
170-
171125
// if (IsCompilerGeneratedAttributeDefined(local.Type.ToTypeDefOrRef().CustomAttributes))
172126
// {
173127
// continue;
174128
// }
175129

176130
localsSymbol[localsCount] = new Symbol
177131
{
178-
Name = localSymbol.Name,
179-
Type = local.Type?.FullName,
132+
Name = MetadataReader.GetString(local.Name),
133+
Type = localTypes[local.Index].Name,
180134
SymbolType = SymbolType.Local,
181135
Line = line
182136
};
@@ -216,7 +170,7 @@ public void GetLocalVariables(MethodDefinition method)
216170
allLocals[localsCount] = new Symbol
217171
{
218172
Name = localName,
219-
// Type = field.FieldType.FullName,
173+
Type = field.DecodeSignature(new TypeProvider(), 0).Name,
220174
SymbolType = SymbolType.Local,
221175
Line = startLine
222176
};
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// <copyright file="TypeMock.cs" company="Datadog">
2+
// Unless explicitly stated otherwise all files in this repository are licensed under the Apache 2 License.
3+
// This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2017 Datadog, Inc.
4+
// </copyright>
5+
6+
namespace Datadog.Trace.Debugger.Symbols;
7+
8+
internal class TypeMock
9+
{
10+
public TypeMock(string name)
11+
{
12+
Name = name;
13+
}
14+
15+
public string Name { get; set; }
16+
17+
public override string ToString()
18+
{
19+
return Name;
20+
}
21+
}

0 commit comments

Comments
 (0)