| 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, 2016-2026 -- leonerd@leonerd.org.uk | ||||||||||
| 5 | |||||||||||
| 6 | package Devel::MAT::Tool::Identify 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::ToolBase::GraphWalker ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | 5 | ▶ | use utf8; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | |||||||||||
| 13 | 5 | ▶ | use feature qw( postderef signatures ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 14 | 5 | ▶ | no warnings qw( experimental::postderef experimental::signatures ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 15 | |||||||||||
| 16 | 5 | ▶ | use constant CMD => "identify"; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 17 | 5 | ▶ | use constant CMD_DESC => "Identify an SV by its referrers"; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 18 | |||||||||||
| 19 | =encoding UTF-8 | ||||||||||
| 20 | |||||||||||
| 21 | =head1 NAME | ||||||||||
| 22 | |||||||||||
| 23 | C<Devel::MAT::Tool::Identify> - identify an SV by its referrers | ||||||||||
| 24 | |||||||||||
| 25 | =head1 DESCRIPTION | ||||||||||
| 26 | |||||||||||
| 27 | This C<Devel::MAT> tool provides a command to identify an SV by walking up its | ||||||||||
| 28 | tree of inrefs, printing useful information that helps to identify what it is | ||||||||||
| 29 | by how it can be reached from well-known program roots. | ||||||||||
| 30 | |||||||||||
| 31 | =cut | ||||||||||
| 32 | |||||||||||
| 33 | =head1 COMMANDS | ||||||||||
| 34 | |||||||||||
| 35 | =for highlighter | ||||||||||
| 36 | |||||||||||
| 37 | =cut | ||||||||||
| 38 | |||||||||||
| 39 | =head2 identify | ||||||||||
| 40 | |||||||||||
| 41 | pmat> identify 0x1bbf640 | ||||||||||
| 42 | IO() at 0x1bbf640 is: | ||||||||||
| 43 | └─the io of GLOB(@*I) at 0x1bbf628, which is: | ||||||||||
| 44 | └─the ARGV GV | ||||||||||
| 45 | |||||||||||
| 46 | Prints a tree of the identification of the SV at the given address. | ||||||||||
| 47 | |||||||||||
| 48 | Takes the following named options: | ||||||||||
| 49 | |||||||||||
| 50 | =over 4 | ||||||||||
| 51 | |||||||||||
| 52 | =item --depth D, -d D | ||||||||||
| 53 | |||||||||||
| 54 | Limits the output to the given number of steps away from the given initial SV. | ||||||||||
| 55 | |||||||||||
| 56 | =item --weak | ||||||||||
| 57 | |||||||||||
| 58 | Include weak direct references in the output (by default only strong direct | ||||||||||
| 59 | ones will be included). | ||||||||||
| 60 | |||||||||||
| 61 | =item --all | ||||||||||
| 62 | |||||||||||
| 63 | Include both weak and indirect references in the output. | ||||||||||
| 64 | |||||||||||
| 65 | =item --no-elide, -n | ||||||||||
| 66 | |||||||||||
| 67 | Don't elide structure in the output. | ||||||||||
| 68 | |||||||||||
| 69 | By default, C<REF()>-type SVs will be skipped over, leading to a shorter | ||||||||||
| 70 | neater output by removing this usually-unnecessary noise. If this option is | ||||||||||
| 71 | not given, elided reference SVs will be notated by adding C<(via RV)> to the | ||||||||||
| 72 | reference description. | ||||||||||
| 73 | |||||||||||
| 74 | Additionally, members of the symbol table will be printed as being root SVs, | ||||||||||
| 75 | noting their symbol table name. This avoids additional nesting due to the | ||||||||||
| 76 | stashes and globs that make up the symbol table. This can also cause SVs to be | ||||||||||
| 77 | recognised as symbol table entries, when without it they might be cut off due | ||||||||||
| 78 | to the depth limit. | ||||||||||
| 79 | |||||||||||
| 80 | =back | ||||||||||
| 81 | |||||||||||
| 82 | =cut | ||||||||||
| 83 | |||||||||||
| 84 | 5 | ▶ | use constant CMD_OPTS => ( | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 85 | depth => { help => "maximum depth to recurse", | ||||||||||
| 86 | type => "i", | ||||||||||
| 87 | alias => "d", | ||||||||||
| 88 | default => 10 }, | ||||||||||
| 89 | weak => { help => "include weak references" }, | ||||||||||
| 90 | all => { help => "include weak and indirect references", | ||||||||||
| 91 | alias => "a" }, | ||||||||||
| 92 | no_elide => { help => "don't elide REF, PAD and symbol structures", | ||||||||||
| 93 | alias => "n" }, | ||||||||||
| 94 | 5 | ); | |||||||||
| 95 | |||||||||||
| 96 | 5 | ▶ | use constant CMD_ARGS_SV => 1; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 97 | |||||||||||
| 98 | 0 | ▶ | sub run ( $self, $optsref, $sv ) | ||||||||
|
Subroutine
run: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||
| 99 | { | ||||||||||
| 100 | 0 | my %opts = $optsref->%*; | |||||||||
| 101 | |||||||||||
| 102 | 0 | $self->reset; | |||||||||
| 103 | |||||||||||
| 104 | 0 | my $STRONG = 1; | |||||||||
| 105 | 0 | my $DIRECT = 1; | |||||||||
| 106 | 0 | my $ELIDE = !$opts{no_elide}; | |||||||||
| 107 | |||||||||||
| 108 | 0 | ▶ | $STRONG = 0 if $opts{weak}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 109 | 0 | ▶ | $STRONG = 0, $DIRECT = 0 if $opts{all}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 110 | |||||||||||
| 111 | 0 | $self->pmat->load_tool( "Inrefs", progress => $self->{progress} ); | |||||||||
| 112 | |||||||||||
| 113 | 0 | Devel::MAT::Cmd->printf( "%s is:\n", | |||||||||
| 114 | Devel::MAT::Cmd->format_sv( $sv ), | ||||||||||
| 115 | ); | ||||||||||
| 116 | |||||||||||
| 117 | $self->walk_graph( $self->pmat->inref_graph( $sv, | ||||||||||
| 118 | depth => $opts{depth}, | ||||||||||
| 119 | 0 | strong => $STRONG, | |||||||||
| 120 | direct => $DIRECT, | ||||||||||
| 121 | elide => $ELIDE, | ||||||||||
| 122 | ), "" ); | ||||||||||
| 123 | } | ||||||||||
| 124 | |||||||||||
| 125 | 7 | ▶ | sub _strength_label ( $strength ) | ||||||||
|
Subroutine
_strength_label: called CC=2 SCAR=7.6
| |||||||||||
| 126 | { | ||||||||||
| 127 | 7 | ▶ | $strength eq "strong" ? "" : | ||||||||
|
Branch: 1 branch
| |||||||||||
| 128 | Devel::MAT::Cmd->format_note( "[$strength]", 1 ) . " ", | ||||||||||
| 129 | } | ||||||||||
| 130 | |||||||||||
| 131 | 0 | ▶ | sub on_walk_nothing ( $, $node, $indent ) | ||||||||
|
Subroutine
on_walk_nothing: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 132 | { | ||||||||||
| 133 | 0 | Devel::MAT::Cmd->printf( "$indent└─not found\n" ); | |||||||||
| 134 | } | ||||||||||
| 135 | |||||||||||
| 136 | 0 | ▶ | sub on_walk_EDEPTH ( $, $node, $indent ) | ||||||||
|
Subroutine
on_walk_EDEPTH: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 137 | { | ||||||||||
| 138 | 0 | Devel::MAT::Cmd->printf( "$indent└─not found at this depth\n" ); | |||||||||
| 139 | } | ||||||||||
| 140 | |||||||||||
| 141 | 0 | ▶ | sub on_walk_again ( $, $node, $cyclic, $id, $indent ) | ||||||||
|
Subroutine
on_walk_again: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||
| 142 | { | ||||||||||
| 143 | 0 | Devel::MAT::Cmd->printf( "$indent└─already found " ); | |||||||||
| 144 | |||||||||||
| 145 | 0 | ▶ | Devel::MAT::Cmd->printf( "%s ", | ||||||||
|
Branch: 1 branch
| |||||||||||
| 146 | Devel::MAT::Cmd->format_note( "circularly" ) | ||||||||||
| 147 | ) if $cyclic; | ||||||||||
| 148 | |||||||||||
| 149 | 0 | ▶ | if( defined $id ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 150 | 0 | Devel::MAT::Cmd->printf( "as %s\n", | |||||||||
| 151 | Devel::MAT::Cmd->format_note( "*$id" ), | ||||||||||
| 152 | ); | ||||||||||
| 153 | } | ||||||||||
| 154 | else { | ||||||||||
| 155 | 0 | Devel::MAT::Cmd->printf( "%s\n", | |||||||||
| 156 | Devel::MAT::Cmd->format_note( "circularly" ), | ||||||||||
| 157 | ); | ||||||||||
| 158 | } | ||||||||||
| 159 | } | ||||||||||
| 160 | |||||||||||
| 161 | 3 | ▶ | sub on_walk_root ( $, $node, $root, $is_final, $indent ) | ||||||||
|
Subroutine
on_walk_root: called CC=2 SCAR=7.6
Pod
undocumented
| |||||||||||
| 162 | { | ||||||||||
| 163 | 3 | ▶ | Devel::MAT::Cmd->printf( $indent . ( $is_final ? "└─%s%s\n" : "├─%s%s\n" ), | ||||||||
|
Branch: 1 branch
| |||||||||||
| 164 | _strength_label( $root->strength ), $root->name, | ||||||||||
| 165 | ); | ||||||||||
| 166 | } | ||||||||||
| 167 | |||||||||||
| 168 | 4 | ▶ | sub on_walk_ref ( $, $node, $ref, $sv, $ref_id, $is_final, $indent ) | ||||||||
|
Subroutine
on_walk_ref: called CC=4 SCAR=14.0
Pod
undocumented
| |||||||||||
| 169 | { | ||||||||||
| 170 | 4 | ▶ | Devel::MAT::Cmd->printf( | ||||||||
|
Branch: 1 branch
| |||||||||||
| 171 | $indent . ( $is_final ? "└─" : "├─" ) ); | ||||||||||
| 172 | |||||||||||
| 173 | 4 | Devel::MAT::Cmd->printf( "%s%s of %s, which is", | |||||||||
| 174 | _strength_label( $ref->strength ), | ||||||||||
| 175 | $ref->name, | ||||||||||
| 176 | Devel::MAT::Cmd->format_sv( $sv ), | ||||||||||
| 177 | ); | ||||||||||
| 178 | |||||||||||
| 179 | 4 | ▶ | if( $ref_id ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 180 | 0 | Devel::MAT::Cmd->printf( " %s", | |||||||||
| 181 | Devel::MAT::Cmd->format_note( "(*$ref_id)" ), | ||||||||||
| 182 | ); | ||||||||||
| 183 | } | ||||||||||
| 184 | |||||||||||
| 185 | 4 | Devel::MAT::Cmd->printf( ":\n" ); | |||||||||
| 186 | |||||||||||
| 187 | # return recursion args: | ||||||||||
| 188 | 4 | ▶ | return ( $indent . ( $is_final ? " " : "│ " ) ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 189 | } | ||||||||||
| 190 | |||||||||||
| 191 | 0 | ▶ | sub on_walk_itself ( $, $node, $indent ) | ||||||||
|
Subroutine
on_walk_itself: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 192 | { | ||||||||||
| 193 | 0 | Devel::MAT::Cmd->printf( "${indent}itself\n" ); | |||||||||
| 194 | } | ||||||||||
| 195 | |||||||||||
| 196 | =head1 AUTHOR | ||||||||||
| 197 | |||||||||||
| 198 | Paul Evans <leonerd@leonerd.org.uk> | ||||||||||
| 199 | |||||||||||
| 200 | =cut | ||||||||||
| 201 | |||||||||||
| 202 | 5 | 0x55AA; | |||||||||