Skip to content

How to write DumpHandles of SOS using ClrMD

After I noticed ClrMD preparing for an updated release ie ClrMD v2, I got excited to dig into the API to see what all we can do to help debugging .NET applications. Lets explore the API!

So here is the code to dump the Object handles grouped into different types. Also a summary of all handles at the end. Don’t forget to reference Microsoft.Diagnostics.Runtime (Nuget package) for this console App.

Code for sos.dumphandle from SOS

            using DataTarget dataTarget = DataTarget.LoadDump(args[0]);

            ClrRuntime runtime = dataTarget.ClrVersions[0].CreateRuntime();

            Console.WriteLine("\n>>> Handles");
            StringBuilder str = new StringBuilder();
            str.Append("Handles Summary:");
            int typeCount = 0;
            var groupedHandles = runtime.EnumerateHandles().GroupBy(k => k.HandleKind);
            foreach (var h in groupedHandles)
            {
                Console.WriteLine($"[Handle Kind: {h.Key}]");
                Console.WriteLine("\t{0,16} {1}", "Address", "Name");
                foreach (var handle in h.ToList<ClrHandle>())
                {
                    Console.WriteLine($"\t{handle.Address:x16} {handle.Object.Type.Name}");
                    typeCount++;
                }

                str.Append($"\n{h.Key,16} handles: {typeCount}");   // Summary
                typeCount = 0;
                Console.WriteLine();
            }

            Console.WriteLine(str.ToString());
            Console.WriteLine("Total {0} objects", runtime.EnumerateHandles().Count());

How the output of the above code looks like

>>>> Handles
[Handle Kind: WeakShort]
	         Address Name
	000001d143dd1140 System.Threading.Thread
	000001d143dd1180 System.Collections.Concurrent.CDSCollectionETWBCLProvider
	000001d143dd1190 System.Threading.Thread
	000001d143dd1198 System.Diagnostics.Tracing.FrameworkEventSource
	000001d143dd11a0 Microsoft.AspNetCore.Hosting.HostingEventSource
	000001d143de17d0 System.Buffers.ArrayPoolEventSource
	000001d143de17d8 System.Threading.Tasks.TplEventSource
	000001d143de17e0 System.Threading.Thread
	000001d143de17e8 System.Xml.Linq.XNamespace
	000001d143de17f0 System.Diagnostics.Tracing.RuntimeEventSource
	000001d143de17f8 System.Diagnostics.Tracing.NativeRuntimeEventSource
	....
[Handle Kind: WeakLong]
	         Address Name
	000001d143dd1600 System.RuntimeType+RuntimeTypeCache
	000001d143dd1778 System.Reflection.Emit.DynamicResolver
	000001d143de1480 System.Runtime.Loader.IndividualAssemblyLoadContext
	000001d143df1130 System.RuntimeType+RuntimeTypeCache
	000001d143df1138 System.RuntimeType+RuntimeTypeCache
	000001d143e011b8 Microsoft.AspNetCore.Server.IIS.NativeMethods+PFN_ASYNC_COMPLETION
	000001d143e01df0 System.RuntimeType+RuntimeTypeCache
	000001d143e01df8 System.RuntimeType+RuntimeTypeCache
	...
[Handle Kind: Strong]
	         Address Name
	000001d143dd1250 System.Object[]
	000001d143dd1260 System.Runtime.CompilerServices.GCHeapHash
	000001d143de1170 System.Object[]
	000001d143de1178 System.Threading.Thread
	000001d143e013e8 System.Runtime.CompilerServices.GCHeapHash
	000001d143e013f0 System.Object[]
	000001d143e013f8 Microsoft.AspNetCore.Server.IIS.Core.IISHttpServer
	...
[Handle Kind: Pinned]
	         Address Name
	000001d143dd15f0 System.Object[]
	000001d143dd15f8 System.Object[]
	000001d143de13f8 System.Object
	000001d143df19f8 System.Object[]
	000001d143e015f0 System.Byte[]
	000001d143e015f8 System.Object[]
	...
[Handle Kind: Dependent]
	         Address Name
	000001d143dd1ff8 Microsoft.AspNetCore.Mvc.ViewFeatures.Buffers.ViewBufferValue[][]
	000001d143de1fd8 System.Byte[][]
	000001d143de1ff0 System.Byte[][]
	000001d143de1ff8 System.Threading.Tasks.ThreadPoolTaskScheduler
	000001d143df23f8 System.Byte[][]
	000001d143e019f0 Microsoft.AspNetCore.Mvc.ViewFeatures.Buffers.ViewBufferValue[][]
	000001d143e019f8 System.Char[][]
	...
[Handle Kind: RefCounted]
	         Address Name
	000001d143de1bf8 System.EventHandler&lt;Windows.Foundation.Diagnostics.TracingStatusChangedEventArgs>
	...
[Handle Kind: AsyncPinned]
	         Address Name
	000001d143de19f8 System.Threading.OverlappedData
	000001d143df1ff0 System.Threading.OverlappedData
	000001d143df1ff8 System.Threading.OverlappedData
	...
Handles Summary:
       WeakShort handles: 35
        WeakLong handles: 763
          Strong handles: 99
          Pinned handles: 13
       Dependent handles: 9
      RefCounted handles: 1
     AsyncPinned handles: 3
Total 923 objects

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 )

Facebook photo

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

Connecting to %s

%d bloggers like this: