Xcode 的 LLDB 调试器中的“p”和“po”有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28806423/
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-15 06:44:55  来源:igfitidea点击:

What's the difference between "p" and "po" in Xcode's LLDB debugger?

objective-cxcodedebuggingconsolelldb

提问by Vyachaslav Gerchicov

Example. I write the following string in my old project and a new, clear one:

例子。我在我的旧项目和一个新的、清晰的项目中写了以下字符串:

UIInterfaceOrientation k = [UIApplication sharedApplication].statusBarOrientation;

Console input-output for a clear project:

清晰项目的控制台输入输出:

(lldb) po k
UIInterfaceOrientationLandscapeLeft

And something awful in my old project if I write "po k" - a list of useless integers.

如果我写“po k” - 一个无用的整数列表,我的旧项目中会有一些可怕的东西。

Additionally I can't print most of objects in the new project.

此外,我无法打印新项目中的大部分对象。

采纳答案by Vyachaslav Gerchicov

Strip debug symbols during copy

In most answers they advice to set optimization to "none" but forget that this option should be set to NO (at least for debug configuration).

在大多数答案中,他们建议将优化设置为“无”,但忘记了此选项应设置为 NO(至少对于调试配置)。

回答by Jim Ingham

I don't know what is going on in your case, but just so folks are clear on the difference between po& p:

我不知道你的情况发生了什么,但只是让人们清楚po&之间的区别p

The pcommand (a.k.a. expr --) takes the arguments it is given, compiles them as though they were a source code expression written in the context of the current frame, executes the result - either by running an interpreter on the result of the compilation if that is possible, or by JITing the result of the compilation, inserting it into the target program, and running it there. Then it prints the result of the evaluation.

p命令(又名expr --)取它被给定的参数,编译他们就好像它们是写在当前帧的上下文中,源代码表达式,执行的结果-或者通过其上运行的编译的结果的解释,如果这是可能的,或者通过 JITing 编译结果,将其插入到目标程序中,并在那里运行它。然后打印评估结果。

The pocommand (a.k.a. expr --O --) does everything that pdoes, but instead of printing the result, if the result is a pointer to an ObjC object, it calls that object's "description" method, and prints the string returned by that method(*). Similarly, if the result is a CF object, it will call CFShow and print the result of that. If both these attempts fail, it will go ahead and print the result as pwould have.

po命令(又名expr --O --)的任何事p做,但而不是打印结果,如果结果是一个指向ObjC对象,它会调用该对象的“描述”的方法,并打印通过该方法(*)返回的字符串。同样,如果结果是一个 CF 对象,它将调用 CFShow 并打印结果。如果这两种尝试都失败了,它将继续打印结果p

So pois mostly like p. But you can get some weird results if you use poon things that it aren't actually objects. For instance, ObjC has a optimization (tagged pointers) that represent the contents of some objects (e.g. NSNumbers) in the object pointer. There isn't a "real" object, just a cooked pointer. So if you try to poan integer that just happens to look like a tagged pointer, you'll get the description of some probably unrelated ObjC object, not the value of the integer.

所以po主要是像p. 但是如果你使用po它实际上不是对象的东西,你会得到一些奇怪的结果。例如,ObjC 有一个优化(标记指针),它表示对象指针中某些对象(例如 NSNumbers)的内容。没有“真正的”对象,只有一个熟指针。因此,如果您尝试使用po一个恰好看起来像标记指针的整数,您将获得一些可能不相关的 ObjC 对象的描述,而不是整数的值。

And of course, pois doing a lot more work, so unless you really want some object's description of itself, pis more efficient.

而且当然po是做更多的工作,所以除非你真的想要某个对象的自身描述,否则p效率更高。

  • Actually, it calls debugDescription, if that exists, and falls back to description if it doesn't...
  • 实际上,它会调用 debugDescription(如果存在),如果不存在则回退到 description...

回答by Thafer Shahin

p= print

p= 打印

po= print object

po= 打印对象

pprints value of primitive variable or value of a reference

p打印原始变量的值或引用的值

potry to call -description for that object and print returned string

po尝试为该对象调用 -description 并打印返回的字符串

回答by Gihan

  • Use po, p, & v to print variables?
    • po ends up compiling & executing twice, once to evaluate your expression and again to get the object description.?
    • v doesn't compile at all: can't evaluate any expression but does allow property accessing and does recursive dynamic type resolution so each property is treated as the actual runtime type.?
  • 使用 po、p 和 v 打印变量?
    • po 最终编译和执行两次,一次是为了评估你的表达式,另一次是为了获得对象描述。?
    • v 根本不编译:无法计算任何表达式,但允许属性访问,并进行递归动态类型解析,因此每个属性都被视为实际的运行时类型。?

enter image description here

在此处输入图片说明

For people who are interested in learning more : https://developer.apple.com/videos/play/wwdc2019/429/

有兴趣了解更多的人:https: //developer.apple.com/videos/play/wwdc2019/429/

Add this as an answer for visibility, Also explains the functionality of each debugging commmand

添加此作为可见性的答案,还解释了每个调试命令的功能

回答by auspicious99

potries to treat what you want to print, as an object. In many cases it is similar to p, but there are cases where the difference emerges.

po尝试将您要打印的内容视为对象。在许多情况下,它类似于p,但在某些情况下会出现差异。

The simplest way to see the difference: compare the output of p 0and po 0.

看出区别的最简单方法:比较器的输出p 0po 0

(lldb) p 0
  (int)  = 0
(lldb) po 0
  <nil>

回答by CrazyPro007

p prints value of primitive variable or value of a reference
po try to call -description for that object and print returned string

There is also **v command** in lldb. Explaining using example.

protocol Activity {} 
struct Trip: Activity { 
var name: String 
var destinations: [String] 
} 
let cruise: Activity = Trip(...)

Using v command 
(lldb) v cruise.name
(string) cruise.name = "print name"
//As it uses dynamic resolution

Using p command
(lldb) p cruise.name
//execution interrupted

回答by Amin Negm-Awad

UIInterfaceOrientationis no (Objective-C) object, but an integer (enum:NSInteger). You should not use po(print object) at all.

UIInterfaceOrientation不是 (Objective-C) 对象,而是整数 ( enum:NSInteger)。您根本不应该使用po(print object)。

回答by Vyachaslav Gerchicov

It seems the difference is lldb/gdb debugger. The only way for iOS project to make debugger more workable is expr @import UIKit. But it may not work for older xcode versions and... you need to input this string in the console after each relaunch. The only way to automate it is an extra breakpoint with this expression.

似乎区别在于 lldb/gdb 调试器。iOS 项目使调试器更有效的唯一方法是expr @import UIKit. 但它可能不适用于较旧的 xcode 版本,并且……每次重新启动后,您都需要在控制台中输入此字符串。自动化它的唯一方法是使用此表达式的额外断点。

回答by Trieu Nguyen

This is in reference to what Jim Ingham said. Type "p " instead of "po ". When I do that Xcode displays the correct information. Try that.

这是参考 Jim Ingham 所说的。输入“p”而不是“po”。当我这样做时,Xcode 会显示正确的信息。试试那个。