I have a memory dump that I’m using to troubleshoot a client issue. This is a .NET (C#) application. The problem with my application is that too many instances of a particular class are being created. There are 6300 instances of this class when there should be something like 20. I want to loop through all of those instances and call the name field of each of those instances. Is there any easy way to do this in WinDbg/SOS?
I know I can use !dumpheap -type {typename} to find all the instances of that class, but I’m not sure how I can expand them all and view the field I am interested in.
You can do this with
.foreachcommand within Windbg.Here is a simple example
The
!dumpheaphas a short option which would just return the object address. In the instance i am debugging MT forProgramis 00293858Here is a code to dump all the objects
.foreach ($obj {!dumpheap -mt 00293858 -short}) {!do $obj}using the foreach construct. The $obj would get assigned with the address of the object. And here is the sample output from the foreach loopNow that we have this , the next step is to get the field “test” within each instance of program and here is the code to do that
I am using
poicommand within the foreach loop. From the above result we can make out thetestvariable is in the 4 offset and that’s the reason for usingpoi(${$obj}+0x4)And here is the sample output from the above foreach
And here is for getting each
Fooinstance within theProgramclassThe Foo is in the 8th offset and here is sample the output for the above foreach
EDIT:- Also here is a post from Tess on dumping session contents
HTH