Anton Golovenko
Anton Golovenko

Reputation: 644

Getting current line number in Cobol

Is it possible to get and display the current line number in the Cobol program? For example, C allows do it by the next way:

...
printf("Current line = %d\n", __LINE__);
...

Upvotes: 3

Views: 640

Answers (2)

Rick Smith
Rick Smith

Reputation: 4417

Q: Is it possible to get and display the current line number in the Cobol program?

There was a feature through COBOL 85 called the DEBUG module. The feature was made obsolete in COBOL 85 and subsequently removed in COBOL 2002. While DEBUG lines were available in the 2002 standard, the DEBUG module was removed from the standard.

NOTE: The DEBUG module may still be available in current compilers.

The feature requires debugging mode in the source-computer paragraph. If the line is removed, source lines with a D or d in column 7 are treated as comments.

Declaratives must be added to access debug-line which is the standard name for the source line number.

I have coded the source such that the source line number of wherever I place perform show-line will be displayed. Notice that show-line doesn't do anything.

Source:

   program-id. dbug.
   environment division.
   source-computer. computer-name debugging mode.
   object-computer. computer-name.
   data division.
   working-storage section.
   01 char pic x.
   procedure division.
   declaratives.
  ddebug section.
  duse for debugging show-line.
  d    display "Source-line: " debug-line.
   end declaratives.
   main-line.
   begin.
       display "Before"
  d    perform show-line
       display "After"
       accept char
       stop run.

  dshow-line.

   end program dbug.

Each implementor has their own means for activating the feature. For the system I use, it's a switch parameter (+D) on the command line. Without the switch parameter the line number will not show. (For GnuCOBOL 3.2 it is, apparently, the environment variable COB_SET_DEBUG with a value of 'Y', 'y' or '1'. ;-))

Command line:

dbug (+D)

Display:

Before
Source-line:     17
After

Upvotes: 1

Simon Sobisch
Simon Sobisch

Reputation: 7297

Short answer: No.
There is no portable COBOL way in doing this, especially not in all places like __LINE__ does.

Long answer with potential alternatives:
COBOL 2002 added intrinsic functions for exception handling. Using these you can get the location where the last error happened, which checks are activated.
You could hack something by raising a non-fatal exception and ideally in the same line use that function... From the standard:

The EXCEPTION-LOCATION function returns an alphanumeric character string, part of which is the implementor-defined location of the statement associated with the last exception status.

So this may provide you with the line number, as the returned value depends on the implementation, additional it seems that - at the time of writing - neither IBM nor MicroFocus nor Fujitsu compilers support that intrinsic function at all. The GnuCOBOL implementation returns a semicolon-separated list with the last entry being the line number.

The upcoming COBOL standard added the MODULE-NAME intrinsic function - but this will only give the name, not the line reference.

If you are free to choose which implementation you use, then an addition of an extra register COB_SOURCE_LINE / COB_SOURCE_FILE in GnuCOBOL should be relative easy to add...

If the intend is a tracing of some kind: many compilers have an extension READY TRACE/ RESET TRACE. With those two statements (and possibly compiler directives / options) they will at least show the name of sections and paragraphs reached, some may also show the line number. Often this could be redirected to a file and will otherwise go to the default error stream.
If you use GnuCOBOL and compile with -ftrace-all you can also use that for line or statement tracing with self-defined format as specified in COB_TRACE_FORMAT [which can also be adjusted within the COBOL program and limited to the line number].

Upvotes: 3

Related Questions