Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[NativeAOT] Add method parameter names to stack trace if available #73578

Merged
merged 5 commits into from
Aug 18, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ public static string FormatMethodName(MetadataReader metadataReader, TypeDefinit
MethodNameFormatter formatter = new MethodNameFormatter(metadataReader, SigTypeContext.FromMethod(metadataReader, enclosingTypeHandle, methodHandle));

Method method = metadataReader.GetMethod(methodHandle);
MethodSignature methodSignature = metadataReader.GetMethodSignature(method.Signature);
formatter.EmitTypeName(enclosingTypeHandle, namespaceQualified: true);
formatter._outputBuilder.Append('.');
formatter.EmitString(method.Name);
Expand All @@ -64,7 +63,7 @@ public static string FormatMethodName(MetadataReader metadataReader, TypeDefinit
}
else
{
formatter._outputBuilder.Append(", ");
formatter._outputBuilder.Append(',');
}
formatter.EmitTypeName(handle, namespaceQualified: false);
}
Expand All @@ -73,7 +72,7 @@ public static string FormatMethodName(MetadataReader metadataReader, TypeDefinit
formatter._outputBuilder.Append(']');
}

formatter.EmitMethodParameters(methodSignature);
formatter.EmitMethodParameters(methodHandle);

return formatter._outputBuilder.ToString();
}
Expand Down Expand Up @@ -124,28 +123,30 @@ private void EmitMethodReferenceName(MemberReferenceHandle memberRefHandle)
private void EmitMethodInstantiationName(MethodInstantiationHandle methodInstHandle)
{
MethodInstantiation methodInst = _metadataReader.GetMethodInstantiation(methodInstHandle);
MethodSignature methodSignature;

if (methodInst.Method.HandleType == HandleType.MemberReference)
{
MemberReferenceHandle methodRefHandle = methodInst.Method.ToMemberReferenceHandle(_metadataReader);
MemberReference methodRef = methodRefHandle.GetMemberReference(_metadataReader);
EmitContainingTypeAndMethodName(methodRef, out methodSignature);
EmitContainingTypeAndMethodName(methodRef, out MethodSignature methodSignature);
EmitGenericArguments(methodInst.GenericTypeArguments);
EmitMethodParameters(methodSignature);
}
else
{
QualifiedMethodHandle qualifiedMethodHandle = methodInst.Method.ToQualifiedMethodHandle(_metadataReader);
QualifiedMethod qualifiedMethod = _metadataReader.GetQualifiedMethod(qualifiedMethodHandle);
EmitContainingTypeAndMethodName(qualifiedMethod, out methodSignature);
EmitContainingTypeAndMethodName(qualifiedMethod);
EmitGenericArguments(methodInst.GenericTypeArguments);
EmitMethodParameters(qualifiedMethod.Method);
}
EmitGenericArguments(methodInst.GenericTypeArguments);
EmitMethodParameters(methodSignature);
}

private void EmitMethodDefinitionName(QualifiedMethodHandle qualifiedMethodHandle)
{
QualifiedMethod qualifiedMethod = _metadataReader.GetQualifiedMethod(qualifiedMethodHandle);
EmitContainingTypeAndMethodName(qualifiedMethod, out MethodSignature methodSignature);
EmitMethodParameters(methodSignature);
EmitContainingTypeAndMethodName(qualifiedMethod);
EmitMethodParameters(qualifiedMethod.Method);
}

/// <summary>
Expand All @@ -161,10 +162,9 @@ private void EmitContainingTypeAndMethodName(MemberReference methodRef, out Meth
EmitString(methodRef.Name);
}

private void EmitContainingTypeAndMethodName(QualifiedMethod qualifiedMethod, out MethodSignature methodSignature)
private void EmitContainingTypeAndMethodName(QualifiedMethod qualifiedMethod)
{
Method method = _metadataReader.GetMethod(qualifiedMethod.Method);
methodSignature = _metadataReader.GetMethodSignature(method.Signature);
EmitTypeName(qualifiedMethod.EnclosingType, namespaceQualified: true);
_outputBuilder.Append('.');
EmitString(method.Name);
Expand All @@ -181,6 +181,39 @@ private void EmitMethodParameters(MethodSignature methodSignature)
_outputBuilder.Append(')');
}

/// <summary>
/// Emit parenthesized method argument type list with parameter names.
/// </summary>
/// <param name="methodHandle">Method handle to use for parameter formatting</param>
private void EmitMethodParameters(MethodHandle methodHandle)
{
Method method = methodHandle.GetMethod(_metadataReader);
HandleCollection typeVector = method.Signature.GetMethodSignature(_metadataReader).Parameters;
ParameterHandleCollection.Enumerator names = method.Parameters.GetEnumerator();

bool first = true;

_outputBuilder.Append('(');
foreach (Handle handle in typeVector)
{
if (first)
{
first = false;
}
else
{
_outputBuilder.Append(", ");
}

names.MoveNext();
MichalStrehovsky marked this conversation as resolved.
Show resolved Hide resolved

EmitTypeName(handle, namespaceQualified: false);
_outputBuilder.Append(' ');
EmitString(names.Current.GetParameter(_metadataReader).Name);
jasper-d marked this conversation as resolved.
Show resolved Hide resolved
}
_outputBuilder.Append(')');
}

/// <summary>
/// Emit comma-separated list of type names into the output string builder.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
MichalStrehovsky marked this conversation as resolved.
Show resolved Hide resolved
using System.IO;
using System.Runtime.CompilerServices;
using System.Text;
Expand All @@ -15,8 +16,9 @@ public class EnvironmentStackTrace
static string s_stackTrace;

[Fact]
[ActiveIssue("https://github.com/dotnet/runtime/issues/73051", typeof(PlatformDetection), nameof(PlatformDetection.IsNativeAot))]
[ActiveIssue("https://github.com/mono/mono/issues/15315", TestRuntimes.Mono)]
// Retain parameter names for NativeAOT
[DynamicDependency(DynamicallyAccessedMemberTypes.NonPublicMethods, typeof(EnvironmentStackTrace))]
jasper-d marked this conversation as resolved.
Show resolved Hide resolved
public void StackTraceTest()
{
//arrange
Expand Down