CLR MD A New Swiss Army Knife tool for Advanced Debugging Kamran Bilgrami
Agenda Lots of Demos Production Debugging Methodology & Challenges CLR Memory Diagnostics (CLR MD) What, Why, Where, How Lots of Demos
What is Production Debugging
Bugs Lifecycle
Production Debugging - Goals Find Actionable information Fast Constrained Environment Limited Access to Client DCs Can’t run intrusive tools
Dump file based approach Free Dump Analysis Tools from Microsoft DebugDiag PerfView Windbg These are extremely useful tools, not always provide root cause. Without additional help (extensibility points)
Real Life Example Extremely slow Web Portal IIS consuming High Memory Dump file collected Dump file Analysis DebugDiag PerfView Windbg
Where CLR MD fits in puzzle Extending existing tools Custom Analysis Rules for DebugDiag Extension Dlls for Windbg Beyond Production Debugging Write your own tools for analysis
What is CLR Memory Diagnostics (CLR MD) APIs for programmatically inspecting a .NET live process or crash dump A.K.A CLR Managed Debugger Microsoft.Diagnostics.Runtime.dll Open Source Still in Beta (0.8.31) Microsoft does not recommend for production use
CLR MD Features Be Mindful of these features during live debugging Memory Diagnostics Walk the GC Heaps Walk Roots in the Process Walk threads in the process to get call stacks Walk AppDomain Lot more Be Mindful of these features during live debugging
CLR MD Object Model DataTarget ClrModule ClrRuntime ClrType ClrHandle ClrStackFrame ClrHeap ClrInterface
DataTarget DataTarget for Extension Dll Represents a crash dump file or a live process that you want to debug. DataTarget for a Crash dump DataTarget target = DataTarget.LoadCrashDump(@"c:\Dumps\myProcess.dmp"); DataTarget for Live Process DataTarget target = DataTarget. AttachToProcess(processId, timeout); DataTarget for Extension Dll DataTarget target = DataTarget.CreateFromDebuggerInterface(IDebugClient);
ClrRuntime Represents data for a CLR version present in a process. Few key properties ServerGC (true/false) HeapCount Threads AppDomains
Writing an Extension Dll – 1/2 Create a Class Library Project Dependencies CLR MD ( Install-Package Microsoft.Diagnostics.Runtime –Pre ) Unmanaged Export (Install-Package UnmanagedExports )
Writing an Extension Dll – 2/2 One-time setup code Function Exports Stream for output IDebugClient DataTarget ClrRuntime
Creating Extension dll
Extending DebugDiag DebugDiag offers extensibility mechanism using .NET Requires to implement one or more DebugDiag API Interfaces
DebugDiag API Interfaces Description IAnalysisRuleMetadata Used for providing classifications for Analysis Rules IMultiDumpRule Executed once per each memory dump when a series of dumps are selected. Could be used for analyzing the increase in memory usage between multiple dump files. IHangDumpRule Executed once per memory dump being analyzed. Used for items on the heap, or deadlocks/blocking involving multiple threads. IHangThreadRule Executed once per thread in each dump being analyzed. Could be used for checking anything thread-specific, such as call stacks or stack objects IExceptionThreadRule Executed on dumps where the context is set to a thread with an Exception. Could be used for Checking a faulting thread for a specific call stack and exception type.
Writing Custom DebugDiag Rule Create a Class Library Project Dependencies CLR MD (DebugDiag Installation Folder ) DebugDiag.DotNet.dll (DebugDiag Installation Folder) Implement one or more interfaces
Custom DebugDiag Rule
Beyond Production Debugging
Useful Links Defrag Tools – DebugDiags Series https://channel9.msdn.com/Shows/Defrag-Tools/Defrag-Tools-124-DebugDiag-Part-4 Defrag Tools – Writing CLR Debugger Extension https://channel9.msdn.com/Shows/Defrag-Tools/Defrag-Tools-109-Writing-a-CLR- Debugger-Extension-Part-1 My contact id: kamran@windowsdebugging.com