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 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
3 changes: 3 additions & 0 deletions documentation/api/definitions.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,11 @@ First Available: 8.0 Preview 7
| Name | Type | Description |
|---|---|---|
| `methodName` | string | Name of the method for this frame. This includes generic parameters. |
| `methodToken` | int | TypeDef token for the method. |
| `parameterTypes` | string[] | Array of parameter types. Empty array if none. |
| `typeName` | string | Name of the class for this frame. This includes generic parameters. |
| `moduleName` | string | Name of the module for this frame. |
| `moduleVersionId` | guid | Unique identifier used to distinguish between two versions of the same module. An empty value: `00000000-0000-0000-0000-000000000000`. |

## CallStackResult

Expand Down
50 changes: 48 additions & 2 deletions documentation/api/exceptions.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,54 @@ Accept: application/x-ndjson
HTTP/1.1 200 OK
Content-Type: application/x-ndjson

{"id":2,"timestamp":"2023-07-13T21:45:11.8056355Z","typeName":"System.InvalidOperationException","moduleName":"System.Private.CoreLib.dll","message":"Operation is not valid due to the current state of the object.","innerExceptions":[],"stack":{"threadId":4768,"threadName":null,"frames":[{"methodName":"MoveNext","parameterTypes":[],"typeName":"WebApplication3.Pages.IndexModel\u002B\u003CGetData\u003Ed__3","moduleName":"WebApplication3.dll"},{"methodName":"RunInternal","parameterTypes":["System.Threading.ExecutionContext","System.Threading.ContextCallback","System.Object"],"typeName":"System.Threading.ExecutionContext","moduleName":"System.Private.CoreLib.dll"},{"methodName":"MoveNext","parameterTypes":["System.Threading.Thread"],"typeName":"System.Runtime.CompilerServices.AsyncTaskMethodBuilder\u00601\u002BAsyncStateMachineBox\u00601[System.Runtime.CompilerServices.AsyncTaskMethodBuilder\u00601\u002BAsyncStateMachineBox\u00601\u002BTResult,System.Runtime.CompilerServices.AsyncTaskMethodBuilder\u00601\u002BAsyncStateMachineBox\u00601\u002BTStateMachine]","moduleName":"System.Private.CoreLib.dll"},{"methodName":"\u003COutputCorrelationEtwEvent\u003Eb__6_0","parameterTypes":["System.Action","System.Threading.Tasks.Task"],"typeName":"System.Runtime.CompilerServices.YieldAwaitable\u002BYieldAwaiter\u002B\u003C\u003Ec","moduleName":"System.Private.CoreLib.dll"},{"methodName":"Dispatch","parameterTypes":[],"typeName":"System.Threading.ThreadPoolWorkQueue","moduleName":"System.Private.CoreLib.dll"},{"methodName":"WorkerThreadStart","parameterTypes":[],"typeName":"System.Threading.PortableThreadPool\u002BWorkerThread","moduleName":"System.Private.CoreLib.dll"}]}}
{"id":3,"timestamp":"2023-07-13T21:46:18.7530773Z","typeName":"System.ObjectDisposedException","moduleName":"System.Private.CoreLib.dll","message":"Cannot access a disposed object.\r\nObject name: \u0027System.Net.Sockets.NetworkStream\u0027.","innerExceptions":[],"stack":{"threadId":15912,"threadName":null,"frames":[{"methodName":"ThrowObjectDisposedException","parameterTypes":["System.Object"],"typeName":"System.ThrowHelper","moduleName":"System.Private.CoreLib.dll"},{"methodName":"ThrowIf","parameterTypes":["System.Boolean","System.Object"],"typeName":"System.ObjectDisposedException","moduleName":"System.Private.CoreLib.dll"},{"methodName":"ReadAsync","parameterTypes":["System.Memory\u00601[[System.Byte, System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]][System.Byte]","System.Threading.CancellationToken"],"typeName":"System.Net.Sockets.NetworkStream","moduleName":"System.Net.Sockets.dll"},{"methodName":"MoveNext","parameterTypes":[],"typeName":"System.Net.Http.HttpConnection\u002B\u003C\u003CEnsureReadAheadTaskHasStarted\u003Eg__ReadAheadWithZeroByteReadAsync|43_0\u003Ed","moduleName":"System.Net.Http.dll"},{"methodName":"ExecutionContextCallback","parameterTypes":["System.Object"],"typeName":"System.Runtime.CompilerServices.AsyncTaskMethodBuilder\u00601\u002BAsyncStateMachineBox\u00601[System.Runtime.CompilerServices.AsyncTaskMethodBuilder\u00601\u002BAsyncStateMachineBox\u00601\u002BTResult,System.Runtime.CompilerServices.AsyncTaskMethodBuilder\u00601\u002BAsyncStateMachineBox\u00601\u002BTStateMachine]","moduleName":"System.Private.CoreLib.dll"},{"methodName":"RunInternal","parameterTypes":["System.Threading.ExecutionContext","System.Threading.ContextCallback","System.Object"],"typeName":"System.Threading.ExecutionContext","moduleName":"System.Private.CoreLib.dll"},{"methodName":"MoveNext","parameterTypes":["System.Threading.Thread"],"typeName":"System.Runtime.CompilerServices.AsyncTaskMethodBuilder\u00601\u002BAsyncStateMachineBox\u00601[System.Runtime.CompilerServices.AsyncTaskMethodBuilder\u00601\u002BAsyncStateMachineBox\u00601\u002BTResult,System.Runtime.CompilerServices.AsyncTaskMethodBuilder\u00601\u002BAsyncStateMachineBox\u00601\u002BTStateMachine]","moduleName":"System.Private.CoreLib.dll"},{"methodName":"MoveNext","parameterTypes":[],"typeName":"System.Runtime.CompilerServices.AsyncTaskMethodBuilder\u00601\u002BAsyncStateMachineBox\u00601[System.Runtime.CompilerServices.AsyncTaskMethodBuilder\u00601\u002BAsyncStateMachineBox\u00601\u002BTResult,System.Runtime.CompilerServices.AsyncTaskMethodBuilder\u00601\u002BAsyncStateMachineBox\u00601\u002BTStateMachine]","moduleName":"System.Private.CoreLib.dll"},{"methodName":"\u003C.cctor\u003Eb__176_0","parameterTypes":["System.UInt32","System.UInt32","System.Threading.NativeOverlapped*"],"typeName":"System.Net.Sockets.SocketAsyncEventArgs\u002B\u003C\u003Ec","moduleName":"System.Net.Sockets.dll"},{"methodName":"Invoke","parameterTypes":["System.Threading.PortableThreadPool\u002BIOCompletionPoller\u002BEvent"],"typeName":"System.Threading.PortableThreadPool\u002BIOCompletionPoller\u002BCallback","moduleName":"System.Private.CoreLib.dll"},{"methodName":"System.Threading.IThreadPoolWorkItem.Execute","parameterTypes":[],"typeName":"System.Threading.ThreadPoolTypedWorkItemQueue\u00602[System.Threading.ThreadPoolTypedWorkItemQueue\u00602\u002BT,System.Threading.ThreadPoolTypedWorkItemQueue\u00602\u002BTCallback]","moduleName":"System.Private.CoreLib.dll"},{"methodName":"Dispatch","parameterTypes":[],"typeName":"System.Threading.ThreadPoolWorkQueue","moduleName":"System.Private.CoreLib.dll"},{"methodName":"WorkerThreadStart","parameterTypes":[],"typeName":"System.Threading.PortableThreadPool\u002BWorkerThread","moduleName":"System.Private.CoreLib.dll"}]}}
{
"id": 2,
"timestamp": "2023-07-13T21:45:11.8056355Z",
"typeName": "System.InvalidOperationException",
"moduleName": "System.Private.CoreLib.dll",
"message": "Operation is not valid due to the current state of the object.",
"innerExceptions": [],
"stack": {
"threadId": 4768,
"threadName": null,
"frames": [
{
"methodName": "MoveNext",
"methodToken": 100663639,
"parameterTypes": [],
"typeName": "WebApplication3.Pages.IndexModel\u002B\u003CGetData\u003Ed__3",
"moduleName": "WebApplication3.dll",
"moduleVersionId": "bf769014-c2e2-496a-93b7-76fbbcd04be5"
},
... // see stacks.md
]
}
}
{
"id": 3,
"timestamp": "2023-07-13T21:46:18.7530773Z",
"typeName": "System.ObjectDisposedException",
"moduleName": "System.Private.CoreLib.dll",
"message": "Cannot access a disposed object.\r\nObject name: \u0027System.Net.Sockets.NetworkStream\u0027.",
"innerExceptions": [],
"stack": {
"threadId": 15912,
"threadName": null,
"frames": [
{
"methodName": "ThrowObjectDisposedException",
"methodToken": 100663639,
"parameterTypes": [
"System.Object"
],
"typeName": "System.ThrowHelper",
"moduleName": "System.Private.CoreLib.dll",
"moduleVersionId": "bf769014-c2e2-496a-93b7-76fbbcd04be5"
},
... // see stacks.md
]
}
}
```

## Supported Runtimes
Expand Down
14 changes: 14 additions & 0 deletions documentation/api/stacks.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,19 +81,33 @@ Location: localhost:52323/operations/67f07e40-5cca-4709-9062-26302c484f18
"frames": [
{
"methodName": "GetQueuedCompletionStatus",
"methodToken": 100663634,
"parameterTypes": [],
"typeName": "Interop\u002BKernel32",
"moduleName": "System.Private.CoreLib.dll",
"moduleVersionId": "194ddabd-a802-4520-90ef-854e2f1cd606"
},
{
"methodName": "WaitForSignal",
"methodToken": 100663639,
"parameterTypes": [
"System.Threading.ExecutionContext",
"System.Threading.ContextCallback",
"System.Object"
],
"typeName": "System.Threading.LowLevelLifoSemaphore",
"moduleName": "System.Private.CoreLib.dll",
"moduleVersionId": "194ddabd-a802-4520-90ef-854e2f1cd606"
},
{
"methodName": "Wait",
"methodToken": 100663643,
"parameterTypes": [],
"typeName": "System.Threading.LowLevelLifoSemaphore",
"moduleName": "System.Private.CoreLib.dll",
"moduleVersionId": "194ddabd-a802-4520-90ef-854e2f1cd606"
}
]
}
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,21 +97,23 @@ internal sealed class ExceptionsEventSource : AbstractMonitorEventSource
[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 @@ internal sealed class ExceptionsEventSource : AbstractMonitorEventSource
[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
Loading