Skip to content

Commit

Permalink
refactor: regs/mem/maps accessors
Browse files Browse the repository at this point in the history
  • Loading branch information
ndrewh committed Dec 18, 2024
1 parent c257fec commit f89bd5f
Showing 1 changed file with 14 additions and 12 deletions.
26 changes: 14 additions & 12 deletions lib/pyda/process.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ def __init__(self, handle, io=False):
self._registered_syscall_post_hook = False
self._has_run = False

self.regs = ProcessRegisters(handle)
self.mem = ProcessMemory(handle)
self.maps = ProcessMaps(handle)

self.exe_path = self._p.get_main_module()

def _hook_dispatch(self, addr):
for h in self._hooks[addr]:
h(self)
Expand Down Expand Up @@ -113,20 +119,13 @@ def write(self, addr, data):
return self._p.write(addr, data)

def __getattr__(self, name):
# TODO: Move these into CPython extension?
if name == "regs":
return ProcessRegisters(self._p)
elif name == "mem":
return ProcessMemory(self)
elif name == "maps":
return ProcessMaps(self._p)
elif name == "exe_path":
return self._p.get_main_module()

raise AttributeError(f"Invalid attribute '{name}'. Did you mean 'regs.{name}'?")
if self.regs.has_reg(name):
raise AttributeError(f"Invalid attribute '{name}'. Did you mean 'regs.{name}'?")
else:
raise AttributeError(f"Invalid attribute '{name}'")

def __setattr__(self, name, value):
if not name.startswith("_") and name not in ["timeout", "buffer", "closed"]:
if not name.startswith("_") and name not in ["timeout", "buffer", "closed", "regs", "mem", "maps", "exe_path"]:
raise AttributeError(f"Cannot set attribute '{name}'")

super().__setattr__(name, value)
Expand Down Expand Up @@ -303,6 +302,9 @@ def __setattr__(self, name, value):
else:
super().__setattr__(name, value)

def has_reg(self, name):
return hasattr(pyda_core, "REG_"+name.upper())

class ProcessMemory():
def __init__(self, p):
self._p = p
Expand Down

0 comments on commit f89bd5f

Please sign in to comment.