highlighting the current IP or line of the code/source section (#49)

pull/72/merge
Levente Polyak 10 years ago committed by Zach Riggle
parent d15e8b9efe
commit 5028f69ce5

@ -2,8 +2,8 @@ from __future__ import print_function
from __future__ import unicode_literals from __future__ import unicode_literals
import functools import functools
import six import six
import re
import gdb import gdb
import pwndbg.config import pwndbg.config
@ -19,6 +19,7 @@ YELLOW = "\x1b[33m"
BLUE = "\x1b[34m" BLUE = "\x1b[34m"
PURPLE = "\x1b[35m" PURPLE = "\x1b[35m"
CYAN = "\x1b[36m" CYAN = "\x1b[36m"
WHITE = "\x1b[37m"
GREY = GRAY = "\x1b[90m" GREY = GRAY = "\x1b[90m"
BOLD = "\x1b[1m" BOLD = "\x1b[1m"
UNDERLINE = "\x1b[4m" UNDERLINE = "\x1b[4m"
@ -29,17 +30,21 @@ pwndbg.config.Parameter('color-code', 'red', 'color for executable memory')
pwndbg.config.Parameter('color-data', 'purple', 'color for all other writable memory') pwndbg.config.Parameter('color-data', 'purple', 'color for all other writable memory')
pwndbg.config.Parameter('color-rodata', 'normal', 'color for all read only memory') pwndbg.config.Parameter('color-rodata', 'normal', 'color for all read only memory')
pwndbg.config.Parameter('color-rwx', 'underline', 'color added to all RWX memory') pwndbg.config.Parameter('color-rwx', 'underline', 'color added to all RWX memory')
pwndbg.config.Parameter('color-highlight', 'green,bold', 'color added to highlights like source/pc')
def normal(x): return NORMAL + str(x)
def bold(x): return BOLD + str(x) + NORMAL def normal(x): return colorize(x, NORMAL)
def red(x): return RED + str(x) + NORMAL def black(x): return colorize(x, BLACK)
def blue(x): return BLUE + str(x) + NORMAL def red(x): return colorize(x, RED)
def gray(x): return GRAY + str(x) + NORMAL def green(x): return colorize(x, GREEN)
def green(x): return GREEN + str(x) + NORMAL def yellow(x): return colorize(x, YELLOW)
def cyan(x): return CYAN + str(x) + NORMAL def blue(x): return colorize(x, BLUE)
def yellow(x): return YELLOW + str(x) + NORMAL def purple(x): return colorize(x, PURPLE)
def purple(x): return PURPLE + str(x) + NORMAL def cyan(x): return colorize(x, CYAN)
def underline(x): return UNDERLINE + str(x) + NORMAL def white(x): return colorize(x, WHITE)
def gray(x): return colorize(x, GRAY)
def bold(x): return colorize(x, BOLD)
def underline(x): return colorize(x, UNDERLINE)
def colorize(x, color): return color + terminateWith(str(x), color) + NORMAL
@pwndbg.memoize.reset_on_stop @pwndbg.memoize.reset_on_stop
def generateColorFunctionInner(old, new): def generateColorFunctionInner(old, new):
@ -71,6 +76,9 @@ def rodata(x):
def rwx(x): def rwx(x):
return generateColorFunction(pwndbg.config.color_rwx)(x) return generateColorFunction(pwndbg.config.color_rwx)(x)
def highlight(x):
return generateColorFunction(pwndbg.config.color_highlight)(x)
def get(address, text = None): def get(address, text = None):
""" """
Returns a colorized string representing the provided address. Returns a colorized string representing the provided address.
@ -111,3 +119,9 @@ def legend():
rwx('RWX'), rwx('RWX'),
rodata('RODATA') rodata('RODATA')
)) ))
def strip(x):
return re.sub('\x1b\\[\d+m', '', x)
def terminateWith(x, color):
return re.sub('\x1b\\[0m', NORMAL + color, x)

@ -141,6 +141,8 @@ def context_code():
return banner + result return banner + result
pwndbg.config.Parameter('highlight-source', True, 'whether to highlight the closest source line')
def context_source(): def context_source():
try: try:
symtab = gdb.selected_frame().find_sal().symtab symtab = gdb.selected_frame().find_sal().symtab
@ -163,8 +165,16 @@ def context_source():
if not source or closest_line <= 1: if not source or closest_line <= 1:
return [] return []
# highlight the current code line
source_lines = source.splitlines()
if pwndbg.config.highlight_source:
for i in range(len(source_lines)):
if source_lines[i].startswith('%s\t' % closest_line):
source_lines[i] = pwndbg.color.highlight(source_lines[i])
break
banner = [pwndbg.color.blue(pwndbg.ui.banner("code"))] banner = [pwndbg.color.blue(pwndbg.ui.banner("code"))]
banner.extend(source.splitlines()) banner.extend(source_lines)
return banner return banner
except: except:
pass pass

@ -21,6 +21,8 @@ import pwndbg.ui
import pwndbg.vmmap import pwndbg.vmmap
pwndbg.config.Parameter('highlight-pc', True, 'whether to highlight the current instruction')
@pwndbg.commands.ParsedCommand @pwndbg.commands.ParsedCommand
@pwndbg.commands.OnlyWhenRunning @pwndbg.commands.OnlyWhenRunning
def nearpc(pc=None, lines=None, to_string=False, emulate=False): def nearpc(pc=None, lines=None, to_string=False, emulate=False):
@ -99,6 +101,10 @@ def nearpc(pc=None, lines=None, to_string=False, emulate=False):
line = ' '.join((prefix, "%#x" % i.address, s or '', asm)) line = ' '.join((prefix, "%#x" % i.address, s or '', asm))
# Highlight the current line if the config is enabled
if pwndbg.config.highlight_pc and i.address == pc:
line = pwndbg.color.highlight(line)
# If there was a branch before this instruction which was not # If there was a branch before this instruction which was not
# contiguous, put in some ellipses. # contiguous, put in some ellipses.
if prev and prev.address + prev.size != i.address: if prev and prev.address + prev.size != i.address:

@ -51,7 +51,6 @@ def instruction(ins):
# XXX: not sure when this ever happens # XXX: not sure when this ever happens
asm += '<-- file a pwndbg bug for this' asm += '<-- file a pwndbg bug for this'
else: else:
colored_addr = pwndbg.color.get(ins.symbol_addr)
asm = asm.replace(hex(ins.symbol_addr), ins.symbol) asm = asm.replace(hex(ins.symbol_addr), ins.symbol)
asm = '%-36s <%s>' % (asm, pwndbg.color.get(ins.symbol_addr)) asm = '%-36s <%s>' % (asm, pwndbg.color.get(ins.symbol_addr))

Loading…
Cancel
Save