Stepping through code in the EGL debugger

This topic introduces you to debugging a program in the EGL debugger.

To debug a sample program, create an EGL basic project, create a package named “common,” and copy the following code into a source file in that package:
package common;

program MyProgram 

  function main()

    //Provide some initial values for the array of items.
    customerItems Items[] = new Items[3];
    customerItems[1].itemNumber=1;
    customerItems[2].itemNumber=2;
    customerItems[3].itemNumber=3;
    customerItems[1].itemCost=12.50;
    customerItems[2].itemCost=200;
    customerItems[3].itemCost=49.95;
    customerItems[1].itemQuantity=30;
    customerItems[2].itemQuantity=10;
    customerItems[3].itemQuantity=60;

    counter int;
    orderTotal float=0;

    //Calculate the total cost of the items.
    //Use the discountPrice function to get the discounted cost of each item.
    for (counter from 1 to customerItems.getSize() by 1)
      orderTotal += discountPrice(customerItems[counter].itemCost, 
                                  customerItems[counter].itemQuantity);
    end // for loop

    //Write the output to the console.
    SysLib.writeStderr("The total cost for the order is $" + orderTotal);
  end // main

  //Return a total price for a group of items 
  //based on the item price and a quantity discount.
  function discountPrice(itemCost float in, itemQuantity int in) returns(float)
    discountRate float=0;
    quantCost float=0;

    //Determine the discount for each quantity.
    //Discount 20% for more than 50 items.
    //Discount 5% for more than 20 items.
    case
      when (itemQuantity > 50)
        discountRate = 1/5;
     when (itemQuantity > 20 && itemQuantity <= 50)
       discountRate = 1/20;
     otherwise
       //bug - division by zero
       discountRate = 1/0;
     end

    //Multiply the cost of the item, the number of items, 
    //and the discounted price.
    quantCost = itemCost*itemQuantity*(1-discountRate); 
    quantCost = MathLib.round(quantCost, -2);

    return (quantCost);
  end // function discountPrice

end // program

Record Items 
  itemNumber    int;
  itemCost      float;
  itemQuantity  int;
end
The heart of the debugging process is to identify the source of a problem in the code. If you generate the example program and run it, EGL returns an error pointing to the discountPrice function and the expression 1/0. Assume that you are looking for a runtime problem that is harder to find.

The debugger always starts debugging from the logic that runs initially. If you want to debug a library, you must step into the library. In some cases you might benefit from writing a simple program that does little more than to call the logic that you want to debug.

To step through an application in the EGL debugger, do as follows:
  1. Set one or more breakpoints, which are locations at which execution pauses.

    After the debugger pauses at a breakpoint, you can check the current values of program variables before telling the debugger how to proceed. Breakpoints do not affect the generated source in any way; they are meaningful only during the debugging session.

    To set a breakpoint, double-click the gray margin to the left of the code in the EGL editor. In the current example, you might want to add breakpoints throughout the discountPrice function because an error told you that this function is where the error occurred. Breakpoints are marked with blue circles in the leftmost gray area:

    Picture of the blue circles indicating breakpoints in the code.

    You can add a breakpoint at most EGL logic statements, though not at the following statements:
    • A variable declaration that does not include an assignment.
    • A line of code that begins a logical unit or that gives a directive. Examples are the package declaration or the line that begins with program.
    • An end statement.
    • A blank line or a line that consists only of a comment.
  2. Run a program in the EGL debugger.

    After you have added breakpoints to your program, you can run it in the debugger.

    The debugger requires a launch configuration to describe how it will run the application. You can create a launch configuration in either of two ways: In most cases, you can use the automatically created launch configuration.
    1. In the Project Explorer view, right-click the EGL source program that you want to debug and then click Debug as > EGL Java Main Application . The debugger performs the following tasks:
      • If no launch configuration exists for the program, the debugger creates a default launch configuration. You can view this configuration by clicking Run > Debug .
      • Depending on your workbench preferences, the debugger might switch to the Debug perspective automatically or prompt you to do so. You can switch perspectives manually by clicking Window > Open Perspective > Other > Debug.
      • The debugger begins running the program.
    2. After the debugger has started running the program, it continues until it encounters a breakpoint, or, if you have set the preference for it, finds the first line of executable code. At this point, the debugger pauses and displays the following information:
      • The EGL editor highlights the line about to be executed.
      • The Variables view shows the value of all the variables in the current logic part, including the value of system variables. You can use this view to track the value of a variable through the program. You can also change the value of a variable while the debugger is paused at a breakpoint.
      • The Debug view lists the threads running within the current run unit. In simple terms, this view shows which program or logic part is currently running. Use this view to resume or stop the debugging process.
      • The Breakpoints view lists the breakpoints in the program. From this view, you can disable a breakpoint temporarily by clearing its check box.
    3. When you want the debugger to continue, click the Resume button at the top of the Debug view. The debugger continues to the next breakpoint. You can also use one of the Step buttons to see the program execute the next line and pause again.

      In the example program, you can run the program from breakpoint to breakpoint until the debugger reaches the line discountRate = 1/0;, at which point the debugger returns the same error that you see in the console when running the program.

    4. When you are finished debugging, click the Terminate button at the top of the Debug view to stop the debugger or click the Resume button to allow the program to finish running.