From 524ce41011b1c3768fbbe2fa5dca26a717a09b47 Mon Sep 17 00:00:00 2001 From: Stefan van der Walt Date: Fri, 8 Sep 2023 13:09:09 -0700 Subject: [PATCH] Add gdb command from numpy --- .github/workflows/test.sh | 1 + example_pkg/pyproject.toml | 3 +++ spin/cmds/meson.py | 45 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 49 insertions(+) diff --git a/.github/workflows/test.sh b/.github/workflows/test.sh index 4dd5422..d73fa6a 100644 --- a/.github/workflows/test.sh +++ b/.github/workflows/test.sh @@ -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 diff --git a/example_pkg/pyproject.toml b/example_pkg/pyproject.toml index 8886419..863c07d 100644 --- a/example_pkg/pyproject.toml +++ b/example_pkg/pyproject.toml @@ -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"] diff --git a/spin/cmds/meson.py b/spin/cmds/meson.py index 1ca3672..98e0544 100644 --- a/spin/cmds/meson.py +++ b/spin/cmds/meson.py @@ -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):