Contained WithinFind More DocumentationFeatured Support Resources | Scarica il manuale in formato PDF (2163 KB)
Chapter 17 Debugging a Java Application With dbxThis chapter describes how you can use dbx to debug an application that is a mixture of JavaTM code and C JNI (Java Native Interface) code or C++ JNI code. The chapter is organized into the following sections: Using dbx With Java CodeYou can use the Sun Studio dbx to debug mixed code (Java code and C code or C++ code) running under the SolarisTM OS and the Linux OS. Capabilities of dbx With Java CodeYou 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. Limitations of dbx With Java Codedbx has the following limitations when debugging Java code:
Environment Variables for Java DebuggingThe 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, or from the dbx command line. 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).
Starting to Debug a Java ApplicationYou 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 FileYou 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.
Debugging a JAR FileA 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.
For each of these examples dbx would do the following:
Debugging a Java Application That Has a WrapperA 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 ApplicationYou 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.
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.
Debugging a C Application or C++ Application That Embeds a Java ApplicationYou 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 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 SoftwareWhen 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 FilesSometimes 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 Filesdbx 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 LoadersAn 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 MethodsUnlike native applications, Java applications do not contain an easily accessible index of names. So you cannot simply type:
Instead, you need to use the full path to the method:
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:
But doing so might cause stops in multiple methods name myMethod. Setting Breakpoints in Native (JNI) CodeThe 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. Customizing Startup of the JVM SoftwareYou 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.
Specifying a Path Name for the JVM SoftwareBy default, dbx starts the JVM software in your path if you do not specify a path name for the JVM software. 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 setting causes dbx to start the JVM software as follows:
Passing Run Arguments to the JVM SoftwareTo 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:
Specifying a Custom Wrapper for Your Java ApplicationA 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:
Using a Custom Wrapper That Accepts Command-Line OptionsThe 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:
Using a Custom Wrapper That Does Not Accept Command-Line OptionsThe 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:
You could use such a script to debug a wrapper using dbx in one of two ways:
Specifying 64-bit JVM SoftwareIf 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:
dbx Modes for Debugging Java CodeWhen 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. Switching from Java or JNI Mode to Native Modedbx 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. Switching Modes When You Interrupt ExecutionIf 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. Using dbx Commands in Java ModeWhen 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 Evaluation in dbx CommandsThe 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 watch facility in the IDE or dbxtool. Depending on precise value semantics in expressions that do more than just inspect data is not recommended. Static and Dynamic Information Used by dbx CommandsMuch 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. Commands With Identical Syntax and Functionality in Java Mode and Native ModeThe following dbx commands have the same syntax and perform the same operations in Java mode as in native mode.
Commands With Different Syntax in Java ModeThe following dbx commands have different syntax for Java debugging than for native code debugging, and operate differently in Java mode than in native mode.
Commands Valid Only in Java ModeThe following dbx commands are valid only in Java mode or JNI mode.
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||