Skip to content

Commit

Permalink
Handle GDB hating quotes for set logging
Browse files Browse the repository at this point in the history
  • Loading branch information
Grazfather committed Aug 26, 2023
1 parent 7efa076 commit b989829
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions gef.py
Original file line number Diff line number Diff line change
Expand Up @@ -1935,14 +1935,15 @@ def __exit__(self, *exc: Any) -> None:


class RedirectOutputContext:
def __init__(self, to: str = "/dev/null") -> None:
self.redirection_target_file = to
def __init__(self, to_file: str = "/dev/null") -> None:
if " " in to_file: raise Exception("Target filepath cannot contain spaces")
self.redirection_target_file = to_file
return

def __enter__(self) -> None:
"""Redirect all GDB output to `to_file` parameter. By default, `to_file` redirects to `/dev/null`."""
gdb.execute("set logging overwrite")
gdb.execute(f"set logging file '{self.redirection_target_file}'")
gdb.execute(f"set logging file {self.redirection_target_file}")
gdb.execute("set logging redirect on")
gdb.execute("set logging on")
return
Expand All @@ -1956,8 +1957,9 @@ def __exit__(self, *exc: Any) -> None:

def enable_redirect_output(to_file: str = "/dev/null") -> None:
"""Redirect all GDB output to `to_file` parameter. By default, `to_file` redirects to `/dev/null`."""
if " " in to_file: raise Exception("Target filepath cannot contain spaces")
gdb.execute("set logging overwrite")
gdb.execute(f"set logging file '{to_file}'")
gdb.execute(f"set logging file {to_file}")
gdb.execute("set logging redirect on")
gdb.execute("set logging on")
return
Expand Down Expand Up @@ -8786,7 +8788,7 @@ def get_frames_size(self) -> int:
def trace(self, loc_start: int, loc_end: int, depth: int) -> None:
info(f"Tracing from {loc_start:#x} to {loc_end:#x} (max depth={depth:d})")
logfile = f"{self['tracefile_prefix']}{loc_start:#x}-{loc_end:#x}.txt"
with RedirectOutputContext(to=logfile):
with RedirectOutputContext(to_file=logfile):
hide_context()
self.start_tracing(loc_start, loc_end, depth)
unhide_context()
Expand Down Expand Up @@ -9184,7 +9186,7 @@ def do_invoke(self, _: List[str]) -> None:

nb_installed_breaks = 0

with RedirectOutputContext(to="/dev/null"):
with RedirectOutputContext(to_file="/dev/null"):
for function_name in dangerous_functions:
argument_number = dangerous_functions[function_name]
FormatStringBreakpoint(function_name, argument_number)
Expand Down

0 comments on commit b989829

Please sign in to comment.