| 1 | 4 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 2 | 4 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 3 | package Proc::Memory; | ||||||||||||||||||||
| 4 | |||||||||||||||||||||
| 5 | # ABSTRACT: Peek/Poke other processes' address spaces | ||||||||||||||||||||
| 6 | 4 | our $VERSION = '0.010'; # VERSION | |||||||||||||||||||
| 7 | |||||||||||||||||||||
| 8 | 4 | ▶ | use Carp; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | 4 | ▶ | use Sentinel; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | 4 | ▶ | use Scalar::Util 'looks_like_number'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 11 | 4 | ▶ | use Alien::libvas; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 12 | 4 | ▶ | use Inline 'C' => 'DATA' => | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 13 | 4 | enable => 'autowrap'; | |||||||||||||||||||
| 14 | 4 | ▶ | use Inline 0.56 with => 'Alien::libvas'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 15 | |||||||||||||||||||||
| 16 | =pod | ||||||||||||||||||||
| 17 | |||||||||||||||||||||
| 18 | =encoding utf8 | ||||||||||||||||||||
| 19 | |||||||||||||||||||||
| 20 | =head1 NAME | ||||||||||||||||||||
| 21 | |||||||||||||||||||||
| 22 | Proc::Memory - Peek/Poke into processes' address spaces | ||||||||||||||||||||
| 23 | |||||||||||||||||||||
| 24 | =head1 SYNOPSIS | ||||||||||||||||||||
| 25 | |||||||||||||||||||||
| 26 | use Proc::Memory; | ||||||||||||||||||||
| 27 | |||||||||||||||||||||
| 28 | my $mem = Proc::Memory->new(pid => $$); | ||||||||||||||||||||
| 29 | |||||||||||||||||||||
| 30 | my $byte = $mem->peek(0x1000); | ||||||||||||||||||||
| 31 | my $u32 = $mem->read(0x1000, 4); | ||||||||||||||||||||
| 32 | $mem->poke(0x1000, 'L') = 12; | ||||||||||||||||||||
| 33 | |||||||||||||||||||||
| 34 | |||||||||||||||||||||
| 35 | =head1 DESCRIPTION | ||||||||||||||||||||
| 36 | |||||||||||||||||||||
| 37 | PEEK/POKE are a BASIC programming language extension for reading and writing memory at a specified address across process boundaries. This module brings similiar capability to Perl. | ||||||||||||||||||||
| 38 | |||||||||||||||||||||
| 39 | Eventually, Memory searching capability will also be added. | ||||||||||||||||||||
| 40 | |||||||||||||||||||||
| 41 | =head1 IMPLEMENTATION | ||||||||||||||||||||
| 42 | |||||||||||||||||||||
| 43 | The module is a Perlish wrapper for L<Alien::libvas> and doesn't expose any extra functionality. L<libvas|http://github.com/a3f/libvas> claims support for following backends: | ||||||||||||||||||||
| 44 | |||||||||||||||||||||
| 45 | • win32 - Windows API's {Read,Write}ProcessMemory | ||||||||||||||||||||
| 46 | • mach - Mach Virtual Memory API (vm_copy) - macOS and GNU Hurd | ||||||||||||||||||||
| 47 | • process_vm - process_vm_{readv, writev} on Linux 3.2+ | ||||||||||||||||||||
| 48 | • procfs - /proc/$pid/mem on Linux and some BSDs, /proc/$pid/as on SunOS | ||||||||||||||||||||
| 49 | • ptrace - ptrace(2), available on many Unices | ||||||||||||||||||||
| 50 | • memcpy - Trivial implementation that doesn't supports foreign address spaces | ||||||||||||||||||||
| 51 | |||||||||||||||||||||
| 52 | Bug reports and contributions are welcome. :-) | ||||||||||||||||||||
| 53 | |||||||||||||||||||||
| 54 | =head1 METHODS AND ARGUMENTS | ||||||||||||||||||||
| 55 | |||||||||||||||||||||
| 56 | =over 4 | ||||||||||||||||||||
| 57 | |||||||||||||||||||||
| 58 | =item new(pid) | ||||||||||||||||||||
| 59 | |||||||||||||||||||||
| 60 | Constructs a new Proc::Memory instance. | ||||||||||||||||||||
| 61 | |||||||||||||||||||||
| 62 | =cut | ||||||||||||||||||||
| 63 | |||||||||||||||||||||
| 64 | 3 | ▶ | sub new { | ||||||||||||||||||
|
Subroutine
new: called CC=5 SCAR=18.3
Pod
documented
| |||||||||||||||||||||
| 65 | 3 | my $class = shift; | |||||||||||||||||||
| 66 | 3 | my @opts = @_; | |||||||||||||||||||
| 67 | 3 | ▶ | unshift @opts, 'pid' if @_ % 2 == 1; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 68 | |||||||||||||||||||||
| 69 | 3 | my $self = { | |||||||||||||||||||
| 70 | @opts | ||||||||||||||||||||
| 71 | }; | ||||||||||||||||||||
| 72 | |||||||||||||||||||||
| 73 | looks_like_number $self->{pid} | ||||||||||||||||||||
| 74 | 3 | ▶ | or croak q/Pid isn't numeric/; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 75 | |||||||||||||||||||||
| 76 | $self->{vas} = xs_vas_open($self->{pid}, 0) | ||||||||||||||||||||
| 77 | 3 | ▶ | or do { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 78 | 0 | ▶ | if (kill 0, $self->{pid}) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 79 | 0 | croak "PID doesn't exist" | |||||||||||||||||||
| 80 | } else { | ||||||||||||||||||||
| 81 | 0 | croak "Process access permission denied" | |||||||||||||||||||
| 82 | } | ||||||||||||||||||||
| 83 | }; | ||||||||||||||||||||
| 84 | |||||||||||||||||||||
| 85 | 3 | bless $self, $class; | |||||||||||||||||||
| 86 | 3 | return $self; | |||||||||||||||||||
| 87 | } | ||||||||||||||||||||
| 88 | |||||||||||||||||||||
| 89 | =item peek(addr [, 'pack-string']) | ||||||||||||||||||||
| 90 | |||||||||||||||||||||
| 91 | Peeks at the given memory address. C<pack-string> defaults to C<'C'> (A single byte) | ||||||||||||||||||||
| 92 | |||||||||||||||||||||
| 93 | =cut | ||||||||||||||||||||
| 94 | |||||||||||||||||||||
| 95 | 0 | ▶ | sub peek { | ||||||||||||||||||
|
Subroutine
peek: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||
| 96 | 0 | my $self = shift; | |||||||||||||||||||
| 97 | 0 | my $addr = shift; | |||||||||||||||||||
| 98 | 0 | ▶ | my $fmt = shift() // 'C'; | ||||||||||||||||||
|
Condition0%0/2
shift() // "C"
MC/DC0%0/1
shift() // "C"
shift()
Truth table0%0/2
A
A= shift()
| |||||||||||||||||||||
| 99 | 0 | ▶ | $fmt eq 'C' | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 100 | or croak 'Pack strings not supported yet'; | ||||||||||||||||||||
| 101 | |||||||||||||||||||||
| 102 | 0 | my $buf = xs_vas_read($self->{vas}, $addr, 1); # FIXME $addr shouldn't be an ulong | |||||||||||||||||||
| 103 | 0 | return $buf; | |||||||||||||||||||
| 104 | } | ||||||||||||||||||||
| 105 | |||||||||||||||||||||
| 106 | |||||||||||||||||||||
| 107 | |||||||||||||||||||||
| 108 | =item poke(addr [, 'pack-string']) = $value # or = ($a, $b) | ||||||||||||||||||||
| 109 | |||||||||||||||||||||
| 110 | Pokes a given memory address. If no pack-string is given, the rvalue is written as is | ||||||||||||||||||||
| 111 | |||||||||||||||||||||
| 112 | =cut | ||||||||||||||||||||
| 113 | |||||||||||||||||||||
| 114 | 0 | ▶ | sub get_poke { | ||||||||||||||||||
|
Subroutine
get_poke: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 115 | 0 | carp 'Useless use of poke'; | |||||||||||||||||||
| 116 | 0 | undef; | |||||||||||||||||||
| 117 | } | ||||||||||||||||||||
| 118 | 3 | ▶ | sub set_poke { | ||||||||||||||||||
|
Subroutine
set_poke: called CC=5 SCAR=16.2
Pod
undocumented
| |||||||||||||||||||||
| 119 | 3 | my @args = @{+shift}; | |||||||||||||||||||
| 120 | 3 | my $self = shift @args; | |||||||||||||||||||
| 121 | 3 | my $buf = shift; | |||||||||||||||||||
| 122 | 3 | ▶ | my $addr = shift @args or croak 'Address must be specified'; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 123 | 3 | ▶ | if (my $fmt = shift @args) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 124 | 2 | ▶ | $buf = pack($fmt, ref($buf) eq 'ARRAY' ? @{$buf} : $buf); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 125 | } | ||||||||||||||||||||
| 126 | |||||||||||||||||||||
| 127 | 3 | my $nbytes = xs_vas_write($self->{vas}, $addr, $buf, length $buf); | |||||||||||||||||||
| 128 | 3 | ▶ | return $nbytes >= 0 ? $nbytes : undef; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 129 | } | ||||||||||||||||||||
| 130 | |||||||||||||||||||||
| 131 | 3 | ▶ | sub poke :lvalue { | ||||||||||||||||||
|
Subroutine
poke: called CC=2 SCAR=7.2
Pod
documented
| |||||||||||||||||||||
| 132 | 3 | ▶ | defined wantarray or croak 'Useless use of poke'; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 133 | 3 | sentinel obj => [@_], get => \&get_poke, set => \&set_poke | |||||||||||||||||||
| 134 | } | ||||||||||||||||||||
| 135 | |||||||||||||||||||||
| 136 | =item read(addr, size) | ||||||||||||||||||||
| 137 | |||||||||||||||||||||
| 138 | Reads size bytes from given memory address. | ||||||||||||||||||||
| 139 | |||||||||||||||||||||
| 140 | =cut | ||||||||||||||||||||
| 141 | |||||||||||||||||||||
| 142 | #SV *xs_vas_read(void* vas, unsigned long src, size_t size) { | ||||||||||||||||||||
| 143 | 3 | ▶ | sub read { | ||||||||||||||||||
|
Subroutine
read: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 144 | 3 | my $self = shift; | |||||||||||||||||||
| 145 | 3 | my $addr = shift; | |||||||||||||||||||
| 146 | 3 | my $size = shift; | |||||||||||||||||||
| 147 | |||||||||||||||||||||
| 148 | 3 | my $buf = xs_vas_read($self->{vas}, $addr, $size); | |||||||||||||||||||
| 149 | 3 | return $buf; | |||||||||||||||||||
| 150 | } | ||||||||||||||||||||
| 151 | |||||||||||||||||||||
| 152 | =item write(addr, buf [, count]) | ||||||||||||||||||||
| 153 | |||||||||||||||||||||
| 154 | Writes C<buf> to C<addr> | ||||||||||||||||||||
| 155 | |||||||||||||||||||||
| 156 | =cut | ||||||||||||||||||||
| 157 | |||||||||||||||||||||
| 158 | #int xs_vas_write(void* vas, unsigned long dst, SV *sv) { | ||||||||||||||||||||
| 159 | 1 | ▶ | sub write { | ||||||||||||||||||
|
Subroutine
write: called CC=3 SCAR=11.6
Pod
documented
| |||||||||||||||||||||
| 160 | 1 | my $self = shift; | |||||||||||||||||||
| 161 | 1 | my $addr = shift; | |||||||||||||||||||
| 162 | 1 | my $buf = shift; | |||||||||||||||||||
| 163 | 1 | ▶ | my $bytes = shift || length $buf; | ||||||||||||||||||
|
Condition33%1/3
shift() || length $buf
MC/DC0%0/2
shift() || length $buf
shift()
length $buf
Truth table33%1/3
A || B
A= shift()B= length $buf
| |||||||||||||||||||||
| 164 | |||||||||||||||||||||
| 165 | 1 | my $nbytes = xs_vas_write($self->{vas}, $addr, $buf, $bytes); | |||||||||||||||||||
| 166 | 1 | ▶ | return $nbytes >= 0 ? $nbytes : undef; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 167 | } | ||||||||||||||||||||
| 168 | |||||||||||||||||||||
| 169 | =item tie(addr, 'pack-string') | ||||||||||||||||||||
| 170 | |||||||||||||||||||||
| 171 | Returns a tied variable which can be used like any other variable. | ||||||||||||||||||||
| 172 | To be implemented | ||||||||||||||||||||
| 173 | |||||||||||||||||||||
| 174 | =cut | ||||||||||||||||||||
| 175 | |||||||||||||||||||||
| 176 | =item search('pack-string') | ||||||||||||||||||||
| 177 | |||||||||||||||||||||
| 178 | To be implemented when libvas provides it | ||||||||||||||||||||
| 179 | |||||||||||||||||||||
| 180 | =cut | ||||||||||||||||||||
| 181 | |||||||||||||||||||||
| 182 | |||||||||||||||||||||
| 183 | |||||||||||||||||||||
| 184 | 3 | ▶ | sub DESTROY { | ||||||||||||||||||
|
Subroutine
DESTROY: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 185 | 3 | my $self = shift; | |||||||||||||||||||
| 186 | 3 | xs_vas_close($self->{vas}); | |||||||||||||||||||
| 187 | } | ||||||||||||||||||||
| 188 | |||||||||||||||||||||
| 189 | 4 | Inline->init(); | |||||||||||||||||||
| 190 | 4 | 1; | |||||||||||||||||||
| 191 | |||||||||||||||||||||
| 192 | =back | ||||||||||||||||||||
| 193 | |||||||||||||||||||||
| 194 | =head1 GIT REPOSITORY | ||||||||||||||||||||
| 195 | |||||||||||||||||||||
| 196 | L<http://github.com/athreef/Proc-Memory> | ||||||||||||||||||||
| 197 | |||||||||||||||||||||
| 198 | =head1 SEE ALSO | ||||||||||||||||||||
| 199 | |||||||||||||||||||||
| 200 | L<libvas|http://github.com/a3f/libvas> | ||||||||||||||||||||
| 201 | L<Alien::libvas> | ||||||||||||||||||||
| 202 | |||||||||||||||||||||
| 203 | =head1 AUTHOR | ||||||||||||||||||||
| 204 | |||||||||||||||||||||
| 205 | Ahmad Fatoum C<< <athreef@cpan.org> >>, L<http://a3f.at> | ||||||||||||||||||||
| 206 | |||||||||||||||||||||
| 207 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||
| 208 | |||||||||||||||||||||
| 209 | Copyright (C) 2016 Ahmad Fatoum | ||||||||||||||||||||
| 210 | |||||||||||||||||||||
| 211 | This library is free software; you can redistribute it and/or modify | ||||||||||||||||||||
| 212 | it under the same terms as Perl itself. | ||||||||||||||||||||
| 213 | |||||||||||||||||||||
| 214 | =cut | ||||||||||||||||||||
| 215 | |||||||||||||||||||||
| 216 | __DATA__ | ||||||||||||||||||||