Dumping Generic List in .NET within Windbg
Most of the code uses List<T> for storing items. The present solutions don’ t have a way to dump List<T> within windbg. Even though sosex has an option to dump the List<T> using !mdt it still doesn’t meet the scripting requirements. For example here is an output using sosex “!mdt -e 029a91c0″
0:000> !mdt -e 029a91c0
029a91c0 (System.Collections.Generic.List`1[[Test.Foo, Test]])
Count = 2
[0] 029a9200 (Test.Foo)
[1] 029a9210 (Test.Foo)
I would have preferred to get the contents of the “Foo” object instead of just the address of Foo. So wrote a script to do that.
$$ pointer to the array within the List
r @$t5 = poi(${$arg1}+@$ptrsize)
.if (@$ptrsize = 8 )
{
r @t7 = 20
}
.else
{
r @$t7 = 10
}
.for (r $t0=0; @$t0 < poi(@$t5+@$ptrsize); r$t0=@$t0+1 )
{
.if(@$t0 = 0)
{
$$ First occurence of the element in the array would be in the 20 offset for x64 and 10 offset for x86
r$t1=@$t7
}
.else
{
$$ the rest of the elements would be in the 8th offset for x64 and 4th offset for x86
r$t1= @$t7+(@$t0*@$ptrsize)
}
$$ Check for null before trying to dump
.if (poi((@$t5-@$ptrsize)+@$t1) = 0 )
{
.continue
}
.else
{
.printf "%N \n" ,poi((@$t5-@$ptrsize)+@$t1)
}
}
This script should work in x86 and x64. To use the above script copy to a file and invoke it like this passing the address of List<T>
$$>a<"d:\Debuggersx86\dumplist.txt" 029a91c0
Here is the output from the above command.
0:000> $$>a<"d:\Debuggersx86\dumplist.txt" 029a91c0
029A9200
029A9210
Now with this script I can use !mdt to get the contents of the “Foo” object.
.foreach ($obj {$$>a<"d:\Debuggersx86\dumplist.txt" 029a91c0}) {!mdt $obj}
0:000> .foreach ($obj {$$>a<"d:\Debuggersx86\dumplist.txt" 029a91c0}) {!mdt $obj}
029a9200 (Test.Foo)
counter:0×1 (System.Int32)
Name:029a917c (System.String: "test")
029a9210 (Test.Foo)
counter:0×2 (System.Int32)
Name:029a9198 (System.String: "test2")
This is one of the scripts that I would use often. Hope it is useful to others also.
Naveen, you can add an expansion level with -e.
!mdt -e:3 will do what you want.
Steve Johnson
December 9, 2010 at 10:22 pm
Steve,
Thanks .I don’t know what happened to my previous reply. My mistake should have read the documentation.
Anyhow I can use this script to automate other things, which wasn’t possible.
Naveen
December 10, 2010 at 8:11 am
Indeed. The script is cool regardless.
Steve Johnson
December 10, 2010 at 5:52 pm
Thanks Steve. BTW are you working on the undocumented sosex features?
Naveen
December 10, 2010 at 6:00 pm
Yes, !mu/!muf are mostly finished. !mt/!mgu will take a bit more work. I’ve been taking a break from sosex for a few months. Hopefully, I can have a new version out early next year. Send me an email if you’d like a test build now and then. I could use the help. :)
Steve Johnson
December 10, 2010 at 8:38 pm