MainframeSupports
tip week 10/2005:

I have often wished to have a way to trace my called modules without having to parse a trace parameter from the caller to the called module. Then one day I got the idea that I could switch on the trace in a called module using the presence or absence of a DD card. I have used this idea in REXX so I might as well use it in COBOL.

In the Environment Division I defined a file using SELECT OPTIONAL MYTRACE ASSIGN TO MYTRACE FILE STATUS IS MYTRACE-STATUS. In the File Section I defined a FD MYTRACE RECORDING MODE F followed by a 01 MYTRACE-FILLER PIC X(80). In the Working Storage Section I remembered to define 01 MYTRACE-STATUS PIC X(2) and to improve performance I declared a 01 MYTRACE-EXT PIC X(2) VALUE SPACES.

Now the definitions were ready. The following piece of code I added as the first piece of code to be executed in my called module:

IF MYTRACE-EXT NOT = '00' AND MYTRACE-EXT NOT = '05'
  OPEN INPUT MYTRACE
  MOVE MYTRACE-STATUS TO MYTRACE-EXT
END-IF

FILE STATUS for a successful OPEN is '00' and if the DD card is missing the FILE STATUS is set to '05' when SELECT OPTIONAL is used. The result of the OPEN is saved in the MYTRACE-EXT variable. Now I don't need to perform the OPEN more than once in my called module even if it is called a lot of times. If the OPEN fails with a FILE STATUS different from '00' and '05' you must consider to implement some kind of error handling.

Now I am able to control behaviour in my called program using the value of MYTRACE-EXT. Maybe I want to display some data on SYSOUT:

IF MYTRACE-EXT = '00'
  DISPLAY 'KILROY WAS HERE'
END-IF

Now the rest is up to you. The above example is very simple and just made to give you an idea of the possibilities. If the DD card MYTRACE is absent in the JCL then only the usual called module behaviour is carried out. If I add a //MYTRACE DD DUMMY to the JCL then suddenly my extra code is carried out. I find it extremely simple and very useful. Consider your production environment. Normally no trace tools are allowed here because of performance considerations. This method will give you the opportunity to built in a simple tracing facility that works even in production.

You don't need to use the SELECT with OPTIONAL. Without OPTIONAL the FILE STATUS returned is '35' instead. I didn't know that when I wrote the original tip. If your version of COBOL doesn't save Working Storage between calls then you can use a 01 MYTRACE-EXT PIC X(2) EXTERNAL instead. VALUE is not allowed for EXTERNAL variables but they are almost always initialized with low values. If you use an EXTERNAL variable you must ensure that no other EXTERNAL variable has the same name in other programs, otherwise you might end up in complete chaos.

Previous tip in english        Sidste danske tip        Tip list