初步了解 Visual Studio 调试器First look at the Visual Studio Debugger

本主题介绍 Visual Studio 提供的调试器工具。This topic introduces the debugger tools provided by Visual Studio. 在 Visual Studio 上下文中,当调试应用时,这通常意味着你在附加了调试器的情况下(即在调试器模式下)运行应用程序。In the Visual Studio context, when you debug your app, it usually means that you are running the application with the debugger attached (that is, in debugger mode). 执行此操作时,调试器在运行过程中可提供许多方法让你查看代码的情况。When you do this, the debugger provides many ways to see what your code is doing while it runs. 你可以逐步执行代码、查看变量中存储的值、设置对变量的监视以查看值何时改变、检查代码的执行路径等。如果这是你第一次尝试调试代码,建议在学习本主题之前阅读零基础调试You can step through your code and look at the values stored in variables, you can set watches on variables to see when values change, you can examine the execution path of your code, et al. If this is the first time that you've tried to debug code, you may want to read Debugging for absolute beginners before going through this topic.

此处所述的功能适用于 C#、C++、Visual Basic、JavaScript 和 Visual Studio 支持的其他语言(除非另有说明)。The features described here are applicable to C#, C++, Visual Basic, JavaScript, and other languages supported by Visual Studio (except where noted).

设置断点并启动调试器Set a breakpoint and start the debugger

要进行调试,需要在调试器附加到应用进程的情况下启动应用。To debug, you need to start your app with the debugger attached to the app process. F5(“调试”>“开始调试”)是执行该操作最常见的方法 。F5 (Debug > Start Debugging) is the most common way to do that. 但是,现在你可能没有设置任何断点来检查应用代码,因此我们首先设置断点再开始调试。However, right now you may not have set any breakpoints to examine your app code, so we will do that first and then start debugging. 断点是可靠调试的最基本和最重要的功能。Breakpoints are the most basic and essential feature of reliable debugging. 断点指示 Visual Studio 应在哪个位置挂起你的运行代码,以使你可以查看变量的值或内存的行为,或确定代码的分支是否运行。A breakpoint indicates where Visual Studio should suspend your running code so you can take a look at the values of variables, or the behavior of memory, or whether or not a branch of code is getting run.

若代码编辑器中打开了文件,则可通过单击代码行左侧的边缘来设置断点。If you have a file open in the code editor, you can set a breakpoint by clicking in the margin to the left of a line of code.

设置断点Set a Breakpoint

按 F5(“调试”>“开始调试”)或调试工具栏中的“开始调试”按钮 开始调试,调试器将运行至它遇到的第一个断点 。Press F5 (Debug > Start Debugging) or the Start Debugging button Start Debugging in the Debug Toolbar, and the debugger runs to the first breakpoint that it encounters. 如果应用尚未运行,则按 F5 会启动调试器并在第一个断点处停止。If the app is not yet running, F5 starts the debugger and stops at the first breakpoint.

当你知道要详细检查的代码行或代码段时,断点功能非常有用。Breakpoints are a useful feature when you know the line of code or the section of code that you want to examine in detail.

我们为大多数命令提供键盘快捷键,便于你更快地移动浏览应用代码。We provide the keyboard shortcuts for most commands because they make navigation of your app code quicker. (括号中显示了等效命令,如菜单命令。)(Equivalent commands such as menu commands are shown in parentheses.)

要在附加了调试器的情况下启动应用,请按 F11(“调试”>“单步执行”) 。To start your app with the debugger attached, press F11 (Debug > Step Into). F11 是“单步执行”命令,每按一次,应用就执行下一个语句。F11 is the Step Into command and advances the app execution one statement at a time. 使用 F11 启动应用时,调试器会在执行的第一个语句上中断。When you start the app with F11, the debugger breaks on the first statement that gets executed.

F11 单步执行F11 Step Into

黄色箭头表示调试器暂停处的语句,它还在同一点上暂停应用执行(此语句尚未执行)。The yellow arrow represents the statement on which the debugger paused, which also suspends app execution at the same point (this statement has not yet executed).

F11 是一种以最详尽方式检查执行流的好方法。F11 is a good way to examine the execution flow in the most detail. (为了更快地浏览代码,我们还向你展示了一些其他选项。)默认情况下,调试器会跳过非用户代码(如果需要更多详细信息,请参阅仅我的代码)。(To move faster through code, we show you some other options as well.) By default, the debugger skips over non-user code (if you want more details, see Just My Code).

备注

在托管代码中将看到一个对话框,询问你是否希望在自动跳过属性和运算符时收到通知(默认行为)。In managed code, you will see a dialog box asking if you want to be notified when you automatically step over properties and operators (default behavior). 若稍后想更改设置,请在“调试”下的“工具”>“选项”菜单中禁用“单步跳过属性和运算符”设置 。If you want to change the setting later, disable Step over properties and operators setting in the Tools > Options menu under Debugging.

单步跳过代码以跳过函数Step over code to skip functions

如果所在的代码行是函数或方法调用,则可以按 F10(“调试”>“单步跳过”)而不是 F11 。When you are on a line of code that is a function or method call, you can press F10 (Debug > Step Over) instead of F11.

按 F10 将使调试器前进,但不会单步执行应用代码中的函数或方法(代码仍将执行)。F10 advances the debugger without stepping into functions or methods in your app code (the code still executes). 按 F10 可跳过你不感兴趣的代码。By pressing F10, you can skip over code that you're not interested in. 这样就可以快速转到更感兴趣的代码。This way, you can quickly get to code that you are more interested in.

单步执行属性Step into a property

如前所述在默认情况下,调试器会跳过托管属性和字段,但通过“单步执行特定内容”命令可替代此行为。As mentioned earlier, by default the debugger skips over managed properties and fields, but the Step Into Specific command allows you to override this behavior.

右键单击属性或字段,选择“单步执行特定内容”,然后选择一个可用选项。Right-click on a property or field and choose Step Into Specific, then choose one of the available options.

Visual Studio 调试器的屏幕截图,其中突出显示了一个代码行。

在此示例中,通过“单步执行特定内容”将转到 Path.set 的代码。In this example, Step Into Specific gets us to the code for Path.set.

显示 Path.set 代码的 Visual Studio 调试器屏幕截图。

使用鼠标快速运行到代码中的某个点Run to a point in your code quickly using the mouse

在调试器中,将鼠标悬停在代码行上,直到“运行到单击处”(将执行运行到此处)按钮 Visual Studio 调试器中的“运行到单击处”按钮的屏幕截图。While in the debugger, hover over a line of code until the Run to Click (Run execution to here) button Screenshot of the Run to Click button from the Visual Studio Debugger. 此按钮指示执行应该运行到按钮所在的行。The button indicates that execution should run to the line where the button is placed. 显示在左侧。appears on the left.

Visual Studio 调试器的屏幕截图,其中显示“运行到单击处”按钮出现在 Update 函数的调用的左侧。

备注

Visual Studio 2017Visual Studio 2017 起,可用使用“运行到单击位置”(将执行运行到此处)按钮。The Run to Click (Run execution to here) button is available starting in Visual Studio 2017Visual Studio 2017.

单击“运行到单击处”(将执行运行到此处)按钮。Click the Run to Click (Run execution to here) button. 调试器将前进到单击的代码行。The debugger advances to the line of code where you clicked.

使用此按钮类似于设置临时断点。Using this button is similar to setting a temporary breakpoint. 此命令对于快速到达应用代码的可见区域也很方便。This command is also handy for getting around quickly within a visible region of app code. 你可在任何打开的文件中使用“运行到单击处”。You can use Run to Click in any open file.

使调试器从当前函数中跳出Advance the debugger out of the current function

有时你可能希望继续调试会话,但在整个当前函数中一直使调试器前进。Sometimes, you might want to continue your debugging session but advance the debugger all the way through the current function.

按 Shift+F11(或“调试”>“单步跳出”) 。Press Shift + F11 (or Debug > Step Out).

此命令将恢复应用执行(并使调试器前进),直到当前函数返回。This command resumes app execution (and advances the debugger) until the current function returns.

运行到光标处Run to cursor

编辑代码(而不是在调试器中暂停)时,右键单击应用中的代码行,然后选择“运行到光标处”(或按 Ctrl 到 F10)。 When you are editing code (rather than paused in the debugger), right-click a line of code in your app and choose Run to Cursor (or press Ctrl to F10). 此命令将启动调试并在当前代码行上设置临时断点。This command starts debugging and sets a temporary breakpoint on the current line of code.

运行到光标处Run to Cursor

如果设置了断点,则调试器会在其命中的第一个断点处暂停。If you have set breakpoints, the debugger pauses on the first breakpoint that it hits.

按 F5,直至到达在其上选择了“运行到光标处”的代码行 。Press F5 until you reach the line of code where you selected Run to Cursor.

当编辑代码并希望快速设置临时断点并同时启动调试器时,此命令很有用。This command is useful when you are editing code and want to quickly set a temporary breakpoint and start the debugger at the same time.

备注

调试时可使用“调用堆栈”窗口中的“运行到光标处” 。You can use Run to Cursor in the Call Stack window while you are debugging.

快速重启应用Restart your app quickly

单击调试工具栏中的“重启”按钮重启应用(或按 Ctrl+Shift+F5) 。Click the Restart Restart App button in the Debug Toolbar (or press Ctrl + Shift + F5).

当你按下“重启”时,与停止应用并重启调试器相比,它节省了时间。When you press Restart, it saves time versus stopping the app and restarting the debugger. 调试器在执行代码命中的第一个断点处暂停。The debugger pauses at the first breakpoint that is hit by executing code.

若确实要停止调试器并返回到代码编辑器,可以按红色停止停止调试按钮而不是“重启”。If you do want to stop the debugger and get back into the code editor, you can press the red stop Stop Debugging button instead of Restart.

编辑代码并继续调试(C#、VB、C++、XAML)Edit your code and continue debugging (C#, VB, C++, XAML)

在 Visual Studio 支持的大多数语言中,你都可以在调试会话的过程中编辑代码,然后继续调试。In most languages supported by Visual Studio, you can edit your code in the middle of a debugging session and continue debugging. 要使用此功能,请先在调试器中暂停,用鼠标点击进入代码,进行编辑,然后按 F5F10F11 键继续调试。To use this feature, click into your code with your cursor while paused in the debugger, make edits, and press F5, F10, or F11 to continue debugging.

编辑并继续调试Edit and continue debugging

有关功能使用和功能限制的详细信息,请参阅编辑并继续For more information on using the feature and on feature limitations, see Edit and Continue.

若要在调试会话期间修改 XAML 代码,请参阅使用 XAML 热重载编写和调试正在运行的 XAML 代码To modify XAML code during a debugging session, see Write and debug running XAML code with XAML Hot Reload.

使用数据提示检查变量Inspect variables with data tips

既然你已经有了粗略的了解,那么就有机会开始使用调试器检查应用状态(变量)。Now that you know your way around a little, you have a good opportunity to start inspecting your app state (variables) with the debugger. 那些允许你检查变量的功能是调试器的一些最有用的功能,并且有不同方法可执行此操作。Features that allow you to inspect variables are some of the most useful features of the debugger, and there are different ways to do it. 通常,当尝试调试问题时,你试图找出变量是否存储了你期望它们在特定应用状态具有的值。Often, when you try to debug an issue, you are attempting to find out whether variables are storing the values that you expect them to have in a particular app state.

在调试器中暂停时,将鼠标悬停在对象上并看到其默认属性值(在此示例中,文件名 market 031.jpg 是默认属性值)。While paused in the debugger, hover over an object with the mouse and you see its default property value (in this example, the file name market 031.jpg is the default property value).

查看数据提示View a Data Tip

展开对象以查看其所有属性(例如本示例中的 FullPath 属性)。Expand the object to see all its properties (such as the FullPath property in this example).

通常,在调试时,你需要快速检查对象的属性值,数据提示是一种实现此目的的好方法。Often, when debugging, you want a quick way to check property values on objects, and the data tips are a good way to do it.

提示

在大多数受支持的语言中,可在调试会话中途编辑代码。In most supported languages, you can edit code in the middle of a debugging session. 有关详细信息,请参阅编辑并继续For more info, see Edit and Continue.

使用“自动”和“局部变量”窗口检查变量Inspect variables with the Autos and Locals windows

调试时,查看代码编辑器底部的“自动”窗口。While debugging, look at the Autos window at the bottom of the code editor.

“自动”窗口Autos Window

在“自动”窗口中,可看到变量及其当前值和类型。In the Autos window, you see variables along with their current value and their type. “自动”窗口显示当前行或前一行使用的所有变量(在 C++ 中,该窗口显示前三个代码行中的变量。The Autos window shows all variables used on the current line or the preceding line (In C++, the window shows variables in the preceding three lines of code. 查看文档以了解特定于语言的行为)。Check documentation for language-specific behavior).

备注

在 JavaScript 中,支持“局部变量”窗口,但不支持“自动”窗口 。In JavaScript, the Locals window is supported but not the Autos window.

接下来,查看“局部变量”窗口。Next, look at the Locals window. “局部变量”窗口显示当前范围中的变量。The Locals window shows you the variables that are currently in scope.

“局部变量”窗口Locals Window

在此示例中,this 对象和 f 对象处于范围内。In this example, the this object and the object f are in scope. 有关详细信息,请参阅在“自动”窗口和“局部变量”窗口中检查变量For more info, see Inspect Variables in the Autos and Locals Windows.

设置监视Set a watch

可使用“监视”窗口指定要关注的变量(或表达式)。You can use a Watch window to specify a variable (or an expression) that you want to keep an eye on.

在调试时,右键单击对象并选择“添加监视”。While debugging, right-click an object and choose Add Watch.

“监视”窗口Watch Window

在本示例中,你在 f 对象上设置了监视,当在调试器中移动时,可看到其值发生了变化。In this example, you have a watch set on the f object, and you can see its value change as you move through the debugger. 与其他变量窗口不同,“监视”窗口始终显示正在监视的变量(当超出范围时,它们会变灰)。Unlike the other variable windows, the Watch windows always show the variables that you are watching (they're grayed out when out of scope).

有关详细信息,请参阅使用“监视”窗口和“快速监视”窗口设置监视For more info, see Set a Watch using the Watch and QuickWatch Windows

检查调用堆栈Examine the call stack

调试时单击“调用堆栈”窗口,默认情况下,该窗口在右下方窗格中打开。Click the Call Stack window while you are debugging, which is by default open in the lower right pane.

检查调用堆栈Examine the Call Stack

“调用堆栈”窗口显示方法和函数被调用的顺序。The Call Stack window shows the order in which methods and functions are getting called. 最上面一行显示当前函数(此示例中的 Update 方法)。The top line shows the current function (the Update method in this example). 第二行显示 Update 是从 Path.set 属性调用的,依此类推。The second line shows that Update was called from the Path.set property, and so on. 调用堆栈是检查和理解应用执行流的好方法。The call stack is a good way to examine and understand the execution flow of an app.

备注

“调用堆栈”窗口类似于某些 IDE(如 Eclipse)中的调试透视图。The Call Stack window is similar to the Debug perspective in some IDEs like Eclipse.

可双击代码行来查看该源代码,这也会更改调试器正在检查的当前范围。You can double-click a line of code to go look at that source code and that also changes the current scope being inspected by the debugger. 此操作不会使调试器前进。This does not advance the debugger.

还可使用“调用堆栈”窗口中的右键单击菜单执行其他操作。You can also use right-click menus from the Call Stack window to do other things. 例如,你可将断点插入到指定的函数中,使用“运行到光标处”重启应用,然后检查源代码。For example, you can insert breakpoints into specific functions, restart your app using Run to Cursor, and to go examine source code. 请参阅如何:检查调用堆栈See How to: Examine the Call Stack.

检查异常Examine an exception

应用引发异常时,调试器会将你转至引发异常的代码行。When your app throws an exception, the debugger takes you to the line of code that threw the exception.

异常帮助程序Exception Helper

在此示例中,异常帮助程序向你显示 System.Argument 异常以及一条错误消息,指出该路径不是合法形式。In this example, the Exception Helper shows you a System.Argument exception and an error message that says that the path is not a legal form. 因此,我们知道方法或函数参数发生了错误。So, we know the error occurred on a method or function argument.

在此示例中,DirectoryInfo 调用在存储于 value 变量中的空字符串上引发了错误。In this example, the DirectoryInfo call gave the error on the empty string stored in the value variable.

异常帮助程序是帮助调试错误的好功能。The Exception Helper is a great feature that can help you debug errors. 你还可以执行其他操作,如查看错误详细信息及从异常帮助程序添加监视。You can also do things like view error details and add a watch from the Exception Helper. 或者,如有需要可更改引发特定异常的条件。Or, if needed, you can change conditions for throwing the particular exception. 有关如何在代码中处理异常的详细信息,请参阅调试技术和工具For more information on how to handle exceptions in your code, see Debugging techniques and tools.

备注

异常帮助程序取代了从 Visual Studio 2017Visual Studio 2017 开始提供的异常情况助手。The Exception Helper replaced the Exception Assistant starting in Visual Studio 2017Visual Studio 2017.

展开“异常设置”节点以查看有关如何处理此异常类型的更多选项,但对于本教程无需更改任何内容!Expand the Exception Settings node to see more options on how to handle this exception type, but you don't need to change anything for this tour!

配置调试Configure debugging

可以将要生成的项目配置为调试或发布配置,配置项目属性进行调试,或配置常规设置进行调试。You can configure your project to build as a Debug or Release configuration, configure project properties for debugging, or configure general settings for debugging. 此外,还可以将调试器配置为使用 DebuggerDisplay 特性或 NatVis 框架(对于 C/C++)等功能显示自定义信息。In addition, you can configure the debugger to display custom information using features such as DebuggerDisplay attribute or, for C/C++, the NatVis framework.

调试属性特定于每个项目类型。Debugging properties are specific to each project type. 例如,在启动应用程序后,可以指定要传递给应用程序的参数。For example, you can specify an argument to pass to the application when you start it. 在解决方案资源管理器中,右键单击项目并选择“属性”,可以访问特定于项目的属性。You can access the project-specific properties by right-clicking the project in Solution Explorer and selecting Properties. 调试属性通常显示在“生成”或“调试”选项卡中,具体取决于特定的项目类型。Debugging properties typically appear in the Build or Debug tab, depending on the particular project type.

项目属性Project properties

在 Azure 应用服务中调试实时 ASP.NET 应用Debug live ASP.NET apps in Azure App Service

当执行感兴趣的代码时,Snapshot Debugger 会为生产中的应用拍摄快照。the Snapshot Debugger takes a snapshot of your in-production apps when code that you are interested in executes. 若要指示该调试器拍摄快照,可以在代码中设置快照点和记录点。To instruct the debugger to take a snapshot, you set snappoints and logpoints in your code. 通过该调试器,可精确查看出错的内容,而不会影响生产应用程序的流量。The debugger lets you see exactly what went wrong, without impacting traffic of your production application. Snapshot Debugger 有助于大幅减少解决生产环境中出现的问题所需的时间。The Snapshot Debugger can help you dramatically reduce the time it takes to resolve issues that occur in production environments.

启动 Snapshot DebuggerLaunch the snapshot debugger

快照集合适用于在 Azure 应用服务中运行的 ASP.NET 应用程序。Snapshot collection is available for ASP.NET applications running in Azure App Service. ASP.NET 应用程序必须在 .NET Framework 4.6.1 或更高版本上运行,并且 ASP.NET Core 应用程序必须在 Windows 上的 .NET Core 2.0 或更高版本上运行。ASP.NET applications must be running on .NET Framework 4.6.1 or later, and ASP.NET Core applications must be running on .NET Core 2.0 or later on Windows.

有关详细信息,请参阅使用 Snapshot Debugger 调试实时 ASP.NET 应用For more information, see Debug live ASP.NET apps using the Snapshot Debugger.

使用 IntelliTrace 后退查看快照 (Visual Studio Enterprise)View snapshots with IntelliTrace step-back (Visual Studio Enterprise)

IntelliTrace 后退会在每个断点处及调试器步骤事件发生时自动拍摄应用程序的快照。IntelliTrace step-back automatically takes a snapshot of your application at every breakpoint and debugger step event. 凭借记录的快照便可以返回到上一个断点或步骤,并查看当时应用程序的状态。The recorded snapshots enable you to go back to previous breakpoints or steps and view the state of the application as it was in the past. 如果希望查看以前的应用程序状态,但不想重新启动调试或重新创建所需应用状态,使用 IntelliTrace 后退可以节省时间。IntelliTrace step-back can save you time when you want to see the previous application state but don't want to restart debugging or recreate the desired app state.

可以通过使用调试工具栏中的“后退”和前进”按钮浏览和查看快照。 You can navigate and view snapshots by using the Step Backward and Step Forward buttons in the Debug toolbar. 这些按钮用于浏览“诊断工具”窗口中“事件”选项卡上显示的事件。 These buttons navigate the events that appear in the Events tab in the Diagnostic Tools window.

“后退”和“前进”按钮Step Backward and Forward Buttons

有关详细信息,请参阅使用 IntelliTrace 检查上一应用状态页。For more information, see the Inspect previous app states using IntelliTrace page.

调试性能问题Debug performance issues

如果应用运行速度太慢或使用了太多内存,你可能需要尽早使用分析工具来测试应用。If your app runs too slowly or uses too much memory, you may need to test your app with the profiling tools early on. 有关分析工具(例如 CPU 使用情况工具和内存分析器)的详细信息,请参阅首先查看分析工具For more information about profiling tools such as the CPU Usage tool and the Memory Analyzer, see First look at the profiling tools.

后续步骤Next steps

在本教程中,你已快速了解了许多调试器功能。In this tutorial, you've had a quick look at many debugger features. 你可能会需要更深入地了解其中一个功能,例如断点。You may want a more in-depth look at one of these features, such as breakpoints.