5 sigtrap - Perl pragma to enable simple signal handling
16 my $handler = \&handler_traceback;
24 if (/^[A-Z][A-Z0-9]*$/) {
26 unless ($untrapped and $SIG{$_} and $SIG{$_} ne 'DEFAULT') {
27 print "Installing handler $handler for $_\n" if $Verbose;
31 elsif ($_ eq 'normal-signals') {
32 unshift @_, qw(HUP INT PIPE TERM);
34 elsif ($_ eq 'error-signals') {
35 unshift @_, qw(ABRT BUS EMT FPE ILL QUIT SEGV SYS TRAP);
37 elsif ($_ eq 'old-interface-signals') {
38 unshift @_, qw(ABRT BUS EMT FPE ILL PIPE QUIT SEGV SYS TERM TRAP);
40 elsif ($_ eq 'stack-trace') {
41 $handler = \&handler_traceback;
44 $handler = \&handler_die;
46 elsif ($_ eq 'handler') {
47 @_ or croak "No argument specified after 'handler'";
49 unless (ref $handler or $handler eq 'IGNORE'
50 or $handler eq 'DEFAULT') {
52 $handler = Symbol::qualify($handler, (caller)[0]);
55 elsif ($_ eq 'untrapped') {
62 $VERSION >= $_ or croak "sigtrap.pm version $_ required,"
63 . " but this is only version $VERSION";
66 croak "Unrecognized argument $_";
70 @_ = qw(old-interface-signals);
76 croak "Caught a SIG$_[0]";
79 sub handler_traceback {
80 package DB; # To get subroutine args.
81 $SIG{'ABRT'} = DEFAULT;
82 kill 'ABRT', $$ if $panic++;
83 syswrite(STDERR, 'Caught a SIG', 12);
84 syswrite(STDERR, $_[0], length($_[0]));
85 syswrite(STDERR, ' at ', 4);
86 ($pack,$file,$line) = caller;
87 syswrite(STDERR, $file, length($file));
88 syswrite(STDERR, ' line ', 6);
89 syswrite(STDERR, $line, length($line));
90 syswrite(STDERR, "\n", 1);
93 for ($i = 1; ($p,$f,$l,$s,$h,$w,$e,$r) = caller($i); $i++) {
99 unless /^(?: -?[\d.]+ | \*[\w:]* )$/x;
100 s/([\200-\377])/sprintf("M-%c",ord($1)&0177)/eg;
101 s/([\0-\37\177])/sprintf("^%c",ord($1)^64)/eg;
104 $w = $w ? '@ = ' : '$ = ';
105 $a = $h ? '(' . join(', ', @a) . ')' : '';
106 $e =~ s/\n\s*\;\s*\Z// if $e;
107 $e =~ s/[\\\']/\\$1/g if $e;
110 } elsif (defined $r) {
112 } elsif ($s eq '(eval)') {
115 $f = "file `$f'" unless $f eq '-e';
116 $mess = "$w$s$a called from $f line $l\n";
117 syswrite(STDERR, $mess, length($mess));
129 use sigtrap qw(stack-trace old-interface-signals); # equivalent
130 use sigtrap qw(BUS SEGV PIPE ABRT);
131 use sigtrap qw(die INT QUIT);
132 use sigtrap qw(die normal-signals);
133 use sigtrap qw(die untrapped normal-signals);
134 use sigtrap qw(die untrapped normal-signals
135 stack-trace any error-signals);
136 use sigtrap 'handler' => \&my_handler, 'normal-signals';
137 use sigtrap qw(handler my_handler normal-signals
138 stack-trace error-signals);
142 The B<sigtrap> pragma is a simple interface to installing signal
143 handlers. You can have it install one of two handlers supplied by
144 B<sigtrap> itself (one which provides a Perl stack trace and one which
145 simply C<die()>s), or alternately you can supply your own handler for it
146 to install. It can be told only to install a handler for signals which
147 are either untrapped or ignored. It has a couple of lists of signals to
148 trap, plus you can supply your own list of signals.
150 The arguments passed to the C<use> statement which invokes B<sigtrap>
151 are processed in order. When a signal name or the name of one of
152 B<sigtrap>'s signal lists is encountered a handler is immediately
153 installed, when an option is encountered it affects subsequently
158 =head2 SIGNAL HANDLERS
160 These options affect which handler will be used for subsequently
167 The handler used for subsequently installed signals will output a Perl
168 stack trace to STDERR and then tries to dump core. This is the default
173 The handler used for subsequently installed signals calls C<die>
174 (actually C<croak>) with a message indicating which signal was caught.
176 =item B<handler> I<your-handler>
178 I<your-handler> will be used as the handler for subsequently installed
179 signals. I<your-handler> can be any value which is valid as an
180 assignment to an element of C<%SIG>.
186 B<sigtrap> has two built-in lists of signals to trap. They are:
190 =item B<normal-signals>
192 These are the signals which a program might normally expect to encounter
193 and which by default cause it to terminate. They are HUP, INT, PIPE and
196 =item B<error-signals>
198 These signals usually indicate a serious problem with the Perl
199 interpreter or with your script. They are ABRT, BUS, EMT, FPE, ILL,
200 QUIT, SEGV, SYS and TRAP.
202 =item B<old-interface-signals>
204 These are the signals which were trapped by default by the old
205 B<sigtrap> interface, they are ABRT, BUS, EMT, FPE, ILL, PIPE, QUIT,
206 SEGV, SYS, TERM, and TRAP. If no signals or signals lists are passed to
207 B<sigtrap> this list is used.
217 This token tells B<sigtrap> only to install handlers for subsequently
218 listed signals which aren't already trapped or ignored.
222 This token tells B<sigtrap> to install handlers for all subsequently
223 listed signals. This is the default behavior.
227 Any argument which looks like a signals name (that is,
228 C</^[A-Z][A-Z0-9]*$/>) is taken as a signal name and indicates that
229 B<sigtrap> should install a handler for it.
233 Require that at least version I<number> of B<sigtrap> is being used.
239 Provide a stack trace for the old-interface-signals:
245 use sigtrap qw(stack-trace old-interface-signals);
247 Provide a stack trace on the 4 listed signals only:
249 use sigtrap qw(BUS SEGV PIPE ABRT);
253 use sigtrap qw(die INT QUIT);
255 Die on HUP, INT, PIPE or TERM:
257 use sigtrap qw(die normal-signals);
259 Die on HUP, INT, PIPE or TERM, except don't change the behavior for
260 signals which are already trapped or ignored:
262 use sigtrap qw(die untrapped normal-signals);
264 Die on receipt one of an of the B<normal-signals> which is currently
265 B<untrapped>, provide a stack trace on receipt of B<any> of the
268 use sigtrap qw(die untrapped normal-signals
269 stack-trace any error-signals);
271 Install my_handler() as the handler for the B<normal-signals>:
273 use sigtrap 'handler', \&my_handler, 'normal-signals';
275 Install my_handler() as the handler for the normal-signals, provide a
276 Perl stack trace on receipt of one of the error-signals:
278 use sigtrap qw(handler my_handler normal-signals
279 stack-trace error-signals);