Guardian Programmer's Guide

Table Of Contents
Debugging, Trap Handling, and Signal Handling
Guardian Programmer’s Guide 421922-014
25 - 19
Writing a Trap Handler: Examples
! Global declarations for trap handling
INT saved_L, ! destination when trap occurs: L,
saved_E, ! ENV (stack-marker form, with space index),
saved_P, ! P,
saved_S; ! S
STRUCT trapframe_template(*);
BEGIN ! stack frame layout of trap handler
INT spaceid,trapnum,S,P,E,L,R[0:-1];
END;
STRUCT .trapstate(trapframe_template); ! data from most
! recent trap
INT .trap_stack[0:499]; ! space for trap handler stack
?PUSHLIST,NOLIST
?SOURCE $SYSTEM.SYSTEM.EXTDECS(ARMTRAP,ABEND)
?POPLIST
INT PROC TRAP_GUARD; ! procedure to set trap destination,
BEGIN ! arm trap handler, and handle traps
STRUCT tf(trapframe_template) = 'L'-5;
INT L = 'L';
saved_L := L; ! Save the
saved_E := L[-1]; ! destination
saved_P := L[-2]; ! code and stack
saved_S := @L-3; ! locations
CALL ARMTRAP(@traphandler,@trap_stack); ! Arm the
! trap handler
RETURN 0; ! Return False: no trap
! If a trap occurs subsequently, TRAP_GUARD will return
! again to the site of the last call, but this time the
! value will be True.
traphandler: ! code invoked by system when trap occurs
CODE(PUSH %777); ! Save all stack registers
trapstate ':=' tf FOR 1 ELEMENTS; ! Save trap state
! Set state to return again from last call to TRAP_GUARD
tf.spaceid := saved_E; ! Only space index is signficant
tf.S := saved_S;
tf.E := saved_E LAND $COMP(%37); ! Clear out space index:
! CC = 0 to ensure legal value
! RP = 0 to return one item in R[0]
tf.P := saved_P;
tf.L := saved_L;
tf.R[0] := -1; ! Return True from TRAP_GUARD
CALL ARMTRAP(0,@trap_stack); ! Exit from trap handler
END; ! TRAP_GUARD