Visual Studio如何调试?VS的调试技巧
调试是软件开发周期中很重要的一部分,对于稍大一点的程序,调试是不可避免的。最近几年,调试工具的发展让很多调试任务变的越来越简单和省时。IT技术网为你总结了可能节省你大量时间的十个visual studio的调试技巧和方法。 1 悬停鼠标查看表达式值 调试是很
调试是软件开发周期中很重要的一部分,对于稍大一点的程序,调试是不可避免的。最近几年,调试工具的发展让很多调试任务变的越来越简单和省时。IT技术网为你总结了可能节省你大量时间的十个visual studio的调试技巧和方法。
1 悬停鼠标查看表达式值
调试是很有挑战性的。比如在函数内逐步运行可以看出哪里出错,查看堆栈信息可以知道函数被谁调用等等。。。
但是无论哪种情况下,查看表达式和局部变量的值都是很麻烦的(把表达式和局部变量放到watch窗口里)。
一种更简单的方法,把鼠标停在所需查看的数据上。如果是类或结构,那么点击展开可以很方便快速地查看其字段。
2 在运行过程中改变变量值
调试器不仅仅是分析程序崩溃和诡异行为的工具,还可以通过逐步调试检查数据和行为是否符合程序预期的方法解决许多bug。有时,你会想是否设置某些条件为真,程序就能正确运行了。其实你只要把鼠标移动到变量上,双击值,然后输入你需要的值。这样就不需要修改代码,重启程序了。
3 设置下一个运行位置
一个典型的调试案例是我们经常会用逐步调试的方法去分析为什么函数出错了。这时你遇到这个函数调用其他函数返回错误,而这个错误不是你想要的,你该怎么办?重启调试器?这里有个更好的方法,直接把黄色的运行位置箭头拖到你想要的运行位置。其实就是跳过中间运行代码,直接到想要的位置。很简单吧。
4 编辑然后继续运行
在运行一个很复杂的程序和插件时,发现一个错误,但是不想浪费时间去重编译重启动程序。
很简单,只要在这个位置修改这个bug,然后继续调试。Visual studio会修改这个程序,使得你可以继续调试而不需要重启程序。
值得注意的是“编辑然后继续运行”这个功能有几个限制。一,它不能在64位代码上使用。如果想使用这个功能,到项目设置里的编译选项,选择”x86”作为目标平台。不要担心,这目标平台在reslease配置是和”debug是分离的,也就是说依然是”Any CPU”的设置。二,“编辑然后继续运行”这个功能仅适用于一个函数内部改变。如果你想要改变这个函数的声明或者增加新的方法,你只能选择重启程序,或者不做任何改变继续。如果修改的方法中包含lambda表达式,则意味着修改了编译器自动生成的委托类型,这样会导致编译器停止运行。
5 一个方便的查看窗口
大部分现代的调试器都有查看窗口。但是,visual studio的查看窗口使用特别简单,你能很方便的增加和删除变量。只要在窗口里点击空白行,输入表达式然后按enter键。或者点击表达式,按delete键删除不需要的表达式。
在调试窗口不仅仅可以查看普通的变量值,甚至可以输入$handles去追踪打开的句柄数量,$err去查看函数的错误代码(然后使用 Tools->Error 查看错误代码的描述)或者输入 @eax(在64位下是@rax )查看包含函数返回值的寄存器值。
6 注释反汇编
使用内部的反汇编功能使得优化局部代码更加简单。Visual studio可以在你的每一行代码下显示汇编指令,并且可以逐步调试汇编代码,也可以在任意位置设置断点。查看和修改汇编代码类似于c++。
7 堆栈信息的线程窗口
调试多线程代码是很痛苦的。或许也是有趣的。这取决于你的调试器。Visual studio 一个很赞的功能就是在线程窗口查看线程的堆栈信息。你能很方便的直接看到所有线程以及他们的堆栈信息。
8 条件断点
如果你想重现一个小概率事件,但是断点在大量不需要的条件下也会触发。你可以很简单的设置条件断点。在断点窗口设置该断点条件,Visual studio 会自动忽略不符合条件的断点。
9 内存窗口
一些bug是由错误的结构定义、缺少对齐属性等原因引起的。查看每行内存的内容很容易定位和解决这些bug. Visual studio 的内存窗口可以把数据翻译成8/16/32/64-bit数字或者浮点数。你可以在编辑窗口直接改变数值。
10 跳转到定义
如果你在解决别人写的代码一个bug,会遇到“这个类型是什么”“这个函数做什么的”之类的问题,你可以使用visual studio的跳转到定义的命令来查看类型或函数的定义。
11 命令窗口
这个小技巧是由chaau建议的,它能节省你大量的时间。Visual studio支持一个命令窗口,你可以通过菜单View->Other Windows->Command Window 打开。你可以在窗口里输入不同命令使调试自动化。比如,可以通过很简单的命令去的测试MFC的COleDateTime变量。
http://blog.csdn.net/CsethCRM/article/details/11564171

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

C++ multi-thread debugging can use GDB: 1. Enable debugging information compilation; 2. Set breakpoints; 3. Use infothreads to view threads; 4. Use thread to switch threads; 5. Use next, stepi, and locals to debug. Actual case debugging deadlock: 1. Use threadapplyallbt to print the stack; 2. Check the thread status; 3. Single-step the main thread; 4. Use condition variables to coordinate access to solve the deadlock.

How to use LeakSanitizer to debug C++ memory leaks? Install LeakSanitizer. Enable LeakSanitizer via compile flag. Run the application and analyze the LeakSanitizer report. Identify memory allocation types and allocation locations. Fix memory leaks and ensure all dynamically allocated memory is released.

This article introduces shortcuts for Go function debugging and analysis, including: built-in debugger dlv, which is used to pause execution, check variables, and set breakpoints. Logging, use the log package to record messages and view them during debugging. The performance analysis tool pprof generates call graphs and analyzes performance, and uses gotoolpprof to analyze data. Practical case: Analyze memory leaks through pprof and generate a call graph to display the functions that cause leaks.

Concurrency testing and debugging Concurrency testing and debugging in Java concurrent programming are crucial and the following techniques are available: Concurrency testing: Unit testing: Isolate and test a single concurrent task. Integration testing: testing the interaction between multiple concurrent tasks. Load testing: Evaluate an application's performance and scalability under heavy load. Concurrency Debugging: Breakpoints: Pause thread execution and inspect variables or execute code. Logging: Record thread events and status. Stack trace: Identify the source of the exception. Visualization tools: Monitor thread activity and resource usage.

Common PHP debugging errors include: Syntax errors: Check the code syntax to make sure there are no errors. Undefined variable: Before using a variable, make sure it is initialized and assigned a value. Missing semicolons: Add semicolons to all code blocks. Function is undefined: Check that the function name is spelled correctly and make sure the correct file or PHP extension is loaded.

Tools for debugging PHP asynchronous code include: Psalm: a static analysis tool that can find potential errors. ParallelLint: A tool that inspects asynchronous code and provides recommendations. Xdebug: An extension for debugging PHP applications by enabling a session and stepping through the code. Other tips include using logging, assertions, running code locally, and writing unit tests.

C++ debugging functions that contain exception handling uses exception point breakpoints to identify exception locations. Use the catch command in gdb to print exception information and stack traces. Use the exception logger to capture and analyze exceptions, including messages, stack traces, and variable values.

The following techniques are available for debugging recursive functions: Check the stack traceSet debug pointsCheck if the base case is implemented correctlyCount the number of recursive callsVisualize the recursive stack
