Skip to content

Commit 5cec941

Browse files
committed
corelens: catch ctrl-c and pipe errors properly
While we do have logic to catch ctrl-c and broken pipe errors, the logic doesn't apply to /usr/bin/corelens, due to the way the script is created by pip/setuptools. Fix this, and while we're at it, de-mystify a comment I wrote regarding the corelens startup sequence. Orabug: 37894865 Signed-off-by: Stephen Brennan <[email protected]>
1 parent 7083f47 commit 5cec941

File tree

1 file changed

+26
-9
lines changed

1 file changed

+26
-9
lines changed

drgn_tools/corelens.py

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -617,7 +617,7 @@ def _version_string() -> str:
617617
return f"drgn-tools {__version__}, {drgn_version_header()}, {ctf_str}"
618618

619619

620-
def main() -> None:
620+
def _do_main() -> None:
621621
corelens_begin_time = time.time()
622622
parser = argparse.ArgumentParser(
623623
description="Kernel core dump analysis tool",
@@ -792,6 +792,20 @@ def info_msg(*args, **kwargs):
792792
sys.exit(1)
793793

794794

795+
def main() -> None:
796+
# Wrap _do_main() with standard Ctrl-C and pipe handling. We do this here,
797+
# rather than in the "if __name__ == '__main__'" area below, because this is
798+
# the entry point called by the command line stub that actually gets
799+
# installed to /usr/bin. If we included it below, then the /usr/bin/corelens
800+
# script wouldn't get these error handlers.
801+
try:
802+
_do_main()
803+
except KeyboardInterrupt:
804+
sys.exit("interrupted")
805+
except BrokenPipeError:
806+
pass
807+
808+
795809
def run(prog: Program, cl_cmd: str) -> None:
796810
"""
797811
Run a single corelens command
@@ -834,13 +848,16 @@ def cl(cl_cmd: str) -> None:
834848

835849

836850
if __name__ == "__main__":
837-
# Please, do not ask too many questions about this line. Please. It is a
838-
# terrible, terrible corner of Python.
851+
# When the module is run directly, as in "python -m drgn_tools.corelens",
852+
# Python actually creates this module with the name "__main__", rather than
853+
# drgn_tools.corelens. Later on, when other code imports
854+
# drgn_tools.corelens, this module is actually re-read and a new module with
855+
# the correct name is created. That means that there are actually two copies
856+
# of the class CorelensModule: the one defined in __main__, and the one
857+
# defined in drgn_tools.corelens. Nothing inherits from the one in __main__,
858+
# so if we run __main__.main(), we don't detect any corelens modules. We
859+
# need to run drgn_tools.corelens.main() in order to actually get a
860+
# functional program.
839861
import drgn_tools.corelens
840862

841-
try:
842-
drgn_tools.corelens.main()
843-
except KeyboardInterrupt:
844-
sys.exit("interrupted")
845-
except BrokenPipeError:
846-
pass
863+
drgn_tools.corelens.main()

0 commit comments

Comments
 (0)