如何在MacOS上将lldb附加到python.org安装的Python?
问题:LLDB无法附加到python.org安装的Python 3.10.6
环境信息
System: Intel MacBook Pro 16" Mac OS 12.5 > lldb --version lldb-1316.0.9.46 Apple Swift version 5.6.1 (swiftlang-5.6.0.323.66 clang-1316.0.20.12)
测试案例
# hello_world.py print("hello world")
问题现象
使用python.org官网安装的Python 3.10.6时,LLDB附加进程失败:
> lldb python3 hello_world.py (lldb) target create "python3" Current executable set to 'python3' (x86_64). (lldb) settings set -- target.run-args "hello_world.py" (lldb) run error: process exited with status -1 (attach failed (Not allowed to attach to process. Look in the console messages (Console.app), near the debugserver entries, when the attach failed. The subsystem that denied the attach permission will likely have logged an informative message about why it was denied.)) (lldb)
即使加sudo执行LLDB也无法解决,且Console.app中无相关日志。但conda环境下的Python可正常被LLDB附加:
> lldb python3 hello_world.py (lldb) target create "python3" Current executable set to 'python3' (x86_64). (lldb) settings set -- target.run-args "hello_world.py" (lldb) run Process 24402 launched: '/Users/vince/miniconda3/envs/py39/bin/python3' (x86_64) hello world Process 24402 exited with status = 0 (0x00000000) (lldb)
解决方法
给LLDB添加完全磁盘访问权限
- 打开系统设置,进入「隐私与安全性」→「完全磁盘访问权限」
- 点击列表下方的加号,找到
/usr/bin/lldb并添加到允许列表 - 关闭并重新打开LLDB,再尝试附加进程
移除Python的签名(针对python.org安装的版本)
执行以下命令(替换为你实际的Python路径):sudo codesign --remove-signature /Library/Frameworks/Python.framework/Versions/3.10/bin/python3.10执行后直接用LLDB启动Python即可
临时关闭系统完整性保护(SIP)(谨慎操作,会降低系统安全性)
- 重启Mac,按住Command+R进入恢复模式
- 打开「实用工具」菜单下的终端
- 输入
csrutil disable并回车执行 - 重启系统后再尝试LLDB附加操作
(若后续需要恢复SIP,重复上述步骤,执行csrutil enable即可)
内容的提问来源于stack exchange,提问作者Vince W.
相关产品推荐
相关产品推荐

