Skip to content

Commit

Permalink
Example support for drgn CLI commands
Browse files Browse the repository at this point in the history
This implements a "cl" command using the drgn CLI command framework
proposed in osandov/drgn#367. The "cl" command invokes Corelens modules
as sub-commands.

Signed-off-by: Stephen Brennan <stephen.s.brennan@oracle.com>
  • Loading branch information
brenns10 committed Nov 9, 2023
1 parent 22d287c commit fe54258
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
17 changes: 16 additions & 1 deletion drgn_tools/corelens.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@
import traceback
from fnmatch import fnmatch
from pathlib import Path
from typing import Any
from typing import Callable
from typing import Dict
from typing import List
from typing import Optional
from typing import Tuple

import drgn.cli
from drgn import Program

from drgn_tools.debuginfo import find_debuginfo
Expand Down Expand Up @@ -469,7 +471,11 @@ def run(prog: Program, cl_cmd: str) -> None:
"""
cmd = shlex.split(cl_cmd)
module_name, args = cmd[0], cmd[1:]
module = all_corelens_modules()[module_name]
try:
module = all_corelens_modules()[module_name]
except KeyError:
print(f"{module_name}: corelens command not found")
return
try:
ns = module._parse_args(args, exit_on_error=False)
except _CorelensArgparseEscapeException:
Expand Down Expand Up @@ -497,6 +503,15 @@ def cl(cl_cmd: str) -> None:
return cl


@drgn.cli.command("cl")
def cl(prog: Program, cmd: str, locals_: Dict[str, Any]) -> None:
args = cmd.split(maxsplit=1)
if len(args) == 2:
run(prog, cmd.split(maxsplit=1)[1])
else:
_print_module_listing()


if __name__ == "__main__":
# Please, do not ask too many questions about this line. Please. It is a
# terrible, terrible corner of Python.
Expand Down
3 changes: 3 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,5 +104,8 @@ def get_version():
"DRGN=drgn_tools.cli:main",
"corelens=drgn_tools.corelens:main",
],
"drgn.command.v1": [
"corelens=drgn_tools.corelens",
],
},
)

0 comments on commit fe54258

Please sign in to comment.