virtualx-engine/modules/mono/glue/cs_files/DebuggingUtils.cs
Ignacio Etcheverry bff9627dc4 Mono: Some StackTrace to StackInfo[] fixes
- Sometimes `StackFrame.GetMethod()` returns null (e.g.: latest frame of a `MissingMethodException`). Still not sure what to do with that frame (maybe skip it), but at least it no longer fails.
- Skip `CSharpLanguage::debug_get_current_stack_info()` if an error is printed from `GDMonoUtils::update_corlib_cache()`.
- Fix crash when calling `GDMonoUtils::print_unhandled_exception(exc)` if there is no ScriptDebugger attached.
2018-01-12 19:31:15 +01:00

83 lines
2.2 KiB
C#

using System;
using System.Diagnostics;
using System.Reflection;
using System.Text;
namespace Godot
{
internal static class DebuggingUtils
{
internal static void AppendTypeName(this StringBuilder sb, Type type)
{
if (type.IsPrimitive)
sb.Append(type.Name);
else if (type == typeof(void))
sb.Append("void");
else
sb.Append(type.ToString());
sb.Append(" ");
}
public static void GetStackFrameInfo(StackFrame frame, out string fileName, out int fileLineNumber, out string methodDecl)
{
fileName = frame.GetFileName();
fileLineNumber = frame.GetFileLineNumber();
MethodBase methodBase = frame.GetMethod();
if (methodBase == null)
{
methodDecl = string.Empty;
return;
}
StringBuilder sb = new StringBuilder();
if (methodBase is MethodInfo methodInfo)
sb.AppendTypeName(methodInfo.ReturnType);
sb.Append(methodBase.DeclaringType.FullName);
sb.Append(".");
sb.Append(methodBase.Name);
if (methodBase.IsGenericMethod)
{
Type[] genericParams = methodBase.GetGenericArguments();
sb.Append("<");
for (int j = 0; j < genericParams.Length; j++)
{
if (j > 0)
sb.Append(", ");
sb.AppendTypeName(genericParams[j]);
}
sb.Append(">");
}
sb.Append("(");
bool varArgs = (methodBase.CallingConvention & CallingConventions.VarArgs) != 0;
ParameterInfo[] parameter = methodBase.GetParameters();
for (int i = 0; i < parameter.Length; i++)
{
if (i > 0)
sb.Append(", ");
if (i == parameter.Length - 1 && varArgs)
sb.Append("params ");
sb.AppendTypeName(parameter[i].ParameterType);
}
sb.Append(")");
methodDecl = sb.ToString();
}
}
}