views:

177

answers:

3

I want jdb (which I'm using via the Eclipse debugger) to break when a variable is assigned some value. I'm not interested in setting a breakpoint at some specific line but rather more generally.

For example, break every time x == null.

Is such a thing achievable?

A: 

Yes, those are called watchpoints, and watchpoints can have watch expressions.

Depending on versions and such, you do this by selecting a variable in the Outline view and right-clicking on it, or in the Variables view, control/click on it.

A context menu will have choices for Add Watch Expression and Edit Watch Expression.

DigitalRoss
I've been playing around with watchpoints but they don't seem to get hit unless I define the watchpoint when the variable is in scope. Can I define a watchpoint and have it enable when the watched variable comes into scope? Is there a way to qualify the watched variable names?
Daniel
A: 

Yes - What you need to setup is a 'Conditional Breakpoint' - this gives you the ability to stop the program execution and step through the debugger when a certain state of the application is reached.

So, let's say you want to jump into a particular point in the execution when a certain condition is fulfilled (as per the image attached), you can do this as follows:

  1. Open your debugger perspective and select the 'BreakPoints' tab

  2. Add a new BreakPoint in the code file - at the appropriate place where you would like to observe the program execution

  3. Then go back to the in the 'Breakpoints' tab, right click on the newly added entry and select 'Breakpoint Properties'

  4. Set the condition upon which it should be activated

alt text

Dinuk
Hi Diunk, Isn't your suggestion is dependent on the condition being satisfied at a particular line number? I'd like a more general solution that obivates the need to identify line numbers. I just want to know when "x" is assigned something, irrespective of the method in which the assignment takes place.
Daniel
Hi Daniel,Your questions states everytime "x" (a variable) gets set a certain value you want the control flow to break . Questions is, where?Well, if you think about it, "x" has to be an instance or local variable. In both contexts you would know exactly which line in your source you would want to observe ,wouldn't you? (the setter, or the local block scope). From that point, you call look at the call stack and backtrack.
Dinuk
Hi Diunk, I was actually trying to debug an issue in a library that isn't mine. The variable is protected and there is a reasonably large type hierarchy extending the base class where it is defined. The access could be in lots of places which is why I wanted a context-free breakpoint. It seems like thSoft and DigitalRoss are correct in their suggestion.
Daniel
Hi Daniel,Thanks for clarifying - I misunderstood the context in which you wanted to observe the variable :-)Best regards,Dinuk
Dinuk
+1  A: 

In the Outline view, you can Toggle Watchpoint.

thSoft