22

I'm debugging some legacy code where we have a cached object that appears to be changed externally.

If I know the object identifier for it (while debugging), is there some way to "watch" it so that if some other code in the same thread or another thread attempts to modify its state it'll trigger my debug?

I can't use just an expression watch for it since there may be references to that object elsewhere and as there are many instances of the same class.

Uri
  • 88,451
  • 51
  • 221
  • 321
  • Can you just set breakpoints at each of it's methods? Are there too many? – matt b Nov 10 '09 at 18:02
  • of course put a conditional breakpoint in each of the methods of the object's class that could modify state of the object. The conditional expression being break only of 'this' is the object identifier. – Murali VP Nov 10 '09 at 18:06
  • The properties are manipulated directly (I DID NOT write this code...) – Uri Nov 10 '09 at 18:53

2 Answers2

17

Set a breakpoint in the code you want to stop in when the value changes.

  • Start in the breakpoint view.
  • Select the breakpoint
  • right-click and goto the "breakpoint properties"
  • Check the 'Enable Condition' box
  • in the text field enter the name of the variable to watch
  • select the 'value of condition changes' radio button
Kelly S. French
  • 12,198
  • 10
  • 63
  • 93
  • 1
    I don't understand "you want to stop in when the value changes". So you can put a breakpoint anywhere (even in dead code), and when the value changes you will be stoped at this point ? Can't we say to eclipse "make a breakpoint and stop when the value changes" ? – lmo Dec 19 '16 at 09:54
14

If it is declared somewhere as a class or instance variable (should be, how else could you cache it), then you can also just set a breakpoint on the particular line. It will be called watchpoint and will by default be triggered on access and modification (configureable through breakpoint properties).

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • It is not a variable change but modification to the state of an object that needs to be watched. – Murali VP Nov 10 '09 at 18:16
  • @BallusC: If only whoever wrote this code actually used getters and setters :) – Uri Nov 10 '09 at 18:38
  • 5
    I do not mean getters/setters. I actually meant class/instance variables. Put the breakpoints on variables, not methods. – BalusC Nov 10 '09 at 19:04