Java Debugger

Java debugger helps in finding and the fixing of bugs in Java language programs. The Java debugger is denoted as jdb.

Java Debugger

Java debugger helps in finding and the fixing of bugs in Java language programs. The Java debugger is denoted as jdb.

Java Debugger

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.<init> (<init> 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<option>.
Some more options by jdb:
-sourcepath <dir1:dir2:...>
Uses the given path in searching for source files in the specified path. If this option is not specified, the default path of "." is used. 
-attach <address>
Attaches the debugger to previously running VM using the default connection mechanism. 
-launch
As soon as the jdb start sup,launch option launches the debugged application immediately. We need not to use the run command after using this option. 
-Joption
It passes option to the Java virtual machine, where option is one of the options described on the reference page for the java application launcher. For example,
-J-Xms48m sets the startup memory to 48 megabytes.

Lets tweak an example:

public class Y {
     public static int add(int a, int b)
	{
		return a+b;
	}
	public static int sub(int a, int b)
	{
		return a-b;
	}
	public static void main(String args[]) 
	{
		int a = 10;
		int b = 20;
		int c;		
		c = add(a, b);
        System.out.println(c);
		c = sub(a, b);
		System.out.println(c);
  } 
}

 

After compiling and running the above program, we will initialize the java debugger and we will use the Stop command to out a breakpoint. After that we will use run command to start the jdb. In the similar way we can use other commands as well.

C:\javac>jdb Y
Initializing jdb ...
> stop at Y:6
Deferring breakpoint Y:6.
It will be set after the class is loaded.
> run
run Y
Set uncaught java.lang.Throwable
Set deferred uncaught java.lang.Throwable
>
VM Started: Set deferred breakpoint Y:6

Breakpoint hit: "thread=main", Y.add(), line=6 bci=0
6 return a+b;

main[1] where
[1] Y.add (Y.java:6)
[2] Y.main (Y.java:17)
main[1] methods Y
** methods list **
Y <init>()
Y add(int, int)
Y sub(int, int)
Y main(java.lang.String[])
java.lang.Object <init>()
java.lang.Object registerNatives()
java.lang.Object getClass()
java.lang.Object hashCode()
java.lang.Object equals(java.lang.Object)
java.lang.Object clone()
java.lang.Object toString()
java.lang.Object notify()
java.lang.Object notifyAll()
java.lang.Object wait(long)
java.lang.Object wait(long, int)
java.lang.Object wait()
java.lang.Object finalize()
java.lang.Object <clinit>()