Sun Studio 12 Update 1: Debugging a Program With dbx

Starting to Debug a Java Application

You can use dbx to debug the following types of Java applications:

dbx recognizes that it is debugging a Java application in all of these cases.

Debugging a Class File

You can debug a file that uses the .class file name extension using dbx as in the following example.


(dbx) debug myclass.class

If the class that defines the application is defined in a package, you need to include the package path just as when running the application under the JVM software, as in the following example.


(dbx) debug java.pkg.Toy.class

You can also use a full path name for the class file. dbx automatically determines the package portion of the class path by looking in the .class file and adds the remaining portion of the full path name to the class path. For example, given the following path name, dbx determines that pkg/Toy.class is the main class name and adds /home/user/java to the class path.


(dbx) debug /home/user/java/pkg/Toy.class

Debugging a JAR File

A Java application can be bundled in a JAR (Java Archive) file. You can debug a JAR file using dbx as in the following example.


(dbx) debug myjar.jar

When you start debugging a file that has a file name ending in .jar, dbx uses the Main_Class attribute specified in the manifest of this JAR file to determine the main class. (The main class is the class within the JAR file that is your application’s entry point. If you use a full path name or relative path name to specify the JAR file, dbx uses the directory name and prefixes it to the class path in the Main-Class attribute.

If you debug a JAR file that does not have the Main-Class attribute, you can use the JAR URL syntax jar:<url>!/{entry} that is specified in the class JarURLConnection of the Java 2 Platform, Standard Edition to specify the name of the main class, as in the following examples.


(dbx) debug jar:myjar.jar!/myclass.class
(dbx) debug jar:/a/b/c/d/e.jar!/x/y/z.class
(dbx) debug jar:file:/a/b/c/d.jar!/myclass.class

For each of these examples dbx would do the following:

Debugging a Java Application That Has a Wrapper

A Java application usually has a wrapper to set environment variables. If your Java application has a wrapper, you need to tell dbx that a wrapper script is being used by setting the jvm_invocation environment variable (see Customizing Startup of the JVM Software).

Attaching dbx to a Running Java Application

You can attach dbx to a running Java application if you specified the options shown in the following example when you started the application. After starting the application, you would use the dbx command (see dbx Command) with the process ID of the running Java process to start debugging.


$ java -Djava.compiler=NONE -Xdebug -Xnoagent -Xrundbx_agent myclass.class
$ dbx - 2345

For the JVM software to locate libdbx_agent.so, you need to add the appropriate path to LD_LIBRARY_PATH before running the Java application:

The installation_directory is the location where the Sun Studio software is installed.

When you attach dbx to the running application, dbx starts debugging the application in Java mode.

If your Java application requires 64-bit object libraries, include the -d64 option when you start the application. Then when you attach dbx to the application, dbx will use the 64-bit JVM software on which the application is running.


$ java -Djava.compiler=NONE -Xdebug -Xnoagent -Xrundbx_agent -d64 myclass.class
$ dbx - 2345

Debugging a C Application or C++ Application That Embeds a Java Application

You can debug a C application or C++ application that embeds a Java application using the JNI_CreateJavaVM interface. The C application or C++ application must start the Java application by specifying the following options to the JVM software:


-Xdebug -Xnoagent -Xrundbx_agent

For the JVM software to locate libdbx_agent.so, you need to add the appropriate path to LD_LIBRARY_PATH before running the Java application:

The installation_directory is the location where the Sun Studio software is installed.

Passing Arguments to the JVM Software

When you use the run command in Java mode, the arguments you give are passed to the application and not to the JVM software. To pass arguments to the JVM software, see Customizing Startup of the JVM Software.

Specifying the Location of Your Java Source Files

Sometimes your Java source files are not in the same directory as the .class or .jar files. You can use the $JAVASRCPATH environment variable to specify the directories in which dbx should look for Java source files. For example JAVASRCPATH=.:/mydir/mysrc:/mydir/mylibsrc:/mydir/myutils causes dbx to look in the listed directories for source files that correspond to the class files being debugged.

Specifying the Location of Your C Source Files or C++ Source Files

dbx might not be able to find your C source files or C++ source files in the following circumstances:

In such cases, use the pathmap command (see pathmap Command) to map one path name to another so that dbx can find your files.

Specifying a Path for Class Files That Use Custom Class Loaders

An application can have custom class loaders that load class files from locations that might not be part of the regular class path. In such situations dbx cannot locate the class files. The CLASSPATHX environment variable lets you specify to dbx a path for the class files that are loaded by their custom class loaders. For example, CLASSPATHX=.:/myloader/myclass:/mydir/mycustom causes dbx to look in the listed directories when it is trying to locate a class file.

Setting Breakpoints on Java Methods

Unlike native applications, Java applications do not contain an easily accessible index of names. So you cannot simply type:


(dbx) stop in myMethod

Instead, you need to use the full path to the method:


(dbx) stop in com.any.library.MyClass.myMethod

An exception is the case where you are stopped with some method of MyClass in which myMethod should be enough.

One way to avoid including the full path to the method is to use stop inmethod:


(dbx) stop inmethod myMethod

But doing so might cause stops in multiple methods name myMethod.

Setting Breakpoints in Native (JNI) Code

The shared libraries contain JNI C or C++ code are dynamically loaded by the JVM and setting breakpoints in them requires some additional steps. For more information, see Setting Breakpoints in Dynamically Loaded Libraries.