Skip to content

Commit d054465

Browse files
committed
More generic less hardcoded values
1 parent 0ac80ef commit d054465

File tree

1 file changed

+24
-29
lines changed

1 file changed

+24
-29
lines changed

qiling/debugger/gdb/gdb.py

Lines changed: 24 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -155,30 +155,21 @@ def handle_exclaim(subcmd: str) -> Reply:
155155

156156

157157
def handle_qmark(subcmd: str) -> Reply:
158-
from unicorn.x86_const import UC_X86_REG_EIP, UC_X86_REG_ESP
159-
from unicorn.x86_const import UC_X86_REG_RIP, UC_X86_REG_RSP
160-
from unicorn.arm_const import UC_ARM_REG_PC, UC_ARM_REG_SP
161-
from unicorn.arm64_const import UC_ARM64_REG_PC, UC_ARM64_REG_SP
162-
from unicorn.mips_const import UC_MIPS_REG_PC, UC_MIPS_REG_SP
163-
164-
# X86 : T0505:00000000;04:c0d3ffff;08:2021fdf7;thread:p15c6.15c6;core:6
165-
# X8664 : T0506:0000000000000000;07:b0e2ffffff7f0000;10:0001fdf7ff7f0000;thread:p15a2.15a2;core:6;
166-
# MIPS32_EL : T051d:00e7ff7f;25:40ccfc77;
167-
# MIPS32_EB : T051d:7fff6dc0;25:77fc4880;thread:28fa;core:0;
168-
# ARM64 : T051d:0000000000000000;1f:80f6ffffffff0000;20:c02cfdb7ffff0000;thread:p1f9.1f9;core:0;
169-
# ARM : T050b:00000000;0d:e0f6ffbe;0f:8079fdb6;
170-
171-
response = {
172-
QL_ARCH.X86 : ( 0x05, UC_X86_REG_ESP, UC_X86_REG_EIP ),
173-
QL_ARCH.X8664 : ( 0x06, UC_X86_REG_RSP, UC_X86_REG_RIP ),
174-
QL_ARCH.ARM : ( 0x0b, UC_ARM_REG_SP, UC_ARM_REG_PC ),
175-
QL_ARCH.ARM64 : ( 0x1d, UC_ARM64_REG_SP, UC_ARM64_REG_PC ),
176-
QL_ARCH.MIPS : ( 0x1d, UC_MIPS_REG_SP, UC_MIPS_REG_PC ),
177-
QL_ARCH.A8086 : ( 0x05, UC_X86_REG_ESP, UC_X86_REG_EIP ),
178-
QL_ARCH.CORTEX_M : ( 0x0b, UC_ARM_REG_SP, UC_ARM_REG_PC )
179-
}
180-
181-
idhex, sp_reg, pc_reg = response[self.ql.arch.type]
158+
from unicorn.x86_const import UC_X86_REG_EBP
159+
from unicorn.x86_const import UC_X86_REG_RBP
160+
from unicorn.arm_const import UC_ARM_REG_R11
161+
from unicorn.arm64_const import UC_ARM64_REG_X29
162+
from unicorn.mips_const import UC_MIPS_REG_29
163+
164+
arch_uc_bp = {
165+
QL_ARCH.X86 : UC_X86_REG_EBP,
166+
QL_ARCH.X8664 : UC_X86_REG_RBP,
167+
QL_ARCH.ARM : UC_ARM_REG_R11,
168+
QL_ARCH.ARM64 : UC_ARM64_REG_X29,
169+
QL_ARCH.MIPS : UC_MIPS_REG_29,
170+
QL_ARCH.A8086 : UC_X86_REG_EBP,
171+
QL_ARCH.CORTEX_M : UC_ARM_REG_R11
172+
}[self.ql.arch.type]
182173

183174
def __get_reg_idx(ucreg: int) -> int:
184175
"""Get the index of a uc reg whithin the regsmap array.
@@ -188,16 +179,20 @@ def __get_reg_idx(ucreg: int) -> int:
188179

189180
return next((i for i, (regnum, _, _) in enumerate(self.regsmap) if regnum == ucreg), -1)
190181

191-
sp_idx = __get_reg_idx(sp_reg)
192-
pc_idx = __get_reg_idx(pc_reg)
182+
# FIXME: a8086 should use 'esp' and 'eip' here instead of 'sp' and 'ip' set by its arch instance
183+
bp_idx = __get_reg_idx(arch_uc_bp)
184+
sp_idx = __get_reg_idx(self.ql.arch.regs.uc_sp)
185+
pc_idx = __get_reg_idx(self.ql.arch.regs.uc_pc)
193186

187+
bp_val = __get_reg_value(*self.regsmap[bp_idx])
194188
sp_val = __get_reg_value(*self.regsmap[sp_idx])
195189
pc_val = __get_reg_value(*self.regsmap[pc_idx])
196190

197-
zfill = __hexstr(0)
191+
bp_info = f'{bp_idx:02x}:{bp_val};'
192+
sp_info = f'{sp_idx:02x}:{sp_val};'
193+
pc_info = f'{pc_idx:02x}:{pc_val};'
198194

199-
info = '' if self.ql.arch.type == QL_ARCH.MIPS else f':{zfill};{sp_idx:02x}'
200-
return f'T{SIGTRAP:02x}{idhex:02x}{info}:{sp_val};{pc_idx:02x}:{pc_val};'
195+
return f'T{SIGTRAP:02x}{"" if self.ql.arch.type == QL_ARCH.MIPS else bp_info}{sp_info}{pc_info}'
201196

202197

203198
def handle_c(subcmd: str) -> Reply:

0 commit comments

Comments
 (0)