When the code is not working correctly, you can debug to locate the error. Common program debugging operations include setting breakpoints, starting, breaking and stopping program execution, stepping through programs, and making programs run to a specified location. These commonly used program debugging operations are described in detail below.
1. Breakpoint Action
Breakpoints notify the debugger that the application is interrupted at a certain point (pause execution) or when a situation occurs. When an outage occurs, the program and debugger are said to be in break mode. Entering break mode does not terminate or end the execution of the program, and all elements, such as functions, variables, and objects, remain in memory. Execution can continue at any time.
There are 3 ways to insert a breakpoint: Click in the gray space next to the line of code where you want to set a breakpoint, right-click the line of code where you want to set a breakpoint, choose breakpoints → insert Breakpoint command from the shortcut menu that pops up, 11.4, click the line of code where you want to set a breakpoint, select Debug → toggle Breakpoint command, shown in 11.5.
Figure 11.5 Menu Bar Insert Breakpoint
When you insert a breakpoint, a red dot appears in the gray space next to the line where you set the breakpoint, and the line code is highlighted, as shown in 11.6.
Figure 11.6 after inserting a breakpoint
There are 3 main ways to delete breakpoints, such as:
(1) You can click the red dot to the left of the line of code where you set the breakpoint.
(2) Right-click on the red dot on the left side of the line where you set the breakpoint, and in the shortcut menu that pops up, choose Delete Breakpoint command, as shown in 11.7.
Figure 11.7 Right-click shortcut menu Delete Breakpoint
(3) Right-click on the line of code that sets the breakpoint and select the breakpoint/delete breakpoint command from the popup shortcut menu.
2. Start execution
Starting execution is one of the most basic debugging features, choose the Start Debugging menu from the Debug menu (11.8), or right-click a row in the executable code in the Source Code window, and choose the Run to Cursor menu from the shortcut menu that appears, as shown in 11.9.
Figure 11.8 Selecting the Start Debugging menu
Figure 11.9 Selecting the Run To Cursor menu
In addition to using the methods described above to start execution, you can also directly click on the toolbar button, start debugging, 11.10 shows.
Figure 11.10 The Start Debugging button in the toolbar
If you select the Start Debugging menu, the application starts and runs to the breakpoint at which point the code at the breakpoint is displayed in yellow color, as shown in 11.11. You can break execution at any time to see the value (move the mouse over the corresponding variable or object to see its specific value, 11.12), modify the variable, or observe the state of the program.
Figure 11.11 Running to a breakpoint
Figure 11.12 Viewing the value of a variable
If you select the run to Cursor command, the application starts and runs to the breakpoint or cursor position, depending on whether the breakpoint is before or before the cursor, and you can set the cursor position in the Source code window. If the cursor is in front of the breakpoint, the code first runs to the cursor, as shown in 11.13.
Figure 11.13 Running to the cursor
3. Interrupt execution
When execution reaches a breakpoint or an exception occurs, the debugger interrupts the execution of the program. When you select Debug → break all menu, the debugger stops execution of all programs running under the debugger. The program does not exit and can resume execution at any time, when the application is in break mode. The "Break all" menu in the "Debug" menu is shown in 11.14.
Figure 11.14 "Debug" → "Break All" menu
In addition to interrupting execution by selecting the debug → break all command, you can click the button in the toolbar to break execution, as shown in 11.15.
Figure 11.15 The Break execution button in the toolbar
4. Stop execution
Stopping execution means terminating the process you are debugging and ending the debugging session by selecting the debug → stop debugging command in the menu to end the run and debug. You can also select a button in the toolbar to stop execution.
5. Single-step and process-wise execution
With stepping, the debugger executes only one line of code at a time, and stepping is done primarily through statement-by-step, process-by-step, and out-of-3 commands. The main difference between "step-by-step" and "process-by-procedure" is that when a row contains a function call, a statement executes only the call itself, and then stops at the first line of code within the function. Instead, step over the entire function, and then stop at the first line of code outside the function. If you are inside a function call and want to return to the calling function, you should use "Jump out" and "Step Out" will execute the code until the function returns and then break at the return point in the calling function.
When you start debugging, you can click a button in the toolbar to perform a step-by-step action, click the button to perform a step over action, and click the button to perform a step out action, as shown in 11.16.
Figure 11.16 3 commands in one step
Note: In addition to clicking on the 3 buttons in the toolbar, you can also perform these 3 actions by using the shortcut key, press <F11> to perform the step by step action, <F10> key to perform step over action, <Shift+F10> key execution "jump out" after starting debugging. Operation
This article is from tomorrow's Science and technology publishing "0 Fundamentals C # " , reproduced please specify the source!!!
How to use Visual Studio to debug a C # program