context: fix code-lines to disasm-lines and code-source-* to code-* (#2316)

* context: fix code-lines to disasm-lines and code-source-* to code-*

Fixes #2300

* fixes

* fix tests

* fix test
pull/2320/head
Disconnect3d 1 year ago committed by GitHub
parent 0dfcf7c0fe
commit 1cba25bdd4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -211,7 +211,7 @@ We also read the program counter from the emulator to determine jumps and so we
## Enhancing
Everytime the inferior process stops (and when the `disasm` context section is displayed), we display the next handful of assembly instructions in the dashboard so the user can understand where the process is headed. The exact amount is determined by the `context-code-lines` setting.
Everytime the inferior process stops (and when the `disasm` context section is displayed), we display the next handful of assembly instructions in the dashboard so the user can understand where the process is headed. The exact amount is determined by the `context-disasm-lines` setting.
We will be enhancing the instruction at the current program counter, as well as all the future instructions that are displayed. The end result of enhancement is that we get a list of `PwndbgInstruction` objects, each encapsulating relevent information regarding the instructions execution.

@ -609,8 +609,8 @@ def get_regs(regs: List[str] = None):
return result
code_lines = pwndbg.config.add_param(
"context-code-lines", 10, "number of additional lines to print in the code context"
disasm_lines = pwndbg.config.add_param(
"context-disasm-lines", 10, "number of additional lines to print in the disasm context"
)
@ -633,7 +633,7 @@ def context_disasm(target=sys.stdout, with_banner=True, width=None):
pwndbg.lib.cache.clear_caches()
result = pwndbg.gdblib.nearpc.nearpc(
lines=code_lines // 2,
lines=disasm_lines // 2,
emulate=bool(not pwndbg.config.emulate == "off"),
use_cache=True,
)
@ -652,18 +652,18 @@ def context_disasm(target=sys.stdout, with_banner=True, width=None):
# If we didn't disassemble backward, try to make sure
# that the amount of screen space taken is roughly constant.
while len(result) < code_lines + 1:
while len(result) < disasm_lines + 1:
result.append("")
return banner + result if with_banner else result
theme.add_param("highlight-source", True, "whether to highlight the closest source line")
source_code_lines = pwndbg.config.add_param(
"context-source-code-lines", 10, "number of source code lines to print by the context command"
source_disasm_lines = pwndbg.config.add_param(
"context-code-lines", 10, "number of source code lines to print by the context command"
)
pwndbg.config.add_param(
"context-source-code-tabstop", 8, "number of spaces that a <tab> in the source code counts for"
"context-code-tabstop", 8, "number of spaces that a <tab> in the source code counts for"
)
theme.add_param("code-prefix", "", "prefix marker for 'context code' command")
@ -705,7 +705,7 @@ def get_filename_and_formatted_source():
if not source:
return "", []
n = int(source_code_lines)
n = int(source_disasm_lines)
# Compute the line range
start = max(closest_line - 1 - n // 2, 0)
@ -722,8 +722,8 @@ def get_filename_and_formatted_source():
# Format the output
formatted_source = []
for line_number, code in enumerate(source, start=start + 1):
if pwndbg.config.context_source_code_tabstop > 0:
code = code.replace("\t", " " * pwndbg.config.context_source_code_tabstop)
if pwndbg.config.context_code_tabstop > 0:
code = code.replace("\t", " " * pwndbg.config.context_code_tabstop)
fmt = " {prefix_sign:{prefix_width}} {line_number:>{num_width}} {code}"
if pwndbg.config.highlight_source and line_number == closest_line:
fmt = C.highlight(fmt)
@ -758,7 +758,7 @@ def context_code(target=sys.stdout, with_banner=True, width=None):
if not pwndbg.ida.available():
return []
n = int(int(int(source_code_lines) / 2)) # int twice to make it a real int instead of inthook
n = int(int(int(source_disasm_lines) / 2)) # int twice to make it a real int instead of inthook
# May be None when decompilation failed or user loaded wrong binary in IDA
code = pwndbg.ida.decompile_context(pwndbg.gdblib.regs.pc, n)

@ -6,7 +6,7 @@ import gdb
def test_config():
gdb.execute("set context-code-lines 8")
gdb.execute("set context-disasm-lines 8")
assert "8 (10)" in gdb.execute("config", to_string=True)
gdb.execute("set banner-separator #")
@ -18,12 +18,13 @@ def test_config():
def test_config_filtering():
out = gdb.execute("config context-code-lines", to_string=True).splitlines()
out = gdb.execute("config context-disasm-lines", to_string=True).splitlines()
assert re.match(r"Name\s+Value\s+\(Default\)\s+Documentation", out[0])
assert re.match(r"-+", out[1])
assert re.match(
r"context-code-lines\s+10\s+number of additional lines to print in the code context", out[2]
r"context-disasm-lines\s+10\s+number of additional lines to print in the disasm context",
out[2],
)
assert out[3] == "You can set config variable with `set <config-var> <value>`"
assert (

@ -103,7 +103,7 @@ def test_source_code_tabstop(start_binary):
gdb.execute("break tabstop.c:6")
gdb.execute("continue")
# Default context-source-code-tabstop = 8
# Default context-code-tabstop = 8
src = gdb.execute("context code", to_string=True)
assert """ 1 #include <stdio.h>\n""" in src
assert """ 2 \n""" in src
@ -116,8 +116,8 @@ def test_source_code_tabstop(start_binary):
assert """ 9 }\n""" in src
assert """10 \n""" in src
# Test context-source-code-tabstop = 2
gdb.execute("set context-source-code-tabstop 2")
# Test context-code-tabstop = 2
gdb.execute("set context-code-tabstop 2")
src = gdb.execute("context code", to_string=True)
assert """ 1 #include <stdio.h>\n""" in src
assert """ 2 \n""" in src
@ -130,8 +130,8 @@ def test_source_code_tabstop(start_binary):
assert """ 9 }\n""" in src
assert """10 \n""" in src
# Disable context-source-code-tabstop
gdb.execute("set context-source-code-tabstop 0")
# Disable context-code-tabstop
gdb.execute("set context-code-tabstop 0")
src = gdb.execute("context code", to_string=True)
assert """ 1 #include <stdio.h>\n""" in src
assert """ 2 \n""" in src

Loading…
Cancel
Save