207

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?

craft
2,1351 gold badge26 silver badges31 bronze badges
asked Mar 28, 2012 at 12:21
1
  • 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円\" Commented Apr 29, 2020 at 10:01

5 Answers 5

306
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'

answered Mar 28, 2012 at 12:43
Sign up to request clarification or add additional context in comments.

6 Comments

Indeed, 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?
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]
Actually, 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.
You can also use p as a shortcut for expr. Example: (lldb) p url = @"http://google.com"
You 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.
|
24

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.

  1. Put the breakpoint properly after initialised to the desired value.
  2. then execute "expression dict = nil" in lldb command line to change it. (for example "nil")
  3. Step over the break point.
  4. 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
answered Jan 6, 2017 at 9:31

Comments

8

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.

answered Sep 30, 2019 at 8:14

Comments

3

If you'd like this to happen every-time the break point is hit you can add the expression to your breakpoint.

  1. Create a breakpoint at the point you want to manipulate the variable
  2. Right click and select edit breakpoint
  3. In the action radio box select 'Debugger Command'
  4. Type in e yourStringName = "Your new value"
  5. Check the 'automatically continue after evaluating actions' checkbox.

Screenshot of example breakpoint with expression command

answered Aug 15, 2022 at 12:53

Comments

0

you can change it by just writing p ... ie

p myBoolVariable = false

verify by po command and it should return false

answered Dec 7, 2024 at 6:05

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.