LLDB po command gives wrong value

I observed a weird issue while debugging my SwiftUI app. The context is SwiftUI, but the issue is related to the language (or more accurately lldb).

The symptom is that lldb po command shows wrong value of a variable. The variable is of Date type. At first po shows its correct value, but then all of sudden it gives a wrong value of Jan 1, 2001. The code where the issue occurs is just regular code.

I'm quite sure it's an lldb issue, because a) if I print the value, the correct value is shown, and b) if I use the value in if statement, the check result is based on the correct value.

But I wasn't aware of the issue at first, so I was very confused and spent quite a while to figure out what went wrong. I knew po might not work in some cases, but showing an incorrect value is much worse than that, I think. The issue can be reproduced consistently in my app, though it's unlikely to demonstrate it with a simple example. I wonder if anyone observed this issue before or have pointers to help better understand it? Thanks.

I filed a bug report of debugger printing wrong things years ago. Unfortunately I don't know enough about lldb to fix it myself.

Simple example of debugger printing a wrong value:

protocol Foo { }
extension Foo {
    func test() -> String { "A" }
struct S: Foo {
    func test() -> String { "B" }
func x<T: Foo>(_ x: T) {
    print(x.test()) // try doing `po x.test()` here
1 Like

Thanks. So I find a simpler one also (sorry I didn't try it at first because I can't image this doesn't work).

enum Foo {
    case b(date: Date)
    func test() {
        switch self {
        case .b(let date):
            print("date: \(date)") // Try doing "po date" and "po self". 
                                   // The former doesn't work.

var x = Foo.b(date: Date())

Since this is a different issue than your example, I just filed a bug in apple/llvm-project repo.

1 Like

It's mostly Foundation types that don't work correctly (I don't know why). There has been some improvement in this functionality over the past couple years. Also Optionals don't always display or print correctly. You can also use the p or v commands to print the variables and sometimes they work when po doesn't.

1 Like