1 Introduction
In the software development cycle, test and fix bugs (Defect,defect versus Bugs: Bugs are a manifestation of defects, and a flaw can cause multiple bugs) for much longer than the time to write code. In general, debug refers to the process of discovering defects and correcting them. Bug fixes are followed by debug, or they are related. If there is a flaw in the code, we first need to identify the root cause of the defect (root cause), a process called debugging (Debugging). Once you have found the root cause, you can fix the defect.
So how do you debug your code? Visual Studio provides a number of tools for debugging. Sometimes debugging takes a lot of time to identify root cause. VS offers a number of handy tools for assisting debugging. The debugger (Debugger) contains error lists, add breakpoints, visual program flow, control execution flow, Data Tips, monitoring Windows (watch Windows), multithreaded debugging, thread windows, parallel debugging overviews, and IntelliTrace debugging overview. I hope this article will benefit users of debugging features. Please note that this article uses VS2010. Some features are consistent in older versions, but VS2010 adds a lot of features (labeling breakpoint, Pinned DataTip, multithreaded debugging, Parallel debugging and IntelliTrace). 2 How to start Debugging. You can start debugging by using the VS Debugging (Debug) menu. Click "Start Debugging" under the Debug menu or press F5 to start. If you've added a breakpoint to the code, the execution will start automatically.
Figure Start Debugging (Start Debugging)
Attaching to processes (Attach to process) is another way to start debugging. The Attach process initiates a debugging session for the application. Perhaps we are more familiar with attach process debugging for ASP.net Web applications. I sent another two related posts. As follows:
Debug Your asp.net application that hosted on IIS
Remote IIS Debugging:debug your asp.net application which is hosted on "Remote IIS Server"
Usually we start debugging by adding a breakpoint to the possible problem code. So we start with the breakpoint. A 3 breakpoint (breakpoints) breakpoint is used to tell the debugger when to suspend execution of a program. Add a breakpoint to the current line by clicking on the left column or pressing the F9 key. Before you add a breakpoint, you need to know what your code will be wrong and where to stop execution. When the debugger executes to the breakpoint, you can use other debugging tools to check the code for errors.
diagram Set Breakpoints (set breakpoint) 3.1 Using breakpoints to debug you have set a breakpoint in the place where you want to suspend execution. Now press F5 to start debugging and suspend execution automatically when the program executes to the breakpoint. There are a number of ways to check your code at this time. After the breakpoint (hit the breakpoint), the line with the break becomes yellow, meaning that the next step is to execute the row.
In break mode, you have more than one command to use, and use the command for further debugging.
Figure Breakpoint Bar (breakpoint Toolbar) 3.1.1 Process ( step over)After the debugger executes to a breakpoint, you may need a single code of execution."Step Over" [F10]command is used for one piece of code execution. This executes the currently highlighted row, and then pauses. If you press F10 when a method call statement is highlighted, execution stops at the next statement in the calling statement. Step over time will be the whole method.
figure: Step by Step (OVER-F10) 3.1.2 per statement ( Step Into)It is similar to step over. The only difference is that if the current highlight statement is a method call, the debugger goes inside the method. Shortcut keys are"F11"。
figure: step-by-Step (step into-f11) 3.1.3 Jump ( step Out)It is used when you are debugging inside a method. If you click within the current methodshift-f11, the debugger completes the execution of this method and then pauses at the next statement in the statement that calls this method.3.1.4 Continue (Continue)It's like a rerun of your program. It continues execution of the program until it encounters the next breakpoint. Shortcut keys are"F5"。3.1.5 Set Next statement (set next Statement)This is a very interesting feature. Set the next statement to allow you to debugchange the execution path of a program。 If your program pauses at a certain line and you want to change the execution path,skip to specified line, right click on this line, and in the right click menu, selectSet Next Statement“。 The program then goes to which line to execute without executing the previous code. This is useful when you find that some lines in your code can cause a program to break (breaks) and you don't want the program to break at that time. Shortcut keys areCtrl + Shift + F10。
figure: Set Next Statement (set next Statement) 3.1.6 Displays the Next statement (show next Statement [ctrl+*])This line is marked with a yellow arrow. This line is a statement that will be executed as the program continues to execute.3.2 Breakpoint Label (labeling in Breaking point)This is the new feature provided by VS2010 (feature). For better management of breakpoints. It allows us to better group and filter breakpoints. This looks like a collation of a breakpoint. If we add different types of breakpoints related to a function, we can enable, cancel (disable), and filter (filter) these breakpoints as needed. For example, suppose we want to debug a block of code.
Class program
{
static void Main (string[] args)
{
string[] strnames = {"Name1", "Name2", "Name3", "Name4" "," Name5 "," Name6 "};
foreach (string name in Strnames)
{
Console.WriteLine (name); Breakpoint
}
int temp = 4;
for (int i = 1; I <= i++)
{
if (i > 6)
temp = 5;
}
}
public static void Method1 ()
{
Console.WriteLine ("Broke point in Method1"); Breakpoint
} public
static void Method2 ()
{
Console.WriteLine ("Broke point in Method2"); Breakpoint
Console.WriteLine ("Break point in Method2"); Breakpoint
} public
static void Method3 ()
{
Console.WriteLine ("Broke point in Method3"); Breakpoint
}
}
The execution program will stop at the first breakpoint. The following figure gives a list of breakpoints.
Figure: Breakpoint listThe labels column in the above figure is empty. The following describes how to set a label on a breakpoint and how to use a label. You can set a label on any breakpoint simply by right-clicking on the breakpoint symbol on a particular line of code (①) or by setting (②) in the breakpoint window.
Figure: Set breakpoint label (Setting Breakpoint label)Right-click the breakpoint and click Edit Labels to add a label to any breakpoint. For the sample code, I have an easy-to-understand name for all of the breakpoints ' labels.
figure: Adding a breakpoint label (adding Breakpoint label)How these tags help us debug. All breakpoints are now enabled (enabled). If you do not want to debug method2, you usually have to go to the corresponding method one of the cancellation breakpoints, but here you can filter or search them through the tag name, and then select them to easily cancel them.
Figure: Filter breakpoints Using labels (filter breakpoint using Labels)Breakpoint label This is the complete description. My example is very simple, but the breakpoint tag is useful when you are debugging a lot of code, multiple projects, and so on.
3.3-Piece Breakpoint (Conditional breakpoint)Let's say you're working on multiple iterations (loops) and you just want to debug a few iterations. This means that you want to suspend your program based on certain conditions. Visual Studio breakpoints allow you to set conditional breakpoints. The debugger stops when and only if the condition is satisfied. First, you need to set breakpoints in the place where you want to suspend execution. Then right click on the Red breakpoint icon. Right-click on "Conditions" in the menu.
Setting Breakpoint conditions (set breakpoint Condition)Clicking on the "condition" in the right menu will pop up the following dialog box to set the condition of the breakpoint.
Figure: Breakpoint Condition SettingsLet's say you want to debug the following code block:
Class program
{
static void Main (string[] args)
{
string [] Strnames = {"Name1", "Name2", "Name3", "Name4" "," Name5 "," Name6 "};
foreach (string name in Strnames)
{
Console.WriteLine (name);//breakpoint are here
}
}}
You set a breakpoint at the Console.WriteLine () statement. Every For-each loop stops when the program is executed. What if you want the code to stop just name= "Name3"? Very simple, you only need to use the condition name. Equals ("Name3").
Setting Breakpoint conditionsView the breakpoint symbol. It should look like a plus (+) number inside the breakpoint symbol, which means that the breakpoint is a conditional breakpoint.
figure: Conditional breakpoint symbol (Conditional breakpoint symbol)After you set the condition of a breakpoint, in the debugger, the debugger only stops when a given condition is met.
figure: Conditional breakpoint hits (Conditional breakpoint hit)
Auto-complement (IntelliSense) for the Conditional input box:The breakpoint conditions given above are very simple and can be easily written to the conditional text box. Sometimes you may need to define a very complex condition. Do not worry, VS is a conditional text input box also provides automatic complement function. As a result, typing in the condition box is as convenient as it is in the editor. The following figure.
figure: Automatic completion of conditional text boxes (IntelliSense in condition textbox)I almost explained all the contents of the conditional breakpoint. Except for the following point. There are two options in the criteria window:
is True
has ChangedWe have seen
' is True 'The purpose of the option.
"has Changed"Use to stop when you want to make certain values change to something specific.
3.4 Import/Export Breakpoints (Import/export breakpoint)
3.5 Breakpoint Hit count (breakpoint Hit count)
3.6 Breakpoint When Hit
3.7 Breakpoint Filter (Breakpoint filter)You can limit breakpoints to only specific processes or threads. This is useful when you are debugging multithreaded threads. Right-click the breakpoint selection
"Filter"You can open the Filter window.
Figure: Breakpoint Filter (Breakpoint filter)In the filter rule, you can set the process name, process ID, machine name, thread ID, and so on. I'll detail the usage in the Multithreaded debugging section.
4 Data sticky Note (database Tip)A data note is a high-level sticky note message that is used to view objects and variables during application debugging. When the debugger executes to a breakpoint, moves the mouse over the object or the variable, and you see the current value. You can even see the details of some complex objects (such as dataset,datatable, etc.). The upper-left corner of the data note has a "+" number to expand its child objects or values.
figure: Data pad for debugging (datatips During debugging)A few months ago, I sent an article about VS DataTip debugging tips. The following are some of the features that are useful when debugging.
4.1 Pin Inspect Value During Debugging
4.2 drag-drop Pin Data Tip
4.3 adding Comments
4.4 Last session debugging Value
4.5 Import Export Data Tips
4.6 Change Value Using Data Tips
4.7 Clear Data Tips
5 Monitoring window (Watch windows)
5.1 Local variables (Locals)Lists all variables in the current method. When the debugger stops at a particular breakpoint and opens the Autos window, the variables associated with this value in the current scope are displayed.
figure: Local Variables
5.2 Automatic window (autos)These variables are automatically detected by the VS debugger when debugging. VS detects the object or variable associated with the current statement, based on which the autos variable is listed. The shortcut key for autos variable is CTRL + D + A.
figure: Autos-ctrl + D, A
5.3 Monitoring (Watch)The Watch window is used to add variables. You can add any number of variables. Add the method by right-clicking the variable and selecting "Add to Watch".
figure: Watch-ctrl + D, WYou can also use drag-and-drop (Drag and drop) to add variables to the Watch window. To remove a variable from the Watch window, right-click the variable and select Delete Watch. You can also edit the values of these variables at run time through the Debug window.
There are 4 monitoring windows that can be used at the same time.
Figure: Multiple Watch WindowsIf the variable contains an object instance, there is a "+" number on the left to view the properties and members of the object.
figure: Expanding monitoring variables
5.3.1 Creating Object IDThe Visual Studio debugger provides another powerful feature, which enables us to create an object ID (ID) for any particular instance of an object. This can be used to monitor arbitrary objects at any time, even if the object is outside the scope (scope). In the Watch Window (Watch Window), right-click a specific object variable, and then click
Make Object ID"Creates an object ID.
figure: Creating an Object IDAfter you create an object ID for a specific object variable, Visual studio adds a digital and "#" number to the object to represent.
figure: After adding an object ID
5.4
6 Instant Windows (Immediate window)The Immediate window is a common feature for developers. It can modify the value of a variable or execute some statements without altering the current debugging steps. We can go through the menu
debugging > Windows > Instant (Debug > Window > Immediate window)Open the Immediate window. The Immediate window supports a set of commands that can be performed at any time of debugging. It also supports IntelliSense. During debugging, we can execute any command or code statement in the Immediate window.
figure: Basic Immediate Window (Immediate windows)This is the most common feature for all developers, so I'm not going to go through every single command in the Immediate window.
7 invocation stack (call stack)
8 Debugging multi-thread program (Debugging multithreaded Program)
8.1 Exploring Threads Window
8.2 Flag Just my Code
8.3 Break point Filter-multithread debugging
9 Debug Parallel programs (Debugging Parallel program)
9.1 Parallel Task and Parallel Stacks
ten debugging with IntelliTrace
10.1 Overview
10.2 Mapping with IntelliTrace
10.3 Filter IntelliTrace Data
11 Debugging common shortcut keys (useful shortcut keys for VS debugging)
Shortcut Keys |
Descriptions |
Ctrl-alt-v, A |
Displays the Auto window |
Ctrl-alt-b |
Displays the breakpoints dialog |
Ctrl-alt-c |
Displays the call Stack |
Ctrl-shift-f9 |
Clears all of the breakpoints in the project |
Ctrl-f9 |
Enables or disables the breakpoint on the current line of code |
Ctrl-alt-e |
Displays the Exceptions dialog |
Ctrl-alt-i |
Displays the Immediate window |
Ctrl-alt-v, L |
Displays the Locals window |
Ctrl-alt-q |
Displays the Quick Watch dialog |
Ctrl-shift-f5 |
Terminates the current debugging sessions, rebuilds if necessary, and starts a new debugging session. |
Ctrl-f10 |
Starts or resumes execution of your code and then halts the execution when it reaches the selected. |
Ctrl-shift-f10 |
Sets the "execution" to "line of code" you choose |
Alt-num * |
Highlights the Next Statement |
F5 |
If not currently debugging, this runs the startup project or projects and attaches the debugger. |
Ctrl-f5 |
Runs the code without invoking the debugger |
F11 |
Step Into |
Shift-f11 |
Executes the remaining lines out from procedure |
F10 |
Executes the ' next line ' of code but does ' not ' step to any function calls |
Shift-f5 |
Available in broke and run modes, this terminates the debugging session |
Ctrl-alt-h |
Displays the Threads window to view all of the Threads for the current process |
F9 |