po in LLDB with swift

后端 未结 4 1480
无人及你
无人及你 2020-12-13 04:26

How can I plot out variable\'s value in a Swift App with LLDB?

Earlier it was like po variable_name

Now I usually get some nasty error, like:

相关标签:
4条回答
  • 2020-12-13 04:31

    Great answers in this page about swift defaulting to return the description when running lldb) po

    If it helps, when hitting errors with lldb and Swift objects I alway tried to be in a good place.

    First, tell lldb you are in a Swift context (not Objective-C):

    (lldb) settings set target.language swift

    Then I would always double-check I had imported the Framework..

    lldb) exp import WebKit
    (lldb) expr let $ds = unsafeBitCast(0x6000006d74b0, to: WKWebsiteDataStore.self)
    (lldb) po $ds
    <WKWebsiteDataStore: 0x6000006d74b0>
    
    0 讨论(0)
  • 2020-12-13 04:34

    I can confirm the same error, for Xcode beta4, and frame variable -L self

    displays something, but seems worst:

    : (SwiftCollectionViewSample.DetailViewController) self =

    I will definitively filed a bug, Enrico

    17819707 debugger prints error: use of unresolved identifier '$__lldb_injected_self'

    0 讨论(0)
  • 2020-12-13 04:44

    I have made a few tests to figure out how it works with Swift, results surprised me a bit. With ObjC objects po calls debugDescription which by default calls description. That is clear. Unfortunately the same doesn't apply while working with Swift classes. I focused on objects rather than on printing single variables.

    To make it working (po command in lldb) I had to override description. Below code I used for testing:

    class Test : NSObject
    {
        var name : String?
        var surname : String?
    
        override var debugDescription : String{
            return "debugDescription method"
        }
    
        override var description : String {
            return "description Method"
        }
    }
    

    Testing:

    let test = Test()
    test.name = "name"
    test.surname = "surname"
    
    (lldb) po test
    description Method
    
    (lldb) p test
    (DebugTest.Test) $R1 = 0x00007fce11404860 {
      ObjectiveC.NSObject = {
        isa = DebugTest.Test
      }
      name = "name"
      surname = "surname"
    }
    
    (lldb) po dump(test)
    ▿ DebugTest.Test #0
      - super: debugDescription method
      ▿ name: name
        - Some: name
      ▿ surname: surname
        - Some: surname
    description Method
    
    (lldb) po print(test)
    description Method
    

    The thing that surprised me is that po on Swift objects calls description rather than debugDescription that differs from ObjC.

    EDIT

    To make it acting like with ObjC, Your class have to implement CustomDebugStringConvertible and then po will call debugDescription, which by default calls description. The only thing which have to be changed in my example would be adding:

    class Test : NSObject, CustomDebugStringConvertible
    

    Reference

    Checked with XCode 7.0.1 and iOS SDK 9, Swift 2.0

    0 讨论(0)
  • 2020-12-13 04:54

    That error sounds like it might be because DWARF is not telling LLDB where to find your "self" object. Given the nature of Swift, LLDB needs to know the type of self in order to be able to inject an expression inside your local scope One way to find out if that is your problem is to do at the LLDB prompt:

    (lldb) frame variable -L self

    You are probably going to not see a location for it. Worth filing a bug report for, just to track your specific repro case

    Anyway, to get to the bulk of your question. In Swift, there is no language-sanctioned mechanism for "print description" like for ObjC, so while you can type "po self", unless self is an Objective-C type, you will pretty much see the same thing that "p self" or even "frame variable self" would tell you - which is entirely based on the LLDB data formatters mechanism. If you want to hook into that to customize the way your Swift objects look, the obligatory reference is: http://lldb.llvm.org/varformats.html

    0 讨论(0)
提交回复
热议问题