Debugging is an important feature in any programming language. It helps the developer to diagnose errors, find the root cause and then resolve them accordingly. In Rexx, the trace utility is used for debugging. The trace instruction can be implemented in 2 ways, one is the batch mode and the other is the interactive mode. Let’s look at how to implement both options.
The trace command is used to give a detailed level of each Rexx command which is executed.
The general syntax of the trace statement is shown as follows −
trace [setting]
Where the setting can be anyone of the following options −
A − Traces all the commands.
C − Only traces the host commands which are sent to the operating system.
E − Only traces the host commands which are sent to the operating system which have resulted in an error.
F − Only traces the host commands which are sent to the operating system which have resulted in a failure.
I − This provides an intermediate level tracing of Rexx commands.
L − This option is if you want to label the tracing as it happens.
N − This is the default option in which no tracing happens.
Let’s take a look at an example of the trace command.
/* Main program */ trace A /* Main program */ n = 100.45 if datatype( n, wholenumber ) then signal msg say 'This is a whole number' return 0 msg : say ' This is an incorrect number '
The output of the above program will be as follows −
5 *-* n = 100.45 if datatype( n, wholenumber ) then signal msg 7 *-* say 'This is a whole number This is a whole number 8 *-* return 0
From the output, you can see that an additional trace was added to the output of the program. The following things can be noted about the output −
The line number along with the statement executed is added to the trace output.
Each line that gets executed is shown in the trace output.
Trace can also be enabled with the help of the trace function. The general syntax and example are shown below.
trace()
The above function returns the current trace level.
None
The above function gives the current trace level.
/* Main program */ say trace() /* Main program */ n = 100.45 if datatype( n, wholenumber ) then signal msg say 'This is a whole number' return 0 msg : say 'This is an incorrect number '
The output of the above program will be as follows.
N This is an incorrect number
The first line of N denotes that the trace is set to Normal.
The trace level can be set with the trace function. The general syntax and example are shown below.
trace(travel_level)
trace_level − This is similar to the options available for setting the trace level.
The above function gives the current trace level.
/* Main program */ say trace() current_trace = trace('A') say current_trace /* Main program */ n = 100.45 if datatype( n, wholenumber ) then signal msg say 'This is a whole number' return 0 msg : say ' This is an incorrect number '
The output of the above program will be as follows −
N 4 *-* say current_trace N 6 *-* n = 100.45 7 *-* if \ datatype( n, wholenumber ) then 8 *-* signal msg 12 *-* say 'This is an incorrect number' 'This is an incorrect number'
Interactive tracing is wherein, tracing is carried out as the program runs. Just like in an IDE such as Visual Studio for .Net, in which you can add breakpoints and see how each statement executes, similarly here also you can see the program as each code line runs.
The general syntax is as follows −
trace ?options
Where, options are the same for the trace command as shown below.
A − Traces all the commands
C − Only traces the host commands which are sent to the operating system.
E − Only traces the host commands which are sent to the operating system which have resulted in an error.
F − Only traces the host commands which are sent to the operating system which have resulted in a failure.
I − This provides an intermediate level tracing of Rexx commands.
L − This option is if you want to label the tracing as it happens.
N − This is the default option in which no tracing happens.
Let’s take a look at an example of implementing active tracing.
/* Main program */ trace ?A /* Main program */ n = 100.45 if datatype( n, wholenumber ) then signal msg say 'This is a whole number' return 0 msg : say 'This is an incorrect number'
The output of the above program will be as shown in the following program. The trace will stop at each line of code; then you need to press the Enter button to move onto the next line of code.
This is an incorrect number +++ "LINUX COMMAND /home/cg/root/5798511/main.rex" 5 *-* n = 100.45 if datatype( n, wholenumber ) then +++ Interactive trace. "Trace Off" to end debug, ENTER to Continue. +++ 6 *-* signal msg 10 *-* msg : 10 *-* say 'This is an incorrect number'