Some Advanced Notes about Debugging
As a particular method of a Java class is called, certain variables and objects will be in scope. The grouping of variables and objects in scope is referred to as a stack. It is common for one method to call another method in a Java program (as the main method called the adder method in our demonstration application). Collectively, the stacks that are existent at any point in time make up the stack frame.
One of the powerful advantages of jdb is the ability to use the up and down commands. The up command can be used to navigate "up" the stack frame. In essence, you can rewind to see how your stack looked before a method was called. Similarly, the down command lets us see how our stack looks after a method is called.
Take a look at the debugging session shown in Figure 1, which demonstrates the use of up and down commands and leverages the locals command to see what variables are in scope and what their value is.
Figure 1 A debugging session showcasing the use of the up and down commands.
You can use the catch command followed by the exception name you want to pause execution at if a provided exception type is thrown (for example, java.io.FileNotFoundException). We have not shown it here, but you can use this in tandem with the ability to pan up and down the stack frame to possibly diagnose what caused your exception to occur.
At any point during the debugging session, we can use the command "classes" to see what classes have been loaded into memory. Also, we can use the "methods" command followed by the class name we are interested in to list the methods of the class we specify.