
VS 2005+Breakpoints and Tracepoints in Visual Studio
When debugging program code it is important to be able to pause the code at a specific location. Using Visual Studio breakpoints and tracepoints, the code can be halted at a desired line or can output trace information when conditions are met.
What are Breakpoints?
A breakpoint is a special marker in your code that is active when executing the program whilst using the Visual Studio debugger. When the marker is reached it causes the program to pause, changing the execution mode to break mode. You can then step through the code line-by-line using the Visual Studio debugging tools, whilst monitoring the contents of local and watched variables.
NB: Breakpoints are only active when using the Visual Studio debugger. When executing a program that has been compiled in release mode, or when the debugger is not active, breakpoints are unavailable.
Setting and Clearing Breakpoints
Breakpoints can be added to functional areas of a program or class library. A functional area is generally a line of code that will be executed when a program runs. Visual Studio will prevent the creation of breakpoints on non-functional lines, such as comments, blank lines or class declarations.
To demonstrate the use of breakpoints we will use a simple console application that calculates and displays multiplication tables. Create a new console application and add the following code to the Main method:
for (int i = 1; i <= 12; i++)
{
Console.WriteLine("Multiplication table {0}.\n", i);
for (int j = 1; j <= 12; j++)
{
Console.WriteLine("{0} x {1} = {2}", j, i, i * j);
}
Console.WriteLine("\n");
}
Setting a Breakpoint
There are several ways in which to set breakpoints. The first manner that we will explore is with the use of the Visual Studio menus. Using this method, a breakpoint is added by positioning the cursor on the desired line before selecting "Toggle Breakpoint" from the Debug menu. You can also press the F9 key for the same result.
To test this, position the cursor in the first line of code containing a Console.WriteLine statement. Press F9 to set the breakpoint. The line should change colour and you will see a circular icon appear in the grey margin area to the left of the code. This icon is called a breakpoint glyph. The filled circle glyph indicates an active, normal breakpoint.

The code can now be executed by pressing F5. When the breakpoint location is hit, the program enters break mode, allowing you to step through the code using the debugging tools. If after breaking you continue execution using the menu option, toolbar button or by pressing F5, you will see that the code pauses each time the breakpoint is hit.
A second way to add a breakpoint is to click within the grey margin where breakpoint glyphs are displayed. Click the position to the left of the line on which a breakpoint is to be set to add it. If a glyph does not appear, adjust the position of the point, ensuring that it lies to the left of a functional area of code.
20 August 2008