2N/A/*
2N/A * CDDL HEADER START
2N/A *
2N/A * The contents of this file are subject to the terms of the
2N/A * Common Development and Distribution License (the "License").
2N/A * You may not use this file except in compliance with the License.
2N/A *
2N/A * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
2N/A * or http://www.opensolaris.org/os/licensing.
2N/A * See the License for the specific language governing permissions
2N/A * and limitations under the License.
2N/A *
2N/A * When distributing Covered Code, include this CDDL HEADER in each
2N/A * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
2N/A * If applicable, add the following below this CDDL HEADER, with the
2N/A * fields enclosed by brackets "[]" replaced with your own identifying
2N/A * information: Portions Copyright [yyyy] [name of copyright owner]
2N/A *
2N/A * CDDL HEADER END
2N/A */
2N/A
2N/A/*
2N/A * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
2N/A * Use is subject to license terms.
2N/A *
2N/A * ident "%Z%%M% %I% %E% SMI"
2N/A */
2N/Apackage org.opensolaris.os.dtrace;
2N/A
2N/Aimport java.io.*;
2N/Aimport java.beans.*;
2N/A
2N/A/**
2N/A * Description of control flow across function boundaries including
2N/A * direction (entry or return) and depth in the call stack. This
2N/A * information is added to {@link ProbeData} instances only when the
2N/A * {@link Option#flowindent flowindent} option is used:
2N/A * <pre><code>
2N/A * Consumer consumer = new LocalConsumer();
2N/A * consumer.open();
2N/A * consumer.setOption(Option.flowindent);
2N/A * ...
2N/A * </code></pre>
2N/A * See the <a
2N/A * href="http://docs.sun.com/app/docs/doc/817-6223/6mlkidlk1?a=view">
2N/A * <b>Examples</b></a> section of the <b>{@code fbt}
2N/A * Provider</b> chapter of the <i>Solaris Dynamic Tracing Guide</i>.
2N/A * <p>
2N/A * Immutable. Supports persistence using {@link java.beans.XMLEncoder}.
2N/A *
2N/A * @see Consumer#setOption(String option)
2N/A * @see Option#flowindent
2N/A *
2N/A * @author Tom Erickson
2N/A */
2N/Apublic final class Flow implements Serializable {
2N/A static final long serialVersionUID = -9178272444872063901L;
2N/A
2N/A /**
2N/A * Indicates direction of flow across a boundary, such as entering
2N/A * or returing from a function.
2N/A */
2N/A public enum Kind {
2N/A /** Entry into a function. */
2N/A ENTRY,
2N/A /** Return from a function. */
2N/A RETURN,
2N/A /** No function boundary crossed. */
2N/A NONE
2N/A }
2N/A
2N/A static {
2N/A try {
2N/A BeanInfo info = Introspector.getBeanInfo(Flow.class);
2N/A PersistenceDelegate persistenceDelegate =
2N/A new DefaultPersistenceDelegate(
2N/A new String[] {"kind", "depth"})
2N/A {
2N/A /*
2N/A * Need to prevent DefaultPersistenceDelegate from using
2N/A * overridden equals() method, resulting in a
2N/A * StackOverFlowError. Revert to PersistenceDelegate
2N/A * implementation. See
2N/A * http://forum.java.sun.com/thread.jspa?threadID=
2N/A * 477019&tstart=135
2N/A */
2N/A protected boolean
2N/A mutatesTo(Object oldInstance, Object newInstance)
2N/A {
2N/A return (newInstance != null && oldInstance != null &&
2N/A oldInstance.getClass() == newInstance.getClass());
2N/A }
2N/A
2N/A protected Expression
2N/A instantiate(Object oldInstance, Encoder out)
2N/A {
2N/A Flow flow = (Flow)oldInstance;
2N/A return new Expression(oldInstance, oldInstance.getClass(),
2N/A "new", new Object[] { flow.getKind().name(),
2N/A flow.getDepth() });
2N/A }
2N/A };
2N/A BeanDescriptor d = info.getBeanDescriptor();
2N/A d.setValue("persistenceDelegate", persistenceDelegate);
2N/A } catch (IntrospectionException e) {
2N/A e.printStackTrace();
2N/A }
2N/A }
2N/A
2N/A /** @serial */
2N/A private final Kind kind;
2N/A /** @serial */
2N/A private final int depth;
2N/A
2N/A /**
2N/A * Creates a {@code Flow} instance with the given flow kind and
2N/A * depth. Supports XML persistence.
2N/A *
2N/A * @param flowKindName name of enumeration value indicating the
2N/A * direction of flow
2N/A * @param flowDepth current depth in the call stack
2N/A * @throws IllegalArgumentException if there is no {@code Flow.Kind}
2N/A * value with the given name or if the given {@code flowDepth} is
2N/A * negative
2N/A * @throws NullPointerException if the given {@code Flow.Kind} name
2N/A * is {@code null}
2N/A */
2N/A public
2N/A Flow(String flowKindName, int flowDepth)
2N/A {
2N/A kind = Enum.valueOf(Kind.class, flowKindName);
2N/A depth = flowDepth;
2N/A if (depth < 0) {
2N/A throw new IllegalArgumentException("depth is negative");
2N/A }
2N/A }
2N/A
2N/A /**
2N/A * Gets the direction of the flow of control (entry or return)
2N/A * across a function boundary.
2N/A *
2N/A * @return non-null flow kind indicating direction of flow (entry or
2N/A * return) across a function boundary
2N/A */
2N/A public Kind
2N/A getKind()
2N/A {
2N/A return kind;
2N/A }
2N/A
2N/A /**
2N/A * Gets the current depth in the call stack.
2N/A *
2N/A * @return A non-negative sum of the function entries minus the
2N/A * function returns up until the moment described by this control
2N/A * flow instance. For example, if the traced flow of control
2N/A * entered two functions but only returned from one, the depth is
2N/A * one (2 entries minus 1 return).
2N/A */
2N/A public int
2N/A getDepth()
2N/A {
2N/A return depth;
2N/A }
2N/A
2N/A /**
2N/A * Compares the specified object with this {@code Flow} instance for
2N/A * equality. Defines equality as having the same flow kind and
2N/A * depth.
2N/A *
2N/A * @return {@code true} if and only if the specified object is of
2N/A * type {@code Flow} and both instances have equal flow kind and
2N/A * depth.
2N/A */
2N/A @Override
2N/A public boolean
2N/A equals(Object o)
2N/A {
2N/A if (o instanceof Flow) {
2N/A Flow f = (Flow)o;
2N/A return ((kind == f.kind) && (depth == f.depth));
2N/A }
2N/A return false;
2N/A }
2N/A
2N/A /**
2N/A * Overridden to ensure that equal instances have equal hash codes.
2N/A */
2N/A @Override
2N/A public int
2N/A hashCode()
2N/A {
2N/A int hash = 17;
2N/A hash = (37 * hash) + kind.hashCode();
2N/A hash = (37 * hash) + depth;
2N/A return hash;
2N/A }
2N/A
2N/A private void
2N/A readObject(ObjectInputStream s)
2N/A throws IOException, ClassNotFoundException
2N/A {
2N/A s.defaultReadObject();
2N/A // check class invariants
2N/A if (kind == null) {
2N/A throw new InvalidObjectException("kind is null");
2N/A }
2N/A if (depth < 0) {
2N/A throw new InvalidObjectException("depth is negative");
2N/A }
2N/A }
2N/A
2N/A /**
2N/A * Gets a string representation of this {@code Flow} instance useful
2N/A * for logging and not intended for display. The exact details of
2N/A * the representation are unspecified and subject to change, but the
2N/A * following format may be regarded as typical:
2N/A * <pre><code>
2N/A * class-name[property1 = value1, property2 = value2]
2N/A * </code></pre>
2N/A */
2N/A public String
2N/A toString()
2N/A {
2N/A StringBuilder buf = new StringBuilder();
2N/A buf.append(Flow.class.getName());
2N/A buf.append("[kind = ");
2N/A buf.append(kind);
2N/A buf.append(", depth = ");
2N/A buf.append(depth);
2N/A buf.append(']');
2N/A return buf.toString();
2N/A }
2N/A}