In Xcode, GDB allows you to change local variables while debugging (see how to change NSString value while debugging in XCode?). Does LLDB offer a similar functionality? If so, how can we use it?
-
1Make sure to see [Advanced debugging in iOS](https://jayeshkawli.ghost.io/advanced-debugging-in-ios/). It's awesome – mfaani Nov 19 '19 at 19:50
-
I'm trying to set an objects property, which works if the string length is between 0-15 characters. Setting a string of 16 or more characters is accepted, but when i print it back, it shows me a nonsense string: po myObj.someString = "1234567890123456", which works, but when i print I get (String? $R68 = "\0\0\0\0@\a\u{1}\c{5}\0\0\0\0\0\0\" – Nick Wright Apr 29 '20 at 10:01
4 Answers
expr myString = @"Foo"
(lldb) help expr
Evaluate a C/ObjC/C++ expression in the current program context, using variables currently in scope. This command takes 'raw' input (no need to quote stuff).Syntax: expression --
Command Options Usage: expression [-f ] [-G ] [-d ] [-u ] -- expression [-o] [-d ] [-u ] -- expression
-G <gdb-format> ( --gdb-format <gdb-format> ) Specify a format using a GDB format specifier string. -d <boolean> ( --dynamic-value <boolean> ) Upcast the value resulting from the expression to its dynamic type if available. -f <format> ( --format <format> ) Specify a format to be used for display. -o ( --object-description ) Print the object description of the value resulting from the expression. -u <boolean> ( --unwind-on-error <boolean> ) Clean up program state if the expression causes a crash, breakpoint hit or signal.
Examples:
expr my_struct->a = my_array[3]
expr -f bin -- (index * 8) + 5
expr char c[] = "foo"; c[0]IMPORTANT NOTE: Because this command takes 'raw' input, if you use any command options you must use ' -- ' between the end of the command options and the beginning of the raw input.
'expr' is an abbreviation for 'expression'

- 89,811
- 20
- 225
- 247
-
1Indeed, thanks! One more little question: I'm doing this to try to change the text of a UILabel: `'expr myLabel.text = @"hello!"` but I get an `error: property 'text' not found on object of type 'UILabel *'`... Any thoughts? – Eric Mar 28 '12 at 13:32
-
10`expr (void)[label setText:@"Foo"]` should do it. Dot-Syntax usually won't work in the debugger. lldb probably interprets it as you wanted to access a member of a c-struct, but I'm not sure if this is the reason it won't work. Dot-Syntax doesn't work for `po` either. instead of `po label.text` you have to use `po [label text]` – Matthias Bauch Mar 28 '12 at 14:48
-
4Actually, lldb handles dot syntax much better than gdb. gdb just assumes you're treating it like a C-struct, which fails. lldb will correctly access properties, but only if they're actually declared with `@property`. – BJ Homer Mar 28 '12 at 17:12
-
29You can also use `p` as a shortcut for `expr`. Example: `(lldb) p url = @"http://google.com"` – funroll Sep 16 '13 at 19:26
-
I found that while using LLVM and Swift, if I had a method parameter that was passed and I tried to change it, the value would change, but the code would not recognize the change (ie I changed a Bool from false to true). I had to change the value in LLVM before the method call and then the code would see the change. – Ray Hunter Oct 08 '15 at 17:41
-
15You can also use `e` as a shortcut for `expr`. BTW, `p` is an alias for print which is an alias for `expr --` (just evalue raw input, no flags) `expr -o -- [object]` or `po` generally gives you more useful output for objects though. – Jason Newell Feb 23 '16 at 15:09
The following stuff works for me. I am using Xcode 8.
If you want to set some variable (for example a "dict") to nil and then test the code flow, you can try the following.
- Put the breakpoint properly after initialised to the desired value.
- then execute "expression dict = nil" in lldb command line to change it. (for example "nil")
- Step over the break point.
- Check the variable "dict" in the next line. It will be nil.
It will look something like in the console.
(lldb) expression dict = nil
(NSDictionary *) $5 = nil

- 4,236
- 4
- 40
- 46
If you are using Xcode 10 or 11 put the breakpoint properly after initialised to the required variable then you can change your variable using po myString = "Hello World"
easily.

- 3,310
- 25
- 42
If you'd like this to happen every-time the break point is hit you can add the expression to your breakpoint.
- Create a breakpoint at the point you want to manipulate the variable
- Right click and select edit breakpoint
- In the action radio box select 'Debugger Command'
- Type in
e yourStringName = "Your new value"
- Check the 'automatically continue after evaluating actions' checkbox.

- 4,321
- 6
- 54
- 72