Ejemplo n.º 1
0
 public void SetPrimitiveValue(Thread evalThread, object value)
 {
     if (this.Type.IsKnownType(KnownTypeCode.String))
     {
         this.SetValue(evalThread, Eval.NewString(evalThread, value.ToString()));
     }
     else
     {
         if (!Type.IsPrimitiveType() && Type.Kind != TypeKind.Enum)
         {
             throw new DebuggerException("Value is not a primitive type");
         }
         if (value == null)
         {
             throw new DebuggerException("Can not set primitive value to null");
         }
         KnownTypeCode knownTypeCode;
         var           typeDefinition = this.Type.GetDefinition();
         if (this.Type.Kind == TypeKind.Enum)
         {
             knownTypeCode = typeDefinition.EnumUnderlyingType.GetDefinition().KnownTypeCode;
         }
         else
         {
             knownTypeCode = typeDefinition.KnownTypeCode;
         }
         CorGenericValue.SetValue(knownTypeCode, value);
     }
 }
Ejemplo n.º 2
0
        public static void SetValue(this CorValRef thisVal, EvaluationContext ctx, CorValRef val)
        {
            CorEvaluationContext cctx = (CorEvaluationContext)ctx;
            CorObjectAdaptor     actx = (CorObjectAdaptor)ctx.Adapter;

            if (actx.IsEnum(ctx, thisVal.Val.ExactType) && !actx.IsEnum(ctx, val.Val.ExactType))
            {
                ValueReference vr = actx.GetMember(ctx, null, thisVal, "value__");
                vr.Value = val;
                // Required to make sure that var returns an up-to-date value object
                thisVal.IsValid = false;
                return;
            }

            CorReferenceValue s = thisVal.Val.CastToReferenceValue();

            if (s != null)
            {
                CorReferenceValue v = val.Val.CastToReferenceValue();
                if (v != null)
                {
                    s.Value = v.Value;
                    return;
                }
            }
            CorGenericValue gv = CorObjectAdaptor.GetRealObject(cctx, thisVal.Val) as CorGenericValue;

            if (gv != null)
            {
                gv.SetValue(ctx.Adapter.TargetObjectToObject(ctx, val));
            }
        }
Ejemplo n.º 3
0
        public static T value <T>(this CorGenericValue genericValue)
        {
            var value = genericValue.GetValue();

            if (value is T)
            {
                return((T)value);
            }
            return(default(T));
        }
Ejemplo n.º 4
0
        private CorGenericValue GetGenericValue()
        {
            CorGenericValue gv = CorValue.CastToGenericValue();

            if (gv == null)
            {
                throw new MDbgValueWrongTypeException();
            }
            return(gv);
        }
Ejemplo n.º 5
0
 public void SetPrimitiveValue(Thread evalThread, object value)
 {
     if (this.Type.IsKnownType(KnownTypeCode.String))
     {
         this.SetValue(evalThread, Eval.NewString(evalThread, value.ToString()));
     }
     else
     {
         if (!this.Type.IsPrimitiveType())
         {
             throw new DebuggerException("Value is not a primitive type");
         }
         if (value == null)
         {
             throw new DebuggerException("Can not set primitive value to null");
         }
         CorGenericValue.SetValue(this.Type.GetDefinition().KnownTypeCode, value);
     }
 }
Ejemplo n.º 6
0
        public override object TargetObjectToObject(EvaluationContext ctx, object objr)
        {
            CorValue obj = GetRealObject(ctx, objr);

            if ((obj is CorReferenceValue) && ((CorReferenceValue)obj).IsNull)
            {
                return(new EvaluationResult("(null)"));
            }

            CorStringValue stringVal = obj as CorStringValue;

            if (stringVal != null)
            {
                return(stringVal.String);
            }

            CorArrayValue arr = obj as CorArrayValue;

            if (arr != null)
            {
                return(base.TargetObjectToObject(ctx, objr));
            }

            CorEvaluationContext cctx = (CorEvaluationContext)ctx;
            CorObjectValue       co   = obj as CorObjectValue;

            if (co != null)
            {
                return(base.TargetObjectToObject(ctx, objr));
            }

            CorGenericValue genVal = obj as CorGenericValue;

            if (genVal != null)
            {
                return(genVal.GetValue());
            }

            return(base.TargetObjectToObject(ctx, objr));
        }
Ejemplo n.º 7
0
        public override object CreateValue(EvaluationContext gctx, object value)
        {
            CorEvaluationContext ctx = (CorEvaluationContext)gctx;

            if (value is string)
            {
                return(new CorValRef(delegate {
                    return ctx.Session.NewString(ctx, (string)value);
                }));
            }

            foreach (KeyValuePair <CorElementType, Type> tt in CorMetadataImport.CoreTypes)
            {
                if (tt.Value == value.GetType())
                {
                    CorValue        val = ctx.Eval.CreateValue(tt.Key, null);
                    CorGenericValue gv  = val.CastToGenericValue();
                    gv.SetValue(value);
                    return(new CorValRef(val));
                }
            }
            throw new NotSupportedException();
        }
Ejemplo n.º 8
0
 public static CorGenericValue value(this CorGenericValue genericValue, object value)
 {
     genericValue.SetValue(value);
     return(genericValue);
 }
Ejemplo n.º 9
0
 public static object value(this CorGenericValue genericValue)
 {
     return(genericValue.GetValue());
 }
Ejemplo n.º 10
0
        public static CorValue GetRealObject(EvaluationContext ctx, CorValue obj)
        {
            CorEvaluationContext cctx = (CorEvaluationContext)ctx;

            if (obj == null)
            {
                return(null);
            }

            try {
                if (obj is CorStringValue)
                {
                    return(obj);
                }

                if (obj is CorGenericValue)
                {
                    return(obj);
                }

                if (obj is CorGenericValue)
                {
                    return(obj);
                }

                if (obj is CorArrayValue)
                {
                    return(obj);
                }

                CorArrayValue arrayVal = obj.CastToArrayValue();
                if (arrayVal != null)
                {
                    return(arrayVal);
                }

                CorReferenceValue refVal = obj.CastToReferenceValue();
                if (refVal != null)
                {
                    if (refVal.IsNull)
                    {
                        return(refVal);
                    }
                    else
                    {
                        cctx.Session.WaitUntilStopped();
                        return(GetRealObject(cctx, refVal.Dereference()));
                    }
                }

                cctx.Session.WaitUntilStopped();
                CorBoxValue boxVal = obj.CastToBoxValue();
                if (boxVal != null)
                {
                    return(Unbox(ctx, boxVal));
                }

                if (obj.ExactType.Type == CorElementType.ELEMENT_TYPE_STRING)
                {
                    return(obj.CastToStringValue());
                }

                if (CorMetadataImport.CoreTypes.ContainsKey(obj.Type))
                {
                    CorGenericValue genVal = obj.CastToGenericValue();
                    if (genVal != null)
                    {
                        return(genVal);
                    }
                }

                if (!(obj is CorObjectValue))
                {
                    return(obj.CastToObjectValue());
                }
            }
            catch {
                // Ignore
                throw;
            }
            return(obj);
        }
Ejemplo n.º 11
0
        public override object TryCast(EvaluationContext ctx, object val, object type)
        {
            CorType  ctype     = (CorType)GetValueType(ctx, val);
            CorValue obj       = GetRealObject(ctx, val);
            string   tname     = GetTypeName(ctx, type);
            string   ctypeName = GetValueTypeName(ctx, val);

            if (tname == "System.Object")
            {
                return(val);
            }

            if (tname == ctypeName)
            {
                return(val);
            }

            if (obj is CorStringValue)
            {
                return(ctypeName == tname ? val : null);
            }

            if (obj is CorArrayValue)
            {
                return((ctypeName == tname || ctypeName == "System.Array") ? val : null);
            }

            if (obj is CorObjectValue)
            {
                CorObjectValue co = (CorObjectValue)obj;
                if (IsEnum(ctx, co.ExactType))
                {
                    ValueReference rval = GetMember(ctx, null, val, "value__");
                    return(TryCast(ctx, rval.Value, type));
                }

                while (ctype != null)
                {
                    if (GetTypeName(ctx, ctype) == tname)
                    {
                        return(val);
                    }
                    ctype = ctype.Base;
                }
                return(null);
            }

            CorGenericValue genVal = obj as CorGenericValue;

            if (genVal != null)
            {
                Type t = Type.GetType(tname);
                if (t != null && t.IsPrimitive && t != typeof(string))
                {
                    object pval = genVal.GetValue();
                    try {
                        pval = System.Convert.ChangeType(pval, t);
                    }
                    catch {
                        return(null);
                    }
                    return(CreateValue(ctx, pval));
                }
                else if (IsEnum(ctx, (CorType)type))
                {
                    return(CreateEnum(ctx, (CorType)type, val));
                }
            }
            return(null);
        }
Ejemplo n.º 12
0
        public override string CallToString(EvaluationContext ctx, object objr)
        {
            CorValue obj = GetRealObject(ctx, objr);

            if ((obj is CorReferenceValue) && ((CorReferenceValue)obj).IsNull)
            {
                return(string.Empty);
            }

            CorStringValue stringVal = obj as CorStringValue;

            if (stringVal != null)
            {
                return(stringVal.String);
            }

            CorArrayValue arr = obj as CorArrayValue;

            if (arr != null)
            {
                StringBuilder tn = new StringBuilder(GetDisplayTypeName(ctx, arr.ExactType.FirstTypeParameter));
                tn.Append("[");
                int[] dims = arr.GetDimensions();
                for (int n = 0; n < dims.Length; n++)
                {
                    if (n > 0)
                    {
                        tn.Append(',');
                    }
                    tn.Append(dims[n]);
                }
                tn.Append("]");
                return(tn.ToString());
            }

            CorEvaluationContext cctx = (CorEvaluationContext)ctx;
            CorObjectValue       co   = obj as CorObjectValue;

            if (co != null)
            {
                if (IsEnum(ctx, co.ExactType))
                {
                    MetadataType   rt             = co.ExactType.GetTypeInfo(cctx.Session) as MetadataType;
                    bool           isFlags        = rt != null && rt.ReallyIsFlagsEnum;
                    string         enumName       = GetTypeName(ctx, co.ExactType);
                    ValueReference val            = GetMember(ctx, null, objr, "value__");
                    ulong          nval           = (ulong)System.Convert.ChangeType(val.ObjectValue, typeof(ulong));
                    ulong          remainingFlags = nval;
                    string         flags          = null;
                    foreach (ValueReference evals in GetMembers(ctx, co.ExactType, null, BindingFlags.Public | BindingFlags.Static))
                    {
                        ulong nev = (ulong)System.Convert.ChangeType(evals.ObjectValue, typeof(ulong));
                        if (nval == nev)
                        {
                            return(evals.Name);
                        }
                        if (isFlags && nev != 0 && (nval & nev) == nev)
                        {
                            if (flags == null)
                            {
                                flags = enumName + "." + evals.Name;
                            }
                            else
                            {
                                flags += " | " + enumName + "." + evals.Name;
                            }
                            remainingFlags &= ~nev;
                        }
                    }
                    if (isFlags)
                    {
                        if (remainingFlags == nval)
                        {
                            return(nval.ToString());
                        }
                        if (remainingFlags != 0)
                        {
                            flags += " | " + remainingFlags;
                        }
                        return(flags);
                    }
                    else
                    {
                        return(nval.ToString());
                    }
                }

                CorType targetType = (CorType)GetValueType(ctx, objr);

                MethodInfo met = OverloadResolve(cctx, "ToString", targetType, new CorType[0], BindingFlags.Public | BindingFlags.Instance, false);
                if (met != null && met.DeclaringType.FullName != "System.Object")
                {
                    object[]       args = new object[0];
                    object         ores = RuntimeInvoke(ctx, targetType, objr, "ToString", args, args);
                    CorStringValue res  = GetRealObject(ctx, ores) as CorStringValue;
                    if (res != null)
                    {
                        return(res.String);
                    }
                }

                return(GetDisplayTypeName(ctx, targetType));
            }

            CorGenericValue genVal = obj as CorGenericValue;

            if (genVal != null)
            {
                return(genVal.GetValue().ToString());
            }

            return(base.CallToString(ctx, obj));
        }
Ejemplo n.º 13
0
        public CorValue VariableExpressionInvoke(ExpressionParsingResult info)
        {
            //based on MdbgCommands.SetCmd(string arguments)
            if (info.Instance == null)
            {
                throw new Exception("Cannot resolve variable ");
            }

            if (info.Arguments == null || !info.Arguments.Any())
            {
                throw new Exception("Cannot resolve arguments ");
            }

            // Arguments has to be in the form of variable=varName, variable=value or variable=(<type>)value,
            // where we use the ldasm naming convention (e.g. "int", "sbyte", "ushort", etc...) for <type>.
            // Example inputs: var=myInt, var=45, var=(long)45

            MDbgValue lsMVar = new MDbgValue(process, info.Instance);
            CorValue  val    = info.Arguments.First();

            CorGenericValue valGeneric        = val as CorGenericValue;
            bool            bIsReferenceValue = val is CorReferenceValue;

            if (lsMVar != null)
            {
                if (valGeneric != null)
                {
                    CorValue lsVar = lsMVar.CorValue;
                    if (lsVar == null)
                    {
                        throw new Exception("cannot set constant values to unavailable variables");
                    }

                    // val is a primitive value
                    CorGenericValue lsGenVal = lsVar.CastToGenericValue();
                    if (lsGenVal == null)
                    {
                        throw new Exception("cannot set constant values to non-primitive values");
                    }

                    try
                    {
                        // We want to allow some type coercion. Eg, casting between integer precisions.
                        lsMVar.Value = val; // This may do type coercion
                    }
                    catch (MDbgValueWrongTypeException)
                    {
                        throw new Exception(String.Format("Type mismatch. Can't convert from {0} to {1}", val.Type, lsGenVal.Type));
                    }
                }
                else if (bIsReferenceValue)
                {
                    //reget variable
                    lsMVar       = process.ResolveVariable(info.Member, process.Threads.Active.CurrentFrame);
                    lsMVar.Value = val;
                }
                else
                {
                    if (val.CastToHeapValue() != null)
                    {
                        throw new Exception("Heap values should be assigned only to debugger variables");
                    }
                    if (val.CastToGenericValue() != null)
                    {
                        lsMVar.Value = val.CastToGenericValue();
                    }
                    else
                    {
                        lsMVar.Value = val.CastToReferenceValue();
                    }
                }
            }

            // as a last thing we do is to return new value of the variable
            lsMVar = process.ResolveVariable(info.Member, process.Threads.Active.CurrentFrame);
            return(lsMVar.CorValue);
        }