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

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);

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.

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.

Removing a Breakpoint

If you no longer require a breakpoint, it can be removed using the same actions used to create it. You can select the marked line of code and choose the "Toggle Breakpoint" command or press F9. Alternatively, simply click the breakpoint glyph. When you successfully remove a breakpoint the glyph disappears.

When debugging large programs it is common to set many breakpoints, possibly in several different files. Once the debugging operation is complete, you can remove all of the breakpoints in a project by selecting the "Delete All Breakpoints" option from the Debug menu.

Disabling Breakpoints

Sometimes you will want to prevent a breakpoint from halting execution without deleting it completely. You can disable all of the breakpoints in the project by selecting the "Disable All Breakpoints" command from the Debug menu. To disable a single breakpoint, right-click its glyph and choose "Disable Breakpoint" from the context-sensitive menu that appears. A disabled breakpoint's glyph is shown without shading, as in the image below:

Disabled breakpoint

File Breakpoint Dialog

In addition to the simple breakpoints that can be created using the above methods, Visual Studio allows fine control over the break action using a series of advanced settings. The first set of these is accessed using the File Breakpoint dialog box. To view these settings, right-click an active breakpoint and choose "Location..." from the context-sensitive menu.

Breakpoint location dialog box

The File Breakpoint dialog box provides more control over the position of a breakpoint using three settings. Firstly, the filename of the code file in which to place the breakpoint can be specified. The line number and character position can also be provided. This allows a breakpoint to skip the first statement if you have placed more than one command on a single line.

Usually when debugging in break mode, the source code file must perfectly match the file that was used when the program was compiled. Sometimes you will have made minor modifications to the code but will still want the breakpoint to be used. In these situations you can place a tick in the checkbox to "Allow the source code to be different from the original version".

20 August 2008