po in LLDB with swift

14,690

Solution 1

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 filling 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

Solution 2

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

Solution 3

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'

Solution 4

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>
Share:
14,690
János
Author by

János

I'm János, and I love Swift. If you have any freelancer work fits my profile, please reach me on Facebook. price: 15 EUR / 15 min. https://www.facebook.com/kukoda.janos

Updated on June 27, 2022

Comments

  • János
    János about 2 years

    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:

    (lldb) po a
    error: <EXPR>:11:5: error: use of unresolved identifier '$__lldb_injected_self'
        $__lldb_injected_self.$__lldb_wrapped_expr_2(     
        ^
    
  • János
    János almost 10 years
    It is freaking annoying that final Xcode 6 is released and they couldn't implement basic feature to watch any variable's value during debugging.
  • Chris Conover
    Chris Conover over 9 years
    @enrico: What is the current state of the art for getting Swift symbols - is po expected to work at all on pure swift objects? Is there a different means of getting data, and are there special build settings that are needed for this? Thanks!
  • poGUIst
    poGUIst about 8 years
    "frame variable" doesn't get same result as print description of object. It prints almost useless trash.
  • Enrico Granata
    Enrico Granata about 8 years
    @poGUIst that's unfortunate - can you please give me more details about this? what data types are involved? what does 'po' give you that frame variable does not?
  • poGUIst
    poGUIst about 8 years
    frame variable displays memory addresses of member references, while 'po' executes 'description' method of NSObject descendant which usually much more informative. Actually i'll try to directly call 'description' method. May be it helps. 8)