使用 Xcode 调试器查看动态分配的数组?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/484707/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 18:28:00  来源:igfitidea点击:

Viewing a dynamically-allocated array with the Xcode debugger?

c++xcodemacosgdb

提问by avalys

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

假设我有一个 C++ 数组:

double* velocity = new double[100];

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

使用 GDB 命令行,我可以使用以下命令查看这个数组:

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

但是,在使用 Xcode 调试器时,它最多将其视为指向单个双精度值的指针,并在变量列表中显示速度 [0]。

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?

这使其成为调试包含大型动态分配数组的程序的真正 PITA。必须有某种方法告诉 Xcode“这是一个指向长度为 100 的数组的指针”,并让它显示这样的东西。有人知道这是什么吗?

采纳答案by mfazekas

You can use gdb syntax as expressions:

您可以使用 gdb 语法作为表达式:

  1. Use Run/Show/Expressions... menu to show the expressions window
  2. Enter '*velocity @ 100'at the bottom of the window (Expression:)
  1. 使用 Run/Show/Expressions... 菜单显示表达式窗口
  2. '*velocity @ 100'在窗口底部输入(表达式:)

回答by Alexander Myshov

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

我认为我的答案将是对旧答案的一个很好的补充。

New versions of Xcode use lldbdebugger as default tool instead of gdb.

新版本的 Xcode 使用lldb调试器作为默认工具而不是gdb.

According this page:

根据这个页面

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

随着 Xcode 5 的发布,LLDB 调试器成为 OS X 上调试体验的基础。

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

因此,对于 Xcode 5 及更高版本,我使用以下lldb命令:

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

其中:
8- 数组中元素的数量 - 数组
array_name的名称 - 数组
int的类型

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

回答by jupp0r

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

从 Xcode 10 开始,您可以右键单击velocity,选择“View value as...”,然后选择“Custom Type”。然后将其转换为(double(&)[100]) *velocity并在 GUI 中显示该数组。

回答by Stefan Arentz

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

不不幸的是,GUI 是有限的,您需要很好地混合 GDB 魔法才能完成任务。