Beispiel #1
0
 public static Value CreateValue(AppDomain appDomain, object value)
 {
     if (value == null)
     {
         ICorDebugClass corClass = appDomain.ObjectType.CorType.GetClass();
         Thread         thread   = GetEvaluationThread(appDomain);
         ICorDebugEval  corEval  = thread.CorThread.CreateEval();
         ICorDebugValue corValue = corEval.CreateValue((uint)CorElementType.CLASS, corClass);
         return(new Value(appDomain, corValue));
     }
     else if (value is string)
     {
         return(Eval.NewString(appDomain, (string)value));
     }
     else
     {
         if (!value.GetType().IsPrimitive)
         {
             throw new DebuggerException("Value must be primitve type.  Seen " + value.GetType());
         }
         Value val = Eval.NewObjectNoConstructor(DebugType.CreateFromType(appDomain.Mscorlib, value.GetType()));
         val.PrimitiveValue = value;
         return(val);
     }
 }
Beispiel #2
0
        private static Value GetLiteralValue(DebugFieldInfo fieldInfo)
        {
            CorElementType corElemType = (CorElementType)fieldInfo.FieldProps.ConstantType;

            if (corElemType == CorElementType.CLASS)
            {
                // Only null literals are allowed
                return(Eval.CreateValue(fieldInfo.AppDomain, null));
            }
            else if (corElemType == CorElementType.STRING)
            {
                string str = Marshal.PtrToStringUni(fieldInfo.FieldProps.ConstantPtr, (int)fieldInfo.FieldProps.ConstantStringLength);
                return(Eval.CreateValue(fieldInfo.AppDomain, str));
            }
            else
            {
                DebugType type = DebugType.CreateFromType(fieldInfo.AppDomain.Mscorlib, DebugType.CorElementTypeToManagedType(corElemType));
                if (fieldInfo.FieldType.IsEnum && fieldInfo.FieldType.GetEnumUnderlyingType() == type)
                {
                    Value val          = Eval.NewObjectNoConstructor((DebugType)fieldInfo.FieldType);
                    Value backingField = val.GetMemberValue("value__");
                    backingField.CorGenericValue.SetValue(fieldInfo.FieldProps.ConstantPtr);
                    return(val);
                }
                else
                {
                    Value val = Eval.NewObjectNoConstructor(type);
                    val.CorGenericValue.SetValue(fieldInfo.FieldProps.ConstantPtr);
                    return(val);
                }
            }
        }
        /// <summary>
        /// Creates an expression which, when evaluated, creates a List&lt;T&gt; in the debugee
        /// filled with contents of IEnumerable&lt;T&gt; from the debugee.
        /// </summary>
        /// <param name="iEnumerableVariable">Expression for IEnumerable variable in the debugee.</param>
        /// <param name="itemType">
        /// The generic argument of IEnumerable&lt;T&gt; that <paramref name="iEnumerableVariable"/> implements.</param>
        public static Expression CreateDebugListExpression(Expression iEnumerableVariable, DebugType itemType, out DebugType listType)
        {
            // is using itemType.AppDomain ok?
            listType = DebugType.CreateFromType(itemType.AppDomain, typeof(System.Collections.Generic.List <>), itemType);
            var iEnumerableType = DebugType.CreateFromType(itemType.AppDomain, typeof(IEnumerable <>), itemType);
            // explicitely cast the variable to IEnumerable<T>, where T is itemType
            Expression iEnumerableVariableExplicitCast = new CastExpression(iEnumerableType.GetTypeReference(), iEnumerableVariable, CastType.Cast);

            return(new ObjectCreateExpression(listType.GetTypeReference(), iEnumerableVariableExplicitCast.ToList()));
        }
Beispiel #4
0
        public static DebugType ResolveType(this AstNode expr, Debugger.AppDomain appDomain)
        {
            var result = expr.GetStaticType();

            if (result != null)
            {
                return(result);
            }

            return(DebugType.CreateFromType(appDomain, expr.Annotation <Type>()));
        }
        /// <summary>
        /// Invokes RuntimeHelpers.GetHashCode on given value, that is a default hashCode ignoring user overrides.
        /// </summary>
        /// <param name="value">Valid value.</param>
        /// <returns>Hash code of the object in the debugee.</returns>
        public static int InvokeDefaultGetHashCode(this Value value)
        {
            if (hashCodeMethod == null || hashCodeMethod.Process.HasExited)
            {
                DebugType typeRuntimeHelpers = DebugType.CreateFromType(value.AppDomain, typeof(System.Runtime.CompilerServices.RuntimeHelpers));
                hashCodeMethod = (DebugMethodInfo)typeRuntimeHelpers.GetMethod("GetHashCode", BindingFlags.Public | BindingFlags.Static);
                if (hashCodeMethod == null)
                {
                    throw new DebuggerException("Cannot obtain method System.Runtime.CompilerServices.RuntimeHelpers.GetHashCode");
                }
            }
            Value defaultHashCode = Eval.InvokeMethod(DebuggerHelpers.hashCodeMethod, null, new Value[] { value });

            //MethodInfo method = value.Type.GetMember("GetHashCode", BindingFlags.Method | BindingFlags.IncludeSuperType) as MethodInfo;
            //string hashCode = value.InvokeMethod(method, null).AsString;
            return((int)defaultHashCode.PrimitiveValue);
        }
Beispiel #6
0
        /// <summary>
        /// Creates an expression which, when evaluated, creates a List&lt;T&gt; in the debugee
        /// filled with contents of IEnumerable&lt;T&gt; from the debugee.
        /// </summary>
        /// <param name="iEnumerableVariable">Expression for IEnumerable variable in the debugee.</param>
        /// <param name="itemType">
        /// The generic argument of IEnumerable&lt;T&gt; that <paramref name="iEnumerableVariable"/> implements.</param>
        public static Expression CreateDebugListExpression(Expression iEnumerableVariable, DebugType itemType, out DebugType listType)
        {
            // is using itemType.AppDomain ok?
            listType = DebugType.CreateFromType(itemType.AppDomain, typeof(System.Collections.Generic.List <>), itemType);
            var iEnumerableType = DebugType.CreateFromType(itemType.AppDomain, typeof(IEnumerable <>), itemType);
            // explicitely cast the variable to IEnumerable<T>, where T is itemType
            Expression iEnumerableVariableExplicitCast = new CastExpression {
                Expression = iEnumerableVariable.Clone(), Type = iEnumerableType.GetTypeReference()
            };

            var obj = new ObjectCreateExpression()
            {
                Type = listType.GetTypeReference()
            };

            obj.Arguments.Add(iEnumerableVariableExplicitCast);

            return(obj);
        }
        /// <summary>
        /// Invokes RuntimeHelpers.GetHashCode on given value, that is a default hashCode ignoring user overrides.
        /// </summary>
        /// <param name="value">Valid value.</param>
        /// <returns>Hash code of the object in the debugee.</returns>
        public static int InvokeDefaultGetHashCode(this Value value)
        {
            if (hashCodeMethod == null || hashCodeMethod.Process.HasExited)
            {
                DebugType typeRuntimeHelpers = DebugType.CreateFromType(value.AppDomain, typeof(System.Runtime.CompilerServices.RuntimeHelpers));
                hashCodeMethod = (DebugMethodInfo)typeRuntimeHelpers.GetMethod("GetHashCode", BindingFlags.Public | BindingFlags.Static);
                if (hashCodeMethod == null)
                {
                    throw new DebuggerException("Cannot obtain method System.Runtime.CompilerServices.RuntimeHelpers.GetHashCode");
                }
            }
            // David: I had hard time finding out how to invoke static method.
            // value.InvokeMethod is nice for instance methods.
            // what about MethodInfo.Invoke() ?
            // also, there could be an overload that takes 1 parameter instead of array
            Value defaultHashCode = Eval.InvokeMethod(DebuggerHelpers.hashCodeMethod, null, new Value[] { value });

            //MethodInfo method = value.Type.GetMember("GetHashCode", BindingFlags.Method | BindingFlags.IncludeSuperType) as MethodInfo;
            //string hashCode = value.InvokeMethod(method, null).AsString;
            return((int)defaultHashCode.PrimitiveValue);
        }