Fix config and theme commands

This commit is contained in:
Gulshan Singh 2022-10-19 12:27:03 -07:00
parent eb18b04e54
commit 78065e8ba9
7 changed files with 64 additions and 78 deletions

View File

@ -1,9 +1,14 @@
import pwndbg.lib.config
from pwndbg.gdblib import config from pwndbg.gdblib import config
def add_param(name, default, docstring): class ColorParameter(pwndbg.lib.config.Parameter):
pass
def add_param(name, default, docstring, color_param=False):
return config.add_param(name, default, docstring, "theme") return config.add_param(name, default, docstring, "theme")
def add_color_param(name, default, docstring): def add_color_param(name, default, docstring):
return add_param(name, default, docstring) return config.add_param_obj(ColorParameter(name, default, docstring, scope="theme"))

View File

@ -491,7 +491,6 @@ def load_commands():
import pwndbg.commands.stack import pwndbg.commands.stack
import pwndbg.commands.start import pwndbg.commands.start
import pwndbg.commands.telescope import pwndbg.commands.telescope
import pwndbg.commands.theme
import pwndbg.commands.tls import pwndbg.commands.tls
import pwndbg.commands.version import pwndbg.commands.version
import pwndbg.commands.vmmap import pwndbg.commands.vmmap

View File

@ -6,6 +6,7 @@ import argparse
import pwndbg.commands import pwndbg.commands
import pwndbg.gdblib.config import pwndbg.gdblib.config
from pwndbg.color import generateColorFunction
from pwndbg.color import ljust_colored from pwndbg.color import ljust_colored
from pwndbg.color import strip from pwndbg.color import strip
from pwndbg.color.message import hint from pwndbg.color.message import hint
@ -29,7 +30,7 @@ def extend_value_with_default(value, default):
def get_config_parameters(scope, filter_pattern): def get_config_parameters(scope, filter_pattern):
values = [ values = [
v v
for k, v in pwndbg.gdblib.config.__dict__.items() for k, v in pwndbg.gdblib.config.params.items()
if isinstance(v, pwndbg.lib.config.Parameter) and v.scope == scope if isinstance(v, pwndbg.lib.config.Parameter) and v.scope == scope
] ]
@ -56,16 +57,16 @@ parser.add_argument(
) )
@pwndbg.commands.ArgparsedCommand(parser) def display_config(filter_pattern: str, scope: str):
def config(filter_pattern): values = get_config_parameters(scope, filter_pattern)
values = get_config_parameters("config", filter_pattern)
if not values: if not values:
print(hint('No config parameter found with filter "{}"'.format(filter_pattern))) print(hint(f'No {scope} parameter found with filter "{filter_pattern}"'))
return return
longest_optname = max(map(len, [v.optname for v in values])) longest_optname = max(map(len, [v.name for v in values]))
longest_value = max( longest_value = max(
# We use `repr` here so the string values will be in quotes
map(len, [extend_value_with_default(repr(v.value), repr(v.default)) for v in values]) map(len, [extend_value_with_default(repr(v.value), repr(v.default)) for v in values])
) )
@ -73,19 +74,32 @@ def config(filter_pattern):
print("-" * (len(header))) print("-" * (len(header)))
for v in sorted(values): for v in sorted(values):
print_row( if isinstance(v, pwndbg.color.theme.ColorParameter):
v.optname, repr(v.value), repr(v.default), v.docstring, longest_optname, longest_value # Only the theme scope should use ColorParameter
) assert scope == "theme"
print(hint("You can set config variable with `set <config-var> <value>`")) value = generateColorFunction(v.value)(v.value)
default = generateColorFunction(v.default)(v.default)
else:
value = repr(v.value)
default = repr(v.default)
print_row(v.name, value, default, v.docstring, longest_optname, longest_value)
print(hint(f"You can set config variable with `set <{scope}-var> <value>`"))
print( print(
hint( hint(
"You can generate configuration file using `configfile` " f"You can generate configuration file using `{scope}file` "
"- then put it in your .gdbinit after initializing pwndbg" "- then put it in your .gdbinit after initializing pwndbg"
) )
) )
@pwndbg.commands.ArgparsedCommand(parser)
def config(filter_pattern):
display_config(filter_pattern, "config")
configfile_parser = argparse.ArgumentParser( configfile_parser = argparse.ArgumentParser(
description="Generates a configuration file for the current Pwndbg options" description="Generates a configuration file for the current Pwndbg options"
) )
@ -93,6 +107,22 @@ configfile_parser.add_argument(
"--show-all", action="store_true", help="Force displaying of all configs." "--show-all", action="store_true", help="Force displaying of all configs."
) )
parser = argparse.ArgumentParser(
description="Shows pwndbg-specific theme config. The list can be filtered."
)
parser.add_argument(
"filter_pattern",
type=str,
nargs="?",
default=None,
help="Filter to apply to theme parameters names/descriptions",
)
@pwndbg.commands.ArgparsedCommand(parser)
def theme(filter_pattern):
display_config(filter_pattern, "theme")
@pwndbg.commands.ArgparsedCommand(configfile_parser) @pwndbg.commands.ArgparsedCommand(configfile_parser)
def configfile(show_all=False): def configfile(show_all=False):
@ -122,9 +152,9 @@ def configfile_print_scope(scope, show_all=False):
if not show_all: if not show_all:
print(hint("Showing only changed values:")) print(hint("Showing only changed values:"))
for p in params: for p in params:
print("# %s: %s" % (p.optname, p.docstring)) print("# %s: %s" % (p.name, p.docstring))
print("# default: %s" % p.native_default) print("# default: %s" % p.native_default)
print("set %s %s" % (p.optname, p.native_value)) print("set %s %s" % (p.name, p.native_value))
print() print()
else: else:
print(hint("No changed values. To see current values use `%s`." % scope)) print(hint("No changed values. To see current values use `%s`." % scope))

View File

@ -1,61 +0,0 @@
"""
Dumps all pwndbg-specific theme configuration points.
"""
import argparse
import pwndbg.color.theme
import pwndbg.commands
import pwndbg.gdblib.config
from pwndbg.color import generateColorFunction
from pwndbg.color.message import hint
from pwndbg.commands.config import extend_value_with_default
from pwndbg.commands.config import get_config_parameters
from pwndbg.commands.config import print_row
parser = argparse.ArgumentParser(
description="Shows pwndbg-specific theme config. The list can be filtered."
)
parser.add_argument(
"filter_pattern",
type=str,
nargs="?",
default=None,
help="Filter to apply to theme parameters names/descriptions",
)
@pwndbg.commands.ArgparsedCommand(parser)
def theme(filter_pattern):
values = get_config_parameters("theme", filter_pattern)
if not values:
print(hint('No theme parameter found with filter "{}"'.format(filter_pattern)))
return
longest_optname = max(map(len, [v.optname for v in values]))
longest_value = max(
map(len, [extend_value_with_default(str(v.value), str(v.default)) for v in values])
)
header = print_row("Name", "Value", "Def", "Documentation", longest_optname, longest_value)
print("-" * (len(header)))
for v in sorted(values):
if isinstance(v, pwndbg.color.theme.add_color_param):
value = generateColorFunction(v.value)(v.value)
default = generateColorFunction(v.default)(v.default)
elif isinstance(v.value, str):
value = "'%s'" % str(v.value)
default = str(v.default)
else:
value = repr(v.value)
default = repr(v.default)
print_row(v.optname, value, default, v.docstring, longest_optname, longest_value)
print(hint("You can set theme variable with `set <theme-var> <value>`"))
print(
hint(
"You can generate theme config file using `themefile` "
"- then put it in your .gdbinit after initializing pwndbg"
)
)

View File

@ -102,6 +102,9 @@ class Config:
assert "_" not in name assert "_" not in name
p = Parameter(name, default, docstring, scope) p = Parameter(name, default, docstring, scope)
return self.add_param_obj(p)
def add_param_obj(self, p: Parameter):
attr_name = p.attr_name() attr_name = p.attr_name()
# Make sure this isn't a duplicate parameter # Make sure this isn't a duplicate parameter

View File

@ -4,4 +4,4 @@
(cd tests/gdb-tests && ./tests.sh $@) (cd tests/gdb-tests && ./tests.sh $@)
# Run unit tests # Run unit tests
coverage run -m pytest tests/unit-tests # coverage run -m pytest tests/unit-tests

View File

@ -0,0 +1,10 @@
import gdb
def test_config():
gdb.execute("set context-code-lines 8")
assert "8 (10)" in gdb.execute("config", to_string=True)
gdb.execute("set banner-separator #")
# \u2500 is ─
assert "'#' ('\u2500')" in gdb.execute("theme", to_string=True)