sofya.ed
Class Instrumentor

java.lang.Object
  extended by sofya.ed.Instrumentor
All Implemented Interfaces:
org.apache.bcel.generic.InstructionConstants, SConstants
Direct Known Subclasses:
BlockInstrumentor, BranchInstrumentor, SemanticInstrumentor

public abstract class Instrumentor
extends java.lang.Object
implements org.apache.bcel.generic.InstructionConstants, SConstants

Abstract base class for all instrumentors, which instrument Java class files for execution by an event dispatcher.

Special note regarding instrumentation of Sofya as a subject: If you change the mode of instrumentation (compatible, normal optimized, etc...) of the instrumentation applied to Sofya as a subject, make sure to delete the 'bootstrap.jar' file in the inst directory of the subject Sofya to force the event dispatcher to rebuild it using the newly instrumented SocketProbe. Otherwise the event dispatcher may refuse to run if the old mode of instrumentation that was applied to SocketProbe in the jar file is not compatible with a form that it accepts.

Version:
07/25/2006
Author:
Alex Kinneer
See Also:
cfInstrumentor, SemanticInstrumentor

Nested Class Summary
 
Nested classes/interfaces inherited from interface org.apache.bcel.generic.InstructionConstants
org.apache.bcel.generic.InstructionConstants.Clinit
 
Nested classes/interfaces inherited from interface sofya.base.SConstants
SConstants.BlockLabel, SConstants.BlockObjectType, SConstants.BlockSubType, SConstants.BlockType, SConstants.BranchObjectType, SConstants.BranchType, SConstants.EDObjectType, SConstants.TraceObjectType
 
Field Summary
protected  int arrayVarref
          Index to a local variable in the instrumented method used to hold the hit-object array (used by optimized normal and JUnit instrumentation).
protected  int blockCount
          Number of blocks in the method currently referenced by instrumentation.
protected  CFHandler classGraphs
          Handler for class method CFGs.
protected  boolean classHasMain
          Flag indicating whether the class has a main method.
protected  boolean classHasStaticInit
          Flag indicating whether the class has a static initializer (<clinit> method).
protected  boolean classIsDispatcher
          Flag indicating whether the class is an event dispatcher.
protected  boolean classIsSocketProbe
          Flag indicating whether the class is SocketProbe.
protected  java.lang.String className
          Name of the class currently loaded by the instrumentor, as provided in the constructor.
protected  org.apache.bcel.generic.ConstantPoolGen cpg
          BCEL data structure representing the class constant pool.
protected  int excExitBooleanVar
          Flag used to control whether the summary exit node will be marked on an exceptional exit.
protected  java.lang.String fullClassName
          Fully qualified name of the class currently loaded by the instrumentor.
protected  java.util.Set handlerStarts
          Cache of instruction handles that begin exception handlers, used by sequence instrumentation to guarantee that exceptional returns from calls will properly record the change in method to the trace.
protected  org.apache.bcel.generic.InstructionFactory iFactory
          Factory object used to generate more complex instructions.
protected  java.lang.String instClassRef
          Name of the class upon which instrumentation methods are to be invoked.
protected  int instMode
          Integer code indicating type of instrumentation to be inserted.
protected  org.apache.bcel.classfile.JavaClass javaClass
          BCEL data structure representing the class.
protected  org.apache.bcel.generic.ClassGen javaClassFile
          BCEL data structure allowing more advanced modifications to class.
protected  java.lang.String lastInstrumented
          Name of method last instrumented.
protected  CFG methodCFG
          CFG for the method currently being instrumented.
protected  int methodEntryMethodref
          Index to a method reference that should be called on entry to a new method, if any.
protected  org.apache.bcel.classfile.Method[] methods
          Collection of uninstrumented methods in the class.
protected  java.lang.String mSignature
          Signature of the method currently referenced by instrumentation.
protected  int port
          Socket port to be used for instrumentation.
protected  boolean starterInserted
          Flag indicating whether the call to SocketProbe.start has been inserted.
protected  int startMethodref
          Index to SocketProbe.start method reference added to constant pool.
protected  java.lang.String tag
          Tag associated with the class database files.
protected  boolean targetJUnit
          Flag indicating that the instrumentation is targeted for a JUnit event dispatcher.
protected  int traceMethodref
          Index to probe statement method reference added to constant pool.
protected  int typeFlags
          Bit vector representing types of blocks that will be instrumented.
protected  boolean useDefaultPort
          Flag indicating whether the port will be selected automatically.
 
Fields inherited from interface org.apache.bcel.generic.InstructionConstants
AALOAD, AASTORE, ACONST_NULL, ALOAD_0, ALOAD_1, ALOAD_2, ARETURN, ARRAYLENGTH, ASTORE_0, ASTORE_1, ASTORE_2, ATHROW, BALOAD, BASTORE, bla, CALOAD, CASTORE, D2F, D2I, D2L, DADD, DALOAD, DASTORE, DCMPG, DCMPL, DCONST_0, DCONST_1, DDIV, DMUL, DNEG, DREM, DRETURN, DSUB, DUP, DUP_X1, DUP_X2, DUP2, DUP2_X1, DUP2_X2, F2D, F2I, F2L, FADD, FALOAD, FASTORE, FCMPG, FCMPL, FCONST_0, FCONST_1, FCONST_2, FDIV, FMUL, FNEG, FREM, FRETURN, FSUB, I2B, I2C, I2D, I2F, I2L, I2S, IADD, IALOAD, IAND, IASTORE, ICONST_0, ICONST_1, ICONST_2, ICONST_3, ICONST_4, ICONST_5, ICONST_M1, IDIV, ILOAD_0, ILOAD_1, ILOAD_2, IMUL, INEG, INSTRUCTIONS, IOR, IREM, IRETURN, ISHL, ISHR, ISTORE_0, ISTORE_1, ISTORE_2, ISUB, IUSHR, IXOR, L2D, L2F, L2I, LADD, LALOAD, LAND, LASTORE, LCMP, LCONST_0, LCONST_1, LDIV, LMUL, LNEG, LOR, LREM, LRETURN, LSHL, LSHR, LSUB, LUSHR, LXOR, MONITORENTER, MONITOREXIT, NOP, POP, POP2, RETURN, SALOAD, SASTORE, SWAP, THIS
 
Fields inherited from interface sofya.base.SConstants
DEFAULT_PORT, INST_COMPATIBLE, INST_OLD_UNSUPPORTED, INST_OPT_NORMAL, INST_OPT_SEQUENCE, SIG_CHKALIVE, SIG_ECHO
 
Constructor Summary
protected Instrumentor()
          Protected default constructor, to prevent unsafe instances of the instrumentor from being created.
  Instrumentor(java.lang.String[] argv)
          Creates am instrumentor configured by a list of command line parameters.
  Instrumentor(java.lang.String className, int typeFlags)
          Creates an instrumentor for the specified class with the given object types activated for instrumentation and using the default port.
  Instrumentor(java.lang.String className, int typeFlags, int port)
          Creates an instrumentor for the specified class with the given object types activated for instrumentation and using the given port.
  Instrumentor(java.lang.String className, int typeFlags, int port, int instMode)
          Creates an instrumentor for the specified class with the given object types activated for instrumentation, using the given port, and activating the specified mode of instrumentation.
 
Method Summary
protected  void addDefaultHandler(org.apache.bcel.generic.MethodGen mg, org.apache.bcel.generic.InstructionList il)
          Modifies the main method, if necessary, to prevent unhandled exceptions from being thrown.
protected  void cacheHandlerStarts(org.apache.bcel.generic.CodeExceptionGen[] excHandlers)
          Reads the exception handlers associated with the method and caches the starting instruction handle for each handler.
 org.apache.bcel.classfile.JavaClass generateClass()
          Gets the instrumented class.
 java.lang.String getClassName()
          Gets the name of the class which is currently loaded in the instrumentor.
 int getInstMode()
          Gets the form of instrumentation that the instrumentor is currently set to insert, which will be one of the following: SConstants.INST_COMPATIBLE SConstants.INST_OPT_NORMAL SConstants.INST_OPT_SEQUENCE
 java.lang.String getLastInstrumented()
          Gets the name of the last method instrumented.
 int getMethodCount()
          Gets the number of methods in the class.
 java.lang.String getMethodName(int methodIndex)
          Gets the name of a method in the class method array.
 java.lang.String getMethodSignature(int methodIndex)
          Gets the signature of a method in the class method array, as a string.
protected abstract  SConstants.TraceObjectType getObjectType()
          Gets the structural entity that is instrumented by this instrumentor.
 int getPort()
          Gets the socket port being used for instrumentation.
protected abstract  java.lang.String getProbeClassName()
          Gets the name of the class referenced by probes inserted by this instrumentor.
 java.lang.String getQualifiedName()
          Gets the fully qualified name of the class (includes package name) which is currently loaded in the instrumentor.
 int getTypeFlags()
          Gets the bit mask controlling the object types that are currently set to be instrumented by this instrumentor.
 boolean hasMain()
          Reports whether the class has a main method.
 boolean hasStaticInit()
          Reports whether the class has a static initializer.
protected  void init(int port)
          Initializes the instrumentor, making it ready to instrument methods in the class.
 void insertFinisher(java.lang.String methodName, org.apache.bcel.generic.Type returnType, org.apache.bcel.generic.Type[] argumentTypes)
          Inserts call to SocketProbe.finish unconditionally at the exit points of the specified method.
protected  void insertProbeFinishCall(org.apache.bcel.generic.InstructionList il)
          Inserts call to SocketProbe.finish() immediately prior to any return instruction in the method.
protected  void insertProbeStartCall(org.apache.bcel.generic.InstructionList il)
          Inserts call to SocketProbe.start(int,int,boolean,boolean,int) at beginning of method.
 void insertStarter(java.lang.String methodName, org.apache.bcel.generic.Type returnType, org.apache.bcel.generic.Type[] argumentTypes)
          Inserts call to SocketProbe.start unconditionally at the beginning of the specified method.
protected abstract  org.apache.bcel.classfile.Method instrument(org.apache.bcel.classfile.Method m, int methodIndex, boolean insertStarter)
          Instruments a method.
 void instrument(java.lang.String methodName, org.apache.bcel.generic.Type returnType, org.apache.bcel.generic.Type[] argumentTypes, boolean insertStarter)
          Instruments the method with the given name and signature using the current instrumentation settings.
 void instrumentAll()
          Instruments every method in the class using the current instrumentation settings.
protected  void loadBlockRefs(Block[] blocks, org.apache.bcel.generic.InstructionList il)
          Loads the instruction handles corresponding to the bytecode offsets encoded in the given blocks into the reference fields for those blocks, using the supplied BCEL instruction list to find the handles.
 void loadClass(java.lang.String className)
          Loads a new class into the instrumentor.
 void loadClass(java.lang.String className, java.io.InputStream source)
          Loads a new class into the instrumentor, from a given stream.
protected  void parseClass(java.lang.String className, java.io.InputStream source)
          Parses the class, making it ready for instrumentation.
protected  void parseCommandLine(java.lang.String[] argv)
          Parses command line parameters that control aspects of the internal state of the instrumentor.
protected abstract  boolean parseTypeCodes(java.lang.String typeCodes)
          Parses the command-line parameter that specifies the types of objects to be instrumented (eg '-BEXC', etc.).
 void reloadClass()
          Reloads the current class from file, destroying any instrumentation that has been performed.
 void reloadMethod(java.lang.String methodName, org.apache.bcel.generic.Type returnType, org.apache.bcel.generic.Type[] argumentTypes)
          Reloads a method, destroying any instrumentation previously applied to it.
 void setInstMode(int instMode)
          Sets the form of instrumentation that the instrumentor is to insert.
 void setPort(int port, boolean auto)
          Sets the socket port to be used in instrumentation.
abstract  void setTypeFlags(int typeFlags)
          Sets the bit mask controlling what object types are to be instrumented by this instrumentor.
 void writeClass(java.io.OutputStream dest)
          Writes the binary instrumented class data to a stream.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

javaClass

protected org.apache.bcel.classfile.JavaClass javaClass
BCEL data structure representing the class.


javaClassFile

protected org.apache.bcel.generic.ClassGen javaClassFile
BCEL data structure allowing more advanced modifications to class.


cpg

protected org.apache.bcel.generic.ConstantPoolGen cpg
BCEL data structure representing the class constant pool.


methods

protected org.apache.bcel.classfile.Method[] methods
Collection of uninstrumented methods in the class.


iFactory

protected org.apache.bcel.generic.InstructionFactory iFactory
Factory object used to generate more complex instructions.


className

protected java.lang.String className
Name of the class currently loaded by the instrumentor, as provided in the constructor.


fullClassName

protected java.lang.String fullClassName
Fully qualified name of the class currently loaded by the instrumentor.


lastInstrumented

protected java.lang.String lastInstrumented
Name of method last instrumented.


port

protected int port
Socket port to be used for instrumentation.


useDefaultPort

protected boolean useDefaultPort
Flag indicating whether the port will be selected automatically.


typeFlags

protected int typeFlags
Bit vector representing types of blocks that will be instrumented.


instMode

protected int instMode
Integer code indicating type of instrumentation to be inserted.


targetJUnit

protected boolean targetJUnit
Flag indicating that the instrumentation is targeted for a JUnit event dispatcher.


classGraphs

protected CFHandler classGraphs
Handler for class method CFGs.


tag

protected java.lang.String tag
Tag associated with the class database files.


methodCFG

protected CFG methodCFG
CFG for the method currently being instrumented.


instClassRef

protected java.lang.String instClassRef
Name of the class upon which instrumentation methods are to be invoked.


classIsDispatcher

protected boolean classIsDispatcher
Flag indicating whether the class is an event dispatcher.


classIsSocketProbe

protected boolean classIsSocketProbe
Flag indicating whether the class is SocketProbe.


classHasMain

protected boolean classHasMain
Flag indicating whether the class has a main method.


classHasStaticInit

protected boolean classHasStaticInit
Flag indicating whether the class has a static initializer (<clinit> method).


starterInserted

protected boolean starterInserted
Flag indicating whether the call to SocketProbe.start has been inserted.


startMethodref

protected int startMethodref
Index to SocketProbe.start method reference added to constant pool.


traceMethodref

protected int traceMethodref
Index to probe statement method reference added to constant pool.


methodEntryMethodref

protected int methodEntryMethodref
Index to a method reference that should be called on entry to a new method, if any.


mSignature

protected java.lang.String mSignature
Signature of the method currently referenced by instrumentation.


blockCount

protected int blockCount
Number of blocks in the method currently referenced by instrumentation.


arrayVarref

protected int arrayVarref
Index to a local variable in the instrumented method used to hold the hit-object array (used by optimized normal and JUnit instrumentation).


excExitBooleanVar

protected int excExitBooleanVar
Flag used to control whether the summary exit node will be marked on an exceptional exit. If the exceptional exit is associated with a precisely known control flow path, this flag is set, which signals the summary exit node handler not to mark the summary node.


handlerStarts

protected java.util.Set handlerStarts
Cache of instruction handles that begin exception handlers, used by sequence instrumentation to guarantee that exceptional returns from calls will properly record the change in method to the trace.

Constructor Detail

Instrumentor

protected Instrumentor()
Protected default constructor, to prevent unsafe instances of the instrumentor from being created.


Instrumentor

public Instrumentor(java.lang.String[] argv)
             throws java.lang.IllegalArgumentException,
                    java.io.IOException,
                    java.lang.ClassFormatError,
                    java.lang.Exception
Creates am instrumentor configured by a list of command line parameters.

Parameters:
argv - Set of command line parameters, such as would be obtained from cfInstrumentor.
Throws:
java.lang.IllegalArgumentException - If required parameters are missing, invalid parameters are encountered, or data required for optional parameters is missing.
BadFileFormatException - If the class is an interface.
java.io.IOException - If there is an error reading the class file.
java.lang.ClassFormatError - If the class file cannot be parsed.
java.lang.Exception - If any other error is raised attempting to parse the class.

Instrumentor

public Instrumentor(java.lang.String className,
                    int typeFlags)
             throws BadFileFormatException,
                    java.lang.IllegalArgumentException,
                    java.io.IOException,
                    java.lang.ClassFormatError,
                    java.lang.Exception
Creates an instrumentor for the specified class with the given object types activated for instrumentation and using the default port.

Parameters:
className - Name of the class to be instrumented.
typeFlags - Bit mask representing the types of objects to be instrumented.
Throws:
java.lang.IllegalArgumentException - If required parameters are missing, invalid parameters are encountered, or data required for optional parameters is missing.
BadFileFormatException - If the class is an interface.
java.io.IOException - If there is an error reading the class file.
java.lang.ClassFormatError - If the class file cannot be parsed.
java.lang.Exception - If any other error is raised attempting to parse the class.

Instrumentor

public Instrumentor(java.lang.String className,
                    int typeFlags,
                    int port)
             throws BadFileFormatException,
                    java.lang.IllegalArgumentException,
                    java.io.IOException,
                    java.lang.ClassFormatError,
                    java.lang.Exception
Creates an instrumentor for the specified class with the given object types activated for instrumentation and using the given port.

Parameters:
className - Name of the class to be instrumented.
typeFlags - Bit mask representing the types of objects to be instrumented.
port - Port to which instrumentation should be set. The valid range is 1024 to 65535.
Throws:
java.lang.IllegalArgumentException - If required parameters are missing, invalid parameters are encountered, or data required for optional parameters is missing.
BadFileFormatException - If the class is an interface.
java.io.IOException - If there is an error reading the class file.
java.lang.ClassFormatError - If the class file cannot be parsed.
java.lang.Exception - If any other error is raised attempting to parse the class.

Instrumentor

public Instrumentor(java.lang.String className,
                    int typeFlags,
                    int port,
                    int instMode)
             throws BadFileFormatException,
                    java.lang.IllegalArgumentException,
                    java.io.IOException,
                    java.lang.ClassFormatError,
                    java.lang.Exception
Creates an instrumentor for the specified class with the given object types activated for instrumentation, using the given port, and activating the specified mode of instrumentation.

Parameters:
className - Name of the class to be instrumented.
typeFlags - Bit mask representing the types of objects to be instrumented.
port - Port to which instrumentation should be set. The valid range is 1024 to 65535.
instMode - Integer flag indicating the form of instrumentation to be inserted. Acceptable values are the following:
Throws:
java.lang.IllegalArgumentException - If required parameters are missing, invalid parameters are encountered, or data required for optional parameters is missing.
BadFileFormatException - If the class is an interface.
java.io.IOException - If there is an error reading the class file.
java.lang.ClassFormatError - If the class file cannot be parsed.
java.lang.Exception - If any other error is raised attempting to parse the class.
Method Detail

parseCommandLine

protected void parseCommandLine(java.lang.String[] argv)
                         throws java.lang.IllegalArgumentException
Parses command line parameters that control aspects of the internal state of the instrumentor.

Note: Some parameters may need to be processed prior to invocation of this method, such as is done by cfInstrumentor, to implement behaviors that should not be reflected in the internal state of the instrumentor itself.

Parameters:
argv - Command line parameters to be parsed.
Throws:
java.lang.IllegalArgumentException - If required parameters are missing, invalid parameters are encountered, or data required for optional parameters is missing.

parseTypeCodes

protected abstract boolean parseTypeCodes(java.lang.String typeCodes)
                                   throws java.lang.IllegalArgumentException
Parses the command-line parameter that specifies the types of objects to be instrumented (eg '-BEXC', etc.).

Parameters:
typeCodes - Parameter read from command-line, including the leading dash.
Returns:
true object types were read from the parameter, false if no types were given.
Throws:
java.lang.IllegalArgumentException - If an unrecognized object type character is encountered.

parseClass

protected void parseClass(java.lang.String className,
                          java.io.InputStream source)
                   throws BadFileFormatException,
                          java.io.IOException,
                          java.lang.ClassFormatError
Parses the class, making it ready for instrumentation.

Uses BCEL to parse the class file and initialize the internal data structures used for inserting instrumentation.

Parameters:
className - Name of the class to be parsed.
source - Stream from which the class should be parsed. May be null, in which case this method will attempt to load the class from the classpath or the filesystem (in that order).
Throws:
BadFileFormatException - If the class is an interface.
java.io.IOException - If there is an error reading the class file.
java.lang.ClassFormatError - If the class file cannot be parsed.
java.lang.Exception - If any other error is raised attempting to parse the class.

init

protected void init(int port)
             throws java.io.IOException
Initializes the instrumentor, making it ready to instrument methods in the class.

Analyzes the class and sets various flags used to control instrumentation. Determines if the subject is a filter class or SocketProbe. Determines the default port or checks that the port number is in range if one is specified. Adds the instrumentation method references to the class constant pool. Determines if the class has a static initializer and/or main method to control where to insert the call to SocketProbe.start.

Parameters:
port - Port which is to be used for instrumentation. If a negative number is supplied, the default port is selected. Otherwise, the valid range for this parameter is 1024 to 65535.
Throws:
java.io.IOException

loadClass

public void loadClass(java.lang.String className)
               throws BadFileFormatException,
                      java.io.FileNotFoundException,
                      java.io.IOException,
                      java.lang.ClassFormatError,
                      java.lang.Exception
Loads a new class into the instrumentor.

Note: If writeClass(java.io.OutputStream) is not called before this method, any instrumentation performed on the last loaded class will be lost.

Parameters:
className - Name of the class to be loaded.
Throws:
BadFileFormatException - If the class is an interface.
java.io.IOException - If there is an error reading the class file.
java.lang.ClassFormatError - If the class file cannot be parsed.
java.lang.Exception - If any other error is raised attempting to load the class.
java.io.FileNotFoundException

loadClass

public void loadClass(java.lang.String className,
                      java.io.InputStream source)
               throws BadFileFormatException,
                      java.io.IOException,
                      java.lang.ClassFormatError,
                      java.lang.Exception
Loads a new class into the instrumentor, from a given stream.

Note: If writeClass(java.io.OutputStream) is not called before this method, any instrumentation performed on the last loaded class will be lost.

Parameters:
className - Name of the class to be loaded.
source - Stream from which the class should be read.
Throws:
BadFileFormatException - If the specified class is an interface.
java.io.IOException - If there is an error reading the class file.
java.lang.ClassFormatError - If BCEL cannot parse the class.
java.lang.Exception - If any other error is raised attempting to load the class.

reloadClass

public void reloadClass()
                 throws java.io.IOException,
                        java.lang.ClassFormatError,
                        java.lang.Exception
Reloads the current class from file, destroying any instrumentation that has been performed.

Throws:
java.io.IOException - If there is an error reading the class file.
java.lang.ClassFormatError - If the class file cannot be parsed.
java.lang.Exception - If any other error is raised attempting to parse the class.

reloadMethod

public void reloadMethod(java.lang.String methodName,
                         org.apache.bcel.generic.Type returnType,
                         org.apache.bcel.generic.Type[] argumentTypes)
                  throws MethodNotFoundException
Reloads a method, destroying any instrumentation previously applied to it.

Parameters:
methodName - Name of the method to be reloaded.
returnType - Return type of the method.
argumentTypes - The types of the arguments to the method. Together with the return type, these constitute the signature which will be used to identify a match.
Throws:
MethodNotFoundException - If no method matching the specified name and signature can be found.

getPort

public int getPort()
Gets the socket port being used for instrumentation.

Returns:
The port which is currently being used for instrumentation.

setPort

public void setPort(int port,
                    boolean auto)
Sets the socket port to be used in instrumentation.

Warning: This method should be used with caution. Only the first occurrence of an invocation of SocketProbe.start during execution will cause the port to be set. The method where the call to SocketProbe.start has been inserted must be re-instrumented for the change to take effect. The hasMain() and hasStaticInit() methods can be used to determine which method in the class represents the first executable code that can be run by the class, if that is the preferred location for the call to SocketProbe.start. A call to instrumentAll() will also cause the change to take effect.

Parameters:
port - Port which is to be used for instrumentation. The valid range is 1024 to 65535.
auto - If true, set to the default port.

getClassName

public java.lang.String getClassName()
Gets the name of the class which is currently loaded in the instrumentor.

Returns:
The name of the class which is being handled by this instrumentor.

getQualifiedName

public java.lang.String getQualifiedName()
Gets the fully qualified name of the class (includes package name) which is currently loaded in the instrumentor.

Returns:
The fully qualified name of the class which is being handled by this instrumentor.

getInstMode

public int getInstMode()
Gets the form of instrumentation that the instrumentor is currently set to insert, which will be one of the following:

Returns:
The form of instrumentation which the instrumentor is set to insert, indicated by one of the above constants.

setInstMode

public void setInstMode(int instMode)
                 throws java.lang.IllegalStateException,
                        java.lang.IllegalArgumentException
Sets the form of instrumentation that the instrumentor is to insert.

Parameters:
instMode - Integer flag indicating the form of instrumentation to be inserted. Acceptable values are the following:
Throws:
java.lang.IllegalStateException - If methods in the class have already been instrumented using a different form of instrumentation. Call reloadClass() to begin instrumenting the current class with a different form of instrumentation.
java.lang.IllegalArgumentException - If the instrumentation mode is invalid.

getTypeFlags

public int getTypeFlags()
Gets the bit mask controlling the object types that are currently set to be instrumented by this instrumentor.

Returns:
Bit mask controlling what types of objects are instrumented.

setTypeFlags

public abstract void setTypeFlags(int typeFlags)
Sets the bit mask controlling what object types are to be instrumented by this instrumentor.

Parameters:
typeFlags - Bit mask representing the types of objects to be instrumented.
Throws:
java.lang.IllegalArgumentException - If the bit mask doesn't have a set bit which corresponds to a valid object type.

getObjectType

protected abstract SConstants.TraceObjectType getObjectType()
Gets the structural entity that is instrumented by this instrumentor. This allows the abstract superclass to implement some general functions, simply using dynamic binding to the actual current object to determine proper actions and values when necessary.

Returns:
The structural entity (e.g. block, edge) that this instrumentor knows how to instrument.

getProbeClassName

protected abstract java.lang.String getProbeClassName()
Gets the name of the class referenced by probes inserted by this instrumentor.

Returns:
The name of the probe class used by this instrumentor.

getMethodCount

public int getMethodCount()
Gets the number of methods in the class.

Returns:
The number of methods found in the class.

getMethodName

public java.lang.String getMethodName(int methodIndex)
Gets the name of a method in the class method array.

Parameters:
methodIndex - Index of the method whose name is to be retrieved.
Returns:
The name of the method at the given location in the method array.

getMethodSignature

public java.lang.String getMethodSignature(int methodIndex)
Gets the signature of a method in the class method array, as a string.

Parameters:
methodIndex - Index of the method whose name is to be retrieved.
Returns:
The signature of the method at the given location in the method array.

getLastInstrumented

public java.lang.String getLastInstrumented()
Gets the name of the last method instrumented.

This may be useful to determine when a method is actually instrumented. Methods declared as native, abstract, or which have no method body will not cause the value returned by this method to be updated.

Returns:
The name of the last method successfully instrumented, or null if no method has been instrumented yet.

hasMain

public boolean hasMain()
Reports whether the class has a main method.

Returns:
true if the class has a main method, false otherwise.

hasStaticInit

public boolean hasStaticInit()
Reports whether the class has a static initializer.

Returns:
true if the class has a <clinit> method, false otherwise.

instrumentAll

public void instrumentAll()
Instruments every method in the class using the current instrumentation settings. This will overwrite any instrumentation previously applied to the methods.

This method will automatically determine the correct method into which to insert the call to SocketProbe.start.


instrument

public void instrument(java.lang.String methodName,
                       org.apache.bcel.generic.Type returnType,
                       org.apache.bcel.generic.Type[] argumentTypes,
                       boolean insertStarter)
                throws MethodNotFoundException
Instruments the method with the given name and signature using the current instrumentation settings. This will overwrite any instrumentation previously applied to the method.

Parameters:
methodName - Name of the method to be reloaded.
returnType - Return type of the method.
argumentTypes - The types of the arguments to the method. Together with the return type, these constitute the signature which will be used to identify a match.
insertStarter - Flag specifying whether call to SocketProbe.start should be unconditionally inserted at the beginning of this method. The SocketProbe.start method is guarded against multiple invocations, such that only the first occurrence during execution has any effect. Nonetheless, setting this flag should be done with discretion and is generally discouraged. Note also that the instrumentAll() method will cause the call to be automatically inserted in the appropriate location.
Throws:
MethodNotFoundException - If no method matching the specified name and signature can be found.

instrument

protected abstract org.apache.bcel.classfile.Method instrument(org.apache.bcel.classfile.Method m,
                                                               int methodIndex,
                                                               boolean insertStarter)
Instruments a method.

Performs the actual instrumentation and update of the method in the class.

Parameters:
m - Method to be instrumented.
methodIndex - Index to the method in the class method array.
insertStarter - If true, force the instrumentor to insert a call to SocketProbe.start at the beginning of this method, otherwise it will be determined automatically whether it should be inserted.
Returns:
The instrumented method. If the method is native, abstract, or has no body, it is returned unchanged.

insertStarter

public void insertStarter(java.lang.String methodName,
                          org.apache.bcel.generic.Type returnType,
                          org.apache.bcel.generic.Type[] argumentTypes)
                   throws MethodNotFoundException
Inserts call to SocketProbe.start unconditionally at the beginning of the specified method.

The SocketProbe.start method is guarded against multiple invocations, such that only the first occurrence during execution has any effect. Nonetheless, this method should be used with discretion and such use is generally discouraged. Note also that the instrumentAll() method will cause the call to be automatically inserted in the appropriate location.

Parameters:
methodName - Name of the method into which to insert call.
returnType - Return type of the method.
argumentTypes - The types of the arguments to the method. Together with the return type, these constitute the signature which will be used to identify a match.
Throws:
MethodNotFoundException - If no method matching the specified name and signature can be found.

insertFinisher

public void insertFinisher(java.lang.String methodName,
                           org.apache.bcel.generic.Type returnType,
                           org.apache.bcel.generic.Type[] argumentTypes)
                    throws MethodNotFoundException
Inserts call to SocketProbe.finish unconditionally at the exit points of the specified method.

Warning: Use this method with extreme caution. The SocketProbe.finish method causes the SocketProbe to transmit all remaining trace data and close its socket(s). Thus it should be called only once, and after all possible instrumentation has been executed, or errors and data loss will occur. Note also that the instrumentAll() method will cause the call to be automatically inserted in the appropriate location.

Parameters:
methodName - Name of the method into which to insert call.
returnType - Return type of the method.
argumentTypes - The types of the arguments to the method. Together with the return type, these constitute the signature which will be used to identify a match.
Throws:
MethodNotFoundException - If no method matching the specified name and signature can be found.

generateClass

public org.apache.bcel.classfile.JavaClass generateClass()
Gets the instrumented class.

Returns:
The BCEL object representing the instrumented class.

writeClass

public void writeClass(java.io.OutputStream dest)
                throws java.io.IOException
Writes the binary instrumented class data to a stream.

The class data written constitutes a valid Java class file. In other words, if the output stream is a file, the resulting file will be a valid class file containing any instrumentation that was applied.

Parameters:
dest - Output stream to which class is to be written.
Throws:
java.io.IOException - If there is an error writing to the stream.

insertProbeStartCall

protected void insertProbeStartCall(org.apache.bcel.generic.InstructionList il)
Inserts call to SocketProbe.start(int,int,boolean,boolean,int) at beginning of method.

The call to SocketProbe.start(int,int,boolean,boolean,int) will be the very first instruction in the method. It should be inserted into the static initializer (<clinit>), or the main method if no static initializer is present. Otherwise, instrumentation calls to SocketProbe may fail.

Parameters:
il - The instruction list of the method into which the call is to be inserted.

insertProbeFinishCall

protected void insertProbeFinishCall(org.apache.bcel.generic.InstructionList il)
Inserts call to SocketProbe.finish() immediately prior to any return instruction in the method.

Parameters:
il - The instruction list of the method into which the call is to be inserted.

addDefaultHandler

protected void addDefaultHandler(org.apache.bcel.generic.MethodGen mg,
                                 org.apache.bcel.generic.InstructionList il)
Modifies the main method, if necessary, to prevent unhandled exceptions from being thrown.

This method wraps the entire main method with a generic handler for any java.lang.Throwable and eliminates the throws declarations. The handler will emulate the behavior of the ThreadGroup class's uncaughtException method. This is effectively a workaround for a stream-redirection related bug in the SDK or JVM implementation that results in non-deterministic display of subject outputs through Filter when exceptions are thrown from the main method.

Parameters:
mg - The BCEL mutable representation of the method, required to manipulate the throws clauses and handlers associated with the method.
il - The instruction list of the method, used to insert the actual handler instructions.

cacheHandlerStarts

protected void cacheHandlerStarts(org.apache.bcel.generic.CodeExceptionGen[] excHandlers)
Reads the exception handlers associated with the method and caches the starting instruction handle for each handler.

During the sequence instrumentation process, this cache is consulted to determine whether a block is the lead block of an exception handler. If it is, a call to record the possible change in method (exceptional return) to the trace is inserted.

Parameters:
excHandlers - Table of exception handlers attached to the method.

loadBlockRefs

protected void loadBlockRefs(Block[] blocks,
                             org.apache.bcel.generic.InstructionList il)
Loads the instruction handles corresponding to the bytecode offsets encoded in the given blocks into the reference fields for those blocks, using the supplied BCEL instruction list to find the handles.

Warning: This method must be called before any changes are made to the instruction list! By ensuring this condition, this method can use what is presumed to be a faster lookup method to locate handles in the list.

Parameters:
blocks - List of blocks for which the reference fields should be assigned the instruction handles corresponding to the start and end offsets of the block.
il - BCEL instruction list from which instruction handles are to be loaded.