Esempio n. 1
0
        /// <summary>
        /// </summary>
        /// <param name="fieldName"></param>
        /// <returns></returns>
        public ClrValueClass GetValueClassField(string fieldName)
        {
            ClrInstanceField field = _type.GetFieldByName(fieldName);

            if (field == null)
            {
                throw new ArgumentException(String.Format("Type '{0}' does not contain a field named '{1}'", _type.Name, fieldName));
            }

            if (!field.IsValueClass)
            {
                throw new ArgumentException(String.Format("Field '{0}.{1}' is not a ValueClass.", _type.Name, fieldName));
            }

            if (field.Type == null)
            {
                throw new Exception("Field does not have an associated class.");
            }

            ClrHeap heap = _type.Heap;

            ulong addr = field.GetAddress(_address, _interior);

            return(new ClrValueClass(addr, field.Type, true));
        }
Esempio n. 2
0
        /// <summary>
        /// Gets an object reference field from ClrObject.  Any field which is a subclass of System.Object
        /// </summary>
        /// <param name="fieldName">The name of the field to retrieve.</param>
        /// <returns></returns>
        public ClrObject GetObject(string fieldName)
        {
            if (IsNull)
            {
                throw new NullReferenceException();
            }

            ClrType          type  = Type;
            ClrInstanceField field = type.GetFieldByName(fieldName);

            if (field == null)
            {
                throw new ArgumentException($"Type '{type.Name}' does not contain a field named '{fieldName}'");
            }

            if (!field.IsObjectReference)
            {
                throw new ArgumentException($"Field '{type.Name}.{fieldName}' is not an object reference.");
            }

            ClrHeap heap = Type.Heap;

            ulong addr = ClrRuntime.IsObjectReference(ElementType) ? Object : Address;

            addr = field.GetAddress(addr, Interior);
            ulong obj;

            if (!heap.ReadPointer(addr, out obj))
            {
                throw new MemoryReadException(addr);
            }

            return(new ClrObject(obj, heap.GetObjectType(obj)));
        }
Esempio n. 3
0
        /// <summary>
        /// Gets the given object reference field from this ClrObject.  Throws ArgumentException if the given field does
        /// not exist in the object.  Throws NullReferenceException if IsNull is true.
        /// </summary>
        /// <param name="fieldName">The name of the field to retrieve.</param>
        /// <returns>A ClrObject of the given field.</returns>
        public ClrObject GetObjectField(string fieldName)
        {
            if (IsNull)
            {
                throw new NullReferenceException();
            }

            ClrInstanceField field = Type.GetFieldByName(fieldName);

            if (field == null)
            {
                throw new ArgumentException($"Type '{Type.Name}' does not contain a field named '{fieldName}'");
            }

            if (!field.IsObjectReference)
            {
                throw new ArgumentException($"Field '{Type.Name}.{fieldName}' is not an object reference.");
            }

            ClrHeap heap = Type.Heap;

            ulong addr = field.GetAddress(Address);

            if (!heap.ReadPointer(addr, out ulong obj))
            {
                throw new MemoryReadException(addr);
            }

            ClrType type = heap.GetObjectType(obj);

            return(new ClrObject(obj, type));
        }
Esempio n. 4
0
        /// <summary>
        /// </summary>
        /// <param name="fieldName"></param>
        /// <returns></returns>
        public ClrValueClass GetValueClassField(string fieldName)
        {
            if (IsNull)
            {
                throw new NullReferenceException();
            }

            ClrInstanceField field = Type.GetFieldByName(fieldName);

            if (field == null)
            {
                throw new ArgumentException($"Type '{Type.Name}' does not contain a field named '{fieldName}'");
            }

            if (!field.IsValueClass)
            {
                throw new ArgumentException($"Field '{Type.Name}.{fieldName}' is not a ValueClass.");
            }

            if (field.Type == null)
            {
                throw new Exception("Field does not have an associated class.");
            }

            ClrHeap heap = Type.Heap;

            ulong addr = field.GetAddress(Address);

            return(new ClrValueClass(addr, field.Type, true));
        }
Esempio n. 5
0
        /// <summary>
        /// Gets an unsigned pointer field from the object.  Note that the type must match exactly, as this method
        /// will not do type coercion.  This method will throw an ArgumentException if no field matches
        /// the given name.  It will throw a NullReferenceException if the target object is null (that is,
        /// if (IsNull returns true).  It will throw an InvalidOperationException if the field is not
        /// of the correct type.  Lastly, it will throw a MemoryReadException if there was an error reading
        /// the value of this field out of the data target.
        /// </summary>
        /// <param name="fieldName">The name of the field to get the value for.</param>
        /// <returns>The value of the given field.</returns>
        public UIntPtr GetUIntPtr(string fieldName)
        {
            ClrType          type  = Type;
            ClrInstanceField field = type.GetFieldByName(fieldName);

            if (field == null)
            {
                throw new ArgumentException($"Type '{type.Name}' does not contain a field named '{fieldName}'");
            }

            if (field.ElementType != ClrElementType.NativeUInt && field.ElementType != ClrElementType.Pointer && field.ElementType != ClrElementType.FunctionPointer)
            {
                throw new InvalidOperationException($"Field '{type.Name}.{fieldName}' is not a pointer.");
            }

            if (IsNull)
            {
                throw new NullReferenceException();
            }

            ulong address = field.GetAddress(Address);
            ulong value;

            if (!((RuntimeBase)type.Heap.Runtime).ReadPointer(address, out value))
            {
                throw new MemoryReadException(address);
            }

            return(new UIntPtr((ulong)value));
        }
Esempio n. 6
0
        private ulong GetFieldAddress(string fieldName, ClrElementType element, string typeName, out ClrType type)
        {
            if (IsNull)
            {
                throw new NullReferenceException();
            }

            type = Type;
            ClrInstanceField field = type.GetFieldByName(fieldName);

            if (field == null)
            {
                throw new ArgumentException($"Type '{type.Name}' does not contain a field named '{fieldName}'");
            }

            if (field.ElementType != element)
            {
                throw new InvalidOperationException($"Field '{type.Name}.{fieldName}' is not of type '{typeName}'.");
            }

            ulong address = ClrRuntime.IsObjectReference(ElementType) ? Object : Address;

            address = field.GetAddress(address, Interior);
            return(address);
        }
Esempio n. 7
0
        /// <summary>
        /// Gets the given object reference field from this ClrObject.  Throws ArgumentException if the given field does
        /// not exist in the object.  Throws NullReferenceException if IsNull is true.
        /// </summary>
        /// <param name="fieldName">The name of the field to retrieve.</param>
        /// <returns>A ClrObject of the given field.</returns>
        public ClrObject GetObjectField(string fieldName)
        {
            ClrInstanceField field = _type.GetFieldByName(fieldName);

            if (field == null)
            {
                throw new ArgumentException(String.Format("Type '{0}' does not contain a field named '{1}'", _type.Name, fieldName));
            }

            if (!field.IsObjectReference)
            {
                throw new ArgumentException(String.Format("Field '{0}.{1}' is not an object reference.", _type.Name, fieldName));
            }

            ClrHeap heap = _type.Heap;

            ulong addr = field.GetAddress(_address, _interior);
            ulong obj;

            if (!heap.ReadPointer(addr, out obj))
            {
                throw new MemoryReadException(addr);
            }

            ClrType type = heap.GetObjectType(obj);

            return(new ClrObject(obj, type));
        }
        /// <summary>
        /// Gets the field value from <see cref="IAddressableTypedEntity"/> with respect to field nature (either reference, or value type).
        /// </summary>
        /// <param name="entity">The entity to read field value from.</param>
        /// <param name="fieldName">The name of the field to get value.</param>
        /// <exception cref="ArgumentNullException">if entity has no type.</exception>
        /// <exception cref="ArgumentException">Thrown when field with matching name was not found.</exception>
        /// <returns></returns>
        public static IAddressableTypedEntity GetFieldFrom(this IAddressableTypedEntity entity, string fieldName)
        {
            ClrType entityType = entity?.Type ?? throw new ArgumentNullException(nameof(entity), "No associated type");

            ClrInstanceField field = entityType.GetFieldByName(fieldName) ?? throw new ArgumentException($"Type '{entityType}' does not contain a field named '{fieldName}'");

            return(field.IsObjectReference ? (IAddressableTypedEntity)entity.GetObjectField(fieldName) : entity.GetValueClassField(fieldName));
        }
Esempio n. 9
0
        /// <summary>
        ///     Converts the specified instance field.
        /// </summary>
        /// <param name="instanceField">The instance field.</param>
        /// <returns>IClrInstanceField.</returns>
        public IClrInstanceField Convert(ClrMd.ClrInstanceField instanceField)
        {
            if (instanceField == null)
            {
                return(null);
            }
            var item = new ClrInstanceFieldAdapter(this, instanceField);

            return(Cache.GetOrAdd <IClrInstanceField>(instanceField, () => item, () => item.Setup()));
        }
		public ClrInstanceFieldDecorator(ClrInstanceField clrInstanceField, ThreadDispatcher threadDispatcher, IClrHeapDecorator heap)
		{
			_clrInstanceField = clrInstanceField;
			_threadDispatcher = threadDispatcher;

			_threadDispatcher.Process(() =>
			{
				Name = _clrInstanceField.Name;
				HasSimpleValue = _clrInstanceField.HasSimpleValue;
				Type = new ClrTypeDecorator(heap, _threadDispatcher, _clrInstanceField.Type);
				IsObjectAndNotString = _clrInstanceField.IsObjectReference() &&_clrInstanceField.ElementType != ClrElementType.String;
			});
		}
Esempio n. 11
0
        /// <summary>
        /// Gets the value of a primitive field.  This will throw an InvalidCastException if the type parameter
        /// does not match the field's type.
        /// </summary>
        /// <typeparam name="T">The type of the field itself.</typeparam>
        /// <param name="fieldName">The name of the field.</param>
        /// <returns>The value of this field.</returns>
        public T GetField <T>(string fieldName) where T : struct
        {
            ClrInstanceField field = _type.GetFieldByName(fieldName);

            if (field == null)
            {
                throw new ArgumentException(String.Format("Type '{0}' does not contain a field named '{1}'", _type.Name, fieldName));
            }

            object value = field.GetValue(_address, _interior);

            return((T)value);
        }
Esempio n. 12
0
        /// <summary>
        /// Gets the value of a primitive field.  This will throw an InvalidCastException if the type parameter
        /// does not match the field's type.
        /// </summary>
        /// <typeparam name="T">The type of the field itself.</typeparam>
        /// <param name="fieldName">The name of the field.</param>
        /// <returns>The value of this field.</returns>
        public T GetField <T>(string fieldName)
            where T : struct
        {
            ClrInstanceField field = Type.GetFieldByName(fieldName);

            if (field == null)
            {
                throw new ArgumentException($"Type '{Type.Name}' does not contain a field named '{fieldName}'");
            }

            object value = field.GetValue(Address);

            return((T)value);
        }
Esempio n. 13
0
        private ulong GetFieldAddress(string fieldName, ClrElementType element, string typeName)
        {
            ClrInstanceField field = Type.GetFieldByName(fieldName);

            if (field == null)
            {
                throw new ArgumentException($"Type '{Type.Name}' does not contain a field named '{fieldName}'");
            }

            if (field.ElementType != element)
            {
                throw new InvalidOperationException($"Field '{Type.Name}.{fieldName}' is not of type '{typeName}'.");
            }

            ulong address = field.GetAddress(Address, _interior);

            return(address);
        }
Esempio n. 14
0
        public ClrValueImpl(ClrRuntime runtime, ulong address, ClrInstanceField field)
            : base(runtime)
        {
            _address  = address;
            _type     = field.Type;
            _interior = field.IsValueClass;

            if (_type.IsObjectReference)
            {
                var heap = _type.Heap;
                if (!heap.ReadPointer(address, out _obj))
                {
                    throw new MemoryReadException(address);
                }

                _type = heap.GetObjectType(_obj);
            }
        }
Esempio n. 15
0
        private ulong GetFieldAddress(string fieldName, ClrElementType element, string typeName)
        {
            ClrInstanceField field = _type.GetFieldByName(fieldName);

            if (field == null)
            {
                throw new ArgumentException(String.Format("Type '{0}' does not contain a field named '{1}'", _type.Name, fieldName));
            }

            if (field.ElementType != element)
            {
                throw new InvalidOperationException(String.Format("Field '{0}.{1}' is not of type '{2}'.", _type.Name, fieldName, typeName));
            }

            ulong address = field.GetAddress(_address, _interior);

            return(address);
        }
Esempio n. 16
0
        /// <summary>
        /// Retrieves a field from this value.
        /// </summary>
        /// <param name="name">The name of the field.</param>
        /// <returns>A ClrValue representing this field.</returns>
        public virtual ClrValue GetField(string name)
        {
            ClrElementType el = ElementType;

            if (ClrRuntime.IsPrimitive(el))
            {
                // Primitives only have one field, named m_value.
                if (name != "m_value")
                {
                    throw new ArgumentException(string.Format("Field '{0}' does not exist in type '{1}'.", name, Type.Name));
                }

                // Getting m_value is the same as this ClrValue...
                return(this);
            }

            if (ClrRuntime.IsObjectReference(el) || !Interior)
            {
                return(AsObject().GetField(name));
            }

            Debug.Assert(ClrRuntime.IsValueClass(el));

            ulong address = Address;

            if (address == 0)
            {
                throw new NullReferenceException();
            }

            ClrType          type  = Type;
            ClrInstanceField field = type.GetFieldByName(name);

            if (field == null)
            {
                throw new ArgumentException(string.Format("Field '{0}' does not exist in type '{1}'.", name, Type.Name));
            }

            ulong result = field.GetAddress(address, Interior);

            return(new ClrValueImpl(_runtime, result, field));
        }
Esempio n. 17
0
        /// <summary>
        /// Gets the given field in this object
        /// </summary>
        /// <param name="fieldName">The name of the field.</param>
        /// <returns>The value of the field.</returns>
        public ClrValue GetFieldOrNull(string fieldName)
        {
            if (IsNull)
            {
                return(new ClrValueImpl(Type.Heap));
            }

            ClrInstanceField field = _type.GetFieldByName(fieldName);

            if (field == null)
            {
                throw new ArgumentException($"Type '{_type.Name}' does not contain a field named '{fieldName}'");
            }

            ulong addr      = Address;
            ulong fieldAddr = field.GetAddress(addr);

            if (fieldAddr == 0)
            {
                throw new MemoryReadException(addr);
            }

            return(new ClrValueImpl(_type.Heap.Runtime, fieldAddr, field));
        }
Esempio n. 18
0
 public override bool GetFieldForOffset(int fieldOffset, bool inner, out ClrInstanceField childField, out int childFieldOffset)
 {
     childField = null;
     childFieldOffset = 0;
     return false;
 }
Esempio n. 19
0
 public ClrObject this[ClrInstanceField field] => GetInnerObject(field.GetAddress(Address, IsInterior), field.Type);
Esempio n. 20
0
 /// <summary>
 /// When you enumerate a object, the offset within the object is returned.  This offset might represent
 /// nested fields (obj.Field1.Field2).    GetFieldOffset returns the first of these field (Field1),
 /// and 'remaining' offset with the type of Field1 (which must be a struct type).   Calling
 /// GetFieldForOffset repeatedly until the childFieldOffset is 0 will retrieve the whole chain.
 /// </summary>
 /// <returns>true if successful.  Will fail if it 'this' is an array type</returns>
 abstract public bool GetFieldForOffset(int fieldOffset, bool inner, out ClrInstanceField childField, out int childFieldOffset);
Esempio n. 21
0
 public FieldInformation(ClrDumpType dumpType, ClrInstanceField clrField) 
 {
     this.dumpType = dumpType;
     this.clrField = clrField;
 }
Esempio n. 22
0
 /// <summary>
 /// When you enumerate a object, the offset within the object is returned.  This offset might represent
 /// nested fields (obj.Field1.Field2).    GetFieldOffset returns the first of these field (Field1), 
 /// and 'remaining' offset with the type of Field1 (which must be a struct type).   Calling 
 /// GetFieldForOffset repeatedly until the childFieldOffset is 0 will retrieve the whole chain.  
 /// </summary>
 /// <returns>true if successful.  Will fail if it 'this' is an array type</returns>
 abstract public bool GetFieldForOffset(int fieldOffset, bool inner, out ClrInstanceField childField, out int childFieldOffset);
Esempio n. 23
0
        public static long CountTargets(ulong address, ClrType clrType, ClrInstanceField targetField, ClrInstanceField invocCountField)
        {
            ClrObject clrObject = new ClrObject(address, clrType);
            ClrObject target = clrObject[targetField];
            if (target.Address != address)
            {
                return 1;
            }

            var invocCount = clrObject[invocCountField];
            var value = invocCount.SimpleValue;
            return (long)value;
        }
Esempio n. 24
0
 public MDField(ClrInstanceField field)
 {
     m_field = field;
 }