summaryrefslogtreecommitdiff
path: root/modules/mono/glue/cs_files/DebuggingUtils.cs
blob: ced78f658d0b16642a2f0b9a742663c2a767fee5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
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();

            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();
        }
    }
}