Chapter 5
The Debug Mode
The term
debug
refers to the process of finding programming
errors and correcting them. BASIC09's debugging features
include
symbolic
debugging capabilities that let you examine
variable values and test and manipulate procedures.
With Debug, you can:
Examine and change variables.
Trace procedure execution. Debug lets you execute procedures and watch them run in slow motion.
Pause procedure execution.
Resume procedure execution.
· Set procedure
breakpoints
that automatically switch to
the debug mode.
· Select the use of degrees or radians for trigonometric
functions.
Perform calculations.
e Call OS-9 system commands.
Entering the Debug Mode
You enter Debug:
9
Automatically, whenever an error occurs during the execution of a procedure (unless you have included an ON
ERROR GOTO statement to handle the error).
Automatically, when a procedure executes a PAUSE
statement.
When you press
cTRyc
during the execution of a
procedure.
You can tell when BASIC09 enters the Debug mode by the
appearance of the
D : prompt
. When you see
D:,
followed by the
cursor, Debug is waiting for your command.
The following is a reference of all the Debug commands and what
they accomplish:
BASIC09 Reference
Command Function
$ Calls OS-9's command shell interpreter to run
a program or an OS-9 command. From the
Debug prompt, type $ , followed by the name of
the program or command you want to execute.
Example:
s
1 i s t procedure-one ENTER
BREAK Sets a breakpoint immediately before the spec
ified procedure. Use this command to re-enter
Debug when one procedure calls another.
If you have three procedures that call each
other Procl, Proc2, and Proc3 and Proc3
does not seem to pass the correct values to
Proc2 when it returns, set a breakpoint at
Proc2. This causes BASIC09 to enter Debug
before re-entering Proc2. You can then check
your variable values.
You can use one breakpoint for each active pro
cedure. Debug removes breakpoints immedi
ately after encountering them.
A procedure must run before you can set a
breakpoint in it. Use BREAK to stop execution
when a called procedure returns to a proce
dure previously executed.
Example:
BREAK p r o c 2 ENTER
CONT Causes procedure execution to continue.
Example:
c o n t ENTER
DEG/RAD Selects either degrees or radians as the unit of
measurement for trigonometric functions. DEG
and RAD affect only the current procedure.
Examples:
d e 9 ENTER
r a d ENTER
5-2
The Debug Mode / 5
DIR
Displays the name, size, and variable storage
requirements of each procedure in the workspace. The current working procedure has an
asterisk before its name. Ail packed procedures have a dash before their names. DIR
also shows the available memory in the
workspace.
If you provide a pathlist, DIR sends its data to
the specified file.
Example:
d i r
ENTER
dir procedures
LET
LIST
PRINT
STATE
Terminates execution of the procedure, closes
any open paths, and exits to the System mode.
Example:
q ENTER
Assigns a new value to a variable. You must
specify variable names that are already initialized by your program. In the Debug mode,
you cannot use LET to copy one array to
another array as you can in BASIC
procedures.
Example:
1 e t a : = 0
ENTER
1 et fruit : = "oranqe5"
ENTER
Displays a source listing of the suspended procedure. The display is formatted and includes
I-code addresses. An asterisk appears to the
left of the last executed statement.
Example:
1 i s t
ENTER
Displays the values of variables used in the
suspended procedure. You cannot introduce
new variable names in the Debug mode, and
you cannot display array structures. -
Example:
print fruit
ENTER
Lists the
nesting
order of active procedures.
STATE displays the highest-level procedure at
the bottom of the calling list. The lowest-level
procedure is the suspended procedure.
Example:
5
t a t e
ENTER
5-3
BASIC09 Reference
STEP Causes execution of the suspended procedure
in specified increments. For example, typing
STEP 5 ENTER
executes the equivalent of the
next five statements. If you enter STEP with
out an increment value, the step rate is 1.
Using STEP with the trace function lets you
observe the source lines as they execute.
Because compiled I-code contains actual state
ment memory addresses, the top or bottom
statements of loop structures execute only
once. For example, in FOR/NEXT loops, FOR
executes once, and the statement following
FOR appears to be the top of the loop.
TRON/TROFF Turns on or turns off the trace function. Trace
on (TRON) causes the system to reconstruct
the compiled code of each statement line into
source code. Debug displays the source code
before the statement is executed. If the statement causes the evaluation of one or more
expressions, Debug displays each result following the statement. The result is preceded by
an equal sign.
The trace function is local to the current procedure. If the suspended procedure calls
another procedure, Debug suspends the trace
function until control returns to the original
procedure. However, once you turn on trace for
a procedure, it continues in effect until you
turn it off. This means that if you turn trace
on in a called procedure, and another procedure subsequently calls it, trace continues to
display the called procedure's operations.
Example: t
r o n ENTER
t
r o f f ENTER
When Things Go Wrong
Programming errors show up in two ways. Either your procedure
produces incorrect results, or it terminates prematurely.
5-4
The Debug Mode / 5
In the first instance, you can stop your procedure and enter
Debug by pressing
CTRL
ff).
However, sometimes your program executes too quickly to allow
you to stop it at the appropriate place. In this case, you can use
the Edit mode to insert a PAUSE command where you wish the
procedure to stop. PAUSE causes the procedure to halt execution
and enter the Debug mode.
Once in Debug, you can use the PRINT command to examine
the procedure variables. You can use LET to manipulate the
variable values to determine where the error or errors occur. Perhaps you forgot to initialize a variable or forgot to increase a loop
counter.
Using the Trace Function
Sometimes, errors are more difficult to discover. If so, the next
step is to use the trace function. To do this, type:
t r o n ENTER
Now press
ENTER
. Each time you press
ENTER , Debug
executes
one line of the procedure. You can see the original source statement, and if an expression is evaluated, Debug prints the result
of the expression, preceded by an equal sign.
In this manner, you can step through the entire procedure, or
any part of it, examining variable values as you go.
What About Loops?
The STEP command is helpful if you find yourself tracing the
operation of a loop. Once you determine that the loop works correctly, you can avoid tedious, step-by-step repetitions by turning
trace off and using STEP to quickly run through the loop. Then,
turn trace back on and resume single-step debugging. For
instance, type:
t r o f f ENTER
step 2 0 0 ENTER
t r o n ENTER
5-5
BASIC09 Reference
In Multiple Procedures
Although the trace function is local to a procedure, you can
pause and turn on the trace function in as many procedures as
you wish. Trace continues to operate in each procedure until you
turn it off using TROFF.
To cause a procedure to halt execution when it is called by
another procedure, use the BREAK command.
5-6