HP OpenVMS Systems Documentation
OpenVMS Programming Concepts Manual
22.214.171.124 Exiting from the Condition Handler
126.96.36.199 Returning Control to the Program
Your handlers should return control either to the program unit that established the handler or to the program unit that invoked the program unit that established the handler.
To return control to the program unit that established the handler, invoke SYS$UNWIND and pass the call depth (third element of the mechanism array) as the first argument with no second argument.
To return control to the caller of the program unit that established the handler, invoke SYS$UNWIND without passing any arguments.
The first argument SYS$UNWIND specifies the number of program units to unwind (remove from the stack). If you specify this argument at all, you should do so as shown in the previous example. (MECHARGS(3) contains the number of program units that must be unwound to reach the program unit that established the handler that invoked SYS$UNWIND.) The second argument SYS$UNWIND contains the location of the next statement to be executed. Typically, you omit the second argument to indicate that the program should resume execution at the statement following the last statement executed in the program unit that is regaining control.
Each time SYS$UNWIND removes a program unit from the stack, it invokes any condition handler established by that program unit and passes the condition handler the SS$_UNWIND condition code. To prevent the condition handler from resignaling the SS$_UNWIND condition code (and so complicating the unwind operation), include SS$_UNWIND as an expected condition code when you invoke LIB$MATCH_COND. When the condition code is SS$_UNWIND, your condition handler might perform necessary cleanup operations or do nothing.
In the following example, if the condition code is SS$_UNWIND, no action is performed. If the condition code is another of the expected codes, the handler displays the message and then returns control to the program unit that called the program unit that established the condition handler.
9.12.5 Example of Condition-Handling Routines
The following example shows two procedures, A and B, that have declared condition handlers. The notes describe the sequence of events that would occur if a call to a system service failed during the execution of procedure B.
9.13 Debugging a Condition Handler
You can debug a condition handler as you would any subprogram, except that you cannot use the DEBUG command STEP/INTO to enter a condition handler. You must set a breakpoint in the handler and wait for the debugger to invoke the handler.
Typically, to trace execution of a condition handler, you set breakpoints at the statement in your program that should signal the condition code, at the statement following the one that should signal, and at the first executable statement in your condition handler.
The SET BREAK debugger command with the /HANDLER qualifier causes the
debugger to scan the call stack and attempt to set a breakpoint on
every established frame-based handler whenever the program being
debugged has an exception. The debugger does not discriminate between
standard RTL handlers and user-defined handlers.
The following sections present information about Alpha systems RTL
jacket handlers, and RTL routines that can be either established as
condition handlers or called from a condition handler to handle
signaled exception conditions.
On Alpha systems, many RTLs establish a jacket RTL handler on a frame where the user program has defined its own handler. This RTL jacket does some setup and argument manipulation before actually calling the handler defined by the user. When processing the exception, the debugger sets the breakpoint on the jacket RTL jacket handler, because that is the address on the call stack. If the debugger suspends program execution at a jacket RTL handler, it is usually possible to reach the user-defined handler by entering a STEP/CALL command followed by a STEP/INTO command. Some cases might require that additional sequences of STEP/CALL and STEP/INTO commands be entered. For more information on frame-based handlers, see OpenVMS Calling Standard.
If the jacket RTL handler is part of an installed shared image such as ALPHA LIBOTS, the debugger cannot set a breakpoint on it. In this case, activate the RTL as a private image by defining the RTL as a logical name, as in the following example:
9.14.2 Converting a Floating-Point Fault to a Floating-Point Trap (VAX Only)
On VAX systems, a trap is an exception condition that is signaled after the instruction that caused it has finished executing. A fault is an exception condition that is signaled during the execution of the instruction. When a trap is signaled, the program counter (PC) in the signal argument vector points to the next instruction after the one that caused the exception condition. When a fault is signaled, the PC in the signal argument vector points to the instruction that caused the exception condition. See the VAX Architecture Reference Manual for more information about faults and traps.
LIB$SIM_TRAP can be established as a condition handler or be called
from a condition handler to convert a floating-point fault to a
floating-point trap. After LIB$SIM_TRAP is called, the PC points to the
instruction after the one that caused the exception condition. Thus,
your program can continue execution without fixing up the original
condition. LIB$SIM_TRAP intercepts only floating-point overflow,
floating-point underflow, and divide-by-zero faults.
When it is preferable to detect errors by signaling but the calling routine expects a returned status, LIB$SIG_TO_RET can be used by the routine that signals. For instance, if you expect a particular condition code to be signaled, you can prevent the operating system from invoking the default condition handler by establishing a different condition handler. LIB$SIG_TO_RET is a condition handler that converts any signaled condition to a return status. The status is returned to the caller of the routine that established LIB$SIG_TO_RET. You may establish LIB$SIG_TO_RET as a condition handler by specifying it in a call to LIB$ESTABLISH.
On Alpha systems, LIB$ESTABLISH is not supported, though high-level languages may support it for compatibility.
LIB$SIG_TO_RET can also be called from another condition handler. If LIB$SIG_TO_RET is called from a condition handler, the signaled condition is returned as a function value to the caller of the establisher of that handler when the handler returns to the OpenVMS Condition Handling facility. When a signaled exception condition occurs, LIB$SIG_TO_RET routine does the following:
To change a signal to a return status, you must put any code that might signal a condition code into a function where the function value is a return status. The function containing the code must perform the following operations:
If the program unit GET_1_STAT in the following function signals a condition code, LIB$SIG_TO_RET changes the signal to the return status of the INTERCEPT_SIGNAL function and returns control to the program unit that invoked INTERCEPT_SIGNAL. (If GET_1_STAT has a condition handler established, the operating system invokes that handler before invoking LIB$SIG_TO_RET.)
When the program unit that invoked INTERCEPT_SIGNAL regains control, it
should check the return status (as shown in Section 9.5.1) to determine
which condition code, if any, was signaled during execution of
LIB$SIG_TO_STOP causes a signal to appear as though it had been signaled by a call to LIB$STOP.
LIB$SIG_TO_STOP can be enabled as a condition handler for a routine or
be called from a condition handler. When a signal is generated by
LIB$STOP, the severity code is forced to severe, and control cannot
return to the routine that signaled the condition. See Section 9.12.1
for a description of continuing normal execution after a signal.
LIB$MATCH_COND checks for a match between two condition values to allow
a program to branch according to the condition found. If no match is
found, the routine returns zero. The routine matches only the condition
identification field (STS$V_COND_ID) of the condition value; it ignores
the control bits and the severity field. If the facility-specific bit
(STS$V_FAC_SP = bit <15>) is clear in cond-val
(meaning that the condition value is systemwide), LIB$MATCH_COND
ignores the facility code field (STS$V_FAC_NO = bits <27:17>) and
compares only the STS$V_MSG_ID fields (bits <15:3>).
On VAX systems, after a signal of SS$_ROPRAND during a floating-point
instruction, LIB$FIXUP_FLT finds the operand and changes it from --0.0
to a new value or to +0.0.
On VAX systems, LIB$DECODE_FAULT locates the operands for an
instruction that caused a fault and passes the information to a user
action routine. When called from a condition handler, LIB$DECODE_FAULT
locates all the operands and calls an action routine that you supply.
Your action routine performs the steps necessary to handle the
exception condition and returns control to LIB$DECODE_FAULT.
LIB$DECODE_FAULT then restores the operands and the environment, as
modified by the action routine, and continues execution of the
If any exit handler exits using the EXIT (SYS$EXIT) system service, none of the remaining handlers is executed. In addition, if an image is aborted by the DCL command STOP (the user presses Ctrl/Y and then enters STOP), the system performs image rundown and does not invoke any exit handlers. Like the DCL STOP/ID, SYS$DELPRC bypasses all exit handlers, except the rundown specified in the privileged library vector (PLV) privileged shareable images, and deletes the process. (The DCL command EXIT invokes the exit handlers before running down the image.)
When a routine is active under OpenVMS, it has available to it temporary storage on a stack, in a construct known as a stack frame, or call frame. Each time a subroutine call is made, another call frame is pushed onto the stack and storage is made available to that subroutine. Each time a subroutine returns to its caller, the subroutine's call frame is pulled off the stack, and the storage is made available for reuse by other subroutines. Call frames therefore are nested. Outer call frames remain active longer, and the outermost call frame, the call frame associated with the main routine, is normally always available.
A primary exception to this call frame condition is when an exit handler runs. With an exit handler running, only static data is available. The exit handler effectively has its own call frame. Exit handlers are declared with the SYS$DCLEXH system service.
The use of call frames for storage means that all routine-local data is reentrant; that is, each subroutine has its own storage for the routine-local data.
The allocation of storage that is known to the exit handler must be in memory that is not volatile over the possible interval the exit handler might be pending. This means you must be familiar with how the compilers allocate routine-local storage using the stack pointer and the frame pointer. This storage is valid only while the stack frame is active. Should the routine that is associated with the stack frame return, the exit handler cannot write to this storage without having the potential for some severe application data corruptions.
A hang-up to a terminal line causes DCL to delete the master process's subprocesses. However, if the subprocesses's exit handler is in a main image installed with privilege, then that exit handler is run even with the DCL command STOP. Also, if the subprocess was spawned NOWAIT, then the spawning process's exit handler is run as well.
Use exit handlers to perform any cleanup that your program requires in
addition to the normal rundown operations performed by the operating
system. In particular, if your program must perform some final action
regardless of whether it exits normally or is aborted, you should write
and establish an exit handler to perform that action.
To establish an exit handler, use the SYS$DCLEXH system service. The SYS$DCLEXH system service requires one argument---a variable-length data structure that describes the exit handler. Figure 9-16 illustrates the structure of an exit handler.
Figure 9-16 Structure of an Exit Handler
The first longword of the structure contains the address of the next handler. The operating system uses this argument to keep track of the established exit handlers; do not modify this value. The second longword of the structure contains the address of the exit handler being established. The low-order byte of the third longword contains the number of arguments to be passed to the exit handler. Each of the remaining longwords contains the address of an argument.
The first argument passed to an exit handler is an integer value containing the final status of the exiting program. The status argument is mandatory. However, do not supply the final status value; when the operating system invokes an exit handler, it passes the handler the final status value of the exiting program.
To pass an argument with a numeric data type, use programming language statements to assign the address of a numeric variable to one of the longwords in the exit-handler data structure. To pass an argument with a character data type, create a descriptor of the following form:
Use the language statements to assign the address of the descriptor to one of the longwords in the exit-handler data structure.
The following program segment establishes an exit handler with two arguments, the mandatory status argument and a character argument:
An exit handler can be established at any time during your program and remains in effect until it is canceled (with SYS$CANEXH) or executed. If you establish more than one handler, the handlers are executed in reverse order: the handler established last is executed first; the handler established first is executed last.