From 1cba25bdd477b26b592c008362329f461385fc98 Mon Sep 17 00:00:00 2001 From: Disconnect3d Date: Wed, 24 Jul 2024 23:59:02 +0200 Subject: [PATCH] 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 --- DEVELOPING.md | 2 +- pwndbg/commands/context.py | 22 +++++++++---------- tests/gdb-tests/tests/test_command_config.py | 7 +++--- .../gdb-tests/tests/test_context_commands.py | 10 ++++----- 4 files changed, 21 insertions(+), 20 deletions(-) diff --git a/DEVELOPING.md b/DEVELOPING.md index 0d9f22e1a..8a289bf13 100644 --- a/DEVELOPING.md +++ b/DEVELOPING.md @@ -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. diff --git a/pwndbg/commands/context.py b/pwndbg/commands/context.py index 7188b7da8..c3433e8db 100644 --- a/pwndbg/commands/context.py +++ b/pwndbg/commands/context.py @@ -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 in the source code counts for" + "context-code-tabstop", 8, "number of spaces that a 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) diff --git a/tests/gdb-tests/tests/test_command_config.py b/tests/gdb-tests/tests/test_command_config.py index 749fb73c5..6cbd95699 100644 --- a/tests/gdb-tests/tests/test_command_config.py +++ b/tests/gdb-tests/tests/test_command_config.py @@ -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 `" assert ( diff --git a/tests/gdb-tests/tests/test_context_commands.py b/tests/gdb-tests/tests/test_context_commands.py index 5378028d8..ecbed0fb1 100644 --- a/tests/gdb-tests/tests/test_context_commands.py +++ b/tests/gdb-tests/tests/test_context_commands.py @@ -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 \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 \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 \n""" in src assert """ 2 \n""" in src