Search code examples
c#.netwindbgsos

Find a .NET object nearest to the given address on the heap (.NET2.0/3.5)


I would like to locate a .NET object nearest to a specific address on the heap.

Example situation

Imagine that from some source I got an address 0x00000000ffaaa690. When I try to dump an object (using SOS !do command) I receive:

0:000> !do 00000000ffaaa690
<Note: this object has an invalid CLASS field> Invalid object

Apparently this address does not point to a valid MT. But I know it's on the .NET heap. How can I locate the nearest MT address (the beginning of the object instance address)?

In the example case the searched object would be at address 00000000ffaaa680:

0:000> !do 00000000ffaaa680 Name: System.String MethodTable: 000007fee6a47d90 EEClass: 000007fee664e560 Size: 36(0x24) bytes (C:\Windows\assembly\GAC_64\mscorlib\2.0.0.0__b77a5c561934e089\mscorlib.dll) String: 16457 Fields: MT Field Offset Type VT Attr Value Name 000007fee6a4f000 4000096 8 System.Int32 1 instance 6 m_arrayLength 000007fee6a4f000 4000097 c System.Int32 1 instance 5 m_stringLength 000007fee6a497d8 4000098 10 System.Char 1 instance 31 m_firstChar 000007fee6a47d90 4000099 20 System.String 0 shared static Empty 000007fee6a49688 400009a 28 System.Char[] 0 shared static WhitespaceChars

Update 1:

As Oguz pointed in the comment, there is the lno command in SOS, but I see that it was probably introduced in .NET4.0. My dump is from .NET2.0/3.5 and it seems that this command is missing :(


Solution

  • Try !sosex.mln. It will show you the type of object the address lies in. It can also tell you if the address is in a managed method or other type of managed data structure.