예제 #1
0
        public static StructFieldsInfo GetStructFields(ClrType type, ClrHeap heap, ulong addr)
        {
            Debug.Assert(type.IsValueClass);
            var flds = type.Fields;
            var cnt  = flds.Count;

            StructFieldsInfo[] structFields = null;
            var types     = new ClrType[cnt];
            var typeKinds = new ClrElementKind[cnt];
            var fields    = new ClrInstanceField[cnt];
            var fldKinds  = new ClrElementKind[cnt];

            for (int i = 0; i < cnt; ++i)
            {
                var fld   = flds[i];
                var ftype = fld.Type;
                fields[i]    = fld;
                fldKinds[i]  = TypeExtractor.GetElementKind(fld.Type);
                types[i]     = fld.Type;
                typeKinds[i] = fldKinds[i];
                if (TypeExtractor.IsAmbiguousKind(fldKinds[i]))
                {
                    ClrType        fType = null;
                    ClrElementKind fKind = ClrElementKind.Unknown;
                    ClrType        rType = null;
                    ClrElementKind rKind = ClrElementKind.Unknown;
                    //object obj = fld.GetValue(addr, false, false);
                    object obj = fld.GetValue(addr, true, false); // TODO JRD
                    if (obj is ulong)
                    {
                        (fType, fKind, rType, rKind) = TypeExtractor.GetRealType(heap, (ulong)obj);
                    }
                    if (fType != null)
                    {
                        types[i]     = fType;
                        typeKinds[i] = fKind;
                        ftype        = fType;
                    }
                }

                if (TypeExtractor.IsUnknownStruct(fldKinds[i]) || TypeExtractor.IsUnknownStruct(typeKinds[i]))
                {
                    if (structFields == null)
                    {
                        structFields = new StructFieldsInfo[cnt];
                    }
                    var faddr = fld.GetAddress(addr, false);
                    structFields[i] = GetStructFields(types[i], heap, faddr);
                }
            }
            return(new StructFieldsInfo(type, types, typeKinds, fields, fldKinds, structFields));
        }
예제 #2
0
        GetClassTypeInfo(ClrHeap heap, ulong addr)
        {
            try
            {
                addr = Utils.RealAddress(addr);
                var type = heap.GetObjectType(addr);
                if (type == null)
                {
                    return("Object Value Error" + Constants.HeavyGreekCrossPadded + "Cannot find an instance." + Constants.HeavyGreekCrossPadded + "Heap cannot get object type at address: " + Utils.RealAddressString(addr), null, ClrElementKind.Unknown, null, null);
                }
                var kind   = TypeExtractor.GetElementKind(type);
                var fldCnt = type.Fields.Count;

                var fldTypes = fldCnt == 0 ? Utils.EmptyArray <ClrType> .Value : new ClrType[fldCnt];
                var fldKinds = fldCnt == 0 ? Utils.EmptyArray <ClrElementKind> .Value : new ClrElementKind[fldCnt];
                var strings  = fldCnt == 0 ? Utils.EmptyArray <string> .Value : new string[fldCnt];
                for (int i = 0; i < fldCnt; ++i)
                {
                    var fld     = type.Fields[i];
                    var fldType = fld.Type; // returns ClrElementKind.Unknown if fld.Type is null
                    fldTypes[i] = fldType;
                    var fldKind = TypeExtractor.GetElementKind(fldType);
                    fldKinds[i] = fldKind;
                    if (fldKind == ClrElementKind.Unknown)
                    {
                        continue;                                    // nothing to do here, from MDR lib: There is
                    }
                    // a bug in several versions of our debugging layer which causes this.
                    if (TypeExtractor.IsAmbiguousKind(fldKind))
                    {
                        (ClrType aType, ClrElementKind aKind) = TypeExtractor.GetReferenceFieldRealTypeAndKind(heap, addr, fld);
                        if (aType != null)
                        {
                            fldType     = aType;
                            fldTypes[i] = fldType;
                            fldKind     = aKind;
                            fldKinds[i] = fldKind;
                        }
                    }
                }
                return(null, type, kind, fldTypes, fldKinds);
            }
            catch (Exception ex)
            {
                return(Utils.GetExceptionErrorString(ex), null, ClrElementKind.Unknown, null, null);
            }
        }
예제 #3
0
        public static StructFieldsEx GetStructFields(StructFields sf, ClrType type, ClrHeap heap, ulong addr)
        {
            Debug.Assert(type.IsValueClass);
            var flds = type.Fields;
            var cnt  = flds.Count;

            StructFieldsEx[] structFields = null;
            var types  = new ClrType[cnt];
            var fields = new ClrInstanceField[cnt];

            for (int i = 0; i < cnt; ++i)
            {
                var     fld   = flds[i];
                var     kind  = TypeExtractor.GetElementKind(fld.Type);
                ClrType fType = null;
                if (TypeExtractor.IsAmbiguousKind(kind))
                {
                    object obj = fld.GetValue(addr, false, false);
                    if (obj is ulong)
                    {
                        fType = heap.GetObjectType((ulong)obj);
                    }
                }

                fields[i] = fld;
                types[i]  = fType ?? fld.Type;

                if (TypeExtractor.IsUnknownStruct(kind))
                {
                    if (structFields == null)
                    {
                        structFields = new StructFieldsEx[cnt];
                    }
                    Debug.Assert(sf.Structs[i] != null);
                    structFields[i] = GetStructFields(sf.Structs[i], fld.Type);
                }
            }
            return(new StructFieldsEx(sf, types, fields, structFields));
        }
예제 #4
0
 public static ValueTuple <bool, ClrType, ClrInstanceField, ClrElementKind, StructFieldsInfo> GetTypeOrFieldForValueExtraction(StructFieldsInfo sfi, string fldName)
 {
     if (sfi == null)
     {
         return(false, null, null, ClrElementKind.Unknown, null);
     }
     for (int i = 0, icnt = sfi._fields.Length; i < icnt; ++i)
     {
         if (Utils.SameStrings(sfi._fields[i].Name, fldName))
         {
             var fsfi = sfi._structFlds == null ? null : sfi._structFlds[i];
             if (TypeExtractor.IsAmbiguousKind(sfi._fldKinds[i]))
             {
                 return(true, sfi._types[i], sfi._fields[i], sfi._typeKinds[i], fsfi);
             }
             else
             {
                 return(false, sfi._types[i], sfi._fields[i], sfi._fldKinds[i], fsfi);
             }
         }
     }
     return(false, null, null, ClrElementKind.Unknown, null);
 }
예제 #5
0
        public static object GetValue(ClrType type, ClrElementKind typeKind, ClrInstanceField field, ClrElementKind fldKind, ClrHeap heap, ulong addr, bool intr)
        {
            Debug.Assert(!TypeExtractor.IsUnknownStruct(typeKind) && !TypeExtractor.IsUnknownStruct(fldKind));
            if (TypeExtractor.IsAmbiguousKind(fldKind))
            {
                if (TypeExtractor.IsString(typeKind))
                {
                    var faddr = ValueExtractor.ReadUlongAtAddress(addr, heap);
                    return(ValueExtractor.GetStringAtAddress(faddr, heap));
                }

                if (TypeExtractor.IsKnownStruct(fldKind))
                {
                    switch (TypeExtractor.GetSpecialKind(fldKind))
                    {
                    case ClrElementKind.Decimal:
                        return(ValueExtractor.GetDecimalValue(addr, type));

                    case ClrElementKind.DateTime:
                        return(ValueExtractor.DateTimeValue(addr, type));

                    case ClrElementKind.TimeSpan:
                        return(ValueExtractor.TimeSpanValue(addr, type));

                    case ClrElementKind.Guid:
                        return(ValueExtractor.GuidValue(addr, type));

                    default:
                        return(Constants.DontKnowHowToGetValue);
                    }
                }

                if (TypeExtractor.IsPrimitive(typeKind))
                {
                    return(ValueExtractor.PrimitiveValueAsString(addr, type, typeKind));
                }
                if (TypeExtractor.IsObjectReference(typeKind))
                {
                    return(field.GetValue(addr, intr, false));
                }
                return(Constants.DontKnowHowToGetValue);
            }

            if (TypeExtractor.IsString(fldKind))
            {
                return(ValueExtractor.GetStringAtAddress(addr, intr, field));
            }

            if (TypeExtractor.IsKnownStruct(fldKind))
            {
                switch (TypeExtractor.GetSpecialKind(fldKind))
                {
                case ClrElementKind.Decimal:
                    return(ValueExtractor.GetDecimalPAF(addr, field, intr));

                case ClrElementKind.DateTime:
                    return(ValueExtractor.GetDateTimePAF(addr, field, intr));

                case ClrElementKind.TimeSpan:
                    return(ValueExtractor.GetTimeSpan(addr, field, intr));

                case ClrElementKind.Guid:
                    return(ValueExtractor.GetGuid(addr, field, intr));

                default:
                    return(Constants.DontKnowHowToGetValue);
                }
            }
            if (TypeExtractor.IsPrimitive(fldKind))
            {
                return(ValueExtractor.GetPrimitiveValueObject(addr, field, intr));
            }
            if (TypeExtractor.IsObjectReference(fldKind))
            {
                return(field.GetValue(addr, intr, false));
            }

            return(Constants.DontKnowHowToGetValue);
        }
예제 #6
0
 public bool IsAmbiguousKind(int i)
 {
     Debug.Assert(i >= 0 && i < _kinds.Length);
     return(TypeExtractor.IsAmbiguousKind(_kinds[i]));
 }
예제 #7
0
        GetClassValueStrings(ClrHeap heap, ulong addr)
        {
            try
            {
                addr = Utils.RealAddress(addr);

                (ClrType type, ClrElementKind kind, ClrType rtype, ClrElementKind rkind) = TypeExtractor.GetRealType(heap, addr);
                if (type == null)
                {
                    return("Object Value Error" + Constants.HeavyGreekCrossPadded + "Cannot find an instance." + Constants.HeavyGreekCrossPadded + "Heap cannot get object type at address: " + Utils.RealAddressString(addr), null, ClrElementKind.Unknown, (null, null, null, null));
                }

                var fldCnt   = type.Fields.Count;
                var fldTypes = fldCnt == 0 ? Utils.EmptyArray <ClrType> .Value : new ClrType[fldCnt];
                var fldKinds = fldCnt == 0 ? Utils.EmptyArray <ClrElementKind> .Value : new ClrElementKind[fldCnt];
                var strings  = fldCnt == 0 ? Utils.EmptyArray <string> .Value : new string[fldCnt];
                StructValueStrings[] structVals = null;
                for (int i = 0; i < fldCnt; ++i)
                {
                    var fld     = type.Fields[i];
                    var fldType = fld.Type; // returns ClrElementKind.Unknown if fld.Type is null
                    fldTypes[i] = fldType;
                    var fldKind = TypeExtractor.GetElementKind(fldType);
                    fldKinds[i] = fldKind;
                    if (fldKind == ClrElementKind.Unknown)
                    {
                        strings[i] = Constants.UnknownValue;
                        continue; // nothing to do here, from MDR lib: There is
                                  // a bug in several versions of our debugging layer which causes this.
                    }
                    if (TypeExtractor.IsAmbiguousKind(fldKind))
                    {
                        (ClrType aType, ClrElementKind aKind) = TypeExtractor.GetReferenceFieldRealTypeAndKind(heap, addr, fld);
                        if (aType != null)
                        {
                            fldType     = aType;
                            fldTypes[i] = fldType;
                            fldKind     = aKind;
                            fldKinds[i] = fldKind;
                        }
                    }
                    if (TypeExtractor.IsString(fldKind))
                    {
                        var o = fld.GetValue(addr, false, true);
                        strings[i] = o == null ? Constants.NullValue : o.ToString();
                    }
                    else if (TypeExtractor.IsObjectReference(fldKind))
                    {
                        var obj = fld.GetValue(addr, false, false);
                        strings[i] = obj == null ? Constants.InvalidAddressStr : Utils.RealAddressString((ulong)obj);
                    }
                    else if (TypeExtractor.IsEnum(fldKind))
                    {
                        strings[i] = ValueExtractor.GetEnumString(addr, fld, false);
                    }
                    else if (fldType.IsPrimitive)
                    {
                        strings[i] = ValueExtractor.PrimitiveValue(addr, fld, false);
                    }
                    else if (TypeExtractor.IsKnownStruct(fldKind))
                    {
                        switch (TypeExtractor.GetSpecialKind(fldKind))
                        {
                        case ClrElementKind.DateTime:
                            strings[i] = ValueExtractor.GetDateTimeString(addr, fld, false);
                            break;

                        case ClrElementKind.Guid:
                            strings[i] = ValueExtractor.GuidValueAsString(addr, fldType, false);
                            break;

                        case ClrElementKind.Decimal:
                            strings[i] = ValueExtractor.DecimalStringPAF(addr, fld, false);
                            break;

                        case ClrElementKind.TimeSpan:
                            strings[i] = ValueExtractor.TimeSpanValueAsString(addr, fldType);
                            break;
                        }
                    }
                    else if (TypeExtractor.IsUnknownStruct(fldKind))
                    {
                        StructFields   sf  = StructFields.GetStructFields(fldType);
                        StructFieldsEx sfx = StructFieldsEx.GetStructFields(sf, fldType);
                        sfx.ResetTypes();
                        if (structVals == null)
                        {
                            structVals = new StructValueStrings[fldCnt];
                        }
                        ulong structAddr = fld.GetAddress(addr, false);
                        structVals[i] = StructFieldsEx.GetStructValueStrings(sfx, heap, structAddr);
                    }
                }
                //if (!Utils.SameStrings(fld.Type.Name,fldType.Name))
                //    {
                //        ulong fldAddr = fld.GetAddress(addr, type.IsValueClass);
                //        if (TypeExtractor.IsString(fldKind))
                //        {
                //            var obj = ValueExtractor.GetStringValue(fldType, fldAddr);
                //            strings[i] = obj == null ? Constants.NullValue : (string)obj;
                //        }
                //        else if (TypeExtractor.IsObjectReference(fldKind))
                //        {
                //            var obj = fld.GetValue(addr, false, false);
                //            strings[i] = obj == null ? Constants.InvalidAddressStr : Utils.RealAddressString((ulong)obj);
                //        }
                //        else if (TypeExtractor.IsEnum(fldKind))
                //        {
                //            long intVal;
                //            strings[i] = ValueExtractor.GetEnumValueString(fldAddr, fldType, out intVal);
                //        }
                //        else if (fldType.IsPrimitive)
                //        {
                //            var obj = fld.Type.GetValue(fldAddr);
                //            strings[i] = ValueExtractor.PrimitiveValue(obj, fldKind);
                //        }
                //        else if (TypeExtractor.IsKnownStruct(fldKind))
                //        {
                //            switch (TypeExtractor.GetSpecialKind(fldKind))
                //            {
                //                case ClrElementKind.DateTime:
                //                    strings[i] = ValueExtractor.DateTimeValueString(fldAddr, fldType, null);
                //                    break;
                //                case ClrElementKind.Guid:
                //                    strings[i] = ValueExtractor.GuidValueAsString(fldAddr, fldType);
                //                    break;
                //                case ClrElementKind.Decimal:
                //                    strings[i] = ValueExtractor.DecimalValueAsString(fldAddr, fldType,null);
                //                    break;
                //                case ClrElementKind.TimeSpan:
                //                    strings[i] = ValueExtractor.TimeSpanValueAsString(fldAddr, fldType);
                //                    break;
                //            }
                //        }
                //        else if (TypeExtractor.IsUnknownStruct(fldKind))
                //        {
                //            StructFields sf = StructFields.GetStructFields(fldType);
                //            StructFieldsEx sfx = StructFieldsEx.GetStructFields(sf, fldType);
                //            sfx.ResetTypes();
                //            if (structVals == null) structVals = new StructValueStrings[fldCnt];
                //            ulong structAddr = fld.GetAddress(addr, false);
                //            structVals[i] = StructFieldsEx.GetStructValueStrings(sfx, heap, structAddr);
                //        }

                //        continue;


                return(null, type, kind, (fldTypes, fldKinds, strings, structVals));
            }
            catch (Exception ex)
            {
                return(Utils.GetExceptionErrorString(ex), null, ClrElementKind.Unknown, (null, null, null, null));
            }
        }
예제 #8
0
        GetClassValues(ClrHeap heap, ulong addr)
        {
            try
            {
                addr = Utils.RealAddress(addr);
                (ClrType type, ClrElementKind kind, ClrType rtType, ClrElementKind rtKind) = TypeExtractor.GetRealType(heap, addr);

                if (type == null)
                {
                    return("Object Value Error" + Constants.HeavyGreekCrossPadded + "Cannot find an instance." + Constants.HeavyGreekCrossPadded + "Heap cannot get object type at address: " + Utils.RealAddressString(addr), null, ClrElementKind.Unknown, (null, null, null, null, null));
                }
                var fldCnt = type.Fields.Count;

                var                fldTypes       = fldCnt == 0 ? Utils.EmptyArray <ClrType> .Value : new ClrType[fldCnt];
                var                fldKinds       = fldCnt == 0 ? Utils.EmptyArray <ClrElementKind> .Value : new ClrElementKind[fldCnt];
                var                objects        = fldCnt == 0 ? Utils.EmptyArray <object> .Value : new object[fldCnt];
                StructValues[]     structVals     = null;
                StructFieldsInfo[] structFldInfos = null;
                for (int i = 0; i < fldCnt; ++i)
                {
                    var fld     = type.Fields[i];
                    var fldType = fld.Type; // returns ClrElementKind.Unknown if fld.Type is null
                    var fldKind = TypeExtractor.GetElementKind(fldType);
                    if (fldKind == ClrElementKind.Unknown)
                    {
                        objects[i] = null;
                        continue; // nothing to do here, from MDR lib: There is
                                  // a bug in several versions of our debugging layer which causes this.
                    }
                    if (fldType == null || TypeExtractor.IsAmbiguousKind(fldKind))
                    {
                        var fldValObj = fld.GetValue(addr, type.IsValueClass, false);
                        if (fldValObj != null && fldValObj is ulong)
                        {
                            var t = heap.GetObjectType((ulong)fldValObj);
                            if (t != null)
                            {
                                fldType = t;
                                fldKind = TypeExtractor.GetElementKind(t);
                            }
                        }
                    }
                    fldTypes[i] = fldType;
                    fldKinds[i] = fldKind;
                    if (fldKind == ClrElementKind.Unknown)
                    {
                        continue;                                    // nothing to do here, from MDR lib: There is
                    }
                    // a bug in several versions of our debugging layer which causes this.
                    if (TypeExtractor.IsString(fldKind))
                    {
                        objects[i] = fld.GetValue(addr, false, true);
                    }
                    else if (TypeExtractor.IsObjectReference(fldKind))
                    {
                        object obj = fld.GetValue(addr, false, false);
                        if (obj != null && (ulong)obj != Constants.InvalidAddress)
                        {
                            var t = heap.GetObjectType((ulong)obj);
                            if (t != null)
                            {
                                var k = TypeExtractor.GetElementKind(t);
                                fldTypes[i] = t;
                                fldKinds[i] = k;
                            }
                        }

                        objects[i] = obj;
                    }
                    else if (TypeExtractor.IsEnum(fldKind))
                    {
                        objects[i] = ValueExtractor.GetEnumValueObject(addr, fld, false);
                    }
                    else if (fldType.IsPrimitive)
                    {
                        objects[i] = ValueExtractor.GetPrimitiveValueObject(addr, fld, false);
                    }
                    else if (TypeExtractor.IsKnownStruct(fldKind))
                    {
                        switch (TypeExtractor.GetSpecialKind(fldKind))
                        {
                        case ClrElementKind.DateTime:
                            objects[i] = ValueExtractor.GetDateTimePAF(addr, fld, false);
                            break;

                        case ClrElementKind.Guid:
                            objects[i] = ValueExtractor.GetGuid(addr, fld, false);
                            break;

                        case ClrElementKind.Decimal:
                            objects[i] = ValueExtractor.GetDecimalPAF(addr, fld, false);
                            break;

                        case ClrElementKind.TimeSpan:
                            objects[i] = ValueExtractor.TimeSpanValue(addr, fldType);
                            break;
                        }
                    }
                    else if (TypeExtractor.IsUnknownStruct(fldKind))
                    {
                        //StructFields sf = StructFields.GetStructFields(fldType);
                        //StructFieldsEx sfx = StructFieldsEx.GetStructFields(sf, fldType);
                        if (structFldInfos == null)
                        {
                            structFldInfos = new StructFieldsInfo[fldCnt];
                        }
                        ulong            saddr = fld.GetAddress(addr, true);
                        StructFieldsInfo sfi   = StructFieldsInfo.GetStructFields(fldType, heap, saddr);
                        structFldInfos[i] = sfi;
                        if (sfi != null)
                        {
                            if (structVals == null)
                            {
                                structVals = new StructValues[fldCnt];
                            }
                            structVals[i] = StructFieldsInfo.GetStructValues(sfi, heap, saddr);
                        }
                    }
                }

                return(null, type, kind, (fldTypes, fldKinds, objects, structFldInfos, structVals));
            }
            catch (Exception ex)
            {
                return(Utils.GetExceptionErrorString(ex), null, ClrElementKind.Unknown, (null, null, null, null, null));
            }
        }