2

Eclipse supports conditional breakpoints which break at a particular line when the condition is true.

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

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.

arcyqwerty
  • 10,325
  • 4
  • 47
  • 84
  • So it is not enough to simply test whether the variable is `> 1000`? Are you able to make changes to the code? – Eric Oct 22 '14 at 07:06
  • Duplicate: https://stackoverflow.com/a/2302945/1367192 – dds Oct 22 '14 at 07:07
  • @dds not quite I think, since conditional breakpoints must be set at a particular line. What I want is the equivalent of setting a conditional breakpoint at all lines that cause modification to the particular variable. – arcyqwerty Oct 22 '14 at 07:29

2 Answers2

1

In this case why not just create a conditional breakpoint "suspend when true" and put as condition

X > 1000 && (your other condition)
Matthias Steinbauer
  • 1,786
  • 11
  • 24
  • Because I don't know at which line it will become true (amongst many files/methods) – arcyqwerty Oct 22 '14 at 07:17
  • @arcyqwerty, so the value change could happen _anywhere_ in the code? So, basically you're trying to find out where it happens?? – Eric Oct 22 '14 at 07:19
  • Yes, where in the code does the variable get modified such that a condition is true. See added note on question. – arcyqwerty Oct 22 '14 at 07:27
0

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.

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.

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. Then you only need to add a breakpoint to the setter method, then you'll very quickly find what you are looking for.

Eric
  • 1,321
  • 2
  • 15
  • 30
  • This requires a breakpoint at a specific place and will not immediately break on all statements that could possibly modify the variable – arcyqwerty Oct 22 '14 at 07:19
  • Have updated it for more clarity in case the watchpoint requirement wasn't clear... – arcyqwerty Oct 22 '14 at 07:24
  • @arcyqwerty, as I mentioned in my EDIT above - if possible you should force the variable to be a private member, then place a breakpoint in the setter method. – Eric Oct 22 '14 at 07:40