How do you debug a CL program in AS400?

Debugging CL programs in AS400 (IBM i) involves a combination of techniques and tools. Here's a breakdown of the process:

1. Preparation:

  • Compile with Debug Information: Ensure your CL program is compiled with debug information. This is crucial for the debugger to access the source code and allow you to set breakpoints, step through the code, and inspect variables. You can achieve this by using the DBGVIEW(*SOURCE) or DBGVIEW(*ALL) parameter on the CRTCLPGM or CRTBNDCL command.
  • Source Member: Make sure the source member for your CL program is available and has not been modified since the program was compiled.

2. Starting the Debugger:

  • STRDBG Command: Use the STRDBG command to start the debugger. Specify the program you want to debug:
STRDBG PGM(library/program-name)
  • OPMSRC Parameter (for OPM Programs): If you are debugging an Original Program Model (OPM) CL program, you might need to specify OPMSRC(*YES) on the STRDBG command.
  • UPDPROD Parameter: The UPDPROD parameter controls whether you can make changes to production files during debugging. It's generally recommended to keep this set to *NO to avoid unintentional modifications to live data.

3. Debugging Commands:

Once the debugger starts, you'll be presented with a display showing your CL program's source code. You can use various debug commands to control the debugging session:

  • Breakpoints:
    • BREAK: Set breakpoints at specific lines in your code to pause execution.
    • CLEAR: Remove breakpoints.
    • F6 (Add/Clear Breakpoint): Use this function key to toggle breakpoints on the displayed source code.
  • Stepping:
    • STEP or F10: Execute the next statement.
    • STEP INTO or F22: Step into a called program or subroutine.
  • Inspecting Variables:
    • EVAL: Display or change the value of a variable.
    • ATTR: Display the attributes (type, length) of a variable.
  • Displaying Source:
    • DISPLAY: Display a different source module.
    • FIND: Search for a string or line number in the source code.
  • Navigation:
    • UP, DOWN, LEFT, RIGHT: Scroll through the source code.
    • TOP, BOTTOM: Go to the beginning or end of the source code.
  • Other Commands:
    • HELP: Display help information about debug commands.
    • SET: Change debugging options.
    • WATCH: Monitor the value of a variable or expression.

4. Debugging Process:

  1. Set Breakpoints: Set breakpoints at strategic locations in your CL program where you want to pause execution and examine the program's state.
  2. Run the Program: Call your CL program. Execution will pause at the first breakpoint you've set.
  3. Step Through Code: Use the stepping commands (STEP, STEP INTO) to execute your CL program line by line. This allows you to observe the program's flow and identify any logical errors.
  4. Inspect Variables: Use the EVAL command to examine the values of variables at different points in your program. This helps you understand how data is being processed and identify any incorrect values.
  5. Evaluate Expressions: You can use EVAL to evaluate expressions and see the results. This can be useful for debugging complex logic.
  6. Identify and Fix Errors: When you encounter an error, analyze the program's state, the values of variables, and the flow of execution to understand the cause of the error. Modify your CL program code to correct the error.
  7. Test and Repeat: After fixing an error, recompile your CL program and repeat the debugging process to ensure that the error is resolved and no new errors have been introduced.

5. Ending the Debugger:

  • ENDDBG Command: Use the ENDDBG command to end the debugging session.

Tips for Effective Debugging:

  • Understand your program: Before you start debugging, make sure you have a good understanding of how your CL program is supposed to work.
  • Use meaningful variable names: This makes it easier to understand the purpose of variables when inspecting them during debugging.
  • Break down complex logic: If you have complex logic in your CL program, break it down into smaller, more manageable chunks. This makes it easier to isolate and debug errors.
  • Use comments: Add comments to your CL program to explain the purpose of different sections of code. This can help you understand the program's logic and identify potential errors.
  • Test thoroughly: After you have fixed an error, test your CL program thoroughly to ensure that it works correctly in all situations.