class MemoryIO::Process

Records information of a process.

def bases

Returns:
  • ({Symbol => Integer}) -
def bases
  file = "/proc/#{@pid}/maps"
  stat = MemoryIO::Util.file_permission(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

def initialize(pid)

Other tags:
    Todo: -
    Todo: -

Other tags:
    Note: -

Parameters:
  • pid (Integer) --

Other tags:
    Api: - private
def initialize(pid)
  @pid = pid
  @mem = "/proc/#{pid}/mem"
  # check permission of '/proc/pid/mem'
  @perm = MemoryIO::Util.file_permission(@mem)
  # TODO: raise custom exception
  raise Errno::ENOENT, @mem if perm.nil?
  # FIXME: use logger
  warn(<<-EOS.strip) unless perm.readable? || perm.writable?
have no permission to read/write this process.
k the setting of /proc/sys/kernel/yama/ptrace_scope, or try
n as the root user.
nable attach another process, do:
ho 0 | sudo tee /proc/sys/kernel/yama/ptrace_scope
  EOS
end

def mem_io(perm)

def mem_io(perm)
  flags = perm == :write ? 'wb' : 'rb'
  File.open(@mem, flags) { |f| yield MemoryIO::IO.new(f) }
end

def read(addr, num_elements, **options)

Other tags:
    See: IO#read -

Returns:
  • (String, Object, Array) -
    Parameters:
    • num_elements (Integer) --
    • addr (Integer, String) --
    def read(addr, num_elements, **options)
      mem_io(:read) { |io| io.read(num_elements, from: MemoryIO::Util.safe_eval(addr, **bases), **options) }
    end

    def write(addr, objects, **options)

    Other tags:
      See: IO#write -

    Returns:
    • (void) -

    Parameters:
    • objects (Object, Array) --
    • addr (Integer, String) --
    • def write(addr, objects, **options)
        mem_io(:write) { |io| io.write(objects, from: MemoryIO::Util.safe_eval(addr, **bases), **options) }
      end