Friday, September 27, 2019

Using Expressions in LLDB

Often times we would want to hold some temporary variables when debugging with lldb. We can make use of expression in such cases. An expression, short form e or expr evaluates the given statement. If we use an assignment with a variable starting with $, that variable will be available through out and can be used in other expressions.
(lldb) expr NSRange $range1 = NSMakeRange(0, 3);
(lldb) expr [@"Hi Olive" substringWithRange:(NSRange)$range1];
(NSTaggedPointerString *) $9 = 0x7a858d27bb2607ed @"Hi "
Since we are in single-line expression mode, there are slight differences in the way the expressions work. We need to cast NSRange. A better option in this case is to use the multi-line expression mode. For that, first enter expr↩ followed by a carriage return. The lldb is now in multi-line expression mode where we can enter normal Objective-C (or Swift) code with each line terminated with a semi-colon and an empty line after the final statement will evalute all the expressions and exit the mode.
(lldb) expr
Enter expressions, then terminate with an empty line to evaluate:
1 NSRange range = NSMakeRange(9, 4);
2 NSString *str = @"Slice of Life";
3 [str substringWithRange:range];
4
(NSTaggedPointerString *) $4 = 0x7a858d42fd26039d @"Life"
(lldb)
Multi-line expression with lldb makes debugging much better.