RY 's Blog

Beyond "po"

2020-06-07

https://developer.apple.com/videos/play/wwdc2019/429

po command

We usually use po to print object description, which does the same job as expression --object-description.

Alias the command

We can also use command alias to custom it. like

command alias my_po expression --object-description img

Custom the output

We can add debugDescription to custom the output when using po

image-20200607171902758

Others jobs po can do

image-20200607172607792

What LLDB does behind the Po command

img

p command

1
p is a alias of `expression

-w398

The result of LLDB is given a increasing name, such as $R1 and $R2

-w509

Dynamic type resolution

img

In Swift, the static representation of a type in the source code and the dynamic type at the runtime, aren’t necessarily the same. For example, a variable might be declared using a protocol of this type. In this example, the static type of cruise is Activity. But at runtime, the variable will have an instance of type Trip which is the dynamic time. If we print the value of cruise, we get back an object of type Trip because LLDB retell results metadata to display the most accurate type for a given variable at a given program point. This is what we call dynamic type resolution.

With the p-command, dynamic type resolution is only performed on the result of the expression.

This happens because if you remember, LLDB compiles code where running p and the only type it sees is the one in your source code, the static one. It’s the same thing as typing the expression cruise.name in your source code. The static compiler will reject it with an error. -w1104

If you want to evaluate the expression without errors, you need to first cast the object explicitly to its dynamic type and then access the field on the result. This is true both for the debugger and your source code.

img

“p” Under the Hood

img

Formatter

After it performed dynamic type resolution on the result, LLDB passes the resulting object to the formatter subsystem which is the part of LLDB responsible for printing a human readable description of objects.

-w850

v Command

  • The output of v is exactly the same as p as it also relies on the formatter we just described.
  • v is just an alias we introduced in Xcode 10.2 for the frame variable command
  • The v-command doesn’t compile and execute code at all which makes it very fast.

img

v under the hood

img

frame command

-w799

po vs p vs v

-w1306

scan qr code and share this article