Guardian Programmer's Guide

Table Of Contents
Debugging, Trap Handling, and Signal Handling
Guardian Programmer’s Guide 421922-014
25 - 16
Writing a Trap Handler: Examples
Writing a Trap Handler: Examples
The following program shows an example of a trap handler that displays the contents
of the P register when an arithmetic overflow occurs. After displaying the P register,
the trap handler returns to the application.
If any trap condition other than arithmetic overflow occurs, then the trap handler calls
the DEBUG procedure.
The example consists of two procedures:
The OVERFLOWTRAP procedure sets up the trap handler with a call to
ARMTRAP and also provides the code for the trap handler itself.
The TRAP^USER procedure is the main procedure. It calls the OVERFLOWTRAP
procedure to set up the trap handler, then causes an arithmetic overflow trap
condition by attempting to divide by zero.
?INSPECT,SYMBOLS
!Global variables:
INT TERM^NAME[0:11];
INT TERM^NUM;
?NOLIST
?SOURCE $SYSTEM.SYSTEM.EXTDECS0(INITIALIZER,PROCESS_GETINFO_,
? FILE_OPEN_,ARMTRAP,WRITE,DEBUG,NUMOUT,LASTADDR)
?LIST
!------------------------------------------------------------
! Sets up the start address of the trap handler and the stack
! space for the trap handler by calling the ARMTRAP
! procedure. This procedure also supplies the code for the
! trap handler, including a second call to ARMTRAP that exits
! the trap handler.
!------------------------------------------------------------
PROC OVERFLOWTRAP;
BEGIN
INT REGS = 'L' +1, !R0 to R7 saved here
WBUF = 'L' +9, !buffer for terminal I/O
PREG = 'L' -2, !P register at time of trap
EREG = 'L' -1, !ENV register at time of trap
TRAPNUM = 'L' -4, !trap number
SPACEID = 'L' -5; !space ID of trap location
DEFINE OVERFLOW = <10>#;!overflow bit in ENV register
STRING SBUF = WBUF; !string overlay for I/O buffer
LITERAL LOCALS = 15; !number of words of local
! storage
! Arm the trap:
CALL ARMTRAP(@TRAP, $LMIN(LASTADDR,%77777) - 500);
RETURN;