Improve cwatch formatting (#1525)

pull/1526/head
Gulshan Singh 3 years ago committed by GitHub
parent beb2d3f8cb
commit ab5a411fb1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -248,20 +248,12 @@ def contextoutput(section, path, clearing, banner="both", width=None):
# Watches # Watches
expressions = [] expressions = []
expression_commands = {
"eval": gdb.parse_and_eval,
"execute": lambda exp: gdb.execute(exp, False, True),
}
parser = argparse.ArgumentParser( parser = argparse.ArgumentParser(
formatter_class=argparse.RawTextHelpFormatter, formatter_class=argparse.RawTextHelpFormatter,
description=""" description="""
Adds an expression to be shown on context. Adds an expression to be shown on context.
'cmd' controls what command is used to interpret the expression:
- eval: the expression is parsed and evaluated as in the debugged language.
- execute: the expression is executed as a GDB command.
To remove an expression, see `cunwatch`. To remove an expression, see `cunwatch`.
""", """,
) )
@ -270,7 +262,10 @@ parser.add_argument(
type=str, type=str,
default="eval", default="eval",
nargs="?", nargs="?",
help="Command to be used with the expression. Values are: eval execute", choices=["eval", "execute"],
help="""Command to be used with the expression.
- eval: the expression is parsed and evaluated as in the debugged language.
- execute: the expression is executed as a GDB command.""",
) )
parser.add_argument( parser.add_argument(
"expression", type=str, help="The expression to be evaluated and shown in context" "expression", type=str, help="The expression to be evaluated and shown in context"
@ -280,8 +275,8 @@ parser.add_argument(
@pwndbg.commands.ArgparsedCommand( @pwndbg.commands.ArgparsedCommand(
parser, aliases=["ctx-watch", "cwatch"], category=CommandCategory.CONTEXT parser, aliases=["ctx-watch", "cwatch"], category=CommandCategory.CONTEXT
) )
def contextwatch(expression, cmd=None) -> None: def contextwatch(expression, cmd) -> None:
expressions.append((expression, expression_commands.get(cmd, gdb.parse_and_eval))) expressions.append((expression, cmd))
parser = argparse.ArgumentParser( parser = argparse.ArgumentParser(
@ -306,27 +301,26 @@ def context_expressions(target=sys.stdout, with_banner=True, width=None):
return [] return []
banner = [pwndbg.ui.banner("expressions", target=target, width=width)] banner = [pwndbg.ui.banner("expressions", target=target, width=width)]
output = [] output = []
if width is None:
_height, width = pwndbg.ui.get_window_size(target=target)
for i, (exp, cmd) in enumerate(expressions): for i, (exp, cmd) in enumerate(expressions):
header = f"{i + 1}: {C.highlight(exp)}"
try: try:
# value = gdb.parse_and_eval(exp) if cmd == "eval":
value = str(cmd(exp)) value = str(gdb.parse_and_eval(exp))
else:
assert cmd == "execute"
value = gdb.execute(exp, from_tty=False, to_string=True)
except gdb.error as err: except gdb.error as err:
value = str(err) value = str(err)
value = value.split("\n")
lines: List[str] = []
for line in value:
if width and len(line) + len(exp) + 3 > width:
n = width - (len(exp) + 3) - 1 # 1 Padding...
lines.extend(line[i : i + n] for i in range(0, len(line), n))
else:
lines.append(line)
fmt = C.highlight(exp) # When evaluating the expression we display it inline with the header, but when executing an
lines[0] = "{}: {} = {}".format(i + 1, fmt, lines[0]) # expression we display it on the next line
lines[1:] = [" " * (len(exp) + 3) + line for line in lines[1:]] if cmd == "eval":
output.extend(lines) header += f" = {value}"
output.append(header)
if cmd == "execute":
output.append(value)
return banner + output if with_banner else output return banner + output if with_banner else output

Loading…
Cancel
Save