/usr/share/perl5/Data/Dump/Trace.pm is in libdata-dump-perl 1.23-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 | package Data::Dump::Trace;
$VERSION = "0.02";
# Todo:
# - prototypes
# in/out parameters key/value style
# - exception
# - wrap class
# - configurable colors
# - show call depth using indentation
# - show nested calls sensibly
# - time calls
use strict;
use base 'Exporter';
our @EXPORT_OK = qw(call mcall wrap autowrap trace);
use Carp qw(croak);
use overload ();
my %obj_name;
my %autowrap_class;
my %name_count;
sub autowrap {
while (@_) {
my $class = shift;
my $info = shift;
$info = { prefix => $info } unless ref($info);
for ($info->{prefix}) {
unless ($_) {
$_ = lc($class);
s/.*:://;
}
$_ = '$' . $_ unless /^\$/;
}
$autowrap_class{$class} = $info;
}
}
sub wrap {
my %arg = @_;
my $name = $arg{name} || "func";
my $func = $arg{func};
my $proto = $arg{proto};
return sub {
call($name, $func, $proto, @_);
} if $func;
if (my $obj = $arg{obj}) {
$name = '$' . $name unless $name =~ /^\$/;
$obj_name{overload::StrVal($obj)} = $name;
return bless {
name => $name,
obj => $obj,
proto => $arg{proto},
}, "Data::Dump::Trace::Wrapper";
}
croak("Either the 'func' or 'obj' option must be given");
}
sub trace {
my($symbol, $prototype) = @_;
no strict 'refs';
no warnings 'redefine';
*{$symbol} = wrap(name => $symbol, func => \&{$symbol}, proto => $prototype);
}
sub call {
my $name = shift;
my $func = shift;
my $proto = shift;
my $fmt = Data::Dump::Trace::Call->new($name, $proto, \@_);
if (!defined wantarray) {
$func->(@_);
return $fmt->return_void(\@_);
}
elsif (wantarray) {
return $fmt->return_list(\@_, $func->(@_));
}
else {
return $fmt->return_scalar(\@_, scalar $func->(@_));
}
}
sub mcall {
my $o = shift;
my $method = shift;
my $proto = shift;
return if $method eq "DESTROY" && !$o->can("DESTROY");
my $oname = ref($o) ? $obj_name{overload::StrVal($o)} || "\$o" : $o;
my $fmt = Data::Dump::Trace::Call->new("$oname->$method", $proto, \@_);
if (!defined wantarray) {
$o->$method(@_);
return $fmt->return_void(\@_);
}
elsif (wantarray) {
return $fmt->return_list(\@_, $o->$method(@_));
}
else {
return $fmt->return_scalar(\@_, scalar $o->$method(@_));
}
}
package Data::Dump::Trace::Wrapper;
sub AUTOLOAD {
my $self = shift;
our $AUTOLOAD;
my $method = substr($AUTOLOAD, rindex($AUTOLOAD, '::')+2);
Data::Dump::Trace::mcall($self->{obj}, $method, $self->{proto}{$method}, @_);
}
package Data::Dump::Trace::Call;
use Term::ANSIColor ();
use Data::Dump ();
*_dump = \&Data::Dump::dump;
our %COLOR = (
name => "yellow",
output => "cyan",
error => "red",
debug => "red",
);
%COLOR = () unless -t STDOUT;
sub _dumpav {
return "(" . _dump(@_) . ")" if @_ == 1;
return _dump(@_);
}
sub _dumpkv {
return _dumpav(@_) if @_ % 2;
my %h = @_;
my $str = _dump(\%h);
$str =~ s/^\{/(/ && $str =~ s/\}\z/)/;
return $str;
}
sub new {
my($class, $name, $proto, $input_args) = @_;
my $self = bless {
name => $name,
proto => $proto,
}, $class;
my $proto_arg = $self->proto_arg;
if ($proto_arg =~ /o/) {
for (@$input_args) {
push(@{$self->{input_av}}, _dump($_));
}
}
else {
$self->{input} = $proto_arg eq "%" ? _dumpkv(@$input_args) : _dumpav(@$input_args);
}
return $self;
}
sub proto_arg {
my $self = shift;
my($arg, $ret) = split(/\s*=\s*/, $self->{proto} || "");
$arg ||= '@';
return $arg;
}
sub proto_ret {
my $self = shift;
my($arg, $ret) = split(/\s*=\s*/, $self->{proto} || "");
$ret ||= '@';
return $ret;
}
sub color {
my($self, $category, $text) = @_;
return $text unless $COLOR{$category};
return Term::ANSIColor::colored($text, $COLOR{$category});
}
sub print_call {
my $self = shift;
my $outarg = shift;
print $self->color("name", "$self->{name}");
if (my $input = $self->{input}) {
$input = "" if $input eq "()" && $self->{name} =~ /->/;
print $self->color("input", $input);
}
else {
my $proto_arg = $self->proto_arg;
print "(";
my $i = 0;
for (@{$self->{input_av}}) {
print ", " if $i;
my $proto = substr($proto_arg, 0, 1, "");
if ($proto ne "o") {
print $self->color("input", $_);
}
if ($proto eq "o" || $proto eq "O") {
print " = " if $proto eq "O";
print $self->color("output", _dump($outarg->[$i]));
}
}
continue {
$i++;
}
print ")";
}
}
sub return_void {
my $self = shift;
my $arg = shift;
$self->print_call($arg);
print "\n";
return;
}
sub return_scalar {
my $self = shift;
my $arg = shift;
$self->print_call($arg);
my $s = shift;
my $name;
my $proto_ret = $self->proto_ret;
my $wrap = $autowrap_class{ref($s)};
if ($proto_ret =~ /^\$\w+\z/ && ref($s) && ref($s) !~ /^(?:ARRAY|HASH|CODE|GLOB)\z/) {
$name = $proto_ret;
}
else {
$name = $wrap->{prefix} if $wrap;
}
if ($name) {
$name .= $name_count{$name} if $name_count{$name}++;
print " = ", $self->color("output", $name), "\n";
$s = Data::Dump::Trace::wrap(name => $name, obj => $s, proto => $wrap->{proto});
}
else {
print " = ", $self->color("output", _dump($s));
if (!$s && $proto_ret =~ /!/ && $!) {
print " ", $self->color("error", errno($!));
}
print "\n";
}
return $s;
}
sub return_list {
my $self = shift;
my $arg = shift;
$self->print_call($arg);
print " = ", $self->color("output", $self->proto_ret eq "%" ? _dumpkv(@_) : _dumpav(@_)), "\n";
return @_;
}
sub errno {
my $t = "";
for (keys %!) {
if ($!{$_}) {
$t = $_;
last;
}
}
my $n = int($!);
return "$t($n) $!";
}
1;
__END__
=head1 NAME
Data::Dump::Trace - Helpers to trace function and method calls
=head1 SYNOPSIS
use Data::Dump::Trace qw(autowrap mcall);
autowrap("LWP::UserAgent" => "ua", "HTTP::Response" => "res");
use LWP::UserAgent;
$ua = mcall(LWP::UserAgent => "new"); # instead of LWP::UserAgent->new;
$ua->get("http://www.example.com")->dump;
=head1 DESCRIPTION
The following functions are provided:
=over
=item autowrap( $class )
=item autowrap( $class => $prefix )
=item autowrap( $class1 => $prefix1, $class2 => $prefix2, ... )
=item autowrap( $class1 => \%info1, $class2 => \%info2, ... )
Register classes whose objects are automatically wrapped when
returned by one of the call functions below. If $prefix is provided
it will be used as to name the objects.
Alternative is to pass an %info hash for each class. The recognized keys are:
=over
=item prefix => $string
The prefix string used to name objects of this type.
=item proto => \%hash
A hash of prototypes to use for the methods when an object is wrapped.
=back
=item wrap( name => $str, func => \&func, proto => $proto )
=item wrap( name => $str, obj => $obj, proto => \%hash )
Returns a wrapped function or object. When a wrapped function is
invoked then a trace is printed after the underlying function has returned.
When a method on a wrapped object is invoked then a trace is printed
after the methods on the underlying objects has returned.
See L</"Prototypes"> for description of the C<proto> argument.
=item call( $name, \&func, $proto, @ARGS )
Calls the given function with the given arguments. The trace will use
$name as the name of the function.
See L</"Prototypes"> for description of the $proto argument.
=item mcall( $class, $method, $proto, @ARGS )
=item mcall( $object, $method, $proto, @ARGS )
Calls the given method with the given arguments.
See L</"Prototypes"> for description of the $proto argument.
=item trace( $symbol, $prototype )
Replaces the function given by $symbol with a wrapped function.
=back
=head2 Prototypes
B<Note: The prototype string syntax described here is experimental and
likely to change in revisions of this interface>.
The $proto argument to call() and mcall() can optionally provide a
prototype for the function call. This give the tracer hints about how
to best format the argument lists and if there are I<in/out> or I<out>
arguments. The general form for the prototype string is:
<arguments> = <return_value>
The default prototype is "@ = @"; list of values as input and list of
values as output.
The value '%' can be used for both arguments and return value to say
that key/value pair style lists are used.
Alternatively, individual positional arguments can be listed each
represented by a letter:
=over
=item C<i>
input argument
=item C<o>
output argument
=item C<O>
both input and output argument
=back
If the return value prototype has C<!> appended, then it signals that
this function sets errno ($!) when it returns a false value. The
trace will display the current value of errno in that case.
If the return value prototype looks like a variable name (with C<$>
prefix), and the function returns a blessed object, then the variable
name will be used as prefix and the returned object automatically
traced.
=head1 SEE ALSO
L<Data::Dump>
=head1 AUTHOR
Copyright 2009 Gisle Aas.
This library is free software; you can redistribute it and/or
modify it under the same terms as Perl itself.
=cut
|