【问题标题】:Change an Int variable value in LLDB更改 LLDB 中的 Int 变量值
【发布时间】:2020-06-25 05:50:46
【问题描述】:

环境:Xcode 11.3.1/Swift 5

函数如下:

func lldbTest() {
    var switchInt = 1

    ...// do something and set a break point here

    if switchInt == 1 {
        print("switchInt == 1")
    } else if switchInt == 2 {
        print("switchInt == 2")
    }
}

在进入if语句前调试,在lldb中将switchInt改为2

e switchInt = 2
p switchInt
(Int) $R4 = 2

但它仍然打印“switchInt == 1” result

【问题讨论】:

    标签: swift stack lldb xcode11


    【解决方案1】:

    我猜这种行为是因为编译器已经评估了 if 语句“if switchInt == 1”,因为在该行之前没有更改 switchInt 值的代码。我尝试了以下并能够获得所需的行为。

    var switchInt = 1
    
    for i in 0..<10 {
        switchInt = 0
    }
    
    if switchInt == 1 {  -> Put a break point here and use (lldb) e switchInt=2
        print("switchInt == 1")
    } else if switchInt == 2 {
        print("switchInt == 2")
    }
    

    现在执行命令 p switchInt,它的值是 2。单步执行断点,它会打印 switchInt == 2。

    【讨论】:

      【解决方案2】:

      在 Swift 中从调试器设置变量有些偶然。因为 swift 使用了很多包装对象(例如 Int 实际上是一个“结构”),所以即使在 -Onone 下编译器也必须进行大量优化,否则代码运行速度会慢得令人无法接受。

      调试器通常只被告知变量的影子副本,而不是代码中实际使用的位置。您可以尝试 Felix 建议的各种技巧,但目前不能保证您一定会成功...

      这是一个已知的错误,但由于技术原因,它很难解决。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-04-12
        • 2018-08-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-06-28
        • 1970-01-01
        相关资源
        最近更新 更多