Collection Contents Index A debugging tutorial PART 4.  Database Administration and Advanced Use pdf/chap19.pdf

User's Guide
   PART 3. Java in the Database
     CHAPTER 19. Debugging Java in the Database       

Using the debugger


This section describes how to accomplish tasks using the Java debugger.

Top of page  Starting the debugger

The debugger is the Jar file Debug.jar, installed into your Adaptive Server Anywhere installation directory. This jar file contains many classes. To start the debugger you invoke the sybase.vm.Debug class, which has a main method.

  To start the debugger:
  1. Ensure that Debug.jar and jdbcdrv.zip are in your CLASSPATH environment variable.

  2. Enter the following command at the command line:

    java sybase.vm.Debug

Connecting to a database on startup 

When you start the debugger in the simple manner described above, you need to provide entries into the text boxes in order to connect to a database. You can also supply additional command-line arguments to the debugger to connect on startup.

You can specify these connection parameters in the following ways:

Top of page  Compiling classes for debugging

Java compilers such as the Sun Microsystems javac compiler can compile Java classes at different levels of optimization. You can opt to compile Java code so that information used by debuggers is retained in the compiled class files.

If you compile your source code without using switches for debugging, you can still step through code and use breakpoints. However, you cannot inspect the values of local variables.

To compile classes for debugging using the javac compiler, use the -g command-line option:

javac -g ClassName.java

Top of page  Attaching to a VM

When you connect to a database from the debugger, the Connection window shows all currently active VMs under the user ID. If there are none, the debugger goes into wait mode. Wait mode works like this:

Top of page  The Source window

The Source window serves the following purposes:

Top of page  The debugger windows

The debugger has the following windows:

Top of page  Setting breakpoints

When you set a breakpoint in the debugger, the VM stops execution at that breakpoint. Once execution is stopped, you can inspect and modify the values of variables and other expressions in order to better understand the state of the program. You can then trace through execution step by step to identify problems.

Setting breakpoints in the proper places is a key to efficiently pinpointing the problem execution steps.

The Java debugger allows you to set breakpoints not only on a line of code, but on many other conditions. This section describes how to set breakpoints using different conditions.

Top of page  Breaking on a line number

When you break on a particular line of code, execution stops whenever that line of code is executed.

  To set a breakpoint on a particular line:
  1. In the Source window, select the line and press F9.

    Alternatively, you can double-click a line.

When a breakpoint is set on a line number, the breakpoint is shown in the Source window by an asterisk in the left hand column. If the Breakpoints window is open, the method and line number is displayed in the list of breakpoints.

You can toggle the breakpoint on and off by repeatedly double-clicking or pressing F9.

Top of page  Breaking on a class method

When you break on a method, the break point is set on the first line of code in the method that contains an executable statement.

  To set a breakpoint on a class method:
  1. From the Source window, choose Break-> New. The Break At window is displayed.

  2. Enter the name of a method in which you wish execution to stop. For example

    JDBCExamples.Query

    stops execution whenever the JDBCExamples.Query method is entered.

When a breakpoint is set on a method, the breakpoint is shown in the Source window by an asterisk in the left hand column of the line where the breakpoint actually occurs. If the Breakpoints window is open, the method is displayed in the list of breakpoints.

Top of page  Using counts with breakpoints

If you set a breakpoint on a line that is in a loop, or in a method that is frequently invoked, you may find that the line is executed many times before the condition you are really interested in takes place. The debugger allows you to associate a count with a breakpoint, so that execution stops only when the line is executed a set number of times.

  To associate a count with a breakpoint:
  1. From the Source window, select Break->Display. The Breakpoints window is displayed.

  2. In the Breakpoints window, click a breakpoint to select it.

  3. Select Break->Count. A window is displayed with a field for entering a number of iterations. Enter an integer value. The execution will stop when the line has been executed the specified number of times.

Example 

This example assumes you have run the tutorial described in A debugging tutorial.

The JDBCExamples.Query method used in the tutorial has a loop in it:

while( result.next() ) {
   int price = result.getInt(2);
   if( price > max_price ) {
     max_price = price ;
   }

The lines inside this loop are executed ten times. You can set a breakpoint on the if statement and associate a count of two with it. Then execute the query to stop at the breakpoint.

Count is decremented 

The count is decremented each time the line of code is executed. A side effect of this is that once the breakpoint is reached, the count is set to zero.

Top of page  Using conditions with breakpoints

The debugger allows you to associate a condition with a breakpoint, so that execution stops only when the line is executed and the condition is met.

  To associate a condition with a breakpoint:
  1. From the Source window, select Break->Display. The Breakpoints window is displayed.

  2. In the Breakpoints window, click a breakpoint to select it.

  3. Select Break->Condition. A window is displayed with a field for entering an expression. The execution will stop when the condition is true.

    The expressions used here are the same as those that can be used in the Inspection window, and include the following:

Example 

This example assumes you have run the tutorial described in A debugging tutorial.

The JDBCExamples.Query method used in the tutorial has a loop in it:

while( result.next() ) {
   int price = result.getInt(2);
   if( price > max_price ) {
     max_price = price ;
   }

The lines inside this loop are executed ten times. You can set a breakpoint on the if statement and associate the following condition with it:

price > 10

Then execute the query to stop at the breakpoint whenever the price is greater than $10.

Top of page  Breaking when execution is not interrupted

With a single exception, breakpoints can only be set when program execution is interrupted. If you clear all breakpoints, and run the program you are debugging to completion, you can no longer set a breakpoint on a line or at the start of a method. Also, if a program is running in a loop, execution is continuing and is not interrupted.

To debug your program under either of these conditions, select Run->Stop from the Source window. This stops execution at the next line of Java code that is executed. You can then set breakpoints at other points in the code.

Top of page  

Collection Contents Index A debugging tutorial PART 4.  Database Administration and Advanced Use pdf/chap19.pdf