| 1 | # You may distribute under the terms of either the GNU General Public License | ||||||||||||||||||||
| 2 | # or the Artistic License (the same terms as Perl itself) | ||||||||||||||||||||
| 3 | # | ||||||||||||||||||||
| 4 | # (C) Paul Evans, 2022-2026 -- leonerd@leonerd.org.uk | ||||||||||||||||||||
| 5 | |||||||||||||||||||||
| 6 | package Devel::MAT::Tool::ListDanglingPtrs 0.56; | ||||||||||||||||||||
| 7 | |||||||||||||||||||||
| 8 | 5 | ▶ | use v5.20; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | 5 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | 5 | ▶ | use base qw( Devel::MAT::Tool ); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 11 | |||||||||||||||||||||
| 12 | 5 | ▶ | use feature qw( postderef signatures ); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 13 | 5 | ▶ | no warnings qw( experimental::postderef experimental::signatures ); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 14 | |||||||||||||||||||||
| 15 | 5 | ▶ | use List::Util qw( pairs ); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 16 | |||||||||||||||||||||
| 17 | 5 | ▶ | use constant CMD => "list-dangling-ptrs"; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 18 | 5 | ▶ | use constant CMD_DESC => "Show pointers in SVs that don't lead anywhere"; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 19 | |||||||||||||||||||||
| 20 | =head1 NAME | ||||||||||||||||||||
| 21 | |||||||||||||||||||||
| 22 | C<Devel::MAT::Tool::ListDanglingPtrs> - display a list of SV pointer fields that do not point at known SVs | ||||||||||||||||||||
| 23 | |||||||||||||||||||||
| 24 | =head1 DESCRIPTION | ||||||||||||||||||||
| 25 | |||||||||||||||||||||
| 26 | This C<Devel::MAT> tool displays a list of fields from known SVs containing | ||||||||||||||||||||
| 27 | non-NULL addresses, but which do not point to other known SVs. These are so-called | ||||||||||||||||||||
| 28 | "dangling pointers". | ||||||||||||||||||||
| 29 | |||||||||||||||||||||
| 30 | =cut | ||||||||||||||||||||
| 31 | |||||||||||||||||||||
| 32 | =head1 COMMANDS | ||||||||||||||||||||
| 33 | |||||||||||||||||||||
| 34 | =for highlighter | ||||||||||||||||||||
| 35 | |||||||||||||||||||||
| 36 | =head2 list-dangling-ptrs | ||||||||||||||||||||
| 37 | |||||||||||||||||||||
| 38 | pmat> list-dangling-ptrs | ||||||||||||||||||||
| 39 | CODE(proto) at 0x55b9d83ae3d8 has no constval SV at addr 0x55b9d83963f0 | ||||||||||||||||||||
| 40 | ... | ||||||||||||||||||||
| 41 | |||||||||||||||||||||
| 42 | Prints a list of fields in SVs which do not point at other valid SVs. | ||||||||||||||||||||
| 43 | |||||||||||||||||||||
| 44 | =cut | ||||||||||||||||||||
| 45 | |||||||||||||||||||||
| 46 | 5 | my %methodcache; | |||||||||||||||||||
| 47 | 0 | ▶ | sub methods_of ( $pkg ) | ||||||||||||||||||
|
Subroutine
methods_of: not called CC=2 SCAR=14.0
Pod
undocumented
| |||||||||||||||||||||
| 48 | { | ||||||||||||||||||||
| 49 | 0 | ▶ | my $methods = $methodcache{$pkg} //= do { | ||||||||||||||||||
|
Condition0%0/3
$methodcache{$pkg} //= do {
no strict 'refs';
my(@syms) = keys %{"${pkg}::";};
[grep({*{"${pkg}::$_";}{"CODE"};} @syms), map({methods_of($_);} @{"${pkg}::ISA";})]
}
MC/DC0%0/2
$methodcache{$pkg} //= do {
no strict 'refs';
my(@syms) = keys %{"${pkg}::";};
[grep({*{"${pkg}::$_";}{"CODE"};} @syms), map({methods_of($_);} @{"${pkg}::ISA";})]
}
$methodcache{$pkg}
do {
no strict 'refs';
my(@syms) = keys %{"${pkg}::";};
[grep({*{"${pkg}::$_";}{"CODE"};} @syms), map({methods_of($_);} @{"${pkg}::ISA";})]
}
Truth table0%0/3
A //= B
A= $methodcache{$pkg}B= do {
no strict 'refs';
my(@syms) = keys %{"${pkg}::";};
[grep({*{"${pkg}::$_";}{"CODE"};} @syms), map({methods_of($_);} @{"${pkg}::ISA";})]
}
| |||||||||||||||||||||
| 50 | 5 | ▶ | no strict 'refs'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 51 | 0 | my @syms = keys %{"${pkg}::"}; | |||||||||||||||||||
| 52 | [ | ||||||||||||||||||||
| 53 | 0 | ( grep { *{"${pkg}::$_"}{CODE} } @syms ), | |||||||||||||||||||
| 54 | 0 | map { methods_of( $_ ) } @{"${pkg}::ISA"} | |||||||||||||||||||
| 55 | ] | ||||||||||||||||||||
| 56 | }; | ||||||||||||||||||||
| 57 | 0 | return @$methods; | |||||||||||||||||||
| 58 | } | ||||||||||||||||||||
| 59 | |||||||||||||||||||||
| 60 | 0 | ▶ | sub run ( $self ) | ||||||||||||||||||
|
Subroutine
run: not called CC=13 SCAR=52.0
Pod
undocumented
| |||||||||||||||||||||
| 61 | { | ||||||||||||||||||||
| 62 | 0 | my $df = $self->df; | |||||||||||||||||||
| 63 | |||||||||||||||||||||
| 64 | 0 | my %roots_at; | |||||||||||||||||||
| 65 | 0 | foreach ( pairs $df->roots ) { | |||||||||||||||||||
| 66 | 0 | my ( $name, $sv ) = @$_; | |||||||||||||||||||
| 67 | $sv and | ||||||||||||||||||||
| 68 | 0 | ▶ | $roots_at{ $sv->addr } = $name; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 69 | } | ||||||||||||||||||||
| 70 | |||||||||||||||||||||
| 71 | 0 | ▶ | my $test_ptr = sub ( $self, $sv, $name, $addr = undef ) { | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=5 SCAR=34.0
| |||||||||||||||||||||
| 72 | 0 | ▶ | $addr or return; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 73 | 0 | ▶ | $roots_at{$addr} and return; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 74 | 0 | ▶ | $df->{heap}{$addr} and return; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 75 | |||||||||||||||||||||
| 76 | 0 | Devel::MAT::Cmd->printf( "%s has no %s SV at addr 0x%x\n", | |||||||||||||||||||
| 77 | Devel::MAT::Cmd->format_sv( $sv ), | ||||||||||||||||||||
| 78 | $name, | ||||||||||||||||||||
| 79 | $addr, | ||||||||||||||||||||
| 80 | ); | ||||||||||||||||||||
| 81 | 0 | }; | |||||||||||||||||||
| 82 | |||||||||||||||||||||
| 83 | 0 | foreach my $sv ( $self->df->heap ) { | |||||||||||||||||||
| 84 | # Quite a bit of cheating here. We'll presume that any _at method gives | ||||||||||||||||||||
| 85 | # a number that should be a raw SV pointer address | ||||||||||||||||||||
| 86 | 0 | foreach my $meth ( methods_of ref $sv ) { | |||||||||||||||||||
| 87 | 0 | ▶ | if( $meth eq "field" ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 88 | # Struct fields might or mightnot be SV pointers. We'll have to ask them | ||||||||||||||||||||
| 89 | 0 | my $fields = $sv->structtype->fields; | |||||||||||||||||||
| 90 | 0 | foreach my $idx ( 0 .. $#$fields ) { | |||||||||||||||||||
| 91 | 0 | my $field = $fields->[$idx]; | |||||||||||||||||||
| 92 | 0 | ▶ | if( $field->type == 0 ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 93 | 0 | $self->$test_ptr( $sv, "field <${\$field->name}>", $sv->$meth( $idx ) ); | |||||||||||||||||||
| 94 | } | ||||||||||||||||||||
| 95 | } | ||||||||||||||||||||
| 96 | } | ||||||||||||||||||||
| 97 | |||||||||||||||||||||
| 98 | 0 | ▶ | next unless $meth =~ m/^([^_].*)_at$/; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 99 | 0 | my $outref = $1; | |||||||||||||||||||
| 100 | |||||||||||||||||||||
| 101 | 0 | ▶ | if( $outref eq "elem" ) { | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 102 | 0 | $self->$test_ptr( $sv, "$outref [$_]", $sv->$meth( $_ ) ) for 0 .. $sv->elems-1; | |||||||||||||||||||
| 103 | } | ||||||||||||||||||||
| 104 | elsif( $outref eq "value" ) { | ||||||||||||||||||||
| 105 | 0 | $self->$test_ptr( $sv, "$outref {$_}", $sv->$meth( $_ ) ) for $sv->keys; | |||||||||||||||||||
| 106 | } | ||||||||||||||||||||
| 107 | else { | ||||||||||||||||||||
| 108 | 0 | $self->$test_ptr( $sv, $outref, $sv->$meth ); | |||||||||||||||||||
| 109 | } | ||||||||||||||||||||
| 110 | } | ||||||||||||||||||||
| 111 | } | ||||||||||||||||||||
| 112 | } | ||||||||||||||||||||
| 113 | |||||||||||||||||||||
| 114 | =head1 AUTHOR | ||||||||||||||||||||
| 115 | |||||||||||||||||||||
| 116 | Paul Evans <leonerd@leonerd.org.uk> | ||||||||||||||||||||
| 117 | |||||||||||||||||||||
| 118 | =cut | ||||||||||||||||||||
| 119 | |||||||||||||||||||||
| 120 | 5 | 0x55AA; | |||||||||||||||||||