11 powerful Visual Studio debugging tips, Visual Studio
Debugging is an important part of the software development cycle. It is challenging and confusing. In general, debugging is inevitable for a slightly larger program. In recent years, the development of debugging tools has made debugging tasks more and more simple and time-saving.
This article summarizes the debugging skills and methods of ten visual studio programs that may save you a lot of time.
1. Hover the mouse over to view the expression value
Debugging is very challenging. For example, you can see where an error occurs when running the function step by step, and view the stack information to know who calls the function...
However, in either case, it is very troublesome to view the expression and local variable values (put the expression and local variable in the watch window ).
A simpler method is to move the cursor over the data to be viewed. If it is a class or structure, click expand to view its fields conveniently and quickly.
2. Change the variable value during running
The debugger is not only a tool for analyzing program crashes and strange behaviors, but also a solution to many bugs by gradually debugging and checking whether data and behavior meet the program's expectations. Sometimes, you may want to set certain conditions to true so that the program can run correctly. In fact, you just need to move the mouse over the variable, double-click the value, and enter the value you need. In this way, you do not need to modify the code and restart the program.
3. Set the next running location
A typical debugging case is that we often use the step-by-step debugging method to analyze why a function error occurs. In this case, you encounter an error returned when this function calls other functions, which is not what you want. What should you do? Restart the debugger? Here is a better way to drag the Yellow running position arrow to the desired running position. In fact, it is to skip the intermediate code and directly go to the desired position. It's easy.
4. Edit and continue running
When running a complicated program and plug-in, an error is found, but you do not want to waste time re-compile and re-start the program.
It's easy. You only need to modify the bug at this location and continue debugging. Visual studio will modify this program so that you can continue debugging without restarting the program.
It is worth noting that there are several restrictions on the "edit and continue running" function. 1. It cannot be used in 64-bit code. If you want to use this function, select "x86" as the target platform for compilation in the Project Settings. Don't worry, the target platform's reslease configuration is separate from "debug", that is, it is still "Any CPU. 2. The "edit and continue running" function only applies to internal changes of a function. If you want to change the declaration of this function or add new methods, you can only restart the program or continue without any changes. If the modified method contains a lambda expression, it means that the delegate type automatically generated by the compiler is modified, which causes the compiler to stop running.
5. A convenient viewing window
Most modern debuggers have Viewing Windows. However, the visual studio display window is very easy to use, you can easily add and delete variables. Just click a blank line in the window, enter the expression, and press enter. Or click the expression and press the delete key to delete unnecessary expressions.
In the debugging window, you can not only view common variable values, but also enter $ handles to track the number of opened handles, $ err: view the Error code of the function (use Tools-> Error to view the description of the Error Code) or input @ eax (@ rax in 64-Bit mode) view the register value that contains the function return value.
6 annotation Disassembly
Using the internal disassembly function makes it easier to optimize local code. Visual studio can display Assembly commands under each line of your code, debug assembly code step by step, or set breakpoints at any location. Viewing and modifying assembly code is similar to c ++.
7. Thread window of stack information
Debugging multi-threaded code is very painful. It may be interesting. It depends on your debugger. A great feature of Visual studio is to view the thread stack information in the thread window. You can easily view all threads and their stack information.
8-condition breakpoint
If you want to reproduce a small probability event, the breakpoint will also be triggered under a large number of unnecessary conditions. You can easily set conditional breakpoints. When this breakpoint condition is set in the breakpoint window, Visual studio automatically ignores non-conforming breakpoints.
9 Memory Window
Some bugs are caused by incorrect structure definitions and missing alignment attributes. It is easy to locate and solve these bugs by viewing the memory content of each row. The Memory Window of Visual studio can translate the data into 8/16/32/64-bit numbers or floating point numbers. You can change the value directly in the editing window.
10 jump to definition
If you are solving a bug in code written by others, you may encounter problems such as "what is this type" and "what is this function, you can use the jump-to-definition command of visual studio to view the definition of a type or function.
11 command window
This tips is recommended by chaau, which can save you a lot of time. Visual studio supports a Command Window, which can be opened through the menu View> Other Windows> Command Window. You can enter different commands in the window to automate debugging. For example, you can use a simple command to test the COleDateTime variable of MFC.