| 1 | package Devel::TraceRun; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | use strict; | ||||||||||||||||||||||||||||
| 4 | use warnings; | ||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | our $VERSION = '0.003'; | ||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | { | ||||||||||||||||||||||||||||
| 9 | package DB; | ||||||||||||||||||||||||||||
| 10 | 0 | ▶ | { no warnings 'redefine'; sub DB {} } | ||||||||||||||||||||||||||
|
Subroutine
DB: not called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 11 | our $sub; | ||||||||||||||||||||||||||||
| 12 | our $indent = ''; | ||||||||||||||||||||||||||||
| 13 | sub ref2nice { | ||||||||||||||||||||||||||||
| 14 | 0 | ▶ | my $l = shift; | ||||||||||||||||||||||||||
|
Subroutine
ref2nice: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 15 | 0 | ▶ | return $l if !ref $l; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 16 | 0 | ▶ | return "main entry" if !@_; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 17 | 0 | "$_[0]::($_[1]:$_[2])"; | |||||||||||||||||||||||||||
| 18 | } | ||||||||||||||||||||||||||||
| 19 | sub niceitem { | ||||||||||||||||||||||||||||
| 20 | 0 | ▶ | !defined $_[0] ? 'undef' : ref($_[0]) || substr $_[0], 0, 10; | ||||||||||||||||||||||||||
|
Subroutine
niceitem: not called CC=3 SCAR=24.8
Pod
undocumented
Branch: 1 branch
Condition0%0/3
ref $_[0] or substr $_[0], 0, 10
MC/DC0%0/2
ref $_[0] or substr $_[0], 0, 10
ref $_[0]
substr $_[0], 0, 10
Truth table0%0/3
A or B
A= ref $_[0]B= substr $_[0], 0, 10
| |||||||||||||||||||||||||||||
| 21 | } | ||||||||||||||||||||||||||||
| 22 | sub niceshow { | ||||||||||||||||||||||||||||
| 23 | 0 | ▶ | my ($label, $first) = (shift, 0); | ||||||||||||||||||||||||||
|
Subroutine
niceshow: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 24 | 0 | print STDOUT $indent, $label, "("; # do in bits so not allocate new memory | |||||||||||||||||||||||||||
| 25 | 0 | ▶ | print STDOUT (!$first++ ? '' : ','), niceitem($_) for @_; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 26 | 0 | print STDOUT ")\n"; | |||||||||||||||||||||||||||
| 27 | } | ||||||||||||||||||||||||||||
| 28 | sub sub { | ||||||||||||||||||||||||||||
| 29 | 0 | ▶ | niceshow(ref2nice($sub, caller), @_); | ||||||||||||||||||||||||||
|
Subroutine
sub: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||||||||||||||||
| 30 | 0 | my @retshow; | |||||||||||||||||||||||||||
| 31 | { | ||||||||||||||||||||||||||||
| 32 | 0 | no strict 'refs'; | |||||||||||||||||||||||||||
| 33 | 0 | local $indent = $indent . ' '; | |||||||||||||||||||||||||||
| 34 | 0 | ▶ | if (!defined wantarray) { &$sub } | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 35 | 0 | elsif (!wantarray) { $retshow[0] = &$sub } | |||||||||||||||||||||||||||
| 36 | 0 | else { @retshow = &$sub } | |||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||
| 38 | 0 | niceshow("return", @retshow); | |||||||||||||||||||||||||||
| 39 | 0 | ▶ | wantarray || !defined wantarray ? @retshow : $retshow[0]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
wantarray or not defined wantarray
MC/DC0%0/2
wantarray or not defined wantarray
wantarray
not defined wantarray
Truth table0%0/3
A or not(B)
A= wantarrayB= not defined wantarray
| |||||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | =head1 NAME | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | Devel::TraceRun - Shows all the function calls and returns in a Perl program | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | =begin markdown | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | # PROJECT STATUS | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | [](https://metacpan.org/pod/Devel::TraceRun) | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | =end markdown | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | $ perl -d -d:TraceRun -S yourscript | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | Figuring out a large system's workings is hard. Figuring out why it's | ||||||||||||||||||||||||||||
| 62 | not working, and where it's going wrong, is even harder. | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | This tool produces an indented list of all function calls with parameters | ||||||||||||||||||||||||||||
| 65 | (in a very very concise format) and return values (ditto). It aims to | ||||||||||||||||||||||||||||
| 66 | minimise diffs between runs of a program that are doing the same thing, | ||||||||||||||||||||||||||||
| 67 | so that differences stand out. | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | The output is on C<STDOUT> currently. That may become overridable in | ||||||||||||||||||||||||||||
| 70 | due course. | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | =head2 Example | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | This code (from the test - C<Thing::func> calls C<Thing::func2> which | ||||||||||||||||||||||||||||
| 75 | returns "2nd retval"): | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | use Thing; | ||||||||||||||||||||||||||||
| 78 | my $f = sub { Thing::func() }; | ||||||||||||||||||||||||||||
| 79 | Thing::func([2]); # void | ||||||||||||||||||||||||||||
| 80 | my @r = Thing::func([2]); | ||||||||||||||||||||||||||||
| 81 | my @r2 = $f->("very long string", 2, bless {}, 'Thing'); | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | run with: | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | perl -d -d:TraceRun script.pl | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | produces on perl 5.8 - 5.14 (output is slightly different from 5.16, | ||||||||||||||||||||||||||||
| 88 | and again from 5.28): | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | main::(script.pl:1)() | ||||||||||||||||||||||||||||
| 91 | return() | ||||||||||||||||||||||||||||
| 92 | Thing::func(ARRAY) | ||||||||||||||||||||||||||||
| 93 | Thing::func2() | ||||||||||||||||||||||||||||
| 94 | return() | ||||||||||||||||||||||||||||
| 95 | return() | ||||||||||||||||||||||||||||
| 96 | Thing::func(ARRAY) | ||||||||||||||||||||||||||||
| 97 | Thing::func2() | ||||||||||||||||||||||||||||
| 98 | return(2nd retval) | ||||||||||||||||||||||||||||
| 99 | return(2nd retval) | ||||||||||||||||||||||||||||
| 100 | main entry(very long ,2,Thing) | ||||||||||||||||||||||||||||
| 101 | Thing::func() | ||||||||||||||||||||||||||||
| 102 | Thing::func2() | ||||||||||||||||||||||||||||
| 103 | return(2nd retval) | ||||||||||||||||||||||||||||
| 104 | return(2nd retval) | ||||||||||||||||||||||||||||
| 105 | return(2nd retval) | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | Note that in void context, nothing is returned. Also note the C<very long | ||||||||||||||||||||||||||||
| 108 | string> is truncated to (currently) 10 characters, array- and hash-refs | ||||||||||||||||||||||||||||
| 109 | are printed just as C<ARRAY> and C<HASH>, and objects as their classname. | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | There is currently no way to change what is printed. One thing that would | ||||||||||||||||||||||||||||
| 112 | increase homogeneity across runs would be a way to specify for certain | ||||||||||||||||||||||||||||
| 113 | function-names, which inputs or outputs to mask. Obvious examples where | ||||||||||||||||||||||||||||
| 114 | this would help are functions that return the current time, or a random | ||||||||||||||||||||||||||||
| 115 | number, or the port number of an incoming IP connection. | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | Another source of output variation is the change in Perl 5.18 to randomise | ||||||||||||||||||||||||||||
| 118 | the order hash-keys are returned in. You can overcome this for tracing | ||||||||||||||||||||||||||||
| 119 | purposes by setting the environment variable C<PERL_HASH_SEED> to 0, | ||||||||||||||||||||||||||||
| 120 | which also has the effect of setting C<PERL_PERTURB_KEYS> to 0. | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | =head2 How it works | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | As may be discerned from the command line, it uses Perl's debugging | ||||||||||||||||||||||||||||
| 125 | functionality. However, unlike the normal use of that, it is entirely | ||||||||||||||||||||||||||||
| 126 | non-interactive. Instead, it replaces C<DB::DB> with a no-op, and uses | ||||||||||||||||||||||||||||
| 127 | the C<DB::sub> hook to report function entries and returns. | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | These reports are indented (currently hardcoded to two spaces), nested | ||||||||||||||||||||||||||||
| 130 | according to stack depth. It is intended to be completely obvious what | ||||||||||||||||||||||||||||
| 131 | everything means. | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | L<perldebguts> | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | Ed J | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||
| 144 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | =cut | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | 1; | ||||||||||||||||||||||||||||