Skip to content

Commit

Permalink
fix/remote-vmmap-updates
Browse files Browse the repository at this point in the history
  • Loading branch information
robwaz committed Feb 16, 2024
1 parent db5b7b8 commit 8a7fabd
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
15 changes: 10 additions & 5 deletions gef.py
Original file line number Diff line number Diff line change
Expand Up @@ -11137,16 +11137,12 @@ def sync(self, src: str, dst: Optional[str] = None) -> bool:
if not dst:
dst = src
tgt = self.root / dst.lstrip("/")
if tgt.exists():
return True
tgt.parent.mkdir(parents=True, exist_ok=True)
dbg(f"[remote] downloading '{src}' -> '{tgt}'")
gdb.execute(f"remote get '{src}' '{tgt.absolute()}'")
return tgt.exists()

def connect(self, pid: int) -> bool:
"""Connect to remote target. If in extended mode, also attach to the given PID."""
# before anything, register our new hook to download files from the remote target
def __register_objfile_handlers(self):
dbg(f"[remote] Installing new objfile handlers")
try:
gef_on_new_unhook(new_objfile_handler)
Expand All @@ -11156,6 +11152,11 @@ def connect(self, pid: int) -> bool:

gef_on_new_hook(self.remote_objfile_event_handler)

def connect(self, pid: int) -> bool:
"""Connect to remote target. If in extended mode, also attach to the given PID."""
# before anything, register our new hook to download files from the remote target
self.__register_objfile_handlers()

# then attempt to connect
is_extended_mode = (pid > -1)
dbg(f"[remote] Enabling extended remote: {bool(is_extended_mode)}")
Expand All @@ -11174,6 +11175,7 @@ def connect(self, pid: int) -> bool:
return False

def setup(self) -> bool:
self.__register_objfile_handlers()
# setup remote adequately depending on remote or qemu mode
if self.in_qemu_user():
dbg(f"Setting up as qemu session, target={self.__qemu}")
Expand Down Expand Up @@ -11246,6 +11248,9 @@ def remote_objfile_event_handler(self, evt: "gdb.events.NewObjFileEvent") -> Non
warn(f"[remote] skipping '{evt.new_objfile.filename}'")
return
if evt.new_objfile.filename.startswith("target:"):
map_file = f"/proc/{self.pid}/maps"
if not self.sync(map_file):
raise FileNotFoundError(f"Failed to sync '{map_file}'")
src: str = evt.new_objfile.filename[len("target:"):]
if not self.sync(src):
raise FileNotFoundError(f"Failed to sync '{src}'")
Expand Down
17 changes: 17 additions & 0 deletions tests/api/gef_memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,3 +145,20 @@ def test_func_parse_maps_remote_qemu(self):
gdb.execute(cmd)
sections = gef.memory.maps
assert len(sections) > 0

def test_func_parse_maps_old_remote_gdbserver(self):
gef, gdb = self._gef, self._gdb
# When in a gdb remote session objfile handlers should
# trigger memory map updates
while True:
port = random.randint(1025, 65535)
if port != self._port:
break

with gdbserver_session(port=port) as _:
gdb.execute(f"target remote {GDBSERVER_DEFAULT_HOST}:{port}")
initial_cnt = len(gef.memory.maps)
gdb.execute(f"break _start")
gdb.execute(f"continue")
start_cnt = len(gef.memory.maps)
assert start_cnt > initial_cnt

0 comments on commit 8a7fabd

Please sign in to comment.