Skip to content

Commit

Permalink
allow older gdb to fallback on procfs
Browse files Browse the repository at this point in the history
  • Loading branch information
hugsy committed Jan 19, 2024
1 parent 8b9464d commit 10b3011
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 8 deletions.
13 changes: 10 additions & 3 deletions gef.py
Original file line number Diff line number Diff line change
Expand Up @@ -10499,6 +10499,7 @@ def parse_procfs_maps() -> Generator[Section, None, None]:
if not procfs_mapfile:
is_remote = gef.session.remote is not None
raise FileNotFoundError(f"Missing {'remote ' if is_remote else ''}procfs map file")

with procfs_mapfile.open("r") as fd:
for line in fd:
line = line.strip()
Expand Down Expand Up @@ -10526,10 +10527,17 @@ def parse_procfs_maps() -> Generator[Section, None, None]:
@staticmethod
def parse_gdb_info_proc_maps() -> Generator[Section, None, None]:
"""Get the memory mapping from GDB's command `maintenance info sections` (limited info)."""

if GDB_VERSION < (10, 0):
raise AttributeError("Disregarding old format")

lines = (gdb.execute("info proc mappings", to_string=True) or "").splitlines()

# See expected format in tests/api/gef_memory.py:test_api_gef_memory_parse_info_proc_maps*
if len(lines) < 5:
# See expected format in tests/api/gef_memory.py:test_api_gef_memory_parse_info_proc_maps*
return
raise AttributeError

# Format seems valid, iterate to generate sections
for line in lines[4:]:
if not line:
break
Expand Down Expand Up @@ -10968,7 +10976,6 @@ def original_canary(self) -> Optional[Tuple[int, int]]:
canary &= ~0xFF
return canary, canary_location


@property
def maps(self) -> Optional[pathlib.Path]:
"""Returns the Path to the procfs entry for the memory mapping."""
Expand Down
17 changes: 13 additions & 4 deletions tests/api/gef_memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,14 @@ def test_api_gef_memory_only_running(self):
assert gef.memory.maps is not None

def test_api_gef_memory_parse_info_proc_maps_expected_format(self):
if self.gdb_version < (10, 0):
pytest.skip(f"Skipping test for version {self.gdb_version} (min 10.0)")

gdb, root = self._gdb, self._conn.root
gdb.execute("start")

#
# The function assumes the following output format (as of GDB 8.3+) for `info proc mappings`
# The function assumes the following output format (as of GDB 10+) for `info proc mappings`
# """"
# process 61789
# Mapped address spaces:
Expand All @@ -60,7 +63,7 @@ def test_api_gef_memory_parse_info_proc_maps_expected_format(self):
size = int(parts[2], 16)
int(parts[3], 16)
assert end_addr == start_addr + size
assert len(parts[4]) == 4
assert len(parts[4]) == 4, parts[4]
Permission = root.eval("Permission")
Permission.from_process_maps(parts[4])

Expand All @@ -78,8 +81,14 @@ def test_api_gef_memory_parse_info_proc_maps(self):

Section = root.eval("Section")

for section in gef.memory.parse_gdb_info_proc_maps():
assert isinstance(section, Section)
if self.gdb_version < (10, 0):
# expect an exception
with pytest.raises(AttributeError):
next(gef.memory.parse_gdb_info_proc_maps())

else:
for section in gef.memory.parse_gdb_info_proc_maps():
assert isinstance(section, Section)

def test_func_parse_permissions(self):
root = self._conn.root
Expand Down
11 changes: 10 additions & 1 deletion tests/base.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import os
import pathlib
import random
import re
import subprocess
import tempfile
import time
from typing import Tuple
import unittest

import rpyc
Expand All @@ -26,7 +28,6 @@ class RemoteGefUnitTestGeneric(unittest.TestCase):
"""

def setUp(self) -> None:

attempt = RPYC_MAX_REMOTE_CONNECTION_ATTEMPTS
while True:
try:
Expand Down Expand Up @@ -106,3 +107,11 @@ def tearDown(self) -> None:
self._conn.close()
self._process.terminate()
return super().tearDown()

@property
def gdb_version(self) -> Tuple[int, int]:
res = tuple(
map(int, re.search(r"(\d+)[^\d]+(\d+)", self._gdb.VERSION).groups())
)
assert len(res) > 2
return (res[0], res[1])

0 comments on commit 10b3011

Please sign in to comment.