[lldb-dev] Various questions about lldbinit and python scripts.

Greg Clayton gclayton at apple.com
Mon Oct 7 16:40:53 PDT 2013

Comments below.

On Oct 4, 2013, at 7:40 PM, Jean-Yves Avenard <jyavenard at gmail.com> wrote:

> Hi.
> Being forced to move from gdb to lldb thanks to XCode 5 dropping
> support for gdb, I find myself having to re-implement several commands
> I had up and running within gdb.
> I use the Qt framework, and in gdb, I had various macros to print the
> content of Qt objects such as QString, QMap, QList etc...
> I tried to follow the tutorials found there:
> http://lldb.llvm.org/tutorial.html
> and scripting:
> http://lldb.llvm.org/scripting.html
> In the python reference documentation:
> http://lldb.llvm.org/python-reference.html
> there's a link to a template example:
> http://llvm.org/svn/llvm-project/lldb/trunk/examples/python/cmdtemplate.py
> which according to the documentation should be invoked from lldb with:
> command script import /path/to/cmdtemplate.py
> When doing so, the module gets properly imported and you see in the
> lldb console:
> The "framestats" command has been installed, type "help framestats" or
> "framestats --help" for detailed help.
> However trying to use the command
> (lldb) framestats --help
> yield:
> error: unable to execute script function

This works for me currently using the exact source for cmdtemplate.py:

(lldb) command script import ~/Documents/src/lldb/tot/examples/python/cmdtemplate.py
The "framestats" command has been installed, type "help framestats" or "framestats --help" for detailed help.
(lldb) framestats --help
Usage: framestats [options]

This command is meant to be an example of how to make an LLDB command that
does something useful, follows best practices, and exploits the SB API.
Specifically, this command computes the aggregate and average size of the
variables in the current frame and allows you to tweak exactly which variables
are to be accounted in the computation.

  -h, --help       show this help message and exit
  -i, --in-scope   in_scope_only = True
  -a, --arguments  arguments = True
  -l, --locals     locals = True
  -s, --statics    statics = True

> I'm just putting this as an example... I've been unable to load *any*
> python script within lldb:
> (lldb) version
> lldb-300.2.47
> it always yield the same error "unable to execute script function"..
> I tried setting the PYTHONPATH variable to:
> /Applications/Xcode.app/Contents/SharedFrameworks/LLDB.framework/Versions/A/Resources/Python

You don't need to do this, the LLDB.framework hard links agains the python framework in /System/Library/Frameworks/Python.framework. You only need to set the PYTHONPATH if you want to run any LLDB API through the "lldb" module from python scripts.

Have you installed your own python? Do you see a ton of files when you execute this shell command?:

% find /System/Library/Frameworks/Python.framework

> which is where it's located, to no available...
> So I'm looking at some pointers on how to use python scripts within lldb.

It should just work as long as you haven't messed with your python installation. We currently assume the system default of Python 2.7.

> Ultimately, what I'm trying to achieve is printing the content of a QString.
> A QString object has a private member QString::Data* d; where d->size
> contains the number of UTF-16 characters
> and d->data is an array of ushort (the UTF-16) characters.
> In gdb I would "simply" have:
> define printqstringdata
>    set $d = ('QString::Data'*) $arg0
>    set $i = 0
>    # abort after a '-1' character, to avoid going on forever when
> printing a garbage string
>    while $i < $d->size && ($i == 0 || (char)$d->data[$i-1] != -1)
>        printf "%c", (char)($d->data[$i++] & 0xff)
>    end
>    printf "\n"
> end
> to print the content of the QString, I would call printqstringdata variable.d

You will want to first get python working. Then you will want to implement a python summary function:


See the section labeled "PYTHON SCRIPTING".

> doing such loop would be easy in python; provided I managed to get the
> script to load and provide and retrieve the QString variable argument.
> Any pointers or link to some tutorials will be welcome...

Let me know what the "find /System/Library/Frameworks/Python.framework" shows on your system. There should be a "/System/Library/Frameworks/Python.framework/Versions/2.7" folder with a ton of stuff in it.

More information about the lldb-dev mailing list