blib/lib/Linux/Proc/Maps.pm

filter: Statementstmt 66.1% 43 / 65 Branchbran 39.4% 15 / 38 Conditioncond 28.5% 4 / 14 MC/DCmcdc 25.0% 2 / 8 Subroutinesub 81.8% 9 / 11 Podpod 100% 4 / 4 totaltotal 55.0% 77 / 140 SCAR 45.7
CC
26
Cov
53%
write_maps
72.0
format_maps_single_line
42.0
read_maps
13.6
CRAP 96.2
↑ Summary
1 package Linux::Proc::Maps;
2 # ABSTRACT: Read and write /proc/[pid]/maps files
3 # KEYWORDS: linux proc procfs
4  
5 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 our $VERSION = '0.001'; # VERSION
9  
10 2 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use namespace::clean -except => [qw(import)];
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 2 our @EXPORT_OK = qw(read_maps write_maps parse_maps_single_line format_maps_single_line);
15  
16  
17 sub read_maps {
18 3 ▶ my %args = @_ == 1 ? (pid => $_[0]) : @_;
Subroutine read_maps: called CC=11 SCAR=26.1
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 2 1 3
19  
20 3 my $file = $args{file};
21  
22 3 ▶ if (!$file and my $pid = $args{pid}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $file and my $pid = $args{"pid"}) 2 1 3
Condition66%2/3
not $file and my $pid = $args{"pid"}
!ll&&!rl&&r
012
MC/DC50%1/2
not $file and my $pid = $args{"pid"}
not $file my $pid = $args{"pid"}
Truth table66%2/3
not(A) and B
A= not $fileB= my $pid = $args{"pid"}
ABresult
0X0
100
111
23 2 ▶ if ($pid =~ /^\d+$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pid =~ /^\d+$/) { } 0 2 2
24 0 require File::Spec::Functions;
25 my $procfs = $args{mnt} || $ENV{PERL_LINUX_PROC_MAPS_MOUNT} ||
26 0 ▶ File::Spec::Functions::catdir(File::Spec::Functions::rootdir(), 'proc');
Condition0%0/6
$args{'mnt'} || $ENV{'PERL_LINUX_PROC_MAPS_MOUNT'} || File::Spec::Functions::catdir(File::Spec::Functions::rootdir(), 'proc')
l!l&&r!l&&!r
000
$args{"mnt"} or $ENV{"PERL_LINUX_PROC_MAPS_MOUNT"}
l!l&&r!l&&!r
000
MC/DC0%0/3
$args{'mnt'} || $ENV{'PERL_LINUX_PROC_MAPS_MOUNT'} || File::Spec::Functions::catdir(File::Spec::Functions::rootdir(), 'proc')
$args{"mnt"} $ENV{"PERL_LINUX_PROC_MAPS_MOUNT"} File::Spec::Functions::catdir(File::Spec::Functions::rootdir(), 'proc')
Truth table0%0/4
A or B || C
A= $args{"mnt"}B= $ENV{"PERL_LINUX_PROC_MAPS_MOUNT"}C= File::Spec::Functions::catdir(File::Spec::Functions::rootdir(), 'proc')
ABCresult
1XX1
01X1
0011
0000
27 0 $file = File::Spec::Functions::catfile($procfs, $pid, 'maps');
28 }
29 else {
30 2 $file = $args{pid};
31 }
32 }
33  
34 3 ▶ $file or croak 'Filename or PID required';
Branch: 1 branch
BranchTrueFalseTotal
$file or croak("Filename or PID required") 1 2 3
35 2 ▶ open(my $fh, '<:encoding(UTF-8)', $file) or croak "Open failed ($file): $!";
Subroutine BEGIN: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<:encoding(UTF-8)", $file or croak("Open failed ($file): $!") 1 1 2
36  
37 1 my @regions;
38  
39 1 ▶ while (my $line = <$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $fh)) 20 1 21
40 20 chomp $line;
41  
42 20 my $region = parse_maps_single_line($line);
43 20 ▶ next if !$region;
Branch: 1 branch
BranchTrueFalseTotal
unless $region 0 20 20
44  
45 20 push @regions, $region;
46 }
47  
48 1 return \@regions;
49 }
50  
51  
52 sub write_maps {
53 0 ▶ my $regions = shift or croak 'Regions required';
Subroutine write_maps: not called CC=8 SCAR=42.8
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
my $regions = shift() or croak("Regions required") 0 0 0
54 0 my %args = @_;
55  
56 0 ▶ ref $regions eq 'ARRAY' or croak 'Regions must be an arrayref';
Branch: 1 branch
BranchTrueFalseTotal
ref $regions eq "ARRAY" or croak("Regions must be an arrayref") 0 0 0
57  
58 0 my $out = '';
59  
60 0 for my $region (@$regions) {
61 0 $out .= format_maps_single_line($region);
62 }
63  
64 # maybe print out the memory regions to a filehandle
65 0 my $fh = $args{fh};
66 0 ▶ if (!$fh and my $file = $args{file}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $fh and my $file = $args{"file"}) 0 0 0
Condition0%0/3
not $fh and my $file = $args{"file"}
!ll&&!rl&&r
000
MC/DC0%0/2
not $fh and my $file = $args{"file"}
not $fh my $file = $args{"file"}
Truth table0%0/3
not(A) and B
A= not $fhB= my $file = $args{"file"}
ABresult
0X0
100
111
67 0 ▶ open($fh, '>:encoding(UTF-8)', $file) or croak "Open failed ($file): $!";
Branch: 1 branch
BranchTrueFalseTotal
open $fh, ">:encoding(UTF-8)", $file or croak("Open failed ($file): $!") 0 0 0
68 }
69 0 ▶ print $fh $out if $fh;
Branch: 1 branch
BranchTrueFalseTotal
if $fh 0 0 0
70  
71 0 return $out;
72 }
73  
74  
75 sub parse_maps_single_line {
76 24 ▶ my $line = shift or croak 'Line from a maps file required';
Subroutine parse_maps_single_line: called CC=4 SCAR=13.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
my $line = shift() or croak("Line from a maps file required") 0 24 24
77  
78 24 chomp $line;
79  
80 24 my ($addr1, $addr2, $read, $write, $exec, $shared, $offset, $device, $inode, $pathname) = $line =~ m{
81 ^
82 ([[:xdigit:]]+)-([[:xdigit:]]+)
83 \s+ ([r-])([w-])([x-])([sp])
84 \s+ ([[:xdigit:]]+)
85 \s+ ([[:xdigit:]]+:[[:xdigit:]]+)
86 \s+ (\d+)
87 (?: \s+ (.*))?
88 }x;
89  
90 24 ▶ return if !$addr1;
Branch: 1 branch
BranchTrueFalseTotal
unless $addr1 1 23 24
91  
92 2 ▶ no warnings 'portable'; # for hex() on 64-bit perls
Subroutine BEGIN: called CC=1 SCAR=0.0
93  
94 return {
95 23 ▶ address_start => hex($addr1),
Condition100%2/2
$pathname || ''
l!l
185
MC/DC100%1/1
$pathname || ''
$pathname
Truth table100%2/2
A
A= $pathname
Aresult
11
00
96 address_end => hex($addr2),
97 read => 'r' eq $read,
98 write => 'w' eq $write,
99 execute => 'x' eq $exec,
100 shared => 's' eq $shared,
101 offset => hex($offset),
102 device => $device,
103 inode => $inode,
104 pathname => $pathname || '',
105 };
106 }
107  
108  
109 sub format_maps_single_line {
110 0 ▶ my $region = shift or croak 'Region required';
Subroutine format_maps_single_line: not called CC=6 SCAR=37.4
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
my $region = shift() or croak("Region required") 0 0 0
111  
112 0 my @args = @{$region}{qw(address_start address_end read write execute shared offset device inode)};
113 0 ▶ $args[2] = $args[2] ? 'r' : '-';
Branch: 1 branch
BranchTrueFalseTotal
$args[2] ? : 0 0 0
114 0 ▶ $args[3] = $args[3] ? 'w' : '-';
Branch: 1 branch
BranchTrueFalseTotal
$args[3] ? : 0 0 0
115 0 ▶ $args[4] = $args[4] ? 'x' : '-';
Branch: 1 branch
BranchTrueFalseTotal
$args[4] ? : 0 0 0
116 0 ▶ $args[5] = $args[5] ? 's' : 'p';
Branch: 1 branch
BranchTrueFalseTotal
$args[5] ? : 0 0 0
117  
118 0 return sprintf("%-72s %s\n", sprintf("%x-%x %s%s%s%s %08x %s %d", @args), $region->{pathname});
119 }
120  
121  
122 2 1;
123  
124 __END__
↑ Summary