Prefer relative paths for vmmap output (#2822)

* prefer relpath in vmmap

* add configuration to turn off relative paths

* Add vmmap_prefer_relpaths to vmmap legend

* address some nits

* explain rel.startswith("../..")

* fix uitests
pull/2826/head
tesuji 8 months ago committed by GitHub
parent 21a83494d8
commit 2c3d11a801
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -7,6 +7,13 @@ import pwndbg.aglib.vmmap_custom
import pwndbg.lib.cache
import pwndbg.lib.memory
pwndbg.config.add_param(
"vmmap-prefer-relpaths",
True,
"show relative paths by default in vmmap",
param_class=pwndbg.lib.config.PARAM_BOOLEAN,
)
@pwndbg.lib.cache.cache_until("start", "stop")
def get() -> Tuple[pwndbg.lib.memory.Page, ...]:

@ -46,8 +46,11 @@ def print_vmmap_table_header() -> None:
"""
Prints the table header for the vmmap command.
"""
prefer_relpaths = "on" if pwndbg.config.vmmap_prefer_relpaths else "off"
width = 2 + 2 * pwndbg.aglib.arch.ptrsize
print(
f"{'Start':>{2 + 2 * pwndbg.aglib.arch.ptrsize}} {'End':>{2 + 2 * pwndbg.aglib.arch.ptrsize}} {'Perm'} {'Size':>8} {'Offset':>6} {'File'}"
f"{'Start':>{width}} {'End':>{width}} {'Perm'} {'Size':>8} {'Offset':>6} "
f"{'File'} (set vmmap_prefer_relpaths {prefer_relpaths})"
)

@ -5,6 +5,7 @@ Reading, writing, and describing memory.
from __future__ import annotations
import os
from os.path import relpath
import pwndbg.aglib.arch
@ -140,7 +141,15 @@ class Page:
)
def __str__(self) -> str:
return f"{self.vaddr:#{2 + 2 * pwndbg.aglib.arch.ptrsize}x} {self.vaddr + self.memsz:#{2 + 2 * pwndbg.aglib.arch.ptrsize}x} {self.permstr} {self.memsz:8x} {self.offset:6x} {self.objfile or ''}"
if pwndbg.config.vmmap_prefer_relpaths:
rel = relpath(self.objfile)
# Prefer absolute paths for system files (for example libc) since
# `os.path.relpath` returns longer relative paths with no clear benefits.
objfile = self.objfile if rel.startswith("../../") else rel
else:
objfile = self.objfile
width = 2 + 2 * pwndbg.aglib.arch.ptrsize
return f"{self.vaddr:#{width}x} {self.vaddr + self.memsz:#{width}x} {self.permstr} {self.memsz:8x} {self.offset:6x} {objfile or ''}"
def __repr__(self) -> str:
return f"{self.__class__.__name__}({self.__str__()!r})"

@ -74,6 +74,7 @@ def test_command_vmmap_on_coredump_on_crash_simple_binary(start_binary, unload_f
expected_maps = get_proc_maps()
gdb.execute("set vmmap-prefer-relpaths off")
vmmaps = gdb.execute("vmmap", to_string=True).splitlines()
# Basic asserts

Loading…
Cancel
Save