Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conditional breakpoint by caller in Java eclipse

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;
        }
    }
like image 618
Radium Avatar asked Jan 17 '13 14:01

Radium


People also ask

How do I apply a conditional breakpoint in Eclipse?

First, set a breakpoint at a given location. Then, use the context menu on the breakpoint in the left editor margin or in the Breakpoints view in the Debug perspective, and select the breakpoint’s properties. In the dialog box, check Enable Condition, and enter an arbitrary Java condition, such as list.

How do you do a conditional breakpoint?

To set a conditional breakpoint, activate the context menu in the source pane, on the line where you want the breakpoint, and select “Add Conditional Breakpoint”. You'll then see a textbox where you can enter the expression. Press Return to finish.

How do you create a breakpoint in Eclipse?

To define a breakpoint in your source code, right-click in the left margin in the Java editor and select Toggle Breakpoint. Alternatively, you can double-click on this position. The Breakpoints view allows you to delete and deactivate Breakpoints and modify their properties.

How do I fix Java exception breakpoint in Eclipse?

You can fix this immediately by opening the Markers view and delete the Java Exception Breakpoints. However, to permanently remove this type of breakpoints, you have to go to the Java Debug options and uncheck the "Suspend excecution on uncaught exceptions" option.


1 Answers

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

like image 145
artbristol Avatar answered Sep 21 '22 00:09

artbristol