为什么我的 C# 调试器会跳过断点?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1199088/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why does my C# debugger skip breakpoints?
提问by
My C# debugger is not working properly. It skips break points and line of codes sometimes. I have checked the configuration manager. I have even tried adding my projects to new solution files. Can someone please help me?
我的 C# 调试器工作不正常。它有时会跳过断点和代码行。我已经检查了配置管理器。我什至尝试将我的项目添加到新的解决方案文件中。有人可以帮帮我吗?
回答by Thomas Levesque
Make sure optimizations are disabled (this is the defaut for the Debug configuration, but they are enabled in Release configuration). Compiler optimizations can mess with the debugger...
确保优化被禁用(这是调试配置的默认设置,但它们在发布配置中启用)。编译器优化可能会干扰调试器...
回答by Marc Gravell
Are you sure that it compiled correctly? It sounds to me like you're debugging against a previous version, which can happen if the build fails (perhaps a code bug, perhaps the files are readonly).
你确定它编译正确吗?在我看来,您正在针对以前的版本进行调试,如果构建失败(可能是代码错误,也许文件是只读的),就会发生这种情况。
回答by Chris S
This sounds like your source code is out of sync with the PDB files. The easiest solution is to clean the solution (which deletes all your dlls from the bin folder). Recompile, and then try stepping through again.
这听起来像是您的源代码与 PDB 文件不同步。最简单的解决方案是清理解决方案(从 bin 文件夹中删除所有 dll)。重新编译,然后再次尝试单步执行。
If it still fails, try closing the solution and deleting the "obj" folders. And then try again.
如果仍然失败,请尝试关闭解决方案并删除“obj”文件夹。然后再试一次。
And also check you are compiling in debug mode- something I've done often ("why isn't it stepping through?!") not getting break point
还要检查您是否在调试模式下编译- 我经常做的事情(“为什么不单步执行?!”)没有得到断点
回答by Justin
My debugging checklist:
我的调试清单:
- Make sure your attaching to the process using the correct code type - if your process has both unmanaged and managed code then dont rely on "auto" to work for you, explicitly state what sort of code your trying to debug
- Goto the modules window (Debug -> Windows -> Modules, you may need to enable it in the "Customize..." menu
- Check to make sure that the assembly your trying to debug has been loaded, and that symbols have been loaded - if they haven't been loaded then right click on that module and select "load symbols"
- Open your code file and place your breakpoint - if it appears with the little warning symbol then look and see what it says,
- You might need to goto "tools -> options - > debugging -> general" and untick "Enable Just My Code (Managed Only)"
- You might also want to uncheck "Require source files to exactly match the original version", if you think your sources might be slightly out (beware however, as this can lead to you debugging with completely the wrong sources, which can be very confusing)
- 确保您使用正确的代码类型附加到流程 - 如果您的流程同时具有非托管和托管代码,则不要依赖“自动”为您工作,明确说明您尝试调试的代码类型
- 转到模块窗口(调试 -> Windows -> 模块,您可能需要在“自定义...”菜单中启用它
- 检查以确保您尝试调试的程序集已加载,并且符号已加载 - 如果尚未加载,则右键单击该模块并选择“加载符号”
- 打开你的代码文件并放置你的断点 - 如果它出现了小警告符号,然后看看它说的是什么,
- 您可能需要转到“工具-> 选项-> 调试-> 常规”并取消选中“仅启用我的代码(仅限托管)”
- 如果您认为您的源代码可能有点过时,您可能还想取消选中“需要源文件与原始版本完全匹配”(但是请注意,因为这可能会导致您使用完全错误的源进行调试,这可能会非常混乱)
On certain cases you might find that your module doesn't get loaded at the point where you attach your debugger (for example if you have some sort of plug in archetecutre, and plugin assemblies are only loaded when you first use them). In these cases all you can do is try and make sure everything is prepared ready for when the module is loaded.
在某些情况下,您可能会发现您的模块不会在您附加调试器时加载(例如,如果您在架构中有某种插件,并且插件程序集仅在您第一次使用它们时加载)。在这些情况下,您所能做的就是尝试确保在加载模块时一切准备就绪。
回答by Brad Bruce
If entire methods are being skipped, look at the source and see if the System.Diagnostics.DebuggerStepThroughAttribute attribute is present.
如果跳过整个方法,请查看源并查看 System.Diagnostics.DebuggerStepThroughAttribute 属性是否存在。
Despite the name, it PREVENTS the debugger from stepping through the method.
尽管名称如此,但它阻止调试器单步执行该方法。
回答by Sz. Moncz
If at least sometimes the break point is hit it means that all the settings are most probably OK.
如果至少有时遇到断点,则意味着所有设置很可能都没有问题。
The missing hits may be caused by some side-effects, for example: property evaluation by the debugger (at least VS skips the breakpoints during the property evaluation for debugger) or some spying tools (but these are usually catch by debugger).
丢失的命中可能是由一些副作用引起的,例如:调试器的属性评估(至少 VS 在调试器的属性评估期间跳过断点)或一些间谍工具(但这些通常被调试器捕获)。
If you think this may be the case, turn off the spying tools and disable the property evaluation by the debugger.
如果您认为可能是这种情况,请关闭监视工具并禁用调试器的属性评估。
This may be sometimes not enough, for example: If your property returns a collection, displaying e.g. a Count()
will evaluate the property - so remove also all references of property from the watch windows, etc.
这有时可能还不够,例如:如果您的属性返回一个集合,则显示例如 aCount()
将评估该属性 - 因此还要从监视窗口中删除所有属性引用等。
回答by Quickee
This worked for me in VS 2017, Go to Tools > Options then under the Options Window go to the Debugging section. Enable - "Enable .NET Framework source stepping"
这在 VS 2017 中对我有用,转到“工具”>“选项”,然后在“选项”窗口下转到“调试”部分。启用 - “启用 .NET Framework 源步进”
回答by Lucas Alves
Disabling "Project Properties/Build/Optimize code" worked for me.
禁用“项目属性/构建/优化代码”对我有用。
回答by Script Kitty
If you're getting this kind of error:
如果您收到此类错误:
The current .NET SDK does not support targeting .NET Core 2.1. Either target .NET Core 2.0 or lower, or use a version of the .NET SDK that supports .NET Core 2.1. DCR_Parser
当前的 .NET SDK 不支持面向 .NET Core 2.1。以 .NET Core 2.0 或更低版本为目标,或使用支持 .NET Core 2.1 的 .NET SDK 版本。DCR_Parser
Right click on the project in solution explorer and click on Properties. Under the Application tab go to Target framework .NET Core 2.0. And save.
右键单击解决方案资源管理器中的项目,然后单击属性。在应用程序选项卡下,转到目标框架 .NET Core 2.0。并保存。