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?
- 
 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– Nick Wright2020年04月29日 10:01:38 +00:00Commented Apr 29, 2020 at 10:01
5 Answers 5
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'
6 Comments
'expr myLabel.text = @"hello!" but I get an error: property 'text' not found on object of type 'UILabel *'... Any thoughts?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]@property.p as a shortcut for expr. Example: (lldb) p url = @"http://google.com"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.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
Comments
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.
Comments
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.
Comments
you can change it by just writing p ...
ie
p myBoolVariable = false
verify by po command and it should return false