Skip to content

Commit 25c6117

Browse files
authored
Add LLDB (debug) command (#137)
LLDB is the default debugger in Xcode on macOS, but also runs on Linux and Windows. See https://lldb.llvm.org/ Closes #125
1 parent 0f73b5a commit 25c6117

File tree

3 files changed

+73
-3
lines changed

3 files changed

+73
-3
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ python -m spin
8080

8181
### [Meson](https://meson-python.readthedocs.io)
8282

83+
Available as `spin.cmds.meson.*`.
84+
8385
```
8486
build 🔧 Build package with Meson/ninja and install to `build-install`
8587
ipython 💻 Launch IPython shell with PYTHONPATH set
@@ -88,7 +90,8 @@ python -m spin
8890
test 🔧 Run pytest
8991
run 🏁 Run a shell command with PYTHONPATH set
9092
docs 📖 Build Sphinx documentation
91-
gdb 👾 Execute a Python snippet with GDB
93+
gdb 👾 Execute a Python snippet with GDB
94+
lldb 👾 Execute a Python snippet with LLDB
9295
```
9396

9497
### [Build](https://pypa-build.readthedocs.io/en/stable/) (PEP 517 builder)

example_pkg/pyproject.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ package = 'example_pkg'
4040
"spin.cmds.meson.run"
4141
]
4242
"Debug" = [
43-
"spin.cmds.meson.gdb"
43+
"spin.cmds.meson.gdb",
44+
"spin.cmds.meson.lldb"
4445
]
4546
"Extensions" = [".spin/cmds.py:example"]

spin/cmds/meson.py

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ def test(ctx, pytest_args, n_jobs, tests, verbose, coverage=False):
326326
@click.argument("gdb_args", nargs=-1)
327327
@click.pass_context
328328
def gdb(ctx, code, gdb_args):
329-
"""👾 Execute a Python snippet with GDB
329+
"""👾 Execute code through GDB
330330
331331
spin gdb -c 'import numpy as np; print(np.__version__)'
332332
@@ -601,3 +601,69 @@ def docs(ctx, sphinx_target, clean, first_build, jobs, sphinx_gallery_plot):
601601
f"$ export PYTHONPATH={os.environ['PYTHONPATH']}", bold=True, fg="bright_blue"
602602
)
603603
_run(["make", "-C", "doc", sphinx_target], replace=True)
604+
605+
606+
@click.command()
607+
@click.option("--code", "-c", help="Python program passed in as a string")
608+
@click.argument("lldb_args", nargs=-1)
609+
@click.pass_context
610+
def lldb(ctx, code, lldb_args):
611+
"""👾 Execute code through LLDB
612+
613+
spin lldb -c 'import numpy as np; print(np.__version__)'
614+
615+
Or run another program, they way you normally would with LLDB:
616+
617+
\b
618+
spin lldb -- ls -al
619+
620+
You can also run Python programs:
621+
622+
\b
623+
spin lldb -- my_tests.py
624+
spin lldb -- my_tests.py --mytest-flag
625+
626+
And specify LLDB-specific flags:
627+
628+
\b
629+
spin lldb -- --arch x86_64 -- ls -al
630+
spin lldb -- --arch x86_64 -- my_tests.py
631+
spin lldb -c 'import numpy as np; print(np.__version__)' -- --arch x86_64
632+
"""
633+
build_cmd = _get_configured_command("build")
634+
if build_cmd:
635+
click.secho(
636+
"Invoking `build` prior to invoking lldb:", bold=True, fg="bright_green"
637+
)
638+
ctx.invoke(build_cmd)
639+
640+
_set_pythonpath()
641+
lldb_args = list(lldb_args)
642+
643+
if code:
644+
if sys.version_info[:2] >= (3, 11):
645+
PYTHON_FLAGS = ["-P"]
646+
code_prefix = ""
647+
else:
648+
PYTHON_FLAGS = []
649+
code_prefix = "import sys; sys.path.pop(0); "
650+
651+
PYTHON_ARGS = ["-c", code_prefix + code]
652+
program = [sys.executable] + PYTHON_FLAGS + PYTHON_ARGS
653+
else:
654+
if "--" in lldb_args:
655+
ix = lldb_args.index("--")
656+
lldb_args, program = lldb_args[:ix], lldb_args[ix + 1 :]
657+
else:
658+
program, lldb_args = lldb_args, []
659+
660+
if program and program[0].endswith(".py"):
661+
program = [sys.executable] + program
662+
663+
lldb_cmd = (
664+
["lldb", "-O", "settings set target.process.follow-fork-mode child"]
665+
+ lldb_args
666+
+ ["--"]
667+
+ program
668+
)
669+
_run(lldb_cmd, replace=True)

0 commit comments

Comments
 (0)