BlackWaspTM
Debugging
VS 2003+

Visual Studio Debug Mode

by Richard Carr, published at http://www.blackwasp.co.uk/DebugMode.aspx

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.

23 October 2008