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

Hide frames with StackTraceHiddenAttribute from /exceptions and /stacks #7532

Merged
merged 16 commits into from
Oct 21, 2024
1 change: 1 addition & 0 deletions documentation/api/definitions.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ First Available: 8.0 Preview 7
| `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`. |
| `hidden`| bool |(8.1+ and 9.0+) Whether this frame has the [StackTraceHiddenAttribute](https://learn.microsoft.com/dotnet/api/system.diagnostics.stacktracehiddenattribute) and should be omitted from stack trace text. |

## CallStackResult

Expand Down
8 changes: 4 additions & 4 deletions documentation/api/exceptions.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,6 @@ System.InvalidOperationException: Operation is not valid due to the current stat
First chance exception at 2023-07-13T21:46:18.7530773Z
System.ObjectDisposedException: Cannot access a disposed object.
Object name: 'System.Net.Sockets.NetworkStream'.
at System.ThrowHelper.ThrowObjectDisposedException(System.Object)
at System.ObjectDisposedException.ThrowIf(System.Boolean,System.Object)
at System.Net.Sockets.NetworkStream.ReadAsync(System.Memory`1[[System.Byte, System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]][System.Byte],System.Threading.CancellationToken)
at System.Net.Http.HttpConnection+<<EnsureReadAheadTaskHasStarted>g__ReadAheadWithZeroByteReadAsync|43_0>d.MoveNext()
at System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1+AsyncStateMachineBox`1[System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1+AsyncStateMachineBox`1+TResult,System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1+AsyncStateMachineBox`1+TStateMachine].ExecutionContextCallback(System.Object)
Expand Down Expand Up @@ -140,7 +138,8 @@ Content-Type: application/x-ndjson
"parameterTypes": [],
"typeName": "WebApplication3.Pages.IndexModel\u002B\u003CGetData\u003Ed__3",
"moduleName": "WebApplication3.dll",
"moduleVersionId": "bf769014-c2e2-496a-93b7-76fbbcd04be5"
"moduleVersionId": "bf769014-c2e2-496a-93b7-76fbbcd04be5",
"hidden": false
},
... // see stacks.md
]
Expand All @@ -165,7 +164,8 @@ Content-Type: application/x-ndjson
],
"typeName": "System.ThrowHelper",
"moduleName": "System.Private.CoreLib.dll",
"moduleVersionId": "bf769014-c2e2-496a-93b7-76fbbcd04be5"
"moduleVersionId": "bf769014-c2e2-496a-93b7-76fbbcd04be5",
"hidden": true
},
... // see stacks.md
]
Expand Down
9 changes: 6 additions & 3 deletions documentation/api/stacks.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,21 +84,24 @@ Location: localhost:52323/operations/67f07e40-5cca-4709-9062-26302c484f18
"methodToken": 100663634,
"typeName": "Interop\u002BKernel32",
"moduleName": "System.Private.CoreLib.dll",
"moduleVersionId": "194ddabd-a802-4520-90ef-854e2f1cd606"
"moduleVersionId": "194ddabd-a802-4520-90ef-854e2f1cd606",
"hidden": false
},
{
"methodName": "WaitForSignal",
"methodToken": 100663639,
"typeName": "System.Threading.LowLevelLifoSemaphore",
"moduleName": "System.Private.CoreLib.dll",
"moduleVersionId": "194ddabd-a802-4520-90ef-854e2f1cd606"
"moduleVersionId": "194ddabd-a802-4520-90ef-854e2f1cd606",
"hidden": false
},
{
"methodName": "Wait",
"methodToken": 100663643,
"typeName": "System.Threading.LowLevelLifoSemaphore",
"moduleName": "System.Private.CoreLib.dll",
"moduleVersionId": "194ddabd-a802-4520-90ef-854e2f1cd606"
"moduleVersionId": "194ddabd-a802-4520-90ef-854e2f1cd606",
"hidden": false
}
]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
using Microsoft.Diagnostics.Monitoring.WebApi.Stacks;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Text.Json.Serialization;

namespace Microsoft.Diagnostics.Monitoring.WebApi.Models
{
[DebuggerDisplay("{ModuleName,nq}!{TypeName,nq}.{MethodName,nq}")]
public class CallStackFrame
{
[JsonPropertyName("methodName")]
Expand Down Expand Up @@ -50,6 +52,9 @@ public string MethodNameWithGenericArgTypes
[JsonPropertyName("moduleVersionId")]
public Guid ModuleVersionId { get; set; }

[JsonPropertyName("hidden")]
public bool Hidden { get; set; }

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ public override async Task FormatStack(CallStackResult stackResult, Cancellation
}
else if (cache.FunctionData.TryGetValue(frame.FunctionId, out FunctionData? functionData))
{
if (StackUtilities.ShouldHideFunctionFromStackTrace(cache, functionData))
{
continue;
}

if (!functionToSharedFrameMap.TryGetValue(frame.FunctionId, out int mapping))
{
// Note this may imply some duplicate frames because we use FunctionId as a unique identifier for a frame,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@ public override async Task FormatStack(CallStackResult stackResult, Cancellation
{
builder.Clear();
builder.Append(Indent);
BuildFrame(builder, stackResult.NameCache, frame);
await writer.WriteLineAsync(builder, token);
if (BuildFrame(builder, stackResult.NameCache, frame))
{
await writer.WriteLineAsync(builder, token);
}
}
await writer.WriteLineAsync();
}
Expand All @@ -42,14 +44,20 @@ public override async Task FormatStack(CallStackResult stackResult, Cancellation
#endif
}

private static void BuildFrame(StringBuilder builder, NameCache cache, CallStackFrame frame)
/// <returns>True if the frame should be included in the stack trace.</returns>
private static bool BuildFrame(StringBuilder builder, NameCache cache, CallStackFrame frame)
{
if (frame.FunctionId == 0)
{
builder.Append(NativeFrame);
}
else if (cache.FunctionData.TryGetValue(frame.FunctionId, out FunctionData? functionData))
{
if (StackUtilities.ShouldHideFunctionFromStackTrace(cache, functionData))
{
return false;
}

builder.Append(NameFormatter.GetModuleName(cache, functionData.ModuleId));
builder.Append(ModuleSeparator);
NameFormatter.BuildTypeName(builder, cache, functionData);
Expand All @@ -61,6 +69,8 @@ private static void BuildFrame(StringBuilder builder, NameCache cache, CallStack
{
builder.Append(UnknownFunction);
}

return true;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ internal static Models.CallStackFrame CreateFrameModel(CallStackFrame frame, Nam
//Offset = frame.Offset,
ModuleName = NameFormatter.UnknownModule,
ModuleVersionId = Guid.Empty,
Hidden = false,

SimpleParameterTypes = ensureParameterTypeFieldsNotNull ? [] : null,
FullParameterTypes = ensureParameterTypeFieldsNotNull ? [] : null,
Expand All @@ -59,6 +60,7 @@ internal static Models.CallStackFrame CreateFrameModel(CallStackFrame frame, Nam
{
frameModel.MethodToken = functionData.MethodToken;
frameModel.ModuleName = NameFormatter.GetModuleName(cache, functionData.ModuleId);
frameModel.Hidden = ShouldHideFunctionFromStackTrace(cache, functionData);

if (cache.ModuleData.TryGetValue(functionData.ModuleId, out ModuleData? moduleData))
{
Expand Down Expand Up @@ -90,6 +92,32 @@ internal static Models.CallStackFrame CreateFrameModel(CallStackFrame frame, Nam
return frameModel;
}

public static bool ShouldHideFunctionFromStackTrace(NameCache cache, FunctionData functionData)
{
if (functionData.StackTraceHidden)
{
return true;
}

if (cache.ClassData.TryGetValue(functionData.ParentClass, out ClassData? classData))
{
if (classData.StackTraceHidden)
{
return true;
}
}

if (cache.TokenData.TryGetValue(new ModuleScopedToken(functionData.ModuleId, functionData.ParentClassToken), out TokenData? tokenData))
{
if (tokenData.StackTraceHidden)
{
return true;
}
}

return false;
}

internal static StacksFormatter CreateFormatter(StackFormat format, Stream outputStream) =>
format switch
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ HRESULT TypeNameUtilities::GetFunctionInfo(NameCache& nameCache, FunctionID id,

IfFailRet(GetModuleInfo(nameCache, moduleId));

bool stackTraceHidden = ShouldHideFromStackTrace(moduleId, classToken);
bool stackTraceHidden = ShouldHideFromStackTrace(moduleId, token);

nameCache.AddFunctionData(moduleId, id, tstring(funcName), classId, token, classToken, typeArgs, typeArgsCount, stackTraceHidden);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ public static class SubScenarios
public const string EclipsingExceptionFromMethodCall = nameof(EclipsingExceptionFromMethodCall);
public const string AggregateException = nameof(AggregateException);
public const string ReflectionTypeLoadException = nameof(ReflectionTypeLoadException);
public const string HiddenFramesExceptionCommand = nameof(HiddenFramesExceptionCommand);
}

public static class Commands
Expand Down
Loading