How to work with non-primitive ClrInstanceField values ​​using ClrMD?

I have very large memory dumps of a managed process from which I am trying to get a lot of statistics, as well as the ability to present an interactive view - rather deep graphs of objects on the heap. Think about what is comparable to !do <address>the prefer_dml 1one installed in WinDbg with SOS, where you can constantly click on properties and see their values, only in a much more friendly user interface for comparing many objects.

I found Microsoft.Diagnostics.Runtime (ClrMD) particularly well suited for this task, but I find it difficult to work with array fields, and I'm a little confused about the object fields that I have working a little better.


Array : If I aim the array at the address directly from the heap and use ClrType.GetArrayLengthand ClrType.GetArrayElementValue, everything works fine, but as soon as I dig the fields on another object, I’m not sure what value I get from ClrInstanceField.GetValuewhen it ClrInstanceField.ElementTypeis equal ClrElementType.SZArray(I haven’t seen Arraythat while digging into my graph of objects, but I would also like to process it).

Edit: I just decided to use ClrTypefor System.UInt64dereferencing an array field (using parent address + offset of the array fieldto calculate the address where the array pointer is stored), then I can work with it the same way as if I got it from EnumerateObjects. I am having some difficulties with some arrays that do not support the property ArrayComponentType. I have not tested Structs arrays yet, so I also wonder if this will be the distribution of C strings in string structures, as it is with int[], or if it will be an array of pointers to structures on the heap. Guid[]is one of the types that I am having trouble getting ArrayComponentType.

: ( ) ClrInstanceField, Type of ClrElementType.Object, , . -, GetFieldValue ulong (?), ClrInstanceField.Type.Fields , . , , ClrHeap.GetObjectType , NULL, - . , , .

: ( ) DbgEng w/SOS, , , ClrInstanceField.GetFieldValue , ( ). , ?


: , LINQPad . , , , . //, , , CodePlex GitHub , .

, , . , ClrMD . SOS-, !dumpheap -stat ( ), ClrHeap.GetTypeByName ClrHeap.GetObjectType. ClrType.Fields ClrInstanceField Type, ElementType GetFieldValue

XML-, NuGet, IntelliSense .

+4
1

, , , :

, , GetFieldAddress/GetFieldValue, - , , , . , (, String vs. Struct ).

GetFieldAddress/GetFieldValue, , , ( , , ).

, .

field.IsPrimitive() : GetFieldValue() (.. Int32, Byte - )

.IsValueClass() , GetFieldAddress() . , GetFieldAddress/Value(), , , !

field.ElementType ClrElementType.String, , , , GetFieldValue, ( , ).

, GetFieldValue() .

?

+3

Source: https://habr.com/ru/post/1529819/


All Articles