public void GetObjectMethodTableTest() { using DataTarget dt = TestTargets.AppDomains.LoadFullDump(); using ClrRuntime runtime = dt.ClrVersions.Single().CreateRuntime(); ClrHeap heap = runtime.Heap; int i = 0; foreach (ClrObject obj in heap.EnumerateObjects()) { i++; ClrType type = obj.Type; Assert.NotNull(type); ulong mt = dt.DataReader.ReadPointer(obj); Assert.NotEqual(0ul, mt); if (dt.CacheOptions.CacheTypes) { Assert.Same(type, runtime.GetTypeByMethodTable(mt)); } else { Assert.Equal(type, runtime.GetTypeByMethodTable(mt)); } Assert.Equal(mt, type.MethodTable); } }
public void NoTypeCachingTest() { using DataTarget dt = TestTargets.Types.LoadFullDump(); dt.CacheOptions.CacheTypes = false; dt.CacheOptions.CacheTypeNames = StringCaching.None; using ClrRuntime runtime = dt.ClrVersions.Single().CreateRuntime(); ClrModule module = runtime.GetModule("sharedlibrary.dll"); ClrType type = module.GetTypeByName("Foo"); Assert.NotEqual(0ul, type.MethodTable); // Sanity test ClrType type2 = runtime.GetTypeByMethodTable(type.MethodTable); Assert.Equal(type, type2); Assert.NotSame(type, type2); Assert.NotNull(type.Name); Assert.Equal(type.Name, type.Name); Assert.NotSame(type.Name, type.Name); dt.CacheOptions.CacheTypeNames = StringCaching.Intern; Assert.Same(type.Name, type.Name); Assert.Same(type.Name, string.Intern(type.Name)); }
public void PrimitiveTypeEquality() { // Make sure ClrmdPrimitiveType always equals "real" ClrmdTypes if their ElementTypes are equal. // ClrmdPrimitiveType are fake, mocked up types we create if we aren't able to create the real // ClrType for a field. using DataTarget dt = TestTargets.Types.LoadFullDump(); dt.CacheOptions.CacheTypes = false; using ClrRuntime runtime = dt.ClrVersions.Single().CreateRuntime(); foreach ((ulong mt, int _) in runtime.BaseClassLibrary.EnumerateTypeDefToMethodTableMap()) { ClrType type = runtime.GetTypeByMethodTable(mt); if (type != null && type.IsPrimitive) { // We are hoping that creating a type through a MT will result in a real ClrmdType and // not a ClrmdPrimitiveType. A ClrmdPrimitiveType is there to mock up a type we cannot // find. Assert.IsType <ClrmdType>(type); ClrmdType ct = (ClrmdType)type; ClrmdPrimitiveType prim = new ClrmdPrimitiveType((ITypeHelpers)type.ClrObjectHelpers, runtime.BaseClassLibrary, runtime.Heap, ct.ElementType); Assert.True(ct == prim); Assert.True(prim == ct); } } }
public static IEnumerable <(ClrStaticField Field, ClrObject Object)> EnumerateAllStaticVariables(this ClrRuntime runtime) { if (runtime is null) { throw new ArgumentNullException(nameof(runtime)); } foreach (ClrModule module in runtime.EnumerateModules()) { foreach ((ulong mt, int _) in module.EnumerateTypeDefToMethodTableMap()) { ClrType?type = runtime.GetTypeByMethodTable(mt); if (type is null) { continue; } foreach (ClrStaticField field in type.StaticFields) { if (field.IsObjectReference) { foreach (ClrAppDomain domain in runtime.AppDomains) { ClrObject obj = field.ReadObject(domain); if (obj.IsValid && !obj.IsNull) { yield return(field, obj); } } } } } } }
/// <summary> /// Note: https://github.com/microsoft/clrmd/issues/567#issuecomment-601314348 /// </summary> /// <param name="runtime"></param> /// <param name="predicate"></param> /// <returns></returns> public static IEnumerable <ClrType> GetConstructedTypeDefinitions(this ClrRuntime runtime, Func <ClrType, bool> predicate) { return(runtime.EnumerateModules() .SelectMany(m => m.EnumerateTypeDefToMethodTableMap()) .Select(t => runtime.GetTypeByMethodTable(t.MethodTable)) .Where(predicate)); }
public static IEnumerable <ClrType> EnumerateTypes(this ClrModule module) { ClrRuntime runtime = module.AppDomain.Runtime; foreach ((ulong mt, uint _) in module.EnumerateTypeDefToMethodTableMap()) { ClrType type = runtime.GetTypeByMethodTable(mt); if (type != null) { yield return(type); } } }
public void MethodTableHeapEnumeration() { using DataTarget dt = TestTargets.Types.LoadFullDump(); using ClrRuntime runtime = dt.ClrVersions.Single().CreateRuntime(); ClrHeap heap = runtime.Heap; foreach (ClrType type in heap.EnumerateObjects().Select(obj => heap.GetObjectType(obj.Address)).Unique()) { Assert.NotEqual(0ul, type.MethodTable); ClrType typeFromHeap; if (type.IsArray) { ClrType componentType = type.ComponentType; Assert.NotNull(componentType); typeFromHeap = runtime.GetTypeByMethodTable(type.MethodTable); } else { typeFromHeap = runtime.GetTypeByMethodTable(type.MethodTable); } Assert.Equal(type.MethodTable, typeFromHeap.MethodTable); if (dt.CacheOptions.CacheTypes) { Assert.Same(type, typeFromHeap); } else { Assert.Equal(type, typeFromHeap); } } }
public static ClrType GetTypeByName(this ClrModule module, string typeName) { ClrRuntime runtime = module.AppDomain.Runtime; foreach ((ulong mt, uint _) in module.EnumerateTypeDefToMethodTableMap()) { ClrType type = runtime.GetTypeByMethodTable(mt); if (type.Name == typeName) { return(type); } } return(null); }
public void TestTypeMapRoundTrip() { using DataTarget dt = TestTargets.Types.LoadFullDump(); using ClrRuntime runtime = dt.ClrVersions.Single().CreateRuntime(); int badTypes = 0; foreach (ClrModule module in runtime.EnumerateModules()) { foreach ((ulong mt, int token) in module.EnumerateTypeDefToMethodTableMap()) { Assert.NotEqual(0, token); Assert.True((token & 0x02000000) == 0x02000000); ClrType type = runtime.GetTypeByMethodTable(mt); if (type == null) { // We really want to Assert.NotNull(type), but it turns out that one type // (System.Runtime.Remoting.Proxies.__TransparentProxy) cannot be constructed because // GetMethodTableData returns null for it. This is an issue with the dac so we'll // simply count types that are null and assert there's only one badTypes++; continue; } Assert.NotNull(type); ClrType typeFromToken = module.ResolveToken(token); Assert.NotNull(typeFromToken); Assert.Same(type, typeFromToken); } } Assert.True(badTypes <= 1); }