From 103b1def610957e2c74ab616b15a0513a1286cd6 Mon Sep 17 00:00:00 2001 From: Levente Polyak Date: Fri, 12 Jan 2018 16:28:48 +0100 Subject: [PATCH] config: validate context-sections and show all available values (#396) When setting an illegal value, fall back to the default sections. --- pwndbg/commands/context.py | 18 ++++++++++++++++-- pwndbg/config.py | 3 +++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/pwndbg/commands/context.py b/pwndbg/commands/context.py index d014634d7..eedf893a8 100644 --- a/pwndbg/commands/context.py +++ b/pwndbg/commands/context.py @@ -42,7 +42,21 @@ def clear_screen(): 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)') + 'which context sections are displayed (controls order)') + + +@pwndbg.config.Trigger([config_context_sections]) +def validate_context_sections(): + # if trying to set an empty string, we assume default is wanted + if not config_context_sections.value: + config_context_sections.value = config_context_sections.default + + valid_values = [context.__name__.replace('context_', '') for context in context_sections.values()] + for section in config_context_sections.split(): + if section not in valid_values: + print(message.warn("Invalid section: %s, valid values: %s" % (section, ', '.join(valid_values)))) + config_context_sections.value = config_context_sections.default + return # @pwndbg.events.stop @@ -55,7 +69,7 @@ def context(*args): Accepts subcommands 'reg', 'disasm', 'code', 'stack', 'backtrace', and 'args'. """ if len(args) == 0: - args = str(config_context_sections).split() + args = config_context_sections.split() args = [a[0] for a in args] diff --git a/pwndbg/config.py b/pwndbg/config.py index eb460dd42..0e53537bd 100644 --- a/pwndbg/config.py +++ b/pwndbg/config.py @@ -123,6 +123,9 @@ class Parameter(gdb.Parameter): def get_show_string(self, svalue): return 'Sets %s (currently: %r)' % (self.docstring, self.value) + def split(self): + return str(self).replace(',', ' ').split() + def __int__(self): return int(self.value)