Godot Engine – Multi-platform 2D and 3D game engine
[.NET] Filter stack frames by StackTraceHidden instead of skipping a fixed count
Matt Parker1e8e1bf510e6
diff --git a/modules/mono/glue/GodotSharp/GodotSharp/Core/DebuggingUtils.cs b/modules/mono/glue/GodotSharp/GodotSharp/Core/DebuggingUtils.csindex 0c83fd58aa1..d52b1f42c9d 100644--- a/modules/mono/glue/GodotSharp/GodotSharp/Core/DebuggingUtils.cs+++ b/modules/mono/glue/GodotSharp/GodotSharp/Core/DebuggingUtils.cs@@ -104,26 +104,42 @@ namespace Godot}#pragma warning restore IDE1006- internal static unsafe StackFrame? GetCurrentStackFrame(int skipFrames = 0)+ // StackTrace.GetFrames() doesn't filter out hidden frames, only ToString() does.+ private static bool IsHiddenFromStackTrace(MethodBase method){- // We skip 2 frames:- // The first skipped frame is the current method.- // The second skipped frame is a method in NativeInterop.NativeFuncs.- var stackTrace = new StackTrace(skipFrames: 2 + skipFrames, fNeedFileInfo: true);- return stackTrace.GetFrame(0);+ if (method.IsDefined(typeof(StackTraceHiddenAttribute), inherit: false))+ return true;++ Type? declaringType = method.DeclaringType;+ return declaringType is not null && declaringType.IsDefined(typeof(StackTraceHiddenAttribute), inherit: false);+ }++ [StackTraceHidden]+ internal static StackFrame? GetCurrentStackFrame()+ {+ var stackTrace = new StackTrace(fNeedFileInfo: true);++ foreach (StackFrame frame in stackTrace.GetFrames())+ {+ MethodBase? method = frame.GetMethod();+ if (method is null || IsHiddenFromStackTrace(method))+ continue;++ return frame;+ }++ return null;}[UnmanagedCallersOnly]+ [StackTraceHidden]internal static unsafe void GetCurrentStackInfo(void* destVector){try{var vector = (godot_stack_info_vector*)destVector;- // We skip 2 frames:- // The first skipped frame is the current method.- // The second skipped frame is a method in NativeInterop.NativeFuncs.- var stackTrace = new StackTrace(skipFrames: 2, fNeedFileInfo: true);+ var stackTrace = new StackTrace(fNeedFileInfo: true);int frameCount = stackTrace.FrameCount;if (frameCount == 0)@@ -136,7 +152,7 @@ namespace Godot{var method = frame.GetMethod();- if (method is MethodInfo methodInfo && methodInfo.IsDefined(typeof(StackTraceHiddenAttribute)))+ if (method is not null && IsHiddenFromStackTrace(method)){// Skip methods marked hidden from the stack trace.continue;diff --git a/modules/mono/glue/GodotSharp/GodotSharp/Core/GD.cs b/modules/mono/glue/GodotSharp/GodotSharp/Core/GD.csindex b6073c5cfc9..9c66b0d6787 100644--- a/modules/mono/glue/GodotSharp/GodotSharp/Core/GD.cs+++ b/modules/mono/glue/GodotSharp/GodotSharp/Core/GD.cs@@ -338,11 +338,20 @@ namespace Godot[StackTraceHidden]private static void ErrPrintError(string message, godot_error_handler_type type = godot_error_handler_type.ERR_HANDLER_ERROR){- // Skip 1 frame to avoid current method.- var stackFrame = DebuggingUtils.GetCurrentStackFrame(skipFrames: 1);- string callerFilePath = ProjectSettings.LocalizePath(stackFrame.GetFileName());- DebuggingUtils.GetStackFrameMethodDecl(stackFrame, out string callerName);- int callerLineNumber = stackFrame.GetFileLineNumber();+ // Find the first frame that is not hidden from stack traces+ var stackFrame = DebuggingUtils.GetCurrentStackFrame();++ string callerName = "<unknown>";+ string callerFilePath = "<unknown>";+ int callerLineNumber = 0;+ if (stackFrame != null)+ {+ string fileName = stackFrame.GetFileName();+ if (fileName is not null)+ callerFilePath = ProjectSettings.LocalizePath(fileName);+ DebuggingUtils.GetStackFrameMethodDecl(stackFrame, out callerName);+ callerLineNumber = stackFrame.GetFileLineNumber();+ }using godot_string messageStr = Marshaling.ConvertStringToNative(message);using godot_string callerNameStr = Marshaling.ConvertStringToNative(callerName);@@ -361,6 +370,7 @@ namespace Godot/// </code>/// </example>/// <param name="message">Error message.</param>+ [StackTraceHidden]public static void PushError(string message){ErrPrintError(message);@@ -377,6 +387,7 @@ namespace Godot/// </code>/// </example>/// <param name="what">Arguments that form the error message.</param>+ [StackTraceHidden]public static void PushError(params object[] what){ErrPrintError(AppendPrintParams(what));@@ -391,6 +402,7 @@ namespace Godot/// </code>/// </example>/// <param name="message">Warning message.</param>+ [StackTraceHidden]public static void PushWarning(string message){ErrPrintError(message, type: godot_error_handler_type.ERR_HANDLER_WARNING);@@ -405,6 +417,7 @@ namespace Godot/// </code>/// </example>/// <param name="what">Arguments that form the warning message.</param>+ [StackTraceHidden]public static void PushWarning(params object[] what){ErrPrintError(AppendPrintParams(what), type: godot_error_handler_type.ERR_HANDLER_WARNING);diff --git a/modules/mono/glue/GodotSharp/GodotSharp/Core/NativeInterop/NativeFuncs.cs b/modules/mono/glue/GodotSharp/GodotSharp/Core/NativeInterop/NativeFuncs.csindex 39abc4d70e3..91cb6f6904f 100644--- a/modules/mono/glue/GodotSharp/GodotSharp/Core/NativeInterop/NativeFuncs.cs+++ b/modules/mono/glue/GodotSharp/GodotSharp/Core/NativeInterop/NativeFuncs.cs@@ -3,6 +3,7 @@// ReSharper disable InconsistentNamingusing System;+using System.Diagnostics;using System.Runtime.CompilerServices;using Godot.SourceGenerators.Internal;@@ -16,6 +17,7 @@ namespace Godot.NativeInterop*/[GenerateUnmanagedCallbacks(typeof(UnmanagedCallbacks))]+ [StackTraceHidden]public static unsafe partial class NativeFuncs{private static bool initialized;