Add __repr__ to all ctypes.Structure subclasses

This commit is contained in:
Robert Xiao 2023-05-10 12:57:36 -07:00
parent d27ca4530b
commit 06a76e98c4
1 changed files with 21 additions and 2 deletions

View File

@ -130,6 +130,9 @@ uc_engine = ctypes.c_void_p
uc_context = ctypes.c_void_p uc_context = ctypes.c_void_p
uc_hook_h = ctypes.c_size_t uc_hook_h = ctypes.c_size_t
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): class _uc_mem_region(ctypes.Structure):
_fields_ = [ _fields_ = [
("begin", ctypes.c_uint64), ("begin", ctypes.c_uint64),
@ -137,6 +140,8 @@ class _uc_mem_region(ctypes.Structure):
("perms", ctypes.c_uint32), ("perms", ctypes.c_uint32),
] ]
__repr__ = _structure_repr
class uc_tb(ctypes.Structure): class uc_tb(ctypes.Structure):
""""TranslationBlock""" """"TranslationBlock"""
_fields_ = [ _fields_ = [
@ -145,6 +150,8 @@ class uc_tb(ctypes.Structure):
("size", ctypes.c_uint16) ("size", ctypes.c_uint16)
] ]
__repr__ = _structure_repr
_setup_prototype(_uc, "uc_version", ctypes.c_uint, ctypes.POINTER(ctypes.c_int), ctypes.POINTER(ctypes.c_int)) _setup_prototype(_uc, "uc_version", ctypes.c_uint, ctypes.POINTER(ctypes.c_int), ctypes.POINTER(ctypes.c_int))
_setup_prototype(_uc, "uc_arch_supported", ctypes.c_bool, ctypes.c_int) _setup_prototype(_uc, "uc_arch_supported", ctypes.c_bool, ctypes.c_int)
_setup_prototype(_uc, "uc_open", ucerr, ctypes.c_uint, ctypes.c_uint, ctypes.POINTER(uc_engine)) _setup_prototype(_uc, "uc_open", ucerr, ctypes.c_uint, ctypes.c_uint, ctypes.POINTER(uc_engine))
@ -413,6 +420,8 @@ class uc_arm_cp_reg(ctypes.Structure):
("val", ctypes.c_uint64) ("val", ctypes.c_uint64)
] ]
__repr__ = _structure_repr
class uc_arm64_cp_reg(ctypes.Structure): class uc_arm64_cp_reg(ctypes.Structure):
"""ARM64 coprocessors registers for instructions MRS, MSR""" """ARM64 coprocessors registers for instructions MRS, MSR"""
_fields_ = [ _fields_ = [
@ -424,8 +433,7 @@ class uc_arm64_cp_reg(ctypes.Structure):
("val", ctypes.c_uint64) ("val", ctypes.c_uint64)
] ]
def __repr__(self): __repr__ = _structure_repr
return "%s(%s)" % (self.__class__.__name__, ", ".join("%s=%s" % (k, getattr(self, k)) for (k, _) in self._fields_))
class uc_x86_mmr(ctypes.Structure): class uc_x86_mmr(ctypes.Structure):
"""Memory-Management Register for instructions IDTR, GDTR, LDTR, TR.""" """Memory-Management Register for instructions IDTR, GDTR, LDTR, TR."""
@ -436,12 +444,16 @@ class uc_x86_mmr(ctypes.Structure):
("flags", ctypes.c_uint32), # not used by GDTR and IDTR ("flags", ctypes.c_uint32), # not used by GDTR and IDTR
] ]
__repr__ = _structure_repr
class uc_x86_msr(ctypes.Structure): class uc_x86_msr(ctypes.Structure):
_fields_ = [ _fields_ = [
("rid", ctypes.c_uint32), ("rid", ctypes.c_uint32),
("value", ctypes.c_uint64), ("value", ctypes.c_uint64),
] ]
__repr__ = _structure_repr
class uc_x86_float80(ctypes.Structure): class uc_x86_float80(ctypes.Structure):
"""Float80""" """Float80"""
_fields_ = [ _fields_ = [
@ -449,6 +461,7 @@ class uc_x86_float80(ctypes.Structure):
("exponent", ctypes.c_uint16), ("exponent", ctypes.c_uint16),
] ]
__repr__ = _structure_repr
class uc_x86_xmm(ctypes.Structure): class uc_x86_xmm(ctypes.Structure):
"""128-bit xmm register""" """128-bit xmm register"""
@ -457,6 +470,8 @@ class uc_x86_xmm(ctypes.Structure):
("high_qword", ctypes.c_uint64), ("high_qword", ctypes.c_uint64),
] ]
__repr__ = _structure_repr
class uc_x86_ymm(ctypes.Structure): class uc_x86_ymm(ctypes.Structure):
"""256-bit ymm register""" """256-bit ymm register"""
_fields_ = [ _fields_ = [
@ -466,6 +481,8 @@ class uc_x86_ymm(ctypes.Structure):
("fourth_qword", ctypes.c_uint64), ("fourth_qword", ctypes.c_uint64),
] ]
__repr__ = _structure_repr
class uc_arm64_neon128(ctypes.Structure): class uc_arm64_neon128(ctypes.Structure):
"""128-bit neon register""" """128-bit neon register"""
_fields_ = [ _fields_ = [
@ -473,6 +490,8 @@ class uc_arm64_neon128(ctypes.Structure):
("high_qword", ctypes.c_uint64), ("high_qword", ctypes.c_uint64),
] ]
__repr__ = _structure_repr
# Subclassing ref to allow property assignment. # Subclassing ref to allow property assignment.
class UcRef(weakref.ref): class UcRef(weakref.ref):
pass pass