Initial commit of OpenSPARC T2 architecture model.
[OpenSPARC-T2-SAM] / sam-t2 / devtools / v8plus / bin / splain
CommitLineData
920dae64
AT
1#!/import/archperf/ws/devtools/4/v8plus/bin/perl
2 eval 'exec /import/archperf/ws/devtools/4/v8plus/bin/perl -S $0 ${1+"$@"}'
3 if $running_under_some_shell;
4
5=head1 NAME
6
7diagnostics, splain - produce verbose warning diagnostics
8
9=head1 SYNOPSIS
10
11Using the C<diagnostics> pragma:
12
13 use diagnostics;
14 use diagnostics -verbose;
15
16 enable diagnostics;
17 disable diagnostics;
18
19Using the C<splain> standalone filter program:
20
21 perl program 2>diag.out
22 splain [-v] [-p] diag.out
23
24Using diagnostics to get stack traces from a misbehaving script:
25
26 perl -Mdiagnostics=-traceonly my_script.pl
27
28=head1 DESCRIPTION
29
30=head2 The C<diagnostics> Pragma
31
32This module extends the terse diagnostics normally emitted by both the
33perl compiler and the perl interpreter (from running perl with a -w
34switch or C<use warnings>), augmenting them with the more
35explicative and endearing descriptions found in L<perldiag>. Like the
36other pragmata, it affects the compilation phase of your program rather
37than merely the execution phase.
38
39To use in your program as a pragma, merely invoke
40
41 use diagnostics;
42
43at the start (or near the start) of your program. (Note
44that this I<does> enable perl's B<-w> flag.) Your whole
45compilation will then be subject(ed :-) to the enhanced diagnostics.
46These still go out B<STDERR>.
47
48Due to the interaction between runtime and compiletime issues,
49and because it's probably not a very good idea anyway,
50you may not use C<no diagnostics> to turn them off at compiletime.
51However, you may control their behaviour at runtime using the
52disable() and enable() methods to turn them off and on respectively.
53
54The B<-verbose> flag first prints out the L<perldiag> introduction before
55any other diagnostics. The $diagnostics::PRETTY variable can generate nicer
56escape sequences for pagers.
57
58Warnings dispatched from perl itself (or more accurately, those that match
59descriptions found in L<perldiag>) are only displayed once (no duplicate
60descriptions). User code generated warnings a la warn() are unaffected,
61allowing duplicate user messages to be displayed.
62
63This module also adds a stack trace to the error message when perl dies.
64This is useful for pinpointing what caused the death. The B<-traceonly> (or
65just B<-t>) flag turns off the explanations of warning messages leaving just
66the stack traces. So if your script is dieing, run it again with
67
68 perl -Mdiagnostics=-traceonly my_bad_script
69
70to see the call stack at the time of death. By supplying the B<-warntrace>
71(or just B<-w>) flag, any warnings emitted will also come with a stack
72trace.
73
74=head2 The I<splain> Program
75
76While apparently a whole nuther program, I<splain> is actually nothing
77more than a link to the (executable) F<diagnostics.pm> module, as well as
78a link to the F<diagnostics.pod> documentation. The B<-v> flag is like
79the C<use diagnostics -verbose> directive.
80The B<-p> flag is like the
81$diagnostics::PRETTY variable. Since you're post-processing with
82I<splain>, there's no sense in being able to enable() or disable() processing.
83
84Output from I<splain> is directed to B<STDOUT>, unlike the pragma.
85
86=head1 EXAMPLES
87
88The following file is certain to trigger a few errors at both
89runtime and compiletime:
90
91 use diagnostics;
92 print NOWHERE "nothing\n";
93 print STDERR "\n\tThis message should be unadorned.\n";
94 warn "\tThis is a user warning";
95 print "\nDIAGNOSTIC TESTER: Please enter a <CR> here: ";
96 my $a, $b = scalar <STDIN>;
97 print "\n";
98 print $x/$y;
99
100If you prefer to run your program first and look at its problem
101afterwards, do this:
102
103 perl -w test.pl 2>test.out
104 ./splain < test.out
105
106Note that this is not in general possible in shells of more dubious heritage,
107as the theoretical
108
109 (perl -w test.pl >/dev/tty) >& test.out
110 ./splain < test.out
111
112Because you just moved the existing B<stdout> to somewhere else.
113
114If you don't want to modify your source code, but still have on-the-fly
115warnings, do this:
116
117 exec 3>&1; perl -w test.pl 2>&1 1>&3 3>&- | splain 1>&2 3>&-
118
119Nifty, eh?
120
121If you want to control warnings on the fly, do something like this.
122Make sure you do the C<use> first, or you won't be able to get
123at the enable() or disable() methods.
124
125 use diagnostics; # checks entire compilation phase
126 print "\ntime for 1st bogus diags: SQUAWKINGS\n";
127 print BOGUS1 'nada';
128 print "done with 1st bogus\n";
129
130 disable diagnostics; # only turns off runtime warnings
131 print "\ntime for 2nd bogus: (squelched)\n";
132 print BOGUS2 'nada';
133 print "done with 2nd bogus\n";
134
135 enable diagnostics; # turns back on runtime warnings
136 print "\ntime for 3rd bogus: SQUAWKINGS\n";
137 print BOGUS3 'nada';
138 print "done with 3rd bogus\n";
139
140 disable diagnostics;
141 print "\ntime for 4th bogus: (squelched)\n";
142 print BOGUS4 'nada';
143 print "done with 4th bogus\n";
144
145=head1 INTERNALS
146
147Diagnostic messages derive from the F<perldiag.pod> file when available at
148runtime. Otherwise, they may be embedded in the file itself when the
149splain package is built. See the F<Makefile> for details.
150
151If an extant $SIG{__WARN__} handler is discovered, it will continue
152to be honored, but only after the diagnostics::splainthis() function
153(the module's $SIG{__WARN__} interceptor) has had its way with your
154warnings.
155
156There is a $diagnostics::DEBUG variable you may set if you're desperately
157curious what sorts of things are being intercepted.
158
159 BEGIN { $diagnostics::DEBUG = 1 }
160
161
162=head1 BUGS
163
164Not being able to say "no diagnostics" is annoying, but may not be
165insurmountable.
166
167The C<-pretty> directive is called too late to affect matters.
168You have to do this instead, and I<before> you load the module.
169
170 BEGIN { $diagnostics::PRETTY = 1 }
171
172I could start up faster by delaying compilation until it should be
173needed, but this gets a "panic: top_level" when using the pragma form
174in Perl 5.001e.
175
176While it's true that this documentation is somewhat subserious, if you use
177a program named I<splain>, you should expect a bit of whimsy.
178
179=head1 AUTHOR
180
181Tom Christiansen <F<tchrist@mox.perl.com>>, 25 June 1995.
182
183=cut
184
185use strict;
186use 5.006;
187use Carp;
188$Carp::Internal{__PACKAGE__.""}++;
189
190our $VERSION = 1.15;
191our $DEBUG;
192our $VERBOSE;
193our $PRETTY;
194our $TRACEONLY = 0;
195our $WARNTRACE = 0;
196
197use Config;
198my($privlib, $archlib) = @Config{qw(privlibexp archlibexp)};
199if ($^O eq 'VMS') {
200 require VMS::Filespec;
201 $privlib = VMS::Filespec::unixify($privlib);
202 $archlib = VMS::Filespec::unixify($archlib);
203}
204my @trypod = (
205 "$archlib/pod/perldiag.pod",
206 "$privlib/pod/perldiag-$Config{version}.pod",
207 "$privlib/pod/perldiag.pod",
208 "$archlib/pods/perldiag.pod",
209 "$privlib/pods/perldiag-$Config{version}.pod",
210 "$privlib/pods/perldiag.pod",
211 );
212# handy for development testing of new warnings etc
213unshift @trypod, "./pod/perldiag.pod" if -e "pod/perldiag.pod";
214(my $PODFILE) = ((grep { -e } @trypod), $trypod[$#trypod])[0];
215
216if ($^O eq 'MacOS') {
217 # just updir one from each lib dir, we'll find it ...
218 ($PODFILE) = grep { -e } map { "$_:pod:perldiag.pod" } @INC;
219}
220
221
222$DEBUG ||= 0;
223my $WHOAMI = ref bless []; # nobody's business, prolly not even mine
224
225local $| = 1;
226local $_;
227
228my $standalone;
229my(%HTML_2_Troff, %HTML_2_Latin_1, %HTML_2_ASCII_7);
230
231CONFIG: {
232 our $opt_p = our $opt_d = our $opt_v = our $opt_f = '';
233
234 unless (caller) {
235 $standalone++;
236 require Getopt::Std;
237 Getopt::Std::getopts('pdvf:')
238 or die "Usage: $0 [-v] [-p] [-f splainpod]";
239 $PODFILE = $opt_f if $opt_f;
240 $DEBUG = 2 if $opt_d;
241 $VERBOSE = $opt_v;
242 $PRETTY = $opt_p;
243 }
244
245 if (open(POD_DIAG, $PODFILE)) {
246 warn "Happy happy podfile from real $PODFILE\n" if $DEBUG;
247 last CONFIG;
248 }
249
250 if (caller) {
251 INCPATH: {
252 for my $file ( (map { "$_/$WHOAMI.pm" } @INC), $0) {
253 warn "Checking $file\n" if $DEBUG;
254 if (open(POD_DIAG, $file)) {
255 while (<POD_DIAG>) {
256 next unless
257 /^__END__\s*# wish diag dbase were more accessible/;
258 print STDERR "podfile is $file\n" if $DEBUG;
259 last INCPATH;
260 }
261 }
262 }
263 }
264 } else {
265 print STDERR "podfile is <DATA>\n" if $DEBUG;
266 *POD_DIAG = *main::DATA;
267 }
268}
269if (eof(POD_DIAG)) {
270 die "couldn't find diagnostic data in $PODFILE @INC $0";
271}
272
273
274%HTML_2_Troff = (
275 'amp' => '&', # ampersand
276 'lt' => '<', # left chevron, less-than
277 'gt' => '>', # right chevron, greater-than
278 'quot' => '"', # double quote
279
280 "Aacute" => "A\\*'", # capital A, acute accent
281 # etc
282
283);
284
285%HTML_2_Latin_1 = (
286 'amp' => '&', # ampersand
287 'lt' => '<', # left chevron, less-than
288 'gt' => '>', # right chevron, greater-than
289 'quot' => '"', # double quote
290
291 "Aacute" => "\xC1" # capital A, acute accent
292
293 # etc
294);
295
296%HTML_2_ASCII_7 = (
297 'amp' => '&', # ampersand
298 'lt' => '<', # left chevron, less-than
299 'gt' => '>', # right chevron, greater-than
300 'quot' => '"', # double quote
301
302 "Aacute" => "A" # capital A, acute accent
303 # etc
304);
305
306our %HTML_Escapes;
307*HTML_Escapes = do {
308 if ($standalone) {
309 $PRETTY ? \%HTML_2_Latin_1 : \%HTML_2_ASCII_7;
310 } else {
311 \%HTML_2_Latin_1;
312 }
313};
314
315*THITHER = $standalone ? *STDOUT : *STDERR;
316
317my %transfmt = ();
318my $transmo = <<EOFUNC;
319sub transmo {
320 #local \$^W = 0; # recursive warnings we do NOT need!
321 study;
322EOFUNC
323
324my %msg;
325{
326 print STDERR "FINISHING COMPILATION for $_\n" if $DEBUG;
327 local $/ = '';
328 local $_;
329 my $header;
330 my $for_item;
331 while (<POD_DIAG>) {
332
333 unescape();
334 if ($PRETTY) {
335 sub noop { return $_[0] } # spensive for a noop
336 sub bold { my $str =$_[0]; $str =~ s/(.)/$1\b$1/g; return $str; }
337 sub italic { my $str = $_[0]; $str =~ s/(.)/_\b$1/g; return $str; }
338 s/C<<< (.*?) >>>|C<< (.*?) >>|[BC]<(.*?)>/bold($+)/ges;
339 s/[LIF]<(.*?)>/italic($1)/ges;
340 } else {
341 s/C<<< (.*?) >>>|C<< (.*?) >>|[BC]<(.*?)>/$+/gs;
342 s/[LIF]<(.*?)>/$1/gs;
343 }
344 unless (/^=/) {
345 if (defined $header) {
346 if ( $header eq 'DESCRIPTION' &&
347 ( /Optional warnings are enabled/
348 || /Some of these messages are generic./
349 ) )
350 {
351 next;
352 }
353 s/^/ /gm;
354 $msg{$header} .= $_;
355 undef $for_item;
356 }
357 next;
358 }
359 unless ( s/=item (.*?)\s*\z//) {
360
361 if ( s/=head1\sDESCRIPTION//) {
362 $msg{$header = 'DESCRIPTION'} = '';
363 undef $for_item;
364 }
365 elsif( s/^=for\s+diagnostics\s*\n(.*?)\s*\z// ) {
366 $for_item = $1;
367 }
368 next;
369 }
370
371 if( $for_item ) { $header = $for_item; undef $for_item }
372 else {
373 $header = $1;
374 while( $header =~ /[;,]\z/ ) {
375 <POD_DIAG> =~ /^\s*(.*?)\s*\z/;
376 $header .= ' '.$1;
377 }
378 }
379
380 # strip formatting directives from =item line
381 $header =~ s/[A-Z]<(.*?)>/$1/g;
382
383 my @toks = split( /(%l?[dx]|%c|%(?:\.\d+)?s)/, $header );
384 if (@toks > 1) {
385 my $conlen = 0;
386 for my $i (0..$#toks){
387 if( $i % 2 ){
388 if( $toks[$i] eq '%c' ){
389 $toks[$i] = '.';
390 } elsif( $toks[$i] eq '%d' ){
391 $toks[$i] = '\d+';
392 } elsif( $toks[$i] eq '%s' ){
393 $toks[$i] = $i == $#toks ? '.*' : '.*?';
394 } elsif( $toks[$i] =~ '%.(\d+)s' ){
395 $toks[$i] = ".{$1}";
396 } elsif( $toks[$i] =~ '^%l*x$' ){
397 $toks[$i] = '[\da-f]+';
398 }
399 } elsif( length( $toks[$i] ) ){
400 $toks[$i] =~ s/^.*$/\Q$&\E/;
401 $conlen += length( $toks[$i] );
402 }
403 }
404 my $lhs = join( '', @toks );
405 $transfmt{$header}{pat} =
406 " s{^$lhs}\n {\Q$header\E}s\n\t&& return 1;\n";
407 $transfmt{$header}{len} = $conlen;
408 } else {
409 $transfmt{$header}{pat} =
410 " m{^\Q$header\E} && return 1;\n";
411 $transfmt{$header}{len} = length( $header );
412 }
413
414 print STDERR "$WHOAMI: Duplicate entry: \"$header\"\n"
415 if $msg{$header};
416
417 $msg{$header} = '';
418 }
419
420
421 close POD_DIAG unless *main::DATA eq *POD_DIAG;
422
423 die "No diagnostics?" unless %msg;
424
425 # Apply patterns in order of decreasing sum of lengths of fixed parts
426 # Seems the best way of hitting the right one.
427 for my $hdr ( sort { $transfmt{$b}{len} <=> $transfmt{$a}{len} }
428 keys %transfmt ){
429 $transmo .= $transfmt{$hdr}{pat};
430 }
431 $transmo .= " return 0;\n}\n";
432 print STDERR $transmo if $DEBUG;
433 eval $transmo;
434 die $@ if $@;
435}
436
437if ($standalone) {
438 if (!@ARGV and -t STDIN) { print STDERR "$0: Reading from STDIN\n" }
439 while (defined (my $error = <>)) {
440 splainthis($error) || print THITHER $error;
441 }
442 exit;
443}
444
445my $olddie;
446my $oldwarn;
447
448sub import {
449 shift;
450 $^W = 1; # yup, clobbered the global variable;
451 # tough, if you want diags, you want diags.
452 return if defined $SIG{__WARN__} && ($SIG{__WARN__} eq \&warn_trap);
453
454 for (@_) {
455
456 /^-d(ebug)?$/ && do {
457 $DEBUG++;
458 next;
459 };
460
461 /^-v(erbose)?$/ && do {
462 $VERBOSE++;
463 next;
464 };
465
466 /^-p(retty)?$/ && do {
467 print STDERR "$0: I'm afraid it's too late for prettiness.\n";
468 $PRETTY++;
469 next;
470 };
471
472 /^-t(race)?$/ && do {
473 $TRACEONLY++;
474 next;
475 };
476 /^-w(arntrace)?$/ && do {
477 $WARNTRACE++;
478 next;
479 };
480
481 warn "Unknown flag: $_";
482 }
483
484 $oldwarn = $SIG{__WARN__};
485 $olddie = $SIG{__DIE__};
486 $SIG{__WARN__} = \&warn_trap;
487 $SIG{__DIE__} = \&death_trap;
488}
489
490sub enable { &import }
491
492sub disable {
493 shift;
494 return unless $SIG{__WARN__} eq \&warn_trap;
495 $SIG{__WARN__} = $oldwarn || '';
496 $SIG{__DIE__} = $olddie || '';
497}
498
499sub warn_trap {
500 my $warning = $_[0];
501 if (caller eq $WHOAMI or !splainthis($warning)) {
502 if ($WARNTRACE) {
503 print STDERR Carp::longmess($warning);
504 } else {
505 print STDERR $warning;
506 }
507 }
508 goto &$oldwarn if defined $oldwarn and $oldwarn and $oldwarn ne \&warn_trap;
509};
510
511sub death_trap {
512 my $exception = $_[0];
513
514 # See if we are coming from anywhere within an eval. If so we don't
515 # want to explain the exception because it's going to get caught.
516 my $in_eval = 0;
517 my $i = 0;
518 while (my $caller = (caller($i++))[3]) {
519 if ($caller eq '(eval)') {
520 $in_eval = 1;
521 last;
522 }
523 }
524
525 splainthis($exception) unless $in_eval;
526 if (caller eq $WHOAMI) { print STDERR "INTERNAL EXCEPTION: $exception"; }
527 &$olddie if defined $olddie and $olddie and $olddie ne \&death_trap;
528
529 return if $in_eval;
530
531 # We don't want to unset these if we're coming from an eval because
532 # then we've turned off diagnostics.
533
534 # Switch off our die/warn handlers so we don't wind up in our own
535 # traps.
536 $SIG{__DIE__} = $SIG{__WARN__} = '';
537
538 # Have carp skip over death_trap() when showing the stack trace.
539 local($Carp::CarpLevel) = 1;
540
541 confess "Uncaught exception from user code:\n\t$exception";
542 # up we go; where we stop, nobody knows, but i think we die now
543 # but i'm deeply afraid of the &$olddie guy reraising and us getting
544 # into an indirect recursion loop
545};
546
547my %exact_duplicate;
548my %old_diag;
549my $count;
550my $wantspace;
551sub splainthis {
552 return 0 if $TRACEONLY;
553 local $_ = shift;
554 local $\;
555 ### &finish_compilation unless %msg;
556 s/\.?\n+$//;
557 my $orig = $_;
558 # return unless defined;
559
560 # get rid of the where-are-we-in-input part
561 s/, <.*?> (?:line|chunk).*$//;
562
563 # Discard 1st " at <file> line <no>" and all text beyond
564 # but be aware of messsages containing " at this-or-that"
565 my $real = 0;
566 my @secs = split( / at / );
567 $_ = $secs[0];
568 for my $i ( 1..$#secs ){
569 if( $secs[$i] =~ /.+? (?:line|chunk) \d+/ ){
570 $real = 1;
571 last;
572 } else {
573 $_ .= ' at ' . $secs[$i];
574 }
575 }
576
577 # remove parenthesis occurring at the end of some messages
578 s/^\((.*)\)$/$1/;
579
580 if ($exact_duplicate{$orig}++) {
581 return &transmo;
582 } else {
583 return 0 unless &transmo;
584 }
585
586 $orig = shorten($orig);
587 if ($old_diag{$_}) {
588 autodescribe();
589 print THITHER "$orig (#$old_diag{$_})\n";
590 $wantspace = 1;
591 } else {
592 autodescribe();
593 $old_diag{$_} = ++$count;
594 print THITHER "\n" if $wantspace;
595 $wantspace = 0;
596 print THITHER "$orig (#$old_diag{$_})\n";
597 if ($msg{$_}) {
598 print THITHER $msg{$_};
599 } else {
600 if (0 and $standalone) {
601 print THITHER " **** Error #$old_diag{$_} ",
602 ($real ? "is" : "appears to be"),
603 " an unknown diagnostic message.\n\n";
604 }
605 return 0;
606 }
607 }
608 return 1;
609}
610
611sub autodescribe {
612 if ($VERBOSE and not $count) {
613 print THITHER &{$PRETTY ? \&bold : \&noop}("DESCRIPTION OF DIAGNOSTICS"),
614 "\n$msg{DESCRIPTION}\n";
615 }
616}
617
618sub unescape {
619 s {
620 E<
621 ( [A-Za-z]+ )
622 >
623 } {
624 do {
625 exists $HTML_Escapes{$1}
626 ? do { $HTML_Escapes{$1} }
627 : do {
628 warn "Unknown escape: E<$1> in $_";
629 "E<$1>";
630 }
631 }
632 }egx;
633}
634
635sub shorten {
636 my $line = $_[0];
637 if (length($line) > 79 and index($line, "\n") == -1) {
638 my $space_place = rindex($line, ' ', 79);
639 if ($space_place != -1) {
640 substr($line, $space_place, 1) = "\n\t";
641 }
642 }
643 return $line;
644}
645
646
6471 unless $standalone; # or it'll complain about itself
648__END__ # wish diag dbase were more accessible