lib/Devel/STrace/Monitor.pm

filter: Statementstmt 47.0% 48 / 102 Branchbran 26.4% 9 / 34 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 50.0% 7 / 14 Podpod 0.0% 0 / 10 totaltotal 35.5% 64 / 180 CC 39 SCAR 35.0
CC
39
Cov
39%
setSignal
56.0
getSignal
56.0
setTrace
56.0
CRAP 33.2
« lib/Devel/STrace.pm ↑ Summary lib/Devel/plstrace.pl »
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.
9 # <p>
10 # Permission is granted to use this software under the same terms as Perl itself.
11 # Refer to the <a href='http://perldoc.perl.org/perlartistic.html'>Perl Artistic License</a>
12 # for details.
13 #
14 # @author D. Arnold
15 # @since 2006-05-01
16 # @see <a href='http://search.cpan.org/perldoc?Devel::RingBuffer'>Devel::RingBuffer</a>
17 # @see <a href='http://search.cpan.org/perldoc?IPC::Mmap'>IPC::Mmap</a>
18 # @see <a href='http://search.cpan.org/perldoc?Devel::STrace'>Devel::STrace</a>
19 # @see <a href='http://perldoc.perl.org/perldebguts.html'>perdebguts</a>
20 # @self $self
21 #*/
22 package Devel::STrace::Monitor;
23  
24 1 require 5.008;
25 1 ▶use Devel::RingBuffer;
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 1 our $VERSION = '0.30';
28  
29 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
31  
32 #/**
33 # Constructor. Opens the specified filename, or,
34 # if no filename is specified, the filename specified by
35 # the DEVEL_RINGBUF_FILE environment variable, using
36 # <a href='http://search.cpan.org/perldoc?Devel::RingBuffer'>Devel::RingBuffer</a>.
37 # Performs an initial scan of the file to create a PID/TID buffer map.
38 #
39 # @static
40 # @param $file name of the mmap()'d file (or namespace on Win32)
41 # @return on success, a new Devel::STrace::Monitor object;
42 # undef on failure.
43 #*/
44 1 ▶sub open {
Subroutine open: called CC=3 SCAR=11.3
Pod undocumented
45 1 my ($class, $file) = @_;
46  
47 $file = $ENV{DEVEL_RINGBUF_FILE}
48 1 ▶ unless $file;
Branch: 1 branch
BranchTrueFalseTotal
unless $file 0 1 1
49  
50 #print $file, "\n";
51 1 ▶ my $ringbuffer = Devel::RingBuffer->monitor($file)
Branch: 1 branch
BranchTrueFalseTotal
my $ringbuffer = "Devel::RingBuffer"->monitor($file) or return undef 1 0 1
52 or return undef;
53  
54 1 my $self = bless {
55 _ring => $ringbuffer,
56 _filename => $file,
57 _map => {},
58 _slots => $ringbuffer->getSlots()
59 }, $class;
60 #
61 # load the map
62 #
63 # my @headers = $ringbuffer->getHeader();
64 # print "header is
65 # single: $headers[0]
66 # msgarea_sz: $headers[1]
67 # max_buffers: $headers[2]
68 # slots: $headers[3]
69 # slot_sz: $headers[4]
70 # stop_on_create: $headers[5]
71 # trace_on_create: $headers[6]
72 # global_sz: $headers[7]
73 # globmsg_total: $headers[8]
74 # globmsg_sz: $headers[9]
75 # ";
76  
77 1 return $self->refresh();
78 }
79  
80 #/**
81 # Refresh the PID/TID buffer map.
82 # Scans the mmap'ed file to refresh the PID/TID buffer map.
83 # (in order to collect buffers for new threads/processes, or to discard
84 # old buffers for threads/processes which have terminated)
85 #
86 # @return the Devel::STrace::Monitor object
87 #*/
88 4 ▶sub refresh {
Subroutine refresh: called CC=3 SCAR=11.0
Pod undocumented
89 4 my $self = shift;
90  
91 4 my @bufmap = $self->{_ring}->getMap();
92 4 my $map = $self->{_map} = {};
93 #
94 # optimization: only inspect buffers that are alloc'd
95 #
96 4 my ($pid, $tid, $current, $depth);
97 4 foreach (0..$#bufmap) {
98 next
99 80 ▶ if $bufmap[$_];
Branch: 1 branch
BranchTrueFalseTotal
if $bufmap[$_] 44 36 80
100  
101 36 my $ring = $self->{_ring}->getRing($_);
102 36 ($pid, $tid, $current, $depth) = $ring->getHeader();
103 36 $map->{"$pid:$tid"} = $ring;
104 }
105 4 return $self;
106 }
107  
108 #/**
109 # Dump the mmap'ed ringbuffer file contents.
110 # Scans the mmap'ed file to refresh the PID/TID buffer map.
111 # (in order to collect buffers for new threads/processes, or to discard
112 # old buffers for threads/processes which have terminated)
113 #
114 # @param $trace_cb callback to which ringbuffer contents are posted
115 # @param @pid_tid_list optional list of PID's, or "PID:TID" keys
116 # for which ringbuffer contents are to be returned;
117 # if none are specified, all PID/TID keys are used;
118 # if only a PID is specified, all threads for the process
119 # are used.
120 #
121 # @return the Devel::STrace::Monitor object
122 #*/
123 3 ▶sub trace {
Subroutine trace: called CC=7 SCAR=27.3
Pod undocumented
124 3 my $self = shift;
125 3 my $trace_cb = shift;
126 #
127 # if pids or pid:tid's provided, return them
128 #
129 3 my @keys = sort keys %{$self->{_map}};
130 3 ▶ if (scalar @_) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_) 0 3 3
131 0 foreach my $pid (@_) {
132 #
133 # if full key, get it
134 #
135 $self->_get_trace($pid, $trace_cb),
136 next
137 0 ▶ if exists $self->{_map}{$pid};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"_map"}{$pid} 0 0 0
138 #
139 # else scan for all matching pids
140 #
141 0 foreach (@keys) {
142 0 ▶ $self->_get_trace($_, $trace_cb)
Branch: 1 branch
BranchTrueFalseTotal
if /^$pid:/ 0 0 0
143 if /^$pid:/;
144 }
145 }
146 0 return $self;
147 }
148 #
149 # else dump everything
150 #
151 $self->_get_trace($_, $trace_cb)
152 3 foreach (@keys);
153  
154 3 return $self;
155 }
156  
157 27 ▶sub _get_trace {
Subroutine _get_trace: called CC=5 SCAR=16.1
158 27 my ($self, $key, $cb) = @_;
159  
160 27 my $ring = $self->{_map}{$key};
161 27 ▶ return undef unless $ring;
Branch: 1 branch
BranchTrueFalseTotal
unless $ring 0 27 27
162  
163 27 my ($pid, $tid, $current, $depth) = $ring->getHeader();
164  
165 27 my $slot;
166 27 my $slots = $self->{_slots};
167 27 my ($trace, $line, $time);
168  
169 27 ▶ $slots = $depth if ($depth < $slots);
Branch: 1 branch
BranchTrueFalseTotal
if $depth < $slots 27 0 27
170 27 foreach (1..$slots) {
171 104 ($line, $time, $trace) = $ring->getSlot($current);
172 104 &$cb($key, $current, $depth, $line, $time, $trace);
173 104 $current--;
174 104 ▶ $current = $slots - 1 if ($current < 0);
Branch: 1 branch
BranchTrueFalseTotal
if $current < 0 27 77 104
175 }
176  
177 27 return $self;
178 }
179  
180 #/**
181 # Set the current ringbuffer global single
182 # control variable value. Setting this to a non-zero
183 # value causes Devel::STrace to trace data for all threads
184 # of all processes; setting it to zero <i>may</i> disable
185 # tracing, <i>but only</i> if the per-thread trace and signal
186 # control variables are also set to zero.
187 #
188 # @param $value new value to assign to single
189 #
190 # @return the prior value of the Devel::RingBuffer global single value
191 #*/
192 0 ▶sub setSingle {
Subroutine setSingle: not called CC=1 SCAR=6.9
Pod undocumented
193 0 my $single = $_[0]->{_ring}->getSingle();
194 0 $_[0]->{_ring}->setSingle($_[1]);
195 0 return $single;
196 }
197  
198 #/**
199 # Get the current ringbuffer global single
200 # control variable value.
201 #
202 # @return the current Devel::RingBuffer global single value
203 #*/
204 0 ▶sub getSingle {
Subroutine getSingle: not called CC=1 SCAR=6.9
Pod undocumented
205 0 return $_[0]->{_ring}->getSingle();
206 }
207 #/**
208 # Set the ringbuffer per-thread signal
209 # control variable value for the specified PID or PID:TID.
210 # Setting this to a non-zero
211 # value causes Devel::STrace to trace data for the specified threads
212 # of the specified processes; setting it to zero <i>may</i> disable
213 # tracing, <i>but only</i> if the global single variable, and the
214 # per-thread trace control variables are also set to zero.
215 #
216 # @param @pid_tid_list optional list of PIDs, or "PID:TID", keys to set signal on;
217 # if no keys are specified, all keys are used
218 # @param $value new value to assign to signal
219 #
220 # @return a hash of the prior values of the Devel::RingBuffer signal values, keyed
221 # by the "PID:TID"
222 #*/
223 0 ▶sub setSignal {
Subroutine setSignal: not called CC=7 SCAR=40.3
Pod undocumented
224 0 my $self = shift;
225 0 my $value = pop;
226 0 my %pidtids = ();
227 0 ▶ if (scalar @_) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_) { } 0 0 0
228 0 foreach my $pidtid (keys %{$self->{_map}}) {
229 0 foreach (@_) {
230 $pidtids{$_} = $self->{_map}{$_}->getSignal(),
231 0 ▶ $self->{_map}{$_}->setSignal($value)
Branch: 1 branch
BranchTrueFalseTotal
if $_ eq $pidtid or substr($_, 0, length($pidtid) + 1) eq "${pidtid}:" 0 0 0
Condition0%0/3
$_ eq $pidtid or substr($_, 0, length($pidtid) + 1) eq "${pidtid}:"
l!l&&r!l&&!r
000
MC/DC0%0/2
$_ eq $pidtid or substr($_, 0, length($pidtid) + 1) eq "${pidtid}:"
$_ eq $pidtid substr($_, 0, length($pidtid) + 1) eq "${pidtid}:"
Truth table0%0/3
A or B
A= $_ eq $pidtidB= substr($_, 0, length($pidtid) + 1) eq "${pidtid}:"
ABresult
1X1
011
000
232 if ($_ eq $pidtid) ||
233 (substr($_, 0, length($pidtid) + 1) eq "$pidtid:");
234 }
235 }
236 }
237 else {
238 $pidtids{$_} = $self->{_map}{$_}->getSignal(),
239 $self->{_map}{$_}->setSignal($value)
240 0 foreach (keys %{$self->{_map}});
241 }
242 0 return %pidtids;
243 }
244  
245 #/**
246 # Get the ringbuffer per-thread signal
247 # control variable value for the specified PIDs or PID:TIDs.
248 #
249 # @param @pid_tid_list optional list of PIDs, or "PID:TID", keys to get signal for;
250 # if no keys are specified, all keys are used
251 #
252 # @return a hash of the Devel::RingBuffer signal values, keyed
253 # by the "PID:TID"
254 #*/
255 0 ▶sub getSignal {
Subroutine getSignal: not called CC=7 SCAR=40.3
Pod undocumented
256 0 my $self = shift;
257 0 my %pidtids = ();
258 0 ▶ if (scalar @_) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_) { } 0 0 0
259 0 foreach my $pidtid (keys %{$self->{_map}}) {
260 0 foreach (@_) {
261 0 ▶ $pidtids{$_} = $self->{_map}{$_}->getSignal()
Branch: 1 branch
BranchTrueFalseTotal
if $_ eq $pidtid or substr($_, 0, length($pidtid) + 1) eq "${pidtid}:" 0 0 0
Condition0%0/3
$_ eq $pidtid or substr($_, 0, length($pidtid) + 1) eq "${pidtid}:"
l!l&&r!l&&!r
000
MC/DC0%0/2
$_ eq $pidtid or substr($_, 0, length($pidtid) + 1) eq "${pidtid}:"
$_ eq $pidtid substr($_, 0, length($pidtid) + 1) eq "${pidtid}:"
Truth table0%0/3
A or B
A= $_ eq $pidtidB= substr($_, 0, length($pidtid) + 1) eq "${pidtid}:"
ABresult
1X1
011
000
262 if ($_ eq $pidtid) ||
263 (substr($_, 0, length($pidtid) + 1) eq "$pidtid:");
264 }
265 }
266 }
267 else {
268 $pidtids{$_} = $self->{_map}{$_}->getSignal()
269 0 foreach (keys %{$self->{_map}});
270 }
271 0 return %pidtids;
272 }
273  
274 #/**
275 # Set the ringbuffer per-thread trace
276 # control variable value for the specified PID or PID:TID.
277 # Setting this to a non-zero
278 # value causes Devel::STrace to trace data for the specified threads
279 # of the specified processes; setting it to zero <i>may</i> disable
280 # tracing, <i>but only</i> if the global single variable, and the
281 # per-thread signal control variables are also set to zero.
282 #
283 # @param @pid_tid_list optional list of PIDs, or "PID:TID", keys to set trace on;
284 # if no keys are specified, all keys are used
285 # @param $value new value to assign to trace
286 #
287 # @return a hash of the prior values of the Devel::RingBuffer trace values, keyed
288 # by the "PID:TID"
289 #*/
290 0 ▶sub setTrace {
Subroutine setTrace: not called CC=7 SCAR=40.3
Pod undocumented
291 0 my $self = shift;
292 0 my $value = pop;
293 0 my %pidtids = ();
294 0 ▶ if (scalar @_) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_) { } 0 0 0
295 0 foreach my $pidtid (keys %{$self->{_map}}) {
296 0 foreach (@_) {
297 $pidtids{$_} = $self->{_map}{$_}->getTrace(),
298 0 ▶ $self->{_map}{$_}->setTrace($value)
Branch: 1 branch
BranchTrueFalseTotal
if $_ eq $pidtid or substr($_, 0, length($pidtid) + 1) eq "${pidtid}:" 0 0 0
Condition0%0/3
$_ eq $pidtid or substr($_, 0, length($pidtid) + 1) eq "${pidtid}:"
l!l&&r!l&&!r
000
MC/DC0%0/2
$_ eq $pidtid or substr($_, 0, length($pidtid) + 1) eq "${pidtid}:"
$_ eq $pidtid substr($_, 0, length($pidtid) + 1) eq "${pidtid}:"
Truth table0%0/3
A or B
A= $_ eq $pidtidB= substr($_, 0, length($pidtid) + 1) eq "${pidtid}:"
ABresult
1X1
011
000
299 if ($_ eq $pidtid) ||
300 (substr($_, 0, length($pidtid) + 1) eq "$pidtid:");
301 }
302 }
303 }
304 else {
305 $pidtids{$_} = $self->{_map}{$_}->getTrace(),
306 $self->{_map}{$_}->setTrace($value)
307 0 foreach (keys %{$self->{_map}});
308 }
309 0 return %pidtids;
310 }
311  
312 #/**
313 # Get the ringbuffer per-thread trace
314 # control variable value for the specified PIDs or PID:TIDs.
315 #
316 # @param @pid_tid_list optional list of PIDs, or "PID:TID", keys to get trace for;
317 # if no keys are specified, all keys are used
318 #
319 # @return a hash of the Devel::RingBuffer trace values, keyed
320 # by the "PID:TID"
321 #*/
322 0 ▶sub getTrace {
Subroutine getTrace: not called CC=7 SCAR=40.3
Pod undocumented
323 0 my $self = shift;
324 0 my %pidtids = ();
325 0 ▶ if (scalar @_) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_) { } 0 0 0
326 0 foreach my $pidtid (keys %{$self->{_map}}) {
327 0 foreach (@_) {
328 0 ▶ $pidtids{$_} = $self->{_map}{$_}->getTrace()
Branch: 1 branch
BranchTrueFalseTotal
if $_ eq $pidtid or substr($_, 0, length($pidtid) + 1) eq "${pidtid}:" 0 0 0
Condition0%0/3
$_ eq $pidtid or substr($_, 0, length($pidtid) + 1) eq "${pidtid}:"
l!l&&r!l&&!r
000
MC/DC0%0/2
$_ eq $pidtid or substr($_, 0, length($pidtid) + 1) eq "${pidtid}:"
$_ eq $pidtid substr($_, 0, length($pidtid) + 1) eq "${pidtid}:"
Truth table0%0/3
A or B
A= $_ eq $pidtidB= substr($_, 0, length($pidtid) + 1) eq "${pidtid}:"
ABresult
1X1
011
000
329 if ($_ eq $pidtid) ||
330 (substr($_, 0, length($pidtid) + 1) eq "$pidtid:");
331 }
332 }
333 }
334 else {
335 $pidtids{$_} = $self->{_map}{$_}->getTrace()
336 0 foreach (keys %{$self->{_map}});
337 }
338 0 return %pidtids;
339 }
340  
341  
342 #/**
343 # Get the current list of PID:TID keys.
344 #
345 # @return a list of currently active PID:TID keys from the Devel::RingBuffer
346 #*/
347 0 ▶sub getPIDTIDs {
Subroutine getPIDTIDs: not called CC=1 SCAR=6.9
Pod undocumented
348 0 my $self = shift;
349 0 return sort keys %{$self->{_map}};
350 }
351  
352 1 1;
« lib/Devel/STrace.pm ↑ Summary lib/Devel/plstrace.pl »