Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

289 lines
7.7 KiB

  1. package sigtrap;
  2. =head1 NAME
  3. sigtrap - Perl pragma to enable simple signal handling
  4. =cut
  5. use Carp;
  6. $VERSION = 1.02;
  7. $Verbose ||= 0;
  8. sub import {
  9. my $pkg = shift;
  10. my $handler = \&handler_traceback;
  11. my $saw_sig = 0;
  12. my $untrapped = 0;
  13. local $_;
  14. Arg_loop:
  15. while (@_) {
  16. $_ = shift;
  17. if (/^[A-Z][A-Z0-9]*$/) {
  18. $saw_sig++;
  19. unless ($untrapped and $SIG{$_} and $SIG{$_} ne 'DEFAULT') {
  20. print "Installing handler $handler for $_\n" if $Verbose;
  21. $SIG{$_} = $handler;
  22. }
  23. }
  24. elsif ($_ eq 'normal-signals') {
  25. unshift @_, grep(exists $SIG{$_}, qw(HUP INT PIPE TERM));
  26. }
  27. elsif ($_ eq 'error-signals') {
  28. unshift @_, grep(exists $SIG{$_},
  29. qw(ABRT BUS EMT FPE ILL QUIT SEGV SYS TRAP));
  30. }
  31. elsif ($_ eq 'old-interface-signals') {
  32. unshift @_,
  33. grep(exists $SIG{$_},
  34. qw(ABRT BUS EMT FPE ILL PIPE QUIT SEGV SYS TERM TRAP));
  35. }
  36. elsif ($_ eq 'stack-trace') {
  37. $handler = \&handler_traceback;
  38. }
  39. elsif ($_ eq 'die') {
  40. $handler = \&handler_die;
  41. }
  42. elsif ($_ eq 'handler') {
  43. @_ or croak "No argument specified after 'handler'";
  44. $handler = shift;
  45. unless (ref $handler or $handler eq 'IGNORE'
  46. or $handler eq 'DEFAULT') {
  47. require Symbol;
  48. $handler = Symbol::qualify($handler, (caller)[0]);
  49. }
  50. }
  51. elsif ($_ eq 'untrapped') {
  52. $untrapped = 1;
  53. }
  54. elsif ($_ eq 'any') {
  55. $untrapped = 0;
  56. }
  57. elsif ($_ =~ /^\d/) {
  58. $VERSION >= $_ or croak "sigtrap.pm version $_ required,"
  59. . " but this is only version $VERSION";
  60. }
  61. else {
  62. croak "Unrecognized argument $_";
  63. }
  64. }
  65. unless ($saw_sig) {
  66. @_ = qw(old-interface-signals);
  67. goto Arg_loop;
  68. }
  69. }
  70. sub handler_die {
  71. croak "Caught a SIG$_[0]";
  72. }
  73. sub handler_traceback {
  74. package DB; # To get subroutine args.
  75. $SIG{'ABRT'} = DEFAULT;
  76. kill 'ABRT', $$ if $panic++;
  77. syswrite(STDERR, 'Caught a SIG', 12);
  78. syswrite(STDERR, $_[0], length($_[0]));
  79. syswrite(STDERR, ' at ', 4);
  80. ($pack,$file,$line) = caller;
  81. syswrite(STDERR, $file, length($file));
  82. syswrite(STDERR, ' line ', 6);
  83. syswrite(STDERR, $line, length($line));
  84. syswrite(STDERR, "\n", 1);
  85. # Now go for broke.
  86. for ($i = 1; ($p,$f,$l,$s,$h,$w,$e,$r) = caller($i); $i++) {
  87. @a = ();
  88. for $arg (@args) {
  89. $_ = "$arg";
  90. s/([\'\\])/\\$1/g;
  91. s/([^\0]*)/'$1'/
  92. unless /^(?: -?[\d.]+ | \*[\w:]* )$/x;
  93. s/([\200-\377])/sprintf("M-%c",ord($1)&0177)/eg;
  94. s/([\0-\37\177])/sprintf("^%c",ord($1)^64)/eg;
  95. push(@a, $_);
  96. }
  97. $w = $w ? '@ = ' : '$ = ';
  98. $a = $h ? '(' . join(', ', @a) . ')' : '';
  99. $e =~ s/\n\s*\;\s*\Z// if $e;
  100. $e =~ s/[\\\']/\\$1/g if $e;
  101. if ($r) {
  102. $s = "require '$e'";
  103. } elsif (defined $r) {
  104. $s = "eval '$e'";
  105. } elsif ($s eq '(eval)') {
  106. $s = "eval {...}";
  107. }
  108. $f = "file `$f'" unless $f eq '-e';
  109. $mess = "$w$s$a called from $f line $l\n";
  110. syswrite(STDERR, $mess, length($mess));
  111. }
  112. kill 'ABRT', $$;
  113. }
  114. 1;
  115. __END__
  116. =head1 SYNOPSIS
  117. use sigtrap;
  118. use sigtrap qw(stack-trace old-interface-signals); # equivalent
  119. use sigtrap qw(BUS SEGV PIPE ABRT);
  120. use sigtrap qw(die INT QUIT);
  121. use sigtrap qw(die normal-signals);
  122. use sigtrap qw(die untrapped normal-signals);
  123. use sigtrap qw(die untrapped normal-signals
  124. stack-trace any error-signals);
  125. use sigtrap 'handler' => \&my_handler, 'normal-signals';
  126. use sigtrap qw(handler my_handler normal-signals
  127. stack-trace error-signals);
  128. =head1 DESCRIPTION
  129. The B<sigtrap> pragma is a simple interface to installing signal
  130. handlers. You can have it install one of two handlers supplied by
  131. B<sigtrap> itself (one which provides a Perl stack trace and one which
  132. simply C<die()>s), or alternately you can supply your own handler for it
  133. to install. It can be told only to install a handler for signals which
  134. are either untrapped or ignored. It has a couple of lists of signals to
  135. trap, plus you can supply your own list of signals.
  136. The arguments passed to the C<use> statement which invokes B<sigtrap>
  137. are processed in order. When a signal name or the name of one of
  138. B<sigtrap>'s signal lists is encountered a handler is immediately
  139. installed, when an option is encountered it affects subsequently
  140. installed handlers.
  141. =head1 OPTIONS
  142. =head2 SIGNAL HANDLERS
  143. These options affect which handler will be used for subsequently
  144. installed signals.
  145. =over 4
  146. =item B<stack-trace>
  147. The handler used for subsequently installed signals outputs a Perl stack
  148. trace to STDERR and then tries to dump core. This is the default signal
  149. handler.
  150. =item B<die>
  151. The handler used for subsequently installed signals calls C<die>
  152. (actually C<croak>) with a message indicating which signal was caught.
  153. =item B<handler> I<your-handler>
  154. I<your-handler> will be used as the handler for subsequently installed
  155. signals. I<your-handler> can be any value which is valid as an
  156. assignment to an element of C<%SIG>.
  157. =back
  158. =head2 SIGNAL LISTS
  159. B<sigtrap> has a few built-in lists of signals to trap. They are:
  160. =over 4
  161. =item B<normal-signals>
  162. These are the signals which a program might normally expect to encounter
  163. and which by default cause it to terminate. They are HUP, INT, PIPE and
  164. TERM.
  165. =item B<error-signals>
  166. These signals usually indicate a serious problem with the Perl
  167. interpreter or with your script. They are ABRT, BUS, EMT, FPE, ILL,
  168. QUIT, SEGV, SYS and TRAP.
  169. =item B<old-interface-signals>
  170. These are the signals which were trapped by default by the old
  171. B<sigtrap> interface, they are ABRT, BUS, EMT, FPE, ILL, PIPE, QUIT,
  172. SEGV, SYS, TERM, and TRAP. If no signals or signals lists are passed to
  173. B<sigtrap>, this list is used.
  174. =back
  175. For each of these three lists, the collection of signals set to be
  176. trapped is checked before trapping; if your architecture does not
  177. implement a particular signal, it will not be trapped but rather
  178. silently ignored.
  179. =head2 OTHER
  180. =over 4
  181. =item B<untrapped>
  182. This token tells B<sigtrap> to install handlers only for subsequently
  183. listed signals which aren't already trapped or ignored.
  184. =item B<any>
  185. This token tells B<sigtrap> to install handlers for all subsequently
  186. listed signals. This is the default behavior.
  187. =item I<signal>
  188. Any argument which looks like a signal name (that is,
  189. C</^[A-Z][A-Z0-9]*$/>) indicates that B<sigtrap> should install a
  190. handler for that name.
  191. =item I<number>
  192. Require that at least version I<number> of B<sigtrap> is being used.
  193. =back
  194. =head1 EXAMPLES
  195. Provide a stack trace for the old-interface-signals:
  196. use sigtrap;
  197. Ditto:
  198. use sigtrap qw(stack-trace old-interface-signals);
  199. Provide a stack trace on the 4 listed signals only:
  200. use sigtrap qw(BUS SEGV PIPE ABRT);
  201. Die on INT or QUIT:
  202. use sigtrap qw(die INT QUIT);
  203. Die on HUP, INT, PIPE or TERM:
  204. use sigtrap qw(die normal-signals);
  205. Die on HUP, INT, PIPE or TERM, except don't change the behavior for
  206. signals which are already trapped or ignored:
  207. use sigtrap qw(die untrapped normal-signals);
  208. Die on receipt one of an of the B<normal-signals> which is currently
  209. B<untrapped>, provide a stack trace on receipt of B<any> of the
  210. B<error-signals>:
  211. use sigtrap qw(die untrapped normal-signals
  212. stack-trace any error-signals);
  213. Install my_handler() as the handler for the B<normal-signals>:
  214. use sigtrap 'handler', \&my_handler, 'normal-signals';
  215. Install my_handler() as the handler for the normal-signals, provide a
  216. Perl stack trace on receipt of one of the error-signals:
  217. use sigtrap qw(handler my_handler normal-signals
  218. stack-trace error-signals);
  219. =cut