Page Header
Left Border
Adapt_j_logo Stack_trace
adaptj.com web    

Documentation

Troubleshooting Tools for Java™

StackTrace provides some additional commands to the BeanShell console.
For more information about these commands use one of the following methods:
- Type help(); for a list of all available commands.
- Type help("<command name>"); for a short description of the specified command.
- Type list("<command name>"); to see the source of the command.
- Type methods(object); to see all the methods of an object.
- Type fields(object); to see all the fields of an object.

Command Description
awtMacro()
awtMacro(String name, java.awt.Component relative)
Creates a macro object which can be used to record and replay the mouse and the key events in an AWT application.
breakpoint(String clazz, String methodRegex) This is a special case of the intercept command. The threads executing the specified methods enter an interpreter which accepts commands from the user. It is possible to run any BeanShell script in the context of the executing threads.
bytecode(Object object) Returns an object containing the bytecode of the argument as String.
captureLog(java.util.logging.Logger[] loggers, String level)
captureLog4j(org.apache.log4j.Logger[] loggers, String level)
Temporarily sets the log level to the specified value and copies the log to the current console.
captureOutput() Temporarily redirects System.out and System.err to the current console.
classpath(String path) Ant style classpath (wrapper arround the Spring classes)
The mapping matches URLs using the following rules:
 ? matches one character
 * matches zero or more characters
 ** matches zero or more 'directories' in a path

Example:
cp = classpath("C:/Java/1.4.2", "lib/*.jar")  
        .add("jre/lib/ext/*.jar")  
        .remove("jre/lib/ext/ldapsec.jar");  
cp.toString(); 
detectBadAwt() Finds Swing threading issues (Based on ideas and code from ClientJava.com. See also How to Use Threads.)
detectDeadlocks() Detects deadlocks by using the JDK 5.0 Managing and Monitoring API (Based on ideas and code from Maximum Solutions.)
detectOutOfMemory() Detects possible out of memory exceptions by using the JDK 5.0 Managing and Monitoring API (Based on ideas and code from Maximum Solutions.)
dumpAwtEvents(int mask) Prints all AWT events. Mask is a constant defined in java.awt.AWTEvent. For all events use -1.
fields(Object object)
fields(Object object, int modifier)
fields(Object object, int modifier, boolean makeAccessible)
Returns an object with access to all the fields of the parameter.

Example:
fields(String.class).serialVersionUID = 0;
s = "test";
fields(s).count = 2;
print(s);
fields(s);
findClass(String className, String path)
findClass(String className, Classpath classpath)
Finds all classes with a given name in the specified classpath. Use the classpath() command to build Ant style classpaths.
help()
help(String command)
Prints a list of the available commands or the documentation for the command specified as a parameter.
hierarchy(java.awt.Component component ) Prints all components in the hierarchy. It is similar to pressing CTRL+SHIFT+F1 (See Bad Data and Buggy Code: Using Java's Exceptions and Debugging Features.)
insaneScan(Object root, String file, boolean inAWT) Calls the NetBeans InsaneLib to dump all objects referenced by root in file.
instrument() Returns a java.lang.instrument.Instrumentation object. This object provides services needed to instrument Java programming language code. In addition to that it can be used to return an array of all classes currently loaded by the JVM - instrument().getAllLoadedClasses(), or to return an approximation of the amount of storage consumed by the specified object - instrument().getObjectSize(object).
intercept(String clazz, String methodRegex, Object advice) Modifies the specified methods at runtime by using bytecode instrumentation. The generated code calls methods from the advice object before and after the method execution. It is possible to inspect and modify the arguments, the return values, and the exception thrown from the methods. The class of the advice argument should have one or more of the following methods:
/**
 * The method descriptor has the following properties
 * - String method.name
 * - Object[] method.args
 * - Object method.object (this or class for static)
 * - Class method.objectClass (class)
 * - Object method.value
 * - String method.trace
 * - StackTraceElement[] method.stacktrace
 * - Method method.instance
 * - Throwable method.exception (pending exception)
 * - boolean method.ignore (do not execute the method body 
 * if true)
 *
 * Assigning a value to method.args[i] will replace
 * the i-th argument of the method.
 * The return value of the method can be modified too.
 * The exception can be re-thrown or ignored
 */
class Advice {
  /**
   * Invoked before the method is executed.
   */
  enter(method) 
  {}

  /**
   * Invoked for methods returning int values.
   * n the return value of the intercepted method.
   * The value returned by this method replaces
   * the value of the intercepted method.
   */
  int value(method, int n) 
  { return n; }

  /**
   * Invoked for methods returning long values.
   * n the return value of the intercepted method.
   * The value returned by this method replaces
   * the value of the intercepted method.
   */
  long value(method, long n) 
  { return n; }

  ...	
	
  /**
   * Invoked for methods returning non-primitive values.
   * n the return value of the intercepted method.
   * The value returned by this method replaces
   * the value of the intercepted method.
   */
  Object value(method, Object n)
  { return n; }

  /**
   * Invoked only when the intercepted method 
   * throws an exception.
   * Set method.exception to null if you want
   * to ignore the exception.
   */
  void exception(method)
  { method.exception = null; }

  /**
   * Invoked always at the end of the intercepted method.
   * Even in case of an exception.
   */
  void exit(method) 
  {}
}
The following BeanShell script will intercept all calls to javax.swing.text.JTextComponent.setText and will change the string argument to upper case.
class Advice {
  enter(m) { 
    m.args[0] = m.args[0].toUpperCase(); 
  }
}

intercept("javax.swing.text.JTextComponent", 
    "setText\\(Ljava/lang/String;\\)V", 
    new Advice());
jconsoleAgent(int port) Starts the JConsole agent for JDK 5.0. Use port -1 for a local connection.
list(String command ) Prints the source of the specified command.
listLog4j(String logNameFilter, boolean silent) Returns all Log4J loggers as an array and prints their names and levels. It may be necessary to push a different class loader. All parameters are optional.
listLoggers(String logNameFilter, boolean silent) Returns all JDK loggers as an array and prints their names and levels. All parameters are optional.
location(Class object) Determines from which file the class was loaded. (See Back to Your Class Roots)
log4jLevel(String loggerName, String level)
log4jLevel(org.apache.log4j.Logger logger, String level)
log4jLevel(org.apache.log4j.Logger[] loggers, String level)
Changes the level of the specified logger(s).
logExec(boolean fullStackTrace ) Logs a Runtime.exec attempt in the target process. Prints the name of the command and a full stack trace if specified as a boolean parameter.
logExit(boolean fullStackTrace ) Logs a System.exit call in the target process. Prints a full stack trace if specified as a boolean parameter.
logFileAccess(boolean fullStackTrace, boolean fileInfo, String regExFilter ) Logs read, write, and delete access attempts in the target process. Prints full stack traces and additional file information if specified. All parameters are optional.
logLevel(String loggerName, String level)
logLevel(java.util.logging.Logger logger, String level)
logLevel(java.util.logging.Logger[] loggers, String level)
Changes the level of the specified logger(s).
logNetAccess(boolean fullStackTrace, String regExFilter) Logs connect, listen, and accept access attempts of the target process. All parameters are optional.
logThreads(boolean fullStackTrace ) Logs threads creation, start, and finish. Prints a full stack trace if specified as a boolean parameter.
logThrows() Logs Throwable objects creation.
methods(Object object)
methods(Object object, int modifier)
methods(Object object, int modifier, boolean makeAccessible)
Returns an array with all the methods of the parameter.
mxThreads() Prints a list of threads by using the java.lang.management API.
mxWhere() Prints the current stack traces of the threads along with some additional information like the locked monitors and the cpu time.
pid() Returns the current process ID.
pipe(String command ) Pipes the output from one command to the next. Here is an examples:
pipe("threads()").to("!grep system").to("!head -2");
The parameter can be a BeanShell command or an external program name with parameters. In the later case the command should start with "!".
pushClassLoader(ClassLoader parent)
popClassLoader()
Changes the interpreter's class loader with a new one with a parent equals to the method parameter.
redefine(Class clazz, byte[] bytes) Redefines the classes passed as parameters. If no byte code is specified the command will try to read the class files from the locations where they were initially loaded. For more details about the limitations of the class redefinition see: java.lang.instrument.Instrumentation
selectColor() Opens a color chooser dialog and returns a Color object.
selectDir()
selectFile()
Opens file/directory chooser dialog and returns a File object.
selectFont() Opens a font chooser dialog and returns a Font object.
selectOnEnter() Returns the component which received the last mouse enter event.
selectOnRelease() Returns the component which received the last mouse release event.
sizeof(Object instance) Obtains a good guess of the object's size. (See Sizeof for Java)
snapshot(java.awt.Component component, File file) Paints the component into an offscreen buffer and saves it to a file in JPEG format.
sql(java.sql.Connection connection)
sql(
String[] args)

Starts a command line interactive session with a SQL server (jisql) inside the target process. It can work with an existing connection or a new one can be created with the following parameters:
-driver specifies the JDBC driver to use. There are several builtin shortcuts - see the docs for details.
-cstring specifies the connection string to use. These are driver specific.
-user specifies a user name to log into a database server with.
-password specifies the user name to log into a database server with.
-c specifies the command terminator. The default is "go".
-w specifies the maximum field width for a column. The default is to output the full width of the column.
-input specifies a file name to read commands from.
-debug prints to stdout (System.out) debugging information.

For more details see:
http://www.xigole.com/software/jisql.jsp

summary() Prints basic information about the running JVM.
swtMacro()
swtMacro(String name, org.eclipse.swt.widgets.Control relative)
Creates a macro object which can be used to record and replay the mouse and the key events in a SWT application.
swtSnapshot(org.eclipse.swt.widgets.Control control, java.io.File) Paints the component into an offscreen buffer and saves it to a file in JPEG format.
textMenu(javax.swing.text.JTextComponent object ) Adds a context menu with the most common text operations (Copy, Cut, Paste, Find) to any JTextComponent.
threadDump() Returns a thread dump as String. Can be used only in a startup script.
thread(String pattern) Returns the first thread object with name matching the pattern parameter.
threads()
threads(String pattern)
Returns an array of all active thread objects.
whenGone(Object object, String message) Prints a message when the object parameter is garbage collected.
where(Thread thread) Prints the stack trace of the thread pointed by the parameter (JDK 5.0 or higher.)


And the corresponding commands for SWT: dumpSwtEvents(mask), dumpSwtEvents(int[] mask), swtSelectOnRelease(), swtSelectOnEnter(), asyncExec(String), asyncPrint(String).

Command line tools

jps.bat, jps.exe - Lists all started Java virtual machines on a target system.
jinfo.bat, jinfo.exe - Prints the system properties, the memory usage information, and the command line used to start the target process.
jstack.bat, jstack.exe - Prints a stack trace of threads for a given process.
jstart.bat, jstart.exe - Executes a BeanShell script inside a target process.
launch.exe (Experimental) - Starts a Java application inside an already running Java virtual machine.

Right Border
Page Footer
Website and logo design by LogoWorks