Prior to COBOL85, COBOL had its own debug module accessed in the DECLARATIVES. This was removed in the 1985 standard. It appears gnuCOBOL still supports the older debugging, but given gdb can be used (next blog post), I won’t go down that route.
Instead, I will examine the debugging that COBOL85 does still natively support.
Debugging Lines
When debugging is enabled, lines with a D in column 7 (fixed format) or start with >>D (free format) are compiled. If not enabled, they are not compiled.
>>D display "debugging line hit".
OR
D display "debugging line hit".
Normally one indicates these lines should be compiled using by specifying WITH DEBUGGING MODE in the SOURCE-COMPUTER paragraph:
source-computer. x86 with debugging mode.
However, you can leave this clause off, and use the -fdebugging-line switch when invoking the compiler.
Here is code and a sample run using debugging statements:
C:\cobol\oldDebugging>cobc -t- -fdebugging-line -xj sw0debug.cbl GnuCOBOL 3.1-rc1.0 sw0debug.cbl Mon Aug 31 15:31:40 2020 Page 0001 LINE PG/LN A...B............................................................ 000001 >>source free 000002 identification division. 000003 program-id. 000004 test. 000005 000006 environment division. 000007 configuration section. 000008 source-computer. 000009 x86. 000010 000011 procedure division. 000012 000013 >>D display "debugging line hit". 000014 display "non debugging line hit". 000015 000016 stop run. 0 warnings in compilation group 0 errors in compilation group debugging line hit non debugging line hit
Using SW0 to Control Debugging
When I was writing COBOL on the HP3000, I almost always wanted my debugging lines compiled. If there was a problem, I didn’t want to have to recompile code. This was probably because minicomputers were so slow and compilers took so many resources, each programmer might be lucky to get in 3 compiles a day. I often worked nights just so I wouldn’t have to fight as much for compile time!
I needed a simple mechanism to enable/disable debugging at run time rather than compile time. There were several ways to do this on the HP3000 and I did it using the switch register with SPECIAL-NAMES of SW0 and I see gnuCOBOL supports a switch register as well.
The first HP3000 I programmed (Series III) had an actual 16 bit switch register on the front panel that primarily was used to enter a start address to boot the system. However, it could also be read programmatically by COBOL.
The switch register went away on later models, but there needed to be a way to continue to support programs that made use of that register. On the HP3000, this was done by passing the value of the 16 bit switch register, in decimal, to the program in the PARM keyword such as:
:RUN MYPROG;PARM=1
This would set bit 0 of the switch register to 1 rather than 0. (PARM=2 would have set switch register bit 1 to 1). Thus, when I ran a COBOL program on the HP3000 with PARM=1, it would display debugging so I would not have to recompile.
Here is the modified program that uses the SWITCH-0 register in special names to create the condition-name called fl-debug which is tested to determine is debugging output should be displayed. Since SWITCH-0 has not yet been set fl-debug will return false and the debugging will not be displayed:
C:\cobol\oldDebugging>cobc -t- -xj sw0debug.cbl GnuCOBOL 3.1-rc1.0 sw0debug.cbl Mon Aug 31 16:09:01 2020 Page 0001 LINE PG/LN A...B............................................................ 000001 >>source free 000002 identification division. 000003 program-id. 000004 test. 000005 000006 environment division. 000007 configuration section. 000008 source-computer. 000009 x86 with debugging mode. 000010 special-names. 000011 SWITCH-0 is fl-debug-flag, 000012 on status is fl-debug. 000013 000014 procedure division. 000015 000016 >>D if fl-debug, 000017 >>D display "debugging line hit". 000018 000019 display "non debugging line hit". 000020 000021 stop run. 0 warnings in compilation group 0 errors in compilation group non debugging line hit
For gnuCOBOL, the SWITCH-0 register is set to 1 by setting an environment variable:
C:\cobol\oldDebugging>set COB_SWITCH_0=on C:\cobol\oldDebugging>sw0debug.exe debugging line hit non debugging line hit
Setting COB_SWITCH_0 to off will disable the debugging.
Next time I’ll discuss using gdb with gnuCOBOL. Compared to using a modern debugger like gdb, having only DISPLAY is pretty primitive, but can still work. I still have to debug MCU code just using printf or even worse, just flashing a LED.