Search This Blog

Thursday, November 5, 2009

Java Debugger

Java debugger helps in finding and the fixing of bugs in Java language programs. The Java debugger is denoted as jdb. It works like a command-line debugger for Java classes.

jdb session

The way to start the jdb session is to have jdb launch a new JVM (Java virtual machine) with the main class. Then the application of the main class is debugged by substituting the command jdb in command-line. For instance, if the main class of the application is TempClass, the following command is used to debug it:

% jdb TempClass

There is another way to use jdb i.e.to attach jdb to Java VM which is already running.

There are few options which are used to debug a VM with jdb. These are:option purpose
-Xdebug Enables debugging in the VM
-Xrunjdwp:transport=dt_socket,server=y,suspend=n Loads in-process debugging libraries and specifies the kind of connection to be made


The following command will run the TempClass application to which the jdb will connect afterwords.

% java -Xdebug -Xrunjdwp:transport=dt_socket,address=8000,server=y,suspend=n TempClass

Now the jdb will be attached to the VM in this way: % jdb -attach 8000

You can go through the basic jdb commands that the Java debugger supports.

cont
Ihis command Continues the execution of the debugged application after a breakpoint, exception, or step.

run
Use run command to start the execution after starting jdb, and setting any necessary breakpoints, use run command to start the execution . When jdb launches the debugged application then only this command is available.

print
This command is used to display Java objects and primitive values. The actual value is printed for the variables or fields of primitive types. For objects, a short description is printed.

Few examples of print command are:
print TempClass.myStaticField
print myObj.myInstanceField
print myObj.myMethod()

dump
This command is similar to print command. For objects, it is used to print the current value of each field defined in the object including Static and instance fields.
The dump command supports the same set of expressions as the print command.

Exceptions
If any kind of exception occurs in the program, the VM will print an exception trace and exits. It will only print this exception when there isn't any catch statement in the throwing thread's call stack.
There is another command to be used to stop the debugged applications at other thrown exceptions. The command is catch command. For instance, "catch java.io.FileNotFoundException" or "catch mypackage.BigTroubleException.
Also the ignore command negates the effect of a previous catch command.

help, or ?
The command which helps in displaying the list of recognized commands is the help or ? command.

threads
This command list the threads that are currently running. The name and current status are printed for each thread. The index is also printed that can be used for other commands, for example:
4. (java.lang.Thread)0x1 main running
This example shows that the thread index is 4, the thread is an instance of java.lang.Thread, the thread name is "main", and it is currently running.

thread
This command selects a thread to be the current thread. Some jdb commands are based on the setting of the current thread. The thread index specifies the thread as explained in the threads command above.

where
The command where is used to dump the stack of the current thread. Whereas the command where all is used to dump the stack of all threads in the current thread group. And the where thread index command is used to dump the stack of the specified thread.

Breakpoints
The way to use Breakpoints is to set it in jdb at line numbers or at the first instruction of a method, for example:
stop at TempClass:14 (sets a breakpoint at the first instruction for line 14 of the source file containing TempClass)
stop in TempClass. ( identifies the TempClass constructor)
It is essential to specify the argument types whenever we overload any method in order to select the proper method for a breakpoint. For example, "TempClass.myMethod(int,java.lang.String)", or "TempClass.myMethod()".
We also use the clear command to remove breakpoints using a syntax as in "clear TempClass:20" and the cont command continues execution.

Command Line Options

Use jdb in place of the Java application launcher on the command line, it accepts most of the same options as the java command, which includes -D, -classpath, and -X

No comments:

Post a Comment