BlackWaspTM

This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

Debugging
VS 2003+

Visual Studio Debug Mode

In addition to being a complex code editing tool, Visual Studio includes a debugger. This tool allows a program to be paused, then stepped through line-by-line whilst allowing you to monitor the values of variables, helping to identify bugs.

The Visual Studio Debugger

Visual Studio's in-built debugger allows you to pause and step through executable programs. These include console applications, Windows Forms applications and Windows Presentation Foundation software. The debugger can also attach to processes in other types of solutions, such as Windows services. However, in this article we will concentrate on programs that can be launched directly from Visual Studio in debug mode.

To enable the debugger to work with a program, the assembly to attach to must include debugging symbols. These symbols are included when an assembly is compiled in debug mode and omitted when built in release mode. If you try to debug a release build, the program will simply execute as normal.

To demonstrate the debugger we need some example code. Start Visual Studio and create a new console application. Add the following code to the Program class, replacing the default Main method.

static void Main(string[] args)
{
    int table = 12;
    Console.WriteLine("Multiplication Table for {0}", table);

    for (int i = 1; i <= 12; i++)
    {
        int multiplied = Multiply(i, table);
        Console.WriteLine("{0} x {1} = {2}", i, table, multiplied);
    }
}

private static int Multiply(int value, int table)
{
    int multiplied = value;
    multiplied *= table;
    return multiplied;
}

Starting a Program in Debug Mode

The easiest manner in which to start a program in the debugger is the way in which most people execute programs from within Visual Studio. This is using the Start Debugging command from the Debug menu, by clicking the associated toolbar button or by pressing the F5 key. Try pressing F5 now. You will see the program execute, output a multiplication table and then close.

In order for a program to be halted during execution using this method, there must be some event that causes the program flow to pause. Usually you will add a breakpoint in the code for this purpose. The program can also break if an unhandled exception is thrown or if an assertion fails.

To demonstrate, add a breakpoint on the following line:

int multiplied = Multiply(i, table);

Execute the program again by pressing F5. When the breakpoint is reached, the program pauses. A yellow background colour is applied to the code associated with the breakpoint. This highlighting indicates the next command that will be executed. To continue processing, press the F5 key again. The for loop will execute once more before the breakpoint is hit and the program halts for a second time. Press the F5 key repeatedly until the program exits. Between iterations of the loop, switch to the console application's window so that you can see the messages being outputted.

Stepping Through Code

Another option for running a program in debug mode is the Step Into command, which can be accessed using the F11 key. This starts the program and pauses immediately before the first statement in the code is executed. If you try running the program using the F11 key, you should see the yellow highlight appear on the first brace character of the Main method's code block.

The Step Into command can be used any time a program is paused by the debugger. Each press of the key causes a single command or line of code to be executed. If you press F11 again, the highlight will move to the line that declares the table variable. Continue pressing the key until you reach the call to the Multiply method. The next press of F11 steps into the method. Keep stepping through the code until you are happy with the manner in which the command works, then remove the breakpoint and press F5 to allow the program to run to completion.

Sometimes you will want to step through a program but without seeing the internal operations of a method or property. In our example, you may want to step through the loop but not work through the Multiply method. In this situation, you can use the Step Over command, which is accessed using the F10 key. You can see this in action by starting the program by pressing F11, then stepping through the code until the code calling Multiply is highlighted. Pressing F11 would step into the method, pressing F10 executes the method and moves to the line following the call.

Occasionally you will be stepping through a program and will step into a method or property. Once within the member, you can use the Step Out command to execute the rest of the method code and return to the calling function. The Step Out command is accessed by pressing Shift-F11. Try stepping into the Multiply method and pressing this key combination.

NB: When using a command that executes several lines of code, the program will pause again if a breakpoint is met.

Stopping and Restarting

Whilst using the debugger, you may want to cease execution of the program so that you can return to editing the code. This is achieved using the Stop Debugging command or toolbar button, or by pressing Shift-F5. You can also stop and restart the program by using the Restart command or pressing Ctrl-Shift-F5.

23 October 2008