From 788f56bec760216e95c3a8df9fc521b0d355776d Mon Sep 17 00:00:00 2001 From: Bart Trojanowski Date: Tue, 28 Nov 2023 16:39:17 -0500 Subject: [PATCH] show basename in source: split line (#1017) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This patch modifies the text shown above the source window as part of the `context_source()` function. Before this change `────────────────────────── source:/server/externa[...].cpp+130 ────` After the change `─────────────── source:/server/externa[...]SendReceive.hpp+110 ────` Introduces new `context` setting to customize the length of file path --- docs/commands/context.md | 20 ++++++++++++++++++++ gef.py | 14 ++++++++++++-- 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/docs/commands/context.md b/docs/commands/context.md index 5eba382bd..616c468b1 100644 --- a/docs/commands/context.md +++ b/docs/commands/context.md @@ -195,6 +195,26 @@ gef➤ gef config context.ignore_registers "$cs $ds $gs" gef➤ gef config context.show_source_code_variable_values 0 ``` +* Control how source file path is displayed. + +When displaying the source file name, above the source code view, the following +settings can be changed: + +```text +gef➤ gef config context.show_full_source_file_name_max_len 30 +gef➤ gef config context.show_prefix_source_path_name_len 10 +gef➤ gef config context.show_basename_source_file_name_max_len 20 +``` + +In this example, if the file path length is less than or equal to 30 it will be +displayed in its entirety. If however, it's more than 30 characters in length, +it will be truncated. + +Truncation first splits the path into the prefix part and file name part. The +`show_prefix_source_path_name_len` controls how many characters of the prefix +path to show, and the `show_basename_source_file_name_max_len` controls how +many characters from the file name to show. + * Show better definitions for call to libc functions. ```text diff --git a/gef.py b/gef.py index 5c187451f..49be5eae7 100644 --- a/gef.py +++ b/gef.py @@ -7220,6 +7220,9 @@ def __init__(self) -> None: super().__init__() self["enable"] = (True, "Enable/disable printing the context when breaking") self["show_source_code_variable_values"] = (True, "Show extra PC context info in the source code") + self["show_full_source_file_name_max_len"] = (30, "Show full source path name, if less than this value") + self["show_basename_source_file_name_max_len"] = (20, "Show the source basename in full, if less than this value") + self["show_prefix_source_path_name_len"] = (10, "When truncating source path, show this many path prefix characters") self["show_stack_raw"] = (False, "Show the stack pane as raw hexdump (no dereference)") self["show_registers_raw"] = (False, "Show the registers pane with raw values (no dereference)") self["show_opcodes_size"] = (0, "Number of bytes of opcodes to display next to the disassembly") @@ -7661,10 +7664,17 @@ def context_source(self) -> None: bp_locations = [b.location for b in breakpoints if b.location and file_base_name in b.location] past_lines_color = gef.config["theme.old_context"] + show_full_path_max = self["show_full_source_file_name_max_len"] + show_basename_path_max = self["show_basename_source_file_name_max_len"] + show_prefix_path_len = self["show_prefix_source_path_name_len"] + nb_line = self["nb_lines_code"] fn = symtab.filename - if len(fn) > 20: - fn = f"{fn[:15]}[...]{os.path.splitext(fn)[1]}" + if len(fn) > show_full_path_max: + base = os.path.basename(fn) + if len(base) > show_basename_path_max: + base = base[-show_basename_path_max:] + fn = fn[:15] + "[...]" + base title = f"source:{fn}+{line_num + 1}" cur_line_color = gef.config["theme.source_current_line"] self.context_title(title)