lib/Devel/plstrace.pl untested

filter: Statementstmt 0.0% 0 / 35 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 11 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 0.0% 0 / 2 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 73 CC 1 SCAR 6.9
CC
1
Cov
0%
usage
2.0
BEGIN
2.0
CRAP 2.0
1 #!/usr/local/bin/perl
2 #
3 # open the mmap'ed file
4 #
5 use Devel::STrace::Monitor;
6 #
7 # process args
8 #
9 my $interval = 0;
10 my $duration = 0;
11 my $file = $ENV{DEVEL_RINGBUF_FILE};
12  
13 while (@ARGV && (substr($ARGV[0], 0, 1) eq '-')) {
14 usage()
15 if ($ARGV[0] eq '-h');
16  
17 shift @ARGV,
18 $interval = shift @ARGV,
19 next
20 if ($ARGV[0] eq '-s');
21  
22 shift @ARGV,
23 $duration = shift @ARGV,
24 next
25 if ($ARGV[0] eq '-d');
26  
27 shift @ARGV,
28 $file = shift @ARGV,
29 next
30 if ($ARGV[0] eq '-f');
31 }
32  
33 die "No trace file specified."
34 unless $file;
35  
36 my $view = Devel::STrace::Monitor->open($file)
37 || die $@;
38  
39 print STDERR "Started $file\n";
40 my ($started, $lastrefresh) = (time(), time());
41 while (1) {
42 print "
43 ***********************************************
44 *** At ", scalar localtime(), "
45 ";
46 my $lastkey = '';
47 #
48 # dump the current traces (for specified pid[:tid] if provided)
49 #
50 $view->refresh();
51 $view->trace(
52 sub {
53 my ($key, $slot, $depth, $line, $time, $entry) = @_;
54 $lastkey = $key,
55 print "\n**************************************\n"
56 unless ($lastkey eq $key);
57 if ($time) {
58 my $frac = ($time - int($time)) * 1000000;
59 $frac=~s/\..*$//;
60 my @parts = split(/\s+/, scalar localtime($time));
61 pop @parts; # get rid of year
62 print "$key($depth) : $slot : $entry:$line at ",
63 join(' ', @parts), '.', $frac, "\n";
64 }
65 else {
66 print "$key($depth) : $slot : $entry:$line (No timestamp)\n";
67 }
68 },
69 @ARGV
70 );
71  
72 exit unless $interval && $duration && (time() - $started < $duration);
73 #
74 # refresh the internal map every 5 secs
75 #
76 $view->refresh(),
77 $lastrefresh = time()
78 if (time() - $lastrefresh > 5);
79  
80 sleep $interval;
81 }
82  
83 sub usage {
84 print "
85 plstrace.pl [ -h ] [ -s <interval> ] [ -f filename ] [ pidlist... ]
86  
87 Displays the current runtime stack of a Perl application.
88 pidlist is a list of either pid:tid (to trace a specific thread),
89 or just pid (to trace all threads in the process) descriptors.
90  
91 Options:
92 -h : this usage text
93 -s <interval> : sample interval; plstrace will sample the trace
94 file every <interval> seconds; 0 (the default) means sample once
95 -d <duration> : number of secs to run; 0 (the default) means sample just once
96 -f <filename> : name of trace file; if not specified, uses DEVEL_RINGBUF_FILE
97 environment variable
98 ";
99 exit;
100 }