10

I am trying to track a change of a value using watchpoint in a Java program in Eclipse debugger. The class hierarchy is pretty complex and the value I am tracking is wrapped in container, which is used on many places.

To be more specific, there is a container SizeRequirement, which has a property minimum, which I am tracking. This class is used by many layout managers on many places for many components to define requirement for component's sizes. I need to catch exact call, where the value changes/is set for one specific layout manager and one specific component in it. Is it possible to filter breakpoints by caller? I will try to explain the problem using some abstract code:

class ValueContainer {
  public String value;
}

class A {

  private ValueContainer valueContainer;

  public A () {
    valueContainer = new ValueContainer();
    valueContainer.value = "setByA";
  }

}

class B {

  private ValueContainer valueContainer;

  public B () {
    valueContainer = new ValueContainer();
    valueContainer.value = "setByB";
  }

}

I set a watchpoint on value and I only want breakpoint to suspend only when the value is set by class A and ignore calls by B.

To make things worse, class SizeRequirement is part of swing library and is deeply integrated in code, so I cannot use inheritance to replace it by some child on some exact place where I want to track it.

EDIT

So this is what I used as conditional breakpoint condition. Believe or not, it works. :)

    StackTraceElement[] arr = Thread.currentThread().getStackTrace();
    boolean contains = false;
    for(StackTraceElement e : arr) {
        if (e.getClassName().contains("A")) {
            contains = true;
            break;
        }
    }
Radium
  • 564
  • 3
  • 17

2 Answers2

6

It's pretty disgusting and probably slow, but you can use

Thread.currentThread().getStackTrace()[2].getClassName().contains("A")

as your breakpoint condition.

Based on this bug https://bugs.eclipse.org/bugs/show_bug.cgi?id=72961 I don't think Eclipse will support it directly

artbristol
  • 32,010
  • 5
  • 70
  • 103
  • That doesn't suspend the VM, but will print a stack trace whenever that piece of code is executed. – Sentry Jan 17 '13 at 15:08
  • Of course, this will return you true/false value, if name of caller class contains "A". I need more general statement - my class can be anywhere on the stack, not only as a direct caller. And then you can use it as a statement for conditional breakpoint, then VM will suspend. Unfortunately watchpoint cannot be conditional, but I am still able to track all places in code, where the value changes using watchpoint and I can put conditional breakpoints there. So this suggestion is very useful. I will do some more experimenting. Thanks! – Radium Jan 17 '13 at 17:06
  • @Radium it should be easy enough to loop through the entire stack trace looking for the class, and of course you don't need to use `contains` - use `equals` if you have the full name of the class. (Make yourself a static method and call that if you can't fit the code into a one-liner) – artbristol Jan 17 '13 at 17:07
0

Eclipse can do that. Create a breakpoint, then go into the Debug perspective and into the "Breakpoints" view. Select the breakpoint and

There you can say enable "Conditional" and select "Suspend when value changes"

You then have to enter your value in the field below, in your case probably "minimum".

Edit:

I just tried that and it didn't work as expected, but I've found another solution that might work:

data breakpoints in java/eclipse

In summary: Create a breakpoint in the line where your field is defined, it will become a watchpoint. In the "Breakpoint Properties" you can specify it to only suspend on change (the symbol on the margin will be a pen), not on access (symbol will be a pair of glasses).

Community
  • 1
  • 1
Sentry
  • 4,102
  • 2
  • 30
  • 38
  • To your first post, this is little bit misunderstanding. The value is not set to "minimum", but itself is named 'minimum', like `public int minimum = 0`. And I need to suspend, when the value is set by some other object of exact class. To your edit, yes, I am trying to use a watchpoint, as you suggested. Problem is, that the value is changed very often by many objects and program is suspended even when I dont want to. Thanks for your time anyways! – Radium Jan 17 '13 at 16:18
  • I do understand that `minimum` is the name of the field, but it somehow slipped my attention that you only want to catch changes from a certain class. I thought is was only about "change" vs "access". So yes, I think that artbristol gave the correct answer. – Sentry Jan 18 '13 at 09:24