Class: MemoryIO::Process
- Inherits:
-
Object
- Object
- MemoryIO::Process
- Defined in:
- lib/memory_io/process.rb
Overview
Records information of a process.
Instance Attribute Summary collapse
- #perm ⇒ #readable?, #writable? readonly private
Instance Method Summary collapse
-
#bases ⇒ {Symbol => Integer}
Parse
/proc/[pid]/maps
to get all bases. -
#initialize(pid) ⇒ Process
constructor
private
Create process object from pid.
-
#read(addr, num_elements, **options) ⇒ String, ...
Read from process’s memory.
-
#write(addr, objects, **options) ⇒ void
Write objects at
addr
.
Constructor Details
#initialize(pid) ⇒ Process
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
TODO:
Support MacOS
TODO:
Support Windows
Note:
This class only supports procfs-based system. i.e. /proc is mounted and readable.
Create process object from pid.
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/memory_io/process.rb', line 24 def initialize(pid) @pid = pid @mem = "/proc/#{pid}/mem" # check permission of '/proc/pid/mem' @perm = MemoryIO::Util.(@mem) # TODO: raise custom exception raise Errno::ENOENT, @mem if perm.nil? # FIXME: use logger warn(<<-EOS.strip) unless perm.readable? || perm.writable? You have no permission to read/write this process. Check the setting of /proc/sys/kernel/yama/ptrace_scope, or try again as the root user. To enable attach another process, do: $ echo 0 | sudo tee /proc/sys/kernel/yama/ptrace_scope EOS end |
Instance Attribute Details
#perm ⇒ #readable?, #writable? (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
8 9 10 |
# File 'lib/memory_io/process.rb', line 8 def perm @perm end |
Instance Method Details
#bases ⇒ {Symbol => Integer}
Parse /proc/[pid]/maps
to get all bases.
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/memory_io/process.rb', line 62 def bases file = "/proc/#{@pid}/maps" stat = MemoryIO::Util.(file) return {} unless stat&.readable? maps = ::IO.binread(file).split("\n").map do |line| # 7f76515cf000-7f76515da000 r-xp 00000000 fd:01 29360257 /lib/x86_64-linux-gnu/libnss_files-2.24.so addr, _perm, _offset, _dev, _inode, pathname = line.strip.split(' ', 6) next nil if pathname.nil? addr = addr.to_i(16) pathname = pathname[1..-2] if pathname =~ /^\[.+\]$/ pathname = ::File.basename(pathname) [MemoryIO::Util.trim_libname(pathname).to_sym, addr] end maps.compact.reverse.to_h end |
#read(addr, num_elements, **options) ⇒ String, ...
111 112 113 |
# File 'lib/memory_io/process.rb', line 111 def read(addr, num_elements, **) mem_io(:read) { |io| io.read(num_elements, from: MemoryIO::Util.safe_eval(addr, **bases), **) } end |
#write(addr, objects, **options) ⇒ void
This method returns an undefined value.
Write objects at addr
.
This method has almost same arguments as IO#write.
135 136 137 |
# File 'lib/memory_io/process.rb', line 135 def write(addr, objects, **) mem_io(:write) { |io| io.write(objects, from: MemoryIO::Util.safe_eval(addr, **bases), **) } end |