Debugging With Visual Studio 2010

The Microsoft Visual Studio 2010/2012 IDE integrates a full-fledged debugger and familiar user interface that provides a variety of tools and behaviors to assist you to debug your application and identify and diagnose bugs and issues during development. The Visual Studio Debugger provides various techniques such as Breakpoints, Watch Windows, Step Into, Call Stack, Thread Monitoring, etc. to inspect the values and monitor the behavior of your program.

Various new features in debugging have been evolved, but this article discusses the most common fundamental techniques that I have seen and experienced myself.

Control Flow of Debugging Session

  • Start Execution

    From the Debug Menu there are various commands to start a debugging sessions such as Start Debugging (F5), Step into (F11), and Step over (F10).

    Figure1.gif
    Figure 1.1 Start Debugging

    You can start a debugging sessions with the "Run To Cursor" command. In the source editor, right-click the target line of the source code and select "Run To Cursor" (Ctrl+F10).

    Code example

    In this example we create a class library MathTest that is responsible for performing some math related operations, such as addition, multiplication and division.

    Figure2.gif
    Figure 1.2 Math operation Library class

    So now in the "Run To Cursor" (Ctrl + F10) debugging command, you don't need to compile or debug (F5) the application, you just right-click over any line of code (such as the Addition() method) in the Main class and debugging starts automatically from that line of code as.

    Figure3.gif
    Figure 1.3 Run To Cursor Debugging

    When you use Run To Cursor, the output will display in the console window as well as a Yellow arrow is highlighted over the Addition() method only in the code file, not on other methods as in:

    Figure4.gif
    Figure 1.4 Run To Cursor output
     
  • Break Execution

    If you start debugging by placing a couple of breakpoints in multiple class library files and you want to terminate everything in the current debugging session then you can break this session of an application that is enabled when you insert a Breakpoint or Run To Cursor on a specified line of code and start debugging. You can forcibly break into an application using the Terminate All command from the Debug menu.

    Figure5.gif
    Figure 1.5 Terminate All
     
  • Stop Execution

    Select "Stop debugging" from the Debug menu to end a debugging session. You also can stop debugging from the Processes window. In that window, right-click the executing process and select the Detach Process command or the Terminate Process command.

    Figure6.gif
    Figure 1.6 Stop Debugging

Breakpoints

If you are performing some sophisticated operations and playing with some variables then you notice that the output is strange. So in this situation Breakpoints are a very useful debugging utility. By this you can cross-check what data is assigned to a specific variable at run time.

In the code editor, pressing F9 on a particular line sets a simple breakpoint (appears as a solid red circle). F9 is a toggle for setting or clearing a breakpoint. You can continue the application by pressing F5.

Figure7.gif
Figure 1.7 Breakpoints

When you insert a Breakpoint on the Addition() method (suspected function) in code using F9 or from the Debug menu and start Debugging by F5 then rather then displaying the output, the application runs in debug mode and the breakpoint color is converted from red to yellow color.

Now you move on further line by line using F10 and see what data has been assigned to a variable by hoverying the cursor over a particular variable. You can go deep into the method's code for inspection of a value by Step Into (F11) as in:

Figure8.gif
Figure 1.8 Breakpoints Step Into (F11)

Function Breakpoints

In this debugging you don't need to place breakpoints on a specifed method, you just set a breakpoint by choosing the New Breakpoint submenu of the debug menu, and then select Break a Function. Then specify the method name Multiply() that you want to inspect and start debugging by F5.

Figure9.gif
Figure 1.9 New Breakpoints (function)

Function Breakpoints break immediately prior to the first line of a function and can be set at compile time or run time. Then you can proceed using F10 to inspect a variable values in the method body as in:

Figure10.gif
Figure 1.10 Function Body inspection

Breakpoints Symbols

Code Stepping

Stepping through source code is the most significant common action in a debugging session. Step commands step through an application in source line increments depending on the open window. Between steps, expressions can be evaluated, variables updated, functions called, and scopes changed.

Step commands

  • Step Over (F10) steps to the next source line or instruction continuously and time to time we can check the value of a specific variable or object by hovering the mouse.

    Figure11.gif
    Figure 1.11 Steps Over
     
  • Step Into (F11) If we want go into any function body or instruction then we can do this by executing F11 when exection reaches a function we can proceed into it using F10 for checking the next instruction.

    Figure12.gif
    Figure 1.12 Steps Into
     
  • Step Out (Shift + F11) If we are executing in a function body by Step Into (F11) and now we want to proceed out from that body or boundary then we use "Step Out" (Shift+ F11). Execution will then proceed to the first line after the function call then execution will break.

Debug Window

Breakpoints Windows (Alt + Ctrl + B)

You can manage breakpoints in the Breakpoint window (opened from the Debug menu and windows submenu).

Figure13.gif
Figure 1.13 Breakpoints Window

The Breakpoint window lists all the breakpoints you have in the source code. In the Breakpoints window the first column of each row is a checkbox (enabled and disabled) that provides an option for inclusion or exclusion of the breakpoint. The condition column shows any condition set on the breakpoint.

Output Window (Ctrl + Alt + O)

The output windows can be opened from the View menu and contains messages from various sources from Visual Studio. This output window shows Visual Studio system services with the path responsible for executing the application. Sometimes an application may malfunction and the application eventually hangs so to overcome this problem we can locate that service and end it manually from the Task Manager.

Figure14.gif
Figure 1.14 Output Window

Watch Window

The Watch window can be opened from the Debug menu and the Windows submenu. The Watch window has the three columns Name, Value and Type. The variables can be viewed at run time and modified directly in the variables window. The changed values are highlighted in red and this is a useful way to test applications with values that stress the program.
Here first we insert a breakpoint over the Addition() method and start debugging by F5. Then we perform "Step Over" (F10) and then open the Watch window from the Debug menu. Then we write manually the var1 and var2 and hit enter. It will display the values that are assigned at run time.

Figure15.gif
Figure 1.15 Watch1 Window

Locals Window (Ctrl +D,L)

The Locals window lists the local variables that are currently in scope. The only difference between watch and locals window is that the locals windows display all the values of variable that are currently in scope. We don't need to specify a variable manually for checking their values as in the watch window.

Figure16.gif
Figure 1.16 Locals Window

Call Stack Window

It shows functions that are presently on the stack. The current location is highlighted with yellow arrow. Here the call stack window displays the current executing function that is in the stack:

Figure17.gif
Figure 1.17 Call Stack Window

Immediate Window

The Immediate window is a command line version of the Visual Studio debugger. You can display a value in the window, evaluate expressions, execute applications and perform menu commands.

Figure18.gif
Figure 1.18 Immediate Window

You just first start the application in debugging mode by F5 and then open this window. This is a shortcut utility to open other debugging windows, for instance a breakpoint by writing -bl, call stack window by -callstack etc.

Immediate windows commands

Quick Watch Window (Shift + F9)

The Quick Watch window is great for the programmer for inspecting values of variables at run time. First you insert a breakpoint over the method (Addition()) that isn't producing a desired result. Now use "Step Over" (by F10) and when you reach the breakpoint you can open the Quick Watch window in one of two ways.

Figure19.gif
Figure 1.19 Variable selection for Quick watch

You can right-click the mouse then choose "Quick Watch..." or you can put the mouse on the variable then hit "Shift+F9" to open the Quick Watch window. It displays the values of the selected variable ("var1") . Here we can reevaluate the current variable or manually enter various expressions.

Figure20.gif
Figure 1.20 Quick watch window

Thread Window

The Thread window lists the active threads of the current process. This window is available only in break mode.

Figure21.gif
Figure 1.21 Thread Window

Up Next
    Ebook Download
    View all
    Learn
    View all