Stop mod_perl looping forever in &Carp::shortmess_jmp as it somehow
[p5sagit/p5-mst-13.2.git] / lib / Carp / Heavy.pm
CommitLineData
b75c8c73 1# Carp::Heavy uses some variables in common with Carp.
3b5ca523 2package Carp;
ca24dfc6 3
ab648d5e 4=head1 NAME
5
4952958f 6Carp::Heavy - heavy machinery, no user serviceable parts inside
ab648d5e 7
8=cut
9
b75c8c73 10# On one line so MakeMaker will see it.
11use Carp; our $VERSION = $Carp::VERSION;
29ddba3b 12# use strict; # not yet
13
14# 'use Carp' just installs some very lightweight stubs; the first time
15# these are called, they require Carp::Heavy which installs the real
16# routines.
17
29ddba3b 18# The members of %Internal are packages that are internal to perl.
19# Carp will not report errors from within these packages if it
20# can. The members of %CarpInternal are internal to Perl's warning
21# system. Carp will not report errors from within these packages
22# either, and will not report calls *to* these packages for carp and
23# croak. They replace $CarpLevel, which is deprecated. The
24# $Max(EvalLen|(Arg(Len|Nums)) variables are used to specify how the eval
25# text and function arguments should be formatted when printed.
26
29ddba3b 27# disable these by default, so they can live w/o require Carp
28$CarpInternal{Carp}++;
29$CarpInternal{warnings}++;
30$Internal{Exporter}++;
31$Internal{'Exporter::Heavy'}++;
32
b75c8c73 33
eb2aae6f 34our ($CarpLevel, $MaxArgNums, $MaxEvalLen, $MaxArgLen, $Verbose);
ca24dfc6 35
29ddba3b 36# XXX longmess_real and shortmess_real should really be merged into
37# XXX {long|sort}mess_heavy at some point
38
39sub longmess_real {
40 # Icky backwards compatibility wrapper. :-(
d735c2ef 41 #
42 # The story is that the original implementation hard-coded the
43 # number of call levels to go back, so calls to longmess were off
44 # by one. Other code began calling longmess and expecting this
45 # behaviour, so the replacement has to emulate that behaviour.
29ddba3b 46 my $call_pack = caller();
47 if ($Internal{$call_pack} or $CarpInternal{$call_pack}) {
48 return longmess_heavy(@_);
49 }
50 else {
51 local $CarpLevel = $CarpLevel + 1;
52 return longmess_heavy(@_);
53 }
54};
55
56sub shortmess_real {
57 # Icky backwards compatibility wrapper. :-(
58 my $call_pack = caller();
59 local @CARP_NOT = caller();
60 shortmess_heavy(@_);
61};
62
63# replace the two hooks added by Carp
64
65# aliasing the whole glob rather than just the CV slot avoids 'redefined'
66# warnings, even in the presence of perl -W (as used by lib/warnings.t !)
792941b2 67# However it has the potential to create infinite loops, if somehow Carp
68# is forcibly reloaded, but $INC{"Carp/Heavy.pm"} remains true.
69# Hence the extra hack of deleting the previous typeglob first.
29ddba3b 70
792941b2 71delete $Carp::{shortmess_jmp};
72delete $Carp::{longmess_jmp};
29ddba3b 73*longmess_jmp = *longmess_real;
74*shortmess_jmp = *shortmess_real;
75
76
66a4a569 77sub caller_info {
78 my $i = shift(@_) + 1;
79 package DB;
80 my %call_info;
81 @call_info{
82 qw(pack file line sub has_args wantarray evaltext is_require)
83 } = caller($i);
84
85 unless (defined $call_info{pack}) {
86 return ();
87 }
88
89 my $sub_name = Carp::get_subname(\%call_info);
90 if ($call_info{has_args}) {
a3775ca3 91 my @args = map {Carp::format_arg($_)} @DB::args;
66a4a569 92 if ($MaxArgNums and @args > $MaxArgNums) { # More than we want to show?
93 $#args = $MaxArgNums;
94 push @args, '...';
95 }
96 # Push the args onto the subroutine
1798c67d 97 $sub_name .= '(' . join (', ', @args) . ')';
66a4a569 98 }
99 $call_info{sub_name} = $sub_name;
100 return wantarray() ? %call_info : \%call_info;
101}
ca24dfc6 102
66a4a569 103# Transform an argument to a function into a string.
104sub format_arg {
105 my $arg = shift;
8a7acfb9 106 if (ref($arg)) {
3a57e0bb 107 $arg = defined($overload::VERSION) ? overload::StrVal($arg) : "$arg";
8a7acfb9 108 }elsif (not defined($arg)) {
109 $arg = 'undef';
66a4a569 110 }
111 $arg =~ s/'/\\'/g;
eb2aae6f 112 $arg = str_len_trim($arg, $MaxArgLen);
66a4a569 113
114 # Quote it?
115 $arg = "'$arg'" unless $arg =~ /^-?[\d.]+\z/;
116
117 # The following handling of "control chars" is direct from
e6ddefa8 118 # the original code - it is broken on Unicode though.
66a4a569 119 # Suggestions?
e6ddefa8 120 utf8::is_utf8($arg)
121 or $arg =~ s/([[:cntrl:]]|[[:^ascii:]])/sprintf("\\x{%x}",ord($1))/eg;
66a4a569 122 return $arg;
123}
ca24dfc6 124
66a4a569 125# Takes an inheritance cache and a package and returns
126# an anon hash of known inheritances and anon array of
127# inheritances which consequences have not been figured
128# for.
129sub get_status {
130 my $cache = shift;
131 my $pkg = shift;
132 $cache->{$pkg} ||= [{$pkg => $pkg}, [trusts_directly($pkg)]];
133 return @{$cache->{$pkg}};
134}
ca24dfc6 135
66a4a569 136# Takes the info from caller() and figures out the name of
137# the sub/require/eval
138sub get_subname {
139 my $info = shift;
976ea96e 140 if (defined($info->{evaltext})) {
141 my $eval = $info->{evaltext};
66a4a569 142 if ($info->{is_require}) {
143 return "require $eval";
144 }
145 else {
146 $eval =~ s/([\\\'])/\\$1/g;
976ea96e 147 return "eval '" . str_len_trim($eval, $MaxEvalLen) . "'";
66a4a569 148 }
149 }
150
151 return ($info->{sub} eq '(eval)') ? 'eval {...}' : $info->{sub};
152}
153
154# Figures out what call (from the point of view of the caller)
155# the long error backtrace should start at.
156sub long_error_loc {
157 my $i;
158 my $lvl = $CarpLevel;
159 {
160 my $pkg = caller(++$i);
161 unless(defined($pkg)) {
162 # This *shouldn't* happen.
163 if (%Internal) {
164 local %Internal;
165 $i = long_error_loc();
166 last;
167 }
168 else {
169 # OK, now I am irritated.
170 return 2;
171 }
172 }
173 redo if $CarpInternal{$pkg};
174 redo unless 0 > --$lvl;
175 redo if $Internal{$pkg};
176 }
177 return $i - 1;
178}
ca24dfc6 179
3cb6de81 180
66a4a569 181sub longmess_heavy {
25f5609c 182 return @_ if ref($_[0]); # don't break references as exceptions
66a4a569 183 my $i = long_error_loc();
184 return ret_backtrace($i, @_);
185}
ca24dfc6 186
66a4a569 187# Returns a full stack backtrace starting from where it is
188# told.
189sub ret_backtrace {
190 my ($i, @error) = @_;
191 my $mess;
192 my $err = join '', @error;
193 $i++;
194
195 my $tid_msg = '';
196 if (defined &Thread::tid) {
197 my $tid = Thread->self->tid;
198 $tid_msg = " thread $tid" if $tid;
199 }
200
59b0a8b7 201 my %i = caller_info($i);
202 $mess = "$err at $i{file} line $i{line}$tid_msg\n";
66a4a569 203
204 while (my %i = caller_info(++$i)) {
205 $mess .= "\t$i{sub_name} called at $i{file} line $i{line}$tid_msg\n";
fbb63a9e 206 }
66a4a569 207
25f5609c 208 return $mess;
66a4a569 209}
3b5ca523 210
66a4a569 211sub ret_summary {
212 my ($i, @error) = @_;
66a4a569 213 my $err = join '', @error;
214 $i++;
3b5ca523 215
66a4a569 216 my $tid_msg = '';
217 if (defined &Thread::tid) {
218 my $tid = Thread->self->tid;
219 $tid_msg = " thread $tid" if $tid;
220 }
3b5ca523 221
66a4a569 222 my %i = caller_info($i);
223 return "$err at $i{file} line $i{line}$tid_msg\n";
3b5ca523 224}
225
226
66a4a569 227sub short_error_loc {
228 my $cache;
229 my $i = 1;
230 my $lvl = $CarpLevel;
231 {
232 my $called = caller($i++);
233 my $caller = caller($i);
29ddba3b 234
66a4a569 235 return 0 unless defined($caller); # What happened?
236 redo if $Internal{$caller};
d735c2ef 237 redo if $CarpInternal{$caller};
66a4a569 238 redo if $CarpInternal{$called};
239 redo if trusts($called, $caller, $cache);
240 redo if trusts($caller, $called, $cache);
241 redo unless 0 > --$lvl;
242 }
243 return $i - 1;
191f2cf3 244}
245
29ddba3b 246
66a4a569 247sub shortmess_heavy {
248 return longmess_heavy(@_) if $Verbose;
25f5609c 249 return @_ if ref($_[0]); # don't break references as exceptions
66a4a569 250 my $i = short_error_loc();
251 if ($i) {
252 ret_summary($i, @_);
253 }
254 else {
255 longmess_heavy(@_);
256 }
257}
258
259# If a string is too long, trims it with ...
260sub str_len_trim {
261 my $str = shift;
262 my $max = shift || 0;
263 if (2 < $max and $max < length($str)) {
264 substr($str, $max - 3) = '...';
265 }
266 return $str;
267}
191f2cf3 268
66a4a569 269# Takes two packages and an optional cache. Says whether the
270# first inherits from the second.
271#
272# Recursive versions of this have to work to avoid certain
273# possible endless loops, and when following long chains of
274# inheritance are less efficient.
275sub trusts {
276 my $child = shift;
277 my $parent = shift;
278 my $cache = shift || {};
279 my ($known, $partial) = get_status($cache, $child);
280 # Figure out consequences until we have an answer
281 while (@$partial and not exists $known->{$parent}) {
282 my $anc = shift @$partial;
283 next if exists $known->{$anc};
284 $known->{$anc}++;
285 my ($anc_knows, $anc_partial) = get_status($cache, $anc);
286 my @found = keys %$anc_knows;
287 @$known{@found} = ();
288 push @$partial, @$anc_partial;
3b5ca523 289 }
66a4a569 290 return exists $known->{$parent};
291}
3b5ca523 292
66a4a569 293# Takes a package and gives a list of those trusted directly
294sub trusts_directly {
295 my $class = shift;
a3775ca3 296 no strict 'refs';
b5777b26 297 no warnings 'once';
a3775ca3 298 return @{"$class\::CARP_NOT"}
299 ? @{"$class\::CARP_NOT"}
300 : @{"$class\::ISA"};
3b5ca523 301}
302
3031;
66a4a569 304