Skip to content

Commit

Permalink
Apply repr for C structs
Browse files Browse the repository at this point in the history
  • Loading branch information
wtdcode committed Mar 8, 2024
1 parent 9463d00 commit 13f17e5
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 4 deletions.
4 changes: 4 additions & 0 deletions bindings/python/unicorn/unicorn_py3/arch/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# from .arm import UcRegCP
# from .arm64 import UcRegCP64
# from .intel import UcRegFPR, UcRegMMR, UcRegMSR
# from .types import UcReg128, UcReg256, UcReg512, UcReg, UcLargeReg, UcTupledReg
4 changes: 4 additions & 0 deletions bindings/python/unicorn/unicorn_py3/arch/arm.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@

ARMCPReg = Tuple[int, int, int, int, int, int, int, int]

def _structure_repr(self):
return "%s(%s)" % (self.__class__.__name__, ", ".join("%s=%s" % (k, getattr(self, k)) for (k, _) in self._fields_))


class UcRegCP(UcTupledReg[ARMCPReg]):
"""ARM coprocessors registers for instructions MRC, MCR, MRRC, MCRR
Expand All @@ -33,6 +36,7 @@ class UcRegCP(UcTupledReg[ARMCPReg]):
def value(self) -> int:
return self.val

__repr__ = _structure_repr

class UcAArch32(Uc):
"""Unicorn subclass for ARM architecture.
Expand Down
8 changes: 4 additions & 4 deletions bindings/python/unicorn/unicorn_py3/arch/arm64.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
HOOK_INSN_SYS_CFUNC = ctypes.CFUNCTYPE(ctypes.c_uint32, uc_engine, ctypes.c_uint32, ctypes.c_void_p, ctypes.c_void_p)


class UcRegCP(UcTupledReg[ARM64CPReg]):
class UcRegCP64(UcTupledReg[ARM64CPReg]):
"""ARM64 coprocessors registers for instructions MRS, MSR
"""

Expand Down Expand Up @@ -52,7 +52,7 @@ def hook_add(self, htype: int, callback: Callable, user_data: Any = None, begin:
def __hook_insn_sys():
@uccallback(HOOK_INSN_SYS_CFUNC)
def __hook_insn_sys_cb(handle: int, reg: int, pcp_reg: Any, key: int) -> int:
cp_reg = ctypes.cast(pcp_reg, ctypes.POINTER(UcRegCP)).contents
cp_reg = ctypes.cast(pcp_reg, ctypes.POINTER(UcRegCP64)).contents

class CpReg(NamedTuple):
crn: int
Expand Down Expand Up @@ -102,7 +102,7 @@ def reg_read(self, reg_id: int, aux: Any = None):

if reg_cls is None:
if reg_id == const.UC_ARM64_REG_CP_REG:
return self._reg_read(reg_id, UcRegCP, *aux)
return self._reg_read(reg_id, UcRegCP64, *aux)

else:
# fallback to default reading method
Expand All @@ -116,7 +116,7 @@ def reg_write(self, reg_id: int, value) -> None:

if reg_cls is None:
if reg_id == const.UC_ARM64_REG_CP_REG:
self._reg_write(reg_id, UcRegCP, value)
self._reg_write(reg_id, UcRegCP64, value)

else:
# fallback to default writing method
Expand Down
6 changes: 6 additions & 0 deletions bindings/python/unicorn/unicorn_py3/unicorn.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@

__version__ = f'{uc.UC_VERSION_MAJOR}.{uc.UC_VERSION_MINOR}.{uc.UC_VERSION_PATCH}'

def _structure_repr(self):
return "%s(%s)" % (self.__class__.__name__, ", ".join("%s=%s" % (k, getattr(self, k)) for (k, _) in self._fields_))


class _uc_mem_region(ctypes.Structure):
_fields_ = (
Expand All @@ -22,6 +25,7 @@ class _uc_mem_region(ctypes.Structure):
def value(self) -> Tuple[int, int, int]:
return tuple(getattr(self, fname) for fname, _ in self._fields_)

__repr__ = _structure_repr

class uc_tb(ctypes.Structure):
""""TranslationBlock
Expand All @@ -32,6 +36,8 @@ class uc_tb(ctypes.Structure):
('icount', ctypes.c_uint16),
('size', ctypes.c_uint16)
)

__repr__ = _structure_repr


def __load_uc_lib() -> ctypes.CDLL:
Expand Down

0 comments on commit 13f17e5

Please sign in to comment.