Skip to content

Commit

Permalink
Add gdb command from numpy
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanv committed Sep 8, 2023
1 parent c06bf60 commit 524ce41
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
1 change: 1 addition & 0 deletions .github/workflows/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ prun spin test
prun spin sdist
prun spin example
prun spin docs
prun spin gdb -c 'import example_pkg; example_pkg.echo("hi")' -- --eval "run" --batch
3 changes: 3 additions & 0 deletions example_pkg/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,7 @@ package = 'example_pkg'
"spin.cmds.meson.python",
"spin.cmds.meson.run"
]
"Debug" = [
"spin.cmds.meson.gdb"
]
"Extensions" = [".spin/cmds.py:example"]
45 changes: 45 additions & 0 deletions spin/cmds/meson.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,51 @@ def test(ctx, pytest_args, coverage=False):
)


@click.command()
@click.option("--code", "-c", help="Python program passed in as a string")
@click.argument("gdb_args", nargs=-1)
def gdb(code, gdb_args):
"""👾 Execute a Python snippet with GDB
spin gdb -c 'import numpy as np; print(np.__version__)'
Or pass arguments to gdb:
spin gdb -c 'import numpy as np; print(np.__version__)' -- --fullname
Or run another program, they way you normally would with gdb:
\b
spin gdb ls
spin gdb -- --args ls -al
You can also run Python programs:
\b
spin gdb my_tests.py
spin gdb -- my_tests.py --mytest-flag
"""
_set_pythonpath()
gdb_args = list(gdb_args)

if gdb_args and gdb_args[0].endswith(".py"):
gdb_args = ["--args", sys.executable] + gdb_args

if sys.version_info[:2] >= (3, 11):
PYTHON_FLAGS = ["-P"]
code_prefix = ""
else:
PYTHON_FLAGS = []
code_prefix = "import sys; sys.path.pop(0); "

if code:
PYTHON_ARGS = ["-c", code_prefix + code]
gdb_args += ["--args", sys.executable] + PYTHON_FLAGS + PYTHON_ARGS

gdb_cmd = ["gdb", "-ex", "set detach-on-fork on"] + gdb_args
_run(gdb_cmd, replace=True)


@click.command()
@click.argument("ipython_args", nargs=-1)
def ipython(ipython_args):
Expand Down

0 comments on commit 524ce41

Please sign in to comment.