69

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?

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
Christopher Martin
  • 927
  • 1
  • 7
  • 9
  • What's "po"? The gdb console will let you evaluate a large set of arbitrary functions. What in particular where you trying to do? – Hack Saw Jan 28 '11 at 06:49
  • 3
    "po" is print object. It's Apple's extension to gcc that results in the *description* message being sent to an ObjC instance so that it returns it's description. – diciu Jan 28 '11 at 07:05
  • Ah, that's why I've not heard of it. Fun. – Hack Saw Jan 28 '11 at 07:14
  • `p` also works (print) -- which gives the object type, in addition to value. – Brent Faust Mar 14 '13 at 19:19
  • 2
    The current "accepted answer" doesn't answer the question on how to use expressions. Another answer does: http://stackoverflow.com/a/15689701/466771 – olafure Jun 19 '13 at 10:49
  • 1
    I think the command you are looking for is `expr`. You use it from `(lldb)` command prompt when the debugger launches. You can do `expr local_int = 5` to modify the current execution context. – wfbarksdale Oct 06 '13 at 00:40

4 Answers4

112

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.

Belden Fox
  • 1,257
  • 1
  • 8
  • 6
59

My practice:

po [NSUserDefaults standardUserDefaults]

displays: <NSUserDefaults: 0x6143040>

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

displays: "CHF"

Johann
  • 4,107
  • 3
  • 40
  • 39
Peter
  • 622
  • 6
  • 4
  • 3
    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 Jan 24 '13 at 23:58
  • 1
    At least this is an answer – Dmitry Minkovsky Feb 06 '13 at 00:01
17

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.

Jeff Ames
  • 1,555
  • 11
  • 27
8

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

Dmitry Minkovsky
  • 36,185
  • 26
  • 116
  • 160
Renetik
  • 5,887
  • 1
  • 47
  • 66