An equivalent to python gdb.execute('...') in lldb
<p>If I have a structure like this (in C)</p>

<pre><code>typedef struct {
    int x;
    int[2] y;
} A;
</code></pre>

<p>And an instance of it, say <code>a</code> with <code>a.x=1</code> and <code>a.y={2,3}</code>.</p>

<p>To access <code>a.y[1]</code> from python scripting, do I actually have to do this very voluble command?</p>

<pre><code>script print lldb.frame.FindVariable('a').GetChildMemberWithName('y').GetChildAtIndex(1)
</code></pre>

<p>I've written this function to help me when I want to print members of struct variables in C:</p>

<pre><code># lldb_cmds.py

import lldb

def get_nested_val_c(vnames,idx=None):
    """
        vnames is a list of strings ['a','b','c',...] that will be evaluated as if
        you called

            a.b.c.,...

        So for [a,b,c] you get:

            a.b.c

        If idx is given, it is evaluated as

            a.b.c[idx]

    """

    try:
        x=lldb.frame.FindVariable(vnames[0])
        for v_ in vnames[1:]:
            x=x.GetChildMemberWithName(v_)
    except TypeError:
        x=lldb.frame.FindVariable(vnames)

    if idx == None:
        return x

    try:
        x=x.GetChildAtIndex(idx[0])
        for i_ in idx[1:]:
            x=x.GetChildAtIndex(i_,False,True)
    except TypeError:
        x=x.GetChildAtIndex(idx)
</code></pre>

<p>which can then be loaded with</p>

<pre><code>command script import lldb_cmds.py
</code></pre>

<p>and called (like above) with</p>

<pre><code>python lldb_cmds.get_nested_val_c[['a','y'],1)
</code></pre>

<p>But is there a shorter way? Yes I know you can write</p>

<pre><code>p a.y[1]
</code></pre>

<p>but since there don't seem to be any while loops in lldb, how could I print this with a variable index without resorting to such long statements?</p>

<p>(And yes I know you can write for this example: <code>p *(int(*)[2])a.y</code> but I am asking in general.)</p>

<p>I'm not entirely sure what you want to do here.  I'll answer a couple of potential questions, and you can tell me if one of them was right...</p>

<p>If you're trying to find a nicer way to reach into known nested structures, this might be more convenient:</p>

<pre><code>var = lldb.frame.FindVariable("a").GetValueForExpressionPath("y[0]")
</code></pre>

<p>If what you are trying to do is run command line commands in Python scripts, there are two ways to do this:</p>

<pre><code>lldb.debugger.HandleCommand("command")
</code></pre>

<p>That just runs the command, and prints the results to lldb's stdout, or:</p>

<pre><code>ret_val = lldb.SBCommandReturnObject()
lldb.debugger.GetCommandInterpreter().HandleCommand("command", ret_val)
</code></pre>

<p>That will run the command, and return the result of the command (which is what in the lldb driver gets printed as the command runs) into the <code>ret_val</code> object.  That's convenient if you want to inspect the results of the command.  The ReturnObject also tells you whether the command was successful or not, and holds the error if it wasn't.</p>

<p>If you are trying to print statically allocated arrays - as in your example - you shouldn't need to do the cast you show above, you should just be able to do:</p>

<p>(lldb) p a.y</p>

<p>and that should print all the elements.  If you are dealing with dynamically sized arrays (e.g. an int * that you point at a malloc'ed array) the lldb in Xcode 8.0 has a new command <code>parray</code> which would let you say:</p>

<p>(lldb) parray 10  a.y</p>

<p>to print 10 elements of the array of ints pointed to by y.</p>

python lldb