Pages

Monday, January 31, 2011

Debugging in Visual Studio 2010

Debugging is the process of finding and fixing the bugs/defects of a software program. And it’s a essential and a critical part of the software development life cycle. Sometime it may take more time than writing the code. So how to debug? Well, literally you have to go through the code and find where, and what went wrong. Then you have to come up with a fix.


Luckily most IDEs have some integrated  facilities that make the debugging process much easier. And Visual studio also come up with some nice and effective debugging tool set. Visual Studio built in debugger come with facilities to add breakpoints, error listing, tools for visualizing program flow, control the flow of execution, view variable values and lot more. This article is an introduction to the debugging tools of Visual Studio 2010. Since this span a little long, I broke it into two parts, so here’s the first one.



Start Debugging

If you want to start debugging your program, you can do it either by Debug menu or pressing F5 key. To start from the Debug Menu, select "Start Debugging". Another option is below the ‘Start Debugging’ which allow you to run the program without going into debug mode (Start Without Debugging). There is another way to start the debugging by "Attach Process". We often use “Attach process” to debug ASP.NET web applications. It will start a debug session for the application. But that is not covered in the context of this article.


Break Points

Breakpoints are used to pause the execution of program. When you insert a break point at a particular line of the code, debugger will pause the execution when it reaches the break point. You can put a breakpoint in code by clicking on the side bar of code or by just pressing F9 at the front of the line, or clicking “Toggle Breakpoint” in the Debug menu.



So after creating a breakpoint, you can check out what's going wrong within the code by using a different debugging tool. Then start the program and it will pause at the previously set break point. Then you can check the code for possible defects.

Step Over

When the debugger pause the program at the breakpoint, you can execute the code line by line using "Step Over". You can execute it by F10 key or either by debug menu or debug tool bar. This will execute the currently highlighted line and then pause. But this slightly changes when you “step over” when a method call statement is highlighted. Step Over will execute the entire method at a time.




Step Into


This is quite similar to Step Over. Use F11 or debug menu/toolbar to execute this. The only difference between them occurs when, debugging a method call. If the current highlighted section is any method call, the debugger will go inside the method. That is it’ll go through the lines of the method.
 
Step Out

Step Out is applied when you are debugging inside a method. When you execute step out [Shift+F11, or Debug Menu/toolbar] within a method, then the execution will complete the execution of the method and will pause at the next statement from where it called.

Continue

This will continue the usual program flow until it reaches the next breakpoint. Then it will pause the program at the second break pointer. Shortcut key for continue is F5.

Break Pont Labels


Visual Studio 2010 allow you to give specific labels to Break Points, which allow to manage the break points easily. This is a new feature in Visual Studio 2010. Imagine if you have multiple break points in the code and suppose you debugged the code and currently stopped at the first break point. Now you must see a window like this. (If you can’t see this by default click on the “”breakpoints” in debugging toolbar)



You can see the label column is currently blank. To set a label on break point, just need to right click on the breakpoint symbol on the particular line (in code window) or you can set it directly from breakpoint window. After that, click on the Edit Labels link, then add the label for the breakpoint. Giving an unique name per each break point you can easily follow where the execution is happening. (There is anohter important thing to mention, in the image you can see there’s a column “Hit Count”. It’ll show how many time the statement has been executed)

Set Next Statement

Set Next Statement allows you to change the path of execution of program while debugging. When your program paused at a particular breakpoint, go to the particular line/breakpoint, then “right click” and select "Set Next Statement" from the menu. That will move the execution to that particular line without going through the lines of code in between. There’s one limitation in this facility, that is, this “next statement” can be only set within the current function/code block.



Setting Conditions

Well, not all the codes are simple and in the other hand we don’t want to break the code for every time it execute. Which is, imagine if the code is huge, so then breaking it unnecessarily will make the debugging process slower. And in the other scenario, we don’t have/want to check the code for every time. What we should do is checking it for some values where we suspect that error might occur.

Imagine there is a code which it’s likely to prone error when a variable reaches a particular value (let’s say 5). So, then we don’t have to run it through a break point for all the time but we must break it at the exact point when variable becomes 5. Visual Studio made that possible! First, put a breakpoint on the particular line where you want to pause execution. Then just "Right Click" on the "Red" breakpoint icon. From there select "Condition" from the menu.



Then you have to enter the condition for the break point in  the “Break Point Condition” window. Currently (as you can see in the picture) the break point is set to “Console.WriteLine(i);” statement. Now you know what you have to do is, give the conditional value for ‘i’ to break the execution.



Since we want to stop the execution when ‘i’ become 5, give the condition as, “i.Equals (5)” Making your work much easier, intellisense is allowed in the condition box. Conditional break points can be identified by the plus (+) symbol inside the breakpoint circle. After setup of the condition of your breakpoint, run the application to debug it, you will see execution of program is only paused when it satisfied the given condition (when i=5).
 
Those are the basics of setting break points and watching the code execution.



No comments:

Post a Comment

Had to enable word verification due to number of spam comments received. Sorry for the inconvenience caused.