如何在Python脚本中导入lldb

5
根据LLDB主页的介绍,可以像以下这样将LLDB导入到Python脚本中:
import lldb

在从发布包中安装LLDB之后(在Lubuntu 15.04上:sudo apt-get install lldb),我收到以下错误:

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/dist-packages/lldb/__init__.py", line 52, in <module>
_lldb = swig_import_helper()
File "/usr/lib/python2.7/dist-packages/lldb/__init__.py", line 44, in swig_import_helper
ImportError: No module named _lldb

这是预期的!LLDB页面上写道:

LLDB has a Python scripting capability and supplies its own Python module named lldb. If a script is run inside the command line lldb application, the Python module is made available automatically. However, if a script is to be run by a Python interpreter outside the command line application, the PYTHONPATH environment variable can be used to let the Python interpreter find the lldb module.

The correct path can be obtained by invoking the command line lldb tool with the -P flag:

> export PYTHONPATH=`$llvm/build/Debug+Asserts/bin/lldb -P`

If you used a different build directory or made a release build, you may need to adjust the above to suit your needs.

所以那些有信心自己构建LLDB的人会得到清晰的指导,而只想使用发布包的新手则只能得到模糊的解释...

有人弄清楚了“根据需要调整上面的内容”具体意味着什么吗?尤其是在你从发布包中安装所有内容的最基本情况下?lldb -P报告的路径并不能解决这个问题:

user@user-VirtualBox:~$ lldb -P
/usr/lib/x86_64-linux-gnu/python2.7/site-packages
user@user-VirtualBox:~$ ls /usr/lib/x86_64-linux-gnu/python2.7/site-packages
ls: cannot access /usr/lib/x86_64-linux-gnu/python2.7/site-packages: No such file or directory
4个回答

16

看起来lldb Python包安装的符号链接有问题。如果你查看/usr/lib/llvm-3.6/lib/python2.7/site-packages/lldb,你会发现三个破损的符号链接引用了不存在的x86_64-linux-gnu目录。以下方法解决了我的问题(在Ubuntu 14.04上测试通过,虽然我假设问题是相同的,但不是Lubuntu):

cd /usr/lib/llvm-3.6/lib/python2.7/site-packages/lldb
sudo ln -sf ../../../liblldb.so.1 _lldb.so
sudo ln -sf ../../../libLLVM-3.6.0.so.1 libLLVM-3.6.0.so.1
sudo ln -sf ../../../libLLVM-3.6.0.so.1 libLLVM-3.6.so.1
export PYTHONPATH='/usr/lib/llvm-3.6/lib/python2.7/site-packages'
vagrant@Ubuntu:~$ python
Python 2.7.6 (default, Mar 22 2014, 22:59:56) 
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import lldb
>>> 

FYI - 在按照这些指示在Ubuntu 16.04.3上操作后,我还需要使用apt-get install python-six安装six模块。 - zekel

2

lldb -P 看起来尝试为 /usr/lib/x86_64-linux-gnu/python2.7 的 Python 安装提供 site-packages (正如您所观察到的,该目录不存在)。

您得到的回溯信息表明 lldb 已添加到 /usr/lib/python2.7 Python 安装中(这是其 __init__.py 执行的位置)。

我建议将 /usr/lib/python2.7/site-packages 目录设置/添加到 PYTHONPATH 中,而不是使用 lldb -P 的结果。


谢谢您的建议,但不幸的是我仍然收到完全相同的错误消息 :-( - acapola

1

https://bugs.launchpad.net/ubuntu/+source/llvm-defaults/+bug/1972855

启动lldb会引起以下事件:

$ lldb
Traceback (most recent call last):
  File "<string>", line 1, in <module>
ModuleNotFoundError: No module named 'lldb.embedded_interpreter'
(lldb)

这会导致问题,例如,QtCreator在尝试使用lldb调试时会停止运行。 < p > $ lldb -P 给出以下路径:

/usr/lib/local/lib/python3.10/dist-packages

然而,该路径不存在:

$ ls -l /usr/lib/local/lib/python3.10/dist-packages
ls: cannot access '/usr/lib/local/lib/python3.10/dist-packages': No such file or directory

然而,Python3-lldb-14包提供了这个路径:

/usr/lib/llvm-14/lib/python3.10/dist-packages/

创建一个符号链接,从/usr/lib/llvm-14/lib/python3.10/dist-packages/usr/lib/local/lib/python3.10/dist-packages,可以使事情按预期工作:

sudo ln -s /usr/lib/llvm-14/lib/python3.10/dist-packages /usr/lib/local/lib/python3.10/dist-packages

$ ls -l /usr/lib/local/lib/python3.10/dist-packages && lldb
lrwxrwxrwx 1 root root 45 May 10 15:23 /usr/lib/local/lib/python3.10/dist-packages -> /usr/lib/llvm-14/lib/python3.10/dist-packages
(lldb)

0
你可以尝试在lldb-dev邮件列表上询问此事,甚至向lldb.llvm.org bugzilla提交错误报告。Linux上的lldb开发阶段比OSX版本早,可能大多数Linux用户实际上都会自己构建它以获取最新的功能,因此没有人注意到这个问题。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接