From a8559fed1314e988e9d03d790bcc49858c3482ad Mon Sep 17 00:00:00 2001 From: MengHuan Yu Date: Sun, 12 Jun 2016 22:44:54 +0800 Subject: [PATCH] Fix the address padding issue in nearpc() --- pwndbg/commands/nearpc.py | 29 +++++++++++------------------ 1 file changed, 11 insertions(+), 18 deletions(-) diff --git a/pwndbg/commands/nearpc.py b/pwndbg/commands/nearpc.py index f397315ef..a1258a404 100644 --- a/pwndbg/commands/nearpc.py +++ b/pwndbg/commands/nearpc.py @@ -18,6 +18,9 @@ import pwndbg.symbol import pwndbg.ui import pwndbg.vmmap +def ljust_padding(lst): + longest_len = max(map(len, lst)) if lst else 0 + return [s.ljust(longest_len) for s in lst] @pwndbg.commands.ParsedCommand @pwndbg.commands.OnlyWhenRunning @@ -64,27 +67,17 @@ def nearpc(pc=None, lines=None, to_string=False, emulate=False): pwndbg.vmmap.find(pc) # Find all of the symbols for the addresses - symbols = [] - for i in instructions: - symbol = pwndbg.symbol.get(i.address) - if symbol: - symbol = '<%s> ' % symbol - symbols.append(symbol) - - # Find the longest symbol name so we can adjust - if symbols: - longest_sym = max(map(len, symbols)) - else: - longest_sym = '' - - # Pad them all out - for i,s in enumerate(symbols): - symbols[i] = s.ljust(longest_sym) + symbols = [pwndbg.symbol.get(i.address) for i in instructions] + symbols = ['<%s> ' % sym if sym else '' for sym in symbols] + symbols = ljust_padding(symbols) + + addresses = ["%#x" % i.address for i in instructions] + addresses = ljust_padding(addresses) prev = None # Print out each instruction - for i,s in zip(instructions, symbols): + for address_str, s, i in zip(addresses, symbols, instructions): asm = pwndbg.disasm.color.instruction(i) prefix = ' =>' if i.address == pc else ' ' @@ -95,7 +88,7 @@ def nearpc(pc=None, lines=None, to_string=False, emulate=False): # for line in pc_to_linenos[i.address]: # result.append('%s %s' % (line, lineno_to_src[line].strip())) - line = ' '.join((prefix, "%#x" % i.address, s or '', asm)) + line = ' '.join((prefix, address_str, s, asm)) # If there was a branch before this instruction which was not # contiguous, put in some ellipses.