Xcode evaluating expressions while debugging

37,763

Solution 1

My practice:

po [NSUserDefaults standardUserDefaults]

displays: <NSUserDefaults: 0x6143040>

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

displays: "CHF"

Solution 2

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 3

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

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

Share:
37,763
Christopher Martin
Author by

Christopher Martin

I'm awesome.

Updated on July 08, 2022

Comments

  • Christopher Martin
    Christopher Martin almost 2 years

    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?

  • Dmitry Minkovsky
    Dmitry Minkovsky over 11 years
    This is the only answer that actually answers the question. Peter is suggesting you use the po command at the (lldb) prompt that is available in the debug console when the execution was paused by the debugger breakpoint. The original poster points out that they already use po, but at least this is an attempt at answering the question.
  • Dmitry Minkovsky
    Dmitry Minkovsky over 11 years
    At least this is an answer
  • Dmitry Minkovsky
    Dmitry Minkovsky over 11 years
    This doesn't answer the question about Xcode, but there's nothing wrong with a lead on another IDE. Thank you.
  • The Muffin Man
    The Muffin Man over 6 years
    I feel like the people who developed xcode somehow never developed an application professionally.
  • Dragas
    Dragas about 5 years
    "Think different"