Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 544
Eric Avatar asked Mar 28 '12 12:03

Eric


People also ask

Is it possible to change the value of a variable while debugging?

Yes u can if you have the authorization: while debugging do a doubleclick on the variable, the system'll show the name and the value of the variable, change the value and press CHANGE icon.

How can you examine a variable while executing code?

Hover over a variable to see its value. The most commonly used way to look at variables is the DataTip. When stopped in the debugger hover the mouse cursor over the variable you want to look at. The DataTip will appear showing you the value of that variable.


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

like image 179
Matthias Bauch Avatar answered Oct 07 '22 01:10

Matthias Bauch


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 
like image 45
arango_86 Avatar answered Oct 07 '22 02:10

arango_86