Xcode evaluating expressions while debugging

Objective CXcodeDebuggingGdb

Objective C Problem Overview


I am working on an iPhone app. I am a full-time Java developer and I am used to using Eclipse where I can put a breakpoint in and stop the process. Then, I can type in any expression that I want and Eclipse will evaluate it using the values from that point in the process.

Is there a way to do that in Xcode? I want to be able to stop at a breakpoint and then enter some code to evaluate it. The gdb console will let me do po (print-object), but it is really limited. Any help?

Objective C Solutions


Solution 1 - Objective C

In XCode 4.0 this is sort of hidden in the GUI. When you're at a breakpoint you can probably see the Variables View inside the Debug Area; it's the pane which shows local variables and such. Right-click on the Variables View and select "Add Expression..."

I realize this is an old thread but it's still a top Google hit so I thought it worth answering.

Solution 2 - Objective C

My practice:

po [NSUserDefaults standardUserDefaults]

displays: <NSUserDefaults: 0x6143040>

po [[NSUserDefaults standardUserDefaults] stringForKey:@"Currency"]

displays: "CHF"

Solution 3 - Objective C

Use the "expression" command in the debugger. Using it is relatively simple. Just type the command expression and press enter. You will then be prompted enter an expression. Here is an example

(lldb) expression
Enter expressions, then terminate with an empty line to evaluate:
2+2

(int) $2 = 4

I also attached the help info for the expression command below. Hope this helps.

Evaluate a C/ObjC/C++ expression in the current program context, using user defined variables and variables currently in scope. This command takes 'raw' input (no need to quote stuff).

Syntax: expression --

Command Options Usage: expression [-f ] [-G ] [-a ] [-d ] [-t ] [-u ] -- expression [-o] [-a ] [-d ] [-t ] [-u ] -- expression

   -G <gdb-format> ( --gdb-format <gdb-format> )
        Specify a format using a GDB format specifier string.

   -a <boolean> ( --all-threads <boolean> )
        Should we run all threads if the execution doesn't complete on one
        thread.

   -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.

   -t <unsigned-integer> ( --timeout <unsigned-integer> )
        Timeout value for running the expression.

   -u <boolean> ( --unwind-on-error <boolean> )
        Clean up program state if the expression causes a crash, breakpoint
        hit or signal.

Timeouts: If the expression can be evaluated statically (without runnning code) then it will be. Otherwise, by default the expression will run on the current thread with a short timeout: currently .25 seconds. If it doesn't return in that time, the evaluation will be interrupted and resumed with all threads running. You can use the -a option to disable retrying on all threads. You can use the -t option to set a shorter timeout.

User defined variables: You can define your own variables for convenience or to be used in subsequent expressions. You define them the same way you would define variables in C. If the first character of your user defined variable is a $, then the variable's value will be available in future expressions, otherwise it will just be available in the current expression.

Examples:

   expr my_struct->a = my_array[3] 
   expr -f bin -- (index * 8) + 5 
   expr unsigned int $foo = 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.

Solution 4 - Objective C

Not answering question about Xcode, but JetBrains' AppCode does this in the standard IDE way most of us know from other platforms.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionChristopher MartinView Question on Stackoverflow
Solution 1 - Objective CBelden FoxView Answer on Stackoverflow
Solution 2 - Objective CPeterView Answer on Stackoverflow
Solution 3 - Objective CJeff AmesView Answer on Stackoverflow
Solution 4 - Objective CRenetikView Answer on Stackoverflow