C++ expression tutorials/samples?

hi,

i am desperately looking for a tutorial or sample on how to write C++ expression for LLDB. unfortunately, even C++ API link at the homepage is virtually empty.

any pointer on some tutorials or samples for beginners?

a question: what C++ expression can do that Python API cannot?

thanks!

The C++ expressions you can write with the "expression" command (and its aliases "p", "expr", "po" etc), are as if you took the code where you are stopped and added a new scope. So lets say you are stopped in a function like:

int main (int argc, char const *argv[])
{
    printf ("Hello world\n");
    return 0; <<<< you are stopped here
}

When you type an expression at the command line:

(lldb) expr
int i = 12;
argc + i

It is as if you are adding a new scope to your code:

int main (int argc, char const *argv[])
{
    printf ("Hello world\n");
    {
  int i = 12;
  argc + i
    }
    return 0;
}

The last statement in your expression is going to be the return value of the expression. Note that you can do unique things when you can have multiple statements.

Like use flow control (the for loop) and expression local variables ("int i" in the for loop)

(lldb) expr
for (int i=0; i<12; ++i)
    (int)printf("number %i\n", i)

You can define expression global variables

(lldb) expr int $my_global = 12

Now this global is persistent and has a type (int) and it will be available in subsequent expressions (unlike the expression local "int i" from the for loop which only exists for the lifetime of the expression).

We can use this global again:

(lldb) expr ++$my_global

You can define your own types from scratch and use them in your expression:

(lldb) expr
struct foo_t { int x; float y; char name[32]; };
foo_t foo = { 1, 2.2, "Hello" }
foo

The type "foo_t" above is an expression local type, but the last statement returns "foo" so the return type is created and stored.

Expressions are just like code you write in your target and the expression have variable visibility just as if you were adding code into your current function. The expression can't access the public API. This is where python comes in. Python gives you the ability to explore your entire process (all threads in a process, and all frames in each thread, etc). Python doesn't JIT code up and run it in your target like "expression" does, it allows your programatic access to your process so you can do things like:

(lldb) script
for thread in lldb.process:
    print thread
    for frame in thread:
        print frame
        expr_result = frame.EvaluateExpression("++$my_global")

Note how python is accessing all the threads and frames via the API, and it can use the lldb.SBFrame.EvaluateExpression() function on the frame, but the python code itself isn't running in the debuggee. When you write and expression we will JIT code up for that expression and run it in your debugged process.

Let me know if you have anymore questions.

Greg