/// <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)); }
/// <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))); }
/// <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)); }
internal virtual IEnumerable <ClrRoot> EnumerateStackReferences(ClrThread thread, bool includeDead) { Address stackBase = thread.StackBase; Address stackLimit = thread.StackLimit; if (stackLimit <= stackBase) { Address tmp = stackLimit; stackLimit = stackBase; stackBase = tmp; } ClrAppDomain domain = GetAppDomainByAddress(thread.AppDomain); ClrHeap heap = GetHeap(); var mask = ((ulong)(PointerSize - 1)); var cache = MemoryReader; cache.EnsureRangeInCache(stackBase); for (Address stackPtr = stackBase; stackPtr < stackLimit; stackPtr += (uint)PointerSize) { Address objRef; if (cache.ReadPtr(stackPtr, out objRef)) { // If the value isn't pointer aligned, it cannot be a managed pointer. if (heap.IsInHeap(objRef)) { ulong mt; if (heap.ReadPointer(objRef, out mt)) { ClrType type = null; if (mt > 1024) { type = heap.GetObjectType(objRef); } if (type != null && !type.IsFree) { yield return(new LocalVarRoot(stackPtr, objRef, type, domain, thread, false, true, false, null)); } } } } } }