Programmers / diagnostics

Stop a program and look inside.

Compile with debug information, set a breakpoint, then evaluate and watch the variables while the program is paused.

Compile with debug data

IBM i stores debug information in the program or module when you compile. Learn400 implements the ILE views *NONE, *STMT, *SOURCE, *COPY, *LIST and *ALL. Create the library and source file, then add a member with PDM or your preferred editor:

CRTLIB APP TYPE(*TEST)
CRTSRCPF FILE(APP/QRPGLESRC) RCDLEN(112)
**free
// Add the numbers 1 to 5 and display the total.
dcl-s counter int(10) inz(0);
dcl-s total   int(10) inz(0);
for counter = 1 to 5;
  total += counter;
endfor;
dsply ('total=' + %char(total));
*inlr = *on;
CRTBNDRPG PGM(APP/SAMPLE) SRCFILE(APP/QRPGLESRC) SRCMBR(SAMPLE) DBGVIEW(*ALL)
CALL APP/SAMPLE

The call displays total=15. *SOURCE reads the source member when you debug; *LIST uses the listing captured at compile time, so a later member change does not alter that view. *STMT shows statement numbers without source.

Set a breakpoint and run

STRDBG PGM(APP/SAMPLE)

Display Module Source shows the source with line numbers, the program, library and module, and a command line. Move the cursor to total += counter and press F6. The message says Breakpoint added to line 6. Press F12 to return to the command line and call the program:

CALL APP/SAMPLE

The program stops at line 6 with Breakpoint at line 6. Type debug commands on the display's command line. EVAL shows a value, ATTR shows its type and length, and WATCH stops the program again when the value changes:

EVAL counter
EVAL total
ATTR total
WATCH total

For this program the values are COUNTER = 1, TOTAL = 0 and TYPE = INTEGER, LENGTH = 4 BYTES. F10 steps one statement, F11 displays the variable under the cursor, F17 sets a watch under the cursor and F18 opens Work with Watch. F13 opens Work with Module Breakpoints, F14 Work with Module List, F15 Select View, F19/F20 scroll left and right, F21 opens CL command entry and F22 steps into a procedure.

CLEAR 6
STEP 1

CLEAR removes a breakpoint; CLEAR WATCH ALL removes every watch. STEP without a count steps one statement, STEP n steps n statements, and STEP INTO follows a CALL into the called procedure or bound module. F12 resumes a stopped program. Back at the CL command line, ENDDBG ends debug mode and removes its breakpoints and watches.

Expressions worth knowing

EVAL counter = 99
ATTR total

Assignment echoes the value: COUNTER = 99 = 99. EVAL accepts scalars, data structures and subfields, arrays and ranges such as arr(1..3), indicators, multiple-occurrence data structures, pointers and based fields. A :x or :c suffix asks for a hexadecimal or character view, and COBOL groups are qualified with OF or IN. CL variables use *CHAR, *DEC, *INT, *UINT, *LGL and *PTR.

Debug an OPM program

OPM CL and RPG programs use ADDBKP, DSPPGMVAR and CHGPGMVAR instead of the ILE panel commands. The commands run in the CL job while STRDBG is active. Create QCLSRC and a member OPMCL with this program:

PGM
  DCL VAR(&N) TYPE(*DEC) LEN(5 0) VALUE(0)
  DCL VAR(&MSG) TYPE(*CHAR) LEN(10) VALUE('START')
LOOP: CHGVAR VAR(&N) VALUE(&N + 1)
  IF COND(&N *LT 3) THEN(GOTO CMDLBL(LOOP))
  SNDPGMMSG MSG(&MSG) TOPGMQ(*EXT)
ENDPGM
CRTSRCPF FILE(APP/QCLSRC) RCDLEN(112)
CRTCLPGM PGM(APP/OPMCL) SRCFILE(APP/QCLSRC) SRCMBR(OPMCL)
STRDBG PGM(APP/OPMCL)
ADDBKP STMT(LOOP) PGMVAR('&N') SKIP(1)
CALL APP/OPMCL

The program stops on the Display Breakpoint screen with &N at 1 (SKIP(1) let the first pass through LOOP go by). Press F10 for Command Entry; while the program waits there, the debug commands work on it:

DSPPGMVAR PGMVAR('&N')
CHGPGMVAR PGMVAR('&MSG') VALUE('XY') START(2)
RMVBKP STMT(*)

F3 returns to the breakpoint display and Enter lets the program continue; it sends SXYRT to the external message queue. Then end debug mode with ENDDBG.

STMT accepts a statement number or a label. SKIP passes the breakpoint the given number of times first, BKPCOND stops only when a condition holds, and PGMVAR adds a variable to the breakpoint display. ADDTRC traces statements or variables; DSPTRCDTA shows the trace and CLRTRCDTA clears it. DSPBKP lists breakpoints and RMVPGM removes a program from debug mode.

Measure statement coverage

CODECOV runs a command with statement counting and writes a coverage archive plus a spooled report. It needs a program compiled with a source or listing view.

CODECOV CMD('CALL APP/SAMPLE') MODULE((APP/SAMPLE)) OUTSTMF('/tmp/sample')
WRKSPLF
QSH CMD('ls -l /tmp/sample.cczip')

The archive contains ccdata.xml with per-line counts and a src directory. OUTDIR writes a timestamped archive into a directory instead. CCVIEW chooses *DFT, *SOURCE or *LIST, and CCLVL chooses *LINE or *PROC. A program without *SOURCE, *LIST or *ALL is refused.

What is not supported yet

The debugging lesson follows an RPG program through a subprocedure. See the command pages for STRDBG, CODECOV and ADDBKP.