13

I've seen passing statements that you can enter complex statements like a for loop in an LLDB command (in the language of the program you're debugging - in this case Objective-C)

I would really like to be able to do this. I've never learned Python, and would prefer not to invest the time to do so in order to use the available python LLDB support - there just aren't enough hours in the day for that.

Duncan C
  • 128,072
  • 22
  • 173
  • 272

2 Answers2

18

You can enter Objective-C statements using expr -- ..., for example:

(lldb) po myArray

(
foo,
bar
)

(lldb) expr -- for (NSString *s in myArray) { (void)NSLog(@"%@", s) ; }

2013-12-03 18:29:03.637 myapp[1373:70b] foo
2013-12-03 18:29:03.639 myapp[1373:70b] bar
Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
  • 1
    Or, more simply, you can just say (lldb) expr and you will be able to type as many lines of code as you like. To end, make an empty line and press again. LLDB will then proceed to evaluate whatever chunk of code you typed. I am not sure, however, that this is a general alternative to the Python support. The two features (expression parser and scripting) have different rationales and use cases, and yes in some cases it’s up to you what to use, but in others it is much more obvious to use one or the other. What exactly are you trying to achieve? – Enrico Granata Dec 03 '13 at 18:43
  • 1
    I can't get it to work. I tried the simple statement expr -- (void) for (int x = 0;x<5;x++){printf("x = %d\n", x);} as a test, and it says "error: Expected expression". I've also tried prefixing x with a "$", but no luck. – Duncan C Dec 03 '13 at 18:48
  • 1
    ok, with some more tinkering I'm finally getting it to work. I needed a void cast on my statement inside the loop. Here is the code I got working: expr for (int x = 0; x< 5; x++) {(void) printf("x = %d\n", x);} Why does the printf need a void cast, but the for loop does not? The (complex) statement that I'm running is the for loop. I would think IT would need the void cast, not the printf. – Duncan C Dec 03 '13 at 18:55
  • @DuncanC: lldb has to know the return type of printf. Compare also http://stackoverflow.com/questions/19339493/why-cant-lldb-evaluate-this-expression, and in particular Jim Ingham's answer about lldb and functions with variable argument lists. – Martin R Dec 03 '13 at 19:06
  • 1
    (in other news, printf returns an int :-) – Enrico Granata Dec 03 '13 at 19:11
  • @Enrico, right now I am skill-building with the lldb command line. In the past I've needed to enter a compound statement like a loop and have not been able to figure out how to do so. Adding a void cast (or I guess an int cast) to printf did the trick in my case, but I'm still not clear on when casts are needed and when not. – Duncan C Dec 04 '13 at 12:46
  • 2
    @DuncanC `expr -- for (int x = 0;x<5;x++) printf("x = %d\n", x);` works for me. Note the lack of void cast for the for **statement**. Casts should only be needed when an expression type cannot be determined by lldb (eg: some objc message passing) – Mihai Timar Feb 10 '16 at 09:16
0

Based on @"Martin R"'s answer.

At least NSLog doesn't seem to print something with Xcode 6.0.1.

(lldb) expr -- for(UIWindow *w in [(UIApplication *)[UIApplication sharedApplication] windows]) { (int)printf("%s\n\n", [(NSString *)[w recursiveDescription] UTF8String]); }
hiroshi
  • 6,871
  • 3
  • 46
  • 59