Search code examples
c#genericsclrcilimetadataimport

Generic type func-eval using ICorDebugEval


I'm making a managed .NET debugger using MDBG sample.

MDBG has no support for property getters evaluation, which I'm trying to add. Please, consider following class structure:

    public abstract class Base<T>{
        public string SomeProp {get;set;}
    }

    public class A : Base<int>{

    }

At some point of time I'm creating an instance of A and stopping at a breakpoint to evaluate it's state.

In my debugger's watch window I introduce "this.SomeProp", that should perform a func-eval of get_SomeProp method on this object and return a null value for given case.

The first issue I've encountered was the fact, that get_SomeProp was defined on the base class, so I had to run through all TypeDefs/TypeRefs/TypeSpecs in the class hierarchy to find the function.

But after it was found, calling

   ICorDebugEval.CallFunction(function.CorFunction, new[] {@object.CorValue});

resulted in: TypeLoadException: The generic type was used with the wrong number of generic arguments in assembly.

As I've realized it happens because non-generic function is defined in a generic class (Base), so when I'm evaluating it I should also indicate class's generic params.

This could be done using

  ICorDebugEval2.CallParameterizedFunction(function.CorFunction,
    genericArguments,
    functionArguments);

The problem is that I have no idea how to extract types of class generic parameters, having only function that I want to evaluate and instance on which I want to evaluate it.

Here is some code I'm currently using:

    private MDbgValue EvaluatePropertyGetter(MDbgFrame scope, MDbgValue @object, string propertyName) {
        var propertyGetter = $"get_{propertyName}";
        var function = ResolveFunctionName(
            scope.Function.Module.CorModule.Name,
            @object.TypeName,
            propertyGetter,
            scope.Thread.CorThread.AppDomain);

        if (function == null) {
            throw new MDbgValueException("Function '" + propertyGetter + "' not found.");
        }

        var eval = Threads.Active.CorThread.CreateEval();
        var typeToken = function.CorFunction.Class.Token;
        var type = function.Module.Importer.GetType(typeToken); //checked that type containing function is generic
        if (type.IsGenericType) {
            //------------->need to get class'es generic param types<------------
            var genericType1 = this.ResolveType("System.Object"); // just a stub
            eval.CallParameterizedFunction(function.CorFunction, new CorType[] {genericType1}, new[] {@object.CorValue});
        }
        else {
            eval.CallFunction(function.CorFunction, new[] {@object.CorValue});
        }

        Go().WaitOne();
        if (!(StopReason is EvalCompleteStopReason)) {
            // we could have received also EvalExceptionStopReason but it's derived from EvalCompleteStopReason
            Console.WriteLine("Func-eval not fully completed and debuggee has stopped");
            Console.WriteLine("Result of funceval won't be printed when finished.");
        }
        else {
            eval = (StopReason as EvalCompleteStopReason).Eval;
            Debug.Assert(eval != null);

            var cv = eval.Result;
            if (cv != null) {
                var mv = new MDbgValue(this, cv);
                return mv;
            }
        }
        return null;
    }

Any suggestion/advice is greatly appreciated!

Regards,


Solution

Thanks to @Brian Reichle outstanding answer I came up with this solution:

 if (type.IsGenericType) {
            //getting Type's Generic parameters
            var typeParams = GetGenericArgs(@object.CorValue.ExactType, function.CorFunction.Class.Token);
            eval.CallParameterizedFunction(function.CorFunction, typeParams.ToArray(), new[] {@object.CorValue});
        }

And the function itself:

 private List<CorType> GetGenericArgs(CorType corType, int classTk) {
        if (corType == null)
            return null;
        List<CorType> list = new List<CorType>();

        var param =corType.TypeParameters;
        var args = GetGenericArgs(corType.Base, classTk);

        if (classTk == corType.Class.Token) {
            list.AddRange(param.Cast<CorType>());
        }

        if (args != null) {
            list.AddRange(args);}

        return list;
    }

Solution

  • You can use ICorDebugValue2::GetExactType on the value object representing an instance of A to get the ICorDebugType for type A, ICorDebugType::GetBase() to get its base class (Base<int>) and ICorDebugType::EnumerateTypeParameters on the base type to get it's type arguments.