Enable customizable context display and ordering (#210)

* Enable customizable context display and ordering

* Resolve ambiguity, shorten default;

* Make function names match the configurations

* Use a dictionary mapping to simplify logic

* Put registry at the end of the file

* Add argumets to documentation

* Fix headings to reflect new names
pull/220/head
Zach Riggle 9 years ago committed by GitHub
parent 7d356d4834
commit 7b001a9ae9

@ -37,6 +37,9 @@ def clear_screen():
sys.stdout.write('\x1b[H\x1b[J') sys.stdout.write('\x1b[H\x1b[J')
config_clear_screen = pwndbg.config.Parameter('context-clear-screen', False, 'whether to clear the screen before printing the context') config_clear_screen = pwndbg.config.Parameter('context-clear-screen', False, 'whether to clear the screen before printing the context')
config_context_sections = pwndbg.config.Parameter('context-sections',
'regs disasm code stack backtrace',
'which context sections are displayed by default (also controls order)')
# @pwndbg.events.stop # @pwndbg.events.stop
@ -46,22 +49,20 @@ def context(*args):
""" """
Print out the current register, instruction, and stack context. Print out the current register, instruction, and stack context.
Accepts subcommands 'reg', 'code', 'stack', 'backtrace', and 'args'. Accepts subcommands 'reg', 'disasm', 'code', 'stack', 'backtrace', and 'args'.
""" """
if len(args) == 0: if len(args) == 0:
args = ['reg','code','stack','backtrace','args'] args = str(config_context_sections).split()
args = [a[0] for a in args] args = [a[0] for a in args]
result = [] result = []
result.append(M.legend()) result.append(M.legend())
if 'r' in args: result.extend(context_regs()) for arg in args:
if 'c' in args: result.extend(context_code()) func = context_sections.get(arg, None)
if 'c' in args: result.extend(context_source()) if func:
if 'a' in args: result.extend(context_args()) result.extend(func())
if 's' in args: result.extend(context_stack())
if 'b' in args: result.extend(context_backtrace())
result.extend(context_signal()) result.extend(context_signal())
if config_clear_screen: if config_clear_screen:
@ -150,8 +151,8 @@ Unicorn emulation of code near the current instruction
''') ''')
code_lines = pwndbg.config.Parameter('context-code-lines', 10, 'number of additional lines to print in the code context') code_lines = pwndbg.config.Parameter('context-code-lines', 10, 'number of additional lines to print in the code context')
def context_code(): def context_disasm():
banner = [pwndbg.ui.banner("code")] banner = [pwndbg.ui.banner("disasm")]
emulate = bool(pwndbg.config.emulate) emulate = bool(pwndbg.config.emulate)
result = pwndbg.commands.nearpc.nearpc(to_string=True, emulate=emulate, lines=code_lines // 2) result = pwndbg.commands.nearpc.nearpc(to_string=True, emulate=emulate, lines=code_lines // 2)
@ -164,7 +165,7 @@ def context_code():
theme.Parameter('highlight-source', True, 'whether to highlight the closest source line') theme.Parameter('highlight-source', True, 'whether to highlight the closest source line')
def context_source(): def context_code():
try: try:
symtab = gdb.selected_frame().find_sal().symtab symtab = gdb.selected_frame().find_sal().symtab
linetable = symtab.linetable() linetable = symtab.linetable()
@ -194,7 +195,7 @@ def context_source():
source_lines[i] = C.highlight(source_lines[i]) source_lines[i] = C.highlight(source_lines[i])
break break
banner = [pwndbg.ui.banner("code")] banner = [pwndbg.ui.banner("source")]
banner.extend(source_lines) banner.extend(source_lines)
return banner return banner
except: except:
@ -323,3 +324,12 @@ gdb.events.exited.connect(save_signal)
def context_signal(): def context_signal():
return last_signal return last_signal
context_sections = {
'r': context_regs,
'd': context_disasm,
'c': context_code,
's': context_stack,
'b': context_backtrace
}

Loading…
Cancel
Save