Skip to content

Commit 04149c3

Browse files
committed
Refine add_to_str implementation
1 parent afaf708 commit 04149c3

File tree

1 file changed

+17
-12
lines changed

1 file changed

+17
-12
lines changed

qiling/debugger/gdb/gdb.py

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -83,18 +83,23 @@ def __init__(self, ql, ip, port):
8383
QL_ARCH.MIPS : list({**mips_reg_map}.keys()),
8484
}
8585

86-
def addr_to_str(self, addr, short=False, endian="big"):
87-
if self.ql.archbit == 64 and short == False:
88-
addr = (hex(int.from_bytes(self.ql.pack64(addr), byteorder=endian)))
89-
addr = '{:0>16}'.format(addr[2:])
90-
elif self.ql.archbit == 32 or short == True:
91-
addr = (hex(int.from_bytes(self.ql.pack32(addr), byteorder=endian)))
92-
addr = ('{:0>8}'.format(addr[2:]))
93-
elif self.ql.archbit == 16 or short == True:
94-
addr = (hex(int.from_bytes(self.ql.pack32(addr), byteorder=endian)))
95-
addr = ('{:0>8}'.format(addr[2:]))
96-
addr = str(addr)
97-
return addr
86+
def addr_to_str(self, addr: int, short: bool = False, endian: Literal['little', 'big'] = 'big') -> str:
87+
# a hacky way to divide archbits by 2 if short, and leave it unchanged if not
88+
nbits = self.ql.archbit // (int(short) + 1)
89+
90+
if nbits == 64:
91+
s = f'{int.from_bytes(self.ql.pack64(addr), byteorder=endian):016x}'
92+
93+
elif nbits == 32:
94+
s = f'{int.from_bytes(self.ql.pack32(addr), byteorder=endian):08x}'
95+
96+
elif nbits == 16:
97+
s = f'{int.from_bytes(self.ql.pack16(addr), byteorder=endian):04x}'
98+
99+
else:
100+
raise RuntimeError
101+
102+
return s
98103

99104
def bin_to_escstr(self, rawbin):
100105
rawbin_escape = ""

0 commit comments

Comments
 (0)