How to import lldb in a python script

10.1k views Asked by At

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
4

There are 4 answers

1
stdout On BEST ANSWER

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
>>> 
1
Dan Cornilescu On

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.

0
Jim Ingham On

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.

0
GrvTyagi On

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

Starting lldb causes the following to happen:

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

This causes issues with, for instance, QtCreator, which stalls when trying to debug anything with lldb.

$ lldb -P gives the following path:

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

However, that path doesn't exist:

$ 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

The package python3-lldb-14 provides this path, though:

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

Creating a symbolic link from /usr/lib/llvm-14/lib/python3.10/dist-packages to /usr/lib/local/lib/python3.10/dist-packages makes things work as expected:

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)