Skip to content

Commit 13f17e5

Browse files
committed
Apply repr for C structs
1 parent 9463d00 commit 13f17e5

File tree

4 files changed

+18
-4
lines changed

4 files changed

+18
-4
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# from .arm import UcRegCP
2+
# from .arm64 import UcRegCP64
3+
# from .intel import UcRegFPR, UcRegMMR, UcRegMSR
4+
# from .types import UcReg128, UcReg256, UcReg512, UcReg, UcLargeReg, UcTupledReg

bindings/python/unicorn/unicorn_py3/arch/arm.py

+4
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313

1414
ARMCPReg = Tuple[int, int, int, int, int, int, int, int]
1515

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

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

39+
__repr__ = _structure_repr
3640

3741
class UcAArch32(Uc):
3842
"""Unicorn subclass for ARM architecture.

bindings/python/unicorn/unicorn_py3/arch/arm64.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
HOOK_INSN_SYS_CFUNC = ctypes.CFUNCTYPE(ctypes.c_uint32, uc_engine, ctypes.c_uint32, ctypes.c_void_p, ctypes.c_void_p)
1919

2020

21-
class UcRegCP(UcTupledReg[ARM64CPReg]):
21+
class UcRegCP64(UcTupledReg[ARM64CPReg]):
2222
"""ARM64 coprocessors registers for instructions MRS, MSR
2323
"""
2424

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

5757
class CpReg(NamedTuple):
5858
crn: int
@@ -102,7 +102,7 @@ def reg_read(self, reg_id: int, aux: Any = None):
102102

103103
if reg_cls is None:
104104
if reg_id == const.UC_ARM64_REG_CP_REG:
105-
return self._reg_read(reg_id, UcRegCP, *aux)
105+
return self._reg_read(reg_id, UcRegCP64, *aux)
106106

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

117117
if reg_cls is None:
118118
if reg_id == const.UC_ARM64_REG_CP_REG:
119-
self._reg_write(reg_id, UcRegCP, value)
119+
self._reg_write(reg_id, UcRegCP64, value)
120120

121121
else:
122122
# fallback to default writing method

bindings/python/unicorn/unicorn_py3/unicorn.py

+6
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010

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

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

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

28+
__repr__ = _structure_repr
2529

2630
class uc_tb(ctypes.Structure):
2731
""""TranslationBlock
@@ -32,6 +36,8 @@ class uc_tb(ctypes.Structure):
3236
('icount', ctypes.c_uint16),
3337
('size', ctypes.c_uint16)
3438
)
39+
40+
__repr__ = _structure_repr
3541

3642

3743
def __load_uc_lib() -> ctypes.CDLL:

0 commit comments

Comments
 (0)