27

Let's say I have an array in C++:

double* velocity = new double[100];

Using the GDB command line, I can view this array with the command:

> print *velocity @ 100

and it will print a nicely-formatted list of all the double values inside the array.

However, when using the Xcode debugger, the most it will do is treat this as a pointer to a single double value, and display velocity[0] in the variable list.

This makes it a real PITA to debug programs that contain large dynamically allocated array. There has got to be some way to tell Xcode "This is a pointer to an array of length 100", and have it display the thing as such. Anyone know what it is?

Lebyrt
  • 1,376
  • 1
  • 9
  • 18
avalys
  • 3,662
  • 4
  • 25
  • 22
  • You mean in the debug console when you break right? XCode 4.3 seems to support it. Thanks for the tip off! – Hari Honor Feb 13 '12 at 14:20
  • even if I type "print *velocity @ 100" or "print *velocity @ 100;" or "p *velocity @ 100;" in the debugger console, it won't print and give this error: error: warningL expression result unused error: expected ';' after expression error: 1 errors parsing expression Any hint? Thanks! – Zennichimaro Mar 24 '13 at 02:44
  • 3
    @Zennichimaro XCode uses lldb, not gdb. The equivalent would be `p *(double( * )[100])velocity`. From this answer: http://stackoverflow.com/questions/7062173/lldb-equivalent-of-gdbs-operator-in-xcode-4-1 – tronda Feb 13 '15 at 09:38
  • 2
    With this original question being 7 years old, I would've figured that this issue would've been one that would've been solved now, but I can't seem to find a definitive answer. I'm using the latest version of Xcode (7.3.1) and I'm unable to see all the contents of an array that is using a pointer to point at it. Any better workarounds in 2016? – JosephTLyons Aug 07 '16 at 10:43
  • 1
    @joe_04_04 it's supported now, see my answer – jupp0r Jun 28 '19 at 00:16

4 Answers4

37

I think that my answer will be a good addition for the old one.

New versions of Xcode use lldb debugger as default tool instead of gdb.

According this page:

With the release of Xcode 5, the LLDB debugger becomes the foundation for the debugging experience on OS X.

So for Xcode since version 5 and up I use this lldb command:

memory read -t int -c8 `array_name`

where:
8 - the number of elements in array
array_name - the name of array
int - the type of array

The result of execution of this command will be something like this:

(lldb) memory read -t int -c8 array
(int) 0x7fff5fbff870 = 7
(int) 0x7fff5fbff874 = 6
(int) 0x7fff5fbff878 = 9
(int) 0x7fff5fbff87c = 10
(int) 0x7fff5fbff880 = 1
(int) 0x7fff5fbff884 = 8
(int) 0x7fff5fbff888 = 4
(int) 0x7fff5fbff88c = 3
Alexander Myshov
  • 2,881
  • 2
  • 20
  • 31
  • If the name of your array happens to be all hex values, such as ABC, xcode will attempt to read values starting from address 0xABC. You can work around this by supplying '0 + ABC' instead (including quotes) – panofsteel Sep 21 '17 at 00:07
14

As of Xcode 10, you can right-click velocity, choose "View value as..." and then "Custom Type". Then cast it to (double(&)[100]) *velocity and display the array in the GUI.

jupp0r
  • 4,502
  • 1
  • 27
  • 34
13

You can use gdb syntax as expressions:

  1. Use Run/Show/Expressions... menu to show the expressions window
  2. Enter '*velocity @ 100' at the bottom of the window (Expression:)
mfazekas
  • 5,589
  • 1
  • 34
  • 25
-2

No unfortunately the GUI is limited and you will need to blend in a good mix of GDB magic to get things done.

Stefan Arentz
  • 34,311
  • 8
  • 67
  • 88
  • 1
    See [this other answer relating to Xcode 10 and above](https://stackoverflow.com/a/56799662/1967396) which suggests that this answer is no longer useful / valid. – Floris Nov 04 '20 at 20:32