Upgrade to base 2.03.
[p5sagit/p5-mst-13.2.git] / lib / dumpvar.pl
CommitLineData
d338d6fe 1require 5.002; # For (defined ref)
2package 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
28sub 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];
d03c2a1b 33 dumpvar::unwrap($_[0],0, $_[1]);
d338d6fe 34}
35
36# This one is good for variable names:
37
38sub 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
f8543d02 47sub uniescape {
48 join("",
49 map { $_ > 255 ? sprintf("\\x{%04X}", $_) : chr($_) }
50 unpack("U*", $_[0]));
51}
52
d338d6fe 53sub 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;
f8543d02 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 }
f8543d02 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
91sub 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
105sub 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";
9bcb75ad 118 unwrap($_[0],$_[1],$_[2]) if ref $_[0];
d338d6fe 119 }
120}
121
122sub unwrap {
123 return if $DB::signal;
124 local($v) = shift ;
125 local($s) = shift ; # extra no of spaces
d03c2a1b 126 local($m) = shift ; # maximum recursion depth
127 return if $m == 0;
ee239bfe 128 local(%v,@v,$sp,$value,$key,@sortKeys,$more,$shortmore,$short) ;
d338d6fe 129 local($tHashDepth,$tArrayDepth) ;
130
131 $sp = " " x $s ;
132 $s += 3 ;
133
134 # Check for reused addresses
135 if (ref $v) {
ee239bfe 136 my $val = $v;
137 $val = &{'overload::StrVal'}($v)
475342a6 138 if %overload:: and defined &{'overload::StrVal'};
9bcb75ad 139 # Match type and address.
140 # Unblessed references will look like TYPE(0x...)
141 # Blessed references will look like Class=TYPE(0x...)
142 ($start_part, $val) = split /=/,$val;
143 $val = $start_part unless defined $val;
144 ($item_type, $address) =
145 $val =~ /([^\(]+) # Keep stuff that's
146 # not an open paren
147 \( # Skip open paren
148 (0x[0-9a-f]+) # Save the address
149 \) # Skip close paren
150 $/x; # Should be at end now
151
22fae026 152 if (!$dumpReused && defined $address) {
d338d6fe 153 $address{$address}++ ;
154 if ( $address{$address} > 1 ) {
155 print "${sp}-> REUSED_ADDRESS\n" ;
156 return ;
157 }
158 }
159 } elsif (ref \$v eq 'GLOB') {
9bcb75ad 160 # This is a raw glob. Special handling for that.
d338d6fe 161 $address = "$v" . ""; # To avoid a bug with globs
162 $address{$address}++ ;
163 if ( $address{$address} > 1 ) {
164 print "${sp}*DUMPED_GLOB*\n" ;
165 return ;
166 }
167 }
168
7894fbab 169 if (ref $v eq 'Regexp') {
9bcb75ad 170 # Reformat the regexp to look the standard way.
7894fbab 171 my $re = "$v";
172 $re =~ s,/,\\/,g;
173 print "$sp-> qr/$re/\n";
174 return;
175 }
176
9bcb75ad 177 if ( $item_type eq 'HASH' ) {
178 # Hash ref or hash-based object.
179 my @sortKeys = sort keys(%$v) ;
d338d6fe 180 undef $more ;
181 $tHashDepth = $#sortKeys ;
182 $tHashDepth = $#sortKeys < $hashDepth-1 ? $#sortKeys : $hashDepth-1
183 unless $hashDepth eq '' ;
184 $more = "....\n" if $tHashDepth < $#sortKeys ;
185 $shortmore = "";
186 $shortmore = ", ..." if $tHashDepth < $#sortKeys ;
187 $#sortKeys = $tHashDepth ;
188 if ($compactDump && !grep(ref $_, values %{$v})) {
189 #$short = $sp .
190 # (join ', ',
191# Next row core dumps during require from DB on 5.000, even with map {"_"}
192 # map {&stringify($_) . " => " . &stringify($v->{$_})}
193 # @sortKeys) . "'$shortmore";
194 $short = $sp;
195 my @keys;
196 for (@sortKeys) {
197 push @keys, &stringify($_) . " => " . &stringify($v->{$_});
198 }
199 $short .= join ', ', @keys;
200 $short .= $shortmore;
201 (print "$short\n"), return if length $short <= $compactDump;
202 }
203 for $key (@sortKeys) {
204 return if $DB::signal;
205 $value = $ {$v}{$key} ;
206 print "$sp", &stringify($key), " => ";
d03c2a1b 207 DumpElem $value, $s, $m-1;
d338d6fe 208 }
209 print "$sp empty hash\n" unless @sortKeys;
210 print "$sp$more" if defined $more ;
9bcb75ad 211 } elsif ( $item_type eq 'ARRAY' ) {
212 # Array ref or array-based object. Also: undef.
213 # See how big the array is.
d338d6fe 214 $tArrayDepth = $#{$v} ;
215 undef $more ;
9bcb75ad 216 # Bigger than the max?
d338d6fe 217 $tArrayDepth = $#{$v} < $arrayDepth-1 ? $#{$v} : $arrayDepth-1
600d99fa 218 if defined $arrayDepth && $arrayDepth ne '';
9bcb75ad 219 # Yep. Don't show it all.
d338d6fe 220 $more = "....\n" if $tArrayDepth < $#{$v} ;
221 $shortmore = "";
222 $shortmore = " ..." if $tArrayDepth < $#{$v} ;
9bcb75ad 223
d338d6fe 224 if ($compactDump && !grep(ref $_, @{$v})) {
225 if ($#$v >= 0) {
54d04a52 226 $short = $sp . "0..$#{$v} " .
227 join(" ",
d9182636 228 map {exists $v->[$_] ? stringify $v->[$_] : "empty"} ($[..$tArrayDepth)
229 ) . "$shortmore";
d338d6fe 230 } else {
231 $short = $sp . "empty array";
232 }
233 (print "$short\n"), return if length $short <= $compactDump;
234 }
235 #if ($compactDump && $short = ShortArray($v)) {
236 # print "$short\n";
237 # return;
238 #}
239 for $num ($[ .. $tArrayDepth) {
240 return if $DB::signal;
241 print "$sp$num ";
d9182636 242 if (exists $v->[$num]) {
9bcb75ad 243 if (defined $v->[$num]) {
244 DumpElem $v->[$num], $s, $m-1;
245 }
246 else {
247 print "undef\n";
248 }
d9182636 249 } else {
250 print "empty slot\n";
251 }
d338d6fe 252 }
253 print "$sp empty array\n" unless @$v;
254 print "$sp$more" if defined $more ;
9bcb75ad 255 } elsif ( $item_type eq 'SCALAR' ) {
256 unless (defined $$v) {
257 print "$sp-> undef\n";
258 return;
259 }
d338d6fe 260 print "$sp-> ";
d03c2a1b 261 DumpElem $$v, $s, $m-1;
9bcb75ad 262 } elsif ( $item_type eq 'REF' ) {
263 print "$sp-> $$v\n";
264 return unless defined $$v;
265 unwrap($$v, $s+3, $m-1);
266 } elsif ( $item_type eq 'CODE' ) {
267 # Code object or reference.
54d04a52 268 print "$sp-> ";
269 dumpsub (0, $v);
9bcb75ad 270 } elsif ( $item_type eq 'GLOB' ) {
271 # Glob object or reference.
d338d6fe 272 print "$sp-> ",&stringify($$v,1),"\n";
273 if ($globPrint) {
274 $s += 3;
3a4b996c 275 dumpglob($s, "{$$v}", $$v, 1, $m-1);
d338d6fe 276 } elsif (defined ($fileno = fileno($v))) {
277 print( (' ' x ($s+3)) . "FileHandle({$$v}) => fileno($fileno)\n" );
278 }
279 } elsif (ref \$v eq 'GLOB') {
9bcb75ad 280 # Raw glob (again?)
d338d6fe 281 if ($globPrint) {
3a4b996c 282 dumpglob($s, "{$v}", $v, 1, $m-1) if $globPrint;
d338d6fe 283 } elsif (defined ($fileno = fileno(\$v))) {
284 print( (' ' x $s) . "FileHandle({$v}) => fileno($fileno)\n" );
285 }
286 }
287}
288
a7b657ee 289sub matchlex {
290 (my $var = $_[0]) =~ s/.//;
291 $var eq $_[1] or
292 ($_[1] =~ /^([!~])(.)([\x00-\xff]*)/) and
293 ($1 eq '!') ^ (eval { $var =~ /$2$3/ });
294}
295
d338d6fe 296sub matchvar {
297 $_[0] eq $_[1] or
b2391ea8 298 ($_[1] =~ /^([!~])(.)([\x00-\xff]*)/) and
299 ($1 eq '!') ^ (eval {($_[2] . "::" . $_[0]) =~ /$2$3/});
d338d6fe 300}
301
302sub compactDump {
303 $compactDump = shift if @_;
304 $compactDump = 6*80-1 if $compactDump and $compactDump < 2;
305 $compactDump;
306}
307
308sub veryCompact {
309 $veryCompact = shift if @_;
310 compactDump(1) if !$compactDump and $veryCompact;
311 $veryCompact;
312}
313
314sub unctrlSet {
315 if (@_) {
316 my $in = shift;
317 if ($in eq 'unctrl' or $in eq 'quote') {
318 $unctrl = $in;
319 } else {
320 print "Unknown value for `unctrl'.\n";
321 }
322 }
323 $unctrl;
324}
325
326sub quote {
327 if (@_ and $_[0] eq '"') {
328 $tick = '"';
329 $unctrl = 'quote';
54d04a52 330 } elsif (@_ and $_[0] eq 'auto') {
331 $tick = 'auto';
332 $unctrl = 'quote';
d338d6fe 333 } elsif (@_) { # Need to set
334 $tick = "'";
335 $unctrl = 'unctrl';
336 }
337 $tick;
338}
339
340sub dumpglob {
341 return if $DB::signal;
3a4b996c 342 my ($off,$key, $val, $all, $m) = @_;
d338d6fe 343 local(*entry) = $val;
344 my $fileno;
54d04a52 345 if (($key !~ /^_</ or $dumpDBFiles) and defined $entry) {
d338d6fe 346 print( (' ' x $off) . "\$", &unctrl($key), " = " );
3a4b996c 347 DumpElem $entry, 3+$off, $m;
d338d6fe 348 }
475342a6 349 if (($key !~ /^_</ or $dumpDBFiles) and @entry) {
d338d6fe 350 print( (' ' x $off) . "\@$key = (\n" );
3a4b996c 351 unwrap(\@entry,3+$off,$m) ;
d338d6fe 352 print( (' ' x $off) . ")\n" );
353 }
475342a6 354 if ($key ne "main::" && $key ne "DB::" && %entry
d338d6fe 355 && ($dumpPackages or $key !~ /::$/)
54d04a52 356 && ($key !~ /^_</ or $dumpDBFiles)
d338d6fe 357 && !($package eq "dumpvar" and $key eq "stab")) {
358 print( (' ' x $off) . "\%$key = (\n" );
3a4b996c 359 unwrap(\%entry,3+$off,$m) ;
d338d6fe 360 print( (' ' x $off) . ")\n" );
361 }
362 if (defined ($fileno = fileno(*entry))) {
363 print( (' ' x $off) . "FileHandle($key) => fileno($fileno)\n" );
364 }
365 if ($all) {
366 if (defined &entry) {
54d04a52 367 dumpsub($off, $key);
d338d6fe 368 }
369 }
370}
371
a7b657ee 372sub dumplex {
373 return if $DB::signal;
374 my ($key, $val, $m, @vars) = @_;
375 return if @vars && !grep( matchlex($key, $_), @vars );
376 local %address;
377 my $off = 0; # It reads better this way
378 my $fileno;
379 if (UNIVERSAL::isa($val,'ARRAY')) {
380 print( (' ' x $off) . "$key = (\n" );
381 unwrap($val,3+$off,$m) ;
382 print( (' ' x $off) . ")\n" );
383 }
384 elsif (UNIVERSAL::isa($val,'HASH')) {
385 print( (' ' x $off) . "$key = (\n" );
386 unwrap($val,3+$off,$m) ;
387 print( (' ' x $off) . ")\n" );
388 }
389 elsif (UNIVERSAL::isa($val,'IO')) {
390 print( (' ' x $off) . "FileHandle($key) => fileno($fileno)\n" );
391 }
392 # No lexical subroutines yet...
393 # elsif (UNIVERSAL::isa($val,'CODE')) {
394 # dumpsub($off, $$val);
395 # }
396 else {
397 print( (' ' x $off) . &unctrl($key), " = " );
398 DumpElem $$val, 3+$off, $m;
399 }
400}
401
83ee9e09 402sub CvGV_name_or_bust {
403 my $in = shift;
404 return if $skipCvGV; # Backdoor to avoid problems if XS broken...
405 $in = \&$in; # Hard reference...
406 eval {require Devel::Peek; 1} or return;
407 my $gv = Devel::Peek::CvGV($in) or return;
408 *$gv{PACKAGE} . '::' . *$gv{NAME};
409}
410
54d04a52 411sub dumpsub {
412 my ($off,$sub) = @_;
83ee9e09 413 my $ini = $sub;
414 my $s;
54d04a52 415 $sub = $1 if $sub =~ /^\{\*(.*)\}$/;
83ee9e09 416 my $subref = defined $1 ? \&$sub : \&$ini;
417 my $place = $DB::sub{$sub} || (($s = $subs{"$subref"}) && $DB::sub{$s})
418 || (($s = CvGV_name_or_bust($subref)) && $DB::sub{$s})
419 || ($subdump && ($s = findsubs("$subref")) && $DB::sub{$s});
54d04a52 420 $place = '???' unless defined $place;
83ee9e09 421 $s = $sub unless defined $s;
422 print( (' ' x $off) . "&$s in $place\n" );
54d04a52 423}
424
425sub findsubs {
475342a6 426 return undef unless %DB::sub;
54d04a52 427 my ($addr, $name, $loc);
428 while (($name, $loc) = each %DB::sub) {
429 $addr = \&$name;
430 $subs{"$addr"} = $name;
431 }
432 $subdump = 0;
433 $subs{ shift() };
434}
435
d338d6fe 436sub main::dumpvar {
3a4b996c 437 my ($package,$m,@vars) = @_;
b2391ea8 438 local(%address,$key,$val,$^W);
d338d6fe 439 $package .= "::" unless $package =~ /::$/;
440 *stab = *{"main::"};
441 while ($package =~ /(\w+?::)/g){
442 *stab = $ {stab}{$1};
443 }
444 local $TotalStrings = 0;
445 local $Strings = 0;
446 local $CompleteTotal = 0;
447 while (($key,$val) = each(%stab)) {
448 return if $DB::signal;
449 next if @vars && !grep( matchvar($key, $_), @vars );
450 if ($usageOnly) {
4c82ae22 451 globUsage(\$val, $key)
452 if ($package ne 'dumpvar' or $key ne 'stab')
453 and ref(\$val) eq 'GLOB';
d338d6fe 454 } else {
3a4b996c 455 dumpglob(0,$key, $val, 0, $m);
d338d6fe 456 }
457 }
458 if ($usageOnly) {
459 print "String space: $TotalStrings bytes in $Strings strings.\n";
460 $CompleteTotal += $TotalStrings;
461 print "Grand total = $CompleteTotal bytes (1 level deep) + overhead.\n";
462 }
463}
464
465sub scalarUsage {
466 my $size = length($_[0]);
467 $TotalStrings += $size;
468 $Strings++;
469 $size;
470}
471
472sub arrayUsage { # array ref, name
473 my $size = 0;
474 map {$size += scalarUsage($_)} @{$_[0]};
475 my $len = @{$_[0]};
476 print "\@$_[1] = $len item", ($len > 1 ? "s" : ""),
477 " (data: $size bytes)\n"
478 if defined $_[1];
479 $CompleteTotal += $size;
480 $size;
481}
482
483sub hashUsage { # hash ref, name
484 my @keys = keys %{$_[0]};
485 my @values = values %{$_[0]};
486 my $keys = arrayUsage \@keys;
487 my $values = arrayUsage \@values;
488 my $len = @keys;
489 my $total = $keys + $values;
490 print "\%$_[1] = $len item", ($len > 1 ? "s" : ""),
491 " (keys: $keys; values: $values; total: $total bytes)\n"
492 if defined $_[1];
493 $total;
494}
495
496sub globUsage { # glob ref, name
497 local *name = *{$_[0]};
498 $total = 0;
499 $total += scalarUsage $name if defined $name;
475342a6 500 $total += arrayUsage \@name, $_[1] if @name;
501 $total += hashUsage \%name, $_[1] if %name and $_[1] ne "main::"
d338d6fe 502 and $_[1] ne "DB::"; #and !($package eq "dumpvar" and $key eq "stab"));
503 $total;
504}
505
506sub packageUsage {
507 my ($package,@vars) = @_;
508 $package .= "::" unless $package =~ /::$/;
509 local *stab = *{"main::"};
510 while ($package =~ /(\w+?::)/g){
511 *stab = $ {stab}{$1};
512 }
513 local $TotalStrings = 0;
514 local $CompleteTotal = 0;
515 my ($key,$val);
516 while (($key,$val) = each(%stab)) {
517 next if @vars && !grep($key eq $_,@vars);
518 globUsage \$val, $key unless $package eq 'dumpvar' and $key eq 'stab';
519 }
520 print "String space: $TotalStrings.\n";
521 $CompleteTotal += $TotalStrings;
522 print "\nGrand total = $CompleteTotal bytes\n";
523}
524
5251;
526