calling a function using expr during an LLDB session

1.8k views Asked by At

I'm trying to get a better grasp of LLDB and am currently stuck trying to call a locally defined function (using LLDB's expr) while debugging some code. For the sake of simplicity, lets consider this toy code:

testing_lldb.c:

unsigned long factorial(unsigned input) {
    unsigned long ret_val = 0;

    if (input == 0)
        ret_val = 1;
    else
        ret_val = input * (factorial(input-1));

    return ret_val; 
}

I compile it like this:

$ clang -g -Weverything -c lldb_test.c

and then run LLDB by typing:

$ lldb testing_lldb.o

In my LLDB session, I'd like to be able to call factorial(). My first attempt:

(lldb) expr unsigned long i = factorial(i);
error: 'factorial' has unknown return type;  
cast the call to its declared return type

The error message includes a clear hint, so I try again:

(lldb) expr unsigned long i = (unsigned long)factorial(i);
error: warning: function 'factorial' has internal linkage but is not defined
note: used here
error: Can't run the expression locally: Interpreter doesn't handle one of the expression's opcodes

Fine, I try to define factorial() manually by following the answer for this SO question:

(lldb) expr typedef unsigned long (*$factorial_type)(unsigned)
(lldb) expr $factorial_type $factorial_function = ($factorial_type)0x0000000000000000
(lldb) expr unsigned long i = (unsigned long)factorial(i);
error: warning: function 'factorial' has internal linkage but is not defined
note: used here
error: Can't run the expression locally: Interpreter doesn't handle one of the expression's opcodes

And this gives me exactly same error as above. I double checked the starting address of factorial() by running:

(lldb) image lookup -Avi -n factorial 

Question

Given testing_lldb.c, what's required to be able to use factorial() in expressions in LLDB?

Some details regarding the enviroment:

$ uname -r
3.16.0-4-amd64
$ lldb -v
lldb version 3.4.2 ( revision )
$ clang -v
Debian clang version 3.4.2-14 (tags/RELEASE_34/dot2-final) (based on LLVM 3.4.2)
Target: x86_64-pc-linux-gnu
1

There are 1 answers

0
Jim Ingham On BEST ANSWER

You can only use the expression parser to evaluate expressions that run functions if you are debugging a live process. You are not only not debugging a live process, but you are also debugging a .o file - which isn't fully linked - so it could never run. lldb can inspect .o files, dump the symbol tables and some things like that, but not everything will work.

You want to add a little main function to your "testing_lldb.c", and build a runnable program (drop the -c flag.) Then set a breakpoint on main:

(lldb) break set -n main

run the program

(lldb) run

then when you hit the breakpoint, try calling your function.