Visual Studio debug series 9 debugger tips and skills

Series catalog [The latest development articles have been updated, click to view details]
01 Fixed data tips

If you are in When debugging, hover the mouse over the data tip frequently, and you may want to fix the data tip of the variable so that you can check it at any time. Even after restarting, the fixed variables can remain intact. To pin the data tip, click the pin icon when hovering over it. You can fix multiple variables.

share picture

02 Edit the code and continue debugging (C#, VB, C++)

Most supported in Visual Studio In the language, you can edit the code during the debugging session, and then continue debugging. To use this function, please pause in the debugger first, click with the mouse to enter the code, edit it, and then press F5, F10 or F11 to continue debugging.

Edit and continue debuggingFor more information about function usage and function limitations, please refer to edit and continue.

03 debugging Problems that are difficult to reproduce

If it is difficult or time-consuming to re-implement a specific state in the application, consider using conditional breakpoints. You can use conditional breakpoints and filter them to avoid damage Apply the code until the application enters the required state (for example, the state where the variable is storing wrong data). You can use expressions, filters, hit counts, etc. to set Condition.

Create conditional breakpoints

  1. Right-click the breakpoint icon (red ball), and select the condition.

  2. < li>

    In the breakpoint setting window, type an expression.

    Conditional BreakpointIf you are interested in another type of condition, please select the filter instead of the conditional expression in the breakpoint setting dialog box, and then follow the prompts of the filter.< /span>

04 Configure the data to be displayed in the debugger

About C#, Visual Basic and C++ (C++ only/CLI code ), you can enable the debugger to use the following options to display the information DebuggerDisplay property. In the relevant C++ code, you can perform the same visualization using Natvis.

05 Change execution flow

Hold the debugger on a certain line of code, and grab the yellow arrow pointer on the left with the mouse. Move the yellow arrow pointer to other points in the code execution path. Then use the F5 key or step commands to continue running the application.

Move execution pointerBy changing the execution flow, you can test different code execution paths or re-run the code without restarting the debugger.

06 Track out-of-scope objects (C#, Visual Basic)

< span style="font-family:'Microsoft YaHei'; font-size: 14px;" data-ttu-id="99350-140">Variables can be easily viewed through a debugger window (such as a monitor window). However, if the variable exceeds the scope of the monitoring window, you may notice that it turns gray. In some application scenarios, if the variable is out of range, the value of the variable may even change, so you may need to pay close attention to it (for example, the variable may be treated as garbage). You can create an object ID for the variable in the monitoring window to track this variable.

Create object ID

  1. Set a breakpoint near the variable to be tracked.

  2. Start the debugger (F5) and stop at the breakpoint.

  3. Find the variable in the local variables window (Debug> Window> Local Variables), right-click the variable, and select Create Object ID.

    create Object IDYou should see in the “local variables” window set a breakpoint to interrupt The execution of the instruction or line returned by the calling function. This variable is the object id.

  4. Right-click the object ID variable and select Add Watch.< /span>

For details, please refer to the created object ID.

07 View the return value of the function

< div>

To view the return value of the function, please execute the code step by step , Check the function displayed in the automatic window. To check the return value of the function, please make sure that the function you are concerned about has been executed (if the function call is currently stopped, Please click the F10 key).< span data-ttu-id="99350-155">If the window is closed, please open the automatic window through Debug> Window> Automatic Window.

automatic windowIn addition, you can also enter a function in the immediate window to view the return value. (Through the debugging> window> Open this immediately Window.)

Instant WindowIn addition, you can also use pseudo variables in the monitoring and immediate windows, such as $ReturnValue.

08 Check the string in the visualizer

When using strings, it would be helpful if you can see the complete, formatted string . To view plain text, XML, HTML, or JSON strings, hover your mouse over the variable containing the string value and click the magnifying glass iconVisualizerIconOpen String Visualizerstring visualization tool can help you determine Whether the format of the string is correct depends on the type of the string. For example, if the value field is empty, it means that the visualizer type does not recognize the string. For more information, please refer to the String Visualizer dialog box.

JSON String VisualizerFor several other types such as data sets and DataTable objects displayed in the debugger window, you can also open the built-in visualization tools.

div>

09 Break code at the handled exception

The debugger will break the code at the unhandled exception. However, it has been handled Exceptions (such as exceptions that occur within the try/catch block) may also cause errors, which may require further investigation. You can configure the debugger To interrupt the code at the handled exception, the method is to configure the options in the exception settings dialog box. To open this dialog box, select Debug>Window>Exception Settings.

Through the exception setting dialog box, you can let the debugger interrupt the code at a specific exception. In the figure below, the debugger will occur System.NullReferenceException interrupt the code. For more information, see Manage exceptions.

share picture

10 Dead debugging Locks and race conditions

If the problem that needs to be debugged is common for multi-threaded applications, it is usually helpful to check the position of the thread when debugging. This operation can be done easily by using the Show thread button in the source.

share picture

Show threads in source code

  1. When debugging, click the Show thread button in the source Show threads in sourceView the scroll bar on the left side of the window .In this line, you can see the Thread Marker icon Thread MarkerThe thread tag indicates that the thread stops at this position.

    Note that thread markers may be incompletely obscured by breakpoints.

  2. Hover the pointer over the thread marker. The data prompt will be displayed on the screen. The data prompt will tell you the name and thread ID of each stopped thread.

    You can also view the position of the thread in the parallel stack window.

11 Check the payload of web services and network resources (UWP)

In UWP applications, you can analyze and use Windows.Web.Http The network operation performed by the API. You can use this tool to help debug web services and network resources. To use this tool, please select Debug> Performance Profiler. Select the network, and then select Start. In the application, browse the application scenarios that use Windows.Web.Http, and then choose to stop collecting and generate reports.

Network usage analysis ToolsSelect an operation in the summary view to see more detailed information.

Detailed Information in the Network Usage ToolFor more information, please See network usage.

12 Attach the debugger to your application

To attach to a running application, the debugger will be loaded as Symbol (.pdb) file generated by the same internal version. In some cases, it is very useful to know some knowledge about symbol files. You can check how Visual Studio loads symbol files in the module window.

During debugging, open the module window by selecting Debug>Window>Module. The module window can tell you which modules the debugger considers as user code or my code, and the status of the symbol loading module. In most cases, the debugger will automatically find the symbol file for the user code, but if you want to single-step trace (or debug) the .NET framework code, system code Or third-party library code, you must perform other steps to obtain the correct symbol file.

View symbol information in the module windowYou can directly right-click in the module window and select Load Symbol to load symbol information.

Sometimes, the application released by the application developer does not contain matching symbol files (in order to reduce the space occupied) , But will keep a matching symbol file for the internal version for future debugging and release versions.

To learn how the debugger distinguishes user codes, please refer to My Code Only. To learn about symbols For details of the file, please refer to Specifying Symbols (.pdb) and Source Files in the Visual Studio Debugger.

The series catalog [The latest development article has been updated, click to view details]

The series catalog [The latest development article has been updated, click to view details] 】

01 Fixed data prompt

If you often hover the mouse on the data tip when debugging, you may want to fix the data tip of the variable so that you can check it at any time. Even after restarting, the fixed variables can remain intact. To pin the data tip, click the pin icon when hovering over it. You can fix multiple variables.

share picture

02 Edit the code and continue debugging (C#, VB, C++)

< p>

In most languages ​​supported by Visual Studio, you You can edit the code during the debugging session and then continue debugging. To use this function, please pause in the debugger first, click with the mouse to enter the code, edit it, and then press F5, F10 or F11 to continue debugging.

Edit and continue debuggingFor more information about function usage and function limitations, please refer to edit and continue.

03 Debug difficult-to-reproduce issues< /strong>

If in the application It is difficult or time-consuming to re-implement a specific state. You can consider using conditional breakpoints. You can use conditional breakpoints and filter them to avoid breaking the application code until the application Enter the desired state (for example, the state where the variable is storing wrong data). You can use expressions, filters, hits, etc. to set conditions.

Create conditional breakpoints

  1. Right-click the breakpoint icon (red ball), and select the condition.

  2. In the breakpoint setting window, type an expression.

    Conditional BreakpointIf you are interested in another type of condition, please select the filter instead of the conditional expression in the breakpoint setting dialog box, and then follow the prompts of the filter.< /span>

04 Configure the data to be displayed in the debugger

< span style="font-family:'Microsoft YaHei'; font-size: 14px;" data-ttu-id="99350-128">For C#, Visual Basic and C++ (C++ only/CLI code), you can debug The program needs to use the following options to display the information DebuggerDisplay attribute. In the relevant C++ code, you can perform the same visualization using Natvis.

05 Change execution flow

Let the debugger pause on a certain line of code, use the mouse to grab the yellow arrow pointer on the left. Place the yellow arrow pointer Move to other points in the code execution path. Then use the F5 key or step commands to continue running the application. p>

Move execution pointerBy changing the execution flow, you can test different code execution paths or re-run the code without restarting the debugger.

06 Track objects outside the scope (C#, Visual Basic)

Variables can be easily viewed through a debugger window (such as a watch window). However, if the variable exceeds the scope of the monitoring window, you may notice that it turns gray. In some application scenarios, if the variable is out of range, the value of the variable may even change, so you may need to pay close attention to it (for example, the variable may be treated as garbage). You can create an object ID for the variable in the monitoring window to track this variable.

Create object ID

  1. Set a breakpoint near the variable to be tracked.

  2. Start the debugger (F5) and stop at the breakpoint.

  3. Find the variable in the local variables window (Debug> Window> Local Variables), right-click the variable, and select Create Object ID.

    create Object IDYou should see in the "local variables" window set a breakpoint to interrupt The execution of the instruction or line returned by the calling function. This variable is the object id.

  4. Right-click the object ID variable and select Add Watch.< /span>

For details, please refer to the created object ID.

07 View the return value of the function

To view the return value of the function, please check the automatic window when executing the code step by step To view the return value of the function, please make sure that the function you are concerned about has been executed (if the function call is currently stopped, please press the F10 key ). If the window is closed, please debug > Window> Auto Window Open Auto Window.

automatic windowIn addition, you can also enter a function in the immediate window to view the return value. (Through the debugging> window> Open this immediately Window.)

Instant WindowIn addition, you can also use pseudo variables in the monitoring and immediate windows, such as $ReturnValue.

08 Check the string in the visualizer

When using strings, it would be helpful if you can see the complete, formatted string. To view plain text, XML, HTML, or JSON strings, hover your mouse over the variable containing the string value and click the magnifying glass iconVisualizerIconOpen String Visualizerstring visualization tool can help you determine Whether the format of the string is correct depends on the type of the string. For example, if the value field is empty, it means that the visualizer type does not recognize the string. For more information, please refer to the String Visualizer dialog box.

JSON String VisualizerFor several other types such as data sets and DataTable objects displayed in the debugger window, you can also open the built-in visualization tools.

p>

09 Break code at the handled exception

The debugger will interrupt the code at unhandled exceptions. However, handled exceptions (e.g. code>try/catch Exceptions that occur within the block) may also cause errors and may require further investigation. The debugger can be configured to The code is interrupted at the exception by configuring the options in the Exception Settings dialog box. To open this dialog box, please select Debug> Window> Exception Settings.< /span>

Through the exception setting dialog box, you can let the debugger interrupt the code at a specific exception. In the figure below, the debugger will occur System.NullReferenceException When the code is interrupted. Yes For details, see Manage exceptions.

share picture

10 Debug deadlocks and race conditions

If you need to debug The problem is very common for multi-threaded applications, it is usually helpful to check the position of the thread when debugging. This operation can be done easily by using the Show thread button in the source.

share picture

Show threads in source code

  1. When debugging, click the Show thread button in the source Show thread in sourceView the scroll bar on the left side of the window .In this line, you can see the Thread Marker icon Thread MarkerThe thread tag indicates that the thread stops at this position.

    Note that thread markers may be incompletely obscured by breakpoints.

  2. Hover the pointer over the thread marker. The data prompt will be displayed on the screen. The data prompt will tell you the name and thread ID of each stopped thread.

    You can also view the position of the thread in the parallel stack window.

11 检查 web 服务和网络资源 (UWP) 的有效负载

在 UWP 应用中,你可以分析使用 Windows.Web.Http API执行的网络操作。 可以使用此工具来帮助调试 web 服务和网络资源。 若要使用该工具,请选择调试 > 性能探查器。 选择网络,然后选择启动。 在应用中,浏览使用 Windows.Web.Http 的应用场景,然后选择 停止收集 生成报表。

网络使用情况分析工具在摘要视图中选择一个操作,查看更多详细信息。

网络使用情况工具中的详细信息有关详细信息,请参阅网络使用情况。

12 将调试器附加到您的应用程序

若要附加到正在运行的应用,调试器将加载为想要调试的应用的相同内部版本生成的符号 (.pdb) 文件。 在某些情况下,了解符号文件的一些知识非常有用。 你可在模块窗口中检查 Visual Studio 如何加载符号文件。

在调试时,通过选择调试 > 窗口 > 模块 打开模块窗口。 模块窗口可以告诉你,调试器将哪些模块视为用户代码或我的代码,以及符号加载模块的状态。 在大多数情况下,调试器会自动为用户代码查找符号文件,但如果你想要单步跟踪 (或调试).NET framework 代码、系统代码或第三方库代码,必须执行其他步骤获取正确的符号文件。

在模块窗口中查看符号信息你可以直接在模块窗口中右键单击并选择加载符号来加载符号信息。

有时,应用开发人员发布的应用不包含匹配的符号文件 (为了减少占用的空间),但会为内部版本保留一份匹配的符号文件,用于以后调试发布版本。

了解如何调试器如何区分用户代码,请参阅仅我的代码。 若要了解有关符号文件的详细信息,请参阅在 Visual Studio 调试器中指定符号 (.pdb) 和源文件。

 

系列目录     【已更新最新开发文章,点击查看详细】

Leave a Comment

Your email address will not be published.