有没有办法有条件地设置或编码断点?
我想知道这个问题有一段时间了 - 有没有办法编码/编程断点......?有条件吗?例如,我可以指定类似“当this变量变为this值时,中断并打开调试器”吗? (非常有用,特别是在长循环中,当您想要调试后期循环值的循环执行时。)
我想这可能是 IDE 特定的,因为调试在不同的 IDE 中实现方式不同......我有兴趣知道如何在任何 IDE 中执行此操作,特别是在 Eclipse 和 Visual Studio 中。
I've been wondering this for a while - is there a way to code/program breakpoints...? Conditionally? For example, can I specify something like - "when this variable becomes this value, break and open the debugger"? (Would be quite useful, especially in long loops when you want to debug loop execution of a late loop value.)
I suppose this may be IDE-specific since debugging is implemented differently in different IDEs... I'd be interested to know how to do this in any IDE, but specifically in Eclipse and Visual Studio.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这在 Visual Studio 中绝对是可能的。通常可以单击左边距插入断点,然后右键单击该断点。右键单击菜单中的选项之一是“条件...”,您可以指定一个谓词,该谓词将告诉调试器仅在满足谓词时在该断点上中断。
This is definitely possible in Visual Studio. You can normally click in the left margin to insert the breakpoint, and then right click on that breakpoint. One of the options in the right click menu is "Condition...", and you can specify a predicate that will tell the debugger only the break on that breakpoint if the predicate is met.
在 Visual Studio 中,您可以以声明方式设置条件断点,这与普通断点类似,但仅在特定条件为真时才会中断。该条件可以使用局部变量以及可从设置断点的位置访问的任何变量。只需右键单击任何断点(红点)并选择“条件...”。
此外,.NET 语言可以调用 Debugger.Break() 方法以编程方式中断执行。这也可以在
if
语句中完成:In Visual Studio, you can declaratively set conditional breakpoint, which are like normal breakpoint but will only break when a certain condition is true. The condition can use local variables and whatever is accessible from where the breakpoint is set. Just right click any breakpoint (red dot) and select "Condition...".
In addition, .NET languages can call the
Debugger.Break()
method to programmatically break the execution. This also can be done within anif
statement:如果您的 IDE 不支持条件断点,请添加 if 语句并在其中中断。
If conditional breakpoints aren't supported by your IDE, add in an if statement and break inside of it.
在 Eclipse 中设置条件断点(感谢所有 Visual Studio 的回答!):
设置断点。右键单击并选择“断点属性...”。选中“启用条件”并在文本区域中输入条件代码。
Setting a conditional breakpoint in Eclipse (thanks for all the Visual Studio answers!):
Set a breakpoint. Right-click and choose "Breakpoint Properties...". Check "Enable Condition" and enter your condition code into the text area.
正是出于这个原因,大多数 IDE 都允许条件断点。在 Visual Studio 中,您可以右键单击页边空白处断点的红点,然后从那里打开条件对话框。您还可以从 Visual Studio 中的断点窗口访问条件对话框。我对 Eclipse 不熟悉。
Most IDEs allow for conditional break points for this very reason. In Visual Studio, you can right click on the red dot for the breakpoint in the margin and open the condition dialog from there. You can also get at the condition dialog from the breakpoint window in Visual Studio. I am not familiar with Eclipse.