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

Use metadata to find type names when appropriate #1179

Merged
merged 2 commits into from
Aug 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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 @@ -54,7 +54,7 @@ public override string? Name
if (_name == null)
{
// GetTypeName returns whether the value should be cached or not.
if (!Helpers.TryGetTypeName(MethodTable, out string? name))
if (!Helpers.TryGetTypeName(this, out string? name))
return name;

// Cache the result or "string.Empty" for null.
Expand Down
40 changes: 37 additions & 3 deletions src/Microsoft.Diagnostics.Runtime/Implementation/ClrTypeHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,14 @@
using System.Runtime.InteropServices;
using Microsoft.Diagnostics.Runtime.DacInterface;
using Microsoft.Diagnostics.Runtime.Implementation;
using Microsoft.Diagnostics.Runtime.Utilities;

namespace Microsoft.Diagnostics.Runtime
{
internal sealed class ClrTypeHelpers : IClrTypeHelpers, IClrFieldHelpers
{
private readonly string UnloadedTypeName = "<Unloaded Type>";

private readonly uint _firstChar = (uint)IntPtr.Size + 4;
private readonly uint _stringLength = (uint)IntPtr.Size;
private readonly SOSDac _sos;
Expand Down Expand Up @@ -138,19 +141,50 @@ private ImmutableArray<ComInterfaceData> GetComInterfaces(COMInterfacePointerDat
return _typeFactory.GetOrCreateType(mt, 0);
}

public bool TryGetTypeName(ulong mt, out string? name)
public bool TryGetTypeName(ClrType type, out string? name)
{
name = _sos.GetMethodTableName(mt);
name = _sos.GetMethodTableName(type.MethodTable);
if (string.IsNullOrWhiteSpace(name))
return true;

name = DACNameParser.Parse(name);
if (name == UnloadedTypeName)
{
string? nameFromToken = GetNameFromToken(type.Module?.MetadataImport, type.MetadataToken);
if (nameFromToken is not null)
name = nameFromToken;
}
else
{
name = DACNameParser.Parse(name);
}

if (CacheOptions.CacheTypeNames == StringCaching.Intern)
name = string.Intern(name);

return CacheOptions.CacheTypeNames != StringCaching.None;
}

private static string? GetNameFromToken(MetadataImport? import, int token)
{
if (import is not null)
{
HResult hr = import.GetTypeDefProperties(token, out string? name, out _, out _);
if (hr && name is not null)
{
hr = import.GetNestedClassProperties(token, out int enclosingToken);
if (hr && enclosingToken != 0 && enclosingToken != token)
{
string? inner = GetNameFromToken(import, enclosingToken) ?? "<UNKNOWN>";
name += $"+{inner}";
}

return name;
}
}

return null;
}

public ulong GetLoaderAllocatorHandle(ulong mt)
{
if (_sos6 != null && _sos6.GetMethodTableCollectibleData(mt, out MethodTableCollectibleData data) && data.Collectible != 0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ internal interface IClrTypeHelpers
ClrHeap Heap { get; }
IDataReader DataReader { get; }

bool TryGetTypeName(ulong mt, out string? name);
bool TryGetTypeName(ClrType type, out string? name);
ulong GetLoaderAllocatorHandle(ulong mt);
ulong GetAssemblyLoadContextAddress(ulong mt);

Expand Down