| 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, 2017-2026 -- leonerd@leonerd.org.uk | ||||||||||||||||||
| 5 | |||||||||||||||||||
| 6 | package Devel::MAT::Tool::Callers 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 constant CMD => "callers"; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 16 | 5 | ▶ | use constant CMD_DESC => "Display the caller stack"; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 17 | |||||||||||||||||||
| 18 | 5 | ▶ | use constant CMD_OPTS => ( | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 19 | pad => { help => "show PAD contents", | ||||||||||||||||||
| 20 | alias => "P" }, | ||||||||||||||||||
| 21 | 5 | ); | |||||||||||||||||
| 22 | |||||||||||||||||||
| 23 | =head1 NAME | ||||||||||||||||||
| 24 | |||||||||||||||||||
| 25 | C<Devel::MAT::Tool::Callers> - display the caller stack | ||||||||||||||||||
| 26 | |||||||||||||||||||
| 27 | =head1 DESCRIPTION | ||||||||||||||||||
| 28 | |||||||||||||||||||
| 29 | This C<Devel::MAT> tool displays the captured state of the caller stack, | ||||||||||||||||||
| 30 | showing which functions have been called, and what their arguments were. | ||||||||||||||||||
| 31 | |||||||||||||||||||
| 32 | =cut | ||||||||||||||||||
| 33 | |||||||||||||||||||
| 34 | =head1 COMMANDS | ||||||||||||||||||
| 35 | |||||||||||||||||||
| 36 | =for highlighter | ||||||||||||||||||
| 37 | |||||||||||||||||||
| 38 | =head2 callers | ||||||||||||||||||
| 39 | |||||||||||||||||||
| 40 | pmat> callers | ||||||||||||||||||
| 41 | caller(0): &main::func => void | ||||||||||||||||||
| 42 | at program.pl line 4 | ||||||||||||||||||
| 43 | $_[0]: SCALAR(PV) at 0x55c2bdce2778 = "arguments" | ||||||||||||||||||
| 44 | $_[1]: SCALAR(PV) at 0x55c2bdce2868 = "go" | ||||||||||||||||||
| 45 | $_[2]: SCALAR(PV) at 0x55c2bdce26e8 = "here" | ||||||||||||||||||
| 46 | |||||||||||||||||||
| 47 | Prints details of the caller stack, including arguments to functions. | ||||||||||||||||||
| 48 | |||||||||||||||||||
| 49 | Takes the following named options: | ||||||||||||||||||
| 50 | |||||||||||||||||||
| 51 | =over 4 | ||||||||||||||||||
| 52 | |||||||||||||||||||
| 53 | =item --pad, -P | ||||||||||||||||||
| 54 | |||||||||||||||||||
| 55 | Additionally show the contents of the active PAD at this depth. | ||||||||||||||||||
| 56 | |||||||||||||||||||
| 57 | =back | ||||||||||||||||||
| 58 | |||||||||||||||||||
| 59 | =cut | ||||||||||||||||||
| 60 | |||||||||||||||||||
| 61 | 0 | ▶ | sub run ( $self, $optref ) | ||||||||||||||||
|
Subroutine
run: not called CC=15 SCAR=54.8
Pod
undocumented
| |||||||||||||||||||
| 62 | { | ||||||||||||||||||
| 63 | 0 | my %opts = $optref->%*; | |||||||||||||||||
| 64 | |||||||||||||||||||
| 65 | 0 | my @contexts = $self->df->contexts; | |||||||||||||||||
| 66 | 0 | foreach my $idx ( 0 .. $#contexts ) { | |||||||||||||||||
| 67 | 0 | my $ctx = $contexts[$idx]; | |||||||||||||||||
| 68 | 0 | my $what; | |||||||||||||||||
| 69 | |||||||||||||||||||
| 70 | 0 | for( $ctx->type ) { | |||||||||||||||||
| 71 | 0 | ▶ | if( $_ eq "SUB" ) { | ||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||
| 72 | 0 | $what = String::Tagged->from_sprintf( "%s=%s", | |||||||||||||||||
| 73 | Devel::MAT::Cmd->format_sv( $ctx->cv ), | ||||||||||||||||||
| 74 | Devel::MAT::Cmd->format_symbol( $ctx->cv->symname ), | ||||||||||||||||||
| 75 | ); | ||||||||||||||||||
| 76 | } | ||||||||||||||||||
| 77 | elsif( $_ eq "TRY" ) { | ||||||||||||||||||
| 78 | 0 | $what = "eval {...}"; | |||||||||||||||||
| 79 | } | ||||||||||||||||||
| 80 | elsif( $_ eq "EVAL" ) { | ||||||||||||||||||
| 81 | 0 | $what = String::Tagged->from_sprintf( "eval (%s)", | |||||||||||||||||
| 82 | Devel::MAT::Cmd->format_value( $ctx->code->pv, pv => 1 ), | ||||||||||||||||||
| 83 | ); | ||||||||||||||||||
| 84 | } | ||||||||||||||||||
| 85 | } | ||||||||||||||||||
| 86 | |||||||||||||||||||
| 87 | 0 | Devel::MAT::Cmd->printf( "%s: %s => %s\n", | |||||||||||||||||
| 88 | Devel::MAT::Cmd->format_note( sprintf "caller(%d)", $idx ), | ||||||||||||||||||
| 89 | $what, | ||||||||||||||||||
| 90 | Devel::MAT::Cmd->format_note( $ctx->gimme ), | ||||||||||||||||||
| 91 | ); | ||||||||||||||||||
| 92 | |||||||||||||||||||
| 93 | 0 | Devel::MAT::Cmd->printf( " at %s\n", | |||||||||||||||||
| 94 | $ctx->location, | ||||||||||||||||||
| 95 | ); | ||||||||||||||||||
| 96 | |||||||||||||||||||
| 97 | 0 | ▶ | next unless $ctx->type eq "SUB"; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 98 | |||||||||||||||||||
| 99 | 0 | ▶ | my $args = $ctx->args or next; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 100 | 0 | my @args = $args->elems; | |||||||||||||||||
| 101 | |||||||||||||||||||
| 102 | 0 | my $doneargs; | |||||||||||||||||
| 103 | |||||||||||||||||||
| 104 | $doneargs++, Devel::MAT::Cmd->printf( " %s: %s\n", | ||||||||||||||||||
| 105 | Devel::MAT::Cmd->format_note( "\$_[$_]", 1 ), | ||||||||||||||||||
| 106 | Devel::MAT::Cmd->format_sv_with_value( $args[$_] ) | ||||||||||||||||||
| 107 | 0 | ) for 0 .. $#args; | |||||||||||||||||
| 108 | |||||||||||||||||||
| 109 | 0 | my $cv = $ctx->cv; | |||||||||||||||||
| 110 | |||||||||||||||||||
| 111 | 0 | Devel::MAT::Cmd->printf( " cv=%s\n", | |||||||||||||||||
| 112 | Devel::MAT::Cmd->format_sv( $cv ), | ||||||||||||||||||
| 113 | ); | ||||||||||||||||||
| 114 | |||||||||||||||||||
| 115 | 0 | ▶ | ( my $depth = $ctx->depth ) > -1 or next; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 116 | 0 | my $pad = $cv->pad( $depth ); | |||||||||||||||||
| 117 | |||||||||||||||||||
| 118 | 0 | ▶ | if( $opts{pad} ) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 119 | 0 | Devel::MAT::Cmd->printf( " curpad=%s\n", | |||||||||||||||||
| 120 | Devel::MAT::Cmd->format_sv( $pad ) | ||||||||||||||||||
| 121 | ); | ||||||||||||||||||
| 122 | |||||||||||||||||||
| 123 | 0 | require Devel::MAT::Tool::Show; | |||||||||||||||||
| 124 | 0 | Devel::MAT::Tool::Show->show_PAD_contents( $pad ); | |||||||||||||||||
| 125 | } | ||||||||||||||||||
| 126 | else { | ||||||||||||||||||
| 127 | 0 | foreach my $name ( '$self' ) { | |||||||||||||||||
| 128 | 0 | ▶ | my $self_padix = $cv->padix_from_padname( $name ) | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 129 | or next; | ||||||||||||||||||
| 130 | |||||||||||||||||||
| 131 | 0 | ▶ | if( my $sv = $pad->elem( $self_padix ) ) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 132 | 0 | $doneargs++; | |||||||||||||||||
| 133 | 0 | Devel::MAT::Cmd->printf( " %s: %s\n", | |||||||||||||||||
| 134 | Devel::MAT::Cmd->format_note( $name, 1 ), | ||||||||||||||||||
| 135 | Devel::MAT::Cmd->format_sv_with_value( $sv ), | ||||||||||||||||||
| 136 | ); | ||||||||||||||||||
| 137 | } | ||||||||||||||||||
| 138 | else { | ||||||||||||||||||
| 139 | 0 | $doneargs++; | |||||||||||||||||
| 140 | 0 | Devel::MAT::Cmd->printf( " no %s\n", | |||||||||||||||||
| 141 | Devel::MAT::Cmd->format_note( $name, 1 ), | ||||||||||||||||||
| 142 | ); | ||||||||||||||||||
| 143 | } | ||||||||||||||||||
| 144 | } | ||||||||||||||||||
| 145 | } | ||||||||||||||||||
| 146 | |||||||||||||||||||
| 147 | 0 | ▶ | $doneargs or | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 148 | Devel::MAT::Cmd->printf( " %s\n", | ||||||||||||||||||
| 149 | Devel::MAT::Cmd->format_note( "(no args)", 1 ), | ||||||||||||||||||
| 150 | ); | ||||||||||||||||||
| 151 | } | ||||||||||||||||||
| 152 | } | ||||||||||||||||||
| 153 | |||||||||||||||||||
| 154 | =head1 AUTHOR | ||||||||||||||||||
| 155 | |||||||||||||||||||
| 156 | Paul Evans <leonerd@leonerd.org.uk> | ||||||||||||||||||
| 157 | |||||||||||||||||||
| 158 | =cut | ||||||||||||||||||
| 159 | |||||||||||||||||||
| 160 | 5 | 0x55AA; | |||||||||||||||||