C H A P T E R 17 |
Debugging a Java Application With dbx |
This chapter describes how you can use dbx to debug an application that is a mixture of Java code and C JNI (Java
Native Interface) code or C++ JNI code.
The chapter is organized into the following sections:
You can use the Sun Studio dbx to debug mixed code (Java code and C code or C++ code) running under the Solaris operating environment.
You can debug several types of Java applications with dbx (see Starting to Debug a Java Application). Most dbx commands operate similarly on native code and Java code.
dbx has the following limitations when debugging Java code:
The following environment variables are specific to debugging a Java application with dbx. You can set the JAVASRCPATH, CLASSPATHX, and jvm_invocation environment variables at a shell prompt before starting dbx. The setting of the jdbx_mode environment variable changes as you are debugging your application. You can change its setting with the jon command (jon Command) and the joff command (see joff Command).
The jdbx_mode environment variable can have the following settings: java, jni, or native. For descriptions of the Java, JNI, and native modes, and how and when the mode changes, see dbx Modes for Debugging Java Code. Default: java. |
|
You can use the JAVASRCPATH environment variable to specify the directories in which dbx should look for Java source files. This variable is useful when the Java sources files are not in the same directory as the .class or .jar files. See Specifying the Location of Your Java Source Files for more information. |
|
The CLASSPATHX environment variable lets you specify to dbx a path for Java class files that are loaded by custom class loaders. For more information, see Specifying a Path for Class Files That Use Custom Class Loaders. |
|
The jvm_invocation environment variable lets you customize the way the JVM |
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.
You can debug a file that uses the .class file name extension using dbx as in the following example.
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.
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.
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.
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:
Note - If you have specified a custom startup of the JVM software using the jvm_invocation environment variable (see Customizing Startup of the JVM Software, the file name of the JAR file is not automatically added to the class path In this case, you must add the file name of the JAR file to the class path when you start debugging. |
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).
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 process to start debugging.
For the JVM software to locate libdbx_agent.so, you need to add installation_directory/SUNWspro/lib to LD_LIBRARY_PATH before running the Java application, where install_directory is the location where dbx was installed. If you are using the 64-bit version of the JVM software, you need to add installation_directory/SUNWspro/lib/v9 to LD_LIBRARY_PATH.
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.
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:
For the JVM software to locate libdbx_agent.so, you need to add install_directory/current/lib to LD_LIBRARY_PATH before running the Java application, where install_directory is the location where dbx was installed. If you are using the 64-bit version of the JVM software, you need to add install_directory/current/lib/v9 to LD_LIBRARY_PATH.
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.
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.
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.
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.
To set a stop breakpoint on a Java method in a class file that has not been loaded by the JVM software, use the full name of the class with a stop in command, or the class name with a stop inmethod command. See the following example.
To set a stop breakpoint on a C function or C++ function in a shared library that has not been loaded by the JVM software, preload the symbol table of the shared library before setting the breakpoint. For example, if you have a library named mylibrary.so that contains a function named myfunc, you could preload the library and set a breakpoint on the function as follows:
You can also load the symbol tables of all dynamically loaded shared objects by running your application once before beginning to debug it with dbx.
You might need to customize startup of the JVM software from dbx to do the following:
You can customize startup of the JVM software using the jvm_invocation environment variable. By default, when the jvm_invocation environment variable is not defined, dbx starts the JVM software as follows:
When the jvm_invocation environment variable is defined, dbx uses the value of the variable to start the JVM software.
You must include the -Xdebug option in the definition of the jvm_invocation environment variable. dbx expands -Xdebug into the internal options -Xdebug
-Xnoagent -Xrundbxagent::sync.
If you do not include the -Xdebug option in the definition, as in the following example, dbx issues an error message.
To specify a path name for the JVM software, set the jvm_invocation environment variable to the appropriate path name, as in the following example.
This causes dbx to start the JVM software as follows:
To pass run arguments to the JVM software, set the jvm_invocation environment variable to start the JVM software with those arguments, as in the following example.
This causes dbx to start the JVM software as follows:
A Java application can use a custom wrapper for startup. If your application uses a custom wrapper, you can use the jvm_invocation environment variable to specify the wrapper to be used, as in the following example.
This causes dbx to start the JVM software as follows:
The following wrapper script (xyz) sets a few environment variables and accepts command line options:
This script accepts some command line options for the JVM software and the user application. For wrapper scripts of this form, you would set the jvm_invocation environment variable and start dbx as follows:
The following wrapper script (xyz) sets a few environment variables and starts the JVM software, but does not accept any command line options or a class name:
#!/bin/sh CLASSPATH=/mydir/myclass:/mydir/myjar.jar; export CLASSPATH ABC=/mydir/abc; export ABC java <options> myclass |
You could use such a script to debug a wrapper using dbx in one of two ways:
Once you have made this modification, you could start the debugging session by running the script.
Once you made this modification, you would set the jvm_invocation environment variable and start dbx as follows:
If you want dbx to start 64-bit JVM software to debug an application that requires 64-bit object libraries, include the -d64 option when you set the jvm_invocation environment variable:
When debugging a Java application, dbx is in one of three modes:
When dbx is Java mode or JNI (Java Native Interface) mode, you can inspect the state of your Java application, including JNI code, and control execution of the code. When dbx is in native mode, you can inspect the state of your C or C++ JNI code. The current mode (java, jni, native) is stored in the environment variable jdbx_mode.
In Java mode, you interact with dbx using Java syntax and dbx uses Java syntax to present information to you. This mode is used for debugging pure Java code, or the Java code in an application that is a mixture of Java code and C JNI code or C++ JNI code.
In JNI mode, dbx commands use native syntax and affect native code, but the output of commands shows Java-related status as well as native status, so JNI mode is a "mixed" mode. This mode is used for debugging the native parts of an application that is a mixture of Java code and C JNI code or C++ JNI code.
In native mode, dbx commands affect only a native program, and all Java-related features are disabled. This mode is used for debugging non-Java related programs.
As you execute your Java application, dbx switches automatically between Java mode and JNI mode as appropriate. For example, when it encounters a Java breakpoint, dbx switches into Java mode, and when you step from Java code into JNI code, it switches into JNI mode.
dbx does not switch automatically into native mode. You can switch explicitly from Java or JNI Mode to native mode with the joff command, and from native mode to Java mode with the jon command.
If you interrupt execution of your Java application (for example, with a control-C), dbx tries to set the mode automatically to Java/JNI mode by bringing the application to a safe state and suspending all threads.
If dbx cannot suspend the application and switch to Java/JNI mode, dbx switches to native mode. You can then use the jon command to switch to Java mode so that you inspect the state of the program.
When you are using dbx to debug a mixture of Java and native code, dbx commands fall into several categories:
Any commands not included in one of these categories work only in native mode.
The Java expression evaluator used in most dbx commands supports the following constructs:
The Java expression evaluator does not support the following constructs:
A particularly useful way of inspecting the state of your Java application is using the display facility in the dbx Debugger.
Depending on precise value semantics in expressions that do more than just inspect data is not recommended.
Much of the information about a Java application is normally available only after the JVM software has started, and is unavailable after the Java application has finished executing. However, when you debug a Java application with dbx, dbx gleans some of the information it needs from class files and JAR files that are part of the system class path and user class path before it starts the JVM software. This allows dbx to do better error checking on breakpoints before you run the application.
Some Java classes and their attributes might not be accessible through the class path. dbx can inspect and step through these classes, and the expression parser can access them, once they are loaded. However, the information it gathers is temporary and is no longer available after the JVM software terminates.
Some information that dbx needs to debug your Java application is not recorded anywhere so dbx skims Java source files to derive this information as it is debugging your code.
The following dbx commands have the same syntax and perform the same operations in Java mode as in native mode.
Attaches dbx to a running process, stopping execution and putting the program under debugging control |
|
Maps one path name to another for finding source files, etc. |
|
The following dbx commands have different syntax for Java debugging than for native code debugging, and operate differently in Java mode than in native mode.
The following dbx commands are valid only in Java mode or JNI mode.
Copyright © 2004, Sun Microsystems, Inc. All rights reserved.