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 * A value generated by the DTrace {@code umod()}, {@code ufunc()}, or
2N/A * {@code usym()} action used to lookup the symbol associated with a
2N/A * user address.
2N/A * <p>
2N/A * Immutable. Supports persistence using {@link java.beans.XMLEncoder}.
2N/A *
2N/A * @author Tom Erickson
2N/A */
2N/Apublic final class UserSymbolRecord implements SymbolValueRecord,
2N/A Serializable, Comparable <UserSymbolRecord>
2N/A{
2N/A static final long serialVersionUID = -591954442654439794L;
2N/A
2N/A static {
2N/A try {
2N/A BeanInfo info = Introspector.getBeanInfo(UserSymbolRecord.class);
2N/A PersistenceDelegate persistenceDelegate =
2N/A new DefaultPersistenceDelegate(
2N/A new String[] {"processID", "symbol", "address"})
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 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 // serialized explicitly to hide implementation; treat as final
2N/A private transient Value value;
2N/A // set natively after creation; treat as final
2N/A private transient String symbol;
2N/A
2N/A /**
2N/A * Called by native code.
2N/A */
2N/A private
2N/A UserSymbolRecord(int pid, long addressValue)
2N/A {
2N/A value = new Value(pid, addressValue);
2N/A }
2N/A
2N/A /**
2N/A * Creates a {@code UserSymbolRecord} with the given process ID,
2N/A * symbol lookup, and user address converted in probe context as a
2N/A * result of the DTrace {@code umod()}, {@code ufunc()}, or {@code
2N/A * usym()} action.
2N/A * <p>
2N/A * Supports XML persistence.
2N/A *
2N/A * @param pid non-negative user process ID
2N/A * @param lookupValue the result in the native DTrace library of
2N/A * looking up the symbol associated with the given user address
2N/A * @param addressValue symbol address
2N/A * @throws NullPointerException if the given lookup value is {@code null}
2N/A * @throws IllegalArgumentException if the given process ID is
2N/A * negative
2N/A */
2N/A public
2N/A UserSymbolRecord(int pid, String lookupValue, long addressValue)
2N/A {
2N/A value = new Value(pid, addressValue);
2N/A symbol = lookupValue;
2N/A validate();
2N/A }
2N/A
2N/A private final void
2N/A validate()
2N/A {
2N/A if (symbol == null) {
2N/A throw new NullPointerException("symbol is null");
2N/A }
2N/A }
2N/A
2N/A /**
2N/A * Gets the process ID associated with this record's symbol.
2N/A *
2N/A * @return non-negative pid
2N/A */
2N/A public int
2N/A getProcessID()
2N/A {
2N/A return value.getProcessID();
2N/A }
2N/A
2N/A /**
2N/A * Gets the result of the address lookup in the same form returned
2N/A * by {@link Consumer#lookupUserFunction(int pid, long address)}.
2N/A *
2N/A * @return non-null address lookup in the format defined by the
2N/A * native DTrace library
2N/A */
2N/A public String
2N/A getSymbol()
2N/A {
2N/A return symbol;
2N/A }
2N/A
2N/A /**
2N/A * Called by native code and ProbeData addSymbolRecord()
2N/A */
2N/A void
2N/A setSymbol(String lookupValue)
2N/A {
2N/A symbol = lookupValue;
2N/A validate();
2N/A }
2N/A
2N/A /**
2N/A * Gets the symbol's user address.
2N/A *
2N/A * @return the symbol's user address
2N/A */
2N/A public long
2N/A getAddress()
2N/A {
2N/A return value.getAddress();
2N/A }
2N/A
2N/A /**
2N/A * Gets the composite value of the symbol's process ID and address.
2N/A * The value is used in {@link #equals(Object o) equals()} and
2N/A * {@link #compareTo(UserSymbolRecord r) compareTo()} to test
2N/A * equality and to determine the natural ordering of {@code
2N/A * UserSymbolRecord} instances.
2N/A *
2N/A * @return non-null composite value of the symbols's process ID and
2N/A * address
2N/A */
2N/A public Value
2N/A getValue()
2N/A {
2N/A return value;
2N/A }
2N/A
2N/A /**
2N/A * Compares the specified object with this {@code UserSymbolRecord}
2N/A * for equality. Returns {@code true} if and only if the specified
2N/A * object is also a {@code UserSymbolRecord} and both records have
2N/A * the same process ID and the same address.
2N/A *
2N/A * @return {@code true} if and only if the specified object is also
2N/A * a {@code UserSymbolRecord} and both records have the same
2N/A * process ID and the same address
2N/A */
2N/A @Override
2N/A public boolean
2N/A equals(Object o)
2N/A {
2N/A if (o instanceof UserSymbolRecord) {
2N/A UserSymbolRecord r = (UserSymbolRecord)o;
2N/A return value.equals(r.value);
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 return value.hashCode();
2N/A }
2N/A
2N/A /**
2N/A * Compares this record with the given user symbol lookup and orders
2N/A * by the combined value of process ID first and address second.
2N/A * The comparison treats addresses as unsigned values so the
2N/A * ordering is consistent with that defined in the native DTrace
2N/A * library. The {@code compareTo()} method is compatible with {@link
2N/A * #equals(Object o) equals()}.
2N/A *
2N/A * @return -1, 0, or 1 as this record's combined process ID and
2N/A * address is less than, equal to, or greater than the given
2N/A * record's combined process ID and address
2N/A */
2N/A public int
2N/A compareTo(UserSymbolRecord r)
2N/A {
2N/A return value.compareTo(r.value);
2N/A }
2N/A
2N/A /**
2N/A * Serialize this {@code UserSymbolRecord} instance.
2N/A *
2N/A * @serialData processID ({@code int}), followed by symbol ({@code
2N/A * java.lang.String}), followed by address ({@code long})
2N/A */
2N/A private void
2N/A writeObject(ObjectOutputStream s) throws IOException
2N/A {
2N/A s.defaultWriteObject();
2N/A s.writeInt(getProcessID());
2N/A s.writeObject(getSymbol());
2N/A s.writeLong(getAddress());
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 int pid = s.readInt();
2N/A symbol = (String)s.readObject();
2N/A long addressValue = s.readLong();
2N/A try {
2N/A value = new Value(pid, addressValue);
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 the result of this symbol lookup. The format is defined in
2N/A * the native DTrace library and is as stable as that library
2N/A * definition.
2N/A *
2N/A * @return {@link #getSymbol()}
2N/A */
2N/A @Override
2N/A public String
2N/A toString()
2N/A {
2N/A return symbol;
2N/A }
2N/A
2N/A /**
2N/A * The composite value of a symbol's process ID and user address.
2N/A * <p>
2N/A * Immutable. Supports persistence using {@link
2N/A * java.beans.XMLEncoder}.
2N/A */
2N/A public static final class Value implements Serializable,
2N/A Comparable <Value> {
2N/A static final long serialVersionUID = -91449037747641135L;
2N/A
2N/A static {
2N/A try {
2N/A BeanInfo info = Introspector.getBeanInfo(
2N/A UserSymbolRecord.Value.class);
2N/A PersistenceDelegate persistenceDelegate =
2N/A new DefaultPersistenceDelegate(
2N/A new String[] {"processID", "address"})
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() ==
2N/A newInstance.getClass());
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 int processID;
2N/A /** @serial */
2N/A private final long address;
2N/A
2N/A /**
2N/A * Creates a composite value with the given user process ID and
2N/A * symbol address.
2N/A * <p>
2N/A * Supports XML persistence.
2N/A *
2N/A * @param pid non-negative process ID
2N/A * @param addressValue symbol address
2N/A * @throws IllegalArgumentException if the given process ID is
2N/A * negative
2N/A */
2N/A public
2N/A Value(int pid, long addressValue)
2N/A {
2N/A processID = pid;
2N/A address = addressValue;
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("process ID is negative");
2N/A }
2N/A }
2N/A
2N/A /**
2N/A * Gets the process ID associated with this value's user
2N/A * address.
2N/A *
2N/A * @return non-negative pid
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 symbol's user address.
2N/A *
2N/A * @return the symbol's user address
2N/A */
2N/A public long
2N/A getAddress()
2N/A {
2N/A return address;
2N/A }
2N/A
2N/A /**
2N/A * Compares the specified object with this {@code
2N/A * UserSymbolRecord.Value} for equality. Returns {@code true}
2N/A * if and only if the specified object is also a {@code
2N/A * UserSymbolRecord.Value} and both values have the same process
2N/A * ID and the same address.
2N/A *
2N/A * @return {@code true} if and only if the specified object is
2N/A * also a {@code UserSymbolRecord.Value} and both values have
2N/A * the same process ID and the same address
2N/A */
2N/A @Override
2N/A public boolean
2N/A equals(Object o)
2N/A {
2N/A if (o instanceof Value) {
2N/A Value v = (Value)o;
2N/A return ((processID == v.processID) && (address == v.address));
2N/A }
2N/A return false;
2N/A }
2N/A
2N/A /**
2N/A * Overridden to ensure that equal instances have equal hash
2N/A * 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 + (int)(address ^ (address >>> 32));
2N/A return hash;
2N/A }
2N/A
2N/A /**
2N/A * Compares this value with the given {@code
2N/A * UserSymbolRecord.Value} and orders by process ID first and
2N/A * address second. The comparison treats addresses as unsigned
2N/A * values so the ordering is consistent with that defined in the
2N/A * native DTrace library. The {@code compareTo()} method is
2N/A * compatible with {@link #equals(Object o) equals()}.
2N/A *
2N/A * @return -1, 0, or 1 as this value's combined process ID and
2N/A * address is less than, equal to, or greater than the given
2N/A * value's combined process ID and address
2N/A */
2N/A public int
2N/A compareTo(Value v)
2N/A {
2N/A int cmp;
2N/A cmp = (processID < v.processID ? -1 :
2N/A (processID > v.processID ? 1 : 0));
2N/A if (cmp == 0) {
2N/A cmp = ProbeData.compareUnsigned(address, v.address);
2N/A }
2N/A return cmp;
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 {@code
2N/A * UserSymbolRecord.Value} instance useful for logging and not
2N/A * 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(Value.class.getName());
2N/A buf.append("[processID = ");
2N/A buf.append(processID);
2N/A buf.append(", address = ");
2N/A buf.append(address);
2N/A buf.append(']');
2N/A return buf.toString();
2N/A }
2N/A }
2N/A}