Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add generic error handler and debug mode #705

Merged
merged 2 commits into from
May 21, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 30 additions & 19 deletions dissect/target/tools/shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -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_"):
Expand Down Expand Up @@ -179,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."""
Expand Down Expand Up @@ -279,6 +277,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."""
Expand Down Expand Up @@ -1241,7 +1247,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


Expand Down