| 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
| |||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||
| 20 | 3 | my $file = $args{file}; | |||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||
| 22 | 3 | ▶ | if (!$file and my $pid = $args{pid}) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not $file and my $pid = $args{"pid"}
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"}
| |||||||||||||||||||||||||||||||||||
| 23 | 2 | ▶ | if ($pid =~ /^\d+$/) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 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')
$args{"mnt"} or $ENV{"PERL_LINUX_PROC_MAPS_MOUNT"}
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')
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||
| 37 | 1 | my @regions; | |||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||
| 39 | 1 | ▶ | while (my $line = <$fh>) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 40 | 20 | chomp $line; | |||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||
| 42 | 20 | my $region = parse_maps_single_line($line); | |||||||||||||||||||||||||||||||||
| 43 | 20 | ▶ | next if !$region; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 54 | 0 | my %args = @_; | |||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||
| 56 | 0 | ▶ | ref $regions eq 'ARRAY' or croak 'Regions must be an arrayref'; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 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
Condition0%0/3
not $fh and my $file = $args{"file"}
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"}
| |||||||||||||||||||||||||||||||||||
| 67 | 0 | ▶ | open($fh, '>:encoding(UTF-8)', $file) or croak "Open failed ($file): $!"; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||||||||
| 69 | 0 | ▶ | print $fh $out if $fh; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 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 || ''
MC/DC100%1/1
$pathname || ''
$pathname
Truth table100%2/2
A
A= $pathname
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 114 | 0 | ▶ | $args[3] = $args[3] ? 'w' : '-'; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 115 | 0 | ▶ | $args[4] = $args[4] ? 'x' : '-'; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 116 | 0 | ▶ | $args[5] = $args[5] ? 's' : 'p'; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||||||||