Setting Breakpoints in Visual Studio

This article explains breakpoints that is one of the most used features by developers to debug applications. Breakpoints break (temporarily halt) execution of a program during runtime at certain point.

This article explains breakpoints that is one of the most used features by developers to debug applications. Breakpoints break (temporarily halt) execution of a program during runtime at certain point. I will describe the feature provided in Visual Studio for breakpoints and how they are useful for debugging code easily.

What is Breakpoint?


Breakpoints is a feature provided by the debugger in Microsoft Visual Studio that allows breaking execution of code at runtime when debugging applications.

For Example

debugging application

When you click on the side (in the margin) of a line of code in Visual Studio it sets a break point in your code as displayed in the image above. So when you start execution (debug) the code it will break execution at this point.
 
BreakPoint Menu
(Option for the Break)

BreakPoint Menu

The precediing image shows the context-menu for breakpoints, in other words the breakpoint menu. The following describes all the options one by one.

  1. Delete Breakpoint

    This option of the breakpoint menu allows removal of a breakpoint.
     
  2. Disable Breakpoint

    This option of the breakpoint menu allows disabling a breakpoint so the breakpoint is just disabled and becomes gray. This is useful when the developer just wants to execute code without stopping at the breakpoint yet later on enable it again when they want to use it again.
     
  3. Location…

    This menu option allows setting a breakpoint at a specified location, in other words at a given line in the source code file.

    break point at given location

    When the menu is clicked a dialog is displayed that provides information about the source code file, line number in code and the character. This dialog allows changing line and character information.

    For Example

    breakpoint change to line number

    Change the line number to 26 in the dialog and thrn the breakpoint changes to line number 26.

    breakpoint change line example
     
  4. Condition…

    When this option is clicked then the following dialog with two options is displayed.

    BreakPoint Condition

    • IS true

      This option allows the setting of a condition for a breakpoint, so depending on this, execution during debugging is stopped at the breakpoint, in other words the breakpoint is hit when the condition is satisfied.

      For Example

      breakpoint Istrue condition

      As in the image above the condition "i==1000" is set; that means that this breakpoint is hit when this condition is true, in other words satisfied.
       
      This is very helpful when you want to stop execution of the application when a certain condition is met specially as in an example loop having many number of elements and we want to stop at a given location. 
       
      The condition for a breakpoint is set as shown in the following:

      stop execution at given location

      The breakpoint is changed to a red icon having a + sign in it. When the mouse is hovered over a breakpoint, Visual Studio displays information related to the breakpoint with its condition in a tooltip.
       

    • Has changed

      This option allows breaking execution of code at a breakpoint when there is a change in the variable or object.

      For Example

      breakpoint has changed condition

      Here the variable "i" is entered into the dialog, in other words if the value of variable "i" changes then execution of the program is halted at the breakpoint.
      The following image shows the tooltip on a breakpoint once a condition has been set.


      execution of program get stopped at breakpoint

      When this option is used then, in the breakpoint context menu, the condition option is checked as in the following image.

      breakpoint context menu condition option
     
  5. Hit Count..

    This option displays the count of the number of times the breakpoint has been hit during execution.

    breakpoint hit count

    This dialog box has three options that allow the hit count to be set and if the hit count matches then program execution is halted at the breakpoint. Each option is self-explanatory; you can see them in the following image.

    breakpoint hit count option
     
  6. Filter..

    This option allows the setting of a condition related to "Machinename and Process"  depending on which breakpoint is hit. The following image explains more details about it.

    breakpoint Filter
     
  7. When Hit...

    This option allows the setting of a message that is printed to the Output Window when program executions passes the breakpoint.

    This option converts a breakpoint to a Tracepoint, in other words so the developer can do tracing of an import variable or things by printing a message on the Output Window when code at the tracepoint is executed.
     
    For Example

    BreakPoint When Hit

    As in the image above, to read the value of the variable the developer created a message that is printed on the Output Window each time code in the for loop is executed.

    You can read the text in the dialog that also provides information about how to provide information in the TextBox so that it gets printed to the Output Window.
     
    Once the developer presses Ok, by setting the tracing message the breakpoint is displayed like a square as in the following window.

    breakpoint get display like square

    So when this program is debugged in Visual Studio, the messages are printed to the Output Window as shown in the following image.

    messages get printed to output window
     
  8. Edit Labels...

    This option allows the setting of labels for your breakpoints. So it displays a dialog as in the following:

    breakpoint Edit Labels

    This is helpful for when the developer exports breakpoints from Visual Studio, it helps to remember the importance of breakpoint via a label, the usefulness of a breakpoint using the same name and so on.
     
  9. Export..

    This option allows exporting of a breakpoint set by the developer.

    exporting breakpoint

    The exported breakpoint information is saved as "XML" as shown in the above image.

    Exported XML file for a breakpoint looks as in the following:

    Exported XML file
     
    This option is helpful when the developer wants to export a breakpoint and save it as XML then remove it.  When there is a need for the same breakpoint he/she can load the breakpoint by loading the XML file using the Debug window of Visual Studio.

BreakPoint window

A breakpoint/tracepoint set by a developer in code is displayed in this window. This window also displays a breakpoint with other information. 

The following is a way that shows how one can start a breakpoint window.

BreakPoint window

So one can either use a debug menu and start a breakpoint window or make use of the shortcut Ctrl+D,B.

Following is an image of the breakpoint window.

start breakpoint window

This window also has a toolbar that allows the following that is also supported by the breakpoint menu explained above.

  1. Set new breakpoint
  2. Remove breakpoint
  3. Delete breakpoint
  4. Enable/Disable breakpoint
  5. Export breakpoint
  6. Import breakpoint

    This option is not part of the breakpoint menu; it is used for importing a breakpoint/tracepoint.

    breakpoint tracepoint

     When this button is clicked by the developer, Visual Studio opens a file browser window to browse the XML file for the breakpoint / tracepoint.

    xml file for breakpoint  tracepoint

    So after loading this file the breakpoint/tracepoint is set again in the code.

    file breakpoint tracepoint
     
  7. Go to Source Code
  8. Go to Assembly
  9. Columns

    This tool menu allows to set the number of the column in the following grid, in other words in the breakpoint window grid that is below the toolbar.

    in breakpoint window grid
     
  10. Search

    Search allows searching for a breakpoint in the breakpoint grid of the breakpoint window. The following is an image of that. As per the criteria specified the breakpoint grid window displays searched breakpoints.

    searching breakpoint

Breakpoint Grid

This grid window displays all the breakpoints set by the developer in the source code. This grid provides information about each breakpoint depending on the columns set by the developer for the grid.

Breakpoint Grid

Conclusion

Breakpoints/tracepoints are a very useful feature provided in Visual Studio, very usefull for debugging a large amount of code.  Visual Studio provides the ability to break execution of the code during debugging at a breakpoint that is very helpful to the developer.