Guardian Programmer's Guide

Table Of Contents
Debugging, Trap Handling, and Signal Handling
Guardian Programmer’s Guide 421922-014
25 - 18
Writing a Trap Handler: Examples
! Open home terminal:
CALL PROCESS_GETINFO_(!process^handle!,
!file^name!,
!file^name^len!,
!priority!,
!moms^processhandle!,
TERM^NAME:MAXLEN,
LEN);
CALL FILE_OPEN_(TERM^NAME:LEN,TERM^NUM);
IF <> THEN CALL DEBUG;
! Set up an arithmetic overflow condition to cause the
! trap:
J := 0;
I := I/J;
END;
In the following example, a trap causes the current code sequence to be abandoned
and an alternate code sequence executed instead. Several points are illustrated:
The procedure TRAP_GUARD sets its caller as the destination (the code address
and stack environment) to be used if a trap occurs; it also arms the trap handler.
When a trap occurs, the information about the trap is saved globally (for possible
display or analysis).
The trap handler exits, and control is returned to the designated destination.
Consistent values are supplied for space ID, S, P, ENV, and L. ENV.RP is set to 0
and RP[0] is set to a specific value so that returning from the trap handler effects a
return from the TRAP_GUARD function with the result equal to TRUE (-1).
Incidentally, the trap handler stack is allocated among the globals (below the
normal stack) instead of at the top of the stack.