简体   繁体   English

Eclipse Java条件观察点

[英]Eclipse Java Conditional Watchpoint

Eclipse supports conditional breakpoints which break at a particular line when the condition is true. Eclipse支持条件断点,条件断点在条件为真时会在特定行处中断。

It also supports watchpoints which break immediately when a given variable is accessed/modified, regardless of the line that caused the modification. 它还支持观察点,这些观察点将在访问/修改给定变量时立即中断,而不考虑引起修改的行。

How would I create a conditional watchpoint such that it breaks at whichever line a given variable is modified AND the condition is true? 我将如何创建条件观察点,以使其在修改给定变量且条件为真的任何行处中断?

For example: set a MODIFY watchpoint on a variable X so that it only breaks when X is set to a value > 1000 例如:在变量X上设置MODIFY监视点,以便仅在X设置为> 1000时才中断

EDIT: Say a field can be changed from many different methods, potentially in different packages. 编辑:说一个字段可以从许多不同的方法更改,可能在不同的程序包中。 One could conceivably create a conditional breakpoint at each of these locations to break when the condition is true. 可以想像,可以在这些位置的每个位置创建一个条件断点,以在条件为真时中断。 However, the number of places this variable is touched can be numerous and placing that make breakpoints is rather inefficient (from a human standpoint). 但是,触摸此变量的位置数可能很多,并且从断点来看,造成断点的位置效率很低。 An alternative is watchpoints which automatically breaks any time the field is modified, regardless of where it was modified from in code. 另一种选择是观察点,它可以在字段被修改的任何时间自动中断,而不管代码中的修改位置如何。 However, I don't know of a way to only have watchpoints break when a condition is met. 但是,我不知道只有在满足条件时才会中断观察点的方法。

在这种情况下,为什么不只创建一个条件断点“ true时暂停”并将其设置为条件

X > 1000 && (your other condition)

I'm not sure if what you want is possible within the breakpoint options or not - however a simple workaround would be: 我不确定断点选项内是否可能实现您想要的东西-但是一个简单的解决方法是:

To add a new variable which holds the origianl value, then use it to create a breakpoint check with your desired affect. 要添加一个保存origianl值的新变量,然后使用它来创建具有所需效果的断点检查。

int originalValue = value;

Then inside the breakpoint: 然后在断点内:

value != originalValue && value > 1000

This breakpoint will then trigger when the value has changed and it is greater than 1000. 然后,当值更改并且大于1000时,将触发此断点。

EDIT: If you are trying to find the random location within your code where such a change could happen, then there is only one way I would suggest: 编辑:如果您试图在代码中查找可能发生这种变化的随机位置,那么我只建议一种方法:

Make the variable in question private and force all references to it do go through getter/setter methods. 将有问题的变量设为private并强制通过getter / setter方法对它进行所有引用。 Then you only need to add a breakpoint to the setter method, then you'll very quickly find what you are looking for. 然后,您只需要在setter方法中添加一个断点,即可快速找到所需的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM