IntelliJ Idea (debugging) conditional breakpoint dependent on other breakpoints

后端 未结 2 903
渐次进展
渐次进展 2021-02-07 02:39

I want to set a debug breakpoint in IntelliJ Idea that is only active, if another previous breakpoint was activated. For example i have a breakpoint B1 on line

相关标签:
2条回答
  • 2021-02-07 02:48

    You can do that in the View Breakpoints... view:

    enter image description here

    In your case you will first have to set a conditional breakpoint on B1 so that when it is hit then and only then B2 will be triggered.

    enter image description here

    0 讨论(0)
  • 2021-02-07 03:02

    An alternate programmatic approach to debug specific classes when some condition in some class is met.

    /*
     * Breakpoint helper, stops based on a shared state
     *  STOP variable
     *
     * Everything in here should be chainable
     *  to allow adding to breakpoints
     */
    public final class DEBUG {
    
    /*
     * global state controlling if we should
     *   stop anywhere
     */
    public static volatile boolean STOP = false;
    
    public static volatile List<Object> REFS = new ArrayList<>();
    
    /**
     * add object references when conditions meet
     * for debugging later
     */
    public static boolean ADD_REF(Object obj) {
        return ADD_REF(obj, () -> true);
    }
    
    public static boolean ADD_REF(Object obj, Supplier<Boolean> condition) {
        if (condition.get()) {
            REFS.add(obj);
            return true;
        }
        return false;
    }
    
    /*
     * STOPs on meeting condition
     *  also RETURNS if we should STOP
     *
     * This should be set when a main condition is satisfied
     *      and can be done as part of a breakpoint as well
     */
    public static boolean STOP(Supplier<Boolean> condition) {
        if (condition.get()) {
            STOP = true;
            return true;
        }
        return false;
    }
    
    public static boolean STOP() {
        return STOP(() -> true);
    }
    

    Where you want a condition to set the breakpoint

    Where you want to stop based on a condition

    0 讨论(0)
提交回复
热议问题