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
-
#context ⇒ MemoryIO::Context
readonly
The context of this process's memory.
- #perm ⇒ #readable?, #writable? readonly private
Instance Method Summary collapse
-
#bases ⇒ {Symbol => Integer}
Parse
/proc/[pid]/mapsto get all bases. -
#initialize(pid, endian: nil, pointer_size: nil) ⇒ 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, endian: nil, pointer_size: nil) ⇒ 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.
This class only supports procfs-based system. i.e. /proc is mounted and readable.
Create process object from pid.
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/memory_io/process.rb', line 40 def initialize(pid, endian: nil, pointer_size: nil) @pid = pid @mem = "/proc/#{pid}/mem" # check permission of '/proc/pid/mem' @perm = MemoryIO::Util.(@mem) raise MemoryIO::ProcessNotFoundError, "#{@mem} does not exist" if perm.nil? @context = build_context(endian, pointer_size) MemoryIO.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
#context ⇒ MemoryIO::Context (readonly)
Returns The context of this process's memory.
17 18 19 |
# File 'lib/memory_io/process.rb', line 17 def context @context end |
#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.
13 14 15 |
# File 'lib/memory_io/process.rb', line 13 def perm @perm end |
Instance Method Details
#bases ⇒ {Symbol => Integer}
Parse /proc/[pid]/maps to get all bases.
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/memory_io/process.rb', line 78 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, ...
130 131 132 |
# File 'lib/memory_io/process.rb', line 130 def read(addr, num_elements, **) mem_io(:read) { |io| io.read(num_elements, from: resolve_address(addr), **) } 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.
157 158 159 |
# File 'lib/memory_io/process.rb', line 157 def write(addr, objects, **) mem_io(:write) { |io| io.write(objects, from: resolve_address(addr), **) } end |