How to import lldb in a python script

狂风中的少年 提交于 2019-12-08 06:38:28

问题


According to LLDB main page, LLDB can be imported in a python script like this:

import lldb

After installing LLDB from a release package (on Lubuntu 15.04: sudo apt-get install lldb), I get the following error:

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

This is expected! the LLDB page says:

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.

So the those confident enough to build LLDB themselves get a clear instruction and the noobs who just want to use a released package are left with a vague explanation...

Does any one figured out what exactly means "adjust the above to suit your needs" for the most basic case where you install everything from release packages ? The path reported by lldb -P does not solve the problem:

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

回答1:


Looks like the symlinks that the lldb python package installs are botched. If you look in /usr/lib/llvm-3.6/lib/python2.7/site-packages/lldb you'll see three broken simlinks referencing the nonexistent x86_64-linux-gnu directory. This fixed it for me (tested on Ubuntu 14.04, not Lubuntu but I'm assuming the issue is the same):

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
>>> 



回答2:


lldb -P apparently attempts to provide the site-packages for a /usr/lib/x86_64-linux-gnu/python2.7 python installation (which, as you observed, doesn't exist).

The traceback you got suggests that lldb was added to a /usr/lib/python2.7 python installation (that's where its __init__.py executes from).

I'd try to set/add to PYTHONPATH the /usr/lib/python2.7/site-packages dir instead of the lldb -P result.




回答3:


You might try asking about this on the lldb-dev mailing list, or even filing a bug with the lldb.llvm.org bugzilla. lldb for Linux is at an earlier stage of development than the OSX version, and it may be that most users on Linux actually do build it themselves in order to get the latest goodness, so nobody has noticed the problem.



来源:https://stackoverflow.com/questions/30869945/how-to-import-lldb-in-a-python-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!