Skip to content

Commit

Permalink
show basename in source: split line (#1017)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
bartman authored Nov 28, 2023
1 parent 5927df4 commit 788f56b
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
20 changes: 20 additions & 0 deletions docs/commands/context.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 12 additions & 2 deletions gef.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 788f56b

Please sign in to comment.