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
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)
The precediing image shows the context-menu for breakpoints, in other words the breakpoint menu. The following describes all the options one by one.
- Delete Breakpoint
This option of the breakpoint menu allows removal of a breakpoint.
- 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.
- Location…
This menu option allows setting a breakpoint at a specified location, in other words at a given line in the source code file.
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
Change the line number to 26 in the dialog and thrn the breakpoint changes to line number 26.
- Condition…
When this option is clicked then the following dialog with two options is displayed.
- Hit Count..
This option displays the count of the number of times the breakpoint has been hit during execution.
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.
- 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.
- 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
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.
So when this program is debugged in Visual Studio, the messages are printed to the Output Window as shown in the following image.
- Edit Labels...
This option allows the setting of labels for your breakpoints. So it displays a dialog as in the following:
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.
- Export..
This option allows exporting of a breakpoint set by the developer.
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:
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.
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.
This window also has a toolbar that allows the following that is also supported by the breakpoint menu explained above.
- Set new breakpoint
- Remove breakpoint
- Delete breakpoint
- Enable/Disable breakpoint
- Export breakpoint
- Import breakpoint
This option is not part of the breakpoint menu; it is used for importing a 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.
So after loading this file the breakpoint/tracepoint is set again in the code.
- Go to Source Code
- Go to Assembly
- 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.
- 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.
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.
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.