view truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/debug/Breakpoint.java @ 22255:e7643754d982

Truffle/Instrumentation: rename Instrument to ProbeInstrument to distinguish from another kind to be added.
author Michael Van De Vanter <michael.van.de.vanter@oracle.com>
date Fri, 25 Sep 2015 14:58:35 -0700
parents dc83cc1f94f2
children 1348cc2e084e
line wrap: on
line source

/*
 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.  Oracle designates this
 * particular file as subject to the "Classpath" exception as provided
 * by Oracle in the LICENSE file that accompanied this code.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */
package com.oracle.truffle.api.debug;

import java.io.IOException;

import javax.sound.midi.Instrument;

import com.oracle.truffle.api.instrument.Probe;
import com.oracle.truffle.api.source.Source;

/**
 * Breakpoint in a {@link com.oracle.truffle.api.vm.TruffleVM} with
 * {@link com.oracle.truffle.api.debug debugging turned on}. You can ask
 * {@link Debugger#setLineBreakpoint(int, com.oracle.truffle.api.source.LineLocation, boolean)} or
 * {@link Debugger#setTagBreakpoint(int, com.oracle.truffle.api.instrument.SyntaxTag, boolean)} to
 * create an instance of {@link Breakpoint}.
 */
@SuppressWarnings("javadoc")
public abstract class Breakpoint {

    /**
     * A general model of the states occupied by a {@link Breakpoint} during its lifetime.
     */
    public enum State {

        /**
         * Not attached, enabled.
         * <p>
         * Created for a source location but not yet attached: perhaps just created and the source
         * hasn't been loaded yet; perhaps source has been loaded, but the line location isn't
         * probed so a breakpoint cannot be attached. Can be either enabled or disabled.
         */
        ENABLED_UNRESOLVED("Enabled/Unresolved"),

        /**
         * Not attached, disabled.
         * <p>
         * Created for a source location but not yet attached: perhaps just created and the source
         * hasn't been loaded yet; perhaps source has been loaded, but the line location isn't
         * probed so a breakpoint cannot be attached.
         */
        DISABLED_UNRESOLVED("Disabled/Unresolved"),

        /**
         * Attached, instrument enabled.
         * <p>
         * Is currently implemented by some {@link Instrument}, which is attached to a {@link Probe}
         * at a specific node in the AST, and the breakpoint is enabled.
         */
        ENABLED("Enabled"),

        /**
         * Attached, instrument disabled.
         * <p>
         * Is currently implemented by some {@link Instrument}, which is attached to a {@link Probe}
         * at a specific node in the AST, and the breakpoint is disabled.
         */
        DISABLED("Disabled"),

        /**
         * Not attached, instrument is permanently disabled.
         */
        DISPOSED("Disposed");

        private final String name;

        State(String name) {
            this.name = name;
        }

        public String getName() {
            return name;
        }

        @Override
        public String toString() {
            return name;
        }

    }

    private final boolean isOneShot;

    private int ignoreCount;

    private int hitCount;

    private State state;

    Breakpoint(State state, int ignoreCount, boolean isOneShot) {
        this.state = state;
        this.isOneShot = isOneShot;
        this.ignoreCount = ignoreCount;
    }

    /**
     * Enables or disables this breakpoint's AST instrumentation. The breakpoint is enabled by
     * default.
     *
     * @param enabled <code>true</code> to activate the instrumentation, <code>false</code> to
     *            deactivate the instrumentation so that it has no effect.
     */
    public abstract void setEnabled(boolean enabled);

    /**
     * Is this breakpoint active?
     */
    public abstract boolean isEnabled();

    /**
     * Sets the condition on this breakpoint, {@code null} to make it unconditional.
     *
     * @param expr if non{@code -null}, a boolean expression, expressed in the guest language, to be
     *            evaluated in the lexical context at the breakpoint location.
     * @throws IOException if condition is invalid
     * @throws UnsupportedOperationException if the breakpoint does not support conditions
     */
    public abstract void setCondition(String expr) throws IOException;

    /**
     * Gets the string, expressed in the Guest Language, that defines the current condition on this
     * breakpoint; {@code null} if this breakpoint is currently unconditional.
     */
    public String getCondition() {
        return null;
    }

    /**
     * Does this breakpoint remove itself after first activation?
     */
    public final boolean isOneShot() {
        return isOneShot;
    }

    /**
     * Gets the number of hits left to be ignored before halting.
     */
    public final int getIgnoreCount() {
        return ignoreCount;
    }

    /**
     * Change the threshold for when this breakpoint should start causing a break. When both an
     * ignore count and a {@linkplain #setCondition(String) condition} are specified, the condition
     * is evaluated first: if {@code false} it is not considered to be a hit. In other words, the
     * ignore count is for successful conditions only.
     */
    public final void setIgnoreCount(int ignoreCount) {
        this.ignoreCount = ignoreCount;
    }

    /**
     * Number of times this breakpoint has reached, with one exception; if the breakpoint has a
     * condition that evaluates to {@code false}, it does not count as a hit.
     */
    public final int getHitCount() {
        return hitCount;
    }

    /**
     * Disables this breakpoint and removes any associated instrumentation; it becomes permanently
     * inert.
     */
    public abstract void dispose();

    /**
     * Gets a human-sensible description of this breakpoint's location in a {@link Source}.
     */
    public abstract String getLocationDescription();

    public final State getState() {
        return state;
    }

    final void assertState(State s) {
        assert state == s;
    }

    final void setState(State state) {
        this.state = state;
    }

    /**
     * Assumes that all conditions for causing the break have been satisfied, so increments the
     * <em>hit count</em>. Then checks if the <em>ignore count</em> has been exceeded, and if so
     * returns {@code true}. If not, it still counts as a <em>hit</em> but should be ignored.
     *
     * @return whether to proceed
     */
    final boolean incrHitCountCheckIgnore() {
        return ++hitCount > ignoreCount;
    }

    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder(getClass().getSimpleName());
        sb.append(" state=");
        sb.append(getState() == null ? "<none>" : getState().getName());
        if (isOneShot()) {
            sb.append(", " + "One-Shot");
        }
        if (getCondition() != null) {
            sb.append(", condition=\"" + getCondition() + "\"");
        }
        return sb.toString();
    }
}