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