Commit | Line | Data |
d338d6fe |
1 | require 5.002; # For (defined ref) |
2 | package dumpvar; |
3 | |
4 | # Needed for PrettyPrinter only: |
5 | |
6 | # require 5.001; # Well, it coredumps anyway undef DB in 5.000 (not now) |
7 | |
8 | # translate control chars to ^X - Randal Schwartz |
9 | # Modifications to print types by Peter Gordon v1.0 |
10 | |
11 | # Ilya Zakharevich -- patches after 5.001 (and some before ;-) |
12 | |
13 | # Won't dump symbol tables and contents of debugged files by default |
14 | |
15 | $winsize = 80 unless defined $winsize; |
16 | |
17 | |
18 | # Defaults |
19 | |
20 | # $globPrint = 1; |
21 | $printUndef = 1 unless defined $printUndef; |
54d04a52 |
22 | $tick = "auto" unless defined $tick; |
d338d6fe |
23 | $unctrl = 'quote' unless defined $unctrl; |
54d04a52 |
24 | $subdump = 1; |
22fae026 |
25 | $dumpReused = 0 unless defined $dumpReused; |
ee239bfe |
26 | $bareStringify = 1 unless defined $bareStringify; |
d338d6fe |
27 | |
28 | sub main::dumpValue { |
29 | local %address; |
b2391ea8 |
30 | local $^W=0; |
d338d6fe |
31 | (print "undef\n"), return unless defined $_[0]; |
32 | (print &stringify($_[0]), "\n"), return unless ref $_[0]; |
33 | dumpvar::unwrap($_[0],0); |
34 | } |
35 | |
36 | # This one is good for variable names: |
37 | |
38 | sub unctrl { |
39 | local($_) = @_; |
40 | local($v) ; |
41 | |
42 | return \$_ if ref \$_ eq "GLOB"; |
43 | s/([\001-\037\177])/'^'.pack('c',ord($1)^64)/eg; |
44 | $_; |
45 | } |
46 | |
eb1102fc |
47 | sub uniescape { |
48 | join("", |
49 | map { $_ > 255 ? sprintf("\\x{%04X}", $_) : chr($_) } |
50 | unpack("U*", $_[0])); |
51 | } |
52 | |
d338d6fe |
53 | sub stringify { |
54 | local($_,$noticks) = @_; |
55 | local($v) ; |
54d04a52 |
56 | my $tick = $tick; |
d338d6fe |
57 | |
58 | return 'undef' unless defined $_ or not $printUndef; |
59 | return $_ . "" if ref \$_ eq 'GLOB'; |
ee239bfe |
60 | $_ = &{'overload::StrVal'}($_) |
61 | if $bareStringify and ref $_ |
475342a6 |
62 | and %overload:: and defined &{'overload::StrVal'}; |
ee239bfe |
63 | |
54d04a52 |
64 | if ($tick eq 'auto') { |
65 | if (/[\000-\011\013-\037\177]/) { |
66 | $tick = '"'; |
67 | }else { |
68 | $tick = "'"; |
69 | } |
70 | } |
d338d6fe |
71 | if ($tick eq "'") { |
72 | s/([\'\\])/\\$1/g; |
73 | } elsif ($unctrl eq 'unctrl') { |
74 | s/([\"\\])/\\$1/g ; |
75 | s/([\000-\037\177])/'^'.pack('c',ord($1)^64)/eg; |
eb1102fc |
76 | # uniescape? |
d338d6fe |
77 | s/([\200-\377])/'\\0x'.sprintf('%2X',ord($1))/eg |
78 | if $quoteHighBit; |
79 | } elsif ($unctrl eq 'quote') { |
80 | s/([\"\\\$\@])/\\$1/g if $tick eq '"'; |
81 | s/\033/\\e/g; |
82 | s/([\000-\037\177])/'\\c'.chr(ord($1)^64)/eg; |
83 | } |
eb1102fc |
84 | $_ = uniescape($_); |
d338d6fe |
85 | s/([\200-\377])/'\\'.sprintf('%3o',ord($1))/eg if $quoteHighBit; |
86 | ($noticks || /^\d+(\.\d*)?\Z/) |
87 | ? $_ |
88 | : $tick . $_ . $tick; |
89 | } |
90 | |
91 | sub ShortArray { |
92 | my $tArrayDepth = $#{$_[0]} ; |
93 | $tArrayDepth = $#{$_[0]} < $arrayDepth-1 ? $#{$_[0]} : $arrayDepth-1 |
94 | unless $arrayDepth eq '' ; |
95 | my $shortmore = ""; |
96 | $shortmore = " ..." if $tArrayDepth < $#{$_[0]} ; |
97 | if (!grep(ref $_, @{$_[0]})) { |
98 | $short = "0..$#{$_[0]} '" . |
99 | join("' '", @{$_[0]}[0..$tArrayDepth]) . "'$shortmore"; |
100 | return $short if length $short <= $compactDump; |
101 | } |
102 | undef; |
103 | } |
104 | |
105 | sub DumpElem { |
106 | my $short = &stringify($_[0], ref $_[0]); |
107 | if ($veryCompact && ref $_[0] |
108 | && (ref $_[0] eq 'ARRAY' and !grep(ref $_, @{$_[0]}) )) { |
109 | my $end = "0..$#{$v} '" . |
110 | join("' '", @{$_[0]}[0..$tArrayDepth]) . "'$shortmore"; |
111 | } elsif ($veryCompact && ref $_[0] |
112 | && (ref $_[0] eq 'HASH') and !grep(ref $_, values %{$_[0]})) { |
113 | my $end = 1; |
114 | $short = $sp . "0..$#{$v} '" . |
115 | join("' '", @{$v}[0..$tArrayDepth]) . "'$shortmore"; |
116 | } else { |
117 | print "$short\n"; |
118 | unwrap($_[0],$_[1]); |
119 | } |
120 | } |
121 | |
122 | sub unwrap { |
123 | return if $DB::signal; |
124 | local($v) = shift ; |
125 | local($s) = shift ; # extra no of spaces |
ee239bfe |
126 | local(%v,@v,$sp,$value,$key,@sortKeys,$more,$shortmore,$short) ; |
d338d6fe |
127 | local($tHashDepth,$tArrayDepth) ; |
128 | |
129 | $sp = " " x $s ; |
130 | $s += 3 ; |
131 | |
132 | # Check for reused addresses |
133 | if (ref $v) { |
ee239bfe |
134 | my $val = $v; |
135 | $val = &{'overload::StrVal'}($v) |
475342a6 |
136 | if %overload:: and defined &{'overload::StrVal'}; |
ee239bfe |
137 | ($address) = $val =~ /(0x[0-9a-f]+)\)$/ ; |
22fae026 |
138 | if (!$dumpReused && defined $address) { |
d338d6fe |
139 | $address{$address}++ ; |
140 | if ( $address{$address} > 1 ) { |
141 | print "${sp}-> REUSED_ADDRESS\n" ; |
142 | return ; |
143 | } |
144 | } |
145 | } elsif (ref \$v eq 'GLOB') { |
146 | $address = "$v" . ""; # To avoid a bug with globs |
147 | $address{$address}++ ; |
148 | if ( $address{$address} > 1 ) { |
149 | print "${sp}*DUMPED_GLOB*\n" ; |
150 | return ; |
151 | } |
152 | } |
153 | |
7894fbab |
154 | if (ref $v eq 'Regexp') { |
155 | my $re = "$v"; |
156 | $re =~ s,/,\\/,g; |
157 | print "$sp-> qr/$re/\n"; |
158 | return; |
159 | } |
160 | |
74b5f52c |
161 | if ( UNIVERSAL::isa($v, 'HASH') ) { |
d338d6fe |
162 | @sortKeys = sort keys(%$v) ; |
163 | undef $more ; |
164 | $tHashDepth = $#sortKeys ; |
165 | $tHashDepth = $#sortKeys < $hashDepth-1 ? $#sortKeys : $hashDepth-1 |
166 | unless $hashDepth eq '' ; |
167 | $more = "....\n" if $tHashDepth < $#sortKeys ; |
168 | $shortmore = ""; |
169 | $shortmore = ", ..." if $tHashDepth < $#sortKeys ; |
170 | $#sortKeys = $tHashDepth ; |
171 | if ($compactDump && !grep(ref $_, values %{$v})) { |
172 | #$short = $sp . |
173 | # (join ', ', |
174 | # Next row core dumps during require from DB on 5.000, even with map {"_"} |
175 | # map {&stringify($_) . " => " . &stringify($v->{$_})} |
176 | # @sortKeys) . "'$shortmore"; |
177 | $short = $sp; |
178 | my @keys; |
179 | for (@sortKeys) { |
180 | push @keys, &stringify($_) . " => " . &stringify($v->{$_}); |
181 | } |
182 | $short .= join ', ', @keys; |
183 | $short .= $shortmore; |
184 | (print "$short\n"), return if length $short <= $compactDump; |
185 | } |
186 | for $key (@sortKeys) { |
187 | return if $DB::signal; |
188 | $value = $ {$v}{$key} ; |
189 | print "$sp", &stringify($key), " => "; |
190 | DumpElem $value, $s; |
191 | } |
192 | print "$sp empty hash\n" unless @sortKeys; |
193 | print "$sp$more" if defined $more ; |
74b5f52c |
194 | } elsif ( UNIVERSAL::isa($v, 'ARRAY') ) { |
d338d6fe |
195 | $tArrayDepth = $#{$v} ; |
196 | undef $more ; |
197 | $tArrayDepth = $#{$v} < $arrayDepth-1 ? $#{$v} : $arrayDepth-1 |
600d99fa |
198 | if defined $arrayDepth && $arrayDepth ne ''; |
d338d6fe |
199 | $more = "....\n" if $tArrayDepth < $#{$v} ; |
200 | $shortmore = ""; |
201 | $shortmore = " ..." if $tArrayDepth < $#{$v} ; |
202 | if ($compactDump && !grep(ref $_, @{$v})) { |
203 | if ($#$v >= 0) { |
54d04a52 |
204 | $short = $sp . "0..$#{$v} " . |
205 | join(" ", |
d9182636 |
206 | map {exists $v->[$_] ? stringify $v->[$_] : "empty"} ($[..$tArrayDepth) |
207 | ) . "$shortmore"; |
d338d6fe |
208 | } else { |
209 | $short = $sp . "empty array"; |
210 | } |
211 | (print "$short\n"), return if length $short <= $compactDump; |
212 | } |
213 | #if ($compactDump && $short = ShortArray($v)) { |
214 | # print "$short\n"; |
215 | # return; |
216 | #} |
217 | for $num ($[ .. $tArrayDepth) { |
218 | return if $DB::signal; |
219 | print "$sp$num "; |
d9182636 |
220 | if (exists $v->[$num]) { |
221 | DumpElem $v->[$num], $s; |
222 | } else { |
223 | print "empty slot\n"; |
224 | } |
d338d6fe |
225 | } |
226 | print "$sp empty array\n" unless @$v; |
227 | print "$sp$more" if defined $more ; |
74b5f52c |
228 | } elsif ( UNIVERSAL::isa($v, 'SCALAR') or ref $v eq 'REF' ) { |
d338d6fe |
229 | print "$sp-> "; |
230 | DumpElem $$v, $s; |
74b5f52c |
231 | } elsif ( UNIVERSAL::isa($v, 'CODE') ) { |
54d04a52 |
232 | print "$sp-> "; |
233 | dumpsub (0, $v); |
74b5f52c |
234 | } elsif ( UNIVERSAL::isa($v, 'GLOB') ) { |
d338d6fe |
235 | print "$sp-> ",&stringify($$v,1),"\n"; |
236 | if ($globPrint) { |
237 | $s += 3; |
238 | dumpglob($s, "{$$v}", $$v, 1); |
239 | } elsif (defined ($fileno = fileno($v))) { |
240 | print( (' ' x ($s+3)) . "FileHandle({$$v}) => fileno($fileno)\n" ); |
241 | } |
242 | } elsif (ref \$v eq 'GLOB') { |
243 | if ($globPrint) { |
244 | dumpglob($s, "{$v}", $v, 1) if $globPrint; |
245 | } elsif (defined ($fileno = fileno(\$v))) { |
246 | print( (' ' x $s) . "FileHandle({$v}) => fileno($fileno)\n" ); |
247 | } |
248 | } |
249 | } |
250 | |
251 | sub matchvar { |
252 | $_[0] eq $_[1] or |
b2391ea8 |
253 | ($_[1] =~ /^([!~])(.)([\x00-\xff]*)/) and |
254 | ($1 eq '!') ^ (eval {($_[2] . "::" . $_[0]) =~ /$2$3/}); |
d338d6fe |
255 | } |
256 | |
257 | sub compactDump { |
258 | $compactDump = shift if @_; |
259 | $compactDump = 6*80-1 if $compactDump and $compactDump < 2; |
260 | $compactDump; |
261 | } |
262 | |
263 | sub veryCompact { |
264 | $veryCompact = shift if @_; |
265 | compactDump(1) if !$compactDump and $veryCompact; |
266 | $veryCompact; |
267 | } |
268 | |
269 | sub unctrlSet { |
270 | if (@_) { |
271 | my $in = shift; |
272 | if ($in eq 'unctrl' or $in eq 'quote') { |
273 | $unctrl = $in; |
274 | } else { |
275 | print "Unknown value for `unctrl'.\n"; |
276 | } |
277 | } |
278 | $unctrl; |
279 | } |
280 | |
281 | sub quote { |
282 | if (@_ and $_[0] eq '"') { |
283 | $tick = '"'; |
284 | $unctrl = 'quote'; |
54d04a52 |
285 | } elsif (@_ and $_[0] eq 'auto') { |
286 | $tick = 'auto'; |
287 | $unctrl = 'quote'; |
d338d6fe |
288 | } elsif (@_) { # Need to set |
289 | $tick = "'"; |
290 | $unctrl = 'unctrl'; |
291 | } |
292 | $tick; |
293 | } |
294 | |
295 | sub dumpglob { |
296 | return if $DB::signal; |
297 | my ($off,$key, $val, $all) = @_; |
298 | local(*entry) = $val; |
299 | my $fileno; |
54d04a52 |
300 | if (($key !~ /^_</ or $dumpDBFiles) and defined $entry) { |
d338d6fe |
301 | print( (' ' x $off) . "\$", &unctrl($key), " = " ); |
302 | DumpElem $entry, 3+$off; |
303 | } |
475342a6 |
304 | if (($key !~ /^_</ or $dumpDBFiles) and @entry) { |
d338d6fe |
305 | print( (' ' x $off) . "\@$key = (\n" ); |
306 | unwrap(\@entry,3+$off) ; |
307 | print( (' ' x $off) . ")\n" ); |
308 | } |
475342a6 |
309 | if ($key ne "main::" && $key ne "DB::" && %entry |
d338d6fe |
310 | && ($dumpPackages or $key !~ /::$/) |
54d04a52 |
311 | && ($key !~ /^_</ or $dumpDBFiles) |
d338d6fe |
312 | && !($package eq "dumpvar" and $key eq "stab")) { |
313 | print( (' ' x $off) . "\%$key = (\n" ); |
314 | unwrap(\%entry,3+$off) ; |
315 | print( (' ' x $off) . ")\n" ); |
316 | } |
317 | if (defined ($fileno = fileno(*entry))) { |
318 | print( (' ' x $off) . "FileHandle($key) => fileno($fileno)\n" ); |
319 | } |
320 | if ($all) { |
321 | if (defined &entry) { |
54d04a52 |
322 | dumpsub($off, $key); |
d338d6fe |
323 | } |
324 | } |
325 | } |
326 | |
83ee9e09 |
327 | sub CvGV_name_or_bust { |
328 | my $in = shift; |
329 | return if $skipCvGV; # Backdoor to avoid problems if XS broken... |
330 | $in = \&$in; # Hard reference... |
331 | eval {require Devel::Peek; 1} or return; |
332 | my $gv = Devel::Peek::CvGV($in) or return; |
333 | *$gv{PACKAGE} . '::' . *$gv{NAME}; |
334 | } |
335 | |
54d04a52 |
336 | sub dumpsub { |
337 | my ($off,$sub) = @_; |
83ee9e09 |
338 | my $ini = $sub; |
339 | my $s; |
54d04a52 |
340 | $sub = $1 if $sub =~ /^\{\*(.*)\}$/; |
83ee9e09 |
341 | my $subref = defined $1 ? \&$sub : \&$ini; |
342 | my $place = $DB::sub{$sub} || (($s = $subs{"$subref"}) && $DB::sub{$s}) |
343 | || (($s = CvGV_name_or_bust($subref)) && $DB::sub{$s}) |
344 | || ($subdump && ($s = findsubs("$subref")) && $DB::sub{$s}); |
54d04a52 |
345 | $place = '???' unless defined $place; |
83ee9e09 |
346 | $s = $sub unless defined $s; |
347 | print( (' ' x $off) . "&$s in $place\n" ); |
54d04a52 |
348 | } |
349 | |
350 | sub findsubs { |
475342a6 |
351 | return undef unless %DB::sub; |
54d04a52 |
352 | my ($addr, $name, $loc); |
353 | while (($name, $loc) = each %DB::sub) { |
354 | $addr = \&$name; |
355 | $subs{"$addr"} = $name; |
356 | } |
357 | $subdump = 0; |
358 | $subs{ shift() }; |
359 | } |
360 | |
d338d6fe |
361 | sub main::dumpvar { |
362 | my ($package,@vars) = @_; |
b2391ea8 |
363 | local(%address,$key,$val,$^W); |
d338d6fe |
364 | $package .= "::" unless $package =~ /::$/; |
365 | *stab = *{"main::"}; |
366 | while ($package =~ /(\w+?::)/g){ |
367 | *stab = $ {stab}{$1}; |
368 | } |
369 | local $TotalStrings = 0; |
370 | local $Strings = 0; |
371 | local $CompleteTotal = 0; |
372 | while (($key,$val) = each(%stab)) { |
373 | return if $DB::signal; |
374 | next if @vars && !grep( matchvar($key, $_), @vars ); |
375 | if ($usageOnly) { |
4c82ae22 |
376 | globUsage(\$val, $key) |
377 | if ($package ne 'dumpvar' or $key ne 'stab') |
378 | and ref(\$val) eq 'GLOB'; |
d338d6fe |
379 | } else { |
380 | dumpglob(0,$key, $val); |
381 | } |
382 | } |
383 | if ($usageOnly) { |
384 | print "String space: $TotalStrings bytes in $Strings strings.\n"; |
385 | $CompleteTotal += $TotalStrings; |
386 | print "Grand total = $CompleteTotal bytes (1 level deep) + overhead.\n"; |
387 | } |
388 | } |
389 | |
390 | sub scalarUsage { |
391 | my $size = length($_[0]); |
392 | $TotalStrings += $size; |
393 | $Strings++; |
394 | $size; |
395 | } |
396 | |
397 | sub arrayUsage { # array ref, name |
398 | my $size = 0; |
399 | map {$size += scalarUsage($_)} @{$_[0]}; |
400 | my $len = @{$_[0]}; |
401 | print "\@$_[1] = $len item", ($len > 1 ? "s" : ""), |
402 | " (data: $size bytes)\n" |
403 | if defined $_[1]; |
404 | $CompleteTotal += $size; |
405 | $size; |
406 | } |
407 | |
408 | sub hashUsage { # hash ref, name |
409 | my @keys = keys %{$_[0]}; |
410 | my @values = values %{$_[0]}; |
411 | my $keys = arrayUsage \@keys; |
412 | my $values = arrayUsage \@values; |
413 | my $len = @keys; |
414 | my $total = $keys + $values; |
415 | print "\%$_[1] = $len item", ($len > 1 ? "s" : ""), |
416 | " (keys: $keys; values: $values; total: $total bytes)\n" |
417 | if defined $_[1]; |
418 | $total; |
419 | } |
420 | |
421 | sub globUsage { # glob ref, name |
422 | local *name = *{$_[0]}; |
423 | $total = 0; |
424 | $total += scalarUsage $name if defined $name; |
475342a6 |
425 | $total += arrayUsage \@name, $_[1] if @name; |
426 | $total += hashUsage \%name, $_[1] if %name and $_[1] ne "main::" |
d338d6fe |
427 | and $_[1] ne "DB::"; #and !($package eq "dumpvar" and $key eq "stab")); |
428 | $total; |
429 | } |
430 | |
431 | sub packageUsage { |
432 | my ($package,@vars) = @_; |
433 | $package .= "::" unless $package =~ /::$/; |
434 | local *stab = *{"main::"}; |
435 | while ($package =~ /(\w+?::)/g){ |
436 | *stab = $ {stab}{$1}; |
437 | } |
438 | local $TotalStrings = 0; |
439 | local $CompleteTotal = 0; |
440 | my ($key,$val); |
441 | while (($key,$val) = each(%stab)) { |
442 | next if @vars && !grep($key eq $_,@vars); |
443 | globUsage \$val, $key unless $package eq 'dumpvar' and $key eq 'stab'; |
444 | } |
445 | print "String space: $TotalStrings.\n"; |
446 | $CompleteTotal += $TotalStrings; |
447 | print "\nGrand total = $CompleteTotal bytes\n"; |
448 | } |
449 | |
450 | 1; |
451 | |