Interactive Debugging and How to create PlugCommands

date March 4, 2009 22:39 by author Sukesh Ashok Kumar

There are times when these automated tools don’t cover everything what we need out of memory dumps. Sometimes information in the dumps might not be required for reports but only required to be checked in specific scenarios. Interactive debugging option in Debug Analyzer.NET is just for that but powered with the same simplicity and object model.

In the previous post I discussed how simple it is to draw graph in such a small number of lines. Let us see how the same information can be added as a PlugCommand for Interactive Debugging session.

//Function Attributes required to enable the function as PlugCommand
[PlugCommand(
Command="top10heapbyreservedsize",
Description="Shows Top 10 Heap By Reserved Size",
Usage="Top10HeapByReservedSize")]
public string Top10HeapByReservedSize()
{
    //Top 10 heaps by reserved memory
    IEnumerable<NTHeap> _Top10HeapByReservedSize = 
                    Analyzer.HeapCollection.AsQueryable().OrderByDescending(h => h.ReservedMemory).Take(10);
    StringBuilder heapstr = new StringBuilder();
    heapstr.AppendFormat("{0} {1} {2} {3}",
                                                "[Handle]".PadRight(10),
                                                "[Reserve Size]".PadLeft(15),
                                                "[Heap Name]",
                                                Environment.NewLine);
    foreach (NTHeap h in _Top10HeapByReservedSize)
    {
        heapstr.AppendFormat("{0} {1} {2} {3}",
                                                Analyzer.GetAsHexString(h.Handle),
                                                Analyzer.FormatByteToString(h.ReservedMemory).PadLeft(15),
                                                h.HeapName,
                                                Environment.NewLine);
    }
    return heapstr.ToString();
}

So the only requirement for PlugCommand is that it should be a function which returns a string and all string parameters (if any) and the “PlugCommand” attribute should be added which publishes the function as a Command to the ‘Plug Framework’

Lets see how it looks like in the output of Interactive session below

Plugcommand Reservedsize

Please let me know the feedback through comments!

Currently rated 5.0 by 1 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5


Sample Code : How to show a graph with Top 10 Heaps by Reserved Size?

date March 2, 2009 01:04 by author Sukesh Ashok Kumar

Let us see the output first and then check the Plug code required to get the same.

Top10HeapsByReservedSize

Now let us check the Analysis code snippet!

void GraphTop10HeapByReservedSize()
{
    //Top 10 Heaps by Reserved Memory Size
    IEnumerable<NTHeap> _Top10HeapByReservedSize = 
                            Analyzer.HeapCollection.AsQueryable().OrderByDescending(h => h.ReservedMemory).Take(10);
    double largestSize = 0;

    WriteBoldLine("Top 10 Heaps by Reserved Size");
    foreach (NTHeap h in _Top10HeapByReservedSize)
    {
        if (largestSize == 0)       
        {
            largestSize = h.ReservedMemory;
            WriteGraphLine(GraphColors.LightGreen, 100,        //1st element is always 100%
                                string.Format("Heap Handle - {0}, Size - {1}, Name - {2}",
                                CreateAnchor(AnchorType.Heap, Analyzer.GetAsHexString(h.Handle)),
                                Analyzer.FormatByteToString(h.ReservedMemory),
                                h.HeapName
                                ));
        }
        else
            //Calculate percentage relative to the 1st element
            WriteGraphLine(GraphColors.LightGreen, (int)((h.ReservedMemory/largestSize)*100) ,
                                string.Format("Heap Handle - {0}, Size - {1}, Name - {2}",
                                CreateAnchor(AnchorType.Heap, Analyzer.GetAsHexString(h.Handle)),
                                Analyzer.FormatByteToString(h.ReservedMemory),
                                h.HeapName
                                ));
    }
    WriteBlankLine();
    WriteBlankLine();
}

You must’ve noticed a function called ‘CreateAnchor’ which is actually used to link that Heap handle to details of Heap in the report… You can see how easy it is to use Lambda expression to get the results we need. Drawing graph has been never this easy. Isn’t it?

Now here is a question. How to show graph for Top 10 Heaps by Committed Size? No prizes for answer!

Currently rated 5.0 by 1 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5


All about Critical Section

date March 1, 2009 22:35 by author Sukesh Ashok Kumar

Critical Sections are synchronization objects used to access shared objects in a sequential manner inside a user mode process. There are several Critical Section objects created and used by the Operating System and applications can also create their own critical sections if there are requirements for sequential access to shared object.

Most important functions are give below

//Declare Critical Section
CRITICAL_SECTION cs;

//Initialize the Critical Section Lock 
//mostly done at initial portion of the application/component
InitializeCriticalSection(&cs);

//Enter Critical Section Lock
EnterCriticalSection(&cs);

//Shared object usage goes here

//Exit the Critical Section Lock
LeaveCriticalSection(&cs);

//Finally cleanup the Critical Section Lock
DeleteCriticalSection(&cs);

 

But all these functions or features become a trouble if not used properly or when the portion of the code has bugs. So there are several types of issues related to Critical Sections we see when we start analyzing dumps and some of them are listed below

  • Blocked Critical Section
  • Loader Locks
  • Leaked Critical Section
  • Orphaned Critical Section
  • Lock Convoys

Let us see the scenarios in which each of these issues happens

… To be continued soon…

Currently rated 5.0 by 1 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5


Sample Code : How to show list of threads (Windbg kbn)?

date February 21, 2009 00:07 by author Sukesh Ashok Kumar

This code snipped shows how easy it is to show list of threads and it's frames with details in the report

// This code snippet shows how easy it is to show list of threads and it's frames with details
public class ThreadAnalysis : iPlugType
{
    public override void RunAnalysis()
    {
        BeginReport("Thread Analysis");

        // ****************** THREAD INFO ********************
        // Iterate through all threads
        foreach (ThreadStack threaditem in Analyzer.ThreadStackCollection)
        {
            WriteHTMLLine("<b>Thread # {0}, SystemID # {1}</b>" , 
                                CreateBookmark(AnchorType.Thread, threaditem.ThreadID.ToString()) ,
                                threaditem.SystemID.ToString());

            WriteDataLine("Entry point : {0}" , 
                                Analyzer.GetSymbolFromAddress(Analyzer.GetEntryPointForThread(threaditem.ThreadID)));
            WriteDataLine("Create Time : {0} {1}" , 
                                threaditem.CreateTime.ToShortDateString(),
                                threaditem.CreateTime.ToLongTimeString());
            WriteDataLine("Time spent in User Mode : {0}",threaditem.UserTimeAsString);
            WriteDataLine("Time spent in Kernel Mode : {0}",threaditem.KernelTimeAsString);
            
            WriteBlankLine();

            //Iterate through all frames
            WriteDataLine("Fr#.ChildEBP...ReturnAddr.Function Arguments");    // Heading
            foreach (ThreadFrame frameitem in threaditem.ThreadFrameCollection)
            {
                WriteDataLine("{0} {1} {2} {3} {4} {5} {6} {7} {8}",
                            frameitem.FrameNumber.ToString("x3"),
                            Analyzer.GetAsHexString(frameitem.ChildEBP),
                            Analyzer.GetAsHexString(frameitem.ReturnAddress),
                            Analyzer.GetAsHexString(frameitem.arg1),
                            Analyzer.GetAsHexString(frameitem.arg2),
                            Analyzer.GetAsHexString(frameitem.arg3),
                            Analyzer.GetAsHexString(frameitem.arg4),
                            frameitem.FunctionName,
                            frameitem.SourceInfo
                            );
            }
            WriteBlankLine();
        }
        EndReport();
    }
}

Currently rated 5.0 by 1 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5


Hello World Analysis Plug

date February 20, 2009 23:31 by author Sukesh Ashok Kumar

A lot of work was done to keep simplicity and zero learning curve for writing Analysis Plugs. Steps to create a Hello World Plug mentioned below

  • Using Visual Studio or Visual Studio Express create a 'Class Library' project
  • Add reference to PlugFramework.Api
  • Inherit the Class from iPlugType
  • Override a method called RunAnalysis()
  • Build the DLL and copy to 'Plugs' folder
  • Add to any Analysis Template!
public class Class1 : iPlugType                                     // Derive your analysis class from iPlugType
{
    public override void RunAnalysis()                              // Override RunAnalysis()
    { 
        // Begin Report & specify Title of Analysis section
        BeginReport("Hello World!");                                

        WriteDataLine("1st Line of the report!");                     // Normal report line using fixed width font
        WriteBlankLine();                                             // Add blank line in the report
        WriteBoldLine("Drawing Graph has become easy as well...");    // Add Bold line
        WriteGraphLine(GraphColors.Gray, 70, "msvcrt.dll - 70%");     // Add Graph with color, percentage and label
        
        // Adding HTML content
        WriteHTMLLine("Testing for HTML content inline like <b>bold</b> <i>italics</i> <u>underline</u> words");

        // Adding analysis recommendation
        WriteRecommendation(ResultSeverity.Information, 
                            "Process crashed during Heap function call normally indicates Heap Corruption",
                            "Please Enable page heap and configure DebugDiag with Crash Rule"
                        );

        // End Reporting
        EndReport();
    }
}

This is a sample plug to show the different functions available but does not do any analysis by itself!

Currently rated 5.0 by 2 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5


Debug Analyzer.NET Screenshots

date February 19, 2009 21:16 by author Sukesh Ashok Kumar

 

Start Screen

Interactive Debugging

Analysis Templates

Symbol Settings

Options

Currently rated 5.0 by 1 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5


Welcome to Debug Analyzer.NET

date February 18, 2009 00:21 by author Sukesh Ashok Kumar

Having a job where you need to deal with user mode memory dumps for customer issues it’s always good to have some cool tools to ease your job.  When you start doing analysis for same kind of issues always this thought arises ‘why not automate’ the analysis.

When Debug Diagnostic Tool was released I was really excited that we could write analysis to target memory dumps and thereby find glaring issues easily. Default analysis scripts provided most of the basic analysis for hang/crash/memory related issues for user mode process in general and some analysis specific to IIS and few other products. I had written a detailed Introduction to Debug Diagnostic Tool here

Debug Diagnostic is an awesome tool but not many know that!

One day I got excited and got my Developer Hat On and started writing some analysis code, and that’s when I realized I had become too lazy to write code in ASP + VBScript and also as a .NET developer how the hell can I write code without intellisense Wink  This made me think about the features and functions missing in Debug Diagnostic Tool to make it the best tool for post mortem debugging of user mode process.

So here are some things I wanted to see in the tool

  • Analysis divided into plug-ins according to their usage
  • Automatic update for analysis plug-ins when analysis is updated
  • Use .NET language (C#) to write the plug-ins with complete Intellisense
  • An object model which can be molded so as to not break all the analysis plug-ins
  • Have the ability to customize the analysis templates
  • Simple HTM based report
  • Report look and feel can be changed without touching the existing plug-ins code
  • Interactive debugging console where you can get commands added to inside the analysis Plug-ins
  • lot more to come here… will update later :)

Adding all these features I know is a big-ask so I took up this task and this new tool called Debug Analyzer.NET was born.

This application was written just like an external person reading through Debug Diagnostic Tool Help file and going through Object Browser in Visual Studio. My development machine (laptop) is configured for Microsoft Public Symbol Server so that even accidently I would not step out to the trouble zone.

Most important ingredient for the tool is an easy to build and use Plug-in model and ‘Plug Framework’ which I wrote couple of years back came into use. For simplicity the plug-ins written with this Framework is called ‘Plugs’. I wrote this Framework to build a number of other cool applications some of which are in cooking stage at the moment (maybe some more will come out once the work on this application is done).

Debug Analyzer.NET has all the above features and much more and I’ll be writing about analysis, things I learnt in the process of building this application etc on this blog. When I started building this application I was trying to target .NET Framework 2.0 but later realized that newer version of the Framework and the newer compiler would make me more productive. Specially I love lambda expressions and LINQ and also some things available as part of Regular Expressions. Will write about these in more details later.

Currently rated 4.0 by 1 people

  • Currently 4/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5



Application Disclaimer

Debug Analyzer.NET and the associated documentation are provided "as-is". You bear the risk of using it. No express warranties, guarantees or conditions are provided. It is not supported or endorsed by my Employer and should be used at your own risk.

Disclaimer

All opinions posted here are those of the author and are in no way intended to represent the opinions of his employer. All posts are provided "AS IS" with no warranties, and confers no rights. © Copyright 2009

Recent Comments

Sign in