Search code examples
debuggingllvmlldb

How can I see printf output when evaluating an expression using the `expr` command in lldb?


Let us say that I have a function in a C program test.c like this:

#include <stdio.h>
char* foo = "test";
void print_foo(void)
{
    printf("%s", foo);
}
main() {  }

I compile and run test.c like this:

gcc -g -o test test.c
chmod 755 test && lldb -s <(echo "b main\nr") test

However, if I then run expr print_foo() no string output occurs:

(lldb) expr print_foo()
(lldb)

Solution

  • STDOUT is line buffered. You haven't emitted a newline yet. Try:

    (lldb) expr (void) fflush(0)
    

    and you should see the output. Or have foo be "test\n".