Application debugging and analysis can be a daunting task, even more when source code neither symbols are not available. Visual Studio provides developers with powerful debugging capabilities, but the problem many times faced by developers is that Visual Studio is not installed on the target computer which is fair enough if it is a production environment.
There are a few tools available, being WinDbg the most powerful and one of my favorite ones. WinDbg allows developers to debug native (in kernel and user mode) and managed code through SOS (Debugging Extension). The options available are more powerful than the ones provided by Visual Studio debugger, however it might not be very user friendly, for the following reasons:
- Load SOS via CLR or mscorwks (depending on the version of the framework)
- Type in commands in WinDbg to perform our analysis. These commands are powerful if the developer knows them besides having a good understanding of how the CLR works, the only thing is that many of these commands’ names are very user friendly, as shown below
There is a great article on MSDN on this subject – Debugging Managed Code using the Windows Debugger.
So, WinDbg is very powerful but some developers might not find it user friendly, what options do we have then? Well, good news is that Microsoft has produced and released a library to diagnose and analyze CLR applications, it is called CLrMD (CLR Memory Diagnostics). It is currently in beta and available to download from nuget.
Image 1 – Install nuget package
Therefore, I have just built an utility to showcase some of the features in the library. The utility is a WPF C# application which implements the ClrMD library as well as the MVVM pattern. The whole idea is to make developers life easier, by providing an easy to use UI and encapsulate some of the commands in SOS as operations that can be selected on the user interface.
Image 2 – Options available in the utility
The utility as of now, provides 3 operations only which are:
- Dump Heap
- Heap Stats
- Threads and StackTrace
These options can be expanded by making changes to the DebuggerOption ViewModel to add a new option, and by implementing the required code in CorDbg.Operations class (Depicted below code for collecting information threads and stack trace information of the attached process.
The operation workflow is as follows:
- Refresh process list – if required (This is if the target application was launched after the utility was running)
- Select the mode to attach to the target process.
- Select operation and click on the Go button… That simple!
In this example, the target application was another instance of Visual Studio.
Image 3 – Managed Heap Stats
Image 4 – Running threads and their stack traces
I hope you’ll find this utility useful, please feel free to download it and extend it.
[office src="https://onedrive.live.com/embed?cid=2FE1291768841ACE&resid=2FE1291768841ACE!5826&authkey=!ADRH2VtXOaWWuRU" width="98" height="120""]