How to change variables value while debugging with LLDB in Xcode?

76,435

Solution 1

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'

Solution 2

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

Solution 3

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.

Share:
76,435

Related videos on Youtube

Eric
Author by

Eric

I got keys coming from KVC Cost a newbie 200 leaks I'm a Swift commando, Xcode for example This coding lifestyle is hard to handle So I go debug cause I'm more like a test player Thug, branded in the business-layer So many agile haters, imitators steady swanging Make me wanna start break pointing

Updated on February 24, 2020

Comments

  • Eric
    Eric about 4 years

    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?

    • mfaani
      mfaani over 4 years
      Make sure to see Advanced debugging in iOS. It's awesome
    • Nick Wright
      Nick Wright about 4 years
      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\"
  • Eric
    Eric about 12 years
    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?
  • Matthias Bauch
    Matthias Bauch about 12 years
    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]
  • BJ Homer
    BJ Homer about 12 years
    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.
  • funroll
    funroll over 10 years
    You can also use p as a shortcut for expr. Example: (lldb) p url = @"http://google.com"
  • Ray Hunter
    Ray Hunter over 8 years
    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.
  • Jason Newell
    Jason Newell about 8 years
    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.