|
| 1 | +--- |
| 2 | +title: Get started with breakpoints |
| 3 | +description: Learn about breakpoints, one of the most important debugging techniques. |
| 4 | +ms.date: 11/22/2024 |
| 5 | +ms.topic: how-to |
| 6 | +f1_keywords: |
| 7 | + - vs.debug.breakpointswin |
| 8 | + - vc.breakpoints |
| 9 | + - vs.debug.breakpoints |
| 10 | + - vs.debug.disassembly.insert |
| 11 | + - vs.debug.sourcewin.edit |
| 12 | + - vs.debug.file |
| 13 | + - vs.debug.breakpoints.delete |
| 14 | + - vs.debug.menu.insert |
| 15 | + - vs.debug.filenames |
| 16 | + - vs.debug.sourcewin.insert |
| 17 | + - vs.debug.address |
| 18 | +helpviewer_keywords: |
| 19 | + - breakpoints, about breakpoints |
| 20 | +author: mikejo5000 |
| 21 | +ms.author: mikejo |
| 22 | +manager: mijacobs |
| 23 | +ms.subservice: debug-diagnostics |
| 24 | +--- |
| 25 | +# Get started with breakpoints in the Visual Studio debugger |
| 26 | + |
| 27 | +Breakpoints are one of the most important debugging techniques in your developer's toolbox. You set breakpoints wherever you want to pause debugger execution. For example, you may want to see the state of code variables or look at the call stack at a certain breakpoint. |
| 28 | + |
| 29 | +## <a name="BKMK_Overview"></a> Set breakpoints in source code |
| 30 | + |
| 31 | +You can set a breakpoint on any line of executable code. For example, take a look at this simple C# code that creates a simple loop. |
| 32 | + |
| 33 | +```csharp |
| 34 | +int testInt = 3; |
| 35 | + |
| 36 | +for (int i = 0; i < 10; i++) |
| 37 | +{ |
| 38 | + testInt += i; |
| 39 | +} |
| 40 | +``` |
| 41 | + |
| 42 | +You could set a breakpoint on the line of code with the variable assignment (`int testInt = 3`), the `for` loop, or any code inside the `for` loop. You can't set a breakpoint on method signatures, declarations for a namespace or class, or variable declarations if there's no assignment and no getter/setter. |
| 43 | + |
| 44 | +To set a breakpoint in source code: |
| 45 | + |
| 46 | +- Click in the far left margin next to a line of code. You can also select the line and press **F9**, select **Debug** > **Toggle Breakpoint**, or right-click and select **Breakpoint** > **Insert breakpoint**. The breakpoint appears as a red dot in the left margin. |
| 47 | + |
| 48 | +For most languages (including C#), Visual Studio automatically highlights breakpoint and current execution lines. For some languages, such as C++, which isn't highlighted by default, you can turn on highlighting of breakpoint and current lines by selecting **Tools** (or **Debug**) > **Options** > **Debugging** > **Highlight entire source line for breakpoints and current statement (C++ only)**. |
| 49 | + |
| 50 | +::: moniker range=">= vs-2022" |
| 51 | + |
| 52 | +::: moniker-end |
| 53 | +::: moniker range="<= vs-2019" |
| 54 | + |
| 55 | +::: moniker-end |
| 56 | + |
| 57 | +To debug, press **F5** or select **Debug** > **Start Debugging**. |
| 58 | + |
| 59 | +When you debug, execution pauses at the breakpoint, before the code on that line is executed. The breakpoint symbol shows a yellow arrow. |
| 60 | + |
| 61 | +::: moniker range=">= vs-2022" |
| 62 | +At the breakpoint in the following example, the value of `testInt` is still 3. So, the value hasn't changed since the variable was initialized (set to a value of 3) because the statement in yellow hasn't yet executed. |
| 63 | + |
| 64 | + |
| 65 | +::: moniker-end |
| 66 | + |
| 67 | +::: moniker range="<= vs-2019" |
| 68 | +At the breakpoint in the following example, the value of `testInt` is still 1. So, the value hasn't changed since the variable was initialized (set to a value of 1) because the statement in yellow hasn't yet executed. |
| 69 | + |
| 70 | + |
| 71 | +::: moniker-end |
| 72 | + |
| 73 | +When the debugger stops at the breakpoint, you can look at the current state of the app, including [variable values](../debugger/debugger-feature-tour.md#inspect-variables-with-data-tips) and the [call stack](../debugger/how-to-use-the-call-stack-window.md). |
| 74 | + |
| 75 | +::: moniker range=">= vs-2022" |
| 76 | +For example, in the following illustration, you can see the value of `testInt` in a datatip and in the **Locals** window. |
| 77 | + |
| 78 | + |
| 79 | +::: moniker-end |
| 80 | + |
| 81 | +Here are a few general instructions for working with breakpoints. |
| 82 | + |
| 83 | +- The breakpoint is a toggle. You can click it, press **F9**, or use **Debug** > **Toggle Breakpoint** to delete or reinsert it. |
| 84 | + |
| 85 | +- To disable a breakpoint without deleting it, hover over or right-click it, and select **Disable breakpoint**. Disabled breakpoints appear as empty dots in the left margin or the **Breakpoints** window. To re-enable a breakpoint, hover over or right-click it, and select **Enable breakpoint**. |
| 86 | + |
| 87 | +- Set conditions and actions, add and edit labels, or export a breakpoint by right-clicking it and selecting the appropriate command, or hovering over it and selecting the **Settings** icon. |
| 88 | + |
| 89 | +## Types of breakpoints |
| 90 | + |
| 91 | +Visual Studio supports different types of breakpoints to support different debugging scenarios, such as conditional breakpoints that only activate based on specified criteria. For more information, see [Use the right type of breakpoint](../debugger/using-breakpoints.md). |
| 92 | + |
| 93 | +## <a name="BKMK_Specify_advanced_properties_of_a_breakpoint_"></a> Manage breakpoints in the Breakpoints window |
| 94 | + |
| 95 | + You can use the **Breakpoints** window to see and manage all the breakpoints in your solution. This centralized location is especially helpful in a large solution, or for complex debugging scenarios where breakpoints are critical. |
| 96 | + |
| 97 | +In the **Breakpoints** window, you can search, sort, filter, enable/disable, or delete breakpoints. You can also set conditions and actions, or add a new function or data breakpoint. |
| 98 | + |
| 99 | +To open the **Breakpoints** window, select **Debug** > **Windows** > **Breakpoints**, or press **Ctrl**+**Alt**+**B**. |
| 100 | + |
| 101 | +::: moniker range=">= vs-2022" |
| 102 | + |
| 103 | +::: moniker-end |
| 104 | +::: moniker range="<= vs-2019" |
| 105 | + |
| 106 | +::: moniker-end |
| 107 | + |
| 108 | +To select the columns to display in the **Breakpoints** window, select **Show Columns**. Select a column header to sort the breakpoints list by that column. |
| 109 | + |
| 110 | +### <a name="BKMK_Set_a_breakpoint_at_a_function_return_in_the_Call_Stack_window"></a> Breakpoint labels |
| 111 | + |
| 112 | +You can use labels to sort and filter the list of breakpoints in the **Breakpoints** window. |
| 113 | + |
| 114 | +1. To add a label to a breakpoint, right-click the breakpoint in the source code or the **Breakpoints** window, and then select **Edit labels**. Add a new label or choose an existing one, and then select **OK**. |
| 115 | +2. Sort the breakpoint list in the **Breakpoints** window by selecting the **Labels**, **Conditions**, or other column headers. You can select the columns to display by selecting **Show Columns** in the toolbar. |
| 116 | + |
| 117 | +::: moniker range=">= vs-2022" |
| 118 | +### Breakpoint groups |
| 119 | + |
| 120 | +For complex debugging scenarios, you may want to create breakpoint groups to organize your breakpoints. This allows you to quickly enable and disable logical groupings of breakpoints, based upon the current scenario that you're trying to debug. |
| 121 | + |
| 122 | +You can create breakpoints in the **Breakpoints** window by selecting **New > Breakpoint Group**, and providing a name for the group. To add a breakpoint to a group, right-click the breakpoint and choose **Add to Breakpoint Group** > **\<group name\>**. Or, drag-and-drop your breakpoints into the desired group. |
| 123 | + |
| 124 | + |
| 125 | + |
| 126 | +To set a default breakpoint group, right-click a group and select **Set as default Breakpoint Group**. When you set a default breakpoint group, newly created breakpoints are automatically added to the group. |
| 127 | + |
| 128 | +::: moniker-end |
| 129 | + |
| 130 | +### Export and import breakpoints |
| 131 | + |
| 132 | +To save or share the state and location of your breakpoints, you can export or import them. |
| 133 | + |
| 134 | +::: moniker range=">= vs-2022" |
| 135 | +Starting in Visual Studio 2022 version 17.12 Preview 3, breakpoint groups are also included with the exported and imported breakpoints. |
| 136 | +::: moniker-end |
| 137 | + |
| 138 | +- To export a single breakpoint to an XML file, right-click the breakpoint in the source code or **Breakpoints** window, and select **Export** or **Export selected**. Select an export location, and then select **Save**. The default location is the solution folder. |
| 139 | +- To export several breakpoints, in the **Breakpoints** window, select the boxes next to the breakpoints, or enter search criteria in the **Search** field. Select the **Export all breakpoints matching the current search criteria** icon, and save the file. |
| 140 | +- To export all breakpoints, deselect all boxes and leave the **Search** field blank. Select the **Export all breakpoints matching the current search criteria** icon, and save the file. |
| 141 | +- To import breakpoints, in the **Breakpoints** window, select the **Import breakpoints from a file** icon, navigate to the XML file location, and select **Open**. |
| 142 | + |
| 143 | +## <a name="BKMK_Set_a_breakpoint_from_debugger_windows"></a> Set breakpoints from debugger windows |
| 144 | + |
| 145 | +You can also set breakpoints from the **Call Stack** and **Disassembly** debugger windows. |
| 146 | + |
| 147 | +### Set a breakpoint in the Call Stack window |
| 148 | + |
| 149 | + To break at the instruction or line that a calling function returns to, you can set a breakpoint in the **Call Stack** window. |
| 150 | + |
| 151 | +**To set a breakpoint in the Call Stack window:** |
| 152 | + |
| 153 | +1. To open the **Call Stack** window, you must be paused during debugging. Select **Debug** > **Windows** > **Call Stack**, or press **Ctrl**+**Alt**+**C**. |
| 154 | + |
| 155 | +2. In the **Call Stack** window, right-click the calling function and select **Breakpoint** > **Insert Breakpoint**, or press **F9**. |
| 156 | + |
| 157 | + A breakpoint symbol appears next to the function call name in the left margin of the call stack. |
| 158 | + |
| 159 | +The call stack breakpoint appears in the **Breakpoints** window as an address, with a memory location that corresponds to the next executable instruction in the function. |
| 160 | + |
| 161 | +The debugger breaks at the instruction. |
| 162 | + |
| 163 | +For more information about the call stack, see [How to: Use the Call Stack window](../debugger/how-to-use-the-call-stack-window.md). |
| 164 | + |
| 165 | +To visually trace breakpoints during code execution, see [Map methods on the call stack while debugging](../debugger/map-methods-on-the-call-stack-while-debugging-in-visual-studio.md). |
| 166 | + |
| 167 | +### Set a breakpoint in the Disassembly window |
| 168 | + |
| 169 | +1. To open the **Disassembly** window, you must be paused during debugging. Select **Debug** > **Windows** > **Disassembly**, or press **Ctrl**+**Alt**+**D**. |
| 170 | + |
| 171 | +2. In the **Disassembly** window, click in the left margin of the instruction you want to break at. You can also select it and press **F9**, or right-click and select **Breakpoint** > **Insert Breakpoint**. |
| 172 | + |
| 173 | +## Related content |
| 174 | + |
| 175 | +- [What is debugging?](../debugger/what-is-debugging.md) |
| 176 | +- [Write better C# code using Visual Studio](../debugger/write-better-code-with-visual-studio.md) |
| 177 | +- [First look at debugging](../debugger/debugger-feature-tour.md) |
| 178 | +- [Troubleshoot breakpoints in the Visual Studio debugger](../debugger/troubleshooting-breakpoints.md) |
0 commit comments