You are here

Debugging

15 January, 2016 - 09:02
Available under Creative Commons-ShareAlike 4.0 International License. Download for free at http://cnx.org/contents/402b20ad-c01f-45f1-9743-05eadb1f710e@37.6

To debug a program in DrJava, first put DrJava into Debugging Mode by setting the check box under the Debugger on the main menu. This will enable all the other debugging features. When debugging mode is active, the debugging pane will appear near the bottom of the DrJava window. This pane contains the Watch window and tabs that show the current Stack and Threads status. The debugging pane also has buttons for Resume debugging, Step Into, Step Over and Step Out. These features are described in detail below.

The basic technique for debugging is to set breakpoints on lines of code that are of interest/problematic and then to either step slowly through the code execution from that point and/or to examine the values of various variables.

Breakpoints: Under debugging mode, DrJava will stop the execution of a program whenever a breakpoint is encountered. Execution stops before the breakpoints line of code is run. To set or clear a breakpoint, place the cursor on the desired line of code and either use the Debugger/Toggle Breakpoint on Current Line or by simply pressing Ctrl-B or by right-clicking the desired line and selecting Toggle Breakpoint. Note that it only makes sense to put a breakpoint on a line of code that actually executes, which excludes

  • Blank lines
  • Comments
  • Method signature lines
  • Lines with a single curly brace on them.
  • Field/attribute declarations that have no initialization code.

To clear a breakpoint, follow the same procedure used to set the breakpoint.

Whenever the program execution is stopped on a line of code, the Interactions pane can be used to examine the value of any variable that is in scope or even to execute methods on those variables (if they are objects). You can even set the values of any available variables by assigning a new value to them. This is useful if they have the wrong value for some reason and you want to check if the rest of your code will run properly if the correct value is present.

Clear All Breakpoints: Clicking on Debugger/Clear All Breakpoints will clear all breakpoints that have been set in your program.

Clicking on Debugger/Breakpoints or pressing Ctrl+Shift+B will bring up a tabbed pane at the bottom of the DrJava window that enables you to conveniently examine, navigate to, delete, or disble (without deleting) all of the breakpoints in your program.

Step Into: When execution has stopped on a line of code, clicking on the Step Into button in the debugging pane or selecting the menu item Debugger/Step Into or pressing F12 will cause the current line of code (highlighted in blue) to be executed. If the current line of code involves the invocation of a method, then the current line of code will be advanced into the first method to be called, as per Java's order of execution rules. If the current line of code is the last line in a method, the current line of code will become the next executable line of code in the method that called the current method. Note that if the caller's line of code involved multiple method calls, then the current line of code will return to that line of code in the caller and then execution will advance into the next method to be called, or if the current method was the last method to be called, the current line of code will be the next line of code in the caller.

Step Over: When execution has stopped on a line of code, clicking on the Step Over button in the debugging pane or selecting the menu item Debugger/Step Over or pressing F11 will cause the current line of code to be executed. This is very similar to Step Into, but if the execution will not stop inside of any methods called in the current line of code. Thus the new current line of code will always be in the same method, unless the executed line was the last line in the method, upon which execution will stop in the calling method as usual. This feature is very useful when you are confident that the methods being called work properly and you are not interested in their detailed execution.

Step Out: When execution has stopped on a line of code, clicking on the Step Out button in the debugging pane or selecting the menu item Debugger/Step Out or pressing Shift+F12 will cause the execution to resume until the current method exits. Execution will stop at the next executable line in the calling method. This is very useful to quickly exit a method whose detailed execution no longer interests you (for instance, if you accidentally stepped into a method that you wanted to step over).

Resume Debugging: When execution has stopped on a line of code, clicking on the Resume button in the debugging pane or selecting the menu item Debugger/Resume Debugging or pressing F7 will cause the program execution to resume and continue until the next breakpoint is encountered. This is useful when you are no longer interested in the detailed execution of the code until the next breakpoint.

Watches: Watches are used when you want to continuously monitor the value of a particular variable. Simply type the name of the variable or field into the Watch pane and when code execution has paused, if that variable name is in scope, its value will be displayed.

Stack: The stack contains an ordered list of all methods with pending operations at any given point of a program's execution. That is, when the program execution has stopped at a particular line of code, the stack contains the method that line of code is in, the method that called that method, the method that called that method, etc. In DrJava, the Stack tab will show the stack with the current method at the top and the calling method below it. Looking at the stack is very useful for determining where in your program you actually are, as often a method is called from locations that are unintended or one discovers that a method is not being called from where you wish it to be called.

Threads: A Java program run on many simultaneous execution paths or "threads", each performing different tasks necessary for the total operation of a Java application. A simple program written by a programmer may have a single thread that runs the code written by that programmer, but may also have many other threads performing such tasks as managing the graphical user interface ("GUI") or managing the memory allocated to the program. It is also possible for a programmer to write code that utilizes multiple threads of execution to accomplish the desired goals. The Threads tab is most useful for debugging these multi-threaded programs by enabling the developer to better understand which threads are accessing which objects.