Reader Level:
ARTICLE

Debugging C# Application - Part 2 : Debugging Windows

Posted by Sivaraman Dhamodaran Articles | C# Language March 10, 2011
In this part we will look at various debugging windows and how will you use it.
  • 0
  • 0
  • 7155
Download Files:
 

1. Introduction


In this second part we will still use the same sample that we used in the previous part. Here, we will cover some other important windows that help the process of debugging.

 

You can read the series from the below links. Thanks for the readers who advised me to provide links.

 

Part 1: Debugging Basic
Part 2: Debugging Windows [You are here]
Part 3: Tracing

 

The screen shot of the application is given below (Yes! I am using the same sample as from the previous part of debugging):

 

Pic000.JPG
 

2. Call Stack


The call stack window helps to see the order in which function calls occurred from the application startup to the place where you break the application. Before I explain more about it please follow the steps below to view the Call Stack window.

 

1) Open the Attached application and put a break point in the function IsPrime as shown below.

 

Pic001.JPG
 

2) Run the Sample and enter 24, then press the button Get Prime Sum. This will get you to the break point.


3) Now view the Call Stack Window by navigating Debug=>Window=>Call Stack. The call stack window is shown below.

 

Pic001A.JPG
 

In the above call stack window, the first line in the top shows that the application is paused in the function IsPrime by taking the input parameter theNumber with the value 3. Now, look at the second line. The function SumIfPrime receives the parameter theNumber with value 24 from the function btnGetResult_Click (Event handler right?) and it called the function IsPrime where we kept the break point to temporarily halt the application. You can also note the beginning of the application, that is the program's entry point Main().

 

You can easily move to the function shown in the call stack by double clicking the particular line. It will be useful when you are working on a big project where the function call as shown above may happen between dll or from different assembly.

 

3. Auto and Local Window

 

Before I explain about these windows, we will add a function so that you can know the difference between them. The function added to the form is shown below:

 

//006: Auto Locals

void AutoLocals()

{

    int a, b, c, d, e;

    a = 10;

    b = 20;

    c = 30;

    d = 40;

    e = 50;

}

 

And this function is called in the constructor:

 

//001 : Declarations

int PrimeSum = 0;

 

public frmDebug()

{

    InitializeComponent();

    AutoLocals();

}

 

Now put a break point on the function in the statement b=20. Run the sample with the input number 24. Once the break point is hit, open the auto and local windows. You can reach these windows by navigating to Debug=>Windows. If it is not available, make it available using the tools=>customize option. It is explained in the first article to bring the step-in, step-out, step-over and the link is provided in the introduction section. The auto window is shown below:


 

Pic002.JPG

The break point is at b=20, and it displays the variable that changed or that was involved in the previous statement plus the variable that was involved in the execution of the current statement. Also the changed variable value is shown in red color, which is not recognized in the first statement as I highlighted it (Oh! Man, my mistake). Now, step over the statement to move the control to the next statement say C=30 and see how the Autos window changes.

 

The local window shows the variable, which is local to the execution scope. Here, in our case if the execution point is at d=40 for example, all the variables are displayed as they are local between the curly braces start to end. The following is a screen shot of the locals window:


Pic003.JPG
 

4. Output Window

 

The output window is useful to display the content written by the code. We should use the System.diagnostic name space to access the debug utility method. Write the following piece of code in the function SumIfPrime as shown below:

 

//004: When a passed in number is prime, sum it.

void SumIfPrime(int theNumber)

{

    for (int i = 1; i <= theNumber; i++)

    {

        if (IsPrime(i) == true)

        {

            txtOutput.AppendText(i.ToString() + ", ");

            PrimeSum = PrimeSum + i;

         System.Diagnostics.Debug.WriteLine( string.Format("Prime Detected {0}", i));

        }

    }

}

 

Now, the above code will write the string specified in the output window. Just, imagine how do you write something on your console output window. It is similar to that but what you write is displayed on the output window. Remove all the break points and run the sample with the input 24 again and look at the output window. Before you click the button on the form, right click the output window and select clear all from the context menu. This will help you to see more clearly what you wrote. If the output window is not shown then show it by using Debug=>Windows or from the customize option. The output window content after running the application is shown below:

 

Pic004.JPG 

 

5.  Immediate window

 

While the output window allows you to print something on it through the utility function supplied by the .Net framework, the immediate window allows you to examine executed expressions. Put a break point in the statement e=50 in the autolocals function. Run the sample attached with the input value 24. Once the break point has been hit, view the output window.

 

Now write the following in the immediate window and hit enter: ?c

The above command retrieves the value of c and displays it in the next line. Looks simple and you are wondering what is there in it? I can simply hover my mouse on the variable c to see what value it has. To know the real use, type the following and hit enter and I think that an explanation of why the immediate window is useful is not required. I have used it many times when framing a complex string manipulation statement. It is painful to run the application again and again to make string manipulation expressions perfect. Use it if you are new to it and you will become addicted to this window.

 

?(a*a) + (2*a*b) + (b*b)

900

 

The below screen shot shows immediate window in action:

Pic005.JPG 


Note: The sample is created in VS2005. If you have the latest IDE, Say yes to the conversion UI displayed.
 

erver'>
COMMENT USING

Trending up