Guardian Programmer's Guide

Table Of Contents
Debugging, Trap Handling, and Signal Handling
Guardian Programmer’s Guide 421922-014
25 - 11
Setting Up a Trap Handler
Choose to handle traps with your own trap-handling code. The ARMTRAP
procedure allows you to specify the address of your trap handler.
The rest of this section describes how to write your own trap handler.
Disable all trap handlers, including Inspect and Debug. In this case, your process
abnormally terminates if a trap condition occurs. The ARMTRAP procedure allows
you to disable traps.
Trap conditions that occur when user code is being processed cause an immediate
trap. If a trap condition should occur when system code is being executed, then the
trap does not occur until control returns to the user code.
Setting Up a Trap Handler
When setting up a handler, you use the ARMTRAP procedure to perform two functions:
Set up a pointer to the start of the trap-handling code. This location will be the
entry point into the trap handler when a trap occurs.
Specify the start of the trap handler local data area. You typically locate this area
at the high end of the user data stack, where it is less likely to compete with the
application for stack space.
The following code fragment performs the two functions described above:
TRAPHANDLR^ADDR := @TRAP;
TRAPSTACK^ADDR := $LMIN(LASTADDR, %77777) - 500;
CALL ARMTRAP(TRAPHANDLR^ADDR,
TRAPSTACK^ADDR);
Here, “@TRAP” identifies the start of the trap code as the following label:
TRAP:
The expression “$LMIN(LASTADDR,%77777) - 500” indicates that the start of the trap-
handler local data area will be 500 locations before the end of the user data area or
500 locations before location %77777, whichever is the lesser.
When a trap occurs, the first six words of the trap handler local data area will contain
environment information as well as the trap number itself. The S (stack pointer) and
L (local data pointer) registers will point to the start of the local data area plus six.
Figure 25-1 shows the allocation of stack space to the trap handler.
Note. HP recommends either writing your own trap handler or disabling traps. We make this
recommendation because a typical user does not know how to respond to an application that
goes into Inspect or Debug mode. Moreover, the operators console does not receive a
message to indicate that the process has trapped. Hence the process hangs in Inspect or
Debug. Stopping the process in the trap handler or disabling traps prevents the application
from hanging. The trap handler should also be written to inform the console operator of the
problem.