lib/Devel/Trepan/DB/Backtrace.pm

filter: Statementstmt 10.6% 17 / 160 Branchbran 0.9% 1 / 106 Conditioncond 0.0% 0 / 52 MC/DCmcdc 0.0% 0 / 28 Subroutinesub 55.5% 5 / 9 Podpod 0.0% 0 / 4 totaltotal 6.4% 23 / 359 CC 73 SCAR 71.3
CC
73
Cov
6%
tbacktrace
1361.4
tbacktrace
1361.4
five
2.0
CRAP 1243.8
« lib/Devel/Trepan/DB.pm ↑ Summary lib/Devel/Trepan/CmdProcessor/Command/List.pm »
1 12 ▶use warnings; no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
2 12 ▶use English qw( -no_match_vars );
Subroutine BEGIN: called CC=1 SCAR=0.0
3 12 ▶use B;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 =pod
6  
7 =head2 tbacktrace(skip[,count,scan_for_DB])
8  
9 Collect the traceback information available via C<caller()>. Some
10 filtering and cleanup of the data is done.
11  
12 C<skip> defines the number of stack frames to be skipped, working
13 backwards from the most current frame before the call the debugger
14 DB::DB call if scan_for_DB is set, or the most-current frame.
15  
16 C<count> determines the total number of call frames to be returned; all of
17 them (well, the first 10^9) are returned if C<count> is omitted.
18  
19 This routine returns a list of hashes, from most-recent to least-recent
20 stack frame. Each has the following keys and values:
21  
22 =over 4
23  
24 =item *
25  
26 C<wantarray> - C<.> (null), C<$> (scalar), or C<@> (array)
27  
28 =item *
29  
30 C<fn> - subroutine name, or C<eval> information
31  
32 =item *
33  
34 C<args> - undef, or a reference to an array of arguments
35  
36 =item *
37  
38 C<file> - the file in which this item was defined (if any)
39  
40 =item *
41  
42 C<line> - the line on which it was defined
43  
44 =item *
45  
46 C<evaltext> - eval text if we are in an eval.
47  
48 =back
49  
50 =cut
51  
52 # NOTE: this routine needs to be in package DB for us to be able to pick up the
53 # subroutine args.
54 0 ▶sub tbacktrace($;$$$) {
Subroutine tbacktrace: not called CC=37 SCAR=72.2
Pod undocumented
55 0 ▶ my ($self, $skip, $count, $scan_for_DB_sub) = @_;
Subroutine tbacktrace: not called CC=37 SCAR=72.2
Pod undocumented
56 0 ▶ $skip = 0 unless defined($skip);
Branch: 2 branches
BranchTrueFalseTotal
unless defined $skip 0 0 0
unless defined $skip 0 0 0
57 0 ▶ $count = 1e9 unless defined($count);
Branch: 2 branches
BranchTrueFalseTotal
unless defined $count 0 0 0
unless defined $count 0 0 0
58  
59 0 ▶ $scan_for_DB_sub ||= 1;
Condition0%0/4
$scan_for_DB_sub ||= 1
l!l
00
$scan_for_DB_sub ||= 1
l!l
00
MC/DC0%0/1
$scan_for_DB_sub ||= 1
$scan_for_DB_sub
MC/DC0%0/1
$scan_for_DB_sub ||= 1
$scan_for_DB_sub
Truth table0%0/2
A
A= $scan_for_DB_sub
Aresult
11
00
Truth table0%0/2
A
A= $scan_for_DB_sub
Aresult
11
00
60 # print "scan: $scan_for_DB_sub\n";
61  
62 # These variables are used to capture output from caller();
63 0 my ( $pkg, $file, $line, $fn, $hasargs, $wantarray, $evaltext, $is_require );
64  
65 0 my $i=0;
66 0 ▶ if ($scan_for_DB_sub) {
Branch: 2 branches
BranchTrueFalseTotal
if ($scan_for_DB_sub) 0 0 0
if ($scan_for_DB_sub) 0 0 0
67 12 ▶ no warnings qw(once uninitialized); # For $DB::event
Subroutine BEGIN: called CC=1 SCAR=0.0
68 0 ▶ my $db_fn = ($DB::event eq 'post-mortem') ? 'catch' : 'DB';
Branch: 2 branches
BranchTrueFalseTotal
$event eq 'post-mortem' ? : 0 0 0
$event eq 'post-mortem' ? : 0 0 0
69 # Warning: There is a bug caller that lists DB:DB as the function
70 # name rather than the name the debugged program may have been in
71 0 ▶ while (my ($pkg, $file, $line, $fn) = caller($i++)) {
Branch: 2 branches
BranchTrueFalseTotal
if (() = caller $i++) 0 0 0
if (() = caller $i++) 0 0 0
72 0 ▶ if ("DB::$db_fn" eq $fn or ('DB' eq $pkg && $db_fn eq $fn)) {
Branch: 2 branches
BranchTrueFalseTotal
if ("DB::$db_fn" eq $fn or "DB" eq $pkg and $db_fn eq $fn) 0 0 0
if ("DB::$db_fn" eq $fn or "DB" eq $pkg and $db_fn eq $fn) 0 0 0
Condition0%0/12
"DB::$db_fn" eq $fn or "DB" eq $pkg and $db_fn eq $fn
l!l&&r!l&&!r
000
"DB" eq $pkg and $db_fn eq $fn
!ll&&!rl&&r
000
"DB::$db_fn" eq $fn or "DB" eq $pkg and $db_fn eq $fn
l!l&&r!l&&!r
000
"DB" eq $pkg and $db_fn eq $fn
!ll&&!rl&&r
000
MC/DC0%0/3
"DB::$db_fn" eq $fn or "DB" eq $pkg and $db_fn eq $fn
"DB::$db_fn" eq $fn "DB" eq $pkg $db_fn eq $fn
MC/DC0%0/3
"DB::$db_fn" eq $fn or "DB" eq $pkg and $db_fn eq $fn
"DB::$db_fn" eq $fn "DB" eq $pkg $db_fn eq $fn
Truth table0%0/4
A or B and C
A= "DB::$db_fn" eq $fnB= "DB" eq $pkgC= $db_fn eq $fn
ABCresult
1XX1
0111
00X0
0100
Truth table0%0/4
A or B and C
A= "DB::$db_fn" eq $fnB= "DB" eq $pkgC= $db_fn eq $fn
ABCresult
1XX1
0111
00X0
0100
73 0 $i--;
74 0 last ;
75 }
76 }
77 }
78  
79 0 $scan_for_DB_sub = $i;
80 0 $count += $i;
81 # print "++count: $count, i $i $DB::event\n"; # XX debug
82 0 ▶ $i -= 2 if $DB::event eq 'call';
Branch: 2 branches
BranchTrueFalseTotal
if $event eq "call" 0 0 0
if $event eq "call" 0 0 0
83  
84 0 my ( @a, $args_ary );
85 0 my @callstack = ();
86  
87 # # XXX Okay... why'd we do that?
88 0 my $nothard = not $DB::frame & 8;
89 0 local $DB::frame = 0;
90  
91 # Start out at the skip count, $i.
92 # If we haven't reached the number of frames requested, and caller() is
93 # still returning something, stay in the loop. (If we pass the requested
94 # number of stack frames, or we run out - caller() returns nothing - we
95 # quit.
96 # Up the stack frame index to go back one more level each time.
97 0 ▶ while ($i <= $count and
Branch: 2 branches
BranchTrueFalseTotal
if ($i <= $count and () = caller $i) 0 0 0
if ($i <= $count and () = caller $i) 0 0 0
Condition0%0/6
$i <= $count and () = caller $i
!ll&&!rl&&r
000
$i <= $count and () = caller $i
!ll&&!rl&&r
000
MC/DC0%0/2
$i <= $count and () = caller $i
$i <= $count () = caller $i
MC/DC0%0/2
$i <= $count and () = caller $i
$i <= $count () = caller $i
Truth table0%0/3
A and B
A= $i <= $countB= () = caller $i
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= $i <= $countB= () = caller $i
ABresult
0X0
100
111
98 ($pkg, $file, $line, $fn, $hasargs, $wantarray, $evaltext,
99 $is_require) = caller($i))
100 {
101 0 my $addr = Devel::Callsite::callsite($i);
102  
103 ## print "++file: $file, line $line $fn\n"; # XX if $DB::DEBUGME;
104 0 $i++;
105 0 ▶ next if $pkg eq 'DB' && ($fn eq 'sub' || $fn eq 'lsub' ||
Branch: 2 branches
BranchTrueFalseTotal
if $pkg eq "DB" and $fn eq "sub" || $fn eq "lsub" || $file =~ m[Devel/Trepan/DB/Sub\.pm$] 0 0 0
if $pkg eq "DB" and $fn eq "sub" || $fn eq "lsub" || $file =~ m[Devel/Trepan/DB/Sub\.pm$] 0 0 0
Condition0%0/18
$pkg eq "DB" and $fn eq "sub" || $fn eq "lsub" || $file =~ m[Devel/Trepan/DB/Sub\.pm$]
!ll&&!rl&&r
000
$fn eq "sub" or $fn eq "lsub" or $file =~ m[Devel/Trepan/DB/Sub\.pm$]
l!l&&r!l&&!r
000
$fn eq "sub" or $fn eq "lsub"
l!l&&r!l&&!r
000
$pkg eq "DB" and $fn eq "sub" || $fn eq "lsub" || $file =~ m[Devel/Trepan/DB/Sub\.pm$]
!ll&&!rl&&r
000
$fn eq "sub" or $fn eq "lsub" or $file =~ m[Devel/Trepan/DB/Sub\.pm$]
l!l&&r!l&&!r
000
$fn eq "sub" or $fn eq "lsub"
l!l&&r!l&&!r
000
MC/DC0%0/4
$pkg eq "DB" and $fn eq "sub" || $fn eq "lsub" || $file =~ m[Devel/Trepan/DB/Sub\.pm$]
$pkg eq "DB" $fn eq "sub" $fn eq "lsub" $file =~ m[Devel/Trepan/DB/Sub\.pm$]
MC/DC0%0/4
$pkg eq "DB" and $fn eq "sub" || $fn eq "lsub" || $file =~ m[Devel/Trepan/DB/Sub\.pm$]
$pkg eq "DB" $fn eq "sub" $fn eq "lsub" $file =~ m[Devel/Trepan/DB/Sub\.pm$]
Truth table0%0/5
A and B or C or D
A= $pkg eq "DB"B= $fn eq "sub"C= $fn eq "lsub"D= $file =~ m[Devel/Trepan/DB/Sub\.pm$]
ABCDresult
0XXX0
10000
11XX1
101X1
10011
Truth table0%0/5
A and B or C or D
A= $pkg eq "DB"B= $fn eq "sub"C= $fn eq "lsub"D= $file =~ m[Devel/Trepan/DB/Sub\.pm$]
ABCDresult
0XXX0
10000
11XX1
101X1
10011
106 $file =~ m{Devel/Trepan/DB/Sub\.pm$});
107 # Go through the arguments and save them for later.
108 0 @a = ();
109 0 for my $arg (@DB::args) {
110 0 my $type;
111 0 ▶ if ( not defined $arg ) { # undefined parameter
Branch: 6 branches
BranchTrueFalseTotal
if (not defined $arg) { } 0 0 0
elsif ($nothard and tied $arg) { } 0 0 0
elsif ($nothard and $type = ref $arg) { } 0 0 0
if (not defined $arg) { } 0 0 0
elsif ($nothard and tied $arg) { } 0 0 0
elsif ($nothard and $type = ref $arg) { } 0 0 0
112 0 push @a, "undef";
113 }
114  
115 ▶ elsif ( $nothard and tied $arg ) { # tied parameter
Condition0%0/6
$nothard and tied $arg
!ll&&!rl&&r
000
$nothard and tied $arg
!ll&&!rl&&r
000
MC/DC0%0/2
$nothard and tied $arg
$nothard tied $arg
MC/DC0%0/2
$nothard and tied $arg
$nothard tied $arg
Truth table0%0/3
A and B
A= $nothardB= tied $arg
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= $nothardB= tied $arg
ABresult
0X0
100
111
116 0 push @a, "tied";
117 }
118 ▶ elsif ( $nothard and $type = ref $arg ) { # reference
Condition0%0/6
$nothard and $type = ref $arg
!ll&&!rl&&r
000
$nothard and $type = ref $arg
!ll&&!rl&&r
000
MC/DC0%0/2
$nothard and $type = ref $arg
$nothard $type = ref $arg
MC/DC0%0/2
$nothard and $type = ref $arg
$nothard $type = ref $arg
Truth table0%0/3
A and B
A= $nothardB= $type = ref $arg
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= $nothardB= $type = ref $arg
ABresult
0X0
100
111
119 0 push @a, "ref($type)";
120 }
121 else { # can be stringified
122 0 local $_ =
123 "$arg"; # Safe to stringify now - should not call f().
124  
125 # Backslash any single-quotes or backslashes.
126 0 s/([\'\\])/\\$1/g;
127  
128 # Single-quote it unless it's a number or a colon-separated
129 # name.
130 0 ▶ s/(.*)/'$1'/s
Branch: 2 branches
BranchTrueFalseTotal
unless /^(?: -?[\d.]+ | \*[\w:]* )$/x 0 0 0
unless /^(?: -?[\d.]+ | \*[\w:]* )$/x 0 0 0
131 unless /^(?: -?[\d.]+ | \*[\w:]* )$/x;
132  
133 # Turn high-bit characters into meta-whatever.
134 0 s/([\200-\377])/sprintf("M-%c",ord($1)&0177)/eg;
135  
136 # Turn control characters into ^-whatever.
137 0 s/([\0-\37\177])/sprintf("^%c",ord($1)^64)/eg;
138  
139 0 push( @a, $_ );
140 } ## end else [ if (not defined $arg)
141 } ## end for $arg (@args)
142  
143 # If $wantarray is true, this is array (@)context.
144 # If $wantarray is false, this is scalar ($) context.
145 # If neither, $wantarray isn't defined. (This is apparently a 'can't
146 # happen' trap.)
147 0 ▶ $wantarray = $wantarray ? '@' : ( defined $wantarray ? '$' : '.' );
Branch: 4 branches
BranchTrueFalseTotal
$wantarray ? : 0 0 0
defined $wantarray ? : 0 0 0
$wantarray ? : 0 0 0
defined $wantarray ? : 0 0 0
148  
149 # if the sub has args ($hasargs true), make an anonymous array of the
150 # dumped args.
151 0 ▶ $args_ary = $hasargs ? [@a] : undef;
Branch: 2 branches
BranchTrueFalseTotal
$hasargs ? : 0 0 0
$hasargs ? : 0 0 0
152  
153 # remove trailing newline-whitespace-semicolon-end of line sequence
154 # from the eval text, if any.
155 0 ▶ $evaltext =~ s/\n\s*\;\s*\Z// if $evaltext;
Branch: 2 branches
BranchTrueFalseTotal
if $evaltext 0 0 0
if $evaltext 0 0 0
156  
157 # Escape backslashed single-quotes again if necessary.
158 0 ▶ $evaltext =~ s/([\\\'])/\\$1/g if $evaltext;
Branch: 2 branches
BranchTrueFalseTotal
if $evaltext 0 0 0
if $evaltext 0 0 0
159  
160 # if the require flag is true, the eval text is from a require.
161 0 ▶ if ($is_require) {
Branch: 6 branches
BranchTrueFalseTotal
if ($is_require) { } 0 0 0
elsif (defined $is_require) { } 0 0 0
elsif ($fn eq '(eval)') { } 0 0 0
if ($is_require) { } 0 0 0
elsif (defined $is_require) { } 0 0 0
elsif ($fn eq '(eval)') { } 0 0 0
162 0 $fn = "require '$evaltext'";
163 }
164  
165 # if it's false, the eval text is really from an eval.
166 elsif ( defined $is_require ) {
167 0 $fn = "eval '$evaltext'";
168 }
169  
170 # If the sub is '(eval)', this is a block eval, meaning we don't
171 # know what the eval'ed text actually was.
172 elsif ( $fn eq '(eval)' ) {
173 0 $fn = "eval {...}";
174 }
175  
176 # Stick the collected information into @callstack a hash reference.
177 0 push(@callstack,
178 {
179 addr => $addr,
180 args => $args_ary,
181 evaltext => $evaltext,
182 file => $file,
183 fn => $fn,
184 line => $line,
185 pkg => $pkg,
186 wantarray => $wantarray,
187 }
188 );
189  
190 # Stop processing frames if the user hit control-C.
191 # last if $signal;
192 } ## end for ($i = $skip ; $i < ...
193  
194 ## use Data::Printer; Data::Printer::p @callstack; # XXX
195  
196 # The function and args for the stopped line is DB::DB,
197 # but we want it to be the function and args of the last call.
198 # Se we need to adjust those in @callstack.
199 # And the function and args for the file and line that called us
200 # should also be the prior function and args.
201 0 ▶ if ($scan_for_DB_sub) {
Branch: 2 branches
BranchTrueFalseTotal
if ($scan_for_DB_sub) 0 0 0
if ($scan_for_DB_sub) 0 0 0
202 0 my $len = @callstack;
203 0 ▶ if ($len) {
Branch: 2 branches
BranchTrueFalseTotal
if ($len) 0 0 0
if ($len) 0 0 0
204 0 ▶ for (my $i=1; $i < $len; $i++) {
Branch: 2 branches
BranchTrueFalseTotal
if ($i < $len) 0 0 0
if ($i < $len) 0 0 0
205 0 $callstack[$i-1]->{args} = $callstack[$i]->{args};
206 0 $callstack[$i-1]->{fn} = $callstack[$i]->{fn};
207 0 $callstack[$i-1]->{wantarray} = $callstack[$i]->{wantarray};
208 }
209 # $callstack[$len]->{args} = undef;
210 # $callstack[$len]->{fn} = undef;
211 }
212 }
213  
214 0 ▶ if ($DB::event eq 'call') {
Branch: 2 branches
BranchTrueFalseTotal
if ($event eq "call") 0 0 0
if ($event eq "call") 0 0 0
215 0 ▶ unshift @callstack, {
Branch: 2 branches
BranchTrueFalseTotal
$wantarray ? : 0 0 0
$wantarray ? : 0 0 0
216 addr => $DB::addr,
217 file => $DB::filename,
218 fn => $DB::subroutine,
219 line => $DB::lineno,
220 pkg => $DB::package,
221 args => $DB::hasargs,
222 wantarray => $DB::wantarray ? $DB::wantarray : '',
223 };
224 }
225 # use Data::Printer; Data::Printer::p @callstack;
226  
227 0 @callstack;
228 }
229  
230 12 ▶unless (caller) {
Branch: 1 branch
BranchTrueFalseTotal
unless (caller) 0 12 12
231 0 require Data::Dumper;
232 0 import Data::Dumper;
233 0 $DB::frame = 0;
234 0 our @callstack = tbacktrace(undef,undef,undef,0);
235 0 our $sep = '-' x 20 . "\n";
236 # print Dumper(@callstack), "\n";
237 # print $sep;
238 0 ▶ sub five {
Subroutine five: not called CC=1 SCAR=6.9
Pod undocumented
239 0 ▶ @callstack = tbacktrace(undef,undef,undef,0);
Subroutine five: not called CC=1 SCAR=6.9
Pod undocumented
240 0 print Dumper(@callstack), "\n";
241 0 print $sep;
242 0 @callstack = tbacktrace(undef,1,undef,0);
243 0 print Dumper(@callstack), "\n";
244 0 print $sep;
245 0 @callstack = tbacktrace(1,0,undef,0);
246 0 print Dumper(@callstack), "\n";
247 0 print $sep;
248 0 5;
249 }
250 0 my $five = five();
251 # $five = eval "@callstack = tbacktrace(undef, undef, undef, 0)";
252 # print Dumper(@callstack), "\n";
253 0 print $sep;
254 0 $five = eval "five";
255 0 print Dumper(@callstack), "\n";
256 }
257  
258 12 1;
« lib/Devel/Trepan/DB.pm ↑ Summary lib/Devel/Trepan/CmdProcessor/Command/List.pm »