Skip to content

Commit

Permalink
corelens: gracefully handle interrupt and broken pipe
Browse files Browse the repository at this point in the history
SIGINT and SIGPIPE are handled by Python as exceptions. It's common to
get a SIGINT with a keyboard interrupt, and in these cases the user
typically doesn't want a traceback, they just want to stop corelens. For
broken pipe, this is relatively common for "corelens ... | head -n $num"
commands where a user wants to get just the first few lines. An
exception here would print more than they expect, and would be
unhelpful. Handle both of these so corelens behaves more like other
system utilities.

Signed-off-by: Stephen Brennan <[email protected]>
  • Loading branch information
brenns10 committed May 30, 2024
1 parent 6a202bc commit 834a94e
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion drgn_tools/corelens.py
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,8 @@ def _run_module(
if print_header:
print(f"\n====== MODULE {mod.name} ======")
mod.run(prog, args)
except (KeyboardInterrupt, BrokenPipeError):
raise
except Exception:
formatted = traceback.format_exc()
errors.append(
Expand Down Expand Up @@ -791,4 +793,9 @@ def cl(cl_cmd: str) -> None:
# terrible, terrible corner of Python.
import drgn_tools.corelens

drgn_tools.corelens.main()
try:
drgn_tools.corelens.main()
except KeyboardInterrupt:
sys.exit("interrupted")
except BrokenPipeError:
pass

0 comments on commit 834a94e

Please sign in to comment.