Skip to content

Commit

Permalink
[Dynamic Instrumentation] Hotfix SymDB and ER (#6468) (#6473)
Browse files Browse the repository at this point in the history
- Disable compression in SymDB
- Fix out of range exception while collecting arguments of some methods
- Fix type mismatch in dnlib metadata reader that causes invalid cast
exception
- Exception Replay should work even Live Debugger has not initialized
  • Loading branch information
dudikeleti authored Dec 20, 2024
1 parent a600786 commit eee5579
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 25 deletions.
3 changes: 2 additions & 1 deletion tracer/src/Datadog.Trace/Debugger/DebuggerSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,8 @@ public DebuggerSettings(IConfigurationSource? source, IConfigurationTelemetry te
.AsInt32(DefaultCodeOriginExitSpanFrames, frames => frames > 0)
.Value;

SymbolDatabaseCompressionEnabled = config.WithKeys(ConfigurationKeys.Debugger.SymbolDatabaseCompressionEnabled).AsBool(true);
// Force it to false to disable SymDB compression, will be re-enabled in the next PR
SymbolDatabaseCompressionEnabled = false;
}

public bool Enabled { get; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Reflection;
using System.Reflection.Emit;
using Datadog.Trace.Debugger.Symbols;
using Datadog.Trace.VendoredMicrosoftCode.System.Collections.Immutable;

#nullable enable
namespace Datadog.Trace.Debugger.ExceptionAutoInstrumentation
Expand Down Expand Up @@ -64,7 +65,7 @@ internal static bool IsBlockList(MethodBase method)
return true;
}

return AssemblyFilter.ShouldSkipAssembly(method.Module.Assembly, LiveDebugger.Instance.Settings.ThirdPartyDetectionExcludes, LiveDebugger.Instance.Settings.ThirdPartyDetectionIncludes);
return AssemblyFilter.ShouldSkipAssembly(method.Module.Assembly, ImmutableHashSet<string>.Empty, ImmutableHashSet<string>.Empty);
}

internal static bool ShouldSkipNamespaceIfOnTopOfStack(MethodBase method)
Expand Down
44 changes: 24 additions & 20 deletions tracer/src/Datadog.Trace/Debugger/Symbols/SymbolExtractor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -683,46 +683,50 @@ void PopulateClosureMethod(MethodDefinition generatedMethod, TypeDefinition owne
return [new Symbol { Name = "this", SymbolType = SymbolType.Arg, Line = UnknownFieldAndArgLine, Type = method.GetDeclaringType().FullName(MetadataReader) }];
}

var argsSymbol = CreateArgSymbolArray(method, parameters);
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 paramTypesMatchArgSymbols = methodSig.ParameterTypes.Length == argsSymbol.Length || methodSig.ParameterTypes.Length == argsSymbol.Length - 1;
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);
if (index == 0 && !method.IsStaticMethod())
var parameterName = MetadataReader.GetString(parameterDef.Name);
if (string.IsNullOrEmpty(parameterName))
{
argsSymbol[index] = new Symbol { Name = "this", SymbolType = SymbolType.Arg, Line = UnknownFieldAndArgLine, Type = method.GetDeclaringType().FullName(MetadataReader) };
index++;
continue;
}

if (parameterDef.IsHiddenThis())
{
continue;
}
if (parameterDef.IsHiddenThis())
{
continue;
}

argsSymbol[index] = new Symbol
{
Name = MetadataReader.GetString(parameterDef.Name),
Name = parameterName,
SymbolType = SymbolType.Arg,
Line = UnknownFieldAndArgLine,
Type = paramTypesMatchArgSymbols ? methodSig.ParameterTypes[parameterDef.IsHiddenThis() ? index : index - 1] : "Unknown"
Type = typesIndex < methodSig.ParameterTypes.Length ? methodSig.ParameterTypes[typesIndex] : "Unknown"
};
index++;
typesIndex++;
}

return argsSymbol;
}

private Symbol[] CreateArgSymbolArray(MethodDefinition method, ParameterHandleCollection parameters)
{
// ReSharper disable once NotDisposedResource
return method.IsStaticMethod() ?
new Symbol[parameters.Count] :
MetadataReader.GetParameter(parameters.GetEnumerator().Current).IsHiddenThis() ?
new Symbol[parameters.Count] : new Symbol[parameters.Count + 1];
}

private string[]? GetClassBaseClassNames(TypeDefinition type)
{
EntityHandle? baseType = type.BaseType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public override async Task<bool> SendBatchAsync(ArraySegment<byte> symbols)
using var gzipStream = new GZipStream(memoryStream, CompressionMode.Compress);
await gzipStream.WriteAsync(symbols.Array, 0, symbols.Array.Length).ConfigureAwait(false);
#endif
symbolsItem = new MultipartFormItem("file", "gzip", "file.gz", new ArraySegment<byte>(memoryStream.ToArray()));
symbolsItem = new MultipartFormItem("file", "application/gzip", "file.gz", new ArraySegment<byte>(memoryStream.ToArray()));
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ private CustomDebugInfoAsyncAndClosure GetAsyncAndClosureCustomDebugInfoDnlib(in
{
cdiAsyncAndClosure.EncLambdaAndClosureMap = true;
}
else if (methodCustomDebugInfo.Kind is PdbCustomDebugInfoKind.StateMachineHoistedLocalScopes)
else if (methodCustomDebugInfo.Kind is PdbCustomDebugInfoKind.AsyncMethod)
{
cdiAsyncAndClosure.StateMachineHoistedLocal = true;
cdiAsyncAndClosure.StateMachineKickoffMethodRid = (int)((PdbAsyncMethodCustomDebugInfo)methodCustomDebugInfo).KickoffMethod.MDToken.Rid;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public void DebuggerSettings_UseSettings()
NullConfigurationTelemetry.Instance);

settings.Enabled.Should().BeTrue();
settings.SymbolDatabaseCompressionEnabled.Should().BeTrue();
settings.SymbolDatabaseCompressionEnabled.Should().BeFalse();
settings.SymbolDatabaseUploadEnabled.Should().BeTrue();
settings.MaximumDepthOfMembersToCopy.Should().Be(100);
settings.MaxSerializationTimeInMilliseconds.Should().Be(1000);
Expand Down

0 comments on commit eee5579

Please sign in to comment.