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 
    r @$t7 = 10

 .for (r $t0=0; @$t0 &lt; 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
         $$ 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 )
     .printf &quot;%N \n&quot; ,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

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:0x1 (System.Int32)
Name:029a917c (System.String: "test")
029a9210 (Test.Foo)
counter:0x2 (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.

5 thoughts on “Dumping Generic List in .NET within Windbg

    1. 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.

      1. 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. :)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s