Skip to content

Commit

Permalink
make telescope print return addresses on the stack
Browse files Browse the repository at this point in the history
  • Loading branch information
smiley committed Aug 25, 2023
1 parent 89761f4 commit d810007
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 16 deletions.
29 changes: 16 additions & 13 deletions pwndbg/commands/telescope.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,29 +130,32 @@ def telescope(address=None, count=telescope_lines, to_string=False, reverse=Fals
count -= address
count = max(math.ceil(count / ptrsize), 1)

reg_values = collections.defaultdict(lambda: [])
name_values = collections.defaultdict(lambda: [])
for reg in pwndbg.gdblib.regs.common:
reg_values[pwndbg.gdblib.regs[reg]].append(reg)
name_values[pwndbg.gdblib.regs[reg]].append(reg)

for idx, retaddr in enumerate(pwndbg.gdblib.stack.yield_return_addresses()):
name_values[retaddr].append(f"ret{idx}")

start = address
stop = address + (count * ptrsize)
step = ptrsize

# Find all registers which show up in the trace
regs = {}
# Find all names which show up in the trace
names = {}
for i in range(start, stop, step):
values = list(reg_values[i])
values = list(name_values[i])

for width in range(1, pwndbg.gdblib.arch.ptrsize):
values.extend("%s-%i" % (r, width) for r in reg_values[i + width])
values.extend("%s-%i" % (r, width) for r in name_values[i + width])

regs[i] = " ".join(values)
names[i] = " ".join(values)

# Find the longest set of register information
if regs:
longest_regs = max(map(len, regs.values()))
# Find the longest set of names
if names:
longest_names = max(map(len, names.values()))
else:
longest_regs = 0
longest_names = 0

Check warning on line 158 in pwndbg/commands/telescope.py

View check run for this annotation

Codecov / codecov/patch

pwndbg/commands/telescope.py#L158

Added line #L158 was not covered by tests

# Print everything out
result = []
Expand All @@ -164,7 +167,7 @@ def telescope(address=None, count=telescope_lines, to_string=False, reverse=Fals
+ 4
+ len(offset_separator)
+ 1
+ longest_regs
+ longest_names
+ 1
- len(repeating_marker)
)
Expand Down Expand Up @@ -200,7 +203,7 @@ def collapse_repeating_values() -> None:
separator,
)
),
T.register(regs[addr].ljust(longest_regs)),
T.register(names[addr].ljust(longest_names)),
pwndbg.chain.format(addr),
)
)
Expand Down
6 changes: 3 additions & 3 deletions tests/gdb-tests/tests/test_command_telescope.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@ def test_telescope_command_with_address_as_count(start_binary):
rsp = pwndbg.gdblib.regs.rsp

assert len(out) == 2
assert out[0] == "00:0000│ rsp %#x ◂— 0x1" % rsp
assert out[0] == "00:0000│ rsp ret0 %#x ◂— 0x1" % rsp

expected = rf"01:0008│ {rsp + 8:#x} —▸ 0x[0-9a-f]+ ◂— '{pwndbg.gdblib.proc.exe}'"
expected = rf"01:0008│ ret1 {rsp + 8:#x} —▸ 0x[0-9a-f]+ ◂— '{pwndbg.gdblib.proc.exe}'"
assert re.search(expected, out[1])


Expand All @@ -83,7 +83,7 @@ def test_telescope_command_with_address_as_count_and_reversed_flag(start_binary)
out = gdb.execute("telescope -r 2", to_string=True).splitlines()
rsp = pwndbg.gdblib.regs.rsp

assert out == ["00:0000│ %#x ◂— 0x0" % (rsp - 8), "01:0008│ rsp %#x ◂— 0x1" % rsp]
assert out == ["00:0000│ %#x ◂— 0x0" % (rsp - 8), "01:0008│ rsp ret0 %#x ◂— 0x1" % rsp]


def test_command_telescope_reverse_skipped_records_shows_input_address(start_binary):
Expand Down

0 comments on commit d810007

Please sign in to comment.