From 6a94e0c856e7b7156ceef4af4417bbbac9a131f0 Mon Sep 17 00:00:00 2001 From: cecinestpasunepipe <110607403+cecinestpasunepipe@users.noreply.github.com> Date: Tue, 14 May 2024 18:31:30 +0200 Subject: [PATCH 1/2] Add generic error handler and debug mode (DIS-2558) --- dissect/target/tools/shell.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/dissect/target/tools/shell.py b/dissect/target/tools/shell.py index 2471cd490..d7fde0c3f 100644 --- a/dissect/target/tools/shell.py +++ b/dissect/target/tools/shell.py @@ -113,6 +113,7 @@ class TargetCmd(cmd.Cmd): def __init__(self, target: Target): cmd.Cmd.__init__(self) self.target = target + self.debug = False def __getattr__(self, attr: str) -> Any: if attr.startswith("help_"): @@ -279,6 +280,14 @@ def do_exit(self, line: str) -> Optional[bool]: """exit shell""" return True + def do_debug(self, line: str) -> Optional[bool]: + """toggle debug mode""" + self.debug = not self.debug + if self.debug: + print("Debug mode on") + else: + print("Debug mode off") + class TargetHubCli(cmd.Cmd): """Hub Cli for interacting with multiple targets.""" @@ -1241,7 +1250,12 @@ def run_cli(cli: cmd.Cmd) -> None: print() pass except Exception as e: - log.exception(e) + if cli.debug: + log.exception(e) + else: + log.info(e) + print(f"*** Unhandled error: {e}") + print("If you wish to see the full debug trace, enable debug mode.") pass From 09e093b2eeaa665cf4afb018c23d80e42de74e99 Mon Sep 17 00:00:00 2001 From: cecinestpasunepipe <110607403+cecinestpasunepipe@users.noreply.github.com> Date: Thu, 16 May 2024 15:08:56 +0200 Subject: [PATCH 2/2] Unhide IOError. --- dissect/target/tools/shell.py | 33 +++++++++++++++------------------ 1 file changed, 15 insertions(+), 18 deletions(-) diff --git a/dissect/target/tools/shell.py b/dissect/target/tools/shell.py index d7fde0c3f..73dae6e9b 100644 --- a/dissect/target/tools/shell.py +++ b/dissect/target/tools/shell.py @@ -180,25 +180,22 @@ def _exec( lexer.whitespace_split = True argparts = list(lexer) - try: - if "|" in argparts: - pipeidx = argparts.index("|") - argparts, pipeparts = argparts[:pipeidx], argparts[pipeidx + 1 :] - try: - with build_pipe_stdout(pipeparts) as pipe_stdin: - return func(argparts, pipe_stdin) - except OSError as e: - # in case of a failure in a subprocess - print(e) - else: - ctx = contextlib.nullcontext() - if self.target.props.get("cyber") and not no_cyber: - ctx = cyber.cyber(color=None, run_at_end=True) + if "|" in argparts: + pipeidx = argparts.index("|") + argparts, pipeparts = argparts[:pipeidx], argparts[pipeidx + 1 :] + try: + with build_pipe_stdout(pipeparts) as pipe_stdin: + return func(argparts, pipe_stdin) + except OSError as e: + # in case of a failure in a subprocess + print(e) + else: + ctx = contextlib.nullcontext() + if self.target.props.get("cyber") and not no_cyber: + ctx = cyber.cyber(color=None, run_at_end=True) - with ctx: - return func(argparts, sys.stdout) - except IOError: - pass + with ctx: + return func(argparts, sys.stdout) def _exec_command(self, command: str, command_args_str: str) -> Optional[bool]: """Command execution helper for ``cmd_`` commands."""