po in LLDB with swift

前提是你 提交于 2019-11-30 06:19: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:

(lldb) po a
error: <EXPR>:11:5: error: use of unresolved identifier '$__lldb_injected_self'
    $__lldb_injected_self.$__lldb_wrapped_expr_2(     
    ^

回答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 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




回答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




回答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'




回答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>


来源:https://stackoverflow.com/questions/24785567/po-in-lldb-with-swift

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!