| 1 | |||
| 2 | |||
| 3 | package Devel::callsfrom; | ||
| 4 | use Data::Dumper; | ||
| 5 | |||
| 6 | # From perldoc perlvar | ||
| 7 | # Debugger flags, so you can see what we turn on below. | ||
| 8 | # | ||
| 9 | # 0x01 Debug subroutine enter/exit. | ||
| 10 | # | ||
| 11 | # 0x02 Line-by-line debugging. | ||
| 12 | # | ||
| 13 | # 0x04 Switch off optimizations. | ||
| 14 | # | ||
| 15 | # 0x08 Preserve more data for future interactive inspections. | ||
| 16 | # | ||
| 17 | # 0x10 Keep info about source lines on which a subroutine is defined. | ||
| 18 | # | ||
| 19 | # 0x20 Start with single-step on. | ||
| 20 | # | ||
| 21 | # 0x40 Use subroutine address instead of name when reporting. | ||
| 22 | # | ||
| 23 | # 0x80 Report "goto &subroutine" as well. | ||
| 24 | # | ||
| 25 | # 0x100 Provide informative "file" names for evals based on the place they were com- | ||
| 26 | # piled. | ||
| 27 | # | ||
| 28 | # 0x200 Provide informative names to anonymous subroutines based on the place they | ||
| 29 | # were compiled. | ||
| 30 | # | ||
| 31 | # 0x400 Debug assertion subroutines enter/exit. | ||
| 32 | # | ||
| 33 | |||
| 34 | BEGIN { $^P |= (0x01 | 0x80 | 0x100 | 0x200); }; | ||
| 35 | #BEGIN { $^P |= (0x004 | 0x100 ); }; | ||
| 36 | |||
| 37 | sub import { } | ||
| 38 | |||
| 39 | package DB; | ||
| 40 | |||
| 41 | # Any debugger needs to have a sub DB. It doesn't need to do anything. | ||
| 42 | sub DB{}; | ||
| 43 | |||
| 44 | # We want to track how deep our subroutines go | ||
| 45 | our $CALL_DEPTH = 0; | ||
| 46 | our %CALLED; | ||
| 47 | our $CALL_WATCH = $ENV{CALL_WATCH}; | ||
| 48 | |||
| 49 | sub sub { | ||
| 50 | local $DB::CALL_DEPTH = $DB::CALL_DEPTH+1; | ||
| 51 | no strict; | ||
| 52 | no warnings; | ||
| 53 | my @c0 = caller(0); | ||
| 54 | my @c1 = caller(-1); | ||
| 55 | my ($pkg,$file,$line) = @c1; | ||
| 56 | my $csub = $c0[3] || '-'; | ||
| 57 | my $caller = join(",", $file,$line,$pkg,$csub); | ||
| 58 | print STDERR ((' ' x $DB::CALL_DEPTH) . $DB::sub{$DB::sub} . ' > ' . $DB::sub . "(@_) : " . $caller . "\n") if $CALL_WATCH; | ||
| 59 | $DB::CALLED{$DB::sub}{$caller}++; | ||
| 60 | &{$DB::sub}; | ||
| 61 | } | ||
| 62 | |||
| 63 | |||
| 64 | END { | ||
| 65 | use strict; | ||
| 66 | use warnings; | ||
| 67 | my %counts; | ||
| 68 | for my $sub (keys %DB::sub) { | ||
| 69 | my $cases = $DB::CALLED{$sub}; | ||
| 70 | my @callers = keys %$cases; | ||
| 71 | my $call_count = scalar(@callers); | ||
| 72 | $counts{$call_count}{$sub} = $cases; | ||
| 73 | } | ||
| 74 | my @counts = keys %counts; | ||
| 75 | my $call_min = $ENV{CALL_MIN}; | ||
| 76 | if (defined $call_min) { | ||
| 77 | @counts = grep { $_ >= $call_min } @counts; | ||
| 78 | } | ||
| 79 | my $call_max = $ENV{CALL_MAX}; | ||
| 80 | if (defined $call_max) { | ||
| 81 | @counts = grep { $_ <= $call_max } @counts; | ||
| 82 | } | ||
| 83 | my $fh; | ||
| 84 | if (my $fname = $ENV{CALL_COUNT_OUTFILE}) { | ||
| 85 | open($fh,">$fname"); | ||
| 86 | unless ($fh) { die "failed to open outfile for call count for $0!" }; | ||
| 87 | } | ||
| 88 | else { | ||
| 89 | open($fh,">$0.callcount"); | ||
| 90 | $fh or die "failed to open output file $0.callcount: $!"; | ||
| 91 | } | ||
| 92 | for my $c (sort { $a <=> $b } @counts) { | ||
| 93 | my $subs = $counts{$c}; | ||
| 94 | for my $sub (sort keys %$subs) { | ||
| 95 | my $cases = $subs->{$sub}; | ||
| 96 | my @calls = sort keys %$cases; | ||
| 97 | print $fh join("\t",$c, $sub,$DB::sub{$sub},@calls),"\n"; | ||
| 98 | } | ||
| 99 | } | ||
| 100 | } | ||
| 101 | |||
| 102 | 1; | ||
| 103 |