Eric
Eric

Reputation: 16921

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

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?

Upvotes: 206

Views: 95508

Answers (5)

Wahab Khan Jadon
Wahab Khan Jadon

Reputation: 1176

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

p myBoolVariable = false

verify by po command and it should return false

Upvotes: 0

Declan McKenna
Declan McKenna

Reputation: 4870

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

Upvotes: 3

atalayasa
atalayasa

Reputation: 3480

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.

Upvotes: 8

arango_86
arango_86

Reputation: 4425

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

Upvotes: 24

Matthias Bauch
Matthias Bauch

Reputation: 90117

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'

Upvotes: 305

Related Questions