Page Header
Left Border
Adapt_j_logo Stack_trace
adaptj.com web    

Howtos

Troubleshooting Tools for Java™


How to get a thread dump with StackTrace?

  • Launch StackTrace. (Please read our Licensing terms.)
    From the Process menu select Thread Dump. Enter the target Process ID or use the "..." button to select one. Use at least one or a combination of the following options:
    • Thread dump - returns a thread dump of the target process.
    • Use Ctrl-Break - Sends QUIT signal to the process. Do not use this option if your process was started with the -Xrs VM option (a Windows service for example) because the process will exit.
    • System properties - returns System.getProperties() for the target process.
    • Memory usage - returns Runtime.freeMemory(), Runtime.totalMemory(), and Runtime.maxMemory().
    • Process command line - returns the command line used to start the target process.
    • Force GC - calls System.gc() inside the target process.
    • Create console - creates a console for a process if it does not have one.
    • Keep remote thread running - creates a remote thread in the target process which waits for an event to make a thread dump. It helps when the regular thread dump procedure cannot be executed because of the system global locks.
    The result of the selected operations will appear inside the StackTrace editor window.
    The current version of the product supports JDKs 1.1 through 6.0 on Windows, JDKs 1.3 through 5.0 on Mac OS X (PPC only),and JDKs 1.4 through 6.0 on Linux x86. It cannot work with processes started in a different session under Windows Terminal Server Environment. The standalone version provides a service with a web based UI and command line tools which can be used as a workaround for this problem. This issue can be avoided by using VNC or PCAnywhere or starting the Remote Client with the console option: mstsc.exe /console

How to get a thread dump without StackTrace?

  • A thread dump is an invaluable source of information to Java developers when investigating deadlocks and some performance issues. It is a textual dump of all active threads and monitors of Java applications running in a Virtual Machine.

    The ways to generate a thread dump differ depending on the platform:

    • Windows systems
      • Press Ctrl-Break in the command console you used to start your application.

        When you don't have a console window your best alternative is StackTrace. It can get thread dumps from applications started with javaw or running as Windows services.

      • If you start your application with the com.sun.management.jmxremote option you should be able to attach jconsole and get a thread dump (JDK 5.0 or higher.) It is possible to use StackTrace to enable this option even if your application was started without it.

      • There is also a little tool called SendSignal which uses a clever trick to call the ctrl-break signal handler of any process.

      • JDK 6.0 will offer tools for monitoring, management, and troubleshooting (jconsole, jps, jstat, jstatd, jinfo, jmap, jstack.)

    • Mac OS X
      • Press Ctrl-\ in the terminal console you used to start your application.

        Alternatively, you can also generate a thread dump by sending the QUIT signal to the Java VM running your application

        kill -QUIT process_id

        where process_id is the process number of the respective java process

      • Thread dump by using gdb.
        Attach to the target process with gdb and run the following command:
        (gdb) call (void)pss()

      • The latest Apple 1.5 JVMs offer all the monitoring tools available for Linux and Solaris (jconsole, jps, jstat, jstatd, jinfo, jmap, jstack.)

      • The following piece of code seems to trigger a thread dump for some JVMs under Mac OS X.
        try {  
            java.lang.reflect.Field field =  
                sun.misc.Signal.class.getDeclaredField("handlers");  
            field.setAccessible(true);  
            java.util.Map handlers =  
                (java.util.Map) field.get(sun.misc.Signal.class);  
            sun.misc.Signal sig = new sun.misc.Signal("QUIT");  
            handlers.put(sig, new sun.misc.SignalHandler() {  
                public void handle(sun.misc.Signal s) {}  
            });  
            sun.misc.Signal.raise(sig);  
        } catch (Exception e) {  
            e.printStackTrace();  
        } 
        
    • Unix systems
      • Press Ctrl-\ in the terminal console you used to start your application.

        Alternatively, you can also generate a thread dump by sending the QUIT signal to the Java VM running your application

        kill -QUIT process_id

        where process_id is the process number of the respective java process

      • JDK 5.0 offers new tools for monitoring, management, and troubleshooting (jconsole, jps, jstat, jstatd, jinfo, jmap, jstack). Most of them are available only for Linux and Solaris.

How to get a thread dump from a Windows service remotely?

  • Due to limitations in the Windows debugging API, it is not possible to run StackTrace in one session and get a thread dump from a process running in a different session. This is a common scenario when an application is running as a service and the user starts StackTrace by using Remote client to a Terminal server. The easiest way to get around this limitation is by using the StackTrace service from version 2.0+. It is running in session 0, so it should not be a problem getting thread dumps or any other information from processes in the same session.
    It is also possible to run the Remote client in session 0: mstsc.exe /console
    The console argument is supported only for Windows XP or higher.

How do I obtain an evaluation license?

  • First you have to download and install the standalone version of StackTrace. It has to be installed on the computer where you intend to use it. Copy the System ID from the License dialog of the tool. From a computer with Internet access open the following URL http://www.adaptj.com/download/evaluate/2.5.8
    Enter the System ID in the text field and press the request button. You will receive an email with the license information.

How to report a problem?

  • When StackTrace fails to work, please send a bug report to support@adaptj.com, check the support forums for possible solutions, or enter a new case here.
    The following information will be useful:
    1. What is your OS?
    2. What JDK were you using?
    3. Do you use a remote client and a Terminal server?
    4. The error message from the editor and the output from the DebugView tool.

How to use StackTrace for automating GUI testing?

  • With StackTrace you can record and replay the mouse and the key events for a Java application.
    Start a BeanShell console for the target process. From the Process menu select Scripts... Enter the target process ID and select <Open BeanShell console>. In the console type:

    bsh % m = awtMacro();
    bsh % m.record();
    Press <Enter> to stop recording.


    Use the mouse or the keyboard to execute some commands inside the target application. Go back to the console and press <Enter> to stop recording.
    The recorded commands can be replayed by using the macro object.

    bsh % m.play();

    To print the recorded script:

    bsh % m.print();

    The script is actually a regular Java class which can be saved, edited, and loaded again.

    To print all available methods and properties:

    bsh % m.help();

    For SWT use swtMacro().

How to trace AWT events?

  • StackTrace can print all AWT events for a running application. First you have to start a BeanShell console for the target process. From the Process menu select Scripts... Enter the target process ID and select <Open BeanShell console>. In the console type:

    bsh % dumpAwtEvents(-1);
    Press <Enter> to stop the event listener

    The events will be printed in the console till you press <Enter>
    DumpAwtEvents accepts one parameter. It is a bit mask of the event types declared in java.awt.AWTEvent.
    For example: dumpAwtEvents(java.awt.AWTEvent.WINDOW_EVENT_MASK|java.awt.AWTEvent.PAINT_EVENT_MASK); will print only the paint and the window events. Value of -1 means all events.

How to get a reference to a UI component in the BeanShell console?

  • With StackTrace you can call methods or change properties on any object instance by using the BeanShell scripting language. The interpreter is being injected and it runs inside the target process.
    In order to get a reference to a UI component you can use the selectOnRelease or selectOnEnter commands. In the console type:

    bsh % c = selectOnRelease();
    Press <Enter> to select the last component.

    Now click on the target component. Press <Enter> in the console. The variable c is a reference to the component instance. It can be used to change its color for example c.setBackground(java.awt.Color.RED).

How to take a snapshot of an AWT/Swing component?

  • With StackTrace you can take a snapshot of a component into an offscreen buffer and then save it to a file in JPEG format.
    First you have to start a BeanShell console for the target process. From the Process menu select Scripts... Enter the target process ID and select <Open BeanShell console>. In the console type:

    bsh % c = selectOnRelease();
    Press <Enter> to select the last component.


    java.awt.TextField[textfield0,105,3,271x23,text=,editable,selection=0-0]

    Now click on the target component. Press <Enter> in the console. The variable c is a reference to the component instance.

    bsh % snapshot(c, new java.io.File("/tmp/image.jpg"));

How to start a Java application inside an already running JVM?

  • StackTrace 2.0 introduces an experimental command line tool (launch.exe) which can be used instead of the standard JDK launcher (java.exe or javaw.exe). It takes the target process ID as an additional parameter. For example the following Java program:

    java -classpath myjar1.jar;myjar2.jar some.package.Main

    Can be started inside an already running JVM like this:

    launch -classpath myjar1.jar;myjar2.jar {pid} some.package.Main

    Where {pid} is the ID of the target process.

How to stop/suspend a Java thread inside an already running process?

  • Please, read the following article to understand why it is not a good idea to stop/suspend threads in Java - Why is Thread.stop deprecated?
    If you still want to do it here is how:
    Start a BeanShell console inside the target process. Get a reference to the thread object by using the StackTrace threads command.

    bsh % threads();

    It returns and array of all active threads inside the process. Invoke the desired method call:

    bsh % threads()[5].suspend();

How to replace/patch classes inside a running JVM?

  • This functionality is available only with JDK 6 or higher.
    Attach the BeanShell console to the target process. Locate the class object you want to patch. The easiest way to do that is by using instrument().getAllLoadedClasses(). This command returns an array of all currently loaded classes.
    If the variable clazz is a reference to the class object being replaced, the following command will load the new class:

    bsh % redefine(clazz, read($file("C:/temp/classes/com/test/MyClass.class")));

    The following command will load back the old class:

    bsh % redefine(clazz);

    The redefinition may change method bodies, the constant pool and attributes. The redefinition must not add, remove or rename fields or methods, change the signatures of methods, or change inheritance. These restrictions may be lifted in future versions of the JDK. The class file bytes are not checked, verified and installed until after the transformations have been applied. If the resultant bytes are in error, this method will throw an exception.
    If this method throws an exception, no classes have been redefined.
Right Border
Page Footer
Website and logo design by LogoWorks