static Value GetLiteralValue(Thread evalThread, IField fieldInfo) { var constValue = fieldInfo.ConstantValue; if (constValue == null || constValue is string) { return(Eval.CreateValue(evalThread, constValue)); } else if (fieldInfo.Type.IsPrimitiveType()) { Value val = Eval.NewObjectNoConstructor(evalThread, fieldInfo.Type); val.CorGenericValue.SetValue(fieldInfo.Type.GetDefinition().KnownTypeCode, constValue); return(val); } else if (fieldInfo.Type.Kind == TypeKind.Enum) { Value val = Eval.NewObjectNoConstructor(evalThread, fieldInfo.Type); Value backingField = val.GetFieldValue("value__"); var enumType = fieldInfo.Type.GetDefinition().EnumUnderlyingType.GetDefinition().KnownTypeCode; backingField.CorGenericValue.SetValue(enumType, constValue); return(val); } else { throw new NotSupportedException(); } }
public static Value CreateValue(Thread evalThread, object value) { if (value == null) { ICorDebugClass corClass = evalThread.AppDomain.ObjectType.ToCorDebug().GetClass(); ICorDebugEval corEval = evalThread.CorThread.CreateEval(); ICorDebugValue corValue = corEval.CreateValue((uint)CorElementType.CLASS, corClass); return(new Value(evalThread.AppDomain, corValue)); } else if (value is string) { return(Eval.NewString(evalThread, (string)value)); } else { if (!value.GetType().IsPrimitive) { throw new DebuggerException("Value must be primitve type. Seen " + value.GetType()); } IType type = evalThread.AppDomain.Compilation.FindType(value.GetType()); Value val = Eval.NewObjectNoConstructor(evalThread, type); val.SetPrimitiveValue(evalThread, value); return(val); } }
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); } } }
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); } }
// Box value type public Value Box(Thread evalThread) { byte[] rawValue = this.CorGenericValue.GetRawValue(); // The type must not be a primive type (always true in current design) ICorDebugReferenceValue corRefValue = Eval.NewObjectNoConstructor(evalThread, this.Type).CorReferenceValue; // Make the reference to box permanent corRefValue = ((ICorDebugHeapValue2)corRefValue.Dereference()).CreateHandle(CorDebugHandleType.HANDLE_STRONG); // Create new value Value newValue = new Value(appDomain, corRefValue); // Copy the data inside the box newValue.CorGenericValue.SetRawValue(rawValue); return(newValue); }
public Value GetPermanentReference() { ICorDebugValue corValue = this.CorValue; if (this.Type.IsClass) { corValue = this.CorObjectValue.CastTo <ICorDebugHeapValue2>().CreateHandle(CorDebugHandleType.HANDLE_STRONG).CastTo <ICorDebugValue>(); } if (this.Type.IsValueType || this.Type.IsPrimitive) { if (!corValue.Is <ICorDebugReferenceValue>()) { // Box the value type if (this.Type.IsPrimitive) { // Get value type for the primive type object o = this.PrimitiveValue; corValue = Eval.NewObjectNoConstructor(DebugType.Create(process, null, this.Type.FullName)).RawCorValue; this.SetPrimitiveValue(o); } else { corValue = Eval.NewObjectNoConstructor(this.Type).RawCorValue; } // Make the reference to box permanent corValue = corValue.CastTo <ICorDebugReferenceValue>().Dereference().CastTo <ICorDebugHeapValue2>().CreateHandle(CorDebugHandleType.HANDLE_STRONG).CastTo <ICorDebugValue>(); // Create new value Value newValue = new Value(process, new IExpirable[] {}, new IMutable[] {}, delegate { return(corValue); }); // Copy the data inside the box newValue.CorGenericValue.RawValue = this.CorGenericValue.RawValue; return(newValue); } else { // Make the reference to box permanent corValue = corValue.CastTo <ICorDebugReferenceValue>().Dereference().CastTo <ICorDebugHeapValue2>().CreateHandle(CorDebugHandleType.HANDLE_STRONG).CastTo <ICorDebugValue>(); } } return(new Value(process, new IExpirable[] {}, new IMutable[] {}, delegate { return corValue; })); }
public void SetValue(Value newValue) { ICorDebugValue corValue = this.CorValue; ICorDebugValue newCorValue = newValue.CorValue; if (corValue.Is <ICorDebugReferenceValue>()) { if (newCorValue.Is <ICorDebugObjectValue>()) { ICorDebugValue box = Eval.NewObjectNoConstructor(newValue.Type).CorValue; newCorValue = box; } corValue.CastTo <ICorDebugReferenceValue>().SetValue(newCorValue.CastTo <ICorDebugReferenceValue>().Value); } else { corValue.CastTo <ICorDebugGenericValue>().RawValue = newCorValue.CastTo <ICorDebugGenericValue>().RawValue; } }
Value Box() { byte[] rawValue = this.CorGenericValue.RawValue; // Box the value type ICorDebugValue corValue; if (this.Type.IsPrimitive) { // Get value type for the primive type corValue = Eval.NewObjectNoConstructor(DebugType.Create(process, null, this.Type.FullName)).CorValue; } else { corValue = Eval.NewObjectNoConstructor(this.Type).CorValue; } // Make the reference to box permanent corValue = corValue.CastTo <ICorDebugReferenceValue>().Dereference().CastTo <ICorDebugHeapValue2>().CreateHandle(CorDebugHandleType.HANDLE_STRONG).CastTo <ICorDebugValue>(); // Create new value Value newValue = new Value(process, expression, corValue); // Copy the data inside the box newValue.CorGenericValue.RawValue = rawValue; return(newValue); }