Home  >  Article  >  Backend Development  >  Summary of Visual Studio debugging tips

Summary of Visual Studio debugging tips

高洛峰
高洛峰Original
2017-02-07 11:13:461304browse

Debugging is an important part of the software development cycle. It’s challenging, confusing and vexing at the same time. In general, for slightly larger programs, debugging is inevitable. In recent years, the development of debugging tools has made many debugging tasks easier and more time-saving.

1 Hover the mouse to see the expression value

Visual Studio调试技巧汇总

Debugging can be challenging. For example, you can see where the error occurred by running step by step within the function, and you can know who called the function by viewing the stack information, etc.

But in any case, it is very troublesome to view the values ​​of expressions and local variables (put the expressions and local variables in the watch window).

A simpler method is to stop the mouse on the data you want to view. If it is a class or structure, you can click to expand it to quickly and easily view its fields.

2 Change variable values ​​during operation

Visual Studio调试技巧汇总

# The debugger is not only a tool to analyze program crashes and strange behaviors, but also can inspect data through step-by-step debugging Many bugs are solved by solving the problem of whether the behavior is as expected by the program. Sometimes, you wonder if you could set certain conditions to be true so that your program would run correctly. In fact, you just move the mouse over the variable, double-click the value, and enter the value you need. This way there is no need to modify the code or restart the program.

3 Set the next running position

Visual Studio调试技巧汇总

A typical debugging case is that we often use the step-by-step debugging method to analyze why the function went wrong. At this time, you encounter an error returned by this function calling other functions, and this error is not what you want. What should you do? Restart debugger? Here is a better way, just drag the yellow running position arrow to the running position you want. In fact, it means skipping the intermediate running code and going directly to the desired location. It's very simple.

4 Edit and continue running

Visual Studio调试技巧汇总

When running a very complex program and plug-in, an error is found, but you don’t want to waste time recompiling and restarting program.

It’s very simple, just modify the bug at this location and continue debugging. Visual studio will modify the program so that you can continue debugging without restarting the program.

It is worth noting that the "Edit and Continue" function has several limitations. One, it doesn't work on 64-bit code. If you want to use this feature, go to the compilation options in the project settings and select "x86" as the target platform. Don't worry, the release configuration of this target platform is separate from "debug", which means it is still the setting of "Any CPU". Second, the "Edit and continue running" function only applies to changes within a function. If you want To change the declaration of this function or add a new method, you can only choose to restart the program or continue without making any changes. If the modified method contains a lambda expression, it means that the delegate type automatically generated by the compiler has been modified, so Will cause the compiler to stop running.

5 A convenient viewing window

Visual Studio调试技巧汇总

# Most modern debuggers have a viewing window. The view window is very easy to use. You can easily add and delete variables. Just click on a blank line in the window, enter the expression and press the enter key, or click on the expression and press the delete key to delete the unnecessary expression.

In the debugging window, you can not only view ordinary variable values, you can even enter $handles to track the number of open handles, and $err to view the error code of the function (then use Tools->Error to view the description of the error code) or Type @eax (@rax on 64-bit) to view the register value containing the function return value

 6 Comment disassembly

Visual Studio调试技巧汇总 Use the internal disassembly. The assembly function makes it easier to optimize local code. Visual studio can display assembly instructions under each line of your code, and can debug assembly code step by step, and set breakpoints at any location similar to c++ ##.

# 7 Thread window of stack information

Visual Studio调试技巧汇总

Debugging multi-threaded code is painful. Might be interesting too. It depends on your debugger. A great feature of Visual studio is to view the stack information of threads in the thread window. You can easily see all threads and their stack information directly.

 8 Conditional Breakpoint

Visual Studio调试技巧汇总

If you want to reproduce a small probability event, but the breakpoint will trigger under a large number of unnecessary conditions. You can easily set conditional breakpoints. Set the breakpoint condition in the breakpoint window, and Visual studio will automatically ignore breakpoints that do not meet the conditions.

 9 Memory Window

Visual Studio调试技巧汇总

Some bugs are caused by incorrect structure definitions, lack of alignment attributes, etc. Viewing the contents of each line of memory makes it easy to locate and resolve these bugs. Visual studio's memory window can translate 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

Visual Studio调试技巧汇总

#If you are solving a bug in the code written by others, you will encounter "What is this type?" "What does this function do?" "What" and other issues, you can use visual studio's jump to definition command to view the definition of the type or function.

 11 Command Window

Visual Studio调试技巧汇总

This little trick was suggested by chaau and it can save you a lot of time. Visual studio supports a command window, which you can open through the menu View->Other Windows->Command Window. You can enter different commands in the window to automate debugging. For example, you can test MFC's COleDateTime variable with a very simple command.

The above are 11 Visual Studio debugging skills shared with you. I hope it will be helpful to your learning.

For more articles related to the summary of Visual Studio debugging skills, please pay attention to the PHP Chinese website!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn