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 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732
|
# You may distribute under the terms of either the GNU General Public License
# or the Artistic License (the same terms as Perl itself)
#
# (C) Paul Evans, 2013-2024 -- leonerd@leonerd.org.uk
package Devel::MAT 0.53;
use v5.14;
use warnings;
use Carp;
use List::Util qw( first pairs );
use List::UtilsBy qw( sort_by );
use Syntax::Keyword::Match;
use Devel::MAT::Dumpfile;
use Devel::MAT::Graph;
use Devel::MAT::InternalTools;
use Module::Pluggable
sub_name => "_available_tools",
search_path => [ "Devel::MAT::Tool" ],
require => 1;
require XSLoader;
XSLoader::load( __PACKAGE__, our $VERSION );
=head1 NAME
C<Devel::MAT> - Perl Memory Analysis Tool
=head1 USER GUIDE
B<NEW USERS:>
If you are new to the C<Devel::MAT> set of tools, this is probably not the
document you want to start with. If you are interested in using C<Devel::MAT>
to help diagnose memory-related problems in a F<perl> program you instead want
to read the user guide, at L<Devel::MAT::UserGuide>.
If you are writing tooling modules to extend the abilities of C<Devel::MAT>
then this may indeed by the document for you; read on...
=head1 DESCRIPTION
A C<Devel::MAT> instance loads a heapdump file, and provides a container to
store analysis tools to work on it. Tools may be provided that conform to the
L<Devel::MAT::Tool> API, which can help analyse the data and interact with the
explorer user interface by using the methods in the L<Devel::MAT::UI> package.
=head2 File Format
The dump file format is still under development, so at present no guarantees
are made on whether files can be loaded over mismatching versions of
C<Devel::MAT>. However, as of version 0.11 the format should be more
extensible, allowing new SV fields to be added without breaking loading - older
tools will ignore new fields and newer tools will just load undef for fields
absent in older files. As the distribution approaches maturity the format will
be made more stable.
=cut
=head1 CONSTRUCTOR
=cut
=head2 load
$pmat = Devel::MAT->load( $path, %args );
Loads a heap dump file from the given path, and returns a new C<Devel::MAT>
instance wrapping it.
=cut
sub load
{
my $class = shift;
my $df = Devel::MAT::Dumpfile->load( @_ );
return bless {
df => $df,
}, $class;
}
=head1 METHODS
=cut
=head2 dumpfile
$df = $pmat->dumpfile;
Returns the underlying L<Devel::MAT::Dumpfile> instance backing this analysis
object.
=cut
sub dumpfile
{
my $self = shift;
return $self->{df};
}
=head2 available_tools
@tools = $pmat->available_tools;
Lists the L<Devel::MAT::Tool> classes that are installed and available.
=cut
{
my @TOOLS;
my $TOOLS_LOADED;
sub available_tools
{
my $self = shift;
return @TOOLS if $TOOLS_LOADED;
$TOOLS_LOADED++;
@TOOLS = map { $_ =~ s/^Devel::MAT::Tool:://; $_ } $self->_available_tools;
foreach my $name ( @TOOLS ) {
my $tool_class = "Devel::MAT::Tool::$name";
next unless $tool_class->can( "AUTOLOAD_TOOL" ) and $tool_class->AUTOLOAD_TOOL( $self );
$self->{tools}{$name} ||= $tool_class->new( $self );
}
return @TOOLS;
}
}
=head2 load_tool
$tool = $pmat->load_tool( $name );
Loads the named L<Devel::MAT::Tool> class.
=cut
sub load_tool
{
my $self = shift;
my ( $name, %args ) = @_;
# Ensure tools are 'require'd
$self->available_tools;
my $tool_class = "Devel::MAT::Tool::$name";
return $self->{tools}{$name} ||= $tool_class->new( $self, %args );
}
sub load_tool_for_command
{
my $self = shift;
my ( $cmd, %args ) = @_;
return $self->{tools_by_command}{$cmd} ||= do {
my $name = first {
my $class = "Devel::MAT::Tool::$_";
$class->can( "CMD" ) and $class->CMD eq $cmd
} $self->available_tools or die "Unrecognised command '$cmd'\n";
$self->load_tool( $name, %args );
};
}
=head2 has_tool
$bool = $pmat->has_tool( $name );
Returns true if the named tool is already loaded.
=cut
sub has_tool
{
my $self = shift;
my ( $name ) = @_;
return defined $self->{tools}{$name};
}
=head2 run_command
$pmat->run_command( $inv );
Runs a tool command given by the L<Commandable::Invocation> instance.
=cut
sub run_command
{
my $self = shift;
my ( $inv, %args ) = @_;
my $cmd = $inv->pull_token;
$self->load_tool_for_command( $cmd,
progress => $args{process},
)->run_cmd( $inv );
}
=head2 inref_graph
$node = $pmat->inref_graph( $sv, %opts );
Traces the tree of inrefs from C<$sv> back towards the known roots, returning
a L<Devel::MAT::Graph> node object representing it, within a graph of reverse
references back to the known roots.
This method will load L<Devel::MAT::Tool::Inrefs> if it isn't yet loaded.
The following named options are recognised:
=over 4
=item depth => INT
If specified, stop recursing after the specified count. A depth of 1 will only
include immediately referring SVs, 2 will print the referrers of those, etc.
Nodes with inrefs that were trimmed because of this limit will appear to be
roots with a special name of C<EDEPTH>.
=item strong => BOOL
=item direct => BOOL
Specifies the type of inrefs followed. By default all inrefs are followed.
Passing C<strong> will follow only strong direct inrefs. Passing C<direct>
will follow only direct inrefs.
=item elide => BOOL
If true, attempt to neaten up the output by skipping over certain structures.
C<REF()>-type SVs will be skipped to their referrant.
Members of the symbol table will be printed as being a 'root' element of the
given symbol name.
C<PAD>s and C<PADLIST>s will be skipped to their referring C<CODE>, giving
shorter output for lexical variables.
=back
=cut
sub inref_graph
{
my $self = shift;
my ( $sv, %opts ) = @_;
my $graph = $opts{graph} //= Devel::MAT::Graph->new( $self->dumpfile );
# TODO: allow separate values for these
my $elide_rv = $opts{elide};
my $elide_sym = $opts{elide};
my $elide_pad = $opts{elide};
$self->load_tool( "Inrefs" );
if( $sv->immortal ) {
my $desc = $sv->type eq "UNDEF" ? "undef" :
$sv->uv ? "true" :
"false";
$graph->add_root( $sv,
Devel::MAT::SV::Reference( $desc, strong => undef ) );
return $graph->get_sv_node( $sv );
}
my $name;
my $foundsv;
if( $elide_sym and $name = $sv->symname and
$name !~ m/^&.*::__ANON__$/ and
$foundsv = eval { $self->find_symbol( $sv->symname ) } and
$foundsv->addr == $sv->addr
) {
$graph->add_root( $sv,
Devel::MAT::SV::Reference( "the symbol '" . Devel::MAT::Cmd->format_symbol( $name, $sv ) . "'", strong => undef ) );
return $graph->get_sv_node( $sv );
}
if( $elide_sym and $sv->type eq "GLOB" and $name = $sv->stashname ) {
$graph->add_root( $sv,
Devel::MAT::SV::Reference( "the glob '" . Devel::MAT::Cmd->format_symbol( "*$name", $sv ) . '"', strong => undef ) );
return $graph->get_sv_node( $sv );
}
$graph->add_sv( $sv );
my @inrefs = $opts{strong} ? $sv->inrefs_strong :
$opts{direct} ? $sv->inrefs_direct :
$sv->inrefs;
# If we didn't find anything at the given option level, try harder
if( !@inrefs and $opts{strong} ) {
@inrefs = $sv->inrefs_direct;
}
if( !@inrefs and $opts{direct} ) {
@inrefs = $sv->inrefs;
}
if( $elide_rv ) {
@inrefs = map { sub {
return $_ unless $_->sv and
$_->sv->type eq "REF" and
$_->name eq "the referrant";
my $rv = $_->sv;
my @rvrefs = $opts{strong} ? $rv->inrefs_strong :
$opts{direct} ? $rv->inrefs_direct :
$rv->inrefs;
return $_ unless @rvrefs == 1;
# Add 'via RV' marker
return map {
Devel::MAT::SV::Reference( Devel::MAT::Cmd->format_note( "(via RV)" ) . " " . $_->name,
$_->strength, $_->sv )
} @rvrefs;
}->() } @inrefs;
}
if( $elide_pad ) {
@inrefs = map { sub {
return $_ unless $_->sv and
$_->sv->type eq "PAD";
my $pad = $_->sv;
my $cv = $pad->padcv;
# Even if the CV isn't active, this might be a state variable so we
# must always consider pad(1) at least.
my ( $depth ) = grep { $cv->pad( $_ ) == $pad } ( 1 .. ( $cv->depth || 1 ) );
return Devel::MAT::SV::Reference( $_->name . " at depth $depth", $_->strength, $cv );
}->() } @inrefs;
}
if( $sv->is_mortal ) {
$graph->add_root( $sv,
Devel::MAT::SV::Reference( "a mortal", strong => undef ) );
}
foreach my $ref ( sort_by { $_->name } @inrefs ) {
if( !defined $ref->sv ) {
$graph->add_root( $sv, $ref );
next;
}
if( defined $opts{depth} and not $opts{depth} ) {
$graph->add_root( $sv, "EDEPTH" );
last;
}
my @me;
if( $graph->has_sv( $ref->sv ) ) {
$graph->add_ref( $ref->sv, $sv, $ref );
# Don't recurse into it as it was already found
}
else {
$graph->add_sv( $ref->sv ); # add first to stop inf. loops
defined $opts{depth} ? $self->inref_graph( $ref->sv, %opts, depth => $opts{depth}-1 )
: $self->inref_graph( $ref->sv, %opts );
$graph->add_ref( $ref->sv, $sv, $ref );
}
}
return $graph->get_sv_node( $sv );
}
=head2 find_symbol
$sv = $pmat->find_symbol( $name );
Attempts to walk the symbol table looking for a symbol of the given name,
which must include the sigil.
$Package::Name::symbol_name => to return a SCALAR SV
@Package::Name::symbol_name => to return an ARRAY SV
%Package::Name::symbol_name => to return a HASH SV
&Package::Name::symbol_name => to return a CODE SV
=cut
sub find_symbol
{
my $self = shift;
my ( $name ) = @_;
my ( $sigil, $globname ) = $name =~ m/^([\$\@%&])(.*)$/ or
croak "Could not parse sigil from $name";
my $stashvalue = $self->find_stashvalue( $globname );
# Perl 5.22 may take CODE shortcuts
if( $sigil eq '&' and $stashvalue->type eq "REF" ) {
return $stashvalue->rv;
}
$stashvalue->type eq "GLOB" or
croak "$globname is not a GLOB";
my $slot = ( $sigil eq '$' ) ? "scalar" :
( $sigil eq '@' ) ? "array" :
( $sigil eq '%' ) ? "hash" :
( $sigil eq '&' ) ? "code" :
die "ARGH"; # won't happen
my $sv = $stashvalue->$slot or
croak "\*$globname has no $slot slot";
return $sv;
}
=head2 find_glob
$gv = $pmat->find_glob( $name );
Attempts to walk the symbol table looking for a symbol of the given name,
returning the C<GLOB> object if found.
=head2 find_stash
$stash = $pmat->find_stash( $name );
Attempts to walk the symbol table looking for a stash of the given name.
=cut
sub find_stashvalue
{
my $self = shift;
my ( $name ) = @_;
my ( $parent, $shortname ) = $name =~ m/^(?:(.*)::)?(.+?)$/;
my $stash;
if( defined $parent and length $parent ) {
$stash = $self->find_stash( $parent );
}
else {
$stash = $self->dumpfile->defstash;
}
my $sv = $stash->value( $shortname ) or
croak $stash->stashname . " has no symbol $shortname";
return $sv;
}
sub find_glob
{
my $self = shift;
my ( $name ) = @_;
my $sv = $self->find_stashvalue( $name ) or return;
$sv->type eq "GLOB" or
croak "$name is not a GLOB";
return $sv;
}
sub find_stash
{
my $self = shift;
my ( $name ) = @_;
my $gv = $self->find_glob( $name . "::" );
return $gv->hash ||
croak "$name has no hash";
}
# Some base implementations of Devel::MAT::Cmd formatters
push @Devel::MAT::Cmd::ISA, qw( Devel::MAT::Cmd::_base );
package
Devel::MAT::Cmd::_base;
use B qw( perlstring );
use List::Util qw( max );
sub print_table
{
my $self = shift;
my ( $rows, %opts ) = @_;
if( $opts{headings} ) {
my @headings = map { $self->format_heading( $_ ) } @{ $opts{headings} };
$rows = [ \@headings, @$rows ];
}
return unless @$rows;
my $cols = max map { scalar @$_ } @$rows;
my @colwidths = map {
my $colidx = $_;
# TODO: consider a unicode/terminal-aware version of length here
max map { length($_->[$colidx]) // 0 } @$rows;
} 0 .. $cols-1;
my $align = $opts{align} // "";
$align = [ ( $align ) x $cols ] if !ref $align;
my $sep = $opts{sep} // " ";
$sep = [ ( $sep ) x ($cols - 1) ] if !ref $sep;
my @leftalign = map { ($align->[$_]//"") ne "right" } 0 .. $cols-1;
my $format = join( "",
( " " x ( $opts{indent} // 0 ) ),
( map {
my $col = $_;
my $width = $colwidths[$col];
my $flags = $leftalign[$col] ? "-" : "";
# If final column should be left-aligned don't bother with width
$width = "" if $col == $cols-1 and $leftalign[$col];
( $col ? $sep->[$col-1] : "" ) . "%${flags}${width}s"
} 0 .. $cols-1 ),
) . "\n";
foreach my $row ( @$rows ) {
my @row = @$row;
@row or @row = map { "-"x$colwidths[$_] } ( 0 .. $cols-1 );
push @row, "" while @row < $cols; # pad with spaces
$self->printf( $format, @row );
}
}
sub format_note
{
shift;
my ( $str, $idx ) = @_;
return $str;
}
sub _format_sv
{
shift;
my ( $ret ) = @_;
return $ret;
}
sub _format_addr
{
shift;
my ( $addr ) = @_;
return sprintf "%#x", $addr;
}
sub format_sv
{
shift;
my ( $sv ) = @_;
my $ret = $sv->desc;
if( my $blessed = $sv->blessed ) {
$ret .= "=" . Devel::MAT::Cmd->format_symbol( $blessed->stashname, $blessed );
}
$ret .= " at " . Devel::MAT::Cmd->_format_addr( $sv->addr );
if( my $rootname = $sv->rootname ) {
$ret .= "=" . Devel::MAT::Cmd->format_note( $rootname, 1 );
}
return Devel::MAT::Cmd->_format_sv( $ret, $sv );
}
sub _format_value
{
shift;
my ( $val ) = @_;
return $val;
}
sub format_value
{
shift;
my ( $val, %opts ) = @_;
my $text;
if( $opts{key} ) {
my $strval = $val;
if( $opts{stash} && $strval =~ m/^([\x00-\x1f])([a-zA-Z0-9_]*)$/ ) {
$strval = "^" . chr( 64 + ord $1 ) . $2;
}
elsif( $strval !~ m/^[a-zA-Z_][a-zA-Z0-9_]*$/ ) {
$strval = perlstring( $val );
}
return "{" . Devel::MAT::Cmd->_format_value( $strval ) . "}";
}
elsif( $opts{index} ) {
return "[" . Devel::MAT::Cmd->_format_value( $val+0 ) . "]";
}
elsif( $opts{addr} ) {
return Devel::MAT::Cmd->_format_addr( $val );
}
elsif( $opts{pv} ) {
my $truncated;
if( my $maxlen = $opts{maxlen} // 64 ) {
( $truncated = length $val > $maxlen ) and
substr( $val, $maxlen ) = "";
}
return Devel::MAT::Cmd->_format_value(
perlstring( $val ) . ( $truncated ? "..." : "" )
);
}
else {
return Devel::MAT::Cmd->_format_value( $val );
}
}
sub format_symbol
{
shift;
my ( $name ) = @_;
return $name;
}
sub format_bytes
{
shift;
my ( $bytes ) = @_;
if( $bytes < 1024 ) {
return sprintf "%d bytes", $bytes;
}
if( $bytes < 1024**2 ) {
return sprintf "%.1f KiB", $bytes / 1024;
}
if( $bytes < 1024**3 ) {
return sprintf "%.1f MiB", $bytes / 1024**2;
}
if( $bytes < 1024**4 ) {
return sprintf "%.1f GiB", $bytes / 1024**3;
}
return sprintf "%.1f TiB", $bytes / 1024**4;
}
sub format_sv_with_value
{
my $self = shift;
my ( $sv ) = @_;
my $repr = $self->format_sv( $sv );
match( $sv->type : eq ) {
case( "SCALAR" ) {
my @reprs;
my $num;
defined( $num = $sv->nv // $sv->uv ) and
push @reprs, $self->format_value( $num, nv => 1 );
defined $sv->pv and
push @reprs, $self->format_value( $sv->pv, pv => 1 );
# Dualvars
return "$repr = $reprs[0] / $reprs[1]" if @reprs > 1;
return "$repr = $reprs[0]" if @reprs;
}
case( "BOOL" ) {
return "$repr = " . $self->format_value( $sv->uv ? "true" : "false" );
}
case( "REF" ) {
#return "REF => NULL" if !$sv->rv;
return "$repr => " . $self->format_sv_with_value( $sv->rv ) if $sv->rv;
}
case( "ARRAY" ) {
return $repr if $sv->blessed;
my $n_elems = $sv->elems;
return "$repr = []" if !$n_elems;
my $elem = $self->format_sv( $sv->elem( 0 ) );
$elem .= ", ..." if $n_elems > 1;
return "$repr = [$elem]";
}
case( "HASH" ) {
return $repr if $sv->blessed;
my $n_values = $sv->values;
return "$repr = {}" if !$n_values;
my $key = ( $sv->keys )[0]; # pick one at random
my $value = $self->format_value( $key, key => 1 ) . " => " . $self->format_sv( $sv->value( $key ) );
$value .= ", ..." if $n_values > 1;
return "$repr = {$value}";
}
case( "GLOB" ) {
return "$repr is " . $self->format_symbol( "*" . $sv->stashname, $sv );
}
case( "STASH" ) {
return "$repr is " . $self->format_symbol( $sv->stashname, $sv );
}
}
return $repr;
}
sub format_heading
{
shift;
my ( $text, $level ) = @_;
return "$text";
}
=head1 AUTHOR
Paul Evans <leonerd@leonerd.org.uk>
=cut
0x55AA;
|