11 Visual Studio debugging tips
Debugging is an important part of the software development cycle. Sometimes it is challenging, sometimes it confuses the programmer and sometimes it drive the programmer crazy! Even so, debugging is inevitable. In recent years, the development of debugging tools has made many debugging tasks a lot simpler.
This article summarizes 11 debugging tips that can save you a lot of time when you use Visual Studio.
1. Hover the mouse to see the expression
Debugging is sometimes challenging. When you step into a function and want to see which block is wrong, look at the call stack to think about where the value comes from. In other cases, you need to add some monitoring expressions or view the list of local variables, which usually takes some time, however. If you point your mouse at a variable you are interested in. You will find things much simpler. Furthermore, classes and structures can be expanded with a single click. such. You can quickly and easily find the variable you want to view.
2. Change the value in real-time
The debugger is not just a tool for analyzing program crashes or abnormal results. Many bugs can be prevented by stepping into a newly written function and checking whether the function works as expected. Sometimes you may be wondering, "Will the function run correctly if the condition is true?" In most cases, you don't need to change the code to restart it. Just hover your mouse over a variable, double-click the value and enter a new value Too.
3. Set the next statement
A typical debugging situation is to analyze why a function call fails through single-step tracking. What do you do when you find that one function calls another function and returns an error? Restart debugging? There is a better way. Drag the yellow statement mark to the statement you want to execute next. For example, the piece you just failed, and then stepped in. Simple, isn't it?
4. Edit and continue
When debugging a complex program or a plug-in, an error is found in a function that is called many times. But don't want to waste time stopping, recompiling and re-debugging. No problem, just correct the code there and continue to step through. VS will correct the program and continue debugging without restarting
Note that there are many known limitations to editing and then continuing. First, a 64-bit code will not work. If he works for your C # program. Just go to the build option of the project settings, and then the target platform is x86. Don't worry. The target platform of the release version and the time of debugging are separate. It can be set to any platform.
Editing and then continuing to change should be local in a method. If you change the method signature, add some new methods or classes. You will have to restart the program. Or undo the changes to continue. The change method also includes an automatically generated proxy class that is implicitly modified by the lambda expression, so it cannot continue.
5. Convenient monitoring window
Probably modern debuggers have a watch window, anyway. VS allows you to simply add or remove variables. Click the blank line, enter your expression and press Enter, or press Delete on an unnecessary expression to delete it.
You can not only see the "normal" variables from the monitoring window. You can enter $
handles to track how many handles your program has opened (you can easily fix memory leaks), enter $ err
to see the error code of the previous function, and then use the tool-error message to see a more detailed description. Also try typing @eax (64-bit @rax) to view the register containing the function return value.
6. Annotated disassembly
Using interactive disassembly mode can make it easy to optimize key parts of the program. VS gives assembly instructions corresponding to each line of your code and runs a single step. At the same time, you can set a breakpoint at any location. Moreover, the viewing and modification of expressions are the same as in C ++ code.
7. Thread window with a stack
Debugging multi-threaded programs is painful. Or it can be very interesting. It depends on your debugger. The really beautiful feature is the stack view of the thread window, and you can easily overview the thread through the call stack of the window.
8. Conditional breakpoints
Not really mind blowing, but definitely something we couldn't leave off the list. If you try to reproduce a rare event through a breakpoint, the situation raises some serious errors. You can add conditional breakpoints. Define a breakpoint condition, and then if the condition is not true, VS will ignore the breakpoint
9. Memory window
Some bugs are caused by incorrect structure definitions or ignored alignment attributes. You can locate and fix the bug by viewing the content in the memory. VS provides a memory window, which can display the value in the form of 8, 16, 32 or 64 bits and also supports floating-point values. It also allows us to change them in real-time, just like in a text editor.
10. Go to definition
This feature is not directly about debugging, but about browsing large projects. If you try to find some errors in the code that you did not write yourself, quickly knowing "what is this type" or "what is this function" can save a lot of time, VS is convenient for you by a go-to definition command.
11. Command Window
The eleventh trick can save a lot of time. VS supports command windows, which can be started through View > Other Windows > Command Window. Here you can execute code directly while the debugging.