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

Add mvid and mdtoken to callstackframe #6839

Merged
merged 18 commits into from
Jun 19, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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 @@ -97,21 +97,23 @@ public void ClassDescription(
[Event(ExceptionEvents.EventIds.FunctionDescription)]
public void FunctionDescription(
ulong FunctionId,
uint MethodToken,
ulong ClassId,
uint ClassToken,
ulong ModuleId,
string Name,
ulong[] TypeArgs,
ulong[] ParameterTypes)
{
Span<EventData> data = stackalloc EventData[7];
Span<EventData> data = stackalloc EventData[8];
using PinnedData namePinned = PinnedData.Create(Name);
Span<byte> typeArgsSpan = stackalloc byte[GetArrayDataSize(TypeArgs)];
FillArrayData(typeArgsSpan, TypeArgs);
Span<byte> parameterTypesSpan = stackalloc byte[GetArrayDataSize(ParameterTypes)];
FillArrayData(parameterTypesSpan, ParameterTypes);

SetValue(ref data[NameIdentificationEvents.FunctionDescPayloads.FunctionId], FunctionId);
SetValue(ref data[NameIdentificationEvents.FunctionDescPayloads.MethodToken], MethodToken);
SetValue(ref data[NameIdentificationEvents.FunctionDescPayloads.ClassId], ClassId);
SetValue(ref data[NameIdentificationEvents.FunctionDescPayloads.ClassToken], ClassToken);
SetValue(ref data[NameIdentificationEvents.FunctionDescPayloads.ModuleId], ModuleId);
Expand All @@ -125,12 +127,14 @@ public void FunctionDescription(
[Event(ExceptionEvents.EventIds.ModuleDescription)]
public void ModuleDescription(
ulong ModuleId,
Guid ModuleVersionId,
string Name)
{
Span<EventData> data = stackalloc EventData[2];
Span<EventData> data = stackalloc EventData[3];
using PinnedData namePinned = PinnedData.Create(Name);

SetValue(ref data[NameIdentificationEvents.ModuleDescPayloads.ModuleId], ModuleId);
SetValue(ref data[NameIdentificationEvents.ModuleDescPayloads.ModuleVersionId], ModuleVersionId);
SetValue(ref data[NameIdentificationEvents.ModuleDescPayloads.Name], namePinned);

WriteEventWithFlushing(ExceptionEvents.EventIds.ModuleDescription, data);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,9 @@ public override void OnFunctionData(ulong functionId, FunctionData data)
{
_source.FunctionDescription(
functionId,
data.MethodToken,
data.ParentClass,
data.ParentToken,
data.ParentClassToken,
data.ModuleId,
data.Name,
data.TypeArgs,
Expand All @@ -50,6 +51,7 @@ public override void OnModuleData(ulong moduleId, ModuleData data)
{
_source.ModuleDescription(
moduleId,
data.ModuleVersionId,
data.Name);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,19 @@ public ulong GetOrAdd(MethodBase method)
return methodId;

// Dynamic methods do not have metadata tokens
uint metadataToken = 0;
uint methodToken = 0;
try
{
metadataToken = Convert.ToUInt32(method.MetadataToken);
methodToken = Convert.ToUInt32(method.MetadataToken);
WilliamXieMSFT marked this conversation as resolved.
Show resolved Hide resolved
}
catch (Exception) { }

uint parentClassToken = 0;
if (null != method.DeclaringType)
{
parentClassToken = Convert.ToUInt32(method.DeclaringType.MetadataToken);
}

// RTDynamicMethod does not implement GetGenericArguments.
Type[] genericArguments = Array.Empty<Type>();
try
Expand All @@ -100,8 +106,9 @@ public ulong GetOrAdd(MethodBase method)

FunctionData data = new(
method.Name,
methodToken,
AddOrDefault(method.DeclaringType),
metadataToken,
parentClassToken,
GetOrAdd(method.Module),
GetOrAdd(genericArguments),
GetOrAdd(method.GetParameters())
Expand All @@ -128,7 +135,7 @@ public ulong GetOrAdd(Module module)
if (!GetOrCreateIdentifier(_moduleIds, module, ref _nextModuleId, out ulong moduleId))
return moduleId;

ModuleData data = new(module.Name);
ModuleData data = new(module.Name, module.ModuleVersionId);

if (_nameCache.ModuleData.TryAdd(moduleId, data))
{
Expand Down Expand Up @@ -212,16 +219,16 @@ public ulong GetOrAdd(Type type)
ModuleScopedToken key = new(moduleId, typeToken);
if (!_nameCache.TokenData.ContainsKey(key))
{
uint parentToken = 0;
uint parentClassToken = 0;
if (null != type.DeclaringType)
{
parentToken = Convert.ToUInt32(type.DeclaringType.MetadataToken);
parentClassToken = Convert.ToUInt32(type.DeclaringType.MetadataToken);
}

TokenData tokenData = new(
type.Name,
null == type.DeclaringType ? type.Namespace ?? string.Empty : string.Empty,
parentToken);
parentClassToken);

if (!_nameCache.TokenData.TryAdd(key, tokenData))
break;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Collections.Generic;
using System.Text.Json.Serialization;

Expand All @@ -11,12 +12,18 @@ public class CallStackFrame
[JsonPropertyName("methodName")]
public string MethodName { get; set; }

[JsonPropertyName("methodToken")]
WilliamXieMSFT marked this conversation as resolved.
Show resolved Hide resolved
public uint MethodToken { get; set; }

[JsonPropertyName("typeName")]
public string TypeName { get; set; }

[JsonPropertyName("moduleName")]
public string ModuleName { get; set; }

[JsonPropertyName("moduleVersionId")]
public Guid ModuleVersionId { get; set; }

[JsonIgnore]
internal IList<string> SimpleGenericArgTypes { get; set; } = new List<string>();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Collections.Generic;

namespace Microsoft.Diagnostics.Monitoring.WebApi.Stacks
Expand All @@ -20,6 +21,10 @@ internal sealed class CallStackFrame
{
public ulong FunctionId { get; set; }

public uint MethodToken { get; set; }

public Guid ModuleVersionId { get; set; }

public ulong Offset { get; set; }
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,22 @@ private void Callback(TraceEvent action)
{
for (int i = 0; i < functionIds.Length; i++)
{
stack.Frames.Add(new CallStackFrame { FunctionId = functionIds[i], Offset = offsets[i] });
CallStackFrame stackFrame = new CallStackFrame
{
FunctionId = functionIds[i],
Offset = offsets[i]
};

if (_result.NameCache.FunctionData.TryGetValue(stackFrame.FunctionId, out FunctionData functionData))
{
stackFrame.MethodToken = functionData.MethodToken;
if (_result.NameCache.ModuleData.TryGetValue(functionData.ModuleId, out ModuleData moduleData))
{
stackFrame.ModuleVersionId = moduleData.ModuleVersionId;
}
}

stack.Frames.Add(stackFrame);
}
}
}
Expand All @@ -94,6 +109,7 @@ private void Callback(TraceEvent action)
ulong id = action.GetPayload<ulong>(NameIdentificationEvents.FunctionDescPayloads.FunctionId);
var functionData = new FunctionData(
action.GetPayload<string>(NameIdentificationEvents.FunctionDescPayloads.Name),
action.GetPayload<uint>(NameIdentificationEvents.FunctionDescPayloads.MethodToken),
action.GetPayload<ulong>(NameIdentificationEvents.FunctionDescPayloads.ClassId),
action.GetPayload<uint>(NameIdentificationEvents.FunctionDescPayloads.ClassToken),
action.GetPayload<ulong>(NameIdentificationEvents.FunctionDescPayloads.ModuleId),
Expand All @@ -119,7 +135,8 @@ private void Callback(TraceEvent action)
{
ulong id = action.GetPayload<ulong>(NameIdentificationEvents.ModuleDescPayloads.ModuleId);
var moduleData = new ModuleData(
action.GetPayload<string>(NameIdentificationEvents.ModuleDescPayloads.Name)
action.GetPayload<string>(NameIdentificationEvents.ModuleDescPayloads.Name),
action.GetPayload<Guid>(NameIdentificationEvents.ModuleDescPayloads.ModuleVersionId)
);

_result.NameCache.ModuleData.TryAdd(id, moduleData);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Collections.Concurrent;
using System.Diagnostics;

Expand Down Expand Up @@ -43,20 +44,22 @@ internal sealed record class ClassData(uint Token, ulong ModuleId, ClassFlags Fl
internal sealed record class TokenData(string Name, string Namespace, uint OuterToken);

/// <param name="Name">The name of the function.</param>
/// <param name="MethodToken">The method token of the function (methodDef token).</param>
/// <param name="ParentClass">The parent class identifier of the function.</param>
/// <param name="ParentToken">The parent metadata token of the function.</param>
/// <param name="ParentClassToken">The parent metadata token of the function (typeDef token).</param>
/// <param name="ModuleId">The identifier of the module that contains the function.</param>
/// <param name="TypeArgs">The class identifiers of the generic type arguments of the function.</param>
/// <param name="ParameterTypes">The class identifiers of the parameter types of the function.</param>
/// <remarks>
/// If <paramref name="ParentClass"/> is 0, then use <paramref name="ParentToken"/>.
/// If <paramref name="ParentClass"/> is 0, then use <paramref name="ParentClassToken"/>.
/// </remarks>
[DebuggerDisplay("{Name}")]
internal sealed record class FunctionData(string Name, ulong ParentClass, uint ParentToken, ulong ModuleId, ulong[] TypeArgs, ulong[] ParameterTypes);
internal sealed record class FunctionData(string Name, uint MethodToken, ulong ParentClass, uint ParentClassToken, ulong ModuleId, ulong[] TypeArgs, ulong[] ParameterTypes);

/// <param name="Name">The name of the module.</param>
/// <param name="ModuleVersionId">The version identifier of the module.</param>
[DebuggerDisplay("{Name}")]
internal sealed record class ModuleData(string Name);
internal sealed record class ModuleData(string Name, Guid ModuleVersionId);

internal sealed record class ModuleScopedToken(ulong ModuleId, uint Token);
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public static void BuildTypeName(StringBuilder builder, NameCache cache, Functio
}
else
{
BuildTypeName(builder, cache, functionData.ModuleId, functionData.ParentToken, TypeFormat.Full);
BuildTypeName(builder, cache, functionData.ModuleId, functionData.ParentClassToken, TypeFormat.Full);
}
}

Expand Down Expand Up @@ -80,11 +80,11 @@ public static void BuildTypeName(StringBuilder builder, NameCache cache, ulong c
}
}

private static void BuildTypeName(StringBuilder builder, NameCache cache, ulong moduleId, uint token, TypeFormat typeFormat)
private static void BuildTypeName(StringBuilder builder, NameCache cache, ulong moduleId, uint classToken, TypeFormat typeFormat)
{
var typeNames = new Stack<string>();

uint currentToken = token;
uint currentToken = classToken;
while (currentToken != 0 && cache.TokenData.TryGetValue(new ModuleScopedToken(moduleId, currentToken), out TokenData? tokenData))
{
string typeName = tokenData.Name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@ internal static class NameIdentificationEvents
public static class FunctionDescPayloads
{
public const int FunctionId = 0;
public const int ClassId = 1;
public const int ClassToken = 2;
public const int ModuleId = 3;
public const int Name = 4;
public const int TypeArgs = 5;
public const int ParameterTypes = 6;
public const int MethodToken = 1;
public const int ClassId = 2;
public const int ClassToken = 3;
public const int ModuleId = 4;
public const int Name = 5;
public const int TypeArgs = 6;
public const int ParameterTypes = 7;
}

public static class ClassDescPayloads
Expand All @@ -32,7 +33,8 @@ public static class ClassDescPayloads
public static class ModuleDescPayloads
{
public const int ModuleId = 0;
public const int Name = 1;
public const int ModuleVersionId = 1;
public const int Name = 2;
}

public static class TokenDescPayloads
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,11 @@ internal static Models.CallStackFrame CreateFrameModel(CallStackFrame frame, Nam
{
TypeName = NameFormatter.UnknownClass,
MethodName = StacksFormatter.UnknownFunction,
MethodToken = 0,
//TODO Bring this back once we have a useful offset value
//Offset = frame.Offset,
ModuleName = NameFormatter.UnknownModule
ModuleName = NameFormatter.UnknownModule,
ModuleVersionId = Guid.Empty
};
if (frame.FunctionId == 0)
{
Expand All @@ -60,8 +62,14 @@ internal static Models.CallStackFrame CreateFrameModel(CallStackFrame frame, Nam
}
else if (cache.FunctionData.TryGetValue(frame.FunctionId, out FunctionData functionData))
{
frameModel.MethodToken = functionData.MethodToken;
frameModel.ModuleName = NameFormatter.GetModuleName(cache, functionData.ModuleId);

if (cache.ModuleData.TryGetValue(functionData.ModuleId, out ModuleData moduleData))
{
frameModel.ModuleVersionId = moduleData.ModuleVersionId;
}

builder.Clear();
builder.Append(functionData.Name);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ protected override void OnEventWritten(EventWrittenEventArgs eventData)
ToUInt64(eventData.Payload[NameIdentificationEvents.FunctionDescPayloads.FunctionId]),
schmittjoseph marked this conversation as resolved.
Show resolved Hide resolved
new FunctionData(
ToString(eventData.Payload[NameIdentificationEvents.FunctionDescPayloads.Name]),
ToUInt32(eventData.Payload[NameIdentificationEvents.FunctionDescPayloads.MethodToken]),
ToUInt64(eventData.Payload[NameIdentificationEvents.FunctionDescPayloads.ClassId]),
ToUInt32(eventData.Payload[NameIdentificationEvents.FunctionDescPayloads.ClassToken]),
ToUInt64(eventData.Payload[NameIdentificationEvents.FunctionDescPayloads.ModuleId]),
Expand All @@ -78,7 +79,9 @@ protected override void OnEventWritten(EventWrittenEventArgs eventData)
NameCache.ModuleData.TryAdd(
ToUInt64(eventData.Payload[NameIdentificationEvents.ModuleDescPayloads.ModuleId]),
new ModuleData(
ToString(eventData.Payload[NameIdentificationEvents.ModuleDescPayloads.Name])));
ToString(eventData.Payload[NameIdentificationEvents.ModuleDescPayloads.Name]),
ToGuid(eventData.Payload[NameIdentificationEvents.ModuleDescPayloads.ModuleVersionId])
));
break;
case ExceptionEvents.EventIds.StackFrameDescription:
StackFrameIdentifiers.TryAdd(
Expand All @@ -104,6 +107,11 @@ protected override void OnEventWritten(EventWrittenEventArgs eventData)
}
}

private static Guid ToGuid(object? value)
{
return ToType<Guid>(value);
}

private static int ToInt32(object? value)
{
return ToType<int>(value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ public void ExceptionGroupIdentifierCache_ThrownException()
// Validate throwing method remaining properties
Assert.Equal(nameof(ExceptionGroupIdentifierCache_ThrownException), throwingMethodData.Name);
Assert.NotEqual(InvalidId, throwingMethodData.ParentClass);
Assert.NotEqual(InvalidToken, throwingMethodData.ParentToken);
Assert.NotEqual(InvalidToken, throwingMethodData.ParentClassToken);
Assert.Empty(throwingMethodData.TypeArgs);

// Validate stack frame data
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ private void Callback(TraceEvent traceEvent)
case "FunctionDescription":
_cache.AddFunction(
traceEvent.GetPayload<ulong>(NameIdentificationEvents.FunctionDescPayloads.FunctionId),
traceEvent.GetPayload<uint>(NameIdentificationEvents.FunctionDescPayloads.MethodToken),
traceEvent.GetPayload<ulong>(NameIdentificationEvents.FunctionDescPayloads.ClassId),
traceEvent.GetPayload<uint>(NameIdentificationEvents.FunctionDescPayloads.ClassToken),
traceEvent.GetPayload<ulong>(NameIdentificationEvents.FunctionDescPayloads.ModuleId),
Expand All @@ -105,6 +106,7 @@ private void Callback(TraceEvent traceEvent)
case "ModuleDescription":
_cache.AddModule(
traceEvent.GetPayload<ulong>(NameIdentificationEvents.ModuleDescPayloads.ModuleId),
traceEvent.GetPayload<Guid>(NameIdentificationEvents.ModuleDescPayloads.ModuleVersionId),
traceEvent.GetPayload<string>(NameIdentificationEvents.ModuleDescPayloads.Name)
);
break;
Expand Down
Loading
Loading