def pretty_code(self, code: str) -> str: if (code.startswith('call') or code.startswith('jp') or code.startswith('jr')) and 'HL' not in code: instr = code.split()[0] code = instr + " " + Addr.get(self.extract_addr()) if '[' in code: b = code.index('[') + 1 e = code.index(']') if ' ' not in code[b:e] and code[b] == '0': addr = code[b:e].split('/')[0] code = code[:b] + Addr.get(addr) + code[e:] elif '+' in code[b:e] and code[b:e].startswith('0xFF00 + 0x'): iaddr = int(code[b + len('0xFF00 + '):e].split('/')[0], 16) code = code[:b] + Addr.get(hex(0xFF00 + iaddr)) + code[e:] return code
def run_dbg(trace_file: str) -> None: call = FunCallTracker() ret = RetTracker() cpu = CPU() dbg = Debugger() for instr in cpu.execute(trace_file): print(Addr.get_aligned(instr.addr) + " " + instr.format_code) dbg.prompt(cpu)
def log(self, instr: Instr) -> None: if not instr.is_interrupt: self.funs[self.stack[-1]].log(instr) if self.debug: print("." * (len(self.stack) - 1) + Addr.get(instr.addr) + ':\t' + instr.code) if instr.iaddr in Addr.jp_to_fun: self.stack[-1] = instr.extract_addr()
def show(self) -> None: for k, v in self.funs.items(): print('-------' + Addr.get(k)) v.show() print('-------')
def show(self) -> None: for i in self.instrs: print(Addr.get_aligned(i.addr) + ' ' + i.format_code)
print('-------') def inline_stack(self) -> None: print("." * (len(self.stack) - 1) + "[" + " -> ".join(self.stack)) def reconstruct_functions(trace_file: str) -> Program: call = FunCallTracker() ret = RetTracker() f = Program(debug=False) f.push('0x100') cpu = CPU() for instr in cpu.execute(trace_file): if ret.happened(instr): f.pop() if call.happened(instr): f.push(instr.addr) f.log(instr) return f if __name__ == '__main__': if len(sys.argv) == 3: Addr.init(sys.argv[2]) reconstruct_functions(sys.argv[1]).show()