如何查看Xcode调试器中的对象?

时间:2021-05-26 07:42:19

I have a simple question about debugging on Xcode and GDB.

我有一个关于在Xcode和GDB上调试的简单问题。

I often run into an error:

我经常遇到一个错误:

unrecognized selector sent to instance 0x1081ad0

which makes the program load into GDB. Is there an easy way to examine what instance is located in that memory from GDB?

这使得程序加载到GDB中。有没有一种简单的方法可以从GDB中检查该内存中的实例?

4 个解决方案

#1


42  

po 0x1081ad0

po 0x1081ad0

po = Print Object. You can even call methods, like

po =打印对象。你甚至可以调用方法,比如

po [myArray objectAtIndex:0]

po [myArray objectAtIndex:0]

Note that it only works on objects, so

请注意,它仅适用于对象,因此

po 1

宝1

will crash your program.

会破坏你的程序。

#2


9  

Steven is correct — the gdb command po is a shortcut for print-object, which actually calls -debugDescription (not -description, as you might expect) on the object provided as an argument. In many cases you'll see the same result from both methods, since one calls the other unless overridden. (See the related Note: callout on this Apple technote for details. Note that in their code sample, po $r3 prints the contents of a PowerPC register, but you can use any object pointer/reference, including Intel registers, etc.)

史蒂文是正确的 - gdb命令po是print-object的快捷方式,它实际上在作为参数提供的对象上调用-debugDescription(不是-description,如您所料)。在许多情况下,您将从两种方法中看到相同的结果,因为除非被覆盖,否则将调用另一种方法。 (有关详细信息,请参阅相关说明:此Apple技术说明中的标注。请注意,在其代码示例中,po $ r3打印PowerPC寄存器的内容,但您可以使用任何对象指针/引用,包括Intel寄存器等)

Also, be aware that print-object will only work on valid objects that haven't been deallocated. It won't help at all if you're sending a message to a borked pointer. Given the error you cited, though, it would seem that it's a valid object instance, it just doesn't implement the method you're trying to invoke.

另外,请注意print-object仅适用于尚未释放的有效对象。如果您向borked指针发送消息,它将毫无帮助。但是,鉴于您引用的错误,它似乎是一个有效的对象实例,它只是没有实现您尝试调用的方法。

It's also remotely possible that the object has already been destroyed. This answer should help in that case.

对象已被破坏的可能性很小。在这种情况下,这个答案应该有所帮


Edit:

编辑:

There are other ways to "examine" objects in the debugger. I asked this SO question about Xcode data formatters, which is one way you can determine how a custom class appears in the Summary column of the debugger. The documentation linked from that question explain how it works. I've found the summary approach to help a lot with seeing the state of an object.

还有其他方法可以在调试器中“检查”对象。我问过这个关于Xcode数据格式化器的问题,这是一种可以确定自定义类如何出现在调试器的Summary列中的方法。从该问题链接的文档解释了它的工作原理。我发现总结方法可以帮助我看到对象的状态。

#3


6  

There are a couple of things you can do.

你可以做几件事。

  1. You can insert a break point that will trigger every time you have an exception, so basically create a break point for this (go to breakpoints and create a new one): -[NSException raise]
  2. 你可以插入一个断点,每次有异常时都会触发,所以基本上为此创建一个断点(转到断点并创建一个断点): - [NSException raise]
  3. Alternatively, you can actually see what the object at that mem location is:

    或者,您实际上可以看到该mem位置的对象是什么:

    info symbol 0x1081ad0 or

    信息符号0x1081ad0或

    info line *0x1081ad0

    信息行* 0x1081ad0

There's more info at the cocoadev wiki entry for exceptionhandling and debugging tips for objective C at cocoawithlove.

有关cocoawithlove目标C的异常处理和调试技巧,请参阅cocoadev wiki条目。

#4


0  

Your instance is not valid. You have release the object somewhere else, but you did not clear out your pointer... enable Zombie detection.

您的实例无效。你已经在其他地方释放了这个对象,但你没有清除你的指针...启用Zombie检测。

#1


42  

po 0x1081ad0

po 0x1081ad0

po = Print Object. You can even call methods, like

po =打印对象。你甚至可以调用方法,比如

po [myArray objectAtIndex:0]

po [myArray objectAtIndex:0]

Note that it only works on objects, so

请注意,它仅适用于对象,因此

po 1

宝1

will crash your program.

会破坏你的程序。

#2


9  

Steven is correct — the gdb command po is a shortcut for print-object, which actually calls -debugDescription (not -description, as you might expect) on the object provided as an argument. In many cases you'll see the same result from both methods, since one calls the other unless overridden. (See the related Note: callout on this Apple technote for details. Note that in their code sample, po $r3 prints the contents of a PowerPC register, but you can use any object pointer/reference, including Intel registers, etc.)

史蒂文是正确的 - gdb命令po是print-object的快捷方式,它实际上在作为参数提供的对象上调用-debugDescription(不是-description,如您所料)。在许多情况下,您将从两种方法中看到相同的结果,因为除非被覆盖,否则将调用另一种方法。 (有关详细信息,请参阅相关说明:此Apple技术说明中的标注。请注意,在其代码示例中,po $ r3打印PowerPC寄存器的内容,但您可以使用任何对象指针/引用,包括Intel寄存器等)

Also, be aware that print-object will only work on valid objects that haven't been deallocated. It won't help at all if you're sending a message to a borked pointer. Given the error you cited, though, it would seem that it's a valid object instance, it just doesn't implement the method you're trying to invoke.

另外,请注意print-object仅适用于尚未释放的有效对象。如果您向borked指针发送消息,它将毫无帮助。但是,鉴于您引用的错误,它似乎是一个有效的对象实例,它只是没有实现您尝试调用的方法。

It's also remotely possible that the object has already been destroyed. This answer should help in that case.

对象已被破坏的可能性很小。在这种情况下,这个答案应该有所帮


Edit:

编辑:

There are other ways to "examine" objects in the debugger. I asked this SO question about Xcode data formatters, which is one way you can determine how a custom class appears in the Summary column of the debugger. The documentation linked from that question explain how it works. I've found the summary approach to help a lot with seeing the state of an object.

还有其他方法可以在调试器中“检查”对象。我问过这个关于Xcode数据格式化器的问题,这是一种可以确定自定义类如何出现在调试器的Summary列中的方法。从该问题链接的文档解释了它的工作原理。我发现总结方法可以帮助我看到对象的状态。

#3


6  

There are a couple of things you can do.

你可以做几件事。

  1. You can insert a break point that will trigger every time you have an exception, so basically create a break point for this (go to breakpoints and create a new one): -[NSException raise]
  2. 你可以插入一个断点,每次有异常时都会触发,所以基本上为此创建一个断点(转到断点并创建一个断点): - [NSException raise]
  3. Alternatively, you can actually see what the object at that mem location is:

    或者,您实际上可以看到该mem位置的对象是什么:

    info symbol 0x1081ad0 or

    信息符号0x1081ad0或

    info line *0x1081ad0

    信息行* 0x1081ad0

There's more info at the cocoadev wiki entry for exceptionhandling and debugging tips for objective C at cocoawithlove.

有关cocoawithlove目标C的异常处理和调试技巧,请参阅cocoadev wiki条目。

#4


0  

Your instance is not valid. You have release the object somewhere else, but you did not clear out your pointer... enable Zombie detection.

您的实例无效。你已经在其他地方释放了这个对象,但你没有清除你的指针...启用Zombie检测。