lib/Devel/STrace.pm

filter: Statementstmt 8.7% 6 / 69 Branchbran 0.0% 0 / 48 Conditioncond 0.0% 0 / 35 MC/DCmcdc 0.0% 0 / 17 Subroutinesub 33.3% 2 / 6 Podpod 50.0% 1 / 2 totaltotal 5.0% 9 / 177 CC 36 SCAR 53.9
CC
36
Cov
4%
sub
329.3
DB
182.0
CLONE
12.0
CRAP 220.1
↑ Summary lib/Devel/STrace/Monitor.pm »
1 #/**
2 # Provides a minimal strace/truss-like utility for
3 # Perl scripts. Using <a href='http://search.cpan.org/perldoc?Devel::RingBuffer'>
4 # Devel::RingBuffer</a>, each new subroutine call is logged to an mmap'ed shared memory
5 # region (as provided by <a href='http://search.cpan.org/perldoc?IPC::Mmap'>IPC::Mmap</a>).
6 # As each statement is executed, the line number and Time::HiRes:;time() timestamp
7 # are written to the current ringbuffer slot. An external application can
8 # then monitor a running application by inspecting the mmap'ed area (see
9 # <a href='http://search.cpan.org/perldoc?Devel::STrace::Monitor'>Devel::STrace::Monitor</a>
10 # and the associated plstrace.pl application for an example).
11 # <p>
12 # Permission is granted to use this software under the same terms as Perl itself.
13 # Refer to the <a href='http://perldoc.perl.org/perlartistic.html'>Perl Artistic License</a>
14 # for details.
15 #
16 # @author D. Arnold
17 # @since 2006-05-01
18 # @see <a href='http://search.cpan.org/perldoc?Devel::RingBuffer'>Devel::RingBuffer</a>
19 # @see <a href='http://search.cpan.org/perldoc?IPC::Mmap'>IPC::Mmap</a>
20 # @see <a href='http://search.cpan.org/perldoc?Devel::STrace::Monitor'>Devel::STrace::Monitor</a>
21 # @see <a href='http://perldoc.perl.org/perldebguts.html'>perdebguts</a>
22 # @self $self
23 #*/
24 package Devel::STrace;
25  
26 require 5.008;
27  
28 package DB;
29  
30 #use threads;
31 use Config;
32 use Time::HiRes qw(time);
33 use Devel::RingBuffer;
34 use Devel::RingBuffer qw(:ringbuffer_consts);
35  
36 $Devel::STrace::VERSION = '0.31';
37  
38 # disable DB single-stepping
39 BEGIN {
40 if ($Config{useithreads} && (!$ENV{DEVEL_RINGBUF_NOTHREADS})) {
41 require Devel::RingBuffer::ThreadFacade;
42 $hasThreads = 1;
43 }
44 else {
45 $hasThreads = undef;
46 }
47 $single = 0;
48 $subtrace = 1;
49 $tid = -1;
50 $pid = -1;
51 $ringbuffer = undef;
52 $myring = undef;
53 $depth = 0;
54 $single = 1;
55 $inited = 0;
56 #
57 # DESTROY occasionally attempts to retie
58 # after we've lost the objects we get our fixed
59 # addresses from, so use local vars for them
60 #
61 $ringaddr = undef;
62 local ($pkg, $filename, $line);
63 }
64  
65 #/**
66 # Threads clone method. Allocates a new ring, and
67 # and populates the existing cloned ring object with
68 # the newly allocate ring information.
69 #
70 # @static
71 #*/
72 sub CLONE {
73 # print STDERR " ++++ In CLONE for ", Devel::RingBuffer::ThreadFacade->tid(), "\n";
74 0 ▶ my ($oldtrace, $oldsingle) = ($subtrace, $single);
Subroutine CLONE: not called CC=3 SCAR=24.8
75 0 $pid = $$;
76 0 ▶ $tid = $hasThreads ? Devel::RingBuffer::ThreadFacade::tid() : 0;
Branch: 1 branch
BranchTrueFalseTotal
$hasThreads ? : 0 0 0
77 0 $subtrace = 0;
78 0 $single = 0;
79 0 ▶ if (defined($myring)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $myring) { } 0 0 0
80 0 $myring->clone();
81 0 $ringaddr = $myring->getAddress();
82 # print STDERR "\n$tid Allocd ring ", $myring->getIndex(), "\n";
83 }
84 else {
85 0 $ringaddr = undef;
86 }
87 0 ($single, $subtrace) = (1, $oldtrace);
88 # print STDERR " ++++ Exit CLONE for ",
89 # Devel::RingBuffer::ThreadFacade->tid(), " with $ringaddr\n";
90 }
91 #/**
92 # Debug a single statement. Creates a ringbuffer if none exists.
93 # Allocates a new ring if executed in a new process. Updates
94 # the ring's current slot with the current line number and timestamp.
95 #
96 # @static
97 #*/
98 sub DB {
99 #
100 # if subtracing is disabled OR
101 # we're tracing one of our debugger modules OR
102 # we've init'd but theres no ringbuffer
103 # (meaning we're in global DESTROY),
104 # then skip out of this
105 #
106 0 ▶ return if (! $subtrace) || ($inited && (! $ringbuffer)) ||
Subroutine DB: not called CC=13 SCAR=52.0
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
if not $subtrace or $inited and not $ringbuffer or $sub and substr($sub, 0, 19) eq "Devel::RingBuffer::" 0 0 0
Condition0%0/12
not $subtrace or $inited and not $ringbuffer or $sub and substr($sub, 0, 19) eq "Devel::RingBuffer::"
l!l&&r!l&&!r
000
not $subtrace or $inited and not $ringbuffer
l!l&&r!l&&!r
000
$inited and not $ringbuffer
!ll&&!rl&&r
000
$sub and substr($sub, 0, 19) eq "Devel::RingBuffer::"
!ll&&!rl&&r
000
MC/DC0%0/5
not $subtrace or $inited and not $ringbuffer or $sub and substr($sub, 0, 19) eq "Devel::RingBuffer::"
not $subtrace $inited not $ringbuffer $sub substr($sub, 0, 19) eq "Devel::RingBuffer::"
Truth table0%0/8
not(A) or B and not(C) or D and E
A= not $subtraceB= $initedC= not $ringbufferD= $subE= substr($sub, 0, 19) eq "Devel::RingBuffer::"
ABCDEresult
1XXXX1
011XX1
00X111
010111
00X0X0
00X100
0100X0
010100
107 ($sub && (substr($sub, 0, 19) eq 'Devel::RingBuffer::'));
108  
109 0 $subtrace = 0;
110 0 $single = 0; # no effect if tied
111  
112 #print STDERR "In DB::DB\n";
113  
114 0 ▶ unless (defined($ringbuffer)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $ringbuffer) 0 0 0
115  
116 #print STDERR "DB::DB initing the ringbuffer\n";
117  
118 0 $ringbuffer = Devel::RingBuffer->new(TraceOnCreate => 1);
119 #
120 # alloc the root's ringbuffer so we can tie to it
121 #
122 0 $pid = $$;
123 0 ▶ $tid = ($hasThreads ? Devel::RingBuffer::ThreadFacade->tid() : 0);
Branch: 1 branch
BranchTrueFalseTotal
$hasThreads ? : 0 0 0
124 0 $myring = $ringbuffer->allocate();
125 0 ▶ die "Can't get a ring!!!!\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $myring 0 0 0
126 unless $myring;
127  
128 0 $depth = 0;
129 0 $ringaddr = $myring->getAddress();
130  
131 0 print STDERR "\n*** Ringbuffer file is ", $ringbuffer->getName(), "\n";
132  
133 # print STDERR "\n$tid Allocd ring ", $myring->getIndex(), "\n";
134 0 $inited = 1;
135 }
136 #
137 # check our pid; CLONE will handle threads for us
138 #
139 0 ▶ if ($pid != $$) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pid != $$) 0 0 0
140 #
141 # we keep the existing realdepth, since we may pop out
142 # of the fork level
143 #
144 0 $pid = $$;
145 0 ▶ $tid = ($hasThreads ? Devel::RingBuffer::ThreadFacade->tid() : 0);
Branch: 1 branch
BranchTrueFalseTotal
$hasThreads ? : 0 0 0
146 0 $myring = $myring->clone();
147 0 $depth = 0;
148 #
149 # if we can't get a ring buffer, exit
150 #
151 0 ▶ $single = 0,
Branch: 1 branch
BranchTrueFalseTotal
unless $myring 0 0 0
Condition0%0/5
$myring || ($single = 0, print(STDERR "Can't get a ring!!!!\n")) && (return)
l!l&&r!l&&!r
000
$single = 0, print(STDERR "Can't get a ring!!!!\n") and return
!ll
00
MC/DC0%0/2
$myring || ($single = 0, print(STDERR "Can't get a ring!!!!\n")) && (return)
$myring $single = 0, print(STDERR "Can't get a ring!!!!\n")
Truth table0%0/3
A || B
A= $myringB= $single = 0, print(STDERR "Can't get a ring!!!!\n")
ABresult
1X1
011
000
152 print STDERR "Can't get a ring!!!!\n" and
153 return
154 unless $myring;
155 0 $ringaddr = $myring->getAddress();
156 }
157 #
158 # update the current slot's line number and timestamp;
159 # note the update is suppressed in updateSlot if trace flag off
160 # (tho it doesn't contribute much optimization)
161 #
162 0 ($pkg, $filename, $line) = caller;
163 0 Devel::RingBuffer::Ring::updateSlot($ringaddr, $line);
164  
165 0 $single = 1;
166 0 $subtrace = 1;
167 }
168  
169 #/**
170 # Debug a subroutine call. If trace is enabled
171 # in the ring, the next slot is allocated and the called
172 # subroutine (from $DB::sub) is written to it. The subroutine
173 # is then called with the caller's return context, and then
174 # the slot is freed before returning. Note that this method
175 # requires additional control to handle re-entrancy when
176 # DB::DB() or DB::sub() make calls to the various support
177 # functions.
178 #
179 # @static
180 # @return The subroutine's return value(s), if the
181 # caller is not in void context.
182 #*/
183 sub sub {
184 #
185 # alloc next slot
186 # fill with info
187 # call &$sub
188 # clear slot
189 # free it up
190 #
191 # !!!TODO: return the current slot contents prior to writing over
192 # it; that way we can restore it after we return!!!
193 #
194 # !!!NOTE NOTE NOTE!!!
195 # we need to untie when realdepth goes to zero, and tie when it goes
196 # to one
197 #
198 0 ▶ my @ret;
Subroutine sub: not called CC=20 SCAR=58.0
Pod documented
199 my $ret;
200  
201 0 ▶ my $wassingle = defined($myring) ? Devel::RingBuffer::Ring::getFlags($ringaddr) : $single;
Branch: 1 branch
BranchTrueFalseTotal
defined $myring ? : 0 0 0
202  
203 0 ▶ unless ($subtrace && ($wassingle & 3) && defined($myring) &&
Branch: 1 branch
BranchTrueFalseTotal
unless ($subtrace and $wassingle & 3 and defined $myring and substr($sub, 0, 19) ne "Devel::RingBuffer::") 0 0 0
Condition0%0/9
$subtrace and $wassingle & 3 and defined $myring and substr($sub, 0, 19) ne "Devel::RingBuffer::"
!ll&&!rl&&r
000
$subtrace and $wassingle & 3 and defined $myring
!ll&&!rl&&r
000
$subtrace and $wassingle & 3
!ll&&!rl&&r
000
MC/DC0%0/4
$subtrace and $wassingle & 3 and defined $myring and substr($sub, 0, 19) ne "Devel::RingBuffer::"
$subtrace $wassingle & 3 defined $myring substr($sub, 0, 19) ne "Devel::RingBuffer::"
Truth table0%0/5
A and B and C and D
A= $subtraceB= $wassingle & 3C= defined $myringD= substr($sub, 0, 19) ne "Devel::RingBuffer::"
ABCDresult
0XXX0
10XX0
110X0
11100
11111
204 (substr($sub, 0, 19) ne 'Devel::RingBuffer::')) {
205 0 ▶ if (wantarray) {
Branch: 2 branches
BranchTrueFalseTotal
if (wantarray) { } 0 0 0
elsif (defined wantarray) { } 0 0 0
206 0 @ret = &$sub;
207 }
208 elsif (defined(wantarray)) {
209 0 $ret = &$sub;
210 }
211 else {
212 0 &$sub;
213 }
214  
215 0 ▶ return (wantarray) ? @ret : defined(wantarray) ? $ret : undef;
Branch: 2 branches
BranchTrueFalseTotal
wantarray ? : 0 0 0
defined wantarray ? : 0 0 0
216 }
217  
218 0 $subtrace = 0;
219  
220 # print STDERR "In $$:$tid DB::sub for $sub $depth\n";
221  
222 0 my $tsub ="$sub";
223 0 ▶ $tsub = 'main' unless $tsub;
Branch: 1 branch
BranchTrueFalseTotal
unless $tsub 0 0 0
224 #
225 # If $sub ends with '::AUTOLOAD', note we've traced into AUTOLOAD
226 #
227 0 ▶ $tsub .= " for $$tsub"
Branch: 1 branch
BranchTrueFalseTotal
if length $tsub > 10 and substr($tsub, -10, 10) eq "::AUTOLOAD" 0 0 0
Condition0%0/3
length $tsub > 10 and substr($tsub, -10, 10) eq "::AUTOLOAD"
!ll&&!rl&&r
000
MC/DC0%0/2
length $tsub > 10 and substr($tsub, -10, 10) eq "::AUTOLOAD"
length $tsub > 10 substr($tsub, -10, 10) eq "::AUTOLOAD"
Truth table0%0/3
A and B
A= length $tsub > 10B= substr($tsub, -10, 10) eq "::AUTOLOAD"
ABresult
0X0
100
111
228 if (( length($tsub) > 10) && (substr( $tsub, -10, 10 ) eq '::AUTOLOAD' ));
229 #
230 # now log the info in the slot; note that we rely on
231 # DB::DB to set the lineno
232 # !!!NOTE: we need to extract startlines of subs
233 #
234 0 $depth = Devel::RingBuffer::Ring::nextSlot($ringaddr, $tsub);
235 # print STDERR "In $$:$tid got depth of $depth\n"
236 # if $depth && (($depth < 0) && ($depth > 20));
237  
238 0 $subtrace = 1;
239 #
240 # call it in proper context, then unwind
241 #
242 0 ▶ if (wantarray) {
Branch: 2 branches
BranchTrueFalseTotal
if (wantarray) { } 0 0 0
elsif (defined wantarray) { } 0 0 0
243 0 @ret = &$sub;
244 }
245 elsif (defined wantarray) {
246 0 $ret = &$sub;
247 }
248 else {
249 0 ▶ &$sub;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
250 }
251  
252 0 ▶ if (defined($depth) && ($depth >= 0)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $depth and $depth >= 0) 0 0 0
Condition0%0/3
defined $depth and $depth >= 0
!ll&&!rl&&r
000
MC/DC0%0/2
defined $depth and $depth >= 0
defined $depth $depth >= 0
Truth table0%0/3
A and B
A= defined $depthB= $depth >= 0
ABresult
0X0
100
111
253 0 $subtrace = 0;
254 0 $depth = Devel::RingBuffer::Ring::freeSlot($ringaddr);
255 0 $subtrace = 1;
256 # print STDERR "*** $$:$tid return from $sub $depth\n";
257 0 ▶ _traceback() if ($depth < 0);
Branch: 1 branch
BranchTrueFalseTotal
if $depth < 0 0 0 0
258 }
259  
260 0 ▶ return (wantarray) ? @ret : defined(wantarray) ? $ret : undef;
Branch: 2 branches
BranchTrueFalseTotal
wantarray ? : 0 0 0
defined wantarray ? : 0 0 0
261 }
262  
263 sub _traceback {
264 0 ▶ my $i = 2;
Subroutine _traceback: not called CC=3 SCAR=24.8
265 0 while (1) {
266 0 my @t = caller($i++);
267 0 ▶ last unless $t[2] || $t[3];
Branch: 1 branch
BranchTrueFalseTotal
unless $t[2] or $t[3] 0 0 0
Condition0%0/3
$t[2] or $t[3]
l!l&&r!l&&!r
000
MC/DC0%0/2
$t[2] or $t[3]
$t[2] $t[3]
Truth table0%0/3
A or B
A= $t[2]B= $t[3]
ABresult
1X1
011
000
268 0 print STDERR "\t*** called from $t[3]:$t[2]\n";
269 }
270 }
271  
272 1;
↑ Summary lib/Devel/STrace/Monitor.pm »