home page - news - syllabus - schedule - assignments - tutorials - tests - java - references - Mike Jacobson |
basics - debugger (jdb) - testing framework (JUnit) - profiler (JRat) - documentation (javadoc) |
Java Resources - Debugger (jdb) |
As you will read when you execute the command
man jdb
when using your computer science account, jdb is “a simple command-line debugger for Java classes.” You have access to this at school and — if you are able to compile and run Java programs there — you almost certainly have access this at home, as well.
We have already seen tools you can use to examine, and validate, the output generated by a method: Combined with some additional programming, JUnit is sufficient for this.
However, it is sometimes important for us to know the values of variables at a specific point during the execution of a method, before its termination. For example, you may wish to examine the value of one or more variables at the beginning of each execution of the body of a loop in order to check whether the loop is doing what it is supposed to. Unless you modify your code to add some “print” statements, it is not clear how you can see these values.
Of course, you can try to use desk checking to see what software is doing during a computation. However, this kind of hand checking of software can be error-prone, especially if the person checking the software is the same as the person who wrote it — we all tend to miss the same mistake over and over again. Furthermore, hand checking is hard to carry out if the computation you wish to check is long or complicated.
When you use a debugger it is the computer that is executing your program, so that you can be confident that you really are learning about what the computer is doing (instead of what you think it is doing) when your progrm is run.
You should not have to do anything at all in order to install jdb: It is definitely available at school and was almost certainly installed at home (if you have set up a Java development environment at home) at the same time as javac was.
At school, you should be able to execute the command
which jdb
in order to confirm that jdb is available: You should receive a path to this utility, like /bin/jdb or /usr/bin/jdb, as a response.
After this, you should execute the command
man jdb
in order to see some useful online documentation about this tool.
To continue, execute the command
jdb
You should see something like the following, which tells you that jdb is starting up and prompts you for your first command:
Initializing jdb... >
If you execute the command
help
at this point then you will see a list of the commands you can execute along with a short description of each — similar to some of the information that you read on the man page for this utility. Commands that you will be using, if you use this debugger, include
stop — used to set “breakpoints” in your program.
run — causes the program to be executed until either the first breakpoint is reached or the program terminates (whichever happens first). You will probably use this command once, after some breakpoints have been set, and then you will use the remaining commands in order to continue.
step — used to execute the next statement in your program.
cont — causes execution to be continue until the next breakpoint is reached or the program terminates (whichever comes first).
print — used to display the value of an expression or a description of an object.
dump — provides more detailed information about an object. For example, this can be used to see all the entries of a given array.
You should execute the command quit when you’ve read enough, in order to stop using the debugger for now.
Of course, things are more interesting when you actually run jdb on a program to inspect or debug it! A self-study exercise in which you will be doing this as you work through the problems in it, is now available.
You should note that you have access to other Java debuggers too. Both NetBeans and Eclipse can be used to debug Java programs, and Eclipse is installed on the computers available for you to use in the computer science department.
You are free to use whichever debugger you wish to. Please note, though, that the CPSC 331 insructors and TAs will probably not have the time to provide additional information about other debuggers besides jdb, or help you to use them.
Last updated:
http://www.cpsc.ucalgary.ca/~jacobs/Courses/cpsc331/W12/java/debugger.html |