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 * State of a target process designated by {@link
2N/A * Consumer#createProcess(String command)} or {@link
2N/A * Consumer#grabProcess(int pid)}.
2N/A * <p>
2N/A * Immutable. Supports persistence using {@link java.beans.XMLEncoder}.
2N/A *
2N/A * @see ConsumerListener#processStateChanged(ProcessEvent e)
2N/A *
2N/A * @author Tom Erickson
2N/A */
2N/Apublic final class ProcessState implements Serializable {
2N/A static final long serialVersionUID = -3395911213431317292L;
2N/A
2N/A static {
2N/A try {
2N/A BeanInfo info = Introspector.getBeanInfo(ProcessState.class);
2N/A PersistenceDelegate persistenceDelegate =
2N/A new DefaultPersistenceDelegate(
2N/A new String[] {"processID", "state",
2N/A "terminationSignal", "terminationSignalName",
2N/A "exitStatus", "message"})
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 ProcessState pstate = (ProcessState)oldInstance;
2N/A return new Expression(oldInstance, oldInstance.getClass(),
2N/A "new", new Object[] { pstate.getProcessID(),
2N/A pstate.getState().name(),
2N/A pstate.getTerminationSignal(),
2N/A pstate.getTerminationSignalName(),
2N/A pstate.getExitStatus(),
2N/A pstate.getMessage() });
2N/A }
2N/A };
2N/A BeanDescriptor d = info.getBeanDescriptor();
2N/A d.setValue("persistenceDelegate", persistenceDelegate);
2N/A } catch (IntrospectionException e) {
2N/A System.out.println(e);
2N/A }
2N/A }
2N/A
2N/A /**
2N/A * State of a target process.
2N/A */
2N/A public enum State {
2N/A /** Process is running. */
2N/A RUN,
2N/A /** Process is stopped. */
2N/A STOP,
2N/A /** Process is lost to control. */
2N/A LOST,
2N/A /** Process is terminated (zombie). */
2N/A UNDEAD,
2N/A /** Process is terminated (core file). */
2N/A DEAD
2N/A }
2N/A
2N/A /** @serial */
2N/A private int processID;
2N/A /** @serial */
2N/A private State state;
2N/A /** @serial */
2N/A private int terminationSignal;
2N/A /** @serial */
2N/A private String terminationSignalName;
2N/A /** @serial */
2N/A private Integer exitStatus;
2N/A /** @serial */
2N/A private String message;
2N/A
2N/A /**
2N/A * Creates a {@code ProcessState} instance with the given state.
2N/A *
2N/A * @param pid non-negative target process ID
2N/A * @param processState target process state
2N/A * @param processTerminationSignal signal that terminated the target
2N/A * process, {@code -1} if the process was not terminated by a signal
2N/A * or if the terminating signal is unknown
2N/A * @param processTerminationSignalName name of the signal that
2N/A * terminated the target process, {@code null} if the process was
2N/A * not terminated by a signal or if the terminating signal is
2N/A * unknown
2N/A * @param processExitStatus target process exit status, {@code null}
2N/A * if the process has not exited or the exit status is unknown
2N/A * @param msg message included by DTrace, if any
2N/A * @throws NullPointerException if the given process state is {@code
2N/A * null}
2N/A * @throws IllegalArgumentException if the given process ID is negative
2N/A */
2N/A public
2N/A ProcessState(int pid, State processState,
2N/A int processTerminationSignal,
2N/A String processTerminationSignalName,
2N/A Integer processExitStatus, String msg)
2N/A {
2N/A processID = pid;
2N/A state = processState;
2N/A terminationSignal = processTerminationSignal;
2N/A terminationSignalName = processTerminationSignalName;
2N/A exitStatus = processExitStatus;
2N/A message = msg;
2N/A validate();
2N/A }
2N/A
2N/A /**
2N/A * Supports XML persistence.
2N/A *
2N/A * @see #ProcessState(int pid, State processState, int
2N/A * processTerminationSignal, String processTerminationSignalName,
2N/A * Integer processExitStatus, String msg)
2N/A * @throws IllegalArgumentException if there is no {@link
2N/A * ProcessState.State} value with the given state name.
2N/A */
2N/A public
2N/A ProcessState(int pid, String processStateName,
2N/A int processTerminationSignal,
2N/A String processTerminationSignalName,
2N/A Integer processExitStatus, String msg)
2N/A {
2N/A processID = pid;
2N/A state = Enum.valueOf(State.class, processStateName);
2N/A terminationSignal = processTerminationSignal;
2N/A terminationSignalName = processTerminationSignalName;
2N/A exitStatus = processExitStatus;
2N/A message = msg;
2N/A validate();
2N/A }
2N/A
2N/A private final void
2N/A validate()
2N/A {
2N/A if (processID < 0) {
2N/A throw new IllegalArgumentException("pid is negative");
2N/A }
2N/A if (state == null) {
2N/A throw new NullPointerException("process state is null");
2N/A }
2N/A }
2N/A
2N/A /**
2N/A * Gets the process ID.
2N/A *
2N/A * @return non-negative target process ID
2N/A */
2N/A public int
2N/A getProcessID()
2N/A {
2N/A return processID;
2N/A }
2N/A
2N/A /**
2N/A * Gets the process state.
2N/A *
2N/A * @return non-null target process state
2N/A */
2N/A public State
2N/A getState()
2N/A {
2N/A return state;
2N/A }
2N/A
2N/A /**
2N/A * Gets the signal that terminated the process.
2N/A *
2N/A * @return termination signal, {@code -1} if the process was not
2N/A * terminated by a signal or if the terminating signal is unknown
2N/A */
2N/A public int
2N/A getTerminationSignal()
2N/A {
2N/A return terminationSignal;
2N/A }
2N/A
2N/A /**
2N/A * Gets the name of the signal that terminated the process.
2N/A *
2N/A * @return termination signal name, {@code null} if the process was
2N/A * not terminated by a signal or if the terminating signal is
2N/A * unknown
2N/A */
2N/A public String
2N/A getTerminationSignalName()
2N/A {
2N/A return terminationSignalName;
2N/A }
2N/A
2N/A /**
2N/A * Gets the process exit status.
2N/A *
2N/A * @return exit status, or {@code null} if the process has not
2N/A * exited or the exit status is unknown
2N/A */
2N/A public Integer
2N/A getExitStatus()
2N/A {
2N/A return exitStatus;
2N/A }
2N/A
2N/A /**
2N/A * Called by native code.
2N/A */
2N/A private void
2N/A setExitStatus(int status)
2N/A {
2N/A exitStatus = new Integer(status);
2N/A }
2N/A
2N/A /**
2N/A * Gets the message from DTrace describing this process state.
2N/A *
2N/A * @return DTrace message, or {@code null} if DTrace did not include
2N/A * a message with this process state
2N/A */
2N/A public String
2N/A getMessage()
2N/A {
2N/A return message;
2N/A }
2N/A
2N/A /**
2N/A * Compares the specified object with this {@code ProcessState}
2N/A * instance for equality. Defines equality as having the same
2N/A * attributes.
2N/A *
2N/A * @return {@code true} if and only if the specified object is also
2N/A * a {@code ProcessState} and both instances have the same
2N/A * attributes
2N/A */
2N/A @Override
2N/A public boolean
2N/A equals(Object o)
2N/A {
2N/A if (o instanceof ProcessState) {
2N/A ProcessState s = (ProcessState)o;
2N/A return ((processID == s.processID) &&
2N/A (state == s.state) &&
2N/A (terminationSignal == s.terminationSignal) &&
2N/A ((terminationSignalName == null) ?
2N/A (s.terminationSignalName == null) :
2N/A terminationSignalName.equals(s.terminationSignalName)) &&
2N/A ((exitStatus == null) ?
2N/A (s.exitStatus == null) :
2N/A exitStatus.equals(s.exitStatus)) &&
2N/A ((message == null) ? (s.message == null) :
2N/A message.equals(s.message)));
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) + processID;
2N/A hash = (37 * hash) + state.hashCode();
2N/A hash = (37 * hash) + terminationSignal;
2N/A hash = (37 * hash) + (exitStatus == null ? 0 :
2N/A exitStatus.hashCode());
2N/A hash = (37 * hash) + (message == null ? 0 : message.hashCode());
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 try {
2N/A validate();
2N/A } catch (Exception e) {
2N/A InvalidObjectException x = new InvalidObjectException(
2N/A e.getMessage());
2N/A x.initCause(e);
2N/A throw x;
2N/A }
2N/A }
2N/A
2N/A /**
2N/A * Gets a string representation of this process state useful for
2N/A * logging and not intended for display. The exact details of the
2N/A * 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(ProcessState.class.getName());
2N/A buf.append("[pid = ");
2N/A buf.append(processID);
2N/A buf.append(", state = ");
2N/A buf.append(state);
2N/A buf.append(", terminationSignal = ");
2N/A buf.append(terminationSignal);
2N/A buf.append(", terminationSignalName = ");
2N/A buf.append(terminationSignalName);
2N/A buf.append(", exitStatus = ");
2N/A buf.append(exitStatus);
2N/A buf.append(", message = ");
2N/A buf.append(message);
2N/A buf.append(']');
2N/A return buf.toString();
2N/A }
2N/A}