Skip to content

Commit

Permalink
Cleanup session managers to encapsulate & defer better (#1066)
Browse files Browse the repository at this point in the history
`gef.session` points to a session manager and keeps state, but was often
referencing `gef.session` instead of `self`. Also, some checks for
remote sessions were unnecessary: Since the session has a remote
attribute, callers on the session can count on the session manager to
provide the correct data/function
  • Loading branch information
Grazfather authored Jan 31, 2024
1 parent c17a0d2 commit 4b47c6d
Showing 1 changed file with 9 additions and 14 deletions.
23 changes: 9 additions & 14 deletions gef.py
Original file line number Diff line number Diff line change
Expand Up @@ -9189,13 +9189,8 @@ def __init__(self):
def do_invoke(self, argv: List[str]) -> None:
readelf = gef.session.constants["readelf"]

if is_remote_debug():
assert gef.session.remote
elf_file = str(gef.session.remote.lfile)
elf_virtual_path = str(gef.session.remote.file)
else:
elf_file = str(gef.session.file)
elf_virtual_path = str(gef.session.file)
elf_file = str(gef.session.file)
elf_virtual_path = str(gef.session.file)

func_names_filter = argv if argv else []
vmmap = gef.memory.maps
Expand Down Expand Up @@ -10996,7 +10991,7 @@ def os(self) -> str:
def pid(self) -> int:
"""Return the PID of the target process."""
if not self._pid:
pid = gdb.selected_inferior().pid if not gef.session.qemu_mode else gdb.selected_thread().ptid[1]
pid = gdb.selected_inferior().pid if not self.qemu_mode else gdb.selected_thread().ptid[1]
if not pid:
raise RuntimeError("cannot retrieve PID for target process")
self._pid = pid
Expand All @@ -11005,8 +11000,8 @@ def pid(self) -> int:
@property
def file(self) -> Optional[pathlib.Path]:
"""Return a Path object of the target process."""
if gef.session.remote is not None:
return gef.session.remote.file
if self.remote is not None:
return self.remote.file
progspace = gdb.current_progspace()
assert progspace
fpath: str = progspace.filename
Expand All @@ -11016,8 +11011,8 @@ def file(self) -> Optional[pathlib.Path]:

@property
def cwd(self) -> Optional[pathlib.Path]:
if gef.session.remote is not None:
return gef.session.remote.root
if self.remote is not None:
return self.remote.root
return self.file.parent if self.file else None

@property
Expand Down Expand Up @@ -11061,8 +11056,8 @@ def maps(self) -> Optional[pathlib.Path]:
if not is_alive():
return None
if not self._maps:
if gef.session.remote is not None:
self._maps = gef.session.remote.maps
if self.remote is not None:
self._maps = self.remote.maps
else:
self._maps = pathlib.Path(f"/proc/{self.pid}/maps")
return self._maps
Expand Down

0 comments on commit 4b47c6d

Please sign in to comment.