Java Resources, CPSC 331, Fall 2008

home page -  news -  syllabus -  schedule -  assignments -  tutorials -  tests -  java -  references -  Mike Jacobson


basics -  debugger (jdb) -  testing framework (JUnit) -  profiler (JRat)

 Java Resources - Debugger (jdb)

Instructions for Use

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.

  1. 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.

  2. 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).

  3. 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:

    1. 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.

    2. 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.

  4. 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.

  5. 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.

    1. Step: The “step” command will tell jdb to continue execution until the next break point is reached.

    2. Continue: The “cont” command can be used to tell jdb to continue execution of the program until it terminates.

    3. 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.

    4. Examine Local Variables: The “locals” command can be used to get a listing of all local variables and their current values.

    5. Examine Specific Values: The command

      print VariableName

      can be used to see the value of the variable with name VariableName.

    6. Exit: The “quit” command can be used at any time to end the use of jdb.

Additional Information

Thanks, Karel!

Karel Bergmann provided the instructions that appear at the beginning of this page.

Help Us To Improve This!

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