February 28, 2011

C# - Debugging C-sharp Applications - Part 1

1. Introduction to Debugging

Debugging is the process of finding the logical errors in the program by examining the statements during the execution. The execution of a program pauses when the breakpoint is hit. Here, I will walk you through the debugging the sample application supplied with this article.

The screen shot of the application is given below:

The first textbox will accept a number below 36000. The Get Prime Sum button will calculate sum of all the prime numbers between 1 and number given in the input. The prime numbers are displayed on the multi-line enabled text box. And, the summation of the prime number is displayed in the label control below the multi-line text box.

I am not going to explain the code that runs behind this form. However, you can understand it. What I do is, explain you some debugging techniques that you can use in your program to get away from the logical troubles.

2. Inserting a Break point

As already told, breakpoint temporarily pauses the program to examine some piece of code. Let us first put a breakpoint on the event handler for the Get Prime Sum button. Follow the Steps:

1) Open the attached solution
2) Locate the source code for the Get Prime Sum Handler
3) In the margin left click as shown below:

4) Notice the color of the statement is changed and there is a red ball in the margin area. We call this as BreakPoint. When we run the program and when the execution reaches the breakpoint, the program pauses on the statement.

5) Run the application. Type 16 in the small text box; click the Get Prime Sum button. Notice how the program pauses the execution at break point. You will get a pointing arrow showing the current statement that is under examination. Below Picture shows this:

6) Press F5 and close the running application.

3. Toolbar buttons: Step into, Step Over, Step Out, Continue, and Stop Debugging

Stepping through the statements one by one allows us to examine the state of each variable and effect of the statement execution. We will see how these debugging commands work in this section.
First, have a look at the screenshot below:

Don’t have this toolbar?

Attempt 1:
Right click next to any of the toolbar buttons already displayed. From the displayed menu select debug:

Still, do not have the all the above-specified toolbar buttons? Follow the Attempt 2.

Attempt 2:
1) Click on the "tools|Customize…" menu option
2) From the displayed dialog under the "Command-Tab", select the debug item in the category listbox.
3) Drag&Drop the commands as shown in the red box below to the debugging toolbar or even to any popup menu item

4. Step Over Debug Option

"Step Over" tells go ahead and execute the next immediate statement. Each time you do step over the next statement will get executed. Now run the application again and type 16 in the input text box and I hope you don’t remove the breakpoint still. The breakpoint will hit at button’s click event handler. Now click on the "Step Over Button". The statement txtOutput.text = “” is executed now. Click the step over button until you reach the last statement as shown below:

Place the cursor over txtInput.Text exactly over the ".Text" portion of it. The displayed pop-up shows the current value in the text box txtInput. The below screen shows this:

Like this, you can examine each variable, before executing a statement and after executing a statement. As we asked for Step Over, the execution moves statement by statement. Press F5 and Close the running application.

5. Step Into and Step Out

I once again expect that the breakpoint not yet removed. If  you already removed it, place it back on the same statement and run the sample application with 12 in the input box. Once execution resumes at second statement inside the button click event handler, click the "Step Over Button". Now we are at the “if” conditional statement.

The return value of the function IsNumberValid is part of the condition. In the previous section when we do step over the execution gone to the next statement. It means the function is executed before moving to the next statement.  When the "Step Into" command is used the execution passed inside the function. Now click the Step Into button. The execution now reaches the first statement in the function now as shown below:

Now step over until you reach the conditional statement. OK. The step into will enters inside the function that is part of the current statement. The step out is a reverse of it. Say for example, in our case once you reached the conditional statement you decided it is not worth to explore further inside this function. So you can go for "Step Out" option, which will execute the remaining statements in the function and the execution pauses on the statement, which called this function. Now click the step out function to go back to the statement that called the IsNumberValid function.

6. Breakpoint window

With a breakpoint window, you can easily manage multiple break points. A sample screen shot of the breakpoint window is shown below:

1) When you un-check the item in the window, the breakpoint will be disabled. You can see a disabled breakpoint in the above screen shot’s code area. You can right click the breakpoint bubble in the code area margin and disable it using the context menu as an alternate method.
2) The breakpoints icon shown in the toolbar in the above picture is used to bring the breakpoint window. If you cannot able to see the toolbar icon bring it from the customize option as explained in section 3 of this article.

Play around this window yourself. It is a kind of easy go.

7. Conditional Breakpoint

Once started using the breakpoints for some period of time on your big projects, you will really come to a situation “Hey, I need to break the execution of my loop when the loop is running for 160th time” or “I need to break my execution when a particular variable’s value changed”.  Of course, it is not a big deal as we can go ahead and place some extra piece of code to have a break point. But, this requires a re-compilation. And, in some corporate project, this re-compilation and re-launch consume considerable amount of time. The "Conditional Breakpoints" help you do this without the need for re-compilation as it does not involve any source code change. Let us explore the conditional breakpoints with the downloaded application.

First let us put a conditional breakpoint, which evaluates a Boolean expression and breaks when the expression returns true. Follow the steps below to do that:
1) Locate the function SumIfPrime.
2) Once you located the function put a breakpoint on a statement shown in the below picture

3) Right click the breakpoint and select Condition… from the context menu
4) From the displayed dialog type the condition as shown in the below picture, make sure the "Is true Radio Button" is in checked state. I will talk about "Has Changed Radio Button" later. Click OK.

Now, the breakpoint will be hit, only when the expression is evaluated to true. The expression will be true when the value of the variable i is between 20 to 30. Also, note the placement of the break point that pauses the execution based on the condition supplied. When the execution reaches the breakpoint, the runtime engines evaluates the expression supplied and pauses the execution only when the expression returns true. In our case, the conditional breakpoint stops the execution for all the prime numbers that comes in between 20 and 30. Below is the screenshot that shows how the break point looks:

To test this conditional breakpoint, launch the application. Then, type 137 in the input box. Click the Get Prime Sum button. How many time the breakpoint hit happened? This is a simple example, but you can find more suitable and useful one when you are debugging big solutions for the corporate company you are working for.

The “has changed” radio option tells the runtime that when the variable typed or return value of the expression changes, hit the break point. Need an Example? OK.

1) Right the click the conditional breakpoint
2) In the condition text box type PrimeSum
3) Check the radio button has changed

Now test the application. Whenever the value in the PrimeSum changed, the breakpoint hit happens. Hope you like this first part of debugging. Rest in Next. I mean part 2 of this article. Bye.

Note: The attached download sample is in VS2005. If you have the latest version, say yes to the conversion UI

Source Code : Download

No comments:

Post a Comment

Leave your comment(s) here.

Like this site? Tell it to your Friend :)