5 sigtrap - Perl pragma to enable stack backtrace on unexpected signals
10 use sigtrap qw(BUS SEGV PIPE SYS ABRT TRAP);
14 The C<sigtrap> pragma initializes some default signal handlers that print
15 a stack dump of your Perl program, then sends itself a SIGABRT. This
16 provides a nice starting point if something horrible goes wrong.
18 By default, handlers are installed for the ABRT, BUS, EMT, FPE, ILL, PIPE,
19 QUIT, SEGV, SYS, TERM, and TRAP signals.
21 See L<perlmod/Pragmatic Modules>.
30 @sigs or @sigs = qw(QUIT ILL TRAP ABRT EMT FPE BUS SEGV SYS PIPE TERM);
31 foreach $sig (@sigs) {
32 $SIG{$sig} = 'sigtrap::trap';
37 package DB; # To get subroutine args.
38 $SIG{'ABRT'} = DEFAULT;
39 kill 'ABRT', $$ if $panic++;
40 syswrite(STDERR, 'Caught a SIG', 12);
41 syswrite(STDERR, $_[0], length($_[0]));
42 syswrite(STDERR, ' at ', 4);
43 ($pack,$file,$line) = caller;
44 syswrite(STDERR, $file, length($file));
45 syswrite(STDERR, ' line ', 6);
46 syswrite(STDERR, $line, length($line));
47 syswrite(STDERR, "\n", 1);
50 for ($i = 1; ($p,$f,$l,$s,$h,$w,$e,$r) = caller($i); $i++) {
56 unless /^(?: -?[\d.]+ | \*[\w:]* )$/x;
57 s/([\200-\377])/sprintf("M-%c",ord($1)&0177)/eg;
58 s/([\0-\37\177])/sprintf("^%c",ord($1)^64)/eg;
61 $w = $w ? '@ = ' : '$ = ';
62 $a = $h ? '(' . join(', ', @a) . ')' : '';
63 $e =~ s/\n\s*\;\s*\Z// if $e;
64 $e =~ s/[\\\']/\\$1/g if $e;
67 } elsif (defined $r) {
69 } elsif ($s eq '(eval)') {
72 $f = "file `$f'" unless $f eq '-e';
73 $mess = "$w$s$a called from $f line $l\n";
74 syswrite(STDERR, $mess, length($mess));