From 7aca39049fbe3ff4d6b5bd44f442c7e21f6a93ff 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() (cherry picked from commit a8559fed1314e988e9d03d790bcc49858c3482ad) --- pwndbg/commands/nearpc.py | 33 ++++++++++++++++----------------- 1 file changed, 16 insertions(+), 17 deletions(-) diff --git a/pwndbg/commands/nearpc.py b/pwndbg/commands/nearpc.py index 0bf1c6094..19e106b95 100644 --- a/pwndbg/commands/nearpc.py +++ b/pwndbg/commands/nearpc.py @@ -21,6 +21,11 @@ import pwndbg.ui import pwndbg.vmmap pwndbg.config.Parameter('highlight-pc', True, 'whether to highlight the current instruction') +pwndbg.config.Parameter('left-pad-disasm', True, 'whether to left-pad disassembly') + +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 @@ -66,28 +71,22 @@ def nearpc(pc=None, lines=None, to_string=False, emulate=False): # this will trigger an exploratory search. 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) + # Gather all addresses and symbols for each instruction + symbols = [pwndbg.symbol.get(i.address) for i in instructions] + addresses = [hex(i.address) for i in instructions] - # Find the longest symbol name so we can adjust - if symbols: - longest_sym = max(map(len, symbols)) - else: - longest_sym = '' + # Format the symbol name for each instruction + symbols = ['<%s> ' % sym if sym else '' for sym in symbols] - # Pad them all out - for i,s in enumerate(symbols): - symbols[i] = s.ljust(longest_sym) + # Pad out all of the symbols and addresses + if pwndbg.config.left_pad_disasm: + symbols = ljust_padding(symbols) + 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 ' ' @@ -98,7 +97,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)) # Highlight the current line if the config is enabled if pwndbg.config.highlight_pc and i.address == pc: