home page - news - syllabus - schedule - assignments - tutorials - tests - java - references - Mike Jacobson |
basics - debugger (jdb) - testing framework (JUnit) - profiler (JRat) |
Java Resources - Debugger (jdb) |
The Java debugger “jdb” is installed on all Unix/Linux machines in the Department of Computer Science. It is located in the directory
/usr/bin
on at at least some of the department&rsqou;s compute servers, and you probably do not need to make any changes to your setup in order to use this.
You should be able to use the following instructions to use this debugger to find errors in your programs.
Recompile Your Code
In order to use jdb you must first recompile your Java files using the “–g” switch. This will add debugging information to the byte code generated by javac.
Start jdb
Start jdb by typing the following:
jdb ClassName
where ClassName is the name of the class you would normally use to run your program (this class must have a main method).
Add Break Points to the Program
“Break points” are usually placed at important junctures in the program, or places where you think an error might be occurring. When your program runs in jdb the debugger will halt execution every time it reaches a break point.
Break points can be placed in the following ways:
By Method Name: A break point can be placed at the point where program flow enters a method by typing
stop in ClassName.MethodName
where ClassName is the name of a class and MethodName is the name of a method at which you want the debugger to stop.
It is only necessary to list the class name (as above) when the method occurs in a class that is not the one you loaded when you started jdb; this is possible if your program includes more than one class.
By Line Number: You can tell jdb to stop at any line number in your program by typing
stop at ClassName:n
were ClassName is the name of a class and n is the number of the line where you want to specify the break point.
Start Your Program
You can start the execution of your program by typing
run ClassName
where ClassName is the name of the class you loaded when you started jdb.
Look Around
When the program reaches a break point jdb will give a prompt. You may choose to do a number of things after this.
Step: The “step” command will tell jdb to continue execution until the next break point is reached.
Continue: The “cont” command can be used to tell jdb to continue execution of the program until it terminates.
List: If you use the “list” command, jdb will show you the code of the method that is being executed, with an arrow showing exactly where program execution currently is.
Examine Local Variables: The “locals” command can be used to get a listing of all local variables and their current values.
Examine Specific Values: The command
print VariableName
can be used to see the value of the variable with name VariableName.
Exit: The “quit” command can be used at any time to end the use of jdb.
You can see the Unix manual page for jdb by typing
man jdb
at the usual command line prompt.
Several other tutorials for jdb are available online. Indeed, several can be found from google by searching for “jdb + Unix.”
Chapter 8 of “Big Java” (on “Testing and Debugging”) includes a tutorial for the use of a different Java debugger in which a specific program is debugged. It might be helpful for you to try to debug the same program using jdb, in the same way, in order to have a better idea about how a debugger can be used.
Karel Bergmann provided the instructions that appear at the beginning of this page.
Please send email to the instructor if you found mistakes on this page or can suggest other information about jdb that will helpful for other students in CPSC 331.
This page last modified:
http://www.cpsc.ucalgary.ca/~jacobs/cpsc331/F08/java/debugger.html |